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