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