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