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