Commit | Line | Data |
---|---|---|
dd2155a4 DM |
1 | /* pad.h |
2 | * | |
1129b882 | 3 | * Copyright (C) 2002, 2003, 2005, 2006, 2007, 2008 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 | * This file defines the types and macros associated with the API for | |
9 | * manipulating scratchpads, which are used by perl to store lexical | |
10 | * variables, op targets and constants. | |
11 | */ | |
12 | ||
cc76b5cc Z |
13 | /* |
14 | =head1 Pad Data Structures | |
15 | */ | |
dd2155a4 DM |
16 | |
17 | ||
18 | /* a padlist is currently just an AV; but that might change, | |
19 | * so hide the type. Ditto a pad. */ | |
20 | ||
21 | typedef AV PADLIST; | |
f3548bdc | 22 | typedef AV PAD; |
dd2155a4 DM |
23 | |
24 | ||
25 | /* offsets within a pad */ | |
26 | ||
27 | #if PTRSIZE == 4 | |
28 | typedef U32TYPE PADOFFSET; | |
29 | #else | |
30 | # if PTRSIZE == 8 | |
31 | typedef U64TYPE PADOFFSET; | |
32 | # endif | |
33 | #endif | |
34 | #define NOT_IN_PAD ((PADOFFSET) -1) | |
809abb02 | 35 | |
2df5bdd7 DM |
36 | /* a value that PL_cop_seqmax is guaranteed never to be, |
37 | * flagging that a lexical is being introduced, or has not yet left scope | |
38 | */ | |
39 | #define PERL_PADSEQ_INTRO U32_MAX | |
40 | ||
41 | ||
7948fc08 | 42 | /* B.xs needs these for the benefit of B::Deparse */ |
809abb02 | 43 | /* Low range end is exclusive (valid from the cop seq after this one) */ |
809abb02 | 44 | /* High range end is inclusive (valid up to this cop seq) */ |
809abb02 | 45 | |
3441fb63 NC |
46 | #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN) |
47 | # define COP_SEQ_RANGE_LOW(sv) \ | |
b1bc3f34 | 48 | (({ const SV *const _sv_cop_seq_range_low = (const SV *) (sv); \ |
7948fc08 RU |
49 | assert(SvTYPE(_sv_cop_seq_range_low) == SVt_NV \ |
50 | || SvTYPE(_sv_cop_seq_range_low) >= SVt_PVNV); \ | |
51 | assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVAV); \ | |
52 | assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVHV); \ | |
53 | assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVCV); \ | |
54 | assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVFM); \ | |
55 | assert(!isGV_with_GP(_sv_cop_seq_range_low)); \ | |
b1bc3f34 | 56 | ((XPVNV*) MUTABLE_PTR(SvANY(_sv_cop_seq_range_low)))->xnv_u.xpad_cop_seq.xlow; \ |
3441fb63 NC |
57 | })) |
58 | # define COP_SEQ_RANGE_HIGH(sv) \ | |
b1bc3f34 | 59 | (({ const SV *const _sv_cop_seq_range_high = (const SV *) (sv); \ |
7948fc08 RU |
60 | assert(SvTYPE(_sv_cop_seq_range_high) == SVt_NV \ |
61 | || SvTYPE(_sv_cop_seq_range_high) >= SVt_PVNV); \ | |
62 | assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVAV); \ | |
63 | assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVHV); \ | |
64 | assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVCV); \ | |
65 | assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVFM); \ | |
66 | assert(!isGV_with_GP(_sv_cop_seq_range_high)); \ | |
b1bc3f34 | 67 | ((XPVNV*) MUTABLE_PTR(SvANY(_sv_cop_seq_range_high)))->xnv_u.xpad_cop_seq.xhigh; \ |
3441fb63 NC |
68 | })) |
69 | # define PARENT_PAD_INDEX(sv) \ | |
b1bc3f34 | 70 | (({ const SV *const _sv_parent_pad_index = (const SV *) (sv); \ |
7948fc08 RU |
71 | assert(SvTYPE(_sv_parent_pad_index) == SVt_NV \ |
72 | || SvTYPE(_sv_parent_pad_index) >= SVt_PVNV); \ | |
73 | assert(SvTYPE(_sv_parent_pad_index) != SVt_PVAV); \ | |
74 | assert(SvTYPE(_sv_parent_pad_index) != SVt_PVHV); \ | |
75 | assert(SvTYPE(_sv_parent_pad_index) != SVt_PVCV); \ | |
76 | assert(SvTYPE(_sv_parent_pad_index) != SVt_PVFM); \ | |
77 | assert(!isGV_with_GP(_sv_parent_pad_index)); \ | |
b1bc3f34 | 78 | ((XPVNV*) MUTABLE_PTR(SvANY(_sv_parent_pad_index)))->xnv_u.xpad_cop_seq.xlow; \ |
3441fb63 NC |
79 | })) |
80 | # define PARENT_FAKELEX_FLAGS(sv) \ | |
b1bc3f34 | 81 | (({ const SV *const _sv_parent_fakelex_flags = (const SV *) (sv); \ |
7948fc08 RU |
82 | assert(SvTYPE(_sv_parent_fakelex_flags) == SVt_NV \ |
83 | || SvTYPE(_sv_parent_fakelex_flags) >= SVt_PVNV); \ | |
84 | assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVAV); \ | |
85 | assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVHV); \ | |
86 | assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVCV); \ | |
87 | assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVFM); \ | |
88 | assert(!isGV_with_GP(_sv_parent_fakelex_flags)); \ | |
b1bc3f34 | 89 | ((XPVNV*) MUTABLE_PTR(SvANY(_sv_parent_fakelex_flags)))->xnv_u.xpad_cop_seq.xhigh; \ |
3441fb63 NC |
90 | })) |
91 | #else | |
92 | # define COP_SEQ_RANGE_LOW(sv) \ | |
93 | (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow)) | |
94 | # define COP_SEQ_RANGE_HIGH(sv) \ | |
95 | (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh)) | |
96 | ||
97 | ||
98 | # define PARENT_PAD_INDEX(sv) \ | |
99 | (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow)) | |
100 | # define PARENT_FAKELEX_FLAGS(sv) \ | |
101 | (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh)) | |
102 | #endif | |
dd2155a4 | 103 | |
6c5e080d | 104 | /* Flags set in the SvIVX field of FAKE namesvs */ |
7948fc08 | 105 | |
6c5e080d NC |
106 | #define PAD_FAKELEX_ANON 1 /* the lex is declared in an ANON, or ... */ |
107 | #define PAD_FAKELEX_MULTI 2 /* the lex can be instantiated multiple times */ | |
108 | ||
dd2155a4 DM |
109 | /* flags for the pad_new() function */ |
110 | ||
c7c737cb DM |
111 | #define padnew_CLONE 1 /* this pad is for a cloned CV */ |
112 | #define padnew_SAVE 2 /* save old globals */ | |
113 | #define padnew_SAVESUB 4 /* also save extra stuff for start of sub */ | |
dd2155a4 DM |
114 | |
115 | /* values for the pad_tidy() function */ | |
116 | ||
117 | typedef enum { | |
118 | padtidy_SUB, /* tidy up a pad for a sub, */ | |
119 | padtidy_SUBCLONE, /* a cloned sub, */ | |
120 | padtidy_FORMAT /* or a format */ | |
121 | } padtidy_type; | |
122 | ||
9f6ec8dd | 123 | /* flags for pad_add_name_pvn. */ |
35f82371 | 124 | |
9f6ec8dd BF |
125 | #define padadd_OUR 0x01 /* our declaration. */ |
126 | #define padadd_STATE 0x02 /* state declaration. */ | |
127 | #define padadd_NO_DUP_CHECK 0x04 /* skip warning on dups. */ | |
128 | #define padadd_UTF8_NAME SVf_UTF8 /* name is UTF-8 encoded. */ | |
35f82371 | 129 | |
f3548bdc DM |
130 | /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine |
131 | * whether PL_comppad and PL_curpad are consistent and whether they have | |
132 | * active values */ | |
dd2155a4 | 133 | |
1dba731d NC |
134 | #ifndef PERL_MAD |
135 | # define pad_peg(label) | |
136 | #endif | |
137 | ||
f3548bdc DM |
138 | #ifdef DEBUGGING |
139 | # define ASSERT_CURPAD_LEGAL(label) \ | |
1dba731d | 140 | pad_peg(label); \ |
f3548bdc DM |
141 | if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0)) \ |
142 | Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%"UVxf"[0x%"UVxf"]",\ | |
143 | label, PTR2UV(PL_comppad), PTR2UV(PL_curpad)); | |
144 | ||
145 | ||
146 | # define ASSERT_CURPAD_ACTIVE(label) \ | |
1dba731d | 147 | pad_peg(label); \ |
f3548bdc DM |
148 | if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad)) \ |
149 | Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%"UVxf"[0x%"UVxf"]",\ | |
150 | label, PTR2UV(PL_comppad), PTR2UV(PL_curpad)); | |
151 | #else | |
152 | # define ASSERT_CURPAD_LEGAL(label) | |
153 | # define ASSERT_CURPAD_ACTIVE(label) | |
154 | #endif | |
155 | ||
156 | ||
157 | ||
158 | /* Note: the following three macros are actually defined in scope.h, but | |
dd2155a4 DM |
159 | * they are documented here for completeness, since they directly or |
160 | * indirectly affect pads. | |
161 | ||
162 | =for apidoc m|void|SAVEPADSV |PADOFFSET po | |
163 | Save a pad slot (used to restore after an iteration) | |
164 | ||
f3548bdc | 165 | XXX DAPM it would make more sense to make the arg a PADOFFSET |
dd2155a4 | 166 | =for apidoc m|void|SAVECLEARSV |SV **svp |
eeb8d49e | 167 | Clear the pointed to pad value on scope exit. (i.e. the runtime action of 'my') |
dd2155a4 DM |
168 | |
169 | =for apidoc m|void|SAVECOMPPAD | |
170 | save PL_comppad and PL_curpad | |
171 | ||
dd2155a4 DM |
172 | |
173 | ||
174 | ||
175 | ||
176 | =for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv | |
177 | Set the slot at offset C<po> in the current pad to C<sv> | |
178 | ||
179 | =for apidoc m|void|PAD_SV |PADOFFSET po | |
180 | Get the value at offset C<po> in the current pad | |
181 | ||
182 | =for apidoc m|SV *|PAD_SVl |PADOFFSET po | |
183 | Lightweight and lvalue version of C<PAD_SV>. | |
184 | Get or set the value at offset C<po> in the current pad. | |
185 | Unlike C<PAD_SV>, does not print diagnostics with -DX. | |
186 | For internal use only. | |
187 | ||
188 | =for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po | |
189 | Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist | |
190 | ||
191 | =for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n | |
192 | Set the current pad to be pad C<n> in the padlist, saving | |
fd617465 DM |
193 | the previous current pad. NB currently this macro expands to a string too |
194 | long for some compilers, so it's best to replace it with | |
195 | ||
196 | SAVECOMPPAD(); | |
197 | PAD_SET_CUR_NOSAVE(padlist,n); | |
198 | ||
dd2155a4 | 199 | |
4e380990 DM |
200 | =for apidoc m|void|PAD_SET_CUR_NOSAVE |PADLIST padlist|I32 n |
201 | like PAD_SET_CUR, but without the save | |
202 | ||
dd2155a4 DM |
203 | =for apidoc m|void|PAD_SAVE_SETNULLPAD |
204 | Save the current pad then set it to null. | |
205 | ||
f3548bdc DM |
206 | =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad |
207 | Save the current pad to the local variable opad, then make the | |
208 | current pad equal to npad | |
209 | ||
210 | =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad | |
211 | Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL() | |
dd2155a4 DM |
212 | |
213 | =cut | |
214 | */ | |
215 | ||
216 | #ifdef DEBUGGING | |
217 | # define PAD_SV(po) pad_sv(po) | |
218 | # define PAD_SETSV(po,sv) pad_setsv(po,sv) | |
219 | #else | |
220 | # define PAD_SV(po) (PL_curpad[po]) | |
221 | # define PAD_SETSV(po,sv) PL_curpad[po] = (sv) | |
222 | #endif | |
223 | ||
224 | #define PAD_SVl(po) (PL_curpad[po]) | |
225 | ||
226 | #define PAD_BASE_SV(padlist, po) \ | |
227 | (AvARRAY(padlist)[1]) \ | |
a062e10d | 228 | ? AvARRAY(MUTABLE_AV((AvARRAY(padlist)[1])))[po] : NULL; |
7948fc08 | 229 | |
dd2155a4 | 230 | |
de5e01c2 JH |
231 | #define PAD_SET_CUR_NOSAVE(padlist,nth) \ |
232 | PL_comppad = (PAD*) (AvARRAY(padlist)[nth]); \ | |
f3548bdc DM |
233 | PL_curpad = AvARRAY(PL_comppad); \ |
234 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, \ | |
235 | "Pad 0x%"UVxf"[0x%"UVxf"] set_cur depth=%d\n", \ | |
de5e01c2 | 236 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth))); |
f3548bdc DM |
237 | |
238 | ||
de5e01c2 | 239 | #define PAD_SET_CUR(padlist,nth) \ |
4e380990 | 240 | SAVECOMPPAD(); \ |
de5e01c2 | 241 | PAD_SET_CUR_NOSAVE(padlist,nth); |
4e380990 DM |
242 | |
243 | ||
f3548bdc | 244 | #define PAD_SAVE_SETNULLPAD() SAVECOMPPAD(); \ |
4608196e | 245 | PL_comppad = NULL; PL_curpad = NULL; \ |
f3548bdc DM |
246 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n")); |
247 | ||
248 | #define PAD_SAVE_LOCAL(opad,npad) \ | |
249 | opad = PL_comppad; \ | |
250 | PL_comppad = (npad); \ | |
4608196e | 251 | PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \ |
f3548bdc DM |
252 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, \ |
253 | "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n", \ | |
254 | PTR2UV(PL_comppad), PTR2UV(PL_curpad))); | |
255 | ||
256 | #define PAD_RESTORE_LOCAL(opad) \ | |
8c63ea58 GG |
257 | assert(!opad || !SvIS_FREED(opad)); \ |
258 | PL_comppad = opad; \ | |
4608196e | 259 | PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \ |
f3548bdc DM |
260 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, \ |
261 | "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n", \ | |
262 | PTR2UV(PL_comppad), PTR2UV(PL_curpad))); | |
dd2155a4 DM |
263 | |
264 | ||
265 | /* | |
266 | =for apidoc m|void|CX_CURPAD_SAVE|struct context | |
267 | Save the current pad in the given context block structure. | |
268 | ||
f3548bdc | 269 | =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po |
dd2155a4 DM |
270 | Access the SV at offset po in the saved current pad in the given |
271 | context block structure (can be used as an lvalue). | |
272 | ||
273 | =cut | |
274 | */ | |
275 | ||
f3548bdc | 276 | #define CX_CURPAD_SAVE(block) (block).oldcomppad = PL_comppad |
a062e10d | 277 | #define CX_CURPAD_SV(block,po) (AvARRAY(MUTABLE_AV(((block).oldcomppad)))[po]) |
dd2155a4 DM |
278 | |
279 | ||
280 | /* | |
281 | =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po | |
282 | Return the flags for the current compiling pad name | |
283 | at offset C<po>. Assumes a valid slot entry. | |
284 | ||
285 | =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po | |
286 | Return the name of the current compiling pad name | |
287 | at offset C<po>. Assumes a valid slot entry. | |
288 | ||
289 | =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po | |
290 | Return the type (stash) of the current compiling pad name at offset | |
291 | C<po>. Must be a valid name. Returns null if not typed. | |
292 | ||
293 | =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po | |
294 | Return the stash associated with an C<our> variable. | |
295 | Assumes the slot entry is a valid C<our> lexical. | |
296 | ||
297 | =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po | |
298 | The generation number of the name at offset C<po> in the current | |
931b58fb | 299 | compiling pad (lvalue). Note that C<SvUVX> is hijacked for this purpose. |
dd2155a4 | 300 | |
b162af07 SP |
301 | =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen |
302 | Sets the generation number of the name at offset C<po> in the current | |
931b58fb | 303 | ling pad (lvalue) to C<gen>. Note that C<SvUV_set> is hijacked for this purpose. |
b162af07 | 304 | |
dd2155a4 | 305 | =cut |
b162af07 | 306 | |
dd2155a4 DM |
307 | */ |
308 | ||
f8503592 NC |
309 | #define PAD_COMPNAME_SV(po) (*av_fetch(PL_comppad_name, (po), FALSE)) |
310 | #define PAD_COMPNAME_FLAGS(po) SvFLAGS(PAD_COMPNAME_SV(po)) | |
1979170b | 311 | #define PAD_COMPNAME_FLAGS_isOUR(po) SvPAD_OUR(PAD_COMPNAME_SV(po)) |
f8503592 | 312 | #define PAD_COMPNAME_PV(po) SvPV_nolen(PAD_COMPNAME_SV(po)) |
dd2155a4 | 313 | |
b21dc031 | 314 | #define PAD_COMPNAME_TYPE(po) pad_compname_type(po) |
dd2155a4 DM |
315 | |
316 | #define PAD_COMPNAME_OURSTASH(po) \ | |
73d95100 | 317 | (SvOURSTASH(PAD_COMPNAME_SV(po))) |
dd2155a4 | 318 | |
931b58fb | 319 | #define PAD_COMPNAME_GEN(po) ((STRLEN)SvUVX(AvARRAY(PL_comppad_name)[po])) |
dd2155a4 | 320 | |
931b58fb | 321 | #define PAD_COMPNAME_GEN_set(po, gen) SvUV_set(AvARRAY(PL_comppad_name)[po], (UV)(gen)) |
dd2155a4 DM |
322 | |
323 | ||
324 | /* | |
d77cdebf | 325 | =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl|CLONE_PARAMS* param |
dd2155a4 DM |
326 | Clone the state variables associated with running and compiling pads. |
327 | ||
328 | =cut | |
329 | */ | |
330 | ||
c5ab0850 DM |
331 | /* NB - we set PL_comppad to null unless it points at a value that |
332 | * has already been dup'ed, ie it points to part of an active padlist. | |
333 | * Otherwise PL_comppad ends up being a leaked scalar in code like | |
334 | * the following: | |
335 | * threads->create(sub { threads->create(sub {...} ) } ); | |
336 | * where the second thread dups the outer sub's comppad but not the | |
337 | * sub's CV or padlist. */ | |
f3548bdc | 338 | |
dd2155a4 | 339 | #define PAD_CLONE_VARS(proto_perl, param) \ |
a062e10d | 340 | PL_comppad = MUTABLE_AV(ptr_table_fetch(PL_ptr_table, proto_perl->Icomppad)); \ |
4608196e | 341 | PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \ |
dd2155a4 DM |
342 | PL_comppad_name = av_dup(proto_perl->Icomppad_name, param); \ |
343 | PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \ | |
344 | PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \ | |
dd2155a4 DM |
345 | PL_min_intro_pending = proto_perl->Imin_intro_pending; \ |
346 | PL_max_intro_pending = proto_perl->Imax_intro_pending; \ | |
347 | PL_padix = proto_perl->Ipadix; \ | |
348 | PL_padix_floor = proto_perl->Ipadix_floor; \ | |
349 | PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \ | |
350 | PL_cop_seqmax = proto_perl->Icop_seqmax; | |
e9a8c099 MHM |
351 | |
352 | /* | |
cc76b5cc Z |
353 | =for apidoc Am|PADOFFSET|pad_add_name_pvs|const char *name|U32 flags|HV *typestash|HV *ourstash |
354 | ||
355 | Exactly like L</pad_add_name_pvn>, but takes a literal string instead | |
356 | of a string/length pair. | |
357 | ||
358 | =cut | |
359 | */ | |
360 | ||
361 | #define pad_add_name_pvs(name,flags,typestash,ourstash) \ | |
362 | Perl_pad_add_name_pvn(aTHX_ STR_WITH_LEN(name), flags, typestash, ourstash) | |
363 | ||
364 | /* | |
365 | =for apidoc Am|PADOFFSET|pad_findmy_pvs|const char *name|U32 flags | |
366 | ||
367 | Exactly like L</pad_findmy_pvn>, but takes a literal string instead | |
368 | of a string/length pair. | |
369 | ||
370 | =cut | |
371 | */ | |
372 | ||
373 | #define pad_findmy_pvs(name,flags) \ | |
374 | Perl_pad_findmy_pvn(aTHX_ STR_WITH_LEN(name), flags) | |
375 | ||
376 | /* | |
e9a8c099 MHM |
377 | * Local variables: |
378 | * c-indentation-style: bsd | |
379 | * c-basic-offset: 4 | |
380 | * indent-tabs-mode: t | |
381 | * End: | |
382 | * | |
383 | * ex: set ts=8 sts=4 sw=4 noet: | |
384 | */ |