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