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