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