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