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