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