Commit | Line | Data |
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 |
25 | This file contains the functions that create and manipulate scratchpads, |
166f8a29 |
26 | which are array-of-array data structures attached to a CV (ie a sub) |
61296642 |
27 | and which store lexical variables and opcode temporary and per-thread |
166f8a29 |
28 | values. |
29 | |
dd2155a4 |
30 | =for apidoc m|AV *|CvPADLIST|CV *cv |
31 | CV's can have CvPADLIST(cv) set to point to an AV. |
32 | |
33 | For these purposes "forms" are a kind-of CV, eval""s are too (except they're |
34 | not callable at will and are always thrown away after the eval"" is done |
b5c19bd7 |
35 | executing). Require'd files are simply evals without any outer lexical |
36 | scope. |
dd2155a4 |
37 | |
38 | XSUBs don't have CvPADLIST set - dXSTARG fetches values from PL_curpad, |
39 | but that is really the callers pad (a slot of which is allocated by |
40 | every entersub). |
41 | |
42 | The CvPADLIST AV has does not have AvREAL set, so REFCNT of component items |
f3548bdc |
43 | is managed "manual" (mostly in pad.c) rather than normal av.c rules. |
dd2155a4 |
44 | The items in the AV are not SVs as for a normal AV, but other AVs: |
45 | |
46 | 0'th Entry of the CvPADLIST is an AV which represents the "names" or rather |
47 | the "static type information" for lexicals. |
48 | |
49 | The CvDEPTH'th entry of CvPADLIST AV is an AV which is the stack frame at that |
50 | depth of recursion into the CV. |
51 | The 0'th slot of a frame AV is an AV which is @_. |
52 | other entries are storage for variables and op targets. |
53 | |
54 | During compilation: |
a6d05634 |
55 | C<PL_comppad_name> is set to the names AV. |
56 | C<PL_comppad> is set to the frame AV for the frame CvDEPTH == 1. |
57 | C<PL_curpad> is set to the body of the frame AV (i.e. AvARRAY(PL_comppad)). |
dd2155a4 |
58 | |
f3548bdc |
59 | During execution, C<PL_comppad> and C<PL_curpad> refer to the live |
60 | frame of the currently executing sub. |
61 | |
62 | Iterating over the names AV iterates over all possible pad |
dd2155a4 |
63 | items. Pad slots that are SVs_PADTMP (targets/GVs/constants) end up having |
64 | &PL_sv_undef "names" (see pad_alloc()). |
65 | |
66 | Only my/our variable (SVs_PADMY/SVs_PADOUR) slots get valid names. |
67 | The rest are op targets/GVs/constants which are statically allocated |
68 | or resolved at compile time. These don't have names by which they |
69 | can be looked up from Perl code at run time through eval"" like |
70 | my/our variables can be. Since they can't be looked up by "name" |
71 | but only by their index allocated at compile time (which is usually |
72 | in PL_op->op_targ), wasting a name SV for them doesn't make sense. |
73 | |
74 | The SVs in the names AV have their PV being the name of the variable. |
75 | NV+1..IV inclusive is a range of cop_seq numbers for which the name is |
76 | valid. For typed lexicals name SV is SVt_PVMG and SvSTASH points at the |
2892acdb |
77 | type. For C<our> lexicals, the type is also SVt_PVGV, with the MAGIC slot |
78 | pointing at the stash of the associated global (so that duplicate C<our> |
79 | declarations in the same package can be detected). SvCUR is sometimes |
80 | hijacked to store the generation number during compilation. |
dd2155a4 |
81 | |
b5c19bd7 |
82 | If SvFAKE is set on the name SV, then that slot in the frame AV is |
83 | a REFCNT'ed reference to a lexical from "outside". In this case, |
84 | the name SV does not use NVX and IVX to store a cop_seq range, since it is |
85 | in scope throughout. Instead IVX stores some flags containing info about |
86 | the real lexical (is it declared in an anon, and is it capable of being |
87 | instantiated multiple times?), and for fake ANONs, NVX contains the index |
88 | within the parent's pad where the lexical's value is stored, to make |
89 | cloning quicker. |
dd2155a4 |
90 | |
a6d05634 |
91 | If the 'name' is '&' the corresponding entry in frame AV |
dd2155a4 |
92 | is a CV representing a possible closure. |
93 | (SvFAKE and name of '&' is not a meaningful combination currently but could |
94 | become so if C<my sub foo {}> is implemented.) |
95 | |
71f882da |
96 | Note that formats are treated as anon subs, and are cloned each time |
97 | write is called (if necessary). |
98 | |
e6e7068b |
99 | The flag SVf_PADSTALE is cleared on lexicals each time the my() is executed, |
100 | and set on scope exit. This allows the 'Variable $x is not available' warning |
101 | to 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 |
117 | void pad_peg(const char* s) { |
118 | static int pegcnt; |
119 | pegcnt++; |
120 | } |
121 | #endif |
dd2155a4 |
122 | |
123 | /* |
124 | =for apidoc pad_new |
125 | |
126 | Create a new compiling padlist, saving and updating the various global |
127 | vars at the same time as creating the pad itself. The following flags |
128 | can 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 | |
137 | PADLIST * |
c7c737cb |
138 | Perl_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 | |
222 | Free the padlist associated with a CV. |
223 | If parts of it happen to be current, we null the relevant |
224 | PL_*pad* global vars so that we don't have any dangling references left. |
225 | We also repoint the CvOUTSIDE of any about-to-be-orphaned |
a3985cdc |
226 | inner subs to the outer of this cv. |
dd2155a4 |
227 | |
7dafbf52 |
228 | (This function should really be called pad_free, but the name was already |
229 | taken) |
230 | |
dd2155a4 |
231 | =cut |
232 | */ |
233 | |
234 | void |
a3985cdc |
235 | Perl_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; |
b37c2d43 |
290 | SvREFCNT_inc_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 |
323 | Create a new name and associated PADMY SV in the current pad; return the |
324 | offset. |
dd2155a4 |
325 | If C<typestash> is valid, the name is for a typed lexical; set the |
326 | name's stash to that value. |
327 | If C<ourstash> is valid, it's an our lexical, set the name's |
035dab74 |
328 | OURSTASH to that value |
dd2155a4 |
329 | |
dd2155a4 |
330 | If fake, it means we're cloning an existing entry |
331 | |
332 | =cut |
333 | */ |
334 | |
dd2155a4 |
335 | PADOFFSET |
e1ec3a88 |
336 | Perl_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); |
b37c2d43 |
355 | SvREFCNT_inc_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 | |
394 | Allocate a new my or tmp pad entry. For a my, simply push a null SV onto |
395 | the end of PL_comppad, but for a tmp, scan the pad from PL_padix upwards |
cf525c36 |
396 | for 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 | |
411 | PADOFFSET |
412 | Perl_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 | |
466 | Add an anon code entry to the current compiling pad |
467 | |
468 | =cut |
469 | */ |
470 | |
471 | PADOFFSET |
472 | Perl_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 | |
503 | Check 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> |
507 | C<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 | |
514 | void |
e1ec3a88 |
515 | Perl_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 | |
575 | Given a lexical name, try to find its offset, first in the current pad, |
576 | or failing that, in the pads of any lexically enclosing subs (including |
577 | the complications introduced by eval). If the name is found in an outer pad, |
578 | then a fake entry is added to the current pad. |
579 | Returns the offset in the current pad, or NOT_IN_PAD on failure. |
580 | |
581 | =cut |
582 | */ |
583 | |
584 | PADOFFSET |
e1ec3a88 |
585 | Perl_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"); |
929a0744 |
595 | offset = pad_findlex(name, PL_compcv, PL_cop_seqmax, 1, |
4608196e |
596 | NULL, &out_sv, &out_flags); |
929a0744 |
597 | if (offset != NOT_IN_PAD) |
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 | |
624 | PADOFFSET |
29289021 |
625 | Perl_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 | |
637 | Find a named lexical anywhere in a chain of nested pads. Add fake entries |
b5c19bd7 |
638 | in the inner pads if it's found in an outer one. |
639 | |
640 | Returns the offset in the bottom pad of the lex or the fake lex. |
641 | cv is the CV in which to start the search, and seq is the current cop_seq |
642 | to match against. If warn is true, print appropriate warnings. The out_* |
643 | vars return values, and so are pointers to where the returned values |
644 | should be stored. out_capture, if non-null, requests that the innermost |
645 | instance of the lexical is captured; out_name_sv is set to the innermost |
646 | matched namesv or fake namesv; out_flags returns the flags normally |
647 | associated with the IVX field of a fake namesv. |
648 | |
649 | Note that pad_findlex() is recursive; it recurses up the chain of CVs, |
650 | then comes back down, adding fake entries as it goes. It has to be this way |
651 | because fake namesvs in anon protoypes have to store in NVX the index into |
652 | the 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 |
670 | STATIC PADOFFSET |
e1ec3a88 |
671 | S_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; |
817 | |
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); |
825 | if (offset == NOT_IN_PAD) |
826 | return NOT_IN_PAD; |
827 | |
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)) { |
bb263b4e |
856 | /*EMPTY*/; /* 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 | |
885 | Get the value at offset po in the current pad. |
886 | Use macro PAD_SV instead of calling this function directly. |
887 | |
888 | =cut |
889 | */ |
890 | |
891 | |
892 | SV * |
893 | Perl_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 | |
911 | Set the entry at offset po in the current pad to sv. |
912 | Use the macro PAD_SETSV() rather than calling this function directly. |
913 | |
914 | =cut |
915 | */ |
916 | |
dd2155a4 |
917 | void |
918 | Perl_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 | |
936 | Update 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 | |
947 | void |
948 | Perl_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 | |
976 | U32 |
977 | Perl_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 | |
1012 | Cleanup at end of scope during compilation: set the max seq number for |
1013 | lexicals in this scope and warn of any lexicals that never got introduced. |
1014 | |
1015 | =cut |
1016 | */ |
1017 | |
1018 | void |
1019 | Perl_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), |
35c1215d |
1034 | "%"SVf" never introduced", sv); |
dd2155a4 |
1035 | } |
1036 | } |
1037 | /* "Deintroduce" my variables that are leaving with this scope. */ |
1038 | for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_fill; off--) { |
53c1dcc0 |
1039 | const SV * const sv = svp[off]; |
1040 | if (sv && sv != &PL_sv_undef && !SvFAKE(sv) && SvIVX(sv) == PAD_MAX) { |
b19bbeda |
1041 | SvIV_set(sv, PL_cop_seqmax); |
dd2155a4 |
1042 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
b5c19bd7 |
1043 | "Pad leavemy: %ld \"%s\", (%ld,%ld)\n", |
b15aece3 |
1044 | (long)off, SvPVX_const(sv), |
4cf4a199 |
1045 | (long)U_32(SvNVX(sv)), (long)SvIVX(sv)) |
dd2155a4 |
1046 | ); |
1047 | } |
1048 | } |
1049 | PL_cop_seqmax++; |
1050 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
1051 | "Pad leavemy: seq = %ld\n", (long)PL_cop_seqmax)); |
1052 | } |
1053 | |
1054 | |
1055 | /* |
1056 | =for apidoc pad_swipe |
1057 | |
1058 | Abandon the tmp in the current pad at offset po and replace with a |
1059 | new one. |
1060 | |
1061 | =cut |
1062 | */ |
1063 | |
1064 | void |
1065 | Perl_pad_swipe(pTHX_ PADOFFSET po, bool refadjust) |
1066 | { |
97aff369 |
1067 | dVAR; |
f3548bdc |
1068 | ASSERT_CURPAD_LEGAL("pad_swipe"); |
dd2155a4 |
1069 | if (!PL_curpad) |
1070 | return; |
1071 | if (AvARRAY(PL_comppad) != PL_curpad) |
1072 | Perl_croak(aTHX_ "panic: pad_swipe curpad"); |
1073 | if (!po) |
1074 | Perl_croak(aTHX_ "panic: pad_swipe po"); |
1075 | |
1076 | DEBUG_X(PerlIO_printf(Perl_debug_log, |
1077 | "Pad 0x%"UVxf"[0x%"UVxf"] swipe: %ld\n", |
1078 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po)); |
1079 | |
1080 | if (PL_curpad[po]) |
1081 | SvPADTMP_off(PL_curpad[po]); |
1082 | if (refadjust) |
1083 | SvREFCNT_dec(PL_curpad[po]); |
1084 | |
9ad9869c |
1085 | |
1086 | /* if pad tmps aren't shared between ops, then there's no need to |
1087 | * create a new tmp when an existing op is freed */ |
1088 | #ifdef USE_BROKEN_PAD_RESET |
561b68a9 |
1089 | PL_curpad[po] = newSV(0); |
dd2155a4 |
1090 | SvPADTMP_on(PL_curpad[po]); |
9ad9869c |
1091 | #else |
1092 | PL_curpad[po] = &PL_sv_undef; |
97bf4a8d |
1093 | #endif |
dd2155a4 |
1094 | if ((I32)po < PL_padix) |
1095 | PL_padix = po - 1; |
1096 | } |
1097 | |
1098 | |
1099 | /* |
1100 | =for apidoc pad_reset |
1101 | |
1102 | Mark all the current temporaries for reuse |
1103 | |
1104 | =cut |
1105 | */ |
1106 | |
1107 | /* XXX pad_reset() is currently disabled because it results in serious bugs. |
1108 | * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed |
1109 | * on the stack by OPs that use them, there are several ways to get an alias |
1110 | * to a shared TARG. Such an alias will change randomly and unpredictably. |
1111 | * We avoid doing this until we can think of a Better Way. |
1112 | * GSAR 97-10-29 */ |
1113 | void |
1114 | Perl_pad_reset(pTHX) |
1115 | { |
97aff369 |
1116 | dVAR; |
dd2155a4 |
1117 | #ifdef USE_BROKEN_PAD_RESET |
dd2155a4 |
1118 | if (AvARRAY(PL_comppad) != PL_curpad) |
1119 | Perl_croak(aTHX_ "panic: pad_reset curpad"); |
1120 | |
1121 | DEBUG_X(PerlIO_printf(Perl_debug_log, |
1122 | "Pad 0x%"UVxf"[0x%"UVxf"] reset: padix %ld -> %ld", |
1123 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), |
1124 | (long)PL_padix, (long)PL_padix_floor |
1125 | ) |
1126 | ); |
1127 | |
1128 | if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */ |
e1ec3a88 |
1129 | register I32 po; |
dd2155a4 |
1130 | for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) { |
1131 | if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po])) |
1132 | SvPADTMP_off(PL_curpad[po]); |
1133 | } |
1134 | PL_padix = PL_padix_floor; |
1135 | } |
1136 | #endif |
1137 | PL_pad_reset_pending = FALSE; |
1138 | } |
1139 | |
1140 | |
1141 | /* |
1142 | =for apidoc pad_tidy |
1143 | |
1144 | Tidy up a pad after we've finished compiling it: |
1145 | * remove most stuff from the pads of anonsub prototypes; |
1146 | * give it a @_; |
1147 | * mark tmps as such. |
1148 | |
1149 | =cut |
1150 | */ |
1151 | |
1152 | /* XXX DAPM surely most of this stuff should be done properly |
1153 | * at the right time beforehand, rather than going around afterwards |
1154 | * cleaning up our mistakes ??? |
1155 | */ |
1156 | |
1157 | void |
1158 | Perl_pad_tidy(pTHX_ padtidy_type type) |
1159 | { |
27da23d5 |
1160 | dVAR; |
dd2155a4 |
1161 | |
f3548bdc |
1162 | ASSERT_CURPAD_ACTIVE("pad_tidy"); |
b5c19bd7 |
1163 | |
1164 | /* If this CV has had any 'eval-capable' ops planted in it |
1165 | * (ie it contains eval '...', //ee, /$var/ or /(?{..})/), Then any |
1166 | * anon prototypes in the chain of CVs should be marked as cloneable, |
1167 | * so that for example the eval's CV in C<< sub { eval '$x' } >> gets |
1168 | * the right CvOUTSIDE. |
1169 | * If running with -d, *any* sub may potentially have an eval |
1170 | * excuted within it. |
1171 | */ |
1172 | |
1173 | if (PL_cv_has_eval || PL_perldb) { |
e1ec3a88 |
1174 | const CV *cv; |
b5c19bd7 |
1175 | for (cv = PL_compcv ;cv; cv = CvOUTSIDE(cv)) { |
1176 | if (cv != PL_compcv && CvCOMPILED(cv)) |
1177 | break; /* no need to mark already-compiled code */ |
1178 | if (CvANON(cv)) { |
1179 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
1180 | "Pad clone on cv=0x%"UVxf"\n", PTR2UV(cv))); |
1181 | CvCLONE_on(cv); |
1182 | } |
1183 | } |
1184 | } |
1185 | |
dd2155a4 |
1186 | /* extend curpad to match namepad */ |
1187 | if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad)) |
a0714e2c |
1188 | av_store(PL_comppad_name, AvFILLp(PL_comppad), NULL); |
dd2155a4 |
1189 | |
1190 | if (type == padtidy_SUBCLONE) { |
551405c4 |
1191 | SV * const * const namep = AvARRAY(PL_comppad_name); |
504618e9 |
1192 | PADOFFSET ix; |
b5c19bd7 |
1193 | |
dd2155a4 |
1194 | for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { |
1195 | SV *namesv; |
1196 | |
1197 | if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix])) |
1198 | continue; |
1199 | /* |
1200 | * The only things that a clonable function needs in its |
b5c19bd7 |
1201 | * pad are anonymous subs. |
dd2155a4 |
1202 | * The rest are created anew during cloning. |
1203 | */ |
a0714e2c |
1204 | if (!((namesv = namep[ix]) != NULL && |
dd2155a4 |
1205 | namesv != &PL_sv_undef && |
b15aece3 |
1206 | *SvPVX_const(namesv) == '&')) |
dd2155a4 |
1207 | { |
1208 | SvREFCNT_dec(PL_curpad[ix]); |
a0714e2c |
1209 | PL_curpad[ix] = NULL; |
dd2155a4 |
1210 | } |
1211 | } |
1212 | } |
1213 | else if (type == padtidy_SUB) { |
1214 | /* XXX DAPM this same bit of code keeps appearing !!! Rationalise? */ |
53c1dcc0 |
1215 | AV * const av = newAV(); /* Will be @_ */ |
dd2155a4 |
1216 | av_extend(av, 0); |
1217 | av_store(PL_comppad, 0, (SV*)av); |
11ca45c0 |
1218 | AvREIFY_only(av); |
dd2155a4 |
1219 | } |
1220 | |
1221 | /* XXX DAPM rationalise these two similar branches */ |
1222 | |
1223 | if (type == padtidy_SUB) { |
504618e9 |
1224 | PADOFFSET ix; |
dd2155a4 |
1225 | for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { |
1226 | if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix])) |
1227 | continue; |
1228 | if (!SvPADMY(PL_curpad[ix])) |
1229 | SvPADTMP_on(PL_curpad[ix]); |
1230 | } |
1231 | } |
1232 | else if (type == padtidy_FORMAT) { |
504618e9 |
1233 | PADOFFSET ix; |
dd2155a4 |
1234 | for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { |
1235 | if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix])) |
1236 | SvPADTMP_on(PL_curpad[ix]); |
1237 | } |
1238 | } |
f3548bdc |
1239 | PL_curpad = AvARRAY(PL_comppad); |
dd2155a4 |
1240 | } |
1241 | |
1242 | |
1243 | /* |
1244 | =for apidoc pad_free |
1245 | |
8627550a |
1246 | Free the SV at offset po in the current pad. |
dd2155a4 |
1247 | |
1248 | =cut |
1249 | */ |
1250 | |
1251 | /* XXX DAPM integrate with pad_swipe ???? */ |
1252 | void |
1253 | Perl_pad_free(pTHX_ PADOFFSET po) |
1254 | { |
97aff369 |
1255 | dVAR; |
f3548bdc |
1256 | ASSERT_CURPAD_LEGAL("pad_free"); |
dd2155a4 |
1257 | if (!PL_curpad) |
1258 | return; |
1259 | if (AvARRAY(PL_comppad) != PL_curpad) |
1260 | Perl_croak(aTHX_ "panic: pad_free curpad"); |
1261 | if (!po) |
1262 | Perl_croak(aTHX_ "panic: pad_free po"); |
1263 | |
1264 | DEBUG_X(PerlIO_printf(Perl_debug_log, |
1265 | "Pad 0x%"UVxf"[0x%"UVxf"] free: %ld\n", |
1266 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po) |
1267 | ); |
1268 | |
1269 | if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) { |
1270 | SvPADTMP_off(PL_curpad[po]); |
1271 | #ifdef USE_ITHREADS |
7e736055 |
1272 | /* SV could be a shared hash key (eg bugid #19022) */ |
1273 | if ( |
f8c7b90f |
1274 | #ifdef PERL_OLD_COPY_ON_WRITE |
7e736055 |
1275 | !SvIsCOW(PL_curpad[po]) |
1276 | #else |
1277 | !SvFAKE(PL_curpad[po]) |
dd2155a4 |
1278 | #endif |
7e736055 |
1279 | ) |
dd2155a4 |
1280 | SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */ |
dd2155a4 |
1281 | #endif |
1282 | } |
1283 | if ((I32)po < PL_padix) |
1284 | PL_padix = po - 1; |
1285 | } |
1286 | |
1287 | |
1288 | |
1289 | /* |
1290 | =for apidoc do_dump_pad |
1291 | |
1292 | Dump the contents of a padlist |
1293 | |
1294 | =cut |
1295 | */ |
1296 | |
1297 | void |
1298 | Perl_do_dump_pad(pTHX_ I32 level, PerlIO *file, PADLIST *padlist, int full) |
1299 | { |
97aff369 |
1300 | dVAR; |
e1ec3a88 |
1301 | const AV *pad_name; |
1302 | const AV *pad; |
dd2155a4 |
1303 | SV **pname; |
1304 | SV **ppad; |
dd2155a4 |
1305 | I32 ix; |
1306 | |
1307 | if (!padlist) { |
1308 | return; |
1309 | } |
1310 | pad_name = (AV*)*av_fetch((AV*)padlist, 0, FALSE); |
1311 | pad = (AV*)*av_fetch((AV*)padlist, 1, FALSE); |
1312 | pname = AvARRAY(pad_name); |
1313 | ppad = AvARRAY(pad); |
1314 | Perl_dump_indent(aTHX_ level, file, |
1315 | "PADNAME = 0x%"UVxf"(0x%"UVxf") PAD = 0x%"UVxf"(0x%"UVxf")\n", |
1316 | PTR2UV(pad_name), PTR2UV(pname), PTR2UV(pad), PTR2UV(ppad) |
1317 | ); |
1318 | |
1319 | for (ix = 1; ix <= AvFILLp(pad_name); ix++) { |
e1ec3a88 |
1320 | const SV *namesv = pname[ix]; |
dd2155a4 |
1321 | if (namesv && namesv == &PL_sv_undef) { |
a0714e2c |
1322 | namesv = NULL; |
dd2155a4 |
1323 | } |
1324 | if (namesv) { |
ee6cee0c |
1325 | if (SvFAKE(namesv)) |
1326 | Perl_dump_indent(aTHX_ level+1, file, |
c0fd1b42 |
1327 | "%2d. 0x%"UVxf"<%lu> FAKE \"%s\" flags=0x%lx index=%lu\n", |
ee6cee0c |
1328 | (int) ix, |
1329 | PTR2UV(ppad[ix]), |
1330 | (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0), |
b15aece3 |
1331 | SvPVX_const(namesv), |
b5c19bd7 |
1332 | (unsigned long)SvIVX(namesv), |
1333 | (unsigned long)SvNVX(namesv) |
1334 | |
ee6cee0c |
1335 | ); |
1336 | else |
1337 | Perl_dump_indent(aTHX_ level+1, file, |
b5c19bd7 |
1338 | "%2d. 0x%"UVxf"<%lu> (%ld,%ld) \"%s\"\n", |
ee6cee0c |
1339 | (int) ix, |
1340 | PTR2UV(ppad[ix]), |
1341 | (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0), |
4cf4a199 |
1342 | (long)U_32(SvNVX(namesv)), |
b5c19bd7 |
1343 | (long)SvIVX(namesv), |
b15aece3 |
1344 | SvPVX_const(namesv) |
ee6cee0c |
1345 | ); |
dd2155a4 |
1346 | } |
1347 | else if (full) { |
1348 | Perl_dump_indent(aTHX_ level+1, file, |
1349 | "%2d. 0x%"UVxf"<%lu>\n", |
1350 | (int) ix, |
1351 | PTR2UV(ppad[ix]), |
1352 | (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0) |
1353 | ); |
1354 | } |
1355 | } |
1356 | } |
1357 | |
1358 | |
1359 | |
1360 | /* |
1361 | =for apidoc cv_dump |
1362 | |
1363 | dump the contents of a CV |
1364 | |
1365 | =cut |
1366 | */ |
1367 | |
1368 | #ifdef DEBUGGING |
1369 | STATIC void |
e1ec3a88 |
1370 | S_cv_dump(pTHX_ const CV *cv, const char *title) |
dd2155a4 |
1371 | { |
97aff369 |
1372 | dVAR; |
53c1dcc0 |
1373 | const CV * const outside = CvOUTSIDE(cv); |
1374 | AV* const padlist = CvPADLIST(cv); |
dd2155a4 |
1375 | |
1376 | PerlIO_printf(Perl_debug_log, |
1377 | " %s: CV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n", |
1378 | title, |
1379 | PTR2UV(cv), |
1380 | (CvANON(cv) ? "ANON" |
71f882da |
1381 | : (SvTYPE(cv) == SVt_PVFM) ? "FORMAT" |
dd2155a4 |
1382 | : (cv == PL_main_cv) ? "MAIN" |
1383 | : CvUNIQUE(cv) ? "UNIQUE" |
1384 | : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"), |
1385 | PTR2UV(outside), |
1386 | (!outside ? "null" |
1387 | : CvANON(outside) ? "ANON" |
1388 | : (outside == PL_main_cv) ? "MAIN" |
1389 | : CvUNIQUE(outside) ? "UNIQUE" |
1390 | : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED")); |
1391 | |
1392 | PerlIO_printf(Perl_debug_log, |
1393 | " PADLIST = 0x%"UVxf"\n", PTR2UV(padlist)); |
1394 | do_dump_pad(1, Perl_debug_log, padlist, 1); |
1395 | } |
1396 | #endif /* DEBUGGING */ |
1397 | |
1398 | |
1399 | |
1400 | |
1401 | |
1402 | /* |
1403 | =for apidoc cv_clone |
1404 | |
1405 | Clone a CV: make a new CV which points to the same code etc, but which |
1406 | has a newly-created pad built by copying the prototype pad and capturing |
1407 | any outer lexicals. |
1408 | |
1409 | =cut |
1410 | */ |
1411 | |
1412 | CV * |
1413 | Perl_cv_clone(pTHX_ CV *proto) |
1414 | { |
27da23d5 |
1415 | dVAR; |
dd2155a4 |
1416 | I32 ix; |
53c1dcc0 |
1417 | AV* const protopadlist = CvPADLIST(proto); |
1418 | const AV* const protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE); |
1419 | const AV* const protopad = (AV*)*av_fetch(protopadlist, 1, FALSE); |
1420 | SV** const pname = AvARRAY(protopad_name); |
1421 | SV** const ppad = AvARRAY(protopad); |
e1ec3a88 |
1422 | const I32 fname = AvFILLp(protopad_name); |
1423 | const I32 fpad = AvFILLp(protopad); |
dd2155a4 |
1424 | CV* cv; |
b5c19bd7 |
1425 | SV** outpad; |
1426 | CV* outside; |
71f882da |
1427 | long depth; |
dd2155a4 |
1428 | |
1429 | assert(!CvUNIQUE(proto)); |
1430 | |
71f882da |
1431 | /* Since cloneable anon subs can be nested, CvOUTSIDE may point |
1432 | * to a prototype; we instead want the cloned parent who called us. |
1433 | * Note that in general for formats, CvOUTSIDE != find_runcv */ |
1434 | |
1435 | outside = CvOUTSIDE(proto); |
1436 | if (outside && CvCLONE(outside) && ! CvCLONED(outside)) |
1437 | outside = find_runcv(NULL); |
1438 | depth = CvDEPTH(outside); |
1439 | assert(depth || SvTYPE(proto) == SVt_PVFM); |
1440 | if (!depth) |
1441 | depth = 1; |
b5c19bd7 |
1442 | assert(CvPADLIST(outside)); |
1443 | |
dd2155a4 |
1444 | ENTER; |
1445 | SAVESPTR(PL_compcv); |
1446 | |
561b68a9 |
1447 | cv = PL_compcv = (CV*)newSV(0); |
dd2155a4 |
1448 | sv_upgrade((SV *)cv, SvTYPE(proto)); |
7dafbf52 |
1449 | CvFLAGS(cv) = CvFLAGS(proto) & ~(CVf_CLONE|CVf_WEAKOUTSIDE); |
dd2155a4 |
1450 | CvCLONED_on(cv); |
1451 | |
dd2155a4 |
1452 | #ifdef USE_ITHREADS |
aed2304a |
1453 | CvFILE(cv) = CvISXSUB(proto) ? CvFILE(proto) |
1454 | : savepv(CvFILE(proto)); |
dd2155a4 |
1455 | #else |
1456 | CvFILE(cv) = CvFILE(proto); |
1457 | #endif |
1458 | CvGV(cv) = CvGV(proto); |
1459 | CvSTASH(cv) = CvSTASH(proto); |
b34c0dd4 |
1460 | OP_REFCNT_LOCK; |
dd2155a4 |
1461 | CvROOT(cv) = OpREFCNT_inc(CvROOT(proto)); |
b34c0dd4 |
1462 | OP_REFCNT_UNLOCK; |
dd2155a4 |
1463 | CvSTART(cv) = CvSTART(proto); |
b37c2d43 |
1464 | CvOUTSIDE(cv) = (CV*)SvREFCNT_inc_simple(outside); |
b5c19bd7 |
1465 | CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(proto); |
dd2155a4 |
1466 | |
1467 | if (SvPOK(proto)) |
b15aece3 |
1468 | sv_setpvn((SV*)cv, SvPVX_const(proto), SvCUR(proto)); |
dd2155a4 |
1469 | |
b7787f18 |
1470 | CvPADLIST(cv) = pad_new(padnew_CLONE|padnew_SAVE); |
dd2155a4 |
1471 | |
b5c19bd7 |
1472 | av_fill(PL_comppad, fpad); |
dd2155a4 |
1473 | for (ix = fname; ix >= 0; ix--) |
1474 | av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix])); |
1475 | |
dd2155a4 |
1476 | PL_curpad = AvARRAY(PL_comppad); |
1477 | |
71f882da |
1478 | outpad = AvARRAY(AvARRAY(CvPADLIST(outside))[depth]); |
b5c19bd7 |
1479 | |
dd2155a4 |
1480 | for (ix = fpad; ix > 0; ix--) { |
a0714e2c |
1481 | SV* const namesv = (ix <= fname) ? pname[ix] : NULL; |
1482 | SV *sv = NULL; |
71f882da |
1483 | if (namesv && namesv != &PL_sv_undef) { /* lexical */ |
b5c19bd7 |
1484 | if (SvFAKE(namesv)) { /* lexical from outside? */ |
71f882da |
1485 | sv = outpad[(I32)SvNVX(namesv)]; |
1486 | assert(sv); |
1487 | /* formats may have an inactive parent */ |
1488 | if (SvTYPE(proto) == SVt_PVFM && SvPADSTALE(sv)) { |
1489 | if (ckWARN(WARN_CLOSURE)) |
1490 | Perl_warner(aTHX_ packWARN(WARN_CLOSURE), |
b15aece3 |
1491 | "Variable \"%s\" is not available", SvPVX_const(namesv)); |
a0714e2c |
1492 | sv = NULL; |
71f882da |
1493 | } |
1494 | else { |
1495 | assert(!SvPADSTALE(sv)); |
b37c2d43 |
1496 | SvREFCNT_inc_simple_void(sv); |
71f882da |
1497 | } |
dd2155a4 |
1498 | } |
71f882da |
1499 | if (!sv) { |
b15aece3 |
1500 | const char sigil = SvPVX_const(namesv)[0]; |
e1ec3a88 |
1501 | if (sigil == '&') |
dd2155a4 |
1502 | sv = SvREFCNT_inc(ppad[ix]); |
e1ec3a88 |
1503 | else if (sigil == '@') |
dd2155a4 |
1504 | sv = (SV*)newAV(); |
e1ec3a88 |
1505 | else if (sigil == '%') |
dd2155a4 |
1506 | sv = (SV*)newHV(); |
1507 | else |
561b68a9 |
1508 | sv = newSV(0); |
235cc2e3 |
1509 | SvPADMY_on(sv); |
dd2155a4 |
1510 | } |
1511 | } |
1512 | else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) { |
71f882da |
1513 | sv = SvREFCNT_inc(ppad[ix]); |
dd2155a4 |
1514 | } |
1515 | else { |
561b68a9 |
1516 | sv = newSV(0); |
dd2155a4 |
1517 | SvPADTMP_on(sv); |
dd2155a4 |
1518 | } |
71f882da |
1519 | PL_curpad[ix] = sv; |
dd2155a4 |
1520 | } |
1521 | |
dd2155a4 |
1522 | DEBUG_Xv( |
1523 | PerlIO_printf(Perl_debug_log, "\nPad CV clone\n"); |
1524 | cv_dump(outside, "Outside"); |
1525 | cv_dump(proto, "Proto"); |
1526 | cv_dump(cv, "To"); |
1527 | ); |
1528 | |
1529 | LEAVE; |
1530 | |
1531 | if (CvCONST(cv)) { |
b5c19bd7 |
1532 | /* Constant sub () { $x } closing over $x - see lib/constant.pm: |
1533 | * The prototype was marked as a candiate for const-ization, |
1534 | * so try to grab the current const value, and if successful, |
1535 | * turn into a const sub: |
1536 | */ |
551405c4 |
1537 | SV* const const_sv = op_const_sv(CvSTART(cv), cv); |
b5c19bd7 |
1538 | if (const_sv) { |
1539 | SvREFCNT_dec(cv); |
bd61b366 |
1540 | cv = newCONSTSUB(CvSTASH(proto), NULL, const_sv); |
b5c19bd7 |
1541 | } |
1542 | else { |
1543 | CvCONST_off(cv); |
1544 | } |
dd2155a4 |
1545 | } |
1546 | |
1547 | return cv; |
1548 | } |
1549 | |
1550 | |
1551 | /* |
1552 | =for apidoc pad_fixup_inner_anons |
1553 | |
1554 | For any anon CVs in the pad, change CvOUTSIDE of that CV from |
7dafbf52 |
1555 | old_cv to new_cv if necessary. Needed when a newly-compiled CV has to be |
1556 | moved to a pre-existing CV struct. |
dd2155a4 |
1557 | |
1558 | =cut |
1559 | */ |
1560 | |
1561 | void |
1562 | Perl_pad_fixup_inner_anons(pTHX_ PADLIST *padlist, CV *old_cv, CV *new_cv) |
1563 | { |
97aff369 |
1564 | dVAR; |
dd2155a4 |
1565 | I32 ix; |
66a1b24b |
1566 | AV * const comppad_name = (AV*)AvARRAY(padlist)[0]; |
1567 | AV * const comppad = (AV*)AvARRAY(padlist)[1]; |
53c1dcc0 |
1568 | SV ** const namepad = AvARRAY(comppad_name); |
1569 | SV ** const curpad = AvARRAY(comppad); |
294a48e9 |
1570 | PERL_UNUSED_ARG(old_cv); |
1571 | |
dd2155a4 |
1572 | for (ix = AvFILLp(comppad_name); ix > 0; ix--) { |
551405c4 |
1573 | const SV * const namesv = namepad[ix]; |
dd2155a4 |
1574 | if (namesv && namesv != &PL_sv_undef |
b15aece3 |
1575 | && *SvPVX_const(namesv) == '&') |
dd2155a4 |
1576 | { |
46c461b5 |
1577 | CV * const innercv = (CV*)curpad[ix]; |
7dafbf52 |
1578 | assert(CvWEAKOUTSIDE(innercv)); |
1579 | assert(CvOUTSIDE(innercv) == old_cv); |
1580 | CvOUTSIDE(innercv) = new_cv; |
dd2155a4 |
1581 | } |
1582 | } |
1583 | } |
1584 | |
7dafbf52 |
1585 | |
dd2155a4 |
1586 | /* |
1587 | =for apidoc pad_push |
1588 | |
1589 | Push a new pad frame onto the padlist, unless there's already a pad at |
26019298 |
1590 | this depth, in which case don't bother creating a new one. Then give |
1591 | the new pad an @_ in slot zero. |
dd2155a4 |
1592 | |
1593 | =cut |
1594 | */ |
1595 | |
1596 | void |
26019298 |
1597 | Perl_pad_push(pTHX_ PADLIST *padlist, int depth) |
dd2155a4 |
1598 | { |
97aff369 |
1599 | dVAR; |
b37c2d43 |
1600 | if (depth > AvFILLp(padlist)) { |
44f8325f |
1601 | SV** const svp = AvARRAY(padlist); |
1602 | AV* const newpad = newAV(); |
1603 | SV** const oldpad = AvARRAY(svp[depth-1]); |
dd2155a4 |
1604 | I32 ix = AvFILLp((AV*)svp[1]); |
e1ec3a88 |
1605 | const I32 names_fill = AvFILLp((AV*)svp[0]); |
44f8325f |
1606 | SV** const names = AvARRAY(svp[0]); |
26019298 |
1607 | AV *av; |
1608 | |
dd2155a4 |
1609 | for ( ;ix > 0; ix--) { |
1610 | if (names_fill >= ix && names[ix] != &PL_sv_undef) { |
b15aece3 |
1611 | const char sigil = SvPVX_const(names[ix])[0]; |
26019298 |
1612 | if ((SvFLAGS(names[ix]) & SVf_FAKE) || sigil == '&') { |
dd2155a4 |
1613 | /* outer lexical or anon code */ |
1614 | av_store(newpad, ix, SvREFCNT_inc(oldpad[ix])); |
1615 | } |
1616 | else { /* our own lexical */ |
26019298 |
1617 | SV *sv; |
1618 | if (sigil == '@') |
1619 | sv = (SV*)newAV(); |
1620 | else if (sigil == '%') |
1621 | sv = (SV*)newHV(); |
dd2155a4 |
1622 | else |
561b68a9 |
1623 | sv = newSV(0); |
26019298 |
1624 | av_store(newpad, ix, sv); |
dd2155a4 |
1625 | SvPADMY_on(sv); |
1626 | } |
1627 | } |
1628 | else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) { |
26019298 |
1629 | av_store(newpad, ix, SvREFCNT_inc(oldpad[ix])); |
dd2155a4 |
1630 | } |
1631 | else { |
1632 | /* save temporaries on recursion? */ |
561b68a9 |
1633 | SV * const sv = newSV(0); |
26019298 |
1634 | av_store(newpad, ix, sv); |
dd2155a4 |
1635 | SvPADTMP_on(sv); |
1636 | } |
1637 | } |
26019298 |
1638 | av = newAV(); |
1639 | av_extend(av, 0); |
1640 | av_store(newpad, 0, (SV*)av); |
11ca45c0 |
1641 | AvREIFY_only(av); |
26019298 |
1642 | |
dd2155a4 |
1643 | av_store(padlist, depth, (SV*)newpad); |
1644 | AvFILLp(padlist) = depth; |
1645 | } |
1646 | } |
b21dc031 |
1647 | |
1648 | |
1649 | HV * |
1650 | Perl_pad_compname_type(pTHX_ const PADOFFSET po) |
1651 | { |
97aff369 |
1652 | dVAR; |
551405c4 |
1653 | SV* const * const av = av_fetch(PL_comppad_name, po, FALSE); |
00b1698f |
1654 | if ( SvPAD_TYPED(*av) ) { |
b21dc031 |
1655 | return SvSTASH(*av); |
1656 | } |
5c284bb0 |
1657 | return NULL; |
b21dc031 |
1658 | } |
66610fdd |
1659 | |
1660 | /* |
1661 | * Local variables: |
1662 | * c-indentation-style: bsd |
1663 | * c-basic-offset: 4 |
1664 | * indent-tabs-mode: t |
1665 | * End: |
1666 | * |
37442d52 |
1667 | * ex: set ts=8 sts=4 sw=4 noet: |
1668 | */ |