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)
35 /* flags for the pad_new() function */
37 #define padnew_CLONE 1 /* this pad is for a cloned CV */
38 #define padnew_SAVE 2 /* save old globals */
39 #define padnew_SAVESUB 4 /* also save extra stuff for start of sub */
41 /* values for the pad_tidy() function */
44 padtidy_SUB, /* tidy up a pad for a sub, */
45 padtidy_SUBCLONE, /* a cloned sub, */
46 padtidy_FORMAT /* or a format */
49 /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
50 * whether PL_comppad and PL_curpad are consistent and whether they have
54 # define pad_peg(label)
58 # define ASSERT_CURPAD_LEGAL(label) \
60 if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0)) \
61 Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
62 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
65 # define ASSERT_CURPAD_ACTIVE(label) \
67 if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad)) \
68 Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
69 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
71 # define ASSERT_CURPAD_LEGAL(label)
72 # define ASSERT_CURPAD_ACTIVE(label)
77 /* Note: the following three macros are actually defined in scope.h, but
78 * they are documented here for completeness, since they directly or
79 * indirectly affect pads.
81 =for apidoc m|void|SAVEPADSV |PADOFFSET po
82 Save a pad slot (used to restore after an iteration)
84 XXX DAPM it would make more sense to make the arg a PADOFFSET
85 =for apidoc m|void|SAVECLEARSV |SV **svp
86 Clear the pointed to pad value on scope exit. (i.e. the runtime action of 'my')
88 =for apidoc m|void|SAVECOMPPAD
89 save PL_comppad and PL_curpad
95 =for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv
96 Set the slot at offset C<po> in the current pad to C<sv>
98 =for apidoc m|void|PAD_SV |PADOFFSET po
99 Get the value at offset C<po> in the current pad
101 =for apidoc m|SV *|PAD_SVl |PADOFFSET po
102 Lightweight and lvalue version of C<PAD_SV>.
103 Get or set the value at offset C<po> in the current pad.
104 Unlike C<PAD_SV>, does not print diagnostics with -DX.
105 For internal use only.
107 =for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po
108 Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
110 =for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n
111 Set the current pad to be pad C<n> in the padlist, saving
112 the previous current pad. NB currently this macro expands to a string too
113 long for some compilers, so it's best to replace it with
116 PAD_SET_CUR_NOSAVE(padlist,n);
119 =for apidoc m|void|PAD_SET_CUR_NOSAVE |PADLIST padlist|I32 n
120 like PAD_SET_CUR, but without the save
122 =for apidoc m|void|PAD_SAVE_SETNULLPAD
123 Save the current pad then set it to null.
125 =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad
126 Save the current pad to the local variable opad, then make the
127 current pad equal to npad
129 =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad
130 Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL()
136 # define PAD_SV(po) pad_sv(po)
137 # define PAD_SETSV(po,sv) pad_setsv(po,sv)
139 # define PAD_SV(po) (PL_curpad[po])
140 # define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
143 #define PAD_SVl(po) (PL_curpad[po])
145 #define PAD_BASE_SV(padlist, po) \
146 (AvARRAY(padlist)[1]) \
147 ? AvARRAY((AV*)(AvARRAY(padlist)[1]))[po] : NULL;
150 #define PAD_SET_CUR_NOSAVE(padlist,nth) \
151 PL_comppad = (PAD*) (AvARRAY(padlist)[nth]); \
152 PL_curpad = AvARRAY(PL_comppad); \
153 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
154 "Pad 0x%"UVxf"[0x%"UVxf"] set_cur depth=%d\n", \
155 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth)));
158 #define PAD_SET_CUR(padlist,nth) \
160 PAD_SET_CUR_NOSAVE(padlist,nth);
163 #define PAD_SAVE_SETNULLPAD() SAVECOMPPAD(); \
164 PL_comppad = NULL; PL_curpad = NULL; \
165 DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n"));
167 #define PAD_SAVE_LOCAL(opad,npad) \
169 PL_comppad = (npad); \
170 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
171 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
172 "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n", \
173 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
175 #define PAD_RESTORE_LOCAL(opad) \
177 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
178 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
179 "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n", \
180 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
184 =for apidoc m|void|CX_CURPAD_SAVE|struct context
185 Save the current pad in the given context block structure.
187 =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po
188 Access the SV at offset po in the saved current pad in the given
189 context block structure (can be used as an lvalue).
194 #define CX_CURPAD_SAVE(block) (block).oldcomppad = PL_comppad
195 #define CX_CURPAD_SV(block,po) (AvARRAY((AV*)((block).oldcomppad))[po])
199 =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
200 Return the flags for the current compiling pad name
201 at offset C<po>. Assumes a valid slot entry.
203 =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
204 Return the name of the current compiling pad name
205 at offset C<po>. Assumes a valid slot entry.
207 =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
208 Return the type (stash) of the current compiling pad name at offset
209 C<po>. Must be a valid name. Returns null if not typed.
211 =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
212 Return the stash associated with an C<our> variable.
213 Assumes the slot entry is a valid C<our> lexical.
215 =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
216 The generation number of the name at offset C<po> in the current
217 compiling pad (lvalue). Note that C<SvCUR> is hijacked for this purpose.
219 =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen
220 Sets the generation number of the name at offset C<po> in the current
221 ling pad (lvalue) to C<gen>. Note that C<SvCUR_set> is hijacked for this purpose.
227 #define PAD_COMPNAME_FLAGS(po) SvFLAGS(*av_fetch(PL_comppad_name, (po), FALSE))
228 #define PAD_COMPNAME_FLAGS_isOUR(po) \
229 ((PAD_COMPNAME_FLAGS(po) & (SVpad_NAME|SVpad_OUR)) == (SVpad_NAME|SVpad_OUR))
230 #define PAD_COMPNAME_PV(po) SvPV_nolen(*av_fetch(PL_comppad_name, (po), FALSE))
232 #define PAD_COMPNAME_TYPE(po) pad_compname_type(po)
234 #define PAD_COMPNAME_OURSTASH(po) \
235 (OURSTASH(*av_fetch(PL_comppad_name, (po), FALSE)))
237 #define PAD_COMPNAME_GEN(po) SvCUR(AvARRAY(PL_comppad_name)[po])
239 #define PAD_COMPNAME_GEN_set(po, gen) SvCUR_set(AvARRAY(PL_comppad_name)[po], gen)
243 =for apidoc m|void|PAD_DUP|PADLIST dstpad|PADLIST srcpad|CLONE_PARAMS* param
246 =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl \
248 Clone the state variables associated with running and compiling pads.
254 #define PAD_DUP(dstpad, srcpad, param) \
255 if ((srcpad) && !AvREAL(srcpad)) { \
256 /* XXX padlists are real, but pretend to be not */ \
258 (dstpad) = av_dup_inc((srcpad), param); \
259 AvREAL_off(srcpad); \
260 AvREAL_off(dstpad); \
263 (dstpad) = av_dup_inc((srcpad), param);
265 /* NB - we set PL_comppad to null unless it points at a value that
266 * has already been dup'ed, ie it points to part of an active padlist.
267 * Otherwise PL_comppad ends up being a leaked scalar in code like
269 * threads->create(sub { threads->create(sub {...} ) } );
270 * where the second thread dups the outer sub's comppad but not the
271 * sub's CV or padlist. */
273 #define PAD_CLONE_VARS(proto_perl, param) \
274 PL_comppad = (AV *) ptr_table_fetch(PL_ptr_table, proto_perl->Icomppad); \
275 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
276 PL_comppad_name = av_dup(proto_perl->Icomppad_name, param); \
277 PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \
278 PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \
279 PL_min_intro_pending = proto_perl->Imin_intro_pending; \
280 PL_max_intro_pending = proto_perl->Imax_intro_pending; \
281 PL_padix = proto_perl->Ipadix; \
282 PL_padix_floor = proto_perl->Ipadix_floor; \
283 PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \
284 PL_cop_seqmax = proto_perl->Icop_seqmax;