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