3 * Copyright (C) 2002, 2003, 2005, by Larry Wall and others
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.
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.
16 /* a padlist is currently just an AV; but that might change,
17 * so hide the type. Ditto a pad. */
23 /* offsets within a pad */
26 typedef U32TYPE PADOFFSET;
29 typedef U64TYPE PADOFFSET;
32 #define NOT_IN_PAD ((PADOFFSET) -1)
34 /* B.xs needs these for the benefit of B::Deparse */
35 /* Low range end is exclusive (valid from the cop seq after this one) */
36 /* High range end is inclusive (valid up to this cop seq) */
38 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
39 # define COP_SEQ_RANGE_LOW(sv) \
40 (({ SV *const _svi = (SV *) (sv); \
41 assert(SvTYPE(_svi) == SVt_NV || SvTYPE(_svi) >= SVt_PVNV); \
42 assert(SvTYPE(_svi) != SVt_PVAV); \
43 assert(SvTYPE(_svi) != SVt_PVHV); \
44 assert(SvTYPE(_svi) != SVt_PVCV); \
45 assert(SvTYPE(_svi) != SVt_PVFM); \
46 assert(!isGV_with_GP(_svi)); \
47 ((XPVNV*) SvANY(_svi))->xnv_u.xpad_cop_seq.xlow; \
49 # define COP_SEQ_RANGE_HIGH(sv) \
50 (({ SV *const _svi = (SV *) (sv); \
51 assert(SvTYPE(_svi) == SVt_NV || SvTYPE(_svi) >= SVt_PVNV); \
52 assert(SvTYPE(_svi) != SVt_PVAV); \
53 assert(SvTYPE(_svi) != SVt_PVHV); \
54 assert(SvTYPE(_svi) != SVt_PVCV); \
55 assert(SvTYPE(_svi) != SVt_PVFM); \
56 assert(!isGV_with_GP(_svi)); \
57 ((XPVNV*) SvANY(_svi))->xnv_u.xpad_cop_seq.xhigh; \
59 # define PARENT_PAD_INDEX(sv) \
60 (({ SV *const _svi = (SV *) (sv); \
61 assert(SvTYPE(_svi) == SVt_NV || SvTYPE(_svi) >= SVt_PVNV); \
62 assert(SvTYPE(_svi) != SVt_PVAV); \
63 assert(SvTYPE(_svi) != SVt_PVHV); \
64 assert(SvTYPE(_svi) != SVt_PVCV); \
65 assert(SvTYPE(_svi) != SVt_PVFM); \
66 assert(!isGV_with_GP(_svi)); \
67 ((XPVNV*) SvANY(_svi))->xnv_u.xpad_cop_seq.xlow; \
69 # define PARENT_FAKELEX_FLAGS(sv) \
70 (({ SV *const _svi = (SV *) (sv); \
71 assert(SvTYPE(_svi) == SVt_NV || SvTYPE(_svi) >= SVt_PVNV); \
72 assert(SvTYPE(_svi) != SVt_PVAV); \
73 assert(SvTYPE(_svi) != SVt_PVHV); \
74 assert(SvTYPE(_svi) != SVt_PVCV); \
75 assert(SvTYPE(_svi) != SVt_PVFM); \
76 assert(!isGV_with_GP(_svi)); \
77 ((XPVNV*) SvANY(_svi))->xnv_u.xpad_cop_seq.xhigh; \
80 # define COP_SEQ_RANGE_LOW(sv) \
81 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow))
82 # define COP_SEQ_RANGE_HIGH(sv) \
83 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh))
86 # define PARENT_PAD_INDEX(sv) \
87 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow))
88 # define PARENT_FAKELEX_FLAGS(sv) \
89 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh))
92 /* Flags set in the SvIVX field of FAKE namesvs */
94 #define PAD_FAKELEX_ANON 1 /* the lex is declared in an ANON, or ... */
95 #define PAD_FAKELEX_MULTI 2 /* the lex can be instantiated multiple times */
97 /* flags for the pad_new() function */
99 #define padnew_CLONE 1 /* this pad is for a cloned CV */
100 #define padnew_SAVE 2 /* save old globals */
101 #define padnew_SAVESUB 4 /* also save extra stuff for start of sub */
103 /* values for the pad_tidy() function */
106 padtidy_SUB, /* tidy up a pad for a sub, */
107 padtidy_SUBCLONE, /* a cloned sub, */
108 padtidy_FORMAT /* or a format */
111 /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
112 * whether PL_comppad and PL_curpad are consistent and whether they have
116 # define pad_peg(label)
120 # define ASSERT_CURPAD_LEGAL(label) \
122 if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0)) \
123 Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
124 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
127 # define ASSERT_CURPAD_ACTIVE(label) \
129 if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad)) \
130 Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
131 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
133 # define ASSERT_CURPAD_LEGAL(label)
134 # define ASSERT_CURPAD_ACTIVE(label)
139 /* Note: the following three macros are actually defined in scope.h, but
140 * they are documented here for completeness, since they directly or
141 * indirectly affect pads.
143 =for apidoc m|void|SAVEPADSV |PADOFFSET po
144 Save a pad slot (used to restore after an iteration)
146 XXX DAPM it would make more sense to make the arg a PADOFFSET
147 =for apidoc m|void|SAVECLEARSV |SV **svp
148 Clear the pointed to pad value on scope exit. (i.e. the runtime action of 'my')
150 =for apidoc m|void|SAVECOMPPAD
151 save PL_comppad and PL_curpad
157 =for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv
158 Set the slot at offset C<po> in the current pad to C<sv>
160 =for apidoc m|void|PAD_SV |PADOFFSET po
161 Get the value at offset C<po> in the current pad
163 =for apidoc m|SV *|PAD_SVl |PADOFFSET po
164 Lightweight and lvalue version of C<PAD_SV>.
165 Get or set the value at offset C<po> in the current pad.
166 Unlike C<PAD_SV>, does not print diagnostics with -DX.
167 For internal use only.
169 =for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po
170 Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
172 =for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n
173 Set the current pad to be pad C<n> in the padlist, saving
174 the previous current pad. NB currently this macro expands to a string too
175 long for some compilers, so it's best to replace it with
178 PAD_SET_CUR_NOSAVE(padlist,n);
181 =for apidoc m|void|PAD_SET_CUR_NOSAVE |PADLIST padlist|I32 n
182 like PAD_SET_CUR, but without the save
184 =for apidoc m|void|PAD_SAVE_SETNULLPAD
185 Save the current pad then set it to null.
187 =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad
188 Save the current pad to the local variable opad, then make the
189 current pad equal to npad
191 =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad
192 Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL()
198 # define PAD_SV(po) pad_sv(po)
199 # define PAD_SETSV(po,sv) pad_setsv(po,sv)
201 # define PAD_SV(po) (PL_curpad[po])
202 # define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
205 #define PAD_SVl(po) (PL_curpad[po])
207 #define PAD_BASE_SV(padlist, po) \
208 (AvARRAY(padlist)[1]) \
209 ? AvARRAY((AV*)(AvARRAY(padlist)[1]))[po] : NULL;
212 #define PAD_SET_CUR_NOSAVE(padlist,nth) \
213 PL_comppad = (PAD*) (AvARRAY(padlist)[nth]); \
214 PL_curpad = AvARRAY(PL_comppad); \
215 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
216 "Pad 0x%"UVxf"[0x%"UVxf"] set_cur depth=%d\n", \
217 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth)));
220 #define PAD_SET_CUR(padlist,nth) \
222 PAD_SET_CUR_NOSAVE(padlist,nth);
225 #define PAD_SAVE_SETNULLPAD() SAVECOMPPAD(); \
226 PL_comppad = NULL; PL_curpad = NULL; \
227 DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n"));
229 #define PAD_SAVE_LOCAL(opad,npad) \
231 PL_comppad = (npad); \
232 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
233 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
234 "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n", \
235 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
237 #define PAD_RESTORE_LOCAL(opad) \
239 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
240 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
241 "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n", \
242 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
246 =for apidoc m|void|CX_CURPAD_SAVE|struct context
247 Save the current pad in the given context block structure.
249 =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po
250 Access the SV at offset po in the saved current pad in the given
251 context block structure (can be used as an lvalue).
256 #define CX_CURPAD_SAVE(block) (block).oldcomppad = PL_comppad
257 #define CX_CURPAD_SV(block,po) (AvARRAY((AV*)((block).oldcomppad))[po])
261 =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
262 Return the flags for the current compiling pad name
263 at offset C<po>. Assumes a valid slot entry.
265 =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
266 Return the name of the current compiling pad name
267 at offset C<po>. Assumes a valid slot entry.
269 =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
270 Return the type (stash) of the current compiling pad name at offset
271 C<po>. Must be a valid name. Returns null if not typed.
273 =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
274 Return the stash associated with an C<our> variable.
275 Assumes the slot entry is a valid C<our> lexical.
277 =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
278 The generation number of the name at offset C<po> in the current
279 compiling pad (lvalue). Note that C<SvCUR> is hijacked for this purpose.
281 =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen
282 Sets the generation number of the name at offset C<po> in the current
283 ling pad (lvalue) to C<gen>. Note that C<SvCUR_set> is hijacked for this purpose.
289 #define PAD_COMPNAME_FLAGS(po) SvFLAGS(*av_fetch(PL_comppad_name, (po), FALSE))
290 #define PAD_COMPNAME_FLAGS_isOUR(po) \
291 ((PAD_COMPNAME_FLAGS(po) & (SVpad_NAME|SVpad_OUR)) == (SVpad_NAME|SVpad_OUR))
292 #define PAD_COMPNAME_PV(po) SvPV_nolen(*av_fetch(PL_comppad_name, (po), FALSE))
294 #define PAD_COMPNAME_TYPE(po) pad_compname_type(po)
296 #define PAD_COMPNAME_OURSTASH(po) \
297 (OURSTASH(*av_fetch(PL_comppad_name, (po), FALSE)))
299 #define PAD_COMPNAME_GEN(po) SvCUR(AvARRAY(PL_comppad_name)[po])
301 #define PAD_COMPNAME_GEN_set(po, gen) SvCUR_set(AvARRAY(PL_comppad_name)[po], gen)
305 =for apidoc m|void|PAD_DUP|PADLIST dstpad|PADLIST srcpad|CLONE_PARAMS* param
308 =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl \
310 Clone the state variables associated with running and compiling pads.
316 #define PAD_DUP(dstpad, srcpad, param) \
317 if ((srcpad) && !AvREAL(srcpad)) { \
318 /* XXX padlists are real, but pretend to be not */ \
320 (dstpad) = av_dup_inc((srcpad), param); \
321 AvREAL_off(srcpad); \
322 AvREAL_off(dstpad); \
325 (dstpad) = av_dup_inc((srcpad), param);
327 /* NB - we set PL_comppad to null unless it points at a value that
328 * has already been dup'ed, ie it points to part of an active padlist.
329 * Otherwise PL_comppad ends up being a leaked scalar in code like
331 * threads->create(sub { threads->create(sub {...} ) } );
332 * where the second thread dups the outer sub's comppad but not the
333 * sub's CV or padlist. */
335 #define PAD_CLONE_VARS(proto_perl, param) \
336 PL_comppad = (AV *) ptr_table_fetch(PL_ptr_table, proto_perl->Icomppad); \
337 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
338 PL_comppad_name = av_dup(proto_perl->Icomppad_name, param); \
339 PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \
340 PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \
341 PL_min_intro_pending = proto_perl->Imin_intro_pending; \
342 PL_max_intro_pending = proto_perl->Imax_intro_pending; \
343 PL_padix = proto_perl->Ipadix; \
344 PL_padix_floor = proto_perl->Ipadix_floor; \
345 PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \
346 PL_cop_seqmax = proto_perl->Icop_seqmax;