Commit | Line | Data |
---|---|---|
dd2155a4 DM |
1 | /* pad.c |
2 | * | |
1129b882 NC |
3 | * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
4 | * by Larry Wall and others | |
dd2155a4 DM |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
4ac71550 TC |
8 | */ |
9 | ||
10 | /* | |
11 | * 'Anyway: there was this Mr. Frodo left an orphan and stranded, as you | |
12 | * might say, among those queer Bucklanders, being brought up anyhow in | |
13 | * Brandy Hall. A regular warren, by all accounts. Old Master Gorbadoc | |
14 | * never had fewer than a couple of hundred relations in the place. | |
15 | * Mr. Bilbo never did a kinder deed than when he brought the lad back | |
16 | * to live among decent folk.' --the Gaffer | |
dd2155a4 | 17 | * |
4ac71550 | 18 | * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"] |
dd2155a4 DM |
19 | */ |
20 | ||
21 | /* XXX DAPM | |
22 | * As of Sept 2002, this file is new and may be in a state of flux for | |
23 | * a while. I've marked things I intent to come back and look at further | |
24 | * with an 'XXX DAPM' comment. | |
25 | */ | |
26 | ||
27 | /* | |
28 | =head1 Pad Data Structures | |
29 | ||
cc76b5cc | 30 | =for apidoc Amx|PADLIST *|CvPADLIST|CV *cv |
166f8a29 | 31 | |
cc76b5cc Z |
32 | CV's can have CvPADLIST(cv) set to point to an AV. This is the CV's |
33 | scratchpad, which stores lexical variables and opcode temporary and | |
34 | per-thread values. | |
dd2155a4 DM |
35 | |
36 | For these purposes "forms" are a kind-of CV, eval""s are too (except they're | |
37 | not callable at will and are always thrown away after the eval"" is done | |
b5c19bd7 DM |
38 | executing). Require'd files are simply evals without any outer lexical |
39 | scope. | |
dd2155a4 DM |
40 | |
41 | XSUBs don't have CvPADLIST set - dXSTARG fetches values from PL_curpad, | |
42 | but that is really the callers pad (a slot of which is allocated by | |
43 | every entersub). | |
44 | ||
45 | The CvPADLIST AV has does not have AvREAL set, so REFCNT of component items | |
f3548bdc | 46 | is managed "manual" (mostly in pad.c) rather than normal av.c rules. |
dd2155a4 DM |
47 | The items in the AV are not SVs as for a normal AV, but other AVs: |
48 | ||
49 | 0'th Entry of the CvPADLIST is an AV which represents the "names" or rather | |
50 | the "static type information" for lexicals. | |
51 | ||
52 | The CvDEPTH'th entry of CvPADLIST AV is an AV which is the stack frame at that | |
53 | depth of recursion into the CV. | |
54 | The 0'th slot of a frame AV is an AV which is @_. | |
55 | other entries are storage for variables and op targets. | |
56 | ||
f3548bdc | 57 | Iterating over the names AV iterates over all possible pad |
dd2155a4 DM |
58 | items. Pad slots that are SVs_PADTMP (targets/GVs/constants) end up having |
59 | &PL_sv_undef "names" (see pad_alloc()). | |
60 | ||
61 | Only my/our variable (SVs_PADMY/SVs_PADOUR) slots get valid names. | |
62 | The rest are op targets/GVs/constants which are statically allocated | |
63 | or resolved at compile time. These don't have names by which they | |
64 | can be looked up from Perl code at run time through eval"" like | |
65 | my/our variables can be. Since they can't be looked up by "name" | |
66 | but only by their index allocated at compile time (which is usually | |
67 | in PL_op->op_targ), wasting a name SV for them doesn't make sense. | |
68 | ||
69 | The SVs in the names AV have their PV being the name of the variable. | |
3441fb63 | 70 | xlow+1..xhigh inclusive in the NV union is a range of cop_seq numbers for |
61f4bfbf DM |
71 | which the name is valid (accessed through the macros COP_SEQ_RANGE_LOW and |
72 | _HIGH). During compilation, these fields may hold the special value | |
73 | PERL_PADSEQ_INTRO to indicate various stages: | |
0d311cdb DM |
74 | |
75 | COP_SEQ_RANGE_LOW _HIGH | |
76 | ----------------- ----- | |
77 | PERL_PADSEQ_INTRO 0 variable not yet introduced: { my ($x | |
78 | valid-seq# PERL_PADSEQ_INTRO variable in scope: { my ($x) | |
79 | valid-seq# valid-seq# compilation of scope complete: { my ($x) } | |
80 | ||
81 | For typed lexicals name SV is SVt_PVMG and SvSTASH | |
3441fb63 | 82 | points at the type. For C<our> lexicals, the type is also SVt_PVMG, with the |
73d95100 | 83 | SvOURSTASH slot pointing at the stash of the associated global (so that |
931b58fb | 84 | duplicate C<our> declarations in the same package can be detected). SvUVX is |
3441fb63 | 85 | sometimes hijacked to store the generation number during compilation. |
dd2155a4 | 86 | |
b5c19bd7 DM |
87 | If SvFAKE is set on the name SV, then that slot in the frame AV is |
88 | a REFCNT'ed reference to a lexical from "outside". In this case, | |
3441fb63 NC |
89 | the name SV does not use xlow and xhigh to store a cop_seq range, since it is |
90 | in scope throughout. Instead xhigh stores some flags containing info about | |
b5c19bd7 | 91 | the real lexical (is it declared in an anon, and is it capable of being |
3441fb63 | 92 | instantiated multiple times?), and for fake ANONs, xlow contains the index |
b5c19bd7 DM |
93 | within the parent's pad where the lexical's value is stored, to make |
94 | cloning quicker. | |
dd2155a4 | 95 | |
a6d05634 | 96 | If the 'name' is '&' the corresponding entry in frame AV |
dd2155a4 DM |
97 | is a CV representing a possible closure. |
98 | (SvFAKE and name of '&' is not a meaningful combination currently but could | |
99 | become so if C<my sub foo {}> is implemented.) | |
100 | ||
71f882da DM |
101 | Note that formats are treated as anon subs, and are cloned each time |
102 | write is called (if necessary). | |
103 | ||
ab8e66c1 | 104 | The flag SVs_PADSTALE is cleared on lexicals each time the my() is executed, |
e6e7068b DM |
105 | and set on scope exit. This allows the 'Variable $x is not available' warning |
106 | to be generated in evals, such as | |
107 | ||
108 | { my $x = 1; sub f { eval '$x'} } f(); | |
109 | ||
ab8e66c1 | 110 | For state vars, SVs_PADSTALE is overloaded to mean 'not yet initialised' |
d1186544 | 111 | |
cc76b5cc Z |
112 | =for apidoc AmxU|AV *|PL_comppad_name |
113 | ||
114 | During compilation, this points to the array containing the names part | |
115 | of the pad for the currently-compiling code. | |
116 | ||
117 | =for apidoc AmxU|AV *|PL_comppad | |
118 | ||
119 | During compilation, this points to the array containing the values | |
120 | part of the pad for the currently-compiling code. (At runtime a CV may | |
121 | have many such value arrays; at compile time just one is constructed.) | |
122 | At runtime, this points to the array containing the currently-relevant | |
123 | values for the pad for the currently-executing code. | |
124 | ||
125 | =for apidoc AmxU|SV **|PL_curpad | |
126 | ||
127 | Points directly to the body of the L</PL_comppad> array. | |
128 | (I.e., this is C<AvARRAY(PL_comppad)>.) | |
129 | ||
dd2155a4 DM |
130 | =cut |
131 | */ | |
132 | ||
133 | ||
134 | #include "EXTERN.h" | |
135 | #define PERL_IN_PAD_C | |
136 | #include "perl.h" | |
952306ac | 137 | #include "keywords.h" |
dd2155a4 | 138 | |
3441fb63 NC |
139 | #define COP_SEQ_RANGE_LOW_set(sv,val) \ |
140 | STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xlow = (val); } STMT_END | |
141 | #define COP_SEQ_RANGE_HIGH_set(sv,val) \ | |
142 | STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xhigh = (val); } STMT_END | |
809abb02 | 143 | |
3441fb63 NC |
144 | #define PARENT_PAD_INDEX_set(sv,val) \ |
145 | STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xlow = (val); } STMT_END | |
146 | #define PARENT_FAKELEX_FLAGS_set(sv,val) \ | |
147 | STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xhigh = (val); } STMT_END | |
dd2155a4 | 148 | |
cc76b5cc Z |
149 | /* |
150 | =for apidoc mx|void|pad_peg|const char *s | |
151 | ||
152 | When PERL_MAD is enabled, this is a small no-op function that gets called | |
153 | at the start of each pad-related function. It can be breakpointed to | |
154 | track all pad operations. The parameter is a string indicating the type | |
155 | of pad operation being performed. | |
156 | ||
157 | =cut | |
158 | */ | |
159 | ||
1dba731d NC |
160 | #ifdef PERL_MAD |
161 | void pad_peg(const char* s) { | |
790427a5 DM |
162 | static int pegcnt; /* XXX not threadsafe */ |
163 | PERL_UNUSED_ARG(s); | |
7918f24d NC |
164 | |
165 | PERL_ARGS_ASSERT_PAD_PEG; | |
166 | ||
1dba731d NC |
167 | pegcnt++; |
168 | } | |
169 | #endif | |
dd2155a4 DM |
170 | |
171 | /* | |
cc76b5cc | 172 | =for apidoc Am|PADLIST *|pad_new|int flags |
dd2155a4 | 173 | |
cc76b5cc Z |
174 | Create a new padlist, updating the global variables for the |
175 | currently-compiling padlist to point to the new padlist. The following | |
176 | flags can be OR'ed together: | |
dd2155a4 DM |
177 | |
178 | padnew_CLONE this pad is for a cloned CV | |
cc76b5cc | 179 | padnew_SAVE save old globals on the save stack |
dd2155a4 DM |
180 | padnew_SAVESUB also save extra stuff for start of sub |
181 | ||
182 | =cut | |
183 | */ | |
184 | ||
185 | PADLIST * | |
c7c737cb | 186 | Perl_pad_new(pTHX_ int flags) |
dd2155a4 | 187 | { |
97aff369 | 188 | dVAR; |
e1ec3a88 | 189 | AV *padlist, *padname, *pad; |
7a6072a8 | 190 | SV **ary; |
dd2155a4 | 191 | |
f3548bdc DM |
192 | ASSERT_CURPAD_LEGAL("pad_new"); |
193 | ||
dd2155a4 DM |
194 | /* XXX DAPM really need a new SAVEt_PAD which restores all or most |
195 | * vars (based on flags) rather than storing vals + addresses for | |
196 | * each individually. Also see pad_block_start. | |
197 | * XXX DAPM Try to see whether all these conditionals are required | |
198 | */ | |
199 | ||
200 | /* save existing state, ... */ | |
201 | ||
202 | if (flags & padnew_SAVE) { | |
3979c56f | 203 | SAVECOMPPAD(); |
dd2155a4 DM |
204 | SAVESPTR(PL_comppad_name); |
205 | if (! (flags & padnew_CLONE)) { | |
206 | SAVEI32(PL_padix); | |
207 | SAVEI32(PL_comppad_name_fill); | |
208 | SAVEI32(PL_min_intro_pending); | |
209 | SAVEI32(PL_max_intro_pending); | |
8bbe96d7 | 210 | SAVEBOOL(PL_cv_has_eval); |
dd2155a4 | 211 | if (flags & padnew_SAVESUB) { |
f0cb02e3 | 212 | SAVEBOOL(PL_pad_reset_pending); |
dd2155a4 DM |
213 | } |
214 | } | |
215 | } | |
216 | /* XXX DAPM interestingly, PL_comppad_name_floor never seems to be | |
217 | * saved - check at some pt that this is okay */ | |
218 | ||
219 | /* ... create new pad ... */ | |
220 | ||
221 | padlist = newAV(); | |
222 | padname = newAV(); | |
223 | pad = newAV(); | |
224 | ||
225 | if (flags & padnew_CLONE) { | |
226 | /* XXX DAPM I dont know why cv_clone needs it | |
227 | * doing differently yet - perhaps this separate branch can be | |
228 | * dispensed with eventually ??? | |
229 | */ | |
230 | ||
e1ec3a88 | 231 | AV * const a0 = newAV(); /* will be @_ */ |
ad64d0ec | 232 | av_store(pad, 0, MUTABLE_SV(a0)); |
11ca45c0 | 233 | AvREIFY_only(a0); |
dd2155a4 DM |
234 | } |
235 | else { | |
a0714e2c | 236 | av_store(pad, 0, NULL); |
dd2155a4 DM |
237 | } |
238 | ||
239 | AvREAL_off(padlist); | |
7a6072a8 NC |
240 | /* Most subroutines never recurse, hence only need 2 entries in the padlist |
241 | array - names, and depth=1. The default for av_store() is to allocate | |
242 | 0..3, and even an explicit call to av_extend() with <3 will be rounded | |
243 | up, so we inline the allocation of the array here. */ | |
244 | Newx(ary, 2, SV*); | |
245 | AvFILLp(padlist) = 1; | |
246 | AvMAX(padlist) = 1; | |
247 | AvALLOC(padlist) = ary; | |
248 | AvARRAY(padlist) = ary; | |
249 | ary[0] = MUTABLE_SV(padname); | |
250 | ary[1] = MUTABLE_SV(pad); | |
dd2155a4 DM |
251 | |
252 | /* ... then update state variables */ | |
253 | ||
403799bf NC |
254 | PL_comppad_name = padname; |
255 | PL_comppad = pad; | |
256 | PL_curpad = AvARRAY(pad); | |
dd2155a4 DM |
257 | |
258 | if (! (flags & padnew_CLONE)) { | |
259 | PL_comppad_name_fill = 0; | |
260 | PL_min_intro_pending = 0; | |
261 | PL_padix = 0; | |
b5c19bd7 | 262 | PL_cv_has_eval = 0; |
dd2155a4 DM |
263 | } |
264 | ||
265 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
b5c19bd7 | 266 | "Pad 0x%"UVxf"[0x%"UVxf"] new: compcv=0x%"UVxf |
dd2155a4 | 267 | " name=0x%"UVxf" flags=0x%"UVxf"\n", |
b5c19bd7 | 268 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), PTR2UV(PL_compcv), |
dd2155a4 DM |
269 | PTR2UV(padname), (UV)flags |
270 | ) | |
271 | ); | |
272 | ||
273 | return (PADLIST*)padlist; | |
274 | } | |
275 | ||
dd2155a4 | 276 | |
c4528262 NC |
277 | /* |
278 | =head1 Embedding Functions | |
279 | ||
280 | =for apidoc cv_undef | |
281 | ||
282 | Clear out all the active components of a CV. This can happen either | |
283 | by an explicit C<undef &foo>, or by the reference count going to zero. | |
284 | In the former case, we keep the CvOUTSIDE pointer, so that any anonymous | |
285 | children can still follow the full lexical scope chain. | |
286 | ||
287 | =cut | |
288 | */ | |
289 | ||
290 | void | |
291 | Perl_cv_undef(pTHX_ CV *cv) | |
292 | { | |
293 | dVAR; | |
c2736fce | 294 | const PADLIST *padlist = CvPADLIST(cv); |
c4528262 NC |
295 | |
296 | PERL_ARGS_ASSERT_CV_UNDEF; | |
297 | ||
298 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
299 | "CV undef: cv=0x%"UVxf" comppad=0x%"UVxf"\n", | |
300 | PTR2UV(cv), PTR2UV(PL_comppad)) | |
301 | ); | |
302 | ||
303 | #ifdef USE_ITHREADS | |
304 | if (CvFILE(cv) && !CvISXSUB(cv)) { | |
305 | /* for XSUBs CvFILE point directly to static memory; __FILE__ */ | |
306 | Safefree(CvFILE(cv)); | |
307 | } | |
308 | CvFILE(cv) = NULL; | |
309 | #endif | |
dd2155a4 | 310 | |
c4528262 NC |
311 | if (!CvISXSUB(cv) && CvROOT(cv)) { |
312 | if (SvTYPE(cv) == SVt_PVCV && CvDEPTH(cv)) | |
313 | Perl_croak(aTHX_ "Can't undef active subroutine"); | |
314 | ENTER; | |
315 | ||
316 | PAD_SAVE_SETNULLPAD(); | |
317 | ||
318 | op_free(CvROOT(cv)); | |
319 | CvROOT(cv) = NULL; | |
320 | CvSTART(cv) = NULL; | |
321 | LEAVE; | |
322 | } | |
323 | SvPOK_off(MUTABLE_SV(cv)); /* forget prototype */ | |
324 | CvGV_set(cv, NULL); | |
325 | ||
c2736fce NC |
326 | /* This statement and the subsequence if block was pad_undef(). */ |
327 | pad_peg("pad_undef"); | |
328 | ||
329 | if (padlist && !SvIS_FREED(padlist) /* may be during global destruction */ | |
330 | ) { | |
331 | I32 ix; | |
332 | ||
333 | /* Free the padlist associated with a CV. | |
334 | If parts of it happen to be current, we null the relevant PL_*pad* | |
335 | global vars so that we don't have any dangling references left. | |
336 | We also repoint the CvOUTSIDE of any about-to-be-orphaned inner | |
337 | subs to the outer of this cv. */ | |
338 | ||
339 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
340 | "Pad undef: cv=0x%"UVxf" padlist=0x%"UVxf" comppad=0x%"UVxf"\n", | |
341 | PTR2UV(cv), PTR2UV(padlist), PTR2UV(PL_comppad)) | |
342 | ); | |
343 | ||
344 | /* detach any '&' anon children in the pad; if afterwards they | |
345 | * are still live, fix up their CvOUTSIDEs to point to our outside, | |
346 | * bypassing us. */ | |
347 | /* XXX DAPM for efficiency, we should only do this if we know we have | |
348 | * children, or integrate this loop with general cleanup */ | |
349 | ||
350 | if (PL_phase != PERL_PHASE_DESTRUCT) { /* don't bother during global destruction */ | |
351 | CV * const outercv = CvOUTSIDE(cv); | |
352 | const U32 seq = CvOUTSIDE_SEQ(cv); | |
353 | AV * const comppad_name = MUTABLE_AV(AvARRAY(padlist)[0]); | |
354 | SV ** const namepad = AvARRAY(comppad_name); | |
355 | AV * const comppad = MUTABLE_AV(AvARRAY(padlist)[1]); | |
356 | SV ** const curpad = AvARRAY(comppad); | |
357 | for (ix = AvFILLp(comppad_name); ix > 0; ix--) { | |
358 | SV * const namesv = namepad[ix]; | |
359 | if (namesv && namesv != &PL_sv_undef | |
360 | && *SvPVX_const(namesv) == '&') | |
361 | { | |
362 | CV * const innercv = MUTABLE_CV(curpad[ix]); | |
363 | U32 inner_rc = SvREFCNT(innercv); | |
364 | assert(inner_rc); | |
365 | namepad[ix] = NULL; | |
366 | SvREFCNT_dec(namesv); | |
367 | ||
368 | if (SvREFCNT(comppad) < 2) { /* allow for /(?{ sub{} })/ */ | |
369 | curpad[ix] = NULL; | |
370 | SvREFCNT_dec(innercv); | |
371 | inner_rc--; | |
372 | } | |
373 | ||
374 | /* in use, not just a prototype */ | |
375 | if (inner_rc && (CvOUTSIDE(innercv) == cv)) { | |
376 | assert(CvWEAKOUTSIDE(innercv)); | |
377 | /* don't relink to grandfather if he's being freed */ | |
378 | if (outercv && SvREFCNT(outercv)) { | |
379 | CvWEAKOUTSIDE_off(innercv); | |
380 | CvOUTSIDE(innercv) = outercv; | |
381 | CvOUTSIDE_SEQ(innercv) = seq; | |
382 | SvREFCNT_inc_simple_void_NN(outercv); | |
383 | } | |
384 | else { | |
385 | CvOUTSIDE(innercv) = NULL; | |
386 | } | |
387 | } | |
388 | } | |
389 | } | |
390 | } | |
391 | ||
392 | ix = AvFILLp(padlist); | |
aa2f79cf | 393 | while (ix > 0) { |
c2736fce NC |
394 | SV* const sv = AvARRAY(padlist)[ix--]; |
395 | if (sv) { | |
aa2f79cf | 396 | if (sv == (const SV *)PL_comppad) { |
c2736fce NC |
397 | PL_comppad = NULL; |
398 | PL_curpad = NULL; | |
399 | } | |
aa2f79cf | 400 | SvREFCNT_dec(sv); |
c2736fce | 401 | } |
aa2f79cf NC |
402 | } |
403 | { | |
404 | SV *const sv = AvARRAY(padlist)[0]; | |
405 | if (sv == (const SV *)PL_comppad_name) | |
406 | PL_comppad_name = NULL; | |
c2736fce NC |
407 | SvREFCNT_dec(sv); |
408 | } | |
409 | SvREFCNT_dec(MUTABLE_SV(CvPADLIST(cv))); | |
410 | CvPADLIST(cv) = NULL; | |
411 | } | |
412 | ||
c4528262 NC |
413 | |
414 | /* remove CvOUTSIDE unless this is an undef rather than a free */ | |
415 | if (!SvREFCNT(cv) && CvOUTSIDE(cv)) { | |
416 | if (!CvWEAKOUTSIDE(cv)) | |
417 | SvREFCNT_dec(CvOUTSIDE(cv)); | |
418 | CvOUTSIDE(cv) = NULL; | |
419 | } | |
420 | if (CvCONST(cv)) { | |
421 | SvREFCNT_dec(MUTABLE_SV(CvXSUBANY(cv).any_ptr)); | |
422 | CvCONST_off(cv); | |
423 | } | |
424 | if (CvISXSUB(cv) && CvXSUB(cv)) { | |
425 | CvXSUB(cv) = NULL; | |
426 | } | |
427 | /* delete all flags except WEAKOUTSIDE and CVGV_RC, which indicate the | |
428 | * ref status of CvOUTSIDE and CvGV */ | |
429 | CvFLAGS(cv) &= (CVf_WEAKOUTSIDE|CVf_CVGV_RC); | |
430 | } | |
dd2155a4 | 431 | |
cc76b5cc Z |
432 | /* |
433 | =for apidoc m|PADOFFSET|pad_alloc_name|SV *namesv|U32 flags|HV *typestash|HV *ourstash | |
434 | ||
435 | Allocates a place in the currently-compiling pad (via L</pad_alloc>) and | |
436 | then stores a name for that entry. I<namesv> is adopted and becomes the | |
437 | name entry; it must already contain the name string and be sufficiently | |
438 | upgraded. I<typestash> and I<ourstash> and the C<padadd_STATE> flag get | |
439 | added to I<namesv>. None of the other processing of L</pad_add_name_pvn> | |
440 | is done. Returns the offset of the allocated pad slot. | |
441 | ||
442 | =cut | |
443 | */ | |
444 | ||
3291825f | 445 | static PADOFFSET |
cc76b5cc | 446 | S_pad_alloc_name(pTHX_ SV *namesv, U32 flags, HV *typestash, HV *ourstash) |
3291825f NC |
447 | { |
448 | dVAR; | |
449 | const PADOFFSET offset = pad_alloc(OP_PADSV, SVs_PADMY); | |
450 | ||
cc76b5cc | 451 | PERL_ARGS_ASSERT_PAD_ALLOC_NAME; |
3291825f | 452 | |
cc76b5cc | 453 | ASSERT_CURPAD_ACTIVE("pad_alloc_name"); |
3291825f NC |
454 | |
455 | if (typestash) { | |
456 | assert(SvTYPE(namesv) == SVt_PVMG); | |
457 | SvPAD_TYPED_on(namesv); | |
458 | SvSTASH_set(namesv, MUTABLE_HV(SvREFCNT_inc_simple_NN(MUTABLE_SV(typestash)))); | |
459 | } | |
460 | if (ourstash) { | |
461 | SvPAD_OUR_on(namesv); | |
462 | SvOURSTASH_set(namesv, ourstash); | |
463 | SvREFCNT_inc_simple_void_NN(ourstash); | |
464 | } | |
59cfed7d | 465 | else if (flags & padadd_STATE) { |
3291825f NC |
466 | SvPAD_STATE_on(namesv); |
467 | } | |
468 | ||
469 | av_store(PL_comppad_name, offset, namesv); | |
470 | return offset; | |
471 | } | |
472 | ||
dd2155a4 | 473 | /* |
cc76b5cc | 474 | =for apidoc Am|PADOFFSET|pad_add_name_pvn|const char *namepv|STRLEN namelen|U32 flags|HV *typestash|HV *ourstash |
dd2155a4 | 475 | |
cc76b5cc Z |
476 | Allocates a place in the currently-compiling pad for a named lexical |
477 | variable. Stores the name and other metadata in the name part of the | |
478 | pad, and makes preparations to manage the variable's lexical scoping. | |
479 | Returns the offset of the allocated pad slot. | |
dd2155a4 | 480 | |
cc76b5cc Z |
481 | I<namepv>/I<namelen> specify the variable's name, including leading sigil. |
482 | If I<typestash> is non-null, the name is for a typed lexical, and this | |
483 | identifies the type. If I<ourstash> is non-null, it's a lexical reference | |
484 | to a package variable, and this identifies the package. The following | |
485 | flags can be OR'ed together: | |
486 | ||
487 | padadd_OUR redundantly specifies if it's a package var | |
488 | padadd_STATE variable will retain value persistently | |
489 | padadd_NO_DUP_CHECK skip check for lexical shadowing | |
dd2155a4 DM |
490 | |
491 | =cut | |
492 | */ | |
493 | ||
dd2155a4 | 494 | PADOFFSET |
cc76b5cc Z |
495 | Perl_pad_add_name_pvn(pTHX_ const char *namepv, STRLEN namelen, |
496 | U32 flags, HV *typestash, HV *ourstash) | |
dd2155a4 | 497 | { |
97aff369 | 498 | dVAR; |
3291825f | 499 | PADOFFSET offset; |
cca43f78 | 500 | SV *namesv; |
dd2155a4 | 501 | |
cc76b5cc | 502 | PERL_ARGS_ASSERT_PAD_ADD_NAME_PVN; |
7918f24d | 503 | |
2435e5d3 | 504 | if (flags & ~(padadd_OUR|padadd_STATE|padadd_NO_DUP_CHECK|padadd_UTF8_NAME)) |
cc76b5cc | 505 | Perl_croak(aTHX_ "panic: pad_add_name_pvn illegal flag bits 0x%" UVxf, |
cca43f78 NC |
506 | (UV)flags); |
507 | ||
508 | namesv = newSV_type((ourstash || typestash) ? SVt_PVMG : SVt_PVNV); | |
cc76b5cc | 509 | sv_setpvn(namesv, namepv, namelen); |
dd2155a4 | 510 | |
59cfed7d | 511 | if ((flags & padadd_NO_DUP_CHECK) == 0) { |
2d12d04f | 512 | /* check for duplicate declaration */ |
59cfed7d | 513 | pad_check_dup(namesv, flags & padadd_OUR, ourstash); |
2d12d04f NC |
514 | } |
515 | ||
2435e5d3 | 516 | offset = pad_alloc_name(namesv, flags & ~padadd_UTF8_NAME, typestash, ourstash); |
3291825f NC |
517 | |
518 | /* not yet introduced */ | |
2df5bdd7 DM |
519 | COP_SEQ_RANGE_LOW_set(namesv, PERL_PADSEQ_INTRO); |
520 | COP_SEQ_RANGE_HIGH_set(namesv, 0); | |
3291825f NC |
521 | |
522 | if (!PL_min_intro_pending) | |
523 | PL_min_intro_pending = offset; | |
524 | PL_max_intro_pending = offset; | |
525 | /* if it's not a simple scalar, replace with an AV or HV */ | |
c1bf42f3 NC |
526 | assert(SvTYPE(PL_curpad[offset]) == SVt_NULL); |
527 | assert(SvREFCNT(PL_curpad[offset]) == 1); | |
cc76b5cc | 528 | if (namelen != 0 && *namepv == '@') |
c1bf42f3 | 529 | sv_upgrade(PL_curpad[offset], SVt_PVAV); |
cc76b5cc | 530 | else if (namelen != 0 && *namepv == '%') |
c1bf42f3 NC |
531 | sv_upgrade(PL_curpad[offset], SVt_PVHV); |
532 | assert(SvPADMY(PL_curpad[offset])); | |
3291825f NC |
533 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
534 | "Pad addname: %ld \"%s\" new lex=0x%"UVxf"\n", | |
cc76b5cc Z |
535 | (long)offset, SvPVX(namesv), |
536 | PTR2UV(PL_curpad[offset]))); | |
dd2155a4 DM |
537 | |
538 | return offset; | |
539 | } | |
540 | ||
cc76b5cc Z |
541 | /* |
542 | =for apidoc Am|PADOFFSET|pad_add_name_pv|const char *name|U32 flags|HV *typestash|HV *ourstash | |
dd2155a4 | 543 | |
cc76b5cc Z |
544 | Exactly like L</pad_add_name_pvn>, but takes a nul-terminated string |
545 | instead of a string/length pair. | |
dd2155a4 | 546 | |
cc76b5cc Z |
547 | =cut |
548 | */ | |
549 | ||
550 | PADOFFSET | |
551 | Perl_pad_add_name_pv(pTHX_ const char *name, | |
552 | U32 flags, HV *typestash, HV *ourstash) | |
553 | { | |
554 | PERL_ARGS_ASSERT_PAD_ADD_NAME_PV; | |
555 | return pad_add_name_pvn(name, strlen(name), flags, typestash, ourstash); | |
556 | } | |
dd2155a4 DM |
557 | |
558 | /* | |
cc76b5cc | 559 | =for apidoc Am|PADOFFSET|pad_add_name_sv|SV *name|U32 flags|HV *typestash|HV *ourstash |
dd2155a4 | 560 | |
cc76b5cc Z |
561 | Exactly like L</pad_add_name_pvn>, but takes the name string in the form |
562 | of an SV instead of a string/length pair. | |
563 | ||
564 | =cut | |
565 | */ | |
566 | ||
567 | PADOFFSET | |
568 | Perl_pad_add_name_sv(pTHX_ SV *name, U32 flags, HV *typestash, HV *ourstash) | |
569 | { | |
570 | char *namepv; | |
571 | STRLEN namelen; | |
572 | PERL_ARGS_ASSERT_PAD_ADD_NAME_SV; | |
573 | namepv = SvPV(name, namelen); | |
574 | return pad_add_name_pvn(namepv, namelen, flags, typestash, ourstash); | |
575 | } | |
576 | ||
577 | /* | |
578 | =for apidoc Amx|PADOFFSET|pad_alloc|I32 optype|U32 tmptype | |
579 | ||
580 | Allocates a place in the currently-compiling pad, | |
581 | returning the offset of the allocated pad slot. | |
582 | No name is initially attached to the pad slot. | |
583 | I<tmptype> is a set of flags indicating the kind of pad entry required, | |
584 | which will be set in the value SV for the allocated pad entry: | |
585 | ||
586 | SVs_PADMY named lexical variable ("my", "our", "state") | |
587 | SVs_PADTMP unnamed temporary store | |
588 | ||
589 | I<optype> should be an opcode indicating the type of operation that the | |
590 | pad entry is to support. This doesn't affect operational semantics, | |
591 | but is used for debugging. | |
dd2155a4 DM |
592 | |
593 | =cut | |
594 | */ | |
595 | ||
596 | /* XXX DAPM integrate alloc(), add_name() and add_anon(), | |
597 | * or at least rationalise ??? */ | |
6d640399 NC |
598 | /* And flag whether the incoming name is UTF8 or 8 bit? |
599 | Could do this either with the +ve/-ve hack of the HV code, or expanding | |
600 | the flag bits. Either way, this makes proper Unicode safe pad support. | |
6d640399 NC |
601 | NWC |
602 | */ | |
dd2155a4 DM |
603 | |
604 | PADOFFSET | |
605 | Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype) | |
606 | { | |
97aff369 | 607 | dVAR; |
dd2155a4 DM |
608 | SV *sv; |
609 | I32 retval; | |
610 | ||
6136c704 | 611 | PERL_UNUSED_ARG(optype); |
f3548bdc DM |
612 | ASSERT_CURPAD_ACTIVE("pad_alloc"); |
613 | ||
dd2155a4 DM |
614 | if (AvARRAY(PL_comppad) != PL_curpad) |
615 | Perl_croak(aTHX_ "panic: pad_alloc"); | |
616 | if (PL_pad_reset_pending) | |
617 | pad_reset(); | |
618 | if (tmptype & SVs_PADMY) { | |
cc76b5cc | 619 | /* For a my, simply push a null SV onto the end of PL_comppad. */ |
235cc2e3 | 620 | sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE); |
dd2155a4 DM |
621 | retval = AvFILLp(PL_comppad); |
622 | } | |
623 | else { | |
cc76b5cc Z |
624 | /* For a tmp, scan the pad from PL_padix upwards |
625 | * for a slot which has no name and no active value. | |
626 | */ | |
551405c4 | 627 | SV * const * const names = AvARRAY(PL_comppad_name); |
e1ec3a88 | 628 | const SSize_t names_fill = AvFILLp(PL_comppad_name); |
dd2155a4 DM |
629 | for (;;) { |
630 | /* | |
631 | * "foreach" index vars temporarily become aliases to non-"my" | |
632 | * values. Thus we must skip, not just pad values that are | |
633 | * marked as current pad values, but also those with names. | |
634 | */ | |
635 | /* HVDS why copy to sv here? we don't seem to use it */ | |
636 | if (++PL_padix <= names_fill && | |
637 | (sv = names[PL_padix]) && sv != &PL_sv_undef) | |
638 | continue; | |
639 | sv = *av_fetch(PL_comppad, PL_padix, TRUE); | |
640 | if (!(SvFLAGS(sv) & (SVs_PADTMP | SVs_PADMY)) && | |
641 | !IS_PADGV(sv) && !IS_PADCONST(sv)) | |
642 | break; | |
643 | } | |
644 | retval = PL_padix; | |
645 | } | |
646 | SvFLAGS(sv) |= tmptype; | |
647 | PL_curpad = AvARRAY(PL_comppad); | |
648 | ||
649 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
650 | "Pad 0x%"UVxf"[0x%"UVxf"] alloc: %ld for %s\n", | |
651 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long) retval, | |
652 | PL_op_name[optype])); | |
fd0854ff DM |
653 | #ifdef DEBUG_LEAKING_SCALARS |
654 | sv->sv_debug_optype = optype; | |
655 | sv->sv_debug_inpad = 1; | |
fd0854ff | 656 | #endif |
a212c8b5 | 657 | return (PADOFFSET)retval; |
dd2155a4 DM |
658 | } |
659 | ||
660 | /* | |
cc76b5cc | 661 | =for apidoc Am|PADOFFSET|pad_add_anon|CV *func|I32 optype |
dd2155a4 | 662 | |
cc76b5cc Z |
663 | Allocates a place in the currently-compiling pad (via L</pad_alloc>) |
664 | for an anonymous function that is lexically scoped inside the | |
665 | currently-compiling function. | |
666 | The function I<func> is linked into the pad, and its C<CvOUTSIDE> link | |
667 | to the outer scope is weakened to avoid a reference loop. | |
668 | ||
669 | I<optype> should be an opcode indicating the type of operation that the | |
670 | pad entry is to support. This doesn't affect operational semantics, | |
671 | but is used for debugging. | |
dd2155a4 DM |
672 | |
673 | =cut | |
674 | */ | |
675 | ||
676 | PADOFFSET | |
cc76b5cc | 677 | Perl_pad_add_anon(pTHX_ CV* func, I32 optype) |
dd2155a4 | 678 | { |
97aff369 | 679 | dVAR; |
dd2155a4 | 680 | PADOFFSET ix; |
b9f83d2f | 681 | SV* const name = newSV_type(SVt_PVNV); |
7918f24d NC |
682 | |
683 | PERL_ARGS_ASSERT_PAD_ADD_ANON; | |
684 | ||
1dba731d | 685 | pad_peg("add_anon"); |
76f68e9b | 686 | sv_setpvs(name, "&"); |
0d311cdb DM |
687 | /* These two aren't used; just make sure they're not equal to |
688 | * PERL_PADSEQ_INTRO */ | |
689 | COP_SEQ_RANGE_LOW_set(name, 0); | |
690 | COP_SEQ_RANGE_HIGH_set(name, 0); | |
cc76b5cc | 691 | ix = pad_alloc(optype, SVs_PADMY); |
dd2155a4 | 692 | av_store(PL_comppad_name, ix, name); |
f3548bdc | 693 | /* XXX DAPM use PL_curpad[] ? */ |
cc76b5cc Z |
694 | av_store(PL_comppad, ix, (SV*)func); |
695 | SvPADMY_on((SV*)func); | |
7dafbf52 DM |
696 | |
697 | /* to avoid ref loops, we never have parent + child referencing each | |
698 | * other simultaneously */ | |
cc76b5cc Z |
699 | if (CvOUTSIDE(func)) { |
700 | assert(!CvWEAKOUTSIDE(func)); | |
701 | CvWEAKOUTSIDE_on(func); | |
702 | SvREFCNT_dec(CvOUTSIDE(func)); | |
7dafbf52 | 703 | } |
dd2155a4 DM |
704 | return ix; |
705 | } | |
706 | ||
dd2155a4 | 707 | /* |
cc76b5cc | 708 | =for apidoc m|pad_check_dup|SV *name|U32 flags|const HV *ourstash |
dd2155a4 DM |
709 | |
710 | Check for duplicate declarations: report any of: | |
711 | * a my in the current scope with the same name; | |
712 | * an our (anywhere in the pad) with the same name and the same stash | |
713 | as C<ourstash> | |
714 | C<is_our> indicates that the name to check is an 'our' declaration | |
715 | ||
716 | =cut | |
717 | */ | |
718 | ||
20381b50 | 719 | STATIC void |
cc76b5cc | 720 | S_pad_check_dup(pTHX_ SV *name, U32 flags, const HV *ourstash) |
dd2155a4 | 721 | { |
97aff369 | 722 | dVAR; |
53c1dcc0 | 723 | SV **svp; |
dd2155a4 | 724 | PADOFFSET top, off; |
59cfed7d | 725 | const U32 is_our = flags & padadd_OUR; |
dd2155a4 | 726 | |
7918f24d NC |
727 | PERL_ARGS_ASSERT_PAD_CHECK_DUP; |
728 | ||
f3548bdc | 729 | ASSERT_CURPAD_ACTIVE("pad_check_dup"); |
35f82371 | 730 | |
59cfed7d | 731 | assert((flags & ~padadd_OUR) == 0); |
35f82371 | 732 | |
041457d9 | 733 | if (AvFILLp(PL_comppad_name) < 0 || !ckWARN(WARN_MISC)) |
dd2155a4 DM |
734 | return; /* nothing to check */ |
735 | ||
736 | svp = AvARRAY(PL_comppad_name); | |
737 | top = AvFILLp(PL_comppad_name); | |
738 | /* check the current scope */ | |
739 | /* XXX DAPM - why the (I32) cast - shouldn't we ensure they're the same | |
740 | * type ? */ | |
741 | for (off = top; (I32)off > PL_comppad_name_floor; off--) { | |
53c1dcc0 AL |
742 | SV * const sv = svp[off]; |
743 | if (sv | |
dd2155a4 | 744 | && sv != &PL_sv_undef |
ee6cee0c | 745 | && !SvFAKE(sv) |
0d311cdb DM |
746 | && ( COP_SEQ_RANGE_LOW(sv) == PERL_PADSEQ_INTRO |
747 | || COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO) | |
2d12d04f | 748 | && sv_eq(name, sv)) |
dd2155a4 | 749 | { |
00b1698f | 750 | if (is_our && (SvPAD_OUR(sv))) |
7f73a9f1 | 751 | break; /* "our" masking "our" */ |
dd2155a4 | 752 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
c541b9b4 | 753 | "\"%s\" variable %"SVf" masks earlier declaration in same %s", |
12bd6ede | 754 | (is_our ? "our" : PL_parser->in_my == KEY_my ? "my" : "state"), |
c541b9b4 | 755 | sv, |
2df5bdd7 DM |
756 | (COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO |
757 | ? "scope" : "statement")); | |
dd2155a4 DM |
758 | --off; |
759 | break; | |
760 | } | |
761 | } | |
762 | /* check the rest of the pad */ | |
763 | if (is_our) { | |
61c5492a | 764 | while (off > 0) { |
53c1dcc0 AL |
765 | SV * const sv = svp[off]; |
766 | if (sv | |
dd2155a4 | 767 | && sv != &PL_sv_undef |
ee6cee0c | 768 | && !SvFAKE(sv) |
0d311cdb DM |
769 | && ( COP_SEQ_RANGE_LOW(sv) == PERL_PADSEQ_INTRO |
770 | || COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO) | |
73d95100 | 771 | && SvOURSTASH(sv) == ourstash |
2d12d04f | 772 | && sv_eq(name, sv)) |
dd2155a4 DM |
773 | { |
774 | Perl_warner(aTHX_ packWARN(WARN_MISC), | |
c541b9b4 | 775 | "\"our\" variable %"SVf" redeclared", sv); |
624f69f5 | 776 | if ((I32)off <= PL_comppad_name_floor) |
7f73a9f1 RGS |
777 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
778 | "\t(Did you mean \"local\" instead of \"our\"?)\n"); | |
dd2155a4 DM |
779 | break; |
780 | } | |
61c5492a NC |
781 | --off; |
782 | } | |
dd2155a4 DM |
783 | } |
784 | } | |
785 | ||
786 | ||
dd2155a4 | 787 | /* |
cc76b5cc | 788 | =for apidoc Am|PADOFFSET|pad_findmy_pvn|const char *namepv|STRLEN namelen|U32 flags |
dd2155a4 | 789 | |
cc76b5cc Z |
790 | Given the name of a lexical variable, find its position in the |
791 | currently-compiling pad. | |
792 | I<namepv>/I<namelen> specify the variable's name, including leading sigil. | |
793 | I<flags> is reserved and must be zero. | |
794 | If it is not in the current pad but appears in the pad of any lexically | |
795 | enclosing scope, then a pseudo-entry for it is added in the current pad. | |
796 | Returns the offset in the current pad, | |
797 | or C<NOT_IN_PAD> if no such lexical is in scope. | |
dd2155a4 DM |
798 | |
799 | =cut | |
800 | */ | |
801 | ||
802 | PADOFFSET | |
cc76b5cc | 803 | Perl_pad_findmy_pvn(pTHX_ const char *namepv, STRLEN namelen, U32 flags) |
dd2155a4 | 804 | { |
97aff369 | 805 | dVAR; |
b5c19bd7 DM |
806 | SV *out_sv; |
807 | int out_flags; | |
929a0744 | 808 | I32 offset; |
e1ec3a88 | 809 | const AV *nameav; |
929a0744 | 810 | SV **name_svp; |
dd2155a4 | 811 | |
cc76b5cc | 812 | PERL_ARGS_ASSERT_PAD_FINDMY_PVN; |
7918f24d | 813 | |
cc76b5cc | 814 | pad_peg("pad_findmy_pvn"); |
f8f98e0a | 815 | |
2435e5d3 | 816 | if (flags & ~padadd_UTF8_NAME) |
cc76b5cc | 817 | Perl_croak(aTHX_ "panic: pad_findmy_pvn illegal flag bits 0x%" UVxf, |
f8f98e0a NC |
818 | (UV)flags); |
819 | ||
fbb889c8 BF |
820 | offset = pad_findlex(namepv, namelen, flags, |
821 | PL_compcv, PL_cop_seqmax, 1, NULL, &out_sv, &out_flags); | |
9f7d9405 | 822 | if ((PADOFFSET)offset != NOT_IN_PAD) |
929a0744 DM |
823 | return offset; |
824 | ||
825 | /* look for an our that's being introduced; this allows | |
826 | * our $foo = 0 unless defined $foo; | |
827 | * to not give a warning. (Yes, this is a hack) */ | |
828 | ||
502c6561 | 829 | nameav = MUTABLE_AV(AvARRAY(CvPADLIST(PL_compcv))[0]); |
929a0744 DM |
830 | name_svp = AvARRAY(nameav); |
831 | for (offset = AvFILLp(nameav); offset > 0; offset--) { | |
551405c4 | 832 | const SV * const namesv = name_svp[offset]; |
929a0744 DM |
833 | if (namesv && namesv != &PL_sv_undef |
834 | && !SvFAKE(namesv) | |
00b1698f | 835 | && (SvPAD_OUR(namesv)) |
cc76b5cc Z |
836 | && SvCUR(namesv) == namelen |
837 | && memEQ(SvPVX_const(namesv), namepv, namelen) | |
2df5bdd7 | 838 | && COP_SEQ_RANGE_LOW(namesv) == PERL_PADSEQ_INTRO |
929a0744 DM |
839 | ) |
840 | return offset; | |
841 | } | |
842 | return NOT_IN_PAD; | |
dd2155a4 DM |
843 | } |
844 | ||
e1f795dc | 845 | /* |
cc76b5cc Z |
846 | =for apidoc Am|PADOFFSET|pad_findmy_pv|const char *name|U32 flags |
847 | ||
848 | Exactly like L</pad_findmy_pvn>, but takes a nul-terminated string | |
849 | instead of a string/length pair. | |
850 | ||
851 | =cut | |
852 | */ | |
853 | ||
854 | PADOFFSET | |
855 | Perl_pad_findmy_pv(pTHX_ const char *name, U32 flags) | |
856 | { | |
857 | PERL_ARGS_ASSERT_PAD_FINDMY_PV; | |
858 | return pad_findmy_pvn(name, strlen(name), flags); | |
859 | } | |
860 | ||
861 | /* | |
862 | =for apidoc Am|PADOFFSET|pad_findmy_sv|SV *name|U32 flags | |
863 | ||
864 | Exactly like L</pad_findmy_pvn>, but takes the name string in the form | |
865 | of an SV instead of a string/length pair. | |
866 | ||
867 | =cut | |
868 | */ | |
869 | ||
870 | PADOFFSET | |
871 | Perl_pad_findmy_sv(pTHX_ SV *name, U32 flags) | |
872 | { | |
873 | char *namepv; | |
874 | STRLEN namelen; | |
875 | PERL_ARGS_ASSERT_PAD_FINDMY_SV; | |
876 | namepv = SvPV(name, namelen); | |
2435e5d3 BF |
877 | if (SvUTF8(name)) |
878 | flags |= padadd_UTF8_NAME; | |
cc76b5cc Z |
879 | return pad_findmy_pvn(namepv, namelen, flags); |
880 | } | |
881 | ||
882 | /* | |
883 | =for apidoc Amp|PADOFFSET|find_rundefsvoffset | |
884 | ||
885 | Find the position of the lexical C<$_> in the pad of the | |
886 | currently-executing function. Returns the offset in the current pad, | |
887 | or C<NOT_IN_PAD> if there is no lexical C<$_> in scope (in which case | |
888 | the global one should be used instead). | |
889 | L</find_rundefsv> is likely to be more convenient. | |
890 | ||
891 | =cut | |
892 | */ | |
e1f795dc RGS |
893 | |
894 | PADOFFSET | |
29289021 | 895 | Perl_find_rundefsvoffset(pTHX) |
e1f795dc | 896 | { |
97aff369 | 897 | dVAR; |
e1f795dc RGS |
898 | SV *out_sv; |
899 | int out_flags; | |
fbb889c8 | 900 | return pad_findlex("$_", 2, 0, find_runcv(NULL), PL_curcop->cop_seq, 1, |
4608196e | 901 | NULL, &out_sv, &out_flags); |
e1f795dc | 902 | } |
dd2155a4 | 903 | |
dd2155a4 | 904 | /* |
cc76b5cc Z |
905 | =for apidoc Am|SV *|find_rundefsv |
906 | ||
907 | Find and return the variable that is named C<$_> in the lexical scope | |
908 | of the currently-executing function. This may be a lexical C<$_>, | |
909 | or will otherwise be the global one. | |
910 | ||
911 | =cut | |
912 | */ | |
789bd863 VP |
913 | |
914 | SV * | |
915 | Perl_find_rundefsv(pTHX) | |
916 | { | |
917 | SV *namesv; | |
918 | int flags; | |
919 | PADOFFSET po; | |
920 | ||
fbb889c8 | 921 | po = pad_findlex("$_", 2, 0, find_runcv(NULL), PL_curcop->cop_seq, 1, |
789bd863 VP |
922 | NULL, &namesv, &flags); |
923 | ||
1979170b | 924 | if (po == NOT_IN_PAD || SvPAD_OUR(namesv)) |
789bd863 VP |
925 | return DEFSV; |
926 | ||
927 | return PAD_SVl(po); | |
928 | } | |
929 | ||
930 | /* | |
fbb889c8 | 931 | =for apidoc m|PADOFFSET|pad_findlex|const char *namepv|STRLEN namelen|U32 flags|const CV* cv|U32 seq|int warn|SV** out_capture|SV** out_name_sv|int *out_flags |
dd2155a4 DM |
932 | |
933 | Find a named lexical anywhere in a chain of nested pads. Add fake entries | |
b5c19bd7 DM |
934 | in the inner pads if it's found in an outer one. |
935 | ||
936 | Returns the offset in the bottom pad of the lex or the fake lex. | |
937 | cv is the CV in which to start the search, and seq is the current cop_seq | |
938 | to match against. If warn is true, print appropriate warnings. The out_* | |
939 | vars return values, and so are pointers to where the returned values | |
940 | should be stored. out_capture, if non-null, requests that the innermost | |
941 | instance of the lexical is captured; out_name_sv is set to the innermost | |
942 | matched namesv or fake namesv; out_flags returns the flags normally | |
943 | associated with the IVX field of a fake namesv. | |
944 | ||
945 | Note that pad_findlex() is recursive; it recurses up the chain of CVs, | |
946 | then comes back down, adding fake entries as it goes. It has to be this way | |
3441fb63 | 947 | because fake namesvs in anon protoypes have to store in xlow the index into |
b5c19bd7 | 948 | the parent pad. |
dd2155a4 DM |
949 | |
950 | =cut | |
951 | */ | |
952 | ||
b5c19bd7 DM |
953 | /* the CV has finished being compiled. This is not a sufficient test for |
954 | * all CVs (eg XSUBs), but suffices for the CVs found in a lexical chain */ | |
955 | #define CvCOMPILED(cv) CvROOT(cv) | |
956 | ||
71f882da DM |
957 | /* the CV does late binding of its lexicals */ |
958 | #define CvLATE(cv) (CvANON(cv) || SvTYPE(cv) == SVt_PVFM) | |
959 | ||
b5c19bd7 | 960 | |
dd2155a4 | 961 | STATIC PADOFFSET |
fbb889c8 | 962 | S_pad_findlex(pTHX_ const char *namepv, STRLEN namelen, U32 flags, const CV* cv, U32 seq, |
cc76b5cc | 963 | int warn, SV** out_capture, SV** out_name_sv, int *out_flags) |
dd2155a4 | 964 | { |
97aff369 | 965 | dVAR; |
b5c19bd7 DM |
966 | I32 offset, new_offset; |
967 | SV *new_capture; | |
968 | SV **new_capturep; | |
b64e5050 | 969 | const AV * const padlist = CvPADLIST(cv); |
dd2155a4 | 970 | |
7918f24d NC |
971 | PERL_ARGS_ASSERT_PAD_FINDLEX; |
972 | ||
2435e5d3 BF |
973 | if (flags & ~padadd_UTF8_NAME) |
974 | Perl_croak(aTHX_ "panic: pad_findlex illegal flag bits 0x%" UVxf, | |
975 | (UV)flags); | |
976 | ||
b5c19bd7 | 977 | *out_flags = 0; |
a3985cdc | 978 | |
b5c19bd7 | 979 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
cc76b5cc Z |
980 | "Pad findlex cv=0x%"UVxf" searching \"%.*s\" seq=%d%s\n", |
981 | PTR2UV(cv), namelen, namepv, (int)seq, | |
982 | out_capture ? " capturing" : "" )); | |
dd2155a4 | 983 | |
b5c19bd7 | 984 | /* first, search this pad */ |
dd2155a4 | 985 | |
b5c19bd7 DM |
986 | if (padlist) { /* not an undef CV */ |
987 | I32 fake_offset = 0; | |
502c6561 | 988 | const AV * const nameav = MUTABLE_AV(AvARRAY(padlist)[0]); |
551405c4 | 989 | SV * const * const name_svp = AvARRAY(nameav); |
ee6cee0c | 990 | |
b5c19bd7 | 991 | for (offset = AvFILLp(nameav); offset > 0; offset--) { |
551405c4 | 992 | const SV * const namesv = name_svp[offset]; |
b5c19bd7 | 993 | if (namesv && namesv != &PL_sv_undef |
cc76b5cc Z |
994 | && SvCUR(namesv) == namelen |
995 | && memEQ(SvPVX_const(namesv), namepv, namelen)) | |
b5c19bd7 | 996 | { |
6012dc80 | 997 | if (SvFAKE(namesv)) { |
b5c19bd7 | 998 | fake_offset = offset; /* in case we don't find a real one */ |
6012dc80 DM |
999 | continue; |
1000 | } | |
1001 | /* is seq within the range _LOW to _HIGH ? | |
1002 | * This is complicated by the fact that PL_cop_seqmax | |
1003 | * may have wrapped around at some point */ | |
1004 | if (COP_SEQ_RANGE_LOW(namesv) == PERL_PADSEQ_INTRO) | |
1005 | continue; /* not yet introduced */ | |
1006 | ||
1007 | if (COP_SEQ_RANGE_HIGH(namesv) == PERL_PADSEQ_INTRO) { | |
1008 | /* in compiling scope */ | |
1009 | if ( | |
1010 | (seq > COP_SEQ_RANGE_LOW(namesv)) | |
1011 | ? (seq - COP_SEQ_RANGE_LOW(namesv) < (U32_MAX >> 1)) | |
1012 | : (COP_SEQ_RANGE_LOW(namesv) - seq > (U32_MAX >> 1)) | |
1013 | ) | |
1014 | break; | |
1015 | } | |
1016 | else if ( | |
1017 | (COP_SEQ_RANGE_LOW(namesv) > COP_SEQ_RANGE_HIGH(namesv)) | |
1018 | ? | |
1019 | ( seq > COP_SEQ_RANGE_LOW(namesv) | |
1020 | || seq <= COP_SEQ_RANGE_HIGH(namesv)) | |
1021 | ||
1022 | : ( seq > COP_SEQ_RANGE_LOW(namesv) | |
1023 | && seq <= COP_SEQ_RANGE_HIGH(namesv)) | |
1024 | ) | |
1025 | break; | |
ee6cee0c DM |
1026 | } |
1027 | } | |
1028 | ||
b5c19bd7 DM |
1029 | if (offset > 0 || fake_offset > 0 ) { /* a match! */ |
1030 | if (offset > 0) { /* not fake */ | |
1031 | fake_offset = 0; | |
1032 | *out_name_sv = name_svp[offset]; /* return the namesv */ | |
1033 | ||
1034 | /* set PAD_FAKELEX_MULTI if this lex can have multiple | |
1035 | * instances. For now, we just test !CvUNIQUE(cv), but | |
1036 | * ideally, we should detect my's declared within loops | |
1037 | * etc - this would allow a wider range of 'not stayed | |
486ec47a | 1038 | * shared' warnings. We also treated already-compiled |
b5c19bd7 DM |
1039 | * lexes as not multi as viewed from evals. */ |
1040 | ||
1041 | *out_flags = CvANON(cv) ? | |
1042 | PAD_FAKELEX_ANON : | |
1043 | (!CvUNIQUE(cv) && ! CvCOMPILED(cv)) | |
1044 | ? PAD_FAKELEX_MULTI : 0; | |
1045 | ||
1046 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, | |
809abb02 NC |
1047 | "Pad findlex cv=0x%"UVxf" matched: offset=%ld (%lu,%lu)\n", |
1048 | PTR2UV(cv), (long)offset, | |
1049 | (unsigned long)COP_SEQ_RANGE_LOW(*out_name_sv), | |
1050 | (unsigned long)COP_SEQ_RANGE_HIGH(*out_name_sv))); | |
b5c19bd7 DM |
1051 | } |
1052 | else { /* fake match */ | |
1053 | offset = fake_offset; | |
1054 | *out_name_sv = name_svp[offset]; /* return the namesv */ | |
809abb02 | 1055 | *out_flags = PARENT_FAKELEX_FLAGS(*out_name_sv); |
b5c19bd7 | 1056 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
19a5c512 | 1057 | "Pad findlex cv=0x%"UVxf" matched: offset=%ld flags=0x%lx index=%lu\n", |
b5c19bd7 | 1058 | PTR2UV(cv), (long)offset, (unsigned long)*out_flags, |
809abb02 | 1059 | (unsigned long) PARENT_PAD_INDEX(*out_name_sv) |
b5c19bd7 DM |
1060 | )); |
1061 | } | |
dd2155a4 | 1062 | |
b5c19bd7 | 1063 | /* return the lex? */ |
dd2155a4 | 1064 | |
b5c19bd7 | 1065 | if (out_capture) { |
dd2155a4 | 1066 | |
b5c19bd7 | 1067 | /* our ? */ |
00b1698f | 1068 | if (SvPAD_OUR(*out_name_sv)) { |
a0714e2c | 1069 | *out_capture = NULL; |
b5c19bd7 DM |
1070 | return offset; |
1071 | } | |
ee6cee0c | 1072 | |
b5c19bd7 DM |
1073 | /* trying to capture from an anon prototype? */ |
1074 | if (CvCOMPILED(cv) | |
1075 | ? CvANON(cv) && CvCLONE(cv) && !CvCLONED(cv) | |
1076 | : *out_flags & PAD_FAKELEX_ANON) | |
1077 | { | |
a2a5de95 NC |
1078 | if (warn) |
1079 | Perl_ck_warner(aTHX_ packWARN(WARN_CLOSURE), | |
cc76b5cc Z |
1080 | "Variable \"%.*s\" is not available", |
1081 | namelen, namepv); | |
a0714e2c | 1082 | *out_capture = NULL; |
b5c19bd7 | 1083 | } |
ee6cee0c | 1084 | |
b5c19bd7 DM |
1085 | /* real value */ |
1086 | else { | |
1087 | int newwarn = warn; | |
1088 | if (!CvCOMPILED(cv) && (*out_flags & PAD_FAKELEX_MULTI) | |
d1186544 | 1089 | && !SvPAD_STATE(name_svp[offset]) |
b5c19bd7 DM |
1090 | && warn && ckWARN(WARN_CLOSURE)) { |
1091 | newwarn = 0; | |
1092 | Perl_warner(aTHX_ packWARN(WARN_CLOSURE), | |
cc76b5cc Z |
1093 | "Variable \"%.*s\" will not stay shared", |
1094 | namelen, namepv); | |
b5c19bd7 | 1095 | } |
dd2155a4 | 1096 | |
b5c19bd7 DM |
1097 | if (fake_offset && CvANON(cv) |
1098 | && CvCLONE(cv) &&!CvCLONED(cv)) | |
1099 | { | |
1100 | SV *n; | |
1101 | /* not yet caught - look further up */ | |
1102 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, | |
1103 | "Pad findlex cv=0x%"UVxf" chasing lex in outer pad\n", | |
1104 | PTR2UV(cv))); | |
1105 | n = *out_name_sv; | |
fbb889c8 | 1106 | (void) pad_findlex(namepv, namelen, flags, CvOUTSIDE(cv), |
282e1742 | 1107 | CvOUTSIDE_SEQ(cv), |
b5c19bd7 DM |
1108 | newwarn, out_capture, out_name_sv, out_flags); |
1109 | *out_name_sv = n; | |
1110 | return offset; | |
dd2155a4 | 1111 | } |
b5c19bd7 | 1112 | |
502c6561 NC |
1113 | *out_capture = AvARRAY(MUTABLE_AV(AvARRAY(padlist)[ |
1114 | CvDEPTH(cv) ? CvDEPTH(cv) : 1]))[offset]; | |
b5c19bd7 DM |
1115 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
1116 | "Pad findlex cv=0x%"UVxf" found lex=0x%"UVxf"\n", | |
19a5c512 | 1117 | PTR2UV(cv), PTR2UV(*out_capture))); |
b5c19bd7 | 1118 | |
d1186544 DM |
1119 | if (SvPADSTALE(*out_capture) |
1120 | && !SvPAD_STATE(name_svp[offset])) | |
1121 | { | |
a2a5de95 | 1122 | Perl_ck_warner(aTHX_ packWARN(WARN_CLOSURE), |
cc76b5cc Z |
1123 | "Variable \"%.*s\" is not available", |
1124 | namelen, namepv); | |
a0714e2c | 1125 | *out_capture = NULL; |
dd2155a4 DM |
1126 | } |
1127 | } | |
b5c19bd7 | 1128 | if (!*out_capture) { |
cc76b5cc | 1129 | if (namelen != 0 && *namepv == '@') |
ad64d0ec | 1130 | *out_capture = sv_2mortal(MUTABLE_SV(newAV())); |
cc76b5cc | 1131 | else if (namelen != 0 && *namepv == '%') |
ad64d0ec | 1132 | *out_capture = sv_2mortal(MUTABLE_SV(newHV())); |
b5c19bd7 DM |
1133 | else |
1134 | *out_capture = sv_newmortal(); | |
1135 | } | |
dd2155a4 | 1136 | } |
b5c19bd7 DM |
1137 | |
1138 | return offset; | |
ee6cee0c | 1139 | } |
b5c19bd7 DM |
1140 | } |
1141 | ||
1142 | /* it's not in this pad - try above */ | |
1143 | ||
1144 | if (!CvOUTSIDE(cv)) | |
1145 | return NOT_IN_PAD; | |
9f7d9405 | 1146 | |
b5c19bd7 | 1147 | /* out_capture non-null means caller wants us to capture lex; in |
71f882da | 1148 | * addition we capture ourselves unless it's an ANON/format */ |
b5c19bd7 | 1149 | new_capturep = out_capture ? out_capture : |
4608196e | 1150 | CvLATE(cv) ? NULL : &new_capture; |
b5c19bd7 | 1151 | |
fbb889c8 | 1152 | offset = pad_findlex(namepv, namelen, flags, CvOUTSIDE(cv), CvOUTSIDE_SEQ(cv), 1, |
b5c19bd7 | 1153 | new_capturep, out_name_sv, out_flags); |
9f7d9405 | 1154 | if ((PADOFFSET)offset == NOT_IN_PAD) |
b5c19bd7 | 1155 | return NOT_IN_PAD; |
9f7d9405 | 1156 | |
b5c19bd7 DM |
1157 | /* found in an outer CV. Add appropriate fake entry to this pad */ |
1158 | ||
1159 | /* don't add new fake entries (via eval) to CVs that we have already | |
1160 | * finished compiling, or to undef CVs */ | |
1161 | if (CvCOMPILED(cv) || !padlist) | |
1162 | return 0; /* this dummy (and invalid) value isnt used by the caller */ | |
1163 | ||
1164 | { | |
3291825f | 1165 | /* This relies on sv_setsv_flags() upgrading the destination to the same |
486ec47a | 1166 | type as the source, independent of the flags set, and on it being |
3291825f NC |
1167 | "good" and only copying flag bits and pointers that it understands. |
1168 | */ | |
1169 | SV *new_namesv = newSVsv(*out_name_sv); | |
53c1dcc0 AL |
1170 | AV * const ocomppad_name = PL_comppad_name; |
1171 | PAD * const ocomppad = PL_comppad; | |
502c6561 NC |
1172 | PL_comppad_name = MUTABLE_AV(AvARRAY(padlist)[0]); |
1173 | PL_comppad = MUTABLE_AV(AvARRAY(padlist)[1]); | |
b5c19bd7 DM |
1174 | PL_curpad = AvARRAY(PL_comppad); |
1175 | ||
3291825f | 1176 | new_offset |
cc76b5cc | 1177 | = pad_alloc_name(new_namesv, |
59cfed7d | 1178 | (SvPAD_STATE(*out_name_sv) ? padadd_STATE : 0), |
3291825f NC |
1179 | SvPAD_TYPED(*out_name_sv) |
1180 | ? SvSTASH(*out_name_sv) : NULL, | |
1181 | SvOURSTASH(*out_name_sv) | |
1182 | ); | |
1183 | ||
1184 | SvFAKE_on(new_namesv); | |
1185 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, | |
1186 | "Pad addname: %ld \"%.*s\" FAKE\n", | |
1187 | (long)new_offset, | |
1188 | (int) SvCUR(new_namesv), SvPVX(new_namesv))); | |
809abb02 | 1189 | PARENT_FAKELEX_FLAGS_set(new_namesv, *out_flags); |
b5c19bd7 | 1190 | |
809abb02 | 1191 | PARENT_PAD_INDEX_set(new_namesv, 0); |
00b1698f | 1192 | if (SvPAD_OUR(new_namesv)) { |
6f207bd3 | 1193 | NOOP; /* do nothing */ |
b5c19bd7 | 1194 | } |
71f882da | 1195 | else if (CvLATE(cv)) { |
b5c19bd7 | 1196 | /* delayed creation - just note the offset within parent pad */ |
809abb02 | 1197 | PARENT_PAD_INDEX_set(new_namesv, offset); |
b5c19bd7 DM |
1198 | CvCLONE_on(cv); |
1199 | } | |
1200 | else { | |
1201 | /* immediate creation - capture outer value right now */ | |
1202 | av_store(PL_comppad, new_offset, SvREFCNT_inc(*new_capturep)); | |
1203 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, | |
1204 | "Pad findlex cv=0x%"UVxf" saved captured sv 0x%"UVxf" at offset %ld\n", | |
1205 | PTR2UV(cv), PTR2UV(*new_capturep), (long)new_offset)); | |
dd2155a4 | 1206 | } |
b5c19bd7 | 1207 | *out_name_sv = new_namesv; |
809abb02 | 1208 | *out_flags = PARENT_FAKELEX_FLAGS(new_namesv); |
b5c19bd7 DM |
1209 | |
1210 | PL_comppad_name = ocomppad_name; | |
1211 | PL_comppad = ocomppad; | |
4608196e | 1212 | PL_curpad = ocomppad ? AvARRAY(ocomppad) : NULL; |
dd2155a4 | 1213 | } |
b5c19bd7 | 1214 | return new_offset; |
dd2155a4 DM |
1215 | } |
1216 | ||
fb8a9836 | 1217 | #ifdef DEBUGGING |
cc76b5cc | 1218 | |
dd2155a4 | 1219 | /* |
cc76b5cc | 1220 | =for apidoc Am|SV *|pad_sv|PADOFFSET po |
dd2155a4 | 1221 | |
cc76b5cc | 1222 | Get the value at offset I<po> in the current (compiling or executing) pad. |
dd2155a4 DM |
1223 | Use macro PAD_SV instead of calling this function directly. |
1224 | ||
1225 | =cut | |
1226 | */ | |
1227 | ||
dd2155a4 DM |
1228 | SV * |
1229 | Perl_pad_sv(pTHX_ PADOFFSET po) | |
1230 | { | |
97aff369 | 1231 | dVAR; |
f3548bdc | 1232 | ASSERT_CURPAD_ACTIVE("pad_sv"); |
dd2155a4 | 1233 | |
dd2155a4 DM |
1234 | if (!po) |
1235 | Perl_croak(aTHX_ "panic: pad_sv po"); | |
dd2155a4 DM |
1236 | DEBUG_X(PerlIO_printf(Perl_debug_log, |
1237 | "Pad 0x%"UVxf"[0x%"UVxf"] sv: %ld sv=0x%"UVxf"\n", | |
f3548bdc | 1238 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(PL_curpad[po])) |
dd2155a4 DM |
1239 | ); |
1240 | return PL_curpad[po]; | |
1241 | } | |
1242 | ||
dd2155a4 | 1243 | /* |
cc76b5cc | 1244 | =for apidoc Am|void|pad_setsv|PADOFFSET po|SV *sv |
dd2155a4 | 1245 | |
cc76b5cc | 1246 | Set the value at offset I<po> in the current (compiling or executing) pad. |
dd2155a4 DM |
1247 | Use the macro PAD_SETSV() rather than calling this function directly. |
1248 | ||
1249 | =cut | |
1250 | */ | |
1251 | ||
dd2155a4 DM |
1252 | void |
1253 | Perl_pad_setsv(pTHX_ PADOFFSET po, SV* sv) | |
1254 | { | |
97aff369 | 1255 | dVAR; |
7918f24d NC |
1256 | |
1257 | PERL_ARGS_ASSERT_PAD_SETSV; | |
1258 | ||
f3548bdc | 1259 | ASSERT_CURPAD_ACTIVE("pad_setsv"); |
dd2155a4 DM |
1260 | |
1261 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
1262 | "Pad 0x%"UVxf"[0x%"UVxf"] setsv: %ld sv=0x%"UVxf"\n", | |
f3548bdc | 1263 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(sv)) |
dd2155a4 DM |
1264 | ); |
1265 | PL_curpad[po] = sv; | |
1266 | } | |
dd2155a4 | 1267 | |
cc76b5cc | 1268 | #endif /* DEBUGGING */ |
dd2155a4 DM |
1269 | |
1270 | /* | |
cc76b5cc | 1271 | =for apidoc m|void|pad_block_start|int full |
dd2155a4 DM |
1272 | |
1273 | Update the pad compilation state variables on entry to a new block | |
1274 | ||
1275 | =cut | |
1276 | */ | |
1277 | ||
1278 | /* XXX DAPM perhaps: | |
1279 | * - integrate this in general state-saving routine ??? | |
1280 | * - combine with the state-saving going on in pad_new ??? | |
1281 | * - introduce a new SAVE type that does all this in one go ? | |
1282 | */ | |
1283 | ||
1284 | void | |
1285 | Perl_pad_block_start(pTHX_ int full) | |
1286 | { | |
97aff369 | 1287 | dVAR; |
f3548bdc | 1288 | ASSERT_CURPAD_ACTIVE("pad_block_start"); |
dd2155a4 DM |
1289 | SAVEI32(PL_comppad_name_floor); |
1290 | PL_comppad_name_floor = AvFILLp(PL_comppad_name); | |
1291 | if (full) | |
1292 | PL_comppad_name_fill = PL_comppad_name_floor; | |
1293 | if (PL_comppad_name_floor < 0) | |
1294 | PL_comppad_name_floor = 0; | |
1295 | SAVEI32(PL_min_intro_pending); | |
1296 | SAVEI32(PL_max_intro_pending); | |
1297 | PL_min_intro_pending = 0; | |
1298 | SAVEI32(PL_comppad_name_fill); | |
1299 | SAVEI32(PL_padix_floor); | |
1300 | PL_padix_floor = PL_padix; | |
1301 | PL_pad_reset_pending = FALSE; | |
1302 | } | |
1303 | ||
dd2155a4 | 1304 | /* |
cc76b5cc | 1305 | =for apidoc m|U32|intro_my |
dd2155a4 DM |
1306 | |
1307 | "Introduce" my variables to visible status. | |
1308 | ||
1309 | =cut | |
1310 | */ | |
1311 | ||
1312 | U32 | |
1313 | Perl_intro_my(pTHX) | |
1314 | { | |
97aff369 | 1315 | dVAR; |
dd2155a4 | 1316 | SV **svp; |
dd2155a4 | 1317 | I32 i; |
6012dc80 | 1318 | U32 seq; |
dd2155a4 | 1319 | |
f3548bdc | 1320 | ASSERT_CURPAD_ACTIVE("intro_my"); |
dd2155a4 DM |
1321 | if (! PL_min_intro_pending) |
1322 | return PL_cop_seqmax; | |
1323 | ||
1324 | svp = AvARRAY(PL_comppad_name); | |
1325 | for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) { | |
53c1dcc0 AL |
1326 | SV * const sv = svp[i]; |
1327 | ||
0d311cdb DM |
1328 | if (sv && sv != &PL_sv_undef && !SvFAKE(sv) |
1329 | && COP_SEQ_RANGE_LOW(sv) == PERL_PADSEQ_INTRO) | |
1330 | { | |
2df5bdd7 | 1331 | COP_SEQ_RANGE_HIGH_set(sv, PERL_PADSEQ_INTRO); /* Don't know scope end yet. */ |
809abb02 | 1332 | COP_SEQ_RANGE_LOW_set(sv, PL_cop_seqmax); |
dd2155a4 | 1333 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
809abb02 | 1334 | "Pad intromy: %ld \"%s\", (%lu,%lu)\n", |
b15aece3 | 1335 | (long)i, SvPVX_const(sv), |
809abb02 NC |
1336 | (unsigned long)COP_SEQ_RANGE_LOW(sv), |
1337 | (unsigned long)COP_SEQ_RANGE_HIGH(sv)) | |
dd2155a4 DM |
1338 | ); |
1339 | } | |
1340 | } | |
6012dc80 DM |
1341 | seq = PL_cop_seqmax; |
1342 | PL_cop_seqmax++; | |
1343 | if (PL_cop_seqmax == PERL_PADSEQ_INTRO) /* not a legal value */ | |
1344 | PL_cop_seqmax++; | |
dd2155a4 DM |
1345 | PL_min_intro_pending = 0; |
1346 | PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */ | |
1347 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, | |
6012dc80 | 1348 | "Pad intromy: seq -> %ld\n", (long)(PL_cop_seqmax))); |
dd2155a4 | 1349 | |
6012dc80 | 1350 | return seq; |
dd2155a4 DM |
1351 | } |
1352 | ||
1353 | /* | |
cc76b5cc | 1354 | =for apidoc m|void|pad_leavemy |
dd2155a4 DM |
1355 | |
1356 | Cleanup at end of scope during compilation: set the max seq number for | |
1357 | lexicals in this scope and warn of any lexicals that never got introduced. | |
1358 | ||
1359 | =cut | |
1360 | */ | |
1361 | ||
1362 | void | |
1363 | Perl_pad_leavemy(pTHX) | |
1364 | { | |
97aff369 | 1365 | dVAR; |
dd2155a4 | 1366 | I32 off; |
551405c4 | 1367 | SV * const * const svp = AvARRAY(PL_comppad_name); |
dd2155a4 DM |
1368 | |
1369 | PL_pad_reset_pending = FALSE; | |
1370 | ||
f3548bdc | 1371 | ASSERT_CURPAD_ACTIVE("pad_leavemy"); |
dd2155a4 DM |
1372 | if (PL_min_intro_pending && PL_comppad_name_fill < PL_min_intro_pending) { |
1373 | for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) { | |
53c1dcc0 | 1374 | const SV * const sv = svp[off]; |
9b387841 NC |
1375 | if (sv && sv != &PL_sv_undef && !SvFAKE(sv)) |
1376 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), | |
1377 | "%"SVf" never introduced", | |
1378 | SVfARG(sv)); | |
dd2155a4 DM |
1379 | } |
1380 | } | |
1381 | /* "Deintroduce" my variables that are leaving with this scope. */ | |
1382 | for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_fill; off--) { | |
53c1dcc0 | 1383 | const SV * const sv = svp[off]; |
2df5bdd7 DM |
1384 | if (sv && sv != &PL_sv_undef && !SvFAKE(sv) |
1385 | && COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO) | |
1386 | { | |
809abb02 | 1387 | COP_SEQ_RANGE_HIGH_set(sv, PL_cop_seqmax); |
dd2155a4 | 1388 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
809abb02 | 1389 | "Pad leavemy: %ld \"%s\", (%lu,%lu)\n", |
b15aece3 | 1390 | (long)off, SvPVX_const(sv), |
809abb02 NC |
1391 | (unsigned long)COP_SEQ_RANGE_LOW(sv), |
1392 | (unsigned long)COP_SEQ_RANGE_HIGH(sv)) | |
dd2155a4 DM |
1393 | ); |
1394 | } | |
1395 | } | |
1396 | PL_cop_seqmax++; | |
6012dc80 DM |
1397 | if (PL_cop_seqmax == PERL_PADSEQ_INTRO) /* not a legal value */ |
1398 | PL_cop_seqmax++; | |
dd2155a4 DM |
1399 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
1400 | "Pad leavemy: seq = %ld\n", (long)PL_cop_seqmax)); | |
1401 | } | |
1402 | ||
dd2155a4 | 1403 | /* |
cc76b5cc | 1404 | =for apidoc m|void|pad_swipe|PADOFFSET po|bool refadjust |
dd2155a4 DM |
1405 | |
1406 | Abandon the tmp in the current pad at offset po and replace with a | |
1407 | new one. | |
1408 | ||
1409 | =cut | |
1410 | */ | |
1411 | ||
1412 | void | |
1413 | Perl_pad_swipe(pTHX_ PADOFFSET po, bool refadjust) | |
1414 | { | |
97aff369 | 1415 | dVAR; |
f3548bdc | 1416 | ASSERT_CURPAD_LEGAL("pad_swipe"); |
dd2155a4 DM |
1417 | if (!PL_curpad) |
1418 | return; | |
1419 | if (AvARRAY(PL_comppad) != PL_curpad) | |
1420 | Perl_croak(aTHX_ "panic: pad_swipe curpad"); | |
1421 | if (!po) | |
1422 | Perl_croak(aTHX_ "panic: pad_swipe po"); | |
1423 | ||
1424 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
1425 | "Pad 0x%"UVxf"[0x%"UVxf"] swipe: %ld\n", | |
1426 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po)); | |
1427 | ||
1428 | if (PL_curpad[po]) | |
1429 | SvPADTMP_off(PL_curpad[po]); | |
1430 | if (refadjust) | |
1431 | SvREFCNT_dec(PL_curpad[po]); | |
1432 | ||
9ad9869c DM |
1433 | |
1434 | /* if pad tmps aren't shared between ops, then there's no need to | |
1435 | * create a new tmp when an existing op is freed */ | |
1436 | #ifdef USE_BROKEN_PAD_RESET | |
561b68a9 | 1437 | PL_curpad[po] = newSV(0); |
dd2155a4 | 1438 | SvPADTMP_on(PL_curpad[po]); |
9ad9869c DM |
1439 | #else |
1440 | PL_curpad[po] = &PL_sv_undef; | |
97bf4a8d | 1441 | #endif |
dd2155a4 DM |
1442 | if ((I32)po < PL_padix) |
1443 | PL_padix = po - 1; | |
1444 | } | |
1445 | ||
dd2155a4 | 1446 | /* |
cc76b5cc | 1447 | =for apidoc m|void|pad_reset |
dd2155a4 DM |
1448 | |
1449 | Mark all the current temporaries for reuse | |
1450 | ||
1451 | =cut | |
1452 | */ | |
1453 | ||
1454 | /* XXX pad_reset() is currently disabled because it results in serious bugs. | |
1455 | * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed | |
1456 | * on the stack by OPs that use them, there are several ways to get an alias | |
1457 | * to a shared TARG. Such an alias will change randomly and unpredictably. | |
1458 | * We avoid doing this until we can think of a Better Way. | |
1459 | * GSAR 97-10-29 */ | |
1f676739 | 1460 | static void |
82af08ae | 1461 | S_pad_reset(pTHX) |
dd2155a4 | 1462 | { |
97aff369 | 1463 | dVAR; |
dd2155a4 | 1464 | #ifdef USE_BROKEN_PAD_RESET |
dd2155a4 DM |
1465 | if (AvARRAY(PL_comppad) != PL_curpad) |
1466 | Perl_croak(aTHX_ "panic: pad_reset curpad"); | |
1467 | ||
1468 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
1469 | "Pad 0x%"UVxf"[0x%"UVxf"] reset: padix %ld -> %ld", | |
1470 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), | |
1471 | (long)PL_padix, (long)PL_padix_floor | |
1472 | ) | |
1473 | ); | |
1474 | ||
1475 | if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */ | |
e1ec3a88 | 1476 | register I32 po; |
dd2155a4 DM |
1477 | for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) { |
1478 | if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po])) | |
1479 | SvPADTMP_off(PL_curpad[po]); | |
1480 | } | |
1481 | PL_padix = PL_padix_floor; | |
1482 | } | |
1483 | #endif | |
1484 | PL_pad_reset_pending = FALSE; | |
1485 | } | |
1486 | ||
dd2155a4 | 1487 | /* |
cc76b5cc Z |
1488 | =for apidoc Amx|void|pad_tidy|padtidy_type type |
1489 | ||
1490 | Tidy up a pad at the end of compilation of the code to which it belongs. | |
1491 | Jobs performed here are: remove most stuff from the pads of anonsub | |
1492 | prototypes; give it a @_; mark temporaries as such. I<type> indicates | |
1493 | the kind of subroutine: | |
dd2155a4 | 1494 | |
cc76b5cc Z |
1495 | padtidy_SUB ordinary subroutine |
1496 | padtidy_SUBCLONE prototype for lexical closure | |
1497 | padtidy_FORMAT format | |
dd2155a4 DM |
1498 | |
1499 | =cut | |
1500 | */ | |
1501 | ||
1502 | /* XXX DAPM surely most of this stuff should be done properly | |
1503 | * at the right time beforehand, rather than going around afterwards | |
1504 | * cleaning up our mistakes ??? | |
1505 | */ | |
1506 | ||
1507 | void | |
1508 | Perl_pad_tidy(pTHX_ padtidy_type type) | |
1509 | { | |
27da23d5 | 1510 | dVAR; |
dd2155a4 | 1511 | |
f3548bdc | 1512 | ASSERT_CURPAD_ACTIVE("pad_tidy"); |
b5c19bd7 DM |
1513 | |
1514 | /* If this CV has had any 'eval-capable' ops planted in it | |
1515 | * (ie it contains eval '...', //ee, /$var/ or /(?{..})/), Then any | |
1516 | * anon prototypes in the chain of CVs should be marked as cloneable, | |
1517 | * so that for example the eval's CV in C<< sub { eval '$x' } >> gets | |
1518 | * the right CvOUTSIDE. | |
1519 | * If running with -d, *any* sub may potentially have an eval | |
486ec47a | 1520 | * executed within it. |
b5c19bd7 DM |
1521 | */ |
1522 | ||
1523 | if (PL_cv_has_eval || PL_perldb) { | |
e1ec3a88 | 1524 | const CV *cv; |
b5c19bd7 DM |
1525 | for (cv = PL_compcv ;cv; cv = CvOUTSIDE(cv)) { |
1526 | if (cv != PL_compcv && CvCOMPILED(cv)) | |
1527 | break; /* no need to mark already-compiled code */ | |
1528 | if (CvANON(cv)) { | |
1529 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, | |
1530 | "Pad clone on cv=0x%"UVxf"\n", PTR2UV(cv))); | |
1531 | CvCLONE_on(cv); | |
1532 | } | |
1533 | } | |
1534 | } | |
1535 | ||
dd2155a4 DM |
1536 | /* extend curpad to match namepad */ |
1537 | if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad)) | |
a0714e2c | 1538 | av_store(PL_comppad_name, AvFILLp(PL_comppad), NULL); |
dd2155a4 DM |
1539 | |
1540 | if (type == padtidy_SUBCLONE) { | |
551405c4 | 1541 | SV * const * const namep = AvARRAY(PL_comppad_name); |
504618e9 | 1542 | PADOFFSET ix; |
b5c19bd7 | 1543 | |
dd2155a4 DM |
1544 | for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { |
1545 | SV *namesv; | |
1546 | ||
1547 | if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix])) | |
1548 | continue; | |
1549 | /* | |
1550 | * The only things that a clonable function needs in its | |
b5c19bd7 | 1551 | * pad are anonymous subs. |
dd2155a4 DM |
1552 | * The rest are created anew during cloning. |
1553 | */ | |
a0714e2c | 1554 | if (!((namesv = namep[ix]) != NULL && |
dd2155a4 | 1555 | namesv != &PL_sv_undef && |
b15aece3 | 1556 | *SvPVX_const(namesv) == '&')) |
dd2155a4 DM |
1557 | { |
1558 | SvREFCNT_dec(PL_curpad[ix]); | |
a0714e2c | 1559 | PL_curpad[ix] = NULL; |
dd2155a4 DM |
1560 | } |
1561 | } | |
1562 | } | |
1563 | else if (type == padtidy_SUB) { | |
1564 | /* XXX DAPM this same bit of code keeps appearing !!! Rationalise? */ | |
53c1dcc0 | 1565 | AV * const av = newAV(); /* Will be @_ */ |
ad64d0ec | 1566 | av_store(PL_comppad, 0, MUTABLE_SV(av)); |
11ca45c0 | 1567 | AvREIFY_only(av); |
dd2155a4 DM |
1568 | } |
1569 | ||
4cee4ca8 | 1570 | if (type == padtidy_SUB || type == padtidy_FORMAT) { |
adf8f095 | 1571 | SV * const * const namep = AvARRAY(PL_comppad_name); |
504618e9 | 1572 | PADOFFSET ix; |
dd2155a4 DM |
1573 | for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { |
1574 | if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix])) | |
1575 | continue; | |
adf8f095 | 1576 | if (!SvPADMY(PL_curpad[ix])) { |
dd2155a4 | 1577 | SvPADTMP_on(PL_curpad[ix]); |
adf8f095 NC |
1578 | } else if (!SvFAKE(namep[ix])) { |
1579 | /* This is a work around for how the current implementation of | |
1580 | ?{ } blocks in regexps interacts with lexicals. | |
1581 | ||
1582 | One of our lexicals. | |
1583 | Can't do this on all lexicals, otherwise sub baz() won't | |
1584 | compile in | |
1585 | ||
1586 | my $foo; | |
1587 | ||
1588 | sub bar { ++$foo; } | |
1589 | ||
1590 | sub baz { ++$foo; } | |
1591 | ||
1592 | because completion of compiling &bar calling pad_tidy() | |
1593 | would cause (top level) $foo to be marked as stale, and | |
1594 | "no longer available". */ | |
1595 | SvPADSTALE_on(PL_curpad[ix]); | |
1596 | } | |
dd2155a4 DM |
1597 | } |
1598 | } | |
f3548bdc | 1599 | PL_curpad = AvARRAY(PL_comppad); |
dd2155a4 DM |
1600 | } |
1601 | ||
dd2155a4 | 1602 | /* |
cc76b5cc | 1603 | =for apidoc m|void|pad_free|PADOFFSET po |
dd2155a4 | 1604 | |
8627550a | 1605 | Free the SV at offset po in the current pad. |
dd2155a4 DM |
1606 | |
1607 | =cut | |
1608 | */ | |
1609 | ||
1610 | /* XXX DAPM integrate with pad_swipe ???? */ | |
1611 | void | |
1612 | Perl_pad_free(pTHX_ PADOFFSET po) | |
1613 | { | |
97aff369 | 1614 | dVAR; |
f3548bdc | 1615 | ASSERT_CURPAD_LEGAL("pad_free"); |
dd2155a4 DM |
1616 | if (!PL_curpad) |
1617 | return; | |
1618 | if (AvARRAY(PL_comppad) != PL_curpad) | |
1619 | Perl_croak(aTHX_ "panic: pad_free curpad"); | |
1620 | if (!po) | |
1621 | Perl_croak(aTHX_ "panic: pad_free po"); | |
1622 | ||
1623 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
1624 | "Pad 0x%"UVxf"[0x%"UVxf"] free: %ld\n", | |
1625 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po) | |
1626 | ); | |
1627 | ||
1628 | if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) { | |
1629 | SvPADTMP_off(PL_curpad[po]); | |
dd2155a4 DM |
1630 | } |
1631 | if ((I32)po < PL_padix) | |
1632 | PL_padix = po - 1; | |
1633 | } | |
1634 | ||
dd2155a4 | 1635 | /* |
cc76b5cc | 1636 | =for apidoc m|void|do_dump_pad|I32 level|PerlIO *file|PADLIST *padlist|int full |
dd2155a4 DM |
1637 | |
1638 | Dump the contents of a padlist | |
1639 | ||
1640 | =cut | |
1641 | */ | |
1642 | ||
1643 | void | |
1644 | Perl_do_dump_pad(pTHX_ I32 level, PerlIO *file, PADLIST *padlist, int full) | |
1645 | { | |
97aff369 | 1646 | dVAR; |
e1ec3a88 AL |
1647 | const AV *pad_name; |
1648 | const AV *pad; | |
dd2155a4 DM |
1649 | SV **pname; |
1650 | SV **ppad; | |
dd2155a4 DM |
1651 | I32 ix; |
1652 | ||
7918f24d NC |
1653 | PERL_ARGS_ASSERT_DO_DUMP_PAD; |
1654 | ||
dd2155a4 DM |
1655 | if (!padlist) { |
1656 | return; | |
1657 | } | |
502c6561 NC |
1658 | pad_name = MUTABLE_AV(*av_fetch(MUTABLE_AV(padlist), 0, FALSE)); |
1659 | pad = MUTABLE_AV(*av_fetch(MUTABLE_AV(padlist), 1, FALSE)); | |
dd2155a4 DM |
1660 | pname = AvARRAY(pad_name); |
1661 | ppad = AvARRAY(pad); | |
1662 | Perl_dump_indent(aTHX_ level, file, | |
1663 | "PADNAME = 0x%"UVxf"(0x%"UVxf") PAD = 0x%"UVxf"(0x%"UVxf")\n", | |
1664 | PTR2UV(pad_name), PTR2UV(pname), PTR2UV(pad), PTR2UV(ppad) | |
1665 | ); | |
1666 | ||
1667 | for (ix = 1; ix <= AvFILLp(pad_name); ix++) { | |
e1ec3a88 | 1668 | const SV *namesv = pname[ix]; |
dd2155a4 | 1669 | if (namesv && namesv == &PL_sv_undef) { |
a0714e2c | 1670 | namesv = NULL; |
dd2155a4 DM |
1671 | } |
1672 | if (namesv) { | |
ee6cee0c DM |
1673 | if (SvFAKE(namesv)) |
1674 | Perl_dump_indent(aTHX_ level+1, file, | |
c0fd1b42 | 1675 | "%2d. 0x%"UVxf"<%lu> FAKE \"%s\" flags=0x%lx index=%lu\n", |
ee6cee0c DM |
1676 | (int) ix, |
1677 | PTR2UV(ppad[ix]), | |
1678 | (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0), | |
b15aece3 | 1679 | SvPVX_const(namesv), |
809abb02 NC |
1680 | (unsigned long)PARENT_FAKELEX_FLAGS(namesv), |
1681 | (unsigned long)PARENT_PAD_INDEX(namesv) | |
b5c19bd7 | 1682 | |
ee6cee0c DM |
1683 | ); |
1684 | else | |
1685 | Perl_dump_indent(aTHX_ level+1, file, | |
809abb02 | 1686 | "%2d. 0x%"UVxf"<%lu> (%lu,%lu) \"%s\"\n", |
ee6cee0c DM |
1687 | (int) ix, |
1688 | PTR2UV(ppad[ix]), | |
1689 | (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0), | |
809abb02 NC |
1690 | (unsigned long)COP_SEQ_RANGE_LOW(namesv), |
1691 | (unsigned long)COP_SEQ_RANGE_HIGH(namesv), | |
b15aece3 | 1692 | SvPVX_const(namesv) |
ee6cee0c | 1693 | ); |
dd2155a4 DM |
1694 | } |
1695 | else if (full) { | |
1696 | Perl_dump_indent(aTHX_ level+1, file, | |
1697 | "%2d. 0x%"UVxf"<%lu>\n", | |
1698 | (int) ix, | |
1699 | PTR2UV(ppad[ix]), | |
1700 | (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0) | |
1701 | ); | |
1702 | } | |
1703 | } | |
1704 | } | |
1705 | ||
cc76b5cc | 1706 | #ifdef DEBUGGING |
dd2155a4 DM |
1707 | |
1708 | /* | |
cc76b5cc | 1709 | =for apidoc m|void|cv_dump|CV *cv|const char *title |
dd2155a4 DM |
1710 | |
1711 | dump the contents of a CV | |
1712 | ||
1713 | =cut | |
1714 | */ | |
1715 | ||
dd2155a4 | 1716 | STATIC void |
e1ec3a88 | 1717 | S_cv_dump(pTHX_ const CV *cv, const char *title) |
dd2155a4 | 1718 | { |
97aff369 | 1719 | dVAR; |
53c1dcc0 AL |
1720 | const CV * const outside = CvOUTSIDE(cv); |
1721 | AV* const padlist = CvPADLIST(cv); | |
dd2155a4 | 1722 | |
7918f24d NC |
1723 | PERL_ARGS_ASSERT_CV_DUMP; |
1724 | ||
dd2155a4 DM |
1725 | PerlIO_printf(Perl_debug_log, |
1726 | " %s: CV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n", | |
1727 | title, | |
1728 | PTR2UV(cv), | |
1729 | (CvANON(cv) ? "ANON" | |
71f882da | 1730 | : (SvTYPE(cv) == SVt_PVFM) ? "FORMAT" |
dd2155a4 DM |
1731 | : (cv == PL_main_cv) ? "MAIN" |
1732 | : CvUNIQUE(cv) ? "UNIQUE" | |
1733 | : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"), | |
1734 | PTR2UV(outside), | |
1735 | (!outside ? "null" | |
1736 | : CvANON(outside) ? "ANON" | |
1737 | : (outside == PL_main_cv) ? "MAIN" | |
1738 | : CvUNIQUE(outside) ? "UNIQUE" | |
1739 | : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED")); | |
1740 | ||
1741 | PerlIO_printf(Perl_debug_log, | |
1742 | " PADLIST = 0x%"UVxf"\n", PTR2UV(padlist)); | |
1743 | do_dump_pad(1, Perl_debug_log, padlist, 1); | |
1744 | } | |
dd2155a4 | 1745 | |
cc76b5cc | 1746 | #endif /* DEBUGGING */ |
dd2155a4 DM |
1747 | |
1748 | /* | |
cc76b5cc | 1749 | =for apidoc Am|CV *|cv_clone|CV *proto |
dd2155a4 | 1750 | |
cc76b5cc Z |
1751 | Clone a CV, making a lexical closure. I<proto> supplies the prototype |
1752 | of the function: its code, pad structure, and other attributes. | |
1753 | The prototype is combined with a capture of outer lexicals to which the | |
1754 | code refers, which are taken from the currently-executing instance of | |
1755 | the immediately surrounding code. | |
dd2155a4 DM |
1756 | |
1757 | =cut | |
1758 | */ | |
1759 | ||
1760 | CV * | |
1761 | Perl_cv_clone(pTHX_ CV *proto) | |
1762 | { | |
27da23d5 | 1763 | dVAR; |
dd2155a4 | 1764 | I32 ix; |
53c1dcc0 | 1765 | AV* const protopadlist = CvPADLIST(proto); |
502c6561 NC |
1766 | const AV *const protopad_name = (const AV *)*av_fetch(protopadlist, 0, FALSE); |
1767 | const AV *const protopad = (const AV *)*av_fetch(protopadlist, 1, FALSE); | |
53c1dcc0 AL |
1768 | SV** const pname = AvARRAY(protopad_name); |
1769 | SV** const ppad = AvARRAY(protopad); | |
e1ec3a88 AL |
1770 | const I32 fname = AvFILLp(protopad_name); |
1771 | const I32 fpad = AvFILLp(protopad); | |
dd2155a4 | 1772 | CV* cv; |
b5c19bd7 DM |
1773 | SV** outpad; |
1774 | CV* outside; | |
71f882da | 1775 | long depth; |
dd2155a4 | 1776 | |
7918f24d NC |
1777 | PERL_ARGS_ASSERT_CV_CLONE; |
1778 | ||
dd2155a4 DM |
1779 | assert(!CvUNIQUE(proto)); |
1780 | ||
71f882da DM |
1781 | /* Since cloneable anon subs can be nested, CvOUTSIDE may point |
1782 | * to a prototype; we instead want the cloned parent who called us. | |
1783 | * Note that in general for formats, CvOUTSIDE != find_runcv */ | |
1784 | ||
1785 | outside = CvOUTSIDE(proto); | |
1786 | if (outside && CvCLONE(outside) && ! CvCLONED(outside)) | |
1787 | outside = find_runcv(NULL); | |
1788 | depth = CvDEPTH(outside); | |
1789 | assert(depth || SvTYPE(proto) == SVt_PVFM); | |
1790 | if (!depth) | |
1791 | depth = 1; | |
b5c19bd7 DM |
1792 | assert(CvPADLIST(outside)); |
1793 | ||
dd2155a4 DM |
1794 | ENTER; |
1795 | SAVESPTR(PL_compcv); | |
1796 | ||
ea726b52 | 1797 | cv = PL_compcv = MUTABLE_CV(newSV_type(SvTYPE(proto))); |
c794ca97 | 1798 | CvFLAGS(cv) = CvFLAGS(proto) & ~(CVf_CLONE|CVf_WEAKOUTSIDE|CVf_CVGV_RC); |
dd2155a4 DM |
1799 | CvCLONED_on(cv); |
1800 | ||
dd2155a4 | 1801 | #ifdef USE_ITHREADS |
aed2304a NC |
1802 | CvFILE(cv) = CvISXSUB(proto) ? CvFILE(proto) |
1803 | : savepv(CvFILE(proto)); | |
dd2155a4 DM |
1804 | #else |
1805 | CvFILE(cv) = CvFILE(proto); | |
1806 | #endif | |
b3f91e91 | 1807 | CvGV_set(cv,CvGV(proto)); |
c68d9564 | 1808 | CvSTASH_set(cv, CvSTASH(proto)); |
b34c0dd4 | 1809 | OP_REFCNT_LOCK; |
dd2155a4 | 1810 | CvROOT(cv) = OpREFCNT_inc(CvROOT(proto)); |
b34c0dd4 | 1811 | OP_REFCNT_UNLOCK; |
dd2155a4 | 1812 | CvSTART(cv) = CvSTART(proto); |
ea726b52 | 1813 | CvOUTSIDE(cv) = MUTABLE_CV(SvREFCNT_inc_simple(outside)); |
b5c19bd7 | 1814 | CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(proto); |
dd2155a4 DM |
1815 | |
1816 | if (SvPOK(proto)) | |
ad64d0ec | 1817 | sv_setpvn(MUTABLE_SV(cv), SvPVX_const(proto), SvCUR(proto)); |
dd2155a4 | 1818 | |
b7787f18 | 1819 | CvPADLIST(cv) = pad_new(padnew_CLONE|padnew_SAVE); |
dd2155a4 | 1820 | |
b5c19bd7 | 1821 | av_fill(PL_comppad, fpad); |
7aaef02a | 1822 | for (ix = fname; ix > 0; ix--) |
dd2155a4 DM |
1823 | av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix])); |
1824 | ||
dd2155a4 DM |
1825 | PL_curpad = AvARRAY(PL_comppad); |
1826 | ||
71f882da | 1827 | outpad = AvARRAY(AvARRAY(CvPADLIST(outside))[depth]); |
b5c19bd7 | 1828 | |
dd2155a4 | 1829 | for (ix = fpad; ix > 0; ix--) { |
a0714e2c SS |
1830 | SV* const namesv = (ix <= fname) ? pname[ix] : NULL; |
1831 | SV *sv = NULL; | |
71f882da | 1832 | if (namesv && namesv != &PL_sv_undef) { /* lexical */ |
b5c19bd7 | 1833 | if (SvFAKE(namesv)) { /* lexical from outside? */ |
809abb02 | 1834 | sv = outpad[PARENT_PAD_INDEX(namesv)]; |
71f882da | 1835 | assert(sv); |
33894c1a DM |
1836 | /* formats may have an inactive parent, |
1837 | while my $x if $false can leave an active var marked as | |
d1186544 DM |
1838 | stale. And state vars are always available */ |
1839 | if (SvPADSTALE(sv) && !SvPAD_STATE(namesv)) { | |
a2a5de95 NC |
1840 | Perl_ck_warner(aTHX_ packWARN(WARN_CLOSURE), |
1841 | "Variable \"%s\" is not available", SvPVX_const(namesv)); | |
a0714e2c | 1842 | sv = NULL; |
71f882da | 1843 | } |
33894c1a | 1844 | else |
f84c484e | 1845 | SvREFCNT_inc_simple_void_NN(sv); |
dd2155a4 | 1846 | } |
71f882da | 1847 | if (!sv) { |
b15aece3 | 1848 | const char sigil = SvPVX_const(namesv)[0]; |
e1ec3a88 | 1849 | if (sigil == '&') |
dd2155a4 | 1850 | sv = SvREFCNT_inc(ppad[ix]); |
e1ec3a88 | 1851 | else if (sigil == '@') |
ad64d0ec | 1852 | sv = MUTABLE_SV(newAV()); |
e1ec3a88 | 1853 | else if (sigil == '%') |
ad64d0ec | 1854 | sv = MUTABLE_SV(newHV()); |
dd2155a4 | 1855 | else |
561b68a9 | 1856 | sv = newSV(0); |
235cc2e3 | 1857 | SvPADMY_on(sv); |
0d3b281c DM |
1858 | /* reset the 'assign only once' flag on each state var */ |
1859 | if (SvPAD_STATE(namesv)) | |
1860 | SvPADSTALE_on(sv); | |
dd2155a4 DM |
1861 | } |
1862 | } | |
1863 | else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) { | |
f84c484e | 1864 | sv = SvREFCNT_inc_NN(ppad[ix]); |
dd2155a4 DM |
1865 | } |
1866 | else { | |
561b68a9 | 1867 | sv = newSV(0); |
dd2155a4 | 1868 | SvPADTMP_on(sv); |
dd2155a4 | 1869 | } |
71f882da | 1870 | PL_curpad[ix] = sv; |
dd2155a4 DM |
1871 | } |
1872 | ||
dd2155a4 DM |
1873 | DEBUG_Xv( |
1874 | PerlIO_printf(Perl_debug_log, "\nPad CV clone\n"); | |
1875 | cv_dump(outside, "Outside"); | |
1876 | cv_dump(proto, "Proto"); | |
1877 | cv_dump(cv, "To"); | |
1878 | ); | |
1879 | ||
1880 | LEAVE; | |
1881 | ||
1882 | if (CvCONST(cv)) { | |
b5c19bd7 DM |
1883 | /* Constant sub () { $x } closing over $x - see lib/constant.pm: |
1884 | * The prototype was marked as a candiate for const-ization, | |
1885 | * so try to grab the current const value, and if successful, | |
1886 | * turn into a const sub: | |
1887 | */ | |
551405c4 | 1888 | SV* const const_sv = op_const_sv(CvSTART(cv), cv); |
b5c19bd7 DM |
1889 | if (const_sv) { |
1890 | SvREFCNT_dec(cv); | |
bd61b366 | 1891 | cv = newCONSTSUB(CvSTASH(proto), NULL, const_sv); |
b5c19bd7 DM |
1892 | } |
1893 | else { | |
1894 | CvCONST_off(cv); | |
1895 | } | |
dd2155a4 DM |
1896 | } |
1897 | ||
1898 | return cv; | |
1899 | } | |
1900 | ||
dd2155a4 | 1901 | /* |
cc76b5cc | 1902 | =for apidoc m|void|pad_fixup_inner_anons|PADLIST *padlist|CV *old_cv|CV *new_cv |
dd2155a4 DM |
1903 | |
1904 | For any anon CVs in the pad, change CvOUTSIDE of that CV from | |
7dafbf52 DM |
1905 | old_cv to new_cv if necessary. Needed when a newly-compiled CV has to be |
1906 | moved to a pre-existing CV struct. | |
dd2155a4 DM |
1907 | |
1908 | =cut | |
1909 | */ | |
1910 | ||
1911 | void | |
1912 | Perl_pad_fixup_inner_anons(pTHX_ PADLIST *padlist, CV *old_cv, CV *new_cv) | |
1913 | { | |
97aff369 | 1914 | dVAR; |
dd2155a4 | 1915 | I32 ix; |
502c6561 NC |
1916 | AV * const comppad_name = MUTABLE_AV(AvARRAY(padlist)[0]); |
1917 | AV * const comppad = MUTABLE_AV(AvARRAY(padlist)[1]); | |
53c1dcc0 AL |
1918 | SV ** const namepad = AvARRAY(comppad_name); |
1919 | SV ** const curpad = AvARRAY(comppad); | |
7918f24d NC |
1920 | |
1921 | PERL_ARGS_ASSERT_PAD_FIXUP_INNER_ANONS; | |
294a48e9 AL |
1922 | PERL_UNUSED_ARG(old_cv); |
1923 | ||
dd2155a4 | 1924 | for (ix = AvFILLp(comppad_name); ix > 0; ix--) { |
551405c4 | 1925 | const SV * const namesv = namepad[ix]; |
dd2155a4 | 1926 | if (namesv && namesv != &PL_sv_undef |
b15aece3 | 1927 | && *SvPVX_const(namesv) == '&') |
dd2155a4 | 1928 | { |
ea726b52 | 1929 | CV * const innercv = MUTABLE_CV(curpad[ix]); |
7dafbf52 DM |
1930 | assert(CvWEAKOUTSIDE(innercv)); |
1931 | assert(CvOUTSIDE(innercv) == old_cv); | |
1932 | CvOUTSIDE(innercv) = new_cv; | |
dd2155a4 DM |
1933 | } |
1934 | } | |
1935 | } | |
1936 | ||
1937 | /* | |
cc76b5cc | 1938 | =for apidoc m|void|pad_push|PADLIST *padlist|int depth |
dd2155a4 DM |
1939 | |
1940 | Push a new pad frame onto the padlist, unless there's already a pad at | |
26019298 AL |
1941 | this depth, in which case don't bother creating a new one. Then give |
1942 | the new pad an @_ in slot zero. | |
dd2155a4 DM |
1943 | |
1944 | =cut | |
1945 | */ | |
1946 | ||
1947 | void | |
26019298 | 1948 | Perl_pad_push(pTHX_ PADLIST *padlist, int depth) |
dd2155a4 | 1949 | { |
97aff369 | 1950 | dVAR; |
7918f24d NC |
1951 | |
1952 | PERL_ARGS_ASSERT_PAD_PUSH; | |
1953 | ||
b37c2d43 | 1954 | if (depth > AvFILLp(padlist)) { |
44f8325f AL |
1955 | SV** const svp = AvARRAY(padlist); |
1956 | AV* const newpad = newAV(); | |
1957 | SV** const oldpad = AvARRAY(svp[depth-1]); | |
502c6561 NC |
1958 | I32 ix = AvFILLp((const AV *)svp[1]); |
1959 | const I32 names_fill = AvFILLp((const AV *)svp[0]); | |
44f8325f | 1960 | SV** const names = AvARRAY(svp[0]); |
26019298 AL |
1961 | AV *av; |
1962 | ||
dd2155a4 DM |
1963 | for ( ;ix > 0; ix--) { |
1964 | if (names_fill >= ix && names[ix] != &PL_sv_undef) { | |
b15aece3 | 1965 | const char sigil = SvPVX_const(names[ix])[0]; |
fda94784 RGS |
1966 | if ((SvFLAGS(names[ix]) & SVf_FAKE) |
1967 | || (SvFLAGS(names[ix]) & SVpad_STATE) | |
1968 | || sigil == '&') | |
1969 | { | |
dd2155a4 DM |
1970 | /* outer lexical or anon code */ |
1971 | av_store(newpad, ix, SvREFCNT_inc(oldpad[ix])); | |
1972 | } | |
1973 | else { /* our own lexical */ | |
26019298 AL |
1974 | SV *sv; |
1975 | if (sigil == '@') | |
ad64d0ec | 1976 | sv = MUTABLE_SV(newAV()); |
26019298 | 1977 | else if (sigil == '%') |
ad64d0ec | 1978 | sv = MUTABLE_SV(newHV()); |
dd2155a4 | 1979 | else |
561b68a9 | 1980 | sv = newSV(0); |
26019298 | 1981 | av_store(newpad, ix, sv); |
dd2155a4 DM |
1982 | SvPADMY_on(sv); |
1983 | } | |
1984 | } | |
1985 | else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) { | |
f84c484e | 1986 | av_store(newpad, ix, SvREFCNT_inc_NN(oldpad[ix])); |
dd2155a4 DM |
1987 | } |
1988 | else { | |
1989 | /* save temporaries on recursion? */ | |
561b68a9 | 1990 | SV * const sv = newSV(0); |
26019298 | 1991 | av_store(newpad, ix, sv); |
dd2155a4 DM |
1992 | SvPADTMP_on(sv); |
1993 | } | |
1994 | } | |
26019298 | 1995 | av = newAV(); |
ad64d0ec | 1996 | av_store(newpad, 0, MUTABLE_SV(av)); |
11ca45c0 | 1997 | AvREIFY_only(av); |
26019298 | 1998 | |
ad64d0ec | 1999 | av_store(padlist, depth, MUTABLE_SV(newpad)); |
dd2155a4 DM |
2000 | AvFILLp(padlist) = depth; |
2001 | } | |
2002 | } | |
b21dc031 | 2003 | |
cc76b5cc Z |
2004 | /* |
2005 | =for apidoc Am|HV *|pad_compname_type|PADOFFSET po | |
2006 | ||
2007 | Looks up the type of the lexical variable at position I<po> in the | |
2008 | currently-compiling pad. If the variable is typed, the stash of the | |
2009 | class to which it is typed is returned. If not, C<NULL> is returned. | |
2010 | ||
2011 | =cut | |
2012 | */ | |
b21dc031 AL |
2013 | |
2014 | HV * | |
2015 | Perl_pad_compname_type(pTHX_ const PADOFFSET po) | |
2016 | { | |
97aff369 | 2017 | dVAR; |
551405c4 | 2018 | SV* const * const av = av_fetch(PL_comppad_name, po, FALSE); |
00b1698f | 2019 | if ( SvPAD_TYPED(*av) ) { |
b21dc031 AL |
2020 | return SvSTASH(*av); |
2021 | } | |
5c284bb0 | 2022 | return NULL; |
b21dc031 | 2023 | } |
66610fdd | 2024 | |
d5b1589c NC |
2025 | #if defined(USE_ITHREADS) |
2026 | ||
2027 | # define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t)) | |
2028 | ||
cc76b5cc Z |
2029 | /* |
2030 | =for apidoc m|AV *|padlist_dup|AV *srcpad|CLONE_PARAMS *param | |
2031 | ||
2032 | Duplicates a pad. | |
2033 | ||
2034 | =cut | |
2035 | */ | |
2036 | ||
d5b1589c | 2037 | AV * |
cc76b5cc | 2038 | Perl_padlist_dup(pTHX_ AV *srcpad, CLONE_PARAMS *param) |
d5b1589c NC |
2039 | { |
2040 | AV *dstpad; | |
2041 | PERL_ARGS_ASSERT_PADLIST_DUP; | |
2042 | ||
2043 | if (!srcpad) | |
2044 | return NULL; | |
2045 | ||
2046 | assert(!AvREAL(srcpad)); | |
6de654a5 NC |
2047 | |
2048 | if (param->flags & CLONEf_COPY_STACKS | |
2049 | || SvREFCNT(AvARRAY(srcpad)[1]) > 1) { | |
2050 | /* XXX padlists are real, but pretend to be not */ | |
2051 | AvREAL_on(srcpad); | |
2052 | dstpad = av_dup_inc(srcpad, param); | |
2053 | AvREAL_off(srcpad); | |
2054 | AvREAL_off(dstpad); | |
2055 | assert (SvREFCNT(AvARRAY(srcpad)[1]) == 1); | |
2056 | } else { | |
2057 | /* CvDEPTH() on our subroutine will be set to 0, so there's no need | |
2058 | to build anything other than the first level of pads. */ | |
2059 | ||
2060 | I32 ix = AvFILLp((const AV *)AvARRAY(srcpad)[1]); | |
2061 | AV *pad1; | |
05d04d9c | 2062 | const I32 names_fill = AvFILLp((const AV *)(AvARRAY(srcpad)[0])); |
6de654a5 NC |
2063 | const AV *const srcpad1 = (const AV *) AvARRAY(srcpad)[1]; |
2064 | SV **oldpad = AvARRAY(srcpad1); | |
2065 | SV **names; | |
2066 | SV **pad1a; | |
2067 | AV *args; | |
2068 | /* look for it in the table first. | |
2069 | I *think* that it shouldn't be possible to find it there. | |
2070 | Well, except for how Perl_sv_compile_2op() "works" :-( */ | |
2071 | dstpad = (AV*)ptr_table_fetch(PL_ptr_table, srcpad); | |
2072 | ||
2073 | if (dstpad) | |
2074 | return dstpad; | |
2075 | ||
2076 | dstpad = newAV(); | |
2077 | ptr_table_store(PL_ptr_table, srcpad, dstpad); | |
2078 | AvREAL_off(dstpad); | |
2079 | av_extend(dstpad, 1); | |
2080 | AvARRAY(dstpad)[0] = MUTABLE_SV(av_dup_inc(AvARRAY(srcpad)[0], param)); | |
2081 | names = AvARRAY(AvARRAY(dstpad)[0]); | |
2082 | ||
2083 | pad1 = newAV(); | |
2084 | ||
2085 | av_extend(pad1, ix); | |
2086 | AvARRAY(dstpad)[1] = MUTABLE_SV(pad1); | |
2087 | pad1a = AvARRAY(pad1); | |
2088 | AvFILLp(dstpad) = 1; | |
2089 | ||
2090 | if (ix > -1) { | |
2091 | AvFILLp(pad1) = ix; | |
2092 | ||
2093 | for ( ;ix > 0; ix--) { | |
05d04d9c NC |
2094 | if (!oldpad[ix]) { |
2095 | pad1a[ix] = NULL; | |
2096 | } else if (names_fill >= ix && names[ix] != &PL_sv_undef) { | |
2097 | const char sigil = SvPVX_const(names[ix])[0]; | |
2098 | if ((SvFLAGS(names[ix]) & SVf_FAKE) | |
2099 | || (SvFLAGS(names[ix]) & SVpad_STATE) | |
2100 | || sigil == '&') | |
2101 | { | |
2102 | /* outer lexical or anon code */ | |
2103 | pad1a[ix] = sv_dup_inc(oldpad[ix], param); | |
2104 | } | |
2105 | else { /* our own lexical */ | |
adf8f095 NC |
2106 | if(SvPADSTALE(oldpad[ix]) && SvREFCNT(oldpad[ix]) > 1) { |
2107 | /* This is a work around for how the current | |
2108 | implementation of ?{ } blocks in regexps | |
2109 | interacts with lexicals. */ | |
05d04d9c NC |
2110 | pad1a[ix] = sv_dup_inc(oldpad[ix], param); |
2111 | } else { | |
2112 | SV *sv; | |
2113 | ||
2114 | if (sigil == '@') | |
2115 | sv = MUTABLE_SV(newAV()); | |
2116 | else if (sigil == '%') | |
2117 | sv = MUTABLE_SV(newHV()); | |
2118 | else | |
2119 | sv = newSV(0); | |
2120 | pad1a[ix] = sv; | |
2121 | SvPADMY_on(sv); | |
2122 | } | |
2123 | } | |
2124 | } | |
2125 | else if (IS_PADGV(oldpad[ix]) || IS_PADCONST(oldpad[ix])) { | |
2126 | pad1a[ix] = sv_dup_inc(oldpad[ix], param); | |
2127 | } | |
2128 | else { | |
2129 | /* save temporaries on recursion? */ | |
2130 | SV * const sv = newSV(0); | |
2131 | pad1a[ix] = sv; | |
2132 | ||
2133 | /* SvREFCNT(oldpad[ix]) != 1 for some code in threads.xs | |
2134 | FIXTHAT before merging this branch. | |
2135 | (And I know how to) */ | |
2136 | if (SvPADMY(oldpad[ix])) | |
2137 | SvPADMY_on(sv); | |
2138 | else | |
2139 | SvPADTMP_on(sv); | |
2140 | } | |
6de654a5 NC |
2141 | } |
2142 | ||
2143 | if (oldpad[0]) { | |
2144 | args = newAV(); /* Will be @_ */ | |
2145 | AvREIFY_only(args); | |
2146 | pad1a[0] = (SV *)args; | |
2147 | } | |
2148 | } | |
2149 | } | |
d5b1589c NC |
2150 | |
2151 | return dstpad; | |
2152 | } | |
2153 | ||
cc76b5cc | 2154 | #endif /* USE_ITHREADS */ |
d5b1589c | 2155 | |
66610fdd RGS |
2156 | /* |
2157 | * Local variables: | |
2158 | * c-indentation-style: bsd | |
2159 | * c-basic-offset: 4 | |
2160 | * indent-tabs-mode: t | |
2161 | * End: | |
2162 | * | |
37442d52 RGS |
2163 | * ex: set ts=8 sts=4 sw=4 noet: |
2164 | */ |