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