This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / pad.h
1 /*    pad.h
2  *
3  *    Copyright (C) 2002, 2003, 2005, by Larry Wall and others
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;
20 typedef AV PAD;
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)
33
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 #define COP_SEQ_RANGE_LOW(sv)                   U_32(SvNVX(sv))
37 /* High range end is inclusive (valid up to this cop seq) */
38 #define COP_SEQ_RANGE_HIGH(sv)                  U_32(SvUVX(sv))
39
40 #define PARENT_PAD_INDEX(sv)                    U_32(SvNVX(sv))
41 #define PARENT_FAKELEX_FLAGS(sv)                U_32(SvUVX(sv))
42
43 /* Flags set in the SvIVX field of FAKE namesvs */
44     
45 #define PAD_FAKELEX_ANON   1 /* the lex is declared in an ANON, or ... */
46 #define PAD_FAKELEX_MULTI  2 /* the lex can be instantiated multiple times */
47
48 /* flags for the pad_new() function */
49
50 #define padnew_CLONE    1       /* this pad is for a cloned CV */
51 #define padnew_SAVE     2       /* save old globals */
52 #define padnew_SAVESUB  4       /* also save extra stuff for start of sub */
53
54 /* values for the pad_tidy() function */
55
56 typedef enum {
57         padtidy_SUB,            /* tidy up a pad for a sub, */
58         padtidy_SUBCLONE,       /* a cloned sub, */
59         padtidy_FORMAT          /* or a format */
60 } padtidy_type;
61
62 /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
63  * whether PL_comppad and PL_curpad are consistent and whether they have
64  * active values */
65
66 #ifdef DEBUGGING
67 #  define ASSERT_CURPAD_LEGAL(label) \
68     if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0))  \
69         Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
70             label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
71
72
73 #  define ASSERT_CURPAD_ACTIVE(label) \
74     if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad))                \
75         Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
76             label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
77 #else
78 #  define ASSERT_CURPAD_LEGAL(label)
79 #  define ASSERT_CURPAD_ACTIVE(label)
80 #endif
81
82
83
84 /* Note: the following three macros are actually defined in scope.h, but
85  * they are documented here for completeness, since they directly or
86  * indirectly affect pads.
87
88 =for apidoc m|void|SAVEPADSV    |PADOFFSET po
89 Save a pad slot (used to restore after an iteration)
90
91 XXX DAPM it would make more sense to make the arg a PADOFFSET
92 =for apidoc m|void|SAVECLEARSV  |SV **svp
93 Clear the pointed to pad value on scope exit. (i.e. the runtime action of 'my')
94
95 =for apidoc m|void|SAVECOMPPAD
96 save PL_comppad and PL_curpad
97
98
99
100
101
102 =for apidoc m|SV *|PAD_SETSV    |PADOFFSET po|SV* sv
103 Set the slot at offset C<po> in the current pad to C<sv>
104
105 =for apidoc m|void|PAD_SV       |PADOFFSET po
106 Get the value at offset C<po> in the current pad
107
108 =for apidoc m|SV *|PAD_SVl      |PADOFFSET po
109 Lightweight and lvalue version of C<PAD_SV>.
110 Get or set the value at offset C<po> in the current pad.
111 Unlike C<PAD_SV>, does not print diagnostics with -DX.
112 For internal use only.
113
114 =for apidoc m|SV *|PAD_BASE_SV  |PADLIST padlist|PADOFFSET po
115 Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
116
117 =for apidoc m|void|PAD_SET_CUR  |PADLIST padlist|I32 n
118 Set the current pad to be pad C<n> in the padlist, saving
119 the previous current pad. NB currently this macro expands to a string too
120 long for some compilers, so it's best to replace it with
121
122     SAVECOMPPAD();
123     PAD_SET_CUR_NOSAVE(padlist,n);
124
125
126 =for apidoc m|void|PAD_SET_CUR_NOSAVE   |PADLIST padlist|I32 n
127 like PAD_SET_CUR, but without the save
128
129 =for apidoc m|void|PAD_SAVE_SETNULLPAD
130 Save the current pad then set it to null.
131
132 =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad
133 Save the current pad to the local variable opad, then make the
134 current pad equal to npad
135
136 =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad
137 Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL()
138
139 =cut
140 */
141
142 #ifdef DEBUGGING
143 #  define PAD_SV(po)       pad_sv(po)
144 #  define PAD_SETSV(po,sv) pad_setsv(po,sv)
145 #else
146 #  define PAD_SV(po)       (PL_curpad[po])
147 #  define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
148 #endif
149
150 #define PAD_SVl(po)       (PL_curpad[po])
151
152 #define PAD_BASE_SV(padlist, po) \
153         (AvARRAY(padlist)[1])   \
154             ? AvARRAY((AV*)(AvARRAY(padlist)[1]))[po] : NULL;
155     
156
157 #define PAD_SET_CUR_NOSAVE(padlist,nth) \
158         PL_comppad = (PAD*) (AvARRAY(padlist)[nth]);            \
159         PL_curpad = AvARRAY(PL_comppad);                        \
160         DEBUG_Xv(PerlIO_printf(Perl_debug_log,                  \
161               "Pad 0x%"UVxf"[0x%"UVxf"] set_cur    depth=%d\n", \
162               PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth)));
163
164
165 #define PAD_SET_CUR(padlist,nth) \
166         SAVECOMPPAD();                                          \
167         PAD_SET_CUR_NOSAVE(padlist,nth);
168
169
170 #define PAD_SAVE_SETNULLPAD()   SAVECOMPPAD(); \
171         PL_comppad = NULL; PL_curpad = NULL;    \
172         DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n"));
173
174 #define PAD_SAVE_LOCAL(opad,npad) \
175         opad = PL_comppad;                                      \
176         PL_comppad = (npad);                                    \
177         PL_curpad =  PL_comppad ? AvARRAY(PL_comppad) : NULL;   \
178         DEBUG_Xv(PerlIO_printf(Perl_debug_log,                  \
179               "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n",          \
180               PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
181
182 #define PAD_RESTORE_LOCAL(opad) \
183         PL_comppad = opad;                                      \
184         PL_curpad =  PL_comppad ? AvARRAY(PL_comppad) : NULL;   \
185         DEBUG_Xv(PerlIO_printf(Perl_debug_log,                  \
186               "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n",       \
187               PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
188
189
190 /*
191 =for apidoc m|void|CX_CURPAD_SAVE|struct context
192 Save the current pad in the given context block structure.
193
194 =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po
195 Access the SV at offset po in the saved current pad in the given
196 context block structure (can be used as an lvalue).
197
198 =cut
199 */
200
201 #define CX_CURPAD_SAVE(block)  (block).oldcomppad = PL_comppad
202 #define CX_CURPAD_SV(block,po) (AvARRAY((AV*)((block).oldcomppad))[po])
203
204
205 /*
206 =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
207 Return the flags for the current compiling pad name
208 at offset C<po>. Assumes a valid slot entry.
209
210 =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
211 Return the name of the current compiling pad name
212 at offset C<po>. Assumes a valid slot entry.
213
214 =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
215 Return the type (stash) of the current compiling pad name at offset
216 C<po>. Must be a valid name. Returns null if not typed.
217
218 =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
219 Return the stash associated with an C<our> variable.
220 Assumes the slot entry is a valid C<our> lexical.
221
222 =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
223 The generation number of the name at offset C<po> in the current
224 compiling pad (lvalue). Note that C<SvCUR> is hijacked for this purpose.
225
226 =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen
227 Sets the generation number of the name at offset C<po> in the current
228 ling pad (lvalue) to C<gen>.  Note that C<SvCUR_set> is hijacked for this purpose.
229
230 =cut
231
232 */
233
234 #define PAD_COMPNAME_SV(po) (*av_fetch(PL_comppad_name, (po), FALSE))
235 #define PAD_COMPNAME_FLAGS(po) SvFLAGS(PAD_COMPNAME_SV(po))
236 #define PAD_COMPNAME_FLAGS_isOUR(po) (PAD_COMPNAME_FLAGS(po) & SVpad_OUR)
237 #define PAD_COMPNAME_PV(po) SvPV_nolen(PAD_COMPNAME_SV(po))
238
239 #define PAD_COMPNAME_TYPE(po) pad_compname_type(po)
240
241 #define PAD_COMPNAME_OURSTASH(po) \
242     (SvOURSTASH(PAD_COMPNAME_SV(po)))
243
244 #define PAD_COMPNAME_GEN(po) SvCUR(AvARRAY(PL_comppad_name)[po])
245
246 #define PAD_COMPNAME_GEN_set(po, gen) SvCUR_set(AvARRAY(PL_comppad_name)[po], gen)
247
248
249 /*
250 =for apidoc m|void|PAD_DUP|PADLIST dstpad|PADLIST srcpad|CLONE_PARAMS* param
251 Clone a padlist.
252
253 =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl \
254 |CLONE_PARAMS* param
255 Clone the state variables associated with running and compiling pads.
256
257 =cut
258 */
259
260
261 #define PAD_DUP(dstpad, srcpad, param)                          \
262     if ((srcpad) && !AvREAL(srcpad)) {                          \
263         /* XXX padlists are real, but pretend to be not */      \
264         AvREAL_on(srcpad);                                      \
265         (dstpad) = av_dup_inc((srcpad), param);                 \
266         AvREAL_off(srcpad);                                     \
267         AvREAL_off(dstpad);                                     \
268     }                                                           \
269     else                                                        \
270         (dstpad) = av_dup_inc((srcpad), param);                 
271
272 /* NB - we set PL_comppad to null unless it points at a value that
273  * has already been dup'ed, ie it points to part of an active padlist.
274  * Otherwise PL_comppad ends up being a leaked scalar in code like
275  * the following:
276  *     threads->create(sub { threads->create(sub {...} ) } );
277  * where the second thread dups the outer sub's comppad but not the
278  * sub's CV or padlist. */
279
280 #define PAD_CLONE_VARS(proto_perl, param)                               \
281     PL_comppad = ptr_table_fetch(PL_ptr_table, proto_perl->Tcomppad);   \
282     PL_curpad = PL_comppad ?  AvARRAY(PL_comppad) : NULL;               \
283     PL_comppad_name             = av_dup(proto_perl->Icomppad_name, param); \
284     PL_comppad_name_fill        = proto_perl->Icomppad_name_fill;       \
285     PL_comppad_name_floor       = proto_perl->Icomppad_name_floor;      \
286     PL_min_intro_pending        = proto_perl->Imin_intro_pending;       \
287     PL_max_intro_pending        = proto_perl->Imax_intro_pending;       \
288     PL_padix                    = proto_perl->Ipadix;                   \
289     PL_padix_floor              = proto_perl->Ipadix_floor;             \
290     PL_pad_reset_pending        = proto_perl->Ipad_reset_pending;       \
291     PL_cop_seqmax               = proto_perl->Icop_seqmax;