This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Tweaks to the debugger test by Richard Foley,
[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
35 /* flags for the pad_new() function */
36
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 */
40
41 /* values for the pad_tidy() function */
42
43 typedef enum {
44         padtidy_SUB,            /* tidy up a pad for a sub, */
45         padtidy_SUBCLONE,       /* a cloned sub, */
46         padtidy_FORMAT          /* or a format */
47 } padtidy_type;
48
49 /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
50  * whether PL_comppad and PL_curpad are consistent and whether they have
51  * active values */
52
53 #ifndef PERL_MAD
54 #  define pad_peg(label)
55 #endif
56
57 #ifdef DEBUGGING
58 #  define ASSERT_CURPAD_LEGAL(label) \
59     pad_peg(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));
63
64
65 #  define ASSERT_CURPAD_ACTIVE(label) \
66     pad_peg(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));
70 #else
71 #  define ASSERT_CURPAD_LEGAL(label)
72 #  define ASSERT_CURPAD_ACTIVE(label)
73 #endif
74
75
76
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.
80
81 =for apidoc m|void|SAVEPADSV    |PADOFFSET po
82 Save a pad slot (used to restore after an iteration)
83
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')
87
88 =for apidoc m|void|SAVECOMPPAD
89 save PL_comppad and PL_curpad
90
91
92
93
94
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>
97
98 =for apidoc m|void|PAD_SV       |PADOFFSET po
99 Get the value at offset C<po> in the current pad
100
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.
106
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
109
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
114
115     SAVECOMPPAD();
116     PAD_SET_CUR_NOSAVE(padlist,n);
117
118
119 =for apidoc m|void|PAD_SET_CUR_NOSAVE   |PADLIST padlist|I32 n
120 like PAD_SET_CUR, but without the save
121
122 =for apidoc m|void|PAD_SAVE_SETNULLPAD
123 Save the current pad then set it to null.
124
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
128
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()
131
132 =cut
133 */
134
135 #ifdef DEBUGGING
136 #  define PAD_SV(po)       pad_sv(po)
137 #  define PAD_SETSV(po,sv) pad_setsv(po,sv)
138 #else
139 #  define PAD_SV(po)       (PL_curpad[po])
140 #  define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
141 #endif
142
143 #define PAD_SVl(po)       (PL_curpad[po])
144
145 #define PAD_BASE_SV(padlist, po) \
146         (AvARRAY(padlist)[1])   \
147             ? AvARRAY((AV*)(AvARRAY(padlist)[1]))[po] : NULL;
148     
149
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)));
156
157
158 #define PAD_SET_CUR(padlist,nth) \
159         SAVECOMPPAD();                                          \
160         PAD_SET_CUR_NOSAVE(padlist,nth);
161
162
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"));
166
167 #define PAD_SAVE_LOCAL(opad,npad) \
168         opad = PL_comppad;                                      \
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)));
174
175 #define PAD_RESTORE_LOCAL(opad) \
176         PL_comppad = 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)));
181
182
183 /*
184 =for apidoc m|void|CX_CURPAD_SAVE|struct context
185 Save the current pad in the given context block structure.
186
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).
190
191 =cut
192 */
193
194 #define CX_CURPAD_SAVE(block)  (block).oldcomppad = PL_comppad
195 #define CX_CURPAD_SV(block,po) (AvARRAY((AV*)((block).oldcomppad))[po])
196
197
198 /*
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.
202
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.
206
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.
210
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.
214
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.
218
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.
222
223 =cut
224
225 */
226
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))
231
232 #define PAD_COMPNAME_TYPE(po) pad_compname_type(po)
233
234 #define PAD_COMPNAME_OURSTASH(po) \
235     (OURSTASH(*av_fetch(PL_comppad_name, (po), FALSE)))
236
237 #define PAD_COMPNAME_GEN(po) SvCUR(AvARRAY(PL_comppad_name)[po])
238
239 #define PAD_COMPNAME_GEN_set(po, gen) SvCUR_set(AvARRAY(PL_comppad_name)[po], gen)
240
241
242 /*
243 =for apidoc m|void|PAD_DUP|PADLIST dstpad|PADLIST srcpad|CLONE_PARAMS* param
244 Clone a padlist.
245
246 =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl \
247 |CLONE_PARAMS* param
248 Clone the state variables associated with running and compiling pads.
249
250 =cut
251 */
252
253
254 #define PAD_DUP(dstpad, srcpad, param)                          \
255     if ((srcpad) && !AvREAL(srcpad)) {                          \
256         /* XXX padlists are real, but pretend to be not */      \
257         AvREAL_on(srcpad);                                      \
258         (dstpad) = av_dup_inc((srcpad), param);                 \
259         AvREAL_off(srcpad);                                     \
260         AvREAL_off(dstpad);                                     \
261     }                                                           \
262     else                                                        \
263         (dstpad) = av_dup_inc((srcpad), param);                 
264
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
268  * the following:
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. */
272
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;