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