4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
15 * youngest of the Old Took's daughters); and Mr. Drogo was his second
16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
17 * either way, as the saying is, if you follow me.' --the Gaffer
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains the functions that create and manipulate the OP
23 * structures that hold a compiled perl program.
25 * Note that during the build of miniperl, a temporary copy of this file
26 * is made, called opmini.c.
28 * A Perl program is compiled into a tree of OP nodes. Each op contains:
29 * * structural OP pointers to its children and siblings (op_sibling,
30 * op_first etc) that define the tree structure;
31 * * execution order OP pointers (op_next, plus sometimes op_other,
32 * op_lastop etc) that define the execution sequence plus variants;
33 * * a pointer to the C "pp" function that would execute the op;
34 * * any data specific to that op.
35 * For example, an OP_CONST op points to the pp_const() function and to an
36 * SV containing the constant value. When pp_const() is executed, its job
37 * is to push that SV onto the stack.
39 * OPs are mainly created by the newFOO() functions, which are mainly
40 * called from the parser (in perly.y) as the code is parsed. For example
41 * the Perl code $a + $b * $c would cause the equivalent of the following
42 * to be called (oversimplifying a bit):
44 * newBINOP(OP_ADD, flags,
46 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
49 * As the parser reduces low-level rules, it creates little op subtrees;
50 * as higher-level rules are resolved, these subtrees get joined together
51 * as branches on a bigger subtree, until eventually a top-level rule like
52 * a subroutine definition is reduced, at which point there is one large
55 * The execution order pointers (op_next) are generated as the subtrees
56 * are joined together. Consider this sub-expression: A*B + C/D: at the
57 * point when it's just been parsed, the op tree looks like:
65 * with the intended execution order being:
67 * [PREV] => A => B => [*] => C => D => [/] => [+] => [NEXT]
69 * At this point all the nodes' op_next pointers will have been set,
71 * * we don't know what the [NEXT] node will be yet;
72 * * we don't know what the [PREV] node will be yet, but when it gets
73 * created and needs its op_next set, it needs to be set to point to
74 * A, which is non-obvious.
75 * To handle both those cases, we temporarily set the top node's
76 * op_next to point to the first node to be executed in this subtree (A in
77 * this case). This means that initially a subtree's op_next chain,
78 * starting from the top node, will visit each node in execution sequence
79 * then point back at the top node.
80 * When we embed this subtree in a larger tree, its top op_next is used
81 * to get the start node, then is set to point to its new neighbour.
82 * For example the two separate [*],A,B and [/],C,D subtrees would
84 * [*] => A; A => B; B => [*]
86 * [/] => C; C => D; D => [/]
87 * When these two subtrees were joined together to make the [+] subtree,
88 * [+]'s op_next was set to [*]'s op_next, i.e. A; then [*]'s op_next was
89 * set to point to [/]'s op_next, i.e. C.
91 * This op_next linking is done by the LINKLIST() macro and its underlying
92 * op_linklist() function. Given a top-level op, if its op_next is
93 * non-null, it's already been linked, so leave it. Otherwise link it with
94 * its children as described above, possibly recursively if any of the
95 * children have a null op_next.
97 * In summary: given a subtree, its top-level node's op_next will either
99 * NULL: the subtree hasn't been LINKLIST()ed yet;
100 * fake: points to the start op for this subtree;
101 * real: once the subtree has been embedded into a larger tree
106 Here's an older description from Larry.
108 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
112 An execution-order pass
114 The bottom-up pass is represented by all the "newOP" routines and
115 the ck_ routines. The bottom-upness is actually driven by yacc.
116 So at the point that a ck_ routine fires, we have no idea what the
117 context is, either upward in the syntax tree, or either forward or
118 backward in the execution order. (The bottom-up parser builds that
119 part of the execution order it knows about, but if you follow the "next"
120 links around, you'll find it's actually a closed loop through the
123 Whenever the bottom-up parser gets to a node that supplies context to
124 its components, it invokes that portion of the top-down pass that applies
125 to that part of the subtree (and marks the top node as processed, so
126 if a node further up supplies context, it doesn't have to take the
127 plunge again). As a particular subcase of this, as the new node is
128 built, it takes all the closed execution loops of its subcomponents
129 and links them into a new closed loop for the higher level node. But
130 it's still not the real execution order.
132 The actual execution order is not known till we get a grammar reduction
133 to a top-level unit like a subroutine or file that will be called by
134 "name" rather than via a "next" pointer. At that point, we can call
135 into peep() to do that code's portion of the 3rd pass. It has to be
136 recursive, but it's recursive on basic blocks, not on tree nodes.
139 /* To implement user lexical pragmas, there needs to be a way at run time to
140 get the compile time state of %^H for that block. Storing %^H in every
141 block (or even COP) would be very expensive, so a different approach is
142 taken. The (running) state of %^H is serialised into a tree of HE-like
143 structs. Stores into %^H are chained onto the current leaf as a struct
144 refcounted_he * with the key and the value. Deletes from %^H are saved
145 with a value of PL_sv_placeholder. The state of %^H at any point can be
146 turned back into a regular HV by walking back up the tree from that point's
147 leaf, ignoring any key you've already seen (placeholder or not), storing
148 the rest into the HV structure, then removing the placeholders. Hence
149 memory is only used to store the %^H deltas from the enclosing COP, rather
150 than the entire %^H on each COP.
152 To cause actions on %^H to write out the serialisation records, it has
153 magic type 'H'. This magic (itself) does nothing, but its presence causes
154 the values to gain magic type 'h', which has entries for set and clear.
155 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
156 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
157 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
158 it will be correctly restored when any inner compiling scope is exited.
164 #include "keywords.h"
167 #include "invlist_inline.h"
169 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
170 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
172 static const char array_passed_to_stat[] = "Array passed to stat will be coerced to a scalar";
174 /* remove any leading "empty" ops from the op_next chain whose first
175 * node's address is stored in op_p. Store the updated address of the
176 * first node in op_p.
180 Perl_op_prune_chain_head(OP** op_p)
182 PERL_ARGS_ASSERT_OP_PRUNE_CHAIN_HEAD;
185 && ( (*op_p)->op_type == OP_NULL
186 || (*op_p)->op_type == OP_SCOPE
187 || (*op_p)->op_type == OP_SCALAR
188 || (*op_p)->op_type == OP_LINESEQ)
190 *op_p = (*op_p)->op_next;
194 /* See the explanatory comments above struct opslab in op.h. */
196 #ifdef PERL_DEBUG_READONLY_OPS
197 # define PERL_SLAB_SIZE 128
198 # define PERL_MAX_SLAB_SIZE 4096
199 # include <sys/mman.h>
202 #ifndef PERL_SLAB_SIZE
203 # define PERL_SLAB_SIZE 64
205 #ifndef PERL_MAX_SLAB_SIZE
206 # define PERL_MAX_SLAB_SIZE 2048
209 /* rounds up to nearest pointer */
210 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
213 (assert(((char *)(p) - (char *)(o)) % sizeof(I32**) == 0), \
214 ((size_t)((I32 **)(p) - (I32**)(o))))
216 /* requires double parens and aTHX_ */
217 #define DEBUG_S_warn(args) \
219 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
222 /* opslot_size includes the size of the slot header, and an op can't be smaller than BASEOP */
223 #define OPSLOT_SIZE_BASE (SIZE_TO_PSIZE(sizeof(OPSLOT)))
225 /* the number of bytes to allocate for a slab with sz * sizeof(I32 **) space for op */
226 #define OpSLABSizeBytes(sz) \
227 ((sz) * sizeof(I32 *) + STRUCT_OFFSET(OPSLAB, opslab_slots))
229 /* malloc a new op slab (suitable for attaching to PL_compcv).
230 * sz is in units of pointers from the beginning of opslab_opslots */
233 S_new_slab(pTHX_ OPSLAB *head, size_t sz)
236 size_t sz_bytes = OpSLABSizeBytes(sz);
238 /* opslot_offset is only U16 */
239 assert(sz < U16_MAX);
240 /* room for at least one op */
241 assert(sz >= OPSLOT_SIZE_BASE);
243 #ifdef PERL_DEBUG_READONLY_OPS
244 slab = (OPSLAB *) mmap(0, sz_bytes,
245 PROT_READ|PROT_WRITE,
246 MAP_ANON|MAP_PRIVATE, -1, 0);
247 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
248 (unsigned long) sz, slab));
249 if (slab == MAP_FAILED) {
250 perror("mmap failed");
254 slab = (OPSLAB *)PerlMemShared_malloc(sz_bytes);
255 Zero(slab, sz_bytes, char);
257 slab->opslab_size = (U16)sz;
260 /* The context is unused in non-Windows */
263 slab->opslab_free_space = sz;
264 slab->opslab_head = head ? head : slab;
265 DEBUG_S_warn((aTHX_ "allocated new op slab sz 0x%x, %p, head slab %p",
266 (unsigned int)slab->opslab_size, (void*)slab,
267 (void*)(slab->opslab_head)));
271 #define OPSLOT_SIZE_TO_INDEX(sz) ((sz) - OPSLOT_SIZE_BASE)
273 #define link_freed_op(slab, o) S_link_freed_op(aTHX_ slab, o)
275 S_link_freed_op(pTHX_ OPSLAB *slab, OP *o) {
276 U16 sz = OpSLOT(o)->opslot_size;
277 U16 index = OPSLOT_SIZE_TO_INDEX(sz);
279 assert(sz >= OPSLOT_SIZE_BASE);
280 /* make sure the array is large enough to include ops this large */
281 if (!slab->opslab_freed) {
282 /* we don't have a free list array yet, make a new one */
283 slab->opslab_freed_size = index+1;
284 slab->opslab_freed = (OP**)PerlMemShared_calloc((slab->opslab_freed_size), sizeof(OP*));
286 if (!slab->opslab_freed)
289 else if (index >= slab->opslab_freed_size) {
290 /* It's probably not worth doing exponential expansion here, the number of op sizes
293 /* We already have a list that isn't large enough, expand it */
294 size_t newsize = index+1;
295 OP **p = (OP **)PerlMemShared_realloc(slab->opslab_freed, newsize * sizeof(OP*));
300 Zero(p+slab->opslab_freed_size, newsize - slab->opslab_freed_size, OP *);
302 slab->opslab_freed = p;
303 slab->opslab_freed_size = newsize;
306 o->op_next = slab->opslab_freed[index];
307 slab->opslab_freed[index] = o;
310 /* Returns a sz-sized block of memory (suitable for holding an op) from
311 * a free slot in the chain of op slabs attached to PL_compcv.
312 * Allocates a new slab if necessary.
313 * if PL_compcv isn't compiling, malloc() instead.
317 Perl_Slab_Alloc(pTHX_ size_t sz)
319 OPSLAB *head_slab; /* first slab in the chain */
323 size_t sz_in_p; /* size in pointer units, including the OPSLOT header */
325 /* We only allocate ops from the slab during subroutine compilation.
326 We find the slab via PL_compcv, hence that must be non-NULL. It could
327 also be pointing to a subroutine which is now fully set up (CvROOT()
328 pointing to the top of the optree for that sub), or a subroutine
329 which isn't using the slab allocator. If our sanity checks aren't met,
330 don't use a slab, but allocate the OP directly from the heap. */
331 if (!PL_compcv || CvROOT(PL_compcv)
332 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
334 o = (OP*)PerlMemShared_calloc(1, sz);
338 /* While the subroutine is under construction, the slabs are accessed via
339 CvSTART(), to avoid needing to expand PVCV by one pointer for something
340 unneeded at runtime. Once a subroutine is constructed, the slabs are
341 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
342 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
344 if (!CvSTART(PL_compcv)) {
346 (OP *)(head_slab = S_new_slab(aTHX_ NULL, PERL_SLAB_SIZE));
347 CvSLABBED_on(PL_compcv);
348 head_slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
350 else ++(head_slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
352 sz_in_p = SIZE_TO_PSIZE(sz + OPSLOT_HEADER);
354 /* The head slab for each CV maintains a free list of OPs. In particular, constant folding
355 will free up OPs, so it makes sense to re-use them where possible. A
356 freed up slot is used in preference to a new allocation. */
357 if (head_slab->opslab_freed &&
358 OPSLOT_SIZE_TO_INDEX(sz_in_p) < head_slab->opslab_freed_size) {
361 /* look for a large enough size with any freed ops */
362 for (base_index = OPSLOT_SIZE_TO_INDEX(sz_in_p);
363 base_index < head_slab->opslab_freed_size && !head_slab->opslab_freed[base_index];
367 if (base_index < head_slab->opslab_freed_size) {
368 /* found a freed op */
369 o = head_slab->opslab_freed[base_index];
371 DEBUG_S_warn((aTHX_ "realloced op at %p, slab %p, head slab %p",
372 (void *)o, (void *)OpMySLAB(o), (void *)head_slab));
373 head_slab->opslab_freed[base_index] = o->op_next;
380 #define INIT_OPSLOT(s) \
381 slot->opslot_offset = DIFF(&slab2->opslab_slots, slot) ; \
382 slot->opslot_size = s; \
383 slab2->opslab_free_space -= s; \
384 o = &slot->opslot_op; \
387 /* The partially-filled slab is next in the chain. */
388 slab2 = head_slab->opslab_next ? head_slab->opslab_next : head_slab;
389 if (slab2->opslab_free_space < sz_in_p) {
390 /* Remaining space is too small. */
391 /* If we can fit a BASEOP, add it to the free chain, so as not
393 if (slab2->opslab_free_space >= OPSLOT_SIZE_BASE) {
394 slot = &slab2->opslab_slots;
395 INIT_OPSLOT(slab2->opslab_free_space);
396 o->op_type = OP_FREED;
397 DEBUG_S_warn((aTHX_ "linked unused op space at %p, slab %p, head slab %p",
398 (void *)o, (void *)slab2, (void *)head_slab));
399 link_freed_op(head_slab, o);
402 /* Create a new slab. Make this one twice as big. */
403 slab2 = S_new_slab(aTHX_ head_slab,
404 slab2->opslab_size > PERL_MAX_SLAB_SIZE / 2
406 : slab2->opslab_size * 2);
407 slab2->opslab_next = head_slab->opslab_next;
408 head_slab->opslab_next = slab2;
410 assert(slab2->opslab_size >= sz_in_p);
412 /* Create a new op slot */
413 slot = OpSLOToff(slab2, slab2->opslab_free_space - sz_in_p);
414 assert(slot >= &slab2->opslab_slots);
415 INIT_OPSLOT(sz_in_p);
416 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p, head slab %p",
417 (void*)o, (void*)slab2, (void*)head_slab));
420 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */
421 assert(!o->op_moresib);
422 assert(!o->op_sibparent);
429 #ifdef PERL_DEBUG_READONLY_OPS
431 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
433 PERL_ARGS_ASSERT_SLAB_TO_RO;
435 if (slab->opslab_readonly) return;
436 slab->opslab_readonly = 1;
437 for (; slab; slab = slab->opslab_next) {
438 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
439 (unsigned long) slab->opslab_size, (void *)slab));*/
440 if (mprotect(slab, OpSLABSizeBytes(slab->opslab_size), PROT_READ))
441 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", (void *)slab,
442 (unsigned long)slab->opslab_size, errno);
447 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
451 PERL_ARGS_ASSERT_SLAB_TO_RW;
453 if (!slab->opslab_readonly) return;
455 for (; slab2; slab2 = slab2->opslab_next) {
456 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
457 (unsigned long) size, (void *)slab2));*/
458 if (mprotect((void *)slab2, OpSLABSizeBytes(slab2->opslab_size),
459 PROT_READ|PROT_WRITE)) {
460 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", (void *)slab,
461 (unsigned long)slab2->opslab_size, errno);
464 slab->opslab_readonly = 0;
468 # define Slab_to_rw(op) NOOP
471 /* make freed ops die if they're inadvertently executed */
476 DIE(aTHX_ "panic: freed op 0x%p called\n", PL_op);
481 /* Return the block of memory used by an op to the free list of
482 * the OP slab associated with that op.
486 Perl_Slab_Free(pTHX_ void *op)
488 OP * const o = (OP *)op;
491 PERL_ARGS_ASSERT_SLAB_FREE;
494 o->op_ppaddr = S_pp_freed;
497 if (!o->op_slabbed) {
499 PerlMemShared_free(op);
504 /* If this op is already freed, our refcount will get screwy. */
505 assert(o->op_type != OP_FREED);
506 o->op_type = OP_FREED;
507 link_freed_op(slab, o);
508 DEBUG_S_warn((aTHX_ "freeing op at %p, slab %p, head slab %p",
509 (void*)o, (void *)OpMySLAB(o), (void*)slab));
510 OpslabREFCNT_dec_padok(slab);
514 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
516 const bool havepad = cBOOL(PL_comppad);
517 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
520 PAD_SAVE_SETNULLPAD();
526 /* Free a chain of OP slabs. Should only be called after all ops contained
527 * in it have been freed. At this point, its reference count should be 1,
528 * because OpslabREFCNT_dec() skips doing rc-- when it detects that rc == 1,
529 * and just directly calls opslab_free().
530 * (Note that the reference count which PL_compcv held on the slab should
531 * have been removed once compilation of the sub was complete).
537 Perl_opslab_free(pTHX_ OPSLAB *slab)
540 PERL_ARGS_ASSERT_OPSLAB_FREE;
542 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
543 assert(slab->opslab_refcnt == 1);
544 PerlMemShared_free(slab->opslab_freed);
546 slab2 = slab->opslab_next;
548 slab->opslab_refcnt = ~(size_t)0;
550 #ifdef PERL_DEBUG_READONLY_OPS
551 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
553 if (munmap(slab, OpSLABSizeBytes(slab->opslab_size))) {
554 perror("munmap failed");
558 PerlMemShared_free(slab);
564 /* like opslab_free(), but first calls op_free() on any ops in the slab
565 * not marked as OP_FREED
569 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
573 size_t savestack_count = 0;
575 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
578 OPSLOT *slot = OpSLOToff(slab2, slab2->opslab_free_space);
579 OPSLOT *end = OpSLOToff(slab2, slab2->opslab_size);
581 slot = (OPSLOT*) ((I32**)slot + slot->opslot_size) )
583 if (slot->opslot_op.op_type != OP_FREED
584 && !(slot->opslot_op.op_savefree
590 assert(slot->opslot_op.op_slabbed);
591 op_free(&slot->opslot_op);
592 if (slab->opslab_refcnt == 1) goto free;
595 } while ((slab2 = slab2->opslab_next));
596 /* > 1 because the CV still holds a reference count. */
597 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
599 assert(savestack_count == slab->opslab_refcnt-1);
601 /* Remove the CV’s reference count. */
602 slab->opslab_refcnt--;
609 #ifdef PERL_DEBUG_READONLY_OPS
611 Perl_op_refcnt_inc(pTHX_ OP *o)
614 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
615 if (slab && slab->opslab_readonly) {
628 Perl_op_refcnt_dec(pTHX_ OP *o)
631 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
633 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
635 if (slab && slab->opslab_readonly) {
637 result = --o->op_targ;
640 result = --o->op_targ;
646 * In the following definition, the ", (OP*)0" is just to make the compiler
647 * think the expression is of the right type: croak actually does a Siglongjmp.
649 #define CHECKOP(type,o) \
650 ((PL_op_mask && PL_op_mask[type]) \
651 ? ( op_free((OP*)o), \
652 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
654 : PL_check[type](aTHX_ (OP*)o))
656 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
659 S_no_fh_allowed(pTHX_ OP *o)
661 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
663 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
669 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
671 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
672 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
677 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
679 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
681 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
686 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid)
688 PERL_ARGS_ASSERT_BAD_TYPE_PV;
690 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
691 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0);
695 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t)
697 SV * const namesv = cv_name((CV *)gv, NULL, 0);
698 PERL_ARGS_ASSERT_BAD_TYPE_GV;
700 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %" SVf " must be %s (not %s)",
701 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv));
705 Perl_no_bareword_allowed(pTHX_ OP *o)
707 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
709 qerror(Perl_mess(aTHX_
710 "Bareword \"%" SVf "\" not allowed while \"strict subs\" in use",
712 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
716 Perl_no_bareword_filehandle(pTHX_ const char *fhname) {
717 PERL_ARGS_ASSERT_NO_BAREWORD_FILEHANDLE;
719 if (strNE(fhname, "STDERR")
720 && strNE(fhname, "STDOUT")
721 && strNE(fhname, "STDIN")
722 && strNE(fhname, "_")
723 && strNE(fhname, "ARGV")
724 && strNE(fhname, "ARGVOUT")
725 && strNE(fhname, "DATA")) {
726 qerror(Perl_mess(aTHX_ "Bareword filehandle \"%s\" not allowed under 'no feature \"bareword_filehandles\"'", fhname));
730 /* "register" allocation */
733 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
736 bool is_idfirst, is_default;
737 const bool is_our = (PL_parser->in_my == KEY_our);
739 PERL_ARGS_ASSERT_ALLOCMY;
741 if (flags & ~SVf_UTF8)
742 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
745 is_idfirst = flags & SVf_UTF8
746 ? isIDFIRST_utf8_safe((U8*)name + 1, name + len)
747 : isIDFIRST_A(name[1]);
750 is_default = len == 2 && name[1] == '_';
752 /* complain about "my $<special_var>" etc etc */
753 if (!is_our && (!is_idfirst || is_default)) {
754 const char * const type =
755 PL_parser->in_my == KEY_sigvar ? "subroutine signature" :
756 PL_parser->in_my == KEY_state ? "\"state\"" : "\"my\"";
758 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
760 && (!isPRINT(name[1]) || memCHRs("\t\n\r\f", name[1]))) {
761 /* diag_listed_as: Can't use global %s in %s */
762 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in %s",
763 name[0], toCTRL(name[1]),
764 (int)(len - 2), name + 2,
767 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in %s",
769 type), flags & SVf_UTF8);
773 /* allocate a spare slot and store the name in that slot */
775 off = pad_add_name_pvn(name, len,
776 (is_our ? padadd_OUR :
777 PL_parser->in_my == KEY_state ? padadd_STATE : 0),
778 PL_parser->in_my_stash,
780 /* $_ is always in main::, even with our */
781 ? (PL_curstash && !memEQs(name,len,"$_")
787 /* anon sub prototypes contains state vars should always be cloned,
788 * otherwise the state var would be shared between anon subs */
790 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
791 CvCLONE_on(PL_compcv);
797 =for apidoc_section $optree_manipulation
799 =for apidoc alloccopstash
801 Available only under threaded builds, this function allocates an entry in
802 C<PL_stashpad> for the stash passed to it.
809 Perl_alloccopstash(pTHX_ HV *hv)
811 PADOFFSET off = 0, o = 1;
812 bool found_slot = FALSE;
814 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
816 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
818 for (; o < PL_stashpadmax; ++o) {
819 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
820 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
821 found_slot = TRUE, off = o;
824 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
825 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
826 off = PL_stashpadmax;
827 PL_stashpadmax += 10;
830 PL_stashpad[PL_stashpadix = off] = hv;
835 /* free the body of an op without examining its contents.
836 * Always use this rather than FreeOp directly */
839 S_op_destroy(pTHX_ OP *o)
849 Free an op and its children. Only use this when an op is no longer linked
856 Perl_op_free(pTHX_ OP *o)
861 bool went_up = FALSE; /* whether we reached the current node by
862 following the parent pointer from a child, and
863 so have already seen this node */
865 if (!o || o->op_type == OP_FREED)
868 if (o->op_private & OPpREFCOUNTED) {
869 /* if base of tree is refcounted, just decrement */
870 switch (o->op_type) {
880 refcnt = OpREFCNT_dec(o);
883 /* Need to find and remove any pattern match ops from
884 * the list we maintain for reset(). */
885 find_and_forget_pmops(o);
898 /* free child ops before ourself, (then free ourself "on the
901 if (!went_up && o->op_flags & OPf_KIDS) {
902 next_op = cUNOPo->op_first;
906 /* find the next node to visit, *then* free the current node
907 * (can't rely on o->op_* fields being valid after o has been
910 /* The next node to visit will be either the sibling, or the
911 * parent if no siblings left, or NULL if we've worked our way
912 * back up to the top node in the tree */
913 next_op = (o == top_op) ? NULL : o->op_sibparent;
914 went_up = cBOOL(!OpHAS_SIBLING(o)); /* parents are already visited */
916 /* Now process the current node */
918 /* Though ops may be freed twice, freeing the op after its slab is a
920 assert(!o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
921 /* During the forced freeing of ops after compilation failure, kidops
922 may be freed before their parents. */
923 if (!o || o->op_type == OP_FREED)
928 /* an op should only ever acquire op_private flags that we know about.
929 * If this fails, you may need to fix something in regen/op_private.
930 * Don't bother testing if:
931 * * the op_ppaddr doesn't match the op; someone may have
932 * overridden the op and be doing strange things with it;
933 * * we've errored, as op flags are often left in an
934 * inconsistent state then. Note that an error when
935 * compiling the main program leaves PL_parser NULL, so
936 * we can't spot faults in the main code, only
937 * evaled/required code;
938 * * it's a banned op - we may be croaking before the op is
939 * fully formed. - see CHECKOP. */
941 if ( o->op_ppaddr == PL_ppaddr[type]
943 && !PL_parser->error_count
944 && !(PL_op_mask && PL_op_mask[type])
947 assert(!(o->op_private & ~PL_op_private_valid[type]));
952 /* Call the op_free hook if it has been set. Do it now so that it's called
953 * at the right time for refcounted ops, but still before all of the kids
958 type = (OPCODE)o->op_targ;
961 Slab_to_rw(OpSLAB(o));
963 /* COP* is not cleared by op_clear() so that we may track line
964 * numbers etc even after null() */
965 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
977 /* S_op_clear_gv(): free a GV attached to an OP */
981 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp)
983 void S_op_clear_gv(pTHX_ OP *o, SV**svp)
987 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV
988 || o->op_type == OP_MULTIDEREF)
991 ? ((GV*)PAD_SVl(*ixp)) : NULL;
993 ? (GV*)(*svp) : NULL;
995 /* It's possible during global destruction that the GV is freed
996 before the optree. Whilst the SvREFCNT_inc is happy to bump from
997 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
998 will trigger an assertion failure, because the entry to sv_clear
999 checks that the scalar is not already freed. A check of for
1000 !SvIS_FREED(gv) turns out to be invalid, because during global
1001 destruction the reference count can be forced down to zero
1002 (with SVf_BREAK set). In which case raising to 1 and then
1003 dropping to 0 triggers cleanup before it should happen. I
1004 *think* that this might actually be a general, systematic,
1005 weakness of the whole idea of SVf_BREAK, in that code *is*
1006 allowed to raise and lower references during global destruction,
1007 so any *valid* code that happens to do this during global
1008 destruction might well trigger premature cleanup. */
1009 bool still_valid = gv && SvREFCNT(gv);
1012 SvREFCNT_inc_simple_void(gv);
1015 pad_swipe(*ixp, TRUE);
1023 int try_downgrade = SvREFCNT(gv) == 2;
1024 SvREFCNT_dec_NN(gv);
1026 gv_try_downgrade(gv);
1032 Perl_op_clear(pTHX_ OP *o)
1036 PERL_ARGS_ASSERT_OP_CLEAR;
1038 switch (o->op_type) {
1039 case OP_NULL: /* Was holding old type, if any. */
1042 case OP_ENTEREVAL: /* Was holding hints. */
1043 case OP_ARGDEFELEM: /* Was holding signature index. */
1047 if (!(o->op_flags & OPf_REF) || !OP_IS_STAT(o->op_type))
1054 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix));
1056 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv));
1059 case OP_METHOD_REDIR:
1060 case OP_METHOD_REDIR_SUPER:
1062 if (cMETHOPo->op_rclass_targ) {
1063 pad_swipe(cMETHOPo->op_rclass_targ, 1);
1064 cMETHOPo->op_rclass_targ = 0;
1067 SvREFCNT_dec(cMETHOPo->op_rclass_sv);
1068 cMETHOPo->op_rclass_sv = NULL;
1071 case OP_METHOD_NAMED:
1072 case OP_METHOD_SUPER:
1073 SvREFCNT_dec(cMETHOPo->op_u.op_meth_sv);
1074 cMETHOPo->op_u.op_meth_sv = NULL;
1077 pad_swipe(o->op_targ, 1);
1084 SvREFCNT_dec(cSVOPo->op_sv);
1085 cSVOPo->op_sv = NULL;
1088 Even if op_clear does a pad_free for the target of the op,
1089 pad_free doesn't actually remove the sv that exists in the pad;
1090 instead it lives on. This results in that it could be reused as
1091 a target later on when the pad was reallocated.
1094 pad_swipe(o->op_targ,1);
1104 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
1109 if ( (o->op_type == OP_TRANS || o->op_type == OP_TRANSR)
1110 && (o->op_private & OPpTRANS_USE_SVOP))
1113 if (cPADOPo->op_padix > 0) {
1114 pad_swipe(cPADOPo->op_padix, TRUE);
1115 cPADOPo->op_padix = 0;
1118 SvREFCNT_dec(cSVOPo->op_sv);
1119 cSVOPo->op_sv = NULL;
1123 PerlMemShared_free(cPVOPo->op_pv);
1124 cPVOPo->op_pv = NULL;
1128 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
1132 if ( (o->op_private & OPpSPLIT_ASSIGN) /* @array = split */
1133 && !(o->op_flags & OPf_STACKED)) /* @{expr} = split */
1135 if (o->op_private & OPpSPLIT_LEX)
1136 pad_free(cPMOPo->op_pmreplrootu.op_pmtargetoff);
1139 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
1141 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
1148 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
1149 op_free(cPMOPo->op_code_list);
1150 cPMOPo->op_code_list = NULL;
1151 forget_pmop(cPMOPo);
1152 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
1153 /* we use the same protection as the "SAFE" version of the PM_ macros
1154 * here since sv_clean_all might release some PMOPs
1155 * after PL_regex_padav has been cleared
1156 * and the clearing of PL_regex_padav needs to
1157 * happen before sv_clean_all
1160 if(PL_regex_pad) { /* We could be in destruction */
1161 const IV offset = (cPMOPo)->op_pmoffset;
1162 ReREFCNT_dec(PM_GETRE(cPMOPo));
1163 PL_regex_pad[offset] = &PL_sv_undef;
1164 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
1168 ReREFCNT_dec(PM_GETRE(cPMOPo));
1169 PM_SETRE(cPMOPo, NULL);
1175 PerlMemShared_free(cUNOP_AUXo->op_aux);
1178 case OP_MULTICONCAT:
1180 UNOP_AUX_item *aux = cUNOP_AUXo->op_aux;
1181 /* aux[PERL_MULTICONCAT_IX_PLAIN_PV] and/or
1182 * aux[PERL_MULTICONCAT_IX_UTF8_PV] point to plain and/or
1183 * utf8 shared strings */
1184 char *p1 = aux[PERL_MULTICONCAT_IX_PLAIN_PV].pv;
1185 char *p2 = aux[PERL_MULTICONCAT_IX_UTF8_PV].pv;
1187 PerlMemShared_free(p1);
1189 PerlMemShared_free(p2);
1190 PerlMemShared_free(aux);
1196 UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
1197 UV actions = items->uv;
1199 bool is_hash = FALSE;
1202 switch (actions & MDEREF_ACTION_MASK) {
1205 actions = (++items)->uv;
1208 case MDEREF_HV_padhv_helem:
1211 case MDEREF_AV_padav_aelem:
1212 pad_free((++items)->pad_offset);
1215 case MDEREF_HV_gvhv_helem:
1218 case MDEREF_AV_gvav_aelem:
1220 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1222 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1226 case MDEREF_HV_gvsv_vivify_rv2hv_helem:
1229 case MDEREF_AV_gvsv_vivify_rv2av_aelem:
1231 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1233 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1235 goto do_vivify_rv2xv_elem;
1237 case MDEREF_HV_padsv_vivify_rv2hv_helem:
1240 case MDEREF_AV_padsv_vivify_rv2av_aelem:
1241 pad_free((++items)->pad_offset);
1242 goto do_vivify_rv2xv_elem;
1244 case MDEREF_HV_pop_rv2hv_helem:
1245 case MDEREF_HV_vivify_rv2hv_helem:
1248 do_vivify_rv2xv_elem:
1249 case MDEREF_AV_pop_rv2av_aelem:
1250 case MDEREF_AV_vivify_rv2av_aelem:
1252 switch (actions & MDEREF_INDEX_MASK) {
1253 case MDEREF_INDEX_none:
1256 case MDEREF_INDEX_const:
1260 pad_swipe((++items)->pad_offset, 1);
1262 SvREFCNT_dec((++items)->sv);
1268 case MDEREF_INDEX_padsv:
1269 pad_free((++items)->pad_offset);
1271 case MDEREF_INDEX_gvsv:
1273 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset));
1275 S_op_clear_gv(aTHX_ o, &((++items)->sv));
1280 if (actions & MDEREF_FLAG_last)
1293 actions >>= MDEREF_SHIFT;
1296 /* start of malloc is at op_aux[-1], where the length is
1298 PerlMemShared_free(cUNOP_AUXo->op_aux - 1);
1303 if (o->op_targ > 0) {
1304 pad_free(o->op_targ);
1310 S_cop_free(pTHX_ COP* cop)
1312 PERL_ARGS_ASSERT_COP_FREE;
1314 /* If called during global destruction PL_defstash might be NULL and there
1315 shouldn't be any code running that will trip over the bad cop address.
1316 This also avoids uselessly creating the AV after it's been destroyed.
1318 if (cop->op_type == OP_DBSTATE && PL_phase != PERL_PHASE_DESTRUCT) {
1319 /* Remove the now invalid op from the line number information.
1320 This could cause a freed memory overwrite if the debugger tried to
1321 set a breakpoint on this line.
1323 AV *av = CopFILEAVn(cop);
1325 SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE);
1326 if (svp && *svp != &PL_sv_undef && SvIVX(*svp) == PTR2IV(cop) ) {
1327 (void)SvIOK_off(*svp);
1333 if (! specialWARN(cop->cop_warnings))
1334 PerlMemShared_free(cop->cop_warnings);
1335 cophh_free(CopHINTHASH_get(cop));
1336 if (PL_curcop == cop)
1341 S_forget_pmop(pTHX_ PMOP *const o)
1343 HV * const pmstash = PmopSTASH(o);
1345 PERL_ARGS_ASSERT_FORGET_PMOP;
1347 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1348 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1350 PMOP **const array = (PMOP**) mg->mg_ptr;
1351 U32 count = mg->mg_len / sizeof(PMOP**);
1355 if (array[i] == o) {
1356 /* Found it. Move the entry at the end to overwrite it. */
1357 array[i] = array[--count];
1358 mg->mg_len = count * sizeof(PMOP**);
1359 /* Could realloc smaller at this point always, but probably
1360 not worth it. Probably worth free()ing if we're the
1363 Safefree(mg->mg_ptr);
1377 S_find_and_forget_pmops(pTHX_ OP *o)
1381 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1384 switch (o->op_type) {
1389 forget_pmop(cPMOPo);
1392 if (o->op_flags & OPf_KIDS) {
1393 o = cUNOPo->op_first;
1399 return; /* at top; no parents/siblings to try */
1400 if (OpHAS_SIBLING(o)) {
1401 o = o->op_sibparent; /* process next sibling */
1404 o = o->op_sibparent; /*try parent's next sibling */
1413 Neutralizes an op when it is no longer needed, but is still linked to from
1420 Perl_op_null(pTHX_ OP *o)
1423 PERL_ARGS_ASSERT_OP_NULL;
1425 if (o->op_type == OP_NULL)
1428 o->op_targ = o->op_type;
1429 OpTYPE_set(o, OP_NULL);
1433 =for apidoc op_refcnt_lock
1435 Implements the C<OP_REFCNT_LOCK> macro which you should use instead.
1441 Perl_op_refcnt_lock(pTHX)
1442 PERL_TSA_ACQUIRE(PL_op_mutex)
1444 PERL_UNUSED_CONTEXT;
1449 =for apidoc op_refcnt_unlock
1451 Implements the C<OP_REFCNT_UNLOCK> macro which you should use instead.
1457 Perl_op_refcnt_unlock(pTHX)
1458 PERL_TSA_RELEASE(PL_op_mutex)
1460 PERL_UNUSED_CONTEXT;
1466 =for apidoc op_sibling_splice
1468 A general function for editing the structure of an existing chain of
1469 op_sibling nodes. By analogy with the perl-level C<splice()> function, allows
1470 you to delete zero or more sequential nodes, replacing them with zero or
1471 more different nodes. Performs the necessary op_first/op_last
1472 housekeeping on the parent node and op_sibling manipulation on the
1473 children. The last deleted node will be marked as the last node by
1474 updating the op_sibling/op_sibparent or op_moresib field as appropriate.
1476 Note that op_next is not manipulated, and nodes are not freed; that is the
1477 responsibility of the caller. It also won't create a new list op for an
1478 empty list etc; use higher-level functions like op_append_elem() for that.
1480 C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if
1481 the splicing doesn't affect the first or last op in the chain.
1483 C<start> is the node preceding the first node to be spliced. Node(s)
1484 following it will be deleted, and ops will be inserted after it. If it is
1485 C<NULL>, the first node onwards is deleted, and nodes are inserted at the
1488 C<del_count> is the number of nodes to delete. If zero, no nodes are deleted.
1489 If -1 or greater than or equal to the number of remaining kids, all
1490 remaining kids are deleted.
1492 C<insert> is the first of a chain of nodes to be inserted in place of the nodes.
1493 If C<NULL>, no nodes are inserted.
1495 The head of the chain of deleted ops is returned, or C<NULL> if no ops were
1500 action before after returns
1501 ------ ----- ----- -------
1504 splice(P, A, 2, X-Y-Z) | | B-C
1508 splice(P, NULL, 1, X-Y) | | A
1512 splice(P, NULL, 3, NULL) | | A-B-C
1516 splice(P, B, 0, X-Y) | | NULL
1520 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>,
1521 see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>.
1527 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1531 OP *last_del = NULL;
1532 OP *last_ins = NULL;
1535 first = OpSIBLING(start);
1539 first = cLISTOPx(parent)->op_first;
1541 assert(del_count >= -1);
1543 if (del_count && first) {
1545 while (--del_count && OpHAS_SIBLING(last_del))
1546 last_del = OpSIBLING(last_del);
1547 rest = OpSIBLING(last_del);
1548 OpLASTSIB_set(last_del, NULL);
1555 while (OpHAS_SIBLING(last_ins))
1556 last_ins = OpSIBLING(last_ins);
1557 OpMAYBESIB_set(last_ins, rest, NULL);
1563 OpMAYBESIB_set(start, insert, NULL);
1567 cLISTOPx(parent)->op_first = insert;
1569 parent->op_flags |= OPf_KIDS;
1571 parent->op_flags &= ~OPf_KIDS;
1575 /* update op_last etc */
1582 /* ought to use OP_CLASS(parent) here, but that can't handle
1583 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't
1585 type = parent->op_type;
1586 if (type == OP_CUSTOM) {
1588 type = XopENTRYCUSTOM(parent, xop_class);
1591 if (type == OP_NULL)
1592 type = parent->op_targ;
1593 type = PL_opargs[type] & OA_CLASS_MASK;
1596 lastop = last_ins ? last_ins : start ? start : NULL;
1597 if ( type == OA_BINOP
1598 || type == OA_LISTOP
1602 cLISTOPx(parent)->op_last = lastop;
1605 OpLASTSIB_set(lastop, parent);
1607 return last_del ? first : NULL;
1610 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent");
1614 =for apidoc op_parent
1616 Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise.
1622 Perl_op_parent(OP *o)
1624 PERL_ARGS_ASSERT_OP_PARENT;
1625 while (OpHAS_SIBLING(o))
1627 return o->op_sibparent;
1630 /* replace the sibling following start with a new UNOP, which becomes
1631 * the parent of the original sibling; e.g.
1633 * op_sibling_newUNOP(P, A, unop-args...)
1641 * where U is the new UNOP.
1643 * parent and start args are the same as for op_sibling_splice();
1644 * type and flags args are as newUNOP().
1646 * Returns the new UNOP.
1650 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1654 kid = op_sibling_splice(parent, start, 1, NULL);
1655 newop = newUNOP(type, flags, kid);
1656 op_sibling_splice(parent, start, 0, newop);
1661 /* lowest-level newLOGOP-style function - just allocates and populates
1662 * the struct. Higher-level stuff should be done by S_new_logop() /
1663 * newLOGOP(). This function exists mainly to avoid op_first assignment
1664 * being spread throughout this file.
1668 Perl_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1672 NewOp(1101, logop, 1, LOGOP);
1673 OpTYPE_set(logop, type);
1674 logop->op_first = first;
1675 logop->op_other = other;
1677 logop->op_flags = OPf_KIDS;
1678 while (kid && OpHAS_SIBLING(kid))
1679 kid = OpSIBLING(kid);
1681 OpLASTSIB_set(kid, (OP*)logop);
1686 /* Contextualizers */
1689 =for apidoc op_contextualize
1691 Applies a syntactic context to an op tree representing an expression.
1692 C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_LIST>,
1693 or C<G_VOID> to specify the context to apply. The modified op tree
1700 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1702 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1704 case G_SCALAR: return scalar(o);
1705 case G_LIST: return list(o);
1706 case G_VOID: return scalarvoid(o);
1708 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1715 =for apidoc op_linklist
1716 This function is the implementation of the L</LINKLIST> macro. It should
1717 not be called directly.
1724 Perl_op_linklist(pTHX_ OP *o)
1731 PERL_ARGS_ASSERT_OP_LINKLIST;
1734 /* Descend down the tree looking for any unprocessed subtrees to
1737 if (o->op_flags & OPf_KIDS) {
1738 o = cUNOPo->op_first;
1741 o->op_next = o; /* leaf node; link to self initially */
1744 /* if we're at the top level, there either weren't any children
1745 * to process, or we've worked our way back to the top. */
1749 /* o is now processed. Next, process any sibling subtrees */
1751 if (OpHAS_SIBLING(o)) {
1756 /* Done all the subtrees at this level. Go back up a level and
1757 * link the parent in with all its (processed) children.
1760 o = o->op_sibparent;
1761 assert(!o->op_next);
1762 prevp = &(o->op_next);
1763 kid = (o->op_flags & OPf_KIDS) ? cUNOPo->op_first : NULL;
1765 *prevp = kid->op_next;
1766 prevp = &(kid->op_next);
1767 kid = OpSIBLING(kid);
1775 S_scalarkids(pTHX_ OP *o)
1777 if (o && o->op_flags & OPf_KIDS) {
1779 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
1786 S_scalarboolean(pTHX_ OP *o)
1788 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1790 if ((o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST &&
1791 !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) ||
1792 (o->op_type == OP_NOT && cUNOPo->op_first->op_type == OP_SASSIGN &&
1793 cBINOPx(cUNOPo->op_first)->op_first->op_type == OP_CONST &&
1794 !(cBINOPx(cUNOPo->op_first)->op_first->op_flags & OPf_SPECIAL))) {
1795 if (ckWARN(WARN_SYNTAX)) {
1796 const line_t oldline = CopLINE(PL_curcop);
1798 if (PL_parser && PL_parser->copline != NOLINE) {
1799 /* This ensures that warnings are reported at the first line
1800 of the conditional, not the last. */
1801 CopLINE_set(PL_curcop, PL_parser->copline);
1803 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1804 CopLINE_set(PL_curcop, oldline);
1811 S_op_varname_subscript(pTHX_ const OP *o, int subscript_type)
1814 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1815 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1817 const char funny = o->op_type == OP_PADAV
1818 || o->op_type == OP_RV2AV ? '@' : '%';
1819 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1821 if (cUNOPo->op_first->op_type != OP_GV
1822 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1824 return varname(gv, funny, 0, NULL, 0, subscript_type);
1827 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, subscript_type);
1832 Perl_op_varname(pTHX_ const OP *o)
1834 PERL_ARGS_ASSERT_OP_VARNAME;
1836 return S_op_varname_subscript(aTHX_ o, 1);
1841 Warns that an access of a single element from a named container variable in
1842 scalar context might not be what the programmer wanted. The container
1843 variable's (sigiled, full) name is given by C<name>, and the key to access
1844 it is given by the C<SVOP_sv> of the C<OP_CONST> op given by C<o>.
1845 C<is_hash> selects whether it prints using {KEY} or [KEY] brackets.
1847 C<is_slice> selects between two different messages used in different places.
1850 Perl_warn_elem_scalar_context(pTHX_ const OP *o, SV *name, bool is_hash, bool is_slice)
1852 PERL_ARGS_ASSERT_WARN_ELEM_SCALAR_CONTEXT;
1855 const char *keypv = NULL;
1857 const char lbrack = is_hash ? '{' : '[';
1858 const char rbrack = is_hash ? '}' : ']';
1860 if (o->op_type == OP_CONST) {
1864 keysv = sv_newmortal();
1865 pv_pretty(keysv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1866 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1868 else if (!SvOK(keysv))
1873 assert(SvPOK(name));
1874 sv_chop(name,SvPVX(name)+1);
1880 "Scalar value @%" SVf "%c%s%c better written as $%" SVf "%c%s%c" :
1881 "%%%" SVf "%c%s%c in scalar context better written as $%" SVf "%c%s%c";
1882 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1883 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1884 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), msg,
1885 SVfARG(name), lbrack, keypv, rbrack,
1886 SVfARG(name), lbrack, keypv, rbrack);
1890 "Scalar value @%" SVf "%c%" SVf "%c better written as $%" SVf "%c%" SVf "%c" :
1891 "%%%" SVf "%c%" SVf "%c in scalar context better written as $%" SVf "%c%" SVf "%c";
1892 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1893 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1894 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), msg,
1895 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1896 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1901 /* apply scalar context to the o subtree */
1904 Perl_scalar(pTHX_ OP *o)
1909 OP *next_kid = NULL; /* what op (if any) to process next */
1912 /* assumes no premature commitment */
1913 if (!o || (PL_parser && PL_parser->error_count)
1914 || (o->op_flags & OPf_WANT)
1915 || o->op_type == OP_RETURN)
1920 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1922 switch (o->op_type) {
1924 scalar(cBINOPo->op_first);
1925 /* convert what initially looked like a list repeat into a
1926 * scalar repeat, e.g. $s = (1) x $n
1928 if (o->op_private & OPpREPEAT_DOLIST) {
1929 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1930 assert(kid->op_type == OP_PUSHMARK);
1931 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) {
1932 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1933 o->op_private &=~ OPpREPEAT_DOLIST;
1941 /* impose scalar context on everything except the condition */
1942 next_kid = OpSIBLING(cUNOPo->op_first);
1946 if (o->op_flags & OPf_KIDS)
1947 next_kid = cUNOPo->op_first; /* do all kids */
1950 /* the children of these ops are usually a list of statements,
1951 * except the leaves, whose first child is a corresponding enter
1956 kid = cLISTOPo->op_first;
1960 kid = cLISTOPo->op_first;
1962 kid = OpSIBLING(kid);
1965 OP *sib = OpSIBLING(kid);
1966 /* Apply void context to all kids except the last, which
1967 * is scalar (ignoring a trailing ex-nextstate in determining
1968 * if it's the last kid). E.g.
1969 * $scalar = do { void; void; scalar }
1970 * Except that 'when's are always scalar, e.g.
1971 * $scalar = do { given(..) {
1972 * when (..) { scalar }
1973 * when (..) { scalar }
1978 || ( !OpHAS_SIBLING(sib)
1979 && sib->op_type == OP_NULL
1980 && ( sib->op_targ == OP_NEXTSTATE
1981 || sib->op_targ == OP_DBSTATE )
1985 /* tail call optimise calling scalar() on the last kid */
1989 else if (kid->op_type == OP_LEAVEWHEN)
1995 NOT_REACHED; /* NOTREACHED */
1999 Perl_ck_warner(aTHX_ packWARN(WARN_SCALAR), "Useless use of %s in scalar context", "sort");
2005 /* Warn about scalar context */
2008 /* This warning can be nonsensical when there is a syntax error. */
2009 if (PL_parser && PL_parser->error_count)
2012 if (!ckWARN(WARN_SYNTAX)) break;
2014 kid = cLISTOPo->op_first;
2015 kid = OpSIBLING(kid); /* get past pushmark */
2016 assert(OpSIBLING(kid));
2017 name = op_varname(OpSIBLING(kid));
2018 if (!name) /* XS module fiddling with the op tree */
2020 warn_elem_scalar_context(kid, name, o->op_type == OP_KVHSLICE, false);
2024 /* If next_kid is set, someone in the code above wanted us to process
2025 * that kid and all its remaining siblings. Otherwise, work our way
2026 * back up the tree */
2030 return top_op; /* at top; no parents/siblings to try */
2031 if (OpHAS_SIBLING(o))
2032 next_kid = o->op_sibparent;
2034 o = o->op_sibparent; /*try parent's next sibling */
2035 switch (o->op_type) {
2041 /* should really restore PL_curcop to its old value, but
2042 * setting it to PL_compiling is better than do nothing */
2043 PL_curcop = &PL_compiling;
2052 /* apply void context to the optree arg */
2055 Perl_scalarvoid(pTHX_ OP *arg)
2061 PERL_ARGS_ASSERT_SCALARVOID;
2065 SV *useless_sv = NULL;
2066 const char* useless = NULL;
2067 OP * next_kid = NULL;
2069 if (o->op_type == OP_NEXTSTATE
2070 || o->op_type == OP_DBSTATE
2071 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
2072 || o->op_targ == OP_DBSTATE)))
2073 PL_curcop = (COP*)o; /* for warning below */
2075 /* assumes no premature commitment */
2076 want = o->op_flags & OPf_WANT;
2077 if ((want && want != OPf_WANT_SCALAR)
2078 || (PL_parser && PL_parser->error_count)
2079 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
2084 if ((o->op_private & OPpTARGET_MY)
2085 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2087 /* newASSIGNOP has already applied scalar context, which we
2088 leave, as if this op is inside SASSIGN. */
2092 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
2094 switch (o->op_type) {
2096 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
2100 if (o->op_flags & OPf_STACKED)
2102 if (o->op_type == OP_REPEAT)
2103 scalar(cBINOPo->op_first);
2106 if ((o->op_flags & OPf_STACKED) &&
2107 !(o->op_private & OPpCONCAT_NESTED))
2111 if (o->op_private == 4)
2146 case OP_GETSOCKNAME:
2147 case OP_GETPEERNAME:
2152 case OP_GETPRIORITY:
2177 useless = OP_DESC(o);
2187 case OP_AELEMFAST_LEX:
2191 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
2192 /* Otherwise it's "Useless use of grep iterator" */
2193 useless = OP_DESC(o);
2197 if (!(o->op_private & OPpSPLIT_ASSIGN))
2198 useless = OP_DESC(o);
2202 kid = cUNOPo->op_first;
2203 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
2204 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
2207 useless = "negative pattern binding (!~)";
2211 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
2212 useless = "non-destructive substitution (s///r)";
2216 useless = "non-destructive transliteration (tr///r)";
2223 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
2224 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE))
2225 useless = "a variable";
2230 if (cSVOPo->op_private & OPpCONST_STRICT)
2231 no_bareword_allowed(o);
2233 if (ckWARN(WARN_VOID)) {
2235 /* don't warn on optimised away booleans, eg
2236 * use constant Foo, 5; Foo || print; */
2237 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
2239 /* the constants 0 and 1 are permitted as they are
2240 conventionally used as dummies in constructs like
2241 1 while some_condition_with_side_effects; */
2242 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
2244 else if (SvPOK(sv)) {
2245 SV * const dsv = newSVpvs("");
2247 = Perl_newSVpvf(aTHX_
2249 pv_pretty(dsv, SvPVX_const(sv),
2250 SvCUR(sv), 32, NULL, NULL,
2252 | PERL_PV_ESCAPE_NOCLEAR
2253 | PERL_PV_ESCAPE_UNI_DETECT));
2254 SvREFCNT_dec_NN(dsv);
2256 else if (SvOK(sv)) {
2257 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%" SVf ")", SVfARG(sv));
2260 useless = "a constant (undef)";
2263 op_null(o); /* don't execute or even remember it */
2267 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */
2271 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */
2275 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */
2279 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */
2284 UNOP *refgen, *rv2cv;
2287 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
2290 rv2gv = cBINOPo->op_last;
2291 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
2294 refgen = cUNOPx(cBINOPo->op_first);
2296 if (!refgen || (refgen->op_type != OP_REFGEN
2297 && refgen->op_type != OP_SREFGEN))
2300 exlist = cLISTOPx(refgen->op_first);
2301 if (!exlist || exlist->op_type != OP_NULL
2302 || exlist->op_targ != OP_LIST)
2305 if (exlist->op_first->op_type != OP_PUSHMARK
2306 && exlist->op_first != exlist->op_last)
2309 rv2cv = cUNOPx(exlist->op_last);
2311 if (rv2cv->op_type != OP_RV2CV)
2314 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
2315 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
2316 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
2318 o->op_private |= OPpASSIGN_CV_TO_GV;
2319 rv2gv->op_private |= OPpDONT_INIT_GV;
2320 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
2332 kid = cLOGOPo->op_first;
2333 if (kid->op_type == OP_NOT
2334 && (kid->op_flags & OPf_KIDS)) {
2335 if (o->op_type == OP_AND) {
2336 OpTYPE_set(o, OP_OR);
2338 OpTYPE_set(o, OP_AND);
2348 next_kid = OpSIBLING(cUNOPo->op_first);
2352 if (o->op_flags & OPf_STACKED)
2359 if (!(o->op_flags & OPf_KIDS))
2370 next_kid = cLISTOPo->op_first;
2373 /* If the first kid after pushmark is something that the padrange
2374 optimisation would reject, then null the list and the pushmark.
2376 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
2377 && ( !(kid = OpSIBLING(kid))
2378 || ( kid->op_type != OP_PADSV
2379 && kid->op_type != OP_PADAV
2380 && kid->op_type != OP_PADHV)
2381 || kid->op_private & ~OPpLVAL_INTRO
2382 || !(kid = OpSIBLING(kid))
2383 || ( kid->op_type != OP_PADSV
2384 && kid->op_type != OP_PADAV
2385 && kid->op_type != OP_PADHV)
2386 || kid->op_private & ~OPpLVAL_INTRO)
2388 op_null(cUNOPo->op_first); /* NULL the pushmark */
2389 op_null(o); /* NULL the list */
2401 /* mortalise it, in case warnings are fatal. */
2402 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2403 "Useless use of %" SVf " in void context",
2404 SVfARG(sv_2mortal(useless_sv)));
2407 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
2408 "Useless use of %s in void context",
2413 /* if a kid hasn't been nominated to process, continue with the
2414 * next sibling, or if no siblings left, go back to the parent's
2415 * siblings and so on
2419 return arg; /* at top; no parents/siblings to try */
2420 if (OpHAS_SIBLING(o))
2421 next_kid = o->op_sibparent;
2423 o = o->op_sibparent; /*try parent's next sibling */
2433 S_listkids(pTHX_ OP *o)
2435 if (o && o->op_flags & OPf_KIDS) {
2437 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2444 /* apply list context to the o subtree */
2447 Perl_list(pTHX_ OP *o)
2452 OP *next_kid = NULL; /* what op (if any) to process next */
2456 /* assumes no premature commitment */
2457 if (!o || (o->op_flags & OPf_WANT)
2458 || (PL_parser && PL_parser->error_count)
2459 || o->op_type == OP_RETURN)
2464 if ((o->op_private & OPpTARGET_MY)
2465 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2467 goto do_next; /* As if inside SASSIGN */
2470 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2472 switch (o->op_type) {
2474 if (o->op_private & OPpREPEAT_DOLIST
2475 && !(o->op_flags & OPf_STACKED))
2477 list(cBINOPo->op_first);
2478 kid = cBINOPo->op_last;
2479 /* optimise away (.....) x 1 */
2480 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2481 && SvIVX(kSVOP_sv) == 1)
2483 op_null(o); /* repeat */
2484 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2486 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2494 /* impose list context on everything except the condition */
2495 next_kid = OpSIBLING(cUNOPo->op_first);
2499 if (!(o->op_flags & OPf_KIDS))
2501 /* possibly flatten 1..10 into a constant array */
2502 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2503 list(cBINOPo->op_first);
2504 gen_constant_list(o);
2507 next_kid = cUNOPo->op_first; /* do all kids */
2511 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2512 op_null(cUNOPo->op_first); /* NULL the pushmark */
2513 op_null(o); /* NULL the list */
2515 if (o->op_flags & OPf_KIDS)
2516 next_kid = cUNOPo->op_first; /* do all kids */
2519 /* the children of these ops are usually a list of statements,
2520 * except the leaves, whose first child is a corresponding enter
2524 kid = cLISTOPo->op_first;
2528 kid = cLISTOPo->op_first;
2530 kid = OpSIBLING(kid);
2533 OP *sib = OpSIBLING(kid);
2534 /* Apply void context to all kids except the last, which
2536 * @a = do { void; void; list }
2537 * Except that 'when's are always list context, e.g.
2538 * @a = do { given(..) {
2539 * when (..) { list }
2540 * when (..) { list }
2545 /* tail call optimise calling list() on the last kid */
2549 else if (kid->op_type == OP_LEAVEWHEN)
2555 NOT_REACHED; /* NOTREACHED */
2560 /* If next_kid is set, someone in the code above wanted us to process
2561 * that kid and all its remaining siblings. Otherwise, work our way
2562 * back up the tree */
2566 return top_op; /* at top; no parents/siblings to try */
2567 if (OpHAS_SIBLING(o))
2568 next_kid = o->op_sibparent;
2570 o = o->op_sibparent; /*try parent's next sibling */
2571 switch (o->op_type) {
2577 /* should really restore PL_curcop to its old value, but
2578 * setting it to PL_compiling is better than do nothing */
2579 PL_curcop = &PL_compiling;
2589 /* apply void context to non-final ops of a sequence */
2592 S_voidnonfinal(pTHX_ OP *o)
2595 const OPCODE type = o->op_type;
2597 if (type == OP_LINESEQ || type == OP_SCOPE ||
2598 type == OP_LEAVE || type == OP_LEAVETRY)
2600 OP *kid = cLISTOPo->op_first, *sib;
2601 if(type == OP_LEAVE) {
2602 /* Don't put the OP_ENTER in void context */
2603 assert(kid->op_type == OP_ENTER);
2604 kid = OpSIBLING(kid);
2606 for (; kid; kid = sib) {
2607 if ((sib = OpSIBLING(kid))
2608 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL
2609 || ( sib->op_targ != OP_NEXTSTATE
2610 && sib->op_targ != OP_DBSTATE )))
2615 PL_curcop = &PL_compiling;
2617 o->op_flags &= ~OPf_PARENS;
2618 if (PL_hints & HINT_BLOCK_SCOPE)
2619 o->op_flags |= OPf_PARENS;
2622 o = newOP(OP_STUB, 0);
2627 S_modkids(pTHX_ OP *o, I32 type)
2629 if (o && o->op_flags & OPf_KIDS) {
2631 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
2632 op_lvalue(kid, type);
2638 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid
2639 * const fields. Also, convert CONST keys to HEK-in-SVs.
2640 * rop is the op that retrieves the hash;
2641 * key_op is the first key
2642 * real if false, only check (and possibly croak); don't update op
2646 Perl_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op, int real)
2652 /* find the padsv corresponding to $lex->{} or @{$lex}{} */
2654 if (rop->op_first->op_type == OP_PADSV)
2655 /* @$hash{qw(keys here)} */
2656 rop = cUNOPx(rop->op_first);
2658 /* @{$hash}{qw(keys here)} */
2659 if (rop->op_first->op_type == OP_SCOPE
2660 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2662 rop = cUNOPx(cLISTOPx(rop->op_first)->op_last);
2669 lexname = NULL; /* just to silence compiler warnings */
2670 fields = NULL; /* just to silence compiler warnings */
2674 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ),
2675 PadnameHasTYPE(lexname))
2676 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE))
2677 && isGV(*fields) && GvHV(*fields);
2679 for (; key_op; key_op = cSVOPx(OpSIBLING(key_op))) {
2681 if (key_op->op_type != OP_CONST)
2683 svp = cSVOPx_svp(key_op);
2685 /* make sure it's not a bareword under strict subs */
2686 if (key_op->op_private & OPpCONST_BARE &&
2687 key_op->op_private & OPpCONST_STRICT)
2689 no_bareword_allowed((OP*)key_op);
2692 /* Make the CONST have a shared SV */
2693 if ( !SvIsCOW_shared_hash(sv = *svp)
2694 && SvTYPE(sv) < SVt_PVMG
2700 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2701 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0);
2702 SvREFCNT_dec_NN(sv);
2707 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0))
2709 Perl_croak(aTHX_ "No such class field \"%" SVf "\" "
2710 "in variable %" PNf " of type %" HEKf,
2711 SVfARG(*svp), PNfARG(lexname),
2712 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname))));
2718 /* do all the final processing on an optree (e.g. running the peephole
2719 * optimiser on it), then attach it to cv (if cv is non-null)
2723 S_process_optree(pTHX_ CV *cv, OP *optree, OP* start)
2727 /* XXX for some reason, evals, require and main optrees are
2728 * never attached to their CV; instead they just hang off
2729 * PL_main_root + PL_main_start or PL_eval_root + PL_eval_start
2730 * and get manually freed when appropriate */
2732 startp = &CvSTART(cv);
2734 startp = PL_in_eval? &PL_eval_start : &PL_main_start;
2737 optree->op_private |= OPpREFCOUNTED;
2738 OpREFCNT_set(optree, 1);
2739 optimize_optree(optree);
2741 finalize_optree(optree);
2742 op_prune_chain_head(startp);
2745 /* now that optimizer has done its work, adjust pad values */
2746 pad_tidy(optree->op_type == OP_LEAVEWRITE ? padtidy_FORMAT
2747 : CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
2752 /* Relocate sv to the pad for thread safety.
2753 * Despite being a "constant", the SV is written to,
2754 * for reference counts, sv_upgrade() etc. */
2756 Perl_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
2759 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
2761 ix = pad_alloc(OP_CONST, SVf_READONLY);
2762 SvREFCNT_dec(PAD_SVl(ix));
2763 PAD_SETSV(ix, *svp);
2764 /* XXX I don't know how this isn't readonly already. */
2765 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
2772 S_mark_padname_lvalue(pTHX_ PADNAME *pn)
2775 PadnameLVALUE_on(pn);
2776 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
2778 /* RT #127786: cv can be NULL due to an eval within the DB package
2779 * called from an anon sub - anon subs don't have CvOUTSIDE() set
2780 * unless they contain an eval, but calling eval within DB
2781 * pretends the eval was done in the caller's scope.
2785 assert(CvPADLIST(cv));
2787 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
2788 assert(PadnameLEN(pn));
2789 PadnameLVALUE_on(pn);
2794 S_vivifies(const OPCODE type)
2797 case OP_RV2AV: case OP_ASLICE:
2798 case OP_RV2HV: case OP_KVASLICE:
2799 case OP_RV2SV: case OP_HSLICE:
2800 case OP_AELEMFAST: case OP_KVHSLICE:
2809 /* apply lvalue reference (aliasing) context to the optree o.
2812 * o would be the list ($x,$y) and type would be OP_AASSIGN.
2813 * It may descend and apply this to children too, for example in
2814 * \( $cond ? $x, $y) = (...)
2818 S_lvref(pTHX_ OP *o, I32 type)
2824 switch (o->op_type) {
2826 o = OpSIBLING(cUNOPo->op_first);
2833 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2834 o->op_flags |= OPf_STACKED;
2835 if (o->op_flags & OPf_PARENS) {
2836 if (o->op_private & OPpLVAL_INTRO) {
2837 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2838 "localized parenthesized array in list assignment"));
2842 OpTYPE_set(o, OP_LVAVREF);
2843 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
2844 o->op_flags |= OPf_MOD|OPf_REF;
2847 o->op_private |= OPpLVREF_AV;
2851 kid = cUNOPo->op_first;
2852 if (kid->op_type == OP_NULL)
2853 kid = cUNOPx(OpSIBLING(kUNOP->op_first))
2855 o->op_private = OPpLVREF_CV;
2856 if (kid->op_type == OP_GV)
2857 o->op_flags |= OPf_STACKED;
2858 else if (kid->op_type == OP_PADCV) {
2859 o->op_targ = kid->op_targ;
2861 op_free(cUNOPo->op_first);
2862 cUNOPo->op_first = NULL;
2863 o->op_flags &=~ OPf_KIDS;
2869 if (o->op_flags & OPf_PARENS) {
2871 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2872 "parenthesized hash in list assignment"));
2875 o->op_private |= OPpLVREF_HV;
2879 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2880 o->op_flags |= OPf_STACKED;
2884 if (o->op_flags & OPf_PARENS) goto parenhash;
2885 o->op_private |= OPpLVREF_HV;
2888 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2892 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2893 if (o->op_flags & OPf_PARENS) goto slurpy;
2894 o->op_private |= OPpLVREF_AV;
2899 o->op_private |= OPpLVREF_ELEM;
2900 o->op_flags |= OPf_STACKED;
2905 OpTYPE_set(o, OP_LVREFSLICE);
2906 o->op_private &= OPpLVAL_INTRO;
2910 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2912 else if (!(o->op_flags & OPf_KIDS))
2915 /* the code formerly only recursed into the first child of
2916 * a non ex-list OP_NULL. if we ever encounter such a null op with
2917 * more than one child, need to decide whether its ok to process
2918 * *all* its kids or not */
2919 assert(o->op_targ == OP_LIST
2920 || !(OpHAS_SIBLING(cBINOPo->op_first)));
2923 o = cLISTOPo->op_first;
2927 if (o->op_flags & OPf_PARENS)
2932 /* diag_listed_as: Can't modify reference to %s in %s assignment */
2933 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
2934 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
2941 OpTYPE_set(o, OP_LVREF);
2943 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
2944 if (type == OP_ENTERLOOP)
2945 o->op_private |= OPpLVREF_ITER;
2950 return; /* at top; no parents/siblings to try */
2951 if (OpHAS_SIBLING(o)) {
2952 o = o->op_sibparent;
2955 o = o->op_sibparent; /*try parent's next sibling */
2961 PERL_STATIC_INLINE bool
2962 S_potential_mod_type(I32 type)
2964 /* Types that only potentially result in modification. */
2965 return type == OP_GREPSTART || type == OP_ENTERSUB
2966 || type == OP_REFGEN || type == OP_LEAVESUBLV;
2971 =for apidoc op_lvalue
2973 Propagate lvalue ("modifiable") context to an op and its children.
2974 C<type> represents the context type, roughly based on the type of op that
2975 would do the modifying, although C<local()> is represented by C<OP_NULL>,
2976 because it has no op type of its own (it is signalled by a flag on
2979 This function detects things that can't be modified, such as C<$x+1>, and
2980 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2981 called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>.
2983 It also flags things that need to behave specially in an lvalue context,
2984 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2988 Perl_op_lvalue_flags() is a non-API lower-level interface to
2989 op_lvalue(). The flags param has these bits:
2990 OP_LVALUE_NO_CROAK: return rather than croaking on error
2995 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2999 if (!o || (PL_parser && PL_parser->error_count))
3004 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
3006 OP *next_kid = NULL;
3008 if ((o->op_private & OPpTARGET_MY)
3009 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
3014 /* elements of a list might be in void context because the list is
3015 in scalar context or because they are attribute sub calls */
3016 if ((o->op_flags & OPf_WANT) == OPf_WANT_VOID)
3019 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
3021 switch (o->op_type) {
3023 if (type == OP_SASSIGN)
3029 if ((o->op_flags & OPf_PARENS))
3034 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
3035 !(o->op_flags & OPf_STACKED)) {
3036 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
3037 assert(cUNOPo->op_first->op_type == OP_NULL);
3038 op_null(cLISTOPx(cUNOPo->op_first)->op_first);/* disable pushmark */
3041 else { /* lvalue subroutine call */
3042 o->op_private |= OPpLVAL_INTRO;
3043 PL_modcount = RETURN_UNLIMITED_NUMBER;
3044 if (S_potential_mod_type(type)) {
3045 o->op_private |= OPpENTERSUB_INARGS;
3048 else { /* Compile-time error message: */
3049 OP *kid = cUNOPo->op_first;
3054 if (kid->op_type != OP_PUSHMARK) {
3055 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
3057 "panic: unexpected lvalue entersub "
3058 "args: type/targ %ld:%" UVuf,
3059 (long)kid->op_type, (UV)kid->op_targ);
3060 kid = kLISTOP->op_first;
3062 while (OpHAS_SIBLING(kid))
3063 kid = OpSIBLING(kid);
3064 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
3065 break; /* Postpone until runtime */
3068 kid = kUNOP->op_first;
3069 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
3070 kid = kUNOP->op_first;
3071 if (kid->op_type == OP_NULL)
3073 "panic: unexpected constant lvalue entersub "
3074 "entry via type/targ %ld:%" UVuf,
3075 (long)kid->op_type, (UV)kid->op_targ);
3076 if (kid->op_type != OP_GV) {
3083 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
3084 ? MUTABLE_CV(SvRV(gv))
3090 if (flags & OP_LVALUE_NO_CROAK)
3093 namesv = cv_name(cv, NULL, 0);
3094 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue "
3095 "subroutine call of &%" SVf " in %s",
3096 SVfARG(namesv), PL_op_desc[type]),
3104 if (flags & OP_LVALUE_NO_CROAK) return NULL;
3105 /* grep, foreach, subcalls, refgen */
3106 if (S_potential_mod_type(type))
3108 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
3109 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
3112 type ? PL_op_desc[type] : "local"));
3125 case OP_RIGHT_SHIFT:
3134 if (!(o->op_flags & OPf_STACKED))
3140 if (o->op_flags & OPf_STACKED) {
3144 if (!(o->op_private & OPpREPEAT_DOLIST))
3147 const I32 mods = PL_modcount;
3148 /* we recurse rather than iterate here because we need to
3149 * calculate and use the delta applied to PL_modcount by the
3150 * first child. So in something like
3151 * ($x, ($y) x 3) = split;
3152 * split knows that 4 elements are wanted
3154 modkids(cBINOPo->op_first, type);
3155 if (type != OP_AASSIGN)
3157 kid = cBINOPo->op_last;
3158 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
3159 const IV iv = SvIV(kSVOP_sv);
3160 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
3162 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
3165 PL_modcount = RETURN_UNLIMITED_NUMBER;
3171 next_kid = OpSIBLING(cUNOPo->op_first);
3176 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
3177 PL_modcount = RETURN_UNLIMITED_NUMBER;
3178 /* Treat \(@foo) like ordinary list, but still mark it as modi-
3179 fiable since some contexts need to know. */
3180 o->op_flags |= OPf_MOD;
3185 if (scalar_mod_type(o, type))
3187 ref(cUNOPo->op_first, o->op_type);
3194 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
3195 if (type == OP_LEAVESUBLV && (
3196 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
3197 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
3199 o->op_private |= OPpMAYBE_LVSUB;
3203 PL_modcount = RETURN_UNLIMITED_NUMBER;
3209 if (type == OP_LEAVESUBLV)
3210 o->op_private |= OPpMAYBE_LVSUB;
3214 if (type == OP_LEAVESUBLV
3215 && (o->op_private & OPpAVHVSWITCH_MASK) + OP_EACH == OP_KEYS)
3216 o->op_private |= OPpMAYBE_LVSUB;
3220 PL_hints |= HINT_BLOCK_SCOPE;
3221 if (type == OP_LEAVESUBLV)
3222 o->op_private |= OPpMAYBE_LVSUB;
3227 ref(cUNOPo->op_first, o->op_type);
3231 PL_hints |= HINT_BLOCK_SCOPE;
3241 case OP_AELEMFAST_LEX:
3248 PL_modcount = RETURN_UNLIMITED_NUMBER;
3249 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
3251 /* Treat \(@foo) like ordinary list, but still mark it as modi-
3252 fiable since some contexts need to know. */
3253 o->op_flags |= OPf_MOD;
3256 if (scalar_mod_type(o, type))
3258 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
3259 && type == OP_LEAVESUBLV)
3260 o->op_private |= OPpMAYBE_LVSUB;
3264 if (!type) /* local() */
3265 Perl_croak(aTHX_ "Can't localize lexical variable %" PNf,
3266 PNfARG(PAD_COMPNAME(o->op_targ)));
3267 if (!(o->op_private & OPpLVAL_INTRO)
3268 || ( type != OP_SASSIGN && type != OP_AASSIGN
3269 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
3270 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
3278 if (type != OP_LEAVESUBLV && !scalar_mod_type(NULL, type))
3282 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
3288 if (type == OP_LEAVESUBLV)
3289 o->op_private |= OPpMAYBE_LVSUB;
3290 if (o->op_flags & OPf_KIDS && OpHAS_SIBLING(cBINOPo->op_first)) {
3291 /* we recurse rather than iterate here because the child
3292 * needs to be processed with a different 'type' parameter */
3294 /* substr and vec */
3295 /* If this op is in merely potential (non-fatal) modifiable
3296 context, then apply OP_ENTERSUB context to
3297 the kid op (to avoid croaking). Other-
3298 wise pass this op’s own type so the correct op is mentioned
3299 in error messages. */
3300 op_lvalue(OpSIBLING(cBINOPo->op_first),
3301 S_potential_mod_type(type)
3309 ref(cBINOPo->op_first, o->op_type);
3310 if (type == OP_ENTERSUB &&
3311 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
3312 o->op_private |= OPpLVAL_DEFER;
3313 if (type == OP_LEAVESUBLV)
3314 o->op_private |= OPpMAYBE_LVSUB;
3321 o->op_private |= OPpLVALUE;
3327 if (o->op_flags & OPf_KIDS)
3328 next_kid = cLISTOPo->op_last;
3333 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
3335 else if (!(o->op_flags & OPf_KIDS))
3338 if (o->op_targ != OP_LIST) {
3339 OP *sib = OpSIBLING(cLISTOPo->op_first);
3340 /* OP_TRANS and OP_TRANSR with argument have a weird optree
3347 * compared with things like OP_MATCH which have the argument
3353 * so handle specially to correctly get "Can't modify" croaks etc
3356 if (sib && (sib->op_type == OP_TRANS || sib->op_type == OP_TRANSR))
3358 /* this should trigger a "Can't modify transliteration" err */
3359 op_lvalue(sib, type);
3361 next_kid = cBINOPo->op_first;
3362 /* we assume OP_NULLs which aren't ex-list have no more than 2
3363 * children. If this assumption is wrong, increase the scan
3365 assert( !OpHAS_SIBLING(next_kid)
3366 || !OpHAS_SIBLING(OpSIBLING(next_kid)));
3372 next_kid = cLISTOPo->op_first;
3380 if (type == OP_LEAVESUBLV
3381 || !S_vivifies(cLOGOPo->op_first->op_type))
3382 next_kid = cLOGOPo->op_first;
3383 else if (type == OP_LEAVESUBLV
3384 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type))
3385 next_kid = OpSIBLING(cLOGOPo->op_first);
3389 if (type == OP_NULL) { /* local */
3391 if (!FEATURE_MYREF_IS_ENABLED)
3392 Perl_croak(aTHX_ "The experimental declared_refs "
3393 "feature is not enabled");
3394 Perl_ck_warner_d(aTHX_
3395 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
3396 "Declaring references is experimental");
3397 next_kid = cUNOPo->op_first;
3400 if (type != OP_AASSIGN && type != OP_SASSIGN
3401 && type != OP_ENTERLOOP)
3403 /* Don’t bother applying lvalue context to the ex-list. */
3404 kid = cUNOPx(cUNOPo->op_first)->op_first;
3405 assert (!OpHAS_SIBLING(kid));
3408 if (type == OP_NULL) /* local */
3410 if (type != OP_AASSIGN) goto nomod;
3411 kid = cUNOPo->op_first;
3414 const U8 ec = PL_parser ? PL_parser->error_count : 0;
3415 S_lvref(aTHX_ kid, type);
3416 if (!PL_parser || PL_parser->error_count == ec) {
3417 if (!FEATURE_REFALIASING_IS_ENABLED)
3419 "Experimental aliasing via reference not enabled");
3420 Perl_ck_warner_d(aTHX_
3421 packWARN(WARN_EXPERIMENTAL__REFALIASING),
3422 "Aliasing via reference is experimental");
3425 if (o->op_type == OP_REFGEN)
3426 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
3431 if ((o->op_private & OPpSPLIT_ASSIGN)) {
3432 /* This is actually @array = split. */
3433 PL_modcount = RETURN_UNLIMITED_NUMBER;
3439 op_lvalue(cUNOPo->op_first, OP_ENTERSUB);
3443 /* [20011101.069 (#7861)] File test operators interpret OPf_REF to mean that
3444 their argument is a filehandle; thus \stat(".") should not set
3446 if (type == OP_REFGEN && OP_IS_STAT(o->op_type))
3449 if (type != OP_LEAVESUBLV)
3450 o->op_flags |= OPf_MOD;
3452 if (type == OP_AASSIGN || type == OP_SASSIGN)
3453 o->op_flags |= o->op_type == OP_ENTERSUB ? 0 : OPf_SPECIAL|OPf_REF;
3454 else if (!type) { /* local() */
3457 o->op_private |= OPpLVAL_INTRO;
3458 o->op_flags &= ~OPf_SPECIAL;
3459 PL_hints |= HINT_BLOCK_SCOPE;
3464 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
3465 "Useless localization of %s", OP_DESC(o));
3468 else if (type != OP_GREPSTART && type != OP_ENTERSUB
3469 && type != OP_LEAVESUBLV && o->op_type != OP_ENTERSUB)
3470 o->op_flags |= OPf_REF;
3475 return top_op; /* at top; no parents/siblings to try */
3476 if (OpHAS_SIBLING(o)) {
3477 next_kid = o->op_sibparent;
3478 if (!OpHAS_SIBLING(next_kid)) {
3479 /* a few node types don't recurse into their second child */
3480 OP *parent = next_kid->op_sibparent;
3481 I32 ptype = parent->op_type;
3482 if ( (ptype == OP_NULL && parent->op_targ != OP_LIST)
3483 || ( (ptype == OP_AND || ptype == OP_OR)
3484 && (type != OP_LEAVESUBLV
3485 && S_vivifies(next_kid->op_type))
3488 /*try parent's next sibling */
3495 o = o->op_sibparent; /*try parent's next sibling */
3506 S_scalar_mod_type(const OP *o, I32 type)
3511 if (o && o->op_type == OP_RV2GV)
3535 case OP_RIGHT_SHIFT:
3564 S_is_handle_constructor(const OP *o, I32 numargs)
3566 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
3568 switch (o->op_type) {
3576 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
3589 S_refkids(pTHX_ OP *o, I32 type)
3591 if (o && o->op_flags & OPf_KIDS) {
3593 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
3600 /* Apply reference (autovivification) context to the subtree at o.
3602 * push @{expression}, ....;
3603 * o will be the head of 'expression' and type will be OP_RV2AV.
3604 * It marks the op o (or a suitable child) as autovivifying, e.g. by
3606 * For OP_RV2AV/OP_PADAV and OP_RV2HV/OP_PADHV sets OPf_REF too if
3607 * set_op_ref is true.
3609 * Also calls scalar(o).
3613 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
3617 PERL_ARGS_ASSERT_DOREF;
3619 if (PL_parser && PL_parser->error_count)
3623 switch (o->op_type) {
3625 if ((type == OP_EXISTS || type == OP_DEFINED) &&
3626 !(o->op_flags & OPf_STACKED)) {
3627 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
3628 assert(cUNOPo->op_first->op_type == OP_NULL);
3629 /* disable pushmark */
3630 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
3631 o->op_flags |= OPf_SPECIAL;
3633 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
3634 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3635 : type == OP_RV2HV ? OPpDEREF_HV
3637 o->op_flags |= OPf_MOD;
3643 o = OpSIBLING(cUNOPo->op_first);
3647 if (type == OP_DEFINED)
3648 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3651 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3652 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3653 : type == OP_RV2HV ? OPpDEREF_HV
3655 o->op_flags |= OPf_MOD;
3657 if (o->op_flags & OPf_KIDS) {
3659 o = cUNOPo->op_first;
3667 o->op_flags |= OPf_REF;
3670 if (type == OP_DEFINED)
3671 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3673 o = cUNOPo->op_first;
3679 o->op_flags |= OPf_REF;
3684 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
3686 o = cBINOPo->op_first;
3691 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3692 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3693 : type == OP_RV2HV ? OPpDEREF_HV
3695 o->op_flags |= OPf_MOD;
3698 o = cBINOPo->op_first;
3707 if (!(o->op_flags & OPf_KIDS))
3709 o = cLISTOPo->op_last;
3718 return scalar(top_op); /* at top; no parents/siblings to try */
3719 if (OpHAS_SIBLING(o)) {
3720 o = o->op_sibparent;
3721 /* Normally skip all siblings and go straight to the parent;
3722 * the only op that requires two children to be processed
3723 * is OP_COND_EXPR */
3724 if (!OpHAS_SIBLING(o)
3725 && o->op_sibparent->op_type == OP_COND_EXPR)
3729 o = o->op_sibparent; /*try parent's next sibling */
3736 S_dup_attrlist(pTHX_ OP *o)
3740 PERL_ARGS_ASSERT_DUP_ATTRLIST;
3742 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
3743 * where the first kid is OP_PUSHMARK and the remaining ones
3744 * are OP_CONST. We need to push the OP_CONST values.
3746 if (o->op_type == OP_CONST)
3747 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
3749 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
3751 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) {
3752 if (o->op_type == OP_CONST)
3753 rop = op_append_elem(OP_LIST, rop,
3754 newSVOP(OP_CONST, o->op_flags,
3755 SvREFCNT_inc_NN(cSVOPo->op_sv)));
3762 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
3764 PERL_ARGS_ASSERT_APPLY_ATTRS;
3766 SV * const stashsv = newSVhek(HvNAME_HEK(stash));
3768 /* fake up C<use attributes $pkg,$rv,@attrs> */
3770 #define ATTRSMODULE "attributes"
3771 #define ATTRSMODULE_PM "attributes.pm"
3774 aTHX_ PERL_LOADMOD_IMPORT_OPS,
3775 newSVpvs(ATTRSMODULE),
3777 op_prepend_elem(OP_LIST,
3778 newSVOP(OP_CONST, 0, stashsv),
3779 op_prepend_elem(OP_LIST,
3780 newSVOP(OP_CONST, 0,
3782 dup_attrlist(attrs))));
3787 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
3789 OP *pack, *imop, *arg;
3790 SV *meth, *stashsv, **svp;
3792 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
3797 assert(target->op_type == OP_PADSV ||
3798 target->op_type == OP_PADHV ||
3799 target->op_type == OP_PADAV);
3801 /* Ensure that attributes.pm is loaded. */
3802 /* Don't force the C<use> if we don't need it. */
3803 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
3804 if (svp && *svp != &PL_sv_undef)
3805 NOOP; /* already in %INC */
3807 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
3808 newSVpvs(ATTRSMODULE), NULL);
3810 /* Need package name for method call. */
3811 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
3813 /* Build up the real arg-list. */
3814 stashsv = newSVhek(HvNAME_HEK(stash));
3816 arg = newOP(OP_PADSV, 0);
3817 arg->op_targ = target->op_targ;
3818 arg = op_prepend_elem(OP_LIST,
3819 newSVOP(OP_CONST, 0, stashsv),
3820 op_prepend_elem(OP_LIST,
3821 newUNOP(OP_REFGEN, 0,
3823 dup_attrlist(attrs)));
3825 /* Fake up a method call to import */
3826 meth = newSVpvs_share("import");
3827 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_WANT_VOID,
3828 op_append_elem(OP_LIST,
3829 op_prepend_elem(OP_LIST, pack, arg),
3830 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
3832 /* Combine the ops. */
3833 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
3837 =notfor apidoc apply_attrs_string
3839 Attempts to apply a list of attributes specified by the C<attrstr> and
3840 C<len> arguments to the subroutine identified by the C<cv> argument which
3841 is expected to be associated with the package identified by the C<stashpv>
3842 argument (see L<attributes>). It gets this wrong, though, in that it
3843 does not correctly identify the boundaries of the individual attribute
3844 specifications within C<attrstr>. This is not really intended for the
3845 public API, but has to be listed here for systems such as AIX which
3846 need an explicit export list for symbols. (It's called from XS code
3847 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
3848 to respect attribute syntax properly would be welcome.
3854 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
3855 const char *attrstr, STRLEN len)
3859 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
3862 len = strlen(attrstr);
3866 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
3868 const char * const sstr = attrstr;
3869 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
3870 attrs = op_append_elem(OP_LIST, attrs,
3871 newSVOP(OP_CONST, 0,
3872 newSVpvn(sstr, attrstr-sstr)));
3876 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3877 newSVpvs(ATTRSMODULE),
3878 NULL, op_prepend_elem(OP_LIST,
3879 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
3880 op_prepend_elem(OP_LIST,
3881 newSVOP(OP_CONST, 0,
3882 newRV(MUTABLE_SV(cv))),
3887 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name,
3890 OP *new_proto = NULL;
3895 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
3901 if (o->op_type == OP_CONST) {
3902 pv = SvPV(cSVOPo_sv, pvlen);
3903 if (memBEGINs(pv, pvlen, "prototype(")) {
3904 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3905 SV ** const tmpo = cSVOPx_svp(o);
3906 SvREFCNT_dec(cSVOPo_sv);
3911 } else if (o->op_type == OP_LIST) {
3913 assert(o->op_flags & OPf_KIDS);
3914 lasto = cLISTOPo->op_first;
3915 assert(lasto->op_type == OP_PUSHMARK);
3916 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) {
3917 if (o->op_type == OP_CONST) {
3918 pv = SvPV(cSVOPo_sv, pvlen);
3919 if (memBEGINs(pv, pvlen, "prototype(")) {
3920 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3921 SV ** const tmpo = cSVOPx_svp(o);
3922 SvREFCNT_dec(cSVOPo_sv);
3924 if (new_proto && ckWARN(WARN_MISC)) {
3926 const char * newp = SvPV(cSVOPo_sv, new_len);
3927 Perl_warner(aTHX_ packWARN(WARN_MISC),
3928 "Attribute prototype(%" UTF8f ") discards earlier prototype attribute in same sub",
3929 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
3935 /* excise new_proto from the list */
3936 op_sibling_splice(*attrs, lasto, 1, NULL);
3943 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
3944 would get pulled in with no real need */
3945 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
3954 svname = sv_newmortal();
3955 gv_efullname3(svname, name, NULL);
3957 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
3958 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
3960 svname = (SV *)name;
3961 if (ckWARN(WARN_ILLEGALPROTO))
3962 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE,
3964 if (*proto && ckWARN(WARN_PROTOTYPE)) {
3965 STRLEN old_len, new_len;
3966 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
3967 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
3969 if (curstash && svname == (SV *)name
3970 && !memchr(SvPVX(svname), ':', SvCUR(svname))) {
3971 svname = sv_2mortal(newSVsv(PL_curstname));
3972 sv_catpvs(svname, "::");
3973 sv_catsv(svname, (SV *)name);
3976 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3977 "Prototype '%" UTF8f "' overridden by attribute 'prototype(%" UTF8f ")'"
3979 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
3980 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
3990 S_cant_declare(pTHX_ OP *o)
3992 if (o->op_type == OP_NULL
3993 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
3994 o = cUNOPo->op_first;
3995 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
3996 o->op_type == OP_NULL
3997 && o->op_flags & OPf_SPECIAL
4000 PL_parser->in_my == KEY_our ? "our" :
4001 PL_parser->in_my == KEY_state ? "state" :
4006 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
4009 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
4011 PERL_ARGS_ASSERT_MY_KID;
4013 if (!o || (PL_parser && PL_parser->error_count))
4018 if (OP_TYPE_IS_OR_WAS(o, OP_LIST)) {
4020 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid))
4021 my_kid(kid, attrs, imopsp);
4023 } else if (type == OP_UNDEF || type == OP_STUB) {
4025 } else if (type == OP_RV2SV || /* "our" declaration */
4028 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
4029 S_cant_declare(aTHX_ o);
4031 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
4033 PL_parser->in_my = FALSE;
4034 PL_parser->in_my_stash = NULL;
4035 apply_attrs(GvSTASH(gv),
4036 (type == OP_RV2SV ? GvSVn(gv) :
4037 type == OP_RV2AV ? MUTABLE_SV(GvAVn(gv)) :
4038 type == OP_RV2HV ? MUTABLE_SV(GvHVn(gv)) : MUTABLE_SV(gv)),
4041 o->op_private |= OPpOUR_INTRO;
4044 else if (type == OP_REFGEN || type == OP_SREFGEN) {
4045 if (!FEATURE_MYREF_IS_ENABLED)
4046 Perl_croak(aTHX_ "The experimental declared_refs "
4047 "feature is not enabled");
4048 Perl_ck_warner_d(aTHX_
4049 packWARN(WARN_EXPERIMENTAL__DECLARED_REFS),
4050 "Declaring references is experimental");
4051 /* Kid is a nulled OP_LIST, handled above. */
4052 my_kid(cUNOPo->op_first, attrs, imopsp);
4055 else if (type != OP_PADSV &&
4058 type != OP_PUSHMARK)
4060 S_cant_declare(aTHX_ o);
4063 else if (attrs && type != OP_PUSHMARK) {
4067 PL_parser->in_my = FALSE;
4068 PL_parser->in_my_stash = NULL;
4070 /* check for C<my Dog $spot> when deciding package */
4071 stash = PAD_COMPNAME_TYPE(o->op_targ);
4073 stash = PL_curstash;
4074 apply_attrs_my(stash, o, attrs, imopsp);
4076 o->op_flags |= OPf_MOD;
4077 o->op_private |= OPpLVAL_INTRO;
4079 o->op_private |= OPpPAD_STATE;
4084 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
4087 int maybe_scalar = 0;
4089 PERL_ARGS_ASSERT_MY_ATTRS;
4091 /* [perl #17376]: this appears to be premature, and results in code such as
4092 C< our(%x); > executing in list mode rather than void mode */
4094 if (o->op_flags & OPf_PARENS)
4104 o = my_kid(o, attrs, &rops);
4106 if (maybe_scalar && o->op_type == OP_PADSV) {
4107 o = scalar(op_append_list(OP_LIST, rops, o));
4108 o->op_private |= OPpLVAL_INTRO;
4111 /* The listop in rops might have a pushmark at the beginning,
4112 which will mess up list assignment. */
4113 LISTOP * const lrops = cLISTOPx(rops); /* for brevity */
4114 if (rops->op_type == OP_LIST &&
4115 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
4117 OP * const pushmark = lrops->op_first;
4118 /* excise pushmark */
4119 op_sibling_splice(rops, NULL, 1, NULL);
4122 o = op_append_list(OP_LIST, o, rops);
4125 PL_parser->in_my = FALSE;
4126 PL_parser->in_my_stash = NULL;
4131 Perl_sawparens(pTHX_ OP *o)
4133 PERL_UNUSED_CONTEXT;
4135 o->op_flags |= OPf_PARENS;
4140 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
4144 const OPCODE ltype = left->op_type;
4145 const OPCODE rtype = right->op_type;
4147 PERL_ARGS_ASSERT_BIND_MATCH;
4149 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
4150 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
4152 const char * const desc
4154 rtype == OP_SUBST || rtype == OP_TRANS
4155 || rtype == OP_TRANSR
4157 ? (int)rtype : OP_MATCH];
4158 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
4159 SV * const name = op_varname(left);
4161 Perl_warner(aTHX_ packWARN(WARN_MISC),
4162 "Applying %s to %" SVf " will act on scalar(%" SVf ")",
4163 desc, SVfARG(name), SVfARG(name));
4165 const char * const sample = (isary
4166 ? "@array" : "%hash");
4167 Perl_warner(aTHX_ packWARN(WARN_MISC),
4168 "Applying %s to %s will act on scalar(%s)",
4169 desc, sample, sample);
4173 if (rtype == OP_CONST &&
4174 cSVOPx(right)->op_private & OPpCONST_BARE &&
4175 cSVOPx(right)->op_private & OPpCONST_STRICT)
4177 no_bareword_allowed(right);
4180 /* !~ doesn't make sense with /r, so error on it for now */
4181 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
4183 /* diag_listed_as: Using !~ with %s doesn't make sense */
4184 yyerror("Using !~ with s///r doesn't make sense");
4185 if (rtype == OP_TRANSR && type == OP_NOT)
4186 /* diag_listed_as: Using !~ with %s doesn't make sense */
4187 yyerror("Using !~ with tr///r doesn't make sense");
4189 ismatchop = (rtype == OP_MATCH ||
4190 rtype == OP_SUBST ||
4191 rtype == OP_TRANS || rtype == OP_TRANSR)
4192 && !(right->op_flags & OPf_SPECIAL);
4193 if (ismatchop && right->op_private & OPpTARGET_MY) {
4195 right->op_private &= ~OPpTARGET_MY;
4197 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) {
4198 if (left->op_type == OP_PADSV
4199 && !(left->op_private & OPpLVAL_INTRO))
4201 right->op_targ = left->op_targ;
4206 right->op_flags |= OPf_STACKED;
4207 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
4208 ! (rtype == OP_TRANS &&
4209 right->op_private & OPpTRANS_IDENTICAL) &&
4210 ! (rtype == OP_SUBST &&
4211 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
4212 left = op_lvalue(left, rtype);
4213 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
4214 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
4216 o = op_prepend_elem(rtype, scalar(left), right);
4219 return newUNOP(OP_NOT, 0, scalar(o));
4223 return bind_match(type, left,
4224 pmruntime(newPMOP(OP_MATCH, 0), right, NULL, 0, 0));
4228 Perl_invert(pTHX_ OP *o)
4232 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
4236 Perl_cmpchain_start(pTHX_ I32 type, OP *left, OP *right)
4242 left = newOP(OP_NULL, 0);
4244 right = newOP(OP_NULL, 0);
4247 NewOp(0, bop, 1, BINOP);
4249 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP);
4250 OpTYPE_set(op, type);
4251 cBINOPx(op)->op_flags = OPf_KIDS;
4252 cBINOPx(op)->op_private = 2;
4253 cBINOPx(op)->op_first = left;
4254 cBINOPx(op)->op_last = right;
4255 OpMORESIB_set(left, right);
4256 OpLASTSIB_set(right, op);
4261 Perl_cmpchain_extend(pTHX_ I32 type, OP *ch, OP *right)
4266 PERL_ARGS_ASSERT_CMPCHAIN_EXTEND;
4268 right = newOP(OP_NULL, 0);
4270 NewOp(0, bop, 1, BINOP);
4272 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP);
4273 OpTYPE_set(op, type);
4274 if (ch->op_type != OP_NULL) {
4276 OP *nch, *cleft, *cright;
4277 NewOp(0, lch, 1, UNOP);
4279 OpTYPE_set(nch, OP_NULL);
4280 nch->op_flags = OPf_KIDS;
4281 cleft = cBINOPx(ch)->op_first;
4282 cright = cBINOPx(ch)->op_last;
4283 cBINOPx(ch)->op_first = NULL;
4284 cBINOPx(ch)->op_last = NULL;
4285 cBINOPx(ch)->op_private = 0;
4286 cBINOPx(ch)->op_flags = 0;
4287 cUNOPx(nch)->op_first = cright;
4288 OpMORESIB_set(cright, ch);
4289 OpMORESIB_set(ch, cleft);
4290 OpLASTSIB_set(cleft, nch);
4293 OpMORESIB_set(right, op);
4294 OpMORESIB_set(op, cUNOPx(ch)->op_first);
4295 cUNOPx(ch)->op_first = right;
4300 Perl_cmpchain_finish(pTHX_ OP *ch)
4303 PERL_ARGS_ASSERT_CMPCHAIN_FINISH;
4304 if (ch->op_type != OP_NULL) {
4305 OPCODE cmpoptype = ch->op_type;
4306 ch = CHECKOP(cmpoptype, ch);
4307 if(!ch->op_next && ch->op_type == cmpoptype)
4308 ch = fold_constants(op_integerize(op_std_init(ch)));
4312 OP *rightarg = cUNOPx(ch)->op_first;
4313 cUNOPx(ch)->op_first = OpSIBLING(rightarg);
4314 OpLASTSIB_set(rightarg, NULL);
4316 OP *cmpop = cUNOPx(ch)->op_first;
4317 OP *leftarg = OpSIBLING(cmpop);
4318 OPCODE cmpoptype = cmpop->op_type;
4321 is_last = !(cUNOPx(ch)->op_first = OpSIBLING(leftarg));
4322 OpLASTSIB_set(cmpop, NULL);
4323 OpLASTSIB_set(leftarg, NULL);
4327 nextrightarg = NULL;
4329 nextrightarg = newUNOP(OP_CMPCHAIN_DUP, 0, leftarg);
4330 leftarg = newOP(OP_NULL, 0);
4332 cBINOPx(cmpop)->op_first = leftarg;
4333 cBINOPx(cmpop)->op_last = rightarg;
4334 OpMORESIB_set(leftarg, rightarg);
4335 OpLASTSIB_set(rightarg, cmpop);
4336 cmpop->op_flags = OPf_KIDS;
4337 cmpop->op_private = 2;
4338 cmpop = CHECKOP(cmpoptype, cmpop);
4339 if(!cmpop->op_next && cmpop->op_type == cmpoptype)
4340 cmpop = op_integerize(op_std_init(cmpop));
4341 condop = condop ? newLOGOP(OP_CMPCHAIN_AND, 0, cmpop, condop) :
4345 rightarg = nextrightarg;
4351 =for apidoc op_scope
4353 Wraps up an op tree with some additional ops so that at runtime a dynamic
4354 scope will be created. The original ops run in the new dynamic scope,
4355 and then, provided that they exit normally, the scope will be unwound.
4356 The additional ops used to create and unwind the dynamic scope will
4357 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
4358 instead if the ops are simple enough to not need the full dynamic scope
4365 Perl_op_scope(pTHX_ OP *o)
4368 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
4369 o = op_prepend_elem(OP_LINESEQ,
4370 newOP(OP_ENTER, (o->op_flags & OPf_WANT)), o);
4371 OpTYPE_set(o, OP_LEAVE);
4373 else if (o->op_type == OP_LINESEQ) {
4375 OpTYPE_set(o, OP_SCOPE);
4376 kid = cLISTOPo->op_first;
4377 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
4380 /* The following deals with things like 'do {1 for 1}' */
4381 kid = OpSIBLING(kid);
4383 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
4388 o = newLISTOP(OP_SCOPE, 0, o, NULL);
4394 Perl_op_unscope(pTHX_ OP *o)
4396 if (o && o->op_type == OP_LINESEQ) {
4397 OP *kid = cLISTOPo->op_first;
4398 for(; kid; kid = OpSIBLING(kid))
4399 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
4406 =for apidoc block_start
4408 Handles compile-time scope entry.
4409 Arranges for hints to be restored on block
4410 exit and also handles pad sequence numbers to make lexical variables scope
4411 right. Returns a savestack index for use with C<block_end>.
4417 Perl_block_start(pTHX_ int full)
4419 const int retval = PL_savestack_ix;
4421 PL_compiling.cop_seq = PL_cop_seqmax;
4423 pad_block_start(full);
4425 PL_hints &= ~HINT_BLOCK_SCOPE;
4426 SAVECOMPILEWARNINGS();
4427 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
4428 SAVEI32(PL_compiling.cop_seq);
4429 PL_compiling.cop_seq = 0;
4431 CALL_BLOCK_HOOKS(bhk_start, full);
4437 =for apidoc block_end
4439 Handles compile-time scope exit. C<floor>
4440 is the savestack index returned by
4441 C<block_start>, and C<seq> is the body of the block. Returns the block,
4448 Perl_block_end(pTHX_ I32 floor, OP *seq)
4450 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
4451 OP* retval = voidnonfinal(seq);
4454 /* XXX Is the null PL_parser check necessary here? */
4455 assert(PL_parser); /* Let’s find out under debugging builds. */
4456 if (PL_parser && PL_parser->parsed_sub) {
4457 o = newSTATEOP(0, NULL, NULL);
4459 retval = op_append_elem(OP_LINESEQ, retval, o);
4462 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
4466 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
4470 /* pad_leavemy has created a sequence of introcv ops for all my
4471 subs declared in the block. We have to replicate that list with
4472 clonecv ops, to deal with this situation:
4477 sub s1 { state sub foo { \&s2 } }
4480 Originally, I was going to have introcv clone the CV and turn
4481 off the stale flag. Since &s1 is declared before &s2, the
4482 introcv op for &s1 is executed (on sub entry) before the one for
4483 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
4484 cloned, since it is a state sub) closes over &s2 and expects
4485 to see it in its outer CV’s pad. If the introcv op clones &s1,
4486 then &s2 is still marked stale. Since &s1 is not active, and
4487 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
4488 ble will not stay shared’ warning. Because it is the same stub
4489 that will be used when the introcv op for &s2 is executed, clos-
4490 ing over it is safe. Hence, we have to turn off the stale flag
4491 on all lexical subs in the block before we clone any of them.
4492 Hence, having introcv clone the sub cannot work. So we create a
4493 list of ops like this:
4517 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
4518 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
4519 for (;; kid = OpSIBLING(kid)) {
4520 OP *newkid = newOP(OP_CLONECV, 0);
4521 newkid->op_targ = kid->op_targ;
4522 o = op_append_elem(OP_LINESEQ, o, newkid);
4523 if (kid == last) break;
4525 retval = op_prepend_elem(OP_LINESEQ, o, retval);
4528 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
4534 =for apidoc_section $scope
4536 =for apidoc blockhook_register
4538 Register a set of hooks to be called when the Perl lexical scope changes
4539 at compile time. See L<perlguts/"Compile-time scope hooks">.
4545 Perl_blockhook_register(pTHX_ BHK *hk)
4547 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
4549 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
4553 Perl_newPROG(pTHX_ OP *o)
4557 PERL_ARGS_ASSERT_NEWPROG;
4564 PL_eval_root = newUNOP(OP_LEAVEEVAL,
4565 ((PL_in_eval & EVAL_KEEPERR)
4566 ? OPf_SPECIAL : 0), o);
4569 assert(CxTYPE(cx) == CXt_EVAL);
4571 if ((cx->blk_gimme & G_WANT) == G_VOID)
4572 scalarvoid(PL_eval_root);
4573 else if ((cx->blk_gimme & G_WANT) == G_LIST)
4576 scalar(PL_eval_root);
4578 start = op_linklist(PL_eval_root);
4579 PL_eval_root->op_next = 0;
4580 i = PL_savestack_ix;
4583 S_process_optree(aTHX_ NULL, PL_eval_root, start);
4585 PL_savestack_ix = i;
4588 if (o->op_type == OP_STUB) {
4589 /* This block is entered if nothing is compiled for the main
4590 program. This will be the case for an genuinely empty main
4591 program, or one which only has BEGIN blocks etc, so already
4594 Historically (5.000) the guard above was !o. However, commit
4595 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
4596 c71fccf11fde0068, changed perly.y so that newPROG() is now
4597 called with the output of block_end(), which returns a new
4598 OP_STUB for the case of an empty optree. ByteLoader (and
4599 maybe other things) also take this path, because they set up
4600 PL_main_start and PL_main_root directly, without generating an
4603 If the parsing the main program aborts (due to parse errors,
4604 or due to BEGIN or similar calling exit), then newPROG()
4605 isn't even called, and hence this code path and its cleanups
4606 are skipped. This shouldn't make a make a difference:
4607 * a non-zero return from perl_parse is a failure, and
4608 perl_destruct() should be called immediately.
4609 * however, if exit(0) is called during the parse, then
4610 perl_parse() returns 0, and perl_run() is called. As
4611 PL_main_start will be NULL, perl_run() will return
4612 promptly, and the exit code will remain 0.
4615 PL_comppad_name = 0;
4617 S_op_destroy(aTHX_ o);
4620 PL_main_root = op_scope(sawparens(scalarvoid(o)));
4621 PL_curcop = &PL_compiling;
4622 start = LINKLIST(PL_main_root);
4623 PL_main_root->op_next = 0;
4624 S_process_optree(aTHX_ NULL, PL_main_root, start);
4625 if (!PL_parser->error_count)
4626 /* on error, leave CV slabbed so that ops left lying around
4627 * will eb cleaned up. Else unslab */
4628 cv_forget_slab(PL_compcv);
4631 /* Register with debugger */
4633 CV * const cv = get_cvs("DB::postponed", 0);
4637 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
4639 call_sv(MUTABLE_SV(cv), G_DISCARD);
4646 Perl_localize(pTHX_ OP *o, I32 lex)
4648 PERL_ARGS_ASSERT_LOCALIZE;
4650 if (o->op_flags & OPf_PARENS)
4651 /* [perl #17376]: this appears to be premature, and results in code such as
4652 C< our(%x); > executing in list mode rather than void mode */
4659 if ( PL_parser->bufptr > PL_parser->oldbufptr
4660 && PL_parser->bufptr[-1] == ','
4661 && ckWARN(WARN_PARENTHESIS))
4663 char *s = PL_parser->bufptr;
4666 /* some heuristics to detect a potential error */
4667 while (*s && (memCHRs(", \t\n", *s)))
4671 if (*s && (memCHRs("@$%", *s) || (!lex && *s == '*'))
4673 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
4676 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
4678 while (*s && (memCHRs(", \t\n", *s)))
4684 if (sigil && (*s == ';' || *s == '=')) {
4685 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
4686 "Parentheses missing around \"%s\" list",
4688 ? (PL_parser->in_my == KEY_our
4690 : PL_parser->in_my == KEY_state
4700 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
4701 PL_parser->in_my = FALSE;
4702 PL_parser->in_my_stash = NULL;
4707 Perl_jmaybe(pTHX_ OP *o)
4709 PERL_ARGS_ASSERT_JMAYBE;
4711 if (o->op_type == OP_LIST) {
4712 if (FEATURE_MULTIDIMENSIONAL_IS_ENABLED) {
4714 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
4715 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
4718 /* If the user disables this, then a warning might not be enough to alert
4719 them to a possible change of behaviour here, so throw an exception.
4721 yyerror("Multidimensional hash lookup is disabled");
4727 PERL_STATIC_INLINE OP *
4728 S_op_std_init(pTHX_ OP *o)
4730 I32 type = o->op_type;
4732 PERL_ARGS_ASSERT_OP_STD_INIT;
4734 if (PL_opargs[type] & OA_RETSCALAR)
4736 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
4737 o->op_targ = pad_alloc(type, SVs_PADTMP);
4742 PERL_STATIC_INLINE OP *
4743 S_op_integerize(pTHX_ OP *o)
4745 I32 type = o->op_type;
4747 PERL_ARGS_ASSERT_OP_INTEGERIZE;
4749 /* integerize op. */
4750 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
4752 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
4755 if (type == OP_NEGATE)
4756 /* XXX might want a ck_negate() for this */
4757 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
4762 /* This function exists solely to provide a scope to limit
4763 setjmp/longjmp() messing with auto variables. It cannot be inlined because
4767 S_fold_constants_eval(pTHX) {
4783 S_fold_constants(pTHX_ OP *const o)
4787 I32 type = o->op_type;
4792 SV * const oldwarnhook = PL_warnhook;
4793 SV * const olddiehook = PL_diehook;
4795 U8 oldwarn = PL_dowarn;
4798 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
4800 if (!(PL_opargs[type] & OA_FOLDCONST))
4809 #ifdef USE_LOCALE_CTYPE
4810 if (IN_LC_COMPILETIME(LC_CTYPE))
4819 #ifdef USE_LOCALE_COLLATE
4820 if (IN_LC_COMPILETIME(LC_COLLATE))
4825 /* XXX what about the numeric ops? */
4826 #ifdef USE_LOCALE_NUMERIC
4827 if (IN_LC_COMPILETIME(LC_NUMERIC))
4832 if (!OpHAS_SIBLING(cLISTOPo->op_first)
4833 || OpSIBLING(cLISTOPo->op_first)->op_type != OP_CONST)
4836 SV * const sv = cSVOPx_sv(OpSIBLING(cLISTOPo->op_first));
4837 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
4839 const char *s = SvPVX_const(sv);
4840 while (s < SvEND(sv)) {
4841 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope;
4848 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
4851 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
4852 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
4856 if (PL_parser && PL_parser->error_count)
4857 goto nope; /* Don't try to run w/ errors */
4859 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4860 switch (curop->op_type) {
4862 if ( (curop->op_private & OPpCONST_BARE)
4863 && (curop->op_private & OPpCONST_STRICT)) {
4864 no_bareword_allowed(curop);
4872 /* Foldable; move to next op in list */
4876 /* No other op types are considered foldable */
4881 curop = LINKLIST(o);
4882 old_next = o->op_next;
4886 old_cxix = cxstack_ix;
4887 create_eval_scope(NULL, G_FAKINGEVAL);
4889 /* Verify that we don't need to save it: */
4890 assert(PL_curcop == &PL_compiling);
4891 StructCopy(&PL_compiling, ¬_compiling, COP);
4892 PL_curcop = ¬_compiling;
4893 /* The above ensures that we run with all the correct hints of the
4894 currently compiling COP, but that IN_PERL_RUNTIME is true. */
4895 assert(IN_PERL_RUNTIME);
4896 PL_warnhook = PERL_WARNHOOK_FATAL;
4899 /* Effective $^W=1. */
4900 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
4901 PL_dowarn |= G_WARN_ON;
4903 ret = S_fold_constants_eval(aTHX);
4907 sv = *(PL_stack_sp--);
4908 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
4909 pad_swipe(o->op_targ, FALSE);
4911 else if (SvTEMP(sv)) { /* grab mortal temp? */
4912 SvREFCNT_inc_simple_void(sv);
4915 else { assert(SvIMMORTAL(sv)); }
4918 /* Something tried to die. Abandon constant folding. */
4919 /* Pretend the error never happened. */
4921 o->op_next = old_next;
4924 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
4925 PL_warnhook = oldwarnhook;
4926 PL_diehook = olddiehook;
4927 /* XXX note that this croak may fail as we've already blown away
4928 * the stack - eg any nested evals */
4929 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
4931 PL_dowarn = oldwarn;
4932 PL_warnhook = oldwarnhook;
4933 PL_diehook = olddiehook;
4934 PL_curcop = &PL_compiling;
4936 /* if we croaked, depending on how we croaked the eval scope
4937 * may or may not have already been popped */
4938 if (cxstack_ix > old_cxix) {
4939 assert(cxstack_ix == old_cxix + 1);
4940 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
4941 delete_eval_scope();
4946 /* OP_STRINGIFY and constant folding are used to implement qq.
4947 Here the constant folding is an implementation detail that we
4948 want to hide. If the stringify op is itself already marked
4949 folded, however, then it is actually a folded join. */
4950 is_stringify = type == OP_STRINGIFY && !o->op_folded;
4955 else if (!SvIMMORTAL(sv)) {
4959 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
4960 if (!is_stringify) newop->op_folded = 1;
4967 /* convert a constant range in list context into an OP_RV2AV, OP_CONST pair;
4968 * the constant value being an AV holding the flattened range.
4972 S_gen_constant_list(pTHX_ OP *o)
4974 OP *curop, *old_next;
4975 SV * const oldwarnhook = PL_warnhook;
4976 SV * const olddiehook = PL_diehook;
4978 U8 oldwarn = PL_dowarn;
4988 if (PL_parser && PL_parser->error_count)
4989 return; /* Don't attempt to run with errors */
4991 curop = LINKLIST(o);
4992 old_next = o->op_next;
4994 op_was_null = o->op_type == OP_NULL;
4995 if (op_was_null) /* b3698342565fb462291fba4b432cfcd05b6eb4e1 */
4996 o->op_type = OP_CUSTOM;
4999 o->op_type = OP_NULL;
5000 op_prune_chain_head(&curop);
5003 old_cxix = cxstack_ix;
5004 create_eval_scope(NULL, G_FAKINGEVAL);
5006 old_curcop = PL_curcop;
5007 StructCopy(old_curcop, ¬_compiling, COP);
5008 PL_curcop = ¬_compiling;
5009 /* The above ensures that we run with all the correct hints of the
5010 current COP, but that IN_PERL_RUNTIME is true. */
5011 assert(IN_PERL_RUNTIME);
5012 PL_warnhook = PERL_WARNHOOK_FATAL;
5016 /* Effective $^W=1. */
5017 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
5018 PL_dowarn |= G_WARN_ON;
5022 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
5023 PL_curstackinfo->si_stack_hwm = 0; /* stop valgrind complaining */
5025 Perl_pp_pushmark(aTHX);
5028 assert (!(curop->op_flags & OPf_SPECIAL));
5029 assert(curop->op_type == OP_RANGE);
5030 Perl_pp_anonlist(aTHX);
5034 o->op_next = old_next;
5038 PL_warnhook = oldwarnhook;
5039 PL_diehook = olddiehook;
5040 Perl_croak(aTHX_ "panic: gen_constant_list JMPENV_PUSH returned %d",
5045 PL_dowarn = oldwarn;
5046 PL_warnhook = oldwarnhook;
5047 PL_diehook = olddiehook;
5048 PL_curcop = old_curcop;
5050 if (cxstack_ix > old_cxix) {
5051 assert(cxstack_ix == old_cxix + 1);
5052 assert(CxTYPE(CX_CUR()) == CXt_EVAL);
5053 delete_eval_scope();
5058 OpTYPE_set(o, OP_RV2AV);
5059 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
5060 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
5061 o->op_opt = 0; /* needs to be revisited in rpeep() */
5062 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
5064 /* replace subtree with an OP_CONST */
5065 curop = cUNOPo->op_first;
5066 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
5069 if (AvFILLp(av) != -1)
5070 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
5073 SvREADONLY_on(*svp);
5081 =for apidoc_section $optree_manipulation
5084 /* List constructors */
5087 =for apidoc op_append_elem
5089 Append an item to the list of ops contained directly within a list-type
5090 op, returning the lengthened list. C<first> is the list-type op,
5091 and C<last> is the op to append to the list. C<optype> specifies the
5092 intended opcode for the list. If C<first> is not already a list of the
5093 right type, it will be upgraded into one. If either C<first> or C<last>
5094 is null, the other is returned unchanged.
5100 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
5108 if (first->op_type != (unsigned)type
5109 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
5111 return newLISTOP(type, 0, first, last);
5114 op_sibling_splice(first, cLISTOPx(first)->op_last, 0, last);
5115 first->op_flags |= OPf_KIDS;
5120 =for apidoc op_append_list
5122 Concatenate the lists of ops contained directly within two list-type ops,
5123 returning the combined list. C<first> and C<last> are the list-type ops
5124 to concatenate. C<optype> specifies the intended opcode for the list.
5125 If either C<first> or C<last> is not already a list of the right type,
5126 it will be upgraded into one. If either C<first> or C<last> is null,
5127 the other is returned unchanged.
5133 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
5141 if (first->op_type != (unsigned)type)
5142 return op_prepend_elem(type, first, last);
5144 if (last->op_type != (unsigned)type)
5145 return op_append_elem(type, first, last);
5147 OpMORESIB_set(cLISTOPx(first)->op_last, cLISTOPx(last)->op_first);
5148 cLISTOPx(first)->op_last = cLISTOPx(last)->op_last;
5149 OpLASTSIB_set(cLISTOPx(first)->op_last, first);
5150 first->op_flags |= (last->op_flags & OPf_KIDS);
5152 S_op_destroy(aTHX_ last);
5158 =for apidoc op_prepend_elem
5160 Prepend an item to the list of ops contained directly within a list-type
5161 op, returning the lengthened list. C<first> is the op to prepend to the
5162 list, and C<last> is the list-type op. C<optype> specifies the intended
5163 opcode for the list. If C<last> is not already a list of the right type,
5164 it will be upgraded into one. If either C<first> or C<last> is null,
5165 the other is returned unchanged.
5171 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
5179 if (last->op_type == (unsigned)type) {
5180 if (type == OP_LIST) { /* already a PUSHMARK there */
5181 /* insert 'first' after pushmark */
5182 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first);
5183 if (!(first->op_flags & OPf_PARENS))
5184 last->op_flags &= ~OPf_PARENS;
5187 op_sibling_splice(last, NULL, 0, first);
5188 last->op_flags |= OPf_KIDS;
5192 return newLISTOP(type, 0, first, last);
5196 =for apidoc op_convert_list
5198 Converts C<o> into a list op if it is not one already, and then converts it
5199 into the specified C<type>, calling its check function, allocating a target if
5200 it needs one, and folding constants.
5202 A list-type op is usually constructed one kid at a time via C<newLISTOP>,
5203 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to
5204 C<op_convert_list> to make it the right type.
5210 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o)
5212 if (type < 0) type = -type, flags |= OPf_SPECIAL;
5213 if (!o || o->op_type != OP_LIST)
5214 o = force_list(o, FALSE);
5217 o->op_flags &= ~OPf_WANT;
5218 o->op_private &= ~OPpLVAL_INTRO;
5221 if (!(PL_opargs[type] & OA_MARK))
5222 op_null(cLISTOPo->op_first);
5224 OP * const kid2 = OpSIBLING(cLISTOPo->op_first);
5225 if (kid2 && kid2->op_type == OP_COREARGS) {
5226 op_null(cLISTOPo->op_first);
5227 kid2->op_private |= OPpCOREARGS_PUSHMARK;
5231 if (type != OP_SPLIT)
5232 /* At this point o is a LISTOP, but OP_SPLIT is a PMOP; let
5233 * ck_split() create a real PMOP and leave the op's type as listop
5234 * for now. Otherwise op_free() etc will crash.
5236 OpTYPE_set(o, type);
5238 o->op_flags |= flags;
5239 if (flags & OPf_FOLDED)
5242 o = CHECKOP(type, o);
5243 if (o->op_type != (unsigned)type)
5246 return fold_constants(op_integerize(op_std_init(o)));
5253 =for apidoc_section $optree_construction
5255 =for apidoc newNULLLIST
5257 Constructs, checks, and returns a new C<stub> op, which represents an
5258 empty list expression.
5264 Perl_newNULLLIST(pTHX)
5266 return newOP(OP_STUB, 0);
5269 /* promote o and any siblings to be a list if its not already; i.e.
5277 * pushmark - o - A - B
5279 * If nullit it true, the list op is nulled.
5283 S_force_list(pTHX_ OP *o, bool nullit)
5285 if (!o || o->op_type != OP_LIST) {
5288 /* manually detach any siblings then add them back later */
5289 rest = OpSIBLING(o);
5290 OpLASTSIB_set(o, NULL);
5292 o = newLISTOP(OP_LIST, 0, o, NULL);
5294 op_sibling_splice(o, cLISTOPo->op_last, 0, rest);
5302 =for apidoc newLISTOP
5304 Constructs, checks, and returns an op of any list type. C<type> is
5305 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
5306 C<OPf_KIDS> will be set automatically if required. C<first> and C<last>
5307 supply up to two ops to be direct children of the list op; they are
5308 consumed by this function and become part of the constructed op tree.
5310 For most list operators, the check function expects all the kid ops to be
5311 present already, so calling C<newLISTOP(OP_JOIN, ...)> (e.g.) is not
5312 appropriate. What you want to do in that case is create an op of type
5313 C<OP_LIST>, append more children to it, and then call L</op_convert_list>.
5314 See L</op_convert_list> for more information.
5321 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
5324 /* Note that allocating an OP_PUSHMARK can die under Safe.pm if
5325 * pushmark is banned. So do it now while existing ops are in a
5326 * consistent state, in case they suddenly get freed */
5327 OP* const pushop = type == OP_LIST ? newOP(OP_PUSHMARK, 0) : NULL;
5329 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP
5330 || type == OP_CUSTOM);
5332 NewOp(1101, listop, 1, LISTOP);
5333 OpTYPE_set(listop, type);
5336 listop->op_flags = (U8)flags;
5340 else if (!first && last)
5343 OpMORESIB_set(first, last);
5344 listop->op_first = first;
5345 listop->op_last = last;
5348 OpMORESIB_set(pushop, first);
5349 listop->op_first = pushop;
5350 listop->op_flags |= OPf_KIDS;
5352 listop->op_last = pushop;
5354 if (listop->op_last)
5355 OpLASTSIB_set(listop->op_last, (OP*)listop);
5357 return CHECKOP(type, listop);
5363 Constructs, checks, and returns an op of any base type (any type that
5364 has no extra fields). C<type> is the opcode. C<flags> gives the
5365 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
5372 Perl_newOP(pTHX_ I32 type, I32 flags)
5376 if (type == -OP_ENTEREVAL) {
5377 type = OP_ENTEREVAL;
5378 flags |= OPpEVAL_BYTES<<8;
5381 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
5382 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
5383 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
5384 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
5386 NewOp(1101, o, 1, OP);
5387 OpTYPE_set(o, type);
5388 o->op_flags = (U8)flags;
5391 o->op_private = (U8)(0 | (flags >> 8));
5392 if (PL_opargs[type] & OA_RETSCALAR)
5394 if (PL_opargs[type] & OA_TARGET)
5395 o->op_targ = pad_alloc(type, SVs_PADTMP);
5396 return CHECKOP(type, o);
5402 Constructs, checks, and returns an op of any unary type. C<type> is
5403 the opcode. C<flags> gives the eight bits of C<op_flags>, except that
5404 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
5405 bits, the eight bits of C<op_private>, except that the bit with value 1
5406 is automatically set. C<first> supplies an optional op to be the direct
5407 child of the unary op; it is consumed by this function and become part
5408 of the constructed op tree.
5410 =for apidoc Amnh||OPf_KIDS
5416 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
5420 if (type == -OP_ENTEREVAL) {
5421 type = OP_ENTEREVAL;
5422 flags |= OPpEVAL_BYTES<<8;
5425 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
5426 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
5427 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
5428 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
5429 || type == OP_SASSIGN
5430 || type == OP_ENTERTRY
5431 || type == OP_ENTERTRYCATCH
5432 || type == OP_CUSTOM
5433 || type == OP_NULL );
5436 first = newOP(OP_STUB, 0);
5437 if (PL_opargs[type] & OA_MARK)
5438 first = force_list(first, TRUE);
5440 NewOp(1101, unop, 1, UNOP);
5441 OpTYPE_set(unop, type);
5442 unop->op_first = first;
5443 unop->op_flags = (U8)(flags | OPf_KIDS);
5444 unop->op_private = (U8)(1 | (flags >> 8));
5446 if (!OpHAS_SIBLING(first)) /* true unless weird syntax error */
5447 OpLASTSIB_set(first, (OP*)unop);
5449 unop = (UNOP*) CHECKOP(type, unop);
5453 return fold_constants(op_integerize(op_std_init((OP *) unop)));
5457 =for apidoc newUNOP_AUX
5459 Similar to C<newUNOP>, but creates an C<UNOP_AUX> struct instead, with C<op_aux>
5460 initialised to C<aux>
5466 Perl_newUNOP_AUX(pTHX_ I32 type, I32 flags, OP *first, UNOP_AUX_item *aux)
5470 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP_AUX
5471 || type == OP_CUSTOM);
5473 NewOp(1101, unop, 1, UNOP_AUX);
5474 unop->op_type = (OPCODE)type;
5475 unop->op_ppaddr = PL_ppaddr[type];
5476 unop->op_first = first;
5477 unop->op_flags = (U8)(flags | (first ? OPf_KIDS : 0));
5478 unop->op_private = (U8)((first ? 1 : 0) | (flags >> 8));
5481 if (first && !OpHAS_SIBLING(first)) /* true unless weird syntax error */
5482 OpLASTSIB_set(first, (OP*)unop);
5484 unop = (UNOP_AUX*) CHECKOP(type, unop);
5486 return op_std_init((OP *) unop);
5490 =for apidoc newMETHOP
5492 Constructs, checks, and returns an op of method type with a method name
5493 evaluated at runtime. C<type> is the opcode. C<flags> gives the eight
5494 bits of C<op_flags>, except that C<OPf_KIDS> will be set automatically,
5495 and, shifted up eight bits, the eight bits of C<op_private>, except that
5496 the bit with value 1 is automatically set. C<dynamic_meth> supplies an
5497 op which evaluates method name; it is consumed by this function and
5498 become part of the constructed op tree.
5499 Supported optypes: C<OP_METHOD>.
5505 S_newMETHOP_internal(pTHX_ I32 type, I32 flags, OP* dynamic_meth, SV* const_meth) {
5508 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_METHOP
5509 || type == OP_CUSTOM);
5511 NewOp(1101, methop, 1, METHOP);
5513 if (PL_opargs[type] & OA_MARK) dynamic_meth = force_list(dynamic_meth, TRUE);
5514 methop->op_flags = (U8)(flags | OPf_KIDS);
5515 methop->op_u.op_first = dynamic_meth;
5516 methop->op_private = (U8)(1 | (flags >> 8));
5518 if (!OpHAS_SIBLING(dynamic_meth))
5519 OpLASTSIB_set(dynamic_meth, (OP*)methop);
5523 methop->op_flags = (U8)(flags & ~OPf_KIDS);
5524 methop->op_u.op_meth_sv = const_meth;
5525 methop->op_private = (U8)(0 | (flags >> 8));
5526 methop->op_next = (OP*)methop;
5530 methop->op_rclass_targ = 0;
5532 methop->op_rclass_sv = NULL;
5535 OpTYPE_set(methop, type);
5536 return CHECKOP(type, methop);
5540 Perl_newMETHOP (pTHX_ I32 type, I32 flags, OP* dynamic_meth) {
5541 PERL_ARGS_ASSERT_NEWMETHOP;
5542 return newMETHOP_internal(type, flags, dynamic_meth, NULL);
5546 =for apidoc newMETHOP_named
5548 Constructs, checks, and returns an op of method type with a constant
5549 method name. C<type> is the opcode. C<flags> gives the eight bits of
5550 C<op_flags>, and, shifted up eight bits, the eight bits of
5551 C<op_private>. C<const_meth> supplies a constant method name;
5552 it must be a shared COW string.
5553 Supported optypes: C<OP_METHOD_NAMED>.
5559 Perl_newMETHOP_named (pTHX_ I32 type, I32 flags, SV* const_meth) {
5560 PERL_ARGS_ASSERT_NEWMETHOP_NAMED;
5561 return newMETHOP_internal(type, flags, NULL, const_meth);
5565 =for apidoc newBINOP
5567 Constructs, checks, and returns an op of any binary type. C<type>
5568 is the opcode. C<flags> gives the eight bits of C<op_flags>, except
5569 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
5570 the eight bits of C<op_private>, except that the bit with value 1 or
5571 2 is automatically set as required. C<first> and C<last> supply up to
5572 two ops to be the direct children of the binary op; they are consumed
5573 by this function and become part of the constructed op tree.
5579 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
5583 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
5584 || type == OP_NULL || type == OP_CUSTOM);
5586 NewOp(1101, binop, 1, BINOP);
5589 first = newOP(OP_NULL, 0);
5591 OpTYPE_set(binop, type);
5592 binop->op_first = first;
5593 binop->op_flags = (U8)(flags | OPf_KIDS);
5596 binop->op_private = (U8)(1 | (flags >> 8));
5599 binop->op_private = (U8)(2 | (flags >> 8));
5600 OpMORESIB_set(first, last);
5603 if (!OpHAS_SIBLING(last)) /* true unless weird syntax error */
5604 OpLASTSIB_set(last, (OP*)binop);
5606 binop->op_last = OpSIBLING(binop->op_first);
5608 OpLASTSIB_set(binop->op_last, (OP*)binop);
5610 binop = (BINOP*) CHECKOP(type, binop);
5611 if (binop->op_next || binop->op_type != (OPCODE)type)
5614 return fold_constants(op_integerize(op_std_init((OP *)binop)));
5618 Perl_invmap_dump(pTHX_ SV* invlist, UV *map)
5620 const char indent[] = " ";
5622 UV len = _invlist_len(invlist);
5623 UV * array = invlist_array(invlist);
5626 PERL_ARGS_ASSERT_INVMAP_DUMP;
5628 for (i = 0; i < len; i++) {
5629 UV start = array[i];
5630 UV end = (i + 1 < len) ? array[i+1] - 1 : IV_MAX;
5632 PerlIO_printf(Perl_debug_log, "%s[%" UVuf "] 0x%04" UVXf, indent, i, start);
5633 if (end == IV_MAX) {
5634 PerlIO_printf(Perl_debug_log, " .. INFTY");
5636 else if (end != start) {
5637 PerlIO_printf(Perl_debug_log, " .. 0x%04" UVXf, end);
5640 PerlIO_printf(Perl_debug_log, " ");
5643 PerlIO_printf(Perl_debug_log, "\t");
5645 if (map[i] == TR_UNLISTED) {
5646 PerlIO_printf(Perl_debug_log, "TR_UNLISTED\n");
5648 else if (map[i] == TR_SPECIAL_HANDLING) {
5649 PerlIO_printf(Perl_debug_log, "TR_SPECIAL_HANDLING\n");
5652 PerlIO_printf(Perl_debug_log, "0x%04" UVXf "\n", map[i]);
5657 /* Given an OP_TRANS / OP_TRANSR op o, plus OP_CONST ops expr and repl
5658 * containing the search and replacement strings, assemble into
5659 * a translation table attached as o->op_pv.
5660 * Free expr and repl.
5661 * It expects the toker to have already set the
5662 * OPpTRANS_COMPLEMENT
5665 * flags as appropriate; this function may add
5667 * OPpTRANS_CAN_FORCE_UTF8
5668 * OPpTRANS_IDENTICAL
5674 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
5676 /* This function compiles a tr///, from data gathered from toke.c, into a
5677 * form suitable for use by do_trans() in doop.c at runtime.
5679 * It first normalizes the data, while discarding extraneous inputs; then
5680 * writes out the compiled data. The normalization allows for complete
5681 * analysis, and avoids some false negatives and positives earlier versions
5684 * The normalization form is an inversion map (described below in detail).
5685 * This is essentially the compiled form for tr///'s that require UTF-8,
5686 * and its easy to use it to write the 257-byte table for tr///'s that
5687 * don't need UTF-8. That table is identical to what's been in use for
5688 * many perl versions, except that it doesn't handle some edge cases that
5689 * it used to, involving code points above 255. The UTF-8 form now handles
5690 * these. (This could be changed with extra coding should it shown to be
5693 * If the complement (/c) option is specified, the lhs string (tstr) is
5694 * parsed into an inversion list. Complementing these is trivial. Then a
5695 * complemented tstr is built from that, and used thenceforth. This hides
5696 * the fact that it was complemented from almost all successive code.
5698 * One of the important characteristics to know about the input is whether
5699 * the transliteration may be done in place, or does a temporary need to be
5700 * allocated, then copied. If the replacement for every character in every
5701 * possible string takes up no more bytes than the character it
5702 * replaces, then it can be edited in place. Otherwise the replacement
5703 * could overwrite a byte we are about to read, depending on the strings
5704 * being processed. The comments and variable names here refer to this as
5705 * "growing". Some inputs won't grow, and might even shrink under /d, but
5706 * some inputs could grow, so we have to assume any given one might grow.
5707 * On very long inputs, the temporary could eat up a lot of memory, so we
5708 * want to avoid it if possible. For non-UTF-8 inputs, everything is
5709 * single-byte, so can be edited in place, unless there is something in the
5710 * pattern that could force it into UTF-8. The inversion map makes it
5711 * feasible to determine this. Previous versions of this code pretty much
5712 * punted on determining if UTF-8 could be edited in place. Now, this code
5713 * is rigorous in making that determination.
5715 * Another characteristic we need to know is whether the lhs and rhs are
5716 * identical. If so, and no other flags are present, the only effect of
5717 * the tr/// is to count the characters present in the input that are
5718 * mentioned in the lhs string. The implementation of that is easier and
5719 * runs faster than the more general case. Normalizing here allows for
5720 * accurate determination of this. Previously there were false negatives
5723 * Instead of 'transliterated', the comments here use 'unmapped' for the
5724 * characters that are left unchanged by the operation; otherwise they are
5727 * The lhs of the tr/// is here referred to as the t side.
5728 * The rhs of the tr/// is here referred to as the r side.
5731 SV * const tstr = cSVOPx(expr)->op_sv;
5732 SV * const rstr = cSVOPx(repl)->op_sv;
5735 const U8 * t0 = (U8*)SvPV_const(tstr, tlen);
5736 const U8 * r0 = (U8*)SvPV_const(rstr, rlen);
5739 UV t_count = 0, r_count = 0; /* Number of characters in search and
5740 replacement lists */
5742 /* khw thinks some of the private flags for this op are quaintly named.
5743 * OPpTRANS_GROWS for example is TRUE if the replacement for some lhs
5744 * character when represented in UTF-8 is longer than the original
5745 * character's UTF-8 representation */
5746 const bool complement = cBOOL(o->op_private & OPpTRANS_COMPLEMENT);
5747 const bool squash = cBOOL(o->op_private & OPpTRANS_SQUASH);
5748 const bool del = cBOOL(o->op_private & OPpTRANS_DELETE);
5750 /* Set to true if there is some character < 256 in the lhs that maps to
5751 * above 255. If so, a non-UTF-8 match string can be forced into being in
5752 * UTF-8 by a tr/// operation. */
5753 bool can_force_utf8 = FALSE;
5755 /* What is the maximum expansion factor in UTF-8 transliterations. If a
5756 * 2-byte UTF-8 encoded character is to be replaced by a 3-byte one, its
5757 * expansion factor is 1.5. This number is used at runtime to calculate
5758 * how much space to allocate for non-inplace transliterations. Without
5759 * this number, the worst case is 14, which is extremely unlikely to happen
5760 * in real life, and could require significant memory overhead. */
5761 NV max_expansion = 1.;
5763 UV t_range_count, r_range_count, min_range_count;
5767 UV r_cp = 0, t_cp = 0;
5768 UV t_cp_end = (UV) -1;
5772 UV final_map = TR_UNLISTED; /* The final character in the replacement
5773 list, updated as we go along. Initialize
5774 to something illegal */
5776 bool rstr_utf8 = cBOOL(SvUTF8(rstr));
5777 bool tstr_utf8 = cBOOL(SvUTF8(tstr));
5779 const U8* tend = t + tlen;
5780 const U8* rend = r + rlen;
5782 SV * inverted_tstr = NULL;
5787 /* This routine implements detection of a transliteration having a longer
5788 * UTF-8 representation than its source, by partitioning all the possible
5789 * code points of the platform into equivalence classes of the same UTF-8
5790 * byte length in the first pass. As it constructs the mappings, it carves
5791 * these up into smaller chunks, but doesn't merge any together. This
5792 * makes it easy to find the instances it's looking for. A second pass is
5793 * done after this has been determined which merges things together to
5794 * shrink the table for runtime. The table below is used for both ASCII
5795 * and EBCDIC platforms. On EBCDIC, the byte length is not monotonically
5796 * increasing for code points below 256. To correct for that, the macro
5797 * CP_ADJUST defined below converts those code points to ASCII in the first
5798 * pass, and we use the ASCII partition values. That works because the
5799 * growth factor will be unaffected, which is all that is calculated during
5800 * the first pass. */
5801 UV PL_partition_by_byte_length[] = {
5803 0x80, /* Below this is 1 byte representations */
5804 (32 * (1UL << ( UTF_ACCUMULATION_SHIFT))), /* 2 bytes below this */
5805 (16 * (1UL << (2 * UTF_ACCUMULATION_SHIFT))), /* 3 bytes below this */
5806 ( 8 * (1UL << (3 * UTF_ACCUMULATION_SHIFT))), /* 4 bytes below this */
5807 ( 4 * (1UL << (4 * UTF_ACCUMULATION_SHIFT))), /* 5 bytes below this */
5808 ( 2 * (1UL << (5 * UTF_ACCUMULATION_SHIFT))) /* 6 bytes below this */
5812 ( ((UV) 1U << (6 * UTF_ACCUMULATION_SHIFT))) /* 7 bytes below this */
5817 PERL_ARGS_ASSERT_PMTRANS;
5819 PL_hints |= HINT_BLOCK_SCOPE;
5821 /* If /c, the search list is sorted and complemented. This is now done by
5822 * creating an inversion list from it, and then trivially inverting that.
5823 * The previous implementation used qsort, but creating the list
5824 * automatically keeps it sorted as we go along */
5827 SV * inverted_tlist = _new_invlist(tlen);
5830 DEBUG_y(PerlIO_printf(Perl_debug_log,
5831 "%s: %d: tstr before inversion=\n%s\n",
5832 __FILE__, __LINE__, _byte_dump_string(t, tend - t, 0)));
5836 /* Non-utf8 strings don't have ranges, so each character is listed
5839 inverted_tlist = add_cp_to_invlist(inverted_tlist, *t);
5842 else { /* But UTF-8 strings have been parsed in toke.c to have
5843 * ranges if appropriate. */
5847 /* Get the first character */
5848 t_cp = valid_utf8_to_uvchr(t, &t_char_len);
5851 /* If the next byte indicates that this wasn't the first
5852 * element of a range, the range is just this one */
5853 if (t >= tend || *t != RANGE_INDICATOR) {
5854 inverted_tlist = add_cp_to_invlist(inverted_tlist, t_cp);
5856 else { /* Otherwise, ignore the indicator byte, and get the
5857 final element, and add the whole range */
5859 t_cp_end = valid_utf8_to_uvchr(t, &t_char_len);
5862 inverted_tlist = _add_range_to_invlist(inverted_tlist,
5866 } /* End of parse through tstr */
5868 /* The inversion list is done; now invert it */
5869 _invlist_invert(inverted_tlist);
5871 /* Now go through the inverted list and create a new tstr for the rest
5872 * of the routine to use. Since the UTF-8 version can have ranges, and
5873 * can be much more compact than the non-UTF-8 version, we create the
5874 * string in UTF-8 even if not necessary. (This is just an intermediate
5875 * value that gets thrown away anyway.) */
5876 invlist_iterinit(inverted_tlist);
5877 inverted_tstr = newSVpvs("");
5878 while (invlist_iternext(inverted_tlist, &start, &end)) {
5879 U8 temp[UTF8_MAXBYTES];
5882 /* IV_MAX keeps things from going out of bounds */
5883 start = MIN(IV_MAX, start);
5884 end = MIN(IV_MAX, end);
5886 temp_end_pos = uvchr_to_utf8(temp, start);
5887 sv_catpvn(inverted_tstr, (char *) temp, temp_end_pos - temp);
5890 Perl_sv_catpvf(aTHX_ inverted_tstr, "%c", RANGE_INDICATOR);
5891 temp_end_pos = uvchr_to_utf8(temp, end);
5892 sv_catpvn(inverted_tstr, (char *) temp, temp_end_pos - temp);
5896 /* Set up so the remainder of the routine uses this complement, instead
5897 * of the actual input */
5898 t0 = t = (U8*)SvPV_const(inverted_tstr, temp_len);
5899 tend = t0 + temp_len;
5902 SvREFCNT_dec_NN(inverted_tlist);
5905 /* For non-/d, an empty rhs means to use the lhs */
5906 if (rlen == 0 && ! del) {
5909 rstr_utf8 = tstr_utf8;
5912 t_invlist = _new_invlist(1);
5914 /* Initialize to a single range */
5915 t_invlist = _add_range_to_invlist(t_invlist, 0, UV_MAX);
5917 /* Below, we parse the (potentially adjusted) input, creating the inversion
5918 * map. This is done in two passes. The first pass is just to determine
5919 * if the transliteration can be done in-place. It can be done in place if
5920 * no possible inputs result in the replacement taking up more bytes than
5921 * the input. To figure that out, in the first pass we start with all the
5922 * possible code points partitioned into ranges so that every code point in
5923 * a range occupies the same number of UTF-8 bytes as every other code
5924 * point in the range. Constructing the inversion map doesn't merge ranges
5925 * together, but can split them into multiple ones. Given the starting
5926 * partition, the ending state will also have the same characteristic,
5927 * namely that each code point in each partition requires the same number
5928 * of UTF-8 bytes to represent as every other code point in the same
5931 * This partioning has been pre-compiled. Copy it to initialize */
5932 len = C_ARRAY_LENGTH(PL_partition_by_byte_length);
5933 invlist_extend(t_invlist, len);
5934 t_array = invlist_array(t_invlist);
5935 Copy(PL_partition_by_byte_length, t_array, len, UV);
5936 invlist_set_len(t_invlist, len, *(get_invlist_offset_addr(t_invlist)));
5937 Newx(r_map, len + 1, UV);
5939 /* The inversion map the first pass creates could be used as-is, but
5940 * generally would be larger and slower to run than the output of the
5943 for (pass2 = 0; pass2 < 2; pass2++) {
5945 /* In the second pass, we start with a single range */
5946 t_invlist = _add_range_to_invlist(t_invlist, 0, UV_MAX);
5948 t_array = invlist_array(t_invlist);
5951 /* As noted earlier, we convert EBCDIC code points to Unicode in the first pass
5952 * so as to get the well-behaved length 1 vs length 2 boundary. Only code
5953 * points below 256 differ between the two character sets in this regard. For
5954 * these, we also can't have any ranges, as they have to be individually
5957 # define CP_ADJUST(x) ((pass2) ? (x) : NATIVE_TO_UNI(x))
5958 # define FORCE_RANGE_LEN_1(x) ((pass2) ? 0 : ((x) < 256))
5959 # define CP_SKIP(x) ((pass2) ? UVCHR_SKIP(x) : OFFUNISKIP(x))
5961 # define CP_ADJUST(x) (x)
5962 # define FORCE_RANGE_LEN_1(x) 0
5963 # define CP_SKIP(x) UVCHR_SKIP(x)
5966 /* And the mapping of each of the ranges is initialized. Initially,
5967 * everything is TR_UNLISTED. */
5968 for (i = 0; i < len; i++) {
5969 r_map[i] = TR_UNLISTED;
5976 t_range_count = r_range_count = 0;
5978 DEBUG_y(PerlIO_printf(Perl_debug_log, "%s: %d:\ntstr=%s\n",
5979 __FILE__, __LINE__, _byte_dump_string(t, tend - t, 0)));
5980 DEBUG_y(PerlIO_printf(Perl_debug_log, "rstr=%s\n",
5981 _byte_dump_string(r, rend - r, 0)));
5982 DEBUG_y(PerlIO_printf(Perl_debug_log, "/c=%d; /s=%d; /d=%d\n",
5983 complement, squash, del));
5984 DEBUG_y(invmap_dump(t_invlist, r_map));
5986 /* Now go through the search list constructing an inversion map. The
5987 * input is not necessarily in any particular order. Making it an
5988 * inversion map orders it, potentially simplifying, and makes it easy
5989 * to deal with at run time. This is the only place in core that
5990 * generates an inversion map; if others were introduced, it might be
5991 * better to create general purpose routines to handle them.
5992 * (Inversion maps are created in perl in other places.)
5994 * An inversion map consists of two parallel arrays. One is
5995 * essentially an inversion list: an ordered list of code points such
5996 * that each element gives the first code point of a range of
5997 * consecutive code points that map to the element in the other array
5998 * that has the same index as this one (in other words, the
5999 * corresponding element). Thus the range extends up to (but not
6000 * including) the code point given by the next higher element. In a
6001 * true inversion map, the corresponding element in the other array
6002 * gives the mapping of the first code point in the range, with the
6003 * understanding that the next higher code point in the inversion
6004 * list's range will map to the next higher code point in the map.
6006 * So if at element [i], let's say we have:
6011 * This means that A => a, B => b, C => c.... Let's say that the
6012 * situation is such that:
6016 * This means the sequence that started at [i] stops at K => k. This
6017 * illustrates that you need to look at the next element to find where
6018 * a sequence stops. Except, the highest element in the inversion list
6019 * begins a range that is understood to extend to the platform's
6022 * This routine modifies traditional inversion maps to reserve two
6025 * TR_UNLISTED (or -1) indicates that no code point in the range
6026 * is listed in the tr/// searchlist. At runtime, these are
6027 * always passed through unchanged. In the inversion map, all
6028 * points in the range are mapped to -1, instead of increasing,
6029 * like the 'L' in the example above.
6031 * We start the parse with every code point mapped to this, and as
6032 * we parse and find ones that are listed in the search list, we
6033 * carve out ranges as we go along that override that.
6035 * TR_SPECIAL_HANDLING (or -2) indicates that every code point in the
6036 * range needs special handling. Again, all code points in the
6037 * range are mapped to -2, instead of increasing.
6039 * Under /d this value means the code point should be deleted from
6040 * the transliteration when encountered.
6042 * Otherwise, it marks that every code point in the range is to
6043 * map to the final character in the replacement list. This
6044 * happens only when the replacement list is shorter than the
6045 * search one, so there are things in the search list that have no
6046 * correspondence in the replacement list. For example, in
6047 * tr/a-z/A/, 'A' is the final value, and the inversion map
6048 * generated for this would be like this:
6053 * 'A' appears once, then the remainder of the range maps to -2.
6054 * The use of -2 isn't strictly necessary, as an inversion map is
6055 * capable of representing this situation, but not nearly so
6056 * compactly, and this is actually quite commonly encountered.
6057 * Indeed, the original design of this code used a full inversion
6058 * map for this. But things like
6060 * generated huge data structures, slowly, and the execution was
6061 * also slow. So the current scheme was implemented.
6063 * So, if the next element in our example is:
6067 * Then all of L, M, N, O, and P map to TR_UNLISTED. If the next
6071 * [i+4] S TR_UNLISTED
6073 * Then Q => q; R => z; and S => TR_UNLISTED. If [i+4] (the 'S') is
6074 * the final element in the arrays, every code point from S to infinity
6075 * maps to TR_UNLISTED.
6078 /* Finish up range started in what otherwise would
6079 * have been the final iteration */
6080 while (t < tend || t_range_count > 0) {
6081 bool adjacent_to_range_above = FALSE;
6082 bool adjacent_to_range_below = FALSE;
6084 bool merge_with_range_above = FALSE;
6085 bool merge_with_range_below = FALSE;
6087 UV span, invmap_range_length_remaining;
6091 /* If we are in the middle of processing a range in the 'target'
6092 * side, the previous iteration has set us up. Otherwise, look at
6093 * the next character in the search list */
6094 if (t_range_count <= 0) {
6097 /* Here, not in the middle of a range, and not UTF-8. The
6098 * next code point is the single byte where we're at */
6099 t_cp = CP_ADJUST(*t);
6106 /* Here, not in the middle of a range, and is UTF-8. The
6107 * next code point is the next UTF-8 char in the input. We
6108 * know the input is valid, because the toker constructed
6110 t_cp = CP_ADJUST(valid_utf8_to_uvchr(t, &t_char_len));
6113 /* UTF-8 strings (only) have been parsed in toke.c to have
6114 * ranges. See if the next byte indicates that this was
6115 * the first element of a range. If so, get the final
6116 * element and calculate the range size. If not, the range
6118 if ( t < tend && *t == RANGE_INDICATOR
6119 && ! FORCE_RANGE_LEN_1(t_cp))
6122 t_range_count = valid_utf8_to_uvchr(t, &t_char_len)
6131 /* Count the total number of listed code points * */
6132 t_count += t_range_count;
6135 /* Similarly, get the next character in the replacement list */
6136 if (r_range_count <= 0) {
6139 /* But if we've exhausted the rhs, there is nothing to map
6140 * to, except the special handling one, and we make the
6141 * range the same size as the lhs one. */
6142 r_cp = TR_SPECIAL_HANDLING;
6143 r_range_count = t_range_count;
6146 DEBUG_yv(PerlIO_printf(Perl_debug_log,
6147 "final_map =%" UVXf "\n", final_map));
6152 r_cp = CP_ADJUST(*r);
6159 r_cp = CP_ADJUST(valid_utf8_to_uvchr(r, &r_char_len));
6161 if ( r < rend && *r == RANGE_INDICATOR
6162 && ! FORCE_RANGE_LEN_1(r_cp))
6165 r_range_count = valid_utf8_to_uvchr(r,
6166 &r_char_len) - r_cp + 1;
6174 if (r_cp == TR_SPECIAL_HANDLING) {
6175 r_range_count = t_range_count;
6178 /* This is the final character so far */
6179 final_map = r_cp + r_range_count - 1;
6181 r_count += r_range_count;
6185 /* Here, we have the next things ready in both sides. They are
6186 * potentially ranges. We try to process as big a chunk as
6187 * possible at once, but the lhs and rhs must be synchronized, so
6188 * things like tr/A-Z/a-ij-z/ will need to be processed in 2 chunks
6190 min_range_count = MIN(t_range_count, r_range_count);
6192 /* Search the inversion list for the entry that contains the input
6193 * code point <cp>. The inversion map was initialized to cover the
6194 * entire range of possible inputs, so this should not fail. So
6195 * the return value is the index into the list's array of the range
6196 * that contains <cp>, that is, 'i' such that array[i] <= cp <
6198 j = _invlist_search(t_invlist, t_cp);
6202 /* Here, the data structure might look like:
6205 * [i-1] J j # J-L => j-l
6206 * [i] M -1 # M => default; as do N, O, P, Q
6207 * [i+1] R x # R => x, S => x+1, T => x+2
6208 * [i+2] U y # U => y, V => y+1, ...
6210 * [-1] Z -1 # Z => default; as do Z+1, ... infinity
6212 * where 'x' and 'y' above are not to be taken literally.
6214 * The maximum chunk we can handle in this loop iteration, is the
6215 * smallest of the three components: the lhs 't_', the rhs 'r_',
6216 * and the remainder of the range in element [i]. (In pass 1, that
6217 * range will have everything in it be of the same class; we can't
6218 * cross into another class.) 'min_range_count' already contains
6219 * the smallest of the first two values. The final one is
6220 * irrelevant if the map is to the special indicator */
6222 invmap_range_length_remaining = (i + 1 < len)
6223 ? t_array[i+1] - t_cp
6225 span = MAX(1, MIN(min_range_count, invmap_range_length_remaining));
6227 /* The end point of this chunk is where we are, plus the span, but
6228 * never larger than the platform's infinity */
6229 t_cp_end = MIN(IV_MAX, t_cp + span - 1);
6231 if (r_cp == TR_SPECIAL_HANDLING) {
6233 /* If unmatched lhs code points map to the final map, use that
6234 * value. This being set to TR_SPECIAL_HANDLING indicates that
6235 * we don't have a final map: unmatched lhs code points are
6237 r_cp_end = (del) ? TR_SPECIAL_HANDLING : final_map;
6240 r_cp_end = MIN(IV_MAX, r_cp + span - 1);
6242 /* If something on the lhs is below 256, and something on the
6243 * rhs is above, there is a potential mapping here across that
6244 * boundary. Indeed the only way there isn't is if both sides
6245 * start at the same point. That means they both cross at the
6246 * same time. But otherwise one crosses before the other */
6247 if (t_cp < 256 && r_cp_end > 255 && r_cp != t_cp) {
6248 can_force_utf8 = TRUE;
6252 /* If a character appears in the search list more than once, the
6253 * 2nd and succeeding occurrences are ignored, so only do this
6254 * range if haven't already processed this character. (The range
6255 * has been set up so that all members in it will be of the same
6257 if (r_map[i] == TR_UNLISTED) {
6258 DEBUG_yv(PerlIO_printf(Perl_debug_log,
6259 "Processing %" UVxf "-%" UVxf " => %" UVxf "-%" UVxf "\n",
6260 t_cp, t_cp_end, r_cp, r_cp_end));
6262 /* This is the first definition for this chunk, hence is valid
6263 * and needs to be processed. Here and in the comments below,
6264 * we use the above sample data. The t_cp chunk must be any
6265 * contiguous subset of M, N, O, P, and/or Q.
6267 * In the first pass, calculate if there is any possible input
6268 * string that has a character whose transliteration will be
6269 * longer than it. If none, the transliteration may be done
6270 * in-place, as it can't write over a so-far unread byte.
6271 * Otherwise, a copy must first be made. This could be
6272 * expensive for long inputs.
6274 * In the first pass, the t_invlist has been partitioned so
6275 * that all elements in any single range have the same number
6276 * of bytes in their UTF-8 representations. And the r space is
6277 * either a single byte, or a range of strictly monotonically
6278 * increasing code points. So the final element in the range
6279 * will be represented by no fewer bytes than the initial one.
6280 * That means that if the final code point in the t range has
6281 * at least as many bytes as the final code point in the r,
6282 * then all code points in the t range have at least as many
6283 * bytes as their corresponding r range element. But if that's
6284 * not true, the transliteration of at least the final code
6285 * point grows in length. As an example, suppose we had
6286 * tr/\x{fff0}-\x{fff1}/\x{ffff}-\x{10000}/
6287 * The UTF-8 for all but 10000 occupies 3 bytes on ASCII
6288 * platforms. We have deliberately set up the data structure
6289 * so that any range in the lhs gets split into chunks for
6290 * processing, such that every code point in a chunk has the
6291 * same number of UTF-8 bytes. We only have to check the final
6292 * code point in the rhs against any code point in the lhs. */
6294 && r_cp_end != TR_SPECIAL_HANDLING
6295 && CP_SKIP(t_cp_end) < CP_SKIP(r_cp_end))
6297 /* Here, we will need to make a copy of the input string
6298 * before doing the transliteration. The worst possible
6299 * case is an expansion ratio of 14:1. This is rare, and
6300 * we'd rather allocate only the necessary amount of extra
6301 * memory for that copy. We can calculate the worst case
6302 * for this particular transliteration is by keeping track
6303 * of the expansion factor for each range.
6305 * Consider tr/\xCB/\X{E000}/. The maximum expansion
6306 * factor is 1 byte going to 3 if the target string is not
6307 * UTF-8, but 2 bytes going to 3 if it is in UTF-8. We
6308 * could pass two different values so doop could choose
6309 * based on the UTF-8ness of the target. But khw thinks
6310 * (perhaps wrongly) that is overkill. It is used only to
6311 * make sure we malloc enough space.
6313 * If no target string can force the result to be UTF-8,
6314 * then we don't have to worry about the case of the target
6315 * string not being UTF-8 */
6316 NV t_size = (can_force_utf8 && t_cp < 256)
6318 : CP_SKIP(t_cp_end);
6319 NV ratio = CP_SKIP(r_cp_end) / t_size;
6321 o->op_private |= OPpTRANS_GROWS;
6323 /* Now that we know it grows, we can keep track of the
6325 if (ratio > max_expansion) {
6326 max_expansion = ratio;
6327 DEBUG_y(PerlIO_printf(Perl_debug_log,
6328 "New expansion factor: %" NVgf "\n",
6333 /* The very first range is marked as adjacent to the
6334 * non-existent range below it, as it causes things to "just
6337 * If the lowest code point in this chunk is M, it adjoins the
6339 if (t_cp == t_array[i]) {
6340 adjacent_to_range_below = TRUE;
6342 /* And if the map has the same offset from the beginning of
6343 * the range as does this new code point (or both are for
6344 * TR_SPECIAL_HANDLING), this chunk can be completely
6345 * merged with the range below. EXCEPT, in the first pass,
6346 * we don't merge ranges whose UTF-8 byte representations
6347 * have different lengths, so that we can more easily
6348 * detect if a replacement is longer than the source, that
6349 * is if it 'grows'. But in the 2nd pass, there's no
6350 * reason to not merge */
6351 if ( (i > 0 && ( pass2
6352 || CP_SKIP(t_array[i-1])
6354 && ( ( r_cp == TR_SPECIAL_HANDLING
6355 && r_map[i-1] == TR_SPECIAL_HANDLING)
6356 || ( r_cp != TR_SPECIAL_HANDLING
6357 && r_cp - r_map[i-1] == t_cp - t_array[i-1])))
6359 merge_with_range_below = TRUE;
6363 /* Similarly, if the highest code point in this chunk is 'Q',
6364 * it adjoins the range above, and if the map is suitable, can
6365 * be merged with it */
6366 if ( t_cp_end >= IV_MAX - 1
6368 && t_cp_end + 1 == t_array[i+1]))
6370 adjacent_to_range_above = TRUE;
6373 || CP_SKIP(t_cp) == CP_SKIP(t_array[i+1]))
6374 && ( ( r_cp == TR_SPECIAL_HANDLING
6375 && r_map[i+1] == (UV) TR_SPECIAL_HANDLING)
6376 || ( r_cp != TR_SPECIAL_HANDLING
6377 && r_cp_end == r_map[i+1] - 1)))
6379 merge_with_range_above = TRUE;
6383 if (merge_with_range_below && merge_with_range_above) {
6385 /* Here the new chunk looks like M => m, ... Q => q; and
6386 * the range above is like R => r, .... Thus, the [i-1]
6387 * and [i+1] ranges should be seamlessly melded so the
6390 * [i-1] J j # J-T => j-t
6391 * [i] U y # U => y, V => y+1, ...
6393 * [-1] Z -1 # Z => default; as do Z+1, ... infinity
6395 Move(t_array + i + 2, t_array + i, len - i - 2, UV);
6396 Move(r_map + i + 2, r_map + i, len - i - 2, UV);
6398 invlist_set_len(t_invlist,
6400 *(get_invlist_offset_addr(t_invlist)));
6402 else if (merge_with_range_below) {
6404 /* Here the new chunk looks like M => m, .... But either
6405 * (or both) it doesn't extend all the way up through Q; or
6406 * the range above doesn't start with R => r. */
6407 if (! adjacent_to_range_above) {
6409 /* In the first case, let's say the new chunk extends
6410 * through O. We then want:
6412 * [i-1] J j # J-O => j-o
6413 * [i] P -1 # P => -1, Q => -1
6414 * [i+1] R x # R => x, S => x+1, T => x+2
6415 * [i+2] U y # U => y, V => y+1, ...
6417 * [-1] Z -1 # Z => default; as do Z+1, ...
6420 t_array[i] = t_cp_end + 1;
6421 r_map[i] = TR_UNLISTED;
6423 else { /* Adjoins the range above, but can't merge with it
6424 (because 'x' is not the next map after q) */
6426 * [i-1] J j # J-Q => j-q
6427 * [i] R x # R => x, S => x+1, T => x+2
6428 * [i+1] U y # U => y, V => y+1, ...
6430 * [-1] Z -1 # Z => default; as do Z+1, ...
6434 Move(t_array + i + 1, t_array + i, len - i - 1, UV);
6435 Move(r_map + i + 1, r_map + i, len - i - 1, UV);
6437 invlist_set_len(t_invlist, len,
6438 *(get_invlist_offset_addr(t_invlist)));
6441 else if (merge_with_range_above) {
6443 /* Here the new chunk ends with Q => q, and the range above
6444 * must start with R => r, so the two can be merged. But
6445 * either (or both) the new chunk doesn't extend all the
6446 * way down to M; or the mapping of the final code point
6447 * range below isn't m */
6448 if (! adjacent_to_range_below) {
6450 /* In the first case, let's assume the new chunk starts
6451 * with P => p. Then, because it's merge-able with the
6452 * range above, that range must be R => r. We want:
6454 * [i-1] J j # J-L => j-l
6455 * [i] M -1 # M => -1, N => -1
6456 * [i+1] P p # P-T => p-t
6457 * [i+2] U y # U => y, V => y+1, ...
6459 * [-1] Z -1 # Z => default; as do Z+1, ...
6462 t_array[i+1] = t_cp;
6465 else { /* Adjoins the range below, but can't merge with it
6468 * [i-1] J j # J-L => j-l
6469 * [i] M x # M-T => x-5 .. x+2
6470 * [i+1] U y # U => y, V => y+1, ...
6472 * [-1] Z -1 # Z => default; as do Z+1, ...
6475 Move(t_array + i + 1, t_array + i, len - i - 1, UV);
6476 Move(r_map + i + 1, r_map + i, len - i - 1, UV);
6480 invlist_set_len(t_invlist, len,
6481 *(get_invlist_offset_addr(t_invlist)));
6484 else if (adjacent_to_range_below && adjacent_to_range_above) {
6485 /* The new chunk completely fills the gap between the
6486 * ranges on either side, but can't merge with either of
6489 * [i-1] J j # J-L => j-l
6490 * [i] M z # M => z, N => z+1 ... Q => z+4
6491 * [i+1] R x # R => x, S => x+1, T => x+2
6492 * [i+2] U y # U => y, V => y+1, ...
6494 * [-1] Z -1 # Z => default; as do Z+1, ... infinity
6498 else if (adjacent_to_range_below) {
6499 /* The new chunk adjoins the range below, but not the range
6500 * above, and can't merge. Let's assume the chunk ends at
6503 * [i-1] J j # J-L => j-l
6504 * [i] M z # M => z, N => z+1, O => z+2
6505 * [i+1] P -1 # P => -1, Q => -1
6506 * [i+2] R x # R => x, S => x+1, T => x+2
6507 * [i+3] U y # U => y, V => y+1, ...
6509 * [-w] Z -1 # Z => default; as do Z+1, ... infinity
6511 invlist_extend(t_invlist, len + 1);
6512 t_array = invlist_array(t_invlist);
6513 Renew(r_map, len + 1, UV);
6515 Move(t_array + i + 1, t_array + i + 2, len - i - 1, UV);
6516 Move(r_map + i + 1, r_map + i + 2, len - i - 1, UV);
6518 t_array[i+1] = t_cp_end + 1;
6519 r_map[i+1] = TR_UNLISTED;
6521 invlist_set_len(t_invlist, len,
6522 *(get_invlist_offset_addr(t_invlist)));
6524 else if (adjacent_to_range_above) {
6525 /* The new chunk adjoins the range above, but not the range
6526 * below, and can't merge. Let's assume the new chunk
6529 * [i-1] J j # J-L => j-l
6530 * [i] M -1 # M => default, N => default
6531 * [i+1] O z # O => z, P => z+1, Q => z+2
6532 * [i+2] R x # R => x, S => x+1, T => x+2
6533 * [i+3] U y # U => y, V => y+1, ...
6535 * [-1] Z -1 # Z => default; as do Z+1, ... infinity
6537 invlist_extend(t_invlist, len + 1);
6538 t_array = invlist_array(t_invlist);
6539 Renew(r_map, len + 1, UV);
6541 Move(t_array + i + 1, t_array + i + 2, len - i - 1, UV);
6542 Move(r_map + i + 1, r_map + i + 2, len - i - 1, UV);
6543 t_array[i+1] = t_cp;
6546 invlist_set_len(t_invlist, len,
6547 *(get_invlist_offset_addr(t_invlist)));
6550 /* The new chunk adjoins neither the range above, nor the
6551 * range below. Lets assume it is N..P => n..p
6553 * [i-1] J j # J-L => j-l
6554 * [i] M -1 # M => default
6555 * [i+1] N n # N..P => n..p
6556 * [i+2] Q -1 # Q => default
6557 * [i+3] R x # R => x, S => x+1, T => x+2
6558 * [i+4] U y # U => y, V => y+1, ...
6560 * [-1] Z -1 # Z => default; as do Z+1, ... infinity
6563 DEBUG_yv(PerlIO_printf(Perl_debug_log,
6564 "Before fixing up: len=%d, i=%d\n",
6565 (int) len, (int) i));
6566 DEBUG_yv(invmap_dump(t_invlist, r_map));
6568 invlist_extend(t_invlist, len + 2);
6569 t_array = invlist_array(t_invlist);
6570 Renew(r_map, len + 2, UV);
6572 Move(t_array + i + 1,
6573 t_array + i + 2 + 1, len - i - (2 - 1), UV);
6575 r_map + i + 2 + 1, len - i - (2 - 1), UV);
6578 invlist_set_len(t_invlist, len,
6579 *(get_invlist_offset_addr(t_invlist)));
6581 t_array[i+1] = t_cp;
6584 t_array[i+2] = t_cp_end + 1;
6585 r_map[i+2] = TR_UNLISTED;
6587 DEBUG_yv(PerlIO_printf(Perl_debug_log,
6588 "After iteration: span=%" UVuf ", t_range_count=%"
6589 UVuf " r_range_count=%" UVuf "\n",
6590 span, t_range_count, r_range_count));
6591 DEBUG_yv(invmap_dump(t_invlist, r_map));
6592 } /* End of this chunk needs to be processed */
6594 /* Done with this chunk. */
6596 if (t_cp >= IV_MAX) {
6599 t_range_count -= span;
6600 if (r_cp != TR_SPECIAL_HANDLING) {
6602 r_range_count -= span;
6608 } /* End of loop through the search list */
6610 /* We don't need an exact count, but we do need to know if there is
6611 * anything left over in the replacement list. So, just assume it's
6612 * one byte per character */
6616 } /* End of passes */
6618 SvREFCNT_dec(inverted_tstr);
6620 DEBUG_y(PerlIO_printf(Perl_debug_log, "After everything: \n"));
6621 DEBUG_y(invmap_dump(t_invlist, r_map));
6623 /* We now have normalized the input into an inversion map.
6625 * See if the lhs and rhs are equivalent. If so, this tr/// is a no-op
6626 * except for the count, and streamlined runtime code can be used */
6627 if (!del && !squash) {
6629 /* They are identical if they point to the same address, or if
6630 * everything maps to UNLISTED or to itself. This catches things that
6631 * not looking at the normalized inversion map doesn't catch, like
6632 * tr/aa/ab/ or tr/\x{100}-\x{104}/\x{100}-\x{102}\x{103}-\x{104} */
6634 for (i = 0; i < len; i++) {
6635 if (r_map[i] != TR_UNLISTED && r_map[i] != t_array[i]) {
6636 goto done_identical_check;
6641 /* Here have gone through entire list, and didn't find any
6642 * non-identical mappings */
6643 o->op_private |= OPpTRANS_IDENTICAL;
6645 done_identical_check: ;
6648 t_array = invlist_array(t_invlist);
6650 /* If has components above 255, we generally need to use the inversion map
6654 && t_array[len-1] > 255
6655 /* If the final range is 0x100-INFINITY and is a special
6656 * mapping, the table implementation can handle it */
6657 && ! ( t_array[len-1] == 256
6658 && ( r_map[len-1] == TR_UNLISTED
6659 || r_map[len-1] == TR_SPECIAL_HANDLING))))
6664 /* A UTF-8 op is generated, indicated by this flag. This op is an
6666 o->op_private |= OPpTRANS_USE_SVOP;
6668 if (can_force_utf8) {
6669 o->op_private |= OPpTRANS_CAN_FORCE_UTF8;
6672 /* The inversion map is pushed; first the list. */
6673 invmap = MUTABLE_AV(newAV());
6675 SvREADONLY_on(t_invlist);
6676 av_push(invmap, t_invlist);
6678 /* 2nd is the mapping */
6679 r_map_sv = newSVpvn((char *) r_map, len * sizeof(UV));
6680 SvREADONLY_on(r_map_sv);
6681 av_push(invmap, r_map_sv);
6683 /* 3rd is the max possible expansion factor */
6684 temp_sv = newSVnv(max_expansion);
6685 SvREADONLY_on(temp_sv);
6686 av_push(invmap, temp_sv);
6688 /* Characters that are in the search list, but not in the replacement
6689 * list are mapped to the final character in the replacement list */
6690 if (! del && r_count < t_count) {
6691 temp_sv = newSVuv(final_map);
6692 SvREADONLY_on(temp_sv);
6693 av_push(invmap, temp_sv);
6697 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY);
6698 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
6699 PAD_SETSV(cPADOPo->op_padix, (SV *) invmap);
6700 SvPADTMP_on(invmap);
6701 SvREADONLY_on(invmap);
6703 cSVOPo->op_sv = (SV *) invmap;
6711 /* The OPtrans_map struct already contains one slot; hence the -1. */
6712 SSize_t struct_size = sizeof(OPtrans_map)
6713 + (256 - 1 + 1)*sizeof(short);
6715 /* Non-utf8 case: set o->op_pv to point to a simple 256+ entry lookup
6716 * table. Entries with the value TR_UNMAPPED indicate chars not to be
6717 * translated, while TR_DELETE indicates a search char without a
6718 * corresponding replacement char under /d.
6720 * In addition, an extra slot at the end is used to store the final
6721 * repeating char, or TR_R_EMPTY under an empty replacement list, or
6722 * TR_DELETE under /d; which makes the runtime code easier. */
6724 /* Indicate this is an op_pv */
6725 o->op_private &= ~OPpTRANS_USE_SVOP;
6727 tbl = (OPtrans_map*)PerlMemShared_calloc(struct_size, 1);
6729 cPVOPo->op_pv = (char*)tbl;
6731 for (i = 0; i < len; i++) {
6732 STATIC_ASSERT_DECL(TR_SPECIAL_HANDLING == TR_DELETE);
6733 short upper = i >= len - 1 ? 256 : (short) t_array[i+1];
6734 short to = (short) r_map[i];
6736 bool do_increment = TRUE;
6738 /* Any code points above our limit should be irrelevant */
6739 if (t_array[i] >= tbl->size) break;
6741 /* Set up the map */
6742 if (to == (short) TR_SPECIAL_HANDLING && ! del) {
6743 to = (short) final_map;
6744 do_increment = FALSE;
6747 do_increment = FALSE;
6750 /* Create a map for everything in this range. The value increases
6751 * except for the special cases */
6752 for (j = (short) t_array[i]; j < upper; j++) {
6754 if (do_increment) to++;
6758 tbl->map[tbl->size] = del
6762 : (short) TR_R_EMPTY;
6763 DEBUG_y(PerlIO_printf(Perl_debug_log,"%s: %d\n", __FILE__, __LINE__));
6764 for (i = 0; i < tbl->size; i++) {
6765 if (tbl->map[i] < 0) {
6766 DEBUG_y(PerlIO_printf(Perl_debug_log," %02x=>%d",
6767 (unsigned) i, tbl->map[i]));
6770 DEBUG_y(PerlIO_printf(Perl_debug_log," %02x=>%02x",
6771 (unsigned) i, tbl->map[i]));
6773 if ((i+1) % 8 == 0 || i + 1 == (short) tbl->size) {
6774 DEBUG_y(PerlIO_printf(Perl_debug_log,"\n"));
6777 DEBUG_y(PerlIO_printf(Perl_debug_log,"Final map 0x%x=>%02x\n",
6778 (unsigned) tbl->size, tbl->map[tbl->size]));
6780 SvREFCNT_dec(t_invlist);
6782 #if 0 /* code that added excess above-255 chars at the end of the table, in
6783 case we ever want to not use the inversion map implementation for
6790 /* More replacement chars than search chars:
6791 * store excess replacement chars at end of main table.
6794 struct_size += excess;
6795 tbl = (OPtrans_map*)PerlMemShared_realloc(tbl,
6796 struct_size + excess * sizeof(short));
6797 tbl->size += excess;
6798 cPVOPo->op_pv = (char*)tbl;
6800 for (i = 0; i < excess; i++)
6801 tbl->map[i + 256] = r[j+i];
6804 /* no more replacement chars than search chars */
6810 DEBUG_y(PerlIO_printf(Perl_debug_log,
6811 "/d=%d, /s=%d, /c=%d, identical=%d, grows=%d,"
6812 " use_svop=%d, can_force_utf8=%d,\nexpansion=%" NVgf "\n",
6813 del, squash, complement,
6814 cBOOL(o->op_private & OPpTRANS_IDENTICAL),
6815 cBOOL(o->op_private & OPpTRANS_USE_SVOP),
6816 cBOOL(o->op_private & OPpTRANS_GROWS),
6817 cBOOL(o->op_private & OPpTRANS_CAN_FORCE_UTF8),
6822 if(del && rlen != 0 && r_count == t_count) {
6823 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
6824 } else if(r_count > t_count) {
6825 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
6838 Constructs, checks, and returns an op of any pattern matching type.
6839 C<type> is the opcode. C<flags> gives the eight bits of C<op_flags>
6840 and, shifted up eight bits, the eight bits of C<op_private>.
6846 Perl_newPMOP(pTHX_ I32 type, I32 flags)
6850 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP
6851 || type == OP_CUSTOM);
6853 NewOp(1101, pmop, 1, PMOP);
6854 OpTYPE_set(pmop, type);
6855 pmop->op_flags = (U8)flags;
6856 pmop->op_private = (U8)(0 | (flags >> 8));
6857 if (PL_opargs[type] & OA_RETSCALAR)
6860 if (PL_hints & HINT_RE_TAINT)
6861 pmop->op_pmflags |= PMf_RETAINT;
6862 #ifdef USE_LOCALE_CTYPE
6863 if (IN_LC_COMPILETIME(LC_CTYPE)) {
6864 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
6869 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
6871 if (PL_hints & HINT_RE_FLAGS) {
6872 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
6873 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
6875 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
6876 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
6877 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
6879 if (reflags && SvOK(reflags)) {
6880 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
6886 assert(SvPOK(PL_regex_pad[0]));
6887 if (SvCUR(PL_regex_pad[0])) {
6888 /* Pop off the "packed" IV from the end. */
6889 SV *const repointer_list = PL_regex_pad[0];
6890 const char *p = SvEND(repointer_list) - sizeof(IV);
6891 const IV offset = *((IV*)p);
6893 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
6895 SvEND_set(repointer_list, p);
6897 pmop->op_pmoffset = offset;
6898 /* This slot should be free, so assert this: */
6899 assert(PL_regex_pad[offset] == &PL_sv_undef);
6901 SV * const repointer = &PL_sv_undef;
6902 av_push(PL_regex_padav, repointer);
6903 pmop->op_pmoffset = av_top_index(PL_regex_padav);
6904 PL_regex_pad = AvARRAY(PL_regex_padav);
6908 return CHECKOP(type, pmop);
6916 /* Any pad names in scope are potentially lvalues. */
6917 for (; i < PadnamelistMAXNAMED(PL_comppad_name); i++) {
6918 PADNAME *pn = PAD_COMPNAME_SV(i);
6919 if (!pn || !PadnameLEN(pn))
6921 if (PadnameOUTER(pn) || PadnameIN_SCOPE(pn, PL_cop_seqmax))
6922 S_mark_padname_lvalue(aTHX_ pn);
6926 /* Given some sort of match op o, and an expression expr containing a
6927 * pattern, either compile expr into a regex and attach it to o (if it's
6928 * constant), or convert expr into a runtime regcomp op sequence (if it's
6931 * Flags currently has 2 bits of meaning:
6932 * 1: isreg indicates that the pattern is part of a regex construct, eg
6933 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
6934 * split "pattern", which aren't. In the former case, expr will be a list
6935 * if the pattern contains more than one term (eg /a$b/).
6936 * 2: The pattern is for a split.
6938 * When the pattern has been compiled within a new anon CV (for
6939 * qr/(?{...})/ ), then floor indicates the savestack level just before
6940 * the new sub was created
6942 * tr/// is also handled.
6946 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl, UV flags, I32 floor)
6950 I32 repl_has_vars = 0;
6951 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
6952 bool is_compiletime;
6954 bool isreg = cBOOL(flags & 1);
6955 bool is_split = cBOOL(flags & 2);
6957 PERL_ARGS_ASSERT_PMRUNTIME;
6960 return pmtrans(o, expr, repl);
6963 /* find whether we have any runtime or code elements;
6964 * at the same time, temporarily set the op_next of each DO block;
6965 * then when we LINKLIST, this will cause the DO blocks to be excluded
6966 * from the op_next chain (and from having LINKLIST recursively
6967 * applied to them). We fix up the DOs specially later */
6971 if (expr->op_type == OP_LIST) {
6973 for (child = cLISTOPx(expr)->op_first; child; child = OpSIBLING(child)) {
6974 if (child->op_type == OP_NULL && (child->op_flags & OPf_SPECIAL)) {
6976 assert(!child->op_next);
6977 if (UNLIKELY(!OpHAS_SIBLING(child))) {
6978 assert(PL_parser && PL_parser->error_count);
6979 /* This can happen with qr/ (?{(^{})/. Just fake up
6980 the op we were expecting to see, to avoid crashing
6982 op_sibling_splice(expr, child, 0,
6983 newSVOP(OP_CONST, 0, &PL_sv_no));
6985 child->op_next = OpSIBLING(child);
6987 else if (child->op_type != OP_CONST && child->op_type != OP_PUSHMARK)
6991 else if (expr->op_type != OP_CONST)
6996 /* fix up DO blocks; treat each one as a separate little sub;
6997 * also, mark any arrays as LIST/REF */
6999 if (expr->op_type == OP_LIST) {
7001 for (child = cLISTOPx(expr)->op_first; child; child = OpSIBLING(child)) {
7003 if (child->op_type == OP_PADAV || child->op_type == OP_RV2AV) {
7004 assert( !(child->op_flags & OPf_WANT));
7005 /* push the array rather than its contents. The regex
7006 * engine will retrieve and join the elements later */
7007 child->op_flags |= (OPf_WANT_LIST | OPf_REF);
7011 if (!(child->op_type == OP_NULL && (child->op_flags & OPf_SPECIAL)))
7013 child->op_next = NULL; /* undo temporary hack from above */
7016 if (cLISTOPx(child)->op_first->op_type == OP_LEAVE) {
7017 LISTOP *leaveop = cLISTOPx(cLISTOPx(child)->op_first);
7019 assert(leaveop->op_first->op_type == OP_ENTER);
7020 assert(OpHAS_SIBLING(leaveop->op_first));
7021 child->op_next = OpSIBLING(leaveop->op_first);
7023 assert(leaveop->op_flags & OPf_KIDS);
7024 assert(leaveop->op_last->op_next == (OP*)leaveop);
7025 leaveop->op_next = NULL; /* stop on last op */
7026 op_null((OP*)leaveop);
7030 OP *scope = cLISTOPx(child)->op_first;
7031 assert(scope->op_type == OP_SCOPE);
7032 assert(scope->op_flags & OPf_KIDS);
7033 scope->op_next = NULL; /* stop on last op */
7037 /* XXX optimize_optree() must be called on o before
7038 * CALL_PEEP(), as currently S_maybe_multiconcat() can't
7039 * currently cope with a peephole-optimised optree.
7040 * Calling optimize_optree() here ensures that condition
7041 * is met, but may mean optimize_optree() is applied
7042 * to the same optree later (where hopefully it won't do any
7043 * harm as it can't convert an op to multiconcat if it's
7044 * already been converted */
7045 optimize_optree(child);
7047 /* have to peep the DOs individually as we've removed it from
7048 * the op_next chain */
7050 op_prune_chain_head(&(child->op_next));
7052 /* runtime finalizes as part of finalizing whole tree */
7053 finalize_optree(child);
7056 else if (expr->op_type == OP_PADAV || expr->op_type == OP_RV2AV) {
7057 assert( !(expr->op_flags & OPf_WANT));
7058 /* push the array rather than its contents. The regex
7059 * engine will retrieve and join the elements later */
7060 expr->op_flags |= (OPf_WANT_LIST | OPf_REF);
7063 PL_hints |= HINT_BLOCK_SCOPE;
7065 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
7067 if (is_compiletime) {
7068 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
7069 regexp_engine const *eng = current_re_engine();
7072 /* make engine handle split ' ' specially */
7073 pm->op_pmflags |= PMf_SPLIT;
7074 rx_flags |= RXf_SPLIT;
7077 if (!has_code || !eng->op_comp) {
7078 /* compile-time simple constant pattern */
7080 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
7081 /* whoops! we guessed that a qr// had a code block, but we
7082 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
7083 * that isn't required now. Note that we have to be pretty
7084 * confident that nothing used that CV's pad while the
7085 * regex was parsed, except maybe op targets for \Q etc.
7086 * If there were any op targets, though, they should have
7087 * been stolen by constant folding.
7091 assert(PadnamelistMAXNAMED(PL_comppad_name) == 0);
7092 while (++i <= AvFILLp(PL_comppad)) {
7093 # ifdef USE_PAD_RESET
7094 /* under USE_PAD_RESET, pad swipe replaces a swiped
7095 * folded constant with a fresh padtmp */
7096 assert(!PL_curpad[i] || SvPADTMP(PL_curpad[i]));
7098 assert(!PL_curpad[i]);
7102 /* This LEAVE_SCOPE will restore PL_compcv to point to the
7103 * outer CV (the one whose slab holds the pm op). The
7104 * inner CV (which holds expr) will be freed later, once
7105 * all the entries on the parse stack have been popped on
7106 * return from this function. Which is why its safe to
7107 * call op_free(expr) below.
7110 pm->op_pmflags &= ~PMf_HAS_CV;
7113 /* Skip compiling if parser found an error for this pattern */
7114 if (pm->op_pmflags & PMf_HAS_ERROR) {
7120 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
7121 rx_flags, pm->op_pmflags)
7122 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
7123 rx_flags, pm->op_pmflags)
7128 /* compile-time pattern that includes literal code blocks */
7132 /* Skip compiling if parser found an error for this pattern */
7133 if (pm->op_pmflags & PMf_HAS_ERROR) {
7137 re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
7140 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
7143 if (pm->op_pmflags & PMf_HAS_CV) {
7145 /* this QR op (and the anon sub we embed it in) is never
7146 * actually executed. It's just a placeholder where we can
7147 * squirrel away expr in op_code_list without the peephole
7148 * optimiser etc processing it for a second time */
7149 OP *qr = newPMOP(OP_QR, 0);
7150 cPMOPx(qr)->op_code_list = expr;
7152 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
7153 SvREFCNT_inc_simple_void(PL_compcv);
7154 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
7155 ReANY(re)->qr_anoncv = cv;
7157 /* attach the anon CV to the pad so that
7158 * pad_fixup_inner_anons() can find it */
7159 (void)pad_add_anon(cv, o->op_type);
7160 SvREFCNT_inc_simple_void(cv);
7163 pm->op_code_list = expr;
7168 /* runtime pattern: build chain of regcomp etc ops */
7170 PADOFFSET cv_targ = 0;
7172 reglist = isreg && expr->op_type == OP_LIST;
7177 pm->op_code_list = expr;
7178 /* don't free op_code_list; its ops are embedded elsewhere too */
7179 pm->op_pmflags |= PMf_CODELIST_PRIVATE;
7183 /* make engine handle split ' ' specially */
7184 pm->op_pmflags |= PMf_SPLIT;
7186 /* the OP_REGCMAYBE is a placeholder in the non-threaded case
7187 * to allow its op_next to be pointed past the regcomp and
7188 * preceding stacking ops;
7189 * OP_REGCRESET is there to reset taint before executing the
7191 if (pm->op_pmflags & PMf_KEEP || TAINTING_get)
7192 expr = newUNOP((TAINTING_get ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
7194 if (pm->op_pmflags & PMf_HAS_CV) {
7195 /* we have a runtime qr with literal code. This means
7196 * that the qr// has been wrapped in a new CV, which
7197 * means that runtime consts, vars etc will have been compiled
7198 * against a new pad. So... we need to execute those ops
7199 * within the environment of the new CV. So wrap them in a call
7200 * to a new anon sub. i.e. for
7204 * we build an anon sub that looks like
7206 * sub { "a", $b, '(?{...})' }
7208 * and call it, passing the returned list to regcomp.
7209 * Or to put it another way, the list of ops that get executed
7213 * ------ -------------------
7214 * pushmark (for regcomp)
7215 * pushmark (for entersub)
7219 * regcreset regcreset
7221 * const("a") const("a")
7223 * const("(?{...})") const("(?{...})")
7228 SvREFCNT_inc_simple_void(PL_compcv);
7229 CvLVALUE_on(PL_compcv);
7230 /* these lines are just an unrolled newANONATTRSUB */
7231 expr = newSVOP(OP_ANONCODE, 0,
7232 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
7233 cv_targ = expr->op_targ;
7234 expr = newUNOP(OP_REFGEN, 0, expr);
7236 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr)), TRUE));
7239 rcop = alloc_LOGOP(OP_REGCOMP, scalar(expr), o);
7240 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
7241 | (reglist ? OPf_STACKED : 0);
7242 rcop->op_targ = cv_targ;
7244 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
7245 if (PL_hints & HINT_RE_EVAL)
7246 S_set_haseval(aTHX);
7248 /* establish postfix order */
7249 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
7251 rcop->op_next = expr;
7252 cUNOPx(expr)->op_first->op_next = (OP*)rcop;
7255 rcop->op_next = LINKLIST(expr);
7256 expr->op_next = (OP*)rcop;
7259 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
7265 /* If we are looking at s//.../e with a single statement, get past
7266 the implicit do{}. */
7267 if (curop->op_type == OP_NULL && curop->op_flags & OPf_KIDS
7268 && cUNOPx(curop)->op_first->op_type == OP_SCOPE
7269 && cUNOPx(curop)->op_first->op_flags & OPf_KIDS)
7272 OP *kid = cUNOPx(cUNOPx(curop)->op_first)->op_first;
7273 if (kid->op_type == OP_NULL && (sib = OpSIBLING(kid))
7274 && !OpHAS_SIBLING(sib))
7277 if (curop->op_type == OP_CONST)
7279 else if (( (curop->op_type == OP_RV2SV ||
7280 curop->op_type == OP_RV2AV ||
7281 curop->op_type == OP_RV2HV ||
7282 curop->op_type == OP_RV2GV)
7283 && cUNOPx(curop)->op_first
7284 && cUNOPx(curop)->op_first->op_type == OP_GV )
7285 || curop->op_type == OP_PADSV
7286 || curop->op_type == OP_PADAV
7287 || curop->op_type == OP_PADHV
7288 || curop->op_type == OP_PADANY) {
7296 || !RX_PRELEN(PM_GETRE(pm))
7297 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
7299 pm->op_pmflags |= PMf_CONST; /* const for long enough */
7300 op_prepend_elem(o->op_type, scalar(repl), o);
7303 rcop = alloc_LOGOP(OP_SUBSTCONT, scalar(repl), o);
7304 rcop->op_private = 1;
7306 /* establish postfix order */
7307 rcop->op_next = LINKLIST(repl);
7308 repl->op_next = (OP*)rcop;
7310 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
7311 assert(!(pm->op_pmflags & PMf_ONCE));
7312 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
7323 Constructs, checks, and returns an op of any type that involves an
7324 embedded SV. C<type> is the opcode. C<flags> gives the eight bits
7325 of C<op_flags>. C<sv> gives the SV to embed in the op; this function
7326 takes ownership of one reference to it.
7332 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
7336 PERL_ARGS_ASSERT_NEWSVOP;
7338 /* OP_RUNCV is allowed specially so rpeep has room to convert it into an
7340 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
7341 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
7342 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
7344 || type == OP_CUSTOM);
7346 NewOp(1101, svop, 1, SVOP);
7347 OpTYPE_set(svop, type);
7349 svop->op_next = (OP*)svop;
7350 svop->op_flags = (U8)flags;
7351 svop->op_private = (U8)(0 | (flags >> 8));
7352 if (PL_opargs[type] & OA_RETSCALAR)
7354 if (PL_opargs[type] & OA_TARGET)
7355 svop->op_targ = pad_alloc(type, SVs_PADTMP);
7356 return CHECKOP(type, svop);
7360 =for apidoc newDEFSVOP
7362 Constructs and returns an op to access C<$_>.
7368 Perl_newDEFSVOP(pTHX)
7370 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
7376 =for apidoc newPADOP
7378 Constructs, checks, and returns an op of any type that involves a
7379 reference to a pad element. C<type> is the opcode. C<flags> gives the
7380 eight bits of C<op_flags>. A pad slot is automatically allocated, and
7381 is populated with C<sv>; this function takes ownership of one reference
7384 This function only exists if Perl has been compiled to use ithreads.
7390 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
7394 PERL_ARGS_ASSERT_NEWPADOP;
7396 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
7397 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
7398 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
7399 || type == OP_CUSTOM);
7401 NewOp(1101, padop, 1, PADOP);
7402 OpTYPE_set(padop, type);
7404 pad_alloc(type, isGV(sv) ? SVf_READONLY : SVs_PADTMP);
7405 SvREFCNT_dec(PAD_SVl(padop->op_padix));
7406 PAD_SETSV(padop->op_padix, sv);
7408 padop->op_next = (OP*)padop;
7409 padop->op_flags = (U8)flags;
7410 if (PL_opargs[type] & OA_RETSCALAR)
7412 if (PL_opargs[type] & OA_TARGET)
7413 padop->op_targ = pad_alloc(type, SVs_PADTMP);
7414 return CHECKOP(type, padop);
7417 #endif /* USE_ITHREADS */
7422 Constructs, checks, and returns an op of any type that involves an
7423 embedded reference to a GV. C<type> is the opcode. C<flags> gives the
7424 eight bits of C<op_flags>. C<gv> identifies the GV that the op should
7425 reference; calling this function does not transfer ownership of any
7432 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
7434 PERL_ARGS_ASSERT_NEWGVOP;
7437 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
7439 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
7446 Constructs, checks, and returns an op of any type that involves an
7447 embedded C-level pointer (PV). C<type> is the opcode. C<flags> gives
7448 the eight bits of C<op_flags>. C<pv> supplies the C-level pointer.
7449 Depending on the op type, the memory referenced by C<pv> may be freed
7450 when the op is destroyed. If the op is of a freeing type, C<pv> must
7451 have been allocated using C<PerlMemShared_malloc>.
7457 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
7459 const bool utf8 = cBOOL(flags & SVf_UTF8);
7464 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
7465 || type == OP_CUSTOM
7466 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
7468 NewOp(1101, pvop, 1, PVOP);
7469 OpTYPE_set(pvop, type);
7471 pvop->op_next = (OP*)pvop;
7472 pvop->op_flags = (U8)flags;
7473 pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0;
7474 if (PL_opargs[type] & OA_RETSCALAR)
7476 if (PL_opargs[type] & OA_TARGET)
7477 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
7478 return CHECKOP(type, pvop);
7482 Perl_package(pTHX_ OP *o)
7484 SV *const sv = cSVOPo->op_sv;
7486 PERL_ARGS_ASSERT_PACKAGE;
7488 SAVEGENERICSV(PL_curstash);
7489 save_item(PL_curstname);
7491 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD));
7493 sv_setsv(PL_curstname, sv);
7495 PL_hints |= HINT_BLOCK_SCOPE;
7496 PL_parser->copline = NOLINE;
7502 Perl_package_version( pTHX_ OP *v )
7504 U32 savehints = PL_hints;
7505 PERL_ARGS_ASSERT_PACKAGE_VERSION;
7506 PL_hints &= ~HINT_STRICT_VARS;
7507 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
7508 PL_hints = savehints;
7512 /* Extract the first two components of a "version" object as two 8bit integers
7513 * and return them packed into a single U16 in the format of PL_prevailing_version.
7514 * This function only ever has to cope with version objects already known
7515 * bounded by the current perl version, so we know its components will fit
7516 * (Up until we reach perl version 5.256 anyway) */
7517 static U16 S_extract_shortver(pTHX_ SV *sv)
7520 if(!SvRV(sv) || !SvOBJECT(rv = SvRV(sv)) || !sv_derived_from(sv, "version"))
7523 AV *av = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(rv), "version", 0)));
7527 IV major = av_count(av) > 0 ? SvIV(*av_fetch(av, 0, false)) : 0;
7529 shortver |= 255 << 8;
7531 shortver |= major << 8;
7533 IV minor = av_count(av) > 1 ? SvIV(*av_fetch(av, 1, false)) : 0;
7541 #define SHORTVER(maj,min) ((maj << 8) | min)
7544 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
7549 SV *use_version = NULL;
7551 PERL_ARGS_ASSERT_UTILIZE;
7553 if (idop->op_type != OP_CONST)
7554 Perl_croak(aTHX_ "Module name must be constant");
7559 SV * const vesv = cSVOPx(version)->op_sv;
7561 if (!arg && !SvNIOKp(vesv)) {
7568 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
7569 Perl_croak(aTHX_ "Version number must be a constant number");
7571 /* Make copy of idop so we don't free it twice */
7572 pack = newSVOP(OP_CONST, 0, newSVsv(cSVOPx(idop)->op_sv));
7574 /* Fake up a method call to VERSION */
7575 meth = newSVpvs_share("VERSION");
7576 veop = op_convert_list(OP_ENTERSUB, OPf_STACKED,
7577 op_append_elem(OP_LIST,
7578 op_prepend_elem(OP_LIST, pack, version),
7579 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
7583 /* Fake up an import/unimport */
7584 if (arg && arg->op_type == OP_STUB) {
7585 imop = arg; /* no import on explicit () */
7587 else if (SvNIOKp(cSVOPx(idop)->op_sv)) {
7588 imop = NULL; /* use 5.0; */
7590 use_version = cSVOPx(idop)->op_sv;
7592 idop->op_private |= OPpCONST_NOVER;
7597 /* Make copy of idop so we don't free it twice */
7598 pack = newSVOP(OP_CONST, 0, newSVsv(cSVOPx(idop)->op_sv));
7600 /* Fake up a method call to import/unimport */
7602 ? newSVpvs_share("import") : newSVpvs_share("unimport");
7603 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED,
7604 op_append_elem(OP_LIST,
7605 op_prepend_elem(OP_LIST, pack, arg),
7606 newMETHOP_named(OP_METHOD_NAMED, 0, meth)
7610 /* Fake up the BEGIN {}, which does its thing immediately. */
7612 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
7615 op_append_elem(OP_LINESEQ,
7616 op_append_elem(OP_LINESEQ,
7617 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
7618 newSTATEOP(0, NULL, veop)),
7619 newSTATEOP(0, NULL, imop) ));
7623 * feature bundle that corresponds to the required version. */
7624 use_version = sv_2mortal(new_version(use_version));
7625 S_enable_feature_bundle(aTHX_ use_version);
7627 U16 shortver = S_extract_shortver(aTHX_ use_version);
7629 /* If a version >= 5.11.0 is requested, strictures are on by default! */
7630 if (shortver >= SHORTVER(5, 11)) {
7631 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
7632 PL_hints |= HINT_STRICT_REFS;
7633 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
7634 PL_hints |= HINT_STRICT_SUBS;
7635 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
7636 PL_hints |= HINT_STRICT_VARS;
7638 if (shortver >= SHORTVER(5, 35))
7639 free_and_set_cop_warnings(&PL_compiling, pWARN_ALL);
7641 /* otherwise they are off */
7643 if(PL_prevailing_version >= SHORTVER(5, 11))
7644 deprecate_fatal_in("5.40",
7645 "Downgrading a use VERSION declaration to below v5.11");
7647 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
7648 PL_hints &= ~HINT_STRICT_REFS;
7649 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
7650 PL_hints &= ~HINT_STRICT_SUBS;
7651 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
7652 PL_hints &= ~HINT_STRICT_VARS;
7655 PL_prevailing_version = shortver;
7658 /* The "did you use incorrect case?" warning used to be here.
7659 * The problem is that on case-insensitive filesystems one
7660 * might get false positives for "use" (and "require"):
7661 * "use Strict" or "require CARP" will work. This causes
7662 * portability problems for the script: in case-strict
7663 * filesystems the script will stop working.
7665 * The "incorrect case" warning checked whether "use Foo"
7666 * imported "Foo" to your namespace, but that is wrong, too:
7667 * there is no requirement nor promise in the language that
7668 * a Foo.pm should or would contain anything in package "Foo".
7670 * There is very little Configure-wise that can be done, either:
7671 * the case-sensitivity of the build filesystem of Perl does not
7672 * help in guessing the case-sensitivity of the runtime environment.
7675 PL_hints |= HINT_BLOCK_SCOPE;
7676 PL_parser->copline = NOLINE;
7677 COP_SEQMAX_INC; /* Purely for B::*'s benefit */
7681 =for apidoc_section $embedding
7683 =for apidoc load_module
7684 =for apidoc_item load_module_nocontext
7686 These load the module whose name is pointed to by the string part of C<name>.
7687 Note that the actual module name, not its filename, should be given.
7688 Eg, "Foo::Bar" instead of "Foo/Bar.pm". ver, if specified and not NULL,
7689 provides version semantics similar to C<use Foo::Bar VERSION>. The optional
7690 trailing arguments can be used to specify arguments to the module's C<import()>
7691 method, similar to C<use Foo::Bar VERSION LIST>; their precise handling depends
7692 on the flags. The flags argument is a bitwise-ORed collection of any of
7693 C<PERL_LOADMOD_DENY>, C<PERL_LOADMOD_NOIMPORT>, or C<PERL_LOADMOD_IMPORT_OPS>
7694 (or 0 for no flags).
7696 If C<PERL_LOADMOD_NOIMPORT> is set, the module is loaded as if with an empty
7697 import list, as in C<use Foo::Bar ()>; this is the only circumstance in which
7698 the trailing optional arguments may be omitted entirely. Otherwise, if
7699 C<PERL_LOADMOD_IMPORT_OPS> is set, the trailing arguments must consist of
7700 exactly one C<OP*>, containing the op tree that produces the relevant import
7701 arguments. Otherwise, the trailing arguments must all be C<SV*> values that
7702 will be used as import arguments; and the list must be terminated with C<(SV*)
7703 NULL>. If neither C<PERL_LOADMOD_NOIMPORT> nor C<PERL_LOADMOD_IMPORT_OPS> is
7704 set, the trailing C<NULL> pointer is needed even if no import arguments are
7705 desired. The reference count for each specified C<SV*> argument is
7706 decremented. In addition, the C<name> argument is modified.
7708 If C<PERL_LOADMOD_DENY> is set, the module is loaded as if with C<no> rather
7711 C<load_module> and C<load_module_nocontext> have the same apparent signature,
7712 but the former hides the fact that it is accessing a thread context parameter.
7713 So use the latter when you get a compilation error about C<pTHX>.
7715 =for apidoc Amnh||PERL_LOADMOD_DENY
7716 =for apidoc Amnh||PERL_LOADMOD_NOIMPORT
7717 =for apidoc Amnh||PERL_LOADMOD_IMPORT_OPS
7719 =for apidoc vload_module
7720 Like C<L</load_module>> but the arguments are an encapsulated argument list.
7725 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
7729 PERL_ARGS_ASSERT_LOAD_MODULE;
7731 va_start(args, ver);
7732 vload_module(flags, name, ver, &args);
7738 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
7742 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
7743 va_start(args, ver);
7744 vload_module(flags, name, ver, &args);
7750 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
7756 PERL_ARGS_ASSERT_VLOAD_MODULE;
7758 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
7759 * that it has a PL_parser to play with while doing that, and also
7760 * that it doesn't mess with any existing parser, by creating a tmp
7761 * new parser with lex_start(). This won't actually be used for much,
7762 * since pp_require() will create another parser for the real work.
7763 * The ENTER/LEAVE pair protect callers from any side effects of use.
7765 * start_subparse() creates a new PL_compcv. This means that any ops
7766 * allocated below will be allocated from that CV's op slab, and so
7767 * will be automatically freed if the utilise() fails
7771 SAVEVPTR(PL_curcop);
7772 lex_start(NULL, NULL, LEX_START_SAME_FILTER);
7773 floor = start_subparse(FALSE, 0);
7775 modname = newSVOP(OP_CONST, 0, name);
7776 modname->op_private |= OPpCONST_BARE;
7778 veop = newSVOP(OP_CONST, 0, ver);
7782 if (flags & PERL_LOADMOD_NOIMPORT) {
7783 imop = sawparens(newNULLLIST());
7785 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
7786 imop = va_arg(*args, OP*);
7791 sv = va_arg(*args, SV*);
7793 imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
7794 sv = va_arg(*args, SV*);
7798 utilize(!(flags & PERL_LOADMOD_DENY), floor, veop, modname, imop);
7802 PERL_STATIC_INLINE OP *
7803 S_new_entersubop(pTHX_ GV *gv, OP *arg)
7805 return newUNOP(OP_ENTERSUB, OPf_STACKED,
7806 newLISTOP(OP_LIST, 0, arg,
7807 newUNOP(OP_RV2CV, 0,
7808 newGVOP(OP_GV, 0, gv))));
7812 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
7817 PERL_ARGS_ASSERT_DOFILE;
7819 if (!force_builtin && (gv = gv_override("do", 2))) {
7820 doop = S_new_entersubop(aTHX_ gv, term);
7823 doop = newUNOP(OP_DOFILE, 0, scalar(term));
7829 =for apidoc_section $optree_construction
7831 =for apidoc newSLICEOP
7833 Constructs, checks, and returns an C<lslice> (list slice) op. C<flags>
7834 gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will
7835 be set automatically, and, shifted up eight bits, the eight bits of
7836 C<op_private>, except that the bit with value 1 or 2 is automatically
7837 set as required. C<listval> and C<subscript> supply the parameters of
7838 the slice; they are consumed by this function and become part of the
7839 constructed op tree.
7845 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
7847 return newBINOP(OP_LSLICE, flags,
7848 list(force_list(subscript, TRUE)),
7849 list(force_list(listval, TRUE)));
7852 #define ASSIGN_SCALAR 0
7853 #define ASSIGN_LIST 1
7854 #define ASSIGN_REF 2
7856 /* given the optree o on the LHS of an assignment, determine whether its:
7857 * ASSIGN_SCALAR $x = ...
7858 * ASSIGN_LIST ($x) = ...
7859 * ASSIGN_REF \$x = ...
7863 S_assignment_type(pTHX_ const OP *o)
7872 if (o->op_type == OP_SREFGEN)
7874 OP * const kid = cUNOPx(cUNOPo->op_first)->op_first;
7875 type = kid->op_type;
7876 flags = o->op_flags | kid->op_flags;
7877 if (!(flags & OPf_PARENS)
7878 && (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV ||
7879 kid->op_type == OP_RV2HV || kid->op_type == OP_PADHV ))
7883 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
7884 o = cUNOPo->op_first;
7885 flags = o->op_flags;
7887 ret = ASSIGN_SCALAR;
7890 if (type == OP_COND_EXPR) {
7891 OP * const sib = OpSIBLING(cLOGOPo->op_first);
7892 const I32 t = assignment_type(sib);
7893 const I32 f = assignment_type(OpSIBLING(sib));
7895 if (t == ASSIGN_LIST && f == ASSIGN_LIST)
7897 if ((t == ASSIGN_LIST) ^ (f == ASSIGN_LIST))
7898 yyerror("Assignment to both a list and a scalar");
7899 return ASSIGN_SCALAR;
7902 if (type == OP_LIST &&
7903 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
7904 o->op_private & OPpLVAL_INTRO)
7907 if (type == OP_LIST || flags & OPf_PARENS ||
7908 type == OP_RV2AV || type == OP_RV2HV ||
7909 type == OP_ASLICE || type == OP_HSLICE ||
7910 type == OP_KVASLICE || type == OP_KVHSLICE || type == OP_REFGEN)
7913 if (type == OP_PADAV || type == OP_PADHV)
7916 if (type == OP_RV2SV)
7923 S_newONCEOP(pTHX_ OP *initop, OP *padop)
7925 const PADOFFSET target = padop->op_targ;
7926 OP *const other = newOP(OP_PADSV,
7928 | ((padop->op_private & ~OPpLVAL_INTRO) << 8));
7929 OP *const first = newOP(OP_NULL, 0);
7930 OP *const nullop = newCONDOP(0, first, initop, other);
7931 /* XXX targlex disabled for now; see ticket #124160
7932 newCONDOP(0, first, S_maybe_targlex(aTHX_ initop), other);
7934 OP *const condop = first->op_next;
7936 OpTYPE_set(condop, OP_ONCE);
7937 other->op_targ = target;
7938 nullop->op_flags |= OPf_WANT_SCALAR;
7940 /* Store the initializedness of state vars in a separate
7943 pad_add_name_pvn("$",1,padadd_NO_DUP_CHECK|padadd_STATE,0,0);
7944 /* hijacking PADSTALE for uninitialized state variables */
7945 SvPADSTALE_on(PAD_SVl(condop->op_targ));
7951 =for apidoc newASSIGNOP
7953 Constructs, checks, and returns an assignment op. C<left> and C<right>
7954 supply the parameters of the assignment; they are consumed by this
7955 function and become part of the constructed op tree.
7957 If C<optype> is C<OP_ANDASSIGN>, C<OP_ORASSIGN>, or C<OP_DORASSIGN>, then
7958 a suitable conditional optree is constructed. If C<optype> is the opcode
7959 of a binary operator, such as C<OP_BIT_OR>, then an op is constructed that
7960 performs the binary operation and assigns the result to the left argument.
7961 Either way, if C<optype> is non-zero then C<flags> has no effect.
7963 If C<optype> is zero, then a plain scalar or list assignment is
7964 constructed. Which type of assignment it is is automatically determined.
7965 C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
7966 will be set automatically, and, shifted up eight bits, the eight bits
7967 of C<op_private>, except that the bit with value 1 or 2 is automatically
7974 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
7984 right = scalar(right);
7985 return newLOGOP(optype, 0,
7986 op_lvalue(scalar(left), optype),
7987 newBINOP(OP_SASSIGN, OPpASSIGN_BACKWARDS<<8, right, right));
7989 return newBINOP(optype, OPf_STACKED,
7990 op_lvalue(scalar(left), optype), scalar(right));
7993 if ((assign_type = assignment_type(left)) == ASSIGN_LIST) {
7994 OP *state_var_op = NULL;
7995 static const char no_list_state[] = "Initialization of state variables"
7996 " in list currently forbidden";
7999 if (left->op_type == OP_ASLICE || left->op_type == OP_HSLICE)
8000 left->op_private &= ~ OPpSLICEWARNING;
8003 left = op_lvalue(left, OP_AASSIGN);
8004 curop = list(force_list(left, TRUE));
8005 o = newBINOP(OP_AASSIGN, flags, list(force_list(right, TRUE)), curop);
8006 o->op_private = (U8)(0 | (flags >> 8));
8008 if (OP_TYPE_IS_OR_WAS(left, OP_LIST))
8010 OP *lop = cLISTOPx(left)->op_first, *vop, *eop;
8011 if (!(left->op_flags & OPf_PARENS) &&
8012 lop->op_type == OP_PUSHMARK &&
8013 (vop = OpSIBLING(lop)) &&
8014 (vop->op_type == OP_PADAV || vop->op_type == OP_PADHV) &&
8015 !(vop->op_flags & OPf_PARENS) &&
8016 (vop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) ==
8017 (OPpLVAL_INTRO|OPpPAD_STATE) &&
8018 (eop = OpSIBLING(vop)) &&
8019 eop->op_type == OP_ENTERSUB &&
8020 !OpHAS_SIBLING(eop)) {
8024 if ((lop->op_type == OP_PADSV ||
8025 lop->op_type == OP_PADAV ||
8026 lop->op_type == OP_PADHV ||
8027 lop->op_type == OP_PADANY)
8028 && (lop->op_private & OPpPAD_STATE)
8030 yyerror(no_list_state);
8031 lop = OpSIBLING(lop);
8035 else if ( (left->op_private & OPpLVAL_INTRO)
8036 && (left->op_private & OPpPAD_STATE)
8037 && ( left->op_type == OP_PADSV
8038 || left->op_type == OP_PADAV
8039 || left->op_type == OP_PADHV
8040 || left->op_type == OP_PADANY)
8042 /* All single variable list context state assignments, hence
8052 if (left->op_flags & OPf_PARENS)
8053 yyerror(no_list_state);
8055 state_var_op = left;
8058 /* optimise @a = split(...) into:
8059 * @{expr}: split(..., @{expr}) (where @a is not flattened)
8060 * @a, my @a, local @a: split(...) (where @a is attached to
8061 * the split op itself)
8065 && right->op_type == OP_SPLIT
8066 /* don't do twice, e.g. @b = (@a = split) */
8067 && !(right->op_private & OPpSPLIT_ASSIGN))
8071 if ( ( left->op_type == OP_RV2AV
8072 && (gvop=cUNOPx(left)->op_first)->op_type==OP_GV)
8073 || left->op_type == OP_PADAV)
8075 /* @pkg or @lex or local @pkg' or 'my @lex' */
8079 cPMOPx(right)->op_pmreplrootu.op_pmtargetoff
8080 = cPADOPx(gvop)->op_padix;
8081 cPADOPx(gvop)->op_padix = 0; /* steal it */
8083 cPMOPx(right)->op_pmreplrootu.op_pmtargetgv
8084 = MUTABLE_GV(cSVOPx(gvop)->op_sv);
8085 cSVOPx(gvop)->op_sv = NULL; /* steal it */
8087 right->op_private |=
8088 left->op_private & OPpOUR_INTRO;
8091 cPMOPx(right)->op_pmreplrootu.op_pmtargetoff = left->op_targ;
8092 left->op_targ = 0; /* steal it */
8093 right->op_private |= OPpSPLIT_LEX;
8095 right->op_private |= left->op_private & OPpLVAL_INTRO;
8098 tmpop = cUNOPo->op_first; /* to list (nulled) */
8099 tmpop = cUNOPx(tmpop)->op_first; /* to pushmark */
8100 assert(OpSIBLING(tmpop) == right);
8101 assert(!OpHAS_SIBLING(right));
8102 /* detach the split subtreee from the o tree,
8103 * then free the residual o tree */
8104 op_sibling_splice(cUNOPo->op_first, tmpop, 1, NULL);
8105 op_free(o); /* blow off assign */
8106 right->op_private |= OPpSPLIT_ASSIGN;
8107 right->op_flags &= ~OPf_WANT;
8108 /* "I don't know and I don't care." */
8111 else if (left->op_type == OP_RV2AV) {
8114 OP *pushop = cUNOPx(cBINOPo->op_last)->op_first;
8115 assert(OpSIBLING(pushop) == left);
8116 /* Detach the array ... */
8117 op_sibling_splice(cBINOPo->op_last, pushop, 1, NULL);
8118 /* ... and attach it to the split. */
8119 op_sibling_splice(right, cLISTOPx(right)->op_last,
8121 right->op_flags |= OPf_STACKED;
8122 /* Detach split and expunge aassign as above. */
8125 else if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
8126 cLISTOPx(right)->op_last->op_type == OP_CONST)
8128 /* convert split(...,0) to split(..., PL_modcount+1) */
8130 &cSVOPx(cLISTOPx(right)->op_last)->op_sv;
8131 SV * const sv = *svp;
8132 if (SvIOK(sv) && SvIVX(sv) == 0)
8134 if (right->op_private & OPpSPLIT_IMPLIM) {
8135 /* our own SV, created in ck_split */
8137 sv_setiv(sv, PL_modcount+1);
8140 /* SV may belong to someone else */
8142 *svp = newSViv(PL_modcount+1);
8149 o = S_newONCEOP(aTHX_ o, state_var_op);
8152 if (assign_type == ASSIGN_REF)
8153 return newBINOP(OP_REFASSIGN, flags, scalar(right), left);
8155 right = newOP(OP_UNDEF, 0);
8156 if (right->op_type == OP_READLINE) {
8157 right->op_flags |= OPf_STACKED;
8158 return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN),
8162 o = newBINOP(OP_SASSIGN, flags,
8163 scalar(right), op_lvalue(scalar(left), OP_SASSIGN) );
8169 =for apidoc newSTATEOP
8171 Constructs a state op (COP). The state op is normally a C<nextstate> op,
8172 but will be a C<dbstate> op if debugging is enabled for currently-compiled
8173 code. The state op is populated from C<PL_curcop> (or C<PL_compiling>).
8174 If C<label> is non-null, it supplies the name of a label to attach to
8175 the state op; this function takes ownership of the memory pointed at by
8176 C<label>, and will free it. C<flags> gives the eight bits of C<op_flags>
8179 If C<o> is null, the state op is returned. Otherwise the state op is
8180 combined with C<o> into a C<lineseq> list op, which is returned. C<o>
8181 is consumed by this function and becomes part of the returned op tree.
8187 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
8189 const U32 seq = intro_my();
8190 const U32 utf8 = flags & SVf_UTF8;
8194 PL_parser->parsed_sub = 0;
8198 NewOp(1101, cop, 1, COP);
8199 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
8200 OpTYPE_set(cop, OP_DBSTATE);
8203 OpTYPE_set(cop, OP_NEXTSTATE);
8205 cop->op_flags = (U8)flags;
8206 CopHINTS_set(cop, PL_hints);
8208 if (VMSISH_HUSHED) cop->op_private |= OPpHUSH_VMSISH;
8210 cop->op_next = (OP*)cop;
8213 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
8214 CopHINTHASH_set(cop, cophh_copy(CopHINTHASH_get(PL_curcop)));
8216 Perl_cop_store_label(aTHX_ cop, label, strlen(label), utf8);
8218 PL_hints |= HINT_BLOCK_SCOPE;
8219 /* It seems that we need to defer freeing this pointer, as other parts
8220 of the grammar end up wanting to copy it after this op has been
8225 if (PL_parser->preambling != NOLINE) {
8226 CopLINE_set(cop, PL_parser->preambling);
8227 PL_parser->copline = NOLINE;
8229 else if (PL_parser->copline == NOLINE)
8230 CopLINE_set(cop, CopLINE(PL_curcop));
8232 CopLINE_set(cop, PL_parser->copline);
8233 PL_parser->copline = NOLINE;
8236 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
8238 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
8240 CopSTASH_set(cop, PL_curstash);
8242 if (cop->op_type == OP_DBSTATE) {
8243 /* this line can have a breakpoint - store the cop in IV */
8244 AV *av = CopFILEAVx(PL_curcop);
8246 SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE);
8247 if (svp && *svp != &PL_sv_undef ) {
8248 (void)SvIOK_on(*svp);
8249 SvIV_set(*svp, PTR2IV(cop));
8254 if (flags & OPf_SPECIAL)
8256 return op_prepend_elem(OP_LINESEQ, (OP*)cop, o);
8260 =for apidoc newLOGOP
8262 Constructs, checks, and returns a logical (flow control) op. C<type>
8263 is the opcode. C<flags> gives the eight bits of C<op_flags>, except
8264 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
8265 the eight bits of C<op_private>, except that the bit with value 1 is
8266 automatically set. C<first> supplies the expression controlling the
8267 flow, and C<other> supplies the side (alternate) chain of ops; they are
8268 consumed by this function and become part of the constructed op tree.
8274 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
8276 PERL_ARGS_ASSERT_NEWLOGOP;
8278 return new_logop(type, flags, &first, &other);
8282 /* See if the optree o contains a single OP_CONST (plus possibly
8283 * surrounding enter/nextstate/null etc). If so, return it, else return
8288 S_search_const(pTHX_ OP *o)
8290 PERL_ARGS_ASSERT_SEARCH_CONST;
8293 switch (o->op_type) {
8297 if (o->op_flags & OPf_KIDS) {
8298 o = cUNOPo->op_first;
8307 if (!(o->op_flags & OPf_KIDS))
8309 kid = cLISTOPo->op_first;
8312 switch (kid->op_type) {
8316 kid = OpSIBLING(kid);
8319 if (kid != cLISTOPo->op_last)
8326 kid = cLISTOPo->op_last;
8338 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
8345 int prepend_not = 0;
8347 PERL_ARGS_ASSERT_NEW_LOGOP;
8352 /* [perl #59802]: Warn about things like "return $a or $b", which
8353 is parsed as "(return $a) or $b" rather than "return ($a or
8354 $b)". NB: This also applies to xor, which is why we do it
8357 switch (first->op_type) {
8361 /* XXX: Perhaps we should emit a stronger warning for these.
8362 Even with the high-precedence operator they don't seem to do
8365 But until we do, fall through here.
8371 /* XXX: Currently we allow people to "shoot themselves in the
8372 foot" by explicitly writing "(return $a) or $b".
8374 Warn unless we are looking at the result from folding or if
8375 the programmer explicitly grouped the operators like this.
8376 The former can occur with e.g.
8378 use constant FEATURE => ( $] >= ... );
8379 sub { not FEATURE and return or do_stuff(); }
8381 if (!first->op_folded && !(first->op_flags & OPf_PARENS))
8382 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
8383 "Possible precedence issue with control flow operator");
8384 /* XXX: Should we optimze this to "return $a;" (i.e. remove
8390 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
8391 return newBINOP(type, flags, scalar(first), scalar(other));
8393 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP
8394 || type == OP_CUSTOM);
8396 scalarboolean(first);
8398 /* search for a constant op that could let us fold the test */
8399 if ((cstop = search_const(first))) {
8400 if (cstop->op_private & OPpCONST_STRICT)
8401 no_bareword_allowed(cstop);
8402 else if ((cstop->op_private & OPpCONST_BARE))
8403 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
8404 if ((type == OP_AND && SvTRUE(cSVOPx(cstop)->op_sv)) ||
8405 (type == OP_OR && !SvTRUE(cSVOPx(cstop)->op_sv)) ||
8406 (type == OP_DOR && !SvOK(cSVOPx(cstop)->op_sv))) {
8407 /* Elide the (constant) lhs, since it can't affect the outcome */
8409 if (other->op_type == OP_CONST)
8410 other->op_private |= OPpCONST_SHORTCIRCUIT;
8412 if (other->op_type == OP_LEAVE)
8413 other = newUNOP(OP_NULL, OPf_SPECIAL, other);
8414 else if (other->op_type == OP_MATCH
8415 || other->op_type == OP_SUBST
8416 || other->op_type == OP_TRANSR
8417 || other->op_type == OP_TRANS)
8418 /* Mark the op as being unbindable with =~ */
8419 other->op_flags |= OPf_SPECIAL;
8421 other->op_folded = 1;
8425 /* Elide the rhs, since the outcome is entirely determined by
8426 * the (constant) lhs */
8428 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
8429 const OP *o2 = other;
8430 if ( ! (o2->op_type == OP_LIST
8431 && (( o2 = cUNOPx(o2)->op_first))
8432 && o2->op_type == OP_PUSHMARK
8433 && (( o2 = OpSIBLING(o2))) )
8436 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
8437 || o2->op_type == OP_PADHV)
8438 && o2->op_private & OPpLVAL_INTRO
8439 && !(o2->op_private & OPpPAD_STATE))
8441 Perl_croak(aTHX_ "This use of my() in false conditional is "
8442 "no longer allowed");
8446 if (cstop->op_type == OP_CONST)
8447 cstop->op_private |= OPpCONST_SHORTCIRCUIT;
8452 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
8453 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
8455 const OP * const k1 = cUNOPx(first)->op_first;
8456 const OP * const k2 = OpSIBLING(k1);
8458 switch (first->op_type)
8461 if (k2 && k2->op_type == OP_READLINE
8462 && (k2->op_flags & OPf_STACKED)
8463 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
8465 warnop = k2->op_type;
8470 if (k1->op_type == OP_READDIR
8471 || k1->op_type == OP_GLOB
8472 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
8473 || k1->op_type == OP_EACH
8474 || k1->op_type == OP_AEACH)
8476 warnop = ((k1->op_type == OP_NULL)
8477 ? (OPCODE)k1->op_targ : k1->op_type);
8482 const line_t oldline = CopLINE(PL_curcop);
8483 /* This ensures that warnings are reported at the first line
8484 of the construction, not the last. */
8485 CopLINE_set(PL_curcop, PL_parser->copline);
8486 Perl_warner(aTHX_ packWARN(WARN_MISC),
8487 "Value of %s%s can be \"0\"; test with defined()",
8489 ((warnop == OP_READLINE || warnop == OP_GLOB)
8490 ? " construct" : "() operator"));
8491 CopLINE_set(PL_curcop, oldline);
8495 /* optimize AND and OR ops that have NOTs as children */
8496 if (first->op_type == OP_NOT
8497 && (first->op_flags & OPf_KIDS)
8498 && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */
8499 || (other->op_type == OP_NOT)) /* if (!$x && !$y) { } */
8501 if (type == OP_AND || type == OP_OR) {
8507 if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */
8509 prepend_not = 1; /* prepend a NOT op later */
8514 logop = alloc_LOGOP(type, first, LINKLIST(other));
8515 logop->op_flags |= (U8)flags;
8516 logop->op_private = (U8)(1 | (flags >> 8));
8518 /* establish postfix order */
8519 logop->op_next = LINKLIST(first);
8520 first->op_next = (OP*)logop;
8521 assert(!OpHAS_SIBLING(first));
8522 op_sibling_splice((OP*)logop, first, 0, other);
8524 CHECKOP(type,logop);
8526 o = newUNOP(prepend_not ? OP_NOT : OP_NULL,
8527 PL_opargs[type] & OA_RETSCALAR ? OPf_WANT_SCALAR : 0,
8535 =for apidoc newCONDOP
8537 Constructs, checks, and returns a conditional-expression (C<cond_expr>)
8538 op. C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS>
8539 will be set automatically, and, shifted up eight bits, the eight bits of
8540 C<op_private>, except that the bit with value 1 is automatically set.
8541 C<first> supplies the expression selecting between the two branches,
8542 and C<trueop> and C<falseop> supply the branches; they are consumed by
8543 this function and become part of the constructed op tree.
8549 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
8556 PERL_ARGS_ASSERT_NEWCONDOP;
8559 return newLOGOP(OP_AND, 0, first, trueop);
8561 return newLOGOP(OP_OR, 0, first, falseop);
8563 scalarboolean(first);
8564 if ((cstop = search_const(first))) {
8565 /* Left or right arm of the conditional? */
8566 const bool left = SvTRUE(cSVOPx(cstop)->op_sv);
8567 OP *live = left ? trueop : falseop;
8568 OP *const dead = left ? falseop : trueop;
8569 if (cstop->op_private & OPpCONST_BARE &&
8570 cstop->op_private & OPpCONST_STRICT) {
8571 no_bareword_allowed(cstop);
8575 if (live->op_type == OP_LEAVE)
8576 live = newUNOP(OP_NULL, OPf_SPECIAL, live);
8577 else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST
8578 || live->op_type == OP_TRANS || live->op_type == OP_TRANSR)
8579 /* Mark the op as being unbindable with =~ */
8580 live->op_flags |= OPf_SPECIAL;
8581 live->op_folded = 1;
8584 logop = alloc_LOGOP(OP_COND_EXPR, first, LINKLIST(trueop));
8585 logop->op_flags |= (U8)flags;
8586 logop->op_private = (U8)(1 | (flags >> 8));
8587 logop->op_next = LINKLIST(falseop);
8589 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
8592 /* establish postfix order */
8593 start = LINKLIST(first);
8594 first->op_next = (OP*)logop;
8596 /* make first, trueop, falseop siblings */
8597 op_sibling_splice((OP*)logop, first, 0, trueop);
8598 op_sibling_splice((OP*)logop, trueop, 0, falseop);
8600 o = newUNOP(OP_NULL, 0, (OP*)logop);
8602 trueop->op_next = falseop->op_next = o;
8609 =for apidoc newTRYCATCHOP
8611 Constructs and returns a conditional execution statement that implements
8612 the C<try>/C<catch> semantics. First the op tree in C<tryblock> is executed,
8613 inside a context that traps exceptions. If an exception occurs then the
8614 optree in C<catchblock> is executed, with the trapped exception set into the
8615 lexical variable given by C<catchvar> (which must be an op of type
8616 C<OP_PADSV>). All the optrees are consumed by this function and become part
8617 of the returned op tree.
8619 The C<flags> argument is currently ignored.
8625 Perl_newTRYCATCHOP(pTHX_ I32 flags, OP *tryblock, OP *catchvar, OP *catchblock)
8629 PERL_ARGS_ASSERT_NEWTRYCATCHOP;
8630 assert(catchvar->op_type == OP_PADSV);
8632 PERL_UNUSED_ARG(flags);
8634 /* The returned optree is shaped as:
8635 * LISTOP leavetrycatch
8636 * LOGOP entertrycatch
8643 if(tryblock->op_type != OP_LINESEQ)
8644 tryblock = op_convert_list(OP_LINESEQ, 0, tryblock);
8645 OpTYPE_set(tryblock, OP_POPTRY);
8647 /* Manually construct a naked LOGOP.
8648 * Normally if we call newLOGOP the returned value is a UNOP(OP_NULL)
8649 * containing the LOGOP we wanted as its op_first */
8650 catchop = (OP *)alloc_LOGOP(OP_CATCH, newOP(OP_NULL, 0), catchblock);
8651 OpMORESIB_set(cUNOPx(catchop)->op_first, catchblock);
8652 OpLASTSIB_set(catchblock, catchop);
8654 /* Inject the catchvar's pad offset into the OP_CATCH targ */
8655 cLOGOPx(catchop)->op_targ = catchvar->op_targ;
8658 /* Build the optree structure */
8659 o = newLISTOP(OP_LIST, 0, tryblock, catchop);
8660 o = op_convert_list(OP_ENTERTRYCATCH, 0, o);
8666 =for apidoc newRANGE
8668 Constructs and returns a C<range> op, with subordinate C<flip> and
8669 C<flop> ops. C<flags> gives the eight bits of C<op_flags> for the
8670 C<flip> op and, shifted up eight bits, the eight bits of C<op_private>
8671 for both the C<flip> and C<range> ops, except that the bit with value
8672 1 is automatically set. C<left> and C<right> supply the expressions
8673 controlling the endpoints of the range; they are consumed by this function
8674 and become part of the constructed op tree.
8680 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
8688 PERL_ARGS_ASSERT_NEWRANGE;
8690 range = alloc_LOGOP(OP_RANGE, left, LINKLIST(right));
8691 range->op_flags = OPf_KIDS;
8692 leftstart = LINKLIST(left);
8693 range->op_private = (U8)(1 | (flags >> 8));
8695 /* make left and right siblings */
8696 op_sibling_splice((OP*)range, left, 0, right);
8698 range->op_next = (OP*)range;
8699 flip = newUNOP(OP_FLIP, flags, (OP*)range);
8700 flop = newUNOP(OP_FLOP, 0, flip);
8701 o = newUNOP(OP_NULL, 0, flop);
8703 range->op_next = leftstart;
8705 left->op_next = flip;
8706 right->op_next = flop;
8709 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);
8710 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
8712 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);;
8713 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
8714 SvPADTMP_on(PAD_SV(flip->op_targ));
8716 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
8717 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
8719 /* check barewords before they might be optimized aways */
8720 if (flip->op_private && cSVOPx(left)->op_private & OPpCONST_STRICT)
8721 no_bareword_allowed(left);
8722 if (flop->op_private && cSVOPx(right)->op_private & OPpCONST_STRICT)
8723 no_bareword_allowed(right);
8726 if (!flip->op_private || !flop->op_private)
8727 LINKLIST(o); /* blow off optimizer unless constant */
8733 =for apidoc newLOOPOP
8735 Constructs, checks, and returns an op tree expressing a loop. This is
8736 only a loop in the control flow through the op tree; it does not have
8737 the heavyweight loop structure that allows exiting the loop by C<last>
8738 and suchlike. C<flags> gives the eight bits of C<op_flags> for the
8739 top-level op, except that some bits will be set automatically as required.
8740 C<expr> supplies the expression controlling loop iteration, and C<block>
8741 supplies the body of the loop; they are consumed by this function and
8742 become part of the constructed op tree. C<debuggable> is currently
8743 unused and should always be 1.
8749 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
8753 const bool once = block && block->op_flags & OPf_SPECIAL &&
8754 block->op_type == OP_NULL;
8756 PERL_UNUSED_ARG(debuggable);
8760 (expr->op_type == OP_CONST && !SvTRUE(cSVOPx(expr)->op_sv))
8761 || ( expr->op_type == OP_NOT
8762 && cUNOPx(expr)->op_first->op_type == OP_CONST
8763 && SvTRUE(cSVOPx_sv(cUNOPx(expr)->op_first))
8766 /* Return the block now, so that S_new_logop does not try to
8770 return block; /* do {} while 0 does once */
8773 if (expr->op_type == OP_READLINE
8774 || expr->op_type == OP_READDIR
8775 || expr->op_type == OP_GLOB
8776 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
8777 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
8778 expr = newUNOP(OP_DEFINED, 0,
8779 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
8780 } else if (expr->op_flags & OPf_KIDS) {
8781 const OP * const k1 = cUNOPx(expr)->op_first;
8782 const OP * const k2 = k1 ? OpSIBLING(k1) : NULL;
8783 switch (expr->op_type) {
8785 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
8786 && (k2->op_flags & OPf_STACKED)
8787 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
8788 expr = newUNOP(OP_DEFINED, 0, expr);
8792 if (k1 && (k1->op_type == OP_READDIR
8793 || k1->op_type == OP_GLOB
8794 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
8795 || k1->op_type == OP_EACH
8796 || k1->op_type == OP_AEACH))
8797 expr = newUNOP(OP_DEFINED, 0, expr);
8803 /* if block is null, the next op_append_elem() would put UNSTACK, a scalar
8804 * op, in listop. This is wrong. [perl #27024] */
8806 block = newOP(OP_NULL, 0);
8807 listop = op_append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
8808 o = new_logop(OP_AND, 0, &expr, &listop);
8815 cLISTOPx(listop)->op_last->op_next = LINKLIST(o);
8817 if (once && o != listop)
8819 assert(cUNOPo->op_first->op_type == OP_AND
8820 || cUNOPo->op_first->op_type == OP_OR);
8821 o->op_next = cLOGOPx(cUNOPo->op_first)->op_other;
8825 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
8827 o->op_flags |= flags;
8829 o->op_flags |= OPf_SPECIAL; /* suppress cx_popblock() curpm restoration*/
8834 =for apidoc newWHILEOP
8836 Constructs, checks, and returns an op tree expressing a C<while> loop.
8837 This is a heavyweight loop, with structure that allows exiting the loop
8838 by C<last> and suchlike.
8840 C<loop> is an optional preconstructed C<enterloop> op to use in the
8841 loop; if it is null then a suitable op will be constructed automatically.
8842 C<expr> supplies the loop's controlling expression. C<block> supplies the
8843 main body of the loop, and C<cont> optionally supplies a C<continue> block
8844 that operates as a second half of the body. All of these optree inputs
8845 are consumed by this function and become part of the constructed op tree.
8847 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
8848 op and, shifted up eight bits, the eight bits of C<op_private> for
8849 the C<leaveloop> op, except that (in both cases) some bits will be set
8850 automatically. C<debuggable> is currently unused and should always be 1.
8851 C<has_my> can be supplied as true to force the
8852 loop body to be enclosed in its own scope.
8858 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop,
8859 OP *expr, OP *block, OP *cont, I32 has_my)
8867 PERL_UNUSED_ARG(debuggable);
8870 if (expr->op_type == OP_READLINE
8871 || expr->op_type == OP_READDIR
8872 || expr->op_type == OP_GLOB
8873 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH
8874 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
8875 expr = newUNOP(OP_DEFINED, 0,
8876 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
8877 } else if (expr->op_flags & OPf_KIDS) {
8878 const OP * const k1 = cUNOPx(expr)->op_first;
8879 const OP * const k2 = (k1) ? OpSIBLING(k1) : NULL;
8880 switch (expr->op_type) {
8882 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR)
8883 && (k2->op_flags & OPf_STACKED)
8884 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
8885 expr = newUNOP(OP_DEFINED, 0, expr);
8889 if (k1 && (k1->op_type == OP_READDIR
8890 || k1->op_type == OP_GLOB
8891 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
8892 || k1->op_type == OP_EACH
8893 || k1->op_type == OP_AEACH))
8894 expr = newUNOP(OP_DEFINED, 0, expr);
8901 block = newOP(OP_NULL, 0);
8902 else if (cont || has_my) {
8903 block = op_scope(block);
8907 next = LINKLIST(cont);
8910 OP * const unstack = newOP(OP_UNSTACK, 0);
8913 cont = op_append_elem(OP_LINESEQ, cont, unstack);
8917 listop = op_append_list(OP_LINESEQ, block, cont);
8919 redo = LINKLIST(listop);
8923 o = new_logop(OP_AND, 0, &expr, &listop);
8924 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
8926 return expr; /* listop already freed by new_logop */
8929 cLISTOPx(listop)->op_last->op_next =
8930 (o == listop ? redo : LINKLIST(o));
8936 NewOp(1101,loop,1,LOOP);
8937 OpTYPE_set(loop, OP_ENTERLOOP);
8938 loop->op_private = 0;
8939 loop->op_next = (OP*)loop;
8942 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
8944 loop->op_redoop = redo;
8945 loop->op_lastop = o;
8946 o->op_private |= loopflags;
8949 loop->op_nextop = next;
8951 loop->op_nextop = o;
8953 o->op_flags |= flags;
8954 o->op_private |= (flags >> 8);
8959 =for apidoc newFOROP
8961 Constructs, checks, and returns an op tree expressing a C<foreach>
8962 loop (iteration through a list of values). This is a heavyweight loop,
8963 with structure that allows exiting the loop by C<last> and suchlike.
8965 C<sv> optionally supplies the variable(s) that will be aliased to each
8966 item in turn; if null, it defaults to C<$_>.
8967 C<expr> supplies the list of values to iterate over. C<block> supplies
8968 the main body of the loop, and C<cont> optionally supplies a C<continue>
8969 block that operates as a second half of the body. All of these optree
8970 inputs are consumed by this function and become part of the constructed
8973 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop>
8974 op and, shifted up eight bits, the eight bits of C<op_private> for
8975 the C<leaveloop> op, except that (in both cases) some bits will be set
8982 Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont)
8986 PADOFFSET padoff = 0;
8987 PADOFFSET how_many_more = 0;
8992 PERL_ARGS_ASSERT_NEWFOROP;
8995 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
8996 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
8997 OpTYPE_set(sv, OP_RV2GV);
8999 /* The op_type check is needed to prevent a possible segfault
9000 * if the loop variable is undeclared and 'strict vars' is in
9001 * effect. This is illegal but is nonetheless parsed, so we
9002 * may reach this point with an OP_CONST where we're expecting
9005 if (cUNOPx(sv)->op_first->op_type == OP_GV
9006 && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
9007 iterpflags |= OPpITER_DEF;
9009 else if (sv->op_type == OP_PADSV) { /* private variable */
9010 if (sv->op_flags & OPf_PARENS) {
9011 /* handle degenerate 1-var form of "for my ($x, ...)" */
9012 sv->op_private |= OPpLVAL_INTRO;
9015 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
9016 padoff = sv->op_targ;
9020 PAD_COMPNAME_GEN_set(padoff, PERL_INT_MAX);
9022 else if (sv->op_type == OP_NULL && sv->op_targ == OP_SREFGEN)
9024 else if (sv->op_type == OP_LIST) {
9025 LISTOP *list = cLISTOPx(sv);
9026 OP *pushmark = list->op_first;
9031 iterpflags = OPpLVAL_INTRO; /* for my ($k, $v) () */
9034 if (!pushmark || pushmark->op_type != OP_PUSHMARK) {
9035 Perl_croak(aTHX_ "panic: newFORLOOP, found %s, expecting pushmark",
9036 pushmark ? PL_op_desc[pushmark->op_type] : "NULL");
9038 first_padsv = OpSIBLING(pushmark);
9039 if (!first_padsv || first_padsv->op_type != OP_PADSV) {
9040 Perl_croak(aTHX_ "panic: newFORLOOP, found %s, expecting padsv",
9041 first_padsv ? PL_op_desc[first_padsv->op_type] : "NULL");
9043 padoff = first_padsv->op_targ;
9045 /* There should be at least one more PADSV to find, and the ops
9046 should have consecutive values in targ: */
9047 padsv = cUNOPx(OpSIBLING(first_padsv));
9049 if (!padsv || padsv->op_type != OP_PADSV) {
9050 Perl_croak(aTHX_ "panic: newFORLOOP, found %s at %zd, expecting padsv",
9051 padsv ? PL_op_desc[padsv->op_type] : "NULL",
9055 if (padsv->op_targ != padoff + how_many_more) {
9056 Perl_croak(aTHX_ "panic: newFORLOOP, padsv at %zd targ is %zd, not %zd",
9057 how_many_more, padsv->op_targ, padoff + how_many_more);
9060 padsv = cUNOPx(OpSIBLING(padsv));
9063 /* OK, this optree has the shape that we expected. So now *we*
9064 "claim" the Pad slots: */
9065 first_padsv->op_targ = 0;
9066 PAD_COMPNAME_GEN_set(padoff, PERL_INT_MAX);
9070 padsv = cUNOPx(OpSIBLING(first_padsv));
9074 PAD_COMPNAME_GEN_set(i, PERL_INT_MAX);
9076 padsv = cUNOPx(OpSIBLING(padsv));
9083 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
9085 PADNAME * const pn = PAD_COMPNAME(padoff);
9086 const char * const name = PadnamePV(pn);
9088 if (PadnameLEN(pn) == 2 && name[0] == '$' && name[1] == '_')
9089 iterpflags |= OPpITER_DEF;
9093 sv = newGVOP(OP_GV, 0, PL_defgv);
9094 iterpflags |= OPpITER_DEF;
9097 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
9098 expr = op_lvalue(force_list(scalar(ref(expr, OP_ITER)), TRUE), OP_GREPSTART);
9099 iterflags |= OPf_STACKED;
9101 else if (expr->op_type == OP_NULL &&
9102 (expr->op_flags & OPf_KIDS) &&
9103 cBINOPx(expr)->op_first->op_type == OP_FLOP)
9105 /* Basically turn for($x..$y) into the same as for($x,$y), but we
9106 * set the STACKED flag to indicate that these values are to be
9107 * treated as min/max values by 'pp_enteriter'.
9109 const UNOP* const flip = cUNOPx(cUNOPx(cBINOPx(expr)->op_first)->op_first);
9110 LOGOP* const range = cLOGOPx(flip->op_first);
9111 OP* const left = range->op_first;
9112 OP* const right = OpSIBLING(left);
9115 range->op_flags &= ~OPf_KIDS;
9116 /* detach range's children */
9117 op_sibling_splice((OP*)range, NULL, -1, NULL);
9119 listop = cLISTOPx(newLISTOP(OP_LIST, 0, left, right));
9120 listop->op_first->op_next = range->op_next;
9121 left->op_next = range->op_other;
9122 right->op_next = (OP*)listop;
9123 listop->op_next = listop->op_first;
9126 expr = (OP*)(listop);
9128 iterflags |= OPf_STACKED;
9131 expr = op_lvalue(force_list(expr, TRUE), OP_GREPSTART);
9134 loop = (LOOP*)op_convert_list(OP_ENTERITER, iterflags,
9135 op_append_elem(OP_LIST, list(expr),
9137 assert(!loop->op_next);
9138 /* for my $x () sets OPpLVAL_INTRO;
9139 * for our $x () sets OPpOUR_INTRO */
9140 loop->op_private = (U8)iterpflags;
9142 /* upgrade loop from a LISTOP to a LOOPOP;
9143 * keep it in-place if there's space */
9144 if (loop->op_slabbed
9145 && OpSLOT(loop)->opslot_size
9146 < SIZE_TO_PSIZE(sizeof(LOOP) + OPSLOT_HEADER))
9148 /* no space; allocate new op */
9150 NewOp(1234,tmp,1,LOOP);
9151 Copy(loop,tmp,1,LISTOP);
9152 assert(loop->op_last->op_sibparent == (OP*)loop);
9153 OpLASTSIB_set(loop->op_last, (OP*)tmp); /*point back to new parent */
9154 S_op_destroy(aTHX_ (OP*)loop);
9157 else if (!loop->op_slabbed)
9159 /* loop was malloc()ed */
9160 loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP));
9161 OpLASTSIB_set(loop->op_last, (OP*)loop);
9163 loop->op_targ = padoff;
9165 /* hint to deparser that this: for my (...) ... */
9166 loop->op_flags |= OPf_PARENS;
9167 iter = newOP(OP_ITER, 0);
9168 iter->op_targ = how_many_more;
9169 return newWHILEOP(flags, 1, loop, iter, block, cont, 0);
9173 =for apidoc newLOOPEX
9175 Constructs, checks, and returns a loop-exiting op (such as C<goto>
9176 or C<last>). C<type> is the opcode. C<label> supplies the parameter
9177 determining the target of the op; it is consumed by this function and
9178 becomes part of the constructed op tree.
9184 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
9188 PERL_ARGS_ASSERT_NEWLOOPEX;
9190 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
9191 || type == OP_CUSTOM);
9193 if (type != OP_GOTO) {
9194 /* "last()" means "last" */
9195 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) {
9196 o = newOP(type, OPf_SPECIAL);
9200 /* Check whether it's going to be a goto &function */
9201 if (label->op_type == OP_ENTERSUB
9202 && !(label->op_flags & OPf_STACKED))
9203 label = newUNOP(OP_REFGEN, 0, op_lvalue(label, OP_REFGEN));
9206 /* Check for a constant argument */
9207 if (label->op_type == OP_CONST) {
9208 SV * const sv = cSVOPx(label)->op_sv;
9210 const char *s = SvPV_const(sv,l);
9211 if (l == strlen(s)) {
9213 SvUTF8(cSVOPx(label)->op_sv),
9215 SvPV_nolen_const(cSVOPx(label)->op_sv)));
9219 /* If we have already created an op, we do not need the label. */
9222 else o = newUNOP(type, OPf_STACKED, label);
9224 PL_hints |= HINT_BLOCK_SCOPE;
9228 /* if the condition is a literal array or hash
9229 (or @{ ... } etc), make a reference to it.
9232 S_ref_array_or_hash(pTHX_ OP *cond)
9235 && (cond->op_type == OP_RV2AV
9236 || cond->op_type == OP_PADAV
9237 || cond->op_type == OP_RV2HV
9238 || cond->op_type == OP_PADHV))
9240 return newUNOP(OP_REFGEN, 0, op_lvalue(cond, OP_REFGEN));
9243 && (cond->op_type == OP_ASLICE
9244 || cond->op_type == OP_KVASLICE
9245 || cond->op_type == OP_HSLICE
9246 || cond->op_type == OP_KVHSLICE)) {
9248 /* anonlist now needs a list from this op, was previously used in
9250 cond->op_flags &= ~(OPf_WANT_SCALAR | OPf_REF);
9251 cond->op_flags |= OPf_WANT_LIST;
9253 return newANONLIST(op_lvalue(cond, OP_ANONLIST));
9260 /* These construct the optree fragments representing given()
9263 entergiven and enterwhen are LOGOPs; the op_other pointer
9264 points up to the associated leave op. We need this so we
9265 can put it in the context and make break/continue work.
9266 (Also, of course, pp_enterwhen will jump straight to
9267 op_other if the match fails.)
9271 S_newGIVWHENOP(pTHX_ OP *cond, OP *block,
9272 I32 enter_opcode, I32 leave_opcode,
9273 PADOFFSET entertarg)
9278 PERL_ARGS_ASSERT_NEWGIVWHENOP;
9279 PERL_UNUSED_ARG(entertarg); /* used to indicate targ of lexical $_ */
9281 enterop = alloc_LOGOP(enter_opcode, block, NULL);
9282 enterop->op_targ = 0;
9283 enterop->op_private = 0;
9285 o = newUNOP(leave_opcode, 0, (OP *) enterop);
9288 /* prepend cond if we have one */
9289 op_sibling_splice((OP*)enterop, NULL, 0, scalar(cond));
9291 o->op_next = LINKLIST(cond);
9292 cond->op_next = (OP *) enterop;
9295 /* This is a default {} block */
9296 enterop->op_flags |= OPf_SPECIAL;
9297 o ->op_flags |= OPf_SPECIAL;
9299 o->op_next = (OP *) enterop;
9302 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since
9303 entergiven and enterwhen both
9306 enterop->op_next = LINKLIST(block);
9307 block->op_next = enterop->op_other = o;
9313 /* For the purposes of 'when(implied_smartmatch)'
9314 * versus 'when(boolean_expression)',
9315 * does this look like a boolean operation? For these purposes
9316 a boolean operation is:
9317 - a subroutine call [*]
9318 - a logical connective
9319 - a comparison operator
9320 - a filetest operator, with the exception of -s -M -A -C
9321 - defined(), exists() or eof()
9322 - /$re/ or $foo =~ /$re/
9324 [*] possibly surprising
9327 S_looks_like_bool(pTHX_ const OP *o)
9329 PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL;
9331 switch(o->op_type) {
9334 return looks_like_bool(cLOGOPo->op_first);
9338 OP* sibl = OpSIBLING(cLOGOPo->op_first);
9341 looks_like_bool(cLOGOPo->op_first)
9342 && looks_like_bool(sibl));
9348 o->op_flags & OPf_KIDS
9349 && looks_like_bool(cUNOPo->op_first));
9353 case OP_NOT: case OP_XOR:
9355 case OP_EQ: case OP_NE: case OP_LT:
9356 case OP_GT: case OP_LE: case OP_GE:
9358 case OP_I_EQ: case OP_I_NE: case OP_I_LT:
9359 case OP_I_GT: case OP_I_LE: case OP_I_GE:
9361 case OP_SEQ: case OP_SNE: case OP_SLT:
9362 case OP_SGT: case OP_SLE: case OP_SGE:
9366 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC:
9367 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC:
9368 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED:
9369 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR:
9370 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR:
9371 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID:
9372 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY:
9373 case OP_FTTEXT: case OP_FTBINARY:
9375 case OP_DEFINED: case OP_EXISTS:
9376 case OP_MATCH: case OP_EOF:
9384 /* optimised-away (index() != -1) or similar comparison */
9385 if (o->op_private & OPpTRUEBOOL)
9390 /* Detect comparisons that have been optimized away */
9391 if (cSVOPo->op_sv == &PL_sv_yes
9392 || cSVOPo->op_sv == &PL_sv_no)
9405 =for apidoc newGIVENOP
9407 Constructs, checks, and returns an op tree expressing a C<given> block.
9408 C<cond> supplies the expression to whose value C<$_> will be locally
9409 aliased, and C<block> supplies the body of the C<given> construct; they
9410 are consumed by this function and become part of the constructed op tree.
9411 C<defsv_off> must be zero (it used to identity the pad slot of lexical $_).
9417 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off)
9419 PERL_ARGS_ASSERT_NEWGIVENOP;
9420 PERL_UNUSED_ARG(defsv_off);
9423 return newGIVWHENOP(
9424 ref_array_or_hash(cond),
9426 OP_ENTERGIVEN, OP_LEAVEGIVEN,
9431 =for apidoc newWHENOP
9433 Constructs, checks, and returns an op tree expressing a C<when> block.
9434 C<cond> supplies the test expression, and C<block> supplies the block
9435 that will be executed if the test evaluates to true; they are consumed
9436 by this function and become part of the constructed op tree. C<cond>
9437 will be interpreted DWIMically, often as a comparison against C<$_>,
9438 and may be null to generate a C<default> block.
9444 Perl_newWHENOP(pTHX_ OP *cond, OP *block)
9446 const bool cond_llb = (!cond || looks_like_bool(cond));
9449 PERL_ARGS_ASSERT_NEWWHENOP;
9454 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL,
9456 scalar(ref_array_or_hash(cond)));
9459 return newGIVWHENOP(cond_op, block, OP_ENTERWHEN, OP_LEAVEWHEN, 0);
9463 =for apidoc newDEFEROP
9465 Constructs and returns a deferred-block statement that implements the
9466 C<defer> semantics. The C<block> optree is consumed by this function and
9467 becomes part of the returned optree.
9469 The C<flags> argument carries additional flags to set on the returned op,
9470 including the C<op_private> field.
9476 Perl_newDEFEROP(pTHX_ I32 flags, OP *block)
9478 OP *o, *start, *blockfirst;
9480 PERL_ARGS_ASSERT_NEWDEFEROP;
9482 start = LINKLIST(block);
9484 /* Hide the block inside an OP_NULL with no exection */
9485 block = newUNOP(OP_NULL, 0, block);
9486 block->op_next = block;
9488 o = (OP *)alloc_LOGOP(OP_PUSHDEFER, block, start);
9489 o->op_flags |= OPf_WANT_VOID | (U8)(flags);
9490 o->op_private = (U8)(flags >> 8);
9492 /* Terminate the block */
9493 blockfirst = cUNOPx(block)->op_first;
9494 assert(blockfirst->op_type == OP_SCOPE || blockfirst->op_type == OP_LEAVE);
9495 blockfirst->op_next = NULL;
9501 =for apidoc op_wrap_finally
9503 Wraps the given C<block> optree fragment in its own scoped block, arranging
9504 for the C<finally> optree fragment to be invoked when leaving that block for
9505 any reason. Both optree fragments are consumed and the combined result is
9512 Perl_op_wrap_finally(pTHX_ OP *block, OP *finally)
9514 PERL_ARGS_ASSERT_OP_WRAP_FINALLY;
9516 /* TODO: If block is already an ENTER/LEAVE-wrapped line sequence we can
9517 * just splice the DEFEROP in at the top, for efficiency.
9520 OP *o = newLISTOP(OP_LINESEQ, 0, newDEFEROP((OPpDEFER_FINALLY << 8), finally), block);
9521 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
9522 OpTYPE_set(o, OP_LEAVE);
9527 /* must not conflict with SVf_UTF8 */
9528 #define CV_CKPROTO_CURSTASH 0x1
9531 Perl_cv_ckproto_len_flags(pTHX_ const CV *cv, const GV *gv, const char *p,
9532 const STRLEN len, const U32 flags)
9534 SV *name = NULL, *msg;
9535 const char * cvp = SvROK(cv)
9536 ? SvTYPE(SvRV_const(cv)) == SVt_PVCV
9537 ? (cv = (const CV *)SvRV_const(cv), CvPROTO(cv))
9540 STRLEN clen = CvPROTOLEN(cv), plen = len;
9542 PERL_ARGS_ASSERT_CV_CKPROTO_LEN_FLAGS;
9544 if (p == NULL && cvp == NULL)
9547 if (!ckWARN_d(WARN_PROTOTYPE))
9551 p = S_strip_spaces(aTHX_ p, &plen);
9552 cvp = S_strip_spaces(aTHX_ cvp, &clen);
9553 if ((flags & SVf_UTF8) == SvUTF8(cv)) {
9554 if (plen == clen && memEQ(cvp, p, plen))
9557 if (flags & SVf_UTF8) {
9558 if (bytes_cmp_utf8((const U8 *)cvp, clen, (const U8 *)p, plen) == 0)
9562 if (bytes_cmp_utf8((const U8 *)p, plen, (const U8 *)cvp, clen) == 0)
9568 msg = sv_newmortal();
9573 gv_efullname3(name = sv_newmortal(), gv, NULL);
9574 else if (SvPOK(gv) && *SvPVX((SV *)gv) == '&')
9575 name = newSVpvn_flags(SvPVX((SV *)gv)+1, SvCUR(gv)-1, SvUTF8(gv)|SVs_TEMP);
9576 else if (flags & CV_CKPROTO_CURSTASH || SvROK(gv)) {
9577 name = newSVhek_mortal(HvNAME_HEK(PL_curstash));
9578 sv_catpvs(name, "::");
9580 assert (SvTYPE(SvRV_const(gv)) == SVt_PVCV);
9581 assert (CvNAMED(SvRV_const(gv)));
9582 sv_cathek(name, CvNAME_HEK(MUTABLE_CV(SvRV_const(gv))));
9584 else sv_catsv(name, (SV *)gv);
9586 else name = (SV *)gv;
9588 sv_setpvs(msg, "Prototype mismatch:");
9590 Perl_sv_catpvf(aTHX_ msg, " sub %" SVf, SVfARG(name));
9592 Perl_sv_catpvf(aTHX_ msg, " (%" UTF8f ")",
9593 UTF8fARG(SvUTF8(cv),clen,cvp)
9596 sv_catpvs(msg, ": none");
9597 sv_catpvs(msg, " vs ");
9599 Perl_sv_catpvf(aTHX_ msg, "(%" UTF8f ")", UTF8fARG(flags & SVf_UTF8,len,p));
9601 sv_catpvs(msg, "none");
9602 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%" SVf, SVfARG(msg));
9605 static void const_sv_xsub(pTHX_ CV* cv);
9606 static void const_av_xsub(pTHX_ CV* cv);
9610 =for apidoc_section $optree_manipulation
9612 =for apidoc cv_const_sv
9614 If C<cv> is a constant sub eligible for inlining, returns the constant
9615 value returned by the sub. Otherwise, returns C<NULL>.
9617 Constant subs can be created with C<newCONSTSUB> or as described in
9618 L<perlsub/"Constant Functions">.
9623 Perl_cv_const_sv(const CV *const cv)
9628 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
9630 sv = CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
9631 if (sv && SvTYPE(sv) == SVt_PVAV) return NULL;
9636 Perl_cv_const_sv_or_av(const CV * const cv)
9640 if (SvROK(cv)) return SvRV((SV *)cv);
9641 assert (SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
9642 return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL;
9645 /* op_const_sv: examine an optree to determine whether it's in-lineable.
9646 * Can be called in 2 ways:
9649 * look for a single OP_CONST with attached value: return the value
9651 * allow_lex && !CvCONST(cv);
9653 * examine the clone prototype, and if contains only a single
9654 * OP_CONST, return the value; or if it contains a single PADSV ref-
9655 * erencing an outer lexical, turn on CvCONST to indicate the CV is
9656 * a candidate for "constizing" at clone time, and return NULL.
9660 S_op_const_sv(pTHX_ const OP *o, CV *cv, bool allow_lex)
9668 for (; o; o = o->op_next) {
9669 const OPCODE type = o->op_type;
9671 if (type == OP_NEXTSTATE || type == OP_LINESEQ
9673 || type == OP_PUSHMARK)
9675 if (type == OP_DBSTATE)
9677 if (type == OP_LEAVESUB)
9681 if (type == OP_CONST && cSVOPo->op_sv)
9683 else if (type == OP_UNDEF && !o->op_private) {
9684 sv = newSV_type(SVt_NULL);
9687 else if (allow_lex && type == OP_PADSV) {
9688 if (PAD_COMPNAME_FLAGS(o->op_targ) & PADNAMEf_OUTER)
9690 sv = &PL_sv_undef; /* an arbitrary non-null value */
9708 S_already_defined(pTHX_ CV *const cv, OP * const block, OP * const o,
9709 PADNAME * const name, SV ** const const_svp)
9715 if (CvFLAGS(PL_compcv)) {
9716 /* might have had built-in attrs applied */
9717 const bool pureperl = !CvISXSUB(cv) && CvROOT(cv);
9718 if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl
9719 && ckWARN(WARN_MISC))
9721 /* protect against fatal warnings leaking compcv */
9722 SAVEFREESV(PL_compcv);
9723 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined");
9724 SvREFCNT_inc_simple_void_NN(PL_compcv);
9727 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS
9728 & ~(CVf_LVALUE * pureperl));
9733 /* redundant check for speed: */
9734 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
9735 const line_t oldline = CopLINE(PL_curcop);
9738 : newSVpvn_flags( PadnamePV(name)+1,PadnameLEN(name)-1,
9739 (PadnameUTF8(name)) ? SVf_UTF8|SVs_TEMP : SVs_TEMP
9741 if (PL_parser && PL_parser->copline != NOLINE)
9742 /* This ensures that warnings are reported at the first
9743 line of a redefinition, not the last. */
9744 CopLINE_set(PL_curcop, PL_parser->copline);
9745 /* protect against fatal warnings leaking compcv */
9746 SAVEFREESV(PL_compcv);
9747 report_redefined_cv(namesv, cv, const_svp);
9748 SvREFCNT_inc_simple_void_NN(PL_compcv);
9749 CopLINE_set(PL_curcop, oldline);
9756 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
9761 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
9764 CV *compcv = PL_compcv;
9767 PADOFFSET pax = o->op_targ;
9768 CV *outcv = CvOUTSIDE(PL_compcv);
9771 bool reusable = FALSE;
9773 #ifdef PERL_DEBUG_READONLY_OPS
9774 OPSLAB *slab = NULL;
9777 PERL_ARGS_ASSERT_NEWMYSUB;
9779 PL_hints |= HINT_BLOCK_SCOPE;
9781 /* Find the pad slot for storing the new sub.
9782 We cannot use PL_comppad, as it is the pad owned by the new sub. We
9783 need to look in CvOUTSIDE and find the pad belonging to the enclos-
9784 ing sub. And then we need to dig deeper if this is a lexical from
9786 my sub foo; sub { sub foo { } }
9789 name = PadlistNAMESARRAY(CvPADLIST(outcv))[pax];
9790 if (PadnameOUTER(name) && PARENT_PAD_INDEX(name)) {
9791 pax = PARENT_PAD_INDEX(name);
9792 outcv = CvOUTSIDE(outcv);
9797 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))
9798 [CvDEPTH(outcv) ? CvDEPTH(outcv) : 1])[pax];
9799 spot = (CV **)svspot;
9801 if (!(PL_parser && PL_parser->error_count))
9802 move_proto_attr(&proto, &attrs, (GV *)PadnameSV(name), 0);
9805 assert(proto->op_type == OP_CONST);
9806 ps = SvPV_const(cSVOPx(proto)->op_sv, ps_len);
9807 ps_utf8 = SvUTF8(cSVOPx(proto)->op_sv);
9817 if (PL_parser && PL_parser->error_count) {
9819 SvREFCNT_dec(PL_compcv);
9824 if (CvDEPTH(outcv) && CvCLONE(compcv)) {
9826 svspot = (SV **)(spot = &clonee);
9828 else if (PadnameIsSTATE(name) || CvDEPTH(outcv))
9831 assert (SvTYPE(*spot) == SVt_PVCV);
9833 hek = CvNAME_HEK(*spot);
9836 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
9837 CvNAME_HEK_set(*spot, hek =
9840 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1),
9844 CvLEXICAL_on(*spot);
9846 cv = PadnamePROTOCV(name);
9847 svspot = (SV **)(spot = &PadnamePROTOCV(name));
9851 /* This makes sub {}; work as expected. */
9852 if (block->op_type == OP_STUB) {
9853 const line_t l = PL_parser->copline;
9855 block = newSTATEOP(0, NULL, 0);
9856 PL_parser->copline = l;
9858 block = CvLVALUE(compcv)
9859 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv))
9860 ? newUNOP(OP_LEAVESUBLV, 0,
9861 op_lvalue(voidnonfinal(block), OP_LEAVESUBLV))
9862 : newUNOP(OP_LEAVESUB, 0, voidnonfinal(block));
9863 start = LINKLIST(block);
9865 if (ps && !*ps && !attrs && !CvLVALUE(compcv))
9866 const_sv = S_op_const_sv(aTHX_ start, compcv, FALSE);
9874 const bool exists = CvROOT(cv) || CvXSUB(cv);
9876 /* if the subroutine doesn't exist and wasn't pre-declared
9877 * with a prototype, assume it will be AUTOLOADed,
9878 * skipping the prototype check
9880 if (exists || SvPOK(cv))
9881 cv_ckproto_len_flags(cv, (GV *)PadnameSV(name), ps, ps_len,
9883 /* already defined? */
9885 S_already_defined(aTHX_ cv, block, NULL, name, &const_sv);
9891 /* just a "sub foo;" when &foo is already defined */
9896 else if (CvDEPTH(outcv) && CvCLONE(compcv)) {
9903 SvREFCNT_inc_simple_void_NN(const_sv);
9904 SvFLAGS(const_sv) |= SVs_PADTMP;
9906 assert(!CvROOT(cv) && !CvCONST(cv));
9910 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
9911 CvFILE_set_from_cop(cv, PL_curcop);
9912 CvSTASH_set(cv, PL_curstash);
9915 SvPVCLEAR(MUTABLE_SV(cv)); /* prototype is "" */
9916 CvXSUBANY(cv).any_ptr = const_sv;
9917 CvXSUB(cv) = const_sv_xsub;
9921 CvFLAGS(cv) |= CvNOWARN_AMBIGUOUS(compcv);
9923 SvREFCNT_dec(compcv);
9928 /* Checking whether outcv is CvOUTSIDE(compcv) is not sufficient to
9929 determine whether this sub definition is in the same scope as its
9930 declaration. If this sub definition is inside an inner named pack-
9931 age sub (my sub foo; sub bar { sub foo { ... } }), outcv points to
9932 the package sub. So check PadnameOUTER(name) too.
9934 if (outcv == CvOUTSIDE(compcv) && !PadnameOUTER(name)) {
9935 assert(!CvWEAKOUTSIDE(compcv));
9936 SvREFCNT_dec(CvOUTSIDE(compcv));
9937 CvWEAKOUTSIDE_on(compcv);
9939 /* XXX else do we have a circular reference? */
9941 if (cv) { /* must reuse cv in case stub is referenced elsewhere */
9942 /* transfer PL_compcv to cv */
9944 bool free_file = CvFILE(cv) && CvDYNFILE(cv);
9945 cv_flags_t preserved_flags =
9946 CvFLAGS(cv) & (CVf_BUILTIN_ATTRS|CVf_NAMED);
9947 PADLIST *const temp_padl = CvPADLIST(cv);
9948 CV *const temp_cv = CvOUTSIDE(cv);
9949 const cv_flags_t other_flags =
9950 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
9951 OP * const cvstart = CvSTART(cv);
9955 CvFLAGS(compcv) | preserved_flags;
9956 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
9957 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(compcv);
9958 CvPADLIST_set(cv, CvPADLIST(compcv));
9959 CvOUTSIDE(compcv) = temp_cv;
9960 CvPADLIST_set(compcv, temp_padl);
9961 CvSTART(cv) = CvSTART(compcv);
9962 CvSTART(compcv) = cvstart;
9963 CvFLAGS(compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
9964 CvFLAGS(compcv) |= other_flags;
9967 Safefree(CvFILE(cv));
9971 /* inner references to compcv must be fixed up ... */
9972 pad_fixup_inner_anons(CvPADLIST(cv), compcv, cv);
9973 if (PERLDB_INTER)/* Advice debugger on the new sub. */
9974 ++PL_sub_generation;
9977 /* Might have had built-in attributes applied -- propagate them. */
9978 CvFLAGS(cv) |= (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS);
9980 /* ... before we throw it away */
9981 SvREFCNT_dec(compcv);
9982 PL_compcv = compcv = cv;
9991 if (!CvNAME_HEK(cv)) {
9992 if (hek) (void)share_hek_hek(hek);
9995 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1);
9996 hek = share_hek(PadnamePV(name)+1,
9997 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1),
10000 CvNAME_HEK_set(cv, hek);
10006 if (CvFILE(cv) && CvDYNFILE(cv))
10007 Safefree(CvFILE(cv));
10008 CvFILE_set_from_cop(cv, PL_curcop);
10009 CvSTASH_set(cv, PL_curstash);
10012 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
10014 SvUTF8_on(MUTABLE_SV(cv));
10018 /* If we assign an optree to a PVCV, then we've defined a
10019 * subroutine that the debugger could be able to set a breakpoint
10020 * in, so signal to pp_entereval that it should not throw away any
10021 * saved lines at scope exit. */
10023 PL_breakable_sub_gen++;
10024 CvROOT(cv) = block;
10025 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
10026 itself has a refcount. */
10028 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
10029 #ifdef PERL_DEBUG_READONLY_OPS
10030 slab = (OPSLAB *)CvSTART(cv);
10032 S_process_optree(aTHX_ cv, block, start);
10037 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
10038 apply_attrs(PL_curstash, MUTABLE_SV(cv), attrs);
10042 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
10043 SV * const tmpstr = sv_newmortal();
10044 GV * const db_postponed = gv_fetchpvs("DB::postponed",
10045 GV_ADDMULTI, SVt_PVHV);
10047 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
10048 CopFILE(PL_curcop),
10050 (long)CopLINE(PL_curcop));
10051 if (HvNAME_HEK(PL_curstash)) {
10052 sv_sethek(tmpstr, HvNAME_HEK(PL_curstash));
10053 sv_catpvs(tmpstr, "::");
10056 sv_setpvs(tmpstr, "__ANON__::");
10058 sv_catpvn_flags(tmpstr, PadnamePV(name)+1, PadnameLEN(name)-1,
10059 PadnameUTF8(name) ? SV_CATUTF8 : SV_CATBYTES);
10060 (void)hv_store_ent(GvHV(PL_DBsub), tmpstr, sv, 0);
10061 hv = GvHVn(db_postponed);
10062 if (HvTOTALKEYS(hv) > 0 && hv_exists_ent(hv, tmpstr, 0)) {
10063 CV * const pcv = GvCV(db_postponed);
10069 call_sv(MUTABLE_SV(pcv), G_DISCARD);
10077 assert(CvDEPTH(outcv));
10079 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[CvDEPTH(outcv)])[pax];
10081 cv_clone_into(clonee, *spot);
10082 else *spot = cv_clone(clonee);
10083 SvREFCNT_dec_NN(clonee);
10087 if (CvDEPTH(outcv) && !reusable && PadnameIsSTATE(name)) {
10088 PADOFFSET depth = CvDEPTH(outcv);
10091 svspot = &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[depth])[pax];
10093 *svspot = SvREFCNT_inc_simple_NN(cv);
10094 SvREFCNT_dec(oldcv);
10100 PL_parser->copline = NOLINE;
10101 LEAVE_SCOPE(floor);
10102 #ifdef PERL_DEBUG_READONLY_OPS
10111 =for apidoc newATTRSUB_x
10113 Construct a Perl subroutine, also performing some surrounding jobs.
10115 This function is expected to be called in a Perl compilation context,
10116 and some aspects of the subroutine are taken from global variables
10117 associated with compilation. In particular, C<PL_compcv> represents
10118 the subroutine that is currently being compiled. It must be non-null
10119 when this function is called, and some aspects of the subroutine being
10120 constructed are taken from it. The constructed subroutine may actually
10121 be a reuse of the C<PL_compcv> object, but will not necessarily be so.
10123 If C<block> is null then the subroutine will have no body, and for the
10124 time being it will be an error to call it. This represents a forward
10125 subroutine declaration such as S<C<sub foo ($$);>>. If C<block> is
10126 non-null then it provides the Perl code of the subroutine body, which
10127 will be executed when the subroutine is called. This body includes
10128 any argument unwrapping code resulting from a subroutine signature or
10129 similar. The pad use of the code must correspond to the pad attached
10130 to C<PL_compcv>. The code is not expected to include a C<leavesub> or
10131 C<leavesublv> op; this function will add such an op. C<block> is consumed
10132 by this function and will become part of the constructed subroutine.
10134 C<proto> specifies the subroutine's prototype, unless one is supplied
10135 as an attribute (see below). If C<proto> is null, then the subroutine
10136 will not have a prototype. If C<proto> is non-null, it must point to a
10137 C<const> op whose value is a string, and the subroutine will have that
10138 string as its prototype. If a prototype is supplied as an attribute, the
10139 attribute takes precedence over C<proto>, but in that case C<proto> should
10140 preferably be null. In any case, C<proto> is consumed by this function.
10142 C<attrs> supplies attributes to be applied the subroutine. A handful of
10143 attributes take effect by built-in means, being applied to C<PL_compcv>
10144 immediately when seen. Other attributes are collected up and attached
10145 to the subroutine by this route. C<attrs> may be null to supply no
10146 attributes, or point to a C<const> op for a single attribute, or point
10147 to a C<list> op whose children apart from the C<pushmark> are C<const>
10148 ops for one or more attributes. Each C<const> op must be a string,
10149 giving the attribute name optionally followed by parenthesised arguments,
10150 in the manner in which attributes appear in Perl source. The attributes
10151 will be applied to the sub by this function. C<attrs> is consumed by
10154 If C<o_is_gv> is false and C<o> is null, then the subroutine will
10155 be anonymous. If C<o_is_gv> is false and C<o> is non-null, then C<o>
10156 must point to a C<const> OP, which will be consumed by this function,
10157 and its string value supplies a name for the subroutine. The name may
10158 be qualified or unqualified, and if it is unqualified then a default
10159 stash will be selected in some manner. If C<o_is_gv> is true, then C<o>
10160 doesn't point to an C<OP> at all, but is instead a cast pointer to a C<GV>
10161 by which the subroutine will be named.
10163 If there is already a subroutine of the specified name, then the new
10164 sub will either replace the existing one in the glob or be merged with
10165 the existing one. A warning may be generated about redefinition.
10167 If the subroutine has one of a few special names, such as C<BEGIN> or
10168 C<END>, then it will be claimed by the appropriate queue for automatic
10169 running of phase-related subroutines. In this case the relevant glob will
10170 be left not containing any subroutine, even if it did contain one before.
10171 In the case of C<BEGIN>, the subroutine will be executed and the reference
10172 to it disposed of before this function returns.
10174 The function returns a pointer to the constructed subroutine. If the sub
10175 is anonymous then ownership of one counted reference to the subroutine
10176 is transferred to the caller. If the sub is named then the caller does
10177 not get ownership of a reference. In most such cases, where the sub
10178 has a non-phase name, the sub will be alive at the point it is returned
10179 by virtue of being contained in the glob that names it. A phase-named
10180 subroutine will usually be alive by virtue of the reference owned by the
10181 phase's automatic run queue. But a C<BEGIN> subroutine, having already
10182 been executed, will quite likely have been destroyed already by the
10183 time this function returns, making it erroneous for the caller to make
10184 any use of the returned pointer. It is the caller's responsibility to
10185 ensure that it knows which of these situations applies.
10187 =for apidoc newATTRSUB
10188 Construct a Perl subroutine, also performing some surrounding jobs.
10190 This is the same as L<perlintern/C<newATTRSUB_x>> with its C<o_is_gv> parameter set to
10191 FALSE. This means that if C<o> is null, the new sub will be anonymous; otherwise
10192 the name will be derived from C<o> in the way described (as with all other
10193 details) in L<perlintern/C<newATTRSUB_x>>.
10196 Like C<L</newATTRSUB>>, but without attributes.
10201 /* _x = extended */
10203 Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs,
10204 OP *block, bool o_is_gv)
10208 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */
10210 CV *cv = NULL; /* the previous CV with this name, if any */
10212 const bool ec = PL_parser && PL_parser->error_count;
10213 /* If the subroutine has no body, no attributes, and no builtin attributes
10214 then it's just a sub declaration, and we may be able to get away with
10215 storing with a placeholder scalar in the symbol table, rather than a
10216 full CV. If anything is present then it will take a full CV to
10218 const I32 gv_fetch_flags
10219 = ec ? GV_NOADD_NOINIT :
10220 (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
10221 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
10223 const char * const name =
10224 o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL;
10226 bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv);
10227 bool evanescent = FALSE;
10229 #ifdef PERL_DEBUG_READONLY_OPS
10230 OPSLAB *slab = NULL;
10238 /* Try to optimise and avoid creating a GV. Instead, the CV’s name
10239 hek and CvSTASH pointer together can imply the GV. If the name
10240 contains a package name, then GvSTASH(CvGV(cv)) may differ from
10241 CvSTASH, so forego the optimisation if we find any.
10242 Also, we may be called from load_module at run time, so
10243 PL_curstash (which sets CvSTASH) may not point to the stash the
10244 sub is stored in. */
10245 /* XXX This optimization is currently disabled for packages other
10246 than main, since there was too much CPAN breakage. */
10248 ec ? GV_NOADD_NOINIT
10249 : (IN_PERL_RUNTIME && PL_curstash != CopSTASH(PL_curcop))
10250 || PL_curstash != PL_defstash
10251 || memchr(name, ':', namlen) || memchr(name, '\'', namlen)
10253 : GV_ADDMULTI | GV_NOINIT | GV_NOTQUAL;
10254 gv = gv_fetchsv(cSVOPo->op_sv, flags, SVt_PVCV);
10256 } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) {
10257 SV * const sv = sv_newmortal();
10258 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%" IVdf "]",
10259 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
10260 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
10261 gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV);
10263 } else if (PL_curstash) {
10264 gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV);
10267 gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV);
10273 move_proto_attr(&proto, &attrs, gv, 0);
10276 move_proto_attr(&proto, &attrs, (GV *)cSVOPo->op_sv, 1);
10281 assert(proto->op_type == OP_CONST);
10282 ps = SvPV_const(cSVOPx(proto)->op_sv, ps_len);
10283 ps_utf8 = SvUTF8(cSVOPx(proto)->op_sv);
10299 SvREFCNT_dec(PL_compcv);
10304 if (name && block) {
10305 const char *s = (char *) my_memrchr(name, ':', namlen);
10306 s = s ? s+1 : name;
10307 if (strEQ(s, "BEGIN")) {
10308 if (PL_in_eval & EVAL_KEEPERR)
10309 Perl_croak_nocontext("BEGIN not safe after errors--compilation aborted");
10311 SV * const errsv = ERRSV;
10312 /* force display of errors found but not reported */
10313 sv_catpvs(errsv, "BEGIN not safe after errors--compilation aborted");
10314 Perl_croak_nocontext("%" SVf, SVfARG(errsv));
10321 if (!block && SvTYPE(gv) != SVt_PVGV) {
10322 /* If we are not defining a new sub and the existing one is not a
10324 if (attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)) {
10325 /* We are applying attributes to an existing sub, so we need it
10326 upgraded if it is a constant. */
10327 if (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)
10328 gv_init_pvn(gv, PL_curstash, name, namlen,
10329 SVf_UTF8 * name_is_utf8);
10331 else { /* Maybe prototype now, and had at maximum
10332 a prototype or const/sub ref before. */
10333 if (SvTYPE(gv) > SVt_NULL) {
10334 cv_ckproto_len_flags((const CV *)gv,
10335 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
10341 sv_setpvn(MUTABLE_SV(gv), ps, ps_len);
10343 SvUTF8_on(MUTABLE_SV(gv));
10346 sv_setiv(MUTABLE_SV(gv), -1);
10349 SvREFCNT_dec(PL_compcv);
10350 cv = PL_compcv = NULL;
10355 cv = (!name || (isGV(gv) && GvCVGEN(gv)))
10359 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
10365 /* This makes sub {}; work as expected. */
10366 if (block->op_type == OP_STUB) {
10367 const line_t l = PL_parser->copline;
10369 block = newSTATEOP(0, NULL, 0);
10370 PL_parser->copline = l;
10372 block = CvLVALUE(PL_compcv)
10373 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv)
10374 && (!isGV(gv) || !GvASSUMECV(gv)))
10375 ? newUNOP(OP_LEAVESUBLV, 0,
10376 op_lvalue(voidnonfinal(block), OP_LEAVESUBLV))
10377 : newUNOP(OP_LEAVESUB, 0, voidnonfinal(block));
10378 start = LINKLIST(block);
10379 block->op_next = 0;
10380 if (ps && !*ps && !attrs && !CvLVALUE(PL_compcv))
10382 S_op_const_sv(aTHX_ start, PL_compcv,
10383 cBOOL(CvCLONE(PL_compcv)));
10390 if (SvPOK(gv) || (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)) {
10391 cv_ckproto_len_flags((const CV *)gv,
10392 o ? (const GV *)cSVOPo->op_sv : NULL, ps,
10393 ps_len, ps_utf8|CV_CKPROTO_CURSTASH);
10395 /* All the other code for sub redefinition warnings expects the
10396 clobbered sub to be a CV. Instead of making all those code
10397 paths more complex, just inline the RV version here. */
10398 const line_t oldline = CopLINE(PL_curcop);
10399 assert(IN_PERL_COMPILETIME);
10400 if (PL_parser && PL_parser->copline != NOLINE)
10401 /* This ensures that warnings are reported at the first
10402 line of a redefinition, not the last. */
10403 CopLINE_set(PL_curcop, PL_parser->copline);
10404 /* protect against fatal warnings leaking compcv */
10405 SAVEFREESV(PL_compcv);
10407 if (ckWARN(WARN_REDEFINE)
10408 || ( ckWARN_d(WARN_REDEFINE)
10409 && ( !const_sv || SvRV(gv) == const_sv
10410 || sv_cmp(SvRV(gv), const_sv) ))) {
10412 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
10413 "Constant subroutine %" SVf " redefined",
10414 SVfARG(cSVOPo->op_sv));
10417 SvREFCNT_inc_simple_void_NN(PL_compcv);
10418 CopLINE_set(PL_curcop, oldline);
10419 SvREFCNT_dec(SvRV(gv));
10424 const bool exists = CvROOT(cv) || CvXSUB(cv);
10426 /* if the subroutine doesn't exist and wasn't pre-declared
10427 * with a prototype, assume it will be AUTOLOADed,
10428 * skipping the prototype check
10430 if (exists || SvPOK(cv))
10431 cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8);
10432 /* already defined (or promised)? */
10433 if (exists || (isGV(gv) && GvASSUMECV(gv))) {
10434 S_already_defined(aTHX_ cv, block, o, NULL, &const_sv);
10440 /* just a "sub foo;" when &foo is already defined */
10441 SAVEFREESV(PL_compcv);
10448 SvREFCNT_inc_simple_void_NN(const_sv);
10449 SvFLAGS(const_sv) |= SVs_PADTMP;
10451 assert(!CvROOT(cv) && !CvCONST(cv));
10452 cv_forget_slab(cv);
10453 SvPVCLEAR(MUTABLE_SV(cv)); /* prototype is "" */
10454 CvXSUBANY(cv).any_ptr = const_sv;
10455 CvXSUB(cv) = const_sv_xsub;
10459 CvFLAGS(cv) |= CvNOWARN_AMBIGUOUS(PL_compcv);
10462 if (isGV(gv) || CvNOWARN_AMBIGUOUS(PL_compcv)) {
10463 if (name && isGV(gv))
10464 GvCV_set(gv, NULL);
10465 cv = newCONSTSUB_flags(
10466 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0,
10470 assert(SvREFCNT((SV*)cv) != 0);
10471 CvFLAGS(cv) |= CvNOWARN_AMBIGUOUS(PL_compcv);
10475 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv);
10476 prepare_SV_for_RV((SV *)gv);
10477 SvOK_off((SV *)gv);
10480 SvRV_set(gv, const_sv);
10484 SvREFCNT_dec(PL_compcv);
10489 /* don't copy new BEGIN CV to old BEGIN CV - RT #129099 */
10490 if (name && cv && *name == 'B' && strEQ(name, "BEGIN"))
10493 if (cv) { /* must reuse cv if autoloaded */
10494 /* transfer PL_compcv to cv */
10496 bool free_file = CvFILE(cv) && CvDYNFILE(cv);
10497 cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS;
10498 PADLIST *const temp_av = CvPADLIST(cv);
10499 CV *const temp_cv = CvOUTSIDE(cv);
10500 const cv_flags_t other_flags =
10501 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE);
10502 OP * const cvstart = CvSTART(cv);
10506 assert(!CvCVGV_RC(cv));
10507 assert(CvGV(cv) == gv);
10511 PERL_HASH(hash, name, namlen);
10521 CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs
10523 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
10524 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
10525 CvPADLIST_set(cv,CvPADLIST(PL_compcv));
10526 CvOUTSIDE(PL_compcv) = temp_cv;
10527 CvPADLIST_set(PL_compcv, temp_av);
10528 CvSTART(cv) = CvSTART(PL_compcv);
10529 CvSTART(PL_compcv) = cvstart;
10530 CvFLAGS(PL_compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE);
10531 CvFLAGS(PL_compcv) |= other_flags;
10534 Safefree(CvFILE(cv));
10536 CvFILE_set_from_cop(cv, PL_curcop);
10537 CvSTASH_set(cv, PL_curstash);
10539 /* inner references to PL_compcv must be fixed up ... */
10540 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
10541 if (PERLDB_INTER)/* Advice debugger on the new sub. */
10542 ++PL_sub_generation;
10545 /* Might have had built-in attributes applied -- propagate them. */
10546 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
10548 /* ... before we throw it away */
10549 SvREFCNT_dec(PL_compcv);
10554 if (name && isGV(gv)) {
10557 if (HvENAME_HEK(GvSTASH(gv)))
10558 /* sub Foo::bar { (shift)+1 } */
10559 gv_method_changed(gv);
10563 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv);
10564 prepare_SV_for_RV((SV *)gv);
10565 SvOK_off((SV *)gv);
10568 SvRV_set(gv, (SV *)cv);
10569 if (HvENAME_HEK(PL_curstash))
10570 mro_method_changed_in(PL_curstash);
10574 assert(SvREFCNT((SV*)cv) != 0);
10576 if (!CvHASGV(cv)) {
10581 PERL_HASH(hash, name, namlen);
10582 CvNAME_HEK_set(cv, share_hek(name,
10588 CvFILE_set_from_cop(cv, PL_curcop);
10589 CvSTASH_set(cv, PL_curstash);
10593 sv_setpvn(MUTABLE_SV(cv), ps, ps_len);
10595 SvUTF8_on(MUTABLE_SV(cv));
10599 /* If we assign an optree to a PVCV, then we've defined a
10600 * subroutine that the debugger could be able to set a breakpoint
10601 * in, so signal to pp_entereval that it should not throw away any
10602 * saved lines at scope exit. */
10604 PL_breakable_sub_gen++;
10605 CvROOT(cv) = block;
10606 /* The cv no longer needs to hold a refcount on the slab, as CvROOT
10607 itself has a refcount. */
10609 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv));
10610 #ifdef PERL_DEBUG_READONLY_OPS
10611 slab = (OPSLAB *)CvSTART(cv);
10613 S_process_optree(aTHX_ cv, block, start);
10618 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */
10619 HV *stash = name && !CvNAMED(cv) && GvSTASH(CvGV(cv))
10620 ? GvSTASH(CvGV(cv))
10624 apply_attrs(stash, MUTABLE_SV(cv), attrs);
10626 SvREFCNT_inc_simple_void_NN(cv);
10629 if (block && has_name) {
10630 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
10631 SV * const tmpstr = cv_name(cv,NULL,0);
10632 GV * const db_postponed = gv_fetchpvs("DB::postponed",
10633 GV_ADDMULTI, SVt_PVHV);
10635 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld",
10636 CopFILE(PL_curcop),
10638 (long)CopLINE(PL_curcop));
10639 (void)hv_store_ent(GvHV(PL_DBsub), tmpstr, sv, 0);
10640 hv = GvHVn(db_postponed);
10641 if (HvTOTALKEYS(hv) > 0 && hv_exists_ent(hv, tmpstr, 0)) {
10642 CV * const pcv = GvCV(db_postponed);
10648 call_sv(MUTABLE_SV(pcv), G_DISCARD);
10654 if (PL_parser && PL_parser->error_count)
10655 clear_special_blocks(name, gv, cv);
10658 process_special_blocks(floor, name, gv, cv);
10664 assert(!cv || evanescent || SvREFCNT((SV*)cv) != 0);
10666 PL_parser->copline = NOLINE;
10667 LEAVE_SCOPE(floor);
10669 assert(!cv || evanescent || SvREFCNT((SV*)cv) != 0);
10671 #ifdef PERL_DEBUG_READONLY_OPS
10675 if (cv && name && block && CvOUTSIDE(cv) && !CvEVAL(CvOUTSIDE(cv)))
10676 pad_add_weakref(cv);
10682 S_clear_special_blocks(pTHX_ const char *const fullname,
10683 GV *const gv, CV *const cv) {
10687 PERL_ARGS_ASSERT_CLEAR_SPECIAL_BLOCKS;
10689 colon = strrchr(fullname,':');
10690 name = colon ? colon + 1 : fullname;
10692 if ((*name == 'B' && strEQ(name, "BEGIN"))
10693 || (*name == 'E' && strEQ(name, "END"))
10694 || (*name == 'U' && strEQ(name, "UNITCHECK"))
10695 || (*name == 'C' && strEQ(name, "CHECK"))
10696 || (*name == 'I' && strEQ(name, "INIT"))) {
10701 GvCV_set(gv, NULL);
10702 SvREFCNT_dec_NN(MUTABLE_SV(cv));
10706 /* Returns true if the sub has been freed. */
10708 S_process_special_blocks(pTHX_ I32 floor, const char *const fullname,
10712 const char *const colon = strrchr(fullname,':');
10713 const char *const name = colon ? colon + 1 : fullname;
10715 PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS;
10717 if (*name == 'B') {
10718 if (strEQ(name, "BEGIN")) {
10719 const I32 oldscope = PL_scopestack_ix;
10722 if (floor) LEAVE_SCOPE(floor);
10725 SAVEVPTR(PL_curcop);
10726 if (PL_curcop == &PL_compiling) {
10727 /* Avoid pushing the "global" &PL_compiling onto the
10728 * context stack. For example, a stack trace inside
10729 * nested use's would show all calls coming from whoever
10730 * most recently updated PL_compiling.cop_file and
10731 * cop_line. So instead, temporarily set PL_curcop to a
10732 * private copy of &PL_compiling. PL_curcop will soon be
10733 * set to point back to &PL_compiling anyway but only
10734 * after the temp value has been pushed onto the context
10735 * stack as blk_oldcop.
10736 * This is slightly hacky, but necessary. Note also
10737 * that in the brief window before PL_curcop is set back
10738 * to PL_compiling, IN_PERL_COMPILETIME/IN_PERL_RUNTIME
10739 * will give the wrong answer.
10741 PL_curcop = (COP*)newSTATEOP(PL_compiling.op_flags, NULL, NULL);
10742 CopLINE_set(PL_curcop, CopLINE(&PL_compiling));
10743 SAVEFREEOP(PL_curcop);
10746 PUSHSTACKi(PERLSI_REQUIRE);
10747 SAVECOPFILE(&PL_compiling);
10748 SAVECOPLINE(&PL_compiling);
10750 DEBUG_x( dump_sub(gv) );
10751 Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv));
10752 GvCV_set(gv,0); /* cv has been hijacked */
10753 call_list(oldscope, PL_beginav);
10757 return !PL_savebegin;
10762 if (*name == 'E') {
10763 if (strEQ(name, "END")) {
10764 DEBUG_x( dump_sub(gv) );
10765 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv));
10768 } else if (*name == 'U') {
10769 if (strEQ(name, "UNITCHECK")) {
10770 /* It's never too late to run a unitcheck block */
10771 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv));
10775 } else if (*name == 'C') {
10776 if (strEQ(name, "CHECK")) {
10778 /* diag_listed_as: Too late to run %s block */
10779 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
10780 "Too late to run CHECK block");
10781 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv));
10785 } else if (*name == 'I') {
10786 if (strEQ(name, "INIT")) {
10788 /* diag_listed_as: Too late to run %s block */
10789 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
10790 "Too late to run INIT block");
10791 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv));
10797 DEBUG_x( dump_sub(gv) );
10799 GvCV_set(gv,0); /* cv has been hijacked */
10805 =for apidoc newCONSTSUB
10807 Behaves like L</newCONSTSUB_flags>, except that C<name> is nul-terminated
10808 rather than of counted length, and no flags are set. (This means that
10809 C<name> is always interpreted as Latin-1.)
10815 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
10817 return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv);
10821 =for apidoc newCONSTSUB_flags
10823 Construct a constant subroutine, also performing some surrounding
10824 jobs. A scalar constant-valued subroutine is eligible for inlining
10825 at compile-time, and in Perl code can be created by S<C<sub FOO () {
10826 123 }>>. Other kinds of constant subroutine have other treatment.
10828 The subroutine will have an empty prototype and will ignore any arguments
10829 when called. Its constant behaviour is determined by C<sv>. If C<sv>
10830 is null, the subroutine will yield an empty list. If C<sv> points to a
10831 scalar, the subroutine will always yield that scalar. If C<sv> points
10832 to an array, the subroutine will always yield a list of the elements of
10833 that array in list context, or the number of elements in the array in
10834 scalar context. This function takes ownership of one counted reference
10835 to the scalar or array, and will arrange for the object to live as long
10836 as the subroutine does. If C<sv> points to a scalar then the inlining
10837 assumes that the value of the scalar will never change, so the caller
10838 must ensure that the scalar is not subsequently written to. If C<sv>
10839 points to an array then no such assumption is made, so it is ostensibly
10840 safe to mutate the array or its elements, but whether this is really
10841 supported has not been determined.
10843 The subroutine will have C<CvFILE> set according to C<PL_curcop>.
10844 Other aspects of the subroutine will be left in their default state.
10845 The caller is free to mutate the subroutine beyond its initial state
10846 after this function has returned.
10848 If C<name> is null then the subroutine will be anonymous, with its
10849 C<CvGV> referring to an C<__ANON__> glob. If C<name> is non-null then the
10850 subroutine will be named accordingly, referenced by the appropriate glob.
10851 C<name> is a string of length C<len> bytes giving a sigilless symbol
10852 name, in UTF-8 if C<flags> has the C<SVf_UTF8> bit set and in Latin-1
10853 otherwise. The name may be either qualified or unqualified. If the
10854 name is unqualified then it defaults to being in the stash specified by
10855 C<stash> if that is non-null, or to C<PL_curstash> if C<stash> is null.
10856 The symbol is always added to the stash if necessary, with C<GV_ADDMULTI>
10859 C<flags> should not have bits set other than C<SVf_UTF8>.
10861 If there is already a subroutine of the specified name, then the new sub
10862 will replace the existing one in the glob. A warning may be generated
10863 about the redefinition.
10865 If the subroutine has one of a few special names, such as C<BEGIN> or
10866 C<END>, then it will be claimed by the appropriate queue for automatic
10867 running of phase-related subroutines. In this case the relevant glob will
10868 be left not containing any subroutine, even if it did contain one before.
10869 Execution of the subroutine will likely be a no-op, unless C<sv> was
10870 a tied array or the caller modified the subroutine in some interesting
10871 way before it was executed. In the case of C<BEGIN>, the treatment is
10872 buggy: the sub will be executed when only half built, and may be deleted
10873 prematurely, possibly causing a crash.
10875 The function returns a pointer to the constructed subroutine. If the sub
10876 is anonymous then ownership of one counted reference to the subroutine
10877 is transferred to the caller. If the sub is named then the caller does
10878 not get ownership of a reference. In most such cases, where the sub
10879 has a non-phase name, the sub will be alive at the point it is returned
10880 by virtue of being contained in the glob that names it. A phase-named
10881 subroutine will usually be alive by virtue of the reference owned by
10882 the phase's automatic run queue. A C<BEGIN> subroutine may have been
10883 destroyed already by the time this function returns, but currently bugs
10884 occur in that case before the caller gets control. It is the caller's
10885 responsibility to ensure that it knows which of these situations applies.
10891 Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len,
10895 const char *const file = CopFILE(PL_curcop);
10899 if (IN_PERL_RUNTIME) {
10900 /* at runtime, it's not safe to manipulate PL_curcop: it may be
10901 * an op shared between threads. Use a non-shared COP for our
10903 SAVEVPTR(PL_curcop);
10904 SAVECOMPILEWARNINGS();
10905 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
10906 PL_curcop = &PL_compiling;
10908 SAVECOPLINE(PL_curcop);
10909 CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE);
10912 PL_hints &= ~HINT_BLOCK_SCOPE;
10915 SAVEGENERICSV(PL_curstash);
10916 PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash);
10919 /* Protect sv against leakage caused by fatal warnings. */
10920 if (sv) SAVEFREESV(sv);
10922 /* file becomes the CvFILE. For an XS, it's usually static storage,
10923 and so doesn't get free()d. (It's expected to be from the C pre-
10924 processor __FILE__ directive). But we need a dynamically allocated one,
10925 and we need it to get freed. */
10926 cv = newXS_len_flags(name, len,
10927 sv && SvTYPE(sv) == SVt_PVAV
10930 file ? file : "", "",
10931 &sv, XS_DYNAMIC_FILENAME | flags);
10933 assert(SvREFCNT((SV*)cv) != 0);
10934 CvXSUBANY(cv).any_ptr = SvREFCNT_inc_simple(sv);
10945 Used by C<xsubpp> to hook up XSUBs as Perl subs. C<filename> needs to be
10946 static storage, as it is used directly as CvFILE(), without a copy being made.
10952 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
10954 PERL_ARGS_ASSERT_NEWXS;
10955 return newXS_len_flags(
10956 name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0
10961 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
10962 const char *const filename, const char *const proto,
10965 PERL_ARGS_ASSERT_NEWXS_FLAGS;
10966 return newXS_len_flags(
10967 name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags
10972 Perl_newXS_deffile(pTHX_ const char *name, XSUBADDR_t subaddr)
10974 PERL_ARGS_ASSERT_NEWXS_DEFFILE;
10975 return newXS_len_flags(
10976 name, strlen(name), subaddr, NULL, NULL, NULL, 0
10981 =for apidoc newXS_len_flags
10983 Construct an XS subroutine, also performing some surrounding jobs.
10985 The subroutine will have the entry point C<subaddr>. It will have
10986 the prototype specified by the nul-terminated string C<proto>, or
10987 no prototype if C<proto> is null. The prototype string is copied;
10988 the caller can mutate the supplied string afterwards. If C<filename>
10989 is non-null, it must be a nul-terminated filename, and the subroutine
10990 will have its C<CvFILE> set accordingly. By default C<CvFILE> is set to
10991 point directly to the supplied string, which must be static. If C<flags>
10992 has the C<XS_DYNAMIC_FILENAME> bit set, then a copy of the string will
10995 Other aspects of the subroutine will be left in their default state.
10996 If anything else needs to be done to the subroutine for it to function
10997 correctly, it is the caller's responsibility to do that after this
10998 function has constructed it. However, beware of the subroutine
10999 potentially being destroyed before this function returns, as described
11002 If C<name> is null then the subroutine will be anonymous, with its
11003 C<CvGV> referring to an C<__ANON__> glob. If C<name> is non-null then the
11004 subroutine will be named accordingly, referenced by the appropriate glob.
11005 C<name> is a string of length C<len> bytes giving a sigilless symbol name,
11006 in UTF-8 if C<flags> has the C<SVf_UTF8> bit set and in Latin-1 otherwise.
11007 The name may be either qualified or unqualified, with the stash defaulting
11008 in the same manner as for C<gv_fetchpvn_flags>. C<flags> may contain
11009 flag bits understood by C<gv_fetchpvn_flags> with the same meaning as
11010 they have there, such as C<GV_ADDWARN>. The symbol is always added to
11011 the stash if necessary, with C<GV_ADDMULTI> semantics.
11013 If there is already a subroutine of the specified name, then the new sub
11014 will replace the existing one in the glob. A warning may be generated
11015 about the redefinition. If the old subroutine was C<CvCONST> then the
11016 decision about whether to warn is influenced by an expectation about
11017 whether the new subroutine will become a constant of similar value.
11018 That expectation is determined by C<const_svp>. (Note that the call to
11019 this function doesn't make the new subroutine C<CvCONST> in any case;
11020 that is left to the caller.) If C<const_svp> is null then it indicates
11021 that the new subroutine will not become a constant. If C<const_svp>
11022 is non-null then it indicates that the new subroutine will become a
11023 constant, and it points to an C<SV*> that provides the constant value
11024 that the subroutine will have.
11026 If the subroutine has one of a few special names, such as C<BEGIN> or
11027 C<END>, then it will be claimed by the appropriate queue for automatic
11028 running of phase-related subroutines. In this case the relevant glob will
11029 be left not containing any subroutine, even if it did contain one before.
11030 In the case of C<BEGIN>, the subroutine will be executed and the reference
11031 to it disposed of before this function returns, and also before its
11032 prototype is set. If a C<BEGIN> subroutine would not be sufficiently
11033 constructed by this function to be ready for execution then the caller
11034 must prevent this happening by giving the subroutine a different name.
11036 The function returns a pointer to the constructed subroutine. If the sub
11037 is anonymous then ownership of one counted reference to the subroutine
11038 is transferred to the caller. If the sub is named then the caller does
11039 not get ownership of a reference. In most such cases, where the sub
11040 has a non-phase name, the sub will be alive at the point it is returned
11041 by virtue of being contained in the glob that names it. A phase-named
11042 subroutine will usually be alive by virtue of the reference owned by the
11043 phase's automatic run queue. But a C<BEGIN> subroutine, having already
11044 been executed, will quite likely have been destroyed already by the
11045 time this function returns, making it erroneous for the caller to make
11046 any use of the returned pointer. It is the caller's responsibility to
11047 ensure that it knows which of these situations applies.
11053 Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len,
11054 XSUBADDR_t subaddr, const char *const filename,
11055 const char *const proto, SV **const_svp,
11059 bool interleave = FALSE;
11060 bool evanescent = FALSE;
11062 PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS;
11065 GV * const gv = gv_fetchpvn(
11066 name ? name : PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
11067 name ? len : PL_curstash ? sizeof("__ANON__") - 1:
11068 sizeof("__ANON__::__ANON__") - 1,
11069 GV_ADDMULTI | flags, SVt_PVCV);
11071 if ((cv = (name ? GvCV(gv) : NULL))) {
11073 /* just a cached method */
11077 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
11078 /* already defined (or promised) */
11079 /* Redundant check that allows us to avoid creating an SV
11080 most of the time: */
11081 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) {
11082 report_redefined_cv(newSVpvn_flags(
11083 name,len,(flags&SVf_UTF8)|SVs_TEMP
11094 if (cv) /* must reuse cv if autoloaded */
11097 cv = MUTABLE_CV(newSV_type(SVt_PVCV));
11101 if (HvENAME_HEK(GvSTASH(gv)))
11102 gv_method_changed(gv); /* newXS */
11106 assert(SvREFCNT((SV*)cv) != 0);
11110 /* XSUBs can't be perl lang/perl5db.pl debugged
11111 if (PERLDB_LINE_OR_SAVESRC)
11112 (void)gv_fetchfile(filename); */
11113 assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */
11114 if (flags & XS_DYNAMIC_FILENAME) {
11116 CvFILE(cv) = savepv(filename);
11118 /* NOTE: not copied, as it is expected to be an external constant string */
11119 CvFILE(cv) = (char *)filename;
11122 assert((flags & XS_DYNAMIC_FILENAME) == 0 && PL_xsubfilename);
11123 CvFILE(cv) = (char*)PL_xsubfilename;
11126 CvXSUB(cv) = subaddr;
11127 #ifndef MULTIPLICITY
11128 CvHSCXT(cv) = &PL_stack_sp;
11134 evanescent = process_special_blocks(0, name, gv, cv);
11137 } /* <- not a conditional branch */
11140 assert(evanescent || SvREFCNT((SV*)cv) != 0);
11142 if (!evanescent) sv_setpv(MUTABLE_SV(cv), proto);
11143 if (interleave) LEAVE;
11144 assert(evanescent || SvREFCNT((SV*)cv) != 0);
11148 /* Add a stub CV to a typeglob.
11149 * This is the implementation of a forward declaration, 'sub foo';'
11153 Perl_newSTUB(pTHX_ GV *gv, bool fake)
11155 CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV));
11157 PERL_ARGS_ASSERT_NEWSTUB;
11158 assert(!GvCVu(gv));
11161 if (!fake && GvSTASH(gv) && HvENAME_HEK(GvSTASH(gv)))
11162 gv_method_changed(gv);
11164 cvgv = gv_fetchsv((SV *)gv, GV_ADDMULTI, SVt_PVCV);
11168 CvGV_set(cv, cvgv);
11169 CvFILE_set_from_cop(cv, PL_curcop);
11170 CvSTASH_set(cv, PL_curstash);
11176 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
11183 if (PL_parser && PL_parser->error_count) {
11189 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
11190 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
11193 if ((cv = GvFORM(gv))) {
11194 if (ckWARN(WARN_REDEFINE)) {
11195 const line_t oldline = CopLINE(PL_curcop);
11196 if (PL_parser && PL_parser->copline != NOLINE)
11197 CopLINE_set(PL_curcop, PL_parser->copline);
11199 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
11200 "Format %" SVf " redefined", SVfARG(cSVOPo->op_sv));
11202 /* diag_listed_as: Format %s redefined */
11203 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
11204 "Format STDOUT redefined");
11206 CopLINE_set(PL_curcop, oldline);
11211 GvFORM(gv) = (CV *)SvREFCNT_inc_simple_NN(cv);
11213 CvFILE_set_from_cop(cv, PL_curcop);
11216 root = newUNOP(OP_LEAVEWRITE, 0, voidnonfinal(block));
11218 start = LINKLIST(root);
11220 S_process_optree(aTHX_ cv, root, start);
11221 cv_forget_slab(cv);
11226 PL_parser->copline = NOLINE;
11227 LEAVE_SCOPE(floor);
11228 PL_compiling.cop_seq = 0;
11232 Perl_newANONLIST(pTHX_ OP *o)
11234 return op_convert_list(OP_ANONLIST, OPf_SPECIAL, o);
11238 Perl_newANONHASH(pTHX_ OP *o)
11240 return op_convert_list(OP_ANONHASH, OPf_SPECIAL, o);
11244 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
11246 return newANONATTRSUB(floor, proto, NULL, block);
11250 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
11252 SV * const cv = MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block));
11254 newSVOP(OP_ANONCODE, 0,
11256 if (CvANONCONST(cv))
11257 anoncode = newUNOP(OP_ANONCONST, 0,
11258 op_convert_list(OP_ENTERSUB,
11259 OPf_STACKED|OPf_WANT_SCALAR,
11261 return newUNOP(OP_REFGEN, 0, anoncode);
11265 Perl_oopsAV(pTHX_ OP *o)
11268 PERL_ARGS_ASSERT_OOPSAV;
11270 switch (o->op_type) {
11273 OpTYPE_set(o, OP_PADAV);
11274 return ref(o, OP_RV2AV);
11278 OpTYPE_set(o, OP_RV2AV);
11283 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
11290 Perl_oopsHV(pTHX_ OP *o)
11293 PERL_ARGS_ASSERT_OOPSHV;
11295 switch (o->op_type) {
11298 OpTYPE_set(o, OP_PADHV);
11299 return ref(o, OP_RV2HV);
11303 OpTYPE_set(o, OP_RV2HV);
11304 /* rv2hv steals the bottom bit for its own uses */
11305 o->op_private &= ~OPpARG1_MASK;
11310 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
11317 Perl_newAVREF(pTHX_ OP *o)
11320 PERL_ARGS_ASSERT_NEWAVREF;
11322 if (o->op_type == OP_PADANY) {
11323 OpTYPE_set(o, OP_PADAV);
11326 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) {
11327 Perl_croak(aTHX_ "Can't use an array as a reference");
11329 return newUNOP(OP_RV2AV, 0, scalar(o));
11333 Perl_newGVREF(pTHX_ I32 type, OP *o)
11335 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
11336 return newUNOP(OP_NULL, 0, o);
11338 if (!FEATURE_BAREWORD_FILEHANDLES_IS_ENABLED &&
11339 ((PL_opargs[type] >> OASHIFT) & 7) == OA_FILEREF &&
11340 o->op_type == OP_CONST && (o->op_private & OPpCONST_BARE)) {
11341 no_bareword_filehandle(SvPVX(cSVOPo_sv));
11344 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
11348 Perl_newHVREF(pTHX_ OP *o)
11351 PERL_ARGS_ASSERT_NEWHVREF;
11353 if (o->op_type == OP_PADANY) {
11354 OpTYPE_set(o, OP_PADHV);
11357 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) {
11358 Perl_croak(aTHX_ "Can't use a hash as a reference");
11360 return newUNOP(OP_RV2HV, 0, scalar(o));
11364 Perl_newCVREF(pTHX_ I32 flags, OP *o)
11366 if (o->op_type == OP_PADANY) {
11367 OpTYPE_set(o, OP_PADCV);
11369 return newUNOP(OP_RV2CV, flags, scalar(o));
11373 Perl_newSVREF(pTHX_ OP *o)
11376 PERL_ARGS_ASSERT_NEWSVREF;
11378 if (o->op_type == OP_PADANY) {
11379 OpTYPE_set(o, OP_PADSV);
11383 return newUNOP(OP_RV2SV, 0, scalar(o));
11386 /* Check routines. See the comments at the top of this file for details
11387 * on when these are called */
11390 Perl_ck_anoncode(pTHX_ OP *o)
11392 PERL_ARGS_ASSERT_CK_ANONCODE;
11394 cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type);
11395 cSVOPo->op_sv = NULL;
11400 S_io_hints(pTHX_ OP *o)
11402 #if O_BINARY != 0 || O_TEXT != 0
11404 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;;
11406 SV **svp = hv_fetchs(table, "open_IN", FALSE);
11409 const char *d = SvPV_const(*svp, len);
11410 const I32 mode = mode_from_discipline(d, len);
11411 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */
11413 if (mode & O_BINARY)
11414 o->op_private |= OPpOPEN_IN_RAW;
11418 o->op_private |= OPpOPEN_IN_CRLF;
11422 svp = hv_fetchs(table, "open_OUT", FALSE);
11425 const char *d = SvPV_const(*svp, len);
11426 const I32 mode = mode_from_discipline(d, len);
11427 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */
11429 if (mode & O_BINARY)
11430 o->op_private |= OPpOPEN_OUT_RAW;
11434 o->op_private |= OPpOPEN_OUT_CRLF;
11439 PERL_UNUSED_CONTEXT;
11440 PERL_UNUSED_ARG(o);
11445 Perl_ck_backtick(pTHX_ OP *o)
11450 PERL_ARGS_ASSERT_CK_BACKTICK;
11452 /* qx and `` have a null pushmark; CORE::readpipe has only one kid. */
11453 if (o->op_flags & OPf_KIDS && (sibl = OpSIBLING(cUNOPo->op_first))
11454 && (gv = gv_override("readpipe",8)))
11456 /* detach rest of siblings from o and its first child */
11457 op_sibling_splice(o, cUNOPo->op_first, -1, NULL);
11458 newop = S_new_entersubop(aTHX_ gv, sibl);
11460 else if (!(o->op_flags & OPf_KIDS))
11461 newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP());
11466 S_io_hints(aTHX_ o);
11471 Perl_ck_bitop(pTHX_ OP *o)
11473 PERL_ARGS_ASSERT_CK_BITOP;
11475 /* get rid of arg count and indicate if in the scope of 'use integer' */
11476 o->op_private = (PL_hints & HINT_INTEGER) ? OPpUSEINT : 0;
11478 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
11479 && OP_IS_INFIX_BIT(o->op_type))
11481 const OP * const left = cBINOPo->op_first;
11482 const OP * const right = OpSIBLING(left);
11483 if ((OP_IS_NUMCOMPARE(left->op_type) &&
11484 (left->op_flags & OPf_PARENS) == 0) ||
11485 (OP_IS_NUMCOMPARE(right->op_type) &&
11486 (right->op_flags & OPf_PARENS) == 0))
11487 Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE),
11488 "Possible precedence problem on bitwise %s operator",
11489 o->op_type == OP_BIT_OR
11490 ||o->op_type == OP_NBIT_OR ? "|"
11491 : o->op_type == OP_BIT_AND
11492 ||o->op_type == OP_NBIT_AND ? "&"
11493 : o->op_type == OP_BIT_XOR
11494 ||o->op_type == OP_NBIT_XOR ? "^"
11495 : o->op_type == OP_SBIT_OR ? "|."
11496 : o->op_type == OP_SBIT_AND ? "&." : "^."
11502 PERL_STATIC_INLINE bool
11503 is_dollar_bracket(pTHX_ const OP * const o)
11506 PERL_UNUSED_CONTEXT;
11507 return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS
11508 && (kid = cUNOPx(o)->op_first)
11509 && kid->op_type == OP_GV
11510 && strEQ(GvNAME(cGVOPx_gv(kid)), "[");
11513 /* for lt, gt, le, ge, eq, ne and their i_ variants */
11516 Perl_ck_cmp(pTHX_ OP *o)
11522 OP *indexop, *constop, *start;
11526 PERL_ARGS_ASSERT_CK_CMP;
11528 is_eq = ( o->op_type == OP_EQ
11529 || o->op_type == OP_NE
11530 || o->op_type == OP_I_EQ
11531 || o->op_type == OP_I_NE);
11533 if (!is_eq && ckWARN(WARN_SYNTAX)) {
11534 const OP *kid = cUNOPo->op_first;
11537 ( is_dollar_bracket(aTHX_ kid)
11538 && OpSIBLING(kid) && OpSIBLING(kid)->op_type == OP_CONST
11540 || ( kid->op_type == OP_CONST
11541 && (kid = OpSIBLING(kid)) && is_dollar_bracket(aTHX_ kid)
11545 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
11546 "$[ used in %s (did you mean $] ?)", OP_DESC(o));
11549 /* convert (index(...) == -1) and variations into
11550 * (r)index/BOOL(,NEG)
11555 indexop = cUNOPo->op_first;
11556 constop = OpSIBLING(indexop);
11558 if (indexop->op_type == OP_CONST) {
11560 indexop = OpSIBLING(constop);
11565 if (indexop->op_type != OP_INDEX && indexop->op_type != OP_RINDEX)
11568 /* ($lex = index(....)) == -1 */
11569 if (indexop->op_private & OPpTARGET_MY)
11572 if (constop->op_type != OP_CONST)
11575 sv = cSVOPx_sv(constop);
11576 if (!(sv && SvIOK_notUV(sv)))
11580 if (iv != -1 && iv != 0)
11584 if (o->op_type == OP_LT || o->op_type == OP_I_LT) {
11585 if (!(iv0 ^ reverse))
11589 else if (o->op_type == OP_LE || o->op_type == OP_I_LE) {
11594 else if (o->op_type == OP_GE || o->op_type == OP_I_GE) {
11595 if (!(iv0 ^ reverse))
11599 else if (o->op_type == OP_GT || o->op_type == OP_I_GT) {
11604 else if (o->op_type == OP_EQ || o->op_type == OP_I_EQ) {
11610 assert(o->op_type == OP_NE || o->op_type == OP_I_NE);
11616 indexop->op_flags &= ~OPf_PARENS;
11617 indexop->op_flags |= (o->op_flags & OPf_PARENS);
11618 indexop->op_private |= OPpTRUEBOOL;
11620 indexop->op_private |= OPpINDEX_BOOLNEG;
11621 /* cut out the index op and free the eq,const ops */
11622 (void)op_sibling_splice(o, start, 1, NULL);
11630 Perl_ck_concat(pTHX_ OP *o)
11632 const OP * const kid = cUNOPo->op_first;
11634 PERL_ARGS_ASSERT_CK_CONCAT;
11635 PERL_UNUSED_CONTEXT;
11637 /* reuse the padtmp returned by the concat child */
11638 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
11639 !(kUNOP->op_first->op_flags & OPf_MOD))
11641 o->op_flags |= OPf_STACKED;
11642 o->op_private |= OPpCONCAT_NESTED;
11648 Perl_ck_spair(pTHX_ OP *o)
11651 PERL_ARGS_ASSERT_CK_SPAIR;
11653 if (o->op_flags & OPf_KIDS) {
11657 const OPCODE type = o->op_type;
11658 o = modkids(ck_fun(o), type);
11659 kid = cUNOPo->op_first;
11660 kidkid = kUNOP->op_first;
11661 newop = OpSIBLING(kidkid);
11663 const OPCODE type = newop->op_type;
11664 if (OpHAS_SIBLING(newop))
11666 if (o->op_type == OP_REFGEN
11667 && ( type == OP_RV2CV
11668 || ( !(newop->op_flags & OPf_PARENS)
11669 && ( type == OP_RV2AV || type == OP_PADAV
11670 || type == OP_RV2HV || type == OP_PADHV))))
11671 NOOP; /* OK (allow srefgen for \@a and \%h) */
11672 else if (OP_GIMME(newop,0) != G_SCALAR)
11675 /* excise first sibling */
11676 op_sibling_splice(kid, NULL, 1, NULL);
11679 /* transforms OP_REFGEN into OP_SREFGEN, OP_CHOP into OP_SCHOP,
11680 * and OP_CHOMP into OP_SCHOMP */
11681 o->op_ppaddr = PL_ppaddr[++o->op_type];
11686 Perl_ck_delete(pTHX_ OP *o)
11688 PERL_ARGS_ASSERT_CK_DELETE;
11692 if (o->op_flags & OPf_KIDS) {
11693 OP * const kid = cUNOPo->op_first;
11694 switch (kid->op_type) {
11696 o->op_flags |= OPf_SPECIAL;
11699 o->op_private |= OPpSLICE;
11702 o->op_flags |= OPf_SPECIAL;
11707 o->op_flags |= OPf_SPECIAL;
11710 o->op_private |= OPpKVSLICE;
11713 Perl_croak(aTHX_ "delete argument is not a HASH or ARRAY "
11714 "element or slice");
11716 if (kid->op_private & OPpLVAL_INTRO)
11717 o->op_private |= OPpLVAL_INTRO;
11724 Perl_ck_eof(pTHX_ OP *o)
11726 PERL_ARGS_ASSERT_CK_EOF;
11728 if (o->op_flags & OPf_KIDS) {
11730 if (cLISTOPo->op_first->op_type == OP_STUB) {
11732 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
11737 kid = cLISTOPo->op_first;
11738 if (kid->op_type == OP_RV2GV)
11739 kid->op_private |= OPpALLOW_FAKE;
11746 Perl_ck_eval(pTHX_ OP *o)
11749 PERL_ARGS_ASSERT_CK_EVAL;
11751 PL_hints |= HINT_BLOCK_SCOPE;
11752 if (o->op_flags & OPf_KIDS) {
11753 SVOP * const kid = cSVOPx(cUNOPo->op_first);
11756 if (o->op_type == OP_ENTERTRY) {
11759 /* cut whole sibling chain free from o */
11760 op_sibling_splice(o, NULL, -1, NULL);
11763 enter = alloc_LOGOP(OP_ENTERTRY, NULL, NULL);
11765 /* establish postfix order */
11766 enter->op_next = (OP*)enter;
11768 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
11769 OpTYPE_set(o, OP_LEAVETRY);
11770 enter->op_other = o;
11775 S_set_haseval(aTHX);
11779 const U8 priv = o->op_private;
11781 /* the newUNOP will recursively call ck_eval(), which will handle
11782 * all the stuff at the end of this function, like adding
11785 return newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP());
11787 o->op_targ = (PADOFFSET)PL_hints;
11788 if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8;
11789 if ((PL_hints & HINT_LOCALIZE_HH) != 0
11790 && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) {
11791 /* Store a copy of %^H that pp_entereval can pick up. */
11792 HV *hh = hv_copy_hints_hv(GvHV(PL_hintgv));
11794 STOREFEATUREBITSHH(hh);
11795 hhop = newSVOP(OP_HINTSEVAL, 0, MUTABLE_SV(hh));
11796 /* append hhop to only child */
11797 op_sibling_splice(o, cUNOPo->op_first, 0, hhop);
11799 o->op_private |= OPpEVAL_HAS_HH;
11801 if (!(o->op_private & OPpEVAL_BYTES)
11802 && FEATURE_UNIEVAL_IS_ENABLED)
11803 o->op_private |= OPpEVAL_UNICODE;
11808 Perl_ck_trycatch(pTHX_ OP *o)
11811 OP *to_free = NULL;
11812 OP *trykid, *catchkid;
11813 OP *catchroot, *catchstart;
11815 PERL_ARGS_ASSERT_CK_TRYCATCH;
11817 trykid = cUNOPo->op_first;
11818 if(trykid->op_type == OP_NULL || trykid->op_type == OP_PUSHMARK) {
11820 trykid = OpSIBLING(trykid);
11822 catchkid = OpSIBLING(trykid);
11824 assert(trykid->op_type == OP_POPTRY);
11825 assert(catchkid->op_type == OP_CATCH);
11827 /* cut whole sibling chain free from o */
11828 op_sibling_splice(o, NULL, -1, NULL);
11833 enter = alloc_LOGOP(OP_ENTERTRYCATCH, NULL, NULL);
11835 /* establish postfix order */
11836 enter->op_next = (OP*)enter;
11838 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, trykid);
11839 op_append_elem(OP_LINESEQ, (OP*)o, catchkid);
11841 OpTYPE_set(o, OP_LEAVETRYCATCH);
11843 /* The returned optree is actually threaded up slightly nonobviously in
11844 * terms of its ->op_next pointers.
11846 * This way, if the tryblock dies, its retop points at the OP_CATCH, but
11847 * if it does not then its leavetry skips over that and continues
11848 * execution past it.
11851 /* First, link up the actual body of the catch block */
11852 catchroot = OpSIBLING(cUNOPx(catchkid)->op_first);
11853 catchstart = LINKLIST(catchroot);
11854 cLOGOPx(catchkid)->op_other = catchstart;
11856 o->op_next = LINKLIST(o);
11858 /* die within try block should jump to the catch */
11859 enter->op_other = catchkid;
11861 /* after try block that doesn't die, just skip straight to leavetrycatch */
11862 trykid->op_next = o;
11864 /* after catch block, skip back up to the leavetrycatch */
11865 catchroot->op_next = o;
11871 Perl_ck_exec(pTHX_ OP *o)
11873 PERL_ARGS_ASSERT_CK_EXEC;
11875 if (o->op_flags & OPf_STACKED) {
11878 kid = OpSIBLING(cUNOPo->op_first);
11879 if (kid->op_type == OP_RV2GV)
11888 Perl_ck_exists(pTHX_ OP *o)
11890 PERL_ARGS_ASSERT_CK_EXISTS;
11893 if (o->op_flags & OPf_KIDS) {
11894 OP * const kid = cUNOPo->op_first;
11895 if (kid->op_type == OP_ENTERSUB) {
11896 (void) ref(kid, o->op_type);
11897 if (kid->op_type != OP_RV2CV
11898 && !(PL_parser && PL_parser->error_count))
11900 "exists argument is not a subroutine name");
11901 o->op_private |= OPpEXISTS_SUB;
11903 else if (kid->op_type == OP_AELEM)
11904 o->op_flags |= OPf_SPECIAL;
11905 else if (kid->op_type != OP_HELEM)
11906 Perl_croak(aTHX_ "exists argument is not a HASH or ARRAY "
11907 "element or a subroutine");
11914 Perl_ck_rvconst(pTHX_ OP *o)
11916 SVOP * const kid = cSVOPx(cUNOPo->op_first);
11918 PERL_ARGS_ASSERT_CK_RVCONST;
11920 if (o->op_type == OP_RV2HV)
11921 /* rv2hv steals the bottom bit for its own uses */
11922 o->op_private &= ~OPpARG1_MASK;
11924 o->op_private |= (PL_hints & HINT_STRICT_REFS);
11926 if (kid->op_type == OP_CONST) {
11929 SV * const kidsv = kid->op_sv;
11931 /* Is it a constant from cv_const_sv()? */
11932 if ((SvROK(kidsv) || isGV_with_GP(kidsv)) && SvREADONLY(kidsv)) {
11935 if (SvTYPE(kidsv) == SVt_PVAV) return o;
11936 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
11937 const char *badthing;
11938 switch (o->op_type) {
11940 badthing = "a SCALAR";
11943 badthing = "an ARRAY";
11946 badthing = "a HASH";
11954 "Can't use bareword (\"%" SVf "\") as %s ref while \"strict refs\" in use",
11955 SVfARG(kidsv), badthing);
11958 * This is a little tricky. We only want to add the symbol if we
11959 * didn't add it in the lexer. Otherwise we get duplicate strict
11960 * warnings. But if we didn't add it in the lexer, we must at
11961 * least pretend like we wanted to add it even if it existed before,
11962 * or we get possible typo warnings. OPpCONST_ENTERED says
11963 * whether the lexer already added THIS instance of this symbol.
11965 iscv = o->op_type == OP_RV2CV ? GV_NOEXPAND|GV_ADDMULTI : 0;
11966 gv = gv_fetchsv(kidsv,
11967 o->op_type == OP_RV2CV
11968 && o->op_private & OPpMAY_RETURN_CONSTANT
11970 : iscv | !(kid->op_private & OPpCONST_ENTERED),
11973 : o->op_type == OP_RV2SV
11975 : o->op_type == OP_RV2AV
11977 : o->op_type == OP_RV2HV
11984 if (!(o->op_private & OPpMAY_RETURN_CONSTANT)
11985 && SvTYPE(SvRV(gv)) != SVt_PVCV)
11986 gv_fetchsv(kidsv, GV_ADDMULTI, SVt_PVCV);
11988 OpTYPE_set(kid, OP_GV);
11989 SvREFCNT_dec(kid->op_sv);
11990 #ifdef USE_ITHREADS
11991 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
11992 STATIC_ASSERT_STMT(sizeof(PADOP) <= sizeof(SVOP));
11993 kPADOP->op_padix = pad_alloc(OP_GV, SVf_READONLY);
11994 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
11995 PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv)));
11997 kid->op_sv = SvREFCNT_inc_simple_NN(gv);
11999 kid->op_private = 0;
12000 /* FAKE globs in the symbol table cause weird bugs (#77810) */
12008 Perl_ck_ftst(pTHX_ OP *o)
12010 const I32 type = o->op_type;
12012 PERL_ARGS_ASSERT_CK_FTST;
12014 if (o->op_flags & OPf_REF) {
12017 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
12018 SVOP * const kid = cSVOPx(cUNOPo->op_first);
12019 const OPCODE kidtype = kid->op_type;
12021 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)
12022 && !kid->op_folded) {
12023 if (!FEATURE_BAREWORD_FILEHANDLES_IS_ENABLED) {
12024 no_bareword_filehandle(SvPVX(kSVOP_sv));
12026 OP * const newop = newGVOP(type, OPf_REF,
12027 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
12032 if ((kidtype == OP_RV2AV || kidtype == OP_PADAV) && ckWARN(WARN_SYNTAX)) {
12033 SV *name = S_op_varname_subscript(aTHX_ (OP*)kid, 2);
12035 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */
12036 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s (did you want stat %" SVf "?)",
12037 array_passed_to_stat, name);
12040 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */
12041 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s", array_passed_to_stat);
12044 scalar((OP *) kid);
12045 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type))
12046 o->op_private |= OPpFT_ACCESS;
12047 if (OP_IS_FILETEST(type)
12048 && OP_IS_FILETEST(kidtype)
12050 o->op_private |= OPpFT_STACKED;
12051 kid->op_private |= OPpFT_STACKING;
12052 if (kidtype == OP_FTTTY && (
12053 !(kid->op_private & OPpFT_STACKED)
12054 || kid->op_private & OPpFT_AFTER_t
12056 o->op_private |= OPpFT_AFTER_t;
12061 if (type == OP_FTTTY)
12062 o = newGVOP(type, OPf_REF, PL_stdingv);
12064 o = newUNOP(type, 0, newDEFSVOP());
12070 Perl_ck_fun(pTHX_ OP *o)
12072 const int type = o->op_type;
12073 I32 oa = PL_opargs[type] >> OASHIFT;
12075 PERL_ARGS_ASSERT_CK_FUN;
12077 if (o->op_flags & OPf_STACKED) {
12078 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
12079 oa &= ~OA_OPTIONAL;
12081 return no_fh_allowed(o);
12084 if (o->op_flags & OPf_KIDS) {
12085 OP *prev_kid = NULL;
12086 OP *kid = cLISTOPo->op_first;
12088 bool seen_optional = FALSE;
12090 if (kid->op_type == OP_PUSHMARK ||
12091 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
12094 kid = OpSIBLING(kid);
12096 if (kid && kid->op_type == OP_COREARGS) {
12097 bool optional = FALSE;
12100 if (oa & OA_OPTIONAL) optional = TRUE;
12103 if (optional) o->op_private |= numargs;
12108 if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) {
12109 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV) {
12110 kid = newDEFSVOP();
12111 /* append kid to chain */
12112 op_sibling_splice(o, prev_kid, 0, kid);
12114 seen_optional = TRUE;
12121 /* list seen where single (scalar) arg expected? */
12122 if (numargs == 1 && !(oa >> 4)
12123 && kid->op_type == OP_LIST && type != OP_SCALAR)
12125 return too_many_arguments_pv(o,PL_op_desc[type], 0);
12127 if (type != OP_DELETE) scalar(kid);
12138 if ((type == OP_PUSH || type == OP_UNSHIFT)
12139 && !OpHAS_SIBLING(kid))
12140 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
12141 "Useless use of %s with no values",
12144 if (kid->op_type == OP_CONST
12145 && ( !SvROK(cSVOPx_sv(kid))
12146 || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV )
12148 bad_type_pv(numargs, "array", o, kid);
12149 else if (kid->op_type == OP_RV2HV || kid->op_type == OP_PADHV
12150 || kid->op_type == OP_RV2GV) {
12151 bad_type_pv(1, "array", o, kid);
12153 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV) {
12154 yyerror_pv(Perl_form(aTHX_ "Experimental %s on scalar is now forbidden",
12155 PL_op_desc[type]), 0);
12158 op_lvalue(kid, type);
12162 if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
12163 bad_type_pv(numargs, "hash", o, kid);
12164 op_lvalue(kid, type);
12168 /* replace kid with newop in chain */
12170 S_op_sibling_newUNOP(aTHX_ o, prev_kid, OP_NULL, 0);
12171 newop->op_next = newop;
12176 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
12177 if (kid->op_type == OP_CONST &&
12178 (kid->op_private & OPpCONST_BARE))
12180 OP * const newop = newGVOP(OP_GV, 0,
12181 gv_fetchsv(kSVOP->op_sv, GV_ADD, SVt_PVIO));
12182 /* a first argument is handled by toke.c, ideally we'd
12183 just check here but several ops don't use ck_fun() */
12184 if (!FEATURE_BAREWORD_FILEHANDLES_IS_ENABLED) {
12185 no_bareword_filehandle(SvPVX(kSVOP_sv));
12187 /* replace kid with newop in chain */
12188 op_sibling_splice(o, prev_kid, 1, newop);
12192 else if (kid->op_type == OP_READLINE) {
12193 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
12194 bad_type_pv(numargs, "HANDLE", o, kid);
12197 I32 flags = OPf_SPECIAL;
12199 PADOFFSET targ = 0;
12201 /* is this op a FH constructor? */
12202 if (is_handle_constructor(o,numargs)) {
12203 const char *name = NULL;
12206 bool want_dollar = TRUE;
12209 /* Set a flag to tell rv2gv to vivify
12210 * need to "prove" flag does not mean something
12211 * else already - NI-S 1999/05/07
12214 if (kid->op_type == OP_PADSV) {
12216 = PAD_COMPNAME_SV(kid->op_targ);
12217 name = PadnamePV (pn);
12218 len = PadnameLEN(pn);
12219 name_utf8 = PadnameUTF8(pn);
12221 else if (kid->op_type == OP_RV2SV
12222 && kUNOP->op_first->op_type == OP_GV)
12224 GV * const gv = cGVOPx_gv(kUNOP->op_first);
12226 len = GvNAMELEN(gv);
12227 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0;
12229 else if (kid->op_type == OP_AELEM
12230 || kid->op_type == OP_HELEM)
12233 OP *op = kBINOP->op_first;
12237 const char * const a =
12238 kid->op_type == OP_AELEM ?
12240 if (((op->op_type == OP_RV2AV) ||
12241 (op->op_type == OP_RV2HV)) &&
12242 (firstop = cUNOPx(op)->op_first) &&
12243 (firstop->op_type == OP_GV)) {
12244 /* packagevar $a[] or $h{} */
12245 GV * const gv = cGVOPx_gv(firstop);
12248 Perl_newSVpvf(aTHX_
12253 else if (op->op_type == OP_PADAV
12254 || op->op_type == OP_PADHV) {
12255 /* lexicalvar $a[] or $h{} */
12256 const char * const padname =
12257 PAD_COMPNAME_PV(op->op_targ);
12260 Perl_newSVpvf(aTHX_
12266 name = SvPV_const(tmpstr, len);
12267 name_utf8 = SvUTF8(tmpstr);
12268 sv_2mortal(tmpstr);
12272 name = "__ANONIO__";
12274 want_dollar = FALSE;
12276 op_lvalue(kid, type);
12280 targ = pad_alloc(OP_RV2GV, SVf_READONLY);
12281 namesv = PAD_SVl(targ);
12282 if (want_dollar && *name != '$')
12283 sv_setpvs(namesv, "$");
12286 sv_catpvn(namesv, name, len);
12287 if ( name_utf8 ) SvUTF8_on(namesv);
12291 kid = S_op_sibling_newUNOP(aTHX_ o, prev_kid,
12293 kid->op_targ = targ;
12294 kid->op_private |= priv;
12300 if ((type == OP_UNDEF || type == OP_POS)
12301 && numargs == 1 && !(oa >> 4)
12302 && kid->op_type == OP_LIST)
12303 return too_many_arguments_pv(o,PL_op_desc[type], 0);
12304 op_lvalue(scalar(kid), type);
12309 kid = OpSIBLING(kid);
12311 /* FIXME - should the numargs or-ing move after the too many
12312 * arguments check? */
12313 o->op_private |= numargs;
12315 return too_many_arguments_pv(o,OP_DESC(o), 0);
12318 else if (PL_opargs[type] & OA_DEFGV) {
12319 /* Ordering of these two is important to keep f_map.t passing. */
12321 return newUNOP(type, 0, newDEFSVOP());
12325 while (oa & OA_OPTIONAL)
12327 if (oa && oa != OA_LIST)
12328 return too_few_arguments_pv(o,OP_DESC(o), 0);
12334 Perl_ck_glob(pTHX_ OP *o)
12338 PERL_ARGS_ASSERT_CK_GLOB;
12341 if ((o->op_flags & OPf_KIDS) && !OpHAS_SIBLING(cLISTOPo->op_first))
12342 op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */
12344 if (!(o->op_flags & OPf_SPECIAL) && (gv = gv_override("glob", 4)))
12348 * \ null - const(wildcard)
12353 * \ mark - glob - rv2cv
12354 * | \ gv(CORE::GLOBAL::glob)
12356 * \ null - const(wildcard)
12358 o->op_flags |= OPf_SPECIAL;
12359 o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP);
12360 o = S_new_entersubop(aTHX_ gv, o);
12361 o = newUNOP(OP_NULL, 0, o);
12362 o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */
12365 else o->op_flags &= ~OPf_SPECIAL;
12366 #if !defined(PERL_EXTERNAL_GLOB)
12367 if (!PL_globhook) {
12369 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
12370 newSVpvs("File::Glob"), NULL, NULL, NULL);
12373 #endif /* !PERL_EXTERNAL_GLOB */
12374 gv = (GV *)newSV_type(SVt_NULL);
12375 gv_init(gv, 0, "", 0, 0);
12377 op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
12378 SvREFCNT_dec_NN(gv); /* newGVOP increased it */
12384 Perl_ck_grep(pTHX_ OP *o)
12388 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
12390 PERL_ARGS_ASSERT_CK_GREP;
12392 /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */
12394 if (o->op_flags & OPf_STACKED) {
12395 kid = cUNOPx(OpSIBLING(cLISTOPo->op_first))->op_first;
12396 if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE)
12397 return no_fh_allowed(o);
12398 o->op_flags &= ~OPf_STACKED;
12400 kid = OpSIBLING(cLISTOPo->op_first);
12401 if (type == OP_MAPWHILE)
12406 if (PL_parser && PL_parser->error_count)
12408 kid = OpSIBLING(cLISTOPo->op_first);
12409 if (kid->op_type != OP_NULL)
12410 Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type);
12411 kid = kUNOP->op_first;
12413 gwop = alloc_LOGOP(type, o, LINKLIST(kid));
12414 kid->op_next = (OP*)gwop;
12415 o->op_private = gwop->op_private = 0;
12416 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
12418 kid = OpSIBLING(cLISTOPo->op_first);
12419 for (kid = OpSIBLING(kid); kid; kid = OpSIBLING(kid))
12420 op_lvalue(kid, OP_GREPSTART);
12426 Perl_ck_index(pTHX_ OP *o)
12428 PERL_ARGS_ASSERT_CK_INDEX;
12430 if (o->op_flags & OPf_KIDS) {
12431 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
12433 kid = OpSIBLING(kid); /* get past "big" */
12434 if (kid && kid->op_type == OP_CONST) {
12435 const bool save_taint = TAINT_get;
12436 SV *sv = kSVOP->op_sv;
12437 if ( (!SvPOK(sv) || SvNIOKp(sv) || isREGEXP(sv))
12438 && SvOK(sv) && !SvROK(sv))
12440 sv = newSV_type(SVt_NULL);
12441 sv_copypv(sv, kSVOP->op_sv);
12442 SvREFCNT_dec_NN(kSVOP->op_sv);
12445 if (SvOK(sv)) fbm_compile(sv, 0);
12446 TAINT_set(save_taint);
12447 #ifdef NO_TAINT_SUPPORT
12448 PERL_UNUSED_VAR(save_taint);
12456 Perl_ck_lfun(pTHX_ OP *o)
12458 const OPCODE type = o->op_type;
12460 PERL_ARGS_ASSERT_CK_LFUN;
12462 return modkids(ck_fun(o), type);
12466 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
12468 PERL_ARGS_ASSERT_CK_DEFINED;
12470 if ((o->op_flags & OPf_KIDS)) {
12471 switch (cUNOPo->op_first->op_type) {
12474 Perl_croak(aTHX_ "Can't use 'defined(@array)'"
12475 " (Maybe you should just omit the defined()?)");
12476 NOT_REACHED; /* NOTREACHED */
12480 Perl_croak(aTHX_ "Can't use 'defined(%%hash)'"
12481 " (Maybe you should just omit the defined()?)");
12482 NOT_REACHED; /* NOTREACHED */
12493 Perl_ck_readline(pTHX_ OP *o)
12495 PERL_ARGS_ASSERT_CK_READLINE;
12497 if (o->op_flags & OPf_KIDS) {
12498 OP *kid = cLISTOPo->op_first;
12499 if (!FEATURE_BAREWORD_FILEHANDLES_IS_ENABLED
12500 && kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
12501 no_bareword_filehandle(SvPVX(kSVOP_sv));
12503 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
12508 = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv));
12516 Perl_ck_rfun(pTHX_ OP *o)
12518 const OPCODE type = o->op_type;
12520 PERL_ARGS_ASSERT_CK_RFUN;
12522 return refkids(ck_fun(o), type);
12526 Perl_ck_listiob(pTHX_ OP *o)
12530 PERL_ARGS_ASSERT_CK_LISTIOB;
12532 kid = cLISTOPo->op_first;
12534 o = force_list(o, TRUE);
12535 kid = cLISTOPo->op_first;
12537 if (kid->op_type == OP_PUSHMARK)
12538 kid = OpSIBLING(kid);
12539 if (kid && o->op_flags & OPf_STACKED)
12540 kid = OpSIBLING(kid);
12541 else if (kid && !OpHAS_SIBLING(kid)) { /* print HANDLE; */
12542 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE
12543 && !kid->op_folded) {
12544 if (!FEATURE_BAREWORD_FILEHANDLES_IS_ENABLED) {
12545 no_bareword_filehandle(SvPVX(kSVOP_sv));
12547 o->op_flags |= OPf_STACKED; /* make it a filehandle */
12549 /* replace old const op with new OP_RV2GV parent */
12550 kid = S_op_sibling_newUNOP(aTHX_ o, cLISTOPo->op_first,
12551 OP_RV2GV, OPf_REF);
12552 kid = OpSIBLING(kid);
12557 op_append_elem(o->op_type, o, newDEFSVOP());
12559 if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF);
12560 return listkids(o);
12564 Perl_ck_smartmatch(pTHX_ OP *o)
12566 PERL_ARGS_ASSERT_CK_SMARTMATCH;
12567 if (0 == (o->op_flags & OPf_SPECIAL)) {
12568 OP *first = cBINOPo->op_first;
12569 OP *second = OpSIBLING(first);
12571 /* Implicitly take a reference to an array or hash */
12573 /* remove the original two siblings, then add back the
12574 * (possibly different) first and second sibs.
12576 op_sibling_splice(o, NULL, 1, NULL);
12577 op_sibling_splice(o, NULL, 1, NULL);
12578 first = ref_array_or_hash(first);
12579 second = ref_array_or_hash(second);
12580 op_sibling_splice(o, NULL, 0, second);
12581 op_sibling_splice(o, NULL, 0, first);
12583 /* Implicitly take a reference to a regular expression */
12584 if (first->op_type == OP_MATCH && !(first->op_flags & OPf_STACKED)) {
12585 OpTYPE_set(first, OP_QR);
12587 if (second->op_type == OP_MATCH && !(second->op_flags & OPf_STACKED)) {
12588 OpTYPE_set(second, OP_QR);
12597 S_maybe_targlex(pTHX_ OP *o)
12599 OP * const kid = cLISTOPo->op_first;
12600 /* has a disposable target? */
12601 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
12602 && !(kid->op_flags & OPf_STACKED)
12603 /* Cannot steal the second time! */
12604 && !(kid->op_private & OPpTARGET_MY)
12607 OP * const kkid = OpSIBLING(kid);
12609 /* Can just relocate the target. */
12610 if (kkid && kkid->op_type == OP_PADSV
12611 && (!(kkid->op_private & OPpLVAL_INTRO)
12612 || kkid->op_private & OPpPAD_STATE))
12614 kid->op_targ = kkid->op_targ;
12616 /* Now we do not need PADSV and SASSIGN.
12617 * Detach kid and free the rest. */
12618 op_sibling_splice(o, NULL, 1, NULL);
12620 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
12628 Perl_ck_sassign(pTHX_ OP *o)
12630 OP * const kid = cBINOPo->op_first;
12632 PERL_ARGS_ASSERT_CK_SASSIGN;
12634 if (OpHAS_SIBLING(kid)) {
12635 OP *kkid = OpSIBLING(kid);
12636 /* For state variable assignment with attributes, kkid is a list op
12637 whose op_last is a padsv. */
12638 if ((kkid->op_type == OP_PADSV ||
12639 (OP_TYPE_IS_OR_WAS(kkid, OP_LIST) &&
12640 (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV
12643 && (kkid->op_private & (OPpLVAL_INTRO|OPpPAD_STATE))
12644 == (OPpLVAL_INTRO|OPpPAD_STATE)) {
12645 return S_newONCEOP(aTHX_ o, kkid);
12648 return S_maybe_targlex(aTHX_ o);
12653 Perl_ck_match(pTHX_ OP *o)
12655 PERL_UNUSED_CONTEXT;
12656 PERL_ARGS_ASSERT_CK_MATCH;
12662 Perl_ck_method(pTHX_ OP *o)
12664 SV *sv, *methsv, *rclass;
12665 const char* method;
12668 STRLEN len, nsplit = 0, i;
12670 OP * const kid = cUNOPo->op_first;
12672 PERL_ARGS_ASSERT_CK_METHOD;
12673 if (kid->op_type != OP_CONST) return o;
12677 /* replace ' with :: */
12678 while ((compatptr = (char *) memchr(SvPVX(sv), '\'',
12679 SvEND(sv) - SvPVX(sv) )))
12682 sv_insert(sv, compatptr - SvPVX_const(sv), 0, ":", 1);
12685 method = SvPVX_const(sv);
12687 utf8 = SvUTF8(sv) ? -1 : 1;
12689 for (i = len - 1; i > 0; --i) if (method[i] == ':') {
12694 methsv = newSVpvn_share(method+nsplit, utf8*(len - nsplit), 0);
12696 if (!nsplit) { /* $proto->method() */
12698 return newMETHOP_named(OP_METHOD_NAMED, 0, methsv);
12701 if (memEQs(method, nsplit, "SUPER::")) { /* $proto->SUPER::method() */
12703 return newMETHOP_named(OP_METHOD_SUPER, 0, methsv);
12706 /* $proto->MyClass::method() and $proto->MyClass::SUPER::method() */
12707 if (nsplit >= 9 && strBEGINs(method+nsplit-9, "::SUPER::")) {
12708 rclass = newSVpvn_share(method, utf8*(nsplit-9), 0);
12709 new_op = newMETHOP_named(OP_METHOD_REDIR_SUPER, 0, methsv);
12711 rclass = newSVpvn_share(method, utf8*(nsplit-2), 0);
12712 new_op = newMETHOP_named(OP_METHOD_REDIR, 0, methsv);
12714 #ifdef USE_ITHREADS
12715 op_relocate_sv(&rclass, &cMETHOPx(new_op)->op_rclass_targ);
12717 cMETHOPx(new_op)->op_rclass_sv = rclass;
12724 Perl_ck_null(pTHX_ OP *o)
12726 PERL_ARGS_ASSERT_CK_NULL;
12727 PERL_UNUSED_CONTEXT;
12732 Perl_ck_open(pTHX_ OP *o)
12734 PERL_ARGS_ASSERT_CK_OPEN;
12736 S_io_hints(aTHX_ o);
12738 /* In case of three-arg dup open remove strictness
12739 * from the last arg if it is a bareword. */
12740 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
12741 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
12745 if ((last->op_type == OP_CONST) && /* The bareword. */
12746 (last->op_private & OPpCONST_BARE) &&
12747 (last->op_private & OPpCONST_STRICT) &&
12748 (oa = OpSIBLING(first)) && /* The fh. */
12749 (oa = OpSIBLING(oa)) && /* The mode. */
12750 (oa->op_type == OP_CONST) &&
12751 SvPOK(cSVOPx(oa)->op_sv) &&
12752 (mode = SvPVX_const(cSVOPx(oa)->op_sv)) &&
12753 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
12754 (last == OpSIBLING(oa))) /* The bareword. */
12755 last->op_private &= ~OPpCONST_STRICT;
12761 Perl_ck_prototype(pTHX_ OP *o)
12763 PERL_ARGS_ASSERT_CK_PROTOTYPE;
12764 if (!(o->op_flags & OPf_KIDS)) {
12766 return newUNOP(OP_PROTOTYPE, 0, newDEFSVOP());
12772 Perl_ck_refassign(pTHX_ OP *o)
12774 OP * const right = cLISTOPo->op_first;
12775 OP * const left = OpSIBLING(right);
12776 OP *varop = cUNOPx(cUNOPx(left)->op_first)->op_first;
12779 PERL_ARGS_ASSERT_CK_REFASSIGN;
12781 assert (left->op_type == OP_SREFGEN);
12784 /* we use OPpPAD_STATE in refassign to mean either of those things,
12785 * and the code assumes the two flags occupy the same bit position
12786 * in the various ops below */
12787 assert(OPpPAD_STATE == OPpOUR_INTRO);
12789 switch (varop->op_type) {
12791 o->op_private |= OPpLVREF_AV;
12794 o->op_private |= OPpLVREF_HV;
12798 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE));
12799 o->op_targ = varop->op_targ;
12800 varop->op_targ = 0;
12801 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
12805 o->op_private |= OPpLVREF_AV;
12807 NOT_REACHED; /* NOTREACHED */
12809 o->op_private |= OPpLVREF_HV;
12813 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO));
12814 if (cUNOPx(varop)->op_first->op_type != OP_GV) goto bad;
12816 /* Point varop to its GV kid, detached. */
12817 varop = op_sibling_splice(varop, NULL, -1, NULL);
12821 OP * const kidparent =
12822 OpSIBLING(cUNOPx(cUNOPx(varop)->op_first)->op_first);
12823 OP * const kid = cUNOPx(kidparent)->op_first;
12824 o->op_private |= OPpLVREF_CV;
12825 if (kid->op_type == OP_GV) {
12826 SV *sv = (SV*)cGVOPx_gv(kid);
12828 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
12829 /* a CVREF here confuses pp_refassign, so make sure
12831 CV *const cv = (CV*)SvRV(sv);
12832 SV *name_sv = newSVhek_mortal(CvNAME_HEK(cv));
12833 (void)gv_init_sv((GV*)sv, CvSTASH(cv), name_sv, 0);
12834 assert(SvTYPE(sv) == SVt_PVGV);
12836 goto detach_and_stack;
12838 if (kid->op_type != OP_PADCV) goto bad;
12839 o->op_targ = kid->op_targ;
12845 o->op_private |= (varop->op_private & OPpLVAL_INTRO);
12846 o->op_private |= OPpLVREF_ELEM;
12849 /* Detach varop. */
12850 op_sibling_splice(cUNOPx(left)->op_first, NULL, -1, NULL);
12854 /* diag_listed_as: Can't modify reference to %s in %s assignment */
12855 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in scalar "
12860 if (!FEATURE_REFALIASING_IS_ENABLED)
12862 "Experimental aliasing via reference not enabled");
12863 Perl_ck_warner_d(aTHX_
12864 packWARN(WARN_EXPERIMENTAL__REFALIASING),
12865 "Aliasing via reference is experimental");
12867 o->op_flags |= OPf_STACKED;
12868 op_sibling_splice(o, right, 1, varop);
12871 o->op_flags &=~ OPf_STACKED;
12872 op_sibling_splice(o, right, 1, NULL);
12879 Perl_ck_repeat(pTHX_ OP *o)
12881 PERL_ARGS_ASSERT_CK_REPEAT;
12883 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
12885 o->op_private |= OPpREPEAT_DOLIST;
12886 kids = op_sibling_splice(o, NULL, 1, NULL); /* detach first kid */
12887 kids = force_list(kids, TRUE); /* promote it to a list */
12888 op_sibling_splice(o, NULL, 0, kids); /* and add back */
12896 Perl_ck_require(pTHX_ OP *o)
12900 PERL_ARGS_ASSERT_CK_REQUIRE;
12902 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
12903 SVOP * const kid = cSVOPx(cUNOPo->op_first);
12907 if (kid->op_type == OP_CONST) {
12908 SV * const sv = kid->op_sv;
12909 U32 const was_readonly = SvREADONLY(sv);
12910 if (kid->op_private & OPpCONST_BARE) {
12914 if (was_readonly) {
12915 SvREADONLY_off(sv);
12918 if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0);
12923 /* treat ::foo::bar as foo::bar */
12924 if (len >= 2 && s[0] == ':' && s[1] == ':')
12925 DIE(aTHX_ "Bareword in require must not start with a double-colon: \"%s\"\n", s);
12927 DIE(aTHX_ "Bareword in require maps to empty filename");
12929 for (; s < end; s++) {
12930 if (*s == ':' && s[1] == ':') {
12932 Move(s+2, s+1, end - s - 1, char);
12936 SvEND_set(sv, end);
12937 sv_catpvs(sv, ".pm");
12938 PERL_HASH(hash, SvPVX(sv), SvCUR(sv));
12939 hek = share_hek(SvPVX(sv),
12940 (SSize_t)SvCUR(sv) * (SvUTF8(sv) ? -1 : 1),
12942 sv_sethek(sv, hek);
12944 SvFLAGS(sv) |= was_readonly;
12946 else if (SvPOK(sv) && !SvNIOK(sv) && !SvGMAGICAL(sv)
12949 if (SvREFCNT(sv) > 1) {
12950 kid->op_sv = newSVpvn_share(
12951 s, SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len, 0);
12952 SvREFCNT_dec_NN(sv);
12956 if (was_readonly) SvREADONLY_off(sv);
12957 PERL_HASH(hash, s, len);
12959 SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len,
12961 sv_sethek(sv, hek);
12963 SvFLAGS(sv) |= was_readonly;
12969 if (!(o->op_flags & OPf_SPECIAL) /* Wasn't written as CORE::require */
12970 /* handle override, if any */
12971 && (gv = gv_override("require", 7))) {
12973 if (o->op_flags & OPf_KIDS) {
12974 kid = cUNOPo->op_first;
12975 op_sibling_splice(o, NULL, -1, NULL);
12978 kid = newDEFSVOP();
12981 newop = S_new_entersubop(aTHX_ gv, kid);
12989 Perl_ck_return(pTHX_ OP *o)
12993 PERL_ARGS_ASSERT_CK_RETURN;
12995 kid = OpSIBLING(cLISTOPo->op_first);
12996 if (PL_compcv && CvLVALUE(PL_compcv)) {
12997 for (; kid; kid = OpSIBLING(kid))
12998 op_lvalue(kid, OP_LEAVESUBLV);
13005 Perl_ck_select(pTHX_ OP *o)
13009 PERL_ARGS_ASSERT_CK_SELECT;
13011 if (o->op_flags & OPf_KIDS) {
13012 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
13013 if (kid && OpHAS_SIBLING(kid)) {
13014 OpTYPE_set(o, OP_SSELECT);
13016 return fold_constants(op_integerize(op_std_init(o)));
13020 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
13021 if (kid && kid->op_type == OP_RV2GV)
13022 kid->op_private &= ~HINT_STRICT_REFS;
13027 Perl_ck_shift(pTHX_ OP *o)
13029 const I32 type = o->op_type;
13031 PERL_ARGS_ASSERT_CK_SHIFT;
13033 if (!(o->op_flags & OPf_KIDS)) {
13036 if (!CvUNIQUE(PL_compcv)) {
13037 o->op_flags |= OPf_SPECIAL;
13041 argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv)));
13043 return newUNOP(type, 0, scalar(argop));
13045 return scalar(ck_fun(o));
13049 Perl_ck_sort(pTHX_ OP *o)
13055 PERL_ARGS_ASSERT_CK_SORT;
13057 if (o->op_flags & OPf_STACKED)
13059 firstkid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
13062 return too_few_arguments_pv(o,OP_DESC(o), 0);
13064 if ((stacked = o->op_flags & OPf_STACKED)) { /* may have been cleared */
13065 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
13067 /* if the first arg is a code block, process it and mark sort as
13069 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
13071 if (kid->op_type == OP_LEAVE)
13072 op_null(kid); /* wipe out leave */
13073 /* Prevent execution from escaping out of the sort block. */
13076 /* provide scalar context for comparison function/block */
13077 kid = scalar(firstkid);
13078 kid->op_next = kid;
13079 o->op_flags |= OPf_SPECIAL;
13081 else if (kid->op_type == OP_CONST
13082 && kid->op_private & OPpCONST_BARE) {
13086 const char * const name = SvPV(kSVOP_sv, len);
13088 assert (len < 256);
13089 Copy(name, tmpbuf+1, len, char);
13090 off = pad_findmy_pvn(tmpbuf, len+1, 0);
13091 if (off != NOT_IN_PAD) {
13092 if (PAD_COMPNAME_FLAGS_isOUR(off)) {
13094 newSVhek(HvNAME_HEK(PAD_COMPNAME_OURSTASH(off)));
13095 sv_catpvs(fq, "::");
13096 sv_catsv(fq, kSVOP_sv);
13097 SvREFCNT_dec_NN(kSVOP_sv);
13101 OP * const padop = newOP(OP_PADCV, 0);
13102 padop->op_targ = off;
13103 /* replace the const op with the pad op */
13104 op_sibling_splice(firstkid, NULL, 1, padop);
13110 firstkid = OpSIBLING(firstkid);
13113 for (kid = firstkid; kid; kid = OpSIBLING(kid)) {
13114 /* provide list context for arguments */
13117 op_lvalue(kid, OP_GREPSTART);
13123 /* for sort { X } ..., where X is one of
13124 * $a <=> $b, $b <=> $a, $a cmp $b, $b cmp $a
13125 * elide the second child of the sort (the one containing X),
13126 * and set these flags as appropriate
13130 * Also, check and warn on lexical $a, $b.
13134 S_simplify_sort(pTHX_ OP *o)
13136 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */
13140 const char *gvname;
13143 PERL_ARGS_ASSERT_SIMPLIFY_SORT;
13145 kid = kUNOP->op_first; /* get past null */
13146 if (!(have_scopeop = kid->op_type == OP_SCOPE)
13147 && kid->op_type != OP_LEAVE)
13149 kid = kLISTOP->op_last; /* get past scope */
13150 switch(kid->op_type) {
13154 if (!have_scopeop) goto padkids;
13159 k = kid; /* remember this node*/
13160 if (kBINOP->op_first->op_type != OP_RV2SV
13161 || kBINOP->op_last ->op_type != OP_RV2SV)
13164 Warn about my($a) or my($b) in a sort block, *if* $a or $b is
13165 then used in a comparison. This catches most, but not
13166 all cases. For instance, it catches
13167 sort { my($a); $a <=> $b }
13169 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; }
13170 (although why you'd do that is anyone's guess).
13174 if (!ckWARN(WARN_SYNTAX)) return;
13175 kid = kBINOP->op_first;
13177 if (kid->op_type == OP_PADSV) {
13178 PADNAME * const name = PAD_COMPNAME(kid->op_targ);
13179 if (PadnameLEN(name) == 2 && *PadnamePV(name) == '$'
13180 && ( PadnamePV(name)[1] == 'a'
13181 || PadnamePV(name)[1] == 'b' ))
13182 /* diag_listed_as: "my %s" used in sort comparison */
13183 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
13184 "\"%s %s\" used in sort comparison",
13185 PadnameIsSTATE(name)
13190 } while ((kid = OpSIBLING(kid)));
13193 kid = kBINOP->op_first; /* get past cmp */
13194 if (kUNOP->op_first->op_type != OP_GV)
13196 kid = kUNOP->op_first; /* get past rv2sv */
13198 if (GvSTASH(gv) != PL_curstash)
13200 gvname = GvNAME(gv);
13201 if (*gvname == 'a' && gvname[1] == '\0')
13203 else if (*gvname == 'b' && gvname[1] == '\0')
13208 kid = k; /* back to cmp */
13209 /* already checked above that it is rv2sv */
13210 kid = kBINOP->op_last; /* down to 2nd arg */
13211 if (kUNOP->op_first->op_type != OP_GV)
13213 kid = kUNOP->op_first; /* get past rv2sv */
13215 if (GvSTASH(gv) != PL_curstash)
13217 gvname = GvNAME(gv);
13219 ? !(*gvname == 'a' && gvname[1] == '\0')
13220 : !(*gvname == 'b' && gvname[1] == '\0'))
13222 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
13224 o->op_private |= OPpSORT_DESCEND;
13225 if (k->op_type == OP_NCMP)
13226 o->op_private |= OPpSORT_NUMERIC;
13227 if (k->op_type == OP_I_NCMP)
13228 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
13229 kid = OpSIBLING(cLISTOPo->op_first);
13230 /* cut out and delete old block (second sibling) */
13231 op_sibling_splice(o, cLISTOPo->op_first, 1, NULL);
13236 Perl_ck_split(pTHX_ OP *o)
13241 PERL_ARGS_ASSERT_CK_SPLIT;
13243 assert(o->op_type == OP_LIST);
13245 if (o->op_flags & OPf_STACKED)
13246 return no_fh_allowed(o);
13248 kid = cLISTOPo->op_first;
13249 /* delete leading NULL node, then add a CONST if no other nodes */
13250 assert(kid->op_type == OP_NULL);
13251 op_sibling_splice(o, NULL, 1,
13252 OpHAS_SIBLING(kid) ? NULL : newSVOP(OP_CONST, 0, newSVpvs(" ")));
13254 kid = cLISTOPo->op_first;
13256 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
13257 /* remove match expression, and replace with new optree with
13258 * a match op at its head */
13259 op_sibling_splice(o, NULL, 1, NULL);
13260 /* pmruntime will handle split " " behavior with flag==2 */
13261 kid = pmruntime(newPMOP(OP_MATCH, 0), kid, NULL, 2, 0);
13262 op_sibling_splice(o, NULL, 0, kid);
13265 assert(kid->op_type == OP_MATCH || kid->op_type == OP_SPLIT);
13267 if (kPMOP->op_pmflags & PMf_GLOBAL) {
13268 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP),
13269 "Use of /g modifier is meaningless in split");
13272 /* eliminate the split op, and move the match op (plus any children)
13273 * into its place, then convert the match op into a split op. i.e.
13275 * SPLIT MATCH SPLIT(ex-MATCH)
13277 * MATCH - A - B - C => R - A - B - C => R - A - B - C
13283 * (R, if it exists, will be a regcomp op)
13286 op_sibling_splice(o, NULL, 1, NULL); /* detach match op from o */
13287 sibs = op_sibling_splice(o, NULL, -1, NULL); /* detach any other sibs */
13288 op_sibling_splice(kid, cLISTOPx(kid)->op_last, 0, sibs); /* and reattach */
13289 OpTYPE_set(kid, OP_SPLIT);
13290 kid->op_flags = (o->op_flags | (kid->op_flags & OPf_KIDS));
13291 kid->op_private = o->op_private;
13294 kid = sibs; /* kid is now the string arg of the split */
13297 kid = newDEFSVOP();
13298 op_append_elem(OP_SPLIT, o, kid);
13302 kid = OpSIBLING(kid);
13304 kid = newSVOP(OP_CONST, 0, newSViv(0));
13305 op_append_elem(OP_SPLIT, o, kid);
13306 o->op_private |= OPpSPLIT_IMPLIM;
13310 if (OpHAS_SIBLING(kid))
13311 return too_many_arguments_pv(o,OP_DESC(o), 0);
13317 Perl_ck_stringify(pTHX_ OP *o)
13319 OP * const kid = OpSIBLING(cUNOPo->op_first);
13320 PERL_ARGS_ASSERT_CK_STRINGIFY;
13321 if (( kid->op_type == OP_JOIN || kid->op_type == OP_QUOTEMETA
13322 || kid->op_type == OP_LC || kid->op_type == OP_LCFIRST
13323 || kid->op_type == OP_UC || kid->op_type == OP_UCFIRST)
13324 && !OpHAS_SIBLING(kid)) /* syntax errs can leave extra children */
13326 op_sibling_splice(o, cUNOPo->op_first, -1, NULL);
13334 Perl_ck_join(pTHX_ OP *o)
13336 OP * const kid = OpSIBLING(cLISTOPo->op_first);
13338 PERL_ARGS_ASSERT_CK_JOIN;
13340 if (kid && kid->op_type == OP_MATCH) {
13341 if (ckWARN(WARN_SYNTAX)) {
13342 const REGEXP *re = PM_GETRE(kPMOP);
13344 ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re),
13345 SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) )
13346 : newSVpvs_flags( "STRING", SVs_TEMP );
13347 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
13348 "/%" SVf "/ should probably be written as \"%" SVf "\"",
13349 SVfARG(msg), SVfARG(msg));
13353 && (kid->op_type == OP_CONST /* an innocent, unsuspicious separator */
13354 || (kid->op_type == OP_PADSV && !(kid->op_private & OPpLVAL_INTRO))
13355 || ( kid->op_type==OP_RV2SV && kUNOP->op_first->op_type == OP_GV
13356 && !(kid->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))))
13358 const OP * const bairn = OpSIBLING(kid); /* the list */
13359 if (bairn && !OpHAS_SIBLING(bairn) /* single-item list */
13360 && OP_GIMME(bairn,0) == G_SCALAR)
13362 OP * const ret = op_convert_list(OP_STRINGIFY, OPf_FOLDED,
13363 op_sibling_splice(o, kid, 1, NULL));
13373 =for apidoc rv2cv_op_cv
13375 Examines an op, which is expected to identify a subroutine at runtime,
13376 and attempts to determine at compile time which subroutine it identifies.
13377 This is normally used during Perl compilation to determine whether
13378 a prototype can be applied to a function call. C<cvop> is the op
13379 being considered, normally an C<rv2cv> op. A pointer to the identified
13380 subroutine is returned, if it could be determined statically, and a null
13381 pointer is returned if it was not possible to determine statically.
13383 Currently, the subroutine can be identified statically if the RV that the
13384 C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op.
13385 A C<gv> op is suitable if the GV's CV slot is populated. A C<const> op is
13386 suitable if the constant value must be an RV pointing to a CV. Details of
13387 this process may change in future versions of Perl. If the C<rv2cv> op
13388 has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify
13389 the subroutine statically: this flag is used to suppress compile-time
13390 magic on a subroutine call, forcing it to use default runtime behaviour.
13392 If C<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling
13393 of a GV reference is modified. If a GV was examined and its CV slot was
13394 found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set.
13395 If the op is not optimised away, and the CV slot is later populated with
13396 a subroutine having a prototype, that flag eventually triggers the warning
13397 "called too early to check prototype".
13399 If C<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead
13400 of returning a pointer to the subroutine it returns a pointer to the
13401 GV giving the most appropriate name for the subroutine in this context.
13402 Normally this is just the C<CvGV> of the subroutine, but for an anonymous
13403 (C<CvANON>) subroutine that is referenced through a GV it will be the
13404 referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned.
13405 A null pointer is returned as usual if there is no statically-determinable
13408 =for apidoc Amnh||OPpEARLY_CV
13409 =for apidoc Amnh||OPpENTERSUB_AMPER
13410 =for apidoc Amnh||RV2CVOPCV_MARK_EARLY
13411 =for apidoc Amnh||RV2CVOPCV_RETURN_NAME_GV
13416 /* shared by toke.c:yylex */
13418 Perl_find_lexical_cv(pTHX_ PADOFFSET off)
13420 PADNAME *name = PAD_COMPNAME(off);
13421 CV *compcv = PL_compcv;
13422 while (PadnameOUTER(name)) {
13423 assert(PARENT_PAD_INDEX(name));
13424 compcv = CvOUTSIDE(compcv);
13425 name = PadlistNAMESARRAY(CvPADLIST(compcv))
13426 [off = PARENT_PAD_INDEX(name)];
13428 assert(!PadnameIsOUR(name));
13429 if (!PadnameIsSTATE(name) && PadnamePROTOCV(name)) {
13430 return PadnamePROTOCV(name);
13432 return (CV *)AvARRAY(PadlistARRAY(CvPADLIST(compcv))[1])[off];
13436 Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags)
13441 PERL_ARGS_ASSERT_RV2CV_OP_CV;
13442 if (flags & ~RV2CVOPCV_FLAG_MASK)
13443 Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags);
13444 if (cvop->op_type != OP_RV2CV)
13446 if (cvop->op_private & OPpENTERSUB_AMPER)
13448 if (!(cvop->op_flags & OPf_KIDS))
13450 rvop = cUNOPx(cvop)->op_first;
13451 switch (rvop->op_type) {
13453 gv = cGVOPx_gv(rvop);
13455 if (SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV) {
13456 cv = MUTABLE_CV(SvRV(gv));
13460 if (flags & RV2CVOPCV_RETURN_STUB)
13466 if (flags & RV2CVOPCV_MARK_EARLY)
13467 rvop->op_private |= OPpEARLY_CV;
13472 SV *rv = cSVOPx_sv(rvop);
13475 cv = (CV*)SvRV(rv);
13479 cv = find_lexical_cv(rvop->op_targ);
13484 } NOT_REACHED; /* NOTREACHED */
13486 if (SvTYPE((SV*)cv) != SVt_PVCV)
13488 if (flags & RV2CVOPCV_RETURN_NAME_GV) {
13489 if ((!CvANON(cv) && !CvLEXICAL(cv)) || !gv)
13493 else if (flags & RV2CVOPCV_MAYBE_NAME_GV) {
13494 if (CvLEXICAL(cv) || CvNAMED(cv))
13496 if (!CvANON(cv) || !gv)
13506 =for apidoc ck_entersub_args_list
13508 Performs the default fixup of the arguments part of an C<entersub>
13509 op tree. This consists of applying list context to each of the
13510 argument ops. This is the standard treatment used on a call marked
13511 with C<&>, or a method call, or a call through a subroutine reference,
13512 or any other call where the callee can't be identified at compile time,
13513 or a call where the callee has no prototype.
13519 Perl_ck_entersub_args_list(pTHX_ OP *entersubop)
13523 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST;
13525 aop = cUNOPx(entersubop)->op_first;
13526 if (!OpHAS_SIBLING(aop))
13527 aop = cUNOPx(aop)->op_first;
13528 for (aop = OpSIBLING(aop); OpHAS_SIBLING(aop); aop = OpSIBLING(aop)) {
13529 /* skip the extra attributes->import() call implicitly added in
13530 * something like foo(my $x : bar)
13532 if ( aop->op_type == OP_ENTERSUB
13533 && (aop->op_flags & OPf_WANT) == OPf_WANT_VOID
13537 op_lvalue(aop, OP_ENTERSUB);
13543 =for apidoc ck_entersub_args_proto
13545 Performs the fixup of the arguments part of an C<entersub> op tree
13546 based on a subroutine prototype. This makes various modifications to
13547 the argument ops, from applying context up to inserting C<refgen> ops,
13548 and checking the number and syntactic types of arguments, as directed by
13549 the prototype. This is the standard treatment used on a subroutine call,
13550 not marked with C<&>, where the callee can be identified at compile time
13551 and has a prototype.
13553 C<protosv> supplies the subroutine prototype to be applied to the call.
13554 It may be a normal defined scalar, of which the string value will be used.
13555 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
13556 that has been cast to C<SV*>) which has a prototype. The prototype
13557 supplied, in whichever form, does not need to match the actual callee
13558 referenced by the op tree.
13560 If the argument ops disagree with the prototype, for example by having
13561 an unacceptable number of arguments, a valid op tree is returned anyway.
13562 The error is reflected in the parser state, normally resulting in a single
13563 exception at the top level of parsing which covers all the compilation
13564 errors that occurred. In the error message, the callee is referred to
13565 by the name defined by the C<namegv> parameter.
13571 Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
13574 const char *proto, *proto_end;
13575 OP *aop, *prev, *cvop, *parent;
13578 I32 contextclass = 0;
13579 const char *e = NULL;
13580 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO;
13581 if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv))
13582 Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, "
13583 "flags=%lx", (unsigned long) SvFLAGS(protosv));
13584 if (SvTYPE(protosv) == SVt_PVCV)
13585 proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv);
13586 else proto = SvPV(protosv, proto_len);
13587 proto = S_strip_spaces(aTHX_ proto, &proto_len);
13588 proto_end = proto + proto_len;
13589 parent = entersubop;
13590 aop = cUNOPx(entersubop)->op_first;
13591 if (!OpHAS_SIBLING(aop)) {
13593 aop = cUNOPx(aop)->op_first;
13596 aop = OpSIBLING(aop);
13597 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
13598 while (aop != cvop) {
13601 if (proto >= proto_end)
13603 SV * const namesv = cv_name((CV *)namegv, NULL, 0);
13604 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %" SVf,
13605 SVfARG(namesv)), SvUTF8(namesv));
13615 /* _ must be at the end */
13616 if (proto[1] && !memCHRs(";@%", proto[1]))
13632 if ( o3->op_type != OP_UNDEF
13633 && (o3->op_type != OP_SREFGEN
13634 || ( cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type
13636 && cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type
13638 bad_type_gv(arg, namegv, o3,
13639 arg == 1 ? "block or sub {}" : "sub {}");
13642 /* '*' allows any scalar type, including bareword */
13645 if (o3->op_type == OP_RV2GV)
13646 goto wrapref; /* autoconvert GLOB -> GLOBref */
13647 else if (o3->op_type == OP_CONST)
13648 o3->op_private &= ~OPpCONST_STRICT;
13654 if (o3->op_type == OP_RV2AV ||
13655 o3->op_type == OP_PADAV ||
13656 o3->op_type == OP_RV2HV ||
13657 o3->op_type == OP_PADHV
13663 case '[': case ']':
13670 switch (*proto++) {
13672 if (contextclass++ == 0) {
13673 e = (char *) memchr(proto, ']', proto_end - proto);
13674 if (!e || e == proto)
13682 if (contextclass) {
13683 const char *p = proto;
13684 const char *const end = proto;
13686 while (*--p != '[')
13687 /* \[$] accepts any scalar lvalue */
13689 && Perl_op_lvalue_flags(aTHX_
13691 OP_READ, /* not entersub */
13694 bad_type_gv(arg, namegv, o3,
13695 Perl_form(aTHX_ "one of %.*s",(int)(end - p), p));
13700 if (o3->op_type == OP_RV2GV)
13703 bad_type_gv(arg, namegv, o3, "symbol");
13706 if (o3->op_type == OP_ENTERSUB
13707 && !(o3->op_flags & OPf_STACKED))
13710 bad_type_gv(arg, namegv, o3, "subroutine");
13713 if (o3->op_type == OP_RV2SV ||
13714 o3->op_type == OP_PADSV ||
13715 o3->op_type == OP_HELEM ||
13716 o3->op_type == OP_AELEM)
13718 if (!contextclass) {
13719 /* \$ accepts any scalar lvalue */
13720 if (Perl_op_lvalue_flags(aTHX_
13722 OP_READ, /* not entersub */
13725 bad_type_gv(arg, namegv, o3, "scalar");
13729 if (o3->op_type == OP_RV2AV ||
13730 o3->op_type == OP_PADAV)
13732 o3->op_flags &=~ OPf_PARENS;
13736 bad_type_gv(arg, namegv, o3, "array");
13739 if (o3->op_type == OP_RV2HV ||
13740 o3->op_type == OP_PADHV)
13742 o3->op_flags &=~ OPf_PARENS;
13746 bad_type_gv(arg, namegv, o3, "hash");
13749 aop = S_op_sibling_newUNOP(aTHX_ parent, prev,
13751 if (contextclass && e) {
13756 default: goto oops;
13766 Perl_croak(aTHX_ "Malformed prototype for %" SVf ": %" SVf,
13767 SVfARG(cv_name((CV *)namegv, NULL, 0)),
13772 op_lvalue(aop, OP_ENTERSUB);
13774 aop = OpSIBLING(aop);
13776 if (aop == cvop && *proto == '_') {
13777 /* generate an access to $_ */
13778 op_sibling_splice(parent, prev, 0, newDEFSVOP());
13780 if (!optional && proto_end > proto &&
13781 (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_'))
13783 SV * const namesv = cv_name((CV *)namegv, NULL, 0);
13784 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %" SVf,
13785 SVfARG(namesv)), SvUTF8(namesv));
13791 =for apidoc ck_entersub_args_proto_or_list
13793 Performs the fixup of the arguments part of an C<entersub> op tree either
13794 based on a subroutine prototype or using default list-context processing.
13795 This is the standard treatment used on a subroutine call, not marked
13796 with C<&>, where the callee can be identified at compile time.
13798 C<protosv> supplies the subroutine prototype to be applied to the call,
13799 or indicates that there is no prototype. It may be a normal scalar,
13800 in which case if it is defined then the string value will be used
13801 as a prototype, and if it is undefined then there is no prototype.
13802 Alternatively, for convenience, it may be a subroutine object (a C<CV*>
13803 that has been cast to C<SV*>), of which the prototype will be used if it
13804 has one. The prototype (or lack thereof) supplied, in whichever form,
13805 does not need to match the actual callee referenced by the op tree.
13807 If the argument ops disagree with the prototype, for example by having
13808 an unacceptable number of arguments, a valid op tree is returned anyway.
13809 The error is reflected in the parser state, normally resulting in a single
13810 exception at the top level of parsing which covers all the compilation
13811 errors that occurred. In the error message, the callee is referred to
13812 by the name defined by the C<namegv> parameter.
13818 Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop,
13819 GV *namegv, SV *protosv)
13821 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST;
13822 if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv))
13823 return ck_entersub_args_proto(entersubop, namegv, protosv);
13825 return ck_entersub_args_list(entersubop);
13829 Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv)
13831 IV cvflags = SvIVX(protosv);
13832 int opnum = cvflags & 0xffff;
13833 OP *aop = cUNOPx(entersubop)->op_first;
13835 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE;
13839 if (!OpHAS_SIBLING(aop))
13840 aop = cUNOPx(aop)->op_first;
13841 aop = OpSIBLING(aop);
13842 for (cvop = aop; OpSIBLING(cvop); cvop = OpSIBLING(cvop)) ;
13844 SV *namesv = cv_name((CV *)namegv, NULL, CV_NAME_NOTQUAL);
13845 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %" SVf,
13846 SVfARG(namesv)), SvUTF8(namesv));
13849 op_free(entersubop);
13850 switch(cvflags >> 16) {
13851 case 'F': return newSVOP(OP_CONST, 0,
13852 newSVpv(CopFILE(PL_curcop),0));
13853 case 'L': return newSVOP(
13855 Perl_newSVpvf(aTHX_
13856 "%" IVdf, (IV)CopLINE(PL_curcop)
13859 case 'P': return newSVOP(OP_CONST, 0,
13861 ? newSVhek(HvNAME_HEK(PL_curstash))
13866 NOT_REACHED; /* NOTREACHED */
13869 OP *prev, *cvop, *first, *parent;
13872 parent = entersubop;
13873 if (!OpHAS_SIBLING(aop)) {
13875 aop = cUNOPx(aop)->op_first;
13878 first = prev = aop;
13879 aop = OpSIBLING(aop);
13880 /* find last sibling */
13882 OpHAS_SIBLING(cvop);
13883 prev = cvop, cvop = OpSIBLING(cvop))
13885 if (!(cvop->op_private & OPpENTERSUB_NOPAREN)
13886 /* Usually, OPf_SPECIAL on an op with no args means that it had
13887 * parens, but these have their own meaning for that flag: */
13888 && opnum != OP_VALUES && opnum != OP_KEYS && opnum != OP_EACH
13889 && opnum != OP_DELETE && opnum != OP_EXISTS)
13890 flags |= OPf_SPECIAL;
13891 /* excise cvop from end of sibling chain */
13892 op_sibling_splice(parent, prev, 1, NULL);
13894 if (aop == cvop) aop = NULL;
13896 /* detach remaining siblings from the first sibling, then
13897 * dispose of original optree */
13900 op_sibling_splice(parent, first, -1, NULL);
13901 op_free(entersubop);
13903 if (cvflags == (OP_ENTEREVAL | (1<<16)))
13904 flags |= OPpEVAL_BYTES <<8;
13906 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
13908 case OA_BASEOP_OR_UNOP:
13909 case OA_FILESTATOP:
13911 return newOP(opnum,flags); /* zero args */
13913 return newUNOP(opnum,flags,aop); /* one arg */
13914 /* too many args */
13921 namesv = cv_name((CV *)namegv, NULL, CV_NAME_NOTQUAL);
13922 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %" SVf,
13923 SVfARG(namesv)), SvUTF8(namesv));
13925 nextop = OpSIBLING(aop);
13931 return opnum == OP_RUNCV
13932 ? newSVOP(OP_RUNCV, 0, &PL_sv_undef)
13935 return op_convert_list(opnum,0,aop);
13938 NOT_REACHED; /* NOTREACHED */
13943 =for apidoc cv_get_call_checker_flags
13945 Retrieves the function that will be used to fix up a call to C<cv>.
13946 Specifically, the function is applied to an C<entersub> op tree for a
13947 subroutine call, not marked with C<&>, where the callee can be identified
13948 at compile time as C<cv>.
13950 The C-level function pointer is returned in C<*ckfun_p>, an SV argument
13951 for it is returned in C<*ckobj_p>, and control flags are returned in
13952 C<*ckflags_p>. The function is intended to be called in this manner:
13954 entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p));
13956 In this call, C<entersubop> is a pointer to the C<entersub> op,
13957 which may be replaced by the check function, and C<namegv> supplies
13958 the name that should be used by the check function to refer
13959 to the callee of the C<entersub> op if it needs to emit any diagnostics.
13960 It is permitted to apply the check function in non-standard situations,
13961 such as to a call to a different subroutine or to a method call.
13963 C<namegv> may not actually be a GV. If the C<CALL_CHECKER_REQUIRE_GV>
13964 bit is clear in C<*ckflags_p>, it is permitted to pass a CV or other SV
13965 instead, anything that can be used as the first argument to L</cv_name>.
13966 If the C<CALL_CHECKER_REQUIRE_GV> bit is set in C<*ckflags_p> then the
13967 check function requires C<namegv> to be a genuine GV.
13969 By default, the check function is
13970 L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>,
13971 the SV parameter is C<cv> itself, and the C<CALL_CHECKER_REQUIRE_GV>
13972 flag is clear. This implements standard prototype processing. It can
13973 be changed, for a particular subroutine, by L</cv_set_call_checker_flags>.
13975 If the C<CALL_CHECKER_REQUIRE_GV> bit is set in C<gflags> then it
13976 indicates that the caller only knows about the genuine GV version of
13977 C<namegv>, and accordingly the corresponding bit will always be set in
13978 C<*ckflags_p>, regardless of the check function's recorded requirements.
13979 If the C<CALL_CHECKER_REQUIRE_GV> bit is clear in C<gflags> then it
13980 indicates the caller knows about the possibility of passing something
13981 other than a GV as C<namegv>, and accordingly the corresponding bit may
13982 be either set or clear in C<*ckflags_p>, indicating the check function's
13983 recorded requirements.
13985 C<gflags> is a bitset passed into C<cv_get_call_checker_flags>, in which
13986 only the C<CALL_CHECKER_REQUIRE_GV> bit currently has a defined meaning
13987 (for which see above). All other bits should be clear.
13989 =for apidoc Amnh||CALL_CHECKER_REQUIRE_GV
13991 =for apidoc cv_get_call_checker
13993 The original form of L</cv_get_call_checker_flags>, which does not return
13994 checker flags. When using a checker function returned by this function,
13995 it is only safe to call it with a genuine GV as its C<namegv> argument.
14001 Perl_cv_get_call_checker_flags(pTHX_ CV *cv, U32 gflags,
14002 Perl_call_checker *ckfun_p, SV **ckobj_p, U32 *ckflags_p)
14005 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER_FLAGS;
14006 PERL_UNUSED_CONTEXT;
14007 callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL;
14009 *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr);
14010 *ckobj_p = callmg->mg_obj;
14011 *ckflags_p = (callmg->mg_flags | gflags) & MGf_REQUIRE_GV;
14013 *ckfun_p = Perl_ck_entersub_args_proto_or_list;
14014 *ckobj_p = (SV*)cv;
14015 *ckflags_p = gflags & MGf_REQUIRE_GV;
14020 Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p)
14023 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER;
14024 PERL_UNUSED_CONTEXT;
14025 cv_get_call_checker_flags(cv, CALL_CHECKER_REQUIRE_GV, ckfun_p, ckobj_p,
14030 =for apidoc cv_set_call_checker_flags
14032 Sets the function that will be used to fix up a call to C<cv>.
14033 Specifically, the function is applied to an C<entersub> op tree for a
14034 subroutine call, not marked with C<&>, where the callee can be identified
14035 at compile time as C<cv>.
14037 The C-level function pointer is supplied in C<ckfun>, an SV argument for
14038 it is supplied in C<ckobj>, and control flags are supplied in C<ckflags>.
14039 The function should be defined like this:
14041 STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj)
14043 It is intended to be called in this manner:
14045 entersubop = ckfun(aTHX_ entersubop, namegv, ckobj);
14047 In this call, C<entersubop> is a pointer to the C<entersub> op,
14048 which may be replaced by the check function, and C<namegv> supplies
14049 the name that should be used by the check function to refer
14050 to the callee of the C<entersub> op if it needs to emit any diagnostics.
14051 It is permitted to apply the check function in non-standard situations,
14052 such as to a call to a different subroutine or to a method call.
14054 C<namegv> may not actually be a GV. For efficiency, perl may pass a
14055 CV or other SV instead. Whatever is passed can be used as the first
14056 argument to L</cv_name>. You can force perl to pass a GV by including
14057 C<CALL_CHECKER_REQUIRE_GV> in the C<ckflags>.
14059 C<ckflags> is a bitset, in which only the C<CALL_CHECKER_REQUIRE_GV>
14060 bit currently has a defined meaning (for which see above). All other
14061 bits should be clear.
14063 The current setting for a particular CV can be retrieved by
14064 L</cv_get_call_checker_flags>.
14066 =for apidoc cv_set_call_checker
14068 The original form of L</cv_set_call_checker_flags>, which passes it the
14069 C<CALL_CHECKER_REQUIRE_GV> flag for backward-compatibility. The effect
14070 of that flag setting is that the check function is guaranteed to get a
14071 genuine GV as its C<namegv> argument.
14077 Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj)
14079 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER;
14080 cv_set_call_checker_flags(cv, ckfun, ckobj, CALL_CHECKER_REQUIRE_GV);
14084 Perl_cv_set_call_checker_flags(pTHX_ CV *cv, Perl_call_checker ckfun,
14085 SV *ckobj, U32 ckflags)
14087 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER_FLAGS;
14088 if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) {
14089 if (SvMAGICAL((SV*)cv))
14090 mg_free_type((SV*)cv, PERL_MAGIC_checkcall);
14093 sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0);
14094 callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall);
14096 if (callmg->mg_flags & MGf_REFCOUNTED) {
14097 SvREFCNT_dec(callmg->mg_obj);
14098 callmg->mg_flags &= ~MGf_REFCOUNTED;
14100 callmg->mg_ptr = FPTR2DPTR(char *, ckfun);
14101 callmg->mg_obj = ckobj;
14102 if (ckobj != (SV*)cv) {
14103 SvREFCNT_inc_simple_void_NN(ckobj);
14104 callmg->mg_flags |= MGf_REFCOUNTED;
14106 callmg->mg_flags = (callmg->mg_flags &~ MGf_REQUIRE_GV)
14107 | (U8)(ckflags & MGf_REQUIRE_GV) | MGf_COPY;
14112 S_entersub_alloc_targ(pTHX_ OP * const o)
14114 o->op_targ = pad_alloc(OP_ENTERSUB, SVs_PADTMP);
14115 o->op_private |= OPpENTERSUB_HASTARG;
14119 Perl_ck_subr(pTHX_ OP *o)
14124 SV **const_class = NULL;
14126 PERL_ARGS_ASSERT_CK_SUBR;
14128 aop = cUNOPx(o)->op_first;
14129 if (!OpHAS_SIBLING(aop))
14130 aop = cUNOPx(aop)->op_first;
14131 aop = OpSIBLING(aop);
14132 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ;
14133 cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY);
14134 namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_MAYBE_NAME_GV) : NULL;
14136 o->op_private &= ~1;
14137 o->op_private |= (PL_hints & HINT_STRICT_REFS);
14138 if (PERLDB_SUB && PL_curstash != PL_debstash)
14139 o->op_private |= OPpENTERSUB_DB;
14140 switch (cvop->op_type) {
14142 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
14146 case OP_METHOD_NAMED:
14147 case OP_METHOD_SUPER:
14148 case OP_METHOD_REDIR:
14149 case OP_METHOD_REDIR_SUPER:
14150 o->op_flags |= OPf_REF;
14151 if (aop->op_type == OP_CONST) {
14152 aop->op_private &= ~OPpCONST_STRICT;
14153 const_class = &cSVOPx(aop)->op_sv;
14155 else if (aop->op_type == OP_LIST) {
14156 OP * const sib = OpSIBLING(cUNOPx(aop)->op_first);
14157 if (sib && sib->op_type == OP_CONST) {
14158 sib->op_private &= ~OPpCONST_STRICT;
14159 const_class = &cSVOPx(sib)->op_sv;
14162 /* make class name a shared cow string to speedup method calls */
14163 /* constant string might be replaced with object, f.e. bigint */
14164 if (const_class && SvPOK(*const_class)) {
14166 const char* str = SvPV(*const_class, len);
14168 SV* const shared = newSVpvn_share(
14169 str, SvUTF8(*const_class)
14170 ? -(SSize_t)len : (SSize_t)len,
14173 if (SvREADONLY(*const_class))
14174 SvREADONLY_on(shared);
14175 SvREFCNT_dec(*const_class);
14176 *const_class = shared;
14183 S_entersub_alloc_targ(aTHX_ o);
14184 return ck_entersub_args_list(o);
14186 Perl_call_checker ckfun;
14189 cv_get_call_checker_flags(cv, 0, &ckfun, &ckobj, &ckflags);
14190 if (CvISXSUB(cv) || !CvROOT(cv))
14191 S_entersub_alloc_targ(aTHX_ o);
14193 /* The original call checker API guarantees that a GV will
14194 be provided with the right name. So, if the old API was
14195 used (or the REQUIRE_GV flag was passed), we have to reify
14196 the CV’s GV, unless this is an anonymous sub. This is not
14197 ideal for lexical subs, as its stringification will include
14198 the package. But it is the best we can do. */
14199 if (ckflags & CALL_CHECKER_REQUIRE_GV) {
14200 if (!CvANON(cv) && (!CvNAMED(cv) || CvNAME_HEK(cv)))
14203 else namegv = MUTABLE_GV(cv);
14204 /* After a syntax error in a lexical sub, the cv that
14205 rv2cv_op_cv returns may be a nameless stub. */
14206 if (!namegv) return ck_entersub_args_list(o);
14209 return ckfun(aTHX_ o, namegv, ckobj);
14214 Perl_ck_svconst(pTHX_ OP *o)
14216 SV * const sv = cSVOPo->op_sv;
14217 PERL_ARGS_ASSERT_CK_SVCONST;
14218 PERL_UNUSED_CONTEXT;
14219 #ifdef PERL_COPY_ON_WRITE
14220 /* Since the read-only flag may be used to protect a string buffer, we
14221 cannot do copy-on-write with existing read-only scalars that are not
14222 already copy-on-write scalars. To allow $_ = "hello" to do COW with
14223 that constant, mark the constant as COWable here, if it is not
14224 already read-only. */
14225 if (!SvREADONLY(sv) && !SvIsCOW(sv) && SvCANCOW(sv)) {
14228 # ifdef PERL_DEBUG_READONLY_COW
14238 Perl_ck_trunc(pTHX_ OP *o)
14240 PERL_ARGS_ASSERT_CK_TRUNC;
14242 if (o->op_flags & OPf_KIDS) {
14243 SVOP *kid = cSVOPx(cUNOPo->op_first);
14245 if (kid->op_type == OP_NULL)
14246 kid = cSVOPx(OpSIBLING(kid));
14247 if (kid && kid->op_type == OP_CONST &&
14248 (kid->op_private & OPpCONST_BARE) &&
14251 o->op_flags |= OPf_SPECIAL;
14252 kid->op_private &= ~OPpCONST_STRICT;
14253 if (!FEATURE_BAREWORD_FILEHANDLES_IS_ENABLED) {
14254 no_bareword_filehandle(SvPVX(cSVOPx_sv(kid)));
14262 Perl_ck_substr(pTHX_ OP *o)
14264 PERL_ARGS_ASSERT_CK_SUBSTR;
14267 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) {
14268 OP *kid = cLISTOPo->op_first;
14270 if (kid->op_type == OP_NULL)
14271 kid = OpSIBLING(kid);
14273 /* Historically, substr(delete $foo{bar},...) has been allowed
14274 with 4-arg substr. Keep it working by applying entersub
14276 op_lvalue(kid, OP_ENTERSUB);
14283 Perl_ck_tell(pTHX_ OP *o)
14285 PERL_ARGS_ASSERT_CK_TELL;
14287 if (o->op_flags & OPf_KIDS) {
14288 OP *kid = cLISTOPo->op_first;
14289 if (kid->op_type == OP_NULL && OpHAS_SIBLING(kid)) kid = OpSIBLING(kid);
14290 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE;
14295 PERL_STATIC_INLINE OP *
14296 S_last_non_null_kid(OP *o) {
14298 if (cUNOPo->op_flags & OPf_KIDS) {
14299 OP *k = cLISTOPo->op_first;
14301 if (k->op_type != OP_NULL) {
14312 Perl_ck_each(pTHX_ OP *o)
14314 OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL;
14315 const unsigned orig_type = o->op_type;
14317 PERL_ARGS_ASSERT_CK_EACH;
14320 switch (kid->op_type) {
14325 /* Catch out an anonhash here, since the behaviour might be
14328 * The typical tree is:
14335 * If the contents of the block is more complex you might get:
14343 * Similarly for the anonlist version below.
14345 if (orig_type == OP_EACH &&
14346 ckWARN(WARN_SYNTAX) &&
14347 (cUNOPx(kid)->op_flags & OPf_KIDS) &&
14348 ( cUNOPx(kid)->op_first->op_type == OP_SCOPE ||
14349 cUNOPx(kid)->op_first->op_type == OP_LEAVE) &&
14350 (cUNOPx(kid)->op_first->op_flags & OPf_KIDS)) {
14351 /* look for last non-null kid, since we might have:
14352 each %{ some code ; +{ anon hash } }
14354 OP *k = S_last_non_null_kid(cUNOPx(kid)->op_first);
14355 if (k && k->op_type == OP_ANONHASH) {
14356 /* diag_listed_as: each on anonymous %s will always start from the beginning */
14357 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "each on anonymous hash will always start from the beginning");
14362 if (orig_type == OP_EACH &&
14363 ckWARN(WARN_SYNTAX) &&
14364 (cUNOPx(kid)->op_flags & OPf_KIDS) &&
14365 (cUNOPx(kid)->op_first->op_type == OP_SCOPE ||
14366 cUNOPx(kid)->op_first->op_type == OP_LEAVE) &&
14367 (cUNOPx(kid)->op_first->op_flags & OPf_KIDS)) {
14368 OP *k = S_last_non_null_kid(cUNOPx(kid)->op_first);
14369 if (k && k->op_type == OP_ANONLIST) {
14370 /* diag_listed_as: each on anonymous %s will always start from the beginning */
14371 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "each on anonymous array will always start from the beginning");
14376 OpTYPE_set(o, orig_type == OP_EACH ? OP_AEACH
14377 : orig_type == OP_KEYS ? OP_AKEYS
14381 if (kid->op_private == OPpCONST_BARE
14382 || !SvROK(cSVOPx_sv(kid))
14383 || ( SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV
14384 && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV )
14389 qerror(Perl_mess(aTHX_
14390 "Experimental %s on scalar is now forbidden",
14391 PL_op_desc[orig_type]));
14393 bad_type_pv(1, "hash or array", o, kid);
14401 Perl_ck_length(pTHX_ OP *o)
14403 PERL_ARGS_ASSERT_CK_LENGTH;
14407 if (ckWARN(WARN_SYNTAX)) {
14408 const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL;
14412 const bool hash = kid->op_type == OP_PADHV
14413 || kid->op_type == OP_RV2HV;
14414 switch (kid->op_type) {
14419 name = op_varname(kid);
14425 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
14426 "length() used on %" SVf " (did you mean \"scalar(%s%" SVf
14428 SVfARG(name), hash ? "keys " : "", SVfARG(name)
14431 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
14432 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
14433 "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)");
14435 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */
14436 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
14437 "length() used on @array (did you mean \"scalar(@array)\"?)");
14446 Perl_ck_isa(pTHX_ OP *o)
14448 OP *classop = cBINOPo->op_last;
14450 PERL_ARGS_ASSERT_CK_ISA;
14452 /* Convert barename into PV */
14453 if(classop->op_type == OP_CONST && classop->op_private & OPpCONST_BARE) {
14454 /* TODO: Optionally convert package to raw HV here */
14455 classop->op_private &= ~(OPpCONST_BARE|OPpCONST_STRICT);
14462 /* Check for in place reverse and sort assignments like "@a = reverse @a"
14463 and modify the optree to make them work inplace */
14466 S_inplace_aassign(pTHX_ OP *o) {
14468 OP *modop, *modop_pushmark;
14470 OP *oleft, *oleft_pushmark;
14472 PERL_ARGS_ASSERT_INPLACE_AASSIGN;
14474 assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID);
14476 assert(cUNOPo->op_first->op_type == OP_NULL);
14477 modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first;
14478 assert(modop_pushmark->op_type == OP_PUSHMARK);
14479 modop = OpSIBLING(modop_pushmark);
14481 if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE)
14484 /* no other operation except sort/reverse */
14485 if (OpHAS_SIBLING(modop))
14488 assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK);
14489 if (!(oright = OpSIBLING(cUNOPx(modop)->op_first))) return;
14491 if (modop->op_flags & OPf_STACKED) {
14492 /* skip sort subroutine/block */
14493 assert(oright->op_type == OP_NULL);
14494 oright = OpSIBLING(oright);
14497 assert(OpSIBLING(cUNOPo->op_first)->op_type == OP_NULL);
14498 oleft_pushmark = cUNOPx(OpSIBLING(cUNOPo->op_first))->op_first;
14499 assert(oleft_pushmark->op_type == OP_PUSHMARK);
14500 oleft = OpSIBLING(oleft_pushmark);
14502 /* Check the lhs is an array */
14504 (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV)
14505 || OpHAS_SIBLING(oleft)
14506 || (oleft->op_private & OPpLVAL_INTRO)
14510 /* Only one thing on the rhs */
14511 if (OpHAS_SIBLING(oright))
14514 /* check the array is the same on both sides */
14515 if (oleft->op_type == OP_RV2AV) {
14516 if (oright->op_type != OP_RV2AV
14517 || !cUNOPx(oright)->op_first
14518 || cUNOPx(oright)->op_first->op_type != OP_GV
14519 || cUNOPx(oleft )->op_first->op_type != OP_GV
14520 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
14521 cGVOPx_gv(cUNOPx(oright)->op_first)
14525 else if (oright->op_type != OP_PADAV
14526 || oright->op_targ != oleft->op_targ
14530 /* This actually is an inplace assignment */
14532 modop->op_private |= OPpSORT_INPLACE;
14534 /* transfer MODishness etc from LHS arg to RHS arg */
14535 oright->op_flags = oleft->op_flags;
14537 /* remove the aassign op and the lhs */
14539 op_null(oleft_pushmark);
14540 if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first)
14541 op_null(cUNOPx(oleft)->op_first);
14547 =for apidoc_section $custom
14549 =for apidoc Perl_custom_op_xop
14550 Return the XOP structure for a given custom op. This macro should be
14551 considered internal to C<OP_NAME> and the other access macros: use them instead.
14552 This macro does call a function. Prior
14553 to 5.19.6, this was implemented as a
14560 /* use PERL_MAGIC_ext to call a function to free the xop structure when
14561 * freeing PL_custom_ops */
14564 custom_op_register_free(pTHX_ SV *sv, MAGIC *mg)
14568 PERL_UNUSED_ARG(mg);
14569 xop = INT2PTR(XOP *, SvIV(sv));
14570 Safefree(xop->xop_name);
14571 Safefree(xop->xop_desc);
14577 static const MGVTBL custom_op_register_vtbl = {
14582 custom_op_register_free, /* free */
14592 Perl_custom_op_get_field(pTHX_ const OP *o, const xop_flags_enum field)
14598 static const XOP xop_null = { 0, 0, 0, 0, 0 };
14600 PERL_ARGS_ASSERT_CUSTOM_OP_GET_FIELD;
14601 assert(o->op_type == OP_CUSTOM);
14603 /* This is wrong. It assumes a function pointer can be cast to IV,
14604 * which isn't guaranteed, but this is what the old custom OP code
14605 * did. In principle it should be safer to Copy the bytes of the
14606 * pointer into a PV: since the new interface is hidden behind
14607 * functions, this can be changed later if necessary. */
14608 /* Change custom_op_xop if this ever happens */
14609 keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr)));
14612 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0);
14614 /* See if the op isn't registered, but its name *is* registered.
14615 * That implies someone is using the pre-5.14 API,where only name and
14616 * description could be registered. If so, fake up a real
14618 * We only check for an existing name, and assume no one will have
14619 * just registered a desc */
14620 if (!he && PL_custom_op_names &&
14621 (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0))
14626 /* XXX does all this need to be shared mem? */
14627 Newxz(xop, 1, XOP);
14628 pv = SvPV(HeVAL(he), l);
14629 XopENTRY_set(xop, xop_name, savepvn(pv, l));
14630 if (PL_custom_op_descs &&
14631 (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0))
14633 pv = SvPV(HeVAL(he), l);
14634 XopENTRY_set(xop, xop_desc, savepvn(pv, l));
14636 Perl_custom_op_register(aTHX_ o->op_ppaddr, xop);
14637 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0);
14638 /* add magic to the SV so that the xop struct (pointed to by
14639 * SvIV(sv)) is freed. Normally a static xop is registered, but
14640 * for this backcompat hack, we've alloced one */
14641 (void)sv_magicext(HeVAL(he), NULL, PERL_MAGIC_ext,
14642 &custom_op_register_vtbl, NULL, 0);
14647 xop = (XOP *)&xop_null;
14649 xop = INT2PTR(XOP *, SvIV(HeVAL(he)));
14654 if(field == XOPe_xop_ptr) {
14657 const U32 flags = XopFLAGS(xop);
14658 if(flags & field) {
14660 case XOPe_xop_name:
14661 any.xop_name = xop->xop_name;
14663 case XOPe_xop_desc:
14664 any.xop_desc = xop->xop_desc;
14666 case XOPe_xop_class:
14667 any.xop_class = xop->xop_class;
14669 case XOPe_xop_peep:
14670 any.xop_peep = xop->xop_peep;
14675 "panic: custom_op_get_field(): invalid field %d\n",
14681 case XOPe_xop_name:
14682 any.xop_name = XOPd_xop_name;
14684 case XOPe_xop_desc:
14685 any.xop_desc = XOPd_xop_desc;
14687 case XOPe_xop_class:
14688 any.xop_class = XOPd_xop_class;
14690 case XOPe_xop_peep:
14691 any.xop_peep = XOPd_xop_peep;
14704 =for apidoc custom_op_register
14705 Register a custom op. See L<perlguts/"Custom Operators">.
14711 Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop)
14715 PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER;
14717 /* see the comment in custom_op_xop */
14718 keysv = sv_2mortal(newSViv(PTR2IV(ppaddr)));
14720 if (!PL_custom_ops)
14721 PL_custom_ops = newHV();
14723 if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0))
14724 Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name);
14729 =for apidoc core_prototype
14731 This function assigns the prototype of the named core function to C<sv>, or
14732 to a new mortal SV if C<sv> is C<NULL>. It returns the modified C<sv>, or
14733 C<NULL> if the core function has no prototype. C<code> is a code as returned
14734 by C<keyword()>. It must not be equal to 0.
14740 Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code,
14743 int i = 0, n = 0, seen_question = 0, defgv = 0;
14745 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
14746 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
14747 bool nullret = FALSE;
14749 PERL_ARGS_ASSERT_CORE_PROTOTYPE;
14753 if (!sv) sv = sv_newmortal();
14755 #define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv
14757 switch (code < 0 ? -code : code) {
14758 case KEY_and : case KEY_chop: case KEY_chomp:
14759 case KEY_cmp : case KEY_defined: case KEY_delete: case KEY_exec :
14760 case KEY_exists: case KEY_eq : case KEY_ge : case KEY_goto :
14761 case KEY_grep : case KEY_gt : case KEY_last : case KEY_le :
14762 case KEY_lt : case KEY_map : case KEY_ne : case KEY_next :
14763 case KEY_or : case KEY_print : case KEY_printf: case KEY_qr :
14764 case KEY_redo : case KEY_require: case KEY_return: case KEY_say :
14765 case KEY_select: case KEY_sort : case KEY_split : case KEY_system:
14766 case KEY_x : case KEY_xor :
14767 if (!opnum) return NULL; nullret = TRUE; goto findopnum;
14768 case KEY_glob: retsetpvs("_;", OP_GLOB);
14769 case KEY_keys: retsetpvs("\\[%@]", OP_KEYS);
14770 case KEY_values: retsetpvs("\\[%@]", OP_VALUES);
14771 case KEY_each: retsetpvs("\\[%@]", OP_EACH);
14772 case KEY_pos: retsetpvs(";\\[$*]", OP_POS);
14773 case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__:
14775 case KEY_evalbytes:
14776 name = "entereval"; break;
14784 while (i < MAXO) { /* The slow way. */
14785 if (strEQ(name, PL_op_name[i])
14786 || strEQ(name, PL_op_desc[i]))
14788 if (nullret) { assert(opnum); *opnum = i; return NULL; }
14795 defgv = PL_opargs[i] & OA_DEFGV;
14796 oa = PL_opargs[i] >> OASHIFT;
14798 if (oa & OA_OPTIONAL && !seen_question && (
14799 !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF
14804 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
14805 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
14806 /* But globs are already references (kinda) */
14807 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
14811 if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF
14812 && !scalar_mod_type(NULL, i)) {
14817 if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&';
14821 else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
14822 if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') {
14823 str[n-1] = '_'; defgv = 0;
14827 if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';';
14829 sv_setpvn(sv, str, n - 1);
14830 if (opnum) *opnum = i;
14835 Perl_coresub_op(pTHX_ SV * const coreargssv, const int code,
14838 OP * const argop = (opnum == OP_SELECT && code) ? NULL :
14839 newSVOP(OP_COREARGS,0,coreargssv);
14842 PERL_ARGS_ASSERT_CORESUB_OP;
14846 return op_append_elem(OP_LINESEQ,
14849 newSVOP(OP_CONST, 0, newSViv(-code % 3)),
14856 o = newUNOP(OP_AVHVSWITCH,0,argop);
14857 o->op_private = opnum-OP_EACH;
14859 case OP_SELECT: /* which represents OP_SSELECT as well */
14864 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
14865 newSVOP(OP_CONST, 0, newSVuv(1))
14867 coresub_op(newSVuv((UV)OP_SSELECT), 0,
14869 coresub_op(coreargssv, 0, OP_SELECT)
14873 switch (PL_opargs[opnum] & OA_CLASS_MASK) {
14875 return op_append_elem(
14878 opnum == OP_WANTARRAY || opnum == OP_RUNCV
14879 ? OPpOFFBYONE << 8 : 0)
14881 case OA_BASEOP_OR_UNOP:
14882 if (opnum == OP_ENTEREVAL) {
14883 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop);
14884 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES;
14886 else o = newUNOP(opnum,0,argop);
14887 if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE;
14890 if (is_handle_constructor(o, 1))
14891 argop->op_private |= OPpCOREARGS_DEREF1;
14892 if (scalar_mod_type(NULL, opnum))
14893 argop->op_private |= OPpCOREARGS_SCALARMOD;
14897 o = op_convert_list(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop);
14898 if (is_handle_constructor(o, 2))
14899 argop->op_private |= OPpCOREARGS_DEREF2;
14900 if (opnum == OP_SUBSTR) {
14901 o->op_private |= OPpMAYBE_LVSUB;
14910 Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv,
14911 SV * const *new_const_svp)
14913 const char *hvname;
14914 bool is_const = cBOOL(CvCONST(old_cv));
14915 SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL;
14917 PERL_ARGS_ASSERT_REPORT_REDEFINED_CV;
14919 if (is_const && new_const_svp && old_const_sv == *new_const_svp)
14921 /* They are 2 constant subroutines generated from
14922 the same constant. This probably means that
14923 they are really the "same" proxy subroutine
14924 instantiated in 2 places. Most likely this is
14925 when a constant is exported twice. Don't warn.
14928 (ckWARN(WARN_REDEFINE)
14930 CvGV(old_cv) && GvSTASH(CvGV(old_cv))
14931 && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7
14932 && (hvname = HvNAME(GvSTASH(CvGV(old_cv))),
14933 strEQ(hvname, "autouse"))
14937 && ckWARN_d(WARN_REDEFINE)
14938 && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp))
14941 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
14943 ? "Constant subroutine %" SVf " redefined"
14944 : "Subroutine %" SVf " redefined",
14949 =for apidoc_section $hook
14951 These functions provide convenient and thread-safe means of manipulating
14958 =for apidoc wrap_op_checker
14960 Puts a C function into the chain of check functions for a specified op
14961 type. This is the preferred way to manipulate the L</PL_check> array.
14962 C<opcode> specifies which type of op is to be affected. C<new_checker>
14963 is a pointer to the C function that is to be added to that opcode's
14964 check chain, and C<old_checker_p> points to the storage location where a
14965 pointer to the next function in the chain will be stored. The value of
14966 C<new_checker> is written into the L</PL_check> array, while the value
14967 previously stored there is written to C<*old_checker_p>.
14969 L</PL_check> is global to an entire process, and a module wishing to
14970 hook op checking may find itself invoked more than once per process,
14971 typically in different threads. To handle that situation, this function
14972 is idempotent. The location C<*old_checker_p> must initially (once
14973 per process) contain a null pointer. A C variable of static duration
14974 (declared at file scope, typically also marked C<static> to give
14975 it internal linkage) will be implicitly initialised appropriately,
14976 if it does not have an explicit initialiser. This function will only
14977 actually modify the check chain if it finds C<*old_checker_p> to be null.
14978 This function is also thread safe on the small scale. It uses appropriate
14979 locking to avoid race conditions in accessing L</PL_check>.
14981 When this function is called, the function referenced by C<new_checker>
14982 must be ready to be called, except for C<*old_checker_p> being unfilled.
14983 In a threading situation, C<new_checker> may be called immediately,
14984 even before this function has returned. C<*old_checker_p> will always
14985 be appropriately set before C<new_checker> is called. If C<new_checker>
14986 decides not to do anything special with an op that it is given (which
14987 is the usual case for most uses of op check hooking), it must chain the
14988 check function referenced by C<*old_checker_p>.
14990 Taken all together, XS code to hook an op checker should typically look
14991 something like this:
14993 static Perl_check_t nxck_frob;
14994 static OP *myck_frob(pTHX_ OP *op) {
14996 op = nxck_frob(aTHX_ op);
15001 wrap_op_checker(OP_FROB, myck_frob, &nxck_frob);
15003 If you want to influence compilation of calls to a specific subroutine,
15004 then use L</cv_set_call_checker_flags> rather than hooking checking of
15005 all C<entersub> ops.
15011 Perl_wrap_op_checker(pTHX_ Optype opcode,
15012 Perl_check_t new_checker, Perl_check_t *old_checker_p)
15015 PERL_UNUSED_CONTEXT;
15016 PERL_ARGS_ASSERT_WRAP_OP_CHECKER;
15017 if (*old_checker_p) return;
15018 OP_CHECK_MUTEX_LOCK;
15019 if (!*old_checker_p) {
15020 *old_checker_p = PL_check[opcode];
15021 PL_check[opcode] = new_checker;
15023 OP_CHECK_MUTEX_UNLOCK;
15028 /* Efficient sub that returns a constant scalar value. */
15030 const_sv_xsub(pTHX_ CV* cv)
15033 SV *const sv = MUTABLE_SV(XSANY.any_ptr);
15034 PERL_UNUSED_ARG(items);
15044 const_av_xsub(pTHX_ CV* cv)
15047 AV * const av = MUTABLE_AV(XSANY.any_ptr);
15055 if (SvRMAGICAL(av))
15056 Perl_croak(aTHX_ "Magical list constants are not supported");
15057 if (GIMME_V != G_LIST) {
15059 ST(0) = sv_2mortal(newSViv((IV)AvFILLp(av)+1));
15062 EXTEND(SP, AvFILLp(av)+1);
15063 Copy(AvARRAY(av), &ST(0), AvFILLp(av)+1, SV *);
15064 XSRETURN(AvFILLp(av)+1);
15067 /* Copy an existing cop->cop_warnings field.
15068 * If it's one of the standard addresses, just re-use the address.
15069 * This is the e implementation for the DUP_WARNINGS() macro
15073 Perl_dup_warnings(pTHX_ STRLEN* warnings)
15076 STRLEN *new_warnings;
15078 if (warnings == NULL || specialWARN(warnings))
15081 size = sizeof(*warnings) + *warnings;
15083 new_warnings = (STRLEN*)PerlMemShared_malloc(size);
15084 Copy(warnings, new_warnings, size, char);
15085 return new_warnings;
15089 * ex: set ts=8 sts=4 sw=4 et: