1 #line 2 "op.c" 2 /* op.c 3 * 4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others 6 * 7 * You may distribute under the terms of either the GNU General Public 8 * License or the Artistic License, as specified in the README file. 9 * 10 */ 11 12 /* 13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was 14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the 15 * youngest of the Old Took's daughters); and Mr. Drogo was his second 16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed 17 * either way, as the saying is, if you follow me.' --the Gaffer 18 * 19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"] 20 */ 21 22 /* This file contains the functions that create, manipulate and optimize 23 * the OP structures that hold a compiled perl program. 24 * 25 * A Perl program is compiled into a tree of OPs. Each op contains 26 * structural pointers (eg to its siblings and the next op in the 27 * execution sequence), a pointer to the function that would execute the 28 * op, plus any data specific to that op. For example, an OP_CONST op 29 * points to the pp_const() function and to an SV containing the constant 30 * value. When pp_const() is executed, its job is to push that SV onto the 31 * stack. 32 * 33 * OPs are mainly created by the newFOO() functions, which are mainly 34 * called from the parser (in perly.y) as the code is parsed. For example 35 * the Perl code $a + $b * $c would cause the equivalent of the following 36 * to be called (oversimplifying a bit): 37 * 38 * newBINOP(OP_ADD, flags, 39 * newSVREF($a), 40 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c)) 41 * ) 42 * 43 * Note that during the build of miniperl, a temporary copy of this file 44 * is made, called opmini.c. 45 */ 46 47 /* 48 Perl's compiler is essentially a 3-pass compiler with interleaved phases: 49 50 A bottom-up pass 51 A top-down pass 52 An execution-order pass 53 54 The bottom-up pass is represented by all the "newOP" routines and 55 the ck_ routines. The bottom-upness is actually driven by yacc. 56 So at the point that a ck_ routine fires, we have no idea what the 57 context is, either upward in the syntax tree, or either forward or 58 backward in the execution order. (The bottom-up parser builds that 59 part of the execution order it knows about, but if you follow the "next" 60 links around, you'll find it's actually a closed loop through the 61 top level node.) 62 63 Whenever the bottom-up parser gets to a node that supplies context to 64 its components, it invokes that portion of the top-down pass that applies 65 to that part of the subtree (and marks the top node as processed, so 66 if a node further up supplies context, it doesn't have to take the 67 plunge again). As a particular subcase of this, as the new node is 68 built, it takes all the closed execution loops of its subcomponents 69 and links them into a new closed loop for the higher level node. But 70 it's still not the real execution order. 71 72 The actual execution order is not known till we get a grammar reduction 73 to a top-level unit like a subroutine or file that will be called by 74 "name" rather than via a "next" pointer. At that point, we can call 75 into peep() to do that code's portion of the 3rd pass. It has to be 76 recursive, but it's recursive on basic blocks, not on tree nodes. 77 */ 78 79 /* To implement user lexical pragmas, there needs to be a way at run time to 80 get the compile time state of %^H for that block. Storing %^H in every 81 block (or even COP) would be very expensive, so a different approach is 82 taken. The (running) state of %^H is serialised into a tree of HE-like 83 structs. Stores into %^H are chained onto the current leaf as a struct 84 refcounted_he * with the key and the value. Deletes from %^H are saved 85 with a value of PL_sv_placeholder. The state of %^H at any point can be 86 turned back into a regular HV by walking back up the tree from that point's 87 leaf, ignoring any key you've already seen (placeholder or not), storing 88 the rest into the HV structure, then removing the placeholders. Hence 89 memory is only used to store the %^H deltas from the enclosing COP, rather 90 than the entire %^H on each COP. 91 92 To cause actions on %^H to write out the serialisation records, it has 93 magic type 'H'. This magic (itself) does nothing, but its presence causes 94 the values to gain magic type 'h', which has entries for set and clear. 95 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store 96 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS> 97 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that 98 it will be correctly restored when any inner compiling scope is exited. 99 */ 100 101 #include "EXTERN.h" 102 #define PERL_IN_OP_C 103 #include "perl.h" 104 #include "keywords.h" 105 #include "feature.h" 106 #include "regcomp.h" 107 108 #define CALL_PEEP(o) PL_peepp(aTHX_ o) 109 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o) 110 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o) 111 112 static const char array_passed_to_stat[] = "Array passed to stat will be coerced to a scalar"; 113 114 /* Used to avoid recursion through the op tree in scalarvoid() and 115 op_free() 116 */ 117 118 #define DEFERRED_OP_STEP 100 119 #define DEFER_OP(o) \ 120 STMT_START { \ 121 if (UNLIKELY(defer_ix == (defer_stack_alloc-1))) { \ 122 defer_stack_alloc += DEFERRED_OP_STEP; \ 123 assert(defer_stack_alloc > 0); \ 124 Renew(defer_stack, defer_stack_alloc, OP *); \ 125 } \ 126 defer_stack[++defer_ix] = o; \ 127 } STMT_END 128 129 #define POP_DEFERRED_OP() (defer_ix >= 0 ? defer_stack[defer_ix--] : (OP *)NULL) 130 131 /* remove any leading "empty" ops from the op_next chain whose first 132 * node's address is stored in op_p. Store the updated address of the 133 * first node in op_p. 134 */ 135 136 STATIC void 137 S_prune_chain_head(OP** op_p) 138 { 139 while (*op_p 140 && ( (*op_p)->op_type == OP_NULL 141 || (*op_p)->op_type == OP_SCOPE 142 || (*op_p)->op_type == OP_SCALAR 143 || (*op_p)->op_type == OP_LINESEQ) 144 ) 145 *op_p = (*op_p)->op_next; 146 } 147 148 149 /* See the explanatory comments above struct opslab in op.h. */ 150 151 #ifdef PERL_DEBUG_READONLY_OPS 152 # define PERL_SLAB_SIZE 128 153 # define PERL_MAX_SLAB_SIZE 4096 154 # include <sys/mman.h> 155 #endif 156 157 #ifndef PERL_SLAB_SIZE 158 # define PERL_SLAB_SIZE 64 159 #endif 160 #ifndef PERL_MAX_SLAB_SIZE 161 # define PERL_MAX_SLAB_SIZE 2048 162 #endif 163 164 /* rounds up to nearest pointer */ 165 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *)) 166 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o))) 167 168 static OPSLAB * 169 S_new_slab(pTHX_ size_t sz) 170 { 171 #ifdef PERL_DEBUG_READONLY_OPS 172 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *), 173 PROT_READ|PROT_WRITE, 174 MAP_ANON|MAP_PRIVATE, -1, 0); 175 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n", 176 (unsigned long) sz, slab)); 177 if (slab == MAP_FAILED) { 178 perror("mmap failed"); 179 abort(); 180 } 181 slab->opslab_size = (U16)sz; 182 #else 183 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *)); 184 #endif 185 #ifndef WIN32 186 /* The context is unused in non-Windows */ 187 PERL_UNUSED_CONTEXT; 188 #endif 189 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1); 190 return slab; 191 } 192 193 /* requires double parens and aTHX_ */ 194 #define DEBUG_S_warn(args) \ 195 DEBUG_S( \ 196 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \ 197 ) 198 199 void * 200 Perl_Slab_Alloc(pTHX_ size_t sz) 201 { 202 OPSLAB *slab; 203 OPSLAB *slab2; 204 OPSLOT *slot; 205 OP *o; 206 size_t opsz, space; 207 208 /* We only allocate ops from the slab during subroutine compilation. 209 We find the slab via PL_compcv, hence that must be non-NULL. It could 210 also be pointing to a subroutine which is now fully set up (CvROOT() 211 pointing to the top of the optree for that sub), or a subroutine 212 which isn't using the slab allocator. If our sanity checks aren't met, 213 don't use a slab, but allocate the OP directly from the heap. */ 214 if (!PL_compcv || CvROOT(PL_compcv) 215 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv))) 216 { 217 o = (OP*)PerlMemShared_calloc(1, sz); 218 goto gotit; 219 } 220 221 /* While the subroutine is under construction, the slabs are accessed via 222 CvSTART(), to avoid needing to expand PVCV by one pointer for something 223 unneeded at runtime. Once a subroutine is constructed, the slabs are 224 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been 225 allocated yet. See the commit message for 8be227ab5eaa23f2 for more 226 details. */ 227 if (!CvSTART(PL_compcv)) { 228 CvSTART(PL_compcv) = 229 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE)); 230 CvSLABBED_on(PL_compcv); 231 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */ 232 } 233 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt; 234 235 opsz = SIZE_TO_PSIZE(sz); 236 sz = opsz + OPSLOT_HEADER_P; 237 238 /* The slabs maintain a free list of OPs. In particular, constant folding 239 will free up OPs, so it makes sense to re-use them where possible. A 240 freed up slot is used in preference to a new allocation. */ 241 if (slab->opslab_freed) { 242 OP **too = &slab->opslab_freed; 243 o = *too; 244 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", (void*)o, (void*)slab)); 245 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) { 246 DEBUG_S_warn((aTHX_ "Alas! too small")); 247 o = *(too = &o->op_next); 248 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", (void*)o)); } 249 } 250 if (o) { 251 *too = o->op_next; 252 Zero(o, opsz, I32 *); 253 o->op_slabbed = 1; 254 goto gotit; 255 } 256 } 257 258 #define INIT_OPSLOT \ 259 slot->opslot_slab = slab; \ 260 slot->opslot_next = slab2->opslab_first; \ 261 slab2->opslab_first = slot; \ 262 o = &slot->opslot_op; \ 263 o->op_slabbed = 1 264 265 /* The partially-filled slab is next in the chain. */ 266 slab2 = slab->opslab_next ? slab->opslab_next : slab; 267 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) { 268 /* Remaining space is too small. */ 269 270 /* If we can fit a BASEOP, add it to the free chain, so as not 271 to waste it. */ 272 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) { 273 slot = &slab2->opslab_slots; 274 INIT_OPSLOT; 275 o->op_type = OP_FREED; 276 o->op_next = slab->opslab_freed; 277 slab->opslab_freed = o; 278 } 279 280 /* Create a new slab. Make this one twice as big. */ 281 slot = slab2->opslab_first; 282 while (slot->opslot_next) slot = slot->opslot_next; 283 slab2 = S_new_slab(aTHX_ 284 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE 285 ? PERL_MAX_SLAB_SIZE 286 : (DIFF(slab2, slot)+1)*2); 287 slab2->opslab_next = slab->opslab_next; 288 slab->opslab_next = slab2; 289 } 290 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz); 291 292 /* Create a new op slot */ 293 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz); 294 assert(slot >= &slab2->opslab_slots); 295 if (DIFF(&slab2->opslab_slots, slot) 296 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) 297 slot = &slab2->opslab_slots; 298 INIT_OPSLOT; 299 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab)); 300 301 gotit: 302 #ifdef PERL_OP_PARENT 303 /* moresib == 0, op_sibling == 0 implies a solitary unattached op */ 304 assert(!o->op_moresib); 305 assert(!o->op_sibparent); 306 #endif 307 308 return (void *)o; 309 } 310 311 #undef INIT_OPSLOT 312 313 #ifdef PERL_DEBUG_READONLY_OPS 314 void 315 Perl_Slab_to_ro(pTHX_ OPSLAB *slab) 316 { 317 PERL_ARGS_ASSERT_SLAB_TO_RO; 318 319 if (slab->opslab_readonly) return; 320 slab->opslab_readonly = 1; 321 for (; slab; slab = slab->opslab_next) { 322 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n", 323 (unsigned long) slab->opslab_size, slab));*/ 324 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ)) 325 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab, 326 (unsigned long)slab->opslab_size, errno); 327 } 328 } 329 330 void 331 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab) 332 { 333 OPSLAB *slab2; 334 335 PERL_ARGS_ASSERT_SLAB_TO_RW; 336 337 if (!slab->opslab_readonly) return; 338 slab2 = slab; 339 for (; slab2; slab2 = slab2->opslab_next) { 340 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n", 341 (unsigned long) size, slab2));*/ 342 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *), 343 PROT_READ|PROT_WRITE)) { 344 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab, 345 (unsigned long)slab2->opslab_size, errno); 346 } 347 } 348 slab->opslab_readonly = 0; 349 } 350 351 #else 352 # define Slab_to_rw(op) NOOP 353 #endif 354 355 /* This cannot possibly be right, but it was copied from the old slab 356 allocator, to which it was originally added, without explanation, in 357 commit 083fcd5. */ 358 #ifdef NETWARE 359 # define PerlMemShared PerlMem 360 #endif 361 362 void 363 Perl_Slab_Free(pTHX_ void *op) 364 { 365 OP * const o = (OP *)op; 366 OPSLAB *slab; 367 368 PERL_ARGS_ASSERT_SLAB_FREE; 369 370 if (!o->op_slabbed) { 371 if (!o->op_static) 372 PerlMemShared_free(op); 373 return; 374 } 375 376 slab = OpSLAB(o); 377 /* If this op is already freed, our refcount will get screwy. */ 378 assert(o->op_type != OP_FREED); 379 o->op_type = OP_FREED; 380 o->op_next = slab->opslab_freed; 381 slab->opslab_freed = o; 382 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab)); 383 OpslabREFCNT_dec_padok(slab); 384 } 385 386 void 387 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab) 388 { 389 const bool havepad = !!PL_comppad; 390 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD; 391 if (havepad) { 392 ENTER; 393 PAD_SAVE_SETNULLPAD(); 394 } 395 opslab_free(slab); 396 if (havepad) LEAVE; 397 } 398 399 void 400 Perl_opslab_free(pTHX_ OPSLAB *slab) 401 { 402 OPSLAB *slab2; 403 PERL_ARGS_ASSERT_OPSLAB_FREE; 404 PERL_UNUSED_CONTEXT; 405 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab)); 406 assert(slab->opslab_refcnt == 1); 407 do { 408 slab2 = slab->opslab_next; 409 #ifdef DEBUGGING 410 slab->opslab_refcnt = ~(size_t)0; 411 #endif 412 #ifdef PERL_DEBUG_READONLY_OPS 413 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n", 414 (void*)slab)); 415 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) { 416 perror("munmap failed"); 417 abort(); 418 } 419 #else 420 PerlMemShared_free(slab); 421 #endif 422 slab = slab2; 423 } while (slab); 424 } 425 426 void 427 Perl_opslab_force_free(pTHX_ OPSLAB *slab) 428 { 429 OPSLAB *slab2; 430 OPSLOT *slot; 431 #ifdef DEBUGGING 432 size_t savestack_count = 0; 433 #endif 434 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE; 435 slab2 = slab; 436 do { 437 for (slot = slab2->opslab_first; 438 slot->opslot_next; 439 slot = slot->opslot_next) { 440 if (slot->opslot_op.op_type != OP_FREED 441 && !(slot->opslot_op.op_savefree 442 #ifdef DEBUGGING 443 && ++savestack_count 444 #endif 445 ) 446 ) { 447 assert(slot->opslot_op.op_slabbed); 448 op_free(&slot->opslot_op); 449 if (slab->opslab_refcnt == 1) goto free; 450 } 451 } 452 } while ((slab2 = slab2->opslab_next)); 453 /* > 1 because the CV still holds a reference count. */ 454 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */ 455 #ifdef DEBUGGING 456 assert(savestack_count == slab->opslab_refcnt-1); 457 #endif 458 /* Remove the CV’s reference count. */ 459 slab->opslab_refcnt--; 460 return; 461 } 462 free: 463 opslab_free(slab); 464 } 465 466 #ifdef PERL_DEBUG_READONLY_OPS 467 OP * 468 Perl_op_refcnt_inc(pTHX_ OP *o) 469 { 470 if(o) { 471 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL; 472 if (slab && slab->opslab_readonly) { 473 Slab_to_rw(slab); 474 ++o->op_targ; 475 Slab_to_ro(slab); 476 } else { 477 ++o->op_targ; 478 } 479 } 480 return o; 481 482 } 483 484 PADOFFSET 485 Perl_op_refcnt_dec(pTHX_ OP *o) 486 { 487 PADOFFSET result; 488 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL; 489 490 PERL_ARGS_ASSERT_OP_REFCNT_DEC; 491 492 if (slab && slab->opslab_readonly) { 493 Slab_to_rw(slab); 494 result = --o->op_targ; 495 Slab_to_ro(slab); 496 } else { 497 result = --o->op_targ; 498 } 499 return result; 500 } 501 #endif 502 /* 503 * In the following definition, the ", (OP*)0" is just to make the compiler 504 * think the expression is of the right type: croak actually does a Siglongjmp. 505 */ 506 #define CHECKOP(type,o) \ 507 ((PL_op_mask && PL_op_mask[type]) \ 508 ? ( op_free((OP*)o), \ 509 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \ 510 (OP*)0 ) \ 511 : PL_check[type](aTHX_ (OP*)o)) 512 513 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2) 514 515 #define OpTYPE_set(o,type) \ 516 STMT_START { \ 517 o->op_type = (OPCODE)type; \ 518 o->op_ppaddr = PL_ppaddr[type]; \ 519 } STMT_END 520 521 STATIC OP * 522 S_no_fh_allowed(pTHX_ OP *o) 523 { 524 PERL_ARGS_ASSERT_NO_FH_ALLOWED; 525 526 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function", 527 OP_DESC(o))); 528 return o; 529 } 530 531 STATIC OP * 532 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags) 533 { 534 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV; 535 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags); 536 return o; 537 } 538 539 STATIC OP * 540 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags) 541 { 542 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV; 543 544 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags); 545 return o; 546 } 547 548 STATIC void 549 S_bad_type_pv(pTHX_ I32 n, const char *t, const OP *o, const OP *kid) 550 { 551 PERL_ARGS_ASSERT_BAD_TYPE_PV; 552 553 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)", 554 (int)n, PL_op_desc[(o)->op_type], t, OP_DESC(kid)), 0); 555 } 556 557 /* remove flags var, its unused in all callers, move to to right end since gv 558 and kid are always the same */ 559 STATIC void 560 S_bad_type_gv(pTHX_ I32 n, GV *gv, const OP *kid, const char *t) 561 { 562 SV * const namesv = cv_name((CV *)gv, NULL, 0); 563 PERL_ARGS_ASSERT_BAD_TYPE_GV; 564 565 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)", 566 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv)); 567 } 568 569 STATIC void 570 S_no_bareword_allowed(pTHX_ OP *o) 571 { 572 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED; 573 574 qerror(Perl_mess(aTHX_ 575 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use", 576 SVfARG(cSVOPo_sv))); 577 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */ 578 } 579 580 /* "register" allocation */ 581 582 PADOFFSET 583 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags) 584 { 585 PADOFFSET off; 586 const bool is_our = (PL_parser->in_my == KEY_our); 587 588 PERL_ARGS_ASSERT_ALLOCMY; 589 590 if (flags & ~SVf_UTF8) 591 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf, 592 (UV)flags); 593 594 /* complain about "my $<special_var>" etc etc */ 595 if (len && 596 !(is_our || 597 isALPHA(name[1]) || 598 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) || 599 (name[1] == '_' && len > 2))) 600 { 601 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1])) 602 && isASCII(name[1]) 603 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) { 604 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"", 605 name[0], toCTRL(name[1]), (int)(len - 2), name + 2, 606 PL_parser->in_my == KEY_state ? "state" : "my")); 607 } else { 608 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name, 609 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8); 610 } 611 } 612 613 /* allocate a spare slot and store the name in that slot */ 614 615 off = pad_add_name_pvn(name, len, 616 (is_our ? padadd_OUR : 617 PL_parser->in_my == KEY_state ? padadd_STATE : 0), 618 PL_parser->in_my_stash, 619 (is_our 620 /* $_ is always in main::, even with our */ 621 ? (PL_curstash && !memEQs(name,len,"$_") 622 ? PL_curstash 623 : PL_defstash) 624 : NULL 625 ) 626 ); 627 /* anon sub prototypes contains state vars should always be cloned, 628 * otherwise the state var would be shared between anon subs */ 629 630 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv)) 631 CvCLONE_on(PL_compcv); 632 633 return off; 634 } 635 636 /* 637 =head1 Optree Manipulation Functions 638 639 =for apidoc alloccopstash 640 641 Available only under threaded builds, this function allocates an entry in 642 C<PL_stashpad> for the stash passed to it. 643 644 =cut 645 */ 646 647 #ifdef USE_ITHREADS 648 PADOFFSET 649 Perl_alloccopstash(pTHX_ HV *hv) 650 { 651 PADOFFSET off = 0, o = 1; 652 bool found_slot = FALSE; 653 654 PERL_ARGS_ASSERT_ALLOCCOPSTASH; 655 656 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix; 657 658 for (; o < PL_stashpadmax; ++o) { 659 if (PL_stashpad[o] == hv) return PL_stashpadix = o; 660 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV) 661 found_slot = TRUE, off = o; 662 } 663 if (!found_slot) { 664 Renew(PL_stashpad, PL_stashpadmax + 10, HV *); 665 Zero(PL_stashpad + PL_stashpadmax, 10, HV *); 666 off = PL_stashpadmax; 667 PL_stashpadmax += 10; 668 } 669 670 PL_stashpad[PL_stashpadix = off] = hv; 671 return off; 672 } 673 #endif 674 675 /* free the body of an op without examining its contents. 676 * Always use this rather than FreeOp directly */ 677 678 static void 679 S_op_destroy(pTHX_ OP *o) 680 { 681 FreeOp(o); 682 } 683 684 /* Destructor */ 685 686 /* 687 =for apidoc Am|void|op_free|OP *o 688 689 Free an op. Only use this when an op is no longer linked to from any 690 optree. 691 692 =cut 693 */ 694 695 void 696 Perl_op_free(pTHX_ OP *o) 697 { 698 dVAR; 699 OPCODE type; 700 SSize_t defer_ix = -1; 701 SSize_t defer_stack_alloc = 0; 702 OP **defer_stack = NULL; 703 704 do { 705 706 /* Though ops may be freed twice, freeing the op after its slab is a 707 big no-no. */ 708 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0); 709 /* During the forced freeing of ops after compilation failure, kidops 710 may be freed before their parents. */ 711 if (!o || o->op_type == OP_FREED) 712 continue; 713 714 type = o->op_type; 715 716 /* an op should only ever acquire op_private flags that we know about. 717 * If this fails, you may need to fix something in regen/op_private. 718 * Don't bother testing if: 719 * * the op_ppaddr doesn't match the op; someone may have 720 * overridden the op and be doing strange things with it; 721 * * we've errored, as op flags are often left in an 722 * inconsistent state then. Note that an error when 723 * compiling the main program leaves PL_parser NULL, so 724 * we can't spot faults in the main code, only 725 * evaled/required code */ 726 #ifdef DEBUGGING 727 if ( o->op_ppaddr == PL_ppaddr[o->op_type] 728 && PL_parser 729 && !PL_parser->error_count) 730 { 731 assert(!(o->op_private & ~PL_op_private_valid[type])); 732 } 733 #endif 734 735 if (o->op_private & OPpREFCOUNTED) { 736 switch (type) { 737 case OP_LEAVESUB: 738 case OP_LEAVESUBLV: 739 case OP_LEAVEEVAL: 740 case OP_LEAVE: 741 case OP_SCOPE: 742 case OP_LEAVEWRITE: 743 { 744 PADOFFSET refcnt; 745 OP_REFCNT_LOCK; 746 refcnt = OpREFCNT_dec(o); 747 OP_REFCNT_UNLOCK; 748 if (refcnt) { 749 /* Need to find and remove any pattern match ops from the list 750 we maintain for reset(). */ 751 find_and_forget_pmops(o); 752 continue; 753 } 754 } 755 break; 756 default: 757 break; 758 } 759 } 760 761 /* Call the op_free hook if it has been set. Do it now so that it's called 762 * at the right time for refcounted ops, but still before all of the kids 763 * are freed. */ 764 CALL_OPFREEHOOK(o); 765 766 if (o->op_flags & OPf_KIDS) { 767 OP *kid, *nextkid; 768 for (kid = cUNOPo->op_first; kid; kid = nextkid) { 769 nextkid = OpSIBLING(kid); /* Get before next freeing kid */ 770 if (!kid || kid->op_type == OP_FREED) 771 /* During the forced freeing of ops after 772 compilation failure, kidops may be freed before 773 their parents. */ 774 continue; 775 if (!(kid->op_flags & OPf_KIDS)) 776 /* If it has no kids, just free it now */ 777 op_free(kid); 778 else 779 DEFER_OP(kid); 780 } 781 } 782 if (type == OP_NULL) 783 type = (OPCODE)o->op_targ; 784 785 if (o->op_slabbed) 786 Slab_to_rw(OpSLAB(o)); 787 788 /* COP* is not cleared by op_clear() so that we may track line 789 * numbers etc even after null() */ 790 if (type == OP_NEXTSTATE || type == OP_DBSTATE) { 791 cop_free((COP*)o); 792 } 793 794 op_clear(o); 795 FreeOp(o); 796 #ifdef DEBUG_LEAKING_SCALARS 797 if (PL_op == o) 798 PL_op = NULL; 799 #endif 800 } while ( (o = POP_DEFERRED_OP()) ); 801 802 Safefree(defer_stack); 803 } 804 805 /* S_op_clear_gv(): free a GV attached to an OP */ 806 807 STATIC 808 #ifdef USE_ITHREADS 809 void S_op_clear_gv(pTHX_ OP *o, PADOFFSET *ixp) 810 #else 811 void S_op_clear_gv(pTHX_ OP *o, SV**svp) 812 #endif 813 { 814 815 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV 816 || o->op_type == OP_MULTIDEREF) 817 #ifdef USE_ITHREADS 818 && PL_curpad 819 ? ((GV*)PAD_SVl(*ixp)) : NULL; 820 #else 821 ? (GV*)(*svp) : NULL; 822 #endif 823 /* It's possible during global destruction that the GV is freed 824 before the optree. Whilst the SvREFCNT_inc is happy to bump from 825 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0 826 will trigger an assertion failure, because the entry to sv_clear 827 checks that the scalar is not already freed. A check of for 828 !SvIS_FREED(gv) turns out to be invalid, because during global 829 destruction the reference count can be forced down to zero 830 (with SVf_BREAK set). In which case raising to 1 and then 831 dropping to 0 triggers cleanup before it should happen. I 832 *think* that this might actually be a general, systematic, 833 weakness of the whole idea of SVf_BREAK, in that code *is* 834 allowed to raise and lower references during global destruction, 835 so any *valid* code that happens to do this during global 836 destruction might well trigger premature cleanup. */ 837 bool still_valid = gv && SvREFCNT(gv); 838 839 if (still_valid) 840 SvREFCNT_inc_simple_void(gv); 841 #ifdef USE_ITHREADS 842 if (*ixp > 0) { 843 pad_swipe(*ixp, TRUE); 844 *ixp = 0; 845 } 846 #else 847 SvREFCNT_dec(*svp); 848 *svp = NULL; 849 #endif 850 if (still_valid) { 851 int try_downgrade = SvREFCNT(gv) == 2; 852 SvREFCNT_dec_NN(gv); 853 if (try_downgrade) 854 gv_try_downgrade(gv); 855 } 856 } 857 858 859 void 860 Perl_op_clear(pTHX_ OP *o) 861 { 862 863 dVAR; 864 865 PERL_ARGS_ASSERT_OP_CLEAR; 866 867 switch (o->op_type) { 868 case OP_NULL: /* Was holding old type, if any. */ 869 /* FALLTHROUGH */ 870 case OP_ENTERTRY: 871 case OP_ENTEREVAL: /* Was holding hints. */ 872 o->op_targ = 0; 873 break; 874 default: 875 if (!(o->op_flags & OPf_REF) 876 || (PL_check[o->op_type] != Perl_ck_ftst)) 877 break; 878 /* FALLTHROUGH */ 879 case OP_GVSV: 880 case OP_GV: 881 case OP_AELEMFAST: 882 #ifdef USE_ITHREADS 883 S_op_clear_gv(aTHX_ o, &(cPADOPx(o)->op_padix)); 884 #else 885 S_op_clear_gv(aTHX_ o, &(cSVOPx(o)->op_sv)); 886 #endif 887 break; 888 case OP_METHOD_REDIR: 889 case OP_METHOD_REDIR_SUPER: 890 #ifdef USE_ITHREADS 891 if (cMETHOPx(o)->op_rclass_targ) { 892 pad_swipe(cMETHOPx(o)->op_rclass_targ, 1); 893 cMETHOPx(o)->op_rclass_targ = 0; 894 } 895 #else 896 SvREFCNT_dec(cMETHOPx(o)->op_rclass_sv); 897 cMETHOPx(o)->op_rclass_sv = NULL; 898 #endif 899 case OP_METHOD_NAMED: 900 case OP_METHOD_SUPER: 901 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv); 902 cMETHOPx(o)->op_u.op_meth_sv = NULL; 903 #ifdef USE_ITHREADS 904 if (o->op_targ) { 905 pad_swipe(o->op_targ, 1); 906 o->op_targ = 0; 907 } 908 #endif 909 break; 910 case OP_CONST: 911 case OP_HINTSEVAL: 912 SvREFCNT_dec(cSVOPo->op_sv); 913 cSVOPo->op_sv = NULL; 914 #ifdef USE_ITHREADS 915 /** Bug #15654 916 Even if op_clear does a pad_free for the target of the op, 917 pad_free doesn't actually remove the sv that exists in the pad; 918 instead it lives on. This results in that it could be reused as 919 a target later on when the pad was reallocated. 920 **/ 921 if(o->op_targ) { 922 pad_swipe(o->op_targ,1); 923 o->op_targ = 0; 924 } 925 #endif 926 break; 927 case OP_DUMP: 928 case OP_GOTO: 929 case OP_NEXT: 930 case OP_LAST: 931 case OP_REDO: 932 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS)) 933 break; 934 /* FALLTHROUGH */ 935 case OP_TRANS: 936 case OP_TRANSR: 937 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) { 938 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR); 939 #ifdef USE_ITHREADS 940 if (cPADOPo->op_padix > 0) { 941 pad_swipe(cPADOPo->op_padix, TRUE); 942 cPADOPo->op_padix = 0; 943 } 944 #else 945 SvREFCNT_dec(cSVOPo->op_sv); 946 cSVOPo->op_sv = NULL; 947 #endif 948 } 949 else { 950 PerlMemShared_free(cPVOPo->op_pv); 951 cPVOPo->op_pv = NULL; 952 } 953 break; 954 case OP_SUBST: 955 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot); 956 goto clear_pmop; 957 case OP_PUSHRE: 958 #ifdef USE_ITHREADS 959 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) { 960 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE); 961 } 962 #else 963 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv)); 964 #endif 965 /* FALLTHROUGH */ 966 case OP_MATCH: 967 case OP_QR: 968 clear_pmop: 969 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE)) 970 op_free(cPMOPo->op_code_list); 971 cPMOPo->op_code_list = NULL; 972 forget_pmop(cPMOPo); 973 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL; 974 /* we use the same protection as the "SAFE" version of the PM_ macros 975 * here since sv_clean_all might release some PMOPs 976 * after PL_regex_padav has been cleared 977 * and the clearing of PL_regex_padav needs to 978 * happen before sv_clean_all 979 */ 980 #ifdef USE_ITHREADS 981 if(PL_regex_pad) { /* We could be in destruction */ 982 const IV offset = (cPMOPo)->op_pmoffset; 983 ReREFCNT_dec(PM_GETRE(cPMOPo)); 984 PL_regex_pad[offset] = &PL_sv_undef; 985 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset, 986 sizeof(offset)); 987 } 988 #else 989 ReREFCNT_dec(PM_GETRE(cPMOPo)); 990 PM_SETRE(cPMOPo, NULL); 991 #endif 992 993 break; 994 995 case OP_MULTIDEREF: 996 { 997 UNOP_AUX_item *items = cUNOP_AUXo->op_aux; 998 UV actions = items->uv; 999 bool last = 0; 1000 bool is_hash = FALSE; 1001 1002 while (!last) { 1003 switch (actions & MDEREF_ACTION_MASK) { 1004 1005 case MDEREF_reload: 1006 actions = (++items)->uv; 1007 continue; 1008 1009 case MDEREF_HV_padhv_helem: 1010 is_hash = TRUE; 1011 case MDEREF_AV_padav_aelem: 1012 pad_free((++items)->pad_offset); 1013 goto do_elem; 1014 1015 case MDEREF_HV_gvhv_helem: 1016 is_hash = TRUE; 1017 case MDEREF_AV_gvav_aelem: 1018 #ifdef USE_ITHREADS 1019 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset)); 1020 #else 1021 S_op_clear_gv(aTHX_ o, &((++items)->sv)); 1022 #endif 1023 goto do_elem; 1024 1025 case MDEREF_HV_gvsv_vivify_rv2hv_helem: 1026 is_hash = TRUE; 1027 case MDEREF_AV_gvsv_vivify_rv2av_aelem: 1028 #ifdef USE_ITHREADS 1029 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset)); 1030 #else 1031 S_op_clear_gv(aTHX_ o, &((++items)->sv)); 1032 #endif 1033 goto do_vivify_rv2xv_elem; 1034 1035 case MDEREF_HV_padsv_vivify_rv2hv_helem: 1036 is_hash = TRUE; 1037 case MDEREF_AV_padsv_vivify_rv2av_aelem: 1038 pad_free((++items)->pad_offset); 1039 goto do_vivify_rv2xv_elem; 1040 1041 case MDEREF_HV_pop_rv2hv_helem: 1042 case MDEREF_HV_vivify_rv2hv_helem: 1043 is_hash = TRUE; 1044 do_vivify_rv2xv_elem: 1045 case MDEREF_AV_pop_rv2av_aelem: 1046 case MDEREF_AV_vivify_rv2av_aelem: 1047 do_elem: 1048 switch (actions & MDEREF_INDEX_MASK) { 1049 case MDEREF_INDEX_none: 1050 last = 1; 1051 break; 1052 case MDEREF_INDEX_const: 1053 if (is_hash) { 1054 #ifdef USE_ITHREADS 1055 /* see RT #15654 */ 1056 pad_swipe((++items)->pad_offset, 1); 1057 #else 1058 SvREFCNT_dec((++items)->sv); 1059 #endif 1060 } 1061 else 1062 items++; 1063 break; 1064 case MDEREF_INDEX_padsv: 1065 pad_free((++items)->pad_offset); 1066 break; 1067 case MDEREF_INDEX_gvsv: 1068 #ifdef USE_ITHREADS 1069 S_op_clear_gv(aTHX_ o, &((++items)->pad_offset)); 1070 #else 1071 S_op_clear_gv(aTHX_ o, &((++items)->sv)); 1072 #endif 1073 break; 1074 } 1075 1076 if (actions & MDEREF_FLAG_last) 1077 last = 1; 1078 is_hash = FALSE; 1079 1080 break; 1081 1082 default: 1083 assert(0); 1084 last = 1; 1085 break; 1086 1087 } /* switch */ 1088 1089 actions >>= MDEREF_SHIFT; 1090 } /* while */ 1091 1092 /* start of malloc is at op_aux[-1], where the length is 1093 * stored */ 1094 PerlMemShared_free(cUNOP_AUXo->op_aux - 1); 1095 } 1096 break; 1097 } 1098 1099 if (o->op_targ > 0) { 1100 pad_free(o->op_targ); 1101 o->op_targ = 0; 1102 } 1103 } 1104 1105 STATIC void 1106 S_cop_free(pTHX_ COP* cop) 1107 { 1108 PERL_ARGS_ASSERT_COP_FREE; 1109 1110 CopFILE_free(cop); 1111 if (! specialWARN(cop->cop_warnings)) 1112 PerlMemShared_free(cop->cop_warnings); 1113 cophh_free(CopHINTHASH_get(cop)); 1114 if (PL_curcop == cop) 1115 PL_curcop = NULL; 1116 } 1117 1118 STATIC void 1119 S_forget_pmop(pTHX_ PMOP *const o 1120 ) 1121 { 1122 HV * const pmstash = PmopSTASH(o); 1123 1124 PERL_ARGS_ASSERT_FORGET_PMOP; 1125 1126 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) { 1127 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab); 1128 if (mg) { 1129 PMOP **const array = (PMOP**) mg->mg_ptr; 1130 U32 count = mg->mg_len / sizeof(PMOP**); 1131 U32 i = count; 1132 1133 while (i--) { 1134 if (array[i] == o) { 1135 /* Found it. Move the entry at the end to overwrite it. */ 1136 array[i] = array[--count]; 1137 mg->mg_len = count * sizeof(PMOP**); 1138 /* Could realloc smaller at this point always, but probably 1139 not worth it. Probably worth free()ing if we're the 1140 last. */ 1141 if(!count) { 1142 Safefree(mg->mg_ptr); 1143 mg->mg_ptr = NULL; 1144 } 1145 break; 1146 } 1147 } 1148 } 1149 } 1150 if (PL_curpm == o) 1151 PL_curpm = NULL; 1152 } 1153 1154 STATIC void 1155 S_find_and_forget_pmops(pTHX_ OP *o) 1156 { 1157 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS; 1158 1159 if (o->op_flags & OPf_KIDS) { 1160 OP *kid = cUNOPo->op_first; 1161 while (kid) { 1162 switch (kid->op_type) { 1163 case OP_SUBST: 1164 case OP_PUSHRE: 1165 case OP_MATCH: 1166 case OP_QR: 1167 forget_pmop((PMOP*)kid); 1168 } 1169 find_and_forget_pmops(kid); 1170 kid = OpSIBLING(kid); 1171 } 1172 } 1173 } 1174 1175 /* 1176 =for apidoc Am|void|op_null|OP *o 1177 1178 Neutralizes an op when it is no longer needed, but is still linked to from 1179 other ops. 1180 1181 =cut 1182 */ 1183 1184 void 1185 Perl_op_null(pTHX_ OP *o) 1186 { 1187 dVAR; 1188 1189 PERL_ARGS_ASSERT_OP_NULL; 1190 1191 if (o->op_type == OP_NULL) 1192 return; 1193 op_clear(o); 1194 o->op_targ = o->op_type; 1195 OpTYPE_set(o, OP_NULL); 1196 } 1197 1198 void 1199 Perl_op_refcnt_lock(pTHX) 1200 PERL_TSA_ACQUIRE(PL_op_mutex) 1201 { 1202 #ifdef USE_ITHREADS 1203 dVAR; 1204 #endif 1205 PERL_UNUSED_CONTEXT; 1206 OP_REFCNT_LOCK; 1207 } 1208 1209 void 1210 Perl_op_refcnt_unlock(pTHX) 1211 PERL_TSA_RELEASE(PL_op_mutex) 1212 { 1213 #ifdef USE_ITHREADS 1214 dVAR; 1215 #endif 1216 PERL_UNUSED_CONTEXT; 1217 OP_REFCNT_UNLOCK; 1218 } 1219 1220 1221 /* 1222 =for apidoc op_sibling_splice 1223 1224 A general function for editing the structure of an existing chain of 1225 op_sibling nodes. By analogy with the perl-level C<splice()> function, allows 1226 you to delete zero or more sequential nodes, replacing them with zero or 1227 more different nodes. Performs the necessary op_first/op_last 1228 housekeeping on the parent node and op_sibling manipulation on the 1229 children. The last deleted node will be marked as as the last node by 1230 updating the op_sibling/op_sibparent or op_moresib field as appropriate. 1231 1232 Note that op_next is not manipulated, and nodes are not freed; that is the 1233 responsibility of the caller. It also won't create a new list op for an 1234 empty list etc; use higher-level functions like op_append_elem() for that. 1235 1236 C<parent> is the parent node of the sibling chain. It may passed as C<NULL> if 1237 the splicing doesn't affect the first or last op in the chain. 1238 1239 C<start> is the node preceding the first node to be spliced. Node(s) 1240 following it will be deleted, and ops will be inserted after it. If it is 1241 C<NULL>, the first node onwards is deleted, and nodes are inserted at the 1242 beginning. 1243 1244 C<del_count> is the number of nodes to delete. If zero, no nodes are deleted. 1245 If -1 or greater than or equal to the number of remaining kids, all 1246 remaining kids are deleted. 1247 1248 C<insert> is the first of a chain of nodes to be inserted in place of the nodes. 1249 If C<NULL>, no nodes are inserted. 1250 1251 The head of the chain of deleted ops is returned, or C<NULL> if no ops were 1252 deleted. 1253 1254 For example: 1255 1256 action before after returns 1257 ------ ----- ----- ------- 1258 1259 P P 1260 splice(P, A, 2, X-Y-Z) | | B-C 1261 A-B-C-D A-X-Y-Z-D 1262 1263 P P 1264 splice(P, NULL, 1, X-Y) | | A 1265 A-B-C-D X-Y-B-C-D 1266 1267 P P 1268 splice(P, NULL, 3, NULL) | | A-B-C 1269 A-B-C-D D 1270 1271 P P 1272 splice(P, B, 0, X-Y) | | NULL 1273 A-B-C-D A-B-X-Y-C-D 1274 1275 1276 For lower-level direct manipulation of C<op_sibparent> and C<op_moresib>, 1277 see C<L</OpMORESIB_set>>, C<L</OpLASTSIB_set>>, C<L</OpMAYBESIB_set>>. 1278 1279 =cut 1280 */ 1281 1282 OP * 1283 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert) 1284 { 1285 OP *first; 1286 OP *rest; 1287 OP *last_del = NULL; 1288 OP *last_ins = NULL; 1289 1290 if (start) 1291 first = OpSIBLING(start); 1292 else if (!parent) 1293 goto no_parent; 1294 else 1295 first = cLISTOPx(parent)->op_first; 1296 1297 assert(del_count >= -1); 1298 1299 if (del_count && first) { 1300 last_del = first; 1301 while (--del_count && OpHAS_SIBLING(last_del)) 1302 last_del = OpSIBLING(last_del); 1303 rest = OpSIBLING(last_del); 1304 OpLASTSIB_set(last_del, NULL); 1305 } 1306 else 1307 rest = first; 1308 1309 if (insert) { 1310 last_ins = insert; 1311 while (OpHAS_SIBLING(last_ins)) 1312 last_ins = OpSIBLING(last_ins); 1313 OpMAYBESIB_set(last_ins, rest, NULL); 1314 } 1315 else 1316 insert = rest; 1317 1318 if (start) { 1319 OpMAYBESIB_set(start, insert, NULL); 1320 } 1321 else { 1322 if (!parent) 1323 goto no_parent; 1324 cLISTOPx(parent)->op_first = insert; 1325 if (insert) 1326 parent->op_flags |= OPf_KIDS; 1327 else 1328 parent->op_flags &= ~OPf_KIDS; 1329 } 1330 1331 if (!rest) { 1332 /* update op_last etc */ 1333 U32 type; 1334 OP *lastop; 1335 1336 if (!parent) 1337 goto no_parent; 1338 1339 /* ought to use OP_CLASS(parent) here, but that can't handle 1340 * ex-foo OP_NULL ops. Also note that XopENTRYCUSTOM() can't 1341 * either */ 1342 type = parent->op_type; 1343 if (type == OP_CUSTOM) { 1344 dTHX; 1345 type = XopENTRYCUSTOM(parent, xop_class); 1346 } 1347 else { 1348 if (type == OP_NULL) 1349 type = parent->op_targ; 1350 type = PL_opargs[type] & OA_CLASS_MASK; 1351 } 1352 1353 lastop = last_ins ? last_ins : start ? start : NULL; 1354 if ( type == OA_BINOP 1355 || type == OA_LISTOP 1356 || type == OA_PMOP 1357 || type == OA_LOOP 1358 ) 1359 cLISTOPx(parent)->op_last = lastop; 1360 1361 if (lastop) 1362 OpLASTSIB_set(lastop, parent); 1363 } 1364 return last_del ? first : NULL; 1365 1366 no_parent: 1367 Perl_croak_nocontext("panic: op_sibling_splice(): NULL parent"); 1368 } 1369 1370 1371 #ifdef PERL_OP_PARENT 1372 1373 /* 1374 =for apidoc op_parent 1375 1376 Returns the parent OP of C<o>, if it has a parent. Returns C<NULL> otherwise. 1377 This function is only available on perls built with C<-DPERL_OP_PARENT>. 1378 1379 =cut 1380 */ 1381 1382 OP * 1383 Perl_op_parent(OP *o) 1384 { 1385 PERL_ARGS_ASSERT_OP_PARENT; 1386 while (OpHAS_SIBLING(o)) 1387 o = OpSIBLING(o); 1388 return o->op_sibparent; 1389 } 1390 1391 #endif 1392 1393 1394 /* replace the sibling following start with a new UNOP, which becomes 1395 * the parent of the original sibling; e.g. 1396 * 1397 * op_sibling_newUNOP(P, A, unop-args...) 1398 * 1399 * P P 1400 * | becomes | 1401 * A-B-C A-U-C 1402 * | 1403 * B 1404 * 1405 * where U is the new UNOP. 1406 * 1407 * parent and start args are the same as for op_sibling_splice(); 1408 * type and flags args are as newUNOP(). 1409 * 1410 * Returns the new UNOP. 1411 */ 1412 1413 STATIC OP * 1414 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags) 1415 { 1416 OP *kid, *newop; 1417 1418 kid = op_sibling_splice(parent, start, 1, NULL); 1419 newop = newUNOP(type, flags, kid); 1420 op_sibling_splice(parent, start, 0, newop); 1421 return newop; 1422 } 1423 1424 1425 /* lowest-level newLOGOP-style function - just allocates and populates 1426 * the struct. Higher-level stuff should be done by S_new_logop() / 1427 * newLOGOP(). This function exists mainly to avoid op_first assignment 1428 * being spread throughout this file. 1429 */ 1430 1431 STATIC LOGOP * 1432 S_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other) 1433 { 1434 dVAR; 1435 LOGOP *logop; 1436 OP *kid = first; 1437 NewOp(1101, logop, 1, LOGOP); 1438 OpTYPE_set(logop, type); 1439 logop->op_first = first; 1440 logop->op_other = other; 1441 logop->op_flags = OPf_KIDS; 1442 while (kid && OpHAS_SIBLING(kid)) 1443 kid = OpSIBLING(kid); 1444 if (kid) 1445 OpLASTSIB_set(kid, (OP*)logop); 1446 return logop; 1447 } 1448 1449 1450 /* Contextualizers */ 1451 1452 /* 1453 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context 1454 1455 Applies a syntactic context to an op tree representing an expression. 1456 C<o> is the op tree, and C<context> must be C<G_SCALAR>, C<G_ARRAY>, 1457 or C<G_VOID> to specify the context to apply. The modified op tree 1458 is returned. 1459 1460 =cut 1461 */ 1462 1463 OP * 1464 Perl_op_contextualize(pTHX_ OP *o, I32 context) 1465 { 1466 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE; 1467 switch (context) { 1468 case G_SCALAR: return scalar(o); 1469 case G_ARRAY: return list(o); 1470 case G_VOID: return scalarvoid(o); 1471 default: 1472 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld", 1473 (long) context); 1474 } 1475 } 1476 1477 /* 1478 1479 =for apidoc Am|OP*|op_linklist|OP *o 1480 This function is the implementation of the L</LINKLIST> macro. It should 1481 not be called directly. 1482 1483 =cut 1484 */ 1485 1486 OP * 1487 Perl_op_linklist(pTHX_ OP *o) 1488 { 1489 OP *first; 1490 1491 PERL_ARGS_ASSERT_OP_LINKLIST; 1492 1493 if (o->op_next) 1494 return o->op_next; 1495 1496 /* establish postfix order */ 1497 first = cUNOPo->op_first; 1498 if (first) { 1499 OP *kid; 1500 o->op_next = LINKLIST(first); 1501 kid = first; 1502 for (;;) { 1503 OP *sibl = OpSIBLING(kid); 1504 if (sibl) { 1505 kid->op_next = LINKLIST(sibl); 1506 kid = sibl; 1507 } else { 1508 kid->op_next = o; 1509 break; 1510 } 1511 } 1512 } 1513 else 1514 o->op_next = o; 1515 1516 return o->op_next; 1517 } 1518 1519 static OP * 1520 S_scalarkids(pTHX_ OP *o) 1521 { 1522 if (o && o->op_flags & OPf_KIDS) { 1523 OP *kid; 1524 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 1525 scalar(kid); 1526 } 1527 return o; 1528 } 1529 1530 STATIC OP * 1531 S_scalarboolean(pTHX_ OP *o) 1532 { 1533 PERL_ARGS_ASSERT_SCALARBOOLEAN; 1534 1535 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST 1536 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) { 1537 if (ckWARN(WARN_SYNTAX)) { 1538 const line_t oldline = CopLINE(PL_curcop); 1539 1540 if (PL_parser && PL_parser->copline != NOLINE) { 1541 /* This ensures that warnings are reported at the first line 1542 of the conditional, not the last. */ 1543 CopLINE_set(PL_curcop, PL_parser->copline); 1544 } 1545 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be =="); 1546 CopLINE_set(PL_curcop, oldline); 1547 } 1548 } 1549 return scalar(o); 1550 } 1551 1552 static SV * 1553 S_op_varname_subscript(pTHX_ const OP *o, int subscript_type) 1554 { 1555 assert(o); 1556 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV || 1557 o->op_type == OP_PADHV || o->op_type == OP_RV2HV); 1558 { 1559 const char funny = o->op_type == OP_PADAV 1560 || o->op_type == OP_RV2AV ? '@' : '%'; 1561 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) { 1562 GV *gv; 1563 if (cUNOPo->op_first->op_type != OP_GV 1564 || !(gv = cGVOPx_gv(cUNOPo->op_first))) 1565 return NULL; 1566 return varname(gv, funny, 0, NULL, 0, subscript_type); 1567 } 1568 return 1569 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, subscript_type); 1570 } 1571 } 1572 1573 static SV * 1574 S_op_varname(pTHX_ const OP *o) 1575 { 1576 return S_op_varname_subscript(aTHX_ o, 1); 1577 } 1578 1579 static void 1580 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv) 1581 { /* or not so pretty :-) */ 1582 if (o->op_type == OP_CONST) { 1583 *retsv = cSVOPo_sv; 1584 if (SvPOK(*retsv)) { 1585 SV *sv = *retsv; 1586 *retsv = sv_newmortal(); 1587 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL, 1588 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT); 1589 } 1590 else if (!SvOK(*retsv)) 1591 *retpv = "undef"; 1592 } 1593 else *retpv = "..."; 1594 } 1595 1596 static void 1597 S_scalar_slice_warning(pTHX_ const OP *o) 1598 { 1599 OP *kid; 1600 const char lbrack = 1601 o->op_type == OP_HSLICE ? '{' : '['; 1602 const char rbrack = 1603 o->op_type == OP_HSLICE ? '}' : ']'; 1604 SV *name; 1605 SV *keysv = NULL; /* just to silence compiler warnings */ 1606 const char *key = NULL; 1607 1608 if (!(o->op_private & OPpSLICEWARNING)) 1609 return; 1610 if (PL_parser && PL_parser->error_count) 1611 /* This warning can be nonsensical when there is a syntax error. */ 1612 return; 1613 1614 kid = cLISTOPo->op_first; 1615 kid = OpSIBLING(kid); /* get past pushmark */ 1616 /* weed out false positives: any ops that can return lists */ 1617 switch (kid->op_type) { 1618 case OP_BACKTICK: 1619 case OP_GLOB: 1620 case OP_READLINE: 1621 case OP_MATCH: 1622 case OP_RV2AV: 1623 case OP_EACH: 1624 case OP_VALUES: 1625 case OP_KEYS: 1626 case OP_SPLIT: 1627 case OP_LIST: 1628 case OP_SORT: 1629 case OP_REVERSE: 1630 case OP_ENTERSUB: 1631 case OP_CALLER: 1632 case OP_LSTAT: 1633 case OP_STAT: 1634 case OP_READDIR: 1635 case OP_SYSTEM: 1636 case OP_TMS: 1637 case OP_LOCALTIME: 1638 case OP_GMTIME: 1639 case OP_ENTEREVAL: 1640 return; 1641 } 1642 1643 /* Don't warn if we have a nulled list either. */ 1644 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST) 1645 return; 1646 1647 assert(OpSIBLING(kid)); 1648 name = S_op_varname(aTHX_ OpSIBLING(kid)); 1649 if (!name) /* XS module fiddling with the op tree */ 1650 return; 1651 S_op_pretty(aTHX_ kid, &keysv, &key); 1652 assert(SvPOK(name)); 1653 sv_chop(name,SvPVX(name)+1); 1654 if (key) 1655 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */ 1656 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 1657 "Scalar value @%"SVf"%c%s%c better written as $%"SVf 1658 "%c%s%c", 1659 SVfARG(name), lbrack, key, rbrack, SVfARG(name), 1660 lbrack, key, rbrack); 1661 else 1662 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */ 1663 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 1664 "Scalar value @%"SVf"%c%"SVf"%c better written as $%" 1665 SVf"%c%"SVf"%c", 1666 SVfARG(name), lbrack, SVfARG(keysv), rbrack, 1667 SVfARG(name), lbrack, SVfARG(keysv), rbrack); 1668 } 1669 1670 OP * 1671 Perl_scalar(pTHX_ OP *o) 1672 { 1673 OP *kid; 1674 1675 /* assumes no premature commitment */ 1676 if (!o || (PL_parser && PL_parser->error_count) 1677 || (o->op_flags & OPf_WANT) 1678 || o->op_type == OP_RETURN) 1679 { 1680 return o; 1681 } 1682 1683 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR; 1684 1685 switch (o->op_type) { 1686 case OP_REPEAT: 1687 scalar(cBINOPo->op_first); 1688 if (o->op_private & OPpREPEAT_DOLIST) { 1689 kid = cLISTOPx(cUNOPo->op_first)->op_first; 1690 assert(kid->op_type == OP_PUSHMARK); 1691 if (OpHAS_SIBLING(kid) && !OpHAS_SIBLING(OpSIBLING(kid))) { 1692 op_null(cLISTOPx(cUNOPo->op_first)->op_first); 1693 o->op_private &=~ OPpREPEAT_DOLIST; 1694 } 1695 } 1696 break; 1697 case OP_OR: 1698 case OP_AND: 1699 case OP_COND_EXPR: 1700 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid)) 1701 scalar(kid); 1702 break; 1703 /* FALLTHROUGH */ 1704 case OP_SPLIT: 1705 case OP_MATCH: 1706 case OP_QR: 1707 case OP_SUBST: 1708 case OP_NULL: 1709 default: 1710 if (o->op_flags & OPf_KIDS) { 1711 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) 1712 scalar(kid); 1713 } 1714 break; 1715 case OP_LEAVE: 1716 case OP_LEAVETRY: 1717 kid = cLISTOPo->op_first; 1718 scalar(kid); 1719 kid = OpSIBLING(kid); 1720 do_kids: 1721 while (kid) { 1722 OP *sib = OpSIBLING(kid); 1723 if (sib && kid->op_type != OP_LEAVEWHEN 1724 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL 1725 || ( sib->op_targ != OP_NEXTSTATE 1726 && sib->op_targ != OP_DBSTATE ))) 1727 scalarvoid(kid); 1728 else 1729 scalar(kid); 1730 kid = sib; 1731 } 1732 PL_curcop = &PL_compiling; 1733 break; 1734 case OP_SCOPE: 1735 case OP_LINESEQ: 1736 case OP_LIST: 1737 kid = cLISTOPo->op_first; 1738 goto do_kids; 1739 case OP_SORT: 1740 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context"); 1741 break; 1742 case OP_KVHSLICE: 1743 case OP_KVASLICE: 1744 { 1745 /* Warn about scalar context */ 1746 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '['; 1747 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']'; 1748 SV *name; 1749 SV *keysv; 1750 const char *key = NULL; 1751 1752 /* This warning can be nonsensical when there is a syntax error. */ 1753 if (PL_parser && PL_parser->error_count) 1754 break; 1755 1756 if (!ckWARN(WARN_SYNTAX)) break; 1757 1758 kid = cLISTOPo->op_first; 1759 kid = OpSIBLING(kid); /* get past pushmark */ 1760 assert(OpSIBLING(kid)); 1761 name = S_op_varname(aTHX_ OpSIBLING(kid)); 1762 if (!name) /* XS module fiddling with the op tree */ 1763 break; 1764 S_op_pretty(aTHX_ kid, &keysv, &key); 1765 assert(SvPOK(name)); 1766 sv_chop(name,SvPVX(name)+1); 1767 if (key) 1768 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */ 1769 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 1770 "%%%"SVf"%c%s%c in scalar context better written " 1771 "as $%"SVf"%c%s%c", 1772 SVfARG(name), lbrack, key, rbrack, SVfARG(name), 1773 lbrack, key, rbrack); 1774 else 1775 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */ 1776 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 1777 "%%%"SVf"%c%"SVf"%c in scalar context better " 1778 "written as $%"SVf"%c%"SVf"%c", 1779 SVfARG(name), lbrack, SVfARG(keysv), rbrack, 1780 SVfARG(name), lbrack, SVfARG(keysv), rbrack); 1781 } 1782 } 1783 return o; 1784 } 1785 1786 OP * 1787 Perl_scalarvoid(pTHX_ OP *arg) 1788 { 1789 dVAR; 1790 OP *kid; 1791 SV* sv; 1792 U8 want; 1793 SSize_t defer_stack_alloc = 0; 1794 SSize_t defer_ix = -1; 1795 OP **defer_stack = NULL; 1796 OP *o = arg; 1797 1798 PERL_ARGS_ASSERT_SCALARVOID; 1799 1800 do { 1801 SV *useless_sv = NULL; 1802 const char* useless = NULL; 1803 1804 if (o->op_type == OP_NEXTSTATE 1805 || o->op_type == OP_DBSTATE 1806 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE 1807 || o->op_targ == OP_DBSTATE))) 1808 PL_curcop = (COP*)o; /* for warning below */ 1809 1810 /* assumes no premature commitment */ 1811 want = o->op_flags & OPf_WANT; 1812 if ((want && want != OPf_WANT_SCALAR) 1813 || (PL_parser && PL_parser->error_count) 1814 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN) 1815 { 1816 continue; 1817 } 1818 1819 if ((o->op_private & OPpTARGET_MY) 1820 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ 1821 { 1822 /* newASSIGNOP has already applied scalar context, which we 1823 leave, as if this op is inside SASSIGN. */ 1824 continue; 1825 } 1826 1827 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID; 1828 1829 switch (o->op_type) { 1830 default: 1831 if (!(PL_opargs[o->op_type] & OA_FOLDCONST)) 1832 break; 1833 /* FALLTHROUGH */ 1834 case OP_REPEAT: 1835 if (o->op_flags & OPf_STACKED) 1836 break; 1837 if (o->op_type == OP_REPEAT) 1838 scalar(cBINOPo->op_first); 1839 goto func_ops; 1840 case OP_SUBSTR: 1841 if (o->op_private == 4) 1842 break; 1843 /* FALLTHROUGH */ 1844 case OP_WANTARRAY: 1845 case OP_GV: 1846 case OP_SMARTMATCH: 1847 case OP_AV2ARYLEN: 1848 case OP_REF: 1849 case OP_REFGEN: 1850 case OP_SREFGEN: 1851 case OP_DEFINED: 1852 case OP_HEX: 1853 case OP_OCT: 1854 case OP_LENGTH: 1855 case OP_VEC: 1856 case OP_INDEX: 1857 case OP_RINDEX: 1858 case OP_SPRINTF: 1859 case OP_KVASLICE: 1860 case OP_KVHSLICE: 1861 case OP_UNPACK: 1862 case OP_PACK: 1863 case OP_JOIN: 1864 case OP_LSLICE: 1865 case OP_ANONLIST: 1866 case OP_ANONHASH: 1867 case OP_SORT: 1868 case OP_REVERSE: 1869 case OP_RANGE: 1870 case OP_FLIP: 1871 case OP_FLOP: 1872 case OP_CALLER: 1873 case OP_FILENO: 1874 case OP_EOF: 1875 case OP_TELL: 1876 case OP_GETSOCKNAME: 1877 case OP_GETPEERNAME: 1878 case OP_READLINK: 1879 case OP_TELLDIR: 1880 case OP_GETPPID: 1881 case OP_GETPGRP: 1882 case OP_GETPRIORITY: 1883 case OP_TIME: 1884 case OP_TMS: 1885 case OP_LOCALTIME: 1886 case OP_GMTIME: 1887 case OP_GHBYNAME: 1888 case OP_GHBYADDR: 1889 case OP_GHOSTENT: 1890 case OP_GNBYNAME: 1891 case OP_GNBYADDR: 1892 case OP_GNETENT: 1893 case OP_GPBYNAME: 1894 case OP_GPBYNUMBER: 1895 case OP_GPROTOENT: 1896 case OP_GSBYNAME: 1897 case OP_GSBYPORT: 1898 case OP_GSERVENT: 1899 case OP_GPWNAM: 1900 case OP_GPWUID: 1901 case OP_GGRNAM: 1902 case OP_GGRGID: 1903 case OP_GETLOGIN: 1904 case OP_PROTOTYPE: 1905 case OP_RUNCV: 1906 func_ops: 1907 useless = OP_DESC(o); 1908 break; 1909 1910 case OP_GVSV: 1911 case OP_PADSV: 1912 case OP_PADAV: 1913 case OP_PADHV: 1914 case OP_PADANY: 1915 case OP_AELEM: 1916 case OP_AELEMFAST: 1917 case OP_AELEMFAST_LEX: 1918 case OP_ASLICE: 1919 case OP_HELEM: 1920 case OP_HSLICE: 1921 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO))) 1922 /* Otherwise it's "Useless use of grep iterator" */ 1923 useless = OP_DESC(o); 1924 break; 1925 1926 case OP_SPLIT: 1927 kid = cLISTOPo->op_first; 1928 if (kid && kid->op_type == OP_PUSHRE 1929 && !kid->op_targ 1930 && !(o->op_flags & OPf_STACKED) 1931 #ifdef USE_ITHREADS 1932 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff 1933 #else 1934 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv 1935 #endif 1936 ) 1937 useless = OP_DESC(o); 1938 break; 1939 1940 case OP_NOT: 1941 kid = cUNOPo->op_first; 1942 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST && 1943 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) { 1944 goto func_ops; 1945 } 1946 useless = "negative pattern binding (!~)"; 1947 break; 1948 1949 case OP_SUBST: 1950 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT) 1951 useless = "non-destructive substitution (s///r)"; 1952 break; 1953 1954 case OP_TRANSR: 1955 useless = "non-destructive transliteration (tr///r)"; 1956 break; 1957 1958 case OP_RV2GV: 1959 case OP_RV2SV: 1960 case OP_RV2AV: 1961 case OP_RV2HV: 1962 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) && 1963 (!OpHAS_SIBLING(o) || OpSIBLING(o)->op_type != OP_READLINE)) 1964 useless = "a variable"; 1965 break; 1966 1967 case OP_CONST: 1968 sv = cSVOPo_sv; 1969 if (cSVOPo->op_private & OPpCONST_STRICT) 1970 no_bareword_allowed(o); 1971 else { 1972 if (ckWARN(WARN_VOID)) { 1973 NV nv; 1974 /* don't warn on optimised away booleans, eg 1975 * use constant Foo, 5; Foo || print; */ 1976 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT) 1977 useless = NULL; 1978 /* the constants 0 and 1 are permitted as they are 1979 conventionally used as dummies in constructs like 1980 1 while some_condition_with_side_effects; */ 1981 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0)) 1982 useless = NULL; 1983 else if (SvPOK(sv)) { 1984 SV * const dsv = newSVpvs(""); 1985 useless_sv 1986 = Perl_newSVpvf(aTHX_ 1987 "a constant (%s)", 1988 pv_pretty(dsv, SvPVX_const(sv), 1989 SvCUR(sv), 32, NULL, NULL, 1990 PERL_PV_PRETTY_DUMP 1991 | PERL_PV_ESCAPE_NOCLEAR 1992 | PERL_PV_ESCAPE_UNI_DETECT)); 1993 SvREFCNT_dec_NN(dsv); 1994 } 1995 else if (SvOK(sv)) { 1996 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", SVfARG(sv)); 1997 } 1998 else 1999 useless = "a constant (undef)"; 2000 } 2001 } 2002 op_null(o); /* don't execute or even remember it */ 2003 break; 2004 2005 case OP_POSTINC: 2006 OpTYPE_set(o, OP_PREINC); /* pre-increment is faster */ 2007 break; 2008 2009 case OP_POSTDEC: 2010 OpTYPE_set(o, OP_PREDEC); /* pre-decrement is faster */ 2011 break; 2012 2013 case OP_I_POSTINC: 2014 OpTYPE_set(o, OP_I_PREINC); /* pre-increment is faster */ 2015 break; 2016 2017 case OP_I_POSTDEC: 2018 OpTYPE_set(o, OP_I_PREDEC); /* pre-decrement is faster */ 2019 break; 2020 2021 case OP_SASSIGN: { 2022 OP *rv2gv; 2023 UNOP *refgen, *rv2cv; 2024 LISTOP *exlist; 2025 2026 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2) 2027 break; 2028 2029 rv2gv = ((BINOP *)o)->op_last; 2030 if (!rv2gv || rv2gv->op_type != OP_RV2GV) 2031 break; 2032 2033 refgen = (UNOP *)((BINOP *)o)->op_first; 2034 2035 if (!refgen || (refgen->op_type != OP_REFGEN 2036 && refgen->op_type != OP_SREFGEN)) 2037 break; 2038 2039 exlist = (LISTOP *)refgen->op_first; 2040 if (!exlist || exlist->op_type != OP_NULL 2041 || exlist->op_targ != OP_LIST) 2042 break; 2043 2044 if (exlist->op_first->op_type != OP_PUSHMARK 2045 && exlist->op_first != exlist->op_last) 2046 break; 2047 2048 rv2cv = (UNOP*)exlist->op_last; 2049 2050 if (rv2cv->op_type != OP_RV2CV) 2051 break; 2052 2053 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0); 2054 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0); 2055 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0); 2056 2057 o->op_private |= OPpASSIGN_CV_TO_GV; 2058 rv2gv->op_private |= OPpDONT_INIT_GV; 2059 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT; 2060 2061 break; 2062 } 2063 2064 case OP_AASSIGN: { 2065 inplace_aassign(o); 2066 break; 2067 } 2068 2069 case OP_OR: 2070 case OP_AND: 2071 kid = cLOGOPo->op_first; 2072 if (kid->op_type == OP_NOT 2073 && (kid->op_flags & OPf_KIDS)) { 2074 if (o->op_type == OP_AND) { 2075 OpTYPE_set(o, OP_OR); 2076 } else { 2077 OpTYPE_set(o, OP_AND); 2078 } 2079 op_null(kid); 2080 } 2081 /* FALLTHROUGH */ 2082 2083 case OP_DOR: 2084 case OP_COND_EXPR: 2085 case OP_ENTERGIVEN: 2086 case OP_ENTERWHEN: 2087 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid)) 2088 if (!(kid->op_flags & OPf_KIDS)) 2089 scalarvoid(kid); 2090 else 2091 DEFER_OP(kid); 2092 break; 2093 2094 case OP_NULL: 2095 if (o->op_flags & OPf_STACKED) 2096 break; 2097 /* FALLTHROUGH */ 2098 case OP_NEXTSTATE: 2099 case OP_DBSTATE: 2100 case OP_ENTERTRY: 2101 case OP_ENTER: 2102 if (!(o->op_flags & OPf_KIDS)) 2103 break; 2104 /* FALLTHROUGH */ 2105 case OP_SCOPE: 2106 case OP_LEAVE: 2107 case OP_LEAVETRY: 2108 case OP_LEAVELOOP: 2109 case OP_LINESEQ: 2110 case OP_LEAVEGIVEN: 2111 case OP_LEAVEWHEN: 2112 kids: 2113 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 2114 if (!(kid->op_flags & OPf_KIDS)) 2115 scalarvoid(kid); 2116 else 2117 DEFER_OP(kid); 2118 break; 2119 case OP_LIST: 2120 /* If the first kid after pushmark is something that the padrange 2121 optimisation would reject, then null the list and the pushmark. 2122 */ 2123 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK 2124 && ( !(kid = OpSIBLING(kid)) 2125 || ( kid->op_type != OP_PADSV 2126 && kid->op_type != OP_PADAV 2127 && kid->op_type != OP_PADHV) 2128 || kid->op_private & ~OPpLVAL_INTRO 2129 || !(kid = OpSIBLING(kid)) 2130 || ( kid->op_type != OP_PADSV 2131 && kid->op_type != OP_PADAV 2132 && kid->op_type != OP_PADHV) 2133 || kid->op_private & ~OPpLVAL_INTRO) 2134 ) { 2135 op_null(cUNOPo->op_first); /* NULL the pushmark */ 2136 op_null(o); /* NULL the list */ 2137 } 2138 goto kids; 2139 case OP_ENTEREVAL: 2140 scalarkids(o); 2141 break; 2142 case OP_SCALAR: 2143 scalar(o); 2144 break; 2145 } 2146 2147 if (useless_sv) { 2148 /* mortalise it, in case warnings are fatal. */ 2149 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), 2150 "Useless use of %"SVf" in void context", 2151 SVfARG(sv_2mortal(useless_sv))); 2152 } 2153 else if (useless) { 2154 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), 2155 "Useless use of %s in void context", 2156 useless); 2157 } 2158 } while ( (o = POP_DEFERRED_OP()) ); 2159 2160 Safefree(defer_stack); 2161 2162 return arg; 2163 } 2164 2165 static OP * 2166 S_listkids(pTHX_ OP *o) 2167 { 2168 if (o && o->op_flags & OPf_KIDS) { 2169 OP *kid; 2170 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 2171 list(kid); 2172 } 2173 return o; 2174 } 2175 2176 OP * 2177 Perl_list(pTHX_ OP *o) 2178 { 2179 OP *kid; 2180 2181 /* assumes no premature commitment */ 2182 if (!o || (o->op_flags & OPf_WANT) 2183 || (PL_parser && PL_parser->error_count) 2184 || o->op_type == OP_RETURN) 2185 { 2186 return o; 2187 } 2188 2189 if ((o->op_private & OPpTARGET_MY) 2190 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ 2191 { 2192 return o; /* As if inside SASSIGN */ 2193 } 2194 2195 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST; 2196 2197 switch (o->op_type) { 2198 case OP_FLOP: 2199 list(cBINOPo->op_first); 2200 break; 2201 case OP_REPEAT: 2202 if (o->op_private & OPpREPEAT_DOLIST 2203 && !(o->op_flags & OPf_STACKED)) 2204 { 2205 list(cBINOPo->op_first); 2206 kid = cBINOPo->op_last; 2207 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv) 2208 && SvIVX(kSVOP_sv) == 1) 2209 { 2210 op_null(o); /* repeat */ 2211 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */ 2212 /* const (rhs): */ 2213 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL)); 2214 } 2215 } 2216 break; 2217 case OP_OR: 2218 case OP_AND: 2219 case OP_COND_EXPR: 2220 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid)) 2221 list(kid); 2222 break; 2223 default: 2224 case OP_MATCH: 2225 case OP_QR: 2226 case OP_SUBST: 2227 case OP_NULL: 2228 if (!(o->op_flags & OPf_KIDS)) 2229 break; 2230 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) { 2231 list(cBINOPo->op_first); 2232 return gen_constant_list(o); 2233 } 2234 listkids(o); 2235 break; 2236 case OP_LIST: 2237 listkids(o); 2238 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) { 2239 op_null(cUNOPo->op_first); /* NULL the pushmark */ 2240 op_null(o); /* NULL the list */ 2241 } 2242 break; 2243 case OP_LEAVE: 2244 case OP_LEAVETRY: 2245 kid = cLISTOPo->op_first; 2246 list(kid); 2247 kid = OpSIBLING(kid); 2248 do_kids: 2249 while (kid) { 2250 OP *sib = OpSIBLING(kid); 2251 if (sib && kid->op_type != OP_LEAVEWHEN) 2252 scalarvoid(kid); 2253 else 2254 list(kid); 2255 kid = sib; 2256 } 2257 PL_curcop = &PL_compiling; 2258 break; 2259 case OP_SCOPE: 2260 case OP_LINESEQ: 2261 kid = cLISTOPo->op_first; 2262 goto do_kids; 2263 } 2264 return o; 2265 } 2266 2267 static OP * 2268 S_scalarseq(pTHX_ OP *o) 2269 { 2270 if (o) { 2271 const OPCODE type = o->op_type; 2272 2273 if (type == OP_LINESEQ || type == OP_SCOPE || 2274 type == OP_LEAVE || type == OP_LEAVETRY) 2275 { 2276 OP *kid, *sib; 2277 for (kid = cLISTOPo->op_first; kid; kid = sib) { 2278 if ((sib = OpSIBLING(kid)) 2279 && ( OpHAS_SIBLING(sib) || sib->op_type != OP_NULL 2280 || ( sib->op_targ != OP_NEXTSTATE 2281 && sib->op_targ != OP_DBSTATE ))) 2282 { 2283 scalarvoid(kid); 2284 } 2285 } 2286 PL_curcop = &PL_compiling; 2287 } 2288 o->op_flags &= ~OPf_PARENS; 2289 if (PL_hints & HINT_BLOCK_SCOPE) 2290 o->op_flags |= OPf_PARENS; 2291 } 2292 else 2293 o = newOP(OP_STUB, 0); 2294 return o; 2295 } 2296 2297 STATIC OP * 2298 S_modkids(pTHX_ OP *o, I32 type) 2299 { 2300 if (o && o->op_flags & OPf_KIDS) { 2301 OP *kid; 2302 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 2303 op_lvalue(kid, type); 2304 } 2305 return o; 2306 } 2307 2308 2309 /* for a helem/hslice/kvslice, if its a fixed hash, croak on invalid 2310 * const fields. Also, convert CONST keys to HEK-in-SVs. 2311 * rop is the op that retrieves the hash; 2312 * key_op is the first key 2313 */ 2314 2315 STATIC void 2316 S_check_hash_fields_and_hekify(pTHX_ UNOP *rop, SVOP *key_op) 2317 { 2318 PADNAME *lexname; 2319 GV **fields; 2320 bool check_fields; 2321 2322 /* find the padsv corresponding to $lex->{} or @{$lex}{} */ 2323 if (rop) { 2324 if (rop->op_first->op_type == OP_PADSV) 2325 /* @$hash{qw(keys here)} */ 2326 rop = (UNOP*)rop->op_first; 2327 else { 2328 /* @{$hash}{qw(keys here)} */ 2329 if (rop->op_first->op_type == OP_SCOPE 2330 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV) 2331 { 2332 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last; 2333 } 2334 else 2335 rop = NULL; 2336 } 2337 } 2338 2339 lexname = NULL; /* just to silence compiler warnings */ 2340 fields = NULL; /* just to silence compiler warnings */ 2341 2342 check_fields = 2343 rop 2344 && (lexname = padnamelist_fetch(PL_comppad_name, rop->op_targ), 2345 SvPAD_TYPED(lexname)) 2346 && (fields = (GV**)hv_fetchs(PadnameTYPE(lexname), "FIELDS", FALSE)) 2347 && isGV(*fields) && GvHV(*fields); 2348 2349 for (; key_op; key_op = (SVOP*)OpSIBLING(key_op)) { 2350 SV **svp, *sv; 2351 if (key_op->op_type != OP_CONST) 2352 continue; 2353 svp = cSVOPx_svp(key_op); 2354 2355 /* make sure it's not a bareword under strict subs */ 2356 if (key_op->op_private & OPpCONST_BARE && 2357 key_op->op_private & OPpCONST_STRICT) 2358 { 2359 no_bareword_allowed((OP*)key_op); 2360 } 2361 2362 /* Make the CONST have a shared SV */ 2363 if ( !SvIsCOW_shared_hash(sv = *svp) 2364 && SvTYPE(sv) < SVt_PVMG 2365 && SvOK(sv) 2366 && !SvROK(sv)) 2367 { 2368 SSize_t keylen; 2369 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen); 2370 SV *nsv = newSVpvn_share(key, SvUTF8(sv) ? -keylen : keylen, 0); 2371 SvREFCNT_dec_NN(sv); 2372 *svp = nsv; 2373 } 2374 2375 if ( check_fields 2376 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0)) 2377 { 2378 Perl_croak(aTHX_ "No such class field \"%"SVf"\" " 2379 "in variable %"PNf" of type %"HEKf, 2380 SVfARG(*svp), PNfARG(lexname), 2381 HEKfARG(HvNAME_HEK(PadnameTYPE(lexname)))); 2382 } 2383 } 2384 } 2385 2386 2387 /* 2388 =for apidoc finalize_optree 2389 2390 This function finalizes the optree. Should be called directly after 2391 the complete optree is built. It does some additional 2392 checking which can't be done in the normal C<ck_>xxx functions and makes 2393 the tree thread-safe. 2394 2395 =cut 2396 */ 2397 void 2398 Perl_finalize_optree(pTHX_ OP* o) 2399 { 2400 PERL_ARGS_ASSERT_FINALIZE_OPTREE; 2401 2402 ENTER; 2403 SAVEVPTR(PL_curcop); 2404 2405 finalize_op(o); 2406 2407 LEAVE; 2408 } 2409 2410 #ifdef USE_ITHREADS 2411 /* Relocate sv to the pad for thread safety. 2412 * Despite being a "constant", the SV is written to, 2413 * for reference counts, sv_upgrade() etc. */ 2414 PERL_STATIC_INLINE void 2415 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp) 2416 { 2417 PADOFFSET ix; 2418 PERL_ARGS_ASSERT_OP_RELOCATE_SV; 2419 if (!*svp) return; 2420 ix = pad_alloc(OP_CONST, SVf_READONLY); 2421 SvREFCNT_dec(PAD_SVl(ix)); 2422 PAD_SETSV(ix, *svp); 2423 /* XXX I don't know how this isn't readonly already. */ 2424 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix)); 2425 *svp = NULL; 2426 *targp = ix; 2427 } 2428 #endif 2429 2430 2431 STATIC void 2432 S_finalize_op(pTHX_ OP* o) 2433 { 2434 PERL_ARGS_ASSERT_FINALIZE_OP; 2435 2436 2437 switch (o->op_type) { 2438 case OP_NEXTSTATE: 2439 case OP_DBSTATE: 2440 PL_curcop = ((COP*)o); /* for warnings */ 2441 break; 2442 case OP_EXEC: 2443 if (OpHAS_SIBLING(o)) { 2444 OP *sib = OpSIBLING(o); 2445 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE) 2446 && ckWARN(WARN_EXEC) 2447 && OpHAS_SIBLING(sib)) 2448 { 2449 const OPCODE type = OpSIBLING(sib)->op_type; 2450 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) { 2451 const line_t oldline = CopLINE(PL_curcop); 2452 CopLINE_set(PL_curcop, CopLINE((COP*)sib)); 2453 Perl_warner(aTHX_ packWARN(WARN_EXEC), 2454 "Statement unlikely to be reached"); 2455 Perl_warner(aTHX_ packWARN(WARN_EXEC), 2456 "\t(Maybe you meant system() when you said exec()?)\n"); 2457 CopLINE_set(PL_curcop, oldline); 2458 } 2459 } 2460 } 2461 break; 2462 2463 case OP_GV: 2464 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) { 2465 GV * const gv = cGVOPo_gv; 2466 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) { 2467 /* XXX could check prototype here instead of just carping */ 2468 SV * const sv = sv_newmortal(); 2469 gv_efullname3(sv, gv, NULL); 2470 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), 2471 "%"SVf"() called too early to check prototype", 2472 SVfARG(sv)); 2473 } 2474 } 2475 break; 2476 2477 case OP_CONST: 2478 if (cSVOPo->op_private & OPpCONST_STRICT) 2479 no_bareword_allowed(o); 2480 /* FALLTHROUGH */ 2481 #ifdef USE_ITHREADS 2482 case OP_HINTSEVAL: 2483 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ); 2484 #endif 2485 break; 2486 2487 #ifdef USE_ITHREADS 2488 /* Relocate all the METHOP's SVs to the pad for thread safety. */ 2489 case OP_METHOD_NAMED: 2490 case OP_METHOD_SUPER: 2491 case OP_METHOD_REDIR: 2492 case OP_METHOD_REDIR_SUPER: 2493 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ); 2494 break; 2495 #endif 2496 2497 case OP_HELEM: { 2498 UNOP *rop; 2499 SVOP *key_op; 2500 OP *kid; 2501 2502 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST) 2503 break; 2504 2505 rop = (UNOP*)((BINOP*)o)->op_first; 2506 2507 goto check_keys; 2508 2509 case OP_HSLICE: 2510 S_scalar_slice_warning(aTHX_ o); 2511 /* FALLTHROUGH */ 2512 2513 case OP_KVHSLICE: 2514 kid = OpSIBLING(cLISTOPo->op_first); 2515 if (/* I bet there's always a pushmark... */ 2516 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST) 2517 && OP_TYPE_ISNT_NN(kid, OP_CONST)) 2518 { 2519 break; 2520 } 2521 2522 key_op = (SVOP*)(kid->op_type == OP_CONST 2523 ? kid 2524 : OpSIBLING(kLISTOP->op_first)); 2525 2526 rop = (UNOP*)((LISTOP*)o)->op_last; 2527 2528 check_keys: 2529 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV) 2530 rop = NULL; 2531 S_check_hash_fields_and_hekify(aTHX_ rop, key_op); 2532 break; 2533 } 2534 case OP_ASLICE: 2535 S_scalar_slice_warning(aTHX_ o); 2536 break; 2537 2538 case OP_SUBST: { 2539 if (cPMOPo->op_pmreplrootu.op_pmreplroot) 2540 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot); 2541 break; 2542 } 2543 default: 2544 break; 2545 } 2546 2547 if (o->op_flags & OPf_KIDS) { 2548 OP *kid; 2549 2550 #ifdef DEBUGGING 2551 /* check that op_last points to the last sibling, and that 2552 * the last op_sibling/op_sibparent field points back to the 2553 * parent, and that the only ops with KIDS are those which are 2554 * entitled to them */ 2555 U32 type = o->op_type; 2556 U32 family; 2557 bool has_last; 2558 2559 if (type == OP_NULL) { 2560 type = o->op_targ; 2561 /* ck_glob creates a null UNOP with ex-type GLOB 2562 * (which is a list op. So pretend it wasn't a listop */ 2563 if (type == OP_GLOB) 2564 type = OP_NULL; 2565 } 2566 family = PL_opargs[type] & OA_CLASS_MASK; 2567 2568 has_last = ( family == OA_BINOP 2569 || family == OA_LISTOP 2570 || family == OA_PMOP 2571 || family == OA_LOOP 2572 ); 2573 assert( has_last /* has op_first and op_last, or ... 2574 ... has (or may have) op_first: */ 2575 || family == OA_UNOP 2576 || family == OA_UNOP_AUX 2577 || family == OA_LOGOP 2578 || family == OA_BASEOP_OR_UNOP 2579 || family == OA_FILESTATOP 2580 || family == OA_LOOPEXOP 2581 || family == OA_METHOP 2582 /* I don't know why SASSIGN is tagged as OA_BASEOP - DAPM */ 2583 || type == OP_SASSIGN 2584 || type == OP_CUSTOM 2585 || type == OP_NULL /* new_logop does this */ 2586 ); 2587 2588 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) { 2589 # ifdef PERL_OP_PARENT 2590 if (!OpHAS_SIBLING(kid)) { 2591 if (has_last) 2592 assert(kid == cLISTOPo->op_last); 2593 assert(kid->op_sibparent == o); 2594 } 2595 # else 2596 if (has_last && !OpHAS_SIBLING(kid)) 2597 assert(kid == cLISTOPo->op_last); 2598 # endif 2599 } 2600 #endif 2601 2602 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) 2603 finalize_op(kid); 2604 } 2605 } 2606 2607 /* 2608 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type 2609 2610 Propagate lvalue ("modifiable") context to an op and its children. 2611 C<type> represents the context type, roughly based on the type of op that 2612 would do the modifying, although C<local()> is represented by C<OP_NULL>, 2613 because it has no op type of its own (it is signalled by a flag on 2614 the lvalue op). 2615 2616 This function detects things that can't be modified, such as C<$x+1>, and 2617 generates errors for them. For example, C<$x+1 = 2> would cause it to be 2618 called with an op of type C<OP_ADD> and a C<type> argument of C<OP_SASSIGN>. 2619 2620 It also flags things that need to behave specially in an lvalue context, 2621 such as C<$$x = 5> which might have to vivify a reference in C<$x>. 2622 2623 =cut 2624 */ 2625 2626 static void 2627 S_mark_padname_lvalue(pTHX_ PADNAME *pn) 2628 { 2629 CV *cv = PL_compcv; 2630 PadnameLVALUE_on(pn); 2631 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) { 2632 cv = CvOUTSIDE(cv); 2633 /* RT #127786: cv can be NULL due to an eval within the DB package 2634 * called from an anon sub - anon subs don't have CvOUTSIDE() set 2635 * unless they contain an eval, but calling eval within DB 2636 * pretends the eval was done in the caller's scope. 2637 */ 2638 if (!cv) 2639 break; 2640 assert(CvPADLIST(cv)); 2641 pn = 2642 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)]; 2643 assert(PadnameLEN(pn)); 2644 PadnameLVALUE_on(pn); 2645 } 2646 } 2647 2648 static bool 2649 S_vivifies(const OPCODE type) 2650 { 2651 switch(type) { 2652 case OP_RV2AV: case OP_ASLICE: 2653 case OP_RV2HV: case OP_KVASLICE: 2654 case OP_RV2SV: case OP_HSLICE: 2655 case OP_AELEMFAST: case OP_KVHSLICE: 2656 case OP_HELEM: 2657 case OP_AELEM: 2658 return 1; 2659 } 2660 return 0; 2661 } 2662 2663 static void 2664 S_lvref(pTHX_ OP *o, I32 type) 2665 { 2666 dVAR; 2667 OP *kid; 2668 switch (o->op_type) { 2669 case OP_COND_EXPR: 2670 for (kid = OpSIBLING(cUNOPo->op_first); kid; 2671 kid = OpSIBLING(kid)) 2672 S_lvref(aTHX_ kid, type); 2673 /* FALLTHROUGH */ 2674 case OP_PUSHMARK: 2675 return; 2676 case OP_RV2AV: 2677 if (cUNOPo->op_first->op_type != OP_GV) goto badref; 2678 o->op_flags |= OPf_STACKED; 2679 if (o->op_flags & OPf_PARENS) { 2680 if (o->op_private & OPpLVAL_INTRO) { 2681 yyerror(Perl_form(aTHX_ "Can't modify reference to " 2682 "localized parenthesized array in list assignment")); 2683 return; 2684 } 2685 slurpy: 2686 OpTYPE_set(o, OP_LVAVREF); 2687 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE; 2688 o->op_flags |= OPf_MOD|OPf_REF; 2689 return; 2690 } 2691 o->op_private |= OPpLVREF_AV; 2692 goto checkgv; 2693 case OP_RV2CV: 2694 kid = cUNOPo->op_first; 2695 if (kid->op_type == OP_NULL) 2696 kid = cUNOPx(OpSIBLING(kUNOP->op_first)) 2697 ->op_first; 2698 o->op_private = OPpLVREF_CV; 2699 if (kid->op_type == OP_GV) 2700 o->op_flags |= OPf_STACKED; 2701 else if (kid->op_type == OP_PADCV) { 2702 o->op_targ = kid->op_targ; 2703 kid->op_targ = 0; 2704 op_free(cUNOPo->op_first); 2705 cUNOPo->op_first = NULL; 2706 o->op_flags &=~ OPf_KIDS; 2707 } 2708 else goto badref; 2709 break; 2710 case OP_RV2HV: 2711 if (o->op_flags & OPf_PARENS) { 2712 parenhash: 2713 yyerror(Perl_form(aTHX_ "Can't modify reference to " 2714 "parenthesized hash in list assignment")); 2715 return; 2716 } 2717 o->op_private |= OPpLVREF_HV; 2718 /* FALLTHROUGH */ 2719 case OP_RV2SV: 2720 checkgv: 2721 if (cUNOPo->op_first->op_type != OP_GV) goto badref; 2722 o->op_flags |= OPf_STACKED; 2723 break; 2724 case OP_PADHV: 2725 if (o->op_flags & OPf_PARENS) goto parenhash; 2726 o->op_private |= OPpLVREF_HV; 2727 /* FALLTHROUGH */ 2728 case OP_PADSV: 2729 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX); 2730 break; 2731 case OP_PADAV: 2732 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX); 2733 if (o->op_flags & OPf_PARENS) goto slurpy; 2734 o->op_private |= OPpLVREF_AV; 2735 break; 2736 case OP_AELEM: 2737 case OP_HELEM: 2738 o->op_private |= OPpLVREF_ELEM; 2739 o->op_flags |= OPf_STACKED; 2740 break; 2741 case OP_ASLICE: 2742 case OP_HSLICE: 2743 OpTYPE_set(o, OP_LVREFSLICE); 2744 o->op_private &= OPpLVAL_INTRO|OPpLVREF_ELEM; 2745 return; 2746 case OP_NULL: 2747 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */ 2748 goto badref; 2749 else if (!(o->op_flags & OPf_KIDS)) 2750 return; 2751 if (o->op_targ != OP_LIST) { 2752 S_lvref(aTHX_ cBINOPo->op_first, type); 2753 return; 2754 } 2755 /* FALLTHROUGH */ 2756 case OP_LIST: 2757 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) { 2758 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID); 2759 S_lvref(aTHX_ kid, type); 2760 } 2761 return; 2762 case OP_STUB: 2763 if (o->op_flags & OPf_PARENS) 2764 return; 2765 /* FALLTHROUGH */ 2766 default: 2767 badref: 2768 /* diag_listed_as: Can't modify reference to %s in %s assignment */ 2769 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s", 2770 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL 2771 ? "do block" 2772 : OP_DESC(o), 2773 PL_op_desc[type])); 2774 } 2775 OpTYPE_set(o, OP_LVREF); 2776 o->op_private &= 2777 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE; 2778 if (type == OP_ENTERLOOP) 2779 o->op_private |= OPpLVREF_ITER; 2780 } 2781 2782 OP * 2783 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags) 2784 { 2785 dVAR; 2786 OP *kid; 2787 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */ 2788 int localize = -1; 2789 2790 if (!o || (PL_parser && PL_parser->error_count)) 2791 return o; 2792 2793 if ((o->op_private & OPpTARGET_MY) 2794 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ 2795 { 2796 return o; 2797 } 2798 2799 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID ); 2800 2801 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB; 2802 2803 switch (o->op_type) { 2804 case OP_UNDEF: 2805 PL_modcount++; 2806 return o; 2807 case OP_STUB: 2808 if ((o->op_flags & OPf_PARENS)) 2809 break; 2810 goto nomod; 2811 case OP_ENTERSUB: 2812 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) && 2813 !(o->op_flags & OPf_STACKED)) { 2814 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */ 2815 assert(cUNOPo->op_first->op_type == OP_NULL); 2816 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */ 2817 break; 2818 } 2819 else { /* lvalue subroutine call */ 2820 o->op_private |= OPpLVAL_INTRO; 2821 PL_modcount = RETURN_UNLIMITED_NUMBER; 2822 if (type == OP_GREPSTART || type == OP_ENTERSUB 2823 || type == OP_REFGEN || type == OP_LEAVESUBLV) { 2824 /* Potential lvalue context: */ 2825 o->op_private |= OPpENTERSUB_INARGS; 2826 break; 2827 } 2828 else { /* Compile-time error message: */ 2829 OP *kid = cUNOPo->op_first; 2830 CV *cv; 2831 GV *gv; 2832 SV *namesv; 2833 2834 if (kid->op_type != OP_PUSHMARK) { 2835 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST) 2836 Perl_croak(aTHX_ 2837 "panic: unexpected lvalue entersub " 2838 "args: type/targ %ld:%"UVuf, 2839 (long)kid->op_type, (UV)kid->op_targ); 2840 kid = kLISTOP->op_first; 2841 } 2842 while (OpHAS_SIBLING(kid)) 2843 kid = OpSIBLING(kid); 2844 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) { 2845 break; /* Postpone until runtime */ 2846 } 2847 2848 kid = kUNOP->op_first; 2849 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV) 2850 kid = kUNOP->op_first; 2851 if (kid->op_type == OP_NULL) 2852 Perl_croak(aTHX_ 2853 "Unexpected constant lvalue entersub " 2854 "entry via type/targ %ld:%"UVuf, 2855 (long)kid->op_type, (UV)kid->op_targ); 2856 if (kid->op_type != OP_GV) { 2857 break; 2858 } 2859 2860 gv = kGVOP_gv; 2861 cv = isGV(gv) 2862 ? GvCV(gv) 2863 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV 2864 ? MUTABLE_CV(SvRV(gv)) 2865 : NULL; 2866 if (!cv) 2867 break; 2868 if (CvLVALUE(cv)) 2869 break; 2870 if (flags & OP_LVALUE_NO_CROAK) 2871 return NULL; 2872 2873 namesv = cv_name(cv, NULL, 0); 2874 yyerror_pv(Perl_form(aTHX_ "Can't modify non-lvalue " 2875 "subroutine call of &%"SVf" in %s", 2876 SVfARG(namesv), PL_op_desc[type]), 2877 SvUTF8(namesv)); 2878 return o; 2879 } 2880 } 2881 /* FALLTHROUGH */ 2882 default: 2883 nomod: 2884 if (flags & OP_LVALUE_NO_CROAK) return NULL; 2885 /* grep, foreach, subcalls, refgen */ 2886 if (type == OP_GREPSTART || type == OP_ENTERSUB 2887 || type == OP_REFGEN || type == OP_LEAVESUBLV) 2888 break; 2889 yyerror(Perl_form(aTHX_ "Can't modify %s in %s", 2890 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL) 2891 ? "do block" 2892 : OP_DESC(o)), 2893 type ? PL_op_desc[type] : "local")); 2894 return o; 2895 2896 case OP_PREINC: 2897 case OP_PREDEC: 2898 case OP_POW: 2899 case OP_MULTIPLY: 2900 case OP_DIVIDE: 2901 case OP_MODULO: 2902 case OP_ADD: 2903 case OP_SUBTRACT: 2904 case OP_CONCAT: 2905 case OP_LEFT_SHIFT: 2906 case OP_RIGHT_SHIFT: 2907 case OP_BIT_AND: 2908 case OP_BIT_XOR: 2909 case OP_BIT_OR: 2910 case OP_I_MULTIPLY: 2911 case OP_I_DIVIDE: 2912 case OP_I_MODULO: 2913 case OP_I_ADD: 2914 case OP_I_SUBTRACT: 2915 if (!(o->op_flags & OPf_STACKED)) 2916 goto nomod; 2917 PL_modcount++; 2918 break; 2919 2920 case OP_REPEAT: 2921 if (o->op_flags & OPf_STACKED) { 2922 PL_modcount++; 2923 break; 2924 } 2925 if (!(o->op_private & OPpREPEAT_DOLIST)) 2926 goto nomod; 2927 else { 2928 const I32 mods = PL_modcount; 2929 modkids(cBINOPo->op_first, type); 2930 if (type != OP_AASSIGN) 2931 goto nomod; 2932 kid = cBINOPo->op_last; 2933 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) { 2934 const IV iv = SvIV(kSVOP_sv); 2935 if (PL_modcount != RETURN_UNLIMITED_NUMBER) 2936 PL_modcount = 2937 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv); 2938 } 2939 else 2940 PL_modcount = RETURN_UNLIMITED_NUMBER; 2941 } 2942 break; 2943 2944 case OP_COND_EXPR: 2945 localize = 1; 2946 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid)) 2947 op_lvalue(kid, type); 2948 break; 2949 2950 case OP_RV2AV: 2951 case OP_RV2HV: 2952 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) { 2953 PL_modcount = RETURN_UNLIMITED_NUMBER; 2954 return o; /* Treat \(@foo) like ordinary list. */ 2955 } 2956 /* FALLTHROUGH */ 2957 case OP_RV2GV: 2958 if (scalar_mod_type(o, type)) 2959 goto nomod; 2960 ref(cUNOPo->op_first, o->op_type); 2961 /* FALLTHROUGH */ 2962 case OP_ASLICE: 2963 case OP_HSLICE: 2964 localize = 1; 2965 /* FALLTHROUGH */ 2966 case OP_AASSIGN: 2967 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */ 2968 if (type == OP_LEAVESUBLV && ( 2969 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV) 2970 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR 2971 )) 2972 o->op_private |= OPpMAYBE_LVSUB; 2973 /* FALLTHROUGH */ 2974 case OP_NEXTSTATE: 2975 case OP_DBSTATE: 2976 PL_modcount = RETURN_UNLIMITED_NUMBER; 2977 break; 2978 case OP_KVHSLICE: 2979 case OP_KVASLICE: 2980 if (type == OP_LEAVESUBLV) 2981 o->op_private |= OPpMAYBE_LVSUB; 2982 goto nomod; 2983 case OP_AV2ARYLEN: 2984 PL_hints |= HINT_BLOCK_SCOPE; 2985 if (type == OP_LEAVESUBLV) 2986 o->op_private |= OPpMAYBE_LVSUB; 2987 PL_modcount++; 2988 break; 2989 case OP_RV2SV: 2990 ref(cUNOPo->op_first, o->op_type); 2991 localize = 1; 2992 /* FALLTHROUGH */ 2993 case OP_GV: 2994 PL_hints |= HINT_BLOCK_SCOPE; 2995 /* FALLTHROUGH */ 2996 case OP_SASSIGN: 2997 case OP_ANDASSIGN: 2998 case OP_ORASSIGN: 2999 case OP_DORASSIGN: 3000 PL_modcount++; 3001 break; 3002 3003 case OP_AELEMFAST: 3004 case OP_AELEMFAST_LEX: 3005 localize = -1; 3006 PL_modcount++; 3007 break; 3008 3009 case OP_PADAV: 3010 case OP_PADHV: 3011 PL_modcount = RETURN_UNLIMITED_NUMBER; 3012 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) 3013 return o; /* Treat \(@foo) like ordinary list. */ 3014 if (scalar_mod_type(o, type)) 3015 goto nomod; 3016 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR 3017 && type == OP_LEAVESUBLV) 3018 o->op_private |= OPpMAYBE_LVSUB; 3019 /* FALLTHROUGH */ 3020 case OP_PADSV: 3021 PL_modcount++; 3022 if (!type) /* local() */ 3023 Perl_croak(aTHX_ "Can't localize lexical variable %"PNf, 3024 PNfARG(PAD_COMPNAME(o->op_targ))); 3025 if (!(o->op_private & OPpLVAL_INTRO) 3026 || ( type != OP_SASSIGN && type != OP_AASSIGN 3027 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) )) 3028 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ)); 3029 break; 3030 3031 case OP_PUSHMARK: 3032 localize = 0; 3033 break; 3034 3035 case OP_KEYS: 3036 if (type != OP_SASSIGN && type != OP_LEAVESUBLV) 3037 goto nomod; 3038 goto lvalue_func; 3039 case OP_SUBSTR: 3040 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */ 3041 goto nomod; 3042 /* FALLTHROUGH */ 3043 case OP_POS: 3044 case OP_VEC: 3045 lvalue_func: 3046 if (type == OP_LEAVESUBLV) 3047 o->op_private |= OPpMAYBE_LVSUB; 3048 if (o->op_flags & OPf_KIDS) 3049 op_lvalue(OpSIBLING(cBINOPo->op_first), type); 3050 break; 3051 3052 case OP_AELEM: 3053 case OP_HELEM: 3054 ref(cBINOPo->op_first, o->op_type); 3055 if (type == OP_ENTERSUB && 3056 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF))) 3057 o->op_private |= OPpLVAL_DEFER; 3058 if (type == OP_LEAVESUBLV) 3059 o->op_private |= OPpMAYBE_LVSUB; 3060 localize = 1; 3061 PL_modcount++; 3062 break; 3063 3064 case OP_LEAVE: 3065 case OP_LEAVELOOP: 3066 o->op_private |= OPpLVALUE; 3067 /* FALLTHROUGH */ 3068 case OP_SCOPE: 3069 case OP_ENTER: 3070 case OP_LINESEQ: 3071 localize = 0; 3072 if (o->op_flags & OPf_KIDS) 3073 op_lvalue(cLISTOPo->op_last, type); 3074 break; 3075 3076 case OP_NULL: 3077 localize = 0; 3078 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */ 3079 goto nomod; 3080 else if (!(o->op_flags & OPf_KIDS)) 3081 break; 3082 if (o->op_targ != OP_LIST) { 3083 op_lvalue(cBINOPo->op_first, type); 3084 break; 3085 } 3086 /* FALLTHROUGH */ 3087 case OP_LIST: 3088 localize = 0; 3089 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 3090 /* elements might be in void context because the list is 3091 in scalar context or because they are attribute sub calls */ 3092 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID ) 3093 op_lvalue(kid, type); 3094 break; 3095 3096 case OP_COREARGS: 3097 return o; 3098 3099 case OP_AND: 3100 case OP_OR: 3101 if (type == OP_LEAVESUBLV 3102 || !S_vivifies(cLOGOPo->op_first->op_type)) 3103 op_lvalue(cLOGOPo->op_first, type); 3104 if (type == OP_LEAVESUBLV 3105 || !S_vivifies(OpSIBLING(cLOGOPo->op_first)->op_type)) 3106 op_lvalue(OpSIBLING(cLOGOPo->op_first), type); 3107 goto nomod; 3108 3109 case OP_SREFGEN: 3110 if (type != OP_AASSIGN && type != OP_SASSIGN 3111 && type != OP_ENTERLOOP) 3112 goto nomod; 3113 /* Don’t bother applying lvalue context to the ex-list. */ 3114 kid = cUNOPx(cUNOPo->op_first)->op_first; 3115 assert (!OpHAS_SIBLING(kid)); 3116 goto kid_2lvref; 3117 case OP_REFGEN: 3118 if (type != OP_AASSIGN) goto nomod; 3119 kid = cUNOPo->op_first; 3120 kid_2lvref: 3121 { 3122 const U8 ec = PL_parser ? PL_parser->error_count : 0; 3123 S_lvref(aTHX_ kid, type); 3124 if (!PL_parser || PL_parser->error_count == ec) { 3125 if (!FEATURE_REFALIASING_IS_ENABLED) 3126 Perl_croak(aTHX_ 3127 "Experimental aliasing via reference not enabled"); 3128 Perl_ck_warner_d(aTHX_ 3129 packWARN(WARN_EXPERIMENTAL__REFALIASING), 3130 "Aliasing via reference is experimental"); 3131 } 3132 } 3133 if (o->op_type == OP_REFGEN) 3134 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */ 3135 op_null(o); 3136 return o; 3137 3138 case OP_SPLIT: 3139 kid = cLISTOPo->op_first; 3140 if (kid && kid->op_type == OP_PUSHRE && 3141 ( kid->op_targ 3142 || o->op_flags & OPf_STACKED 3143 #ifdef USE_ITHREADS 3144 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff 3145 #else 3146 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv 3147 #endif 3148 )) { 3149 /* This is actually @array = split. */ 3150 PL_modcount = RETURN_UNLIMITED_NUMBER; 3151 break; 3152 } 3153 goto nomod; 3154 3155 case OP_SCALAR: 3156 op_lvalue(cUNOPo->op_first, OP_ENTERSUB); 3157 goto nomod; 3158 } 3159 3160 /* [20011101.069] File test operators interpret OPf_REF to mean that 3161 their argument is a filehandle; thus \stat(".") should not set 3162 it. AMS 20011102 */ 3163 if (type == OP_REFGEN && 3164 PL_check[o->op_type] == Perl_ck_ftst) 3165 return o; 3166 3167 if (type != OP_LEAVESUBLV) 3168 o->op_flags |= OPf_MOD; 3169 3170 if (type == OP_AASSIGN || type == OP_SASSIGN) 3171 o->op_flags |= OPf_SPECIAL|OPf_REF; 3172 else if (!type) { /* local() */ 3173 switch (localize) { 3174 case 1: 3175 o->op_private |= OPpLVAL_INTRO; 3176 o->op_flags &= ~OPf_SPECIAL; 3177 PL_hints |= HINT_BLOCK_SCOPE; 3178 break; 3179 case 0: 3180 break; 3181 case -1: 3182 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), 3183 "Useless localization of %s", OP_DESC(o)); 3184 } 3185 } 3186 else if (type != OP_GREPSTART && type != OP_ENTERSUB 3187 && type != OP_LEAVESUBLV) 3188 o->op_flags |= OPf_REF; 3189 return o; 3190 } 3191 3192 STATIC bool 3193 S_scalar_mod_type(const OP *o, I32 type) 3194 { 3195 switch (type) { 3196 case OP_POS: 3197 case OP_SASSIGN: 3198 if (o && o->op_type == OP_RV2GV) 3199 return FALSE; 3200 /* FALLTHROUGH */ 3201 case OP_PREINC: 3202 case OP_PREDEC: 3203 case OP_POSTINC: 3204 case OP_POSTDEC: 3205 case OP_I_PREINC: 3206 case OP_I_PREDEC: 3207 case OP_I_POSTINC: 3208 case OP_I_POSTDEC: 3209 case OP_POW: 3210 case OP_MULTIPLY: 3211 case OP_DIVIDE: 3212 case OP_MODULO: 3213 case OP_REPEAT: 3214 case OP_ADD: 3215 case OP_SUBTRACT: 3216 case OP_I_MULTIPLY: 3217 case OP_I_DIVIDE: 3218 case OP_I_MODULO: 3219 case OP_I_ADD: 3220 case OP_I_SUBTRACT: 3221 case OP_LEFT_SHIFT: 3222 case OP_RIGHT_SHIFT: 3223 case OP_BIT_AND: 3224 case OP_BIT_XOR: 3225 case OP_BIT_OR: 3226 case OP_CONCAT: 3227 case OP_SUBST: 3228 case OP_TRANS: 3229 case OP_TRANSR: 3230 case OP_READ: 3231 case OP_SYSREAD: 3232 case OP_RECV: 3233 case OP_ANDASSIGN: 3234 case OP_ORASSIGN: 3235 case OP_DORASSIGN: 3236 return TRUE; 3237 default: 3238 return FALSE; 3239 } 3240 } 3241 3242 STATIC bool 3243 S_is_handle_constructor(const OP *o, I32 numargs) 3244 { 3245 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR; 3246 3247 switch (o->op_type) { 3248 case OP_PIPE_OP: 3249 case OP_SOCKPAIR: 3250 if (numargs == 2) 3251 return TRUE; 3252 /* FALLTHROUGH */ 3253 case OP_SYSOPEN: 3254 case OP_OPEN: 3255 case OP_SELECT: /* XXX c.f. SelectSaver.pm */ 3256 case OP_SOCKET: 3257 case OP_OPEN_DIR: 3258 case OP_ACCEPT: 3259 if (numargs == 1) 3260 return TRUE; 3261 /* FALLTHROUGH */ 3262 default: 3263 return FALSE; 3264 } 3265 } 3266 3267 static OP * 3268 S_refkids(pTHX_ OP *o, I32 type) 3269 { 3270 if (o && o->op_flags & OPf_KIDS) { 3271 OP *kid; 3272 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 3273 ref(kid, type); 3274 } 3275 return o; 3276 } 3277 3278 OP * 3279 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref) 3280 { 3281 dVAR; 3282 OP *kid; 3283 3284 PERL_ARGS_ASSERT_DOREF; 3285 3286 if (PL_parser && PL_parser->error_count) 3287 return o; 3288 3289 switch (o->op_type) { 3290 case OP_ENTERSUB: 3291 if ((type == OP_EXISTS || type == OP_DEFINED) && 3292 !(o->op_flags & OPf_STACKED)) { 3293 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */ 3294 assert(cUNOPo->op_first->op_type == OP_NULL); 3295 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */ 3296 o->op_flags |= OPf_SPECIAL; 3297 } 3298 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){ 3299 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV 3300 : type == OP_RV2HV ? OPpDEREF_HV 3301 : OPpDEREF_SV); 3302 o->op_flags |= OPf_MOD; 3303 } 3304 3305 break; 3306 3307 case OP_COND_EXPR: 3308 for (kid = OpSIBLING(cUNOPo->op_first); kid; kid = OpSIBLING(kid)) 3309 doref(kid, type, set_op_ref); 3310 break; 3311 case OP_RV2SV: 3312 if (type == OP_DEFINED) 3313 o->op_flags |= OPf_SPECIAL; /* don't create GV */ 3314 doref(cUNOPo->op_first, o->op_type, set_op_ref); 3315 /* FALLTHROUGH */ 3316 case OP_PADSV: 3317 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { 3318 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV 3319 : type == OP_RV2HV ? OPpDEREF_HV 3320 : OPpDEREF_SV); 3321 o->op_flags |= OPf_MOD; 3322 } 3323 break; 3324 3325 case OP_RV2AV: 3326 case OP_RV2HV: 3327 if (set_op_ref) 3328 o->op_flags |= OPf_REF; 3329 /* FALLTHROUGH */ 3330 case OP_RV2GV: 3331 if (type == OP_DEFINED) 3332 o->op_flags |= OPf_SPECIAL; /* don't create GV */ 3333 doref(cUNOPo->op_first, o->op_type, set_op_ref); 3334 break; 3335 3336 case OP_PADAV: 3337 case OP_PADHV: 3338 if (set_op_ref) 3339 o->op_flags |= OPf_REF; 3340 break; 3341 3342 case OP_SCALAR: 3343 case OP_NULL: 3344 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED) 3345 break; 3346 doref(cBINOPo->op_first, type, set_op_ref); 3347 break; 3348 case OP_AELEM: 3349 case OP_HELEM: 3350 doref(cBINOPo->op_first, o->op_type, set_op_ref); 3351 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { 3352 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV 3353 : type == OP_RV2HV ? OPpDEREF_HV 3354 : OPpDEREF_SV); 3355 o->op_flags |= OPf_MOD; 3356 } 3357 break; 3358 3359 case OP_SCOPE: 3360 case OP_LEAVE: 3361 set_op_ref = FALSE; 3362 /* FALLTHROUGH */ 3363 case OP_ENTER: 3364 case OP_LIST: 3365 if (!(o->op_flags & OPf_KIDS)) 3366 break; 3367 doref(cLISTOPo->op_last, type, set_op_ref); 3368 break; 3369 default: 3370 break; 3371 } 3372 return scalar(o); 3373 3374 } 3375 3376 STATIC OP * 3377 S_dup_attrlist(pTHX_ OP *o) 3378 { 3379 OP *rop; 3380 3381 PERL_ARGS_ASSERT_DUP_ATTRLIST; 3382 3383 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids, 3384 * where the first kid is OP_PUSHMARK and the remaining ones 3385 * are OP_CONST. We need to push the OP_CONST values. 3386 */ 3387 if (o->op_type == OP_CONST) 3388 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv)); 3389 else { 3390 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS)); 3391 rop = NULL; 3392 for (o = cLISTOPo->op_first; o; o = OpSIBLING(o)) { 3393 if (o->op_type == OP_CONST) 3394 rop = op_append_elem(OP_LIST, rop, 3395 newSVOP(OP_CONST, o->op_flags, 3396 SvREFCNT_inc_NN(cSVOPo->op_sv))); 3397 } 3398 } 3399 return rop; 3400 } 3401 3402 STATIC void 3403 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs) 3404 { 3405 PERL_ARGS_ASSERT_APPLY_ATTRS; 3406 { 3407 SV * const stashsv = newSVhek(HvNAME_HEK(stash)); 3408 3409 /* fake up C<use attributes $pkg,$rv,@attrs> */ 3410 3411 #define ATTRSMODULE "attributes" 3412 #define ATTRSMODULE_PM "attributes.pm" 3413 3414 Perl_load_module( 3415 aTHX_ PERL_LOADMOD_IMPORT_OPS, 3416 newSVpvs(ATTRSMODULE), 3417 NULL, 3418 op_prepend_elem(OP_LIST, 3419 newSVOP(OP_CONST, 0, stashsv), 3420 op_prepend_elem(OP_LIST, 3421 newSVOP(OP_CONST, 0, 3422 newRV(target)), 3423 dup_attrlist(attrs)))); 3424 } 3425 } 3426 3427 STATIC void 3428 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp) 3429 { 3430 OP *pack, *imop, *arg; 3431 SV *meth, *stashsv, **svp; 3432 3433 PERL_ARGS_ASSERT_APPLY_ATTRS_MY; 3434 3435 if (!attrs) 3436 return; 3437 3438 assert(target->op_type == OP_PADSV || 3439 target->op_type == OP_PADHV || 3440 target->op_type == OP_PADAV); 3441 3442 /* Ensure that attributes.pm is loaded. */ 3443 /* Don't force the C<use> if we don't need it. */ 3444 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE); 3445 if (svp && *svp != &PL_sv_undef) 3446 NOOP; /* already in %INC */ 3447 else 3448 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, 3449 newSVpvs(ATTRSMODULE), NULL); 3450 3451 /* Need package name for method call. */ 3452 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE)); 3453 3454 /* Build up the real arg-list. */ 3455 stashsv = newSVhek(HvNAME_HEK(stash)); 3456 3457 arg = newOP(OP_PADSV, 0); 3458 arg->op_targ = target->op_targ; 3459 arg = op_prepend_elem(OP_LIST, 3460 newSVOP(OP_CONST, 0, stashsv), 3461 op_prepend_elem(OP_LIST, 3462 newUNOP(OP_REFGEN, 0, 3463 arg), 3464 dup_attrlist(attrs))); 3465 3466 /* Fake up a method call to import */ 3467 meth = newSVpvs_share("import"); 3468 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID, 3469 op_append_elem(OP_LIST, 3470 op_prepend_elem(OP_LIST, pack, arg), 3471 newMETHOP_named(OP_METHOD_NAMED, 0, meth))); 3472 3473 /* Combine the ops. */ 3474 *imopsp = op_append_elem(OP_LIST, *imopsp, imop); 3475 } 3476 3477 /* 3478 =notfor apidoc apply_attrs_string 3479 3480 Attempts to apply a list of attributes specified by the C<attrstr> and 3481 C<len> arguments to the subroutine identified by the C<cv> argument which 3482 is expected to be associated with the package identified by the C<stashpv> 3483 argument (see L<attributes>). It gets this wrong, though, in that it 3484 does not correctly identify the boundaries of the individual attribute 3485 specifications within C<attrstr>. This is not really intended for the 3486 public API, but has to be listed here for systems such as AIX which 3487 need an explicit export list for symbols. (It's called from XS code 3488 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it 3489 to respect attribute syntax properly would be welcome. 3490 3491 =cut 3492 */ 3493 3494 void 3495 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv, 3496 const char *attrstr, STRLEN len) 3497 { 3498 OP *attrs = NULL; 3499 3500 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING; 3501 3502 if (!len) { 3503 len = strlen(attrstr); 3504 } 3505 3506 while (len) { 3507 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ; 3508 if (len) { 3509 const char * const sstr = attrstr; 3510 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ; 3511 attrs = op_append_elem(OP_LIST, attrs, 3512 newSVOP(OP_CONST, 0, 3513 newSVpvn(sstr, attrstr-sstr))); 3514 } 3515 } 3516 3517 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS, 3518 newSVpvs(ATTRSMODULE), 3519 NULL, op_prepend_elem(OP_LIST, 3520 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)), 3521 op_prepend_elem(OP_LIST, 3522 newSVOP(OP_CONST, 0, 3523 newRV(MUTABLE_SV(cv))), 3524 attrs))); 3525 } 3526 3527 STATIC void 3528 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name) 3529 { 3530 OP *new_proto = NULL; 3531 STRLEN pvlen; 3532 char *pv; 3533 OP *o; 3534 3535 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR; 3536 3537 if (!*attrs) 3538 return; 3539 3540 o = *attrs; 3541 if (o->op_type == OP_CONST) { 3542 pv = SvPV(cSVOPo_sv, pvlen); 3543 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) { 3544 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv)); 3545 SV ** const tmpo = cSVOPx_svp(o); 3546 SvREFCNT_dec(cSVOPo_sv); 3547 *tmpo = tmpsv; 3548 new_proto = o; 3549 *attrs = NULL; 3550 } 3551 } else if (o->op_type == OP_LIST) { 3552 OP * lasto; 3553 assert(o->op_flags & OPf_KIDS); 3554 lasto = cLISTOPo->op_first; 3555 assert(lasto->op_type == OP_PUSHMARK); 3556 for (o = OpSIBLING(lasto); o; o = OpSIBLING(o)) { 3557 if (o->op_type == OP_CONST) { 3558 pv = SvPV(cSVOPo_sv, pvlen); 3559 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) { 3560 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv)); 3561 SV ** const tmpo = cSVOPx_svp(o); 3562 SvREFCNT_dec(cSVOPo_sv); 3563 *tmpo = tmpsv; 3564 if (new_proto && ckWARN(WARN_MISC)) { 3565 STRLEN new_len; 3566 const char * newp = SvPV(cSVOPo_sv, new_len); 3567 Perl_warner(aTHX_ packWARN(WARN_MISC), 3568 "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub", 3569 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp)); 3570 op_free(new_proto); 3571 } 3572 else if (new_proto) 3573 op_free(new_proto); 3574 new_proto = o; 3575 /* excise new_proto from the list */ 3576 op_sibling_splice(*attrs, lasto, 1, NULL); 3577 o = lasto; 3578 continue; 3579 } 3580 } 3581 lasto = o; 3582 } 3583 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs 3584 would get pulled in with no real need */ 3585 if (!OpHAS_SIBLING(cLISTOPx(*attrs)->op_first)) { 3586 op_free(*attrs); 3587 *attrs = NULL; 3588 } 3589 } 3590 3591 if (new_proto) { 3592 SV *svname; 3593 if (isGV(name)) { 3594 svname = sv_newmortal(); 3595 gv_efullname3(svname, name, NULL); 3596 } 3597 else if (SvPOK(name) && *SvPVX((SV *)name) == '&') 3598 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP); 3599 else 3600 svname = (SV *)name; 3601 if (ckWARN(WARN_ILLEGALPROTO)) 3602 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE); 3603 if (*proto && ckWARN(WARN_PROTOTYPE)) { 3604 STRLEN old_len, new_len; 3605 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len); 3606 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len); 3607 3608 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), 3609 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'" 3610 " in %"SVf, 3611 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp), 3612 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp), 3613 SVfARG(svname)); 3614 } 3615 if (*proto) 3616 op_free(*proto); 3617 *proto = new_proto; 3618 } 3619 } 3620 3621 static void 3622 S_cant_declare(pTHX_ OP *o) 3623 { 3624 if (o->op_type == OP_NULL 3625 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS) 3626 o = cUNOPo->op_first; 3627 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"", 3628 o->op_type == OP_NULL 3629 && o->op_flags & OPf_SPECIAL 3630 ? "do block" 3631 : OP_DESC(o), 3632 PL_parser->in_my == KEY_our ? "our" : 3633 PL_parser->in_my == KEY_state ? "state" : 3634 "my")); 3635 } 3636 3637 STATIC OP * 3638 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp) 3639 { 3640 I32 type; 3641 const bool stately = PL_parser && PL_parser->in_my == KEY_state; 3642 3643 PERL_ARGS_ASSERT_MY_KID; 3644 3645 if (!o || (PL_parser && PL_parser->error_count)) 3646 return o; 3647 3648 type = o->op_type; 3649 3650 if (type == OP_LIST) { 3651 OP *kid; 3652 for (kid = cLISTOPo->op_first; kid; kid = OpSIBLING(kid)) 3653 my_kid(kid, attrs, imopsp); 3654 return o; 3655 } else if (type == OP_UNDEF || type == OP_STUB) { 3656 return o; 3657 } else if (type == OP_RV2SV || /* "our" declaration */ 3658 type == OP_RV2AV || 3659 type == OP_RV2HV) { /* XXX does this let anything illegal in? */ 3660 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */ 3661 S_cant_declare(aTHX_ o); 3662 } else if (attrs) { 3663 GV * const gv = cGVOPx_gv(cUNOPo->op_first); 3664 assert(PL_parser); 3665 PL_parser->in_my = FALSE; 3666 PL_parser->in_my_stash = NULL; 3667 apply_attrs(GvSTASH(gv), 3668 (type == OP_RV2SV ? GvSV(gv) : 3669 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) : 3670 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)), 3671 attrs); 3672 } 3673 o->op_private |= OPpOUR_INTRO; 3674 return o; 3675 } 3676 else if (type != OP_PADSV && 3677 type != OP_PADAV && 3678 type != OP_PADHV && 3679 type != OP_PUSHMARK) 3680 { 3681 S_cant_declare(aTHX_ o); 3682 return o; 3683 } 3684 else if (attrs && type != OP_PUSHMARK) { 3685 HV *stash; 3686 3687 assert(PL_parser); 3688 PL_parser->in_my = FALSE; 3689 PL_parser->in_my_stash = NULL; 3690 3691 /* check for C<my Dog $spot> when deciding package */ 3692 stash = PAD_COMPNAME_TYPE(o->op_targ); 3693 if (!stash) 3694 stash = PL_curstash; 3695 apply_attrs_my(stash, o, attrs, imopsp); 3696 } 3697 o->op_flags |= OPf_MOD; 3698 o->op_private |= OPpLVAL_INTRO; 3699 if (stately) 3700 o->op_private |= OPpPAD_STATE; 3701 return o; 3702 } 3703 3704 OP * 3705 Perl_my_attrs(pTHX_ OP *o, OP *attrs) 3706 { 3707 OP *rops; 3708 int maybe_scalar = 0; 3709 3710 PERL_ARGS_ASSERT_MY_ATTRS; 3711 3712 /* [perl #17376]: this appears to be premature, and results in code such as 3713 C< our(%x); > executing in list mode rather than void mode */ 3714 #if 0 3715 if (o->op_flags & OPf_PARENS) 3716 list(o); 3717 else 3718 maybe_scalar = 1; 3719 #else 3720 maybe_scalar = 1; 3721 #endif 3722 if (attrs) 3723 SAVEFREEOP(attrs); 3724 rops = NULL; 3725 o = my_kid(o, attrs, &rops); 3726 if (rops) { 3727 if (maybe_scalar && o->op_type == OP_PADSV) { 3728 o = scalar(op_append_list(OP_LIST, rops, o)); 3729 o->op_private |= OPpLVAL_INTRO; 3730 } 3731 else { 3732 /* The listop in rops might have a pushmark at the beginning, 3733 which will mess up list assignment. */ 3734 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */ 3735 if (rops->op_type == OP_LIST && 3736 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK) 3737 { 3738 OP * const pushmark = lrops->op_first; 3739 /* excise pushmark */ 3740 op_sibling_splice(rops, NULL, 1, NULL); 3741 op_free(pushmark); 3742 } 3743 o = op_append_list(OP_LIST, o, rops); 3744 } 3745 } 3746 PL_parser->in_my = FALSE; 3747 PL_parser->in_my_stash = NULL; 3748 return o; 3749 } 3750 3751 OP * 3752 Perl_sawparens(pTHX_ OP *o) 3753 { 3754 PERL_UNUSED_CONTEXT; 3755 if (o) 3756 o->op_flags |= OPf_PARENS; 3757 return o; 3758 } 3759 3760 OP * 3761 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right) 3762 { 3763 OP *o; 3764 bool ismatchop = 0; 3765 const OPCODE ltype = left->op_type; 3766 const OPCODE rtype = right->op_type; 3767 3768 PERL_ARGS_ASSERT_BIND_MATCH; 3769 3770 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV 3771 || ltype == OP_PADHV) && ckWARN(WARN_MISC)) 3772 { 3773 const char * const desc 3774 = PL_op_desc[( 3775 rtype == OP_SUBST || rtype == OP_TRANS 3776 || rtype == OP_TRANSR 3777 ) 3778 ? (int)rtype : OP_MATCH]; 3779 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV; 3780 SV * const name = 3781 S_op_varname(aTHX_ left); 3782 if (name) 3783 Perl_warner(aTHX_ packWARN(WARN_MISC), 3784 "Applying %s to %"SVf" will act on scalar(%"SVf")", 3785 desc, SVfARG(name), SVfARG(name)); 3786 else { 3787 const char * const sample = (isary 3788 ? "@array" : "%hash"); 3789 Perl_warner(aTHX_ packWARN(WARN_MISC), 3790 "Applying %s to %s will act on scalar(%s)", 3791 desc, sample, sample); 3792 } 3793 } 3794 3795 if (rtype == OP_CONST && 3796 cSVOPx(right)->op_private & OPpCONST_BARE && 3797 cSVOPx(right)->op_private & OPpCONST_STRICT) 3798 { 3799 no_bareword_allowed(right); 3800 } 3801 3802 /* !~ doesn't make sense with /r, so error on it for now */ 3803 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) && 3804 type == OP_NOT) 3805 /* diag_listed_as: Using !~ with %s doesn't make sense */ 3806 yyerror("Using !~ with s///r doesn't make sense"); 3807 if (rtype == OP_TRANSR && type == OP_NOT) 3808 /* diag_listed_as: Using !~ with %s doesn't make sense */ 3809 yyerror("Using !~ with tr///r doesn't make sense"); 3810 3811 ismatchop = (rtype == OP_MATCH || 3812 rtype == OP_SUBST || 3813 rtype == OP_TRANS || rtype == OP_TRANSR) 3814 && !(right->op_flags & OPf_SPECIAL); 3815 if (ismatchop && right->op_private & OPpTARGET_MY) { 3816 right->op_targ = 0; 3817 right->op_private &= ~OPpTARGET_MY; 3818 } 3819 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) { 3820 if (left->op_type == OP_PADSV 3821 && !(left->op_private & OPpLVAL_INTRO)) 3822 { 3823 right->op_targ = left->op_targ; 3824 op_free(left); 3825 o = right; 3826 } 3827 else { 3828 right->op_flags |= OPf_STACKED; 3829 if (rtype != OP_MATCH && rtype != OP_TRANSR && 3830 ! (rtype == OP_TRANS && 3831 right->op_private & OPpTRANS_IDENTICAL) && 3832 ! (rtype == OP_SUBST && 3833 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT))) 3834 left = op_lvalue(left, rtype); 3835 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR) 3836 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right); 3837 else 3838 o = op_prepend_elem(rtype, scalar(left), right); 3839 } 3840 if (type == OP_NOT) 3841 return newUNOP(OP_NOT, 0, scalar(o)); 3842 return o; 3843 } 3844 else 3845 return bind_match(type, left, 3846 pmruntime(newPMOP(OP_MATCH, 0), right, NULL, 0, 0)); 3847 } 3848 3849 OP * 3850 Perl_invert(pTHX_ OP *o) 3851 { 3852 if (!o) 3853 return NULL; 3854 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o)); 3855 } 3856 3857 /* 3858 =for apidoc Amx|OP *|op_scope|OP *o 3859 3860 Wraps up an op tree with some additional ops so that at runtime a dynamic 3861 scope will be created. The original ops run in the new dynamic scope, 3862 and then, provided that they exit normally, the scope will be unwound. 3863 The additional ops used to create and unwind the dynamic scope will 3864 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used 3865 instead if the ops are simple enough to not need the full dynamic scope 3866 structure. 3867 3868 =cut 3869 */ 3870 3871 OP * 3872 Perl_op_scope(pTHX_ OP *o) 3873 { 3874 dVAR; 3875 if (o) { 3876 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) { 3877 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o); 3878 OpTYPE_set(o, OP_LEAVE); 3879 } 3880 else if (o->op_type == OP_LINESEQ) { 3881 OP *kid; 3882 OpTYPE_set(o, OP_SCOPE); 3883 kid = ((LISTOP*)o)->op_first; 3884 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { 3885 op_null(kid); 3886 3887 /* The following deals with things like 'do {1 for 1}' */ 3888 kid = OpSIBLING(kid); 3889 if (kid && 3890 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)) 3891 op_null(kid); 3892 } 3893 } 3894 else 3895 o = newLISTOP(OP_SCOPE, 0, o, NULL); 3896 } 3897 return o; 3898 } 3899 3900 OP * 3901 Perl_op_unscope(pTHX_ OP *o) 3902 { 3903 if (o && o->op_type == OP_LINESEQ) { 3904 OP *kid = cLISTOPo->op_first; 3905 for(; kid; kid = OpSIBLING(kid)) 3906 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) 3907 op_null(kid); 3908 } 3909 return o; 3910 } 3911 3912 /* 3913 =for apidoc Am|int|block_start|int full 3914 3915 Handles compile-time scope entry. 3916 Arranges for hints to be restored on block 3917 exit and also handles pad sequence numbers to make lexical variables scope 3918 right. Returns a savestack index for use with C<block_end>. 3919 3920 =cut 3921 */ 3922 3923 int 3924 Perl_block_start(pTHX_ int full) 3925 { 3926 const int retval = PL_savestack_ix; 3927 3928 PL_compiling.cop_seq = PL_cop_seqmax; 3929 COP_SEQMAX_INC; 3930 pad_block_start(full); 3931 SAVEHINTS(); 3932 PL_hints &= ~HINT_BLOCK_SCOPE; 3933 SAVECOMPILEWARNINGS(); 3934 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings); 3935 SAVEI32(PL_compiling.cop_seq); 3936 PL_compiling.cop_seq = 0; 3937 3938 CALL_BLOCK_HOOKS(bhk_start, full); 3939 3940 return retval; 3941 } 3942 3943 /* 3944 =for apidoc Am|OP *|block_end|I32 floor|OP *seq 3945 3946 Handles compile-time scope exit. C<floor> 3947 is the savestack index returned by 3948 C<block_start>, and C<seq> is the body of the block. Returns the block, 3949 possibly modified. 3950 3951 =cut 3952 */ 3953 3954 OP* 3955 Perl_block_end(pTHX_ I32 floor, OP *seq) 3956 { 3957 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE; 3958 OP* retval = scalarseq(seq); 3959 OP *o; 3960 3961 /* XXX Is the null PL_parser check necessary here? */ 3962 assert(PL_parser); /* Let’s find out under debugging builds. */ 3963 if (PL_parser && PL_parser->parsed_sub) { 3964 o = newSTATEOP(0, NULL, NULL); 3965 op_null(o); 3966 retval = op_append_elem(OP_LINESEQ, retval, o); 3967 } 3968 3969 CALL_BLOCK_HOOKS(bhk_pre_end, &retval); 3970 3971 LEAVE_SCOPE(floor); 3972 if (needblockscope) 3973 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */ 3974 o = pad_leavemy(); 3975 3976 if (o) { 3977 /* pad_leavemy has created a sequence of introcv ops for all my 3978 subs declared in the block. We have to replicate that list with 3979 clonecv ops, to deal with this situation: 3980 3981 sub { 3982 my sub s1; 3983 my sub s2; 3984 sub s1 { state sub foo { \&s2 } } 3985 }->() 3986 3987 Originally, I was going to have introcv clone the CV and turn 3988 off the stale flag. Since &s1 is declared before &s2, the 3989 introcv op for &s1 is executed (on sub entry) before the one for 3990 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is 3991 cloned, since it is a state sub) closes over &s2 and expects 3992 to see it in its outer CV’s pad. If the introcv op clones &s1, 3993 then &s2 is still marked stale. Since &s1 is not active, and 3994 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia- 3995 ble will not stay shared’ warning. Because it is the same stub 3996 that will be used when the introcv op for &s2 is executed, clos- 3997 ing over it is safe. Hence, we have to turn off the stale flag 3998 on all lexical subs in the block before we clone any of them. 3999 Hence, having introcv clone the sub cannot work. So we create a 4000 list of ops like this: 4001 4002 lineseq 4003 | 4004 +-- introcv 4005 | 4006 +-- introcv 4007 | 4008 +-- introcv 4009 | 4010 . 4011 . 4012 . 4013 | 4014 +-- clonecv 4015 | 4016 +-- clonecv 4017 | 4018 +-- clonecv 4019 | 4020 . 4021 . 4022 . 4023 */ 4024 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o; 4025 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o; 4026 for (;; kid = OpSIBLING(kid)) { 4027 OP *newkid = newOP(OP_CLONECV, 0); 4028 newkid->op_targ = kid->op_targ; 4029 o = op_append_elem(OP_LINESEQ, o, newkid); 4030 if (kid == last) break; 4031 } 4032 retval = op_prepend_elem(OP_LINESEQ, o, retval); 4033 } 4034 4035 CALL_BLOCK_HOOKS(bhk_post_end, &retval); 4036 4037 return retval; 4038 } 4039 4040 /* 4041 =head1 Compile-time scope hooks 4042 4043 =for apidoc Aox||blockhook_register 4044 4045 Register a set of hooks to be called when the Perl lexical scope changes 4046 at compile time. See L<perlguts/"Compile-time scope hooks">. 4047 4048 =cut 4049 */ 4050 4051 void 4052 Perl_blockhook_register(pTHX_ BHK *hk) 4053 { 4054 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER; 4055 4056 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk))); 4057 } 4058 4059 void 4060 Perl_newPROG(pTHX_ OP *o) 4061 { 4062 PERL_ARGS_ASSERT_NEWPROG; 4063 4064 if (PL_in_eval) { 4065 PERL_CONTEXT *cx; 4066 I32 i; 4067 if (PL_eval_root) 4068 return; 4069 PL_eval_root = newUNOP(OP_LEAVEEVAL, 4070 ((PL_in_eval & EVAL_KEEPERR) 4071 ? OPf_SPECIAL : 0), o); 4072 4073 cx = CX_CUR(); 4074 assert(CxTYPE(cx) == CXt_EVAL); 4075 4076 if ((cx->blk_gimme & G_WANT) == G_VOID) 4077 scalarvoid(PL_eval_root); 4078 else if ((cx->blk_gimme & G_WANT) == G_ARRAY) 4079 list(PL_eval_root); 4080 else 4081 scalar(PL_eval_root); 4082 4083 PL_eval_start = op_linklist(PL_eval_root); 4084 PL_eval_root->op_private |= OPpREFCOUNTED; 4085 OpREFCNT_set(PL_eval_root, 1); 4086 PL_eval_root->op_next = 0; 4087 i = PL_savestack_ix; 4088 SAVEFREEOP(o); 4089 ENTER; 4090 CALL_PEEP(PL_eval_start); 4091 finalize_optree(PL_eval_root); 4092 S_prune_chain_head(&PL_eval_start); 4093 LEAVE; 4094 PL_savestack_ix = i; 4095 } 4096 else { 4097 if (o->op_type == OP_STUB) { 4098 /* This block is entered if nothing is compiled for the main 4099 program. This will be the case for an genuinely empty main 4100 program, or one which only has BEGIN blocks etc, so already 4101 run and freed. 4102 4103 Historically (5.000) the guard above was !o. However, commit 4104 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as 4105 c71fccf11fde0068, changed perly.y so that newPROG() is now 4106 called with the output of block_end(), which returns a new 4107 OP_STUB for the case of an empty optree. ByteLoader (and 4108 maybe other things) also take this path, because they set up 4109 PL_main_start and PL_main_root directly, without generating an 4110 optree. 4111 4112 If the parsing the main program aborts (due to parse errors, 4113 or due to BEGIN or similar calling exit), then newPROG() 4114 isn't even called, and hence this code path and its cleanups 4115 are skipped. This shouldn't make a make a difference: 4116 * a non-zero return from perl_parse is a failure, and 4117 perl_destruct() should be called immediately. 4118 * however, if exit(0) is called during the parse, then 4119 perl_parse() returns 0, and perl_run() is called. As 4120 PL_main_start will be NULL, perl_run() will return 4121 promptly, and the exit code will remain 0. 4122 */ 4123 4124 PL_comppad_name = 0; 4125 PL_compcv = 0; 4126 S_op_destroy(aTHX_ o); 4127 return; 4128 } 4129 PL_main_root = op_scope(sawparens(scalarvoid(o))); 4130 PL_curcop = &PL_compiling; 4131 PL_main_start = LINKLIST(PL_main_root); 4132 PL_main_root->op_private |= OPpREFCOUNTED; 4133 OpREFCNT_set(PL_main_root, 1); 4134 PL_main_root->op_next = 0; 4135 CALL_PEEP(PL_main_start); 4136 finalize_optree(PL_main_root); 4137 S_prune_chain_head(&PL_main_start); 4138 cv_forget_slab(PL_compcv); 4139 PL_compcv = 0; 4140 4141 /* Register with debugger */ 4142 if (PERLDB_INTER) { 4143 CV * const cv = get_cvs("DB::postponed", 0); 4144 if (cv) { 4145 dSP; 4146 PUSHMARK(SP); 4147 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling))); 4148 PUTBACK; 4149 call_sv(MUTABLE_SV(cv), G_DISCARD); 4150 } 4151 } 4152 } 4153 } 4154 4155 OP * 4156 Perl_localize(pTHX_ OP *o, I32 lex) 4157 { 4158 PERL_ARGS_ASSERT_LOCALIZE; 4159 4160 if (o->op_flags & OPf_PARENS) 4161 /* [perl #17376]: this appears to be premature, and results in code such as 4162 C< our(%x); > executing in list mode rather than void mode */ 4163 #if 0 4164 list(o); 4165 #else 4166 NOOP; 4167 #endif 4168 else { 4169 if ( PL_parser->bufptr > PL_parser->oldbufptr 4170 && PL_parser->bufptr[-1] == ',' 4171 && ckWARN(WARN_PARENTHESIS)) 4172 { 4173 char *s = PL_parser->bufptr; 4174 bool sigil = FALSE; 4175 4176 /* some heuristics to detect a potential error */ 4177 while (*s && (strchr(", \t\n", *s))) 4178 s++; 4179 4180 while (1) { 4181 if (*s && (strchr("@$%", *s) || (!lex && *s == '*')) 4182 && *++s 4183 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) { 4184 s++; 4185 sigil = TRUE; 4186 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) 4187 s++; 4188 while (*s && (strchr(", \t\n", *s))) 4189 s++; 4190 } 4191 else 4192 break; 4193 } 4194 if (sigil && (*s == ';' || *s == '=')) { 4195 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS), 4196 "Parentheses missing around \"%s\" list", 4197 lex 4198 ? (PL_parser->in_my == KEY_our 4199 ? "our" 4200 : PL_parser->in_my == KEY_state 4201 ? "state" 4202 : "my") 4203 : "local"); 4204 } 4205 } 4206 } 4207 if (lex) 4208 o = my(o); 4209 else 4210 o = op_lvalue(o, OP_NULL); /* a bit kludgey */ 4211 PL_parser->in_my = FALSE; 4212 PL_parser->in_my_stash = NULL; 4213 return o; 4214 } 4215 4216 OP * 4217 Perl_jmaybe(pTHX_ OP *o) 4218 { 4219 PERL_ARGS_ASSERT_JMAYBE; 4220 4221 if (o->op_type == OP_LIST) { 4222 OP * const o2 4223 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV))); 4224 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o)); 4225 } 4226 return o; 4227 } 4228 4229 PERL_STATIC_INLINE OP * 4230 S_op_std_init(pTHX_ OP *o) 4231 { 4232 I32 type = o->op_type; 4233 4234 PERL_ARGS_ASSERT_OP_STD_INIT; 4235 4236 if (PL_opargs[type] & OA_RETSCALAR) 4237 scalar(o); 4238 if (PL_opargs[type] & OA_TARGET && !o->op_targ) 4239 o->op_targ = pad_alloc(type, SVs_PADTMP); 4240 4241 return o; 4242 } 4243 4244 PERL_STATIC_INLINE OP * 4245 S_op_integerize(pTHX_ OP *o) 4246 { 4247 I32 type = o->op_type; 4248 4249 PERL_ARGS_ASSERT_OP_INTEGERIZE; 4250 4251 /* integerize op. */ 4252 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)) 4253 { 4254 dVAR; 4255 o->op_ppaddr = PL_ppaddr[++(o->op_type)]; 4256 } 4257 4258 if (type == OP_NEGATE) 4259 /* XXX might want a ck_negate() for this */ 4260 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT; 4261 4262 return o; 4263 } 4264 4265 static OP * 4266 S_fold_constants(pTHX_ OP *o) 4267 { 4268 dVAR; 4269 OP * VOL curop; 4270 OP *newop; 4271 VOL I32 type = o->op_type; 4272 bool is_stringify; 4273 SV * VOL sv = NULL; 4274 int ret = 0; 4275 OP *old_next; 4276 SV * const oldwarnhook = PL_warnhook; 4277 SV * const olddiehook = PL_diehook; 4278 COP not_compiling; 4279 U8 oldwarn = PL_dowarn; 4280 I32 old_cxix; 4281 dJMPENV; 4282 4283 PERL_ARGS_ASSERT_FOLD_CONSTANTS; 4284 4285 if (!(PL_opargs[type] & OA_FOLDCONST)) 4286 goto nope; 4287 4288 switch (type) { 4289 case OP_UCFIRST: 4290 case OP_LCFIRST: 4291 case OP_UC: 4292 case OP_LC: 4293 case OP_FC: 4294 #ifdef USE_LOCALE_CTYPE 4295 if (IN_LC_COMPILETIME(LC_CTYPE)) 4296 goto nope; 4297 #endif 4298 break; 4299 case OP_SLT: 4300 case OP_SGT: 4301 case OP_SLE: 4302 case OP_SGE: 4303 case OP_SCMP: 4304 #ifdef USE_LOCALE_COLLATE 4305 if (IN_LC_COMPILETIME(LC_COLLATE)) 4306 goto nope; 4307 #endif 4308 break; 4309 case OP_SPRINTF: 4310 /* XXX what about the numeric ops? */ 4311 #ifdef USE_LOCALE_NUMERIC 4312 if (IN_LC_COMPILETIME(LC_NUMERIC)) 4313 goto nope; 4314 #endif 4315 break; 4316 case OP_PACK: 4317 if (!OpHAS_SIBLING(cLISTOPo->op_first) 4318 || OpSIBLING(cLISTOPo->op_first)->op_type != OP_CONST) 4319 goto nope; 4320 { 4321 SV * const sv = cSVOPx_sv(OpSIBLING(cLISTOPo->op_first)); 4322 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope; 4323 { 4324 const char *s = SvPVX_const(sv); 4325 while (s < SvEND(sv)) { 4326 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope; 4327 s++; 4328 } 4329 } 4330 } 4331 break; 4332 case OP_REPEAT: 4333 if (o->op_private & OPpREPEAT_DOLIST) goto nope; 4334 break; 4335 case OP_SREFGEN: 4336 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST 4337 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first))) 4338 goto nope; 4339 } 4340 4341 if (PL_parser && PL_parser->error_count) 4342 goto nope; /* Don't try to run w/ errors */ 4343 4344 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { 4345 const OPCODE type = curop->op_type; 4346 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) && 4347 type != OP_LIST && 4348 type != OP_SCALAR && 4349 type != OP_NULL && 4350 type != OP_PUSHMARK) 4351 { 4352 goto nope; 4353 } 4354 } 4355 4356 curop = LINKLIST(o); 4357 old_next = o->op_next; 4358 o->op_next = 0; 4359 PL_op = curop; 4360 4361 old_cxix = cxstack_ix; 4362 create_eval_scope(NULL, G_FAKINGEVAL); 4363 4364 /* Verify that we don't need to save it: */ 4365 assert(PL_curcop == &PL_compiling); 4366 StructCopy(&PL_compiling, ¬_compiling, COP); 4367 PL_curcop = ¬_compiling; 4368 /* The above ensures that we run with all the correct hints of the 4369 currently compiling COP, but that IN_PERL_RUNTIME is true. */ 4370 assert(IN_PERL_RUNTIME); 4371 PL_warnhook = PERL_WARNHOOK_FATAL; 4372 PL_diehook = NULL; 4373 JMPENV_PUSH(ret); 4374 4375 /* Effective $^W=1. */ 4376 if ( ! (PL_dowarn & G_WARN_ALL_MASK)) 4377 PL_dowarn |= G_WARN_ON; 4378 4379 switch (ret) { 4380 case 0: 4381 CALLRUNOPS(aTHX); 4382 sv = *(PL_stack_sp--); 4383 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */ 4384 pad_swipe(o->op_targ, FALSE); 4385 } 4386 else if (SvTEMP(sv)) { /* grab mortal temp? */ 4387 SvREFCNT_inc_simple_void(sv); 4388 SvTEMP_off(sv); 4389 } 4390 else { assert(SvIMMORTAL(sv)); } 4391 break; 4392 case 3: 4393 /* Something tried to die. Abandon constant folding. */ 4394 /* Pretend the error never happened. */ 4395 CLEAR_ERRSV(); 4396 o->op_next = old_next; 4397 break; 4398 default: 4399 JMPENV_POP; 4400 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */ 4401 PL_warnhook = oldwarnhook; 4402 PL_diehook = olddiehook; 4403 /* XXX note that this croak may fail as we've already blown away 4404 * the stack - eg any nested evals */ 4405 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret); 4406 } 4407 JMPENV_POP; 4408 PL_dowarn = oldwarn; 4409 PL_warnhook = oldwarnhook; 4410 PL_diehook = olddiehook; 4411 PL_curcop = &PL_compiling; 4412 4413 /* if we croaked, depending on how we croaked the eval scope 4414 * may or may not have already been popped */ 4415 if (cxstack_ix > old_cxix) { 4416 assert(cxstack_ix == old_cxix + 1); 4417 assert(CxTYPE(CX_CUR()) == CXt_EVAL); 4418 delete_eval_scope(); 4419 } 4420 if (ret) 4421 goto nope; 4422 4423 /* OP_STRINGIFY and constant folding are used to implement qq. 4424 Here the constant folding is an implementation detail that we 4425 want to hide. If the stringify op is itself already marked 4426 folded, however, then it is actually a folded join. */ 4427 is_stringify = type == OP_STRINGIFY && !o->op_folded; 4428 op_free(o); 4429 assert(sv); 4430 if (is_stringify) 4431 SvPADTMP_off(sv); 4432 else if (!SvIMMORTAL(sv)) { 4433 SvPADTMP_on(sv); 4434 SvREADONLY_on(sv); 4435 } 4436 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv)); 4437 if (!is_stringify) newop->op_folded = 1; 4438 return newop; 4439 4440 nope: 4441 return o; 4442 } 4443 4444 static OP * 4445 S_gen_constant_list(pTHX_ OP *o) 4446 { 4447 dVAR; 4448 OP *curop; 4449 const SSize_t oldtmps_floor = PL_tmps_floor; 4450 SV **svp; 4451 AV *av; 4452 4453 list(o); 4454 if (PL_parser && PL_parser->error_count) 4455 return o; /* Don't attempt to run with errors */ 4456 4457 curop = LINKLIST(o); 4458 o->op_next = 0; 4459 CALL_PEEP(curop); 4460 S_prune_chain_head(&curop); 4461 PL_op = curop; 4462 Perl_pp_pushmark(aTHX); 4463 CALLRUNOPS(aTHX); 4464 PL_op = curop; 4465 assert (!(curop->op_flags & OPf_SPECIAL)); 4466 assert(curop->op_type == OP_RANGE); 4467 Perl_pp_anonlist(aTHX); 4468 PL_tmps_floor = oldtmps_floor; 4469 4470 OpTYPE_set(o, OP_RV2AV); 4471 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */ 4472 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */ 4473 o->op_opt = 0; /* needs to be revisited in rpeep() */ 4474 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--); 4475 4476 /* replace subtree with an OP_CONST */ 4477 curop = ((UNOP*)o)->op_first; 4478 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av)); 4479 op_free(curop); 4480 4481 if (AvFILLp(av) != -1) 4482 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp) 4483 { 4484 SvPADTMP_on(*svp); 4485 SvREADONLY_on(*svp); 4486 } 4487 LINKLIST(o); 4488 return list(o); 4489 } 4490 4491 /* 4492 =head1 Optree Manipulation Functions 4493 */ 4494 4495 /* List constructors */ 4496 4497 /* 4498 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last 4499 4500 Append an item to the list of ops contained directly within a list-type 4501 op, returning the lengthened list. C<first> is the list-type op, 4502 and C<last> is the op to append to the list. C<optype> specifies the 4503 intended opcode for the list. If C<first> is not already a list of the 4504 right type, it will be upgraded into one. If either C<first> or C<last> 4505 is null, the other is returned unchanged. 4506 4507 =cut 4508 */ 4509 4510 OP * 4511 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last) 4512 { 4513 if (!first) 4514 return last; 4515 4516 if (!last) 4517 return first; 4518 4519 if (first->op_type != (unsigned)type 4520 || (type == OP_LIST && (first->op_flags & OPf_PARENS))) 4521 { 4522 return newLISTOP(type, 0, first, last); 4523 } 4524 4525 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last); 4526 first->op_flags |= OPf_KIDS; 4527 return first; 4528 } 4529 4530 /* 4531 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last 4532 4533 Concatenate the lists of ops contained directly within two list-type ops, 4534 returning the combined list. C<first> and C<last> are the list-type ops 4535 to concatenate. C<optype> specifies the intended opcode for the list. 4536 If either C<first> or C<last> is not already a list of the right type, 4537 it will be upgraded into one. If either C<first> or C<last> is null, 4538 the other is returned unchanged. 4539 4540 =cut 4541 */ 4542 4543 OP * 4544 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last) 4545 { 4546 if (!first) 4547 return last; 4548 4549 if (!last) 4550 return first; 4551 4552 if (first->op_type != (unsigned)type) 4553 return op_prepend_elem(type, first, last); 4554 4555 if (last->op_type != (unsigned)type) 4556 return op_append_elem(type, first, last); 4557 4558 OpMORESIB_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first); 4559 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last; 4560 OpLASTSIB_set(((LISTOP*)first)->op_last, first); 4561 first->op_flags |= (last->op_flags & OPf_KIDS); 4562 4563 S_op_destroy(aTHX_ last); 4564 4565 return first; 4566 } 4567 4568 /* 4569 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last 4570 4571 Prepend an item to the list of ops contained directly within a list-type 4572 op, returning the lengthened list. C<first> is the op to prepend to the 4573 list, and C<last> is the list-type op. C<optype> specifies the intended 4574 opcode for the list. If C<last> is not already a list of the right type, 4575 it will be upgraded into one. If either C<first> or C<last> is null, 4576 the other is returned unchanged. 4577 4578 =cut 4579 */ 4580 4581 OP * 4582 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last) 4583 { 4584 if (!first) 4585 return last; 4586 4587 if (!last) 4588 return first; 4589 4590 if (last->op_type == (unsigned)type) { 4591 if (type == OP_LIST) { /* already a PUSHMARK there */ 4592 /* insert 'first' after pushmark */ 4593 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first); 4594 if (!(first->op_flags & OPf_PARENS)) 4595 last->op_flags &= ~OPf_PARENS; 4596 } 4597 else 4598 op_sibling_splice(last, NULL, 0, first); 4599 last->op_flags |= OPf_KIDS; 4600 return last; 4601 } 4602 4603 return newLISTOP(type, 0, first, last); 4604 } 4605 4606 /* 4607 =for apidoc Am|OP *|op_convert_list|I32 type|I32 flags|OP *o 4608 4609 Converts C<o> into a list op if it is not one already, and then converts it 4610 into the specified C<type>, calling its check function, allocating a target if 4611 it needs one, and folding constants. 4612 4613 A list-type op is usually constructed one kid at a time via C<newLISTOP>, 4614 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to 4615 C<op_convert_list> to make it the right type. 4616 4617 =cut 4618 */ 4619 4620 OP * 4621 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o) 4622 { 4623 dVAR; 4624 if (type < 0) type = -type, flags |= OPf_SPECIAL; 4625 if (!o || o->op_type != OP_LIST) 4626 o = force_list(o, 0); 4627 else 4628 { 4629 o->op_flags &= ~OPf_WANT; 4630 o->op_private &= ~OPpLVAL_INTRO; 4631 } 4632 4633 if (!(PL_opargs[type] & OA_MARK)) 4634 op_null(cLISTOPo->op_first); 4635 else { 4636 OP * const kid2 = OpSIBLING(cLISTOPo->op_first); 4637 if (kid2 && kid2->op_type == OP_COREARGS) { 4638 op_null(cLISTOPo->op_first); 4639 kid2->op_private |= OPpCOREARGS_PUSHMARK; 4640 } 4641 } 4642 4643 OpTYPE_set(o, type); 4644 o->op_flags |= flags; 4645 if (flags & OPf_FOLDED) 4646 o->op_folded = 1; 4647 4648 o = CHECKOP(type, o); 4649 if (o->op_type != (unsigned)type) 4650 return o; 4651 4652 return fold_constants(op_integerize(op_std_init(o))); 4653 } 4654 4655 /* Constructors */ 4656 4657 4658 /* 4659 =head1 Optree construction 4660 4661 =for apidoc Am|OP *|newNULLLIST 4662 4663 Constructs, checks, and returns a new C<stub> op, which represents an 4664 empty list expression. 4665 4666 =cut 4667 */ 4668 4669 OP * 4670 Perl_newNULLLIST(pTHX) 4671 { 4672 return newOP(OP_STUB, 0); 4673 } 4674 4675 /* promote o and any siblings to be a list if its not already; i.e. 4676 * 4677 * o - A - B 4678 * 4679 * becomes 4680 * 4681 * list 4682 * | 4683 * pushmark - o - A - B 4684 * 4685 * If nullit it true, the list op is nulled. 4686 */ 4687 4688 static OP * 4689 S_force_list(pTHX_ OP *o, bool nullit) 4690 { 4691 if (!o || o->op_type != OP_LIST) { 4692 OP *rest = NULL; 4693 if (o) { 4694 /* manually detach any siblings then add them back later */ 4695 rest = OpSIBLING(o); 4696 OpLASTSIB_set(o, NULL); 4697 } 4698 o = newLISTOP(OP_LIST, 0, o, NULL); 4699 if (rest) 4700 op_sibling_splice(o, cLISTOPo->op_last, 0, rest); 4701 } 4702 if (nullit) 4703 op_null(o); 4704 return o; 4705 } 4706 4707 /* 4708 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last 4709 4710 Constructs, checks, and returns an op of any list type. C<type> is 4711 the opcode. C<flags> gives the eight bits of C<op_flags>, except that 4712 C<OPf_KIDS> will be set automatically if required. C<first> and C<last> 4713 supply up to two ops to be direct children of the list op; they are 4714 consumed by this function and become part of the constructed op tree. 4715 4716 For most list operators, the check function expects all the kid ops to be 4717 present already, so calling C<newLISTOP(OP_JOIN, ...)> (e.g.) is not 4718 appropriate. What you want to do in that case is create an op of type 4719 C<OP_LIST>, append more children to it, and then call L</op_convert_list>. 4720 See L</op_convert_list> for more information. 4721 4722 4723 =cut 4724 */ 4725 4726 OP * 4727 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last) 4728 { 4729 dVAR; 4730 LISTOP *listop; 4731 4732 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP 4733 || type == OP_CUSTOM); 4734 4735 NewOp(1101, listop, 1, LISTOP); 4736 4737 OpTYPE_set(listop, type); 4738 if (first || last) 4739 flags |= OPf_KIDS; 4740 listop->op_flags = (U8)flags; 4741 4742 if (!last && first) 4743 last = first; 4744 else if (!first && last) 4745 first = last; 4746 else if (first) 4747 OpMORESIB_set(first, last); 4748 listop->op_first = first; 4749 listop->op_last = last; 4750 if (type == OP_LIST) { 4751 OP* const pushop = newOP(OP_PUSHMARK, 0); 4752 OpMORESIB_set(pushop, first); 4753 listop->op_first = pushop; 4754 listop->op_flags |= OPf_KIDS; 4755 if (!last) 4756 listop->op_last = pushop; 4757 } 4758 if (listop->op_last) 4759 OpLASTSIB_set(listop->op_last, (OP*)listop); 4760 4761 return CHECKOP(type, listop); 4762 } 4763 4764 /* 4765 =for apidoc Am|OP *|newOP|I32 type|I32 flags 4766 4767 Constructs, checks, and returns an op of any base type (any type that 4768 has no extra fields). C<type> is the opcode. C<flags> gives the 4769 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits 4770 of C<op_private>. 4771 4772 =cut 4773 */ 4774 4775 OP * 4776 Perl_newOP(pTHX_ I32 type, I32 flags) 4777 { 4778 dVAR; 4779 OP *o; 4780 4781 if (type == -OP_ENTEREVAL) { 4782 type = OP_ENTEREVAL; 4783 flags |= OPpEVAL_BYTES<<8; 4784 } 4785 4786 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP 4787 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP 4788 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP 4789 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP); 4790 4791 NewOp(1101, o, 1, OP); 4792 OpTYPE_set(o, type); 4793 o->op_flags = (U8)flags; 4794 4795 o->op_next = o; 4796 o->op_private = (U8)(0 | (flags >> 8)); 4797 if (PL_opargs[type] & OA_RETSCALAR) 4798 scalar(o); 4799 if (PL_opargs[type] & OA_TARGET) 4800 o->op_targ = pad_alloc(type, SVs_PADTMP); 4801 return CHECKOP(type, o); 4802 } 4803 4804 /* 4805 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first 4806 4807 Constructs, checks, and returns an op of any unary type. C<type> is 4808 the opcode. C<flags> gives the eight bits of C<op_flags>, except that 4809 C<OPf_KIDS> will be set automatically if required, and, shifted up eight 4810 bits, the eight bits of C<op_private>, except that the bit with value 1 4811 is automatically set. C<first> supplies an optional op to be the direct 4812 child of the unary op; it is consumed by this function and become part 4813 of the constructed op tree. 4814 4815 =cut 4816 */ 4817 4818 OP * 4819 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first) 4820 { 4821 dVAR; 4822 UNOP *unop; 4823 4824 if (type == -OP_ENTEREVAL) { 4825 type = OP_ENTEREVAL; 4826 flags |= OPpEVAL_BYTES<<8; 4827 } 4828 4829 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP 4830 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP 4831 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP 4832 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP 4833 || type == OP_SASSIGN 4834 || type == OP_ENTERTRY 4835 || type == OP_CUSTOM 4836 || type == OP_NULL ); 4837 4838 if (!first) 4839 first = newOP(OP_STUB, 0); 4840 if (PL_opargs[type] & OA_MARK) 4841 first = force_list(first, 1); 4842 4843 NewOp(1101, unop, 1, UNOP); 4844 OpTYPE_set(unop, type); 4845 unop->op_first = first; 4846 unop->op_flags = (U8)(flags | OPf_KIDS); 4847 unop->op_private = (U8)(1 | (flags >> 8)); 4848 4849 if (!OpHAS_SIBLING(first)) /* true unless weird syntax error */ 4850 OpLASTSIB_set(first, (OP*)unop); 4851 4852 unop = (UNOP*) CHECKOP(type, unop); 4853 if (unop->op_next) 4854 return (OP*)unop; 4855 4856 return fold_constants(op_integerize(op_std_init((OP *) unop))); 4857 } 4858 4859 /* 4860 =for apidoc newUNOP_AUX 4861 4862 Similar to C<newUNOP>, but creates an C<UNOP_AUX> struct instead, with C<op_aux> 4863 initialised to C<aux> 4864 4865 =cut 4866 */ 4867 4868 OP * 4869 Perl_newUNOP_AUX(pTHX_ I32 type, I32 flags, OP *first, UNOP_AUX_item *aux) 4870 { 4871 dVAR; 4872 UNOP_AUX *unop; 4873 4874 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP_AUX 4875 || type == OP_CUSTOM); 4876 4877 NewOp(1101, unop, 1, UNOP_AUX); 4878 unop->op_type = (OPCODE)type; 4879 unop->op_ppaddr = PL_ppaddr[type]; 4880 unop->op_first = first; 4881 unop->op_flags = (U8)(flags | (first ? OPf_KIDS : 0)); 4882 unop->op_private = (U8)((first ? 1 : 0) | (flags >> 8)); 4883 unop->op_aux = aux; 4884 4885 if (first && !OpHAS_SIBLING(first)) /* true unless weird syntax error */ 4886 OpLASTSIB_set(first, (OP*)unop); 4887 4888 unop = (UNOP_AUX*) CHECKOP(type, unop); 4889 4890 return op_std_init((OP *) unop); 4891 } 4892 4893 /* 4894 =for apidoc Am|OP *|newMETHOP|I32 type|I32 flags|OP *first 4895 4896 Constructs, checks, and returns an op of method type with a method name 4897 evaluated at runtime. C<type> is the opcode. C<flags> gives the eight 4898 bits of C<op_flags>, except that C<OPf_KIDS> will be set automatically, 4899 and, shifted up eight bits, the eight bits of C<op_private>, except that 4900 the bit with value 1 is automatically set. C<dynamic_meth> supplies an 4901 op which evaluates method name; it is consumed by this function and 4902 become part of the constructed op tree. 4903 Supported optypes: C<OP_METHOD>. 4904 4905 =cut 4906 */ 4907 4908 static OP* 4909 S_newMETHOP_internal(pTHX_ I32 type, I32 flags, OP* dynamic_meth, SV* const_meth) { 4910 dVAR; 4911 METHOP *methop; 4912 4913 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_METHOP 4914 || type == OP_CUSTOM); 4915 4916 NewOp(1101, methop, 1, METHOP); 4917 if (dynamic_meth) { 4918 if (PL_opargs[type] & OA_MARK) dynamic_meth = force_list(dynamic_meth, 1); 4919 methop->op_flags = (U8)(flags | OPf_KIDS); 4920 methop->op_u.op_first = dynamic_meth; 4921 methop->op_private = (U8)(1 | (flags >> 8)); 4922 4923 if (!OpHAS_SIBLING(dynamic_meth)) 4924 OpLASTSIB_set(dynamic_meth, (OP*)methop); 4925 } 4926 else { 4927 assert(const_meth); 4928 methop->op_flags = (U8)(flags & ~OPf_KIDS); 4929 methop->op_u.op_meth_sv = const_meth; 4930 methop->op_private = (U8)(0 | (flags >> 8)); 4931 methop->op_next = (OP*)methop; 4932 } 4933 4934 #ifdef USE_ITHREADS 4935 methop->op_rclass_targ = 0; 4936 #else 4937 methop->op_rclass_sv = NULL; 4938 #endif 4939 4940 OpTYPE_set(methop, type); 4941 return CHECKOP(type, methop); 4942 } 4943 4944 OP * 4945 Perl_newMETHOP (pTHX_ I32 type, I32 flags, OP* dynamic_meth) { 4946 PERL_ARGS_ASSERT_NEWMETHOP; 4947 return newMETHOP_internal(type, flags, dynamic_meth, NULL); 4948 } 4949 4950 /* 4951 =for apidoc Am|OP *|newMETHOP_named|I32 type|I32 flags|SV *const_meth 4952 4953 Constructs, checks, and returns an op of method type with a constant 4954 method name. C<type> is the opcode. C<flags> gives the eight bits of 4955 C<op_flags>, and, shifted up eight bits, the eight bits of 4956 C<op_private>. C<const_meth> supplies a constant method name; 4957 it must be a shared COW string. 4958 Supported optypes: C<OP_METHOD_NAMED>. 4959 4960 =cut 4961 */ 4962 4963 OP * 4964 Perl_newMETHOP_named (pTHX_ I32 type, I32 flags, SV* const_meth) { 4965 PERL_ARGS_ASSERT_NEWMETHOP_NAMED; 4966 return newMETHOP_internal(type, flags, NULL, const_meth); 4967 } 4968 4969 /* 4970 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last 4971 4972 Constructs, checks, and returns an op of any binary type. C<type> 4973 is the opcode. C<flags> gives the eight bits of C<op_flags>, except 4974 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits, 4975 the eight bits of C<op_private>, except that the bit with value 1 or 4976 2 is automatically set as required. C<first> and C<last> supply up to 4977 two ops to be the direct children of the binary op; they are consumed 4978 by this function and become part of the constructed op tree. 4979 4980 =cut 4981 */ 4982 4983 OP * 4984 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last) 4985 { 4986 dVAR; 4987 BINOP *binop; 4988 4989 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP 4990 || type == OP_SASSIGN || type == OP_NULL || type == OP_CUSTOM); 4991 4992 NewOp(1101, binop, 1, BINOP); 4993 4994 if (!first) 4995 first = newOP(OP_NULL, 0); 4996 4997 OpTYPE_set(binop, type); 4998 binop->op_first = first; 4999 binop->op_flags = (U8)(flags | OPf_KIDS); 5000 if (!last) { 5001 last = first; 5002 binop->op_private = (U8)(1 | (flags >> 8)); 5003 } 5004 else { 5005 binop->op_private = (U8)(2 | (flags >> 8)); 5006 OpMORESIB_set(first, last); 5007 } 5008 5009 if (!OpHAS_SIBLING(last)) /* true unless weird syntax error */ 5010 OpLASTSIB_set(last, (OP*)binop); 5011 5012 binop->op_last = OpSIBLING(binop->op_first); 5013 if (binop->op_last) 5014 OpLASTSIB_set(binop->op_last, (OP*)binop); 5015 5016 binop = (BINOP*)CHECKOP(type, binop); 5017 if (binop->op_next || binop->op_type != (OPCODE)type) 5018 return (OP*)binop; 5019 5020 return fold_constants(op_integerize(op_std_init((OP *)binop))); 5021 } 5022 5023 static int uvcompare(const void *a, const void *b) 5024 __attribute__nonnull__(1) 5025 __attribute__nonnull__(2) 5026 __attribute__pure__; 5027 static int uvcompare(const void *a, const void *b) 5028 { 5029 if (*((const UV *)a) < (*(const UV *)b)) 5030 return -1; 5031 if (*((const UV *)a) > (*(const UV *)b)) 5032 return 1; 5033 if (*((const UV *)a+1) < (*(const UV *)b+1)) 5034 return -1; 5035 if (*((const UV *)a+1) > (*(const UV *)b+1)) 5036 return 1; 5037 return 0; 5038 } 5039 5040 static OP * 5041 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) 5042 { 5043 SV * const tstr = ((SVOP*)expr)->op_sv; 5044 SV * const rstr = 5045 ((SVOP*)repl)->op_sv; 5046 STRLEN tlen; 5047 STRLEN rlen; 5048 const U8 *t = (U8*)SvPV_const(tstr, tlen); 5049 const U8 *r = (U8*)SvPV_const(rstr, rlen); 5050 I32 i; 5051 I32 j; 5052 I32 grows = 0; 5053 short *tbl; 5054 5055 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT; 5056 const I32 squash = o->op_private & OPpTRANS_SQUASH; 5057 I32 del = o->op_private & OPpTRANS_DELETE; 5058 SV* swash; 5059 5060 PERL_ARGS_ASSERT_PMTRANS; 5061 5062 PL_hints |= HINT_BLOCK_SCOPE; 5063 5064 if (SvUTF8(tstr)) 5065 o->op_private |= OPpTRANS_FROM_UTF; 5066 5067 if (SvUTF8(rstr)) 5068 o->op_private |= OPpTRANS_TO_UTF; 5069 5070 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) { 5071 SV* const listsv = newSVpvs("# comment\n"); 5072 SV* transv = NULL; 5073 const U8* tend = t + tlen; 5074 const U8* rend = r + rlen; 5075 STRLEN ulen; 5076 UV tfirst = 1; 5077 UV tlast = 0; 5078 IV tdiff; 5079 STRLEN tcount = 0; 5080 UV rfirst = 1; 5081 UV rlast = 0; 5082 IV rdiff; 5083 STRLEN rcount = 0; 5084 IV diff; 5085 I32 none = 0; 5086 U32 max = 0; 5087 I32 bits; 5088 I32 havefinal = 0; 5089 U32 final = 0; 5090 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF; 5091 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF; 5092 U8* tsave = NULL; 5093 U8* rsave = NULL; 5094 const U32 flags = UTF8_ALLOW_DEFAULT; 5095 5096 if (!from_utf) { 5097 STRLEN len = tlen; 5098 t = tsave = bytes_to_utf8(t, &len); 5099 tend = t + len; 5100 } 5101 if (!to_utf && rlen) { 5102 STRLEN len = rlen; 5103 r = rsave = bytes_to_utf8(r, &len); 5104 rend = r + len; 5105 } 5106 5107 /* There is a snag with this code on EBCDIC: scan_const() in toke.c has 5108 * encoded chars in native encoding which makes ranges in the EBCDIC 0..255 5109 * odd. */ 5110 5111 if (complement) { 5112 U8 tmpbuf[UTF8_MAXBYTES+1]; 5113 UV *cp; 5114 UV nextmin = 0; 5115 Newx(cp, 2*tlen, UV); 5116 i = 0; 5117 transv = newSVpvs(""); 5118 while (t < tend) { 5119 cp[2*i] = utf8n_to_uvchr(t, tend-t, &ulen, flags); 5120 t += ulen; 5121 if (t < tend && *t == ILLEGAL_UTF8_BYTE) { 5122 t++; 5123 cp[2*i+1] = utf8n_to_uvchr(t, tend-t, &ulen, flags); 5124 t += ulen; 5125 } 5126 else { 5127 cp[2*i+1] = cp[2*i]; 5128 } 5129 i++; 5130 } 5131 qsort(cp, i, 2*sizeof(UV), uvcompare); 5132 for (j = 0; j < i; j++) { 5133 UV val = cp[2*j]; 5134 diff = val - nextmin; 5135 if (diff > 0) { 5136 t = uvchr_to_utf8(tmpbuf,nextmin); 5137 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); 5138 if (diff > 1) { 5139 U8 range_mark = ILLEGAL_UTF8_BYTE; 5140 t = uvchr_to_utf8(tmpbuf, val - 1); 5141 sv_catpvn(transv, (char *)&range_mark, 1); 5142 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); 5143 } 5144 } 5145 val = cp[2*j+1]; 5146 if (val >= nextmin) 5147 nextmin = val + 1; 5148 } 5149 t = uvchr_to_utf8(tmpbuf,nextmin); 5150 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); 5151 { 5152 U8 range_mark = ILLEGAL_UTF8_BYTE; 5153 sv_catpvn(transv, (char *)&range_mark, 1); 5154 } 5155 t = uvchr_to_utf8(tmpbuf, 0x7fffffff); 5156 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); 5157 t = (const U8*)SvPVX_const(transv); 5158 tlen = SvCUR(transv); 5159 tend = t + tlen; 5160 Safefree(cp); 5161 } 5162 else if (!rlen && !del) { 5163 r = t; rlen = tlen; rend = tend; 5164 } 5165 if (!squash) { 5166 if ((!rlen && !del) || t == r || 5167 (tlen == rlen && memEQ((char *)t, (char *)r, tlen))) 5168 { 5169 o->op_private |= OPpTRANS_IDENTICAL; 5170 } 5171 } 5172 5173 while (t < tend || tfirst <= tlast) { 5174 /* see if we need more "t" chars */ 5175 if (tfirst > tlast) { 5176 tfirst = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags); 5177 t += ulen; 5178 if (t < tend && *t == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */ 5179 t++; 5180 tlast = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags); 5181 t += ulen; 5182 } 5183 else 5184 tlast = tfirst; 5185 } 5186 5187 /* now see if we need more "r" chars */ 5188 if (rfirst > rlast) { 5189 if (r < rend) { 5190 rfirst = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags); 5191 r += ulen; 5192 if (r < rend && *r == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */ 5193 r++; 5194 rlast = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags); 5195 r += ulen; 5196 } 5197 else 5198 rlast = rfirst; 5199 } 5200 else { 5201 if (!havefinal++) 5202 final = rlast; 5203 rfirst = rlast = 0xffffffff; 5204 } 5205 } 5206 5207 /* now see which range will peter out first, if either. */ 5208 tdiff = tlast - tfirst; 5209 rdiff = rlast - rfirst; 5210 tcount += tdiff + 1; 5211 rcount += rdiff + 1; 5212 5213 if (tdiff <= rdiff) 5214 diff = tdiff; 5215 else 5216 diff = rdiff; 5217 5218 if (rfirst == 0xffffffff) { 5219 diff = tdiff; /* oops, pretend rdiff is infinite */ 5220 if (diff > 0) 5221 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n", 5222 (long)tfirst, (long)tlast); 5223 else 5224 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst); 5225 } 5226 else { 5227 if (diff > 0) 5228 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n", 5229 (long)tfirst, (long)(tfirst + diff), 5230 (long)rfirst); 5231 else 5232 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n", 5233 (long)tfirst, (long)rfirst); 5234 5235 if (rfirst + diff > max) 5236 max = rfirst + diff; 5237 if (!grows) 5238 grows = (tfirst < rfirst && 5239 UVCHR_SKIP(tfirst) < UVCHR_SKIP(rfirst + diff)); 5240 rfirst += diff + 1; 5241 } 5242 tfirst += diff + 1; 5243 } 5244 5245 none = ++max; 5246 if (del) 5247 del = ++max; 5248 5249 if (max > 0xffff) 5250 bits = 32; 5251 else if (max > 0xff) 5252 bits = 16; 5253 else 5254 bits = 8; 5255 5256 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none)); 5257 #ifdef USE_ITHREADS 5258 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY); 5259 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix)); 5260 PAD_SETSV(cPADOPo->op_padix, swash); 5261 SvPADTMP_on(swash); 5262 SvREADONLY_on(swash); 5263 #else 5264 cSVOPo->op_sv = swash; 5265 #endif 5266 SvREFCNT_dec(listsv); 5267 SvREFCNT_dec(transv); 5268 5269 if (!del && havefinal && rlen) 5270 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5, 5271 newSVuv((UV)final), 0); 5272 5273 Safefree(tsave); 5274 Safefree(rsave); 5275 5276 tlen = tcount; 5277 rlen = rcount; 5278 if (r < rend) 5279 rlen++; 5280 else if (rlast == 0xffffffff) 5281 rlen = 0; 5282 5283 goto warnins; 5284 } 5285 5286 tbl = (short*)PerlMemShared_calloc( 5287 (o->op_private & OPpTRANS_COMPLEMENT) && 5288 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256, 5289 sizeof(short)); 5290 cPVOPo->op_pv = (char*)tbl; 5291 if (complement) { 5292 for (i = 0; i < (I32)tlen; i++) 5293 tbl[t[i]] = -1; 5294 for (i = 0, j = 0; i < 256; i++) { 5295 if (!tbl[i]) { 5296 if (j >= (I32)rlen) { 5297 if (del) 5298 tbl[i] = -2; 5299 else if (rlen) 5300 tbl[i] = r[j-1]; 5301 else 5302 tbl[i] = (short)i; 5303 } 5304 else { 5305 if (i < 128 && r[j] >= 128) 5306 grows = 1; 5307 tbl[i] = r[j++]; 5308 } 5309 } 5310 } 5311 if (!del) { 5312 if (!rlen) { 5313 j = rlen; 5314 if (!squash) 5315 o->op_private |= OPpTRANS_IDENTICAL; 5316 } 5317 else if (j >= (I32)rlen) 5318 j = rlen - 1; 5319 else { 5320 tbl = 5321 (short *) 5322 PerlMemShared_realloc(tbl, 5323 (0x101+rlen-j) * sizeof(short)); 5324 cPVOPo->op_pv = (char*)tbl; 5325 } 5326 tbl[0x100] = (short)(rlen - j); 5327 for (i=0; i < (I32)rlen - j; i++) 5328 tbl[0x101+i] = r[j+i]; 5329 } 5330 } 5331 else { 5332 if (!rlen && !del) { 5333 r = t; rlen = tlen; 5334 if (!squash) 5335 o->op_private |= OPpTRANS_IDENTICAL; 5336 } 5337 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) { 5338 o->op_private |= OPpTRANS_IDENTICAL; 5339 } 5340 for (i = 0; i < 256; i++) 5341 tbl[i] = -1; 5342 for (i = 0, j = 0; i < (I32)tlen; i++,j++) { 5343 if (j >= (I32)rlen) { 5344 if (del) { 5345 if (tbl[t[i]] == -1) 5346 tbl[t[i]] = -2; 5347 continue; 5348 } 5349 --j; 5350 } 5351 if (tbl[t[i]] == -1) { 5352 if (t[i] < 128 && r[j] >= 128) 5353 grows = 1; 5354 tbl[t[i]] = r[j]; 5355 } 5356 } 5357 } 5358 5359 warnins: 5360 if(del && rlen == tlen) { 5361 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator"); 5362 } else if(rlen > tlen && !complement) { 5363 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list"); 5364 } 5365 5366 if (grows) 5367 o->op_private |= OPpTRANS_GROWS; 5368 op_free(expr); 5369 op_free(repl); 5370 5371 return o; 5372 } 5373 5374 /* 5375 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags 5376 5377 Constructs, checks, and returns an op of any pattern matching type. 5378 C<type> is the opcode. C<flags> gives the eight bits of C<op_flags> 5379 and, shifted up eight bits, the eight bits of C<op_private>. 5380 5381 =cut 5382 */ 5383 5384 OP * 5385 Perl_newPMOP(pTHX_ I32 type, I32 flags) 5386 { 5387 dVAR; 5388 PMOP *pmop; 5389 5390 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP 5391 || type == OP_CUSTOM); 5392 5393 NewOp(1101, pmop, 1, PMOP); 5394 OpTYPE_set(pmop, type); 5395 pmop->op_flags = (U8)flags; 5396 pmop->op_private = (U8)(0 | (flags >> 8)); 5397 if (PL_opargs[type] & OA_RETSCALAR) 5398 scalar((OP *)pmop); 5399 5400 if (PL_hints & HINT_RE_TAINT) 5401 pmop->op_pmflags |= PMf_RETAINT; 5402 #ifdef USE_LOCALE_CTYPE 5403 if (IN_LC_COMPILETIME(LC_CTYPE)) { 5404 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET); 5405 } 5406 else 5407 #endif 5408 if (IN_UNI_8_BIT) { 5409 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET); 5410 } 5411 if (PL_hints & HINT_RE_FLAGS) { 5412 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_ 5413 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0 5414 ); 5415 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags); 5416 reflags = Perl_refcounted_he_fetch_pvn(aTHX_ 5417 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0 5418 ); 5419 if (reflags && SvOK(reflags)) { 5420 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags)); 5421 } 5422 } 5423 5424 5425 #ifdef USE_ITHREADS 5426 assert(SvPOK(PL_regex_pad[0])); 5427 if (SvCUR(PL_regex_pad[0])) { 5428 /* Pop off the "packed" IV from the end. */ 5429 SV *const repointer_list = PL_regex_pad[0]; 5430 const char *p = SvEND(repointer_list) - sizeof(IV); 5431 const IV offset = *((IV*)p); 5432 5433 assert(SvCUR(repointer_list) % sizeof(IV) == 0); 5434 5435 SvEND_set(repointer_list, p); 5436 5437 pmop->op_pmoffset = offset; 5438 /* This slot should be free, so assert this: */ 5439 assert(PL_regex_pad[offset] == &PL_sv_undef); 5440 } else { 5441 SV * const repointer = &PL_sv_undef; 5442 av_push(PL_regex_padav, repointer); 5443 pmop->op_pmoffset = av_tindex(PL_regex_padav); 5444 PL_regex_pad = AvARRAY(PL_regex_padav); 5445 } 5446 #endif 5447 5448 return CHECKOP(type, pmop); 5449 } 5450 5451 static void 5452 S_set_haseval(pTHX) 5453 { 5454 PADOFFSET i = 1; 5455 PL_cv_has_eval = 1; 5456 /* Any pad names in scope are potentially lvalues. */ 5457 for (; i < PadnamelistMAXNAMED(PL_comppad_name); i++) { 5458 PADNAME *pn = PAD_COMPNAME_SV(i); 5459 if (!pn || !PadnameLEN(pn)) 5460 continue; 5461 if (PadnameOUTER(pn) || PadnameIN_SCOPE(pn, PL_cop_seqmax)) 5462 S_mark_padname_lvalue(aTHX_ pn); 5463 } 5464 } 5465 5466 /* Given some sort of match op o, and an expression expr containing a 5467 * pattern, either compile expr into a regex and attach it to o (if it's 5468 * constant), or convert expr into a runtime regcomp op sequence (if it's 5469 * not) 5470 * 5471 * isreg indicates that the pattern is part of a regex construct, eg 5472 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or 5473 * split "pattern", which aren't. In the former case, expr will be a list 5474 * if the pattern contains more than one term (eg /a$b/). 5475 * 5476 * When the pattern has been compiled within a new anon CV (for 5477 * qr/(?{...})/ ), then floor indicates the savestack level just before 5478 * the new sub was created 5479 */ 5480 5481 OP * 5482 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl, bool isreg, I32 floor) 5483 { 5484 PMOP *pm; 5485 LOGOP *rcop; 5486 I32 repl_has_vars = 0; 5487 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR); 5488 bool is_compiletime; 5489 bool has_code; 5490 5491 PERL_ARGS_ASSERT_PMRUNTIME; 5492 5493 if (is_trans) { 5494 return pmtrans(o, expr, repl); 5495 } 5496 5497 /* find whether we have any runtime or code elements; 5498 * at the same time, temporarily set the op_next of each DO block; 5499 * then when we LINKLIST, this will cause the DO blocks to be excluded 5500 * from the op_next chain (and from having LINKLIST recursively 5501 * applied to them). We fix up the DOs specially later */ 5502 5503 is_compiletime = 1; 5504 has_code = 0; 5505 if (expr->op_type == OP_LIST) { 5506 OP *o; 5507 for (o = cLISTOPx(expr)->op_first; o; o = OpSIBLING(o)) { 5508 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) { 5509 has_code = 1; 5510 assert(!o->op_next); 5511 if (UNLIKELY(!OpHAS_SIBLING(o))) { 5512 assert(PL_parser && PL_parser->error_count); 5513 /* This can happen with qr/ (?{(^{})/. Just fake up 5514 the op we were expecting to see, to avoid crashing 5515 elsewhere. */ 5516 op_sibling_splice(expr, o, 0, 5517 newSVOP(OP_CONST, 0, &PL_sv_no)); 5518 } 5519 o->op_next = OpSIBLING(o); 5520 } 5521 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK) 5522 is_compiletime = 0; 5523 } 5524 } 5525 else if (expr->op_type != OP_CONST) 5526 is_compiletime = 0; 5527 5528 LINKLIST(expr); 5529 5530 /* fix up DO blocks; treat each one as a separate little sub; 5531 * also, mark any arrays as LIST/REF */ 5532 5533 if (expr->op_type == OP_LIST) { 5534 OP *o; 5535 for (o = cLISTOPx(expr)->op_first; o; o = OpSIBLING(o)) { 5536 5537 if (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) { 5538 assert( !(o->op_flags & OPf_WANT)); 5539 /* push the array rather than its contents. The regex 5540 * engine will retrieve and join the elements later */ 5541 o->op_flags |= (OPf_WANT_LIST | OPf_REF); 5542 continue; 5543 } 5544 5545 if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL))) 5546 continue; 5547 o->op_next = NULL; /* undo temporary hack from above */ 5548 scalar(o); 5549 LINKLIST(o); 5550 if (cLISTOPo->op_first->op_type == OP_LEAVE) { 5551 LISTOP *leaveop = cLISTOPx(cLISTOPo->op_first); 5552 /* skip ENTER */ 5553 assert(leaveop->op_first->op_type == OP_ENTER); 5554 assert(OpHAS_SIBLING(leaveop->op_first)); 5555 o->op_next = OpSIBLING(leaveop->op_first); 5556 /* skip leave */ 5557 assert(leaveop->op_flags & OPf_KIDS); 5558 assert(leaveop->op_last->op_next == (OP*)leaveop); 5559 leaveop->op_next = NULL; /* stop on last op */ 5560 op_null((OP*)leaveop); 5561 } 5562 else { 5563 /* skip SCOPE */ 5564 OP *scope = cLISTOPo->op_first; 5565 assert(scope->op_type == OP_SCOPE); 5566 assert(scope->op_flags & OPf_KIDS); 5567 scope->op_next = NULL; /* stop on last op */ 5568 op_null(scope); 5569 } 5570 /* have to peep the DOs individually as we've removed it from 5571 * the op_next chain */ 5572 CALL_PEEP(o); 5573 S_prune_chain_head(&(o->op_next)); 5574 if (is_compiletime) 5575 /* runtime finalizes as part of finalizing whole tree */ 5576 finalize_optree(o); 5577 } 5578 } 5579 else if (expr->op_type == OP_PADAV || expr->op_type == OP_RV2AV) { 5580 assert( !(expr->op_flags & OPf_WANT)); 5581 /* push the array rather than its contents. The regex 5582 * engine will retrieve and join the elements later */ 5583 expr->op_flags |= (OPf_WANT_LIST | OPf_REF); 5584 } 5585 5586 PL_hints |= HINT_BLOCK_SCOPE; 5587 pm = (PMOP*)o; 5588 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV)); 5589 5590 if (is_compiletime) { 5591 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME; 5592 regexp_engine const *eng = current_re_engine(); 5593 5594 if (o->op_flags & OPf_SPECIAL) 5595 rx_flags |= RXf_SPLIT; 5596 5597 if (!has_code || !eng->op_comp) { 5598 /* compile-time simple constant pattern */ 5599 5600 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) { 5601 /* whoops! we guessed that a qr// had a code block, but we 5602 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv 5603 * that isn't required now. Note that we have to be pretty 5604 * confident that nothing used that CV's pad while the 5605 * regex was parsed, except maybe op targets for \Q etc. 5606 * If there were any op targets, though, they should have 5607 * been stolen by constant folding. 5608 */ 5609 #ifdef DEBUGGING 5610 SSize_t i = 0; 5611 assert(PadnamelistMAXNAMED(PL_comppad_name) == 0); 5612 while (++i <= AvFILLp(PL_comppad)) { 5613 assert(!PL_curpad[i]); 5614 } 5615 #endif 5616 /* But we know that one op is using this CV's slab. */ 5617 cv_forget_slab(PL_compcv); 5618 LEAVE_SCOPE(floor); 5619 pm->op_pmflags &= ~PMf_HAS_CV; 5620 } 5621 5622 PM_SETRE(pm, 5623 eng->op_comp 5624 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL, 5625 rx_flags, pm->op_pmflags) 5626 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL, 5627 rx_flags, pm->op_pmflags) 5628 ); 5629 op_free(expr); 5630 } 5631 else { 5632 /* compile-time pattern that includes literal code blocks */ 5633 REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL, 5634 rx_flags, 5635 (pm->op_pmflags | 5636 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0)) 5637 ); 5638 PM_SETRE(pm, re); 5639 if (pm->op_pmflags & PMf_HAS_CV) { 5640 CV *cv; 5641 /* this QR op (and the anon sub we embed it in) is never 5642 * actually executed. It's just a placeholder where we can 5643 * squirrel away expr in op_code_list without the peephole 5644 * optimiser etc processing it for a second time */ 5645 OP *qr = newPMOP(OP_QR, 0); 5646 ((PMOP*)qr)->op_code_list = expr; 5647 5648 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */ 5649 SvREFCNT_inc_simple_void(PL_compcv); 5650 cv = newATTRSUB(floor, 0, NULL, NULL, qr); 5651 ReANY(re)->qr_anoncv = cv; 5652 5653 /* attach the anon CV to the pad so that 5654 * pad_fixup_inner_anons() can find it */ 5655 (void)pad_add_anon(cv, o->op_type); 5656 SvREFCNT_inc_simple_void(cv); 5657 } 5658 else { 5659 pm->op_code_list = expr; 5660 } 5661 } 5662 } 5663 else { 5664 /* runtime pattern: build chain of regcomp etc ops */ 5665 bool reglist; 5666 PADOFFSET cv_targ = 0; 5667 5668 reglist = isreg && expr->op_type == OP_LIST; 5669 if (reglist) 5670 op_null(expr); 5671 5672 if (has_code) { 5673 pm->op_code_list = expr; 5674 /* don't free op_code_list; its ops are embedded elsewhere too */ 5675 pm->op_pmflags |= PMf_CODELIST_PRIVATE; 5676 } 5677 5678 if (o->op_flags & OPf_SPECIAL) 5679 pm->op_pmflags |= PMf_SPLIT; 5680 5681 /* the OP_REGCMAYBE is a placeholder in the non-threaded case 5682 * to allow its op_next to be pointed past the regcomp and 5683 * preceding stacking ops; 5684 * OP_REGCRESET is there to reset taint before executing the 5685 * stacking ops */ 5686 if (pm->op_pmflags & PMf_KEEP || TAINTING_get) 5687 expr = newUNOP((TAINTING_get ? OP_REGCRESET : OP_REGCMAYBE),0,expr); 5688 5689 if (pm->op_pmflags & PMf_HAS_CV) { 5690 /* we have a runtime qr with literal code. This means 5691 * that the qr// has been wrapped in a new CV, which 5692 * means that runtime consts, vars etc will have been compiled 5693 * against a new pad. So... we need to execute those ops 5694 * within the environment of the new CV. So wrap them in a call 5695 * to a new anon sub. i.e. for 5696 * 5697 * qr/a$b(?{...})/, 5698 * 5699 * we build an anon sub that looks like 5700 * 5701 * sub { "a", $b, '(?{...})' } 5702 * 5703 * and call it, passing the returned list to regcomp. 5704 * Or to put it another way, the list of ops that get executed 5705 * are: 5706 * 5707 * normal PMf_HAS_CV 5708 * ------ ------------------- 5709 * pushmark (for regcomp) 5710 * pushmark (for entersub) 5711 * anoncode 5712 * srefgen 5713 * entersub 5714 * regcreset regcreset 5715 * pushmark pushmark 5716 * const("a") const("a") 5717 * gvsv(b) gvsv(b) 5718 * const("(?{...})") const("(?{...})") 5719 * leavesub 5720 * regcomp regcomp 5721 */ 5722 5723 SvREFCNT_inc_simple_void(PL_compcv); 5724 CvLVALUE_on(PL_compcv); 5725 /* these lines are just an unrolled newANONATTRSUB */ 5726 expr = newSVOP(OP_ANONCODE, 0, 5727 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr))); 5728 cv_targ = expr->op_targ; 5729 expr = newUNOP(OP_REFGEN, 0, expr); 5730 5731 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr)), 1)); 5732 } 5733 5734 rcop = S_alloc_LOGOP(aTHX_ OP_REGCOMP, scalar(expr), o); 5735 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0) 5736 | (reglist ? OPf_STACKED : 0); 5737 rcop->op_targ = cv_targ; 5738 5739 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */ 5740 if (PL_hints & HINT_RE_EVAL) 5741 S_set_haseval(aTHX); 5742 5743 /* establish postfix order */ 5744 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) { 5745 LINKLIST(expr); 5746 rcop->op_next = expr; 5747 ((UNOP*)expr)->op_first->op_next = (OP*)rcop; 5748 } 5749 else { 5750 rcop->op_next = LINKLIST(expr); 5751 expr->op_next = (OP*)rcop; 5752 } 5753 5754 op_prepend_elem(o->op_type, scalar((OP*)rcop), o); 5755 } 5756 5757 if (repl) { 5758 OP *curop = repl; 5759 bool konst; 5760 /* If we are looking at s//.../e with a single statement, get past 5761 the implicit do{}. */ 5762 if (curop->op_type == OP_NULL && curop->op_flags & OPf_KIDS 5763 && cUNOPx(curop)->op_first->op_type == OP_SCOPE 5764 && cUNOPx(curop)->op_first->op_flags & OPf_KIDS) 5765 { 5766 OP *sib; 5767 OP *kid = cUNOPx(cUNOPx(curop)->op_first)->op_first; 5768 if (kid->op_type == OP_NULL && (sib = OpSIBLING(kid)) 5769 && !OpHAS_SIBLING(sib)) 5770 curop = sib; 5771 } 5772 if (curop->op_type == OP_CONST) 5773 konst = TRUE; 5774 else if (( (curop->op_type == OP_RV2SV || 5775 curop->op_type == OP_RV2AV || 5776 curop->op_type == OP_RV2HV || 5777 curop->op_type == OP_RV2GV) 5778 && cUNOPx(curop)->op_first 5779 && cUNOPx(curop)->op_first->op_type == OP_GV ) 5780 || curop->op_type == OP_PADSV 5781 || curop->op_type == OP_PADAV 5782 || curop->op_type == OP_PADHV 5783 || curop->op_type == OP_PADANY) { 5784 repl_has_vars = 1; 5785 konst = TRUE; 5786 } 5787 else konst = FALSE; 5788 if (konst 5789 && !(repl_has_vars 5790 && (!PM_GETRE(pm) 5791 || !RX_PRELEN(PM_GETRE(pm)) 5792 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN))) 5793 { 5794 pm->op_pmflags |= PMf_CONST; /* const for long enough */ 5795 op_prepend_elem(o->op_type, scalar(repl), o); 5796 } 5797 else { 5798 rcop = S_alloc_LOGOP(aTHX_ OP_SUBSTCONT, scalar(repl), o); 5799 rcop->op_private = 1; 5800 5801 /* establish postfix order */ 5802 rcop->op_next = LINKLIST(repl); 5803 repl->op_next = (OP*)rcop; 5804 5805 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop); 5806 assert(!(pm->op_pmflags & PMf_ONCE)); 5807 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop); 5808 rcop->op_next = 0; 5809 } 5810 } 5811 5812 return (OP*)pm; 5813 } 5814 5815 /* 5816 =for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv 5817 5818 Constructs, checks, and returns an op of any type that involves an 5819 embedded SV. C<type> is the opcode. C<flags> gives the eight bits 5820 of C<op_flags>. C<sv> gives the SV to embed in the op; this function 5821 takes ownership of one reference to it. 5822 5823 =cut 5824 */ 5825 5826 OP * 5827 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv) 5828 { 5829 dVAR; 5830 SVOP *svop; 5831 5832 PERL_ARGS_ASSERT_NEWSVOP; 5833 5834 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP 5835 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP 5836 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP 5837 || type == OP_CUSTOM); 5838 5839 NewOp(1101, svop, 1, SVOP); 5840 OpTYPE_set(svop, type); 5841 svop->op_sv = sv; 5842 svop->op_next = (OP*)svop; 5843 svop->op_flags = (U8)flags; 5844 svop->op_private = (U8)(0 | (flags >> 8)); 5845 if (PL_opargs[type] & OA_RETSCALAR) 5846 scalar((OP*)svop); 5847 if (PL_opargs[type] & OA_TARGET) 5848 svop->op_targ = pad_alloc(type, SVs_PADTMP); 5849 return CHECKOP(type, svop); 5850 } 5851 5852 /* 5853 =for apidoc Am|OP *|newDEFSVOP| 5854 5855 Constructs and returns an op to access C<$_>. 5856 5857 =cut 5858 */ 5859 5860 OP * 5861 Perl_newDEFSVOP(pTHX) 5862 { 5863 return newSVREF(newGVOP(OP_GV, 0, PL_defgv)); 5864 } 5865 5866 #ifdef USE_ITHREADS 5867 5868 /* 5869 =for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv 5870 5871 Constructs, checks, and returns an op of any type that involves a 5872 reference to a pad element. C<type> is the opcode. C<flags> gives the 5873 eight bits of C<op_flags>. A pad slot is automatically allocated, and 5874 is populated with C<sv>; this function takes ownership of one reference 5875 to it. 5876 5877 This function only exists if Perl has been compiled to use ithreads. 5878 5879 =cut 5880 */ 5881 5882 OP * 5883 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv) 5884 { 5885 dVAR; 5886 PADOP *padop; 5887 5888 PERL_ARGS_ASSERT_NEWPADOP; 5889 5890 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP 5891 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP 5892 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP 5893 || type == OP_CUSTOM); 5894 5895 NewOp(1101, padop, 1, PADOP); 5896 OpTYPE_set(padop, type); 5897 padop->op_padix = 5898 pad_alloc(type, isGV(sv) ? SVf_READONLY : SVs_PADTMP); 5899 SvREFCNT_dec(PAD_SVl(padop->op_padix)); 5900 PAD_SETSV(padop->op_padix, sv); 5901 assert(sv); 5902 padop->op_next = (OP*)padop; 5903 padop->op_flags = (U8)flags; 5904 if (PL_opargs[type] & OA_RETSCALAR) 5905 scalar((OP*)padop); 5906 if (PL_opargs[type] & OA_TARGET) 5907 padop->op_targ = pad_alloc(type, SVs_PADTMP); 5908 return CHECKOP(type, padop); 5909 } 5910 5911 #endif /* USE_ITHREADS */ 5912 5913 /* 5914 =for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv 5915 5916 Constructs, checks, and returns an op of any type that involves an 5917 embedded reference to a GV. C<type> is the opcode. C<flags> gives the 5918 eight bits of C<op_flags>. C<gv> identifies the GV that the op should 5919 reference; calling this function does not transfer ownership of any 5920 reference to it. 5921 5922 =cut 5923 */ 5924 5925 OP * 5926 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv) 5927 { 5928 PERL_ARGS_ASSERT_NEWGVOP; 5929 5930 #ifdef USE_ITHREADS 5931 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv)); 5932 #else 5933 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv)); 5934 #endif 5935 } 5936 5937 /* 5938 =for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv 5939 5940 Constructs, checks, and returns an op of any type that involves an 5941 embedded C-level pointer (PV). C<type> is the opcode. C<flags> gives 5942 the eight bits of C<op_flags>. C<pv> supplies the C-level pointer, which 5943 must have been allocated using C<PerlMemShared_malloc>; the memory will 5944 be freed when the op is destroyed. 5945 5946 =cut 5947 */ 5948 5949 OP * 5950 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv) 5951 { 5952 dVAR; 5953 const bool utf8 = cBOOL(flags & SVf_UTF8); 5954 PVOP *pvop; 5955 5956 flags &= ~SVf_UTF8; 5957 5958 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP 5959 || type == OP_RUNCV || type == OP_CUSTOM 5960 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP); 5961 5962 NewOp(1101, pvop, 1, PVOP); 5963 OpTYPE_set(pvop, type); 5964 pvop->op_pv = pv; 5965 pvop->op_next = (OP*)pvop; 5966 pvop->op_flags = (U8)flags; 5967 pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0; 5968 if (PL_opargs[type] & OA_RETSCALAR) 5969 scalar((OP*)pvop); 5970 if (PL_opargs[type] & OA_TARGET) 5971 pvop->op_targ = pad_alloc(type, SVs_PADTMP); 5972 return CHECKOP(type, pvop); 5973 } 5974 5975 void 5976 Perl_package(pTHX_ OP *o) 5977 { 5978 SV *const sv = cSVOPo->op_sv; 5979 5980 PERL_ARGS_ASSERT_PACKAGE; 5981 5982 SAVEGENERICSV(PL_curstash); 5983 save_item(PL_curstname); 5984 5985 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD)); 5986 5987 sv_setsv(PL_curstname, sv); 5988 5989 PL_hints |= HINT_BLOCK_SCOPE; 5990 PL_parser->copline = NOLINE; 5991 5992 op_free(o); 5993 } 5994 5995 void 5996 Perl_package_version( pTHX_ OP *v ) 5997 { 5998 U32 savehints = PL_hints; 5999 PERL_ARGS_ASSERT_PACKAGE_VERSION; 6000 PL_hints &= ~HINT_STRICT_VARS; 6001 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv ); 6002 PL_hints = savehints; 6003 op_free(v); 6004 } 6005 6006 void 6007 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg) 6008 { 6009 OP *pack; 6010 OP *imop; 6011 OP *veop; 6012 SV *use_version = NULL; 6013 6014 PERL_ARGS_ASSERT_UTILIZE; 6015 6016 if (idop->op_type != OP_CONST) 6017 Perl_croak(aTHX_ "Module name must be constant"); 6018 6019 veop = NULL; 6020 6021 if (version) { 6022 SV * const vesv = ((SVOP*)version)->op_sv; 6023 6024 if (!arg && !SvNIOKp(vesv)) { 6025 arg = version; 6026 } 6027 else { 6028 OP *pack; 6029 SV *meth; 6030 6031 if (version->op_type != OP_CONST || !SvNIOKp(vesv)) 6032 Perl_croak(aTHX_ "Version number must be a constant number"); 6033 6034 /* Make copy of idop so we don't free it twice */ 6035 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv)); 6036 6037 /* Fake up a method call to VERSION */ 6038 meth = newSVpvs_share("VERSION"); 6039 veop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, 6040 op_append_elem(OP_LIST, 6041 op_prepend_elem(OP_LIST, pack, version), 6042 newMETHOP_named(OP_METHOD_NAMED, 0, meth))); 6043 } 6044 } 6045 6046 /* Fake up an import/unimport */ 6047 if (arg && arg->op_type == OP_STUB) { 6048 imop = arg; /* no import on explicit () */ 6049 } 6050 else if (SvNIOKp(((SVOP*)idop)->op_sv)) { 6051 imop = NULL; /* use 5.0; */ 6052 if (aver) 6053 use_version = ((SVOP*)idop)->op_sv; 6054 else 6055 idop->op_private |= OPpCONST_NOVER; 6056 } 6057 else { 6058 SV *meth; 6059 6060 /* Make copy of idop so we don't free it twice */ 6061 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv)); 6062 6063 /* Fake up a method call to import/unimport */ 6064 meth = aver 6065 ? newSVpvs_share("import") : newSVpvs_share("unimport"); 6066 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, 6067 op_append_elem(OP_LIST, 6068 op_prepend_elem(OP_LIST, pack, arg), 6069 newMETHOP_named(OP_METHOD_NAMED, 0, meth) 6070 )); 6071 } 6072 6073 /* Fake up the BEGIN {}, which does its thing immediately. */ 6074 newATTRSUB(floor, 6075 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")), 6076 NULL, 6077 NULL, 6078 op_append_elem(OP_LINESEQ, 6079 op_append_elem(OP_LINESEQ, 6080 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)), 6081 newSTATEOP(0, NULL, veop)), 6082 newSTATEOP(0, NULL, imop) )); 6083 6084 if (use_version) { 6085 /* Enable the 6086 * feature bundle that corresponds to the required version. */ 6087 use_version = sv_2mortal(new_version(use_version)); 6088 S_enable_feature_bundle(aTHX_ use_version); 6089 6090 /* If a version >= 5.11.0 is requested, strictures are on by default! */ 6091 if (vcmp(use_version, 6092 sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) { 6093 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS)) 6094 PL_hints |= HINT_STRICT_REFS; 6095 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS)) 6096 PL_hints |= HINT_STRICT_SUBS; 6097 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS)) 6098 PL_hints |= HINT_STRICT_VARS; 6099 } 6100 /* otherwise they are off */ 6101 else { 6102 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS)) 6103 PL_hints &= ~HINT_STRICT_REFS; 6104 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS)) 6105 PL_hints &= ~HINT_STRICT_SUBS; 6106 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS)) 6107 PL_hints &= ~HINT_STRICT_VARS; 6108 } 6109 } 6110 6111 /* The "did you use incorrect case?" warning used to be here. 6112 * The problem is that on case-insensitive filesystems one 6113 * might get false positives for "use" (and "require"): 6114 * "use Strict" or "require CARP" will work. This causes 6115 * portability problems for the script: in case-strict 6116 * filesystems the script will stop working. 6117 * 6118 * The "incorrect case" warning checked whether "use Foo" 6119 * imported "Foo" to your namespace, but that is wrong, too: 6120 * there is no requirement nor promise in the language that 6121 * a Foo.pm should or would contain anything in package "Foo". 6122 * 6123 * There is very little Configure-wise that can be done, either: 6124 * the case-sensitivity of the build filesystem of Perl does not 6125 * help in guessing the case-sensitivity of the runtime environment. 6126 */ 6127 6128 PL_hints |= HINT_BLOCK_SCOPE; 6129 PL_parser->copline = NOLINE; 6130 COP_SEQMAX_INC; /* Purely for B::*'s benefit */ 6131 } 6132 6133 /* 6134 =head1 Embedding Functions 6135 6136 =for apidoc load_module 6137 6138 Loads the module whose name is pointed to by the string part of name. 6139 Note that the actual module name, not its filename, should be given. 6140 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of 6141 C<PERL_LOADMOD_DENY>, C<PERL_LOADMOD_NOIMPORT>, or C<PERL_LOADMOD_IMPORT_OPS> 6142 (or 0 for no flags). ver, if specified 6143 and not NULL, provides version semantics 6144 similar to C<use Foo::Bar VERSION>. The optional trailing SV* 6145 arguments can be used to specify arguments to the module's C<import()> 6146 method, similar to C<use Foo::Bar VERSION LIST>. They must be 6147 terminated with a final C<NULL> pointer. Note that this list can only 6148 be omitted when the C<PERL_LOADMOD_NOIMPORT> flag has been used. 6149 Otherwise at least a single C<NULL> pointer to designate the default 6150 import list is required. 6151 6152 The reference count for each specified C<SV*> parameter is decremented. 6153 6154 =cut */ 6155 6156 void 6157 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...) 6158 { 6159 va_list args; 6160 6161 PERL_ARGS_ASSERT_LOAD_MODULE; 6162 6163 va_start(args, ver); 6164 vload_module(flags, name, ver, &args); 6165 va_end(args); 6166 } 6167 6168 #ifdef PERL_IMPLICIT_CONTEXT 6169 void 6170 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...) 6171 { 6172 dTHX; 6173 va_list args; 6174 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT; 6175 va_start(args, ver); 6176 vload_module(flags, name, ver, &args); 6177 va_end(args); 6178 } 6179 #endif 6180 6181 void 6182 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args) 6183 { 6184 OP *veop, *imop; 6185 OP * const modname = newSVOP(OP_CONST, 0, name); 6186 6187 PERL_ARGS_ASSERT_VLOAD_MODULE; 6188 6189 modname->op_private |= OPpCONST_BARE; 6190 if (ver) { 6191 veop = newSVOP(OP_CONST, 0, ver); 6192 } 6193 else 6194 veop = NULL; 6195 if (flags & PERL_LOADMOD_NOIMPORT) { 6196 imop = sawparens(newNULLLIST()); 6197 } 6198 else if (flags & PERL_LOADMOD_IMPORT_OPS) { 6199 imop = va_arg(*args, OP*); 6200 } 6201 else { 6202 SV *sv; 6203 imop = NULL; 6204 sv = va_arg(*args, SV*); 6205 while (sv) { 6206 imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv)); 6207 sv = va_arg(*args, SV*); 6208 } 6209 } 6210 6211 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure 6212 * that it has a PL_parser to play with while doing that, and also 6213 * that it doesn't mess with any existing parser, by creating a tmp 6214 * new parser with lex_start(). This won't actually be used for much, 6215 * since pp_require() will create another parser for the real work. 6216 * The ENTER/LEAVE pair protect callers from any side effects of use. */ 6217 6218 ENTER; 6219 SAVEVPTR(PL_curcop); 6220 lex_start(NULL, NULL, LEX_START_SAME_FILTER); 6221 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0), 6222 veop, modname, imop); 6223 LEAVE; 6224 } 6225 6226 PERL_STATIC_INLINE OP * 6227 S_new_entersubop(pTHX_ GV *gv, OP *arg) 6228 { 6229 return newUNOP(OP_ENTERSUB, OPf_STACKED, 6230 newLISTOP(OP_LIST, 0, arg, 6231 newUNOP(OP_RV2CV, 0, 6232 newGVOP(OP_GV, 0, gv)))); 6233 } 6234 6235 OP * 6236 Perl_dofile(pTHX_ OP *term, I32 force_builtin) 6237 { 6238 OP *doop; 6239 GV *gv; 6240 6241 PERL_ARGS_ASSERT_DOFILE; 6242 6243 if (!force_builtin && (gv = gv_override("do", 2))) { 6244 doop = S_new_entersubop(aTHX_ gv, term); 6245 } 6246 else { 6247 doop = newUNOP(OP_DOFILE, 0, scalar(term)); 6248 } 6249 return doop; 6250 } 6251 6252 /* 6253 =head1 Optree construction 6254 6255 =for apidoc Am|OP *|newSLICEOP|I32 flags|OP *subscript|OP *listval 6256 6257 Constructs, checks, and returns an C<lslice> (list slice) op. C<flags> 6258 gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will 6259 be set automatically, and, shifted up eight bits, the eight bits of 6260 C<op_private>, except that the bit with value 1 or 2 is automatically 6261 set as required. C<listval> and C<subscript> supply the parameters of 6262 the slice; they are consumed by this function and become part of the 6263 constructed op tree. 6264 6265 =cut 6266 */ 6267 6268 OP * 6269 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval) 6270 { 6271 return newBINOP(OP_LSLICE, flags, 6272 list(force_list(subscript, 1)), 6273 list(force_list(listval, 1)) ); 6274 } 6275 6276 #define ASSIGN_LIST 1 6277 #define ASSIGN_REF 2 6278 6279 STATIC I32 6280 S_assignment_type(pTHX_ const OP *o) 6281 { 6282 unsigned type; 6283 U8 flags; 6284 U8 ret; 6285 6286 if (!o) 6287 return TRUE; 6288 6289 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS)) 6290 o = cUNOPo->op_first; 6291 6292 flags = o->op_flags; 6293 type = o->op_type; 6294 if (type == OP_COND_EXPR) { 6295 OP * const sib = OpSIBLING(cLOGOPo->op_first); 6296 const I32 t = assignment_type(sib); 6297 const I32 f = assignment_type(OpSIBLING(sib)); 6298 6299 if (t == ASSIGN_LIST && f == ASSIGN_LIST) 6300 return ASSIGN_LIST; 6301 if ((t == ASSIGN_LIST) ^ (f == ASSIGN_LIST)) 6302 yyerror("Assignment to both a list and a scalar"); 6303 return FALSE; 6304 } 6305 6306 if (type == OP_SREFGEN) 6307 { 6308 OP * const kid = cUNOPx(cUNOPo->op_first)->op_first; 6309 type = kid->op_type; 6310 flags |= kid->op_flags; 6311 if (!(flags & OPf_PARENS) 6312 && (kid->op_type == OP_RV2AV || kid->op_type == OP_PADAV || 6313 kid->op_type == OP_RV2HV || kid->op_type == OP_PADHV )) 6314 return ASSIGN_REF; 6315 ret = ASSIGN_REF; 6316 } 6317 else ret = 0; 6318 6319 if (type == OP_LIST && 6320 (flags & OPf_WANT) == OPf_WANT_SCALAR && 6321 o->op_private & OPpLVAL_INTRO) 6322 return ret; 6323 6324 if (type == OP_LIST || flags & OPf_PARENS || 6325 type == OP_RV2AV || type == OP_RV2HV || 6326 type == OP_ASLICE || type == OP_HSLICE || 6327 type == OP_KVASLICE || type == OP_KVHSLICE || type == OP_REFGEN) 6328 return TRUE; 6329 6330 if (type == OP_PADAV || type == OP_PADHV) 6331 return TRUE; 6332 6333 if (type == OP_RV2SV) 6334 return ret; 6335 6336 return ret; 6337 } 6338 6339 6340 /* 6341 =for apidoc Am|OP *|newASSIGNOP|I32 flags|OP *left|I32 optype|OP *right 6342 6343 Constructs, checks, and returns an assignment op. C<left> and C<right> 6344 supply the parameters of the assignment; they are consumed by this 6345 function and become part of the constructed op tree. 6346 6347 If C<optype> is C<OP_ANDASSIGN>, C<OP_ORASSIGN>, or C<OP_DORASSIGN>, then 6348 a suitable conditional optree is constructed. If C<optype> is the opcode 6349 of a binary operator, such as C<OP_BIT_OR>, then an op is constructed that 6350 performs the binary operation and assigns the result to the left argument. 6351 Either way, if C<optype> is non-zero then C<flags> has no effect. 6352 6353 If C<optype> is zero, then a plain scalar or list assignment is 6354 constructed. Which type of assignment it is is automatically determined. 6355 C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS> 6356 will be set automatically, and, shifted up eight bits, the eight bits 6357 of C<op_private>, except that the bit with value 1 or 2 is automatically 6358 set as required. 6359 6360 =cut 6361 */ 6362 6363 OP * 6364 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right) 6365 { 6366 OP *o; 6367 I32 assign_type; 6368 6369 if (optype) { 6370 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) { 6371 return newLOGOP(optype, 0, 6372 op_lvalue(scalar(left), optype), 6373 newUNOP(OP_SASSIGN, 0, scalar(right))); 6374 } 6375 else { 6376 return newBINOP(optype, OPf_STACKED, 6377 op_lvalue(scalar(left), optype), scalar(right)); 6378 } 6379 } 6380 6381 if ((assign_type = assignment_type(left)) == ASSIGN_LIST) { 6382 static const char no_list_state[] = "Initialization of state variables" 6383 " in list context currently forbidden"; 6384 OP *curop; 6385 6386 if (left->op_type == OP_ASLICE || left->op_type == OP_HSLICE) 6387 left->op_private &= ~ OPpSLICEWARNING; 6388 6389 PL_modcount = 0; 6390 left = op_lvalue(left, OP_AASSIGN); 6391 curop = list(force_list(left, 1)); 6392 o = newBINOP(OP_AASSIGN, flags, list(force_list(right, 1)), curop); 6393 o->op_private = (U8)(0 | (flags >> 8)); 6394 6395 if (OP_TYPE_IS_OR_WAS(left, OP_LIST)) 6396 { 6397 OP* lop = ((LISTOP*)left)->op_first; 6398 while (lop) { 6399 if ((lop->op_type == OP_PADSV || 6400 lop->op_type == OP_PADAV || 6401 lop->op_type == OP_PADHV || 6402 lop->op_type == OP_PADANY) 6403 && (lop->op_private & OPpPAD_STATE) 6404 ) 6405 yyerror(no_list_state); 6406 lop = OpSIBLING(lop); 6407 } 6408 } 6409 else if ( (left->op_private & OPpLVAL_INTRO) 6410 && (left->op_private & OPpPAD_STATE) 6411 && ( left->op_type == OP_PADSV 6412 || left->op_type == OP_PADAV 6413 || left->op_type == OP_PADHV 6414 || left->op_type == OP_PADANY) 6415 ) { 6416 /* All single variable list context state assignments, hence 6417 state ($a) = ... 6418 (state $a) = ... 6419 state @a = ... 6420 state (@a) = ... 6421 (state @a) = ... 6422 state %a = ... 6423 state (%a) = ... 6424 (state %a) = ... 6425 */ 6426 yyerror(no_list_state); 6427 } 6428 6429 if (right && right->op_type == OP_SPLIT 6430 && !(right->op_flags & OPf_STACKED)) { 6431 OP* tmpop = ((LISTOP*)right)->op_first; 6432 PMOP * const pm = (PMOP*)tmpop; 6433 assert (tmpop && (tmpop->op_type == OP_PUSHRE)); 6434 if ( 6435 #ifdef USE_ITHREADS 6436 !pm->op_pmreplrootu.op_pmtargetoff 6437 #else 6438 !pm->op_pmreplrootu.op_pmtargetgv 6439 #endif 6440 && !pm->op_targ 6441 ) { 6442 if (!(left->op_private & OPpLVAL_INTRO) && 6443 ( (left->op_type == OP_RV2AV && 6444 (tmpop=((UNOP*)left)->op_first)->op_type==OP_GV) 6445 || left->op_type == OP_PADAV ) 6446 ) { 6447 if (tmpop != (OP *)pm) { 6448 #ifdef USE_ITHREADS 6449 pm->op_pmreplrootu.op_pmtargetoff 6450 = cPADOPx(tmpop)->op_padix; 6451 cPADOPx(tmpop)->op_padix = 0; /* steal it */ 6452 #else 6453 pm->op_pmreplrootu.op_pmtargetgv 6454 = MUTABLE_GV(cSVOPx(tmpop)->op_sv); 6455 cSVOPx(tmpop)->op_sv = NULL; /* steal it */ 6456 #endif 6457 right->op_private |= 6458 left->op_private & OPpOUR_INTRO; 6459 } 6460 else { 6461 pm->op_targ = left->op_targ; 6462 left->op_targ = 0; /* filch it */ 6463 } 6464 detach_split: 6465 tmpop = cUNOPo->op_first; /* to list (nulled) */ 6466 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */ 6467 /* detach rest of siblings from o subtree, 6468 * and free subtree */ 6469 op_sibling_splice(cUNOPo->op_first, tmpop, -1, NULL); 6470 op_free(o); /* blow off assign */ 6471 right->op_flags &= ~OPf_WANT; 6472 /* "I don't know and I don't care." */ 6473 return right; 6474 } 6475 else if (left->op_type == OP_RV2AV 6476 || left->op_type == OP_PADAV) 6477 { 6478 /* Detach the array. */ 6479 #ifdef DEBUGGING 6480 OP * const ary = 6481 #endif 6482 op_sibling_splice(cBINOPo->op_last, 6483 cUNOPx(cBINOPo->op_last) 6484 ->op_first, 1, NULL); 6485 assert(ary == left); 6486 /* Attach it to the split. */ 6487 op_sibling_splice(right, cLISTOPx(right)->op_last, 6488 0, left); 6489 right->op_flags |= OPf_STACKED; 6490 /* Detach split and expunge aassign as above. */ 6491 goto detach_split; 6492 } 6493 else if (PL_modcount < RETURN_UNLIMITED_NUMBER && 6494 ((LISTOP*)right)->op_last->op_type == OP_CONST) 6495 { 6496 SV ** const svp = 6497 &((SVOP*)((LISTOP*)right)->op_last)->op_sv; 6498 SV * const sv = *svp; 6499 if (SvIOK(sv) && SvIVX(sv) == 0) 6500 { 6501 if (right->op_private & OPpSPLIT_IMPLIM) { 6502 /* our own SV, created in ck_split */ 6503 SvREADONLY_off(sv); 6504 sv_setiv(sv, PL_modcount+1); 6505 } 6506 else { 6507 /* SV may belong to someone else */ 6508 SvREFCNT_dec(sv); 6509 *svp = newSViv(PL_modcount+1); 6510 } 6511 } 6512 } 6513 } 6514 } 6515 return o; 6516 } 6517 if (assign_type == ASSIGN_REF) 6518 return newBINOP(OP_REFASSIGN, flags, scalar(right), left); 6519 if (!right) 6520 right = newOP(OP_UNDEF, 0); 6521 if (right->op_type == OP_READLINE) { 6522 right->op_flags |= OPf_STACKED; 6523 return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN), 6524 scalar(right)); 6525 } 6526 else { 6527 o = newBINOP(OP_SASSIGN, flags, 6528 scalar(right), op_lvalue(scalar(left), OP_SASSIGN) ); 6529 } 6530 return o; 6531 } 6532 6533 /* 6534 =for apidoc Am|OP *|newSTATEOP|I32 flags|char *label|OP *o 6535 6536 Constructs a state op (COP). The state op is normally a C<nextstate> op, 6537 but will be a C<dbstate> op if debugging is enabled for currently-compiled 6538 code. The state op is populated from C<PL_curcop> (or C<PL_compiling>). 6539 If C<label> is non-null, it supplies the name of a label to attach to 6540 the state op; this function takes ownership of the memory pointed at by 6541 C<label>, and will free it. C<flags> gives the eight bits of C<op_flags> 6542 for the state op. 6543 6544 If C<o> is null, the state op is returned. Otherwise the state op is 6545 combined with C<o> into a C<lineseq> list op, which is returned. C<o> 6546 is consumed by this function and becomes part of the returned op tree. 6547 6548 =cut 6549 */ 6550 6551 OP * 6552 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o) 6553 { 6554 dVAR; 6555 const U32 seq = intro_my(); 6556 const U32 utf8 = flags & SVf_UTF8; 6557 COP *cop; 6558 6559 PL_parser->parsed_sub = 0; 6560 6561 flags &= ~SVf_UTF8; 6562 6563 NewOp(1101, cop, 1, COP); 6564 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) { 6565 OpTYPE_set(cop, OP_DBSTATE); 6566 } 6567 else { 6568 OpTYPE_set(cop, OP_NEXTSTATE); 6569 } 6570 cop->op_flags = (U8)flags; 6571 CopHINTS_set(cop, PL_hints); 6572 #ifdef VMS 6573 if (VMSISH_HUSHED) cop->op_private |= OPpHUSH_VMSISH; 6574 #endif 6575 cop->op_next = (OP*)cop; 6576 6577 cop->cop_seq = seq; 6578 cop->cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings); 6579 CopHINTHASH_set(cop, cophh_copy(CopHINTHASH_get(PL_curcop))); 6580 if (label) { 6581 Perl_cop_store_label(aTHX_ cop, label, strlen(label), utf8); 6582 6583 PL_hints |= HINT_BLOCK_SCOPE; 6584 /* It seems that we need to defer freeing this pointer, as other parts 6585 of the grammar end up wanting to copy it after this op has been 6586 created. */ 6587 SAVEFREEPV(label); 6588 } 6589 6590 if (PL_parser->preambling != NOLINE) { 6591 CopLINE_set(cop, PL_parser->preambling); 6592 PL_parser->copline = NOLINE; 6593 } 6594 else if (PL_parser->copline == NOLINE) 6595 CopLINE_set(cop, CopLINE(PL_curcop)); 6596 else { 6597 CopLINE_set(cop, PL_parser->copline); 6598 PL_parser->copline = NOLINE; 6599 } 6600 #ifdef USE_ITHREADS 6601 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */ 6602 #else 6603 CopFILEGV_set(cop, CopFILEGV(PL_curcop)); 6604 #endif 6605 CopSTASH_set(cop, PL_curstash); 6606 6607 if (cop->op_type == OP_DBSTATE) { 6608 /* this line can have a breakpoint - store the cop in IV */ 6609 AV *av = CopFILEAVx(PL_curcop); 6610 if (av) { 6611 SV * const * const svp = av_fetch(av, CopLINE(cop), FALSE); 6612 if (svp && *svp != &PL_sv_undef ) { 6613 (void)SvIOK_on(*svp); 6614 SvIV_set(*svp, PTR2IV(cop)); 6615 } 6616 } 6617 } 6618 6619 if (flags & OPf_SPECIAL) 6620 op_null((OP*)cop); 6621 return op_prepend_elem(OP_LINESEQ, (OP*)cop, o); 6622 } 6623 6624 /* 6625 =for apidoc Am|OP *|newLOGOP|I32 type|I32 flags|OP *first|OP *other 6626 6627 Constructs, checks, and returns a logical (flow control) op. C<type> 6628 is the opcode. C<flags> gives the eight bits of C<op_flags>, except 6629 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits, 6630 the eight bits of C<op_private>, except that the bit with value 1 is 6631 automatically set. C<first> supplies the expression controlling the 6632 flow, and C<other> supplies the side (alternate) chain of ops; they are 6633 consumed by this function and become part of the constructed op tree. 6634 6635 =cut 6636 */ 6637 6638 OP * 6639 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other) 6640 { 6641 PERL_ARGS_ASSERT_NEWLOGOP; 6642 6643 return new_logop(type, flags, &first, &other); 6644 } 6645 6646 STATIC OP * 6647 S_search_const(pTHX_ OP *o) 6648 { 6649 PERL_ARGS_ASSERT_SEARCH_CONST; 6650 6651 switch (o->op_type) { 6652 case OP_CONST: 6653 return o; 6654 case OP_NULL: 6655 if (o->op_flags & OPf_KIDS) 6656 return search_const(cUNOPo->op_first); 6657 break; 6658 case OP_LEAVE: 6659 case OP_SCOPE: 6660 case OP_LINESEQ: 6661 { 6662 OP *kid; 6663 if (!(o->op_flags & OPf_KIDS)) 6664 return NULL; 6665 kid = cLISTOPo->op_first; 6666 do { 6667 switch (kid->op_type) { 6668 case OP_ENTER: 6669 case OP_NULL: 6670 case OP_NEXTSTATE: 6671 kid = OpSIBLING(kid); 6672 break; 6673 default: 6674 if (kid != cLISTOPo->op_last) 6675 return NULL; 6676 goto last; 6677 } 6678 } while (kid); 6679 if (!kid) 6680 kid = cLISTOPo->op_last; 6681 last: 6682 return search_const(kid); 6683 } 6684 } 6685 6686 return NULL; 6687 } 6688 6689 STATIC OP * 6690 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp) 6691 { 6692 dVAR; 6693 LOGOP *logop; 6694 OP *o; 6695 OP *first; 6696 OP *other; 6697 OP *cstop = NULL; 6698 int prepend_not = 0; 6699 6700 PERL_ARGS_ASSERT_NEW_LOGOP; 6701 6702 first = *firstp; 6703 other = *otherp; 6704 6705 /* [perl #59802]: Warn about things like "return $a or $b", which 6706 is parsed as "(return $a) or $b" rather than "return ($a or 6707 $b)". NB: This also applies to xor, which is why we do it 6708 here. 6709 */ 6710 switch (first->op_type) { 6711 case OP_NEXT: 6712 case OP_LAST: 6713 case OP_REDO: 6714 /* XXX: Perhaps we should emit a stronger warning for these. 6715 Even with the high-precedence operator they don't seem to do 6716 anything sensible. 6717 6718 But until we do, fall through here. 6719 */ 6720 case OP_RETURN: 6721 case OP_EXIT: 6722 case OP_DIE: 6723 case OP_GOTO: 6724 /* XXX: Currently we allow people to "shoot themselves in the 6725 foot" by explicitly writing "(return $a) or $b". 6726 6727 Warn unless we are looking at the result from folding or if 6728 the programmer explicitly grouped the operators like this. 6729 The former can occur with e.g. 6730 6731 use constant FEATURE => ( $] >= ... ); 6732 sub { not FEATURE and return or do_stuff(); } 6733 */ 6734 if (!first->op_folded && !(first->op_flags & OPf_PARENS)) 6735 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), 6736 "Possible precedence issue with control flow operator"); 6737 /* XXX: Should we optimze this to "return $a;" (i.e. remove 6738 the "or $b" part)? 6739 */ 6740 break; 6741 } 6742 6743 if (type == OP_XOR) /* Not short circuit, but here by precedence. */ 6744 return newBINOP(type, flags, scalar(first), scalar(other)); 6745 6746 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOGOP 6747 || type == OP_CUSTOM); 6748 6749 scalarboolean(first); 6750 /* optimize AND and OR ops that have NOTs as children */ 6751 if (first->op_type == OP_NOT 6752 && (first->op_flags & OPf_KIDS) 6753 && ((first->op_flags & OPf_SPECIAL) /* unless ($x) { } */ 6754 || (other->op_type == OP_NOT)) /* if (!$x && !$y) { } */ 6755 ) { 6756 if (type == OP_AND || type == OP_OR) { 6757 if (type == OP_AND) 6758 type = OP_OR; 6759 else 6760 type = OP_AND; 6761 op_null(first); 6762 if (other->op_type == OP_NOT) { /* !a AND|OR !b => !(a OR|AND b) */ 6763 op_null(other); 6764 prepend_not = 1; /* prepend a NOT op later */ 6765 } 6766 } 6767 } 6768 /* search for a constant op that could let us fold the test */ 6769 if ((cstop = search_const(first))) { 6770 if (cstop->op_private & OPpCONST_STRICT) 6771 no_bareword_allowed(cstop); 6772 else if ((cstop->op_private & OPpCONST_BARE)) 6773 Perl_ck_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional"); 6774 if ((type == OP_AND && SvTRUE(((SVOP*)cstop)->op_sv)) || 6775 (type == OP_OR && !SvTRUE(((SVOP*)cstop)->op_sv)) || 6776 (type == OP_DOR && !SvOK(((SVOP*)cstop)->op_sv))) { 6777 *firstp = NULL; 6778 if (other->op_type == OP_CONST) 6779 other->op_private |= OPpCONST_SHORTCIRCUIT; 6780 op_free(first); 6781 if (other->op_type == OP_LEAVE) 6782 other = newUNOP(OP_NULL, OPf_SPECIAL, other); 6783 else if (other->op_type == OP_MATCH 6784 || other->op_type == OP_SUBST 6785 || other->op_type == OP_TRANSR 6786 || other->op_type == OP_TRANS) 6787 /* Mark the op as being unbindable with =~ */ 6788 other->op_flags |= OPf_SPECIAL; 6789 6790 other->op_folded = 1; 6791 return other; 6792 } 6793 else { 6794 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */ 6795 const OP *o2 = other; 6796 if ( ! (o2->op_type == OP_LIST 6797 && (( o2 = cUNOPx(o2)->op_first)) 6798 && o2->op_type == OP_PUSHMARK 6799 && (( o2 = OpSIBLING(o2))) ) 6800 ) 6801 o2 = other; 6802 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV 6803 || o2->op_type == OP_PADHV) 6804 && o2->op_private & OPpLVAL_INTRO 6805 && !(o2->op_private & OPpPAD_STATE)) 6806 { 6807 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED), 6808 "Deprecated use of my() in false conditional"); 6809 } 6810 6811 *otherp = NULL; 6812 if (cstop->op_type == OP_CONST) 6813 cstop->op_private |= OPpCONST_SHORTCIRCUIT; 6814 op_free(other); 6815 return first; 6816 } 6817 } 6818 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR 6819 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */ 6820 { 6821 const OP * const k1 = ((UNOP*)first)->op_first; 6822 const OP * const k2 = OpSIBLING(k1); 6823 OPCODE warnop = 0; 6824 switch (first->op_type) 6825 { 6826 case OP_NULL: 6827 if (k2 && k2->op_type == OP_READLINE 6828 && (k2->op_flags & OPf_STACKED) 6829 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 6830 { 6831 warnop = k2->op_type; 6832 } 6833 break; 6834 6835 case OP_SASSIGN: 6836 if (k1->op_type == OP_READDIR 6837 || k1->op_type == OP_GLOB 6838 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) 6839 || k1->op_type == OP_EACH 6840 || k1->op_type == OP_AEACH) 6841 { 6842 warnop = ((k1->op_type == OP_NULL) 6843 ? (OPCODE)k1->op_targ : k1->op_type); 6844 } 6845 break; 6846 } 6847 if (warnop) { 6848 const line_t oldline = CopLINE(PL_curcop); 6849 /* This ensures that warnings are reported at the first line 6850 of the construction, not the last. */ 6851 CopLINE_set(PL_curcop, PL_parser->copline); 6852 Perl_warner(aTHX_ packWARN(WARN_MISC), 6853 "Value of %s%s can be \"0\"; test with defined()", 6854 PL_op_desc[warnop], 6855 ((warnop == OP_READLINE || warnop == OP_GLOB) 6856 ? " construct" : "() operator")); 6857 CopLINE_set(PL_curcop, oldline); 6858 } 6859 } 6860 6861 if (!other) 6862 return first; 6863 6864 if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN) 6865 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */ 6866 6867 logop = S_alloc_LOGOP(aTHX_ type, first, LINKLIST(other)); 6868 logop->op_flags |= (U8)flags; 6869 logop->op_private = (U8)(1 | (flags >> 8)); 6870 6871 /* establish postfix order */ 6872 logop->op_next = LINKLIST(first); 6873 first->op_next = (OP*)logop; 6874 assert(!OpHAS_SIBLING(first)); 6875 op_sibling_splice((OP*)logop, first, 0, other); 6876 6877 CHECKOP(type,logop); 6878 6879 o = newUNOP(prepend_not ? OP_NOT : OP_NULL, 6880 PL_opargs[type] & OA_RETSCALAR ? OPf_WANT_SCALAR : 0, 6881 (OP*)logop); 6882 other->op_next = o; 6883 6884 return o; 6885 } 6886 6887 /* 6888 =for apidoc Am|OP *|newCONDOP|I32 flags|OP *first|OP *trueop|OP *falseop 6889 6890 Constructs, checks, and returns a conditional-expression (C<cond_expr>) 6891 op. C<flags> gives the eight bits of C<op_flags>, except that C<OPf_KIDS> 6892 will be set automatically, and, shifted up eight bits, the eight bits of 6893 C<op_private>, except that the bit with value 1 is automatically set. 6894 C<first> supplies the expression selecting between the two branches, 6895 and C<trueop> and C<falseop> supply the branches; they are consumed by 6896 this function and become part of the constructed op tree. 6897 6898 =cut 6899 */ 6900 6901 OP * 6902 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop) 6903 { 6904 dVAR; 6905 LOGOP *logop; 6906 OP *start; 6907 OP *o; 6908 OP *cstop; 6909 6910 PERL_ARGS_ASSERT_NEWCONDOP; 6911 6912 if (!falseop) 6913 return newLOGOP(OP_AND, 0, first, trueop); 6914 if (!trueop) 6915 return newLOGOP(OP_OR, 0, first, falseop); 6916 6917 scalarboolean(first); 6918 if ((cstop = search_const(first))) { 6919 /* Left or right arm of the conditional? */ 6920 const bool left = SvTRUE(((SVOP*)cstop)->op_sv); 6921 OP *live = left ? trueop : falseop; 6922 OP *const dead = left ? falseop : trueop; 6923 if (cstop->op_private & OPpCONST_BARE && 6924 cstop->op_private & OPpCONST_STRICT) { 6925 no_bareword_allowed(cstop); 6926 } 6927 op_free(first); 6928 op_free(dead); 6929 if (live->op_type == OP_LEAVE) 6930 live = newUNOP(OP_NULL, OPf_SPECIAL, live); 6931 else if (live->op_type == OP_MATCH || live->op_type == OP_SUBST 6932 || live->op_type == OP_TRANS || live->op_type == OP_TRANSR) 6933 /* Mark the op as being unbindable with =~ */ 6934 live->op_flags |= OPf_SPECIAL; 6935 live->op_folded = 1; 6936 return live; 6937 } 6938 logop = S_alloc_LOGOP(aTHX_ OP_COND_EXPR, first, LINKLIST(trueop)); 6939 logop->op_flags |= (U8)flags; 6940 logop->op_private = (U8)(1 | (flags >> 8)); 6941 logop->op_next = LINKLIST(falseop); 6942 6943 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */ 6944 logop); 6945 6946 /* establish postfix order */ 6947 start = LINKLIST(first); 6948 first->op_next = (OP*)logop; 6949 6950 /* make first, trueop, falseop siblings */ 6951 op_sibling_splice((OP*)logop, first, 0, trueop); 6952 op_sibling_splice((OP*)logop, trueop, 0, falseop); 6953 6954 o = newUNOP(OP_NULL, 0, (OP*)logop); 6955 6956 trueop->op_next = falseop->op_next = o; 6957 6958 o->op_next = start; 6959 return o; 6960 } 6961 6962 /* 6963 =for apidoc Am|OP *|newRANGE|I32 flags|OP *left|OP *right 6964 6965 Constructs and returns a C<range> op, with subordinate C<flip> and 6966 C<flop> ops. C<flags> gives the eight bits of C<op_flags> for the 6967 C<flip> op and, shifted up eight bits, the eight bits of C<op_private> 6968 for both the C<flip> and C<range> ops, except that the bit with value 6969 1 is automatically set. C<left> and C<right> supply the expressions 6970 controlling the endpoints of the range; they are consumed by this function 6971 and become part of the constructed op tree. 6972 6973 =cut 6974 */ 6975 6976 OP * 6977 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right) 6978 { 6979 LOGOP *range; 6980 OP *flip; 6981 OP *flop; 6982 OP *leftstart; 6983 OP *o; 6984 6985 PERL_ARGS_ASSERT_NEWRANGE; 6986 6987 range = S_alloc_LOGOP(aTHX_ OP_RANGE, left, LINKLIST(right)); 6988 range->op_flags = OPf_KIDS; 6989 leftstart = LINKLIST(left); 6990 range->op_private = (U8)(1 | (flags >> 8)); 6991 6992 /* make left and right siblings */ 6993 op_sibling_splice((OP*)range, left, 0, right); 6994 6995 range->op_next = (OP*)range; 6996 flip = newUNOP(OP_FLIP, flags, (OP*)range); 6997 flop = newUNOP(OP_FLOP, 0, flip); 6998 o = newUNOP(OP_NULL, 0, flop); 6999 LINKLIST(flop); 7000 range->op_next = leftstart; 7001 7002 left->op_next = flip; 7003 right->op_next = flop; 7004 7005 range->op_targ = 7006 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0); 7007 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV); 7008 flip->op_targ = 7009 pad_add_name_pvn("$", 1, padadd_NO_DUP_CHECK|padadd_STATE, 0, 0);; 7010 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV); 7011 SvPADTMP_on(PAD_SV(flip->op_targ)); 7012 7013 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; 7014 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; 7015 7016 /* check barewords before they might be optimized aways */ 7017 if (flip->op_private && cSVOPx(left)->op_private & OPpCONST_STRICT) 7018 no_bareword_allowed(left); 7019 if (flop->op_private && cSVOPx(right)->op_private & OPpCONST_STRICT) 7020 no_bareword_allowed(right); 7021 7022 flip->op_next = o; 7023 if (!flip->op_private || !flop->op_private) 7024 LINKLIST(o); /* blow off optimizer unless constant */ 7025 7026 return o; 7027 } 7028 7029 /* 7030 =for apidoc Am|OP *|newLOOPOP|I32 flags|I32 debuggable|OP *expr|OP *block 7031 7032 Constructs, checks, and returns an op tree expressing a loop. This is 7033 only a loop in the control flow through the op tree; it does not have 7034 the heavyweight loop structure that allows exiting the loop by C<last> 7035 and suchlike. C<flags> gives the eight bits of C<op_flags> for the 7036 top-level op, except that some bits will be set automatically as required. 7037 C<expr> supplies the expression controlling loop iteration, and C<block> 7038 supplies the body of the loop; they are consumed by this function and 7039 become part of the constructed op tree. C<debuggable> is currently 7040 unused and should always be 1. 7041 7042 =cut 7043 */ 7044 7045 OP * 7046 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block) 7047 { 7048 OP* listop; 7049 OP* o; 7050 const bool once = block && block->op_flags & OPf_SPECIAL && 7051 block->op_type == OP_NULL; 7052 7053 PERL_UNUSED_ARG(debuggable); 7054 7055 if (expr) { 7056 if (once && ( 7057 (expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv)) 7058 || ( expr->op_type == OP_NOT 7059 && cUNOPx(expr)->op_first->op_type == OP_CONST 7060 && SvTRUE(cSVOPx_sv(cUNOPx(expr)->op_first)) 7061 ) 7062 )) 7063 /* Return the block now, so that S_new_logop does not try to 7064 fold it away. */ 7065 return block; /* do {} while 0 does once */ 7066 if (expr->op_type == OP_READLINE 7067 || expr->op_type == OP_READDIR 7068 || expr->op_type == OP_GLOB 7069 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH 7070 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) { 7071 expr = newUNOP(OP_DEFINED, 0, 7072 newASSIGNOP(0, newDEFSVOP(), 0, expr) ); 7073 } else if (expr->op_flags & OPf_KIDS) { 7074 const OP * const k1 = ((UNOP*)expr)->op_first; 7075 const OP * const k2 = k1 ? OpSIBLING(k1) : NULL; 7076 switch (expr->op_type) { 7077 case OP_NULL: 7078 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR) 7079 && (k2->op_flags & OPf_STACKED) 7080 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 7081 expr = newUNOP(OP_DEFINED, 0, expr); 7082 break; 7083 7084 case OP_SASSIGN: 7085 if (k1 && (k1->op_type == OP_READDIR 7086 || k1->op_type == OP_GLOB 7087 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) 7088 || k1->op_type == OP_EACH 7089 || k1->op_type == OP_AEACH)) 7090 expr = newUNOP(OP_DEFINED, 0, expr); 7091 break; 7092 } 7093 } 7094 } 7095 7096 /* if block is null, the next op_append_elem() would put UNSTACK, a scalar 7097 * op, in listop. This is wrong. [perl #27024] */ 7098 if (!block) 7099 block = newOP(OP_NULL, 0); 7100 listop = op_append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0)); 7101 o = new_logop(OP_AND, 0, &expr, &listop); 7102 7103 if (once) { 7104 ASSUME(listop); 7105 } 7106 7107 if (listop) 7108 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o); 7109 7110 if (once && o != listop) 7111 { 7112 assert(cUNOPo->op_first->op_type == OP_AND 7113 || cUNOPo->op_first->op_type == OP_OR); 7114 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other; 7115 } 7116 7117 if (o == listop) 7118 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */ 7119 7120 o->op_flags |= flags; 7121 o = op_scope(o); 7122 o->op_flags |= OPf_SPECIAL; /* suppress cx_popblock() curpm restoration*/ 7123 return o; 7124 } 7125 7126 /* 7127 =for apidoc Am|OP *|newWHILEOP|I32 flags|I32 debuggable|LOOP *loop|OP *expr|OP *block|OP *cont|I32 has_my 7128 7129 Constructs, checks, and returns an op tree expressing a C<while> loop. 7130 This is a heavyweight loop, with structure that allows exiting the loop 7131 by C<last> and suchlike. 7132 7133 C<loop> is an optional preconstructed C<enterloop> op to use in the 7134 loop; if it is null then a suitable op will be constructed automatically. 7135 C<expr> supplies the loop's controlling expression. C<block> supplies the 7136 main body of the loop, and C<cont> optionally supplies a C<continue> block 7137 that operates as a second half of the body. All of these optree inputs 7138 are consumed by this function and become part of the constructed op tree. 7139 7140 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop> 7141 op and, shifted up eight bits, the eight bits of C<op_private> for 7142 the C<leaveloop> op, except that (in both cases) some bits will be set 7143 automatically. C<debuggable> is currently unused and should always be 1. 7144 C<has_my> can be supplied as true to force the 7145 loop body to be enclosed in its own scope. 7146 7147 =cut 7148 */ 7149 7150 OP * 7151 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, 7152 OP *expr, OP *block, OP *cont, I32 has_my) 7153 { 7154 dVAR; 7155 OP *redo; 7156 OP *next = NULL; 7157 OP *listop; 7158 OP *o; 7159 U8 loopflags = 0; 7160 7161 PERL_UNUSED_ARG(debuggable); 7162 7163 if (expr) { 7164 if (expr->op_type == OP_READLINE 7165 || expr->op_type == OP_READDIR 7166 || expr->op_type == OP_GLOB 7167 || expr->op_type == OP_EACH || expr->op_type == OP_AEACH 7168 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) { 7169 expr = newUNOP(OP_DEFINED, 0, 7170 newASSIGNOP(0, newDEFSVOP(), 0, expr) ); 7171 } else if (expr->op_flags & OPf_KIDS) { 7172 const OP * const k1 = ((UNOP*)expr)->op_first; 7173 const OP * const k2 = (k1) ? OpSIBLING(k1) : NULL; 7174 switch (expr->op_type) { 7175 case OP_NULL: 7176 if (k2 && (k2->op_type == OP_READLINE || k2->op_type == OP_READDIR) 7177 && (k2->op_flags & OPf_STACKED) 7178 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 7179 expr = newUNOP(OP_DEFINED, 0, expr); 7180 break; 7181 7182 case OP_SASSIGN: 7183 if (k1 && (k1->op_type == OP_READDIR 7184 || k1->op_type == OP_GLOB 7185 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB) 7186 || k1->op_type == OP_EACH 7187 || k1->op_type == OP_AEACH)) 7188 expr = newUNOP(OP_DEFINED, 0, expr); 7189 break; 7190 } 7191 } 7192 } 7193 7194 if (!block) 7195 block = newOP(OP_NULL, 0); 7196 else if (cont || has_my) { 7197 block = op_scope(block); 7198 } 7199 7200 if (cont) { 7201 next = LINKLIST(cont); 7202 } 7203 if (expr) { 7204 OP * const unstack = newOP(OP_UNSTACK, 0); 7205 if (!next) 7206 next = unstack; 7207 cont = op_append_elem(OP_LINESEQ, cont, unstack); 7208 } 7209 7210 assert(block); 7211 listop = op_append_list(OP_LINESEQ, block, cont); 7212 assert(listop); 7213 redo = LINKLIST(listop); 7214 7215 if (expr) { 7216 scalar(listop); 7217 o = new_logop(OP_AND, 0, &expr, &listop); 7218 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) { 7219 op_free((OP*)loop); 7220 return expr; /* listop already freed by new_logop */ 7221 } 7222 if (listop) 7223 ((LISTOP*)listop)->op_last->op_next = 7224 (o == listop ? redo : LINKLIST(o)); 7225 } 7226 else 7227 o = listop; 7228 7229 if (!loop) { 7230 NewOp(1101,loop,1,LOOP); 7231 OpTYPE_set(loop, OP_ENTERLOOP); 7232 loop->op_private = 0; 7233 loop->op_next = (OP*)loop; 7234 } 7235 7236 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o); 7237 7238 loop->op_redoop = redo; 7239 loop->op_lastop = o; 7240 o->op_private |= loopflags; 7241 7242 if (next) 7243 loop->op_nextop = next; 7244 else 7245 loop->op_nextop = o; 7246 7247 o->op_flags |= flags; 7248 o->op_private |= (flags >> 8); 7249 return o; 7250 } 7251 7252 /* 7253 =for apidoc Am|OP *|newFOROP|I32 flags|OP *sv|OP *expr|OP *block|OP *cont 7254 7255 Constructs, checks, and returns an op tree expressing a C<foreach> 7256 loop (iteration through a list of values). This is a heavyweight loop, 7257 with structure that allows exiting the loop by C<last> and suchlike. 7258 7259 C<sv> optionally supplies the variable that will be aliased to each 7260 item in turn; if null, it defaults to C<$_>. 7261 C<expr> supplies the list of values to iterate over. C<block> supplies 7262 the main body of the loop, and C<cont> optionally supplies a C<continue> 7263 block that operates as a second half of the body. All of these optree 7264 inputs are consumed by this function and become part of the constructed 7265 op tree. 7266 7267 C<flags> gives the eight bits of C<op_flags> for the C<leaveloop> 7268 op and, shifted up eight bits, the eight bits of C<op_private> for 7269 the C<leaveloop> op, except that (in both cases) some bits will be set 7270 automatically. 7271 7272 =cut 7273 */ 7274 7275 OP * 7276 Perl_newFOROP(pTHX_ I32 flags, OP *sv, OP *expr, OP *block, OP *cont) 7277 { 7278 dVAR; 7279 LOOP *loop; 7280 OP *wop; 7281 PADOFFSET padoff = 0; 7282 I32 iterflags = 0; 7283 I32 iterpflags = 0; 7284 7285 PERL_ARGS_ASSERT_NEWFOROP; 7286 7287 if (sv) { 7288 if (sv->op_type == OP_RV2SV) { /* symbol table variable */ 7289 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */ 7290 OpTYPE_set(sv, OP_RV2GV); 7291 7292 /* The op_type check is needed to prevent a possible segfault 7293 * if the loop variable is undeclared and 'strict vars' is in 7294 * effect. This is illegal but is nonetheless parsed, so we 7295 * may reach this point with an OP_CONST where we're expecting 7296 * an OP_GV. 7297 */ 7298 if (cUNOPx(sv)->op_first->op_type == OP_GV 7299 && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv) 7300 iterpflags |= OPpITER_DEF; 7301 } 7302 else if (sv->op_type == OP_PADSV) { /* private variable */ 7303 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */ 7304 padoff = sv->op_targ; 7305 sv->op_targ = 0; 7306 op_free(sv); 7307 sv = NULL; 7308 PAD_COMPNAME_GEN_set(padoff, PERL_INT_MAX); 7309 } 7310 else if (sv->op_type == OP_NULL && sv->op_targ == OP_SREFGEN) 7311 NOOP; 7312 else 7313 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]); 7314 if (padoff) { 7315 PADNAME * const pn = PAD_COMPNAME(padoff); 7316 const char * const name = PadnamePV(pn); 7317 7318 if (PadnameLEN(pn) == 2 && name[0] == '$' && name[1] == '_') 7319 iterpflags |= OPpITER_DEF; 7320 } 7321 } 7322 else { 7323 sv = newGVOP(OP_GV, 0, PL_defgv); 7324 iterpflags |= OPpITER_DEF; 7325 } 7326 7327 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) { 7328 expr = op_lvalue(force_list(scalar(ref(expr, OP_ITER)), 1), OP_GREPSTART); 7329 iterflags |= OPf_STACKED; 7330 } 7331 else if (expr->op_type == OP_NULL && 7332 (expr->op_flags & OPf_KIDS) && 7333 ((BINOP*)expr)->op_first->op_type == OP_FLOP) 7334 { 7335 /* Basically turn for($x..$y) into the same as for($x,$y), but we 7336 * set the STACKED flag to indicate that these values are to be 7337 * treated as min/max values by 'pp_enteriter'. 7338 */ 7339 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first; 7340 LOGOP* const range = (LOGOP*) flip->op_first; 7341 OP* const left = range->op_first; 7342 OP* const right = OpSIBLING(left); 7343 LISTOP* listop; 7344 7345 range->op_flags &= ~OPf_KIDS; 7346 /* detach range's children */ 7347 op_sibling_splice((OP*)range, NULL, -1, NULL); 7348 7349 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right); 7350 listop->op_first->op_next = range->op_next; 7351 left->op_next = range->op_other; 7352 right->op_next = (OP*)listop; 7353 listop->op_next = listop->op_first; 7354 7355 op_free(expr); 7356 expr = (OP*)(listop); 7357 op_null(expr); 7358 iterflags |= OPf_STACKED; 7359 } 7360 else { 7361 expr = op_lvalue(force_list(expr, 1), OP_GREPSTART); 7362 } 7363 7364 loop = (LOOP*)op_convert_list(OP_ENTERITER, iterflags, 7365 op_append_elem(OP_LIST, list(expr), 7366 scalar(sv))); 7367 assert(!loop->op_next); 7368 /* for my $x () sets OPpLVAL_INTRO; 7369 * for our $x () sets OPpOUR_INTRO */ 7370 loop->op_private = (U8)iterpflags; 7371 if (loop->op_slabbed 7372 && DIFF(loop, OpSLOT(loop)->opslot_next) 7373 < SIZE_TO_PSIZE(sizeof(LOOP))) 7374 { 7375 LOOP *tmp; 7376 NewOp(1234,tmp,1,LOOP); 7377 Copy(loop,tmp,1,LISTOP); 7378 #ifdef PERL_OP_PARENT 7379 assert(loop->op_last->op_sibparent == (OP*)loop); 7380 OpLASTSIB_set(loop->op_last, (OP*)tmp); /*point back to new parent */ 7381 #endif 7382 S_op_destroy(aTHX_ (OP*)loop); 7383 loop = tmp; 7384 } 7385 else if (!loop->op_slabbed) 7386 { 7387 loop = (LOOP*)PerlMemShared_realloc(loop, sizeof(LOOP)); 7388 #ifdef PERL_OP_PARENT 7389 OpLASTSIB_set(loop->op_last, (OP*)loop); 7390 #endif 7391 } 7392 loop->op_targ = padoff; 7393 wop = newWHILEOP(flags, 1, loop, newOP(OP_ITER, 0), block, cont, 0); 7394 return wop; 7395 } 7396 7397 /* 7398 =for apidoc Am|OP *|newLOOPEX|I32 type|OP *label 7399 7400 Constructs, checks, and returns a loop-exiting op (such as C<goto> 7401 or C<last>). C<type> is the opcode. C<label> supplies the parameter 7402 determining the target of the op; it is consumed by this function and 7403 becomes part of the constructed op tree. 7404 7405 =cut 7406 */ 7407 7408 OP* 7409 Perl_newLOOPEX(pTHX_ I32 type, OP *label) 7410 { 7411 OP *o = NULL; 7412 7413 PERL_ARGS_ASSERT_NEWLOOPEX; 7414 7415 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP 7416 || type == OP_CUSTOM); 7417 7418 if (type != OP_GOTO) { 7419 /* "last()" means "last" */ 7420 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) { 7421 o = newOP(type, OPf_SPECIAL); 7422 } 7423 } 7424 else { 7425 /* Check whether it's going to be a goto &function */ 7426 if (label->op_type == OP_ENTERSUB 7427 && !(label->op_flags & OPf_STACKED)) 7428 label = newUNOP(OP_REFGEN, 0, op_lvalue(label, OP_REFGEN)); 7429 } 7430 7431 /* Check for a constant argument */ 7432 if (label->op_type == OP_CONST) { 7433 SV * const sv = ((SVOP *)label)->op_sv; 7434 STRLEN l; 7435 const char *s = SvPV_const(sv,l); 7436 if (l == strlen(s)) { 7437 o = newPVOP(type, 7438 SvUTF8(((SVOP*)label)->op_sv), 7439 savesharedpv( 7440 SvPV_nolen_const(((SVOP*)label)->op_sv))); 7441 } 7442 } 7443 7444 /* If we have already created an op, we do not need the label. */ 7445 if (o) 7446 op_free(label); 7447 else o = newUNOP(type, OPf_STACKED, label); 7448 7449 PL_hints |= HINT_BLOCK_SCOPE; 7450 return o; 7451 } 7452 7453 /* if the condition is a literal array or hash 7454 (or @{ ... } etc), make a reference to it. 7455 */ 7456 STATIC OP * 7457 S_ref_array_or_hash(pTHX_ OP *cond) 7458 { 7459 if (cond 7460 && (cond->op_type == OP_RV2AV 7461 || cond->op_type == OP_PADAV 7462 || cond->op_type == OP_RV2HV 7463 || cond->op_type == OP_PADHV)) 7464 7465 return newUNOP(OP_REFGEN, 0, op_lvalue(cond, OP_REFGEN)); 7466 7467 else if(cond 7468 && (cond->op_type == OP_ASLICE 7469 || cond->op_type == OP_KVASLICE 7470 || cond->op_type == OP_HSLICE 7471 || cond->op_type == OP_KVHSLICE)) { 7472 7473 /* anonlist now needs a list from this op, was previously used in 7474 * scalar context */ 7475 cond->op_flags &= ~(OPf_WANT_SCALAR | OPf_REF); 7476 cond->op_flags |= OPf_WANT_LIST; 7477 7478 return newANONLIST(op_lvalue(cond, OP_ANONLIST)); 7479 } 7480 7481 else 7482 return cond; 7483 } 7484 7485 /* These construct the optree fragments representing given() 7486 and when() blocks. 7487 7488 entergiven and enterwhen are LOGOPs; the op_other pointer 7489 points up to the associated leave op. We need this so we 7490 can put it in the context and make break/continue work. 7491 (Also, of course, pp_enterwhen will jump straight to 7492 op_other if the match fails.) 7493 */ 7494 7495 STATIC OP * 7496 S_newGIVWHENOP(pTHX_ OP *cond, OP *block, 7497 I32 enter_opcode, I32 leave_opcode, 7498 PADOFFSET entertarg) 7499 { 7500 dVAR; 7501 LOGOP *enterop; 7502 OP *o; 7503 7504 PERL_ARGS_ASSERT_NEWGIVWHENOP; 7505 PERL_UNUSED_ARG(entertarg); /* used to indicate targ of lexical $_ */ 7506 7507 enterop = S_alloc_LOGOP(aTHX_ enter_opcode, block, NULL); 7508 enterop->op_targ = 0; 7509 enterop->op_private = 0; 7510 7511 o = newUNOP(leave_opcode, 0, (OP *) enterop); 7512 7513 if (cond) { 7514 /* prepend cond if we have one */ 7515 op_sibling_splice((OP*)enterop, NULL, 0, scalar(cond)); 7516 7517 o->op_next = LINKLIST(cond); 7518 cond->op_next = (OP *) enterop; 7519 } 7520 else { 7521 /* This is a default {} block */ 7522 enterop->op_flags |= OPf_SPECIAL; 7523 o ->op_flags |= OPf_SPECIAL; 7524 7525 o->op_next = (OP *) enterop; 7526 } 7527 7528 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since 7529 entergiven and enterwhen both 7530 use ck_null() */ 7531 7532 enterop->op_next = LINKLIST(block); 7533 block->op_next = enterop->op_other = o; 7534 7535 return o; 7536 } 7537 7538 /* Does this look like a boolean operation? For these purposes 7539 a boolean operation is: 7540 - a subroutine call [*] 7541 - a logical connective 7542 - a comparison operator 7543 - a filetest operator, with the exception of -s -M -A -C 7544 - defined(), exists() or eof() 7545 - /$re/ or $foo =~ /$re/ 7546 7547 [*] possibly surprising 7548 */ 7549 STATIC bool 7550 S_looks_like_bool(pTHX_ const OP *o) 7551 { 7552 PERL_ARGS_ASSERT_LOOKS_LIKE_BOOL; 7553 7554 switch(o->op_type) { 7555 case OP_OR: 7556 case OP_DOR: 7557 return looks_like_bool(cLOGOPo->op_first); 7558 7559 case OP_AND: 7560 { 7561 OP* sibl = OpSIBLING(cLOGOPo->op_first); 7562 ASSUME(sibl); 7563 return ( 7564 looks_like_bool(cLOGOPo->op_first) 7565 && looks_like_bool(sibl)); 7566 } 7567 7568 case OP_NULL: 7569 case OP_SCALAR: 7570 return ( 7571 o->op_flags & OPf_KIDS 7572 && looks_like_bool(cUNOPo->op_first)); 7573 7574 case OP_ENTERSUB: 7575 7576 case OP_NOT: case OP_XOR: 7577 7578 case OP_EQ: case OP_NE: case OP_LT: 7579 case OP_GT: case OP_LE: case OP_GE: 7580 7581 case OP_I_EQ: case OP_I_NE: case OP_I_LT: 7582 case OP_I_GT: case OP_I_LE: case OP_I_GE: 7583 7584 case OP_SEQ: case OP_SNE: case OP_SLT: 7585 case OP_SGT: case OP_SLE: case OP_SGE: 7586 7587 case OP_SMARTMATCH: 7588 7589 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC: 7590 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC: 7591 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED: 7592 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR: 7593 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR: 7594 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID: 7595 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY: 7596 case OP_FTTEXT: case OP_FTBINARY: 7597 7598 case OP_DEFINED: case OP_EXISTS: 7599 case OP_MATCH: case OP_EOF: 7600 7601 case OP_FLOP: 7602 7603 return TRUE; 7604 7605 case OP_CONST: 7606 /* Detect comparisons that have been optimized away */ 7607 if (cSVOPo->op_sv == &PL_sv_yes 7608 || cSVOPo->op_sv == &PL_sv_no) 7609 7610 return TRUE; 7611 else 7612 return FALSE; 7613 7614 /* FALLTHROUGH */ 7615 default: 7616 return FALSE; 7617 } 7618 } 7619 7620 /* 7621 =for apidoc Am|OP *|newGIVENOP|OP *cond|OP *block|PADOFFSET defsv_off 7622 7623 Constructs, checks, and returns an op tree expressing a C<given> block. 7624 C<cond> supplies the expression that will be locally assigned to a lexical 7625 variable, and C<block> supplies the body of the C<given> construct; they 7626 are consumed by this function and become part of the constructed op tree. 7627 C<defsv_off> must be zero (it used to identity the pad slot of lexical $_). 7628 7629 =cut 7630 */ 7631 7632 OP * 7633 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off) 7634 { 7635 PERL_ARGS_ASSERT_NEWGIVENOP; 7636 PERL_UNUSED_ARG(defsv_off); 7637 7638 assert(!defsv_off); 7639 return newGIVWHENOP( 7640 ref_array_or_hash(cond), 7641 block, 7642 OP_ENTERGIVEN, OP_LEAVEGIVEN, 7643 0); 7644 } 7645 7646 /* 7647 =for apidoc Am|OP *|newWHENOP|OP *cond|OP *block 7648 7649 Constructs, checks, and returns an op tree expressing a C<when> block. 7650 C<cond> supplies the test expression, and C<block> supplies the block 7651 that will be executed if the test evaluates to true; they are consumed 7652 by this function and become part of the constructed op tree. C<cond> 7653 will be interpreted DWIMically, often as a comparison against C<$_>, 7654 and may be null to generate a C<default> block. 7655 7656 =cut 7657 */ 7658 7659 OP * 7660 Perl_newWHENOP(pTHX_ OP *cond, OP *block) 7661 { 7662 const bool cond_llb = (!cond || looks_like_bool(cond)); 7663 OP *cond_op; 7664 7665 PERL_ARGS_ASSERT_NEWWHENOP; 7666 7667 if (cond_llb) 7668 cond_op = cond; 7669 else { 7670 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL, 7671 newDEFSVOP(), 7672 scalar(ref_array_or_hash(cond))); 7673 } 7674 7675 return newGIVWHENOP(cond_op, block, OP_ENTERWHEN, OP_LEAVEWHEN, 0); 7676 } 7677 7678 /* must not conflict with SVf_UTF8 */ 7679 #define CV_CKPROTO_CURSTASH 0x1 7680 7681 void 7682 Perl_cv_ckproto_len_flags(pTHX_ const CV *cv, const GV *gv, const char *p, 7683 const STRLEN len, const U32 flags) 7684 { 7685 SV *name = NULL, *msg; 7686 const char * cvp = SvROK(cv) 7687 ? SvTYPE(SvRV_const(cv)) == SVt_PVCV 7688 ? (cv = (const CV *)SvRV_const(cv), CvPROTO(cv)) 7689 : "" 7690 : CvPROTO(cv); 7691 STRLEN clen = CvPROTOLEN(cv), plen = len; 7692 7693 PERL_ARGS_ASSERT_CV_CKPROTO_LEN_FLAGS; 7694 7695 if (p == NULL && cvp == NULL) 7696 return; 7697 7698 if (!ckWARN_d(WARN_PROTOTYPE)) 7699 return; 7700 7701 if (p && cvp) { 7702 p = S_strip_spaces(aTHX_ p, &plen); 7703 cvp = S_strip_spaces(aTHX_ cvp, &clen); 7704 if ((flags & SVf_UTF8) == SvUTF8(cv)) { 7705 if (plen == clen && memEQ(cvp, p, plen)) 7706 return; 7707 } else { 7708 if (flags & SVf_UTF8) { 7709 if (bytes_cmp_utf8((const U8 *)cvp, clen, (const U8 *)p, plen) == 0) 7710 return; 7711 } 7712 else { 7713 if (bytes_cmp_utf8((const U8 *)p, plen, (const U8 *)cvp, clen) == 0) 7714 return; 7715 } 7716 } 7717 } 7718 7719 msg = sv_newmortal(); 7720 7721 if (gv) 7722 { 7723 if (isGV(gv)) 7724 gv_efullname3(name = sv_newmortal(), gv, NULL); 7725 else if (SvPOK(gv) && *SvPVX((SV *)gv) == '&') 7726 name = newSVpvn_flags(SvPVX((SV *)gv)+1, SvCUR(gv)-1, SvUTF8(gv)|SVs_TEMP); 7727 else if (flags & CV_CKPROTO_CURSTASH || SvROK(gv)) { 7728 name = sv_2mortal(newSVhek(HvNAME_HEK(PL_curstash))); 7729 sv_catpvs(name, "::"); 7730 if (SvROK(gv)) { 7731 assert (SvTYPE(SvRV_const(gv)) == SVt_PVCV); 7732 assert (CvNAMED(SvRV_const(gv))); 7733 sv_cathek(name, CvNAME_HEK(MUTABLE_CV(SvRV_const(gv)))); 7734 } 7735 else sv_catsv(name, (SV *)gv); 7736 } 7737 else name = (SV *)gv; 7738 } 7739 sv_setpvs(msg, "Prototype mismatch:"); 7740 if (name) 7741 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, SVfARG(name)); 7742 if (cvp) 7743 Perl_sv_catpvf(aTHX_ msg, " (%"UTF8f")", 7744 UTF8fARG(SvUTF8(cv),clen,cvp) 7745 ); 7746 else 7747 sv_catpvs(msg, ": none"); 7748 sv_catpvs(msg, " vs "); 7749 if (p) 7750 Perl_sv_catpvf(aTHX_ msg, "(%"UTF8f")", UTF8fARG(flags & SVf_UTF8,len,p)); 7751 else 7752 sv_catpvs(msg, "none"); 7753 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, SVfARG(msg)); 7754 } 7755 7756 static void const_sv_xsub(pTHX_ CV* cv); 7757 static void const_av_xsub(pTHX_ CV* cv); 7758 7759 /* 7760 7761 =head1 Optree Manipulation Functions 7762 7763 =for apidoc cv_const_sv 7764 7765 If C<cv> is a constant sub eligible for inlining, returns the constant 7766 value returned by the sub. Otherwise, returns C<NULL>. 7767 7768 Constant subs can be created with C<newCONSTSUB> or as described in 7769 L<perlsub/"Constant Functions">. 7770 7771 =cut 7772 */ 7773 SV * 7774 Perl_cv_const_sv(const CV *const cv) 7775 { 7776 SV *sv; 7777 if (!cv) 7778 return NULL; 7779 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM)) 7780 return NULL; 7781 sv = CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL; 7782 if (sv && SvTYPE(sv) == SVt_PVAV) return NULL; 7783 return sv; 7784 } 7785 7786 SV * 7787 Perl_cv_const_sv_or_av(const CV * const cv) 7788 { 7789 if (!cv) 7790 return NULL; 7791 if (SvROK(cv)) return SvRV((SV *)cv); 7792 assert (SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM); 7793 return CvCONST(cv) ? MUTABLE_SV(CvXSUBANY(cv).any_ptr) : NULL; 7794 } 7795 7796 /* op_const_sv: examine an optree to determine whether it's in-lineable. 7797 * Can be called in 2 ways: 7798 * 7799 * !allow_lex 7800 * look for a single OP_CONST with attached value: return the value 7801 * 7802 * allow_lex && !CvCONST(cv); 7803 * 7804 * examine the clone prototype, and if contains only a single 7805 * OP_CONST, return the value; or if it contains a single PADSV ref- 7806 * erencing an outer lexical, turn on CvCONST to indicate the CV is 7807 * a candidate for "constizing" at clone time, and return NULL. 7808 */ 7809 7810 static SV * 7811 S_op_const_sv(pTHX_ const OP *o, CV *cv, bool allow_lex) 7812 { 7813 SV *sv = NULL; 7814 bool padsv = FALSE; 7815 7816 assert(o); 7817 assert(cv); 7818 7819 for (; o; o = o->op_next) { 7820 const OPCODE type = o->op_type; 7821 7822 if (type == OP_NEXTSTATE || type == OP_LINESEQ 7823 || type == OP_NULL 7824 || type == OP_PUSHMARK) 7825 continue; 7826 if (type == OP_DBSTATE) 7827 continue; 7828 if (type == OP_LEAVESUB) 7829 break; 7830 if (sv) 7831 return NULL; 7832 if (type == OP_CONST && cSVOPo->op_sv) 7833 sv = cSVOPo->op_sv; 7834 else if (type == OP_UNDEF && !o->op_private) { 7835 sv = newSV(0); 7836 SAVEFREESV(sv); 7837 } 7838 else if (allow_lex && type == OP_PADSV) { 7839 if (PAD_COMPNAME_FLAGS(o->op_targ) & PADNAMEt_OUTER) 7840 { 7841 sv = &PL_sv_undef; /* an arbitrary non-null value */ 7842 padsv = TRUE; 7843 } 7844 else 7845 return NULL; 7846 } 7847 else { 7848 return NULL; 7849 } 7850 } 7851 if (padsv) { 7852 CvCONST_on(cv); 7853 return NULL; 7854 } 7855 return sv; 7856 } 7857 7858 static bool 7859 S_already_defined(pTHX_ CV *const cv, OP * const block, OP * const o, 7860 PADNAME * const name, SV ** const const_svp) 7861 { 7862 assert (cv); 7863 assert (o || name); 7864 assert (const_svp); 7865 if ((!block 7866 )) { 7867 if (CvFLAGS(PL_compcv)) { 7868 /* might have had built-in attrs applied */ 7869 const bool pureperl = !CvISXSUB(cv) && CvROOT(cv); 7870 if (CvLVALUE(PL_compcv) && ! CvLVALUE(cv) && pureperl 7871 && ckWARN(WARN_MISC)) 7872 { 7873 /* protect against fatal warnings leaking compcv */ 7874 SAVEFREESV(PL_compcv); 7875 Perl_warner(aTHX_ packWARN(WARN_MISC), "lvalue attribute ignored after the subroutine has been defined"); 7876 SvREFCNT_inc_simple_void_NN(PL_compcv); 7877 } 7878 CvFLAGS(cv) |= 7879 (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS 7880 & ~(CVf_LVALUE * pureperl)); 7881 } 7882 return FALSE; 7883 } 7884 7885 /* redundant check for speed: */ 7886 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) { 7887 const line_t oldline = CopLINE(PL_curcop); 7888 SV *namesv = o 7889 ? cSVOPo->op_sv 7890 : sv_2mortal(newSVpvn_utf8( 7891 PadnamePV(name)+1,PadnameLEN(name)-1, PadnameUTF8(name) 7892 )); 7893 if (PL_parser && PL_parser->copline != NOLINE) 7894 /* This ensures that warnings are reported at the first 7895 line of a redefinition, not the last. */ 7896 CopLINE_set(PL_curcop, PL_parser->copline); 7897 /* protect against fatal warnings leaking compcv */ 7898 SAVEFREESV(PL_compcv); 7899 report_redefined_cv(namesv, cv, const_svp); 7900 SvREFCNT_inc_simple_void_NN(PL_compcv); 7901 CopLINE_set(PL_curcop, oldline); 7902 } 7903 SAVEFREESV(cv); 7904 return TRUE; 7905 } 7906 7907 CV * 7908 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block) 7909 { 7910 CV **spot; 7911 SV **svspot; 7912 const char *ps; 7913 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */ 7914 U32 ps_utf8 = 0; 7915 CV *cv = NULL; 7916 CV *compcv = PL_compcv; 7917 SV *const_sv; 7918 PADNAME *name; 7919 PADOFFSET pax = o->op_targ; 7920 CV *outcv = CvOUTSIDE(PL_compcv); 7921 CV *clonee = NULL; 7922 HEK *hek = NULL; 7923 bool reusable = FALSE; 7924 OP *start = NULL; 7925 #ifdef PERL_DEBUG_READONLY_OPS 7926 OPSLAB *slab = NULL; 7927 #endif 7928 7929 PERL_ARGS_ASSERT_NEWMYSUB; 7930 7931 /* Find the pad slot for storing the new sub. 7932 We cannot use PL_comppad, as it is the pad owned by the new sub. We 7933 need to look in CvOUTSIDE and find the pad belonging to the enclos- 7934 ing sub. And then we need to dig deeper if this is a lexical from 7935 outside, as in: 7936 my sub foo; sub { sub foo { } } 7937 */ 7938 redo: 7939 name = PadlistNAMESARRAY(CvPADLIST(outcv))[pax]; 7940 if (PadnameOUTER(name) && PARENT_PAD_INDEX(name)) { 7941 pax = PARENT_PAD_INDEX(name); 7942 outcv = CvOUTSIDE(outcv); 7943 assert(outcv); 7944 goto redo; 7945 } 7946 svspot = 7947 &PadARRAY(PadlistARRAY(CvPADLIST(outcv)) 7948 [CvDEPTH(outcv) ? CvDEPTH(outcv) : 1])[pax]; 7949 spot = (CV **)svspot; 7950 7951 if (!(PL_parser && PL_parser->error_count)) 7952 move_proto_attr(&proto, &attrs, (GV *)PadnameSV(name)); 7953 7954 if (proto) { 7955 assert(proto->op_type == OP_CONST); 7956 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len); 7957 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv); 7958 } 7959 else 7960 ps = NULL; 7961 7962 if (proto) 7963 SAVEFREEOP(proto); 7964 if (attrs) 7965 SAVEFREEOP(attrs); 7966 7967 if (PL_parser && PL_parser->error_count) { 7968 op_free(block); 7969 SvREFCNT_dec(PL_compcv); 7970 PL_compcv = 0; 7971 goto done; 7972 } 7973 7974 if (CvDEPTH(outcv) && CvCLONE(compcv)) { 7975 cv = *spot; 7976 svspot = (SV **)(spot = &clonee); 7977 } 7978 else if (PadnameIsSTATE(name) || CvDEPTH(outcv)) 7979 cv = *spot; 7980 else { 7981 assert (SvTYPE(*spot) == SVt_PVCV); 7982 if (CvNAMED(*spot)) 7983 hek = CvNAME_HEK(*spot); 7984 else { 7985 dVAR; 7986 U32 hash; 7987 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1); 7988 CvNAME_HEK_set(*spot, hek = 7989 share_hek( 7990 PadnamePV(name)+1, 7991 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1), 7992 hash 7993 ) 7994 ); 7995 CvLEXICAL_on(*spot); 7996 } 7997 cv = PadnamePROTOCV(name); 7998 svspot = (SV **)(spot = &PadnamePROTOCV(name)); 7999 } 8000 8001 if (block) { 8002 /* This makes sub {}; work as expected. */ 8003 if (block->op_type == OP_STUB) { 8004 const line_t l = PL_parser->copline; 8005 op_free(block); 8006 block = newSTATEOP(0, NULL, 0); 8007 PL_parser->copline = l; 8008 } 8009 block = CvLVALUE(compcv) 8010 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv)) 8011 ? newUNOP(OP_LEAVESUBLV, 0, 8012 op_lvalue(scalarseq(block), OP_LEAVESUBLV)) 8013 : newUNOP(OP_LEAVESUB, 0, scalarseq(block)); 8014 start = LINKLIST(block); 8015 block->op_next = 0; 8016 if (ps && !*ps && !attrs && !CvLVALUE(compcv)) 8017 const_sv = S_op_const_sv(aTHX_ start, compcv, FALSE); 8018 else 8019 const_sv = NULL; 8020 } 8021 else 8022 const_sv = NULL; 8023 8024 if (cv) { 8025 const bool exists = CvROOT(cv) || CvXSUB(cv); 8026 8027 /* if the subroutine doesn't exist and wasn't pre-declared 8028 * with a prototype, assume it will be AUTOLOADed, 8029 * skipping the prototype check 8030 */ 8031 if (exists || SvPOK(cv)) 8032 cv_ckproto_len_flags(cv, (GV *)PadnameSV(name), ps, ps_len, 8033 ps_utf8); 8034 /* already defined? */ 8035 if (exists) { 8036 if (S_already_defined(aTHX_ cv,block,NULL,name,&const_sv)) 8037 cv = NULL; 8038 else { 8039 if (attrs) goto attrs; 8040 /* just a "sub foo;" when &foo is already defined */ 8041 SAVEFREESV(compcv); 8042 goto done; 8043 } 8044 } 8045 else if (CvDEPTH(outcv) && CvCLONE(compcv)) { 8046 cv = NULL; 8047 reusable = TRUE; 8048 } 8049 } 8050 if (const_sv) { 8051 SvREFCNT_inc_simple_void_NN(const_sv); 8052 SvFLAGS(const_sv) |= SVs_PADTMP; 8053 if (cv) { 8054 assert(!CvROOT(cv) && !CvCONST(cv)); 8055 cv_forget_slab(cv); 8056 } 8057 else { 8058 cv = MUTABLE_CV(newSV_type(SVt_PVCV)); 8059 CvFILE_set_from_cop(cv, PL_curcop); 8060 CvSTASH_set(cv, PL_curstash); 8061 *spot = cv; 8062 } 8063 sv_setpvs(MUTABLE_SV(cv), ""); /* prototype is "" */ 8064 CvXSUBANY(cv).any_ptr = const_sv; 8065 CvXSUB(cv) = const_sv_xsub; 8066 CvCONST_on(cv); 8067 CvISXSUB_on(cv); 8068 PoisonPADLIST(cv); 8069 CvFLAGS(cv) |= CvMETHOD(compcv); 8070 op_free(block); 8071 SvREFCNT_dec(compcv); 8072 PL_compcv = NULL; 8073 goto setname; 8074 } 8075 /* Checking whether outcv is CvOUTSIDE(compcv) is not sufficient to 8076 determine whether this sub definition is in the same scope as its 8077 declaration. If this sub definition is inside an inner named pack- 8078 age sub (my sub foo; sub bar { sub foo { ... } }), outcv points to 8079 the package sub. So check PadnameOUTER(name) too. 8080 */ 8081 if (outcv == CvOUTSIDE(compcv) && !PadnameOUTER(name)) { 8082 assert(!CvWEAKOUTSIDE(compcv)); 8083 SvREFCNT_dec(CvOUTSIDE(compcv)); 8084 CvWEAKOUTSIDE_on(compcv); 8085 } 8086 /* XXX else do we have a circular reference? */ 8087 if (cv) { /* must reuse cv in case stub is referenced elsewhere */ 8088 /* transfer PL_compcv to cv */ 8089 if (block 8090 ) { 8091 cv_flags_t preserved_flags = 8092 CvFLAGS(cv) & (CVf_BUILTIN_ATTRS|CVf_NAMED); 8093 PADLIST *const temp_padl = CvPADLIST(cv); 8094 CV *const temp_cv = CvOUTSIDE(cv); 8095 const cv_flags_t other_flags = 8096 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE); 8097 OP * const cvstart = CvSTART(cv); 8098 8099 SvPOK_off(cv); 8100 CvFLAGS(cv) = 8101 CvFLAGS(compcv) | preserved_flags; 8102 CvOUTSIDE(cv) = CvOUTSIDE(compcv); 8103 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(compcv); 8104 CvPADLIST_set(cv, CvPADLIST(compcv)); 8105 CvOUTSIDE(compcv) = temp_cv; 8106 CvPADLIST_set(compcv, temp_padl); 8107 CvSTART(cv) = CvSTART(compcv); 8108 CvSTART(compcv) = cvstart; 8109 CvFLAGS(compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE); 8110 CvFLAGS(compcv) |= other_flags; 8111 8112 if (CvFILE(cv) && CvDYNFILE(cv)) { 8113 Safefree(CvFILE(cv)); 8114 } 8115 8116 /* inner references to compcv must be fixed up ... */ 8117 pad_fixup_inner_anons(CvPADLIST(cv), compcv, cv); 8118 if (PERLDB_INTER)/* Advice debugger on the new sub. */ 8119 ++PL_sub_generation; 8120 } 8121 else { 8122 /* Might have had built-in attributes applied -- propagate them. */ 8123 CvFLAGS(cv) |= (CvFLAGS(compcv) & CVf_BUILTIN_ATTRS); 8124 } 8125 /* ... before we throw it away */ 8126 SvREFCNT_dec(compcv); 8127 PL_compcv = compcv = cv; 8128 } 8129 else { 8130 cv = compcv; 8131 *spot = cv; 8132 } 8133 setname: 8134 CvLEXICAL_on(cv); 8135 if (!CvNAME_HEK(cv)) { 8136 if (hek) (void)share_hek_hek(hek); 8137 else { 8138 dVAR; 8139 U32 hash; 8140 PERL_HASH(hash, PadnamePV(name)+1, PadnameLEN(name)-1); 8141 hek = share_hek(PadnamePV(name)+1, 8142 (PadnameLEN(name)-1) * (PadnameUTF8(name) ? -1 : 1), 8143 hash); 8144 } 8145 CvNAME_HEK_set(cv, hek); 8146 } 8147 if (const_sv) goto clone; 8148 8149 CvFILE_set_from_cop(cv, PL_curcop); 8150 CvSTASH_set(cv, PL_curstash); 8151 8152 if (ps) { 8153 sv_setpvn(MUTABLE_SV(cv), ps, ps_len); 8154 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv)); 8155 } 8156 8157 if (!block) 8158 goto attrs; 8159 8160 /* If we assign an optree to a PVCV, then we've defined a subroutine that 8161 the debugger could be able to set a breakpoint in, so signal to 8162 pp_entereval that it should not throw away any saved lines at scope 8163 exit. */ 8164 8165 PL_breakable_sub_gen++; 8166 CvROOT(cv) = block; 8167 CvROOT(cv)->op_private |= OPpREFCOUNTED; 8168 OpREFCNT_set(CvROOT(cv), 1); 8169 /* The cv no longer needs to hold a refcount on the slab, as CvROOT 8170 itself has a refcount. */ 8171 CvSLABBED_off(cv); 8172 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv)); 8173 #ifdef PERL_DEBUG_READONLY_OPS 8174 slab = (OPSLAB *)CvSTART(cv); 8175 #endif 8176 CvSTART(cv) = start; 8177 CALL_PEEP(start); 8178 finalize_optree(CvROOT(cv)); 8179 S_prune_chain_head(&CvSTART(cv)); 8180 8181 /* now that optimizer has done its work, adjust pad values */ 8182 8183 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB); 8184 8185 attrs: 8186 if (attrs) { 8187 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */ 8188 apply_attrs(PL_curstash, MUTABLE_SV(cv), attrs); 8189 } 8190 8191 if (block) { 8192 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) { 8193 SV * const tmpstr = sv_newmortal(); 8194 GV * const db_postponed = gv_fetchpvs("DB::postponed", 8195 GV_ADDMULTI, SVt_PVHV); 8196 HV *hv; 8197 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld", 8198 CopFILE(PL_curcop), 8199 (long)PL_subline, 8200 (long)CopLINE(PL_curcop)); 8201 if (HvNAME_HEK(PL_curstash)) { 8202 sv_sethek(tmpstr, HvNAME_HEK(PL_curstash)); 8203 sv_catpvs(tmpstr, "::"); 8204 } 8205 else sv_setpvs(tmpstr, "__ANON__::"); 8206 sv_catpvn_flags(tmpstr, PadnamePV(name)+1, PadnameLEN(name)-1, 8207 PadnameUTF8(name) ? SV_CATUTF8 : SV_CATBYTES); 8208 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr), 8209 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0); 8210 hv = GvHVn(db_postponed); 8211 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) { 8212 CV * const pcv = GvCV(db_postponed); 8213 if (pcv) { 8214 dSP; 8215 PUSHMARK(SP); 8216 XPUSHs(tmpstr); 8217 PUTBACK; 8218 call_sv(MUTABLE_SV(pcv), G_DISCARD); 8219 } 8220 } 8221 } 8222 } 8223 8224 clone: 8225 if (clonee) { 8226 assert(CvDEPTH(outcv)); 8227 spot = (CV **) 8228 &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[CvDEPTH(outcv)])[pax]; 8229 if (reusable) cv_clone_into(clonee, *spot); 8230 else *spot = cv_clone(clonee); 8231 SvREFCNT_dec_NN(clonee); 8232 cv = *spot; 8233 } 8234 if (CvDEPTH(outcv) && !reusable && PadnameIsSTATE(name)) { 8235 PADOFFSET depth = CvDEPTH(outcv); 8236 while (--depth) { 8237 SV *oldcv; 8238 svspot = &PadARRAY(PadlistARRAY(CvPADLIST(outcv))[depth])[pax]; 8239 oldcv = *svspot; 8240 *svspot = SvREFCNT_inc_simple_NN(cv); 8241 SvREFCNT_dec(oldcv); 8242 } 8243 } 8244 8245 done: 8246 if (PL_parser) 8247 PL_parser->copline = NOLINE; 8248 LEAVE_SCOPE(floor); 8249 #ifdef PERL_DEBUG_READONLY_OPS 8250 if (slab) 8251 Slab_to_ro(slab); 8252 #endif 8253 op_free(o); 8254 return cv; 8255 } 8256 8257 /* _x = extended */ 8258 CV * 8259 Perl_newATTRSUB_x(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, 8260 OP *block, bool o_is_gv) 8261 { 8262 GV *gv; 8263 const char *ps; 8264 STRLEN ps_len = 0; /* init it to avoid false uninit warning from icc */ 8265 U32 ps_utf8 = 0; 8266 CV *cv = NULL; 8267 SV *const_sv; 8268 const bool ec = PL_parser && PL_parser->error_count; 8269 /* If the subroutine has no body, no attributes, and no builtin attributes 8270 then it's just a sub declaration, and we may be able to get away with 8271 storing with a placeholder scalar in the symbol table, rather than a 8272 full CV. If anything is present then it will take a full CV to 8273 store it. */ 8274 const I32 gv_fetch_flags 8275 = ec ? GV_NOADD_NOINIT : 8276 (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)) 8277 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT; 8278 STRLEN namlen = 0; 8279 const char * const name = 8280 o ? SvPV_const(o_is_gv ? (SV *)o : cSVOPo->op_sv, namlen) : NULL; 8281 bool has_name; 8282 bool name_is_utf8 = o && !o_is_gv && SvUTF8(cSVOPo->op_sv); 8283 bool evanescent = FALSE; 8284 OP *start = NULL; 8285 #ifdef PERL_DEBUG_READONLY_OPS 8286 OPSLAB *slab = NULL; 8287 #endif 8288 8289 if (o_is_gv) { 8290 gv = (GV*)o; 8291 o = NULL; 8292 has_name = TRUE; 8293 } else if (name) { 8294 /* Try to optimise and avoid creating a GV. Instead, the CV’s name 8295 hek and CvSTASH pointer together can imply the GV. If the name 8296 contains a package name, then GvSTASH(CvGV(cv)) may differ from 8297 CvSTASH, so forego the optimisation if we find any. 8298 Also, we may be called from load_module at run time, so 8299 PL_curstash (which sets CvSTASH) may not point to the stash the 8300 sub is stored in. */ 8301 const I32 flags = 8302 ec ? GV_NOADD_NOINIT 8303 : PL_curstash != CopSTASH(PL_curcop) 8304 || memchr(name, ':', namlen) || memchr(name, '\'', namlen) 8305 ? gv_fetch_flags 8306 : GV_ADDMULTI | GV_NOINIT | GV_NOTQUAL; 8307 gv = gv_fetchsv(cSVOPo->op_sv, flags, SVt_PVCV); 8308 has_name = TRUE; 8309 } else if (PERLDB_NAMEANON && CopLINE(PL_curcop)) { 8310 SV * const sv = sv_newmortal(); 8311 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]", 8312 PL_curstash ? "__ANON__" : "__ANON__::__ANON__", 8313 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop)); 8314 gv = gv_fetchsv(sv, gv_fetch_flags, SVt_PVCV); 8315 has_name = TRUE; 8316 } else if (PL_curstash) { 8317 gv = gv_fetchpvs("__ANON__", gv_fetch_flags, SVt_PVCV); 8318 has_name = FALSE; 8319 } else { 8320 gv = gv_fetchpvs("__ANON__::__ANON__", gv_fetch_flags, SVt_PVCV); 8321 has_name = FALSE; 8322 } 8323 if (!ec) { 8324 if (isGV(gv)) { 8325 move_proto_attr(&proto, &attrs, gv); 8326 } else { 8327 assert(cSVOPo); 8328 move_proto_attr(&proto, &attrs, (GV *)cSVOPo->op_sv); 8329 } 8330 } 8331 8332 if (proto) { 8333 assert(proto->op_type == OP_CONST); 8334 ps = SvPV_const(((SVOP*)proto)->op_sv, ps_len); 8335 ps_utf8 = SvUTF8(((SVOP*)proto)->op_sv); 8336 } 8337 else 8338 ps = NULL; 8339 8340 if (o) 8341 SAVEFREEOP(o); 8342 if (proto) 8343 SAVEFREEOP(proto); 8344 if (attrs) 8345 SAVEFREEOP(attrs); 8346 8347 if (ec) { 8348 op_free(block); 8349 if (name) SvREFCNT_dec(PL_compcv); 8350 else cv = PL_compcv; 8351 PL_compcv = 0; 8352 if (name && block) { 8353 const char *s = strrchr(name, ':'); 8354 s = s ? s+1 : name; 8355 if (strEQ(s, "BEGIN")) { 8356 if (PL_in_eval & EVAL_KEEPERR) 8357 Perl_croak_nocontext("BEGIN not safe after errors--compilation aborted"); 8358 else { 8359 SV * const errsv = ERRSV; 8360 /* force display of errors found but not reported */ 8361 sv_catpvs(errsv, "BEGIN not safe after errors--compilation aborted"); 8362 Perl_croak_nocontext("%"SVf, SVfARG(errsv)); 8363 } 8364 } 8365 } 8366 goto done; 8367 } 8368 8369 if (!block && SvTYPE(gv) != SVt_PVGV) { 8370 /* If we are not defining a new sub and the existing one is not a 8371 full GV + CV... */ 8372 if (attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS)) { 8373 /* We are applying attributes to an existing sub, so we need it 8374 upgraded if it is a constant. */ 8375 if (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV) 8376 gv_init_pvn(gv, PL_curstash, name, namlen, 8377 SVf_UTF8 * name_is_utf8); 8378 } 8379 else { /* Maybe prototype now, and had at maximum 8380 a prototype or const/sub ref before. */ 8381 if (SvTYPE(gv) > SVt_NULL) { 8382 cv_ckproto_len_flags((const CV *)gv, 8383 o ? (const GV *)cSVOPo->op_sv : NULL, ps, 8384 ps_len, ps_utf8); 8385 } 8386 if (!SvROK(gv)) { 8387 if (ps) { 8388 sv_setpvn(MUTABLE_SV(gv), ps, ps_len); 8389 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(gv)); 8390 } 8391 else 8392 sv_setiv(MUTABLE_SV(gv), -1); 8393 } 8394 8395 SvREFCNT_dec(PL_compcv); 8396 cv = PL_compcv = NULL; 8397 goto done; 8398 } 8399 } 8400 8401 cv = (!name || (isGV(gv) && GvCVGEN(gv))) 8402 ? NULL 8403 : isGV(gv) 8404 ? GvCV(gv) 8405 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV 8406 ? (CV *)SvRV(gv) 8407 : NULL; 8408 8409 if (block) { 8410 assert(PL_parser); 8411 /* This makes sub {}; work as expected. */ 8412 if (block->op_type == OP_STUB) { 8413 const line_t l = PL_parser->copline; 8414 op_free(block); 8415 block = newSTATEOP(0, NULL, 0); 8416 PL_parser->copline = l; 8417 } 8418 block = CvLVALUE(PL_compcv) 8419 || (cv && CvLVALUE(cv) && !CvROOT(cv) && !CvXSUB(cv) 8420 && (!isGV(gv) || !GvASSUMECV(gv))) 8421 ? newUNOP(OP_LEAVESUBLV, 0, 8422 op_lvalue(scalarseq(block), OP_LEAVESUBLV)) 8423 : newUNOP(OP_LEAVESUB, 0, scalarseq(block)); 8424 start = LINKLIST(block); 8425 block->op_next = 0; 8426 if (ps && !*ps && !attrs && !CvLVALUE(PL_compcv)) 8427 const_sv = 8428 S_op_const_sv(aTHX_ start, PL_compcv, 8429 cBOOL(CvCLONE(PL_compcv))); 8430 else 8431 const_sv = NULL; 8432 } 8433 else 8434 const_sv = NULL; 8435 8436 if (SvPOK(gv) || (SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVCV)) { 8437 cv_ckproto_len_flags((const CV *)gv, 8438 o ? (const GV *)cSVOPo->op_sv : NULL, ps, 8439 ps_len, ps_utf8|CV_CKPROTO_CURSTASH); 8440 if (SvROK(gv)) { 8441 /* All the other code for sub redefinition warnings expects the 8442 clobbered sub to be a CV. Instead of making all those code 8443 paths more complex, just inline the RV version here. */ 8444 const line_t oldline = CopLINE(PL_curcop); 8445 assert(IN_PERL_COMPILETIME); 8446 if (PL_parser && PL_parser->copline != NOLINE) 8447 /* This ensures that warnings are reported at the first 8448 line of a redefinition, not the last. */ 8449 CopLINE_set(PL_curcop, PL_parser->copline); 8450 /* protect against fatal warnings leaking compcv */ 8451 SAVEFREESV(PL_compcv); 8452 8453 if (ckWARN(WARN_REDEFINE) 8454 || ( ckWARN_d(WARN_REDEFINE) 8455 && ( !const_sv || SvRV(gv) == const_sv 8456 || sv_cmp(SvRV(gv), const_sv) ))) 8457 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), 8458 "Constant subroutine %"SVf" redefined", 8459 SVfARG(cSVOPo->op_sv)); 8460 8461 SvREFCNT_inc_simple_void_NN(PL_compcv); 8462 CopLINE_set(PL_curcop, oldline); 8463 SvREFCNT_dec(SvRV(gv)); 8464 } 8465 } 8466 8467 if (cv) { 8468 const bool exists = CvROOT(cv) || CvXSUB(cv); 8469 8470 /* if the subroutine doesn't exist and wasn't pre-declared 8471 * with a prototype, assume it will be AUTOLOADed, 8472 * skipping the prototype check 8473 */ 8474 if (exists || SvPOK(cv)) 8475 cv_ckproto_len_flags(cv, gv, ps, ps_len, ps_utf8); 8476 /* already defined (or promised)? */ 8477 if (exists || (isGV(gv) && GvASSUMECV(gv))) { 8478 if (S_already_defined(aTHX_ cv, block, o, NULL, &const_sv)) 8479 cv = NULL; 8480 else { 8481 if (attrs) goto attrs; 8482 /* just a "sub foo;" when &foo is already defined */ 8483 SAVEFREESV(PL_compcv); 8484 goto done; 8485 } 8486 } 8487 } 8488 if (const_sv) { 8489 SvREFCNT_inc_simple_void_NN(const_sv); 8490 SvFLAGS(const_sv) |= SVs_PADTMP; 8491 if (cv) { 8492 assert(!CvROOT(cv) && !CvCONST(cv)); 8493 cv_forget_slab(cv); 8494 sv_setpvs(MUTABLE_SV(cv), ""); /* prototype is "" */ 8495 CvXSUBANY(cv).any_ptr = const_sv; 8496 CvXSUB(cv) = const_sv_xsub; 8497 CvCONST_on(cv); 8498 CvISXSUB_on(cv); 8499 PoisonPADLIST(cv); 8500 CvFLAGS(cv) |= CvMETHOD(PL_compcv); 8501 } 8502 else { 8503 if (isGV(gv) || CvMETHOD(PL_compcv)) { 8504 if (name && isGV(gv)) 8505 GvCV_set(gv, NULL); 8506 cv = newCONSTSUB_flags( 8507 NULL, name, namlen, name_is_utf8 ? SVf_UTF8 : 0, 8508 const_sv 8509 ); 8510 CvFLAGS(cv) |= CvMETHOD(PL_compcv); 8511 } 8512 else { 8513 if (!SvROK(gv)) { 8514 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv); 8515 prepare_SV_for_RV((SV *)gv); 8516 SvOK_off((SV *)gv); 8517 SvROK_on(gv); 8518 } 8519 SvRV_set(gv, const_sv); 8520 } 8521 } 8522 op_free(block); 8523 SvREFCNT_dec(PL_compcv); 8524 PL_compcv = NULL; 8525 goto done; 8526 } 8527 if (cv) { /* must reuse cv if autoloaded */ 8528 /* transfer PL_compcv to cv */ 8529 if (block 8530 ) { 8531 cv_flags_t existing_builtin_attrs = CvFLAGS(cv) & CVf_BUILTIN_ATTRS; 8532 PADLIST *const temp_av = CvPADLIST(cv); 8533 CV *const temp_cv = CvOUTSIDE(cv); 8534 const cv_flags_t other_flags = 8535 CvFLAGS(cv) & (CVf_SLABBED|CVf_WEAKOUTSIDE); 8536 OP * const cvstart = CvSTART(cv); 8537 8538 if (isGV(gv)) { 8539 CvGV_set(cv,gv); 8540 assert(!CvCVGV_RC(cv)); 8541 assert(CvGV(cv) == gv); 8542 } 8543 else { 8544 dVAR; 8545 U32 hash; 8546 PERL_HASH(hash, name, namlen); 8547 CvNAME_HEK_set(cv, 8548 share_hek(name, 8549 name_is_utf8 8550 ? -(SSize_t)namlen 8551 : (SSize_t)namlen, 8552 hash)); 8553 } 8554 8555 SvPOK_off(cv); 8556 CvFLAGS(cv) = CvFLAGS(PL_compcv) | existing_builtin_attrs 8557 | CvNAMED(cv); 8558 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv); 8559 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv); 8560 CvPADLIST_set(cv,CvPADLIST(PL_compcv)); 8561 CvOUTSIDE(PL_compcv) = temp_cv; 8562 CvPADLIST_set(PL_compcv, temp_av); 8563 CvSTART(cv) = CvSTART(PL_compcv); 8564 CvSTART(PL_compcv) = cvstart; 8565 CvFLAGS(PL_compcv) &= ~(CVf_SLABBED|CVf_WEAKOUTSIDE); 8566 CvFLAGS(PL_compcv) |= other_flags; 8567 8568 if (CvFILE(cv) && CvDYNFILE(cv)) { 8569 Safefree(CvFILE(cv)); 8570 } 8571 CvFILE_set_from_cop(cv, PL_curcop); 8572 CvSTASH_set(cv, PL_curstash); 8573 8574 /* inner references to PL_compcv must be fixed up ... */ 8575 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv); 8576 if (PERLDB_INTER)/* Advice debugger on the new sub. */ 8577 ++PL_sub_generation; 8578 } 8579 else { 8580 /* Might have had built-in attributes applied -- propagate them. */ 8581 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS); 8582 } 8583 /* ... before we throw it away */ 8584 SvREFCNT_dec(PL_compcv); 8585 PL_compcv = cv; 8586 } 8587 else { 8588 cv = PL_compcv; 8589 if (name && isGV(gv)) { 8590 GvCV_set(gv, cv); 8591 GvCVGEN(gv) = 0; 8592 if (HvENAME_HEK(GvSTASH(gv))) 8593 /* sub Foo::bar { (shift)+1 } */ 8594 gv_method_changed(gv); 8595 } 8596 else if (name) { 8597 if (!SvROK(gv)) { 8598 SV_CHECK_THINKFIRST_COW_DROP((SV *)gv); 8599 prepare_SV_for_RV((SV *)gv); 8600 SvOK_off((SV *)gv); 8601 SvROK_on(gv); 8602 } 8603 SvRV_set(gv, (SV *)cv); 8604 } 8605 } 8606 if (!CvHASGV(cv)) { 8607 if (isGV(gv)) CvGV_set(cv, gv); 8608 else { 8609 dVAR; 8610 U32 hash; 8611 PERL_HASH(hash, name, namlen); 8612 CvNAME_HEK_set(cv, share_hek(name, 8613 name_is_utf8 8614 ? -(SSize_t)namlen 8615 : (SSize_t)namlen, 8616 hash)); 8617 } 8618 CvFILE_set_from_cop(cv, PL_curcop); 8619 CvSTASH_set(cv, PL_curstash); 8620 } 8621 8622 if (ps) { 8623 sv_setpvn(MUTABLE_SV(cv), ps, ps_len); 8624 if ( ps_utf8 ) SvUTF8_on(MUTABLE_SV(cv)); 8625 } 8626 8627 if (!block) 8628 goto attrs; 8629 8630 /* If we assign an optree to a PVCV, then we've defined a subroutine that 8631 the debugger could be able to set a breakpoint in, so signal to 8632 pp_entereval that it should not throw away any saved lines at scope 8633 exit. */ 8634 8635 PL_breakable_sub_gen++; 8636 CvROOT(cv) = block; 8637 CvROOT(cv)->op_private |= OPpREFCOUNTED; 8638 OpREFCNT_set(CvROOT(cv), 1); 8639 /* The cv no longer needs to hold a refcount on the slab, as CvROOT 8640 itself has a refcount. */ 8641 CvSLABBED_off(cv); 8642 OpslabREFCNT_dec_padok((OPSLAB *)CvSTART(cv)); 8643 #ifdef PERL_DEBUG_READONLY_OPS 8644 slab = (OPSLAB *)CvSTART(cv); 8645 #endif 8646 CvSTART(cv) = start; 8647 CALL_PEEP(start); 8648 finalize_optree(CvROOT(cv)); 8649 S_prune_chain_head(&CvSTART(cv)); 8650 8651 /* now that optimizer has done its work, adjust pad values */ 8652 8653 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB); 8654 8655 attrs: 8656 if (attrs) { 8657 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>. */ 8658 HV *stash = name && !CvNAMED(cv) && GvSTASH(CvGV(cv)) 8659 ? GvSTASH(CvGV(cv)) 8660 : PL_curstash; 8661 if (!name) SAVEFREESV(cv); 8662 apply_attrs(stash, MUTABLE_SV(cv), attrs); 8663 if (!name) SvREFCNT_inc_simple_void_NN(cv); 8664 } 8665 8666 if (block && has_name) { 8667 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) { 8668 SV * const tmpstr = cv_name(cv,NULL,0); 8669 GV * const db_postponed = gv_fetchpvs("DB::postponed", 8670 GV_ADDMULTI, SVt_PVHV); 8671 HV *hv; 8672 SV * const sv = Perl_newSVpvf(aTHX_ "%s:%ld-%ld", 8673 CopFILE(PL_curcop), 8674 (long)PL_subline, 8675 (long)CopLINE(PL_curcop)); 8676 (void)hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr), 8677 SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr), sv, 0); 8678 hv = GvHVn(db_postponed); 8679 if (HvTOTALKEYS(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvUTF8(tmpstr) ? -(I32)SvCUR(tmpstr) : (I32)SvCUR(tmpstr))) { 8680 CV * const pcv = GvCV(db_postponed); 8681 if (pcv) { 8682 dSP; 8683 PUSHMARK(SP); 8684 XPUSHs(tmpstr); 8685 PUTBACK; 8686 call_sv(MUTABLE_SV(pcv), G_DISCARD); 8687 } 8688 } 8689 } 8690 8691 if (name) { 8692 if (PL_parser && PL_parser->error_count) 8693 clear_special_blocks(name, gv, cv); 8694 else 8695 evanescent = 8696 process_special_blocks(floor, name, gv, cv); 8697 } 8698 } 8699 8700 done: 8701 if (PL_parser) 8702 PL_parser->copline = NOLINE; 8703 LEAVE_SCOPE(floor); 8704 if (!evanescent) { 8705 #ifdef PERL_DEBUG_READONLY_OPS 8706 if (slab) 8707 Slab_to_ro(slab); 8708 #endif 8709 if (cv && name && CvOUTSIDE(cv) && !CvEVAL(CvOUTSIDE(cv))) 8710 pad_add_weakref(cv); 8711 } 8712 return cv; 8713 } 8714 8715 STATIC void 8716 S_clear_special_blocks(pTHX_ const char *const fullname, 8717 GV *const gv, CV *const cv) { 8718 const char *colon; 8719 const char *name; 8720 8721 PERL_ARGS_ASSERT_CLEAR_SPECIAL_BLOCKS; 8722 8723 colon = strrchr(fullname,':'); 8724 name = colon ? colon + 1 : fullname; 8725 8726 if ((*name == 'B' && strEQ(name, "BEGIN")) 8727 || (*name == 'E' && strEQ(name, "END")) 8728 || (*name == 'U' && strEQ(name, "UNITCHECK")) 8729 || (*name == 'C' && strEQ(name, "CHECK")) 8730 || (*name == 'I' && strEQ(name, "INIT"))) { 8731 if (!isGV(gv)) { 8732 (void)CvGV(cv); 8733 assert(isGV(gv)); 8734 } 8735 GvCV_set(gv, NULL); 8736 SvREFCNT_dec_NN(MUTABLE_SV(cv)); 8737 } 8738 } 8739 8740 /* Returns true if the sub has been freed. */ 8741 STATIC bool 8742 S_process_special_blocks(pTHX_ I32 floor, const char *const fullname, 8743 GV *const gv, 8744 CV *const cv) 8745 { 8746 const char *const colon = strrchr(fullname,':'); 8747 const char *const name = colon ? colon + 1 : fullname; 8748 8749 PERL_ARGS_ASSERT_PROCESS_SPECIAL_BLOCKS; 8750 8751 if (*name == 'B') { 8752 if (strEQ(name, "BEGIN")) { 8753 const I32 oldscope = PL_scopestack_ix; 8754 dSP; 8755 (void)CvGV(cv); 8756 if (floor) LEAVE_SCOPE(floor); 8757 ENTER; 8758 PUSHSTACKi(PERLSI_REQUIRE); 8759 SAVECOPFILE(&PL_compiling); 8760 SAVECOPLINE(&PL_compiling); 8761 SAVEVPTR(PL_curcop); 8762 8763 DEBUG_x( dump_sub(gv) ); 8764 Perl_av_create_and_push(aTHX_ &PL_beginav, MUTABLE_SV(cv)); 8765 GvCV_set(gv,0); /* cv has been hijacked */ 8766 call_list(oldscope, PL_beginav); 8767 8768 POPSTACK; 8769 LEAVE; 8770 return !PL_savebegin; 8771 } 8772 else 8773 return FALSE; 8774 } else { 8775 if (*name == 'E') { 8776 if strEQ(name, "END") { 8777 DEBUG_x( dump_sub(gv) ); 8778 Perl_av_create_and_unshift_one(aTHX_ &PL_endav, MUTABLE_SV(cv)); 8779 } else 8780 return FALSE; 8781 } else if (*name == 'U') { 8782 if (strEQ(name, "UNITCHECK")) { 8783 /* It's never too late to run a unitcheck block */ 8784 Perl_av_create_and_unshift_one(aTHX_ &PL_unitcheckav, MUTABLE_SV(cv)); 8785 } 8786 else 8787 return FALSE; 8788 } else if (*name == 'C') { 8789 if (strEQ(name, "CHECK")) { 8790 if (PL_main_start) 8791 /* diag_listed_as: Too late to run %s block */ 8792 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), 8793 "Too late to run CHECK block"); 8794 Perl_av_create_and_unshift_one(aTHX_ &PL_checkav, MUTABLE_SV(cv)); 8795 } 8796 else 8797 return FALSE; 8798 } else if (*name == 'I') { 8799 if (strEQ(name, "INIT")) { 8800 if (PL_main_start) 8801 /* diag_listed_as: Too late to run %s block */ 8802 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), 8803 "Too late to run INIT block"); 8804 Perl_av_create_and_push(aTHX_ &PL_initav, MUTABLE_SV(cv)); 8805 } 8806 else 8807 return FALSE; 8808 } else 8809 return FALSE; 8810 DEBUG_x( dump_sub(gv) ); 8811 (void)CvGV(cv); 8812 GvCV_set(gv,0); /* cv has been hijacked */ 8813 return FALSE; 8814 } 8815 } 8816 8817 /* 8818 =for apidoc newCONSTSUB 8819 8820 See L</newCONSTSUB_flags>. 8821 8822 =cut 8823 */ 8824 8825 CV * 8826 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv) 8827 { 8828 return newCONSTSUB_flags(stash, name, name ? strlen(name) : 0, 0, sv); 8829 } 8830 8831 /* 8832 =for apidoc newCONSTSUB_flags 8833 8834 Creates a constant sub equivalent to Perl S<C<sub FOO () { 123 }>> which is 8835 eligible for inlining at compile-time. 8836 8837 Currently, the only useful value for C<flags> is C<SVf_UTF8>. 8838 8839 The newly created subroutine takes ownership of a reference to the passed in 8840 SV. 8841 8842 Passing C<NULL> for SV creates a constant sub equivalent to S<C<sub BAR () {}>>, 8843 which won't be called if used as a destructor, but will suppress the overhead 8844 of a call to C<AUTOLOAD>. (This form, however, isn't eligible for inlining at 8845 compile time.) 8846 8847 =cut 8848 */ 8849 8850 CV * 8851 Perl_newCONSTSUB_flags(pTHX_ HV *stash, const char *name, STRLEN len, 8852 U32 flags, SV *sv) 8853 { 8854 CV* cv; 8855 const char *const file = CopFILE(PL_curcop); 8856 8857 ENTER; 8858 8859 if (IN_PERL_RUNTIME) { 8860 /* at runtime, it's not safe to manipulate PL_curcop: it may be 8861 * an op shared between threads. Use a non-shared COP for our 8862 * dirty work */ 8863 SAVEVPTR(PL_curcop); 8864 SAVECOMPILEWARNINGS(); 8865 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings); 8866 PL_curcop = &PL_compiling; 8867 } 8868 SAVECOPLINE(PL_curcop); 8869 CopLINE_set(PL_curcop, PL_parser ? PL_parser->copline : NOLINE); 8870 8871 SAVEHINTS(); 8872 PL_hints &= ~HINT_BLOCK_SCOPE; 8873 8874 if (stash) { 8875 SAVEGENERICSV(PL_curstash); 8876 PL_curstash = (HV *)SvREFCNT_inc_simple_NN(stash); 8877 } 8878 8879 /* Protect sv against leakage caused by fatal warnings. */ 8880 if (sv) SAVEFREESV(sv); 8881 8882 /* file becomes the CvFILE. For an XS, it's usually static storage, 8883 and so doesn't get free()d. (It's expected to be from the C pre- 8884 processor __FILE__ directive). But we need a dynamically allocated one, 8885 and we need it to get freed. */ 8886 cv = newXS_len_flags(name, len, 8887 sv && SvTYPE(sv) == SVt_PVAV 8888 ? const_av_xsub 8889 : const_sv_xsub, 8890 file ? file : "", "", 8891 &sv, XS_DYNAMIC_FILENAME | flags); 8892 CvXSUBANY(cv).any_ptr = SvREFCNT_inc_simple(sv); 8893 CvCONST_on(cv); 8894 8895 LEAVE; 8896 8897 return cv; 8898 } 8899 8900 /* 8901 =for apidoc U||newXS 8902 8903 Used by C<xsubpp> to hook up XSUBs as Perl subs. C<filename> needs to be 8904 static storage, as it is used directly as CvFILE(), without a copy being made. 8905 8906 =cut 8907 */ 8908 8909 CV * 8910 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename) 8911 { 8912 PERL_ARGS_ASSERT_NEWXS; 8913 return newXS_len_flags( 8914 name, name ? strlen(name) : 0, subaddr, filename, NULL, NULL, 0 8915 ); 8916 } 8917 8918 CV * 8919 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr, 8920 const char *const filename, const char *const proto, 8921 U32 flags) 8922 { 8923 PERL_ARGS_ASSERT_NEWXS_FLAGS; 8924 return newXS_len_flags( 8925 name, name ? strlen(name) : 0, subaddr, filename, proto, NULL, flags 8926 ); 8927 } 8928 8929 CV * 8930 Perl_newXS_deffile(pTHX_ const char *name, XSUBADDR_t subaddr) 8931 { 8932 PERL_ARGS_ASSERT_NEWXS_DEFFILE; 8933 return newXS_len_flags( 8934 name, strlen(name), subaddr, NULL, NULL, NULL, 0 8935 ); 8936 } 8937 8938 CV * 8939 Perl_newXS_len_flags(pTHX_ const char *name, STRLEN len, 8940 XSUBADDR_t subaddr, const char *const filename, 8941 const char *const proto, SV **const_svp, 8942 U32 flags) 8943 { 8944 CV *cv; 8945 bool interleave = FALSE; 8946 8947 PERL_ARGS_ASSERT_NEWXS_LEN_FLAGS; 8948 8949 { 8950 GV * const gv = gv_fetchpvn( 8951 name ? name : PL_curstash ? "__ANON__" : "__ANON__::__ANON__", 8952 name ? len : PL_curstash ? sizeof("__ANON__") - 1: 8953 sizeof("__ANON__::__ANON__") - 1, 8954 GV_ADDMULTI | flags, SVt_PVCV); 8955 8956 if ((cv = (name ? GvCV(gv) : NULL))) { 8957 if (GvCVGEN(gv)) { 8958 /* just a cached method */ 8959 SvREFCNT_dec(cv); 8960 cv = NULL; 8961 } 8962 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) { 8963 /* already defined (or promised) */ 8964 /* Redundant check that allows us to avoid creating an SV 8965 most of the time: */ 8966 if (CvCONST(cv) || ckWARN(WARN_REDEFINE)) { 8967 report_redefined_cv(newSVpvn_flags( 8968 name,len,(flags&SVf_UTF8)|SVs_TEMP 8969 ), 8970 cv, const_svp); 8971 } 8972 interleave = TRUE; 8973 ENTER; 8974 SAVEFREESV(cv); 8975 cv = NULL; 8976 } 8977 } 8978 8979 if (cv) /* must reuse cv if autoloaded */ 8980 cv_undef(cv); 8981 else { 8982 cv = MUTABLE_CV(newSV_type(SVt_PVCV)); 8983 if (name) { 8984 GvCV_set(gv,cv); 8985 GvCVGEN(gv) = 0; 8986 if (HvENAME_HEK(GvSTASH(gv))) 8987 gv_method_changed(gv); /* newXS */ 8988 } 8989 } 8990 8991 CvGV_set(cv, gv); 8992 if(filename) { 8993 /* XSUBs can't be perl lang/perl5db.pl debugged 8994 if (PERLDB_LINE_OR_SAVESRC) 8995 (void)gv_fetchfile(filename); */ 8996 assert(!CvDYNFILE(cv)); /* cv_undef should have turned it off */ 8997 if (flags & XS_DYNAMIC_FILENAME) { 8998 CvDYNFILE_on(cv); 8999 CvFILE(cv) = savepv(filename); 9000 } else { 9001 /* NOTE: not copied, as it is expected to be an external constant string */ 9002 CvFILE(cv) = (char *)filename; 9003 } 9004 } else { 9005 assert((flags & XS_DYNAMIC_FILENAME) == 0 && PL_xsubfilename); 9006 CvFILE(cv) = (char*)PL_xsubfilename; 9007 } 9008 CvISXSUB_on(cv); 9009 CvXSUB(cv) = subaddr; 9010 #ifndef PERL_IMPLICIT_CONTEXT 9011 CvHSCXT(cv) = &PL_stack_sp; 9012 #else 9013 PoisonPADLIST(cv); 9014 #endif 9015 9016 if (name) 9017 process_special_blocks(0, name, gv, cv); 9018 else 9019 CvANON_on(cv); 9020 } /* <- not a conditional branch */ 9021 9022 9023 sv_setpv(MUTABLE_SV(cv), proto); 9024 if (interleave) LEAVE; 9025 return cv; 9026 } 9027 9028 CV * 9029 Perl_newSTUB(pTHX_ GV *gv, bool fake) 9030 { 9031 CV *cv = MUTABLE_CV(newSV_type(SVt_PVCV)); 9032 GV *cvgv; 9033 PERL_ARGS_ASSERT_NEWSTUB; 9034 assert(!GvCVu(gv)); 9035 GvCV_set(gv, cv); 9036 GvCVGEN(gv) = 0; 9037 if (!fake && HvENAME_HEK(GvSTASH(gv))) 9038 gv_method_changed(gv); 9039 if (SvFAKE(gv)) { 9040 cvgv = gv_fetchsv((SV *)gv, GV_ADDMULTI, SVt_PVCV); 9041 SvFAKE_off(cvgv); 9042 } 9043 else cvgv = gv; 9044 CvGV_set(cv, cvgv); 9045 CvFILE_set_from_cop(cv, PL_curcop); 9046 CvSTASH_set(cv, PL_curstash); 9047 GvMULTI_on(gv); 9048 return cv; 9049 } 9050 9051 void 9052 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block) 9053 { 9054 CV *cv; 9055 9056 GV *gv; 9057 9058 if (PL_parser && PL_parser->error_count) { 9059 op_free(block); 9060 goto finish; 9061 } 9062 9063 gv = o 9064 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM) 9065 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM); 9066 9067 GvMULTI_on(gv); 9068 if ((cv = GvFORM(gv))) { 9069 if (ckWARN(WARN_REDEFINE)) { 9070 const line_t oldline = CopLINE(PL_curcop); 9071 if (PL_parser && PL_parser->copline != NOLINE) 9072 CopLINE_set(PL_curcop, PL_parser->copline); 9073 if (o) { 9074 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), 9075 "Format %"SVf" redefined", SVfARG(cSVOPo->op_sv)); 9076 } else { 9077 /* diag_listed_as: Format %s redefined */ 9078 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), 9079 "Format STDOUT redefined"); 9080 } 9081 CopLINE_set(PL_curcop, oldline); 9082 } 9083 SvREFCNT_dec(cv); 9084 } 9085 cv = PL_compcv; 9086 GvFORM(gv) = (CV *)SvREFCNT_inc_simple_NN(cv); 9087 CvGV_set(cv, gv); 9088 CvFILE_set_from_cop(cv, PL_curcop); 9089 9090 9091 pad_tidy(padtidy_FORMAT); 9092 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block)); 9093 CvROOT(cv)->op_private |= OPpREFCOUNTED; 9094 OpREFCNT_set(CvROOT(cv), 1); 9095 CvSTART(cv) = LINKLIST(CvROOT(cv)); 9096 CvROOT(cv)->op_next = 0; 9097 CALL_PEEP(CvSTART(cv)); 9098 finalize_optree(CvROOT(cv)); 9099 S_prune_chain_head(&CvSTART(cv)); 9100 cv_forget_slab(cv); 9101 9102 finish: 9103 op_free(o); 9104 if (PL_parser) 9105 PL_parser->copline = NOLINE; 9106 LEAVE_SCOPE(floor); 9107 PL_compiling.cop_seq = 0; 9108 } 9109 9110 OP * 9111 Perl_newANONLIST(pTHX_ OP *o) 9112 { 9113 return op_convert_list(OP_ANONLIST, OPf_SPECIAL, o); 9114 } 9115 9116 OP * 9117 Perl_newANONHASH(pTHX_ OP *o) 9118 { 9119 return op_convert_list(OP_ANONHASH, OPf_SPECIAL, o); 9120 } 9121 9122 OP * 9123 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block) 9124 { 9125 return newANONATTRSUB(floor, proto, NULL, block); 9126 } 9127 9128 OP * 9129 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block) 9130 { 9131 SV * const cv = MUTABLE_SV(newATTRSUB(floor, 0, proto, attrs, block)); 9132 OP * anoncode = 9133 newSVOP(OP_ANONCODE, 0, 9134 cv); 9135 if (CvANONCONST(cv)) 9136 anoncode = newUNOP(OP_ANONCONST, 0, 9137 op_convert_list(OP_ENTERSUB, 9138 OPf_STACKED|OPf_WANT_SCALAR, 9139 anoncode)); 9140 return newUNOP(OP_REFGEN, 0, anoncode); 9141 } 9142 9143 OP * 9144 Perl_oopsAV(pTHX_ OP *o) 9145 { 9146 dVAR; 9147 9148 PERL_ARGS_ASSERT_OOPSAV; 9149 9150 switch (o->op_type) { 9151 case OP_PADSV: 9152 case OP_PADHV: 9153 OpTYPE_set(o, OP_PADAV); 9154 return ref(o, OP_RV2AV); 9155 9156 case OP_RV2SV: 9157 case OP_RV2HV: 9158 OpTYPE_set(o, OP_RV2AV); 9159 ref(o, OP_RV2AV); 9160 break; 9161 9162 default: 9163 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV"); 9164 break; 9165 } 9166 return o; 9167 } 9168 9169 OP * 9170 Perl_oopsHV(pTHX_ OP *o) 9171 { 9172 dVAR; 9173 9174 PERL_ARGS_ASSERT_OOPSHV; 9175 9176 switch (o->op_type) { 9177 case OP_PADSV: 9178 case OP_PADAV: 9179 OpTYPE_set(o, OP_PADHV); 9180 return ref(o, OP_RV2HV); 9181 9182 case OP_RV2SV: 9183 case OP_RV2AV: 9184 OpTYPE_set(o, OP_RV2HV); 9185 ref(o, OP_RV2HV); 9186 break; 9187 9188 default: 9189 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV"); 9190 break; 9191 } 9192 return o; 9193 } 9194 9195 OP * 9196 Perl_newAVREF(pTHX_ OP *o) 9197 { 9198 dVAR; 9199 9200 PERL_ARGS_ASSERT_NEWAVREF; 9201 9202 if (o->op_type == OP_PADANY) { 9203 OpTYPE_set(o, OP_PADAV); 9204 return o; 9205 } 9206 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) { 9207 Perl_croak(aTHX_ "Can't use an array as a reference"); 9208 } 9209 return newUNOP(OP_RV2AV, 0, scalar(o)); 9210 } 9211 9212 OP * 9213 Perl_newGVREF(pTHX_ I32 type, OP *o) 9214 { 9215 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT) 9216 return newUNOP(OP_NULL, 0, o); 9217 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type); 9218 } 9219 9220 OP * 9221 Perl_newHVREF(pTHX_ OP *o) 9222 { 9223 dVAR; 9224 9225 PERL_ARGS_ASSERT_NEWHVREF; 9226 9227 if (o->op_type == OP_PADANY) { 9228 OpTYPE_set(o, OP_PADHV); 9229 return o; 9230 } 9231 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) { 9232 Perl_croak(aTHX_ "Can't use a hash as a reference"); 9233 } 9234 return newUNOP(OP_RV2HV, 0, scalar(o)); 9235 } 9236 9237 OP * 9238 Perl_newCVREF(pTHX_ I32 flags, OP *o) 9239 { 9240 if (o->op_type == OP_PADANY) { 9241 dVAR; 9242 OpTYPE_set(o, OP_PADCV); 9243 } 9244 return newUNOP(OP_RV2CV, flags, scalar(o)); 9245 } 9246 9247 OP * 9248 Perl_newSVREF(pTHX_ OP *o) 9249 { 9250 dVAR; 9251 9252 PERL_ARGS_ASSERT_NEWSVREF; 9253 9254 if (o->op_type == OP_PADANY) { 9255 OpTYPE_set(o, OP_PADSV); 9256 scalar(o); 9257 return o; 9258 } 9259 return newUNOP(OP_RV2SV, 0, scalar(o)); 9260 } 9261 9262 /* Check routines. See the comments at the top of this file for details 9263 * on when these are called */ 9264 9265 OP * 9266 Perl_ck_anoncode(pTHX_ OP *o) 9267 { 9268 PERL_ARGS_ASSERT_CK_ANONCODE; 9269 9270 cSVOPo->op_targ = pad_add_anon((CV*)cSVOPo->op_sv, o->op_type); 9271 cSVOPo->op_sv = NULL; 9272 return o; 9273 } 9274 9275 static void 9276 S_io_hints(pTHX_ OP *o) 9277 { 9278 #if O_BINARY != 0 || O_TEXT != 0 9279 HV * const table = 9280 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL;; 9281 if (table) { 9282 SV **svp = hv_fetchs(table, "open_IN", FALSE); 9283 if (svp && *svp) { 9284 STRLEN len = 0; 9285 const char *d = SvPV_const(*svp, len); 9286 const I32 mode = mode_from_discipline(d, len); 9287 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */ 9288 # if O_BINARY != 0 9289 if (mode & O_BINARY) 9290 o->op_private |= OPpOPEN_IN_RAW; 9291 # endif 9292 # if O_TEXT != 0 9293 if (mode & O_TEXT) 9294 o->op_private |= OPpOPEN_IN_CRLF; 9295 # endif 9296 } 9297 9298 svp = hv_fetchs(table, "open_OUT", FALSE); 9299 if (svp && *svp) { 9300 STRLEN len = 0; 9301 const char *d = SvPV_const(*svp, len); 9302 const I32 mode = mode_from_discipline(d, len); 9303 /* bit-and:ing with zero O_BINARY or O_TEXT would be useless. */ 9304 # if O_BINARY != 0 9305 if (mode & O_BINARY) 9306 o->op_private |= OPpOPEN_OUT_RAW; 9307 # endif 9308 # if O_TEXT != 0 9309 if (mode & O_TEXT) 9310 o->op_private |= OPpOPEN_OUT_CRLF; 9311 # endif 9312 } 9313 } 9314 #else 9315 PERL_UNUSED_CONTEXT; 9316 PERL_UNUSED_ARG(o); 9317 #endif 9318 } 9319 9320 OP * 9321 Perl_ck_backtick(pTHX_ OP *o) 9322 { 9323 GV *gv; 9324 OP *newop = NULL; 9325 OP *sibl; 9326 PERL_ARGS_ASSERT_CK_BACKTICK; 9327 /* qx and `` have a null pushmark; CORE::readpipe has only one kid. */ 9328 if (o->op_flags & OPf_KIDS && (sibl = OpSIBLING(cUNOPo->op_first)) 9329 && (gv = gv_override("readpipe",8))) 9330 { 9331 /* detach rest of siblings from o and its first child */ 9332 op_sibling_splice(o, cUNOPo->op_first, -1, NULL); 9333 newop = S_new_entersubop(aTHX_ gv, sibl); 9334 } 9335 else if (!(o->op_flags & OPf_KIDS)) 9336 newop = newUNOP(OP_BACKTICK, 0, newDEFSVOP()); 9337 if (newop) { 9338 op_free(o); 9339 return newop; 9340 } 9341 S_io_hints(aTHX_ o); 9342 return o; 9343 } 9344 9345 OP * 9346 Perl_ck_bitop(pTHX_ OP *o) 9347 { 9348 PERL_ARGS_ASSERT_CK_BITOP; 9349 9350 o->op_private = (U8)(PL_hints & HINT_INTEGER); 9351 9352 if (o->op_type == OP_NBIT_OR || o->op_type == OP_SBIT_OR 9353 || o->op_type == OP_NBIT_XOR || o->op_type == OP_SBIT_XOR 9354 || o->op_type == OP_NBIT_AND || o->op_type == OP_SBIT_AND 9355 || o->op_type == OP_NCOMPLEMENT || o->op_type == OP_SCOMPLEMENT) 9356 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__BITWISE), 9357 "The bitwise feature is experimental"); 9358 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */ 9359 && OP_IS_INFIX_BIT(o->op_type)) 9360 { 9361 const OP * const left = cBINOPo->op_first; 9362 const OP * const right = OpSIBLING(left); 9363 if ((OP_IS_NUMCOMPARE(left->op_type) && 9364 (left->op_flags & OPf_PARENS) == 0) || 9365 (OP_IS_NUMCOMPARE(right->op_type) && 9366 (right->op_flags & OPf_PARENS) == 0)) 9367 Perl_ck_warner(aTHX_ packWARN(WARN_PRECEDENCE), 9368 "Possible precedence problem on bitwise %s operator", 9369 o->op_type == OP_BIT_OR 9370 ||o->op_type == OP_NBIT_OR ? "|" 9371 : o->op_type == OP_BIT_AND 9372 ||o->op_type == OP_NBIT_AND ? "&" 9373 : o->op_type == OP_BIT_XOR 9374 ||o->op_type == OP_NBIT_XOR ? "^" 9375 : o->op_type == OP_SBIT_OR ? "|." 9376 : o->op_type == OP_SBIT_AND ? "&." : "^." 9377 ); 9378 } 9379 return o; 9380 } 9381 9382 PERL_STATIC_INLINE bool 9383 is_dollar_bracket(pTHX_ const OP * const o) 9384 { 9385 const OP *kid; 9386 PERL_UNUSED_CONTEXT; 9387 return o->op_type == OP_RV2SV && o->op_flags & OPf_KIDS 9388 && (kid = cUNOPx(o)->op_first) 9389 && kid->op_type == OP_GV 9390 && strEQ(GvNAME(cGVOPx_gv(kid)), "["); 9391 } 9392 9393 OP * 9394 Perl_ck_cmp(pTHX_ OP *o) 9395 { 9396 PERL_ARGS_ASSERT_CK_CMP; 9397 if (ckWARN(WARN_SYNTAX)) { 9398 const OP *kid = cUNOPo->op_first; 9399 if (kid && 9400 ( 9401 ( is_dollar_bracket(aTHX_ kid) 9402 && OpSIBLING(kid) && OpSIBLING(kid)->op_type == OP_CONST 9403 ) 9404 || ( kid->op_type == OP_CONST 9405 && (kid = OpSIBLING(kid)) && is_dollar_bracket(aTHX_ kid) 9406 ) 9407 ) 9408 ) 9409 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 9410 "$[ used in %s (did you mean $] ?)", OP_DESC(o)); 9411 } 9412 return o; 9413 } 9414 9415 OP * 9416 Perl_ck_concat(pTHX_ OP *o) 9417 { 9418 const OP * const kid = cUNOPo->op_first; 9419 9420 PERL_ARGS_ASSERT_CK_CONCAT; 9421 PERL_UNUSED_CONTEXT; 9422 9423 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) && 9424 !(kUNOP->op_first->op_flags & OPf_MOD)) 9425 o->op_flags |= OPf_STACKED; 9426 return o; 9427 } 9428 9429 OP * 9430 Perl_ck_spair(pTHX_ OP *o) 9431 { 9432 dVAR; 9433 9434 PERL_ARGS_ASSERT_CK_SPAIR; 9435 9436 if (o->op_flags & OPf_KIDS) { 9437 OP* newop; 9438 OP* kid; 9439 OP* kidkid; 9440 const OPCODE type = o->op_type; 9441 o = modkids(ck_fun(o), type); 9442 kid = cUNOPo->op_first; 9443 kidkid = kUNOP->op_first; 9444 newop = OpSIBLING(kidkid); 9445 if (newop) { 9446 const OPCODE type = newop->op_type; 9447 if (OpHAS_SIBLING(newop)) 9448 return o; 9449 if (o->op_type == OP_REFGEN 9450 && ( type == OP_RV2CV 9451 || ( !(newop->op_flags & OPf_PARENS) 9452 && ( type == OP_RV2AV || type == OP_PADAV 9453 || type == OP_RV2HV || type == OP_PADHV)))) 9454 NOOP; /* OK (allow srefgen for \@a and \%h) */ 9455 else if (OP_GIMME(newop,0) != G_SCALAR) 9456 return o; 9457 } 9458 /* excise first sibling */ 9459 op_sibling_splice(kid, NULL, 1, NULL); 9460 op_free(kidkid); 9461 } 9462 /* transforms OP_REFGEN into OP_SREFGEN, OP_CHOP into OP_SCHOP, 9463 * and OP_CHOMP into OP_SCHOMP */ 9464 o->op_ppaddr = PL_ppaddr[++o->op_type]; 9465 return ck_fun(o); 9466 } 9467 9468 OP * 9469 Perl_ck_delete(pTHX_ OP *o) 9470 { 9471 PERL_ARGS_ASSERT_CK_DELETE; 9472 9473 o = ck_fun(o); 9474 o->op_private = 0; 9475 if (o->op_flags & OPf_KIDS) { 9476 OP * const kid = cUNOPo->op_first; 9477 switch (kid->op_type) { 9478 case OP_ASLICE: 9479 o->op_flags |= OPf_SPECIAL; 9480 /* FALLTHROUGH */ 9481 case OP_HSLICE: 9482 o->op_private |= OPpSLICE; 9483 break; 9484 case OP_AELEM: 9485 o->op_flags |= OPf_SPECIAL; 9486 /* FALLTHROUGH */ 9487 case OP_HELEM: 9488 break; 9489 case OP_KVASLICE: 9490 Perl_croak(aTHX_ "delete argument is index/value array slice," 9491 " use array slice"); 9492 case OP_KVHSLICE: 9493 Perl_croak(aTHX_ "delete argument is key/value hash slice, use" 9494 " hash slice"); 9495 default: 9496 Perl_croak(aTHX_ "delete argument is not a HASH or ARRAY " 9497 "element or slice"); 9498 } 9499 if (kid->op_private & OPpLVAL_INTRO) 9500 o->op_private |= OPpLVAL_INTRO; 9501 op_null(kid); 9502 } 9503 return o; 9504 } 9505 9506 OP * 9507 Perl_ck_eof(pTHX_ OP *o) 9508 { 9509 PERL_ARGS_ASSERT_CK_EOF; 9510 9511 if (o->op_flags & OPf_KIDS) { 9512 OP *kid; 9513 if (cLISTOPo->op_first->op_type == OP_STUB) { 9514 OP * const newop 9515 = newUNOP(o->op_type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv)); 9516 op_free(o); 9517 o = newop; 9518 } 9519 o = ck_fun(o); 9520 kid = cLISTOPo->op_first; 9521 if (kid->op_type == OP_RV2GV) 9522 kid->op_private |= OPpALLOW_FAKE; 9523 } 9524 return o; 9525 } 9526 9527 OP * 9528 Perl_ck_eval(pTHX_ OP *o) 9529 { 9530 dVAR; 9531 9532 PERL_ARGS_ASSERT_CK_EVAL; 9533 9534 PL_hints |= HINT_BLOCK_SCOPE; 9535 if (o->op_flags & OPf_KIDS) { 9536 SVOP * const kid = (SVOP*)cUNOPo->op_first; 9537 assert(kid); 9538 9539 if (o->op_type == OP_ENTERTRY) { 9540 LOGOP *enter; 9541 9542 /* cut whole sibling chain free from o */ 9543 op_sibling_splice(o, NULL, -1, NULL); 9544 op_free(o); 9545 9546 enter = S_alloc_LOGOP(aTHX_ OP_ENTERTRY, NULL, NULL); 9547 9548 /* establish postfix order */ 9549 enter->op_next = (OP*)enter; 9550 9551 o = op_prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid); 9552 OpTYPE_set(o, OP_LEAVETRY); 9553 enter->op_other = o; 9554 return o; 9555 } 9556 else { 9557 scalar((OP*)kid); 9558 S_set_haseval(aTHX); 9559 } 9560 } 9561 else { 9562 const U8 priv = o->op_private; 9563 op_free(o); 9564 /* the newUNOP will recursively call ck_eval(), which will handle 9565 * all the stuff at the end of this function, like adding 9566 * OP_HINTSEVAL 9567 */ 9568 return newUNOP(OP_ENTEREVAL, priv <<8, newDEFSVOP()); 9569 } 9570 o->op_targ = (PADOFFSET)PL_hints; 9571 if (o->op_private & OPpEVAL_BYTES) o->op_targ &= ~HINT_UTF8; 9572 if ((PL_hints & HINT_LOCALIZE_HH) != 0 9573 && !(o->op_private & OPpEVAL_COPHH) && GvHV(PL_hintgv)) { 9574 /* Store a copy of %^H that pp_entereval can pick up. */ 9575 OP *hhop = newSVOP(OP_HINTSEVAL, 0, 9576 MUTABLE_SV(hv_copy_hints_hv(GvHV(PL_hintgv)))); 9577 /* append hhop to only child */ 9578 op_sibling_splice(o, cUNOPo->op_first, 0, hhop); 9579 9580 o->op_private |= OPpEVAL_HAS_HH; 9581 } 9582 if (!(o->op_private & OPpEVAL_BYTES) 9583 && FEATURE_UNIEVAL_IS_ENABLED) 9584 o->op_private |= OPpEVAL_UNICODE; 9585 return o; 9586 } 9587 9588 OP * 9589 Perl_ck_exec(pTHX_ OP *o) 9590 { 9591 PERL_ARGS_ASSERT_CK_EXEC; 9592 9593 if (o->op_flags & OPf_STACKED) { 9594 OP *kid; 9595 o = ck_fun(o); 9596 kid = OpSIBLING(cUNOPo->op_first); 9597 if (kid->op_type == OP_RV2GV) 9598 op_null(kid); 9599 } 9600 else 9601 o = listkids(o); 9602 return o; 9603 } 9604 9605 OP * 9606 Perl_ck_exists(pTHX_ OP *o) 9607 { 9608 PERL_ARGS_ASSERT_CK_EXISTS; 9609 9610 o = ck_fun(o); 9611 if (o->op_flags & OPf_KIDS) { 9612 OP * const kid = cUNOPo->op_first; 9613 if (kid->op_type == OP_ENTERSUB) { 9614 (void) ref(kid, o->op_type); 9615 if (kid->op_type != OP_RV2CV 9616 && !(PL_parser && PL_parser->error_count)) 9617 Perl_croak(aTHX_ 9618 "exists argument is not a subroutine name"); 9619 o->op_private |= OPpEXISTS_SUB; 9620 } 9621 else if (kid->op_type == OP_AELEM) 9622 o->op_flags |= OPf_SPECIAL; 9623 else if (kid->op_type != OP_HELEM) 9624 Perl_croak(aTHX_ "exists argument is not a HASH or ARRAY " 9625 "element or a subroutine"); 9626 op_null(kid); 9627 } 9628 return o; 9629 } 9630 9631 OP * 9632 Perl_ck_rvconst(pTHX_ OP *o) 9633 { 9634 dVAR; 9635 SVOP * const kid = (SVOP*)cUNOPo->op_first; 9636 9637 PERL_ARGS_ASSERT_CK_RVCONST; 9638 9639 o->op_private |= (PL_hints & HINT_STRICT_REFS); 9640 9641 if (kid->op_type == OP_CONST) { 9642 int iscv; 9643 GV *gv; 9644 SV * const kidsv = kid->op_sv; 9645 9646 /* Is it a constant from cv_const_sv()? */ 9647 if ((SvROK(kidsv) || isGV_with_GP(kidsv)) && SvREADONLY(kidsv)) { 9648 return o; 9649 } 9650 if (SvTYPE(kidsv) == SVt_PVAV) return o; 9651 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) { 9652 const char *badthing; 9653 switch (o->op_type) { 9654 case OP_RV2SV: 9655 badthing = "a SCALAR"; 9656 break; 9657 case OP_RV2AV: 9658 badthing = "an ARRAY"; 9659 break; 9660 case OP_RV2HV: 9661 badthing = "a HASH"; 9662 break; 9663 default: 9664 badthing = NULL; 9665 break; 9666 } 9667 if (badthing) 9668 Perl_croak(aTHX_ 9669 "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use", 9670 SVfARG(kidsv), badthing); 9671 } 9672 /* 9673 * This is a little tricky. We only want to add the symbol if we 9674 * didn't add it in the lexer. Otherwise we get duplicate strict 9675 * warnings. But if we didn't add it in the lexer, we must at 9676 * least pretend like we wanted to add it even if it existed before, 9677 * or we get possible typo warnings. OPpCONST_ENTERED says 9678 * whether the lexer already added THIS instance of this symbol. 9679 */ 9680 iscv = o->op_type == OP_RV2CV ? GV_NOEXPAND|GV_ADDMULTI : 0; 9681 gv = gv_fetchsv(kidsv, 9682 o->op_type == OP_RV2CV 9683 && o->op_private & OPpMAY_RETURN_CONSTANT 9684 ? GV_NOEXPAND 9685 : iscv | !(kid->op_private & OPpCONST_ENTERED), 9686 iscv 9687 ? SVt_PVCV 9688 : o->op_type == OP_RV2SV 9689 ? SVt_PV 9690 : o->op_type == OP_RV2AV 9691 ? SVt_PVAV 9692 : o->op_type == OP_RV2HV 9693 ? SVt_PVHV 9694 : SVt_PVGV); 9695 if (gv) { 9696 if (!isGV(gv)) { 9697 assert(iscv); 9698 assert(SvROK(gv)); 9699 if (!(o->op_private & OPpMAY_RETURN_CONSTANT) 9700 && SvTYPE(SvRV(gv)) != SVt_PVCV) 9701 gv_fetchsv(kidsv, GV_ADDMULTI, SVt_PVCV); 9702 } 9703 OpTYPE_set(kid, OP_GV); 9704 SvREFCNT_dec(kid->op_sv); 9705 #ifdef USE_ITHREADS 9706 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */ 9707 STATIC_ASSERT_STMT(sizeof(PADOP) <= sizeof(SVOP)); 9708 kPADOP->op_padix = pad_alloc(OP_GV, SVf_READONLY); 9709 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix)); 9710 PAD_SETSV(kPADOP->op_padix, MUTABLE_SV(SvREFCNT_inc_simple_NN(gv))); 9711 #else 9712 kid->op_sv = SvREFCNT_inc_simple_NN(gv); 9713 #endif 9714 kid->op_private = 0; 9715 /* FAKE globs in the symbol table cause weird bugs (#77810) */ 9716 SvFAKE_off(gv); 9717 } 9718 } 9719 return o; 9720 } 9721 9722 OP * 9723 Perl_ck_ftst(pTHX_ OP *o) 9724 { 9725 dVAR; 9726 const I32 type = o->op_type; 9727 9728 PERL_ARGS_ASSERT_CK_FTST; 9729 9730 if (o->op_flags & OPf_REF) { 9731 NOOP; 9732 } 9733 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) { 9734 SVOP * const kid = (SVOP*)cUNOPo->op_first; 9735 const OPCODE kidtype = kid->op_type; 9736 9737 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE) 9738 && !kid->op_folded) { 9739 OP * const newop = newGVOP(type, OPf_REF, 9740 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO)); 9741 op_free(o); 9742 return newop; 9743 } 9744 9745 if ((kidtype == OP_RV2AV || kidtype == OP_PADAV) && ckWARN(WARN_SYNTAX)) { 9746 SV *name = S_op_varname_subscript(aTHX_ (OP*)kid, 2); 9747 if (name) { 9748 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */ 9749 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s (did you want stat %" SVf "?)", 9750 array_passed_to_stat, name); 9751 } 9752 else { 9753 /* diag_listed_as: Array passed to stat will be coerced to a scalar%s */ 9754 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "%s", array_passed_to_stat); 9755 } 9756 } 9757 scalar((OP *) kid); 9758 if ((PL_hints & HINT_FILETEST_ACCESS) && OP_IS_FILETEST_ACCESS(o->op_type)) 9759 o->op_private |= OPpFT_ACCESS; 9760 if (type != OP_STAT && type != OP_LSTAT 9761 && PL_check[kidtype] == Perl_ck_ftst 9762 && kidtype != OP_STAT && kidtype != OP_LSTAT 9763 ) { 9764 o->op_private |= OPpFT_STACKED; 9765 kid->op_private |= OPpFT_STACKING; 9766 if (kidtype == OP_FTTTY && ( 9767 !(kid->op_private & OPpFT_STACKED) 9768 || kid->op_private & OPpFT_AFTER_t 9769 )) 9770 o->op_private |= OPpFT_AFTER_t; 9771 } 9772 } 9773 else { 9774 op_free(o); 9775 if (type == OP_FTTTY) 9776 o = newGVOP(type, OPf_REF, PL_stdingv); 9777 else 9778 o = newUNOP(type, 0, newDEFSVOP()); 9779 } 9780 return o; 9781 } 9782 9783 OP * 9784 Perl_ck_fun(pTHX_ OP *o) 9785 { 9786 const int type = o->op_type; 9787 I32 oa = PL_opargs[type] >> OASHIFT; 9788 9789 PERL_ARGS_ASSERT_CK_FUN; 9790 9791 if (o->op_flags & OPf_STACKED) { 9792 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL)) 9793 oa &= ~OA_OPTIONAL; 9794 else 9795 return no_fh_allowed(o); 9796 } 9797 9798 if (o->op_flags & OPf_KIDS) { 9799 OP *prev_kid = NULL; 9800 OP *kid = cLISTOPo->op_first; 9801 I32 numargs = 0; 9802 bool seen_optional = FALSE; 9803 9804 if (kid->op_type == OP_PUSHMARK || 9805 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)) 9806 { 9807 prev_kid = kid; 9808 kid = OpSIBLING(kid); 9809 } 9810 if (kid && kid->op_type == OP_COREARGS) { 9811 bool optional = FALSE; 9812 while (oa) { 9813 numargs++; 9814 if (oa & OA_OPTIONAL) optional = TRUE; 9815 oa = oa >> 4; 9816 } 9817 if (optional) o->op_private |= numargs; 9818 return o; 9819 } 9820 9821 while (oa) { 9822 if (oa & OA_OPTIONAL || (oa & 7) == OA_LIST) { 9823 if (!kid && !seen_optional && PL_opargs[type] & OA_DEFGV) { 9824 kid = newDEFSVOP(); 9825 /* append kid to chain */ 9826 op_sibling_splice(o, prev_kid, 0, kid); 9827 } 9828 seen_optional = TRUE; 9829 } 9830 if (!kid) break; 9831 9832 numargs++; 9833 switch (oa & 7) { 9834 case OA_SCALAR: 9835 /* list seen where single (scalar) arg expected? */ 9836 if (numargs == 1 && !(oa >> 4) 9837 && kid->op_type == OP_LIST && type != OP_SCALAR) 9838 { 9839 return too_many_arguments_pv(o,PL_op_desc[type], 0); 9840 } 9841 if (type != OP_DELETE) scalar(kid); 9842 break; 9843 case OA_LIST: 9844 if (oa < 16) { 9845 kid = 0; 9846 continue; 9847 } 9848 else 9849 list(kid); 9850 break; 9851 case OA_AVREF: 9852 if ((type == OP_PUSH || type == OP_UNSHIFT) 9853 && !OpHAS_SIBLING(kid)) 9854 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), 9855 "Useless use of %s with no values", 9856 PL_op_desc[type]); 9857 9858 if (kid->op_type == OP_CONST 9859 && ( !SvROK(cSVOPx_sv(kid)) 9860 || SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV ) 9861 ) 9862 bad_type_pv(numargs, "array", o, kid); 9863 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV) { 9864 yyerror_pv(Perl_form(aTHX_ "Experimental %s on scalar is now forbidden", 9865 PL_op_desc[type]), 0); 9866 } 9867 else { 9868 op_lvalue(kid, type); 9869 } 9870 break; 9871 case OA_HVREF: 9872 if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV) 9873 bad_type_pv(numargs, "hash", o, kid); 9874 op_lvalue(kid, type); 9875 break; 9876 case OA_CVREF: 9877 { 9878 /* replace kid with newop in chain */ 9879 OP * const newop = 9880 S_op_sibling_newUNOP(aTHX_ o, prev_kid, OP_NULL, 0); 9881 newop->op_next = newop; 9882 kid = newop; 9883 } 9884 break; 9885 case OA_FILEREF: 9886 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) { 9887 if (kid->op_type == OP_CONST && 9888 (kid->op_private & OPpCONST_BARE)) 9889 { 9890 OP * const newop = newGVOP(OP_GV, 0, 9891 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO)); 9892 /* replace kid with newop in chain */ 9893 op_sibling_splice(o, prev_kid, 1, newop); 9894 op_free(kid); 9895 kid = newop; 9896 } 9897 else if (kid->op_type == OP_READLINE) { 9898 /* neophyte patrol: open(<FH>), close(<FH>) etc. */ 9899 bad_type_pv(numargs, "HANDLE", o, kid); 9900 } 9901 else { 9902 I32 flags = OPf_SPECIAL; 9903 I32 priv = 0; 9904 PADOFFSET targ = 0; 9905 9906 /* is this op a FH constructor? */ 9907 if (is_handle_constructor(o,numargs)) { 9908 const char *name = NULL; 9909 STRLEN len = 0; 9910 U32 name_utf8 = 0; 9911 bool want_dollar = TRUE; 9912 9913 flags = 0; 9914 /* Set a flag to tell rv2gv to vivify 9915 * need to "prove" flag does not mean something 9916 * else already - NI-S 1999/05/07 9917 */ 9918 priv = OPpDEREF; 9919 if (kid->op_type == OP_PADSV) { 9920 PADNAME * const pn 9921 = PAD_COMPNAME_SV(kid->op_targ); 9922 name = PadnamePV (pn); 9923 len = PadnameLEN(pn); 9924 name_utf8 = PadnameUTF8(pn); 9925 } 9926 else if (kid->op_type == OP_RV2SV 9927 && kUNOP->op_first->op_type == OP_GV) 9928 { 9929 GV * const gv = cGVOPx_gv(kUNOP->op_first); 9930 name = GvNAME(gv); 9931 len = GvNAMELEN(gv); 9932 name_utf8 = GvNAMEUTF8(gv) ? SVf_UTF8 : 0; 9933 } 9934 else if (kid->op_type == OP_AELEM 9935 || kid->op_type == OP_HELEM) 9936 { 9937 OP *firstop; 9938 OP *op = ((BINOP*)kid)->op_first; 9939 name = NULL; 9940 if (op) { 9941 SV *tmpstr = NULL; 9942 const char * const a = 9943 kid->op_type == OP_AELEM ? 9944 "[]" : "{}"; 9945 if (((op->op_type == OP_RV2AV) || 9946 (op->op_type == OP_RV2HV)) && 9947 (firstop = ((UNOP*)op)->op_first) && 9948 (firstop->op_type == OP_GV)) { 9949 /* packagevar $a[] or $h{} */ 9950 GV * const gv = cGVOPx_gv(firstop); 9951 if (gv) 9952 tmpstr = 9953 Perl_newSVpvf(aTHX_ 9954 "%s%c...%c", 9955 GvNAME(gv), 9956 a[0], a[1]); 9957 } 9958 else if (op->op_type == OP_PADAV 9959 || op->op_type == OP_PADHV) { 9960 /* lexicalvar $a[] or $h{} */ 9961 const char * const padname = 9962 PAD_COMPNAME_PV(op->op_targ); 9963 if (padname) 9964 tmpstr = 9965 Perl_newSVpvf(aTHX_ 9966 "%s%c...%c", 9967 padname + 1, 9968 a[0], a[1]); 9969 } 9970 if (tmpstr) { 9971 name = SvPV_const(tmpstr, len); 9972 name_utf8 = SvUTF8(tmpstr); 9973 sv_2mortal(tmpstr); 9974 } 9975 } 9976 if (!name) { 9977 name = "__ANONIO__"; 9978 len = 10; 9979 want_dollar = FALSE; 9980 } 9981 op_lvalue(kid, type); 9982 } 9983 if (name) { 9984 SV *namesv; 9985 targ = pad_alloc(OP_RV2GV, SVf_READONLY); 9986 namesv = PAD_SVl(targ); 9987 if (want_dollar && *name != '$') 9988 sv_setpvs(namesv, "$"); 9989 else 9990 sv_setpvs(namesv, ""); 9991 sv_catpvn(namesv, name, len); 9992 if ( name_utf8 ) SvUTF8_on(namesv); 9993 } 9994 } 9995 scalar(kid); 9996 kid = S_op_sibling_newUNOP(aTHX_ o, prev_kid, 9997 OP_RV2GV, flags); 9998 kid->op_targ = targ; 9999 kid->op_private |= priv; 10000 } 10001 } 10002 scalar(kid); 10003 break; 10004 case OA_SCALARREF: 10005 if ((type == OP_UNDEF || type == OP_POS) 10006 && numargs == 1 && !(oa >> 4) 10007 && kid->op_type == OP_LIST) 10008 return too_many_arguments_pv(o,PL_op_desc[type], 0); 10009 op_lvalue(scalar(kid), type); 10010 break; 10011 } 10012 oa >>= 4; 10013 prev_kid = kid; 10014 kid = OpSIBLING(kid); 10015 } 10016 /* FIXME - should the numargs or-ing move after the too many 10017 * arguments check? */ 10018 o->op_private |= numargs; 10019 if (kid) 10020 return too_many_arguments_pv(o,OP_DESC(o), 0); 10021 listkids(o); 10022 } 10023 else if (PL_opargs[type] & OA_DEFGV) { 10024 /* Ordering of these two is important to keep f_map.t passing. */ 10025 op_free(o); 10026 return newUNOP(type, 0, newDEFSVOP()); 10027 } 10028 10029 if (oa) { 10030 while (oa & OA_OPTIONAL) 10031 oa >>= 4; 10032 if (oa && oa != OA_LIST) 10033 return too_few_arguments_pv(o,OP_DESC(o), 0); 10034 } 10035 return o; 10036 } 10037 10038 OP * 10039 Perl_ck_glob(pTHX_ OP *o) 10040 { 10041 GV *gv; 10042 10043 PERL_ARGS_ASSERT_CK_GLOB; 10044 10045 o = ck_fun(o); 10046 if ((o->op_flags & OPf_KIDS) && !OpHAS_SIBLING(cLISTOPo->op_first)) 10047 op_append_elem(OP_GLOB, o, newDEFSVOP()); /* glob() => glob($_) */ 10048 10049 if (!(o->op_flags & OPf_SPECIAL) && (gv = gv_override("glob", 4))) 10050 { 10051 /* convert 10052 * glob 10053 * \ null - const(wildcard) 10054 * into 10055 * null 10056 * \ enter 10057 * \ list 10058 * \ mark - glob - rv2cv 10059 * | \ gv(CORE::GLOBAL::glob) 10060 * | 10061 * \ null - const(wildcard) 10062 */ 10063 o->op_flags |= OPf_SPECIAL; 10064 o->op_targ = pad_alloc(OP_GLOB, SVs_PADTMP); 10065 o = S_new_entersubop(aTHX_ gv, o); 10066 o = newUNOP(OP_NULL, 0, o); 10067 o->op_targ = OP_GLOB; /* hint at what it used to be: eg in newWHILEOP */ 10068 return o; 10069 } 10070 else o->op_flags &= ~OPf_SPECIAL; 10071 #if !defined(PERL_EXTERNAL_GLOB) 10072 if (!PL_globhook) { 10073 ENTER; 10074 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, 10075 newSVpvs("File::Glob"), NULL, NULL, NULL); 10076 LEAVE; 10077 } 10078 #endif /* !PERL_EXTERNAL_GLOB */ 10079 gv = (GV *)newSV(0); 10080 gv_init(gv, 0, "", 0, 0); 10081 gv_IOadd(gv); 10082 op_append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv)); 10083 SvREFCNT_dec_NN(gv); /* newGVOP increased it */ 10084 scalarkids(o); 10085 return o; 10086 } 10087 10088 OP * 10089 Perl_ck_grep(pTHX_ OP *o) 10090 { 10091 LOGOP *gwop; 10092 OP *kid; 10093 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE; 10094 10095 PERL_ARGS_ASSERT_CK_GREP; 10096 10097 /* don't allocate gwop here, as we may leak it if PL_parser->error_count > 0 */ 10098 10099 if (o->op_flags & OPf_STACKED) { 10100 kid = cUNOPx(OpSIBLING(cLISTOPo->op_first))->op_first; 10101 if (kid->op_type != OP_SCOPE && kid->op_type != OP_LEAVE) 10102 return no_fh_allowed(o); 10103 o->op_flags &= ~OPf_STACKED; 10104 } 10105 kid = OpSIBLING(cLISTOPo->op_first); 10106 if (type == OP_MAPWHILE) 10107 list(kid); 10108 else 10109 scalar(kid); 10110 o = ck_fun(o); 10111 if (PL_parser && PL_parser->error_count) 10112 return o; 10113 kid = OpSIBLING(cLISTOPo->op_first); 10114 if (kid->op_type != OP_NULL) 10115 Perl_croak(aTHX_ "panic: ck_grep, type=%u", (unsigned) kid->op_type); 10116 kid = kUNOP->op_first; 10117 10118 gwop = S_alloc_LOGOP(aTHX_ type, o, LINKLIST(kid)); 10119 kid->op_next = (OP*)gwop; 10120 o->op_private = gwop->op_private = 0; 10121 gwop->op_targ = pad_alloc(type, SVs_PADTMP); 10122 10123 kid = OpSIBLING(cLISTOPo->op_first); 10124 for (kid = OpSIBLING(kid); kid; kid = OpSIBLING(kid)) 10125 op_lvalue(kid, OP_GREPSTART); 10126 10127 return (OP*)gwop; 10128 } 10129 10130 OP * 10131 Perl_ck_index(pTHX_ OP *o) 10132 { 10133 PERL_ARGS_ASSERT_CK_INDEX; 10134 10135 if (o->op_flags & OPf_KIDS) { 10136 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ 10137 if (kid) 10138 kid = OpSIBLING(kid); /* get past "big" */ 10139 if (kid && kid->op_type == OP_CONST) { 10140 const bool save_taint = TAINT_get; 10141 SV *sv = kSVOP->op_sv; 10142 if ((!SvPOK(sv) || SvNIOKp(sv)) && SvOK(sv) && !SvROK(sv)) { 10143 sv = newSV(0); 10144 sv_copypv(sv, kSVOP->op_sv); 10145 SvREFCNT_dec_NN(kSVOP->op_sv); 10146 kSVOP->op_sv = sv; 10147 } 10148 if (SvOK(sv)) fbm_compile(sv, 0); 10149 TAINT_set(save_taint); 10150 #ifdef NO_TAINT_SUPPORT 10151 PERL_UNUSED_VAR(save_taint); 10152 #endif 10153 } 10154 } 10155 return ck_fun(o); 10156 } 10157 10158 OP * 10159 Perl_ck_lfun(pTHX_ OP *o) 10160 { 10161 const OPCODE type = o->op_type; 10162 10163 PERL_ARGS_ASSERT_CK_LFUN; 10164 10165 return modkids(ck_fun(o), type); 10166 } 10167 10168 OP * 10169 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */ 10170 { 10171 PERL_ARGS_ASSERT_CK_DEFINED; 10172 10173 if ((o->op_flags & OPf_KIDS)) { 10174 switch (cUNOPo->op_first->op_type) { 10175 case OP_RV2AV: 10176 case OP_PADAV: 10177 Perl_croak(aTHX_ "Can't use 'defined(@array)'" 10178 " (Maybe you should just omit the defined()?)"); 10179 break; 10180 case OP_RV2HV: 10181 case OP_PADHV: 10182 Perl_croak(aTHX_ "Can't use 'defined(%%hash)'" 10183 " (Maybe you should just omit the defined()?)"); 10184 break; 10185 default: 10186 /* no warning */ 10187 break; 10188 } 10189 } 10190 return ck_rfun(o); 10191 } 10192 10193 OP * 10194 Perl_ck_readline(pTHX_ OP *o) 10195 { 10196 PERL_ARGS_ASSERT_CK_READLINE; 10197 10198 if (o->op_flags & OPf_KIDS) { 10199 OP *kid = cLISTOPo->op_first; 10200 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE; 10201 } 10202 else { 10203 OP * const newop 10204 = newUNOP(OP_READLINE, 0, newGVOP(OP_GV, 0, PL_argvgv)); 10205 op_free(o); 10206 return newop; 10207 } 10208 return o; 10209 } 10210 10211 OP * 10212 Perl_ck_rfun(pTHX_ OP *o) 10213 { 10214 const OPCODE type = o->op_type; 10215 10216 PERL_ARGS_ASSERT_CK_RFUN; 10217 10218 return refkids(ck_fun(o), type); 10219 } 10220 10221 OP * 10222 Perl_ck_listiob(pTHX_ OP *o) 10223 { 10224 OP *kid; 10225 10226 PERL_ARGS_ASSERT_CK_LISTIOB; 10227 10228 kid = cLISTOPo->op_first; 10229 if (!kid) { 10230 o = force_list(o, 1); 10231 kid = cLISTOPo->op_first; 10232 } 10233 if (kid->op_type == OP_PUSHMARK) 10234 kid = OpSIBLING(kid); 10235 if (kid && o->op_flags & OPf_STACKED) 10236 kid = OpSIBLING(kid); 10237 else if (kid && !OpHAS_SIBLING(kid)) { /* print HANDLE; */ 10238 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE 10239 && !kid->op_folded) { 10240 o->op_flags |= OPf_STACKED; /* make it a filehandle */ 10241 scalar(kid); 10242 /* replace old const op with new OP_RV2GV parent */ 10243 kid = S_op_sibling_newUNOP(aTHX_ o, cLISTOPo->op_first, 10244 OP_RV2GV, OPf_REF); 10245 kid = OpSIBLING(kid); 10246 } 10247 } 10248 10249 if (!kid) 10250 op_append_elem(o->op_type, o, newDEFSVOP()); 10251 10252 if (o->op_type == OP_PRTF) return modkids(listkids(o), OP_PRTF); 10253 return listkids(o); 10254 } 10255 10256 OP * 10257 Perl_ck_smartmatch(pTHX_ OP *o) 10258 { 10259 dVAR; 10260 PERL_ARGS_ASSERT_CK_SMARTMATCH; 10261 if (0 == (o->op_flags & OPf_SPECIAL)) { 10262 OP *first = cBINOPo->op_first; 10263 OP *second = OpSIBLING(first); 10264 10265 /* Implicitly take a reference to an array or hash */ 10266 10267 /* remove the original two siblings, then add back the 10268 * (possibly different) first and second sibs. 10269 */ 10270 op_sibling_splice(o, NULL, 1, NULL); 10271 op_sibling_splice(o, NULL, 1, NULL); 10272 first = ref_array_or_hash(first); 10273 second = ref_array_or_hash(second); 10274 op_sibling_splice(o, NULL, 0, second); 10275 op_sibling_splice(o, NULL, 0, first); 10276 10277 /* Implicitly take a reference to a regular expression */ 10278 if (first->op_type == OP_MATCH) { 10279 OpTYPE_set(first, OP_QR); 10280 } 10281 if (second->op_type == OP_MATCH) { 10282 OpTYPE_set(second, OP_QR); 10283 } 10284 } 10285 10286 return o; 10287 } 10288 10289 10290 static OP * 10291 S_maybe_targlex(pTHX_ OP *o) 10292 { 10293 OP * const kid = cLISTOPo->op_first; 10294 /* has a disposable target? */ 10295 if ((PL_opargs[kid->op_type] & OA_TARGLEX) 10296 && !(kid->op_flags & OPf_STACKED) 10297 /* Cannot steal the second time! */ 10298 && !(kid->op_private & OPpTARGET_MY) 10299 ) 10300 { 10301 OP * const kkid = OpSIBLING(kid); 10302 10303 /* Can just relocate the target. */ 10304 if (kkid && kkid->op_type == OP_PADSV 10305 && (!(kkid->op_private & OPpLVAL_INTRO) 10306 || kkid->op_private & OPpPAD_STATE)) 10307 { 10308 kid->op_targ = kkid->op_targ; 10309 kkid->op_targ = 0; 10310 /* Now we do not need PADSV and SASSIGN. 10311 * Detach kid and free the rest. */ 10312 op_sibling_splice(o, NULL, 1, NULL); 10313 op_free(o); 10314 kid->op_private |= OPpTARGET_MY; /* Used for context settings */ 10315 return kid; 10316 } 10317 } 10318 return o; 10319 } 10320 10321 OP * 10322 Perl_ck_sassign(pTHX_ OP *o) 10323 { 10324 dVAR; 10325 OP * const kid = cLISTOPo->op_first; 10326 10327 PERL_ARGS_ASSERT_CK_SASSIGN; 10328 10329 if (OpHAS_SIBLING(kid)) { 10330 OP *kkid = OpSIBLING(kid); 10331 /* For state variable assignment with attributes, kkid is a list op 10332 whose op_last is a padsv. */ 10333 if ((kkid->op_type == OP_PADSV || 10334 (OP_TYPE_IS_OR_WAS(kkid, OP_LIST) && 10335 (kkid = cLISTOPx(kkid)->op_last)->op_type == OP_PADSV 10336 ) 10337 ) 10338 && (kkid->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)) 10339 == (OPpLVAL_INTRO|OPpPAD_STATE)) { 10340 const PADOFFSET target = kkid->op_targ; 10341 OP *const other = newOP(OP_PADSV, 10342 kkid->op_flags 10343 | ((kkid->op_private & ~OPpLVAL_INTRO) << 8)); 10344 OP *const first = newOP(OP_NULL, 0); 10345 OP *const nullop = 10346 newCONDOP(0, first, o, other); 10347 /* XXX targlex disabled for now; see ticket #124160 10348 newCONDOP(0, first, S_maybe_targlex(aTHX_ o), other); 10349 */ 10350 OP *const condop = first->op_next; 10351 10352 OpTYPE_set(condop, OP_ONCE); 10353 other->op_targ = target; 10354 nullop->op_flags |= OPf_WANT_SCALAR; 10355 10356 /* Store the initializedness of state vars in a separate 10357 pad entry. */ 10358 condop->op_targ = 10359 pad_add_name_pvn("$",1,padadd_NO_DUP_CHECK|padadd_STATE,0,0); 10360 /* hijacking PADSTALE for uninitialized state variables */ 10361 SvPADSTALE_on(PAD_SVl(condop->op_targ)); 10362 10363 return nullop; 10364 } 10365 } 10366 return S_maybe_targlex(aTHX_ o); 10367 } 10368 10369 OP * 10370 Perl_ck_match(pTHX_ OP *o) 10371 { 10372 PERL_UNUSED_CONTEXT; 10373 PERL_ARGS_ASSERT_CK_MATCH; 10374 10375 if (o->op_type == OP_MATCH || o->op_type == OP_QR) 10376 o->op_private |= OPpRUNTIME; 10377 return o; 10378 } 10379 10380 OP * 10381 Perl_ck_method(pTHX_ OP *o) 10382 { 10383 SV *sv, *methsv, *rclass; 10384 const char* method; 10385 char* compatptr; 10386 int utf8; 10387 STRLEN len, nsplit = 0, i; 10388 OP* new_op; 10389 OP * const kid = cUNOPo->op_first; 10390 10391 PERL_ARGS_ASSERT_CK_METHOD; 10392 if (kid->op_type != OP_CONST) return o; 10393 10394 sv = kSVOP->op_sv; 10395 10396 /* replace ' with :: */ 10397 while ((compatptr = strchr(SvPVX(sv), '\''))) { 10398 *compatptr = ':'; 10399 sv_insert(sv, compatptr - SvPVX_const(sv), 0, ":", 1); 10400 } 10401 10402 method = SvPVX_const(sv); 10403 len = SvCUR(sv); 10404 utf8 = SvUTF8(sv) ? -1 : 1; 10405 10406 for (i = len - 1; i > 0; --i) if (method[i] == ':') { 10407 nsplit = i+1; 10408 break; 10409 } 10410 10411 methsv = newSVpvn_share(method+nsplit, utf8*(len - nsplit), 0); 10412 10413 if (!nsplit) { /* $proto->method() */ 10414 op_free(o); 10415 return newMETHOP_named(OP_METHOD_NAMED, 0, methsv); 10416 } 10417 10418 if (nsplit == 7 && memEQ(method, "SUPER::", nsplit)) { /* $proto->SUPER::method() */ 10419 op_free(o); 10420 return newMETHOP_named(OP_METHOD_SUPER, 0, methsv); 10421 } 10422 10423 /* $proto->MyClass::method() and $proto->MyClass::SUPER::method() */ 10424 if (nsplit >= 9 && strnEQ(method+nsplit-9, "::SUPER::", 9)) { 10425 rclass = newSVpvn_share(method, utf8*(nsplit-9), 0); 10426 new_op = newMETHOP_named(OP_METHOD_REDIR_SUPER, 0, methsv); 10427 } else { 10428 rclass = newSVpvn_share(method, utf8*(nsplit-2), 0); 10429 new_op = newMETHOP_named(OP_METHOD_REDIR, 0, methsv); 10430 } 10431 #ifdef USE_ITHREADS 10432 op_relocate_sv(&rclass, &cMETHOPx(new_op)->op_rclass_targ); 10433 #else 10434 cMETHOPx(new_op)->op_rclass_sv = rclass; 10435 #endif 10436 op_free(o); 10437 return new_op; 10438 } 10439 10440 OP * 10441 Perl_ck_null(pTHX_ OP *o) 10442 { 10443 PERL_ARGS_ASSERT_CK_NULL; 10444 PERL_UNUSED_CONTEXT; 10445 return o; 10446 } 10447 10448 OP * 10449 Perl_ck_open(pTHX_ OP *o) 10450 { 10451 PERL_ARGS_ASSERT_CK_OPEN; 10452 10453 S_io_hints(aTHX_ o); 10454 { 10455 /* In case of three-arg dup open remove strictness 10456 * from the last arg if it is a bareword. */ 10457 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */ 10458 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */ 10459 OP *oa; 10460 const char *mode; 10461 10462 if ((last->op_type == OP_CONST) && /* The bareword. */ 10463 (last->op_private & OPpCONST_BARE) && 10464 (last->op_private & OPpCONST_STRICT) && 10465 (oa = OpSIBLING(first)) && /* The fh. */ 10466 (oa = OpSIBLING(oa)) && /* The mode. */ 10467 (oa->op_type == OP_CONST) && 10468 SvPOK(((SVOP*)oa)->op_sv) && 10469 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) && 10470 mode[0] == '>' && mode[1] == '&' && /* A dup open. */ 10471 (last == OpSIBLING(oa))) /* The bareword. */ 10472 last->op_private &= ~OPpCONST_STRICT; 10473 } 10474 return ck_fun(o); 10475 } 10476 10477 OP * 10478 Perl_ck_prototype(pTHX_ OP *o) 10479 { 10480 PERL_ARGS_ASSERT_CK_PROTOTYPE; 10481 if (!(o->op_flags & OPf_KIDS)) { 10482 op_free(o); 10483 return newUNOP(OP_PROTOTYPE, 0, newDEFSVOP()); 10484 } 10485 return o; 10486 } 10487 10488 OP * 10489 Perl_ck_refassign(pTHX_ OP *o) 10490 { 10491 OP * const right = cLISTOPo->op_first; 10492 OP * const left = OpSIBLING(right); 10493 OP *varop = cUNOPx(cUNOPx(left)->op_first)->op_first; 10494 bool stacked = 0; 10495 10496 PERL_ARGS_ASSERT_CK_REFASSIGN; 10497 assert (left); 10498 assert (left->op_type == OP_SREFGEN); 10499 10500 o->op_private = 0; 10501 /* we use OPpPAD_STATE in refassign to mean either of those things, 10502 * and the code assumes the two flags occupy the same bit position 10503 * in the various ops below */ 10504 assert(OPpPAD_STATE == OPpOUR_INTRO); 10505 10506 switch (varop->op_type) { 10507 case OP_PADAV: 10508 o->op_private |= OPpLVREF_AV; 10509 goto settarg; 10510 case OP_PADHV: 10511 o->op_private |= OPpLVREF_HV; 10512 /* FALLTHROUGH */ 10513 case OP_PADSV: 10514 settarg: 10515 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpPAD_STATE)); 10516 o->op_targ = varop->op_targ; 10517 varop->op_targ = 0; 10518 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX); 10519 break; 10520 10521 case OP_RV2AV: 10522 o->op_private |= OPpLVREF_AV; 10523 goto checkgv; 10524 NOT_REACHED; /* NOTREACHED */ 10525 case OP_RV2HV: 10526 o->op_private |= OPpLVREF_HV; 10527 /* FALLTHROUGH */ 10528 case OP_RV2SV: 10529 checkgv: 10530 o->op_private |= (varop->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)); 10531 if (cUNOPx(varop)->op_first->op_type != OP_GV) goto bad; 10532 detach_and_stack: 10533 /* Point varop to its GV kid, detached. */ 10534 varop = op_sibling_splice(varop, NULL, -1, NULL); 10535 stacked = TRUE; 10536 break; 10537 case OP_RV2CV: { 10538 OP * const kidparent = 10539 OpSIBLING(cUNOPx(cUNOPx(varop)->op_first)->op_first); 10540 OP * const kid = cUNOPx(kidparent)->op_first; 10541 o->op_private |= OPpLVREF_CV; 10542 if (kid->op_type == OP_GV) { 10543 varop = kidparent; 10544 goto detach_and_stack; 10545 } 10546 if (kid->op_type != OP_PADCV) goto bad; 10547 o->op_targ = kid->op_targ; 10548 kid->op_targ = 0; 10549 break; 10550 } 10551 case OP_AELEM: 10552 case OP_HELEM: 10553 o->op_private |= (varop->op_private & OPpLVAL_INTRO); 10554 o->op_private |= OPpLVREF_ELEM; 10555 op_null(varop); 10556 stacked = TRUE; 10557 /* Detach varop. */ 10558 op_sibling_splice(cUNOPx(left)->op_first, NULL, -1, NULL); 10559 break; 10560 default: 10561 bad: 10562 /* diag_listed_as: Can't modify reference to %s in %s assignment */ 10563 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in scalar " 10564 "assignment", 10565 OP_DESC(varop))); 10566 return o; 10567 } 10568 if (!FEATURE_REFALIASING_IS_ENABLED) 10569 Perl_croak(aTHX_ 10570 "Experimental aliasing via reference not enabled"); 10571 Perl_ck_warner_d(aTHX_ 10572 packWARN(WARN_EXPERIMENTAL__REFALIASING), 10573 "Aliasing via reference is experimental"); 10574 if (stacked) { 10575 o->op_flags |= OPf_STACKED; 10576 op_sibling_splice(o, right, 1, varop); 10577 } 10578 else { 10579 o->op_flags &=~ OPf_STACKED; 10580 op_sibling_splice(o, right, 1, NULL); 10581 } 10582 op_free(left); 10583 return o; 10584 } 10585 10586 OP * 10587 Perl_ck_repeat(pTHX_ OP *o) 10588 { 10589 PERL_ARGS_ASSERT_CK_REPEAT; 10590 10591 if (cBINOPo->op_first->op_flags & OPf_PARENS) { 10592 OP* kids; 10593 o->op_private |= OPpREPEAT_DOLIST; 10594 kids = op_sibling_splice(o, NULL, 1, NULL); /* detach first kid */ 10595 kids = force_list(kids, 1); /* promote it to a list */ 10596 op_sibling_splice(o, NULL, 0, kids); /* and add back */ 10597 } 10598 else 10599 scalar(o); 10600 return o; 10601 } 10602 10603 OP * 10604 Perl_ck_require(pTHX_ OP *o) 10605 { 10606 GV* gv; 10607 10608 PERL_ARGS_ASSERT_CK_REQUIRE; 10609 10610 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */ 10611 SVOP * const kid = (SVOP*)cUNOPo->op_first; 10612 HEK *hek; 10613 U32 hash; 10614 char *s; 10615 STRLEN len; 10616 if (kid->op_type == OP_CONST) { 10617 SV * const sv = kid->op_sv; 10618 U32 const was_readonly = SvREADONLY(sv); 10619 if (kid->op_private & OPpCONST_BARE) { 10620 dVAR; 10621 const char *end; 10622 10623 if (was_readonly) { 10624 SvREADONLY_off(sv); 10625 } 10626 if (SvIsCOW(sv)) sv_force_normal_flags(sv, 0); 10627 10628 s = SvPVX(sv); 10629 len = SvCUR(sv); 10630 end = s + len; 10631 for (; s < end; s++) { 10632 if (*s == ':' && s[1] == ':') { 10633 *s = '/'; 10634 Move(s+2, s+1, end - s - 1, char); 10635 --end; 10636 } 10637 } 10638 SvEND_set(sv, end); 10639 sv_catpvs(sv, ".pm"); 10640 PERL_HASH(hash, SvPVX(sv), SvCUR(sv)); 10641 hek = share_hek(SvPVX(sv), 10642 (SSize_t)SvCUR(sv) * (SvUTF8(sv) ? -1 : 1), 10643 hash); 10644 sv_sethek(sv, hek); 10645 unshare_hek(hek); 10646 SvFLAGS(sv) |= was_readonly; 10647 } 10648 else if (SvPOK(sv) && !SvNIOK(sv) && !SvGMAGICAL(sv) 10649 && !SvVOK(sv)) { 10650 s = SvPV(sv, len); 10651 if (SvREFCNT(sv) > 1) { 10652 kid->op_sv = newSVpvn_share( 10653 s, SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len, 0); 10654 SvREFCNT_dec_NN(sv); 10655 } 10656 else { 10657 dVAR; 10658 if (was_readonly) SvREADONLY_off(sv); 10659 PERL_HASH(hash, s, len); 10660 hek = share_hek(s, 10661 SvUTF8(sv) ? -(SSize_t)len : (SSize_t)len, 10662 hash); 10663 sv_sethek(sv, hek); 10664 unshare_hek(hek); 10665 SvFLAGS(sv) |= was_readonly; 10666 } 10667 } 10668 } 10669 } 10670 10671 if (!(o->op_flags & OPf_SPECIAL) /* Wasn't written as CORE::require */ 10672 /* handle override, if any */ 10673 && (gv = gv_override("require", 7))) { 10674 OP *kid, *newop; 10675 if (o->op_flags & OPf_KIDS) { 10676 kid = cUNOPo->op_first; 10677 op_sibling_splice(o, NULL, -1, NULL); 10678 } 10679 else { 10680 kid = newDEFSVOP(); 10681 } 10682 op_free(o); 10683 newop = S_new_entersubop(aTHX_ gv, kid); 10684 return newop; 10685 } 10686 10687 return ck_fun(o); 10688 } 10689 10690 OP * 10691 Perl_ck_return(pTHX_ OP *o) 10692 { 10693 OP *kid; 10694 10695 PERL_ARGS_ASSERT_CK_RETURN; 10696 10697 kid = OpSIBLING(cLISTOPo->op_first); 10698 if (CvLVALUE(PL_compcv)) { 10699 for (; kid; kid = OpSIBLING(kid)) 10700 op_lvalue(kid, OP_LEAVESUBLV); 10701 } 10702 10703 return o; 10704 } 10705 10706 OP * 10707 Perl_ck_select(pTHX_ OP *o) 10708 { 10709 dVAR; 10710 OP* kid; 10711 10712 PERL_ARGS_ASSERT_CK_SELECT; 10713 10714 if (o->op_flags & OPf_KIDS) { 10715 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ 10716 if (kid && OpHAS_SIBLING(kid)) { 10717 OpTYPE_set(o, OP_SSELECT); 10718 o = ck_fun(o); 10719 return fold_constants(op_integerize(op_std_init(o))); 10720 } 10721 } 10722 o = ck_fun(o); 10723 kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ 10724 if (kid && kid->op_type == OP_RV2GV) 10725 kid->op_private &= ~HINT_STRICT_REFS; 10726 return o; 10727 } 10728 10729 OP * 10730 Perl_ck_shift(pTHX_ OP *o) 10731 { 10732 const I32 type = o->op_type; 10733 10734 PERL_ARGS_ASSERT_CK_SHIFT; 10735 10736 if (!(o->op_flags & OPf_KIDS)) { 10737 OP *argop; 10738 10739 if (!CvUNIQUE(PL_compcv)) { 10740 o->op_flags |= OPf_SPECIAL; 10741 return o; 10742 } 10743 10744 argop = newUNOP(OP_RV2AV, 0, scalar(newGVOP(OP_GV, 0, PL_argvgv))); 10745 op_free(o); 10746 return newUNOP(type, 0, scalar(argop)); 10747 } 10748 return scalar(ck_fun(o)); 10749 } 10750 10751 OP * 10752 Perl_ck_sort(pTHX_ OP *o) 10753 { 10754 OP *firstkid; 10755 OP *kid; 10756 HV * const hinthv = 10757 PL_hints & HINT_LOCALIZE_HH ? GvHV(PL_hintgv) : NULL; 10758 U8 stacked; 10759 10760 PERL_ARGS_ASSERT_CK_SORT; 10761 10762 if (hinthv) { 10763 SV ** const svp = hv_fetchs(hinthv, "sort", FALSE); 10764 if (svp) { 10765 const I32 sorthints = (I32)SvIV(*svp); 10766 if ((sorthints & HINT_SORT_QUICKSORT) != 0) 10767 o->op_private |= OPpSORT_QSORT; 10768 if ((sorthints & HINT_SORT_STABLE) != 0) 10769 o->op_private |= OPpSORT_STABLE; 10770 } 10771 } 10772 10773 if (o->op_flags & OPf_STACKED) 10774 simplify_sort(o); 10775 firstkid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ 10776 10777 if ((stacked = o->op_flags & OPf_STACKED)) { /* may have been cleared */ 10778 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */ 10779 10780 /* if the first arg is a code block, process it and mark sort as 10781 * OPf_SPECIAL */ 10782 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) { 10783 LINKLIST(kid); 10784 if (kid->op_type == OP_LEAVE) 10785 op_null(kid); /* wipe out leave */ 10786 /* Prevent execution from escaping out of the sort block. */ 10787 kid->op_next = 0; 10788 10789 /* provide scalar context for comparison function/block */ 10790 kid = scalar(firstkid); 10791 kid->op_next = kid; 10792 o->op_flags |= OPf_SPECIAL; 10793 } 10794 else if (kid->op_type == OP_CONST 10795 && kid->op_private & OPpCONST_BARE) { 10796 char tmpbuf[256]; 10797 STRLEN len; 10798 PADOFFSET off; 10799 const char * const name = SvPV(kSVOP_sv, len); 10800 *tmpbuf = '&'; 10801 assert (len < 256); 10802 Copy(name, tmpbuf+1, len, char); 10803 off = pad_findmy_pvn(tmpbuf, len+1, SvUTF8(kSVOP_sv)); 10804 if (off != NOT_IN_PAD) { 10805 if (PAD_COMPNAME_FLAGS_isOUR(off)) { 10806 SV * const fq = 10807 newSVhek(HvNAME_HEK(PAD_COMPNAME_OURSTASH(off))); 10808 sv_catpvs(fq, "::"); 10809 sv_catsv(fq, kSVOP_sv); 10810 SvREFCNT_dec_NN(kSVOP_sv); 10811 kSVOP->op_sv = fq; 10812 } 10813 else { 10814 OP * const padop = newOP(OP_PADCV, 0); 10815 padop->op_targ = off; 10816 /* replace the const op with the pad op */ 10817 op_sibling_splice(firstkid, NULL, 1, padop); 10818 op_free(kid); 10819 } 10820 } 10821 } 10822 10823 firstkid = OpSIBLING(firstkid); 10824 } 10825 10826 for (kid = firstkid; kid; kid = OpSIBLING(kid)) { 10827 /* provide list context for arguments */ 10828 list(kid); 10829 if (stacked) 10830 op_lvalue(kid, OP_GREPSTART); 10831 } 10832 10833 return o; 10834 } 10835 10836 /* for sort { X } ..., where X is one of 10837 * $a <=> $b, $b <= $a, $a cmp $b, $b cmp $a 10838 * elide the second child of the sort (the one containing X), 10839 * and set these flags as appropriate 10840 OPpSORT_NUMERIC; 10841 OPpSORT_INTEGER; 10842 OPpSORT_DESCEND; 10843 * Also, check and warn on lexical $a, $b. 10844 */ 10845 10846 STATIC void 10847 S_simplify_sort(pTHX_ OP *o) 10848 { 10849 OP *kid = OpSIBLING(cLISTOPo->op_first); /* get past pushmark */ 10850 OP *k; 10851 int descending; 10852 GV *gv; 10853 const char *gvname; 10854 bool have_scopeop; 10855 10856 PERL_ARGS_ASSERT_SIMPLIFY_SORT; 10857 10858 kid = kUNOP->op_first; /* get past null */ 10859 if (!(have_scopeop = kid->op_type == OP_SCOPE) 10860 && kid->op_type != OP_LEAVE) 10861 return; 10862 kid = kLISTOP->op_last; /* get past scope */ 10863 switch(kid->op_type) { 10864 case OP_NCMP: 10865 case OP_I_NCMP: 10866 case OP_SCMP: 10867 if (!have_scopeop) goto padkids; 10868 break; 10869 default: 10870 return; 10871 } 10872 k = kid; /* remember this node*/ 10873 if (kBINOP->op_first->op_type != OP_RV2SV 10874 || kBINOP->op_last ->op_type != OP_RV2SV) 10875 { 10876 /* 10877 Warn about my($a) or my($b) in a sort block, *if* $a or $b is 10878 then used in a comparison. This catches most, but not 10879 all cases. For instance, it catches 10880 sort { my($a); $a <=> $b } 10881 but not 10882 sort { my($a); $a < $b ? -1 : $a == $b ? 0 : 1; } 10883 (although why you'd do that is anyone's guess). 10884 */ 10885 10886 padkids: 10887 if (!ckWARN(WARN_SYNTAX)) return; 10888 kid = kBINOP->op_first; 10889 do { 10890 if (kid->op_type == OP_PADSV) { 10891 PADNAME * const name = PAD_COMPNAME(kid->op_targ); 10892 if (PadnameLEN(name) == 2 && *PadnamePV(name) == '$' 10893 && ( PadnamePV(name)[1] == 'a' 10894 || PadnamePV(name)[1] == 'b' )) 10895 /* diag_listed_as: "my %s" used in sort comparison */ 10896 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 10897 "\"%s %s\" used in sort comparison", 10898 PadnameIsSTATE(name) 10899 ? "state" 10900 : "my", 10901 PadnamePV(name)); 10902 } 10903 } while ((kid = OpSIBLING(kid))); 10904 return; 10905 } 10906 kid = kBINOP->op_first; /* get past cmp */ 10907 if (kUNOP->op_first->op_type != OP_GV) 10908 return; 10909 kid = kUNOP->op_first; /* get past rv2sv */ 10910 gv = kGVOP_gv; 10911 if (GvSTASH(gv) != PL_curstash) 10912 return; 10913 gvname = GvNAME(gv); 10914 if (*gvname == 'a' && gvname[1] == '\0') 10915 descending = 0; 10916 else if (*gvname == 'b' && gvname[1] == '\0') 10917 descending = 1; 10918 else 10919 return; 10920 10921 kid = k; /* back to cmp */ 10922 /* already checked above that it is rv2sv */ 10923 kid = kBINOP->op_last; /* down to 2nd arg */ 10924 if (kUNOP->op_first->op_type != OP_GV) 10925 return; 10926 kid = kUNOP->op_first; /* get past rv2sv */ 10927 gv = kGVOP_gv; 10928 if (GvSTASH(gv) != PL_curstash) 10929 return; 10930 gvname = GvNAME(gv); 10931 if ( descending 10932 ? !(*gvname == 'a' && gvname[1] == '\0') 10933 : !(*gvname == 'b' && gvname[1] == '\0')) 10934 return; 10935 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL); 10936 if (descending) 10937 o->op_private |= OPpSORT_DESCEND; 10938 if (k->op_type == OP_NCMP) 10939 o->op_private |= OPpSORT_NUMERIC; 10940 if (k->op_type == OP_I_NCMP) 10941 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER; 10942 kid = OpSIBLING(cLISTOPo->op_first); 10943 /* cut out and delete old block (second sibling) */ 10944 op_sibling_splice(o, cLISTOPo->op_first, 1, NULL); 10945 op_free(kid); 10946 } 10947 10948 OP * 10949 Perl_ck_split(pTHX_ OP *o) 10950 { 10951 dVAR; 10952 OP *kid; 10953 10954 PERL_ARGS_ASSERT_CK_SPLIT; 10955 10956 if (o->op_flags & OPf_STACKED) 10957 return no_fh_allowed(o); 10958 10959 kid = cLISTOPo->op_first; 10960 if (kid->op_type != OP_NULL) 10961 Perl_croak(aTHX_ "panic: ck_split, type=%u", (unsigned) kid->op_type); 10962 /* delete leading NULL node, then add a CONST if no other nodes */ 10963 op_sibling_splice(o, NULL, 1, 10964 OpHAS_SIBLING(kid) ? NULL : newSVOP(OP_CONST, 0, newSVpvs(" "))); 10965 op_free(kid); 10966 kid = cLISTOPo->op_first; 10967 10968 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) { 10969 /* remove kid, and replace with new optree */ 10970 op_sibling_splice(o, NULL, 1, NULL); 10971 /* OPf_SPECIAL is used to trigger split " " behavior */ 10972 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, NULL, 0, 0); 10973 op_sibling_splice(o, NULL, 0, kid); 10974 } 10975 OpTYPE_set(kid, OP_PUSHRE); 10976 /* target implies @ary=..., so wipe it */ 10977 kid->op_targ = 0; 10978 scalar(kid); 10979 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL) { 10980 Perl_ck_warner(aTHX_ packWARN(WARN_REGEXP), 10981 "Use of /g modifier is meaningless in split"); 10982 } 10983 10984 if (!OpHAS_SIBLING(kid)) 10985 op_append_elem(OP_SPLIT, o, newDEFSVOP()); 10986 10987 kid = OpSIBLING(kid); 10988 assert(kid); 10989 scalar(kid); 10990 10991 if (!OpHAS_SIBLING(kid)) 10992 { 10993 op_append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0))); 10994 o->op_private |= OPpSPLIT_IMPLIM; 10995 } 10996 assert(OpHAS_SIBLING(kid)); 10997 10998 kid = OpSIBLING(kid); 10999 scalar(kid); 11000 11001 if (OpHAS_SIBLING(kid)) 11002 return too_many_arguments_pv(o,OP_DESC(o), 0); 11003 11004 return o; 11005 } 11006 11007 OP * 11008 Perl_ck_stringify(pTHX_ OP *o) 11009 { 11010 OP * const kid = OpSIBLING(cUNOPo->op_first); 11011 PERL_ARGS_ASSERT_CK_STRINGIFY; 11012 if (( kid->op_type == OP_JOIN || kid->op_type == OP_QUOTEMETA 11013 || kid->op_type == OP_LC || kid->op_type == OP_LCFIRST 11014 || kid->op_type == OP_UC || kid->op_type == OP_UCFIRST) 11015 && !OpHAS_SIBLING(kid)) /* syntax errs can leave extra children */ 11016 { 11017 op_sibling_splice(o, cUNOPo->op_first, -1, NULL); 11018 op_free(o); 11019 return kid; 11020 } 11021 return ck_fun(o); 11022 } 11023 11024 OP * 11025 Perl_ck_join(pTHX_ OP *o) 11026 { 11027 OP * const kid = OpSIBLING(cLISTOPo->op_first); 11028 11029 PERL_ARGS_ASSERT_CK_JOIN; 11030 11031 if (kid && kid->op_type == OP_MATCH) { 11032 if (ckWARN(WARN_SYNTAX)) { 11033 const REGEXP *re = PM_GETRE(kPMOP); 11034 const SV *msg = re 11035 ? newSVpvn_flags( RX_PRECOMP_const(re), RX_PRELEN(re), 11036 SVs_TEMP | ( RX_UTF8(re) ? SVf_UTF8 : 0 ) ) 11037 : newSVpvs_flags( "STRING", SVs_TEMP ); 11038 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 11039 "/%"SVf"/ should probably be written as \"%"SVf"\"", 11040 SVfARG(msg), SVfARG(msg)); 11041 } 11042 } 11043 if (kid 11044 && (kid->op_type == OP_CONST /* an innocent, unsuspicious separator */ 11045 || (kid->op_type == OP_PADSV && !(kid->op_private & OPpLVAL_INTRO)) 11046 || ( kid->op_type==OP_RV2SV && kUNOP->op_first->op_type == OP_GV 11047 && !(kid->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO))))) 11048 { 11049 const OP * const bairn = OpSIBLING(kid); /* the list */ 11050 if (bairn && !OpHAS_SIBLING(bairn) /* single-item list */ 11051 && OP_GIMME(bairn,0) == G_SCALAR) 11052 { 11053 OP * const ret = op_convert_list(OP_STRINGIFY, OPf_FOLDED, 11054 op_sibling_splice(o, kid, 1, NULL)); 11055 op_free(o); 11056 return ret; 11057 } 11058 } 11059 11060 return ck_fun(o); 11061 } 11062 11063 /* 11064 =for apidoc Am|CV *|rv2cv_op_cv|OP *cvop|U32 flags 11065 11066 Examines an op, which is expected to identify a subroutine at runtime, 11067 and attempts to determine at compile time which subroutine it identifies. 11068 This is normally used during Perl compilation to determine whether 11069 a prototype can be applied to a function call. C<cvop> is the op 11070 being considered, normally an C<rv2cv> op. A pointer to the identified 11071 subroutine is returned, if it could be determined statically, and a null 11072 pointer is returned if it was not possible to determine statically. 11073 11074 Currently, the subroutine can be identified statically if the RV that the 11075 C<rv2cv> is to operate on is provided by a suitable C<gv> or C<const> op. 11076 A C<gv> op is suitable if the GV's CV slot is populated. A C<const> op is 11077 suitable if the constant value must be an RV pointing to a CV. Details of 11078 this process may change in future versions of Perl. If the C<rv2cv> op 11079 has the C<OPpENTERSUB_AMPER> flag set then no attempt is made to identify 11080 the subroutine statically: this flag is used to suppress compile-time 11081 magic on a subroutine call, forcing it to use default runtime behaviour. 11082 11083 If C<flags> has the bit C<RV2CVOPCV_MARK_EARLY> set, then the handling 11084 of a GV reference is modified. If a GV was examined and its CV slot was 11085 found to be empty, then the C<gv> op has the C<OPpEARLY_CV> flag set. 11086 If the op is not optimised away, and the CV slot is later populated with 11087 a subroutine having a prototype, that flag eventually triggers the warning 11088 "called too early to check prototype". 11089 11090 If C<flags> has the bit C<RV2CVOPCV_RETURN_NAME_GV> set, then instead 11091 of returning a pointer to the subroutine it returns a pointer to the 11092 GV giving the most appropriate name for the subroutine in this context. 11093 Normally this is just the C<CvGV> of the subroutine, but for an anonymous 11094 (C<CvANON>) subroutine that is referenced through a GV it will be the 11095 referencing GV. The resulting C<GV*> is cast to C<CV*> to be returned. 11096 A null pointer is returned as usual if there is no statically-determinable 11097 subroutine. 11098 11099 =cut 11100 */ 11101 11102 /* shared by toke.c:yylex */ 11103 CV * 11104 Perl_find_lexical_cv(pTHX_ PADOFFSET off) 11105 { 11106 PADNAME *name = PAD_COMPNAME(off); 11107 CV *compcv = PL_compcv; 11108 while (PadnameOUTER(name)) { 11109 assert(PARENT_PAD_INDEX(name)); 11110 compcv = CvOUTSIDE(compcv); 11111 name = PadlistNAMESARRAY(CvPADLIST(compcv)) 11112 [off = PARENT_PAD_INDEX(name)]; 11113 } 11114 assert(!PadnameIsOUR(name)); 11115 if (!PadnameIsSTATE(name) && PadnamePROTOCV(name)) { 11116 return PadnamePROTOCV(name); 11117 } 11118 return (CV *)AvARRAY(PadlistARRAY(CvPADLIST(compcv))[1])[off]; 11119 } 11120 11121 CV * 11122 Perl_rv2cv_op_cv(pTHX_ OP *cvop, U32 flags) 11123 { 11124 OP *rvop; 11125 CV *cv; 11126 GV *gv; 11127 PERL_ARGS_ASSERT_RV2CV_OP_CV; 11128 if (flags & ~RV2CVOPCV_FLAG_MASK) 11129 Perl_croak(aTHX_ "panic: rv2cv_op_cv bad flags %x", (unsigned)flags); 11130 if (cvop->op_type != OP_RV2CV) 11131 return NULL; 11132 if (cvop->op_private & OPpENTERSUB_AMPER) 11133 return NULL; 11134 if (!(cvop->op_flags & OPf_KIDS)) 11135 return NULL; 11136 rvop = cUNOPx(cvop)->op_first; 11137 switch (rvop->op_type) { 11138 case OP_GV: { 11139 gv = cGVOPx_gv(rvop); 11140 if (!isGV(gv)) { 11141 if (SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV) { 11142 cv = MUTABLE_CV(SvRV(gv)); 11143 gv = NULL; 11144 break; 11145 } 11146 if (flags & RV2CVOPCV_RETURN_STUB) 11147 return (CV *)gv; 11148 else return NULL; 11149 } 11150 cv = GvCVu(gv); 11151 if (!cv) { 11152 if (flags & RV2CVOPCV_MARK_EARLY) 11153 rvop->op_private |= OPpEARLY_CV; 11154 return NULL; 11155 } 11156 } break; 11157 case OP_CONST: { 11158 SV *rv = cSVOPx_sv(rvop); 11159 if (!SvROK(rv)) 11160 return NULL; 11161 cv = (CV*)SvRV(rv); 11162 gv = NULL; 11163 } break; 11164 case OP_PADCV: { 11165 cv = find_lexical_cv(rvop->op_targ); 11166 gv = NULL; 11167 } break; 11168 default: { 11169 return NULL; 11170 } NOT_REACHED; /* NOTREACHED */ 11171 } 11172 if (SvTYPE((SV*)cv) != SVt_PVCV) 11173 return NULL; 11174 if (flags & (RV2CVOPCV_RETURN_NAME_GV|RV2CVOPCV_MAYBE_NAME_GV)) { 11175 if ((!CvANON(cv) || !gv) && !CvLEXICAL(cv) 11176 && ((flags & RV2CVOPCV_RETURN_NAME_GV) || !CvNAMED(cv))) 11177 gv = CvGV(cv); 11178 return (CV*)gv; 11179 } else { 11180 return cv; 11181 } 11182 } 11183 11184 /* 11185 =for apidoc Am|OP *|ck_entersub_args_list|OP *entersubop 11186 11187 Performs the default fixup of the arguments part of an C<entersub> 11188 op tree. This consists of applying list context to each of the 11189 argument ops. This is the standard treatment used on a call marked 11190 with C<&>, or a method call, or a call through a subroutine reference, 11191 or any other call where the callee can't be identified at compile time, 11192 or a call where the callee has no prototype. 11193 11194 =cut 11195 */ 11196 11197 OP * 11198 Perl_ck_entersub_args_list(pTHX_ OP *entersubop) 11199 { 11200 OP *aop; 11201 11202 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_LIST; 11203 11204 aop = cUNOPx(entersubop)->op_first; 11205 if (!OpHAS_SIBLING(aop)) 11206 aop = cUNOPx(aop)->op_first; 11207 for (aop = OpSIBLING(aop); OpHAS_SIBLING(aop); aop = OpSIBLING(aop)) { 11208 /* skip the extra attributes->import() call implicitly added in 11209 * something like foo(my $x : bar) 11210 */ 11211 if ( aop->op_type == OP_ENTERSUB 11212 && (aop->op_flags & OPf_WANT) == OPf_WANT_VOID 11213 ) 11214 continue; 11215 list(aop); 11216 op_lvalue(aop, OP_ENTERSUB); 11217 } 11218 return entersubop; 11219 } 11220 11221 /* 11222 =for apidoc Am|OP *|ck_entersub_args_proto|OP *entersubop|GV *namegv|SV *protosv 11223 11224 Performs the fixup of the arguments part of an C<entersub> op tree 11225 based on a subroutine prototype. This makes various modifications to 11226 the argument ops, from applying context up to inserting C<refgen> ops, 11227 and checking the number and syntactic types of arguments, as directed by 11228 the prototype. This is the standard treatment used on a subroutine call, 11229 not marked with C<&>, where the callee can be identified at compile time 11230 and has a prototype. 11231 11232 C<protosv> supplies the subroutine prototype to be applied to the call. 11233 It may be a normal defined scalar, of which the string value will be used. 11234 Alternatively, for convenience, it may be a subroutine object (a C<CV*> 11235 that has been cast to C<SV*>) which has a prototype. The prototype 11236 supplied, in whichever form, does not need to match the actual callee 11237 referenced by the op tree. 11238 11239 If the argument ops disagree with the prototype, for example by having 11240 an unacceptable number of arguments, a valid op tree is returned anyway. 11241 The error is reflected in the parser state, normally resulting in a single 11242 exception at the top level of parsing which covers all the compilation 11243 errors that occurred. In the error message, the callee is referred to 11244 by the name defined by the C<namegv> parameter. 11245 11246 =cut 11247 */ 11248 11249 OP * 11250 Perl_ck_entersub_args_proto(pTHX_ OP *entersubop, GV *namegv, SV *protosv) 11251 { 11252 STRLEN proto_len; 11253 const char *proto, *proto_end; 11254 OP *aop, *prev, *cvop, *parent; 11255 int optional = 0; 11256 I32 arg = 0; 11257 I32 contextclass = 0; 11258 const char *e = NULL; 11259 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO; 11260 if (SvTYPE(protosv) == SVt_PVCV ? !SvPOK(protosv) : !SvOK(protosv)) 11261 Perl_croak(aTHX_ "panic: ck_entersub_args_proto CV with no proto, " 11262 "flags=%lx", (unsigned long) SvFLAGS(protosv)); 11263 if (SvTYPE(protosv) == SVt_PVCV) 11264 proto = CvPROTO(protosv), proto_len = CvPROTOLEN(protosv); 11265 else proto = SvPV(protosv, proto_len); 11266 proto = S_strip_spaces(aTHX_ proto, &proto_len); 11267 proto_end = proto + proto_len; 11268 parent = entersubop; 11269 aop = cUNOPx(entersubop)->op_first; 11270 if (!OpHAS_SIBLING(aop)) { 11271 parent = aop; 11272 aop = cUNOPx(aop)->op_first; 11273 } 11274 prev = aop; 11275 aop = OpSIBLING(aop); 11276 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; 11277 while (aop != cvop) { 11278 OP* o3 = aop; 11279 11280 if (proto >= proto_end) 11281 { 11282 SV * const namesv = cv_name((CV *)namegv, NULL, 0); 11283 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf, 11284 SVfARG(namesv)), SvUTF8(namesv)); 11285 return entersubop; 11286 } 11287 11288 switch (*proto) { 11289 case ';': 11290 optional = 1; 11291 proto++; 11292 continue; 11293 case '_': 11294 /* _ must be at the end */ 11295 if (proto[1] && !strchr(";@%", proto[1])) 11296 goto oops; 11297 /* FALLTHROUGH */ 11298 case '$': 11299 proto++; 11300 arg++; 11301 scalar(aop); 11302 break; 11303 case '%': 11304 case '@': 11305 list(aop); 11306 arg++; 11307 break; 11308 case '&': 11309 proto++; 11310 arg++; 11311 if ( o3->op_type != OP_UNDEF 11312 && (o3->op_type != OP_SREFGEN 11313 || ( cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type 11314 != OP_ANONCODE 11315 && cUNOPx(cUNOPx(o3)->op_first)->op_first->op_type 11316 != OP_RV2CV))) 11317 bad_type_gv(arg, namegv, o3, 11318 arg == 1 ? "block or sub {}" : "sub {}"); 11319 break; 11320 case '*': 11321 /* '*' allows any scalar type, including bareword */ 11322 proto++; 11323 arg++; 11324 if (o3->op_type == OP_RV2GV) 11325 goto wrapref; /* autoconvert GLOB -> GLOBref */ 11326 else if (o3->op_type == OP_CONST) 11327 o3->op_private &= ~OPpCONST_STRICT; 11328 scalar(aop); 11329 break; 11330 case '+': 11331 proto++; 11332 arg++; 11333 if (o3->op_type == OP_RV2AV || 11334 o3->op_type == OP_PADAV || 11335 o3->op_type == OP_RV2HV || 11336 o3->op_type == OP_PADHV 11337 ) { 11338 goto wrapref; 11339 } 11340 scalar(aop); 11341 break; 11342 case '[': case ']': 11343 goto oops; 11344 11345 case '\\': 11346 proto++; 11347 arg++; 11348 again: 11349 switch (*proto++) { 11350 case '[': 11351 if (contextclass++ == 0) { 11352 e = strchr(proto, ']'); 11353 if (!e || e == proto) 11354 goto oops; 11355 } 11356 else 11357 goto oops; 11358 goto again; 11359 11360 case ']': 11361 if (contextclass) { 11362 const char *p = proto; 11363 const char *const end = proto; 11364 contextclass = 0; 11365 while (*--p != '[') 11366 /* \[$] accepts any scalar lvalue */ 11367 if (*p == '$' 11368 && Perl_op_lvalue_flags(aTHX_ 11369 scalar(o3), 11370 OP_READ, /* not entersub */ 11371 OP_LVALUE_NO_CROAK 11372 )) goto wrapref; 11373 bad_type_gv(arg, namegv, o3, 11374 Perl_form(aTHX_ "one of %.*s",(int)(end - p), p)); 11375 } else 11376 goto oops; 11377 break; 11378 case '*': 11379 if (o3->op_type == OP_RV2GV) 11380 goto wrapref; 11381 if (!contextclass) 11382 bad_type_gv(arg, namegv, o3, "symbol"); 11383 break; 11384 case '&': 11385 if (o3->op_type == OP_ENTERSUB 11386 && !(o3->op_flags & OPf_STACKED)) 11387 goto wrapref; 11388 if (!contextclass) 11389 bad_type_gv(arg, namegv, o3, "subroutine"); 11390 break; 11391 case '$': 11392 if (o3->op_type == OP_RV2SV || 11393 o3->op_type == OP_PADSV || 11394 o3->op_type == OP_HELEM || 11395 o3->op_type == OP_AELEM) 11396 goto wrapref; 11397 if (!contextclass) { 11398 /* \$ accepts any scalar lvalue */ 11399 if (Perl_op_lvalue_flags(aTHX_ 11400 scalar(o3), 11401 OP_READ, /* not entersub */ 11402 OP_LVALUE_NO_CROAK 11403 )) goto wrapref; 11404 bad_type_gv(arg, namegv, o3, "scalar"); 11405 } 11406 break; 11407 case '@': 11408 if (o3->op_type == OP_RV2AV || 11409 o3->op_type == OP_PADAV) 11410 { 11411 o3->op_flags &=~ OPf_PARENS; 11412 goto wrapref; 11413 } 11414 if (!contextclass) 11415 bad_type_gv(arg, namegv, o3, "array"); 11416 break; 11417 case '%': 11418 if (o3->op_type == OP_RV2HV || 11419 o3->op_type == OP_PADHV) 11420 { 11421 o3->op_flags &=~ OPf_PARENS; 11422 goto wrapref; 11423 } 11424 if (!contextclass) 11425 bad_type_gv(arg, namegv, o3, "hash"); 11426 break; 11427 wrapref: 11428 aop = S_op_sibling_newUNOP(aTHX_ parent, prev, 11429 OP_REFGEN, 0); 11430 if (contextclass && e) { 11431 proto = e + 1; 11432 contextclass = 0; 11433 } 11434 break; 11435 default: goto oops; 11436 } 11437 if (contextclass) 11438 goto again; 11439 break; 11440 case ' ': 11441 proto++; 11442 continue; 11443 default: 11444 oops: { 11445 Perl_croak(aTHX_ "Malformed prototype for %"SVf": %"SVf, 11446 SVfARG(cv_name((CV *)namegv, NULL, 0)), 11447 SVfARG(protosv)); 11448 } 11449 } 11450 11451 op_lvalue(aop, OP_ENTERSUB); 11452 prev = aop; 11453 aop = OpSIBLING(aop); 11454 } 11455 if (aop == cvop && *proto == '_') { 11456 /* generate an access to $_ */ 11457 op_sibling_splice(parent, prev, 0, newDEFSVOP()); 11458 } 11459 if (!optional && proto_end > proto && 11460 (*proto != '@' && *proto != '%' && *proto != ';' && *proto != '_')) 11461 { 11462 SV * const namesv = cv_name((CV *)namegv, NULL, 0); 11463 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf, 11464 SVfARG(namesv)), SvUTF8(namesv)); 11465 } 11466 return entersubop; 11467 } 11468 11469 /* 11470 =for apidoc Am|OP *|ck_entersub_args_proto_or_list|OP *entersubop|GV *namegv|SV *protosv 11471 11472 Performs the fixup of the arguments part of an C<entersub> op tree either 11473 based on a subroutine prototype or using default list-context processing. 11474 This is the standard treatment used on a subroutine call, not marked 11475 with C<&>, where the callee can be identified at compile time. 11476 11477 C<protosv> supplies the subroutine prototype to be applied to the call, 11478 or indicates that there is no prototype. It may be a normal scalar, 11479 in which case if it is defined then the string value will be used 11480 as a prototype, and if it is undefined then there is no prototype. 11481 Alternatively, for convenience, it may be a subroutine object (a C<CV*> 11482 that has been cast to C<SV*>), of which the prototype will be used if it 11483 has one. The prototype (or lack thereof) supplied, in whichever form, 11484 does not need to match the actual callee referenced by the op tree. 11485 11486 If the argument ops disagree with the prototype, for example by having 11487 an unacceptable number of arguments, a valid op tree is returned anyway. 11488 The error is reflected in the parser state, normally resulting in a single 11489 exception at the top level of parsing which covers all the compilation 11490 errors that occurred. In the error message, the callee is referred to 11491 by the name defined by the C<namegv> parameter. 11492 11493 =cut 11494 */ 11495 11496 OP * 11497 Perl_ck_entersub_args_proto_or_list(pTHX_ OP *entersubop, 11498 GV *namegv, SV *protosv) 11499 { 11500 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_PROTO_OR_LIST; 11501 if (SvTYPE(protosv) == SVt_PVCV ? SvPOK(protosv) : SvOK(protosv)) 11502 return ck_entersub_args_proto(entersubop, namegv, protosv); 11503 else 11504 return ck_entersub_args_list(entersubop); 11505 } 11506 11507 OP * 11508 Perl_ck_entersub_args_core(pTHX_ OP *entersubop, GV *namegv, SV *protosv) 11509 { 11510 int opnum = SvTYPE(protosv) == SVt_PVCV ? 0 : (int)SvUV(protosv); 11511 OP *aop = cUNOPx(entersubop)->op_first; 11512 11513 PERL_ARGS_ASSERT_CK_ENTERSUB_ARGS_CORE; 11514 11515 if (!opnum) { 11516 OP *cvop; 11517 if (!OpHAS_SIBLING(aop)) 11518 aop = cUNOPx(aop)->op_first; 11519 aop = OpSIBLING(aop); 11520 for (cvop = aop; OpSIBLING(cvop); cvop = OpSIBLING(cvop)) ; 11521 if (aop != cvop) 11522 (void)too_many_arguments_pv(entersubop, GvNAME(namegv), 0); 11523 11524 op_free(entersubop); 11525 switch(GvNAME(namegv)[2]) { 11526 case 'F': return newSVOP(OP_CONST, 0, 11527 newSVpv(CopFILE(PL_curcop),0)); 11528 case 'L': return newSVOP( 11529 OP_CONST, 0, 11530 Perl_newSVpvf(aTHX_ 11531 "%"IVdf, (IV)CopLINE(PL_curcop) 11532 ) 11533 ); 11534 case 'P': return newSVOP(OP_CONST, 0, 11535 (PL_curstash 11536 ? newSVhek(HvNAME_HEK(PL_curstash)) 11537 : &PL_sv_undef 11538 ) 11539 ); 11540 } 11541 NOT_REACHED; /* NOTREACHED */ 11542 } 11543 else { 11544 OP *prev, *cvop, *first, *parent; 11545 U32 flags = 0; 11546 11547 parent = entersubop; 11548 if (!OpHAS_SIBLING(aop)) { 11549 parent = aop; 11550 aop = cUNOPx(aop)->op_first; 11551 } 11552 11553 first = prev = aop; 11554 aop = OpSIBLING(aop); 11555 /* find last sibling */ 11556 for (cvop = aop; 11557 OpHAS_SIBLING(cvop); 11558 prev = cvop, cvop = OpSIBLING(cvop)) 11559 ; 11560 if (!(cvop->op_private & OPpENTERSUB_NOPAREN) 11561 /* Usually, OPf_SPECIAL on an op with no args means that it had 11562 * parens, but these have their own meaning for that flag: */ 11563 && opnum != OP_VALUES && opnum != OP_KEYS && opnum != OP_EACH 11564 && opnum != OP_DELETE && opnum != OP_EXISTS) 11565 flags |= OPf_SPECIAL; 11566 /* excise cvop from end of sibling chain */ 11567 op_sibling_splice(parent, prev, 1, NULL); 11568 op_free(cvop); 11569 if (aop == cvop) aop = NULL; 11570 11571 /* detach remaining siblings from the first sibling, then 11572 * dispose of original optree */ 11573 11574 if (aop) 11575 op_sibling_splice(parent, first, -1, NULL); 11576 op_free(entersubop); 11577 11578 if (opnum == OP_ENTEREVAL 11579 && GvNAMELEN(namegv)==9 && strnEQ(GvNAME(namegv), "evalbytes", 9)) 11580 flags |= OPpEVAL_BYTES <<8; 11581 11582 switch (PL_opargs[opnum] & OA_CLASS_MASK) { 11583 case OA_UNOP: 11584 case OA_BASEOP_OR_UNOP: 11585 case OA_FILESTATOP: 11586 return aop ? newUNOP(opnum,flags,aop) : newOP(opnum,flags); 11587 case OA_BASEOP: 11588 if (aop) { 11589 (void)too_many_arguments_pv(aop, GvNAME(namegv), 0); 11590 op_free(aop); 11591 } 11592 return opnum == OP_RUNCV 11593 ? newPVOP(OP_RUNCV,0,NULL) 11594 : newOP(opnum,0); 11595 default: 11596 return op_convert_list(opnum,0,aop); 11597 } 11598 } 11599 NOT_REACHED; /* NOTREACHED */ 11600 return entersubop; 11601 } 11602 11603 /* 11604 =for apidoc Am|void|cv_get_call_checker|CV *cv|Perl_call_checker *ckfun_p|SV **ckobj_p 11605 11606 Retrieves the function that will be used to fix up a call to C<cv>. 11607 Specifically, the function is applied to an C<entersub> op tree for a 11608 subroutine call, not marked with C<&>, where the callee can be identified 11609 at compile time as C<cv>. 11610 11611 The C-level function pointer is returned in C<*ckfun_p>, and an SV 11612 argument for it is returned in C<*ckobj_p>. The function is intended 11613 to be called in this manner: 11614 11615 entersubop = (*ckfun_p)(aTHX_ entersubop, namegv, (*ckobj_p)); 11616 11617 In this call, C<entersubop> is a pointer to the C<entersub> op, 11618 which may be replaced by the check function, and C<namegv> is a GV 11619 supplying the name that should be used by the check function to refer 11620 to the callee of the C<entersub> op if it needs to emit any diagnostics. 11621 It is permitted to apply the check function in non-standard situations, 11622 such as to a call to a different subroutine or to a method call. 11623 11624 By default, the function is 11625 L<Perl_ck_entersub_args_proto_or_list|/ck_entersub_args_proto_or_list>, 11626 and the SV parameter is C<cv> itself. This implements standard 11627 prototype processing. It can be changed, for a particular subroutine, 11628 by L</cv_set_call_checker>. 11629 11630 =cut 11631 */ 11632 11633 static void 11634 S_cv_get_call_checker(CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p, 11635 U8 *flagsp) 11636 { 11637 MAGIC *callmg; 11638 callmg = SvMAGICAL((SV*)cv) ? mg_find((SV*)cv, PERL_MAGIC_checkcall) : NULL; 11639 if (callmg) { 11640 *ckfun_p = DPTR2FPTR(Perl_call_checker, callmg->mg_ptr); 11641 *ckobj_p = callmg->mg_obj; 11642 if (flagsp) *flagsp = callmg->mg_flags; 11643 } else { 11644 *ckfun_p = Perl_ck_entersub_args_proto_or_list; 11645 *ckobj_p = (SV*)cv; 11646 if (flagsp) *flagsp = 0; 11647 } 11648 } 11649 11650 void 11651 Perl_cv_get_call_checker(pTHX_ CV *cv, Perl_call_checker *ckfun_p, SV **ckobj_p) 11652 { 11653 PERL_ARGS_ASSERT_CV_GET_CALL_CHECKER; 11654 PERL_UNUSED_CONTEXT; 11655 S_cv_get_call_checker(cv, ckfun_p, ckobj_p, NULL); 11656 } 11657 11658 /* 11659 =for apidoc Am|void|cv_set_call_checker_flags|CV *cv|Perl_call_checker ckfun|SV *ckobj|U32 flags 11660 11661 Sets the function that will be used to fix up a call to C<cv>. 11662 Specifically, the function is applied to an C<entersub> op tree for a 11663 subroutine call, not marked with C<&>, where the callee can be identified 11664 at compile time as C<cv>. 11665 11666 The C-level function pointer is supplied in C<ckfun>, and an SV argument 11667 for it is supplied in C<ckobj>. The function should be defined like this: 11668 11669 STATIC OP * ckfun(pTHX_ OP *op, GV *namegv, SV *ckobj) 11670 11671 It is intended to be called in this manner: 11672 11673 entersubop = ckfun(aTHX_ entersubop, namegv, ckobj); 11674 11675 In this call, C<entersubop> is a pointer to the C<entersub> op, 11676 which may be replaced by the check function, and C<namegv> supplies 11677 the name that should be used by the check function to refer 11678 to the callee of the C<entersub> op if it needs to emit any diagnostics. 11679 It is permitted to apply the check function in non-standard situations, 11680 such as to a call to a different subroutine or to a method call. 11681 11682 C<namegv> may not actually be a GV. For efficiency, perl may pass a 11683 CV or other SV instead. Whatever is passed can be used as the first 11684 argument to L</cv_name>. You can force perl to pass a GV by including 11685 C<CALL_CHECKER_REQUIRE_GV> in the C<flags>. 11686 11687 The current setting for a particular CV can be retrieved by 11688 L</cv_get_call_checker>. 11689 11690 =for apidoc Am|void|cv_set_call_checker|CV *cv|Perl_call_checker ckfun|SV *ckobj 11691 11692 The original form of L</cv_set_call_checker_flags>, which passes it the 11693 C<CALL_CHECKER_REQUIRE_GV> flag for backward-compatibility. 11694 11695 =cut 11696 */ 11697 11698 void 11699 Perl_cv_set_call_checker(pTHX_ CV *cv, Perl_call_checker ckfun, SV *ckobj) 11700 { 11701 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER; 11702 cv_set_call_checker_flags(cv, ckfun, ckobj, CALL_CHECKER_REQUIRE_GV); 11703 } 11704 11705 void 11706 Perl_cv_set_call_checker_flags(pTHX_ CV *cv, Perl_call_checker ckfun, 11707 SV *ckobj, U32 flags) 11708 { 11709 PERL_ARGS_ASSERT_CV_SET_CALL_CHECKER_FLAGS; 11710 if (ckfun == Perl_ck_entersub_args_proto_or_list && ckobj == (SV*)cv) { 11711 if (SvMAGICAL((SV*)cv)) 11712 mg_free_type((SV*)cv, PERL_MAGIC_checkcall); 11713 } else { 11714 MAGIC *callmg; 11715 sv_magic((SV*)cv, &PL_sv_undef, PERL_MAGIC_checkcall, NULL, 0); 11716 callmg = mg_find((SV*)cv, PERL_MAGIC_checkcall); 11717 assert(callmg); 11718 if (callmg->mg_flags & MGf_REFCOUNTED) { 11719 SvREFCNT_dec(callmg->mg_obj); 11720 callmg->mg_flags &= ~MGf_REFCOUNTED; 11721 } 11722 callmg->mg_ptr = FPTR2DPTR(char *, ckfun); 11723 callmg->mg_obj = ckobj; 11724 if (ckobj != (SV*)cv) { 11725 SvREFCNT_inc_simple_void_NN(ckobj); 11726 callmg->mg_flags |= MGf_REFCOUNTED; 11727 } 11728 callmg->mg_flags = (callmg->mg_flags &~ MGf_REQUIRE_GV) 11729 | (U8)(flags & MGf_REQUIRE_GV) | MGf_COPY; 11730 } 11731 } 11732 11733 static void 11734 S_entersub_alloc_targ(pTHX_ OP * const o) 11735 { 11736 o->op_targ = pad_alloc(OP_ENTERSUB, SVs_PADTMP); 11737 o->op_private |= OPpENTERSUB_HASTARG; 11738 } 11739 11740 OP * 11741 Perl_ck_subr(pTHX_ OP *o) 11742 { 11743 OP *aop, *cvop; 11744 CV *cv; 11745 GV *namegv; 11746 SV **const_class = NULL; 11747 11748 PERL_ARGS_ASSERT_CK_SUBR; 11749 11750 aop = cUNOPx(o)->op_first; 11751 if (!OpHAS_SIBLING(aop)) 11752 aop = cUNOPx(aop)->op_first; 11753 aop = OpSIBLING(aop); 11754 for (cvop = aop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; 11755 cv = rv2cv_op_cv(cvop, RV2CVOPCV_MARK_EARLY); 11756 namegv = cv ? (GV*)rv2cv_op_cv(cvop, RV2CVOPCV_MAYBE_NAME_GV) : NULL; 11757 11758 o->op_private &= ~1; 11759 o->op_private |= (PL_hints & HINT_STRICT_REFS); 11760 if (PERLDB_SUB && PL_curstash != PL_debstash) 11761 o->op_private |= OPpENTERSUB_DB; 11762 switch (cvop->op_type) { 11763 case OP_RV2CV: 11764 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER); 11765 op_null(cvop); 11766 break; 11767 case OP_METHOD: 11768 case OP_METHOD_NAMED: 11769 case OP_METHOD_SUPER: 11770 case OP_METHOD_REDIR: 11771 case OP_METHOD_REDIR_SUPER: 11772 if (aop->op_type == OP_CONST) { 11773 aop->op_private &= ~OPpCONST_STRICT; 11774 const_class = &cSVOPx(aop)->op_sv; 11775 } 11776 else if (aop->op_type == OP_LIST) { 11777 OP * const sib = OpSIBLING(((UNOP*)aop)->op_first); 11778 if (sib && sib->op_type == OP_CONST) { 11779 sib->op_private &= ~OPpCONST_STRICT; 11780 const_class = &cSVOPx(sib)->op_sv; 11781 } 11782 } 11783 /* make class name a shared cow string to speedup method calls */ 11784 /* constant string might be replaced with object, f.e. bigint */ 11785 if (const_class && SvPOK(*const_class)) { 11786 STRLEN len; 11787 const char* str = SvPV(*const_class, len); 11788 if (len) { 11789 SV* const shared = newSVpvn_share( 11790 str, SvUTF8(*const_class) 11791 ? -(SSize_t)len : (SSize_t)len, 11792 0 11793 ); 11794 if (SvREADONLY(*const_class)) 11795 SvREADONLY_on(shared); 11796 SvREFCNT_dec(*const_class); 11797 *const_class = shared; 11798 } 11799 } 11800 break; 11801 } 11802 11803 if (!cv) { 11804 S_entersub_alloc_targ(aTHX_ o); 11805 return ck_entersub_args_list(o); 11806 } else { 11807 Perl_call_checker ckfun; 11808 SV *ckobj; 11809 U8 flags; 11810 S_cv_get_call_checker(cv, &ckfun, &ckobj, &flags); 11811 if (CvISXSUB(cv) || !CvROOT(cv)) 11812 S_entersub_alloc_targ(aTHX_ o); 11813 if (!namegv) { 11814 /* The original call checker API guarantees that a GV will be 11815 be provided with the right name. So, if the old API was 11816 used (or the REQUIRE_GV flag was passed), we have to reify 11817 the CV’s GV, unless this is an anonymous sub. This is not 11818 ideal for lexical subs, as its stringification will include 11819 the package. But it is the best we can do. */ 11820 if (flags & MGf_REQUIRE_GV) { 11821 if (!CvANON(cv) && (!CvNAMED(cv) || CvNAME_HEK(cv))) 11822 namegv = CvGV(cv); 11823 } 11824 else namegv = MUTABLE_GV(cv); 11825 /* After a syntax error in a lexical sub, the cv that 11826 rv2cv_op_cv returns may be a nameless stub. */ 11827 if (!namegv) return ck_entersub_args_list(o); 11828 11829 } 11830 return ckfun(aTHX_ o, namegv, ckobj); 11831 } 11832 } 11833 11834 OP * 11835 Perl_ck_svconst(pTHX_ OP *o) 11836 { 11837 SV * const sv = cSVOPo->op_sv; 11838 PERL_ARGS_ASSERT_CK_SVCONST; 11839 PERL_UNUSED_CONTEXT; 11840 #ifdef PERL_COPY_ON_WRITE 11841 /* Since the read-only flag may be used to protect a string buffer, we 11842 cannot do copy-on-write with existing read-only scalars that are not 11843 already copy-on-write scalars. To allow $_ = "hello" to do COW with 11844 that constant, mark the constant as COWable here, if it is not 11845 already read-only. */ 11846 if (!SvREADONLY(sv) && !SvIsCOW(sv) && SvCANCOW(sv)) { 11847 SvIsCOW_on(sv); 11848 CowREFCNT(sv) = 0; 11849 # ifdef PERL_DEBUG_READONLY_COW 11850 sv_buf_to_ro(sv); 11851 # endif 11852 } 11853 #endif 11854 SvREADONLY_on(sv); 11855 return o; 11856 } 11857 11858 OP * 11859 Perl_ck_trunc(pTHX_ OP *o) 11860 { 11861 PERL_ARGS_ASSERT_CK_TRUNC; 11862 11863 if (o->op_flags & OPf_KIDS) { 11864 SVOP *kid = (SVOP*)cUNOPo->op_first; 11865 11866 if (kid->op_type == OP_NULL) 11867 kid = (SVOP*)OpSIBLING(kid); 11868 if (kid && kid->op_type == OP_CONST && 11869 (kid->op_private & OPpCONST_BARE) && 11870 !kid->op_folded) 11871 { 11872 o->op_flags |= OPf_SPECIAL; 11873 kid->op_private &= ~OPpCONST_STRICT; 11874 } 11875 } 11876 return ck_fun(o); 11877 } 11878 11879 OP * 11880 Perl_ck_substr(pTHX_ OP *o) 11881 { 11882 PERL_ARGS_ASSERT_CK_SUBSTR; 11883 11884 o = ck_fun(o); 11885 if ((o->op_flags & OPf_KIDS) && (o->op_private == 4)) { 11886 OP *kid = cLISTOPo->op_first; 11887 11888 if (kid->op_type == OP_NULL) 11889 kid = OpSIBLING(kid); 11890 if (kid) 11891 kid->op_flags |= OPf_MOD; 11892 11893 } 11894 return o; 11895 } 11896 11897 OP * 11898 Perl_ck_tell(pTHX_ OP *o) 11899 { 11900 PERL_ARGS_ASSERT_CK_TELL; 11901 o = ck_fun(o); 11902 if (o->op_flags & OPf_KIDS) { 11903 OP *kid = cLISTOPo->op_first; 11904 if (kid->op_type == OP_NULL && OpHAS_SIBLING(kid)) kid = OpSIBLING(kid); 11905 if (kid->op_type == OP_RV2GV) kid->op_private |= OPpALLOW_FAKE; 11906 } 11907 return o; 11908 } 11909 11910 OP * 11911 Perl_ck_each(pTHX_ OP *o) 11912 { 11913 dVAR; 11914 OP *kid = o->op_flags & OPf_KIDS ? cUNOPo->op_first : NULL; 11915 const unsigned orig_type = o->op_type; 11916 11917 PERL_ARGS_ASSERT_CK_EACH; 11918 11919 if (kid) { 11920 switch (kid->op_type) { 11921 case OP_PADHV: 11922 case OP_RV2HV: 11923 break; 11924 case OP_PADAV: 11925 case OP_RV2AV: 11926 OpTYPE_set(o, orig_type == OP_EACH ? OP_AEACH 11927 : orig_type == OP_KEYS ? OP_AKEYS 11928 : OP_AVALUES); 11929 break; 11930 case OP_CONST: 11931 if (kid->op_private == OPpCONST_BARE 11932 || !SvROK(cSVOPx_sv(kid)) 11933 || ( SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVAV 11934 && SvTYPE(SvRV(cSVOPx_sv(kid))) != SVt_PVHV ) 11935 ) 11936 /* we let ck_fun handle it */ 11937 break; 11938 default: 11939 Perl_croak_nocontext( 11940 "Experimental %s on scalar is now forbidden", 11941 PL_op_desc[orig_type]); 11942 break; 11943 } 11944 } 11945 return ck_fun(o); 11946 } 11947 11948 OP * 11949 Perl_ck_length(pTHX_ OP *o) 11950 { 11951 PERL_ARGS_ASSERT_CK_LENGTH; 11952 11953 o = ck_fun(o); 11954 11955 if (ckWARN(WARN_SYNTAX)) { 11956 const OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : NULL; 11957 11958 if (kid) { 11959 SV *name = NULL; 11960 const bool hash = kid->op_type == OP_PADHV 11961 || kid->op_type == OP_RV2HV; 11962 switch (kid->op_type) { 11963 case OP_PADHV: 11964 case OP_PADAV: 11965 case OP_RV2HV: 11966 case OP_RV2AV: 11967 name = S_op_varname(aTHX_ kid); 11968 break; 11969 default: 11970 return o; 11971 } 11972 if (name) 11973 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 11974 "length() used on %"SVf" (did you mean \"scalar(%s%"SVf 11975 ")\"?)", 11976 SVfARG(name), hash ? "keys " : "", SVfARG(name) 11977 ); 11978 else if (hash) 11979 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */ 11980 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 11981 "length() used on %%hash (did you mean \"scalar(keys %%hash)\"?)"); 11982 else 11983 /* diag_listed_as: length() used on %s (did you mean "scalar(%s)"?) */ 11984 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), 11985 "length() used on @array (did you mean \"scalar(@array)\"?)"); 11986 } 11987 } 11988 11989 return o; 11990 } 11991 11992 11993 11994 /* 11995 --------------------------------------------------------- 11996 11997 Common vars in list assignment 11998 11999 There now follows some enums and static functions for detecting 12000 common variables in list assignments. Here is a little essay I wrote 12001 for myself when trying to get my head around this. DAPM. 12002 12003 ---- 12004 12005 First some random observations: 12006 12007 * If a lexical var is an alias of something else, e.g. 12008 for my $x ($lex, $pkg, $a[0]) {...} 12009 then the act of aliasing will increase the reference count of the SV 12010 12011 * If a package var is an alias of something else, it may still have a 12012 reference count of 1, depending on how the alias was created, e.g. 12013 in *a = *b, $a may have a refcount of 1 since the GP is shared 12014 with a single GvSV pointer to the SV. So If it's an alias of another 12015 package var, then RC may be 1; if it's an alias of another scalar, e.g. 12016 a lexical var or an array element, then it will have RC > 1. 12017 12018 * There are many ways to create a package alias; ultimately, XS code 12019 may quite legally do GvSV(gv) = SvREFCNT_inc(sv) for example, so 12020 run-time tracing mechanisms are unlikely to be able to catch all cases. 12021 12022 * When the LHS is all my declarations, the same vars can't appear directly 12023 on the RHS, but they can indirectly via closures, aliasing and lvalue 12024 subs. But those techniques all involve an increase in the lexical 12025 scalar's ref count. 12026 12027 * When the LHS is all lexical vars (but not necessarily my declarations), 12028 it is possible for the same lexicals to appear directly on the RHS, and 12029 without an increased ref count, since the stack isn't refcounted. 12030 This case can be detected at compile time by scanning for common lex 12031 vars with PL_generation. 12032 12033 * lvalue subs defeat common var detection, but they do at least 12034 return vars with a temporary ref count increment. Also, you can't 12035 tell at compile time whether a sub call is lvalue. 12036 12037 12038 So... 12039 12040 A: There are a few circumstances where there definitely can't be any 12041 commonality: 12042 12043 LHS empty: () = (...); 12044 RHS empty: (....) = (); 12045 RHS contains only constants or other 'can't possibly be shared' 12046 elements (e.g. ops that return PADTMPs): (...) = (1,2, length) 12047 i.e. they only contain ops not marked as dangerous, whose children 12048 are also not dangerous; 12049 LHS ditto; 12050 LHS contains a single scalar element: e.g. ($x) = (....); because 12051 after $x has been modified, it won't be used again on the RHS; 12052 RHS contains a single element with no aggregate on LHS: e.g. 12053 ($a,$b,$c) = ($x); again, once $a has been modified, its value 12054 won't be used again. 12055 12056 B: If LHS are all 'my' lexical var declarations (or safe ops, which 12057 we can ignore): 12058 12059 my ($a, $b, @c) = ...; 12060 12061 Due to closure and goto tricks, these vars may already have content. 12062 For the same reason, an element on the RHS may be a lexical or package 12063 alias of one of the vars on the left, or share common elements, for 12064 example: 12065 12066 my ($x,$y) = f(); # $x and $y on both sides 12067 sub f : lvalue { ($x,$y) = (1,2); $y, $x } 12068 12069 and 12070 12071 my $ra = f(); 12072 my @a = @$ra; # elements of @a on both sides 12073 sub f { @a = 1..4; \@a } 12074 12075 12076 First, just consider scalar vars on LHS: 12077 12078 RHS is safe only if (A), or in addition, 12079 * contains only lexical *scalar* vars, where neither side's 12080 lexicals have been flagged as aliases 12081 12082 If RHS is not safe, then it's always legal to check LHS vars for 12083 RC==1, since the only RHS aliases will always be associated 12084 with an RC bump. 12085 12086 Note that in particular, RHS is not safe if: 12087 12088 * it contains package scalar vars; e.g.: 12089 12090 f(); 12091 my ($x, $y) = (2, $x_alias); 12092 sub f { $x = 1; *x_alias = \$x; } 12093 12094 * It contains other general elements, such as flattened or 12095 * spliced or single array or hash elements, e.g. 12096 12097 f(); 12098 my ($x,$y) = @a; # or $a[0] or @a{@b} etc 12099 12100 sub f { 12101 ($x, $y) = (1,2); 12102 use feature 'refaliasing'; 12103 \($a[0], $a[1]) = \($y,$x); 12104 } 12105 12106 It doesn't matter if the array/hash is lexical or package. 12107 12108 * it contains a function call that happens to be an lvalue 12109 sub which returns one or more of the above, e.g. 12110 12111 f(); 12112 my ($x,$y) = f(); 12113 12114 sub f : lvalue { 12115 ($x, $y) = (1,2); 12116 *x1 = \$x; 12117 $y, $x1; 12118 } 12119 12120 (so a sub call on the RHS should be treated the same 12121 as having a package var on the RHS). 12122 12123 * any other "dangerous" thing, such an op or built-in that 12124 returns one of the above, e.g. pp_preinc 12125 12126 12127 If RHS is not safe, what we can do however is at compile time flag 12128 that the LHS are all my declarations, and at run time check whether 12129 all the LHS have RC == 1, and if so skip the full scan. 12130 12131 Now consider array and hash vars on LHS: e.g. my (...,@a) = ...; 12132 12133 Here the issue is whether there can be elements of @a on the RHS 12134 which will get prematurely freed when @a is cleared prior to 12135 assignment. This is only a problem if the aliasing mechanism 12136 is one which doesn't increase the refcount - only if RC == 1 12137 will the RHS element be prematurely freed. 12138 12139 Because the array/hash is being INTROed, it or its elements 12140 can't directly appear on the RHS: 12141 12142 my (@a) = ($a[0], @a, etc) # NOT POSSIBLE 12143 12144 but can indirectly, e.g.: 12145 12146 my $r = f(); 12147 my (@a) = @$r; 12148 sub f { @a = 1..3; \@a } 12149 12150 So if the RHS isn't safe as defined by (A), we must always 12151 mortalise and bump the ref count of any remaining RHS elements 12152 when assigning to a non-empty LHS aggregate. 12153 12154 Lexical scalars on the RHS aren't safe if they've been involved in 12155 aliasing, e.g. 12156 12157 use feature 'refaliasing'; 12158 12159 f(); 12160 \(my $lex) = \$pkg; 12161 my @a = ($lex,3); # equivalent to ($a[0],3) 12162 12163 sub f { 12164 @a = (1,2); 12165 \$pkg = \$a[0]; 12166 } 12167 12168 Similarly with lexical arrays and hashes on the RHS: 12169 12170 f(); 12171 my @b; 12172 my @a = (@b); 12173 12174 sub f { 12175 @a = (1,2); 12176 \$b[0] = \$a[1]; 12177 \$b[1] = \$a[0]; 12178 } 12179 12180 12181 12182 C: As (B), but in addition the LHS may contain non-intro lexicals, e.g. 12183 my $a; ($a, my $b) = (....); 12184 12185 The difference between (B) and (C) is that it is now physically 12186 possible for the LHS vars to appear on the RHS too, where they 12187 are not reference counted; but in this case, the compile-time 12188 PL_generation sweep will detect such common vars. 12189 12190 So the rules for (C) differ from (B) in that if common vars are 12191 detected, the runtime "test RC==1" optimisation can no longer be used, 12192 and a full mark and sweep is required 12193 12194 D: As (C), but in addition the LHS may contain package vars. 12195 12196 Since package vars can be aliased without a corresponding refcount 12197 increase, all bets are off. It's only safe if (A). E.g. 12198 12199 my ($x, $y) = (1,2); 12200 12201 for $x_alias ($x) { 12202 ($x_alias, $y) = (3, $x); # whoops 12203 } 12204 12205 Ditto for LHS aggregate package vars. 12206 12207 E: Any other dangerous ops on LHS, e.g. 12208 (f(), $a[0], @$r) = (...); 12209 12210 this is similar to (E) in that all bets are off. In addition, it's 12211 impossible to determine at compile time whether the LHS 12212 contains a scalar or an aggregate, e.g. 12213 12214 sub f : lvalue { @a } 12215 (f()) = 1..3; 12216 12217 * --------------------------------------------------------- 12218 */ 12219 12220 12221 /* A set of bit flags returned by S_aassign_scan(). Each flag indicates 12222 * that at least one of the things flagged was seen. 12223 */ 12224 12225 enum { 12226 AAS_MY_SCALAR = 0x001, /* my $scalar */ 12227 AAS_MY_AGG = 0x002, /* aggregate: my @array or my %hash */ 12228 AAS_LEX_SCALAR = 0x004, /* $lexical */ 12229 AAS_LEX_AGG = 0x008, /* @lexical or %lexical aggregate */ 12230 AAS_LEX_SCALAR_COMM = 0x010, /* $lexical seen on both sides */ 12231 AAS_PKG_SCALAR = 0x020, /* $scalar (where $scalar is pkg var) */ 12232 AAS_PKG_AGG = 0x040, /* package @array or %hash aggregate */ 12233 AAS_DANGEROUS = 0x080, /* an op (other than the above) 12234 that's flagged OA_DANGEROUS */ 12235 AAS_SAFE_SCALAR = 0x100, /* produces at least one scalar SV that's 12236 not in any of the categories above */ 12237 AAS_DEFAV = 0x200 /* contains just a single '@_' on RHS */ 12238 }; 12239 12240 12241 12242 /* helper function for S_aassign_scan(). 12243 * check a PAD-related op for commonality and/or set its generation number. 12244 * Returns a boolean indicating whether its shared */ 12245 12246 static bool 12247 S_aassign_padcheck(pTHX_ OP* o, bool rhs) 12248 { 12249 if (PAD_COMPNAME_GEN(o->op_targ) == PERL_INT_MAX) 12250 /* lexical used in aliasing */ 12251 return TRUE; 12252 12253 if (rhs) 12254 return cBOOL(PAD_COMPNAME_GEN(o->op_targ) == (STRLEN)PL_generation); 12255 else 12256 PAD_COMPNAME_GEN_set(o->op_targ, PL_generation); 12257 12258 return FALSE; 12259 } 12260 12261 12262 /* 12263 Helper function for OPpASSIGN_COMMON* detection in rpeep(). 12264 It scans the left or right hand subtree of the aassign op, and returns a 12265 set of flags indicating what sorts of things it found there. 12266 'rhs' indicates whether we're scanning the LHS or RHS. If the former, we 12267 set PL_generation on lexical vars; if the latter, we see if 12268 PL_generation matches. 12269 'top' indicates whether we're recursing or at the top level. 12270 'scalars_p' is a pointer to a counter of the number of scalar SVs seen. 12271 This fn will increment it by the number seen. It's not intended to 12272 be an accurate count (especially as many ops can push a variable 12273 number of SVs onto the stack); rather it's used as to test whether there 12274 can be at most 1 SV pushed; so it's only meanings are "0, 1, many". 12275 */ 12276 12277 static int 12278 S_aassign_scan(pTHX_ OP* o, bool rhs, bool top, int *scalars_p) 12279 { 12280 int flags = 0; 12281 bool kid_top = FALSE; 12282 12283 /* first, look for a solitary @_ on the RHS */ 12284 if ( rhs 12285 && top 12286 && (o->op_flags & OPf_KIDS) 12287 && OP_TYPE_IS_OR_WAS(o, OP_LIST) 12288 ) { 12289 OP *kid = cUNOPo->op_first; 12290 if ( ( kid->op_type == OP_PUSHMARK 12291 || kid->op_type == OP_PADRANGE) /* ex-pushmark */ 12292 && ((kid = OpSIBLING(kid))) 12293 && !OpHAS_SIBLING(kid) 12294 && kid->op_type == OP_RV2AV 12295 && !(kid->op_flags & OPf_REF) 12296 && !(kid->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB)) 12297 && ((kid->op_flags & OPf_WANT) == OPf_WANT_LIST) 12298 && ((kid = cUNOPx(kid)->op_first)) 12299 && kid->op_type == OP_GV 12300 && cGVOPx_gv(kid) == PL_defgv 12301 ) 12302 flags |= AAS_DEFAV; 12303 } 12304 12305 switch (o->op_type) { 12306 case OP_GVSV: 12307 (*scalars_p)++; 12308 return AAS_PKG_SCALAR; 12309 12310 case OP_PADAV: 12311 case OP_PADHV: 12312 (*scalars_p) += 2; 12313 if (top && (o->op_flags & OPf_REF)) 12314 return (o->op_private & OPpLVAL_INTRO) 12315 ? AAS_MY_AGG : AAS_LEX_AGG; 12316 return AAS_DANGEROUS; 12317 12318 case OP_PADSV: 12319 { 12320 int comm = S_aassign_padcheck(aTHX_ o, rhs) 12321 ? AAS_LEX_SCALAR_COMM : 0; 12322 (*scalars_p)++; 12323 return (o->op_private & OPpLVAL_INTRO) 12324 ? (AAS_MY_SCALAR|comm) : (AAS_LEX_SCALAR|comm); 12325 } 12326 12327 case OP_RV2AV: 12328 case OP_RV2HV: 12329 (*scalars_p) += 2; 12330 if (cUNOPx(o)->op_first->op_type != OP_GV) 12331 return AAS_DANGEROUS; /* @{expr}, %{expr} */ 12332 /* @pkg, %pkg */ 12333 if (top && (o->op_flags & OPf_REF)) 12334 return AAS_PKG_AGG; 12335 return AAS_DANGEROUS; 12336 12337 case OP_RV2SV: 12338 (*scalars_p)++; 12339 if (cUNOPx(o)->op_first->op_type != OP_GV) { 12340 (*scalars_p) += 2; 12341 return AAS_DANGEROUS; /* ${expr} */ 12342 } 12343 return AAS_PKG_SCALAR; /* $pkg */ 12344 12345 case OP_SPLIT: 12346 if (cLISTOPo->op_first->op_type == OP_PUSHRE) { 12347 /* "@foo = split... " optimises away the aassign and stores its 12348 * destination array in the OP_PUSHRE that precedes it. 12349 * A flattened array is always dangerous. 12350 */ 12351 (*scalars_p) += 2; 12352 return AAS_DANGEROUS; 12353 } 12354 break; 12355 12356 case OP_UNDEF: 12357 /* undef counts as a scalar on the RHS: 12358 * (undef, $x) = ...; # only 1 scalar on LHS: always safe 12359 * ($x, $y) = (undef, $x); # 2 scalars on RHS: unsafe 12360 */ 12361 if (rhs) 12362 (*scalars_p)++; 12363 flags = AAS_SAFE_SCALAR; 12364 break; 12365 12366 case OP_PUSHMARK: 12367 case OP_STUB: 12368 /* these are all no-ops; they don't push a potentially common SV 12369 * onto the stack, so they are neither AAS_DANGEROUS nor 12370 * AAS_SAFE_SCALAR */ 12371 return 0; 12372 12373 case OP_PADRANGE: /* Ignore padrange; checking its siblings is enough */ 12374 break; 12375 12376 case OP_NULL: 12377 case OP_LIST: 12378 /* these do nothing but may have children; but their children 12379 * should also be treated as top-level */ 12380 kid_top = top; 12381 break; 12382 12383 default: 12384 if (PL_opargs[o->op_type] & OA_DANGEROUS) { 12385 (*scalars_p) += 2; 12386 flags = AAS_DANGEROUS; 12387 break; 12388 } 12389 12390 if ( (PL_opargs[o->op_type] & OA_TARGLEX) 12391 && (o->op_private & OPpTARGET_MY)) 12392 { 12393 (*scalars_p)++; 12394 return S_aassign_padcheck(aTHX_ o, rhs) 12395 ? AAS_LEX_SCALAR_COMM : AAS_LEX_SCALAR; 12396 } 12397 12398 /* if its an unrecognised, non-dangerous op, assume that it 12399 * it the cause of at least one safe scalar */ 12400 (*scalars_p)++; 12401 flags = AAS_SAFE_SCALAR; 12402 break; 12403 } 12404 12405 if (o->op_flags & OPf_KIDS) { 12406 OP *kid; 12407 for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) 12408 flags |= S_aassign_scan(aTHX_ kid, rhs, kid_top, scalars_p); 12409 } 12410 return flags; 12411 } 12412 12413 12414 /* Check for in place reverse and sort assignments like "@a = reverse @a" 12415 and modify the optree to make them work inplace */ 12416 12417 STATIC void 12418 S_inplace_aassign(pTHX_ OP *o) { 12419 12420 OP *modop, *modop_pushmark; 12421 OP *oright; 12422 OP *oleft, *oleft_pushmark; 12423 12424 PERL_ARGS_ASSERT_INPLACE_AASSIGN; 12425 12426 assert((o->op_flags & OPf_WANT) == OPf_WANT_VOID); 12427 12428 assert(cUNOPo->op_first->op_type == OP_NULL); 12429 modop_pushmark = cUNOPx(cUNOPo->op_first)->op_first; 12430 assert(modop_pushmark->op_type == OP_PUSHMARK); 12431 modop = OpSIBLING(modop_pushmark); 12432 12433 if (modop->op_type != OP_SORT && modop->op_type != OP_REVERSE) 12434 return; 12435 12436 /* no other operation except sort/reverse */ 12437 if (OpHAS_SIBLING(modop)) 12438 return; 12439 12440 assert(cUNOPx(modop)->op_first->op_type == OP_PUSHMARK); 12441 if (!(oright = OpSIBLING(cUNOPx(modop)->op_first))) return; 12442 12443 if (modop->op_flags & OPf_STACKED) { 12444 /* skip sort subroutine/block */ 12445 assert(oright->op_type == OP_NULL); 12446 oright = OpSIBLING(oright); 12447 } 12448 12449 assert(OpSIBLING(cUNOPo->op_first)->op_type == OP_NULL); 12450 oleft_pushmark = cUNOPx(OpSIBLING(cUNOPo->op_first))->op_first; 12451 assert(oleft_pushmark->op_type == OP_PUSHMARK); 12452 oleft = OpSIBLING(oleft_pushmark); 12453 12454 /* Check the lhs is an array */ 12455 if (!oleft || 12456 (oleft->op_type != OP_RV2AV && oleft->op_type != OP_PADAV) 12457 || OpHAS_SIBLING(oleft) 12458 || (oleft->op_private & OPpLVAL_INTRO) 12459 ) 12460 return; 12461 12462 /* Only one thing on the rhs */ 12463 if (OpHAS_SIBLING(oright)) 12464 return; 12465 12466 /* check the array is the same on both sides */ 12467 if (oleft->op_type == OP_RV2AV) { 12468 if (oright->op_type != OP_RV2AV 12469 || !cUNOPx(oright)->op_first 12470 || cUNOPx(oright)->op_first->op_type != OP_GV 12471 || cUNOPx(oleft )->op_first->op_type != OP_GV 12472 || cGVOPx_gv(cUNOPx(oleft)->op_first) != 12473 cGVOPx_gv(cUNOPx(oright)->op_first) 12474 ) 12475 return; 12476 } 12477 else if (oright->op_type != OP_PADAV 12478 || oright->op_targ != oleft->op_targ 12479 ) 12480 return; 12481 12482 /* This actually is an inplace assignment */ 12483 12484 modop->op_private |= OPpSORT_INPLACE; 12485 12486 /* transfer MODishness etc from LHS arg to RHS arg */ 12487 oright->op_flags = oleft->op_flags; 12488 12489 /* remove the aassign op and the lhs */ 12490 op_null(o); 12491 op_null(oleft_pushmark); 12492 if (oleft->op_type == OP_RV2AV && cUNOPx(oleft)->op_first) 12493 op_null(cUNOPx(oleft)->op_first); 12494 op_null(oleft); 12495 } 12496 12497 12498 12499 /* S_maybe_multideref(): given an op_next chain of ops beginning at 'start' 12500 * that potentially represent a series of one or more aggregate derefs 12501 * (such as $a->[1]{$key}), examine the chain, and if appropriate, convert 12502 * the whole chain to a single OP_MULTIDEREF op (maybe with a few 12503 * additional ops left in too). 12504 * 12505 * The caller will have already verified that the first few ops in the 12506 * chain following 'start' indicate a multideref candidate, and will have 12507 * set 'orig_o' to the point further on in the chain where the first index 12508 * expression (if any) begins. 'orig_action' specifies what type of 12509 * beginning has already been determined by the ops between start..orig_o 12510 * (e.g. $lex_ary[], $pkg_ary->{}, expr->[], etc). 12511 * 12512 * 'hints' contains any hints flags that need adding (currently just 12513 * OPpHINT_STRICT_REFS) as found in any rv2av/hv skipped by the caller. 12514 */ 12515 12516 STATIC void 12517 S_maybe_multideref(pTHX_ OP *start, OP *orig_o, UV orig_action, U8 hints) 12518 { 12519 dVAR; 12520 int pass; 12521 UNOP_AUX_item *arg_buf = NULL; 12522 bool reset_start_targ = FALSE; /* start->op_targ needs zeroing */ 12523 int index_skip = -1; /* don't output index arg on this action */ 12524 12525 /* similar to regex compiling, do two passes; the first pass 12526 * determines whether the op chain is convertible and calculates the 12527 * buffer size; the second pass populates the buffer and makes any 12528 * changes necessary to ops (such as moving consts to the pad on 12529 * threaded builds). 12530 * 12531 * NB: for things like Coverity, note that both passes take the same 12532 * path through the logic tree (except for 'if (pass)' bits), since 12533 * both passes are following the same op_next chain; and in 12534 * particular, if it would return early on the second pass, it would 12535 * already have returned early on the first pass. 12536 */ 12537 for (pass = 0; pass < 2; pass++) { 12538 OP *o = orig_o; 12539 UV action = orig_action; 12540 OP *first_elem_op = NULL; /* first seen aelem/helem */ 12541 OP *top_op = NULL; /* highest [ah]elem/exists/del/rv2[ah]v */ 12542 int action_count = 0; /* number of actions seen so far */ 12543 int action_ix = 0; /* action_count % (actions per IV) */ 12544 bool next_is_hash = FALSE; /* is the next lookup to be a hash? */ 12545 bool is_last = FALSE; /* no more derefs to follow */ 12546 bool maybe_aelemfast = FALSE; /* we can replace with aelemfast? */ 12547 UNOP_AUX_item *arg = arg_buf; 12548 UNOP_AUX_item *action_ptr = arg_buf; 12549 12550 if (pass) 12551 action_ptr->uv = 0; 12552 arg++; 12553 12554 switch (action) { 12555 case MDEREF_HV_gvsv_vivify_rv2hv_helem: 12556 case MDEREF_HV_gvhv_helem: 12557 next_is_hash = TRUE; 12558 /* FALLTHROUGH */ 12559 case MDEREF_AV_gvsv_vivify_rv2av_aelem: 12560 case MDEREF_AV_gvav_aelem: 12561 if (pass) { 12562 #ifdef USE_ITHREADS 12563 arg->pad_offset = cPADOPx(start)->op_padix; 12564 /* stop it being swiped when nulled */ 12565 cPADOPx(start)->op_padix = 0; 12566 #else 12567 arg->sv = cSVOPx(start)->op_sv; 12568 cSVOPx(start)->op_sv = NULL; 12569 #endif 12570 } 12571 arg++; 12572 break; 12573 12574 case MDEREF_HV_padhv_helem: 12575 case MDEREF_HV_padsv_vivify_rv2hv_helem: 12576 next_is_hash = TRUE; 12577 /* FALLTHROUGH */ 12578 case MDEREF_AV_padav_aelem: 12579 case MDEREF_AV_padsv_vivify_rv2av_aelem: 12580 if (pass) { 12581 arg->pad_offset = start->op_targ; 12582 /* we skip setting op_targ = 0 for now, since the intact 12583 * OP_PADXV is needed by S_check_hash_fields_and_hekify */ 12584 reset_start_targ = TRUE; 12585 } 12586 arg++; 12587 break; 12588 12589 case MDEREF_HV_pop_rv2hv_helem: 12590 next_is_hash = TRUE; 12591 /* FALLTHROUGH */ 12592 case MDEREF_AV_pop_rv2av_aelem: 12593 break; 12594 12595 default: 12596 NOT_REACHED; /* NOTREACHED */ 12597 return; 12598 } 12599 12600 while (!is_last) { 12601 /* look for another (rv2av/hv; get index; 12602 * aelem/helem/exists/delele) sequence */ 12603 12604 OP *kid; 12605 bool is_deref; 12606 bool ok; 12607 UV index_type = MDEREF_INDEX_none; 12608 12609 if (action_count) { 12610 /* if this is not the first lookup, consume the rv2av/hv */ 12611 12612 /* for N levels of aggregate lookup, we normally expect 12613 * that the first N-1 [ah]elem ops will be flagged as 12614 * /DEREF (so they autovivifiy if necessary), and the last 12615 * lookup op not to be. 12616 * For other things (like @{$h{k1}{k2}}) extra scope or 12617 * leave ops can appear, so abandon the effort in that 12618 * case */ 12619 if (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV) 12620 return; 12621 12622 /* rv2av or rv2hv sKR/1 */ 12623 12624 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS 12625 |OPf_REF|OPf_MOD|OPf_SPECIAL))); 12626 if (o->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF)) 12627 return; 12628 12629 /* at this point, we wouldn't expect any of these 12630 * possible private flags: 12631 * OPpMAYBE_LVSUB, OPpOUR_INTRO, OPpLVAL_INTRO 12632 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL (rv2hv only) 12633 */ 12634 ASSUME(!(o->op_private & 12635 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING))); 12636 12637 hints = (o->op_private & OPpHINT_STRICT_REFS); 12638 12639 /* make sure the type of the previous /DEREF matches the 12640 * type of the next lookup */ 12641 ASSUME(o->op_type == (next_is_hash ? OP_RV2HV : OP_RV2AV)); 12642 top_op = o; 12643 12644 action = next_is_hash 12645 ? MDEREF_HV_vivify_rv2hv_helem 12646 : MDEREF_AV_vivify_rv2av_aelem; 12647 o = o->op_next; 12648 } 12649 12650 /* if this is the second pass, and we're at the depth where 12651 * previously we encountered a non-simple index expression, 12652 * stop processing the index at this point */ 12653 if (action_count != index_skip) { 12654 12655 /* look for one or more simple ops that return an array 12656 * index or hash key */ 12657 12658 switch (o->op_type) { 12659 case OP_PADSV: 12660 /* it may be a lexical var index */ 12661 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_PARENS 12662 |OPf_REF|OPf_MOD|OPf_SPECIAL))); 12663 ASSUME(!(o->op_private & 12664 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO))); 12665 12666 if ( OP_GIMME(o,0) == G_SCALAR 12667 && !(o->op_flags & (OPf_REF|OPf_MOD)) 12668 && o->op_private == 0) 12669 { 12670 if (pass) 12671 arg->pad_offset = o->op_targ; 12672 arg++; 12673 index_type = MDEREF_INDEX_padsv; 12674 o = o->op_next; 12675 } 12676 break; 12677 12678 case OP_CONST: 12679 if (next_is_hash) { 12680 /* it's a constant hash index */ 12681 if (!(SvFLAGS(cSVOPo_sv) & (SVf_IOK|SVf_NOK|SVf_POK))) 12682 /* "use constant foo => FOO; $h{+foo}" for 12683 * some weird FOO, can leave you with constants 12684 * that aren't simple strings. It's not worth 12685 * the extra hassle for those edge cases */ 12686 break; 12687 12688 if (pass) { 12689 UNOP *rop = NULL; 12690 OP * helem_op = o->op_next; 12691 12692 ASSUME( helem_op->op_type == OP_HELEM 12693 || helem_op->op_type == OP_NULL); 12694 if (helem_op->op_type == OP_HELEM) { 12695 rop = (UNOP*)(((BINOP*)helem_op)->op_first); 12696 if ( helem_op->op_private & OPpLVAL_INTRO 12697 || rop->op_type != OP_RV2HV 12698 ) 12699 rop = NULL; 12700 } 12701 S_check_hash_fields_and_hekify(aTHX_ rop, cSVOPo); 12702 12703 #ifdef USE_ITHREADS 12704 /* Relocate sv to the pad for thread safety */ 12705 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ); 12706 arg->pad_offset = o->op_targ; 12707 o->op_targ = 0; 12708 #else 12709 arg->sv = cSVOPx_sv(o); 12710 #endif 12711 } 12712 } 12713 else { 12714 /* it's a constant array index */ 12715 IV iv; 12716 SV *ix_sv = cSVOPo->op_sv; 12717 if (!SvIOK(ix_sv)) 12718 break; 12719 iv = SvIV(ix_sv); 12720 12721 if ( action_count == 0 12722 && iv >= -128 12723 && iv <= 127 12724 && ( action == MDEREF_AV_padav_aelem 12725 || action == MDEREF_AV_gvav_aelem) 12726 ) 12727 maybe_aelemfast = TRUE; 12728 12729 if (pass) { 12730 arg->iv = iv; 12731 SvREFCNT_dec_NN(cSVOPo->op_sv); 12732 } 12733 } 12734 if (pass) 12735 /* we've taken ownership of the SV */ 12736 cSVOPo->op_sv = NULL; 12737 arg++; 12738 index_type = MDEREF_INDEX_const; 12739 o = o->op_next; 12740 break; 12741 12742 case OP_GV: 12743 /* it may be a package var index */ 12744 12745 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_SPECIAL))); 12746 ASSUME(!(o->op_private & ~(OPpEARLY_CV))); 12747 if ( (o->op_flags &~ OPf_SPECIAL) != OPf_WANT_SCALAR 12748 || o->op_private != 0 12749 ) 12750 break; 12751 12752 kid = o->op_next; 12753 if (kid->op_type != OP_RV2SV) 12754 break; 12755 12756 ASSUME(!(kid->op_flags & 12757 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_REF 12758 |OPf_SPECIAL|OPf_PARENS))); 12759 ASSUME(!(kid->op_private & 12760 ~(OPpARG1_MASK 12761 |OPpHINT_STRICT_REFS|OPpOUR_INTRO 12762 |OPpDEREF|OPpLVAL_INTRO))); 12763 if( (kid->op_flags &~ OPf_PARENS) 12764 != (OPf_WANT_SCALAR|OPf_KIDS) 12765 || (kid->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS)) 12766 ) 12767 break; 12768 12769 if (pass) { 12770 #ifdef USE_ITHREADS 12771 arg->pad_offset = cPADOPx(o)->op_padix; 12772 /* stop it being swiped when nulled */ 12773 cPADOPx(o)->op_padix = 0; 12774 #else 12775 arg->sv = cSVOPx(o)->op_sv; 12776 cSVOPo->op_sv = NULL; 12777 #endif 12778 } 12779 arg++; 12780 index_type = MDEREF_INDEX_gvsv; 12781 o = kid->op_next; 12782 break; 12783 12784 } /* switch */ 12785 } /* action_count != index_skip */ 12786 12787 action |= index_type; 12788 12789 12790 /* at this point we have either: 12791 * * detected what looks like a simple index expression, 12792 * and expect the next op to be an [ah]elem, or 12793 * an nulled [ah]elem followed by a delete or exists; 12794 * * found a more complex expression, so something other 12795 * than the above follows. 12796 */ 12797 12798 /* possibly an optimised away [ah]elem (where op_next is 12799 * exists or delete) */ 12800 if (o->op_type == OP_NULL) 12801 o = o->op_next; 12802 12803 /* at this point we're looking for an OP_AELEM, OP_HELEM, 12804 * OP_EXISTS or OP_DELETE */ 12805 12806 /* if something like arybase (a.k.a $[ ) is in scope, 12807 * abandon optimisation attempt */ 12808 if ( (o->op_type == OP_AELEM || o->op_type == OP_HELEM) 12809 && PL_check[o->op_type] != Perl_ck_null) 12810 return; 12811 12812 if ( o->op_type != OP_AELEM 12813 || (o->op_private & 12814 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) 12815 ) 12816 maybe_aelemfast = FALSE; 12817 12818 /* look for aelem/helem/exists/delete. If it's not the last elem 12819 * lookup, it *must* have OPpDEREF_AV/HV, but not many other 12820 * flags; if it's the last, then it mustn't have 12821 * OPpDEREF_AV/HV, but may have lots of other flags, like 12822 * OPpLVAL_INTRO etc 12823 */ 12824 12825 if ( index_type == MDEREF_INDEX_none 12826 || ( o->op_type != OP_AELEM && o->op_type != OP_HELEM 12827 && o->op_type != OP_EXISTS && o->op_type != OP_DELETE) 12828 ) 12829 ok = FALSE; 12830 else { 12831 /* we have aelem/helem/exists/delete with valid simple index */ 12832 12833 is_deref = (o->op_type == OP_AELEM || o->op_type == OP_HELEM) 12834 && ( (o->op_private & OPpDEREF) == OPpDEREF_AV 12835 || (o->op_private & OPpDEREF) == OPpDEREF_HV); 12836 12837 if (is_deref) { 12838 ASSUME(!(o->op_flags & 12839 ~(OPf_WANT|OPf_KIDS|OPf_MOD|OPf_PARENS))); 12840 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpDEREF))); 12841 12842 ok = (o->op_flags &~ OPf_PARENS) 12843 == (OPf_WANT_SCALAR|OPf_KIDS|OPf_MOD) 12844 && !(o->op_private & ~(OPpDEREF|OPpARG2_MASK)); 12845 } 12846 else if (o->op_type == OP_EXISTS) { 12847 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS 12848 |OPf_REF|OPf_MOD|OPf_SPECIAL))); 12849 ASSUME(!(o->op_private & ~(OPpARG1_MASK|OPpEXISTS_SUB))); 12850 ok = !(o->op_private & ~OPpARG1_MASK); 12851 } 12852 else if (o->op_type == OP_DELETE) { 12853 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS 12854 |OPf_REF|OPf_MOD|OPf_SPECIAL))); 12855 ASSUME(!(o->op_private & 12856 ~(OPpARG1_MASK|OPpSLICE|OPpLVAL_INTRO))); 12857 /* don't handle slices or 'local delete'; the latter 12858 * is fairly rare, and has a complex runtime */ 12859 ok = !(o->op_private & ~OPpARG1_MASK); 12860 if (OP_TYPE_IS_OR_WAS(cUNOPo->op_first, OP_AELEM)) 12861 /* skip handling run-tome error */ 12862 ok = (ok && cBOOL(o->op_flags & OPf_SPECIAL)); 12863 } 12864 else { 12865 ASSUME(o->op_type == OP_AELEM || o->op_type == OP_HELEM); 12866 ASSUME(!(o->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_MOD 12867 |OPf_PARENS|OPf_REF|OPf_SPECIAL))); 12868 ASSUME(!(o->op_private & ~(OPpARG2_MASK|OPpMAYBE_LVSUB 12869 |OPpLVAL_DEFER|OPpDEREF|OPpLVAL_INTRO))); 12870 ok = (o->op_private & OPpDEREF) != OPpDEREF_SV; 12871 } 12872 } 12873 12874 if (ok) { 12875 if (!first_elem_op) 12876 first_elem_op = o; 12877 top_op = o; 12878 if (is_deref) { 12879 next_is_hash = cBOOL((o->op_private & OPpDEREF) == OPpDEREF_HV); 12880 o = o->op_next; 12881 } 12882 else { 12883 is_last = TRUE; 12884 action |= MDEREF_FLAG_last; 12885 } 12886 } 12887 else { 12888 /* at this point we have something that started 12889 * promisingly enough (with rv2av or whatever), but failed 12890 * to find a simple index followed by an 12891 * aelem/helem/exists/delete. If this is the first action, 12892 * give up; but if we've already seen at least one 12893 * aelem/helem, then keep them and add a new action with 12894 * MDEREF_INDEX_none, which causes it to do the vivify 12895 * from the end of the previous lookup, and do the deref, 12896 * but stop at that point. So $a[0][expr] will do one 12897 * av_fetch, vivify and deref, then continue executing at 12898 * expr */ 12899 if (!action_count) 12900 return; 12901 is_last = TRUE; 12902 index_skip = action_count; 12903 action |= MDEREF_FLAG_last; 12904 } 12905 12906 if (pass) 12907 action_ptr->uv |= (action << (action_ix * MDEREF_SHIFT)); 12908 action_ix++; 12909 action_count++; 12910 /* if there's no space for the next action, create a new slot 12911 * for it *before* we start adding args for that action */ 12912 if ((action_ix + 1) * MDEREF_SHIFT > UVSIZE*8) { 12913 action_ptr = arg; 12914 if (pass) 12915 arg->uv = 0; 12916 arg++; 12917 action_ix = 0; 12918 } 12919 } /* while !is_last */ 12920 12921 /* success! */ 12922 12923 if (pass) { 12924 OP *mderef; 12925 OP *p, *q; 12926 12927 mderef = newUNOP_AUX(OP_MULTIDEREF, 0, NULL, arg_buf); 12928 if (index_skip == -1) { 12929 mderef->op_flags = o->op_flags 12930 & (OPf_WANT|OPf_MOD|(next_is_hash ? OPf_SPECIAL : 0)); 12931 if (o->op_type == OP_EXISTS) 12932 mderef->op_private = OPpMULTIDEREF_EXISTS; 12933 else if (o->op_type == OP_DELETE) 12934 mderef->op_private = OPpMULTIDEREF_DELETE; 12935 else 12936 mderef->op_private = o->op_private 12937 & (OPpMAYBE_LVSUB|OPpLVAL_DEFER|OPpLVAL_INTRO); 12938 } 12939 /* accumulate strictness from every level (although I don't think 12940 * they can actually vary) */ 12941 mderef->op_private |= hints; 12942 12943 /* integrate the new multideref op into the optree and the 12944 * op_next chain. 12945 * 12946 * In general an op like aelem or helem has two child 12947 * sub-trees: the aggregate expression (a_expr) and the 12948 * index expression (i_expr): 12949 * 12950 * aelem 12951 * | 12952 * a_expr - i_expr 12953 * 12954 * The a_expr returns an AV or HV, while the i-expr returns an 12955 * index. In general a multideref replaces most or all of a 12956 * multi-level tree, e.g. 12957 * 12958 * exists 12959 * | 12960 * ex-aelem 12961 * | 12962 * rv2av - i_expr1 12963 * | 12964 * helem 12965 * | 12966 * rv2hv - i_expr2 12967 * | 12968 * aelem 12969 * | 12970 * a_expr - i_expr3 12971 * 12972 * With multideref, all the i_exprs will be simple vars or 12973 * constants, except that i_expr1 may be arbitrary in the case 12974 * of MDEREF_INDEX_none. 12975 * 12976 * The bottom-most a_expr will be either: 12977 * 1) a simple var (so padXv or gv+rv2Xv); 12978 * 2) a simple scalar var dereferenced (e.g. $r->[0]): 12979 * so a simple var with an extra rv2Xv; 12980 * 3) or an arbitrary expression. 12981 * 12982 * 'start', the first op in the execution chain, will point to 12983 * 1),2): the padXv or gv op; 12984 * 3): the rv2Xv which forms the last op in the a_expr 12985 * execution chain, and the top-most op in the a_expr 12986 * subtree. 12987 * 12988 * For all cases, the 'start' node is no longer required, 12989 * but we can't free it since one or more external nodes 12990 * may point to it. E.g. consider 12991 * $h{foo} = $a ? $b : $c 12992 * Here, both the op_next and op_other branches of the 12993 * cond_expr point to the gv[*h] of the hash expression, so 12994 * we can't free the 'start' op. 12995 * 12996 * For expr->[...], we need to save the subtree containing the 12997 * expression; for the other cases, we just need to save the 12998 * start node. 12999 * So in all cases, we null the start op and keep it around by 13000 * making it the child of the multideref op; for the expr-> 13001 * case, the expr will be a subtree of the start node. 13002 * 13003 * So in the simple 1,2 case the optree above changes to 13004 * 13005 * ex-exists 13006 * | 13007 * multideref 13008 * | 13009 * ex-gv (or ex-padxv) 13010 * 13011 * with the op_next chain being 13012 * 13013 * -> ex-gv -> multideref -> op-following-ex-exists -> 13014 * 13015 * In the 3 case, we have 13016 * 13017 * ex-exists 13018 * | 13019 * multideref 13020 * | 13021 * ex-rv2xv 13022 * | 13023 * rest-of-a_expr 13024 * subtree 13025 * 13026 * and 13027 * 13028 * -> rest-of-a_expr subtree -> 13029 * ex-rv2xv -> multideref -> op-following-ex-exists -> 13030 * 13031 * 13032 * Where the last i_expr is non-simple (i.e. MDEREF_INDEX_none, 13033 * e.g. $a[0]{foo}[$x+1], the next rv2xv is nulled and the 13034 * multideref attached as the child, e.g. 13035 * 13036 * exists 13037 * | 13038 * ex-aelem 13039 * | 13040 * ex-rv2av - i_expr1 13041 * | 13042 * multideref 13043 * | 13044 * ex-whatever 13045 * 13046 */ 13047 13048 /* if we free this op, don't free the pad entry */ 13049 if (reset_start_targ) 13050 start->op_targ = 0; 13051 13052 13053 /* Cut the bit we need to save out of the tree and attach to 13054 * the multideref op, then free the rest of the tree */ 13055 13056 /* find parent of node to be detached (for use by splice) */ 13057 p = first_elem_op; 13058 if ( orig_action == MDEREF_AV_pop_rv2av_aelem 13059 || orig_action == MDEREF_HV_pop_rv2hv_helem) 13060 { 13061 /* there is an arbitrary expression preceding us, e.g. 13062 * expr->[..]? so we need to save the 'expr' subtree */ 13063 if (p->op_type == OP_EXISTS || p->op_type == OP_DELETE) 13064 p = cUNOPx(p)->op_first; 13065 ASSUME( start->op_type == OP_RV2AV 13066 || start->op_type == OP_RV2HV); 13067 } 13068 else { 13069 /* either a padXv or rv2Xv+gv, maybe with an ex-Xelem 13070 * above for exists/delete. */ 13071 while ( (p->op_flags & OPf_KIDS) 13072 && cUNOPx(p)->op_first != start 13073 ) 13074 p = cUNOPx(p)->op_first; 13075 } 13076 ASSUME(cUNOPx(p)->op_first == start); 13077 13078 /* detach from main tree, and re-attach under the multideref */ 13079 op_sibling_splice(mderef, NULL, 0, 13080 op_sibling_splice(p, NULL, 1, NULL)); 13081 op_null(start); 13082 13083 start->op_next = mderef; 13084 13085 mderef->op_next = index_skip == -1 ? o->op_next : o; 13086 13087 /* excise and free the original tree, and replace with 13088 * the multideref op */ 13089 p = op_sibling_splice(top_op, NULL, -1, mderef); 13090 while (p) { 13091 q = OpSIBLING(p); 13092 op_free(p); 13093 p = q; 13094 } 13095 op_null(top_op); 13096 } 13097 else { 13098 Size_t size = arg - arg_buf; 13099 13100 if (maybe_aelemfast && action_count == 1) 13101 return; 13102 13103 arg_buf = (UNOP_AUX_item*)PerlMemShared_malloc( 13104 sizeof(UNOP_AUX_item) * (size + 1)); 13105 /* for dumping etc: store the length in a hidden first slot; 13106 * we set the op_aux pointer to the second slot */ 13107 arg_buf->uv = size; 13108 arg_buf++; 13109 } 13110 } /* for (pass = ...) */ 13111 } 13112 13113 13114 13115 /* mechanism for deferring recursion in rpeep() */ 13116 13117 #define MAX_DEFERRED 4 13118 13119 #define DEFER(o) \ 13120 STMT_START { \ 13121 if (defer_ix == (MAX_DEFERRED-1)) { \ 13122 OP **defer = defer_queue[defer_base]; \ 13123 CALL_RPEEP(*defer); \ 13124 S_prune_chain_head(defer); \ 13125 defer_base = (defer_base + 1) % MAX_DEFERRED; \ 13126 defer_ix--; \ 13127 } \ 13128 defer_queue[(defer_base + ++defer_ix) % MAX_DEFERRED] = &(o); \ 13129 } STMT_END 13130 13131 #define IS_AND_OP(o) (o->op_type == OP_AND) 13132 #define IS_OR_OP(o) (o->op_type == OP_OR) 13133 13134 13135 /* A peephole optimizer. We visit the ops in the order they're to execute. 13136 * See the comments at the top of this file for more details about when 13137 * peep() is called */ 13138 13139 void 13140 Perl_rpeep(pTHX_ OP *o) 13141 { 13142 dVAR; 13143 OP* oldop = NULL; 13144 OP* oldoldop = NULL; 13145 OP** defer_queue[MAX_DEFERRED]; /* small queue of deferred branches */ 13146 int defer_base = 0; 13147 int defer_ix = -1; 13148 OP *fop; 13149 OP *sop; 13150 13151 if (!o || o->op_opt) 13152 return; 13153 ENTER; 13154 SAVEOP(); 13155 SAVEVPTR(PL_curcop); 13156 for (;; o = o->op_next) { 13157 if (o && o->op_opt) 13158 o = NULL; 13159 if (!o) { 13160 while (defer_ix >= 0) { 13161 OP **defer = 13162 defer_queue[(defer_base + defer_ix--) % MAX_DEFERRED]; 13163 CALL_RPEEP(*defer); 13164 S_prune_chain_head(defer); 13165 } 13166 break; 13167 } 13168 13169 redo: 13170 13171 /* oldoldop -> oldop -> o should be a chain of 3 adjacent ops */ 13172 assert(!oldoldop || oldoldop->op_next == oldop); 13173 assert(!oldop || oldop->op_next == o); 13174 13175 /* By default, this op has now been optimised. A couple of cases below 13176 clear this again. */ 13177 o->op_opt = 1; 13178 PL_op = o; 13179 13180 /* look for a series of 1 or more aggregate derefs, e.g. 13181 * $a[1]{foo}[$i]{$k} 13182 * and replace with a single OP_MULTIDEREF op. 13183 * Each index must be either a const, or a simple variable, 13184 * 13185 * First, look for likely combinations of starting ops, 13186 * corresponding to (global and lexical variants of) 13187 * $a[...] $h{...} 13188 * $r->[...] $r->{...} 13189 * (preceding expression)->[...] 13190 * (preceding expression)->{...} 13191 * and if so, call maybe_multideref() to do a full inspection 13192 * of the op chain and if appropriate, replace with an 13193 * OP_MULTIDEREF 13194 */ 13195 { 13196 UV action; 13197 OP *o2 = o; 13198 U8 hints = 0; 13199 13200 switch (o2->op_type) { 13201 case OP_GV: 13202 /* $pkg[..] : gv[*pkg] 13203 * $pkg->[...]: gv[*pkg]; rv2sv sKM/DREFAV */ 13204 13205 /* Fail if there are new op flag combinations that we're 13206 * not aware of, rather than: 13207 * * silently failing to optimise, or 13208 * * silently optimising the flag away. 13209 * If this ASSUME starts failing, examine what new flag 13210 * has been added to the op, and decide whether the 13211 * optimisation should still occur with that flag, then 13212 * update the code accordingly. This applies to all the 13213 * other ASSUMEs in the block of code too. 13214 */ 13215 ASSUME(!(o2->op_flags & 13216 ~(OPf_WANT|OPf_MOD|OPf_PARENS|OPf_SPECIAL))); 13217 ASSUME(!(o2->op_private & ~OPpEARLY_CV)); 13218 13219 o2 = o2->op_next; 13220 13221 if (o2->op_type == OP_RV2AV) { 13222 action = MDEREF_AV_gvav_aelem; 13223 goto do_deref; 13224 } 13225 13226 if (o2->op_type == OP_RV2HV) { 13227 action = MDEREF_HV_gvhv_helem; 13228 goto do_deref; 13229 } 13230 13231 if (o2->op_type != OP_RV2SV) 13232 break; 13233 13234 /* at this point we've seen gv,rv2sv, so the only valid 13235 * construct left is $pkg->[] or $pkg->{} */ 13236 13237 ASSUME(!(o2->op_flags & OPf_STACKED)); 13238 if ((o2->op_flags & (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL)) 13239 != (OPf_WANT_SCALAR|OPf_MOD)) 13240 break; 13241 13242 ASSUME(!(o2->op_private & ~(OPpARG1_MASK|HINT_STRICT_REFS 13243 |OPpOUR_INTRO|OPpDEREF|OPpLVAL_INTRO))); 13244 if (o2->op_private & (OPpOUR_INTRO|OPpLVAL_INTRO)) 13245 break; 13246 if ( (o2->op_private & OPpDEREF) != OPpDEREF_AV 13247 && (o2->op_private & OPpDEREF) != OPpDEREF_HV) 13248 break; 13249 13250 o2 = o2->op_next; 13251 if (o2->op_type == OP_RV2AV) { 13252 action = MDEREF_AV_gvsv_vivify_rv2av_aelem; 13253 goto do_deref; 13254 } 13255 if (o2->op_type == OP_RV2HV) { 13256 action = MDEREF_HV_gvsv_vivify_rv2hv_helem; 13257 goto do_deref; 13258 } 13259 break; 13260 13261 case OP_PADSV: 13262 /* $lex->[...]: padsv[$lex] sM/DREFAV */ 13263 13264 ASSUME(!(o2->op_flags & 13265 ~(OPf_WANT|OPf_PARENS|OPf_REF|OPf_MOD|OPf_SPECIAL))); 13266 if ((o2->op_flags & 13267 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL)) 13268 != (OPf_WANT_SCALAR|OPf_MOD)) 13269 break; 13270 13271 ASSUME(!(o2->op_private & 13272 ~(OPpPAD_STATE|OPpDEREF|OPpLVAL_INTRO))); 13273 /* skip if state or intro, or not a deref */ 13274 if ( o2->op_private != OPpDEREF_AV 13275 && o2->op_private != OPpDEREF_HV) 13276 break; 13277 13278 o2 = o2->op_next; 13279 if (o2->op_type == OP_RV2AV) { 13280 action = MDEREF_AV_padsv_vivify_rv2av_aelem; 13281 goto do_deref; 13282 } 13283 if (o2->op_type == OP_RV2HV) { 13284 action = MDEREF_HV_padsv_vivify_rv2hv_helem; 13285 goto do_deref; 13286 } 13287 break; 13288 13289 case OP_PADAV: 13290 case OP_PADHV: 13291 /* $lex[..]: padav[@lex:1,2] sR * 13292 * or $lex{..}: padhv[%lex:1,2] sR */ 13293 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_MOD|OPf_PARENS| 13294 OPf_REF|OPf_SPECIAL))); 13295 if ((o2->op_flags & 13296 (OPf_WANT|OPf_REF|OPf_MOD|OPf_SPECIAL)) 13297 != (OPf_WANT_SCALAR|OPf_REF)) 13298 break; 13299 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_REF)) 13300 break; 13301 /* OPf_PARENS isn't currently used in this case; 13302 * if that changes, let us know! */ 13303 ASSUME(!(o2->op_flags & OPf_PARENS)); 13304 13305 /* at this point, we wouldn't expect any of the remaining 13306 * possible private flags: 13307 * OPpPAD_STATE, OPpLVAL_INTRO, OPpTRUEBOOL, 13308 * OPpMAYBE_TRUEBOOL, OPpMAYBE_LVSUB 13309 * 13310 * OPpSLICEWARNING shouldn't affect runtime 13311 */ 13312 ASSUME(!(o2->op_private & ~(OPpSLICEWARNING))); 13313 13314 action = o2->op_type == OP_PADAV 13315 ? MDEREF_AV_padav_aelem 13316 : MDEREF_HV_padhv_helem; 13317 o2 = o2->op_next; 13318 S_maybe_multideref(aTHX_ o, o2, action, 0); 13319 break; 13320 13321 13322 case OP_RV2AV: 13323 case OP_RV2HV: 13324 action = o2->op_type == OP_RV2AV 13325 ? MDEREF_AV_pop_rv2av_aelem 13326 : MDEREF_HV_pop_rv2hv_helem; 13327 /* FALLTHROUGH */ 13328 do_deref: 13329 /* (expr)->[...]: rv2av sKR/1; 13330 * (expr)->{...}: rv2hv sKR/1; */ 13331 13332 ASSUME(o2->op_type == OP_RV2AV || o2->op_type == OP_RV2HV); 13333 13334 ASSUME(!(o2->op_flags & ~(OPf_WANT|OPf_KIDS|OPf_PARENS 13335 |OPf_REF|OPf_MOD|OPf_STACKED|OPf_SPECIAL))); 13336 if (o2->op_flags != (OPf_WANT_SCALAR|OPf_KIDS|OPf_REF)) 13337 break; 13338 13339 /* at this point, we wouldn't expect any of these 13340 * possible private flags: 13341 * OPpMAYBE_LVSUB, OPpLVAL_INTRO 13342 * OPpTRUEBOOL, OPpMAYBE_TRUEBOOL, (rv2hv only) 13343 */ 13344 ASSUME(!(o2->op_private & 13345 ~(OPpHINT_STRICT_REFS|OPpARG1_MASK|OPpSLICEWARNING 13346 |OPpOUR_INTRO))); 13347 hints |= (o2->op_private & OPpHINT_STRICT_REFS); 13348 13349 o2 = o2->op_next; 13350 13351 S_maybe_multideref(aTHX_ o, o2, action, hints); 13352 break; 13353 13354 default: 13355 break; 13356 } 13357 } 13358 13359 13360 switch (o->op_type) { 13361 case OP_DBSTATE: 13362 PL_curcop = ((COP*)o); /* for warnings */ 13363 break; 13364 case OP_NEXTSTATE: 13365 PL_curcop = ((COP*)o); /* for warnings */ 13366 13367 /* Optimise a "return ..." at the end of a sub to just be "...". 13368 * This saves 2 ops. Before: 13369 * 1 <;> nextstate(main 1 -e:1) v ->2 13370 * 4 <@> return K ->5 13371 * 2 <0> pushmark s ->3 13372 * - <1> ex-rv2sv sK/1 ->4 13373 * 3 <#> gvsv[*cat] s ->4 13374 * 13375 * After: 13376 * - <@> return K ->- 13377 * - <0> pushmark s ->2 13378 * - <1> ex-rv2sv sK/1 ->- 13379 * 2 <$> gvsv(*cat) s ->3 13380 */ 13381 { 13382 OP *next = o->op_next; 13383 OP *sibling = OpSIBLING(o); 13384 if ( OP_TYPE_IS(next, OP_PUSHMARK) 13385 && OP_TYPE_IS(sibling, OP_RETURN) 13386 && OP_TYPE_IS(sibling->op_next, OP_LINESEQ) 13387 && ( OP_TYPE_IS(sibling->op_next->op_next, OP_LEAVESUB) 13388 ||OP_TYPE_IS(sibling->op_next->op_next, 13389 OP_LEAVESUBLV)) 13390 && cUNOPx(sibling)->op_first == next 13391 && OpHAS_SIBLING(next) && OpSIBLING(next)->op_next 13392 && next->op_next 13393 ) { 13394 /* Look through the PUSHMARK's siblings for one that 13395 * points to the RETURN */ 13396 OP *top = OpSIBLING(next); 13397 while (top && top->op_next) { 13398 if (top->op_next == sibling) { 13399 top->op_next = sibling->op_next; 13400 o->op_next = next->op_next; 13401 break; 13402 } 13403 top = OpSIBLING(top); 13404 } 13405 } 13406 } 13407 13408 /* Optimise 'my $x; my $y;' into 'my ($x, $y);' 13409 * 13410 * This latter form is then suitable for conversion into padrange 13411 * later on. Convert: 13412 * 13413 * nextstate1 -> padop1 -> nextstate2 -> padop2 -> nextstate3 13414 * 13415 * into: 13416 * 13417 * nextstate1 -> listop -> nextstate3 13418 * / \ 13419 * pushmark -> padop1 -> padop2 13420 */ 13421 if (o->op_next && ( 13422 o->op_next->op_type == OP_PADSV 13423 || o->op_next->op_type == OP_PADAV 13424 || o->op_next->op_type == OP_PADHV 13425 ) 13426 && !(o->op_next->op_private & ~OPpLVAL_INTRO) 13427 && o->op_next->op_next && o->op_next->op_next->op_type == OP_NEXTSTATE 13428 && o->op_next->op_next->op_next && ( 13429 o->op_next->op_next->op_next->op_type == OP_PADSV 13430 || o->op_next->op_next->op_next->op_type == OP_PADAV 13431 || o->op_next->op_next->op_next->op_type == OP_PADHV 13432 ) 13433 && !(o->op_next->op_next->op_next->op_private & ~OPpLVAL_INTRO) 13434 && o->op_next->op_next->op_next->op_next && o->op_next->op_next->op_next->op_next->op_type == OP_NEXTSTATE 13435 && (!CopLABEL((COP*)o)) /* Don't mess with labels */ 13436 && (!CopLABEL((COP*)o->op_next->op_next)) /* ... */ 13437 ) { 13438 OP *pad1, *ns2, *pad2, *ns3, *newop, *newpm; 13439 13440 pad1 = o->op_next; 13441 ns2 = pad1->op_next; 13442 pad2 = ns2->op_next; 13443 ns3 = pad2->op_next; 13444 13445 /* we assume here that the op_next chain is the same as 13446 * the op_sibling chain */ 13447 assert(OpSIBLING(o) == pad1); 13448 assert(OpSIBLING(pad1) == ns2); 13449 assert(OpSIBLING(ns2) == pad2); 13450 assert(OpSIBLING(pad2) == ns3); 13451 13452 /* excise and delete ns2 */ 13453 op_sibling_splice(NULL, pad1, 1, NULL); 13454 op_free(ns2); 13455 13456 /* excise pad1 and pad2 */ 13457 op_sibling_splice(NULL, o, 2, NULL); 13458 13459 /* create new listop, with children consisting of: 13460 * a new pushmark, pad1, pad2. */ 13461 newop = newLISTOP(OP_LIST, 0, pad1, pad2); 13462 newop->op_flags |= OPf_PARENS; 13463 newop->op_flags = (newop->op_flags & ~OPf_WANT) | OPf_WANT_VOID; 13464 13465 /* insert newop between o and ns3 */ 13466 op_sibling_splice(NULL, o, 0, newop); 13467 13468 /*fixup op_next chain */ 13469 newpm = cUNOPx(newop)->op_first; /* pushmark */ 13470 o ->op_next = newpm; 13471 newpm->op_next = pad1; 13472 pad1 ->op_next = pad2; 13473 pad2 ->op_next = newop; /* listop */ 13474 newop->op_next = ns3; 13475 13476 /* Ensure pushmark has this flag if padops do */ 13477 if (pad1->op_flags & OPf_MOD && pad2->op_flags & OPf_MOD) { 13478 newpm->op_flags |= OPf_MOD; 13479 } 13480 13481 break; 13482 } 13483 13484 /* Two NEXTSTATEs in a row serve no purpose. Except if they happen 13485 to carry two labels. For now, take the easier option, and skip 13486 this optimisation if the first NEXTSTATE has a label. */ 13487 if (!CopLABEL((COP*)o) && !PERLDB_NOOPT) { 13488 OP *nextop = o->op_next; 13489 while (nextop && nextop->op_type == OP_NULL) 13490 nextop = nextop->op_next; 13491 13492 if (nextop && (nextop->op_type == OP_NEXTSTATE)) { 13493 op_null(o); 13494 if (oldop) 13495 oldop->op_next = nextop; 13496 o = nextop; 13497 /* Skip (old)oldop assignment since the current oldop's 13498 op_next already points to the next op. */ 13499 goto redo; 13500 } 13501 } 13502 break; 13503 13504 case OP_CONCAT: 13505 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) { 13506 if (o->op_next->op_private & OPpTARGET_MY) { 13507 if (o->op_flags & OPf_STACKED) /* chained concats */ 13508 break; /* ignore_optimization */ 13509 else { 13510 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */ 13511 o->op_targ = o->op_next->op_targ; 13512 o->op_next->op_targ = 0; 13513 o->op_private |= OPpTARGET_MY; 13514 } 13515 } 13516 op_null(o->op_next); 13517 } 13518 break; 13519 case OP_STUB: 13520 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) { 13521 break; /* Scalar stub must produce undef. List stub is noop */ 13522 } 13523 goto nothin; 13524 case OP_NULL: 13525 if (o->op_targ == OP_NEXTSTATE 13526 || o->op_targ == OP_DBSTATE) 13527 { 13528 PL_curcop = ((COP*)o); 13529 } 13530 /* XXX: We avoid setting op_seq here to prevent later calls 13531 to rpeep() from mistakenly concluding that optimisation 13532 has already occurred. This doesn't fix the real problem, 13533 though (See 20010220.007). AMS 20010719 */ 13534 /* op_seq functionality is now replaced by op_opt */ 13535 o->op_opt = 0; 13536 /* FALLTHROUGH */ 13537 case OP_SCALAR: 13538 case OP_LINESEQ: 13539 case OP_SCOPE: 13540 nothin: 13541 if (oldop) { 13542 oldop->op_next = o->op_next; 13543 o->op_opt = 0; 13544 continue; 13545 } 13546 break; 13547 13548 case OP_PUSHMARK: 13549 13550 /* Given 13551 5 repeat/DOLIST 13552 3 ex-list 13553 1 pushmark 13554 2 scalar or const 13555 4 const[0] 13556 convert repeat into a stub with no kids. 13557 */ 13558 if (o->op_next->op_type == OP_CONST 13559 || ( o->op_next->op_type == OP_PADSV 13560 && !(o->op_next->op_private & OPpLVAL_INTRO)) 13561 || ( o->op_next->op_type == OP_GV 13562 && o->op_next->op_next->op_type == OP_RV2SV 13563 && !(o->op_next->op_next->op_private 13564 & (OPpLVAL_INTRO|OPpOUR_INTRO)))) 13565 { 13566 const OP *kid = o->op_next->op_next; 13567 if (o->op_next->op_type == OP_GV) 13568 kid = kid->op_next; 13569 /* kid is now the ex-list. */ 13570 if (kid->op_type == OP_NULL 13571 && (kid = kid->op_next)->op_type == OP_CONST 13572 /* kid is now the repeat count. */ 13573 && kid->op_next->op_type == OP_REPEAT 13574 && kid->op_next->op_private & OPpREPEAT_DOLIST 13575 && (kid->op_next->op_flags & OPf_WANT) == OPf_WANT_LIST 13576 && SvIOK(kSVOP_sv) && SvIVX(kSVOP_sv) == 0) 13577 { 13578 o = kid->op_next; /* repeat */ 13579 assert(oldop); 13580 oldop->op_next = o; 13581 op_free(cBINOPo->op_first); 13582 op_free(cBINOPo->op_last ); 13583 o->op_flags &=~ OPf_KIDS; 13584 /* stub is a baseop; repeat is a binop */ 13585 STATIC_ASSERT_STMT(sizeof(OP) <= sizeof(BINOP)); 13586 OpTYPE_set(o, OP_STUB); 13587 o->op_private = 0; 13588 break; 13589 } 13590 } 13591 13592 /* Convert a series of PAD ops for my vars plus support into a 13593 * single padrange op. Basically 13594 * 13595 * pushmark -> pad[ahs]v -> pad[ahs]?v -> ... -> (list) -> rest 13596 * 13597 * becomes, depending on circumstances, one of 13598 * 13599 * padrange ----------------------------------> (list) -> rest 13600 * padrange --------------------------------------------> rest 13601 * 13602 * where all the pad indexes are sequential and of the same type 13603 * (INTRO or not). 13604 * We convert the pushmark into a padrange op, then skip 13605 * any other pad ops, and possibly some trailing ops. 13606 * Note that we don't null() the skipped ops, to make it 13607 * easier for Deparse to undo this optimisation (and none of 13608 * the skipped ops are holding any resourses). It also makes 13609 * it easier for find_uninit_var(), as it can just ignore 13610 * padrange, and examine the original pad ops. 13611 */ 13612 { 13613 OP *p; 13614 OP *followop = NULL; /* the op that will follow the padrange op */ 13615 U8 count = 0; 13616 U8 intro = 0; 13617 PADOFFSET base = 0; /* init only to stop compiler whining */ 13618 bool gvoid = 0; /* init only to stop compiler whining */ 13619 bool defav = 0; /* seen (...) = @_ */ 13620 bool reuse = 0; /* reuse an existing padrange op */ 13621 13622 /* look for a pushmark -> gv[_] -> rv2av */ 13623 13624 { 13625 OP *rv2av, *q; 13626 p = o->op_next; 13627 if ( p->op_type == OP_GV 13628 && cGVOPx_gv(p) == PL_defgv 13629 && (rv2av = p->op_next) 13630 && rv2av->op_type == OP_RV2AV 13631 && !(rv2av->op_flags & OPf_REF) 13632 && !(rv2av->op_private & (OPpLVAL_INTRO|OPpMAYBE_LVSUB)) 13633 && ((rv2av->op_flags & OPf_WANT) == OPf_WANT_LIST) 13634 ) { 13635 q = rv2av->op_next; 13636 if (q->op_type == OP_NULL) 13637 q = q->op_next; 13638 if (q->op_type == OP_PUSHMARK) { 13639 defav = 1; 13640 p = q; 13641 } 13642 } 13643 } 13644 if (!defav) { 13645 p = o; 13646 } 13647 13648 /* scan for PAD ops */ 13649 13650 for (p = p->op_next; p; p = p->op_next) { 13651 if (p->op_type == OP_NULL) 13652 continue; 13653 13654 if (( p->op_type != OP_PADSV 13655 && p->op_type != OP_PADAV 13656 && p->op_type != OP_PADHV 13657 ) 13658 /* any private flag other than INTRO? e.g. STATE */ 13659 || (p->op_private & ~OPpLVAL_INTRO) 13660 ) 13661 break; 13662 13663 /* let $a[N] potentially be optimised into AELEMFAST_LEX 13664 * instead */ 13665 if ( p->op_type == OP_PADAV 13666 && p->op_next 13667 && p->op_next->op_type == OP_CONST 13668 && p->op_next->op_next 13669 && p->op_next->op_next->op_type == OP_AELEM 13670 ) 13671 break; 13672 13673 /* for 1st padop, note what type it is and the range 13674 * start; for the others, check that it's the same type 13675 * and that the targs are contiguous */ 13676 if (count == 0) { 13677 intro = (p->op_private & OPpLVAL_INTRO); 13678 base = p->op_targ; 13679 gvoid = OP_GIMME(p,0) == G_VOID; 13680 } 13681 else { 13682 if ((p->op_private & OPpLVAL_INTRO) != intro) 13683 break; 13684 /* Note that you'd normally expect targs to be 13685 * contiguous in my($a,$b,$c), but that's not the case 13686 * when external modules start doing things, e.g. 13687 * Function::Parameters */ 13688 if (p->op_targ != base + count) 13689 break; 13690 assert(p->op_targ == base + count); 13691 /* Either all the padops or none of the padops should 13692 be in void context. Since we only do the optimisa- 13693 tion for av/hv when the aggregate itself is pushed 13694 on to the stack (one item), there is no need to dis- 13695 tinguish list from scalar context. */ 13696 if (gvoid != (OP_GIMME(p,0) == G_VOID)) 13697 break; 13698 } 13699 13700 /* for AV, HV, only when we're not flattening */ 13701 if ( p->op_type != OP_PADSV 13702 && !gvoid 13703 && !(p->op_flags & OPf_REF) 13704 ) 13705 break; 13706 13707 if (count >= OPpPADRANGE_COUNTMASK) 13708 break; 13709 13710 /* there's a biggest base we can fit into a 13711 * SAVEt_CLEARPADRANGE in pp_padrange. 13712 * (The sizeof() stuff will be constant-folded, and is 13713 * intended to avoid getting "comparison is always false" 13714 * compiler warnings. See the comments above 13715 * MEM_WRAP_CHECK for more explanation on why we do this 13716 * in a weird way to avoid compiler warnings.) 13717 */ 13718 if ( intro 13719 && (8*sizeof(base) > 13720 8*sizeof(UV)-OPpPADRANGE_COUNTSHIFT-SAVE_TIGHT_SHIFT 13721 ? base 13722 : (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT)) 13723 ) > 13724 (UV_MAX >> (OPpPADRANGE_COUNTSHIFT+SAVE_TIGHT_SHIFT)) 13725 ) 13726 break; 13727 13728 /* Success! We've got another valid pad op to optimise away */ 13729 count++; 13730 followop = p->op_next; 13731 } 13732 13733 if (count < 1 || (count == 1 && !defav)) 13734 break; 13735 13736 /* pp_padrange in specifically compile-time void context 13737 * skips pushing a mark and lexicals; in all other contexts 13738 * (including unknown till runtime) it pushes a mark and the 13739 * lexicals. We must be very careful then, that the ops we 13740 * optimise away would have exactly the same effect as the 13741 * padrange. 13742 * In particular in void context, we can only optimise to 13743 * a padrange if we see the complete sequence 13744 * pushmark, pad*v, ...., list 13745 * which has the net effect of leaving the markstack as it 13746 * was. Not pushing onto the stack (whereas padsv does touch 13747 * the stack) makes no difference in void context. 13748 */ 13749 assert(followop); 13750 if (gvoid) { 13751 if (followop->op_type == OP_LIST 13752 && OP_GIMME(followop,0) == G_VOID 13753 ) 13754 { 13755 followop = followop->op_next; /* skip OP_LIST */ 13756 13757 /* consolidate two successive my(...);'s */ 13758 13759 if ( oldoldop 13760 && oldoldop->op_type == OP_PADRANGE 13761 && (oldoldop->op_flags & OPf_WANT) == OPf_WANT_VOID 13762 && (oldoldop->op_private & OPpLVAL_INTRO) == intro 13763 && !(oldoldop->op_flags & OPf_SPECIAL) 13764 ) { 13765 U8 old_count; 13766 assert(oldoldop->op_next == oldop); 13767 assert( oldop->op_type == OP_NEXTSTATE 13768 || oldop->op_type == OP_DBSTATE); 13769 assert(oldop->op_next == o); 13770 13771 old_count 13772 = (oldoldop->op_private & OPpPADRANGE_COUNTMASK); 13773 13774 /* Do not assume pad offsets for $c and $d are con- 13775 tiguous in 13776 my ($a,$b,$c); 13777 my ($d,$e,$f); 13778 */ 13779 if ( oldoldop->op_targ + old_count == base 13780 && old_count < OPpPADRANGE_COUNTMASK - count) { 13781 base = oldoldop->op_targ; 13782 count += old_count; 13783 reuse = 1; 13784 } 13785 } 13786 13787 /* if there's any immediately following singleton 13788 * my var's; then swallow them and the associated 13789 * nextstates; i.e. 13790 * my ($a,$b); my $c; my $d; 13791 * is treated as 13792 * my ($a,$b,$c,$d); 13793 */ 13794 13795 while ( ((p = followop->op_next)) 13796 && ( p->op_type == OP_PADSV 13797 || p->op_type == OP_PADAV 13798 || p->op_type == OP_PADHV) 13799 && (p->op_flags & OPf_WANT) == OPf_WANT_VOID 13800 && (p->op_private & OPpLVAL_INTRO) == intro 13801 && !(p->op_private & ~OPpLVAL_INTRO) 13802 && p->op_next 13803 && ( p->op_next->op_type == OP_NEXTSTATE 13804 || p->op_next->op_type == OP_DBSTATE) 13805 && count < OPpPADRANGE_COUNTMASK 13806 && base + count == p->op_targ 13807 ) { 13808 count++; 13809 followop = p->op_next; 13810 } 13811 } 13812 else 13813 break; 13814 } 13815 13816 if (reuse) { 13817 assert(oldoldop->op_type == OP_PADRANGE); 13818 oldoldop->op_next = followop; 13819 oldoldop->op_private = (intro | count); 13820 o = oldoldop; 13821 oldop = NULL; 13822 oldoldop = NULL; 13823 } 13824 else { 13825 /* Convert the pushmark into a padrange. 13826 * To make Deparse easier, we guarantee that a padrange was 13827 * *always* formerly a pushmark */ 13828 assert(o->op_type == OP_PUSHMARK); 13829 o->op_next = followop; 13830 OpTYPE_set(o, OP_PADRANGE); 13831 o->op_targ = base; 13832 /* bit 7: INTRO; bit 6..0: count */ 13833 o->op_private = (intro | count); 13834 o->op_flags = ((o->op_flags & ~(OPf_WANT|OPf_SPECIAL)) 13835 | gvoid * OPf_WANT_VOID 13836 | (defav ? OPf_SPECIAL : 0)); 13837 } 13838 break; 13839 } 13840 13841 case OP_PADAV: 13842 case OP_PADSV: 13843 case OP_PADHV: 13844 /* Skip over state($x) in void context. */ 13845 if (oldop && o->op_private == (OPpPAD_STATE|OPpLVAL_INTRO) 13846 && (o->op_flags & OPf_WANT) == OPf_WANT_VOID) 13847 { 13848 oldop->op_next = o->op_next; 13849 goto redo_nextstate; 13850 } 13851 if (o->op_type != OP_PADAV) 13852 break; 13853 /* FALLTHROUGH */ 13854 case OP_GV: 13855 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) { 13856 OP* const pop = (o->op_type == OP_PADAV) ? 13857 o->op_next : o->op_next->op_next; 13858 IV i; 13859 if (pop && pop->op_type == OP_CONST && 13860 ((PL_op = pop->op_next)) && 13861 pop->op_next->op_type == OP_AELEM && 13862 !(pop->op_next->op_private & 13863 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) && 13864 (i = SvIV(((SVOP*)pop)->op_sv)) >= -128 && i <= 127) 13865 { 13866 GV *gv; 13867 if (cSVOPx(pop)->op_private & OPpCONST_STRICT) 13868 no_bareword_allowed(pop); 13869 if (o->op_type == OP_GV) 13870 op_null(o->op_next); 13871 op_null(pop->op_next); 13872 op_null(pop); 13873 o->op_flags |= pop->op_next->op_flags & OPf_MOD; 13874 o->op_next = pop->op_next->op_next; 13875 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST]; 13876 o->op_private = (U8)i; 13877 if (o->op_type == OP_GV) { 13878 gv = cGVOPo_gv; 13879 GvAVn(gv); 13880 o->op_type = OP_AELEMFAST; 13881 } 13882 else 13883 o->op_type = OP_AELEMFAST_LEX; 13884 } 13885 if (o->op_type != OP_GV) 13886 break; 13887 } 13888 13889 /* Remove $foo from the op_next chain in void context. */ 13890 if (oldop 13891 && ( o->op_next->op_type == OP_RV2SV 13892 || o->op_next->op_type == OP_RV2AV 13893 || o->op_next->op_type == OP_RV2HV ) 13894 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID 13895 && !(o->op_next->op_private & OPpLVAL_INTRO)) 13896 { 13897 oldop->op_next = o->op_next->op_next; 13898 /* Reprocess the previous op if it is a nextstate, to 13899 allow double-nextstate optimisation. */ 13900 redo_nextstate: 13901 if (oldop->op_type == OP_NEXTSTATE) { 13902 oldop->op_opt = 0; 13903 o = oldop; 13904 oldop = oldoldop; 13905 oldoldop = NULL; 13906 goto redo; 13907 } 13908 o = oldop->op_next; 13909 goto redo; 13910 } 13911 else if (o->op_next->op_type == OP_RV2SV) { 13912 if (!(o->op_next->op_private & OPpDEREF)) { 13913 op_null(o->op_next); 13914 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO 13915 | OPpOUR_INTRO); 13916 o->op_next = o->op_next->op_next; 13917 OpTYPE_set(o, OP_GVSV); 13918 } 13919 } 13920 else if (o->op_next->op_type == OP_READLINE 13921 && o->op_next->op_next->op_type == OP_CONCAT 13922 && (o->op_next->op_next->op_flags & OPf_STACKED)) 13923 { 13924 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */ 13925 OpTYPE_set(o, OP_RCATLINE); 13926 o->op_flags |= OPf_STACKED; 13927 op_null(o->op_next->op_next); 13928 op_null(o->op_next); 13929 } 13930 13931 break; 13932 13933 #define HV_OR_SCALARHV(op) \ 13934 ( (op)->op_type == OP_PADHV || (op)->op_type == OP_RV2HV \ 13935 ? (op) \ 13936 : (op)->op_type == OP_SCALAR && (op)->op_flags & OPf_KIDS \ 13937 && ( cUNOPx(op)->op_first->op_type == OP_PADHV \ 13938 || cUNOPx(op)->op_first->op_type == OP_RV2HV) \ 13939 ? cUNOPx(op)->op_first \ 13940 : NULL) 13941 13942 case OP_NOT: 13943 if ((fop = HV_OR_SCALARHV(cUNOP->op_first))) 13944 fop->op_private |= OPpTRUEBOOL; 13945 break; 13946 13947 case OP_AND: 13948 case OP_OR: 13949 case OP_DOR: 13950 fop = cLOGOP->op_first; 13951 sop = OpSIBLING(fop); 13952 while (cLOGOP->op_other->op_type == OP_NULL) 13953 cLOGOP->op_other = cLOGOP->op_other->op_next; 13954 while (o->op_next && ( o->op_type == o->op_next->op_type 13955 || o->op_next->op_type == OP_NULL)) 13956 o->op_next = o->op_next->op_next; 13957 13958 /* If we're an OR and our next is an AND in void context, we'll 13959 follow its op_other on short circuit, same for reverse. 13960 We can't do this with OP_DOR since if it's true, its return 13961 value is the underlying value which must be evaluated 13962 by the next op. */ 13963 if (o->op_next && 13964 ( 13965 (IS_AND_OP(o) && IS_OR_OP(o->op_next)) 13966 || (IS_OR_OP(o) && IS_AND_OP(o->op_next)) 13967 ) 13968 && (o->op_next->op_flags & OPf_WANT) == OPf_WANT_VOID 13969 ) { 13970 o->op_next = ((LOGOP*)o->op_next)->op_other; 13971 } 13972 DEFER(cLOGOP->op_other); 13973 13974 o->op_opt = 1; 13975 fop = HV_OR_SCALARHV(fop); 13976 if (sop) sop = HV_OR_SCALARHV(sop); 13977 if (fop || sop 13978 ){ 13979 OP * nop = o; 13980 OP * lop = o; 13981 if (!((nop->op_flags & OPf_WANT) == OPf_WANT_VOID)) { 13982 while (nop && nop->op_next) { 13983 switch (nop->op_next->op_type) { 13984 case OP_NOT: 13985 case OP_AND: 13986 case OP_OR: 13987 case OP_DOR: 13988 lop = nop = nop->op_next; 13989 break; 13990 case OP_NULL: 13991 nop = nop->op_next; 13992 break; 13993 default: 13994 nop = NULL; 13995 break; 13996 } 13997 } 13998 } 13999 if (fop) { 14000 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID 14001 || o->op_type == OP_AND ) 14002 fop->op_private |= OPpTRUEBOOL; 14003 else if (!(lop->op_flags & OPf_WANT)) 14004 fop->op_private |= OPpMAYBE_TRUEBOOL; 14005 } 14006 if ( (lop->op_flags & OPf_WANT) == OPf_WANT_VOID 14007 && sop) 14008 sop->op_private |= OPpTRUEBOOL; 14009 } 14010 14011 14012 break; 14013 14014 case OP_COND_EXPR: 14015 if ((fop = HV_OR_SCALARHV(cLOGOP->op_first))) 14016 fop->op_private |= OPpTRUEBOOL; 14017 #undef HV_OR_SCALARHV 14018 /* GERONIMO! */ /* FALLTHROUGH */ 14019 14020 case OP_MAPWHILE: 14021 case OP_GREPWHILE: 14022 case OP_ANDASSIGN: 14023 case OP_ORASSIGN: 14024 case OP_DORASSIGN: 14025 case OP_RANGE: 14026 case OP_ONCE: 14027 while (cLOGOP->op_other->op_type == OP_NULL) 14028 cLOGOP->op_other = cLOGOP->op_other->op_next; 14029 DEFER(cLOGOP->op_other); 14030 break; 14031 14032 case OP_ENTERLOOP: 14033 case OP_ENTERITER: 14034 while (cLOOP->op_redoop->op_type == OP_NULL) 14035 cLOOP->op_redoop = cLOOP->op_redoop->op_next; 14036 while (cLOOP->op_nextop->op_type == OP_NULL) 14037 cLOOP->op_nextop = cLOOP->op_nextop->op_next; 14038 while (cLOOP->op_lastop->op_type == OP_NULL) 14039 cLOOP->op_lastop = cLOOP->op_lastop->op_next; 14040 /* a while(1) loop doesn't have an op_next that escapes the 14041 * loop, so we have to explicitly follow the op_lastop to 14042 * process the rest of the code */ 14043 DEFER(cLOOP->op_lastop); 14044 break; 14045 14046 case OP_ENTERTRY: 14047 assert(cLOGOPo->op_other->op_type == OP_LEAVETRY); 14048 DEFER(cLOGOPo->op_other); 14049 break; 14050 14051 case OP_SUBST: 14052 assert(!(cPMOP->op_pmflags & PMf_ONCE)); 14053 while (cPMOP->op_pmstashstartu.op_pmreplstart && 14054 cPMOP->op_pmstashstartu.op_pmreplstart->op_type == OP_NULL) 14055 cPMOP->op_pmstashstartu.op_pmreplstart 14056 = cPMOP->op_pmstashstartu.op_pmreplstart->op_next; 14057 DEFER(cPMOP->op_pmstashstartu.op_pmreplstart); 14058 break; 14059 14060 case OP_SORT: { 14061 OP *oright; 14062 14063 if (o->op_flags & OPf_SPECIAL) { 14064 /* first arg is a code block */ 14065 OP * const nullop = OpSIBLING(cLISTOP->op_first); 14066 OP * kid = cUNOPx(nullop)->op_first; 14067 14068 assert(nullop->op_type == OP_NULL); 14069 assert(kid->op_type == OP_SCOPE 14070 || (kid->op_type == OP_NULL && kid->op_targ == OP_LEAVE)); 14071 /* since OP_SORT doesn't have a handy op_other-style 14072 * field that can point directly to the start of the code 14073 * block, store it in the otherwise-unused op_next field 14074 * of the top-level OP_NULL. This will be quicker at 14075 * run-time, and it will also allow us to remove leading 14076 * OP_NULLs by just messing with op_nexts without 14077 * altering the basic op_first/op_sibling layout. */ 14078 kid = kLISTOP->op_first; 14079 assert( 14080 (kid->op_type == OP_NULL 14081 && ( kid->op_targ == OP_NEXTSTATE 14082 || kid->op_targ == OP_DBSTATE )) 14083 || kid->op_type == OP_STUB 14084 || kid->op_type == OP_ENTER); 14085 nullop->op_next = kLISTOP->op_next; 14086 DEFER(nullop->op_next); 14087 } 14088 14089 /* check that RHS of sort is a single plain array */ 14090 oright = cUNOPo->op_first; 14091 if (!oright || oright->op_type != OP_PUSHMARK) 14092 break; 14093 14094 if (o->op_private & OPpSORT_INPLACE) 14095 break; 14096 14097 /* reverse sort ... can be optimised. */ 14098 if (!OpHAS_SIBLING(cUNOPo)) { 14099 /* Nothing follows us on the list. */ 14100 OP * const reverse = o->op_next; 14101 14102 if (reverse->op_type == OP_REVERSE && 14103 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) { 14104 OP * const pushmark = cUNOPx(reverse)->op_first; 14105 if (pushmark && (pushmark->op_type == OP_PUSHMARK) 14106 && (OpSIBLING(cUNOPx(pushmark)) == o)) { 14107 /* reverse -> pushmark -> sort */ 14108 o->op_private |= OPpSORT_REVERSE; 14109 op_null(reverse); 14110 pushmark->op_next = oright->op_next; 14111 op_null(oright); 14112 } 14113 } 14114 } 14115 14116 break; 14117 } 14118 14119 case OP_REVERSE: { 14120 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av; 14121 OP *gvop = NULL; 14122 LISTOP *enter, *exlist; 14123 14124 if (o->op_private & OPpSORT_INPLACE) 14125 break; 14126 14127 enter = (LISTOP *) o->op_next; 14128 if (!enter) 14129 break; 14130 if (enter->op_type == OP_NULL) { 14131 enter = (LISTOP *) enter->op_next; 14132 if (!enter) 14133 break; 14134 } 14135 /* for $a (...) will have OP_GV then OP_RV2GV here. 14136 for (...) just has an OP_GV. */ 14137 if (enter->op_type == OP_GV) { 14138 gvop = (OP *) enter; 14139 enter = (LISTOP *) enter->op_next; 14140 if (!enter) 14141 break; 14142 if (enter->op_type == OP_RV2GV) { 14143 enter = (LISTOP *) enter->op_next; 14144 if (!enter) 14145 break; 14146 } 14147 } 14148 14149 if (enter->op_type != OP_ENTERITER) 14150 break; 14151 14152 iter = enter->op_next; 14153 if (!iter || iter->op_type != OP_ITER) 14154 break; 14155 14156 expushmark = enter->op_first; 14157 if (!expushmark || expushmark->op_type != OP_NULL 14158 || expushmark->op_targ != OP_PUSHMARK) 14159 break; 14160 14161 exlist = (LISTOP *) OpSIBLING(expushmark); 14162 if (!exlist || exlist->op_type != OP_NULL 14163 || exlist->op_targ != OP_LIST) 14164 break; 14165 14166 if (exlist->op_last != o) { 14167 /* Mmm. Was expecting to point back to this op. */ 14168 break; 14169 } 14170 theirmark = exlist->op_first; 14171 if (!theirmark || theirmark->op_type != OP_PUSHMARK) 14172 break; 14173 14174 if (OpSIBLING(theirmark) != o) { 14175 /* There's something between the mark and the reverse, eg 14176 for (1, reverse (...)) 14177 so no go. */ 14178 break; 14179 } 14180 14181 ourmark = ((LISTOP *)o)->op_first; 14182 if (!ourmark || ourmark->op_type != OP_PUSHMARK) 14183 break; 14184 14185 ourlast = ((LISTOP *)o)->op_last; 14186 if (!ourlast || ourlast->op_next != o) 14187 break; 14188 14189 rv2av = OpSIBLING(ourmark); 14190 if (rv2av && rv2av->op_type == OP_RV2AV && !OpHAS_SIBLING(rv2av) 14191 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)) { 14192 /* We're just reversing a single array. */ 14193 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF; 14194 enter->op_flags |= OPf_STACKED; 14195 } 14196 14197 /* We don't have control over who points to theirmark, so sacrifice 14198 ours. */ 14199 theirmark->op_next = ourmark->op_next; 14200 theirmark->op_flags = ourmark->op_flags; 14201 ourlast->op_next = gvop ? gvop : (OP *) enter; 14202 op_null(ourmark); 14203 op_null(o); 14204 enter->op_private |= OPpITER_REVERSED; 14205 iter->op_private |= OPpITER_REVERSED; 14206 14207 oldoldop = NULL; 14208 oldop = ourlast; 14209 o = oldop->op_next; 14210 goto redo; 14211 14212 break; 14213 } 14214 14215 case OP_QR: 14216 case OP_MATCH: 14217 if (!(cPMOP->op_pmflags & PMf_ONCE)) { 14218 assert (!cPMOP->op_pmstashstartu.op_pmreplstart); 14219 } 14220 break; 14221 14222 case OP_RUNCV: 14223 if (!(o->op_private & OPpOFFBYONE) && !CvCLONE(PL_compcv) 14224 && (!CvANON(PL_compcv) || (!PL_cv_has_eval && !PL_perldb))) 14225 { 14226 SV *sv; 14227 if (CvEVAL(PL_compcv)) sv = &PL_sv_undef; 14228 else { 14229 sv = newRV((SV *)PL_compcv); 14230 sv_rvweaken(sv); 14231 SvREADONLY_on(sv); 14232 } 14233 OpTYPE_set(o, OP_CONST); 14234 o->op_flags |= OPf_SPECIAL; 14235 cSVOPo->op_sv = sv; 14236 } 14237 break; 14238 14239 case OP_SASSIGN: 14240 if (OP_GIMME(o,0) == G_VOID 14241 || ( o->op_next->op_type == OP_LINESEQ 14242 && ( o->op_next->op_next->op_type == OP_LEAVESUB 14243 || ( o->op_next->op_next->op_type == OP_RETURN 14244 && !CvLVALUE(PL_compcv))))) 14245 { 14246 OP *right = cBINOP->op_first; 14247 if (right) { 14248 /* sassign 14249 * RIGHT 14250 * substr 14251 * pushmark 14252 * arg1 14253 * arg2 14254 * ... 14255 * becomes 14256 * 14257 * ex-sassign 14258 * substr 14259 * pushmark 14260 * RIGHT 14261 * arg1 14262 * arg2 14263 * ... 14264 */ 14265 OP *left = OpSIBLING(right); 14266 if (left->op_type == OP_SUBSTR 14267 && (left->op_private & 7) < 4) { 14268 op_null(o); 14269 /* cut out right */ 14270 op_sibling_splice(o, NULL, 1, NULL); 14271 /* and insert it as second child of OP_SUBSTR */ 14272 op_sibling_splice(left, cBINOPx(left)->op_first, 0, 14273 right); 14274 left->op_private |= OPpSUBSTR_REPL_FIRST; 14275 left->op_flags = 14276 (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID; 14277 } 14278 } 14279 } 14280 break; 14281 14282 case OP_AASSIGN: { 14283 int l, r, lr, lscalars, rscalars; 14284 14285 /* handle common vars detection, e.g. ($a,$b) = ($b,$a). 14286 Note that we do this now rather than in newASSIGNOP(), 14287 since only by now are aliased lexicals flagged as such 14288 14289 See the essay "Common vars in list assignment" above for 14290 the full details of the rationale behind all the conditions 14291 below. 14292 14293 PL_generation sorcery: 14294 To detect whether there are common vars, the global var 14295 PL_generation is incremented for each assign op we scan. 14296 Then we run through all the lexical variables on the LHS, 14297 of the assignment, setting a spare slot in each of them to 14298 PL_generation. Then we scan the RHS, and if any lexicals 14299 already have that value, we know we've got commonality. 14300 Also, if the generation number is already set to 14301 PERL_INT_MAX, then the variable is involved in aliasing, so 14302 we also have potential commonality in that case. 14303 */ 14304 14305 PL_generation++; 14306 /* scan LHS */ 14307 lscalars = 0; 14308 l = S_aassign_scan(aTHX_ cLISTOPo->op_last, FALSE, 1, &lscalars); 14309 /* scan RHS */ 14310 rscalars = 0; 14311 r = S_aassign_scan(aTHX_ cLISTOPo->op_first, TRUE, 1, &rscalars); 14312 lr = (l|r); 14313 14314 14315 /* After looking for things which are *always* safe, this main 14316 * if/else chain selects primarily based on the type of the 14317 * LHS, gradually working its way down from the more dangerous 14318 * to the more restrictive and thus safer cases */ 14319 14320 if ( !l /* () = ....; */ 14321 || !r /* .... = (); */ 14322 || !(l & ~AAS_SAFE_SCALAR) /* (undef, pos()) = ...; */ 14323 || !(r & ~AAS_SAFE_SCALAR) /* ... = (1,2,length,undef); */ 14324 || (lscalars < 2) /* ($x, undef) = ... */ 14325 ) { 14326 NOOP; /* always safe */ 14327 } 14328 else if (l & AAS_DANGEROUS) { 14329 /* always dangerous */ 14330 o->op_private |= OPpASSIGN_COMMON_SCALAR; 14331 o->op_private |= OPpASSIGN_COMMON_AGG; 14332 } 14333 else if (l & (AAS_PKG_SCALAR|AAS_PKG_AGG)) { 14334 /* package vars are always dangerous - too many 14335 * aliasing possibilities */ 14336 if (l & AAS_PKG_SCALAR) 14337 o->op_private |= OPpASSIGN_COMMON_SCALAR; 14338 if (l & AAS_PKG_AGG) 14339 o->op_private |= OPpASSIGN_COMMON_AGG; 14340 } 14341 else if (l & ( AAS_MY_SCALAR|AAS_MY_AGG 14342 |AAS_LEX_SCALAR|AAS_LEX_AGG)) 14343 { 14344 /* LHS contains only lexicals and safe ops */ 14345 14346 if (l & (AAS_MY_AGG|AAS_LEX_AGG)) 14347 o->op_private |= OPpASSIGN_COMMON_AGG; 14348 14349 if (l & (AAS_MY_SCALAR|AAS_LEX_SCALAR)) { 14350 if (lr & AAS_LEX_SCALAR_COMM) 14351 o->op_private |= OPpASSIGN_COMMON_SCALAR; 14352 else if ( !(l & AAS_LEX_SCALAR) 14353 && (r & AAS_DEFAV)) 14354 { 14355 /* falsely mark 14356 * my (...) = @_ 14357 * as scalar-safe for performance reasons. 14358 * (it will still have been marked _AGG if necessary */ 14359 NOOP; 14360 } 14361 else if (r & (AAS_PKG_SCALAR|AAS_PKG_AGG|AAS_DANGEROUS)) 14362 o->op_private |= OPpASSIGN_COMMON_RC1; 14363 } 14364 } 14365 14366 /* ... = ($x) 14367 * may have to handle aggregate on LHS, but we can't 14368 * have common scalars. */ 14369 if (rscalars < 2) 14370 o->op_private &= 14371 ~(OPpASSIGN_COMMON_SCALAR|OPpASSIGN_COMMON_RC1); 14372 14373 break; 14374 } 14375 14376 case OP_CUSTOM: { 14377 Perl_cpeep_t cpeep = 14378 XopENTRYCUSTOM(o, xop_peep); 14379 if (cpeep) 14380 cpeep(aTHX_ o, oldop); 14381 break; 14382 } 14383 14384 } 14385 /* did we just null the current op? If so, re-process it to handle 14386 * eliding "empty" ops from the chain */ 14387 if (o->op_type == OP_NULL && oldop && oldop->op_next == o) { 14388 o->op_opt = 0; 14389 o = oldop; 14390 } 14391 else { 14392 oldoldop = oldop; 14393 oldop = o; 14394 } 14395 } 14396 LEAVE; 14397 } 14398 14399 void 14400 Perl_peep(pTHX_ OP *o) 14401 { 14402 CALL_RPEEP(o); 14403 } 14404 14405 /* 14406 =head1 Custom Operators 14407 14408 =for apidoc Ao||custom_op_xop 14409 Return the XOP structure for a given custom op. This macro should be 14410 considered internal to C<OP_NAME> and the other access macros: use them instead. 14411 This macro does call a function. Prior 14412 to 5.19.6, this was implemented as a 14413 function. 14414 14415 =cut 14416 */ 14417 14418 XOPRETANY 14419 Perl_custom_op_get_field(pTHX_ const OP *o, const xop_flags_enum field) 14420 { 14421 SV *keysv; 14422 HE *he = NULL; 14423 XOP *xop; 14424 14425 static const XOP xop_null = { 0, 0, 0, 0, 0 }; 14426 14427 PERL_ARGS_ASSERT_CUSTOM_OP_GET_FIELD; 14428 assert(o->op_type == OP_CUSTOM); 14429 14430 /* This is wrong. It assumes a function pointer can be cast to IV, 14431 * which isn't guaranteed, but this is what the old custom OP code 14432 * did. In principle it should be safer to Copy the bytes of the 14433 * pointer into a PV: since the new interface is hidden behind 14434 * functions, this can be changed later if necessary. */ 14435 /* Change custom_op_xop if this ever happens */ 14436 keysv = sv_2mortal(newSViv(PTR2IV(o->op_ppaddr))); 14437 14438 if (PL_custom_ops) 14439 he = hv_fetch_ent(PL_custom_ops, keysv, 0, 0); 14440 14441 /* assume noone will have just registered a desc */ 14442 if (!he && PL_custom_op_names && 14443 (he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0)) 14444 ) { 14445 const char *pv; 14446 STRLEN l; 14447 14448 /* XXX does all this need to be shared mem? */ 14449 Newxz(xop, 1, XOP); 14450 pv = SvPV(HeVAL(he), l); 14451 XopENTRY_set(xop, xop_name, savepvn(pv, l)); 14452 if (PL_custom_op_descs && 14453 (he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0)) 14454 ) { 14455 pv = SvPV(HeVAL(he), l); 14456 XopENTRY_set(xop, xop_desc, savepvn(pv, l)); 14457 } 14458 Perl_custom_op_register(aTHX_ o->op_ppaddr, xop); 14459 } 14460 else { 14461 if (!he) 14462 xop = (XOP *)&xop_null; 14463 else 14464 xop = INT2PTR(XOP *, SvIV(HeVAL(he))); 14465 } 14466 { 14467 XOPRETANY any; 14468 if(field == XOPe_xop_ptr) { 14469 any.xop_ptr = xop; 14470 } else { 14471 const U32 flags = XopFLAGS(xop); 14472 if(flags & field) { 14473 switch(field) { 14474 case XOPe_xop_name: 14475 any.xop_name = xop->xop_name; 14476 break; 14477 case XOPe_xop_desc: 14478 any.xop_desc = xop->xop_desc; 14479 break; 14480 case XOPe_xop_class: 14481 any.xop_class = xop->xop_class; 14482 break; 14483 case XOPe_xop_peep: 14484 any.xop_peep = xop->xop_peep; 14485 break; 14486 default: 14487 NOT_REACHED; /* NOTREACHED */ 14488 break; 14489 } 14490 } else { 14491 switch(field) { 14492 case XOPe_xop_name: 14493 any.xop_name = XOPd_xop_name; 14494 break; 14495 case XOPe_xop_desc: 14496 any.xop_desc = XOPd_xop_desc; 14497 break; 14498 case XOPe_xop_class: 14499 any.xop_class = XOPd_xop_class; 14500 break; 14501 case XOPe_xop_peep: 14502 any.xop_peep = XOPd_xop_peep; 14503 break; 14504 default: 14505 NOT_REACHED; /* NOTREACHED */ 14506 break; 14507 } 14508 } 14509 } 14510 /* On some platforms (HP-UX, IA64) gcc emits a warning for this function: 14511 * op.c: In function 'Perl_custom_op_get_field': 14512 * op.c:...: warning: 'any.xop_name' may be used uninitialized in this function [-Wmaybe-uninitialized] 14513 * This is because on those platforms (with -DEBUGGING) NOT_REACHED 14514 * expands to assert(0), which expands to ((0) ? (void)0 : 14515 * __assert(...)), and gcc doesn't know that __assert can never return. */ 14516 return any; 14517 } 14518 } 14519 14520 /* 14521 =for apidoc Ao||custom_op_register 14522 Register a custom op. See L<perlguts/"Custom Operators">. 14523 14524 =cut 14525 */ 14526 14527 void 14528 Perl_custom_op_register(pTHX_ Perl_ppaddr_t ppaddr, const XOP *xop) 14529 { 14530 SV *keysv; 14531 14532 PERL_ARGS_ASSERT_CUSTOM_OP_REGISTER; 14533 14534 /* see the comment in custom_op_xop */ 14535 keysv = sv_2mortal(newSViv(PTR2IV(ppaddr))); 14536 14537 if (!PL_custom_ops) 14538 PL_custom_ops = newHV(); 14539 14540 if (!hv_store_ent(PL_custom_ops, keysv, newSViv(PTR2IV(xop)), 0)) 14541 Perl_croak(aTHX_ "panic: can't register custom OP %s", xop->xop_name); 14542 } 14543 14544 /* 14545 14546 =for apidoc core_prototype 14547 14548 This function assigns the prototype of the named core function to C<sv>, or 14549 to a new mortal SV if C<sv> is C<NULL>. It returns the modified C<sv>, or 14550 C<NULL> if the core function has no prototype. C<code> is a code as returned 14551 by C<keyword()>. It must not be equal to 0. 14552 14553 =cut 14554 */ 14555 14556 SV * 14557 Perl_core_prototype(pTHX_ SV *sv, const char *name, const int code, 14558 int * const opnum) 14559 { 14560 int i = 0, n = 0, seen_question = 0, defgv = 0; 14561 I32 oa; 14562 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2) 14563 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */ 14564 bool nullret = FALSE; 14565 14566 PERL_ARGS_ASSERT_CORE_PROTOTYPE; 14567 14568 assert (code); 14569 14570 if (!sv) sv = sv_newmortal(); 14571 14572 #define retsetpvs(x,y) sv_setpvs(sv, x); if(opnum) *opnum=(y); return sv 14573 14574 switch (code < 0 ? -code : code) { 14575 case KEY_and : case KEY_chop: case KEY_chomp: 14576 case KEY_cmp : case KEY_defined: case KEY_delete: case KEY_exec : 14577 case KEY_exists: case KEY_eq : case KEY_ge : case KEY_goto : 14578 case KEY_grep : case KEY_gt : case KEY_last : case KEY_le : 14579 case KEY_lt : case KEY_map : case KEY_ne : case KEY_next : 14580 case KEY_or : case KEY_print : case KEY_printf: case KEY_qr : 14581 case KEY_redo : case KEY_require: case KEY_return: case KEY_say : 14582 case KEY_select: case KEY_sort : case KEY_split : case KEY_system: 14583 case KEY_x : case KEY_xor : 14584 if (!opnum) return NULL; nullret = TRUE; goto findopnum; 14585 case KEY_glob: retsetpvs("_;", OP_GLOB); 14586 case KEY_keys: retsetpvs("\\[%@]", OP_KEYS); 14587 case KEY_values: retsetpvs("\\[%@]", OP_VALUES); 14588 case KEY_each: retsetpvs("\\[%@]", OP_EACH); 14589 case KEY_push: retsetpvs("\\@@", OP_PUSH); 14590 case KEY_unshift: retsetpvs("\\@@", OP_UNSHIFT); 14591 case KEY_pop: retsetpvs(";\\@", OP_POP); 14592 case KEY_shift: retsetpvs(";\\@", OP_SHIFT); 14593 case KEY_pos: retsetpvs(";\\[$*]", OP_POS); 14594 case KEY_splice: 14595 retsetpvs("\\@;$$@", OP_SPLICE); 14596 case KEY___FILE__: case KEY___LINE__: case KEY___PACKAGE__: 14597 retsetpvs("", 0); 14598 case KEY_evalbytes: 14599 name = "entereval"; break; 14600 case KEY_readpipe: 14601 name = "backtick"; 14602 } 14603 14604 #undef retsetpvs 14605 14606 findopnum: 14607 while (i < MAXO) { /* The slow way. */ 14608 if (strEQ(name, PL_op_name[i]) 14609 || strEQ(name, PL_op_desc[i])) 14610 { 14611 if (nullret) { assert(opnum); *opnum = i; return NULL; } 14612 goto found; 14613 } 14614 i++; 14615 } 14616 return NULL; 14617 found: 14618 defgv = PL_opargs[i] & OA_DEFGV; 14619 oa = PL_opargs[i] >> OASHIFT; 14620 while (oa) { 14621 if (oa & OA_OPTIONAL && !seen_question && ( 14622 !defgv || (oa & (OA_OPTIONAL - 1)) == OA_FILEREF 14623 )) { 14624 seen_question = 1; 14625 str[n++] = ';'; 14626 } 14627 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF 14628 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF 14629 /* But globs are already references (kinda) */ 14630 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF 14631 ) { 14632 str[n++] = '\\'; 14633 } 14634 if ((oa & (OA_OPTIONAL - 1)) == OA_SCALARREF 14635 && !scalar_mod_type(NULL, i)) { 14636 str[n++] = '['; 14637 str[n++] = '$'; 14638 str[n++] = '@'; 14639 str[n++] = '%'; 14640 if (i == OP_LOCK || i == OP_UNDEF) str[n++] = '&'; 14641 str[n++] = '*'; 14642 str[n++] = ']'; 14643 } 14644 else str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)]; 14645 if (oa & OA_OPTIONAL && defgv && str[n-1] == '$') { 14646 str[n-1] = '_'; defgv = 0; 14647 } 14648 oa = oa >> 4; 14649 } 14650 if (code == -KEY_not || code == -KEY_getprotobynumber) str[n++] = ';'; 14651 str[n++] = '\0'; 14652 sv_setpvn(sv, str, n - 1); 14653 if (opnum) *opnum = i; 14654 return sv; 14655 } 14656 14657 OP * 14658 Perl_coresub_op(pTHX_ SV * const coreargssv, const int code, 14659 const int opnum) 14660 { 14661 OP * const argop = newSVOP(OP_COREARGS,0,coreargssv); 14662 OP *o; 14663 14664 PERL_ARGS_ASSERT_CORESUB_OP; 14665 14666 switch(opnum) { 14667 case 0: 14668 return op_append_elem(OP_LINESEQ, 14669 argop, 14670 newSLICEOP(0, 14671 newSVOP(OP_CONST, 0, newSViv(-code % 3)), 14672 newOP(OP_CALLER,0) 14673 ) 14674 ); 14675 case OP_SELECT: /* which represents OP_SSELECT as well */ 14676 if (code) 14677 return newCONDOP( 14678 0, 14679 newBINOP(OP_GT, 0, 14680 newAVREF(newGVOP(OP_GV, 0, PL_defgv)), 14681 newSVOP(OP_CONST, 0, newSVuv(1)) 14682 ), 14683 coresub_op(newSVuv((UV)OP_SSELECT), 0, 14684 OP_SSELECT), 14685 coresub_op(coreargssv, 0, OP_SELECT) 14686 ); 14687 /* FALLTHROUGH */ 14688 default: 14689 switch (PL_opargs[opnum] & OA_CLASS_MASK) { 14690 case OA_BASEOP: 14691 return op_append_elem( 14692 OP_LINESEQ, argop, 14693 newOP(opnum, 14694 opnum == OP_WANTARRAY || opnum == OP_RUNCV 14695 ? OPpOFFBYONE << 8 : 0) 14696 ); 14697 case OA_BASEOP_OR_UNOP: 14698 if (opnum == OP_ENTEREVAL) { 14699 o = newUNOP(OP_ENTEREVAL,OPpEVAL_COPHH<<8,argop); 14700 if (code == -KEY_evalbytes) o->op_private |= OPpEVAL_BYTES; 14701 } 14702 else o = newUNOP(opnum,0,argop); 14703 if (opnum == OP_CALLER) o->op_private |= OPpOFFBYONE; 14704 else { 14705 onearg: 14706 if (is_handle_constructor(o, 1)) 14707 argop->op_private |= OPpCOREARGS_DEREF1; 14708 if (scalar_mod_type(NULL, opnum)) 14709 argop->op_private |= OPpCOREARGS_SCALARMOD; 14710 } 14711 return o; 14712 default: 14713 o = op_convert_list(opnum,OPf_SPECIAL*(opnum == OP_GLOB),argop); 14714 if (is_handle_constructor(o, 2)) 14715 argop->op_private |= OPpCOREARGS_DEREF2; 14716 if (opnum == OP_SUBSTR) { 14717 o->op_private |= OPpMAYBE_LVSUB; 14718 return o; 14719 } 14720 else goto onearg; 14721 } 14722 } 14723 } 14724 14725 void 14726 Perl_report_redefined_cv(pTHX_ const SV *name, const CV *old_cv, 14727 SV * const *new_const_svp) 14728 { 14729 const char *hvname; 14730 bool is_const = !!CvCONST(old_cv); 14731 SV *old_const_sv = is_const ? cv_const_sv(old_cv) : NULL; 14732 14733 PERL_ARGS_ASSERT_REPORT_REDEFINED_CV; 14734 14735 if (is_const && new_const_svp && old_const_sv == *new_const_svp) 14736 return; 14737 /* They are 2 constant subroutines generated from 14738 the same constant. This probably means that 14739 they are really the "same" proxy subroutine 14740 instantiated in 2 places. Most likely this is 14741 when a constant is exported twice. Don't warn. 14742 */ 14743 if ( 14744 (ckWARN(WARN_REDEFINE) 14745 && !( 14746 CvGV(old_cv) && GvSTASH(CvGV(old_cv)) 14747 && HvNAMELEN(GvSTASH(CvGV(old_cv))) == 7 14748 && (hvname = HvNAME(GvSTASH(CvGV(old_cv))), 14749 strEQ(hvname, "autouse")) 14750 ) 14751 ) 14752 || (is_const 14753 && ckWARN_d(WARN_REDEFINE) 14754 && (!new_const_svp || sv_cmp(old_const_sv, *new_const_svp)) 14755 ) 14756 ) 14757 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), 14758 is_const 14759 ? "Constant subroutine %"SVf" redefined" 14760 : "Subroutine %"SVf" redefined", 14761 SVfARG(name)); 14762 } 14763 14764 /* 14765 =head1 Hook manipulation 14766 14767 These functions provide convenient and thread-safe means of manipulating 14768 hook variables. 14769 14770 =cut 14771 */ 14772 14773 /* 14774 =for apidoc Am|void|wrap_op_checker|Optype opcode|Perl_check_t new_checker|Perl_check_t *old_checker_p 14775 14776 Puts a C function into the chain of check functions for a specified op 14777 type. This is the preferred way to manipulate the L</PL_check> array. 14778 C<opcode> specifies which type of op is to be affected. C<new_checker> 14779 is a pointer to the C function that is to be added to that opcode's 14780 check chain, and C<old_checker_p> points to the storage location where a 14781 pointer to the next function in the chain will be stored. The value of 14782 C<new_pointer> is written into the L</PL_check> array, while the value 14783 previously stored there is written to C<*old_checker_p>. 14784 14785 The function should be defined like this: 14786 14787 static OP *new_checker(pTHX_ OP *op) { ... } 14788 14789 It is intended to be called in this manner: 14790 14791 new_checker(aTHX_ op) 14792 14793 C<old_checker_p> should be defined like this: 14794 14795 static Perl_check_t old_checker_p; 14796 14797 L</PL_check> is global to an entire process, and a module wishing to 14798 hook op checking may find itself invoked more than once per process, 14799 typically in different threads. To handle that situation, this function 14800 is idempotent. The location C<*old_checker_p> must initially (once 14801 per process) contain a null pointer. A C variable of static duration 14802 (declared at file scope, typically also marked C<static> to give 14803 it internal linkage) will be implicitly initialised appropriately, 14804 if it does not have an explicit initialiser. This function will only 14805 actually modify the check chain if it finds C<*old_checker_p> to be null. 14806 This function is also thread safe on the small scale. It uses appropriate 14807 locking to avoid race conditions in accessing L</PL_check>. 14808 14809 When this function is called, the function referenced by C<new_checker> 14810 must be ready to be called, except for C<*old_checker_p> being unfilled. 14811 In a threading situation, C<new_checker> may be called immediately, 14812 even before this function has returned. C<*old_checker_p> will always 14813 be appropriately set before C<new_checker> is called. If C<new_checker> 14814 decides not to do anything special with an op that it is given (which 14815 is the usual case for most uses of op check hooking), it must chain the 14816 check function referenced by C<*old_checker_p>. 14817 14818 If you want to influence compilation of calls to a specific subroutine, 14819 then use L</cv_set_call_checker> rather than hooking checking of all 14820 C<entersub> ops. 14821 14822 =cut 14823 */ 14824 14825 void 14826 Perl_wrap_op_checker(pTHX_ Optype opcode, 14827 Perl_check_t new_checker, Perl_check_t *old_checker_p) 14828 { 14829 dVAR; 14830 14831 PERL_UNUSED_CONTEXT; 14832 PERL_ARGS_ASSERT_WRAP_OP_CHECKER; 14833 if (*old_checker_p) return; 14834 OP_CHECK_MUTEX_LOCK; 14835 if (!*old_checker_p) { 14836 *old_checker_p = PL_check[opcode]; 14837 PL_check[opcode] = new_checker; 14838 } 14839 OP_CHECK_MUTEX_UNLOCK; 14840 } 14841 14842 #include "XSUB.h" 14843 14844 /* Efficient sub that returns a constant scalar value. */ 14845 static void 14846 const_sv_xsub(pTHX_ CV* cv) 14847 { 14848 dXSARGS; 14849 SV *const sv = MUTABLE_SV(XSANY.any_ptr); 14850 PERL_UNUSED_ARG(items); 14851 if (!sv) { 14852 XSRETURN(0); 14853 } 14854 EXTEND(sp, 1); 14855 ST(0) = sv; 14856 XSRETURN(1); 14857 } 14858 14859 static void 14860 const_av_xsub(pTHX_ CV* cv) 14861 { 14862 dXSARGS; 14863 AV * const av = MUTABLE_AV(XSANY.any_ptr); 14864 SP -= items; 14865 assert(av); 14866 #ifndef DEBUGGING 14867 if (!av) { 14868 XSRETURN(0); 14869 } 14870 #endif 14871 if (SvRMAGICAL(av)) 14872 Perl_croak(aTHX_ "Magical list constants are not supported"); 14873 if (GIMME_V != G_ARRAY) { 14874 EXTEND(SP, 1); 14875 ST(0) = sv_2mortal(newSViv((IV)AvFILLp(av)+1)); 14876 XSRETURN(1); 14877 } 14878 EXTEND(SP, AvFILLp(av)+1); 14879 Copy(AvARRAY(av), &ST(0), AvFILLp(av)+1, SV *); 14880 XSRETURN(AvFILLp(av)+1); 14881 } 14882 14883 /* 14884 * ex: set ts=8 sts=4 sw=4 et: 14885 */ 14886