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