This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
yet another way of debugging memory allocations
[perl5.git] / pad.c
CommitLineData
dd2155a4
DM
1/* pad.c
2 *
1d325971 3 * Copyright (C) 2002, 2003, 2004, 2005 by Larry Wall and others
dd2155a4
DM
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 * "Anyway: there was this Mr Frodo left an orphan and stranded, as you
9 * might say, among those queer Bucklanders, being brought up anyhow in
10 * Brandy Hall. A regular warren, by all accounts. Old Master Gorbadoc
11 * never had fewer than a couple of hundred relations in the place. Mr
12 * Bilbo never did a kinder deed than when he brought the lad back to
13 * live among decent folk." --the Gaffer
14 */
15
16/* XXX DAPM
17 * As of Sept 2002, this file is new and may be in a state of flux for
18 * a while. I've marked things I intent to come back and look at further
19 * with an 'XXX DAPM' comment.
20 */
21
22/*
23=head1 Pad Data Structures
24
61296642 25This file contains the functions that create and manipulate scratchpads,
166f8a29 26which are array-of-array data structures attached to a CV (ie a sub)
61296642 27and which store lexical variables and opcode temporary and per-thread
166f8a29
DM
28values.
29
dd2155a4
DM
30=for apidoc m|AV *|CvPADLIST|CV *cv
31CV's can have CvPADLIST(cv) set to point to an AV.
32
33For these purposes "forms" are a kind-of CV, eval""s are too (except they're
34not callable at will and are always thrown away after the eval"" is done
b5c19bd7
DM
35executing). Require'd files are simply evals without any outer lexical
36scope.
dd2155a4
DM
37
38XSUBs don't have CvPADLIST set - dXSTARG fetches values from PL_curpad,
39but that is really the callers pad (a slot of which is allocated by
40every entersub).
41
42The CvPADLIST AV has does not have AvREAL set, so REFCNT of component items
f3548bdc 43is managed "manual" (mostly in pad.c) rather than normal av.c rules.
dd2155a4
DM
44The items in the AV are not SVs as for a normal AV, but other AVs:
45
460'th Entry of the CvPADLIST is an AV which represents the "names" or rather
47the "static type information" for lexicals.
48
49The CvDEPTH'th entry of CvPADLIST AV is an AV which is the stack frame at that
50depth of recursion into the CV.
51The 0'th slot of a frame AV is an AV which is @_.
52other entries are storage for variables and op targets.
53
54During compilation:
a6d05634
TM
55C<PL_comppad_name> is set to the names AV.
56C<PL_comppad> is set to the frame AV for the frame CvDEPTH == 1.
57C<PL_curpad> is set to the body of the frame AV (i.e. AvARRAY(PL_comppad)).
dd2155a4 58
f3548bdc
DM
59During execution, C<PL_comppad> and C<PL_curpad> refer to the live
60frame of the currently executing sub.
61
62Iterating over the names AV iterates over all possible pad
dd2155a4
DM
63items. Pad slots that are SVs_PADTMP (targets/GVs/constants) end up having
64&PL_sv_undef "names" (see pad_alloc()).
65
66Only my/our variable (SVs_PADMY/SVs_PADOUR) slots get valid names.
67The rest are op targets/GVs/constants which are statically allocated
68or resolved at compile time. These don't have names by which they
69can be looked up from Perl code at run time through eval"" like
70my/our variables can be. Since they can't be looked up by "name"
71but only by their index allocated at compile time (which is usually
72in PL_op->op_targ), wasting a name SV for them doesn't make sense.
73
74The SVs in the names AV have their PV being the name of the variable.
75NV+1..IV inclusive is a range of cop_seq numbers for which the name is
76valid. For typed lexicals name SV is SVt_PVMG and SvSTASH points at the
77type. For C<our> lexicals, the type is SVt_PVGV, and GvSTASH points at the
78stash of the associated global (so that duplicate C<our> delarations in the
79same package can be detected). SvCUR is sometimes hijacked to
80store the generation number during compilation.
81
b5c19bd7
DM
82If SvFAKE is set on the name SV, then that slot in the frame AV is
83a REFCNT'ed reference to a lexical from "outside". In this case,
84the name SV does not use NVX and IVX to store a cop_seq range, since it is
85in scope throughout. Instead IVX stores some flags containing info about
86the real lexical (is it declared in an anon, and is it capable of being
87instantiated multiple times?), and for fake ANONs, NVX contains the index
88within the parent's pad where the lexical's value is stored, to make
89cloning quicker.
dd2155a4 90
a6d05634 91If the 'name' is '&' the corresponding entry in frame AV
dd2155a4
DM
92is a CV representing a possible closure.
93(SvFAKE and name of '&' is not a meaningful combination currently but could
94become so if C<my sub foo {}> is implemented.)
95
71f882da
DM
96Note that formats are treated as anon subs, and are cloned each time
97write is called (if necessary).
98
e6e7068b
DM
99The flag SVf_PADSTALE is cleared on lexicals each time the my() is executed,
100and set on scope exit. This allows the 'Variable $x is not available' warning
101to be generated in evals, such as
102
103 { my $x = 1; sub f { eval '$x'} } f();
104
dd2155a4
DM
105=cut
106*/
107
108
109#include "EXTERN.h"
110#define PERL_IN_PAD_C
111#include "perl.h"
112
113
114#define PAD_MAX 999999999
115
116
117
118/*
119=for apidoc pad_new
120
121Create a new compiling padlist, saving and updating the various global
122vars at the same time as creating the pad itself. The following flags
123can be OR'ed together:
124
125 padnew_CLONE this pad is for a cloned CV
126 padnew_SAVE save old globals
127 padnew_SAVESUB also save extra stuff for start of sub
128
129=cut
130*/
131
132PADLIST *
c7c737cb 133Perl_pad_new(pTHX_ int flags)
dd2155a4 134{
e1ec3a88 135 AV *padlist, *padname, *pad;
dd2155a4 136
f3548bdc
DM
137 ASSERT_CURPAD_LEGAL("pad_new");
138
dd2155a4
DM
139 /* XXX DAPM really need a new SAVEt_PAD which restores all or most
140 * vars (based on flags) rather than storing vals + addresses for
141 * each individually. Also see pad_block_start.
142 * XXX DAPM Try to see whether all these conditionals are required
143 */
144
145 /* save existing state, ... */
146
147 if (flags & padnew_SAVE) {
3979c56f 148 SAVECOMPPAD();
dd2155a4
DM
149 SAVESPTR(PL_comppad_name);
150 if (! (flags & padnew_CLONE)) {
151 SAVEI32(PL_padix);
152 SAVEI32(PL_comppad_name_fill);
153 SAVEI32(PL_min_intro_pending);
154 SAVEI32(PL_max_intro_pending);
b5c19bd7 155 SAVEI32(PL_cv_has_eval);
dd2155a4
DM
156 if (flags & padnew_SAVESUB) {
157 SAVEI32(PL_pad_reset_pending);
158 }
159 }
160 }
161 /* XXX DAPM interestingly, PL_comppad_name_floor never seems to be
162 * saved - check at some pt that this is okay */
163
164 /* ... create new pad ... */
165
166 padlist = newAV();
167 padname = newAV();
168 pad = newAV();
169
170 if (flags & padnew_CLONE) {
171 /* XXX DAPM I dont know why cv_clone needs it
172 * doing differently yet - perhaps this separate branch can be
173 * dispensed with eventually ???
174 */
175
e1ec3a88 176 AV * const a0 = newAV(); /* will be @_ */
dd2155a4
DM
177 av_extend(a0, 0);
178 av_store(pad, 0, (SV*)a0);
11ca45c0 179 AvREIFY_only(a0);
dd2155a4
DM
180 }
181 else {
dd2155a4 182 av_store(pad, 0, Nullsv);
dd2155a4
DM
183 }
184
185 AvREAL_off(padlist);
186 av_store(padlist, 0, (SV*)padname);
187 av_store(padlist, 1, (SV*)pad);
188
189 /* ... then update state variables */
190
191 PL_comppad_name = (AV*)(*av_fetch(padlist, 0, FALSE));
192 PL_comppad = (AV*)(*av_fetch(padlist, 1, FALSE));
193 PL_curpad = AvARRAY(PL_comppad);
194
195 if (! (flags & padnew_CLONE)) {
196 PL_comppad_name_fill = 0;
197 PL_min_intro_pending = 0;
198 PL_padix = 0;
b5c19bd7 199 PL_cv_has_eval = 0;
dd2155a4
DM
200 }
201
202 DEBUG_X(PerlIO_printf(Perl_debug_log,
b5c19bd7 203 "Pad 0x%"UVxf"[0x%"UVxf"] new: compcv=0x%"UVxf
dd2155a4 204 " name=0x%"UVxf" flags=0x%"UVxf"\n",
b5c19bd7 205 PTR2UV(PL_comppad), PTR2UV(PL_curpad), PTR2UV(PL_compcv),
dd2155a4
DM
206 PTR2UV(padname), (UV)flags
207 )
208 );
209
210 return (PADLIST*)padlist;
211}
212
213/*
214=for apidoc pad_undef
215
216Free the padlist associated with a CV.
217If parts of it happen to be current, we null the relevant
218PL_*pad* global vars so that we don't have any dangling references left.
219We also repoint the CvOUTSIDE of any about-to-be-orphaned
a3985cdc 220inner subs to the outer of this cv.
dd2155a4 221
7dafbf52
DM
222(This function should really be called pad_free, but the name was already
223taken)
224
dd2155a4
DM
225=cut
226*/
227
228void
a3985cdc 229Perl_pad_undef(pTHX_ CV* cv)
dd2155a4
DM
230{
231 I32 ix;
b64e5050 232 const PADLIST * const padlist = CvPADLIST(cv);
dd2155a4
DM
233
234 if (!padlist)
235 return;
236 if (!SvREFCNT(CvPADLIST(cv))) /* may be during global destruction */
237 return;
238
239 DEBUG_X(PerlIO_printf(Perl_debug_log,
b5c19bd7
DM
240 "Pad undef: cv=0x%"UVxf" padlist=0x%"UVxf"\n",
241 PTR2UV(cv), PTR2UV(padlist))
dd2155a4
DM
242 );
243
7dafbf52
DM
244 /* detach any '&' anon children in the pad; if afterwards they
245 * are still live, fix up their CvOUTSIDEs to point to our outside,
246 * bypassing us. */
247 /* XXX DAPM for efficiency, we should only do this if we know we have
248 * children, or integrate this loop with general cleanup */
dd2155a4 249
7dafbf52 250 if (!PL_dirty) { /* don't bother during global destruction */
53c1dcc0 251 CV * const outercv = CvOUTSIDE(cv);
e1ec3a88 252 const U32 seq = CvOUTSIDE_SEQ(cv);
53c1dcc0
AL
253 AV * const comppad_name = (AV*)AvARRAY(padlist)[0];
254 SV ** const namepad = AvARRAY(comppad_name);
255 AV * const comppad = (AV*)AvARRAY(padlist)[1];
256 SV ** const curpad = AvARRAY(comppad);
dd2155a4 257 for (ix = AvFILLp(comppad_name); ix > 0; ix--) {
504618e9 258 SV * const namesv = namepad[ix];
dd2155a4 259 if (namesv && namesv != &PL_sv_undef
b15aece3 260 && *SvPVX_const(namesv) == '&')
dd2155a4 261 {
7fc63493 262 CV * const innercv = (CV*)curpad[ix];
10dc53a8
DM
263 U32 inner_rc = SvREFCNT(innercv);
264 assert(inner_rc);
7dafbf52
DM
265 namepad[ix] = Nullsv;
266 SvREFCNT_dec(namesv);
01773faa
DM
267
268 if (SvREFCNT(comppad) < 2) { /* allow for /(?{ sub{} })/ */
269 curpad[ix] = Nullsv;
270 SvREFCNT_dec(innercv);
10dc53a8 271 inner_rc--;
01773faa 272 }
10dc53a8 273 if (inner_rc /* in use, not just a prototype */
dd2155a4
DM
274 && CvOUTSIDE(innercv) == cv)
275 {
7dafbf52 276 assert(CvWEAKOUTSIDE(innercv));
9d1ce744
JH
277 /* don't relink to grandfather if he's being freed */
278 if (outercv && SvREFCNT(outercv)) {
279 CvWEAKOUTSIDE_off(innercv);
280 CvOUTSIDE(innercv) = outercv;
281 CvOUTSIDE_SEQ(innercv) = seq;
7fc63493 282 (void)SvREFCNT_inc(outercv);
9d1ce744
JH
283 }
284 else {
285 CvOUTSIDE(innercv) = Nullcv;
286 }
287
dd2155a4 288 }
9d1ce744 289
dd2155a4
DM
290 }
291 }
292 }
7dafbf52 293
dd2155a4
DM
294 ix = AvFILLp(padlist);
295 while (ix >= 0) {
53c1dcc0 296 SV* const sv = AvARRAY(padlist)[ix--];
dd2155a4
DM
297 if (!sv)
298 continue;
299 if (sv == (SV*)PL_comppad_name)
300 PL_comppad_name = Nullav;
301 else if (sv == (SV*)PL_comppad) {
f3548bdc 302 PL_comppad = Null(PAD*);
dd2155a4
DM
303 PL_curpad = Null(SV**);
304 }
305 SvREFCNT_dec(sv);
306 }
307 SvREFCNT_dec((SV*)CvPADLIST(cv));
308 CvPADLIST(cv) = Null(PADLIST*);
309}
310
311
312
313
314/*
315=for apidoc pad_add_name
316
b5c19bd7
DM
317Create a new name and associated PADMY SV in the current pad; return the
318offset.
dd2155a4
DM
319If C<typestash> is valid, the name is for a typed lexical; set the
320name's stash to that value.
321If C<ourstash> is valid, it's an our lexical, set the name's
322GvSTASH to that value
323
dd2155a4
DM
324If fake, it means we're cloning an existing entry
325
326=cut
327*/
328
dd2155a4 329PADOFFSET
e1ec3a88 330Perl_pad_add_name(pTHX_ const char *name, HV* typestash, HV* ourstash, bool fake)
dd2155a4 331{
504618e9 332 const PADOFFSET offset = pad_alloc(OP_PADSV, SVs_PADMY);
53c1dcc0 333 SV* const namesv = NEWSV(1102, 0);
dd2155a4 334
f3548bdc
DM
335 ASSERT_CURPAD_ACTIVE("pad_add_name");
336
dd2155a4 337
dd2155a4
DM
338 sv_upgrade(namesv, ourstash ? SVt_PVGV : typestash ? SVt_PVMG : SVt_PVNV);
339 sv_setpv(namesv, name);
340
341 if (typestash) {
342 SvFLAGS(namesv) |= SVpad_TYPED;
b162af07 343 SvSTASH_set(namesv, (HV*)SvREFCNT_inc((SV*) typestash));
dd2155a4
DM
344 }
345 if (ourstash) {
346 SvFLAGS(namesv) |= SVpad_OUR;
e15faf7d
NC
347 GvSTASH(namesv) = ourstash;
348 Perl_sv_add_backref(aTHX_ (SV*)ourstash, namesv);
dd2155a4
DM
349 }
350
351 av_store(PL_comppad_name, offset, namesv);
b5c19bd7 352 if (fake) {
dd2155a4 353 SvFAKE_on(namesv);
b5c19bd7
DM
354 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
355 "Pad addname: %ld \"%s\" FAKE\n", (long)offset, name));
356 }
dd2155a4 357 else {
ee6cee0c 358 /* not yet introduced */
9d6ce603 359 SvNV_set(namesv, (NV)PAD_MAX); /* min */
b19bbeda 360 SvIV_set(namesv, 0); /* max */
ee6cee0c 361
dd2155a4
DM
362 if (!PL_min_intro_pending)
363 PL_min_intro_pending = offset;
364 PL_max_intro_pending = offset;
b5c19bd7 365 /* if it's not a simple scalar, replace with an AV or HV */
f3548bdc
DM
366 /* XXX DAPM since slot has been allocated, replace
367 * av_store with PL_curpad[offset] ? */
dd2155a4
DM
368 if (*name == '@')
369 av_store(PL_comppad, offset, (SV*)newAV());
370 else if (*name == '%')
371 av_store(PL_comppad, offset, (SV*)newHV());
372 SvPADMY_on(PL_curpad[offset]);
b5c19bd7
DM
373 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
374 "Pad addname: %ld \"%s\" new lex=0x%"UVxf"\n",
375 (long)offset, name, PTR2UV(PL_curpad[offset])));
dd2155a4
DM
376 }
377
378 return offset;
379}
380
381
382
383
384/*
385=for apidoc pad_alloc
386
387Allocate a new my or tmp pad entry. For a my, simply push a null SV onto
388the end of PL_comppad, but for a tmp, scan the pad from PL_padix upwards
389for a slot which has no name and and no active value.
390
391=cut
392*/
393
394/* XXX DAPM integrate alloc(), add_name() and add_anon(),
395 * or at least rationalise ??? */
396
397
398PADOFFSET
399Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
400{
401 SV *sv;
402 I32 retval;
403
f3548bdc
DM
404 ASSERT_CURPAD_ACTIVE("pad_alloc");
405
dd2155a4
DM
406 if (AvARRAY(PL_comppad) != PL_curpad)
407 Perl_croak(aTHX_ "panic: pad_alloc");
408 if (PL_pad_reset_pending)
409 pad_reset();
410 if (tmptype & SVs_PADMY) {
235cc2e3 411 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
dd2155a4
DM
412 retval = AvFILLp(PL_comppad);
413 }
414 else {
53c1dcc0 415 SV ** const names = AvARRAY(PL_comppad_name);
e1ec3a88 416 const SSize_t names_fill = AvFILLp(PL_comppad_name);
dd2155a4
DM
417 for (;;) {
418 /*
419 * "foreach" index vars temporarily become aliases to non-"my"
420 * values. Thus we must skip, not just pad values that are
421 * marked as current pad values, but also those with names.
422 */
423 /* HVDS why copy to sv here? we don't seem to use it */
424 if (++PL_padix <= names_fill &&
425 (sv = names[PL_padix]) && sv != &PL_sv_undef)
426 continue;
427 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
428 if (!(SvFLAGS(sv) & (SVs_PADTMP | SVs_PADMY)) &&
429 !IS_PADGV(sv) && !IS_PADCONST(sv))
430 break;
431 }
432 retval = PL_padix;
433 }
434 SvFLAGS(sv) |= tmptype;
435 PL_curpad = AvARRAY(PL_comppad);
436
437 DEBUG_X(PerlIO_printf(Perl_debug_log,
438 "Pad 0x%"UVxf"[0x%"UVxf"] alloc: %ld for %s\n",
439 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long) retval,
440 PL_op_name[optype]));
fd0854ff
DM
441#ifdef DEBUG_LEAKING_SCALARS
442 sv->sv_debug_optype = optype;
443 sv->sv_debug_inpad = 1;
fd0854ff 444#endif
a212c8b5 445 return (PADOFFSET)retval;
dd2155a4
DM
446}
447
448/*
449=for apidoc pad_add_anon
450
451Add an anon code entry to the current compiling pad
452
453=cut
454*/
455
456PADOFFSET
457Perl_pad_add_anon(pTHX_ SV* sv, OPCODE op_type)
458{
459 PADOFFSET ix;
b64e5050 460 SV* const name = NEWSV(1106, 0);
dd2155a4
DM
461 sv_upgrade(name, SVt_PVNV);
462 sv_setpvn(name, "&", 1);
b19bbeda 463 SvIV_set(name, -1);
9d6ce603 464 SvNV_set(name, 1);
dd2155a4
DM
465 ix = pad_alloc(op_type, SVs_PADMY);
466 av_store(PL_comppad_name, ix, name);
f3548bdc 467 /* XXX DAPM use PL_curpad[] ? */
dd2155a4
DM
468 av_store(PL_comppad, ix, sv);
469 SvPADMY_on(sv);
7dafbf52
DM
470
471 /* to avoid ref loops, we never have parent + child referencing each
472 * other simultaneously */
473 if (CvOUTSIDE((CV*)sv)) {
474 assert(!CvWEAKOUTSIDE((CV*)sv));
475 CvWEAKOUTSIDE_on((CV*)sv);
476 SvREFCNT_dec(CvOUTSIDE((CV*)sv));
477 }
dd2155a4
DM
478 return ix;
479}
480
481
482
483/*
484=for apidoc pad_check_dup
485
486Check for duplicate declarations: report any of:
487 * a my in the current scope with the same name;
488 * an our (anywhere in the pad) with the same name and the same stash
489 as C<ourstash>
490C<is_our> indicates that the name to check is an 'our' declaration
491
492=cut
493*/
494
495/* XXX DAPM integrate this into pad_add_name ??? */
496
497void
e1ec3a88 498Perl_pad_check_dup(pTHX_ const char *name, bool is_our, const HV *ourstash)
dd2155a4 499{
53c1dcc0 500 SV **svp;
dd2155a4
DM
501 PADOFFSET top, off;
502
f3548bdc 503 ASSERT_CURPAD_ACTIVE("pad_check_dup");
dd2155a4
DM
504 if (!ckWARN(WARN_MISC) || AvFILLp(PL_comppad_name) < 0)
505 return; /* nothing to check */
506
507 svp = AvARRAY(PL_comppad_name);
508 top = AvFILLp(PL_comppad_name);
509 /* check the current scope */
510 /* XXX DAPM - why the (I32) cast - shouldn't we ensure they're the same
511 * type ? */
512 for (off = top; (I32)off > PL_comppad_name_floor; off--) {
53c1dcc0
AL
513 SV * const sv = svp[off];
514 if (sv
dd2155a4 515 && sv != &PL_sv_undef
ee6cee0c 516 && !SvFAKE(sv)
dd2155a4
DM
517 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
518 && (!is_our
519 || ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash))
b15aece3 520 && strEQ(name, SvPVX_const(sv)))
dd2155a4
DM
521 {
522 Perl_warner(aTHX_ packWARN(WARN_MISC),
523 "\"%s\" variable %s masks earlier declaration in same %s",
524 (is_our ? "our" : "my"),
525 name,
526 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
527 --off;
528 break;
529 }
530 }
531 /* check the rest of the pad */
532 if (is_our) {
533 do {
53c1dcc0
AL
534 SV * const sv = svp[off];
535 if (sv
dd2155a4 536 && sv != &PL_sv_undef
ee6cee0c 537 && !SvFAKE(sv)
dd2155a4
DM
538 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
539 && ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash)
b15aece3 540 && strEQ(name, SvPVX_const(sv)))
dd2155a4
DM
541 {
542 Perl_warner(aTHX_ packWARN(WARN_MISC),
543 "\"our\" variable %s redeclared", name);
544 Perl_warner(aTHX_ packWARN(WARN_MISC),
545 "\t(Did you mean \"local\" instead of \"our\"?)\n");
546 break;
547 }
548 } while ( off-- > 0 );
549 }
550}
551
552
dd2155a4
DM
553/*
554=for apidoc pad_findmy
555
556Given a lexical name, try to find its offset, first in the current pad,
557or failing that, in the pads of any lexically enclosing subs (including
558the complications introduced by eval). If the name is found in an outer pad,
559then a fake entry is added to the current pad.
560Returns the offset in the current pad, or NOT_IN_PAD on failure.
561
562=cut
563*/
564
565PADOFFSET
e1ec3a88 566Perl_pad_findmy(pTHX_ const char *name)
dd2155a4 567{
b5c19bd7
DM
568 SV *out_sv;
569 int out_flags;
929a0744 570 I32 offset;
e1ec3a88 571 const AV *nameav;
929a0744 572 SV **name_svp;
dd2155a4 573
929a0744 574 offset = pad_findlex(name, PL_compcv, PL_cop_seqmax, 1,
b5c19bd7 575 Null(SV**), &out_sv, &out_flags);
929a0744
DM
576 if (offset != NOT_IN_PAD)
577 return offset;
578
579 /* look for an our that's being introduced; this allows
580 * our $foo = 0 unless defined $foo;
581 * to not give a warning. (Yes, this is a hack) */
582
583 nameav = (AV*)AvARRAY(CvPADLIST(PL_compcv))[0];
584 name_svp = AvARRAY(nameav);
585 for (offset = AvFILLp(nameav); offset > 0; offset--) {
e1ec3a88 586 const SV *namesv = name_svp[offset];
929a0744
DM
587 if (namesv && namesv != &PL_sv_undef
588 && !SvFAKE(namesv)
589 && (SvFLAGS(namesv) & SVpad_OUR)
b15aece3 590 && strEQ(SvPVX_const(namesv), name)
4cf4a199 591 && U_32(SvNVX(namesv)) == PAD_MAX /* min */
929a0744
DM
592 )
593 return offset;
594 }
595 return NOT_IN_PAD;
dd2155a4
DM
596}
597
e1f795dc
RGS
598/*
599 * Returns the offset of a lexical $_, if there is one, at run time.
600 * Used by the UNDERBAR XS macro.
601 */
602
603PADOFFSET
29289021 604Perl_find_rundefsvoffset(pTHX)
e1f795dc
RGS
605{
606 SV *out_sv;
607 int out_flags;
608 return pad_findlex("$_", find_runcv(NULL), PL_curcop->cop_seq, 1,
609 Null(SV**), &out_sv, &out_flags);
610}
dd2155a4 611
dd2155a4
DM
612/*
613=for apidoc pad_findlex
614
615Find a named lexical anywhere in a chain of nested pads. Add fake entries
b5c19bd7
DM
616in the inner pads if it's found in an outer one.
617
618Returns the offset in the bottom pad of the lex or the fake lex.
619cv is the CV in which to start the search, and seq is the current cop_seq
620to match against. If warn is true, print appropriate warnings. The out_*
621vars return values, and so are pointers to where the returned values
622should be stored. out_capture, if non-null, requests that the innermost
623instance of the lexical is captured; out_name_sv is set to the innermost
624matched namesv or fake namesv; out_flags returns the flags normally
625associated with the IVX field of a fake namesv.
626
627Note that pad_findlex() is recursive; it recurses up the chain of CVs,
628then comes back down, adding fake entries as it goes. It has to be this way
629because fake namesvs in anon protoypes have to store in NVX the index into
630the parent pad.
dd2155a4
DM
631
632=cut
633*/
634
b5c19bd7
DM
635/* Flags set in the SvIVX field of FAKE namesvs */
636
637#define PAD_FAKELEX_ANON 1 /* the lex is declared in an ANON, or ... */
638#define PAD_FAKELEX_MULTI 2 /* the lex can be instantiated multiple times */
639
640/* the CV has finished being compiled. This is not a sufficient test for
641 * all CVs (eg XSUBs), but suffices for the CVs found in a lexical chain */
642#define CvCOMPILED(cv) CvROOT(cv)
643
71f882da
DM
644/* the CV does late binding of its lexicals */
645#define CvLATE(cv) (CvANON(cv) || SvTYPE(cv) == SVt_PVFM)
646
b5c19bd7 647
dd2155a4 648STATIC PADOFFSET
e1ec3a88 649S_pad_findlex(pTHX_ const char *name, const CV* cv, U32 seq, int warn,
b5c19bd7 650 SV** out_capture, SV** out_name_sv, int *out_flags)
dd2155a4 651{
b5c19bd7
DM
652 I32 offset, new_offset;
653 SV *new_capture;
654 SV **new_capturep;
b64e5050 655 const AV * const padlist = CvPADLIST(cv);
dd2155a4 656
b5c19bd7 657 *out_flags = 0;
a3985cdc 658
b5c19bd7
DM
659 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
660 "Pad findlex cv=0x%"UVxf" searching \"%s\" seq=%d%s\n",
661 PTR2UV(cv), name, (int)seq, out_capture ? " capturing" : "" ));
dd2155a4 662
b5c19bd7 663 /* first, search this pad */
dd2155a4 664
b5c19bd7
DM
665 if (padlist) { /* not an undef CV */
666 I32 fake_offset = 0;
e1ec3a88 667 const AV *nameav = (AV*)AvARRAY(padlist)[0];
b5c19bd7 668 SV **name_svp = AvARRAY(nameav);
ee6cee0c 669
b5c19bd7 670 for (offset = AvFILLp(nameav); offset > 0; offset--) {
e1ec3a88 671 const SV *namesv = name_svp[offset];
b5c19bd7 672 if (namesv && namesv != &PL_sv_undef
b15aece3 673 && strEQ(SvPVX_const(namesv), name))
b5c19bd7
DM
674 {
675 if (SvFAKE(namesv))
676 fake_offset = offset; /* in case we don't find a real one */
4cf4a199
JH
677 else if ( seq > U_32(SvNVX(namesv)) /* min */
678 && seq <= (U32)SvIVX(namesv)) /* max */
b5c19bd7 679 break;
ee6cee0c
DM
680 }
681 }
682
b5c19bd7
DM
683 if (offset > 0 || fake_offset > 0 ) { /* a match! */
684 if (offset > 0) { /* not fake */
685 fake_offset = 0;
686 *out_name_sv = name_svp[offset]; /* return the namesv */
687
688 /* set PAD_FAKELEX_MULTI if this lex can have multiple
689 * instances. For now, we just test !CvUNIQUE(cv), but
690 * ideally, we should detect my's declared within loops
691 * etc - this would allow a wider range of 'not stayed
692 * shared' warnings. We also treated alreadly-compiled
693 * lexes as not multi as viewed from evals. */
694
695 *out_flags = CvANON(cv) ?
696 PAD_FAKELEX_ANON :
697 (!CvUNIQUE(cv) && ! CvCOMPILED(cv))
698 ? PAD_FAKELEX_MULTI : 0;
699
700 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
701 "Pad findlex cv=0x%"UVxf" matched: offset=%ld (%ld,%ld)\n",
4cf4a199 702 PTR2UV(cv), (long)offset, (long)U_32(SvNVX(*out_name_sv)),
b5c19bd7
DM
703 (long)SvIVX(*out_name_sv)));
704 }
705 else { /* fake match */
706 offset = fake_offset;
707 *out_name_sv = name_svp[offset]; /* return the namesv */
708 *out_flags = SvIVX(*out_name_sv);
709 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
19a5c512 710 "Pad findlex cv=0x%"UVxf" matched: offset=%ld flags=0x%lx index=%lu\n",
b5c19bd7
DM
711 PTR2UV(cv), (long)offset, (unsigned long)*out_flags,
712 (unsigned long)SvNVX(*out_name_sv)
713 ));
714 }
dd2155a4 715
b5c19bd7 716 /* return the lex? */
dd2155a4 717
b5c19bd7 718 if (out_capture) {
dd2155a4 719
b5c19bd7
DM
720 /* our ? */
721 if ((SvFLAGS(*out_name_sv) & SVpad_OUR)) {
722 *out_capture = Nullsv;
723 return offset;
724 }
ee6cee0c 725
b5c19bd7
DM
726 /* trying to capture from an anon prototype? */
727 if (CvCOMPILED(cv)
728 ? CvANON(cv) && CvCLONE(cv) && !CvCLONED(cv)
729 : *out_flags & PAD_FAKELEX_ANON)
730 {
731 if (warn && ckWARN(WARN_CLOSURE))
732 Perl_warner(aTHX_ packWARN(WARN_CLOSURE),
733 "Variable \"%s\" is not available", name);
734 *out_capture = Nullsv;
735 }
ee6cee0c 736
b5c19bd7
DM
737 /* real value */
738 else {
739 int newwarn = warn;
740 if (!CvCOMPILED(cv) && (*out_flags & PAD_FAKELEX_MULTI)
741 && warn && ckWARN(WARN_CLOSURE)) {
742 newwarn = 0;
743 Perl_warner(aTHX_ packWARN(WARN_CLOSURE),
744 "Variable \"%s\" will not stay shared", name);
745 }
dd2155a4 746
b5c19bd7
DM
747 if (fake_offset && CvANON(cv)
748 && CvCLONE(cv) &&!CvCLONED(cv))
749 {
750 SV *n;
751 /* not yet caught - look further up */
752 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
753 "Pad findlex cv=0x%"UVxf" chasing lex in outer pad\n",
754 PTR2UV(cv)));
755 n = *out_name_sv;
756 pad_findlex(name, CvOUTSIDE(cv), CvOUTSIDE_SEQ(cv),
757 newwarn, out_capture, out_name_sv, out_flags);
758 *out_name_sv = n;
759 return offset;
dd2155a4 760 }
b5c19bd7
DM
761
762 *out_capture = AvARRAY((AV*)AvARRAY(padlist)[
763 CvDEPTH(cv) ? CvDEPTH(cv) : 1])[offset];
764 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
765 "Pad findlex cv=0x%"UVxf" found lex=0x%"UVxf"\n",
19a5c512 766 PTR2UV(cv), PTR2UV(*out_capture)));
b5c19bd7
DM
767
768 if (SvPADSTALE(*out_capture)) {
769 if (ckWARN(WARN_CLOSURE))
ee6cee0c 770 Perl_warner(aTHX_ packWARN(WARN_CLOSURE),
b5c19bd7
DM
771 "Variable \"%s\" is not available", name);
772 *out_capture = Nullsv;
dd2155a4
DM
773 }
774 }
b5c19bd7
DM
775 if (!*out_capture) {
776 if (*name == '@')
777 *out_capture = sv_2mortal((SV*)newAV());
778 else if (*name == '%')
779 *out_capture = sv_2mortal((SV*)newHV());
780 else
781 *out_capture = sv_newmortal();
782 }
dd2155a4 783 }
b5c19bd7
DM
784
785 return offset;
ee6cee0c 786 }
b5c19bd7
DM
787 }
788
789 /* it's not in this pad - try above */
790
791 if (!CvOUTSIDE(cv))
792 return NOT_IN_PAD;
793
794 /* out_capture non-null means caller wants us to capture lex; in
71f882da 795 * addition we capture ourselves unless it's an ANON/format */
b5c19bd7 796 new_capturep = out_capture ? out_capture :
71f882da 797 CvLATE(cv) ? Null(SV**) : &new_capture;
b5c19bd7
DM
798
799 offset = pad_findlex(name, CvOUTSIDE(cv), CvOUTSIDE_SEQ(cv), 1,
800 new_capturep, out_name_sv, out_flags);
801 if (offset == NOT_IN_PAD)
802 return NOT_IN_PAD;
803
804 /* found in an outer CV. Add appropriate fake entry to this pad */
805
806 /* don't add new fake entries (via eval) to CVs that we have already
807 * finished compiling, or to undef CVs */
808 if (CvCOMPILED(cv) || !padlist)
809 return 0; /* this dummy (and invalid) value isnt used by the caller */
810
811 {
812 SV *new_namesv;
53c1dcc0
AL
813 AV * const ocomppad_name = PL_comppad_name;
814 PAD * const ocomppad = PL_comppad;
b5c19bd7
DM
815 PL_comppad_name = (AV*)AvARRAY(padlist)[0];
816 PL_comppad = (AV*)AvARRAY(padlist)[1];
817 PL_curpad = AvARRAY(PL_comppad);
818
819 new_offset = pad_add_name(
b15aece3 820 SvPVX_const(*out_name_sv),
b5c19bd7
DM
821 (SvFLAGS(*out_name_sv) & SVpad_TYPED)
822 ? SvSTASH(*out_name_sv) : Nullhv,
823 (SvFLAGS(*out_name_sv) & SVpad_OUR)
824 ? GvSTASH(*out_name_sv) : Nullhv,
825 1 /* fake */
826 );
827
828 new_namesv = AvARRAY(PL_comppad_name)[new_offset];
b19bbeda 829 SvIV_set(new_namesv, *out_flags);
b5c19bd7 830
9d6ce603 831 SvNV_set(new_namesv, (NV)0);
b5c19bd7
DM
832 if (SvFLAGS(new_namesv) & SVpad_OUR) {
833 /* do nothing */
834 }
71f882da 835 else if (CvLATE(cv)) {
b5c19bd7 836 /* delayed creation - just note the offset within parent pad */
9d6ce603 837 SvNV_set(new_namesv, (NV)offset);
b5c19bd7
DM
838 CvCLONE_on(cv);
839 }
840 else {
841 /* immediate creation - capture outer value right now */
842 av_store(PL_comppad, new_offset, SvREFCNT_inc(*new_capturep));
843 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
844 "Pad findlex cv=0x%"UVxf" saved captured sv 0x%"UVxf" at offset %ld\n",
845 PTR2UV(cv), PTR2UV(*new_capturep), (long)new_offset));
dd2155a4 846 }
b5c19bd7
DM
847 *out_name_sv = new_namesv;
848 *out_flags = SvIVX(new_namesv);
849
850 PL_comppad_name = ocomppad_name;
851 PL_comppad = ocomppad;
852 PL_curpad = ocomppad ? AvARRAY(ocomppad) : Null(SV **);
dd2155a4 853 }
b5c19bd7 854 return new_offset;
dd2155a4
DM
855}
856
b5c19bd7 857
dd2155a4
DM
858/*
859=for apidoc pad_sv
860
861Get the value at offset po in the current pad.
862Use macro PAD_SV instead of calling this function directly.
863
864=cut
865*/
866
867
868SV *
869Perl_pad_sv(pTHX_ PADOFFSET po)
870{
f3548bdc 871 ASSERT_CURPAD_ACTIVE("pad_sv");
dd2155a4 872
dd2155a4
DM
873 if (!po)
874 Perl_croak(aTHX_ "panic: pad_sv po");
dd2155a4
DM
875 DEBUG_X(PerlIO_printf(Perl_debug_log,
876 "Pad 0x%"UVxf"[0x%"UVxf"] sv: %ld sv=0x%"UVxf"\n",
f3548bdc 877 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(PL_curpad[po]))
dd2155a4
DM
878 );
879 return PL_curpad[po];
880}
881
882
883/*
884=for apidoc pad_setsv
885
886Set the entry at offset po in the current pad to sv.
887Use the macro PAD_SETSV() rather than calling this function directly.
888
889=cut
890*/
891
892#ifdef DEBUGGING
893void
894Perl_pad_setsv(pTHX_ PADOFFSET po, SV* sv)
895{
f3548bdc 896 ASSERT_CURPAD_ACTIVE("pad_setsv");
dd2155a4
DM
897
898 DEBUG_X(PerlIO_printf(Perl_debug_log,
899 "Pad 0x%"UVxf"[0x%"UVxf"] setsv: %ld sv=0x%"UVxf"\n",
f3548bdc 900 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(sv))
dd2155a4
DM
901 );
902 PL_curpad[po] = sv;
903}
904#endif
905
906
907
908/*
909=for apidoc pad_block_start
910
911Update the pad compilation state variables on entry to a new block
912
913=cut
914*/
915
916/* XXX DAPM perhaps:
917 * - integrate this in general state-saving routine ???
918 * - combine with the state-saving going on in pad_new ???
919 * - introduce a new SAVE type that does all this in one go ?
920 */
921
922void
923Perl_pad_block_start(pTHX_ int full)
924{
f3548bdc 925 ASSERT_CURPAD_ACTIVE("pad_block_start");
dd2155a4
DM
926 SAVEI32(PL_comppad_name_floor);
927 PL_comppad_name_floor = AvFILLp(PL_comppad_name);
928 if (full)
929 PL_comppad_name_fill = PL_comppad_name_floor;
930 if (PL_comppad_name_floor < 0)
931 PL_comppad_name_floor = 0;
932 SAVEI32(PL_min_intro_pending);
933 SAVEI32(PL_max_intro_pending);
934 PL_min_intro_pending = 0;
935 SAVEI32(PL_comppad_name_fill);
936 SAVEI32(PL_padix_floor);
937 PL_padix_floor = PL_padix;
938 PL_pad_reset_pending = FALSE;
939}
940
941
942/*
943=for apidoc intro_my
944
945"Introduce" my variables to visible status.
946
947=cut
948*/
949
950U32
951Perl_intro_my(pTHX)
952{
953 SV **svp;
dd2155a4
DM
954 I32 i;
955
f3548bdc 956 ASSERT_CURPAD_ACTIVE("intro_my");
dd2155a4
DM
957 if (! PL_min_intro_pending)
958 return PL_cop_seqmax;
959
960 svp = AvARRAY(PL_comppad_name);
961 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
53c1dcc0
AL
962 SV * const sv = svp[i];
963
964 if (sv && sv != &PL_sv_undef && !SvFAKE(sv) && !SvIVX(sv)) {
b19bbeda 965 SvIV_set(sv, PAD_MAX); /* Don't know scope end yet. */
9d6ce603 966 SvNV_set(sv, (NV)PL_cop_seqmax);
dd2155a4 967 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
b5c19bd7 968 "Pad intromy: %ld \"%s\", (%ld,%ld)\n",
b15aece3 969 (long)i, SvPVX_const(sv),
4cf4a199 970 (long)U_32(SvNVX(sv)), (long)SvIVX(sv))
dd2155a4
DM
971 );
972 }
973 }
974 PL_min_intro_pending = 0;
975 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
976 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
977 "Pad intromy: seq -> %ld\n", (long)(PL_cop_seqmax+1)));
978
979 return PL_cop_seqmax++;
980}
981
982/*
983=for apidoc pad_leavemy
984
985Cleanup at end of scope during compilation: set the max seq number for
986lexicals in this scope and warn of any lexicals that never got introduced.
987
988=cut
989*/
990
991void
992Perl_pad_leavemy(pTHX)
993{
994 I32 off;
53c1dcc0 995 SV ** const svp = AvARRAY(PL_comppad_name);
dd2155a4
DM
996
997 PL_pad_reset_pending = FALSE;
998
f3548bdc 999 ASSERT_CURPAD_ACTIVE("pad_leavemy");
dd2155a4
DM
1000 if (PL_min_intro_pending && PL_comppad_name_fill < PL_min_intro_pending) {
1001 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
53c1dcc0
AL
1002 const SV * const sv = svp[off];
1003 if (sv && sv != &PL_sv_undef
ee6cee0c 1004 && !SvFAKE(sv) && ckWARN_d(WARN_INTERNAL))
dd2155a4 1005 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
35c1215d 1006 "%"SVf" never introduced", sv);
dd2155a4
DM
1007 }
1008 }
1009 /* "Deintroduce" my variables that are leaving with this scope. */
1010 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_fill; off--) {
53c1dcc0
AL
1011 const SV * const sv = svp[off];
1012 if (sv && sv != &PL_sv_undef && !SvFAKE(sv) && SvIVX(sv) == PAD_MAX) {
b19bbeda 1013 SvIV_set(sv, PL_cop_seqmax);
dd2155a4 1014 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
b5c19bd7 1015 "Pad leavemy: %ld \"%s\", (%ld,%ld)\n",
b15aece3 1016 (long)off, SvPVX_const(sv),
4cf4a199 1017 (long)U_32(SvNVX(sv)), (long)SvIVX(sv))
dd2155a4
DM
1018 );
1019 }
1020 }
1021 PL_cop_seqmax++;
1022 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
1023 "Pad leavemy: seq = %ld\n", (long)PL_cop_seqmax));
1024}
1025
1026
1027/*
1028=for apidoc pad_swipe
1029
1030Abandon the tmp in the current pad at offset po and replace with a
1031new one.
1032
1033=cut
1034*/
1035
1036void
1037Perl_pad_swipe(pTHX_ PADOFFSET po, bool refadjust)
1038{
f3548bdc 1039 ASSERT_CURPAD_LEGAL("pad_swipe");
dd2155a4
DM
1040 if (!PL_curpad)
1041 return;
1042 if (AvARRAY(PL_comppad) != PL_curpad)
1043 Perl_croak(aTHX_ "panic: pad_swipe curpad");
1044 if (!po)
1045 Perl_croak(aTHX_ "panic: pad_swipe po");
1046
1047 DEBUG_X(PerlIO_printf(Perl_debug_log,
1048 "Pad 0x%"UVxf"[0x%"UVxf"] swipe: %ld\n",
1049 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po));
1050
1051 if (PL_curpad[po])
1052 SvPADTMP_off(PL_curpad[po]);
1053 if (refadjust)
1054 SvREFCNT_dec(PL_curpad[po]);
1055
9ad9869c
DM
1056
1057 /* if pad tmps aren't shared between ops, then there's no need to
1058 * create a new tmp when an existing op is freed */
1059#ifdef USE_BROKEN_PAD_RESET
dd2155a4
DM
1060 PL_curpad[po] = NEWSV(1107,0);
1061 SvPADTMP_on(PL_curpad[po]);
9ad9869c
DM
1062#else
1063 PL_curpad[po] = &PL_sv_undef;
97bf4a8d 1064#endif
dd2155a4
DM
1065 if ((I32)po < PL_padix)
1066 PL_padix = po - 1;
1067}
1068
1069
1070/*
1071=for apidoc pad_reset
1072
1073Mark all the current temporaries for reuse
1074
1075=cut
1076*/
1077
1078/* XXX pad_reset() is currently disabled because it results in serious bugs.
1079 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
1080 * on the stack by OPs that use them, there are several ways to get an alias
1081 * to a shared TARG. Such an alias will change randomly and unpredictably.
1082 * We avoid doing this until we can think of a Better Way.
1083 * GSAR 97-10-29 */
1084void
1085Perl_pad_reset(pTHX)
1086{
1087#ifdef USE_BROKEN_PAD_RESET
dd2155a4
DM
1088 if (AvARRAY(PL_comppad) != PL_curpad)
1089 Perl_croak(aTHX_ "panic: pad_reset curpad");
1090
1091 DEBUG_X(PerlIO_printf(Perl_debug_log,
1092 "Pad 0x%"UVxf"[0x%"UVxf"] reset: padix %ld -> %ld",
1093 PTR2UV(PL_comppad), PTR2UV(PL_curpad),
1094 (long)PL_padix, (long)PL_padix_floor
1095 )
1096 );
1097
1098 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
e1ec3a88 1099 register I32 po;
dd2155a4
DM
1100 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
1101 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
1102 SvPADTMP_off(PL_curpad[po]);
1103 }
1104 PL_padix = PL_padix_floor;
1105 }
1106#endif
1107 PL_pad_reset_pending = FALSE;
1108}
1109
1110
1111/*
1112=for apidoc pad_tidy
1113
1114Tidy up a pad after we've finished compiling it:
1115 * remove most stuff from the pads of anonsub prototypes;
1116 * give it a @_;
1117 * mark tmps as such.
1118
1119=cut
1120*/
1121
1122/* XXX DAPM surely most of this stuff should be done properly
1123 * at the right time beforehand, rather than going around afterwards
1124 * cleaning up our mistakes ???
1125 */
1126
1127void
1128Perl_pad_tidy(pTHX_ padtidy_type type)
1129{
27da23d5 1130 dVAR;
dd2155a4 1131
f3548bdc 1132 ASSERT_CURPAD_ACTIVE("pad_tidy");
b5c19bd7
DM
1133
1134 /* If this CV has had any 'eval-capable' ops planted in it
1135 * (ie it contains eval '...', //ee, /$var/ or /(?{..})/), Then any
1136 * anon prototypes in the chain of CVs should be marked as cloneable,
1137 * so that for example the eval's CV in C<< sub { eval '$x' } >> gets
1138 * the right CvOUTSIDE.
1139 * If running with -d, *any* sub may potentially have an eval
1140 * excuted within it.
1141 */
1142
1143 if (PL_cv_has_eval || PL_perldb) {
e1ec3a88 1144 const CV *cv;
b5c19bd7
DM
1145 for (cv = PL_compcv ;cv; cv = CvOUTSIDE(cv)) {
1146 if (cv != PL_compcv && CvCOMPILED(cv))
1147 break; /* no need to mark already-compiled code */
1148 if (CvANON(cv)) {
1149 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
1150 "Pad clone on cv=0x%"UVxf"\n", PTR2UV(cv)));
1151 CvCLONE_on(cv);
1152 }
1153 }
1154 }
1155
dd2155a4
DM
1156 /* extend curpad to match namepad */
1157 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
1158 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
1159
1160 if (type == padtidy_SUBCLONE) {
53c1dcc0 1161 SV ** const namep = AvARRAY(PL_comppad_name);
504618e9 1162 PADOFFSET ix;
b5c19bd7 1163
dd2155a4
DM
1164 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1165 SV *namesv;
1166
1167 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
1168 continue;
1169 /*
1170 * The only things that a clonable function needs in its
b5c19bd7 1171 * pad are anonymous subs.
dd2155a4
DM
1172 * The rest are created anew during cloning.
1173 */
1174 if (!((namesv = namep[ix]) != Nullsv &&
1175 namesv != &PL_sv_undef &&
b15aece3 1176 *SvPVX_const(namesv) == '&'))
dd2155a4
DM
1177 {
1178 SvREFCNT_dec(PL_curpad[ix]);
1179 PL_curpad[ix] = Nullsv;
1180 }
1181 }
1182 }
1183 else if (type == padtidy_SUB) {
1184 /* XXX DAPM this same bit of code keeps appearing !!! Rationalise? */
53c1dcc0 1185 AV * const av = newAV(); /* Will be @_ */
dd2155a4
DM
1186 av_extend(av, 0);
1187 av_store(PL_comppad, 0, (SV*)av);
11ca45c0 1188 AvREIFY_only(av);
dd2155a4
DM
1189 }
1190
1191 /* XXX DAPM rationalise these two similar branches */
1192
1193 if (type == padtidy_SUB) {
504618e9 1194 PADOFFSET ix;
dd2155a4
DM
1195 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1196 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
1197 continue;
1198 if (!SvPADMY(PL_curpad[ix]))
1199 SvPADTMP_on(PL_curpad[ix]);
1200 }
1201 }
1202 else if (type == padtidy_FORMAT) {
504618e9 1203 PADOFFSET ix;
dd2155a4
DM
1204 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1205 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
1206 SvPADTMP_on(PL_curpad[ix]);
1207 }
1208 }
f3548bdc 1209 PL_curpad = AvARRAY(PL_comppad);
dd2155a4
DM
1210}
1211
1212
1213/*
1214=for apidoc pad_free
1215
1216Free the SV at offet po in the current pad.
1217
1218=cut
1219*/
1220
1221/* XXX DAPM integrate with pad_swipe ???? */
1222void
1223Perl_pad_free(pTHX_ PADOFFSET po)
1224{
f3548bdc 1225 ASSERT_CURPAD_LEGAL("pad_free");
dd2155a4
DM
1226 if (!PL_curpad)
1227 return;
1228 if (AvARRAY(PL_comppad) != PL_curpad)
1229 Perl_croak(aTHX_ "panic: pad_free curpad");
1230 if (!po)
1231 Perl_croak(aTHX_ "panic: pad_free po");
1232
1233 DEBUG_X(PerlIO_printf(Perl_debug_log,
1234 "Pad 0x%"UVxf"[0x%"UVxf"] free: %ld\n",
1235 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po)
1236 );
1237
1238 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) {
1239 SvPADTMP_off(PL_curpad[po]);
1240#ifdef USE_ITHREADS
7e736055
HS
1241 /* SV could be a shared hash key (eg bugid #19022) */
1242 if (
f8c7b90f 1243#ifdef PERL_OLD_COPY_ON_WRITE
7e736055
HS
1244 !SvIsCOW(PL_curpad[po])
1245#else
1246 !SvFAKE(PL_curpad[po])
dd2155a4 1247#endif
7e736055 1248 )
dd2155a4 1249 SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */
dd2155a4
DM
1250#endif
1251 }
1252 if ((I32)po < PL_padix)
1253 PL_padix = po - 1;
1254}
1255
1256
1257
1258/*
1259=for apidoc do_dump_pad
1260
1261Dump the contents of a padlist
1262
1263=cut
1264*/
1265
1266void
1267Perl_do_dump_pad(pTHX_ I32 level, PerlIO *file, PADLIST *padlist, int full)
1268{
e1ec3a88
AL
1269 const AV *pad_name;
1270 const AV *pad;
dd2155a4
DM
1271 SV **pname;
1272 SV **ppad;
dd2155a4
DM
1273 I32 ix;
1274
1275 if (!padlist) {
1276 return;
1277 }
1278 pad_name = (AV*)*av_fetch((AV*)padlist, 0, FALSE);
1279 pad = (AV*)*av_fetch((AV*)padlist, 1, FALSE);
1280 pname = AvARRAY(pad_name);
1281 ppad = AvARRAY(pad);
1282 Perl_dump_indent(aTHX_ level, file,
1283 "PADNAME = 0x%"UVxf"(0x%"UVxf") PAD = 0x%"UVxf"(0x%"UVxf")\n",
1284 PTR2UV(pad_name), PTR2UV(pname), PTR2UV(pad), PTR2UV(ppad)
1285 );
1286
1287 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
e1ec3a88 1288 const SV *namesv = pname[ix];
dd2155a4
DM
1289 if (namesv && namesv == &PL_sv_undef) {
1290 namesv = Nullsv;
1291 }
1292 if (namesv) {
ee6cee0c
DM
1293 if (SvFAKE(namesv))
1294 Perl_dump_indent(aTHX_ level+1, file,
c0fd1b42 1295 "%2d. 0x%"UVxf"<%lu> FAKE \"%s\" flags=0x%lx index=%lu\n",
ee6cee0c
DM
1296 (int) ix,
1297 PTR2UV(ppad[ix]),
1298 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0),
b15aece3 1299 SvPVX_const(namesv),
b5c19bd7
DM
1300 (unsigned long)SvIVX(namesv),
1301 (unsigned long)SvNVX(namesv)
1302
ee6cee0c
DM
1303 );
1304 else
1305 Perl_dump_indent(aTHX_ level+1, file,
b5c19bd7 1306 "%2d. 0x%"UVxf"<%lu> (%ld,%ld) \"%s\"\n",
ee6cee0c
DM
1307 (int) ix,
1308 PTR2UV(ppad[ix]),
1309 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0),
4cf4a199 1310 (long)U_32(SvNVX(namesv)),
b5c19bd7 1311 (long)SvIVX(namesv),
b15aece3 1312 SvPVX_const(namesv)
ee6cee0c 1313 );
dd2155a4
DM
1314 }
1315 else if (full) {
1316 Perl_dump_indent(aTHX_ level+1, file,
1317 "%2d. 0x%"UVxf"<%lu>\n",
1318 (int) ix,
1319 PTR2UV(ppad[ix]),
1320 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0)
1321 );
1322 }
1323 }
1324}
1325
1326
1327
1328/*
1329=for apidoc cv_dump
1330
1331dump the contents of a CV
1332
1333=cut
1334*/
1335
1336#ifdef DEBUGGING
1337STATIC void
e1ec3a88 1338S_cv_dump(pTHX_ const CV *cv, const char *title)
dd2155a4 1339{
53c1dcc0
AL
1340 const CV * const outside = CvOUTSIDE(cv);
1341 AV* const padlist = CvPADLIST(cv);
dd2155a4
DM
1342
1343 PerlIO_printf(Perl_debug_log,
1344 " %s: CV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
1345 title,
1346 PTR2UV(cv),
1347 (CvANON(cv) ? "ANON"
71f882da 1348 : (SvTYPE(cv) == SVt_PVFM) ? "FORMAT"
dd2155a4
DM
1349 : (cv == PL_main_cv) ? "MAIN"
1350 : CvUNIQUE(cv) ? "UNIQUE"
1351 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
1352 PTR2UV(outside),
1353 (!outside ? "null"
1354 : CvANON(outside) ? "ANON"
1355 : (outside == PL_main_cv) ? "MAIN"
1356 : CvUNIQUE(outside) ? "UNIQUE"
1357 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
1358
1359 PerlIO_printf(Perl_debug_log,
1360 " PADLIST = 0x%"UVxf"\n", PTR2UV(padlist));
1361 do_dump_pad(1, Perl_debug_log, padlist, 1);
1362}
1363#endif /* DEBUGGING */
1364
1365
1366
1367
1368
1369/*
1370=for apidoc cv_clone
1371
1372Clone a CV: make a new CV which points to the same code etc, but which
1373has a newly-created pad built by copying the prototype pad and capturing
1374any outer lexicals.
1375
1376=cut
1377*/
1378
1379CV *
1380Perl_cv_clone(pTHX_ CV *proto)
1381{
27da23d5 1382 dVAR;
dd2155a4 1383 I32 ix;
53c1dcc0
AL
1384 AV* const protopadlist = CvPADLIST(proto);
1385 const AV* const protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
1386 const AV* const protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
1387 SV** const pname = AvARRAY(protopad_name);
1388 SV** const ppad = AvARRAY(protopad);
e1ec3a88
AL
1389 const I32 fname = AvFILLp(protopad_name);
1390 const I32 fpad = AvFILLp(protopad);
dd2155a4 1391 CV* cv;
b5c19bd7
DM
1392 SV** outpad;
1393 CV* outside;
71f882da 1394 long depth;
dd2155a4
DM
1395
1396 assert(!CvUNIQUE(proto));
1397
71f882da
DM
1398 /* Since cloneable anon subs can be nested, CvOUTSIDE may point
1399 * to a prototype; we instead want the cloned parent who called us.
1400 * Note that in general for formats, CvOUTSIDE != find_runcv */
1401
1402 outside = CvOUTSIDE(proto);
1403 if (outside && CvCLONE(outside) && ! CvCLONED(outside))
1404 outside = find_runcv(NULL);
1405 depth = CvDEPTH(outside);
1406 assert(depth || SvTYPE(proto) == SVt_PVFM);
1407 if (!depth)
1408 depth = 1;
b5c19bd7
DM
1409 assert(CvPADLIST(outside));
1410
dd2155a4
DM
1411 ENTER;
1412 SAVESPTR(PL_compcv);
1413
1414 cv = PL_compcv = (CV*)NEWSV(1104, 0);
1415 sv_upgrade((SV *)cv, SvTYPE(proto));
7dafbf52 1416 CvFLAGS(cv) = CvFLAGS(proto) & ~(CVf_CLONE|CVf_WEAKOUTSIDE);
dd2155a4
DM
1417 CvCLONED_on(cv);
1418
dd2155a4
DM
1419#ifdef USE_ITHREADS
1420 CvFILE(cv) = CvXSUB(proto) ? CvFILE(proto)
1421 : savepv(CvFILE(proto));
1422#else
1423 CvFILE(cv) = CvFILE(proto);
1424#endif
1425 CvGV(cv) = CvGV(proto);
1426 CvSTASH(cv) = CvSTASH(proto);
b34c0dd4 1427 OP_REFCNT_LOCK;
dd2155a4 1428 CvROOT(cv) = OpREFCNT_inc(CvROOT(proto));
b34c0dd4 1429 OP_REFCNT_UNLOCK;
dd2155a4 1430 CvSTART(cv) = CvSTART(proto);
b5c19bd7
DM
1431 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
1432 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(proto);
dd2155a4
DM
1433
1434 if (SvPOK(proto))
b15aece3 1435 sv_setpvn((SV*)cv, SvPVX_const(proto), SvCUR(proto));
dd2155a4 1436
b7787f18 1437 CvPADLIST(cv) = pad_new(padnew_CLONE|padnew_SAVE);
dd2155a4 1438
b5c19bd7 1439 av_fill(PL_comppad, fpad);
dd2155a4
DM
1440 for (ix = fname; ix >= 0; ix--)
1441 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
1442
dd2155a4
DM
1443 PL_curpad = AvARRAY(PL_comppad);
1444
71f882da 1445 outpad = AvARRAY(AvARRAY(CvPADLIST(outside))[depth]);
b5c19bd7 1446
dd2155a4 1447 for (ix = fpad; ix > 0; ix--) {
53c1dcc0 1448 SV* const namesv = (ix <= fname) ? pname[ix] : Nullsv;
71f882da
DM
1449 SV *sv = Nullsv;
1450 if (namesv && namesv != &PL_sv_undef) { /* lexical */
b5c19bd7 1451 if (SvFAKE(namesv)) { /* lexical from outside? */
71f882da
DM
1452 sv = outpad[(I32)SvNVX(namesv)];
1453 assert(sv);
1454 /* formats may have an inactive parent */
1455 if (SvTYPE(proto) == SVt_PVFM && SvPADSTALE(sv)) {
1456 if (ckWARN(WARN_CLOSURE))
1457 Perl_warner(aTHX_ packWARN(WARN_CLOSURE),
b15aece3 1458 "Variable \"%s\" is not available", SvPVX_const(namesv));
71f882da
DM
1459 sv = Nullsv;
1460 }
1461 else {
1462 assert(!SvPADSTALE(sv));
1463 sv = SvREFCNT_inc(sv);
1464 }
dd2155a4 1465 }
71f882da 1466 if (!sv) {
b15aece3 1467 const char sigil = SvPVX_const(namesv)[0];
e1ec3a88 1468 if (sigil == '&')
dd2155a4 1469 sv = SvREFCNT_inc(ppad[ix]);
e1ec3a88 1470 else if (sigil == '@')
dd2155a4 1471 sv = (SV*)newAV();
e1ec3a88 1472 else if (sigil == '%')
dd2155a4
DM
1473 sv = (SV*)newHV();
1474 else
1475 sv = NEWSV(0, 0);
235cc2e3 1476 SvPADMY_on(sv);
dd2155a4
DM
1477 }
1478 }
1479 else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) {
71f882da 1480 sv = SvREFCNT_inc(ppad[ix]);
dd2155a4
DM
1481 }
1482 else {
b5c19bd7 1483 sv = NEWSV(0, 0);
dd2155a4 1484 SvPADTMP_on(sv);
dd2155a4 1485 }
71f882da 1486 PL_curpad[ix] = sv;
dd2155a4
DM
1487 }
1488
dd2155a4
DM
1489 DEBUG_Xv(
1490 PerlIO_printf(Perl_debug_log, "\nPad CV clone\n");
1491 cv_dump(outside, "Outside");
1492 cv_dump(proto, "Proto");
1493 cv_dump(cv, "To");
1494 );
1495
1496 LEAVE;
1497
1498 if (CvCONST(cv)) {
b5c19bd7
DM
1499 /* Constant sub () { $x } closing over $x - see lib/constant.pm:
1500 * The prototype was marked as a candiate for const-ization,
1501 * so try to grab the current const value, and if successful,
1502 * turn into a const sub:
1503 */
dd2155a4 1504 SV* const_sv = op_const_sv(CvSTART(cv), cv);
b5c19bd7
DM
1505 if (const_sv) {
1506 SvREFCNT_dec(cv);
1507 cv = newCONSTSUB(CvSTASH(proto), 0, const_sv);
1508 }
1509 else {
1510 CvCONST_off(cv);
1511 }
dd2155a4
DM
1512 }
1513
1514 return cv;
1515}
1516
1517
1518/*
1519=for apidoc pad_fixup_inner_anons
1520
1521For any anon CVs in the pad, change CvOUTSIDE of that CV from
7dafbf52
DM
1522old_cv to new_cv if necessary. Needed when a newly-compiled CV has to be
1523moved to a pre-existing CV struct.
dd2155a4
DM
1524
1525=cut
1526*/
1527
1528void
1529Perl_pad_fixup_inner_anons(pTHX_ PADLIST *padlist, CV *old_cv, CV *new_cv)
1530{
1531 I32 ix;
66a1b24b
AL
1532 AV * const comppad_name = (AV*)AvARRAY(padlist)[0];
1533 AV * const comppad = (AV*)AvARRAY(padlist)[1];
53c1dcc0
AL
1534 SV ** const namepad = AvARRAY(comppad_name);
1535 SV ** const curpad = AvARRAY(comppad);
dd2155a4 1536 for (ix = AvFILLp(comppad_name); ix > 0; ix--) {
e1ec3a88 1537 const SV *namesv = namepad[ix];
dd2155a4 1538 if (namesv && namesv != &PL_sv_undef
b15aece3 1539 && *SvPVX_const(namesv) == '&')
dd2155a4
DM
1540 {
1541 CV *innercv = (CV*)curpad[ix];
7dafbf52
DM
1542 assert(CvWEAKOUTSIDE(innercv));
1543 assert(CvOUTSIDE(innercv) == old_cv);
1544 CvOUTSIDE(innercv) = new_cv;
dd2155a4
DM
1545 }
1546 }
1547}
1548
7dafbf52 1549
dd2155a4
DM
1550/*
1551=for apidoc pad_push
1552
1553Push a new pad frame onto the padlist, unless there's already a pad at
26019298
AL
1554this depth, in which case don't bother creating a new one. Then give
1555the new pad an @_ in slot zero.
dd2155a4
DM
1556
1557=cut
1558*/
1559
1560void
26019298 1561Perl_pad_push(pTHX_ PADLIST *padlist, int depth)
dd2155a4
DM
1562{
1563 if (depth <= AvFILLp(padlist))
1564 return;
1565
1566 {
1567 SV** svp = AvARRAY(padlist);
1568 AV *newpad = newAV();
1569 SV **oldpad = AvARRAY(svp[depth-1]);
1570 I32 ix = AvFILLp((AV*)svp[1]);
e1ec3a88 1571 const I32 names_fill = AvFILLp((AV*)svp[0]);
dd2155a4 1572 SV** names = AvARRAY(svp[0]);
26019298
AL
1573 AV *av;
1574
dd2155a4
DM
1575 for ( ;ix > 0; ix--) {
1576 if (names_fill >= ix && names[ix] != &PL_sv_undef) {
b15aece3 1577 const char sigil = SvPVX_const(names[ix])[0];
26019298 1578 if ((SvFLAGS(names[ix]) & SVf_FAKE) || sigil == '&') {
dd2155a4
DM
1579 /* outer lexical or anon code */
1580 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
1581 }
1582 else { /* our own lexical */
26019298
AL
1583 SV *sv;
1584 if (sigil == '@')
1585 sv = (SV*)newAV();
1586 else if (sigil == '%')
1587 sv = (SV*)newHV();
dd2155a4 1588 else
26019298
AL
1589 sv = NEWSV(0, 0);
1590 av_store(newpad, ix, sv);
dd2155a4
DM
1591 SvPADMY_on(sv);
1592 }
1593 }
1594 else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) {
26019298 1595 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
dd2155a4
DM
1596 }
1597 else {
1598 /* save temporaries on recursion? */
26019298
AL
1599 SV *sv = NEWSV(0, 0);
1600 av_store(newpad, ix, sv);
dd2155a4
DM
1601 SvPADTMP_on(sv);
1602 }
1603 }
26019298
AL
1604 av = newAV();
1605 av_extend(av, 0);
1606 av_store(newpad, 0, (SV*)av);
11ca45c0 1607 AvREIFY_only(av);
26019298 1608
dd2155a4
DM
1609 av_store(padlist, depth, (SV*)newpad);
1610 AvFILLp(padlist) = depth;
1611 }
1612}
b21dc031
AL
1613
1614
1615HV *
1616Perl_pad_compname_type(pTHX_ const PADOFFSET po)
1617{
1618 SV** const av = av_fetch(PL_comppad_name, po, FALSE);
1619 if ( SvFLAGS(*av) & SVpad_TYPED ) {
1620 return SvSTASH(*av);
1621 }
1622 return Nullhv;
1623}
66610fdd
RGS
1624
1625/*
1626 * Local variables:
1627 * c-indentation-style: bsd
1628 * c-basic-offset: 4
1629 * indent-tabs-mode: t
1630 * End:
1631 *
37442d52
RGS
1632 * ex: set ts=8 sts=4 sw=4 noet:
1633 */