1 /* pad.c 2 * 3 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 4 * by Larry Wall and others 5 * 6 * You may distribute under the terms of either the GNU General Public 7 * License or the Artistic License, as specified in the README file. 8 */ 9 10 /* 11 * 'Anyway: there was this Mr. Frodo left an orphan and stranded, as you 12 * might say, among those queer Bucklanders, being brought up anyhow in 13 * Brandy Hall. A regular warren, by all accounts. Old Master Gorbadoc 14 * never had fewer than a couple of hundred relations in the place. 15 * Mr. Bilbo never did a kinder deed than when he brought the lad back 16 * to live among decent folk.' --the Gaffer 17 * 18 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"] 19 */ 20 21 /* XXX DAPM 22 * As of Sept 2002, this file is new and may be in a state of flux for 23 * a while. I've marked things I intent to come back and look at further 24 * with an 'XXX DAPM' comment. 25 */ 26 27 /* 28 =head1 Pad Data Structures 29 30 =for apidoc Amx|PADLIST *|CvPADLIST|CV *cv 31 32 CV's can have CvPADLIST(cv) set to point to a PADLIST. This is the CV's 33 scratchpad, which stores lexical variables and opcode temporary and 34 per-thread values. 35 36 For these purposes "formats" are a kind-of CV; eval""s are too (except they're 37 not callable at will and are always thrown away after the eval"" is done 38 executing). Require'd files are simply evals without any outer lexical 39 scope. 40 41 XSUBs don't have CvPADLIST set - dXSTARG fetches values from PL_curpad, 42 but that is really the callers pad (a slot of which is allocated by 43 every entersub). 44 45 The PADLIST has a C array where pads are stored. 46 47 The 0th entry of the PADLIST is a PADNAMELIST (which is actually just an 48 AV, but that may change) which represents the "names" or rather 49 the "static type information" for lexicals. The individual elements of a 50 PADNAMELIST are PADNAMEs (just SVs; but, again, that may change). Future 51 refactorings might stop the PADNAMELIST from being stored in the PADLIST's 52 array, so don't rely on it. See L</PadlistNAMES>. 53 54 The CvDEPTH'th entry of a PADLIST is a PAD (an AV) which is the stack frame 55 at that depth of recursion into the CV. The 0th slot of a frame AV is an 56 AV which is @_. Other entries are storage for variables and op targets. 57 58 Iterating over the PADNAMELIST iterates over all possible pad 59 items. Pad slots for targets (SVs_PADTMP) 60 and GVs end up having &PL_sv_undef 61 "names", while slots for constants have &PL_sv_no "names" (see 62 pad_alloc()). That &PL_sv_no is used is an implementation detail subject 63 to change. To test for it, use C<PadnamePV(name) && !PadnameLEN(name)>. 64 65 Only my/our variable (SvPADMY/PADNAME_isOUR) slots get valid names. 66 The rest are op targets/GVs/constants which are statically allocated 67 or resolved at compile time. These don't have names by which they 68 can be looked up from Perl code at run time through eval"" the way 69 my/our variables can be. Since they can't be looked up by "name" 70 but only by their index allocated at compile time (which is usually 71 in PL_op->op_targ), wasting a name SV for them doesn't make sense. 72 73 The SVs in the names AV have their PV being the name of the variable. 74 xlow+1..xhigh inclusive in the NV union is a range of cop_seq numbers for 75 which the name is valid (accessed through the macros COP_SEQ_RANGE_LOW and 76 _HIGH). During compilation, these fields may hold the special value 77 PERL_PADSEQ_INTRO to indicate various stages: 78 79 COP_SEQ_RANGE_LOW _HIGH 80 ----------------- ----- 81 PERL_PADSEQ_INTRO 0 variable not yet introduced: { my ($x 82 valid-seq# PERL_PADSEQ_INTRO variable in scope: { my ($x) 83 valid-seq# valid-seq# compilation of scope complete: { my ($x) } 84 85 For typed lexicals name SV is SVt_PVMG and SvSTASH 86 points at the type. For C<our> lexicals, the type is also SVt_PVMG, with the 87 SvOURSTASH slot pointing at the stash of the associated global (so that 88 duplicate C<our> declarations in the same package can be detected). SvUVX is 89 sometimes hijacked to store the generation number during compilation. 90 91 If PADNAME_OUTER (SvFAKE) is set on the 92 name SV, then that slot in the frame AV is 93 a REFCNT'ed reference to a lexical from "outside". In this case, 94 the name SV does not use xlow and xhigh to store a cop_seq range, since it is 95 in scope throughout. Instead xhigh stores some flags containing info about 96 the real lexical (is it declared in an anon, and is it capable of being 97 instantiated multiple times?), and for fake ANONs, xlow contains the index 98 within the parent's pad where the lexical's value is stored, to make 99 cloning quicker. 100 101 If the 'name' is '&' the corresponding entry in the PAD 102 is a CV representing a possible closure. 103 (PADNAME_OUTER and name of '&' is not a 104 meaningful combination currently but could 105 become so if C<my sub foo {}> is implemented.) 106 107 Note that formats are treated as anon subs, and are cloned each time 108 write is called (if necessary). 109 110 The flag SVs_PADSTALE is cleared on lexicals each time the my() is executed, 111 and set on scope exit. This allows the 112 'Variable $x is not available' warning 113 to be generated in evals, such as 114 115 { my $x = 1; sub f { eval '$x'} } f(); 116 117 For state vars, SVs_PADSTALE is overloaded to mean 'not yet initialised'. 118 119 =for apidoc AmxU|PADNAMELIST *|PL_comppad_name 120 121 During compilation, this points to the array containing the names part 122 of the pad for the currently-compiling code. 123 124 =for apidoc AmxU|PAD *|PL_comppad 125 126 During compilation, this points to the array containing the values 127 part of the pad for the currently-compiling code. (At runtime a CV may 128 have many such value arrays; at compile time just one is constructed.) 129 At runtime, this points to the array containing the currently-relevant 130 values for the pad for the currently-executing code. 131 132 =for apidoc AmxU|SV **|PL_curpad 133 134 Points directly to the body of the L</PL_comppad> array. 135 (I.e., this is C<PAD_ARRAY(PL_comppad)>.) 136 137 =cut 138 */ 139 140 141 #include "EXTERN.h" 142 #define PERL_IN_PAD_C 143 #include "perl.h" 144 #include "keywords.h" 145 146 #define COP_SEQ_RANGE_LOW_set(sv,val) \ 147 STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xlow = (val); } STMT_END 148 #define COP_SEQ_RANGE_HIGH_set(sv,val) \ 149 STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xhigh = (val); } STMT_END 150 151 #define PARENT_PAD_INDEX_set(sv,val) \ 152 STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xlow = (val); } STMT_END 153 #define PARENT_FAKELEX_FLAGS_set(sv,val) \ 154 STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xhigh = (val); } STMT_END 155 156 /* 157 =for apidoc mx|void|pad_peg|const char *s 158 159 When PERL_MAD is enabled, this is a small no-op function that gets called 160 at the start of each pad-related function. It can be breakpointed to 161 track all pad operations. The parameter is a string indicating the type 162 of pad operation being performed. 163 164 =cut 165 */ 166 167 #ifdef PERL_MAD 168 void pad_peg(const char* s) { 169 static int pegcnt; /* XXX not threadsafe */ 170 PERL_UNUSED_ARG(s); 171 172 PERL_ARGS_ASSERT_PAD_PEG; 173 174 pegcnt++; 175 } 176 #endif 177 178 /* 179 This is basically sv_eq_flags() in sv.c, but we avoid the magic 180 and bytes checking. 181 */ 182 183 static bool 184 sv_eq_pvn_flags(pTHX_ const SV *sv, const char* pv, const STRLEN pvlen, const U32 flags) { 185 if ( (SvUTF8(sv) & SVf_UTF8 ) != (flags & SVf_UTF8) ) { 186 const char *pv1 = SvPVX_const(sv); 187 STRLEN cur1 = SvCUR(sv); 188 const char *pv2 = pv; 189 STRLEN cur2 = pvlen; 190 if (PL_encoding) { 191 SV* svrecode = NULL; 192 if (SvUTF8(sv)) { 193 svrecode = newSVpvn(pv2, cur2); 194 sv_recode_to_utf8(svrecode, PL_encoding); 195 pv2 = SvPV_const(svrecode, cur2); 196 } 197 else { 198 svrecode = newSVpvn(pv1, cur1); 199 sv_recode_to_utf8(svrecode, PL_encoding); 200 pv1 = SvPV_const(svrecode, cur1); 201 } 202 SvREFCNT_dec_NN(svrecode); 203 } 204 if (flags & SVf_UTF8) 205 return (bytes_cmp_utf8( 206 (const U8*)pv1, cur1, 207 (const U8*)pv2, cur2) == 0); 208 else 209 return (bytes_cmp_utf8( 210 (const U8*)pv2, cur2, 211 (const U8*)pv1, cur1) == 0); 212 } 213 else 214 return ((SvPVX_const(sv) == pv) 215 || memEQ(SvPVX_const(sv), pv, pvlen)); 216 } 217 218 219 /* 220 =for apidoc Am|PADLIST *|pad_new|int flags 221 222 Create a new padlist, updating the global variables for the 223 currently-compiling padlist to point to the new padlist. The following 224 flags can be OR'ed together: 225 226 padnew_CLONE this pad is for a cloned CV 227 padnew_SAVE save old globals on the save stack 228 padnew_SAVESUB also save extra stuff for start of sub 229 230 =cut 231 */ 232 233 PADLIST * 234 Perl_pad_new(pTHX_ int flags) 235 { 236 dVAR; 237 PADLIST *padlist; 238 PAD *padname, *pad; 239 PAD **ary; 240 241 ASSERT_CURPAD_LEGAL("pad_new"); 242 243 /* XXX DAPM really need a new SAVEt_PAD which restores all or most 244 * vars (based on flags) rather than storing vals + addresses for 245 * each individually. Also see pad_block_start. 246 * XXX DAPM Try to see whether all these conditionals are required 247 */ 248 249 /* save existing state, ... */ 250 251 if (flags & padnew_SAVE) { 252 SAVECOMPPAD(); 253 if (! (flags & padnew_CLONE)) { 254 SAVESPTR(PL_comppad_name); 255 SAVEI32(PL_padix); 256 SAVEI32(PL_comppad_name_fill); 257 SAVEI32(PL_min_intro_pending); 258 SAVEI32(PL_max_intro_pending); 259 SAVEBOOL(PL_cv_has_eval); 260 if (flags & padnew_SAVESUB) { 261 SAVEBOOL(PL_pad_reset_pending); 262 } 263 } 264 } 265 /* XXX DAPM interestingly, PL_comppad_name_floor never seems to be 266 * saved - check at some pt that this is okay */ 267 268 /* ... create new pad ... */ 269 270 Newxz(padlist, 1, PADLIST); 271 pad = newAV(); 272 273 if (flags & padnew_CLONE) { 274 /* XXX DAPM I dont know why cv_clone needs it 275 * doing differently yet - perhaps this separate branch can be 276 * dispensed with eventually ??? 277 */ 278 279 AV * const a0 = newAV(); /* will be @_ */ 280 av_store(pad, 0, MUTABLE_SV(a0)); 281 AvREIFY_only(a0); 282 283 padname = (PAD *)SvREFCNT_inc_simple_NN(PL_comppad_name); 284 } 285 else { 286 av_store(pad, 0, NULL); 287 padname = newAV(); 288 AvPAD_NAMELIST_on(padname); 289 av_store(padname, 0, &PL_sv_undef); 290 } 291 292 /* Most subroutines never recurse, hence only need 2 entries in the padlist 293 array - names, and depth=1. The default for av_store() is to allocate 294 0..3, and even an explicit call to av_extend() with <3 will be rounded 295 up, so we inline the allocation of the array here. */ 296 Newx(ary, 2, PAD *); 297 PadlistMAX(padlist) = 1; 298 PadlistARRAY(padlist) = ary; 299 ary[0] = padname; 300 ary[1] = pad; 301 302 /* ... then update state variables */ 303 304 PL_comppad = pad; 305 PL_curpad = AvARRAY(pad); 306 307 if (! (flags & padnew_CLONE)) { 308 PL_comppad_name = padname; 309 PL_comppad_name_fill = 0; 310 PL_min_intro_pending = 0; 311 PL_padix = 0; 312 PL_cv_has_eval = 0; 313 } 314 315 DEBUG_X(PerlIO_printf(Perl_debug_log, 316 "Pad 0x%"UVxf"[0x%"UVxf"] new: compcv=0x%"UVxf 317 " name=0x%"UVxf" flags=0x%"UVxf"\n", 318 PTR2UV(PL_comppad), PTR2UV(PL_curpad), PTR2UV(PL_compcv), 319 PTR2UV(padname), (UV)flags 320 ) 321 ); 322 323 return (PADLIST*)padlist; 324 } 325 326 327 /* 328 =head1 Embedding Functions 329 330 =for apidoc cv_undef 331 332 Clear out all the active components of a CV. This can happen either 333 by an explicit C<undef &foo>, or by the reference count going to zero. 334 In the former case, we keep the CvOUTSIDE pointer, so that any anonymous 335 children can still follow the full lexical scope chain. 336 337 =cut 338 */ 339 340 void 341 Perl_cv_undef(pTHX_ CV *cv) 342 { 343 dVAR; 344 const PADLIST *padlist = CvPADLIST(cv); 345 bool const slabbed = !!CvSLABBED(cv); 346 347 PERL_ARGS_ASSERT_CV_UNDEF; 348 349 DEBUG_X(PerlIO_printf(Perl_debug_log, 350 "CV undef: cv=0x%"UVxf" comppad=0x%"UVxf"\n", 351 PTR2UV(cv), PTR2UV(PL_comppad)) 352 ); 353 354 if (CvFILE(cv) && CvDYNFILE(cv)) { 355 Safefree(CvFILE(cv)); 356 } 357 CvFILE(cv) = NULL; 358 359 CvSLABBED_off(cv); 360 if (!CvISXSUB(cv) && CvROOT(cv)) { 361 if (SvTYPE(cv) == SVt_PVCV && CvDEPTH(cv)) 362 Perl_croak(aTHX_ "Can't undef active subroutine"); 363 ENTER; 364 365 PAD_SAVE_SETNULLPAD(); 366 367 if (slabbed) OpslabREFCNT_dec_padok(OpSLAB(CvROOT(cv))); 368 op_free(CvROOT(cv)); 369 CvROOT(cv) = NULL; 370 CvSTART(cv) = NULL; 371 LEAVE; 372 } 373 else if (slabbed && CvSTART(cv)) { 374 ENTER; 375 PAD_SAVE_SETNULLPAD(); 376 377 /* discard any leaked ops */ 378 if (PL_parser) 379 parser_free_nexttoke_ops(PL_parser, (OPSLAB *)CvSTART(cv)); 380 opslab_force_free((OPSLAB *)CvSTART(cv)); 381 CvSTART(cv) = NULL; 382 383 LEAVE; 384 } 385 #ifdef DEBUGGING 386 else if (slabbed) Perl_warn(aTHX_ "Slab leaked from cv %p", cv); 387 #endif 388 SvPOK_off(MUTABLE_SV(cv)); /* forget prototype */ 389 sv_unmagic((SV *)cv, PERL_MAGIC_checkcall); 390 if (CvNAMED(cv)) CvNAME_HEK_set(cv, NULL); 391 else CvGV_set(cv, NULL); 392 393 /* This statement and the subsequence if block was pad_undef(). */ 394 pad_peg("pad_undef"); 395 396 if (padlist) { 397 I32 ix; 398 399 /* Free the padlist associated with a CV. 400 If parts of it happen to be current, we null the relevant PL_*pad* 401 global vars so that we don't have any dangling references left. 402 We also repoint the CvOUTSIDE of any about-to-be-orphaned inner 403 subs to the outer of this cv. */ 404 405 DEBUG_X(PerlIO_printf(Perl_debug_log, 406 "Pad undef: cv=0x%"UVxf" padlist=0x%"UVxf" comppad=0x%"UVxf"\n", 407 PTR2UV(cv), PTR2UV(padlist), PTR2UV(PL_comppad)) 408 ); 409 410 /* detach any '&' anon children in the pad; if afterwards they 411 * are still live, fix up their CvOUTSIDEs to point to our outside, 412 * bypassing us. */ 413 /* XXX DAPM for efficiency, we should only do this if we know we have 414 * children, or integrate this loop with general cleanup */ 415 416 if (PL_phase != PERL_PHASE_DESTRUCT) { /* don't bother during global destruction */ 417 CV * const outercv = CvOUTSIDE(cv); 418 const U32 seq = CvOUTSIDE_SEQ(cv); 419 PAD * const comppad_name = PadlistARRAY(padlist)[0]; 420 SV ** const namepad = AvARRAY(comppad_name); 421 PAD * const comppad = PadlistARRAY(padlist)[1]; 422 SV ** const curpad = AvARRAY(comppad); 423 for (ix = AvFILLp(comppad_name); ix > 0; ix--) { 424 SV * const namesv = namepad[ix]; 425 if (namesv && namesv != &PL_sv_undef 426 && *SvPVX_const(namesv) == '&') 427 { 428 CV * const innercv = MUTABLE_CV(curpad[ix]); 429 U32 inner_rc = SvREFCNT(innercv); 430 assert(inner_rc); 431 assert(SvTYPE(innercv) != SVt_PVFM); 432 433 if (SvREFCNT(comppad) < 2) { /* allow for /(?{ sub{} })/ */ 434 curpad[ix] = NULL; 435 SvREFCNT_dec_NN(innercv); 436 inner_rc--; 437 } 438 439 /* in use, not just a prototype */ 440 if (inner_rc && (CvOUTSIDE(innercv) == cv)) { 441 assert(CvWEAKOUTSIDE(innercv)); 442 /* don't relink to grandfather if he's being freed */ 443 if (outercv && SvREFCNT(outercv)) { 444 CvWEAKOUTSIDE_off(innercv); 445 CvOUTSIDE(innercv) = outercv; 446 CvOUTSIDE_SEQ(innercv) = seq; 447 SvREFCNT_inc_simple_void_NN(outercv); 448 } 449 else { 450 CvOUTSIDE(innercv) = NULL; 451 } 452 } 453 } 454 } 455 } 456 457 ix = PadlistMAX(padlist); 458 while (ix > 0) { 459 PAD * const sv = PadlistARRAY(padlist)[ix--]; 460 if (sv) { 461 if (sv == PL_comppad) { 462 PL_comppad = NULL; 463 PL_curpad = NULL; 464 } 465 SvREFCNT_dec_NN(sv); 466 } 467 } 468 { 469 PAD * const sv = PadlistARRAY(padlist)[0]; 470 if (sv == PL_comppad_name && SvREFCNT(sv) == 1) 471 PL_comppad_name = NULL; 472 SvREFCNT_dec(sv); 473 } 474 if (PadlistARRAY(padlist)) Safefree(PadlistARRAY(padlist)); 475 Safefree(padlist); 476 CvPADLIST(cv) = NULL; 477 } 478 479 480 /* remove CvOUTSIDE unless this is an undef rather than a free */ 481 if (!SvREFCNT(cv) && CvOUTSIDE(cv)) { 482 if (!CvWEAKOUTSIDE(cv)) 483 SvREFCNT_dec(CvOUTSIDE(cv)); 484 CvOUTSIDE(cv) = NULL; 485 } 486 if (CvCONST(cv)) { 487 SvREFCNT_dec(MUTABLE_SV(CvXSUBANY(cv).any_ptr)); 488 CvCONST_off(cv); 489 } 490 if (CvISXSUB(cv) && CvXSUB(cv)) { 491 CvXSUB(cv) = NULL; 492 } 493 /* delete all flags except WEAKOUTSIDE and CVGV_RC, which indicate the 494 * ref status of CvOUTSIDE and CvGV, and ANON, which pp_entersub uses 495 * to choose an error message */ 496 CvFLAGS(cv) &= (CVf_WEAKOUTSIDE|CVf_CVGV_RC|CVf_ANON); 497 } 498 499 /* 500 =for apidoc cv_forget_slab 501 502 When a CV has a reference count on its slab (CvSLABBED), it is responsible 503 for making sure it is freed. (Hence, no two CVs should ever have a 504 reference count on the same slab.) The CV only needs to reference the slab 505 during compilation. Once it is compiled and CvROOT attached, it has 506 finished its job, so it can forget the slab. 507 508 =cut 509 */ 510 511 void 512 Perl_cv_forget_slab(pTHX_ CV *cv) 513 { 514 const bool slabbed = !!CvSLABBED(cv); 515 OPSLAB *slab = NULL; 516 517 PERL_ARGS_ASSERT_CV_FORGET_SLAB; 518 519 if (!slabbed) return; 520 521 CvSLABBED_off(cv); 522 523 if (CvROOT(cv)) slab = OpSLAB(CvROOT(cv)); 524 else if (CvSTART(cv)) slab = (OPSLAB *)CvSTART(cv); 525 #ifdef DEBUGGING 526 else if (slabbed) Perl_warn(aTHX_ "Slab leaked from cv %p", cv); 527 #endif 528 529 if (slab) { 530 #ifdef PERL_DEBUG_READONLY_OPS 531 const size_t refcnt = slab->opslab_refcnt; 532 #endif 533 OpslabREFCNT_dec(slab); 534 #ifdef PERL_DEBUG_READONLY_OPS 535 if (refcnt > 1) Slab_to_ro(slab); 536 #endif 537 } 538 } 539 540 /* 541 =for apidoc m|PADOFFSET|pad_alloc_name|SV *namesv|U32 flags|HV *typestash|HV *ourstash 542 543 Allocates a place in the currently-compiling 544 pad (via L<perlapi/pad_alloc>) and 545 then stores a name for that entry. I<namesv> is adopted and becomes the 546 name entry; it must already contain the name string and be sufficiently 547 upgraded. I<typestash> and I<ourstash> and the C<padadd_STATE> flag get 548 added to I<namesv>. None of the other 549 processing of L<perlapi/pad_add_name_pvn> 550 is done. Returns the offset of the allocated pad slot. 551 552 =cut 553 */ 554 555 static PADOFFSET 556 S_pad_alloc_name(pTHX_ SV *namesv, U32 flags, HV *typestash, HV *ourstash) 557 { 558 dVAR; 559 const PADOFFSET offset = pad_alloc(OP_PADSV, SVs_PADMY); 560 561 PERL_ARGS_ASSERT_PAD_ALLOC_NAME; 562 563 ASSERT_CURPAD_ACTIVE("pad_alloc_name"); 564 565 if (typestash) { 566 assert(SvTYPE(namesv) == SVt_PVMG); 567 SvPAD_TYPED_on(namesv); 568 SvSTASH_set(namesv, MUTABLE_HV(SvREFCNT_inc_simple_NN(MUTABLE_SV(typestash)))); 569 } 570 if (ourstash) { 571 SvPAD_OUR_on(namesv); 572 SvOURSTASH_set(namesv, ourstash); 573 SvREFCNT_inc_simple_void_NN(ourstash); 574 } 575 else if (flags & padadd_STATE) { 576 SvPAD_STATE_on(namesv); 577 } 578 579 av_store(PL_comppad_name, offset, namesv); 580 PadnamelistMAXNAMED(PL_comppad_name) = offset; 581 return offset; 582 } 583 584 /* 585 =for apidoc Am|PADOFFSET|pad_add_name_pvn|const char *namepv|STRLEN namelen|U32 flags|HV *typestash|HV *ourstash 586 587 Allocates a place in the currently-compiling pad for a named lexical 588 variable. Stores the name and other metadata in the name part of the 589 pad, and makes preparations to manage the variable's lexical scoping. 590 Returns the offset of the allocated pad slot. 591 592 I<namepv>/I<namelen> specify the variable's name, including leading sigil. 593 If I<typestash> is non-null, the name is for a typed lexical, and this 594 identifies the type. If I<ourstash> is non-null, it's a lexical reference 595 to a package variable, and this identifies the package. The following 596 flags can be OR'ed together: 597 598 padadd_OUR redundantly specifies if it's a package var 599 padadd_STATE variable will retain value persistently 600 padadd_NO_DUP_CHECK skip check for lexical shadowing 601 602 =cut 603 */ 604 605 PADOFFSET 606 Perl_pad_add_name_pvn(pTHX_ const char *namepv, STRLEN namelen, 607 U32 flags, HV *typestash, HV *ourstash) 608 { 609 dVAR; 610 PADOFFSET offset; 611 SV *namesv; 612 bool is_utf8; 613 614 PERL_ARGS_ASSERT_PAD_ADD_NAME_PVN; 615 616 if (flags & ~(padadd_OUR|padadd_STATE|padadd_NO_DUP_CHECK|padadd_UTF8_NAME)) 617 Perl_croak(aTHX_ "panic: pad_add_name_pvn illegal flag bits 0x%" UVxf, 618 (UV)flags); 619 620 namesv = newSV_type((ourstash || typestash) ? SVt_PVMG : SVt_PVNV); 621 622 if ((is_utf8 = ((flags & padadd_UTF8_NAME) != 0))) { 623 namepv = (const char*)bytes_from_utf8((U8*)namepv, &namelen, &is_utf8); 624 } 625 626 sv_setpvn(namesv, namepv, namelen); 627 628 if (is_utf8) { 629 flags |= padadd_UTF8_NAME; 630 SvUTF8_on(namesv); 631 } 632 else 633 flags &= ~padadd_UTF8_NAME; 634 635 if ((flags & padadd_NO_DUP_CHECK) == 0) { 636 ENTER; 637 SAVEFREESV(namesv); /* in case of fatal warnings */ 638 /* check for duplicate declaration */ 639 pad_check_dup(namesv, flags & padadd_OUR, ourstash); 640 SvREFCNT_inc_simple_void_NN(namesv); 641 LEAVE; 642 } 643 644 offset = pad_alloc_name(namesv, flags & ~padadd_UTF8_NAME, typestash, ourstash); 645 646 /* not yet introduced */ 647 COP_SEQ_RANGE_LOW_set(namesv, PERL_PADSEQ_INTRO); 648 COP_SEQ_RANGE_HIGH_set(namesv, 0); 649 650 if (!PL_min_intro_pending) 651 PL_min_intro_pending = offset; 652 PL_max_intro_pending = offset; 653 /* if it's not a simple scalar, replace with an AV or HV */ 654 assert(SvTYPE(PL_curpad[offset]) == SVt_NULL); 655 assert(SvREFCNT(PL_curpad[offset]) == 1); 656 if (namelen != 0 && *namepv == '@') 657 sv_upgrade(PL_curpad[offset], SVt_PVAV); 658 else if (namelen != 0 && *namepv == '%') 659 sv_upgrade(PL_curpad[offset], SVt_PVHV); 660 else if (namelen != 0 && *namepv == '&') 661 sv_upgrade(PL_curpad[offset], SVt_PVCV); 662 assert(SvPADMY(PL_curpad[offset])); 663 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 664 "Pad addname: %ld \"%s\" new lex=0x%"UVxf"\n", 665 (long)offset, SvPVX(namesv), 666 PTR2UV(PL_curpad[offset]))); 667 668 return offset; 669 } 670 671 /* 672 =for apidoc Am|PADOFFSET|pad_add_name_pv|const char *name|U32 flags|HV *typestash|HV *ourstash 673 674 Exactly like L</pad_add_name_pvn>, but takes a nul-terminated string 675 instead of a string/length pair. 676 677 =cut 678 */ 679 680 PADOFFSET 681 Perl_pad_add_name_pv(pTHX_ const char *name, 682 const U32 flags, HV *typestash, HV *ourstash) 683 { 684 PERL_ARGS_ASSERT_PAD_ADD_NAME_PV; 685 return pad_add_name_pvn(name, strlen(name), flags, typestash, ourstash); 686 } 687 688 /* 689 =for apidoc Am|PADOFFSET|pad_add_name_sv|SV *name|U32 flags|HV *typestash|HV *ourstash 690 691 Exactly like L</pad_add_name_pvn>, but takes the name string in the form 692 of an SV instead of a string/length pair. 693 694 =cut 695 */ 696 697 PADOFFSET 698 Perl_pad_add_name_sv(pTHX_ SV *name, U32 flags, HV *typestash, HV *ourstash) 699 { 700 char *namepv; 701 STRLEN namelen; 702 PERL_ARGS_ASSERT_PAD_ADD_NAME_SV; 703 namepv = SvPV(name, namelen); 704 if (SvUTF8(name)) 705 flags |= padadd_UTF8_NAME; 706 return pad_add_name_pvn(namepv, namelen, flags, typestash, ourstash); 707 } 708 709 /* 710 =for apidoc Amx|PADOFFSET|pad_alloc|I32 optype|U32 tmptype 711 712 Allocates a place in the currently-compiling pad, 713 returning the offset of the allocated pad slot. 714 No name is initially attached to the pad slot. 715 I<tmptype> is a set of flags indicating the kind of pad entry required, 716 which will be set in the value SV for the allocated pad entry: 717 718 SVs_PADMY named lexical variable ("my", "our", "state") 719 SVs_PADTMP unnamed temporary store 720 SVf_READONLY constant shared between recursion levels 721 722 C<SVf_READONLY> has been supported here only since perl 5.20. To work with 723 earlier versions as well, use C<SVf_READONLY|SVs_PADTMP>. C<SVf_READONLY> 724 does not cause the SV in the pad slot to be marked read-only, but simply 725 tells C<pad_alloc> that it I<will> be made read-only (by the caller), or at 726 least should be treated as such. 727 728 I<optype> should be an opcode indicating the type of operation that the 729 pad entry is to support. This doesn't affect operational semantics, 730 but is used for debugging. 731 732 =cut 733 */ 734 735 /* XXX DAPM integrate alloc(), add_name() and add_anon(), 736 * or at least rationalise ??? */ 737 738 PADOFFSET 739 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype) 740 { 741 dVAR; 742 SV *sv; 743 I32 retval; 744 745 PERL_UNUSED_ARG(optype); 746 ASSERT_CURPAD_ACTIVE("pad_alloc"); 747 748 if (AvARRAY(PL_comppad) != PL_curpad) 749 Perl_croak(aTHX_ "panic: pad_alloc, %p!=%p", 750 AvARRAY(PL_comppad), PL_curpad); 751 if (PL_pad_reset_pending) 752 pad_reset(); 753 if (tmptype & SVs_PADMY) { 754 /* For a my, simply push a null SV onto the end of PL_comppad. */ 755 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE); 756 retval = AvFILLp(PL_comppad); 757 } 758 else { 759 /* For a tmp, scan the pad from PL_padix upwards 760 * for a slot which has no name and no active value. 761 */ 762 SV * const * const names = AvARRAY(PL_comppad_name); 763 const SSize_t names_fill = AvFILLp(PL_comppad_name); 764 for (;;) { 765 /* 766 * Entries that close over unavailable variables 767 * in outer subs contain values not marked PADMY. 768 * Thus we must skip, not just pad values that are 769 * marked as current pad values, but also those with names. 770 */ 771 if (++PL_padix <= names_fill && 772 (sv = names[PL_padix]) && sv != &PL_sv_undef) 773 continue; 774 sv = *av_fetch(PL_comppad, PL_padix, TRUE); 775 if (!(SvFLAGS(sv) & (SVs_PADTMP | SVs_PADMY)) && 776 !IS_PADGV(sv)) 777 break; 778 } 779 if (tmptype & SVf_READONLY) { 780 av_store(PL_comppad_name, PL_padix, &PL_sv_no); 781 tmptype &= ~SVf_READONLY; 782 tmptype |= SVs_PADTMP; 783 } 784 retval = PL_padix; 785 } 786 SvFLAGS(sv) |= tmptype; 787 PL_curpad = AvARRAY(PL_comppad); 788 789 DEBUG_X(PerlIO_printf(Perl_debug_log, 790 "Pad 0x%"UVxf"[0x%"UVxf"] alloc: %ld for %s\n", 791 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long) retval, 792 PL_op_name[optype])); 793 #ifdef DEBUG_LEAKING_SCALARS 794 sv->sv_debug_optype = optype; 795 sv->sv_debug_inpad = 1; 796 #endif 797 return (PADOFFSET)retval; 798 } 799 800 /* 801 =for apidoc Am|PADOFFSET|pad_add_anon|CV *func|I32 optype 802 803 Allocates a place in the currently-compiling pad (via L</pad_alloc>) 804 for an anonymous function that is lexically scoped inside the 805 currently-compiling function. 806 The function I<func> is linked into the pad, and its C<CvOUTSIDE> link 807 to the outer scope is weakened to avoid a reference loop. 808 809 One reference count is stolen, so you may need to do C<SvREFCNT_inc(func)>. 810 811 I<optype> should be an opcode indicating the type of operation that the 812 pad entry is to support. This doesn't affect operational semantics, 813 but is used for debugging. 814 815 =cut 816 */ 817 818 PADOFFSET 819 Perl_pad_add_anon(pTHX_ CV* func, I32 optype) 820 { 821 dVAR; 822 PADOFFSET ix; 823 SV* const name = newSV_type(SVt_PVNV); 824 825 PERL_ARGS_ASSERT_PAD_ADD_ANON; 826 827 pad_peg("add_anon"); 828 sv_setpvs(name, "&"); 829 /* These two aren't used; just make sure they're not equal to 830 * PERL_PADSEQ_INTRO */ 831 COP_SEQ_RANGE_LOW_set(name, 0); 832 COP_SEQ_RANGE_HIGH_set(name, 0); 833 ix = pad_alloc(optype, SVs_PADMY); 834 av_store(PL_comppad_name, ix, name); 835 /* XXX DAPM use PL_curpad[] ? */ 836 if (SvTYPE(func) == SVt_PVCV || !CvOUTSIDE(func)) 837 av_store(PL_comppad, ix, (SV*)func); 838 else { 839 SV *rv = newRV_noinc((SV *)func); 840 sv_rvweaken(rv); 841 assert (SvTYPE(func) == SVt_PVFM); 842 av_store(PL_comppad, ix, rv); 843 } 844 SvPADMY_on((SV*)func); 845 846 /* to avoid ref loops, we never have parent + child referencing each 847 * other simultaneously */ 848 if (CvOUTSIDE(func) && SvTYPE(func) == SVt_PVCV) { 849 assert(!CvWEAKOUTSIDE(func)); 850 CvWEAKOUTSIDE_on(func); 851 SvREFCNT_dec_NN(CvOUTSIDE(func)); 852 } 853 return ix; 854 } 855 856 /* 857 =for apidoc pad_check_dup 858 859 Check for duplicate declarations: report any of: 860 861 * a my in the current scope with the same name; 862 * an our (anywhere in the pad) with the same name and the 863 same stash as C<ourstash> 864 865 C<is_our> indicates that the name to check is an 'our' declaration. 866 867 =cut 868 */ 869 870 STATIC void 871 S_pad_check_dup(pTHX_ SV *name, U32 flags, const HV *ourstash) 872 { 873 dVAR; 874 SV **svp; 875 PADOFFSET top, off; 876 const U32 is_our = flags & padadd_OUR; 877 878 PERL_ARGS_ASSERT_PAD_CHECK_DUP; 879 880 ASSERT_CURPAD_ACTIVE("pad_check_dup"); 881 882 assert((flags & ~padadd_OUR) == 0); 883 884 if (AvFILLp(PL_comppad_name) < 0 || !ckWARN(WARN_MISC)) 885 return; /* nothing to check */ 886 887 svp = AvARRAY(PL_comppad_name); 888 top = AvFILLp(PL_comppad_name); 889 /* check the current scope */ 890 /* XXX DAPM - why the (I32) cast - shouldn't we ensure they're the same 891 * type ? */ 892 for (off = top; (I32)off > PL_comppad_name_floor; off--) { 893 SV * const sv = svp[off]; 894 if (sv 895 && PadnameLEN(sv) 896 && !SvFAKE(sv) 897 && ( COP_SEQ_RANGE_LOW(sv) == PERL_PADSEQ_INTRO 898 || COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO) 899 && sv_eq(name, sv)) 900 { 901 if (is_our && (SvPAD_OUR(sv))) 902 break; /* "our" masking "our" */ 903 /* diag_listed_as: "%s" variable %s masks earlier declaration in same %s */ 904 Perl_warner(aTHX_ packWARN(WARN_MISC), 905 "\"%s\" %s %"SVf" masks earlier declaration in same %s", 906 (is_our ? "our" : PL_parser->in_my == KEY_my ? "my" : "state"), 907 *SvPVX(sv) == '&' ? "subroutine" : "variable", 908 sv, 909 (COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO 910 ? "scope" : "statement")); 911 --off; 912 break; 913 } 914 } 915 /* check the rest of the pad */ 916 if (is_our) { 917 while (off > 0) { 918 SV * const sv = svp[off]; 919 if (sv 920 && PadnameLEN(sv) 921 && !SvFAKE(sv) 922 && ( COP_SEQ_RANGE_LOW(sv) == PERL_PADSEQ_INTRO 923 || COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO) 924 && SvOURSTASH(sv) == ourstash 925 && sv_eq(name, sv)) 926 { 927 Perl_warner(aTHX_ packWARN(WARN_MISC), 928 "\"our\" variable %"SVf" redeclared", sv); 929 if ((I32)off <= PL_comppad_name_floor) 930 Perl_warner(aTHX_ packWARN(WARN_MISC), 931 "\t(Did you mean \"local\" instead of \"our\"?)\n"); 932 break; 933 } 934 --off; 935 } 936 } 937 } 938 939 940 /* 941 =for apidoc Am|PADOFFSET|pad_findmy_pvn|const char *namepv|STRLEN namelen|U32 flags 942 943 Given the name of a lexical variable, find its position in the 944 currently-compiling pad. 945 I<namepv>/I<namelen> specify the variable's name, including leading sigil. 946 I<flags> is reserved and must be zero. 947 If it is not in the current pad but appears in the pad of any lexically 948 enclosing scope, then a pseudo-entry for it is added in the current pad. 949 Returns the offset in the current pad, 950 or C<NOT_IN_PAD> if no such lexical is in scope. 951 952 =cut 953 */ 954 955 PADOFFSET 956 Perl_pad_findmy_pvn(pTHX_ const char *namepv, STRLEN namelen, U32 flags) 957 { 958 dVAR; 959 SV *out_sv; 960 int out_flags; 961 I32 offset; 962 const AV *nameav; 963 SV **name_svp; 964 965 PERL_ARGS_ASSERT_PAD_FINDMY_PVN; 966 967 pad_peg("pad_findmy_pvn"); 968 969 if (flags & ~padadd_UTF8_NAME) 970 Perl_croak(aTHX_ "panic: pad_findmy_pvn illegal flag bits 0x%" UVxf, 971 (UV)flags); 972 973 if (flags & padadd_UTF8_NAME) { 974 bool is_utf8 = TRUE; 975 namepv = (const char*)bytes_from_utf8((U8*)namepv, &namelen, &is_utf8); 976 977 if (is_utf8) 978 flags |= padadd_UTF8_NAME; 979 else { 980 flags &= ~padadd_UTF8_NAME; 981 SAVEFREEPV(namepv); 982 } 983 } 984 985 offset = pad_findlex(namepv, namelen, flags, 986 PL_compcv, PL_cop_seqmax, 1, NULL, &out_sv, &out_flags); 987 if ((PADOFFSET)offset != NOT_IN_PAD) 988 return offset; 989 990 /* look for an our that's being introduced; this allows 991 * our $foo = 0 unless defined $foo; 992 * to not give a warning. (Yes, this is a hack) */ 993 994 nameav = PadlistARRAY(CvPADLIST(PL_compcv))[0]; 995 name_svp = AvARRAY(nameav); 996 for (offset = AvFILLp(nameav); offset > 0; offset--) { 997 const SV * const namesv = name_svp[offset]; 998 if (namesv && PadnameLEN(namesv) == namelen 999 && !SvFAKE(namesv) 1000 && (SvPAD_OUR(namesv)) 1001 && sv_eq_pvn_flags(aTHX_ namesv, namepv, namelen, 1002 flags & padadd_UTF8_NAME ? SVf_UTF8 : 0 ) 1003 && COP_SEQ_RANGE_LOW(namesv) == PERL_PADSEQ_INTRO 1004 ) 1005 return offset; 1006 } 1007 return NOT_IN_PAD; 1008 } 1009 1010 /* 1011 =for apidoc Am|PADOFFSET|pad_findmy_pv|const char *name|U32 flags 1012 1013 Exactly like L</pad_findmy_pvn>, but takes a nul-terminated string 1014 instead of a string/length pair. 1015 1016 =cut 1017 */ 1018 1019 PADOFFSET 1020 Perl_pad_findmy_pv(pTHX_ const char *name, U32 flags) 1021 { 1022 PERL_ARGS_ASSERT_PAD_FINDMY_PV; 1023 return pad_findmy_pvn(name, strlen(name), flags); 1024 } 1025 1026 /* 1027 =for apidoc Am|PADOFFSET|pad_findmy_sv|SV *name|U32 flags 1028 1029 Exactly like L</pad_findmy_pvn>, but takes the name string in the form 1030 of an SV instead of a string/length pair. 1031 1032 =cut 1033 */ 1034 1035 PADOFFSET 1036 Perl_pad_findmy_sv(pTHX_ SV *name, U32 flags) 1037 { 1038 char *namepv; 1039 STRLEN namelen; 1040 PERL_ARGS_ASSERT_PAD_FINDMY_SV; 1041 namepv = SvPV(name, namelen); 1042 if (SvUTF8(name)) 1043 flags |= padadd_UTF8_NAME; 1044 return pad_findmy_pvn(namepv, namelen, flags); 1045 } 1046 1047 /* 1048 =for apidoc Amp|PADOFFSET|find_rundefsvoffset 1049 1050 Find the position of the lexical C<$_> in the pad of the 1051 currently-executing function. Returns the offset in the current pad, 1052 or C<NOT_IN_PAD> if there is no lexical C<$_> in scope (in which case 1053 the global one should be used instead). 1054 L</find_rundefsv> is likely to be more convenient. 1055 1056 =cut 1057 */ 1058 1059 PADOFFSET 1060 Perl_find_rundefsvoffset(pTHX) 1061 { 1062 dVAR; 1063 SV *out_sv; 1064 int out_flags; 1065 return pad_findlex("$_", 2, 0, find_runcv(NULL), PL_curcop->cop_seq, 1, 1066 NULL, &out_sv, &out_flags); 1067 } 1068 1069 /* 1070 =for apidoc Am|SV *|find_rundefsv 1071 1072 Find and return the variable that is named C<$_> in the lexical scope 1073 of the currently-executing function. This may be a lexical C<$_>, 1074 or will otherwise be the global one. 1075 1076 =cut 1077 */ 1078 1079 SV * 1080 Perl_find_rundefsv(pTHX) 1081 { 1082 SV *namesv; 1083 int flags; 1084 PADOFFSET po; 1085 1086 po = pad_findlex("$_", 2, 0, find_runcv(NULL), PL_curcop->cop_seq, 1, 1087 NULL, &namesv, &flags); 1088 1089 if (po == NOT_IN_PAD || SvPAD_OUR(namesv)) 1090 return DEFSV; 1091 1092 return PAD_SVl(po); 1093 } 1094 1095 SV * 1096 Perl_find_rundefsv2(pTHX_ CV *cv, U32 seq) 1097 { 1098 SV *namesv; 1099 int flags; 1100 PADOFFSET po; 1101 1102 PERL_ARGS_ASSERT_FIND_RUNDEFSV2; 1103 1104 po = pad_findlex("$_", 2, 0, cv, seq, 1, 1105 NULL, &namesv, &flags); 1106 1107 if (po == NOT_IN_PAD || SvPAD_OUR(namesv)) 1108 return DEFSV; 1109 1110 return AvARRAY(PadlistARRAY(CvPADLIST(cv))[CvDEPTH(cv)])[po]; 1111 } 1112 1113 /* 1114 =for apidoc m|PADOFFSET|pad_findlex|const char *namepv|STRLEN namelen|U32 flags|const CV* cv|U32 seq|int warn|SV** out_capture|SV** out_name_sv|int *out_flags 1115 1116 Find a named lexical anywhere in a chain of nested pads. Add fake entries 1117 in the inner pads if it's found in an outer one. 1118 1119 Returns the offset in the bottom pad of the lex or the fake lex. 1120 cv is the CV in which to start the search, and seq is the current cop_seq 1121 to match against. If warn is true, print appropriate warnings. The out_* 1122 vars return values, and so are pointers to where the returned values 1123 should be stored. out_capture, if non-null, requests that the innermost 1124 instance of the lexical is captured; out_name_sv is set to the innermost 1125 matched namesv or fake namesv; out_flags returns the flags normally 1126 associated with the IVX field of a fake namesv. 1127 1128 Note that pad_findlex() is recursive; it recurses up the chain of CVs, 1129 then comes back down, adding fake entries 1130 as it goes. It has to be this way 1131 because fake namesvs in anon protoypes have to store in xlow the index into 1132 the parent pad. 1133 1134 =cut 1135 */ 1136 1137 /* the CV has finished being compiled. This is not a sufficient test for 1138 * all CVs (eg XSUBs), but suffices for the CVs found in a lexical chain */ 1139 #define CvCOMPILED(cv) CvROOT(cv) 1140 1141 /* the CV does late binding of its lexicals */ 1142 #define CvLATE(cv) (CvANON(cv) || CvCLONE(cv) || SvTYPE(cv) == SVt_PVFM) 1143 1144 static void 1145 S_unavailable(pTHX_ SV *namesv) 1146 { 1147 /* diag_listed_as: Variable "%s" is not available */ 1148 Perl_ck_warner(aTHX_ packWARN(WARN_CLOSURE), 1149 "%se \"%"SVf"\" is not available", 1150 *SvPVX_const(namesv) == '&' 1151 ? "Subroutin" 1152 : "Variabl", 1153 namesv); 1154 } 1155 1156 STATIC PADOFFSET 1157 S_pad_findlex(pTHX_ const char *namepv, STRLEN namelen, U32 flags, const CV* cv, U32 seq, 1158 int warn, SV** out_capture, SV** out_name_sv, int *out_flags) 1159 { 1160 dVAR; 1161 I32 offset, new_offset; 1162 SV *new_capture; 1163 SV **new_capturep; 1164 const PADLIST * const padlist = CvPADLIST(cv); 1165 const bool staleok = !!(flags & padadd_STALEOK); 1166 1167 PERL_ARGS_ASSERT_PAD_FINDLEX; 1168 1169 if (flags & ~(padadd_UTF8_NAME|padadd_STALEOK)) 1170 Perl_croak(aTHX_ "panic: pad_findlex illegal flag bits 0x%" UVxf, 1171 (UV)flags); 1172 flags &= ~ padadd_STALEOK; /* one-shot flag */ 1173 1174 *out_flags = 0; 1175 1176 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1177 "Pad findlex cv=0x%"UVxf" searching \"%.*s\" seq=%d%s\n", 1178 PTR2UV(cv), (int)namelen, namepv, (int)seq, 1179 out_capture ? " capturing" : "" )); 1180 1181 /* first, search this pad */ 1182 1183 if (padlist) { /* not an undef CV */ 1184 I32 fake_offset = 0; 1185 const AV * const nameav = PadlistARRAY(padlist)[0]; 1186 SV * const * const name_svp = AvARRAY(nameav); 1187 1188 for (offset = PadnamelistMAXNAMED(nameav); offset > 0; offset--) { 1189 const SV * const namesv = name_svp[offset]; 1190 if (namesv && PadnameLEN(namesv) == namelen 1191 && sv_eq_pvn_flags(aTHX_ namesv, namepv, namelen, 1192 flags & padadd_UTF8_NAME ? SVf_UTF8 : 0)) 1193 { 1194 if (SvFAKE(namesv)) { 1195 fake_offset = offset; /* in case we don't find a real one */ 1196 continue; 1197 } 1198 /* is seq within the range _LOW to _HIGH ? 1199 * This is complicated by the fact that PL_cop_seqmax 1200 * may have wrapped around at some point */ 1201 if (COP_SEQ_RANGE_LOW(namesv) == PERL_PADSEQ_INTRO) 1202 continue; /* not yet introduced */ 1203 1204 if (COP_SEQ_RANGE_HIGH(namesv) == PERL_PADSEQ_INTRO) { 1205 /* in compiling scope */ 1206 if ( 1207 (seq > COP_SEQ_RANGE_LOW(namesv)) 1208 ? (seq - COP_SEQ_RANGE_LOW(namesv) < (U32_MAX >> 1)) 1209 : (COP_SEQ_RANGE_LOW(namesv) - seq > (U32_MAX >> 1)) 1210 ) 1211 break; 1212 } 1213 else if ( 1214 (COP_SEQ_RANGE_LOW(namesv) > COP_SEQ_RANGE_HIGH(namesv)) 1215 ? 1216 ( seq > COP_SEQ_RANGE_LOW(namesv) 1217 || seq <= COP_SEQ_RANGE_HIGH(namesv)) 1218 1219 : ( seq > COP_SEQ_RANGE_LOW(namesv) 1220 && seq <= COP_SEQ_RANGE_HIGH(namesv)) 1221 ) 1222 break; 1223 } 1224 } 1225 1226 if (offset > 0 || fake_offset > 0 ) { /* a match! */ 1227 if (offset > 0) { /* not fake */ 1228 fake_offset = 0; 1229 *out_name_sv = name_svp[offset]; /* return the namesv */ 1230 1231 /* set PAD_FAKELEX_MULTI if this lex can have multiple 1232 * instances. For now, we just test !CvUNIQUE(cv), but 1233 * ideally, we should detect my's declared within loops 1234 * etc - this would allow a wider range of 'not stayed 1235 * shared' warnings. We also treated already-compiled 1236 * lexes as not multi as viewed from evals. */ 1237 1238 *out_flags = CvANON(cv) ? 1239 PAD_FAKELEX_ANON : 1240 (!CvUNIQUE(cv) && ! CvCOMPILED(cv)) 1241 ? PAD_FAKELEX_MULTI : 0; 1242 1243 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1244 "Pad findlex cv=0x%"UVxf" matched: offset=%ld (%lu,%lu)\n", 1245 PTR2UV(cv), (long)offset, 1246 (unsigned long)COP_SEQ_RANGE_LOW(*out_name_sv), 1247 (unsigned long)COP_SEQ_RANGE_HIGH(*out_name_sv))); 1248 } 1249 else { /* fake match */ 1250 offset = fake_offset; 1251 *out_name_sv = name_svp[offset]; /* return the namesv */ 1252 *out_flags = PARENT_FAKELEX_FLAGS(*out_name_sv); 1253 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1254 "Pad findlex cv=0x%"UVxf" matched: offset=%ld flags=0x%lx index=%lu\n", 1255 PTR2UV(cv), (long)offset, (unsigned long)*out_flags, 1256 (unsigned long) PARENT_PAD_INDEX(*out_name_sv) 1257 )); 1258 } 1259 1260 /* return the lex? */ 1261 1262 if (out_capture) { 1263 1264 /* our ? */ 1265 if (SvPAD_OUR(*out_name_sv)) { 1266 *out_capture = NULL; 1267 return offset; 1268 } 1269 1270 /* trying to capture from an anon prototype? */ 1271 if (CvCOMPILED(cv) 1272 ? CvANON(cv) && CvCLONE(cv) && !CvCLONED(cv) 1273 : *out_flags & PAD_FAKELEX_ANON) 1274 { 1275 if (warn) 1276 S_unavailable(aTHX_ 1277 newSVpvn_flags(namepv, namelen, 1278 SVs_TEMP | 1279 (flags & padadd_UTF8_NAME ? SVf_UTF8 : 0))); 1280 1281 *out_capture = NULL; 1282 } 1283 1284 /* real value */ 1285 else { 1286 int newwarn = warn; 1287 if (!CvCOMPILED(cv) && (*out_flags & PAD_FAKELEX_MULTI) 1288 && !SvPAD_STATE(name_svp[offset]) 1289 && warn && ckWARN(WARN_CLOSURE)) { 1290 newwarn = 0; 1291 Perl_warner(aTHX_ packWARN(WARN_CLOSURE), 1292 "Variable \"%"SVf"\" will not stay shared", 1293 newSVpvn_flags(namepv, namelen, 1294 SVs_TEMP | 1295 (flags & padadd_UTF8_NAME ? SVf_UTF8 : 0))); 1296 } 1297 1298 if (fake_offset && CvANON(cv) 1299 && CvCLONE(cv) &&!CvCLONED(cv)) 1300 { 1301 SV *n; 1302 /* not yet caught - look further up */ 1303 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1304 "Pad findlex cv=0x%"UVxf" chasing lex in outer pad\n", 1305 PTR2UV(cv))); 1306 n = *out_name_sv; 1307 (void) pad_findlex(namepv, namelen, flags, CvOUTSIDE(cv), 1308 CvOUTSIDE_SEQ(cv), 1309 newwarn, out_capture, out_name_sv, out_flags); 1310 *out_name_sv = n; 1311 return offset; 1312 } 1313 1314 *out_capture = AvARRAY(PadlistARRAY(padlist)[ 1315 CvDEPTH(cv) ? CvDEPTH(cv) : 1])[offset]; 1316 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1317 "Pad findlex cv=0x%"UVxf" found lex=0x%"UVxf"\n", 1318 PTR2UV(cv), PTR2UV(*out_capture))); 1319 1320 if (SvPADSTALE(*out_capture) 1321 && (!CvDEPTH(cv) || !staleok) 1322 && !SvPAD_STATE(name_svp[offset])) 1323 { 1324 S_unavailable(aTHX_ 1325 newSVpvn_flags(namepv, namelen, 1326 SVs_TEMP | 1327 (flags & padadd_UTF8_NAME ? SVf_UTF8 : 0))); 1328 *out_capture = NULL; 1329 } 1330 } 1331 if (!*out_capture) { 1332 if (namelen != 0 && *namepv == '@') 1333 *out_capture = sv_2mortal(MUTABLE_SV(newAV())); 1334 else if (namelen != 0 && *namepv == '%') 1335 *out_capture = sv_2mortal(MUTABLE_SV(newHV())); 1336 else if (namelen != 0 && *namepv == '&') 1337 *out_capture = sv_2mortal(newSV_type(SVt_PVCV)); 1338 else 1339 *out_capture = sv_newmortal(); 1340 } 1341 } 1342 1343 return offset; 1344 } 1345 } 1346 1347 /* it's not in this pad - try above */ 1348 1349 if (!CvOUTSIDE(cv)) 1350 return NOT_IN_PAD; 1351 1352 /* out_capture non-null means caller wants us to capture lex; in 1353 * addition we capture ourselves unless it's an ANON/format */ 1354 new_capturep = out_capture ? out_capture : 1355 CvLATE(cv) ? NULL : &new_capture; 1356 1357 offset = pad_findlex(namepv, namelen, 1358 flags | padadd_STALEOK*(new_capturep == &new_capture), 1359 CvOUTSIDE(cv), CvOUTSIDE_SEQ(cv), 1, 1360 new_capturep, out_name_sv, out_flags); 1361 if ((PADOFFSET)offset == NOT_IN_PAD) 1362 return NOT_IN_PAD; 1363 1364 /* found in an outer CV. Add appropriate fake entry to this pad */ 1365 1366 /* don't add new fake entries (via eval) to CVs that we have already 1367 * finished compiling, or to undef CVs */ 1368 if (CvCOMPILED(cv) || !padlist) 1369 return 0; /* this dummy (and invalid) value isnt used by the caller */ 1370 1371 { 1372 /* This relies on sv_setsv_flags() upgrading the destination to the same 1373 type as the source, independent of the flags set, and on it being 1374 "good" and only copying flag bits and pointers that it understands. 1375 */ 1376 SV *new_namesv = newSVsv(*out_name_sv); 1377 AV * const ocomppad_name = PL_comppad_name; 1378 PAD * const ocomppad = PL_comppad; 1379 PL_comppad_name = PadlistARRAY(padlist)[0]; 1380 PL_comppad = PadlistARRAY(padlist)[1]; 1381 PL_curpad = AvARRAY(PL_comppad); 1382 1383 new_offset 1384 = pad_alloc_name(new_namesv, 1385 (SvPAD_STATE(*out_name_sv) ? padadd_STATE : 0), 1386 SvPAD_TYPED(*out_name_sv) 1387 ? SvSTASH(*out_name_sv) : NULL, 1388 SvOURSTASH(*out_name_sv) 1389 ); 1390 1391 SvFAKE_on(new_namesv); 1392 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1393 "Pad addname: %ld \"%.*s\" FAKE\n", 1394 (long)new_offset, 1395 (int) SvCUR(new_namesv), SvPVX(new_namesv))); 1396 PARENT_FAKELEX_FLAGS_set(new_namesv, *out_flags); 1397 1398 PARENT_PAD_INDEX_set(new_namesv, 0); 1399 if (SvPAD_OUR(new_namesv)) { 1400 NOOP; /* do nothing */ 1401 } 1402 else if (CvLATE(cv)) { 1403 /* delayed creation - just note the offset within parent pad */ 1404 PARENT_PAD_INDEX_set(new_namesv, offset); 1405 CvCLONE_on(cv); 1406 } 1407 else { 1408 /* immediate creation - capture outer value right now */ 1409 av_store(PL_comppad, new_offset, SvREFCNT_inc(*new_capturep)); 1410 /* But also note the offset, as newMYSUB needs it */ 1411 PARENT_PAD_INDEX_set(new_namesv, offset); 1412 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1413 "Pad findlex cv=0x%"UVxf" saved captured sv 0x%"UVxf" at offset %ld\n", 1414 PTR2UV(cv), PTR2UV(*new_capturep), (long)new_offset)); 1415 } 1416 *out_name_sv = new_namesv; 1417 *out_flags = PARENT_FAKELEX_FLAGS(new_namesv); 1418 1419 PL_comppad_name = ocomppad_name; 1420 PL_comppad = ocomppad; 1421 PL_curpad = ocomppad ? AvARRAY(ocomppad) : NULL; 1422 } 1423 return new_offset; 1424 } 1425 1426 #ifdef DEBUGGING 1427 1428 /* 1429 =for apidoc Am|SV *|pad_sv|PADOFFSET po 1430 1431 Get the value at offset I<po> in the current (compiling or executing) pad. 1432 Use macro PAD_SV instead of calling this function directly. 1433 1434 =cut 1435 */ 1436 1437 SV * 1438 Perl_pad_sv(pTHX_ PADOFFSET po) 1439 { 1440 dVAR; 1441 ASSERT_CURPAD_ACTIVE("pad_sv"); 1442 1443 if (!po) 1444 Perl_croak(aTHX_ "panic: pad_sv po"); 1445 DEBUG_X(PerlIO_printf(Perl_debug_log, 1446 "Pad 0x%"UVxf"[0x%"UVxf"] sv: %ld sv=0x%"UVxf"\n", 1447 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(PL_curpad[po])) 1448 ); 1449 return PL_curpad[po]; 1450 } 1451 1452 /* 1453 =for apidoc Am|void|pad_setsv|PADOFFSET po|SV *sv 1454 1455 Set the value at offset I<po> in the current (compiling or executing) pad. 1456 Use the macro PAD_SETSV() rather than calling this function directly. 1457 1458 =cut 1459 */ 1460 1461 void 1462 Perl_pad_setsv(pTHX_ PADOFFSET po, SV* sv) 1463 { 1464 dVAR; 1465 1466 PERL_ARGS_ASSERT_PAD_SETSV; 1467 1468 ASSERT_CURPAD_ACTIVE("pad_setsv"); 1469 1470 DEBUG_X(PerlIO_printf(Perl_debug_log, 1471 "Pad 0x%"UVxf"[0x%"UVxf"] setsv: %ld sv=0x%"UVxf"\n", 1472 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(sv)) 1473 ); 1474 PL_curpad[po] = sv; 1475 } 1476 1477 #endif /* DEBUGGING */ 1478 1479 /* 1480 =for apidoc m|void|pad_block_start|int full 1481 1482 Update the pad compilation state variables on entry to a new block. 1483 1484 =cut 1485 */ 1486 1487 /* XXX DAPM perhaps: 1488 * - integrate this in general state-saving routine ??? 1489 * - combine with the state-saving going on in pad_new ??? 1490 * - introduce a new SAVE type that does all this in one go ? 1491 */ 1492 1493 void 1494 Perl_pad_block_start(pTHX_ int full) 1495 { 1496 dVAR; 1497 ASSERT_CURPAD_ACTIVE("pad_block_start"); 1498 SAVEI32(PL_comppad_name_floor); 1499 PL_comppad_name_floor = AvFILLp(PL_comppad_name); 1500 if (full) 1501 PL_comppad_name_fill = PL_comppad_name_floor; 1502 if (PL_comppad_name_floor < 0) 1503 PL_comppad_name_floor = 0; 1504 SAVEI32(PL_min_intro_pending); 1505 SAVEI32(PL_max_intro_pending); 1506 PL_min_intro_pending = 0; 1507 SAVEI32(PL_comppad_name_fill); 1508 SAVEI32(PL_padix_floor); 1509 PL_padix_floor = PL_padix; 1510 PL_pad_reset_pending = FALSE; 1511 } 1512 1513 /* 1514 =for apidoc m|U32|intro_my 1515 1516 "Introduce" my variables to visible status. This is called during parsing 1517 at the end of each statement to make lexical variables visible to 1518 subsequent statements. 1519 1520 =cut 1521 */ 1522 1523 U32 1524 Perl_intro_my(pTHX) 1525 { 1526 dVAR; 1527 SV **svp; 1528 I32 i; 1529 U32 seq; 1530 1531 ASSERT_CURPAD_ACTIVE("intro_my"); 1532 if (! PL_min_intro_pending) 1533 return PL_cop_seqmax; 1534 1535 svp = AvARRAY(PL_comppad_name); 1536 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) { 1537 SV * const sv = svp[i]; 1538 1539 if (sv && PadnameLEN(sv) && !SvFAKE(sv) 1540 && COP_SEQ_RANGE_LOW(sv) == PERL_PADSEQ_INTRO) 1541 { 1542 COP_SEQ_RANGE_HIGH_set(sv, PERL_PADSEQ_INTRO); /* Don't know scope end yet. */ 1543 COP_SEQ_RANGE_LOW_set(sv, PL_cop_seqmax); 1544 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1545 "Pad intromy: %ld \"%s\", (%lu,%lu)\n", 1546 (long)i, SvPVX_const(sv), 1547 (unsigned long)COP_SEQ_RANGE_LOW(sv), 1548 (unsigned long)COP_SEQ_RANGE_HIGH(sv)) 1549 ); 1550 } 1551 } 1552 seq = PL_cop_seqmax; 1553 PL_cop_seqmax++; 1554 if (PL_cop_seqmax == PERL_PADSEQ_INTRO) /* not a legal value */ 1555 PL_cop_seqmax++; 1556 PL_min_intro_pending = 0; 1557 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */ 1558 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1559 "Pad intromy: seq -> %ld\n", (long)(PL_cop_seqmax))); 1560 1561 return seq; 1562 } 1563 1564 /* 1565 =for apidoc m|void|pad_leavemy 1566 1567 Cleanup at end of scope during compilation: set the max seq number for 1568 lexicals in this scope and warn of any lexicals that never got introduced. 1569 1570 =cut 1571 */ 1572 1573 OP * 1574 Perl_pad_leavemy(pTHX) 1575 { 1576 dVAR; 1577 I32 off; 1578 OP *o = NULL; 1579 SV * const * const svp = AvARRAY(PL_comppad_name); 1580 1581 PL_pad_reset_pending = FALSE; 1582 1583 ASSERT_CURPAD_ACTIVE("pad_leavemy"); 1584 if (PL_min_intro_pending && PL_comppad_name_fill < PL_min_intro_pending) { 1585 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) { 1586 const SV * const sv = svp[off]; 1587 if (sv && PadnameLEN(sv) && !SvFAKE(sv)) 1588 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), 1589 "%"SVf" never introduced", 1590 SVfARG(sv)); 1591 } 1592 } 1593 /* "Deintroduce" my variables that are leaving with this scope. */ 1594 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_fill; off--) { 1595 SV * const sv = svp[off]; 1596 if (sv && PadnameLEN(sv) && !SvFAKE(sv) 1597 && COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO) 1598 { 1599 COP_SEQ_RANGE_HIGH_set(sv, PL_cop_seqmax); 1600 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1601 "Pad leavemy: %ld \"%s\", (%lu,%lu)\n", 1602 (long)off, SvPVX_const(sv), 1603 (unsigned long)COP_SEQ_RANGE_LOW(sv), 1604 (unsigned long)COP_SEQ_RANGE_HIGH(sv)) 1605 ); 1606 if (!PadnameIsSTATE(sv) && !PadnameIsOUR(sv) 1607 && *PadnamePV(sv) == '&' && PadnameLEN(sv) > 1) { 1608 OP *kid = newOP(OP_INTROCV, 0); 1609 kid->op_targ = off; 1610 o = op_prepend_elem(OP_LINESEQ, kid, o); 1611 } 1612 } 1613 } 1614 PL_cop_seqmax++; 1615 if (PL_cop_seqmax == PERL_PADSEQ_INTRO) /* not a legal value */ 1616 PL_cop_seqmax++; 1617 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1618 "Pad leavemy: seq = %ld\n", (long)PL_cop_seqmax)); 1619 return o; 1620 } 1621 1622 /* 1623 =for apidoc m|void|pad_swipe|PADOFFSET po|bool refadjust 1624 1625 Abandon the tmp in the current pad at offset po and replace with a 1626 new one. 1627 1628 =cut 1629 */ 1630 1631 void 1632 Perl_pad_swipe(pTHX_ PADOFFSET po, bool refadjust) 1633 { 1634 dVAR; 1635 ASSERT_CURPAD_LEGAL("pad_swipe"); 1636 if (!PL_curpad) 1637 return; 1638 if (AvARRAY(PL_comppad) != PL_curpad) 1639 Perl_croak(aTHX_ "panic: pad_swipe curpad, %p!=%p", 1640 AvARRAY(PL_comppad), PL_curpad); 1641 if (!po || ((SSize_t)po) > AvFILLp(PL_comppad)) 1642 Perl_croak(aTHX_ "panic: pad_swipe po=%ld, fill=%ld", 1643 (long)po, (long)AvFILLp(PL_comppad)); 1644 1645 DEBUG_X(PerlIO_printf(Perl_debug_log, 1646 "Pad 0x%"UVxf"[0x%"UVxf"] swipe: %ld\n", 1647 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po)); 1648 1649 if (refadjust) 1650 SvREFCNT_dec(PL_curpad[po]); 1651 1652 1653 /* if pad tmps aren't shared between ops, then there's no need to 1654 * create a new tmp when an existing op is freed */ 1655 #ifdef USE_BROKEN_PAD_RESET 1656 PL_curpad[po] = newSV(0); 1657 SvPADTMP_on(PL_curpad[po]); 1658 #else 1659 PL_curpad[po] = NULL; 1660 #endif 1661 if (PadnamelistMAX(PL_comppad_name) != -1 1662 && (PADOFFSET)PadnamelistMAX(PL_comppad_name) >= po) { 1663 if (PadnamelistARRAY(PL_comppad_name)[po]) { 1664 assert(!PadnameLEN(PadnamelistARRAY(PL_comppad_name)[po])); 1665 } 1666 PadnamelistARRAY(PL_comppad_name)[po] = &PL_sv_undef; 1667 } 1668 if ((I32)po < PL_padix) 1669 PL_padix = po - 1; 1670 } 1671 1672 /* 1673 =for apidoc m|void|pad_reset 1674 1675 Mark all the current temporaries for reuse 1676 1677 =cut 1678 */ 1679 1680 /* XXX pad_reset() is currently disabled because it results in serious bugs. 1681 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed 1682 * on the stack by OPs that use them, there are several ways to get an alias 1683 * to a shared TARG. Such an alias will change randomly and unpredictably. 1684 * We avoid doing this until we can think of a Better Way. 1685 * GSAR 97-10-29 */ 1686 static void 1687 S_pad_reset(pTHX) 1688 { 1689 dVAR; 1690 #ifdef USE_BROKEN_PAD_RESET 1691 if (AvARRAY(PL_comppad) != PL_curpad) 1692 Perl_croak(aTHX_ "panic: pad_reset curpad, %p!=%p", 1693 AvARRAY(PL_comppad), PL_curpad); 1694 1695 DEBUG_X(PerlIO_printf(Perl_debug_log, 1696 "Pad 0x%"UVxf"[0x%"UVxf"] reset: padix %ld -> %ld", 1697 PTR2UV(PL_comppad), PTR2UV(PL_curpad), 1698 (long)PL_padix, (long)PL_padix_floor 1699 ) 1700 ); 1701 1702 if (!TAINTING_get) { /* Can't mix tainted and non-tainted temporaries. */ 1703 I32 po; 1704 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) { 1705 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po])) 1706 SvPADTMP_off(PL_curpad[po]); 1707 } 1708 PL_padix = PL_padix_floor; 1709 } 1710 #endif 1711 PL_pad_reset_pending = FALSE; 1712 } 1713 1714 /* 1715 =for apidoc Amx|void|pad_tidy|padtidy_type type 1716 1717 Tidy up a pad at the end of compilation of the code to which it belongs. 1718 Jobs performed here are: remove most stuff from the pads of anonsub 1719 prototypes; give it a @_; mark temporaries as such. I<type> indicates 1720 the kind of subroutine: 1721 1722 padtidy_SUB ordinary subroutine 1723 padtidy_SUBCLONE prototype for lexical closure 1724 padtidy_FORMAT format 1725 1726 =cut 1727 */ 1728 1729 /* XXX DAPM surely most of this stuff should be done properly 1730 * at the right time beforehand, rather than going around afterwards 1731 * cleaning up our mistakes ??? 1732 */ 1733 1734 void 1735 Perl_pad_tidy(pTHX_ padtidy_type type) 1736 { 1737 dVAR; 1738 1739 ASSERT_CURPAD_ACTIVE("pad_tidy"); 1740 1741 /* If this CV has had any 'eval-capable' ops planted in it: 1742 * i.e. it contains any of: 1743 * 1744 * * eval '...', 1745 * * //ee, 1746 * * use re 'eval'; /$var/ 1747 * * /(?{..})/), 1748 * 1749 * Then any anon prototypes in the chain of CVs should be marked as 1750 * cloneable, so that for example the eval's CV in 1751 * 1752 * sub { eval '$x' } 1753 * 1754 * gets the right CvOUTSIDE. If running with -d, *any* sub may 1755 * potentially have an eval executed within it. 1756 */ 1757 1758 if (PL_cv_has_eval || PL_perldb) { 1759 const CV *cv; 1760 for (cv = PL_compcv ;cv; cv = CvOUTSIDE(cv)) { 1761 if (cv != PL_compcv && CvCOMPILED(cv)) 1762 break; /* no need to mark already-compiled code */ 1763 if (CvANON(cv)) { 1764 DEBUG_Xv(PerlIO_printf(Perl_debug_log, 1765 "Pad clone on cv=0x%"UVxf"\n", PTR2UV(cv))); 1766 CvCLONE_on(cv); 1767 } 1768 CvHASEVAL_on(cv); 1769 } 1770 } 1771 1772 /* extend namepad to match curpad */ 1773 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad)) 1774 av_store(PL_comppad_name, AvFILLp(PL_comppad), NULL); 1775 1776 if (type == padtidy_SUBCLONE) { 1777 SV ** const namep = AvARRAY(PL_comppad_name); 1778 PADOFFSET ix; 1779 1780 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { 1781 SV *namesv; 1782 if (!namep[ix]) namep[ix] = &PL_sv_undef; 1783 1784 /* 1785 * The only things that a clonable function needs in its 1786 * pad are anonymous subs, constants and GVs. 1787 * The rest are created anew during cloning. 1788 */ 1789 if (!PL_curpad[ix] || SvIMMORTAL(PL_curpad[ix]) 1790 || IS_PADGV(PL_curpad[ix])) 1791 continue; 1792 namesv = namep[ix]; 1793 if (!(PadnamePV(namesv) && 1794 (!PadnameLEN(namesv) || *SvPVX_const(namesv) == '&'))) 1795 { 1796 SvREFCNT_dec(PL_curpad[ix]); 1797 PL_curpad[ix] = NULL; 1798 } 1799 } 1800 } 1801 else if (type == padtidy_SUB) { 1802 /* XXX DAPM this same bit of code keeps appearing !!! Rationalise? */ 1803 AV * const av = newAV(); /* Will be @_ */ 1804 av_store(PL_comppad, 0, MUTABLE_SV(av)); 1805 AvREIFY_only(av); 1806 } 1807 1808 if (type == padtidy_SUB || type == padtidy_FORMAT) { 1809 SV ** const namep = AvARRAY(PL_comppad_name); 1810 PADOFFSET ix; 1811 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { 1812 if (!namep[ix]) namep[ix] = &PL_sv_undef; 1813 if (!PL_curpad[ix] || SvIMMORTAL(PL_curpad[ix]) 1814 || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix])) 1815 continue; 1816 if (!SvPADMY(PL_curpad[ix])) { 1817 SvPADTMP_on(PL_curpad[ix]); 1818 } else if (!SvFAKE(namep[ix])) { 1819 /* This is a work around for how the current implementation of 1820 ?{ } blocks in regexps interacts with lexicals. 1821 1822 One of our lexicals. 1823 Can't do this on all lexicals, otherwise sub baz() won't 1824 compile in 1825 1826 my $foo; 1827 1828 sub bar { ++$foo; } 1829 1830 sub baz { ++$foo; } 1831 1832 because completion of compiling &bar calling pad_tidy() 1833 would cause (top level) $foo to be marked as stale, and 1834 "no longer available". */ 1835 SvPADSTALE_on(PL_curpad[ix]); 1836 } 1837 } 1838 } 1839 PL_curpad = AvARRAY(PL_comppad); 1840 } 1841 1842 /* 1843 =for apidoc m|void|pad_free|PADOFFSET po 1844 1845 Free the SV at offset po in the current pad. 1846 1847 =cut 1848 */ 1849 1850 /* XXX DAPM integrate with pad_swipe ???? */ 1851 void 1852 Perl_pad_free(pTHX_ PADOFFSET po) 1853 { 1854 dVAR; 1855 SV *sv; 1856 ASSERT_CURPAD_LEGAL("pad_free"); 1857 if (!PL_curpad) 1858 return; 1859 if (AvARRAY(PL_comppad) != PL_curpad) 1860 Perl_croak(aTHX_ "panic: pad_free curpad, %p!=%p", 1861 AvARRAY(PL_comppad), PL_curpad); 1862 if (!po) 1863 Perl_croak(aTHX_ "panic: pad_free po"); 1864 1865 DEBUG_X(PerlIO_printf(Perl_debug_log, 1866 "Pad 0x%"UVxf"[0x%"UVxf"] free: %ld\n", 1867 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po) 1868 ); 1869 1870 1871 sv = PL_curpad[po]; 1872 if (sv && sv != &PL_sv_undef && !SvPADMY(sv)) 1873 SvFLAGS(sv) &= ~SVs_PADTMP; 1874 1875 if ((I32)po < PL_padix) 1876 PL_padix = po - 1; 1877 } 1878 1879 /* 1880 =for apidoc m|void|do_dump_pad|I32 level|PerlIO *file|PADLIST *padlist|int full 1881 1882 Dump the contents of a padlist 1883 1884 =cut 1885 */ 1886 1887 void 1888 Perl_do_dump_pad(pTHX_ I32 level, PerlIO *file, PADLIST *padlist, int full) 1889 { 1890 dVAR; 1891 const AV *pad_name; 1892 const AV *pad; 1893 SV **pname; 1894 SV **ppad; 1895 I32 ix; 1896 1897 PERL_ARGS_ASSERT_DO_DUMP_PAD; 1898 1899 if (!padlist) { 1900 return; 1901 } 1902 pad_name = *PadlistARRAY(padlist); 1903 pad = PadlistARRAY(padlist)[1]; 1904 pname = AvARRAY(pad_name); 1905 ppad = AvARRAY(pad); 1906 Perl_dump_indent(aTHX_ level, file, 1907 "PADNAME = 0x%"UVxf"(0x%"UVxf") PAD = 0x%"UVxf"(0x%"UVxf")\n", 1908 PTR2UV(pad_name), PTR2UV(pname), PTR2UV(pad), PTR2UV(ppad) 1909 ); 1910 1911 for (ix = 1; ix <= AvFILLp(pad_name); ix++) { 1912 const SV *namesv = pname[ix]; 1913 if (namesv && !PadnameLEN(namesv)) { 1914 namesv = NULL; 1915 } 1916 if (namesv) { 1917 if (SvFAKE(namesv)) 1918 Perl_dump_indent(aTHX_ level+1, file, 1919 "%2d. 0x%"UVxf"<%lu> FAKE \"%s\" flags=0x%lx index=%lu\n", 1920 (int) ix, 1921 PTR2UV(ppad[ix]), 1922 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0), 1923 SvPVX_const(namesv), 1924 (unsigned long)PARENT_FAKELEX_FLAGS(namesv), 1925 (unsigned long)PARENT_PAD_INDEX(namesv) 1926 1927 ); 1928 else 1929 Perl_dump_indent(aTHX_ level+1, file, 1930 "%2d. 0x%"UVxf"<%lu> (%lu,%lu) \"%s\"\n", 1931 (int) ix, 1932 PTR2UV(ppad[ix]), 1933 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0), 1934 (unsigned long)COP_SEQ_RANGE_LOW(namesv), 1935 (unsigned long)COP_SEQ_RANGE_HIGH(namesv), 1936 SvPVX_const(namesv) 1937 ); 1938 } 1939 else if (full) { 1940 Perl_dump_indent(aTHX_ level+1, file, 1941 "%2d. 0x%"UVxf"<%lu>\n", 1942 (int) ix, 1943 PTR2UV(ppad[ix]), 1944 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0) 1945 ); 1946 } 1947 } 1948 } 1949 1950 #ifdef DEBUGGING 1951 1952 /* 1953 =for apidoc m|void|cv_dump|CV *cv|const char *title 1954 1955 dump the contents of a CV 1956 1957 =cut 1958 */ 1959 1960 STATIC void 1961 S_cv_dump(pTHX_ const CV *cv, const char *title) 1962 { 1963 dVAR; 1964 const CV * const outside = CvOUTSIDE(cv); 1965 PADLIST* const padlist = CvPADLIST(cv); 1966 1967 PERL_ARGS_ASSERT_CV_DUMP; 1968 1969 PerlIO_printf(Perl_debug_log, 1970 " %s: CV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n", 1971 title, 1972 PTR2UV(cv), 1973 (CvANON(cv) ? "ANON" 1974 : (SvTYPE(cv) == SVt_PVFM) ? "FORMAT" 1975 : (cv == PL_main_cv) ? "MAIN" 1976 : CvUNIQUE(cv) ? "UNIQUE" 1977 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"), 1978 PTR2UV(outside), 1979 (!outside ? "null" 1980 : CvANON(outside) ? "ANON" 1981 : (outside == PL_main_cv) ? "MAIN" 1982 : CvUNIQUE(outside) ? "UNIQUE" 1983 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED")); 1984 1985 PerlIO_printf(Perl_debug_log, 1986 " PADLIST = 0x%"UVxf"\n", PTR2UV(padlist)); 1987 do_dump_pad(1, Perl_debug_log, padlist, 1); 1988 } 1989 1990 #endif /* DEBUGGING */ 1991 1992 /* 1993 =for apidoc Am|CV *|cv_clone|CV *proto 1994 1995 Clone a CV, making a lexical closure. I<proto> supplies the prototype 1996 of the function: its code, pad structure, and other attributes. 1997 The prototype is combined with a capture of outer lexicals to which the 1998 code refers, which are taken from the currently-executing instance of 1999 the immediately surrounding code. 2000 2001 =cut 2002 */ 2003 2004 static CV *S_cv_clone(pTHX_ CV *proto, CV *cv, CV *outside); 2005 2006 static void 2007 S_cv_clone_pad(pTHX_ CV *proto, CV *cv, CV *outside, bool newcv) 2008 { 2009 dVAR; 2010 I32 ix; 2011 PADLIST* const protopadlist = CvPADLIST(proto); 2012 PAD *const protopad_name = *PadlistARRAY(protopadlist); 2013 const PAD *const protopad = PadlistARRAY(protopadlist)[1]; 2014 SV** const pname = AvARRAY(protopad_name); 2015 SV** const ppad = AvARRAY(protopad); 2016 const I32 fname = AvFILLp(protopad_name); 2017 const I32 fpad = AvFILLp(protopad); 2018 SV** outpad; 2019 long depth; 2020 bool subclones = FALSE; 2021 2022 assert(!CvUNIQUE(proto)); 2023 2024 /* Anonymous subs have a weak CvOUTSIDE pointer, so its value is not 2025 * reliable. The currently-running sub is always the one we need to 2026 * close over. 2027 * For my subs, the currently-running sub may not be the one we want. 2028 * We have to check whether it is a clone of CvOUTSIDE. 2029 * Note that in general for formats, CvOUTSIDE != find_runcv. 2030 * Since formats may be nested inside closures, CvOUTSIDE may point 2031 * to a prototype; we instead want the cloned parent who called us. 2032 */ 2033 2034 if (!outside) { 2035 if (CvWEAKOUTSIDE(proto)) 2036 outside = find_runcv(NULL); 2037 else { 2038 outside = CvOUTSIDE(proto); 2039 if ((CvCLONE(outside) && ! CvCLONED(outside)) 2040 || !CvPADLIST(outside) 2041 || PadlistNAMES(CvPADLIST(outside)) 2042 != protopadlist->xpadl_outid) { 2043 outside = find_runcv_where( 2044 FIND_RUNCV_padid_eq, PTR2IV(protopadlist->xpadl_outid), NULL 2045 ); 2046 /* outside could be null */ 2047 } 2048 } 2049 } 2050 depth = outside ? CvDEPTH(outside) : 0; 2051 if (!depth) 2052 depth = 1; 2053 2054 ENTER; 2055 SAVESPTR(PL_compcv); 2056 PL_compcv = cv; 2057 if (newcv) SAVEFREESV(cv); /* in case of fatal warnings */ 2058 2059 if (CvHASEVAL(cv)) 2060 CvOUTSIDE(cv) = MUTABLE_CV(SvREFCNT_inc_simple(outside)); 2061 2062 SAVESPTR(PL_comppad_name); 2063 PL_comppad_name = protopad_name; 2064 CvPADLIST(cv) = pad_new(padnew_CLONE|padnew_SAVE); 2065 2066 av_fill(PL_comppad, fpad); 2067 2068 PL_curpad = AvARRAY(PL_comppad); 2069 2070 outpad = outside && CvPADLIST(outside) 2071 ? AvARRAY(PadlistARRAY(CvPADLIST(outside))[depth]) 2072 : NULL; 2073 if (outpad) 2074 CvPADLIST(cv)->xpadl_outid = PadlistNAMES(CvPADLIST(outside)); 2075 2076 for (ix = fpad; ix > 0; ix--) { 2077 SV* const namesv = (ix <= fname) ? pname[ix] : NULL; 2078 SV *sv = NULL; 2079 if (namesv && PadnameLEN(namesv)) { /* lexical */ 2080 if (PadnameIsOUR(namesv)) { /* or maybe not so lexical */ 2081 NOOP; 2082 } 2083 else { 2084 if (SvFAKE(namesv)) { /* lexical from outside? */ 2085 /* formats may have an inactive, or even undefined, parent; 2086 but state vars are always available. */ 2087 if (!outpad || !(sv = outpad[PARENT_PAD_INDEX(namesv)]) 2088 || ( SvPADSTALE(sv) && !SvPAD_STATE(namesv) 2089 && (!outside || !CvDEPTH(outside))) ) { 2090 S_unavailable(aTHX_ namesv); 2091 sv = NULL; 2092 } 2093 else 2094 SvREFCNT_inc_simple_void_NN(sv); 2095 } 2096 if (!sv) { 2097 const char sigil = SvPVX_const(namesv)[0]; 2098 if (sigil == '&') 2099 /* If there are state subs, we need to clone them, too. 2100 But they may need to close over variables we have 2101 not cloned yet. So we will have to do a second 2102 pass. Furthermore, there may be state subs clos- 2103 ing over other state subs’ entries, so we have 2104 to put a stub here and then clone into it on the 2105 second pass. */ 2106 if (SvPAD_STATE(namesv) && !CvCLONED(ppad[ix])) { 2107 assert(SvTYPE(ppad[ix]) == SVt_PVCV); 2108 subclones = 1; 2109 sv = newSV_type(SVt_PVCV); 2110 } 2111 else if (PadnameLEN(namesv)>1 && !PadnameIsOUR(namesv)) 2112 { 2113 /* my sub */ 2114 /* Just provide a stub, but name it. It will be 2115 upgrade to the real thing on scope entry. */ 2116 sv = newSV_type(SVt_PVCV); 2117 CvNAME_HEK_set( 2118 sv, 2119 share_hek(SvPVX_const(namesv)+1, 2120 SvCUR(namesv) - 1 2121 * (SvUTF8(namesv) ? -1 : 1), 2122 0) 2123 ); 2124 } 2125 else sv = SvREFCNT_inc(ppad[ix]); 2126 else if (sigil == '@') 2127 sv = MUTABLE_SV(newAV()); 2128 else if (sigil == '%') 2129 sv = MUTABLE_SV(newHV()); 2130 else 2131 sv = newSV(0); 2132 SvPADMY_on(sv); 2133 /* reset the 'assign only once' flag on each state var */ 2134 if (sigil != '&' && SvPAD_STATE(namesv)) 2135 SvPADSTALE_on(sv); 2136 } 2137 } 2138 } 2139 else if (IS_PADGV(ppad[ix]) || (namesv && PadnamePV(namesv))) { 2140 sv = SvREFCNT_inc_NN(ppad[ix]); 2141 } 2142 else { 2143 sv = newSV(0); 2144 SvPADTMP_on(sv); 2145 } 2146 PL_curpad[ix] = sv; 2147 } 2148 2149 if (subclones) 2150 for (ix = fpad; ix > 0; ix--) { 2151 SV* const namesv = (ix <= fname) ? pname[ix] : NULL; 2152 if (namesv && namesv != &PL_sv_undef && !SvFAKE(namesv) 2153 && SvPVX_const(namesv)[0] == '&' && SvPAD_STATE(namesv)) 2154 S_cv_clone(aTHX_ (CV *)ppad[ix], (CV *)PL_curpad[ix], cv); 2155 } 2156 2157 if (newcv) SvREFCNT_inc_simple_void_NN(cv); 2158 LEAVE; 2159 } 2160 2161 static CV * 2162 S_cv_clone(pTHX_ CV *proto, CV *cv, CV *outside) 2163 { 2164 dVAR; 2165 const bool newcv = !cv; 2166 2167 assert(!CvUNIQUE(proto)); 2168 2169 if (!cv) cv = MUTABLE_CV(newSV_type(SvTYPE(proto))); 2170 CvFLAGS(cv) = CvFLAGS(proto) & ~(CVf_CLONE|CVf_WEAKOUTSIDE|CVf_CVGV_RC 2171 |CVf_SLABBED); 2172 CvCLONED_on(cv); 2173 2174 CvFILE(cv) = CvDYNFILE(proto) ? savepv(CvFILE(proto)) 2175 : CvFILE(proto); 2176 if (CvNAMED(proto)) 2177 CvNAME_HEK_set(cv, share_hek_hek(CvNAME_HEK(proto))); 2178 else CvGV_set(cv,CvGV(proto)); 2179 CvSTASH_set(cv, CvSTASH(proto)); 2180 OP_REFCNT_LOCK; 2181 CvROOT(cv) = OpREFCNT_inc(CvROOT(proto)); 2182 OP_REFCNT_UNLOCK; 2183 CvSTART(cv) = CvSTART(proto); 2184 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(proto); 2185 2186 if (SvPOK(proto)) { 2187 sv_setpvn(MUTABLE_SV(cv), SvPVX_const(proto), SvCUR(proto)); 2188 if (SvUTF8(proto)) 2189 SvUTF8_on(MUTABLE_SV(cv)); 2190 } 2191 if (SvMAGIC(proto)) 2192 mg_copy((SV *)proto, (SV *)cv, 0, 0); 2193 2194 if (CvPADLIST(proto)) S_cv_clone_pad(aTHX_ proto, cv, outside, newcv); 2195 2196 DEBUG_Xv( 2197 PerlIO_printf(Perl_debug_log, "\nPad CV clone\n"); 2198 if (CvOUTSIDE(cv)) cv_dump(CvOUTSIDE(cv), "Outside"); 2199 cv_dump(proto, "Proto"); 2200 cv_dump(cv, "To"); 2201 ); 2202 2203 if (CvCONST(cv)) { 2204 /* Constant sub () { $x } closing over $x - see lib/constant.pm: 2205 * The prototype was marked as a candiate for const-ization, 2206 * so try to grab the current const value, and if successful, 2207 * turn into a const sub: 2208 */ 2209 SV* const const_sv = op_const_sv(CvSTART(cv), cv); 2210 if (const_sv) { 2211 SvREFCNT_dec_NN(cv); 2212 /* For this calling case, op_const_sv returns a *copy*, which we 2213 donate to newCONSTSUB. Yes, this is ugly, and should be killed. 2214 Need to fix how lib/constant.pm works to eliminate this. */ 2215 cv = newCONSTSUB(CvSTASH(proto), NULL, const_sv); 2216 } 2217 else { 2218 CvCONST_off(cv); 2219 } 2220 } 2221 2222 return cv; 2223 } 2224 2225 CV * 2226 Perl_cv_clone(pTHX_ CV *proto) 2227 { 2228 PERL_ARGS_ASSERT_CV_CLONE; 2229 2230 if (!CvPADLIST(proto)) Perl_croak(aTHX_ "panic: no pad in cv_clone"); 2231 return S_cv_clone(aTHX_ proto, NULL, NULL); 2232 } 2233 2234 /* Called only by pp_clonecv */ 2235 CV * 2236 Perl_cv_clone_into(pTHX_ CV *proto, CV *target) 2237 { 2238 PERL_ARGS_ASSERT_CV_CLONE_INTO; 2239 cv_undef(target); 2240 return S_cv_clone(aTHX_ proto, target, NULL); 2241 } 2242 2243 /* 2244 =for apidoc m|void|pad_fixup_inner_anons|PADLIST *padlist|CV *old_cv|CV *new_cv 2245 2246 For any anon CVs in the pad, change CvOUTSIDE of that CV from 2247 old_cv to new_cv if necessary. Needed when a newly-compiled CV has to be 2248 moved to a pre-existing CV struct. 2249 2250 =cut 2251 */ 2252 2253 void 2254 Perl_pad_fixup_inner_anons(pTHX_ PADLIST *padlist, CV *old_cv, CV *new_cv) 2255 { 2256 dVAR; 2257 I32 ix; 2258 AV * const comppad_name = PadlistARRAY(padlist)[0]; 2259 AV * const comppad = PadlistARRAY(padlist)[1]; 2260 SV ** const namepad = AvARRAY(comppad_name); 2261 SV ** const curpad = AvARRAY(comppad); 2262 2263 PERL_ARGS_ASSERT_PAD_FIXUP_INNER_ANONS; 2264 PERL_UNUSED_ARG(old_cv); 2265 2266 for (ix = AvFILLp(comppad_name); ix > 0; ix--) { 2267 const SV * const namesv = namepad[ix]; 2268 if (namesv && namesv != &PL_sv_undef && !SvPAD_STATE(namesv) 2269 && *SvPVX_const(namesv) == '&') 2270 { 2271 if (SvTYPE(curpad[ix]) == SVt_PVCV) { 2272 MAGIC * const mg = 2273 SvMAGICAL(curpad[ix]) 2274 ? mg_find(curpad[ix], PERL_MAGIC_proto) 2275 : NULL; 2276 CV * const innercv = MUTABLE_CV(mg ? mg->mg_obj : curpad[ix]); 2277 if (CvOUTSIDE(innercv) == old_cv) { 2278 if (!CvWEAKOUTSIDE(innercv)) { 2279 SvREFCNT_dec(old_cv); 2280 SvREFCNT_inc_simple_void_NN(new_cv); 2281 } 2282 CvOUTSIDE(innercv) = new_cv; 2283 } 2284 } 2285 else { /* format reference */ 2286 SV * const rv = curpad[ix]; 2287 CV *innercv; 2288 if (!SvOK(rv)) continue; 2289 assert(SvROK(rv)); 2290 assert(SvWEAKREF(rv)); 2291 innercv = (CV *)SvRV(rv); 2292 assert(!CvWEAKOUTSIDE(innercv)); 2293 SvREFCNT_dec(CvOUTSIDE(innercv)); 2294 CvOUTSIDE(innercv) = (CV *)SvREFCNT_inc_simple_NN(new_cv); 2295 } 2296 } 2297 } 2298 } 2299 2300 /* 2301 =for apidoc m|void|pad_push|PADLIST *padlist|int depth 2302 2303 Push a new pad frame onto the padlist, unless there's already a pad at 2304 this depth, in which case don't bother creating a new one. Then give 2305 the new pad an @_ in slot zero. 2306 2307 =cut 2308 */ 2309 2310 void 2311 Perl_pad_push(pTHX_ PADLIST *padlist, int depth) 2312 { 2313 dVAR; 2314 2315 PERL_ARGS_ASSERT_PAD_PUSH; 2316 2317 if (depth > PadlistMAX(padlist) || !PadlistARRAY(padlist)[depth]) { 2318 PAD** const svp = PadlistARRAY(padlist); 2319 AV* const newpad = newAV(); 2320 SV** const oldpad = AvARRAY(svp[depth-1]); 2321 I32 ix = AvFILLp((const AV *)svp[1]); 2322 const I32 names_fill = AvFILLp((const AV *)svp[0]); 2323 SV** const names = AvARRAY(svp[0]); 2324 AV *av; 2325 2326 for ( ;ix > 0; ix--) { 2327 if (names_fill >= ix && PadnameLEN(names[ix])) { 2328 const char sigil = SvPVX_const(names[ix])[0]; 2329 if ((SvFLAGS(names[ix]) & SVf_FAKE) 2330 || (SvFLAGS(names[ix]) & SVpad_STATE) 2331 || sigil == '&') 2332 { 2333 /* outer lexical or anon code */ 2334 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix])); 2335 } 2336 else { /* our own lexical */ 2337 SV *sv; 2338 if (sigil == '@') 2339 sv = MUTABLE_SV(newAV()); 2340 else if (sigil == '%') 2341 sv = MUTABLE_SV(newHV()); 2342 else 2343 sv = newSV(0); 2344 av_store(newpad, ix, sv); 2345 SvPADMY_on(sv); 2346 } 2347 } 2348 else if (IS_PADGV(oldpad[ix]) || PadnamePV(names[ix])) { 2349 av_store(newpad, ix, SvREFCNT_inc_NN(oldpad[ix])); 2350 } 2351 else { 2352 /* save temporaries on recursion? */ 2353 SV * const sv = newSV(0); 2354 av_store(newpad, ix, sv); 2355 SvPADTMP_on(sv); 2356 } 2357 } 2358 av = newAV(); 2359 av_store(newpad, 0, MUTABLE_SV(av)); 2360 AvREIFY_only(av); 2361 2362 padlist_store(padlist, depth, newpad); 2363 } 2364 } 2365 2366 /* 2367 =for apidoc Am|HV *|pad_compname_type|PADOFFSET po 2368 2369 Looks up the type of the lexical variable at position I<po> in the 2370 currently-compiling pad. If the variable is typed, the stash of the 2371 class to which it is typed is returned. If not, C<NULL> is returned. 2372 2373 =cut 2374 */ 2375 2376 HV * 2377 Perl_pad_compname_type(pTHX_ const PADOFFSET po) 2378 { 2379 dVAR; 2380 SV* const * const av = av_fetch(PL_comppad_name, po, FALSE); 2381 if ( SvPAD_TYPED(*av) ) { 2382 return SvSTASH(*av); 2383 } 2384 return NULL; 2385 } 2386 2387 #if defined(USE_ITHREADS) 2388 2389 # define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t)) 2390 2391 /* 2392 =for apidoc padlist_dup 2393 2394 Duplicates a pad. 2395 2396 =cut 2397 */ 2398 2399 PADLIST * 2400 Perl_padlist_dup(pTHX_ PADLIST *srcpad, CLONE_PARAMS *param) 2401 { 2402 PADLIST *dstpad; 2403 bool cloneall; 2404 PADOFFSET max; 2405 2406 PERL_ARGS_ASSERT_PADLIST_DUP; 2407 2408 if (!srcpad) 2409 return NULL; 2410 2411 cloneall = param->flags & CLONEf_COPY_STACKS 2412 || SvREFCNT(PadlistARRAY(srcpad)[1]) > 1; 2413 assert (SvREFCNT(PadlistARRAY(srcpad)[1]) == 1); 2414 2415 max = cloneall ? PadlistMAX(srcpad) : 1; 2416 2417 Newx(dstpad, 1, PADLIST); 2418 ptr_table_store(PL_ptr_table, srcpad, dstpad); 2419 PadlistMAX(dstpad) = max; 2420 Newx(PadlistARRAY(dstpad), max + 1, PAD *); 2421 2422 if (cloneall) { 2423 PADOFFSET depth; 2424 for (depth = 0; depth <= max; ++depth) 2425 PadlistARRAY(dstpad)[depth] = 2426 av_dup_inc(PadlistARRAY(srcpad)[depth], param); 2427 } else { 2428 /* CvDEPTH() on our subroutine will be set to 0, so there's no need 2429 to build anything other than the first level of pads. */ 2430 I32 ix = AvFILLp(PadlistARRAY(srcpad)[1]); 2431 AV *pad1; 2432 const I32 names_fill = AvFILLp(PadlistARRAY(srcpad)[0]); 2433 const PAD *const srcpad1 = PadlistARRAY(srcpad)[1]; 2434 SV **oldpad = AvARRAY(srcpad1); 2435 SV **names; 2436 SV **pad1a; 2437 AV *args; 2438 2439 PadlistARRAY(dstpad)[0] = 2440 av_dup_inc(PadlistARRAY(srcpad)[0], param); 2441 names = AvARRAY(PadlistARRAY(dstpad)[0]); 2442 2443 pad1 = newAV(); 2444 2445 av_extend(pad1, ix); 2446 PadlistARRAY(dstpad)[1] = pad1; 2447 pad1a = AvARRAY(pad1); 2448 2449 if (ix > -1) { 2450 AvFILLp(pad1) = ix; 2451 2452 for ( ;ix > 0; ix--) { 2453 if (!oldpad[ix]) { 2454 pad1a[ix] = NULL; 2455 } else if (names_fill >= ix && names[ix] && 2456 PadnameLEN(names[ix])) { 2457 const char sigil = SvPVX_const(names[ix])[0]; 2458 if ((SvFLAGS(names[ix]) & SVf_FAKE) 2459 || (SvFLAGS(names[ix]) & SVpad_STATE) 2460 || sigil == '&') 2461 { 2462 /* outer lexical or anon code */ 2463 pad1a[ix] = sv_dup_inc(oldpad[ix], param); 2464 } 2465 else { /* our own lexical */ 2466 if(SvPADSTALE(oldpad[ix]) && SvREFCNT(oldpad[ix]) > 1) { 2467 /* This is a work around for how the current 2468 implementation of ?{ } blocks in regexps 2469 interacts with lexicals. */ 2470 pad1a[ix] = sv_dup_inc(oldpad[ix], param); 2471 } else { 2472 SV *sv; 2473 2474 if (sigil == '@') 2475 sv = MUTABLE_SV(newAV()); 2476 else if (sigil == '%') 2477 sv = MUTABLE_SV(newHV()); 2478 else 2479 sv = newSV(0); 2480 pad1a[ix] = sv; 2481 SvPADMY_on(sv); 2482 } 2483 } 2484 } 2485 else if (IS_PADGV(oldpad[ix]) 2486 || ( names_fill >= ix && names[ix] 2487 && PadnamePV(names[ix]) )) { 2488 pad1a[ix] = sv_dup_inc(oldpad[ix], param); 2489 } 2490 else { 2491 /* save temporaries on recursion? */ 2492 SV * const sv = newSV(0); 2493 pad1a[ix] = sv; 2494 2495 /* SvREFCNT(oldpad[ix]) != 1 for some code in threads.xs 2496 FIXTHAT before merging this branch. 2497 (And I know how to) */ 2498 if (SvPADMY(oldpad[ix])) 2499 SvPADMY_on(sv); 2500 else 2501 SvPADTMP_on(sv); 2502 } 2503 } 2504 2505 if (oldpad[0]) { 2506 args = newAV(); /* Will be @_ */ 2507 AvREIFY_only(args); 2508 pad1a[0] = (SV *)args; 2509 } 2510 } 2511 } 2512 2513 return dstpad; 2514 } 2515 2516 #endif /* USE_ITHREADS */ 2517 2518 PAD ** 2519 Perl_padlist_store(pTHX_ PADLIST *padlist, I32 key, PAD *val) 2520 { 2521 dVAR; 2522 PAD **ary; 2523 SSize_t const oldmax = PadlistMAX(padlist); 2524 2525 PERL_ARGS_ASSERT_PADLIST_STORE; 2526 2527 assert(key >= 0); 2528 2529 if (key > PadlistMAX(padlist)) { 2530 av_extend_guts(NULL,key,&PadlistMAX(padlist), 2531 (SV ***)&PadlistARRAY(padlist), 2532 (SV ***)&PadlistARRAY(padlist)); 2533 Zero(PadlistARRAY(padlist)+oldmax+1, PadlistMAX(padlist)-oldmax, 2534 PAD *); 2535 } 2536 ary = PadlistARRAY(padlist); 2537 SvREFCNT_dec(ary[key]); 2538 ary[key] = val; 2539 return &ary[key]; 2540 } 2541 2542 /* 2543 * Local variables: 2544 * c-indentation-style: bsd 2545 * c-basic-offset: 4 2546 * indent-tabs-mode: nil 2547 * End: 2548 * 2549 * ex: set ts=8 sts=4 sw=4 et: 2550 */ 2551