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