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;
e4a519ba 229 const PADLIST * const 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 246 if (!PL_dirty) { /* don't bother during global destruction */
0188be2e 247 CV * const outercv = CvOUTSIDE(cv);
c05e0e2f 248 const U32 seq = CvOUTSIDE_SEQ(cv);
0188be2e
AL
249 AV * const comppad_name = (AV*)AvARRAY(padlist)[0];
250 SV ** const namepad = AvARRAY(comppad_name);
251 AV * const comppad = (AV*)AvARRAY(padlist)[1];
252 SV ** const curpad = AvARRAY(comppad);
9755d405 253 for (ix = AvFILLp(comppad_name); ix > 0; ix--) {
547d29e4 254 SV * const namesv = namepad[ix];
9755d405 255 if (namesv && namesv != &PL_sv_undef
5e7e76a3 256 && *SvPVX_const(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) {
0188be2e 292 SV* const sv = AvARRAY(padlist)[ix--];
9755d405
JH
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{
547d29e4 336 const PADOFFSET offset = pad_alloc(OP_PADSV, SVs_PADMY);
0188be2e 337 SV* const 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 {
0188be2e 419 SV ** const 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;
e4a519ba 460 SV* const name = NEWSV(1106, 0);
9755d405
JH
461 sv_upgrade(name, SVt_PVNV);
462 sv_setpvn(name, "&", 1);
0da6cfda
SP
463 SvIV_set(name, -1);
464 SvNV_set(name, 1);
9755d405
JH
465 ix = pad_alloc(op_type, SVs_PADMY);
466 av_store(PL_comppad_name, ix, name);
d7afa7f5 467 /* XXX DAPM use PL_curpad[] ? */
9755d405
JH
468 av_store(PL_comppad, ix, sv);
469 SvPADMY_on(sv);
d7afa7f5
JH
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 }
9755d405
JH
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
498Perl_pad_check_dup(pTHX_ char *name, bool is_our, HV *ourstash)
499{
0188be2e 500 SV **svp;
9755d405
JH
501 PADOFFSET top, off;
502
d7afa7f5 503 ASSERT_CURPAD_ACTIVE("pad_check_dup");
f5e9f069 504 if (AvFILLp(PL_comppad_name) < 0 || !ckWARN(WARN_MISC))
9755d405
JH
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--) {
0188be2e
AL
513 SV * const sv = svp[off];
514 if (sv
9755d405 515 && sv != &PL_sv_undef
d7afa7f5 516 && !SvFAKE(sv)
9755d405
JH
517 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
518 && (!is_our
519 || ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash))
5e7e76a3 520 && strEQ(name, SvPVX_const(sv)))
9755d405
JH
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 {
0188be2e
AL
534 SV * const sv = svp[off];
535 if (sv
9755d405 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)
5e7e76a3 540 && strEQ(name, SvPVX_const(sv)))
9755d405
JH
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 594 sv = svp[off];
5e7e76a3 595 if (!sv || sv == &PL_sv_undef || !strEQ(SvPVX_const(sv), name))
d7afa7f5
JH
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 686 sv = svp[off];
5e7e76a3 687 if (!sv || sv == &PL_sv_undef || !strEQ(SvPVX_const(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;
9755d405
JH
919 I32 i;
920
d7afa7f5 921 ASSERT_CURPAD_ACTIVE("intro_my");
9755d405
JH
922 if (! PL_min_intro_pending)
923 return PL_cop_seqmax;
924
925 svp = AvARRAY(PL_comppad_name);
926 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
0188be2e
AL
927 SV * const sv = svp[i];
928
929 if (sv && sv != &PL_sv_undef && !SvFAKE(sv) && !SvIVX(sv)) {
0da6cfda
SP
930 SvIV_set(sv, PAD_MAX); /* Don't know scope end yet. */
931 SvNV_set(sv, (NV)PL_cop_seqmax);
9755d405
JH
932 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
933 "Pad intromy: %ld \"%s\", (%lu,%lu)\n",
5e7e76a3 934 (long)i, SvPVX_const(sv),
91ca9d19 935 (unsigned long)U_32(SvNVX(sv)), (unsigned long)SvIVX(sv))
9755d405
JH
936 );
937 }
938 }
939 PL_min_intro_pending = 0;
940 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
941 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
942 "Pad intromy: seq -> %ld\n", (long)(PL_cop_seqmax+1)));
943
944 return PL_cop_seqmax++;
945}
946
947/*
948=for apidoc pad_leavemy
949
950Cleanup at end of scope during compilation: set the max seq number for
951lexicals in this scope and warn of any lexicals that never got introduced.
952
953=cut
954*/
955
956void
957Perl_pad_leavemy(pTHX)
958{
959 I32 off;
0188be2e 960 SV ** const svp = AvARRAY(PL_comppad_name);
9755d405
JH
961
962 PL_pad_reset_pending = FALSE;
963
d7afa7f5 964 ASSERT_CURPAD_ACTIVE("pad_leavemy");
9755d405
JH
965 if (PL_min_intro_pending && PL_comppad_name_fill < PL_min_intro_pending) {
966 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
0188be2e
AL
967 const SV * const sv = svp[off];
968 if (sv && sv != &PL_sv_undef
d7afa7f5 969 && !SvFAKE(sv) && ckWARN_d(WARN_INTERNAL))
9755d405 970 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
c293eb2b 971 "%"SVf" never introduced", sv);
9755d405
JH
972 }
973 }
974 /* "Deintroduce" my variables that are leaving with this scope. */
975 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_fill; off--) {
0188be2e
AL
976 const SV * const sv = svp[off];
977 if (sv && sv != &PL_sv_undef && !SvFAKE(sv) && SvIVX(sv) == PAD_MAX) {
0da6cfda 978 SvIV_set(sv, PL_cop_seqmax);
9755d405
JH
979 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
980 "Pad leavemy: %ld \"%s\", (%lu,%lu)\n",
5e7e76a3 981 (long)off, SvPVX_const(sv),
91ca9d19 982 (unsigned long)U_32(SvNVX(sv)), (unsigned long)SvIVX(sv))
9755d405
JH
983 );
984 }
985 }
986 PL_cop_seqmax++;
987 DEBUG_Xv(PerlIO_printf(Perl_debug_log,
988 "Pad leavemy: seq = %ld\n", (long)PL_cop_seqmax));
989}
990
991
992/*
993=for apidoc pad_swipe
994
995Abandon the tmp in the current pad at offset po and replace with a
996new one.
997
998=cut
999*/
1000
1001void
1002Perl_pad_swipe(pTHX_ PADOFFSET po, bool refadjust)
1003{
d7afa7f5 1004 ASSERT_CURPAD_LEGAL("pad_swipe");
9755d405
JH
1005 if (!PL_curpad)
1006 return;
1007 if (AvARRAY(PL_comppad) != PL_curpad)
1008 Perl_croak(aTHX_ "panic: pad_swipe curpad");
1009 if (!po)
1010 Perl_croak(aTHX_ "panic: pad_swipe po");
1011
1012 DEBUG_X(PerlIO_printf(Perl_debug_log,
1013 "Pad 0x%"UVxf"[0x%"UVxf"] swipe: %ld\n",
1014 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po));
1015
1016 if (PL_curpad[po])
1017 SvPADTMP_off(PL_curpad[po]);
1018 if (refadjust)
1019 SvREFCNT_dec(PL_curpad[po]);
1020
8aea1737
NC
1021
1022 /* if pad tmps aren't shared between ops, then there's no need to
1023 * create a new tmp when an existing op is freed */
1024#ifdef USE_BROKEN_PAD_RESET
9755d405
JH
1025 PL_curpad[po] = NEWSV(1107,0);
1026 SvPADTMP_on(PL_curpad[po]);
8aea1737
NC
1027#else
1028 PL_curpad[po] = &PL_sv_undef;
1029#endif
9755d405
JH
1030 if ((I32)po < PL_padix)
1031 PL_padix = po - 1;
1032}
1033
1034
1035/*
1036=for apidoc pad_reset
1037
1038Mark all the current temporaries for reuse
1039
1040=cut
1041*/
1042
1043/* XXX pad_reset() is currently disabled because it results in serious bugs.
1044 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
1045 * on the stack by OPs that use them, there are several ways to get an alias
1046 * to a shared TARG. Such an alias will change randomly and unpredictably.
1047 * We avoid doing this until we can think of a Better Way.
1048 * GSAR 97-10-29 */
1049void
1050Perl_pad_reset(pTHX)
1051{
1052#ifdef USE_BROKEN_PAD_RESET
9755d405
JH
1053 if (AvARRAY(PL_comppad) != PL_curpad)
1054 Perl_croak(aTHX_ "panic: pad_reset curpad");
1055
1056 DEBUG_X(PerlIO_printf(Perl_debug_log,
1057 "Pad 0x%"UVxf"[0x%"UVxf"] reset: padix %ld -> %ld",
1058 PTR2UV(PL_comppad), PTR2UV(PL_curpad),
1059 (long)PL_padix, (long)PL_padix_floor
1060 )
1061 );
1062
1063 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
c05e0e2f 1064 register I32 po;
9755d405
JH
1065 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
1066 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
1067 SvPADTMP_off(PL_curpad[po]);
1068 }
1069 PL_padix = PL_padix_floor;
1070 }
1071#endif
1072 PL_pad_reset_pending = FALSE;
1073}
1074
1075
1076/*
1077=for apidoc pad_tidy
1078
1079Tidy up a pad after we've finished compiling it:
1080 * remove most stuff from the pads of anonsub prototypes;
1081 * give it a @_;
1082 * mark tmps as such.
1083
1084=cut
1085*/
1086
1087/* XXX DAPM surely most of this stuff should be done properly
1088 * at the right time beforehand, rather than going around afterwards
1089 * cleaning up our mistakes ???
1090 */
1091
1092void
1093Perl_pad_tidy(pTHX_ padtidy_type type)
1094{
9755d405 1095
d7afa7f5 1096 ASSERT_CURPAD_ACTIVE("pad_tidy");
9755d405
JH
1097 /* extend curpad to match namepad */
1098 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
1099 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
1100
1101 if (type == padtidy_SUBCLONE) {
0188be2e 1102 SV ** const namep = AvARRAY(PL_comppad_name);
547d29e4 1103 PADOFFSET ix;
9755d405
JH
1104 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1105 SV *namesv;
1106
1107 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
1108 continue;
1109 /*
1110 * The only things that a clonable function needs in its
1111 * pad are references to outer lexicals and anonymous subs.
1112 * The rest are created anew during cloning.
1113 */
1114 if (!((namesv = namep[ix]) != Nullsv &&
1115 namesv != &PL_sv_undef &&
1116 (SvFAKE(namesv) ||
5e7e76a3 1117 *SvPVX_const(namesv) == '&')))
9755d405
JH
1118 {
1119 SvREFCNT_dec(PL_curpad[ix]);
1120 PL_curpad[ix] = Nullsv;
1121 }
1122 }
1123 }
1124 else if (type == padtidy_SUB) {
1125 /* XXX DAPM this same bit of code keeps appearing !!! Rationalise? */
0188be2e 1126 AV * const av = newAV(); /* Will be @_ */
9755d405
JH
1127 av_extend(av, 0);
1128 av_store(PL_comppad, 0, (SV*)av);
1129 AvFLAGS(av) = AVf_REIFY;
1130 }
1131
1132 /* XXX DAPM rationalise these two similar branches */
1133
1134 if (type == padtidy_SUB) {
547d29e4 1135 PADOFFSET ix;
9755d405
JH
1136 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1137 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
1138 continue;
1139 if (!SvPADMY(PL_curpad[ix]))
1140 SvPADTMP_on(PL_curpad[ix]);
1141 }
1142 }
1143 else if (type == padtidy_FORMAT) {
547d29e4 1144 PADOFFSET ix;
9755d405
JH
1145 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
1146 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
1147 SvPADTMP_on(PL_curpad[ix]);
1148 }
1149 }
d7afa7f5 1150 PL_curpad = AvARRAY(PL_comppad);
9755d405
JH
1151}
1152
1153
1154/*
1155=for apidoc pad_free
1156
de7b736d 1157Free the SV at offset po in the current pad.
9755d405
JH
1158
1159=cut
1160*/
1161
1162/* XXX DAPM integrate with pad_swipe ???? */
1163void
1164Perl_pad_free(pTHX_ PADOFFSET po)
1165{
d7afa7f5 1166 ASSERT_CURPAD_LEGAL("pad_free");
9755d405
JH
1167 if (!PL_curpad)
1168 return;
1169 if (AvARRAY(PL_comppad) != PL_curpad)
1170 Perl_croak(aTHX_ "panic: pad_free curpad");
1171 if (!po)
1172 Perl_croak(aTHX_ "panic: pad_free po");
1173
1174 DEBUG_X(PerlIO_printf(Perl_debug_log,
1175 "Pad 0x%"UVxf"[0x%"UVxf"] free: %ld\n",
1176 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po)
1177 );
1178
1179 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) {
1180 SvPADTMP_off(PL_curpad[po]);
1181#ifdef USE_ITHREADS
1182 /* SV could be a shared hash key (eg bugid #19022) */
1183 if (!SvFAKE(PL_curpad[po]))
1184 SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */
1185#endif
1186
1187 }
1188 if ((I32)po < PL_padix)
1189 PL_padix = po - 1;
1190}
1191
1192
1193
1194/*
1195=for apidoc do_dump_pad
1196
1197Dump the contents of a padlist
1198
1199=cut
1200*/
1201
1202void
1203Perl_do_dump_pad(pTHX_ I32 level, PerlIO *file, PADLIST *padlist, int full)
1204{
c05e0e2f
AL
1205 const AV *pad_name;
1206 const AV *pad;
9755d405
JH
1207 SV **pname;
1208 SV **ppad;
9755d405
JH
1209 I32 ix;
1210
1211 if (!padlist) {
1212 return;
1213 }
1214 pad_name = (AV*)*av_fetch((AV*)padlist, 0, FALSE);
1215 pad = (AV*)*av_fetch((AV*)padlist, 1, FALSE);
1216 pname = AvARRAY(pad_name);
1217 ppad = AvARRAY(pad);
1218 Perl_dump_indent(aTHX_ level, file,
1219 "PADNAME = 0x%"UVxf"(0x%"UVxf") PAD = 0x%"UVxf"(0x%"UVxf")\n",
1220 PTR2UV(pad_name), PTR2UV(pname), PTR2UV(pad), PTR2UV(ppad)
1221 );
1222
1223 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
c05e0e2f 1224 const SV *namesv = pname[ix];
9755d405
JH
1225 if (namesv && namesv == &PL_sv_undef) {
1226 namesv = Nullsv;
1227 }
1228 if (namesv) {
d7afa7f5
JH
1229 if (SvFAKE(namesv))
1230 Perl_dump_indent(aTHX_ level+1, file,
a72b686c 1231 "%2d. 0x%"UVxf"<%lu> FAKE \"%s\"\n",
d7afa7f5
JH
1232 (int) ix,
1233 PTR2UV(ppad[ix]),
1234 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0),
5e7e76a3 1235 SvPVX_const(namesv)
d7afa7f5
JH
1236 );
1237 else
1238 Perl_dump_indent(aTHX_ level+1, file,
a72b686c 1239 "%2d. 0x%"UVxf"<%lu> (%lu,%lu) \"%s\"\n",
d7afa7f5
JH
1240 (int) ix,
1241 PTR2UV(ppad[ix]),
1242 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0),
91ca9d19 1243 (unsigned long)U_32(SvNVX(namesv)),
d7afa7f5 1244 (unsigned long)SvIVX(namesv),
5e7e76a3 1245 SvPVX_const(namesv)
d7afa7f5 1246 );
9755d405
JH
1247 }
1248 else if (full) {
1249 Perl_dump_indent(aTHX_ level+1, file,
1250 "%2d. 0x%"UVxf"<%lu>\n",
1251 (int) ix,
1252 PTR2UV(ppad[ix]),
1253 (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0)
1254 );
1255 }
1256 }
1257}
1258
1259
1260
1261/*
1262=for apidoc cv_dump
1263
1264dump the contents of a CV
1265
1266=cut
1267*/
1268
1269#ifdef DEBUGGING
1270STATIC void
c05e0e2f 1271S_cv_dump(pTHX_ const CV *cv, const char *title)
9755d405 1272{
0188be2e
AL
1273 const CV * const outside = CvOUTSIDE(cv);
1274 AV* const padlist = CvPADLIST(cv);
9755d405
JH
1275
1276 PerlIO_printf(Perl_debug_log,
1277 " %s: CV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
1278 title,
1279 PTR2UV(cv),
1280 (CvANON(cv) ? "ANON"
1281 : (cv == PL_main_cv) ? "MAIN"
1282 : CvUNIQUE(cv) ? "UNIQUE"
1283 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
1284 PTR2UV(outside),
1285 (!outside ? "null"
1286 : CvANON(outside) ? "ANON"
1287 : (outside == PL_main_cv) ? "MAIN"
1288 : CvUNIQUE(outside) ? "UNIQUE"
1289 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
1290
1291 PerlIO_printf(Perl_debug_log,
1292 " PADLIST = 0x%"UVxf"\n", PTR2UV(padlist));
1293 do_dump_pad(1, Perl_debug_log, padlist, 1);
1294}
1295#endif /* DEBUGGING */
1296
1297
1298
1299
1300
1301/*
1302=for apidoc cv_clone
1303
1304Clone a CV: make a new CV which points to the same code etc, but which
1305has a newly-created pad built by copying the prototype pad and capturing
1306any outer lexicals.
1307
1308=cut
1309*/
1310
1311CV *
1312Perl_cv_clone(pTHX_ CV *proto)
1313{
1314 CV *cv;
1315
1316 LOCK_CRED_MUTEX; /* XXX create separate mutex */
1317 cv = cv_clone2(proto, CvOUTSIDE(proto));
1318 UNLOCK_CRED_MUTEX; /* XXX create separate mutex */
1319 return cv;
1320}
1321
1322
1323/* XXX DAPM separate out cv and paddish bits ???
1324 * ideally the CV-related stuff shouldn't be in pad.c - how about
1325 * a cv.c? */
1326
1327STATIC CV *
1328S_cv_clone2(pTHX_ CV *proto, CV *outside)
1329{
1330 I32 ix;
0188be2e
AL
1331 AV* const protopadlist = CvPADLIST(proto);
1332 const AV* const protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
1333 const AV* const protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
1334 SV** const pname = AvARRAY(protopad_name);
1335 SV** const ppad = AvARRAY(protopad);
c05e0e2f
AL
1336 const I32 fname = AvFILLp(protopad_name);
1337 const I32 fpad = AvFILLp(protopad);
9755d405
JH
1338 CV* cv;
1339
1340 assert(!CvUNIQUE(proto));
1341
1342 ENTER;
1343 SAVESPTR(PL_compcv);
1344
1345 cv = PL_compcv = (CV*)NEWSV(1104, 0);
1346 sv_upgrade((SV *)cv, SvTYPE(proto));
d7afa7f5 1347 CvFLAGS(cv) = CvFLAGS(proto) & ~(CVf_CLONE|CVf_WEAKOUTSIDE);
9755d405
JH
1348 CvCLONED_on(cv);
1349
1350#ifdef USE_5005THREADS
1351 New(666, CvMUTEXP(cv), 1, perl_mutex);
1352 MUTEX_INIT(CvMUTEXP(cv));
1353 CvOWNER(cv) = 0;
1354#endif /* USE_5005THREADS */
1355#ifdef USE_ITHREADS
1356 CvFILE(cv) = CvXSUB(proto) ? CvFILE(proto)
1357 : savepv(CvFILE(proto));
1358#else
1359 CvFILE(cv) = CvFILE(proto);
1360#endif
1361 CvGV(cv) = CvGV(proto);
1362 CvSTASH(cv) = CvSTASH(proto);
46330ab1 1363 OP_REFCNT_LOCK;
9755d405 1364 CvROOT(cv) = OpREFCNT_inc(CvROOT(proto));
46330ab1 1365 OP_REFCNT_UNLOCK;
9755d405 1366 CvSTART(cv) = CvSTART(proto);
d7afa7f5 1367 if (outside) {
9755d405 1368 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
d7afa7f5
JH
1369 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(proto);
1370 }
9755d405
JH
1371
1372 if (SvPOK(proto))
5e7e76a3 1373 sv_setpvn((SV*)cv, SvPVX_const(proto), SvCUR(proto));
9755d405 1374
cdf9dde0 1375 CvPADLIST(cv) = pad_new(padnew_CLONE|padnew_SAVE);
9755d405
JH
1376
1377 for (ix = fname; ix >= 0; ix--)
1378 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
1379
1380 av_fill(PL_comppad, fpad);
1381 PL_curpad = AvARRAY(PL_comppad);
1382
1383 for (ix = fpad; ix > 0; ix--) {
0188be2e 1384 SV* const namesv = (ix <= fname) ? pname[ix] : Nullsv;
9755d405 1385 if (namesv && namesv != &PL_sv_undef) {
5e7e76a3 1386 const char *name = SvPVX_const(namesv); /* XXX */
9755d405 1387 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
d7afa7f5 1388 I32 off = pad_findlex(name, ix, cv);
9755d405
JH
1389 if (!off)
1390 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
1391 else if (off != ix)
1392 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
1393 }
1394 else { /* our own lexical */
1395 SV* sv;
1396 if (*name == '&') {
1397 /* anon code -- we'll come back for it */
1398 sv = SvREFCNT_inc(ppad[ix]);
1399 }
1400 else if (*name == '@')
1401 sv = (SV*)newAV();
1402 else if (*name == '%')
1403 sv = (SV*)newHV();
1404 else
1405 sv = NEWSV(0, 0);
1406 if (!SvPADBUSY(sv))
1407 SvPADMY_on(sv);
1408 PL_curpad[ix] = sv;
1409 }
1410 }
1411 else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) {
1412 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
1413 }
1414 else {
1415 SV* sv = NEWSV(0, 0);
1416 SvPADTMP_on(sv);
1417 PL_curpad[ix] = sv;
1418 }
1419 }
1420
1421 /* Now that vars are all in place, clone nested closures. */
1422
1423 for (ix = fpad; ix > 0; ix--) {
1424 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
1425 if (namesv
1426 && namesv != &PL_sv_undef
1427 && !(SvFLAGS(namesv) & SVf_FAKE)
1428 && *SvPVX(namesv) == '&'
1429 && CvCLONE(ppad[ix]))
1430 {
1431 CV *kid = cv_clone2((CV*)ppad[ix], cv);
1432 SvREFCNT_dec(ppad[ix]);
1433 CvCLONE_on(kid);
1434 SvPADMY_on(kid);
1435 PL_curpad[ix] = (SV*)kid;
d7afa7f5
JH
1436 /* '&' entry points to child, so child mustn't refcnt parent */
1437 CvWEAKOUTSIDE_on(kid);
1438 SvREFCNT_dec(cv);
9755d405
JH
1439 }
1440 }
1441
1442 DEBUG_Xv(
1443 PerlIO_printf(Perl_debug_log, "\nPad CV clone\n");
1444 cv_dump(outside, "Outside");
1445 cv_dump(proto, "Proto");
1446 cv_dump(cv, "To");
1447 );
1448
1449 LEAVE;
1450
1451 if (CvCONST(cv)) {
1452 SV* const_sv = op_const_sv(CvSTART(cv), cv);
1453 assert(const_sv);
1454 /* constant sub () { $x } closing over $x - see lib/constant.pm */
1455 SvREFCNT_dec(cv);
e7b555a6 1456 cv = newCONSTSUB(CvSTASH(proto), Nullch, const_sv);
9755d405
JH
1457 }
1458
1459 return cv;
1460}
1461
1462
1463/*
1464=for apidoc pad_fixup_inner_anons
1465
1466For any anon CVs in the pad, change CvOUTSIDE of that CV from
d7afa7f5
JH
1467old_cv to new_cv if necessary. Needed when a newly-compiled CV has to be
1468moved to a pre-existing CV struct.
9755d405
JH
1469
1470=cut
1471*/
1472
1473void
1474Perl_pad_fixup_inner_anons(pTHX_ PADLIST *padlist, CV *old_cv, CV *new_cv)
1475{
1476 I32 ix;
4996ee04
AL
1477 AV * const comppad_name = (AV*)AvARRAY(padlist)[0];
1478 AV * const comppad = (AV*)AvARRAY(padlist)[1];
0188be2e
AL
1479 SV ** const namepad = AvARRAY(comppad_name);
1480 SV ** const curpad = AvARRAY(comppad);
9755d405 1481 for (ix = AvFILLp(comppad_name); ix > 0; ix--) {
c05e0e2f 1482 const SV *namesv = namepad[ix];
9755d405 1483 if (namesv && namesv != &PL_sv_undef
5e7e76a3 1484 && *SvPVX_const(namesv) == '&')
9755d405 1485 {
217ef1d7 1486 CV * const innercv = (CV*)curpad[ix];
d7afa7f5
JH
1487 assert(CvWEAKOUTSIDE(innercv));
1488 assert(CvOUTSIDE(innercv) == old_cv);
1489 CvOUTSIDE(innercv) = new_cv;
9755d405
JH
1490 }
1491 }
1492}
1493
d7afa7f5 1494
9755d405
JH
1495/*
1496=for apidoc pad_push
1497
1498Push a new pad frame onto the padlist, unless there's already a pad at
1499this depth, in which case don't bother creating a new one.
1500If has_args is true, give the new pad an @_ in slot zero.
1501
1502=cut
1503*/
1504
8b3a4b74
NC
1505/* XXX pad_push is now always called with has_args == 1. Get rid of
1506 * this arg at some point */
1507
9755d405
JH
1508void
1509Perl_pad_push(pTHX_ PADLIST *padlist, int depth, int has_args)
1510{
1511 if (depth <= AvFILLp(padlist))
1512 return;
1513
1514 {
1515 SV** svp = AvARRAY(padlist);
1516 AV *newpad = newAV();
1517 SV **oldpad = AvARRAY(svp[depth-1]);
1518 I32 ix = AvFILLp((AV*)svp[1]);
1519 I32 names_fill = AvFILLp((AV*)svp[0]);
1520 SV** names = AvARRAY(svp[0]);
1521 SV* sv;
1522 for ( ;ix > 0; ix--) {
1523 if (names_fill >= ix && names[ix] != &PL_sv_undef) {
5e7e76a3 1524 const char *name = SvPVX_const(names[ix]);
9755d405
JH
1525 if ((SvFLAGS(names[ix]) & SVf_FAKE) || *name == '&') {
1526 /* outer lexical or anon code */
1527 av_store(newpad, ix, SvREFCNT_inc(oldpad[ix]));
1528 }
1529 else { /* our own lexical */
1530 if (*name == '@')
1531 av_store(newpad, ix, sv = (SV*)newAV());
1532 else if (*name == '%')
1533 av_store(newpad, ix, sv = (SV*)newHV());
1534 else
1535 av_store(newpad, ix, sv = NEWSV(0, 0));
1536 SvPADMY_on(sv);
1537 }
1538 }
1539 else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) {
1540 av_store(newpad, ix, sv = SvREFCNT_inc(oldpad[ix]));
1541 }
1542 else {
1543 /* save temporaries on recursion? */
1544 av_store(newpad, ix, sv = NEWSV(0, 0));
1545 SvPADTMP_on(sv);
1546 }
1547 }
1548 if (has_args) {
1549 AV* av = newAV();
1550 av_extend(av, 0);
1551 av_store(newpad, 0, (SV*)av);
1552 AvFLAGS(av) = AVf_REIFY;
1553 }
1554 av_store(padlist, depth, (SV*)newpad);
1555 AvFILLp(padlist) = depth;
1556 }
1557}
25e59d17
NC
1558
1559
1560HV *
1561Perl_pad_compname_type(pTHX_ const PADOFFSET po)
1562{
1563 SV** const av = av_fetch(PL_comppad_name, po, FALSE);
1564 if ( SvFLAGS(*av) & SVpad_TYPED ) {
1565 return SvSTASH(*av);
1566 }
1567 return Nullhv;
1568}
d8294a4d
NC
1569
1570/*
1571 * Local variables:
1572 * c-indentation-style: bsd
1573 * c-basic-offset: 4
1574 * indent-tabs-mode: t
1575 * End:
1576 *
1577 * ex: set ts=8 sts=4 sw=4 noet:
1578 */