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