This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Trim second block (nolinenumbers) to avert crashes on Win32.
[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 /* offsets within a pad */
20
21 #if PTRSIZE == 4
22 typedef U32TYPE PADOFFSET;
23 #else
24 #   if PTRSIZE == 8
25 typedef U64TYPE PADOFFSET;
26 #   endif
27 #endif
28 #define NOT_IN_PAD ((PADOFFSET) -1)
29
30
31 struct padlist {
32     SSize_t     xpadl_max;      /* max index for which array has space */
33     PAD **      xpadl_alloc;    /* pointer to beginning of array of AVs */
34     PADNAMELIST*xpadl_outid;    /* Padnamelist of outer pad; used as ID */
35 };
36
37
38 /* a value that PL_cop_seqmax is guaranteed never to be,
39  * flagging that a lexical is being introduced, or has not yet left scope
40  */
41 #define PERL_PADSEQ_INTRO  U32_MAX
42
43
44 /* B.xs needs these for the benefit of B::Deparse */
45 /* Low range end is exclusive (valid from the cop seq after this one) */
46 /* High range end is inclusive (valid up to this cop seq) */
47
48 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
49 #  define COP_SEQ_RANGE_LOW(sv)                                         \
50         (({ const SV *const _sv_cop_seq_range_low = (const SV *) (sv);  \
51           assert(SvTYPE(_sv_cop_seq_range_low) == SVt_NV                \
52                  || SvTYPE(_sv_cop_seq_range_low) >= SVt_PVNV);         \
53           assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVAV);            \
54           assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVHV);            \
55           assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVCV);            \
56           assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVFM);            \
57           assert(!isGV_with_GP(_sv_cop_seq_range_low));                 \
58           ((XPVNV*) MUTABLE_PTR(SvANY(_sv_cop_seq_range_low)))->xnv_u.xpad_cop_seq.xlow; \
59          }))
60 #  define COP_SEQ_RANGE_HIGH(sv)                                        \
61         (({ const SV *const _sv_cop_seq_range_high = (const SV *) (sv); \
62           assert(SvTYPE(_sv_cop_seq_range_high) == SVt_NV               \
63                  || SvTYPE(_sv_cop_seq_range_high) >= SVt_PVNV);        \
64           assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVAV);           \
65           assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVHV);           \
66           assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVCV);           \
67           assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVFM);           \
68           assert(!isGV_with_GP(_sv_cop_seq_range_high));                \
69           ((XPVNV*) MUTABLE_PTR(SvANY(_sv_cop_seq_range_high)))->xnv_u.xpad_cop_seq.xhigh; \
70          }))
71 #  define PARENT_PAD_INDEX(sv)                                          \
72         (({ const SV *const _sv_parent_pad_index = (const SV *) (sv);   \
73           assert(SvTYPE(_sv_parent_pad_index) == SVt_NV                 \
74                  || SvTYPE(_sv_parent_pad_index) >= SVt_PVNV);          \
75           assert(SvTYPE(_sv_parent_pad_index) != SVt_PVAV);             \
76           assert(SvTYPE(_sv_parent_pad_index) != SVt_PVHV);             \
77           assert(SvTYPE(_sv_parent_pad_index) != SVt_PVCV);             \
78           assert(SvTYPE(_sv_parent_pad_index) != SVt_PVFM);             \
79           assert(!isGV_with_GP(_sv_parent_pad_index));                  \
80           ((XPVNV*) MUTABLE_PTR(SvANY(_sv_parent_pad_index)))->xnv_u.xpad_cop_seq.xlow; \
81          }))
82 #  define PARENT_FAKELEX_FLAGS(sv)                                      \
83         (({ const SV *const _sv_parent_fakelex_flags = (const SV *) (sv); \
84           assert(SvTYPE(_sv_parent_fakelex_flags) == SVt_NV             \
85                  || SvTYPE(_sv_parent_fakelex_flags) >= SVt_PVNV);      \
86           assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVAV);         \
87           assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVHV);         \
88           assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVCV);         \
89           assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVFM);         \
90           assert(!isGV_with_GP(_sv_parent_fakelex_flags));              \
91           ((XPVNV*) MUTABLE_PTR(SvANY(_sv_parent_fakelex_flags)))->xnv_u.xpad_cop_seq.xhigh; \
92          }))
93 #else
94 #  define COP_SEQ_RANGE_LOW(sv)         \
95         (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow))
96 #  define COP_SEQ_RANGE_HIGH(sv)        \
97         (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh))
98
99
100 #  define PARENT_PAD_INDEX(sv)          \
101         (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow))
102 #  define PARENT_FAKELEX_FLAGS(sv)      \
103         (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh))
104 #endif
105
106 /* Flags set in the SvIVX field of FAKE namesvs */
107
108 #define PAD_FAKELEX_ANON   1 /* the lex is declared in an ANON, or ... */
109 #define PAD_FAKELEX_MULTI  2 /* the lex can be instantiated multiple times */
110
111 /* flags for the pad_new() function */
112
113 #define padnew_CLONE    1       /* this pad is for a cloned CV */
114 #define padnew_SAVE     2       /* save old globals */
115 #define padnew_SAVESUB  4       /* also save extra stuff for start of sub */
116
117 /* values for the pad_tidy() function */
118
119 typedef enum {
120         padtidy_SUB,            /* tidy up a pad for a sub, */
121         padtidy_SUBCLONE,       /* a cloned sub, */
122         padtidy_FORMAT          /* or a format */
123 } padtidy_type;
124
125 /* flags for pad_add_name_pvn. */
126
127 #define padadd_OUR              0x01       /* our declaration. */
128 #define padadd_STATE            0x02       /* state declaration. */
129 #define padadd_NO_DUP_CHECK     0x04       /* skip warning on dups. */
130 #define padadd_STALEOK          0x08       /* allow stale lexical in active
131                                             * sub, but only one level up */
132 #define padadd_UTF8_NAME        SVf_UTF8   /* name is UTF-8 encoded. */
133
134 /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
135  * whether PL_comppad and PL_curpad are consistent and whether they have
136  * active values */
137
138 #ifndef PERL_MAD
139 #  define pad_peg(label)
140 #endif
141
142 #ifdef DEBUGGING
143 #  define ASSERT_CURPAD_LEGAL(label) \
144     pad_peg(label); \
145     if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0))  \
146         Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
147             label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
148
149
150 #  define ASSERT_CURPAD_ACTIVE(label) \
151     pad_peg(label); \
152     if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad))                \
153         Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
154             label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
155 #else
156 #  define ASSERT_CURPAD_LEGAL(label)
157 #  define ASSERT_CURPAD_ACTIVE(label)
158 #endif
159
160
161
162 /* Note: the following three macros are actually defined in scope.h, but
163  * they are documented here for completeness, since they directly or
164  * indirectly affect pads.
165
166 =for apidoc m|void|SAVEPADSV    |PADOFFSET po
167 Save a pad slot (used to restore after an iteration)
168
169 XXX DAPM it would make more sense to make the arg a PADOFFSET
170 =for apidoc m|void|SAVECLEARSV  |SV **svp
171 Clear the pointed to pad value on scope exit. (i.e. the runtime action of 'my')
172
173 =for apidoc m|void|SAVECOMPPAD
174 save PL_comppad and PL_curpad
175
176
177 =for apidoc Amx|PAD **|PadlistARRAY|PADLIST padlist
178 The C array of a padlist, containing the pads.  Only subscript it with
179 numbers >= 1, as the 0th entry is not guaranteed to remain usable.
180
181 =for apidoc Amx|SSize_t|PadlistMAX|PADLIST padlist
182 The index of the last pad in the padlist.
183
184 =for apidoc Amx|PADNAMELIST *|PadlistNAMES|PADLIST padlist
185 The names associated with pad entries.
186
187 =for apidoc Amx|PADNAME **|PadlistNAMESARRAY|PADLIST padlist
188 The C array of pad names.
189
190 =for apidoc Amx|SSize_t|PadlistNAMESMAX|PADLIST padlist
191 The index of the last pad name.
192
193 =for apidoc Amx|U32|PadlistREFCNT|PADLIST padlist
194 The reference count of the padlist.  Currently this is always 1.
195
196 =for apidoc Amx|PADNAME **|PadnamelistARRAY|PADNAMELIST pnl
197 The C array of pad names.
198
199 =for apidoc Amx|SSize_t|PadnamelistMAX|PADNAMELIST pnl
200 The index of the last pad name.
201
202 =for apidoc Amx|SV **|PadARRAY|PAD pad
203 The C array of pad entries.
204
205 =for apidoc Amx|SSize_t|PadMAX|PAD pad
206 The index of the last pad entry.
207
208 =for apidoc Amx|char *|PadnamePV|PADNAME pn     
209 The name stored in the pad name struct.  This returns NULL for a target or
210 GV slot.
211
212 =for apidoc Amx|STRLEN|PadnameLEN|PADNAME pn    
213 The length of the name.
214
215 =for apidoc Amx|bool|PadnameUTF8|PADNAME pn
216 Whether PadnamePV is in UTF8.
217
218 =for apidoc Amx|SV *|PadnameSV|PADNAME pn
219 Returns the pad name as an SV.  This is currently just C<pn>.  It will
220 begin returning a new mortal SV if pad names ever stop being SVs.
221
222 =for apidoc m|bool|PadnameIsOUR|PADNAME pn
223 Whether this is an "our" variable.
224
225 =for apidoc m|HV *|PadnameOURSTASH
226 The stash in which this "our" variable was declared.
227
228 =for apidoc m|bool|PadnameOUTER|PADNAME pn
229 Whether this entry belongs to an outer pad.
230
231 =for apidoc m|bool|PadnameIsSTATE|PADNAME pn
232 Whether this is a "state" variable.
233
234 =for apidoc m|HV *|PadnameTYPE|PADNAME pn
235 The stash associated with a typed lexical.  This returns the %Foo:: hash
236 for C<my Foo $bar>.
237
238
239 =for apidoc m|SV *|PAD_SETSV    |PADOFFSET po|SV* sv
240 Set the slot at offset C<po> in the current pad to C<sv>
241
242 =for apidoc m|void|PAD_SV       |PADOFFSET po
243 Get the value at offset C<po> in the current pad
244
245 =for apidoc m|SV *|PAD_SVl      |PADOFFSET po
246 Lightweight and lvalue version of C<PAD_SV>.
247 Get or set the value at offset C<po> in the current pad.
248 Unlike C<PAD_SV>, does not print diagnostics with -DX.
249 For internal use only.
250
251 =for apidoc m|SV *|PAD_BASE_SV  |PADLIST padlist|PADOFFSET po
252 Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
253
254 =for apidoc m|void|PAD_SET_CUR  |PADLIST padlist|I32 n
255 Set the current pad to be pad C<n> in the padlist, saving
256 the previous current pad. NB currently this macro expands to a string too
257 long for some compilers, so it's best to replace it with
258
259     SAVECOMPPAD();
260     PAD_SET_CUR_NOSAVE(padlist,n);
261
262
263 =for apidoc m|void|PAD_SET_CUR_NOSAVE   |PADLIST padlist|I32 n
264 like PAD_SET_CUR, but without the save
265
266 =for apidoc m|void|PAD_SAVE_SETNULLPAD
267 Save the current pad then set it to null.
268
269 =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad
270 Save the current pad to the local variable opad, then make the
271 current pad equal to npad
272
273 =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad
274 Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL()
275
276 =cut
277 */
278
279 #define PadlistARRAY(pl)        (pl)->xpadl_alloc
280 #define PadlistMAX(pl)          (pl)->xpadl_max
281 #define PadlistNAMES(pl)        (*PadlistARRAY(pl))
282 #define PadlistNAMESARRAY(pl)   PadnamelistARRAY(PadlistNAMES(pl))
283 #define PadlistNAMESMAX(pl)     PadnamelistMAX(PadlistNAMES(pl))
284 #define PadlistREFCNT(pl)       1       /* reserved for future use */
285
286 #define PadnamelistARRAY(pnl)   AvARRAY(pnl)
287 #define PadnamelistMAX(pnl)     AvFILLp(pnl)
288 #define PadnamelistMAXNAMED(pnl) \
289         ((XPVAV*) SvANY(pnl))->xmg_u.xmg_hash_index
290
291 #define PadARRAY(pad)           AvARRAY(pad)
292 #define PadMAX(pad)             AvFILLp(pad)
293
294 #define PadnamePV(pn)           (SvPOKp(pn) ? SvPVX(pn) : NULL)
295 #define PadnameLEN(pn)          ((pn) == &PL_sv_undef ? 0 : SvCUR(pn))
296 #define PadnameUTF8(pn)         !!SvUTF8(pn)
297 #define PadnameSV(pn)           pn
298 #define PadnameIsOUR(pn)        !!SvPAD_OUR(pn)
299 #define PadnameOURSTASH(pn)     SvOURSTASH(pn)
300 #define PadnameOUTER(pn)        !!SvFAKE(pn)
301 #define PadnameIsSTATE(pn)      !!SvPAD_STATE(pn)
302 #define PadnameTYPE(pn)         (SvPAD_TYPED(pn) ? SvSTASH(pn) : NULL)
303
304
305 #ifdef DEBUGGING
306 #  define PAD_SV(po)       pad_sv(po)
307 #  define PAD_SETSV(po,sv) pad_setsv(po,sv)
308 #else
309 #  define PAD_SV(po)       (PL_curpad[po])
310 #  define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
311 #endif
312
313 #define PAD_SVl(po)       (PL_curpad[po])
314
315 #define PAD_BASE_SV(padlist, po) \
316         (PadlistARRAY(padlist)[1])                                      \
317             ? AvARRAY(MUTABLE_AV((PadlistARRAY(padlist)[1])))[po] \
318             : NULL;
319
320
321 #define PAD_SET_CUR_NOSAVE(padlist,nth) \
322         PL_comppad = (PAD*) (PadlistARRAY(padlist)[nth]);       \
323         PL_curpad = AvARRAY(PL_comppad);                        \
324         DEBUG_Xv(PerlIO_printf(Perl_debug_log,                  \
325               "Pad 0x%"UVxf"[0x%"UVxf"] set_cur    depth=%d\n", \
326               PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth)));
327
328
329 #define PAD_SET_CUR(padlist,nth) \
330         SAVECOMPPAD();                                          \
331         PAD_SET_CUR_NOSAVE(padlist,nth);
332
333
334 #define PAD_SAVE_SETNULLPAD()   SAVECOMPPAD(); \
335         PL_comppad = NULL; PL_curpad = NULL;    \
336         DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n"));
337
338 #define PAD_SAVE_LOCAL(opad,npad) \
339         opad = PL_comppad;                                      \
340         PL_comppad = (npad);                                    \
341         PL_curpad =  PL_comppad ? AvARRAY(PL_comppad) : NULL;   \
342         DEBUG_Xv(PerlIO_printf(Perl_debug_log,                  \
343               "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n",          \
344               PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
345
346 #define PAD_RESTORE_LOCAL(opad) \
347         assert(!opad || !SvIS_FREED(opad));                                     \
348         PL_comppad = opad;                                              \
349         PL_curpad =  PL_comppad ? AvARRAY(PL_comppad) : NULL;   \
350         DEBUG_Xv(PerlIO_printf(Perl_debug_log,                  \
351               "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n",       \
352               PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
353
354
355 /*
356 =for apidoc m|void|CX_CURPAD_SAVE|struct context
357 Save the current pad in the given context block structure.
358
359 =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po
360 Access the SV at offset po in the saved current pad in the given
361 context block structure (can be used as an lvalue).
362
363 =cut
364 */
365
366 #define CX_CURPAD_SAVE(block)  (block).oldcomppad = PL_comppad
367 #define CX_CURPAD_SV(block,po) (AvARRAY(MUTABLE_AV(((block).oldcomppad)))[po])
368
369
370 /*
371 =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
372 Return the flags for the current compiling pad name
373 at offset C<po>. Assumes a valid slot entry.
374
375 =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
376 Return the name of the current compiling pad name
377 at offset C<po>. Assumes a valid slot entry.
378
379 =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
380 Return the type (stash) of the current compiling pad name at offset
381 C<po>. Must be a valid name. Returns null if not typed.
382
383 =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
384 Return the stash associated with an C<our> variable.
385 Assumes the slot entry is a valid C<our> lexical.
386
387 =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
388 The generation number of the name at offset C<po> in the current
389 compiling pad (lvalue). Note that C<SvUVX> is hijacked for this purpose.
390
391 =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen
392 Sets the generation number of the name at offset C<po> in the current
393 ling pad (lvalue) to C<gen>.  Note that C<SvUV_set> is hijacked for this purpose.
394
395 =cut
396
397 */
398
399 #define PAD_COMPNAME(po)        PAD_COMPNAME_SV(po)
400 #define PAD_COMPNAME_SV(po) (*av_fetch(PL_comppad_name, (po), FALSE))
401 #define PAD_COMPNAME_FLAGS(po) SvFLAGS(PAD_COMPNAME_SV(po))
402 #define PAD_COMPNAME_FLAGS_isOUR(po) SvPAD_OUR(PAD_COMPNAME_SV(po))
403 #define PAD_COMPNAME_PV(po) SvPV_nolen(PAD_COMPNAME_SV(po))
404
405 #define PAD_COMPNAME_TYPE(po) pad_compname_type(po)
406
407 #define PAD_COMPNAME_OURSTASH(po) \
408     (SvOURSTASH(PAD_COMPNAME_SV(po)))
409
410 #define PAD_COMPNAME_GEN(po) ((STRLEN)SvUVX(AvARRAY(PL_comppad_name)[po]))
411
412 #define PAD_COMPNAME_GEN_set(po, gen) SvUV_set(AvARRAY(PL_comppad_name)[po], (UV)(gen))
413
414
415 /*
416 =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl|CLONE_PARAMS* param
417 Clone the state variables associated with running and compiling pads.
418
419 =cut
420 */
421
422 /* NB - we set PL_comppad to null unless it points at a value that
423  * has already been dup'ed, ie it points to part of an active padlist.
424  * Otherwise PL_comppad ends up being a leaked scalar in code like
425  * the following:
426  *     threads->create(sub { threads->create(sub {...} ) } );
427  * where the second thread dups the outer sub's comppad but not the
428  * sub's CV or padlist. */
429
430 #define PAD_CLONE_VARS(proto_perl, param)                               \
431     PL_comppad                  = av_dup(proto_perl->Icomppad, param);  \
432     PL_curpad = PL_comppad ?  AvARRAY(PL_comppad) : NULL;               \
433     PL_comppad_name             = av_dup(proto_perl->Icomppad_name, param); \
434     PL_comppad_name_fill        = proto_perl->Icomppad_name_fill;       \
435     PL_comppad_name_floor       = proto_perl->Icomppad_name_floor;      \
436     PL_min_intro_pending        = proto_perl->Imin_intro_pending;       \
437     PL_max_intro_pending        = proto_perl->Imax_intro_pending;       \
438     PL_padix                    = proto_perl->Ipadix;                   \
439     PL_padix_floor              = proto_perl->Ipadix_floor;             \
440     PL_pad_reset_pending        = proto_perl->Ipad_reset_pending;       \
441     PL_cop_seqmax               = proto_perl->Icop_seqmax;
442
443 /*
444 =for apidoc Am|PADOFFSET|pad_add_name_pvs|const char *name|U32 flags|HV *typestash|HV *ourstash
445
446 Exactly like L</pad_add_name_pvn>, but takes a literal string instead
447 of a string/length pair.
448
449 =cut
450 */
451
452 #define pad_add_name_pvs(name,flags,typestash,ourstash) \
453     Perl_pad_add_name_pvn(aTHX_ STR_WITH_LEN(name), flags, typestash, ourstash)
454
455 /*
456 =for apidoc Am|PADOFFSET|pad_findmy_pvs|const char *name|U32 flags
457
458 Exactly like L</pad_findmy_pvn>, but takes a literal string instead
459 of a string/length pair.
460
461 =cut
462 */
463
464 #define pad_findmy_pvs(name,flags) \
465     Perl_pad_findmy_pvn(aTHX_ STR_WITH_LEN(name), flags)
466
467 /*
468  * Local variables:
469  * c-indentation-style: bsd
470  * c-basic-offset: 4
471  * indent-tabs-mode: nil
472  * End:
473  *
474  * ex: set ts=8 sts=4 sw=4 et:
475  */