This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Promote v5.36 usage and feature bundles doc
[perl5.git] / sv.h
1 /*    sv.h
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4  *    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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  */
10
11 #ifdef sv_flags
12 #undef sv_flags         /* Convex has this in <signal.h> for sigvec() */
13 #endif
14
15 /*
16 =for apidoc_section $SV_flags
17
18 =for apidoc Ay||svtype
19 An enum of flags for Perl types.  These are found in the file F<sv.h>
20 in the C<svtype> enum.  Test these flags with the C<SvTYPE> macro.
21
22 The types are:
23
24     SVt_NULL
25     SVt_IV
26     SVt_NV
27     SVt_RV
28     SVt_PV
29     SVt_PVIV
30     SVt_PVNV
31     SVt_PVMG
32     SVt_INVLIST
33     SVt_REGEXP
34     SVt_PVGV
35     SVt_PVLV
36     SVt_PVAV
37     SVt_PVHV
38     SVt_PVCV
39     SVt_PVFM
40     SVt_PVIO
41
42 These are most easily explained from the bottom up.
43
44 C<SVt_PVIO> is for I/O objects, C<SVt_PVFM> for formats, C<SVt_PVCV> for
45 subroutines, C<SVt_PVHV> for hashes and C<SVt_PVAV> for arrays.
46
47 All the others are scalar types, that is, things that can be bound to a
48 C<$> variable.  For these, the internal types are mostly orthogonal to
49 types in the Perl language.
50
51 Hence, checking C<< SvTYPE(sv) < SVt_PVAV >> is the best way to see whether
52 something is a scalar.
53
54 C<SVt_PVGV> represents a typeglob.  If C<!SvFAKE(sv)>, then it is a real,
55 incoercible typeglob.  If C<SvFAKE(sv)>, then it is a scalar to which a
56 typeglob has been assigned.  Assigning to it again will stop it from being
57 a typeglob.  C<SVt_PVLV> represents a scalar that delegates to another scalar
58 behind the scenes.  It is used, e.g., for the return value of C<substr> and
59 for tied hash and array elements.  It can hold any scalar value, including
60 a typeglob.  C<SVt_REGEXP> is for regular
61 expressions.  C<SVt_INVLIST> is for Perl
62 core internal use only.
63
64 C<SVt_PVMG> represents a "normal" scalar (not a typeglob, regular expression,
65 or delegate).  Since most scalars do not need all the internal fields of a
66 PVMG, we save memory by allocating smaller structs when possible.  All the
67 other types are just simpler forms of C<SVt_PVMG>, with fewer internal fields.
68 C<SVt_NULL> can only hold undef.  C<SVt_IV> can hold undef, an integer, or a
69 reference.  (C<SVt_RV> is an alias for C<SVt_IV>, which exists for backward
70 compatibility.)  C<SVt_NV> can hold any of those or a double.  C<SVt_PV> can only
71 hold C<undef> or a string.  C<SVt_PVIV> is a superset of C<SVt_PV> and C<SVt_IV>.
72 C<SVt_PVNV> is similar.  C<SVt_PVMG> can hold anything C<SVt_PVNV> can hold, but it
73 can, but does not have to, be blessed or magical.
74
75 =for apidoc AmnU||SVt_NULL
76 Type flag for scalars.  See L</svtype>.
77
78 =for apidoc AmnU||SVt_IV
79 Type flag for scalars.  See L</svtype>.
80
81 =for apidoc AmnU||SVt_NV
82 Type flag for scalars.  See L</svtype>.
83
84 =for apidoc AmnU||SVt_PV
85 Type flag for scalars.  See L</svtype>.
86
87 =for apidoc AmnU||SVt_PVIV
88 Type flag for scalars.  See L</svtype>.
89
90 =for apidoc AmnU||SVt_PVNV
91 Type flag for scalars.  See L</svtype>.
92
93 =for apidoc AmnU||SVt_PVMG
94 Type flag for scalars.  See L</svtype>.
95
96 =for apidoc CmnU||SVt_INVLIST
97 Type flag for scalars.  See L<perlapi/svtype>.
98
99 =for apidoc AmnU||SVt_REGEXP
100 Type flag for regular expressions.  See L</svtype>.
101
102 =for apidoc AmnU||SVt_PVGV
103 Type flag for typeglobs.  See L</svtype>.
104
105 =for apidoc AmnU||SVt_PVLV
106 Type flag for scalars.  See L</svtype>.
107
108 =for apidoc AmnU||SVt_PVAV
109 Type flag for arrays.  See L</svtype>.
110
111 =for apidoc AmnU||SVt_PVHV
112 Type flag for hashes.  See L</svtype>.
113
114 =for apidoc AmnU||SVt_PVCV
115 Type flag for subroutines.  See L</svtype>.
116
117 =for apidoc AmnU||SVt_PVFM
118 Type flag for formats.  See L</svtype>.
119
120 =for apidoc AmnU||SVt_PVIO
121 Type flag for I/O objects.  See L</svtype>.
122
123 =cut
124
125   These are ordered so that the simpler types have a lower value; SvUPGRADE
126   doesn't allow you to upgrade from a higher numbered type to a lower numbered
127   one; also there is code that assumes that anything that has as a PV component
128   has a type numbered >= SVt_PV.
129 */
130
131
132 typedef enum {
133         SVt_NULL,       /* 0 */
134         /* BIND was here, before INVLIST replaced it.  */
135         SVt_IV,         /* 1 */
136         SVt_NV,         /* 2 */
137         /* RV was here, before it was merged with IV.  */
138         SVt_PV,         /* 3 */
139         SVt_INVLIST,    /* 4, implemented as a PV */
140         SVt_PVIV,       /* 5 */
141         SVt_PVNV,       /* 6 */
142         SVt_PVMG,       /* 7 */
143         SVt_REGEXP,     /* 8 */
144         /* PVBM was here, before BIND replaced it.  */
145         SVt_PVGV,       /* 9 */
146         SVt_PVLV,       /* 10 */
147         SVt_PVAV,       /* 11 */
148         SVt_PVHV,       /* 12 */
149         SVt_PVCV,       /* 13 */
150         SVt_PVFM,       /* 14 */
151         SVt_PVIO,       /* 15 */
152                         /* 16-31: Unused, though one should be reserved for a
153                          * freed sv, if the other 3 bits below the flags ones
154                          * get allocated */
155         SVt_LAST        /* keep last in enum. used to size arrays */
156 } svtype;
157
158 /* *** any alterations to the SV types above need to be reflected in
159  * SVt_MASK and the various PL_valid_types_* tables.  As of this writing those
160  * tables are in perl.h.  There are also two affected names tables in dump.c,
161  * one in B.xs, and 'bodies_by_type[]' in sv_inline.h.
162  *
163  * The bits that match 0xe0 are CURRENTLY UNUSED
164  * The bits above that are for flags, like SVf_IOK */
165
166 #define SVt_MASK 0x1f   /* smallest bitmask that covers all types */
167
168 #ifndef PERL_CORE
169 /* Fast Boyer Moore tables are now stored in magic attached to PVMGs */
170 #  define SVt_PVBM      SVt_PVMG
171 /* Anything wanting to create a reference from clean should ensure that it has
172    a scalar of type SVt_IV now:  */
173 #  define SVt_RV        SVt_IV
174 #endif
175
176 /* The array of arena roots for SV bodies is indexed by SvTYPE. SVt_NULL doesn't
177  * use a body, so that arena root is re-used for HEs. SVt_IV also doesn't, so
178  * that arena root is used for HVs with struct xpvhv_aux. */
179
180 #if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
181 #  define HE_ARENA_ROOT_IX      SVt_NULL
182 #endif
183 #if defined(PERL_IN_HV_C) || defined(PERL_IN_SV_C)
184 #  define HVAUX_ARENA_ROOT_IX   SVt_IV
185 #endif
186 #ifdef PERL_IN_SV_C
187 #  define SVt_FIRST SVt_NULL    /* the type of SV that new_SV() in sv.c returns */
188 #endif
189
190 #define PERL_ARENA_ROOTS_SIZE   (SVt_LAST)
191
192 /* typedefs to eliminate some typing */
193 typedef struct he HE;
194 typedef struct hek HEK;
195
196 /* Using C's structural equivalence to help emulate C++ inheritance here... */
197
198 /* start with 2 sv-head building blocks */
199 #define _SV_HEAD(ptrtype) \
200     ptrtype     sv_any;         /* pointer to body */   \
201     U32         sv_refcnt;      /* how many references to us */ \
202     U32         sv_flags        /* what we are */
203
204 #if NVSIZE <= IVSIZE
205 #  define _NV_BODYLESS_UNION NV svu_nv;
206 #else
207 #  define _NV_BODYLESS_UNION
208 #endif
209
210 #define _SV_HEAD_UNION \
211     union {                             \
212         char*   svu_pv;         /* pointer to malloced string */        \
213         IV      svu_iv;                 \
214         UV      svu_uv;                 \
215         _NV_BODYLESS_UNION              \
216         SV*     svu_rv;         /* pointer to another SV */             \
217         SV**    svu_array;              \
218         HE**    svu_hash;               \
219         GP*     svu_gp;                 \
220         PerlIO *svu_fp;                 \
221     }   sv_u                            \
222     _SV_HEAD_DEBUG
223
224 #ifdef DEBUG_LEAKING_SCALARS
225 #define _SV_HEAD_DEBUG ;\
226     PERL_BITFIELD32 sv_debug_optype:9;  /* the type of OP that allocated us */ \
227     PERL_BITFIELD32 sv_debug_inpad:1;   /* was allocated in a pad for an OP */ \
228     PERL_BITFIELD32 sv_debug_line:16;   /* the line where we were allocated */ \
229     UV              sv_debug_serial;    /* serial number of sv allocation   */ \
230     char *          sv_debug_file;      /* the file where we were allocated */ \
231     SV *            sv_debug_parent     /* what we were cloned from (ithreads)*/
232 #else
233 #define _SV_HEAD_DEBUG
234 #endif
235
236 struct STRUCT_SV {              /* struct sv { */
237     _SV_HEAD(void*);
238     _SV_HEAD_UNION;
239 };
240
241 struct gv {
242     _SV_HEAD(XPVGV*);           /* pointer to xpvgv body */
243     _SV_HEAD_UNION;
244 };
245
246 struct cv {
247     _SV_HEAD(XPVCV*);           /* pointer to xpvcv body */
248     _SV_HEAD_UNION;
249 };
250
251 struct av {
252     _SV_HEAD(XPVAV*);           /* pointer to xpvav body */
253     _SV_HEAD_UNION;
254 };
255
256 struct hv {
257     _SV_HEAD(XPVHV*);           /* pointer to xpvhv body */
258     _SV_HEAD_UNION;
259 };
260
261 struct io {
262     _SV_HEAD(XPVIO*);           /* pointer to xpvio body */
263     _SV_HEAD_UNION;
264 };
265
266 struct p5rx {
267     _SV_HEAD(struct regexp*);   /* pointer to regexp body */
268     _SV_HEAD_UNION;
269 };
270
271 struct invlist {
272     _SV_HEAD(XINVLIST*);       /* pointer to xpvinvlist body */
273     _SV_HEAD_UNION;
274 };
275
276 #undef _SV_HEAD
277 #undef _SV_HEAD_UNION           /* ensure no pollution */
278
279 /*
280 =for apidoc_section $SV
281
282 =for apidoc Am|U32|SvREFCNT|SV* sv
283 Returns the value of the object's reference count. Exposed
284 to perl code via Internals::SvREFCNT().
285
286 =for apidoc            SvREFCNT_inc
287 =for apidoc_item       SvREFCNT_inc_NN
288 =for apidoc_item |SV* |SvREFCNT_inc_simple|SV* sv
289 =for apidoc_item |SV* |SvREFCNT_inc_simple_NN|SV* sv
290 =for apidoc_item |void|SvREFCNT_inc_simple_void|SV* sv
291 =for apidoc_item |void|SvREFCNT_inc_simple_void_NN|SV* sv
292 =for apidoc_item       SvREFCNT_inc_void
293 =for apidoc_item |void|SvREFCNT_inc_void_NN|SV* sv
294
295 These all increment the reference count of the given SV.
296 The ones without C<void> in their names return the SV.
297
298 C<SvREFCNT_inc> is the base operation; the rest are optimizations if various
299 input constraints are known to be true; hence, all can be replaced with
300 C<SvREFCNT_inc>.
301
302 C<SvREFCNT_inc_NN> can only be used if you know C<sv> is not C<NULL>.  Since we
303 don't have to check the NULLness, it's faster and smaller.
304
305 C<SvREFCNT_inc_void> can only be used if you don't need the
306 return value.  The macro doesn't need to return a meaningful value.
307
308 C<SvREFCNT_inc_void_NN> can only be used if you both don't need the return
309 value, and you know that C<sv> is not C<NULL>.  The macro doesn't need to
310 return a meaningful value, or check for NULLness, so it's smaller and faster.
311
312 C<SvREFCNT_inc_simple> can only be used with expressions without side
313 effects.  Since we don't have to store a temporary value, it's faster.
314
315 C<SvREFCNT_inc_simple_NN> can only be used with expressions without side
316 effects and you know C<sv> is not C<NULL>.  Since we don't have to store a
317 temporary value, nor check for NULLness, it's faster and smaller.
318
319 C<SvREFCNT_inc_simple_void> can only be used with expressions without side
320 effects and you don't need the return value.
321
322 C<SvREFCNT_inc_simple_void_NN> can only be used with expressions without side
323 effects, you don't need the return value, and you know C<sv> is not C<NULL>.
324
325 =for apidoc SvREFCNT_dec
326 =for apidoc_item SvREFCNT_dec_NN
327
328 These decrement the reference count of the given SV.
329
330 C<SvREFCNT_dec_NN> may only be used when C<sv> is known to not be C<NULL>.
331
332 =for apidoc Am|svtype|SvTYPE|SV* sv
333 Returns the type of the SV.  See C<L</svtype>>.
334
335 =for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
336 Used to upgrade an SV to a more complex form.  Uses C<sv_upgrade> to
337 perform the upgrade if necessary.  See C<L</svtype>>.
338
339 =cut
340 */
341
342 #define SvANY(sv)       (sv)->sv_any
343 #define SvFLAGS(sv)     (sv)->sv_flags
344 #define SvREFCNT(sv)    (sv)->sv_refcnt
345
346 #define SvREFCNT_inc(sv)                Perl_SvREFCNT_inc(MUTABLE_SV(sv))
347 #define SvREFCNT_inc_simple(sv)         SvREFCNT_inc(sv)
348 #define SvREFCNT_inc_NN(sv)             Perl_SvREFCNT_inc_NN(MUTABLE_SV(sv))
349 #define SvREFCNT_inc_void(sv)           Perl_SvREFCNT_inc_void(MUTABLE_SV(sv))
350
351 /* These guys don't need the curly blocks */
352 #define SvREFCNT_inc_simple_void(sv)                                    \
353         STMT_START {                                                    \
354             SV * sv_ = MUTABLE_SV(sv);                                  \
355             if (sv_)                                                    \
356                 SvREFCNT(sv_)++;                                        \
357         } STMT_END
358
359 #define SvREFCNT_inc_simple_NN(sv)      (++(SvREFCNT(sv)),MUTABLE_SV(sv))
360 #define SvREFCNT_inc_void_NN(sv)        (void)(++SvREFCNT(MUTABLE_SV(sv)))
361 #define SvREFCNT_inc_simple_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
362
363 #define SvREFCNT_dec(sv)        Perl_SvREFCNT_dec(aTHX_ MUTABLE_SV(sv))
364 #define SvREFCNT_dec_NN(sv)     Perl_SvREFCNT_dec_NN(aTHX_ MUTABLE_SV(sv))
365
366 #define SVTYPEMASK      0xff
367 #define SvTYPE(sv)      ((svtype)((sv)->sv_flags & SVTYPEMASK))
368
369 /* Sadly there are some parts of the core that have pointers to already-freed
370    SV heads, and rely on being able to tell that they are now free. So mark
371    them all by using a consistent macro.  */
372 #define SvIS_FREED(sv)  UNLIKELY(((sv)->sv_flags == SVTYPEMASK))
373
374 /* this is defined in this peculiar way to avoid compiler warnings.
375  * See the <20121213131428.GD1842@iabyn.com> thread in p5p */
376 #define SvUPGRADE(sv, mt) \
377     ((void)(SvTYPE(sv) >= (mt) || (sv_upgrade(sv, mt),1)))
378
379 #define SVf_IOK         0x00000100  /* has valid public integer value */
380 #define SVf_NOK         0x00000200  /* has valid public numeric value */
381 #define SVf_POK         0x00000400  /* has valid public pointer value */
382 #define SVf_ROK         0x00000800  /* has a valid reference pointer */
383
384 #define SVp_IOK         0x00001000  /* has valid non-public integer value */
385 #define SVp_NOK         0x00002000  /* has valid non-public numeric value */
386 #define SVp_POK         0x00004000  /* has valid non-public pointer value */
387 #define SVp_SCREAM      0x00008000  /* currently unused on plain scalars */
388 #define SVphv_CLONEABLE SVp_SCREAM  /* PVHV (stashes) clone its objects */
389 #define SVpgv_GP        SVp_SCREAM  /* GV has a valid GP */
390 #define SVprv_PCS_IMPORTED  SVp_SCREAM  /* RV is a proxy for a constant
391                                        subroutine in another package. Set the
392                                        GvIMPORTED_CV_on() if it needs to be
393                                        expanded to a real GV */
394
395 /* SVf_PROTECT is what SVf_READONLY should have been: i.e. modifying
396  * this SV is completely illegal. However, SVf_READONLY (via
397  * Internals::SvREADONLY()) has come to be seen as a flag that can be
398  * temporarily set and unset by the user to indicate e.g. whether a hash
399  * is "locked". Now, Hash::Util et al only set SVf_READONLY, while core
400  * sets both (SVf_READONLY|SVf_PROTECT) to indicate both to core and user
401  * code that this SV should not be messed with.
402  */
403 #define SVf_PROTECT     0x00010000  /* very read-only */
404 #define SVs_PADTMP      0x00020000  /* in use as tmp */
405 #define SVs_PADSTALE    0x00040000  /* lexical has gone out of scope;
406                                         only used when !PADTMP */
407 #define SVs_TEMP        0x00080000  /* mortal (implies string is stealable) */
408 #define SVs_OBJECT      0x00100000  /* is "blessed" */
409 #define SVs_GMG         0x00200000  /* has magical get method */
410 #define SVs_SMG         0x00400000  /* has magical set method */
411 #define SVs_RMG         0x00800000  /* has random magical methods */
412
413 #define SVf_FAKE        0x01000000  /* 0: glob is just a copy
414                                        1: SV head arena wasn't malloc()ed
415                                        2: For PVCV, whether CvUNIQUE(cv)
416                                           refers to an eval or once only
417                                           [CvEVAL(cv), CvSPECIAL(cv)]
418                                        3: HV: informally reserved by DAPM
419                                           for vtables
420                                        4: Together with other flags (or
421                                            lack thereof) indicates a regex,
422                                            including PVLV-as-regex. See
423                                            isREGEXP().
424                                        */
425 #define SVf_OOK         0x02000000  /* has valid offset value */
426 #define SVphv_HasAUX    SVf_OOK     /* PVHV has an additional hv_aux struct */
427 #define SVf_BREAK       0x04000000  /* refcnt is artificially low - used by
428                                        SVs in final arena cleanup.
429                                        Set in S_regtry on PL_reg_curpm, so that
430                                        perl_destruct will skip it.
431                                        Used for mark and sweep by OP_AASSIGN
432                                        */
433 #define SVf_READONLY    0x08000000  /* may not be modified */
434
435
436
437
438 #define SVf_THINKFIRST  (SVf_READONLY|SVf_PROTECT|SVf_ROK|SVf_FAKE \
439                         |SVs_RMG|SVf_IsCOW)
440
441 #define SVf_OK          (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
442                          SVp_IOK|SVp_NOK|SVp_POK|SVpgv_GP)
443
444 #define PRIVSHIFT 4     /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
445
446 /* SVf_AMAGIC means that the stash *may* have overload methods. It's
447  * set each time a function is compiled into a stash, and is reset by the
448  * overload code when called for the first time and finds that there are
449  * no overload methods. Note that this used to be set on the object; but
450  * is now only set on stashes.
451  */
452 #define SVf_AMAGIC      0x10000000  /* has magical overloaded methods */
453 #define SVf_IsCOW       0x10000000  /* copy on write (shared hash key if
454                                        SvLEN == 0) */
455
456 /* Ensure this value does not clash with the GV_ADD* flags in gv.h, or the
457    CV_CKPROTO_* flags in op.c, or the padadd_* flags in pad.h: */
458 #define SVf_UTF8        0x20000000  /* SvPV is UTF-8 encoded
459                                        This is also set on RVs whose overloaded
460                                        stringification is UTF-8. This might
461                                        only happen as a side effect of SvPV() */
462 /* PVHV */
463 #define SVphv_SHAREKEYS 0x20000000  /* PVHV keys live on shared string table */
464
465 /* PVAV could probably use 0x2000000 without conflict. I assume that PVFM can
466    be UTF-8 encoded, and PVCVs could well have UTF-8 prototypes. PVIOs haven't
467    been restructured, so sometimes get used as string buffers.  */
468
469
470 /* Some private flags. */
471
472
473 /* scalar SVs with SVp_POK */
474 #define SVppv_STATIC    0x40000000 /* PV is pointer to static const; must be set with SVf_IsCOW */
475 /* PVAV */
476 #define SVpav_REAL      0x40000000  /* free old entries */
477 /* PVHV */
478 #define SVphv_LAZYDEL   0x40000000  /* entry in xhv_eiter must be deleted */
479
480 /* IV, PVIV, PVNV, PVMG, PVGV and (I assume) PVLV  */
481 #define SVf_IVisUV      0x80000000  /* use XPVUV instead of XPVIV */
482 /* PVAV */
483 #define SVpav_REIFY     0x80000000  /* can become real */
484 /* PVHV */
485 #define SVphv_HASKFLAGS 0x80000000  /* keys have flag byte after hash */
486 /* RV upwards. However, SVf_ROK and SVp_IOK are exclusive  */
487 #define SVprv_WEAKREF   0x80000000  /* Weak reference */
488 /* pad name vars only */
489
490 #define _XPV_HEAD                                                       \
491     HV*         xmg_stash;      /* class package */                     \
492     union _xmgu xmg_u;                                                  \
493     STRLEN      xpv_cur;        /* length of svu_pv as a C string */    \
494     union {                                                             \
495         STRLEN  xpvlenu_len;    /* allocated size */                    \
496         struct regexp* xpvlenu_rx; /* regex when SV body is XPVLV */    \
497     } xpv_len_u
498
499 #define xpv_len xpv_len_u.xpvlenu_len
500
501 union _xnvu {
502     NV      xnv_nv;             /* numeric value, if any */
503     HV *    xgv_stash;
504     line_t  xnv_lines;           /* used internally by S_scan_subst() */
505     bool    xnv_bm_tail;        /* an SvVALID (BM) SV has an implicit "\n" */
506 };
507
508 union _xivu {
509     IV      xivu_iv;            /* integer value */
510     UV      xivu_uv;
511     HEK *   xivu_namehek;       /* xpvlv, xpvgv: GvNAME */
512     bool    xivu_eval_seen;     /* used internally by S_scan_subst() */
513
514 };
515
516 union _xmgu {
517     MAGIC*  xmg_magic;          /* linked list of magicalness */
518     STRLEN  xmg_hash_index;     /* used while freeing hash entries */
519 };
520
521 struct xpv {
522     _XPV_HEAD;
523 };
524
525 struct xpviv {
526     _XPV_HEAD;
527     union _xivu xiv_u;
528 };
529
530 #define xiv_iv xiv_u.xivu_iv
531
532 struct xpvuv {
533     _XPV_HEAD;
534     union _xivu xuv_u;
535 };
536
537 #define xuv_uv xuv_u.xivu_uv
538
539 struct xpvnv {
540     _XPV_HEAD;
541     union _xivu xiv_u;
542     union _xnvu xnv_u;
543 };
544
545 /* This structure must match the beginning of struct xpvhv in hv.h. */
546 struct xpvmg {
547     _XPV_HEAD;
548     union _xivu xiv_u;
549     union _xnvu xnv_u;
550 };
551
552 struct xpvlv {
553     _XPV_HEAD;
554     union _xivu xiv_u;
555     union _xnvu xnv_u;
556     union {
557         STRLEN  xlvu_targoff;
558         SSize_t xlvu_stargoff;
559     } xlv_targoff_u;
560     STRLEN      xlv_targlen;
561     SV*         xlv_targ;
562     char        xlv_type;       /* k=keys .=pos x=substr v=vec /=join/re
563                                  * y=alem/helem/iter t=tie T=tied HE */
564     char        xlv_flags;      /* 1 = negative offset  2 = negative len
565                                    4 = out of range (vec) */
566 };
567
568 #define xlv_targoff xlv_targoff_u.xlvu_targoff
569
570 struct xpvinvlist {
571     _XPV_HEAD;
572     IV          prev_index;     /* caches result of previous invlist_search() */
573     STRLEN      iterator;       /* Stores where we are in iterating */
574     bool        is_offset;      /* The data structure for all inversion lists
575                                    begins with an element for code point U+0000.
576                                    If this bool is set, the actual list contains
577                                    that 0; otherwise, the list actually begins
578                                    with the following element.  Thus to invert
579                                    the list, merely toggle this flag  */
580 };
581
582 /* This structure works in 2 ways - regular scalar, or GV with GP */
583
584 struct xpvgv {
585     _XPV_HEAD;
586     union _xivu xiv_u;
587     union _xnvu xnv_u;
588 };
589
590 typedef U32 cv_flags_t;
591
592 #define _XPVCV_COMMON                                                           \
593     HV *        xcv_stash;                                                      \
594     union {                                                                     \
595         OP *    xcv_start;                                                      \
596         ANY     xcv_xsubany;                                                    \
597     }           xcv_start_u;                                                    \
598     union {                                                                     \
599         OP *    xcv_root;                                                       \
600         void    (*xcv_xsub) (pTHX_ CV*);                                        \
601     }           xcv_root_u;                                                     \
602     union {                                                             \
603         GV *    xcv_gv;                                                 \
604         HEK *   xcv_hek;                                                \
605     }           xcv_gv_u;                                               \
606     char *      xcv_file;                                                       \
607     union {                                                                     \
608         PADLIST *       xcv_padlist;                                            \
609         void *          xcv_hscxt;                                              \
610     }           xcv_padlist_u;                                                  \
611     CV *        xcv_outside;                                                    \
612     U32         xcv_outside_seq; /* the COP sequence (at the point of our       \
613                                   * compilation) in the lexically enclosing     \
614                                   * sub */                                      \
615     cv_flags_t  xcv_flags;                                              \
616     I32 xcv_depth       /* >= 2 indicates recursive call */
617
618 /* This structure must match XPVCV in cv.h */
619
620 struct xpvfm {
621     _XPV_HEAD;
622     _XPVCV_COMMON;
623 };
624
625
626 struct xpvio {
627     _XPV_HEAD;
628     union _xivu xiv_u;
629     /* ifp and ofp are normally the same, but sockets need separate streams */
630     PerlIO *    xio_ofp;
631     /* Cray addresses everything by word boundaries (64 bits) and
632      * code and data pointers cannot be mixed (which is exactly what
633      * Perl_filter_add() tries to do with the dirp), hence the
634      *  following union trick (as suggested by Gurusamy Sarathy).
635      * For further information see Geir Johansen's problem report
636      * titled [ID 20000612.002 (#3366)] Perl problem on Cray system
637      * The any pointer (known as IoANY()) will also be a good place
638      * to hang any IO disciplines to.
639      */
640     union {
641         DIR *   xiou_dirp;      /* for opendir, readdir, etc */
642         void *  xiou_any;       /* for alignment */
643     } xio_dirpu;
644     /* IV xio_lines is now in IVX  $. */
645     IV          xio_page;       /* $% */
646     IV          xio_page_len;   /* $= */
647     IV          xio_lines_left; /* $- */
648     char *      xio_top_name;   /* $^ */
649     GV *        xio_top_gv;     /* $^ */
650     char *      xio_fmt_name;   /* $~ */
651     GV *        xio_fmt_gv;     /* $~ */
652     char *      xio_bottom_name;/* $^B */
653     GV *        xio_bottom_gv;  /* $^B */
654     char        xio_type;
655     U8          xio_flags;
656 };
657
658 #define xio_dirp        xio_dirpu.xiou_dirp
659 #define xio_any         xio_dirpu.xiou_any
660
661 #define IOf_ARGV        1       /* this fp iterates over ARGV */
662 #define IOf_START       2       /* check for null ARGV and substitute '-' */
663 #define IOf_FLUSH       4       /* this fp wants a flush after write op */
664 #define IOf_DIDTOP      8       /* just did top of form */
665 #define IOf_UNTAINT     16      /* consider this fp (and its data) "safe" */
666 #define IOf_NOLINE      32      /* slurped a pseudo-line from empty file */
667 #define IOf_FAKE_DIRP   64      /* xio_dirp is fake (source filters kludge)
668                                    Also, when this is set, SvPVX() is valid */
669
670 /* The following macros define implementation-independent predicates on SVs. */
671
672 /*
673 =for apidoc Am|U32|SvNIOK|SV* sv
674 Returns a U32 value indicating whether the SV contains a number, integer or
675 double.
676
677 =for apidoc Am|U32|SvNIOKp|SV* sv
678 Returns a U32 value indicating whether the SV contains a number, integer or
679 double.  Checks the B<private> setting.  Use C<SvNIOK> instead.
680
681 =for apidoc Am|void|SvNIOK_off|SV* sv
682 Unsets the NV/IV status of an SV.
683
684 =for apidoc Am|U32|SvOK|SV* sv
685 Returns a U32 value indicating whether the value is defined.  This is
686 only meaningful for scalars.
687
688 =for apidoc Am|U32|SvIOKp|SV* sv
689 Returns a U32 value indicating whether the SV contains an integer.  Checks
690 the B<private> setting.  Use C<SvIOK> instead.
691
692 =for apidoc Am|U32|SvNOKp|SV* sv
693 Returns a U32 value indicating whether the SV contains a double.  Checks the
694 B<private> setting.  Use C<SvNOK> instead.
695
696 =for apidoc Am|U32|SvPOKp|SV* sv
697 Returns a U32 value indicating whether the SV contains a character string.
698 Checks the B<private> setting.  Use C<SvPOK> instead.
699
700 =for apidoc Am|U32|SvIOK|SV* sv
701 Returns a U32 value indicating whether the SV contains an integer.
702
703 =for apidoc Am|void|SvIOK_on|SV* sv
704 Tells an SV that it is an integer.
705
706 =for apidoc Am|void|SvIOK_off|SV* sv
707 Unsets the IV status of an SV.
708
709 =for apidoc Am|void|SvIOK_only|SV* sv
710 Tells an SV that it is an integer and disables all other C<OK> bits.
711
712 =for apidoc Am|void|SvIOK_only_UV|SV* sv
713 Tells an SV that it is an unsigned integer and disables all other C<OK> bits.
714
715 =for apidoc Am|bool|SvIOK_UV|SV* sv
716 Returns a boolean indicating whether the SV contains an integer that must be
717 interpreted as unsigned.  A non-negative integer whose value is within the
718 range of both an IV and a UV may be flagged as either C<SvUOK> or C<SvIOK>.
719
720 =for apidoc Am|bool|SvUOK|SV* sv
721 Returns a boolean indicating whether the SV contains an integer that must be
722 interpreted as unsigned.  A non-negative integer whose value is within the
723 range of both an IV and a UV may be flagged as either C<SvUOK> or C<SvIOK>.
724
725 =for apidoc Am|bool|SvIOK_notUV|SV* sv
726 Returns a boolean indicating whether the SV contains a signed integer.
727
728 =for apidoc Am|U32|SvNOK|SV* sv
729 Returns a U32 value indicating whether the SV contains a double.
730
731 =for apidoc Am|void|SvNOK_on|SV* sv
732 Tells an SV that it is a double.
733
734 =for apidoc Am|void|SvNOK_off|SV* sv
735 Unsets the NV status of an SV.
736
737 =for apidoc Am|void|SvNOK_only|SV* sv
738 Tells an SV that it is a double and disables all other OK bits.
739
740 =for apidoc Am|U32|SvPOK|SV* sv
741 Returns a U32 value indicating whether the SV contains a character
742 string.
743
744 =for apidoc Am|void|SvPOK_on|SV* sv
745 Tells an SV that it is a string.
746
747 =for apidoc Am|void|SvPOK_off|SV* sv
748 Unsets the PV status of an SV.
749
750 =for apidoc Am|void|SvPOK_only|SV* sv
751 Tells an SV that it is a string and disables all other C<OK> bits.
752 Will also turn off the UTF-8 status.
753
754 =for apidoc Am|U32|SvBoolFlagsOK|SV* sv
755 Returns a bool indicating whether the SV has the right flags set such
756 that it is safe to call C<BOOL_INTERNALS_sv_isbool()> or
757 C<BOOL_INTERNALS_sv_isbool_true()> or
758 C<BOOL_INTERNALS_sv_isbool_false()>. Currently equivalent to
759 C<SvIandPOK(sv)> or C<SvIOK(sv) && SvPOK(sv)>. Serialization may want to
760 unroll this check. If so you are strongly recommended to add code like
761 C<assert(SvBoolFlagsOK(sv));> B<before> calling using any of the
762 BOOL_INTERNALS macros.
763
764 =for apidoc Am|U32|SvIandPOK|SV* sv
765 Returns a bool indicating whether the SV is both C<SvPOK()> and
766 C<SvIOK()> at the same time. Equivalent to C<SvIOK(sv) && SvPOK(sv)> but
767 more efficient.
768
769 =for apidoc Am|void|SvIandPOK_on|SV* sv
770 Tells an SV that is a string and a number in one operation. Equivalent
771 to C<SvIOK_on(sv); SvPOK_on(sv);> but more efficient.
772
773 =for apidoc Am|void|SvIandPOK_off|SV* sv
774 Unsets the PV and IV status of an SV in one operation. Equivalent to
775 C<SvIOK_off(sv); SvPK_off(v);> but more efficient.
776
777 =for apidoc Am|bool|BOOL_INTERNALS_sv_isbool|SV* sv
778 Checks if a C<SvBoolFlagsOK()> sv is a bool. B<Note> that it is the
779 caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()> before
780 calling this. This is only useful in specialized logic like
781 serialization code where performance is critical and the flags have
782 already been checked to be correct. Almost always you should be using
783 C<sv_isbool(sv)> instead.
784
785 =for apidoc Am|bool|BOOL_INTERNALS_sv_isbool_true|SV* sv
786 Checks if a C<SvBoolFlagsOK()> sv is a true bool. B<Note> that it is
787 the caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()>
788 before calling this. This is only useful in specialized logic like
789 serialization code where performance is critical and the flags have
790 already been checked to be correct. This is B<NOT> what you should use
791 to check if an SV is "true", for that you should be using
792 C<SvTRUE(sv)> instead.
793
794 =for apidoc Am|bool|BOOL_INTERNALS_sv_isbool_false|SV* sv
795 Checks if a C<SvBoolFlagsOK()> sv is a false bool. B<Note> that it is
796 the caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()>
797 before calling this. This is only useful in specialized logic like
798 serialization code where performance is critical and the flags have
799 already been checked to be correct. This is B<NOT> what you should use
800 to check if an SV is "false", for that you should be using
801 C<!SvTRUE(sv)> instead.
802
803 =for apidoc Am|bool|SvVOK|SV* sv
804 Returns a boolean indicating whether the SV contains a v-string.
805
806 =for apidoc Am|U32|SvOOK|SV* sv
807 Returns a U32 indicating whether the pointer to the string buffer is offset.
808 This hack is used internally to speed up removal of characters from the
809 beginning of a C<L</SvPV>>.  When C<SvOOK> is true, then the start of the
810 allocated string buffer is actually C<SvOOK_offset()> bytes before C<SvPVX>.
811 This offset used to be stored in C<SvIVX>, but is now stored within the spare
812 part of the buffer.
813
814 =for apidoc Am|U32|SvROK|SV* sv
815 Tests if the SV is an RV.
816
817 =for apidoc Am|void|SvROK_on|SV* sv
818 Tells an SV that it is an RV.
819
820 =for apidoc Am|void|SvROK_off|SV* sv
821 Unsets the RV status of an SV.
822
823 =for apidoc Am|SV*|SvRV|SV* sv
824 Dereferences an RV to return the SV.
825
826 =for apidoc Am|IV|SvIVX|SV* sv
827 Returns the raw value in the SV's IV slot, without checks or conversions.
828 Only use when you are sure C<SvIOK> is true.  See also C<L</SvIV>>.
829
830 =for apidoc Am|UV|SvUVX|SV* sv
831 Returns the raw value in the SV's UV slot, without checks or conversions.
832 Only use when you are sure C<SvIOK> is true.  See also C<L</SvUV>>.
833
834 =for apidoc AmD|UV|SvUVXx|SV* sv
835 This is an unnecessary synonym for L</SvUVX>
836
837 =for apidoc Am|NV|SvNVX|SV* sv
838 Returns the raw value in the SV's NV slot, without checks or conversions.
839 Only use when you are sure C<SvNOK> is true.  See also C<L</SvNV>>.
840
841 =for apidoc Am   |char*      |SvPVX|SV* sv
842 =for apidoc_item |const char*|SvPVX_const|SV* sv
843 =for apidoc_item |char*      |SvPVX_mutable|SV* sv
844 =for apidoc_item |char*      |SvPVXx|SV* sv
845
846 These return a pointer to the physical string in the SV.  The SV must contain a
847 string.  Prior to 5.9.3 it is not safe to execute these unless the SV's
848 type >= C<SVt_PV>.
849
850 These are also used to store the name of an autoloaded subroutine in an XS
851 AUTOLOAD routine.  See L<perlguts/Autoloading with XSUBs>.
852
853 C<SvPVXx> is identical to C<SvPVX>.
854
855 C<SvPVX_mutable> is merely a synonym for C<SvPVX>, but its name emphasizes that
856 the string is modifiable by the caller.
857
858 C<SvPVX_const> differs in that the return value has been cast so that the
859 compiler will complain if you were to try to modify the contents of the string,
860 (unless you cast away const yourself).
861
862 =for apidoc Am|STRLEN|SvCUR|SV* sv
863 Returns the length, in bytes, of the PV inside the SV.
864 Note that this may not match Perl's C<length>; for that, use
865 C<sv_len_utf8(sv)>. See C<L</SvLEN>> also.
866
867 =for apidoc Am|STRLEN|SvLEN|SV* sv
868 Returns the size of the string buffer in the SV, not including any part
869 attributable to C<SvOOK>.  See C<L</SvCUR>>.
870
871 =for apidoc Am|char*|SvEND|SV* sv
872 Returns a pointer to the spot just after the last character in
873 the string which is in the SV, where there is usually a trailing
874 C<NUL> character (even though Perl scalars do not strictly require it).
875 See C<L</SvCUR>>.  Access the character as C<*(SvEND(sv))>.
876
877 Warning: If C<SvCUR> is equal to C<SvLEN>, then C<SvEND> points to
878 unallocated memory.
879
880 =for apidoc Am|HV*|SvSTASH|SV* sv
881 Returns the stash of the SV.
882
883 =for apidoc Am|void|SvIV_set|SV* sv|IV val
884 Set the value of the IV pointer in sv to val.  It is possible to perform
885 the same function of this macro with an lvalue assignment to C<SvIVX>.
886 With future Perls, however, it will be more efficient to use
887 C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.
888
889 =for apidoc Am|void|SvNV_set|SV* sv|NV val
890 Set the value of the NV pointer in C<sv> to val.  See C<L</SvIV_set>>.
891
892 =for apidoc Am|void|SvPV_set|SV* sv|char* val
893 This is probably not what you want to use, you probably wanted
894 L</sv_usepvn_flags> or L</sv_setpvn> or L</sv_setpvs>.
895
896 Set the value of the PV pointer in C<sv> to the Perl allocated
897 C<NUL>-terminated string C<val>.  See also C<L</SvIV_set>>.
898
899 Remember to free the previous PV buffer. There are many things to check.
900 Beware that the existing pointer may be involved in copy-on-write or other
901 mischief, so do C<SvOOK_off(sv)> and use C<sv_force_normal> or
902 C<SvPV_force> (or check the C<SvIsCOW> flag) first to make sure this
903 modification is safe. Then finally, if it is not a COW, call
904 C<L</SvPV_free>> to free the previous PV buffer.
905
906 =for apidoc Am|void|SvUV_set|SV* sv|UV val
907 Set the value of the UV pointer in C<sv> to val.  See C<L</SvIV_set>>.
908
909 =for apidoc Am|void|SvRV_set|SV* sv|SV* val
910 Set the value of the RV pointer in C<sv> to val.  See C<L</SvIV_set>>.
911
912 =for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
913 Set the value of the MAGIC pointer in C<sv> to val.  See C<L</SvIV_set>>.
914
915 =for apidoc Am|void|SvSTASH_set|SV* sv|HV* val
916 Set the value of the STASH pointer in C<sv> to val.  See C<L</SvIV_set>>.
917
918 =for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
919 Sets the current length, in bytes, of the C string which is in the SV.
920 See C<L</SvCUR>> and C<SvIV_set>>.
921
922 =for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
923 Set the size of the string buffer for the SV. See C<L</SvLEN>>.
924
925 =cut
926 */
927
928 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
929 #define SvNIOKp(sv)             (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
930 #define SvNIOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
931                                                   SVp_IOK|SVp_NOK|SVf_IVisUV))
932
933 #define assert_not_ROK(sv)      assert_(!SvROK(sv) || !SvRV(sv))
934 #define assert_not_glob(sv)     assert_(!isGV_with_GP(sv))
935
936 #define SvOK(sv)                (SvFLAGS(sv) & SVf_OK)
937 #define SvOK_off(sv)            (assert_not_ROK(sv) assert_not_glob(sv) \
938                                  SvFLAGS(sv) &= ~(SVf_OK|               \
939                                                   SVf_IVisUV|SVf_UTF8), \
940                                                         SvOOK_off(sv))
941 #define SvOK_off_exc_UV(sv)     (assert_not_ROK(sv)                     \
942                                  SvFLAGS(sv) &= ~(SVf_OK|               \
943                                                   SVf_UTF8),            \
944                                                         SvOOK_off(sv))
945
946 #define SvOKp(sv)               (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
947 #define SvIOKp(sv)              (SvFLAGS(sv) & SVp_IOK)
948 #define SvIOKp_on(sv)           (assert_not_glob(sv)    \
949                                     SvFLAGS(sv) |= SVp_IOK)
950 #define SvNOKp(sv)              (SvFLAGS(sv) & SVp_NOK)
951 #define SvNOKp_on(sv)           (assert_not_glob(sv) SvFLAGS(sv) |= SVp_NOK)
952 #define SvPOKp(sv)              (SvFLAGS(sv) & SVp_POK)
953 #define SvPOKp_on(sv)           (assert_not_ROK(sv) assert_not_glob(sv) \
954                                  SvFLAGS(sv) |= SVp_POK)
955
956 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
957 #define SvIOK_on(sv)            (assert_not_glob(sv)    \
958                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
959 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
960 #define SvIOK_only(sv)          (SvOK_off(sv), \
961                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
962 #define SvIOK_only_UV(sv)       (assert_not_glob(sv) SvOK_off_exc_UV(sv), \
963                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
964
965 #define SvIOK_UV(sv)            ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
966                                  == (SVf_IOK|SVf_IVisUV))
967 #define SvUOK(sv)               SvIOK_UV(sv)
968 #define SvIOK_notUV(sv)         ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
969                                  == SVf_IOK)
970
971 #define SvIandPOK(sv)              ((SvFLAGS(sv) & (SVf_IOK|SVf_POK)) == (SVf_IOK|SVf_POK))
972 #define SvIandPOK_on(sv)           (assert_not_glob(sv) \
973                                     (SvFLAGS(sv) |= (SVf_IOK|SVp_IOK|SVf_POK|SVp_POK)))
974 #define SvIandPOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_POK|SVp_POK))
975
976 #define SvBoolFlagsOK(sv)           SvIandPOK(sv)
977
978 #define BOOL_INTERNALS_sv_isbool(sv)      (SvIsCOW_static(sv) && \
979         (SvPVX_const(sv) == PL_Yes || SvPVX_const(sv) == PL_No))
980 #define BOOL_INTERNALS_sv_isbool_true(sv)      (SvIsCOW_static(sv) && \
981         (SvPVX_const(sv) == PL_Yes))
982 #define BOOL_INTERNALS_sv_isbool_false(sv)      (SvIsCOW_static(sv) && \
983         (SvPVX_const(sv) == PL_No))
984
985 #define SvIsUV(sv)              (SvFLAGS(sv) & SVf_IVisUV)
986 #define SvIsUV_on(sv)           (SvFLAGS(sv) |= SVf_IVisUV)
987 #define SvIsUV_off(sv)          (SvFLAGS(sv) &= ~SVf_IVisUV)
988
989 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
990 #define SvNOK_on(sv)            (assert_not_glob(sv) \
991                                  SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
992 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
993 #define SvNOK_only(sv)          (SvOK_off(sv), \
994                                     SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
995
996 /*
997 =for apidoc Am|U32|SvUTF8|SV* sv
998 Returns a U32 value indicating the UTF-8 status of an SV.  If things are set-up
999 properly, this indicates whether or not the SV contains UTF-8 encoded data.
1000 You should use this I<after> a call to C<L</SvPV>> or one of its variants, in
1001 case any call to string overloading updates the internal flag.
1002
1003 If you want to take into account the L<bytes> pragma, use C<L</DO_UTF8>>
1004 instead.
1005
1006 =for apidoc Am|void|SvUTF8_on|SV *sv
1007 Turn on the UTF-8 status of an SV (the data is not changed, just the flag).
1008 Do not use frivolously.
1009
1010 =for apidoc Am|void|SvUTF8_off|SV *sv
1011 Unsets the UTF-8 status of an SV (the data is not changed, just the flag).
1012 Do not use frivolously.
1013
1014 =for apidoc Am|void|SvPOK_only_UTF8|SV* sv
1015 Tells an SV that it is a string and disables all other C<OK> bits,
1016 and leaves the UTF-8 status as it was.
1017
1018 =cut
1019  */
1020
1021 /* Ensure the return value of this macro does not clash with the GV_ADD* flags
1022 in gv.h: */
1023 #define SvUTF8(sv)              (SvFLAGS(sv) & SVf_UTF8)
1024 #define SvUTF8_on(sv)           (SvFLAGS(sv) |= (SVf_UTF8))
1025 #define SvUTF8_off(sv)          (SvFLAGS(sv) &= ~(SVf_UTF8))
1026
1027 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
1028 #define SvPOK_on(sv)            (assert_not_ROK(sv) assert_not_glob(sv) \
1029                                  SvFLAGS(sv) |= (SVf_POK|SVp_POK))
1030 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
1031 #define SvPOK_only(sv)          (assert_not_ROK(sv) assert_not_glob(sv) \
1032                                  SvFLAGS(sv) &= ~(SVf_OK|               \
1033                                                   SVf_IVisUV|SVf_UTF8), \
1034                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
1035 #define SvPOK_only_UTF8(sv)     (assert_not_ROK(sv) assert_not_glob(sv) \
1036                                  SvFLAGS(sv) &= ~(SVf_OK|               \
1037                                                   SVf_IVisUV),          \
1038                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
1039
1040 #define SvVOK(sv)               (SvMAGICAL(sv)                          \
1041                                  && mg_find(sv,PERL_MAGIC_vstring))
1042 /*
1043 =for apidoc Am|MAGIC*|SvVSTRING_mg|SV * sv
1044
1045 Returns the vstring magic, or NULL if none
1046
1047 =cut
1048 */
1049 #define SvVSTRING_mg(sv)        (SvMAGICAL(sv) \
1050                                  ? mg_find(sv,PERL_MAGIC_vstring) : NULL)
1051
1052 #define SvOOK(sv)               (SvFLAGS(sv) & SVf_OOK)
1053 #define SvOOK_on(sv)            (SvFLAGS(sv) |= SVf_OOK)
1054
1055
1056 /*
1057 =for apidoc Am|void|SvOOK_off|SV * sv
1058
1059 Remove any string offset.
1060
1061 =cut
1062 */
1063
1064 #define SvOOK_off(sv)           ((void)(SvOOK(sv) && (sv_backoff(sv),0)))
1065
1066 #define SvFAKE(sv)              (SvFLAGS(sv) & SVf_FAKE)
1067 #define SvFAKE_on(sv)           (SvFLAGS(sv) |= SVf_FAKE)
1068 #define SvFAKE_off(sv)          (SvFLAGS(sv) &= ~SVf_FAKE)
1069
1070 #define SvROK(sv)               (SvFLAGS(sv) & SVf_ROK)
1071 #define SvROK_on(sv)            (SvFLAGS(sv) |= SVf_ROK)
1072 #define SvROK_off(sv)           (SvFLAGS(sv) &= ~(SVf_ROK))
1073
1074 #define SvMAGICAL(sv)           (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
1075 #define SvMAGICAL_on(sv)        (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
1076 #define SvMAGICAL_off(sv)       (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
1077
1078 #define SvGMAGICAL(sv)          (SvFLAGS(sv) & SVs_GMG)
1079 #define SvGMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_GMG)
1080 #define SvGMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_GMG)
1081
1082 #define SvSMAGICAL(sv)          (SvFLAGS(sv) & SVs_SMG)
1083 #define SvSMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_SMG)
1084 #define SvSMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_SMG)
1085
1086 #define SvRMAGICAL(sv)          (SvFLAGS(sv) & SVs_RMG)
1087 #define SvRMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_RMG)
1088 #define SvRMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_RMG)
1089
1090 /*
1091 =for apidoc Am|bool|SvAMAGIC|SV * sv
1092
1093 Returns a boolean as to whether C<sv> has overloading (active magic) enabled or
1094 not.
1095
1096 =cut
1097 */
1098
1099 #define SvAMAGIC(sv)            (SvROK(sv) && SvOBJECT(SvRV(sv)) &&     \
1100                                  HvAMAGIC(SvSTASH(SvRV(sv))))
1101
1102 /* To be used on the stashes themselves: */
1103 #define HvAMAGIC(hv)            (SvFLAGS(hv) & SVf_AMAGIC)
1104 #define HvAMAGIC_on(hv)         (SvFLAGS(hv) |= SVf_AMAGIC)
1105 #define HvAMAGIC_off(hv)        (SvFLAGS(hv) &=~ SVf_AMAGIC)
1106
1107
1108 /* "nog" means "doesn't have get magic" */
1109 #define SvPOK_nog(sv)           ((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK)
1110 #define SvIOK_nog(sv)           ((SvFLAGS(sv) & (SVf_IOK|SVs_GMG)) == SVf_IOK)
1111 #define SvUOK_nog(sv)           ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
1112 #define SvNOK_nog(sv)           ((SvFLAGS(sv) & (SVf_NOK|SVs_GMG)) == SVf_NOK)
1113 #define SvNIOK_nog(sv)          (SvNIOK(sv) && !(SvFLAGS(sv) & SVs_GMG))
1114
1115 #define SvPOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1116 #define SvIOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_IOK|SVf_THINKFIRST|SVs_GMG)) == SVf_IOK)
1117 #define SvUOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVf_THINKFIRST|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
1118 #define SvNOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_NOK|SVf_THINKFIRST|SVs_GMG)) == SVf_NOK)
1119 #define SvNIOK_nogthink(sv)     (SvNIOK(sv) && !(SvFLAGS(sv) & (SVf_THINKFIRST|SVs_GMG)))
1120
1121 #define SvPOK_utf8_nog(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1122 #define SvPOK_utf8_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1123
1124 #define SvPOK_byte_nog(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == SVf_POK)
1125 #define SvPOK_byte_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1126
1127 #define SvPOK_pure_nogthink(sv) \
1128     ((SvFLAGS(sv) & (SVf_POK|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1129 #define SvPOK_utf8_pure_nogthink(sv) \
1130     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1131 #define SvPOK_byte_pure_nogthink(sv) \
1132     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1133
1134 /*
1135 =for apidoc Am|bool|SvIsBOOL|SV* sv
1136
1137 Returns true if the SV is one of the special boolean constants (PL_sv_yes or
1138 PL_sv_no), or is a regular SV whose last assignment stored a copy of one.
1139
1140 =cut
1141 */
1142
1143 #define SvIsBOOL(sv)            Perl_sv_isbool(aTHX_ sv)
1144
1145 /*
1146 =for apidoc Am|U32|SvGAMAGIC|SV* sv
1147
1148 Returns true if the SV has get magic or
1149 overloading.  If either is true then
1150 the scalar is active data, and has the potential to return a new value every
1151 time it is accessed.  Hence you must be careful to
1152 only read it once per user logical operation and work
1153 with that returned value.  If neither is true then
1154 the scalar's value cannot change unless written to.
1155
1156 =cut
1157 */
1158
1159 #define SvGAMAGIC(sv)           (SvGMAGICAL(sv) || SvAMAGIC(sv))
1160
1161 #define Gv_AMG(stash) \
1162         (HvNAME(stash) && Gv_AMupdate(stash,FALSE) \
1163             ? 1                                     \
1164             : (HvAMAGIC_off(stash), 0))
1165
1166 #define SvWEAKREF(sv)           ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
1167                                   == (SVf_ROK|SVprv_WEAKREF))
1168 #define SvWEAKREF_on(sv)        (SvFLAGS(sv) |=  (SVf_ROK|SVprv_WEAKREF))
1169 #define SvWEAKREF_off(sv)       (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
1170
1171 #define SvPCS_IMPORTED(sv)      ((SvFLAGS(sv) & (SVf_ROK|SVprv_PCS_IMPORTED)) \
1172                                  == (SVf_ROK|SVprv_PCS_IMPORTED))
1173 #define SvPCS_IMPORTED_on(sv)   (SvFLAGS(sv) |=  (SVf_ROK|SVprv_PCS_IMPORTED))
1174 #define SvPCS_IMPORTED_off(sv)  (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_PCS_IMPORTED))
1175
1176 /*
1177 =for apidoc m|U32|SvTHINKFIRST|SV *sv
1178
1179 A quick flag check to see whether an C<sv> should be passed to C<sv_force_normal>
1180 to be "downgraded" before C<SvIVX> or C<SvPVX> can be modified directly.
1181
1182 For example, if your scalar is a reference and you want to modify the C<SvIVX>
1183 slot, you can't just do C<SvROK_off>, as that will leak the referent.
1184
1185 This is used internally by various sv-modifying functions, such as
1186 C<sv_setsv>, C<sv_setiv> and C<sv_pvn_force>.
1187
1188 One case that this does not handle is a gv without SvFAKE set.  After
1189
1190     if (SvTHINKFIRST(gv)) sv_force_normal(gv);
1191
1192 it will still be a gv.
1193
1194 C<SvTHINKFIRST> sometimes produces false positives.  In those cases
1195 C<sv_force_normal> does nothing.
1196
1197 =cut
1198 */
1199
1200 #define SvTHINKFIRST(sv)        (SvFLAGS(sv) & SVf_THINKFIRST)
1201
1202 #define SVs_PADMY               0
1203 #define SvPADMY(sv)             !(SvFLAGS(sv) & SVs_PADTMP)
1204 #ifndef PERL_CORE
1205 # define SvPADMY_on(sv)         SvPADTMP_off(sv)
1206 #endif
1207
1208 #define SvPADTMP(sv)            (SvFLAGS(sv) & (SVs_PADTMP))
1209 #define SvPADSTALE(sv)          (SvFLAGS(sv) & (SVs_PADSTALE))
1210
1211 #define SvPADTMP_on(sv)         (SvFLAGS(sv) |= SVs_PADTMP)
1212 #define SvPADTMP_off(sv)        (SvFLAGS(sv) &= ~SVs_PADTMP)
1213 #define SvPADSTALE_on(sv)       Perl_SvPADSTALE_on(MUTABLE_SV(sv))
1214 #define SvPADSTALE_off(sv)      Perl_SvPADSTALE_off(MUTABLE_SV(sv))
1215
1216 #define SvTEMP(sv)              (SvFLAGS(sv) & SVs_TEMP)
1217 #define SvTEMP_on(sv)           (SvFLAGS(sv) |= SVs_TEMP)
1218 #define SvTEMP_off(sv)          (SvFLAGS(sv) &= ~SVs_TEMP)
1219
1220 #define SvOBJECT(sv)            (SvFLAGS(sv) & SVs_OBJECT)
1221 #define SvOBJECT_on(sv)         (SvFLAGS(sv) |= SVs_OBJECT)
1222 #define SvOBJECT_off(sv)        (SvFLAGS(sv) &= ~SVs_OBJECT)
1223
1224 /*
1225 =for apidoc Am|U32|SvREADONLY|SV* sv
1226 Returns true if the argument is readonly, otherwise returns false.
1227 Exposed to perl code via Internals::SvREADONLY().
1228
1229 =for apidoc Am|U32|SvREADONLY_on|SV* sv
1230 Mark an object as readonly. Exactly what this means depends on the object
1231 type. Exposed to perl code via Internals::SvREADONLY().
1232
1233 =for apidoc Am|U32|SvREADONLY_off|SV* sv
1234 Mark an object as not-readonly. Exactly what this mean depends on the
1235 object type. Exposed to perl code via Internals::SvREADONLY().
1236
1237 =cut
1238 */
1239
1240 #define SvREADONLY(sv)          (SvFLAGS(sv) & (SVf_READONLY|SVf_PROTECT))
1241 #ifdef PERL_CORE
1242 # define SvREADONLY_on(sv)      (SvFLAGS(sv) |= (SVf_READONLY|SVf_PROTECT))
1243 # define SvREADONLY_off(sv)     (SvFLAGS(sv) &=~(SVf_READONLY|SVf_PROTECT))
1244 #else
1245 # define SvREADONLY_on(sv)      (SvFLAGS(sv) |= SVf_READONLY)
1246 # define SvREADONLY_off(sv)     (SvFLAGS(sv) &= ~SVf_READONLY)
1247 #endif
1248
1249 #define SvSCREAM(sv) ((SvFLAGS(sv) & (SVp_SCREAM|SVp_POK)) == (SVp_SCREAM|SVp_POK))
1250 #define SvSCREAM_on(sv)         (SvFLAGS(sv) |= SVp_SCREAM)
1251 #define SvSCREAM_off(sv)        (SvFLAGS(sv) &= ~SVp_SCREAM)
1252
1253 #ifndef PERL_CORE
1254 #  define SvCOMPILED(sv)        0
1255 #  define SvCOMPILED_on(sv)
1256 #  define SvCOMPILED_off(sv)
1257 #endif
1258
1259
1260 #if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
1261 #  define SvTAIL(sv)    ({ const SV *const _svtail = (const SV *)(sv);  \
1262                             assert(SvTYPE(_svtail) != SVt_PVAV);        \
1263                             assert(SvTYPE(_svtail) != SVt_PVHV);        \
1264                             assert(!(SvFLAGS(_svtail) & (SVf_NOK|SVp_NOK))); \
1265                             assert(SvVALID(_svtail));                        \
1266                             ((XPVNV*)SvANY(_svtail))->xnv_u.xnv_bm_tail;     \
1267                         })
1268 #else
1269 #  define SvTAIL(_svtail)  (((XPVNV*)SvANY(_svtail))->xnv_u.xnv_bm_tail)
1270 #endif
1271
1272 /* Does the SV have a Boyer-Moore table attached as magic?
1273  * 'VALID' is a poor name, but is kept for historical reasons.  */
1274 #define SvVALID(_svvalid) (                                  \
1275                SvPOKp(_svvalid)                              \
1276             && SvSMAGICAL(_svvalid)                          \
1277             && SvMAGIC(_svvalid)                             \
1278             && (SvMAGIC(_svvalid)->mg_type == PERL_MAGIC_bm  \
1279                 || mg_find(_svvalid, PERL_MAGIC_bm))         \
1280         )
1281
1282 #define SvRVx(sv) SvRV(sv)
1283
1284 #ifdef PERL_DEBUG_COW
1285 /* Need -0.0 for SvNVX to preserve IEEE FP "negative zero" because
1286    +0.0 + -0.0 => +0.0 but -0.0 + -0.0 => -0.0 */
1287 #  define SvIVX(sv) (0 + ((XPVIV*) SvANY(sv))->xiv_iv)
1288 #  define SvUVX(sv) (0 + ((XPVUV*) SvANY(sv))->xuv_uv)
1289 #  define SvNVX(sv) (-0.0 + ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv)
1290 #  define SvRV(sv) (0 + (sv)->sv_u.svu_rv)
1291 #  define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1292 /* Don't test the core XS code yet.  */
1293 #  if defined (PERL_CORE) && PERL_DEBUG_COW > 1
1294 #    define SvPVX(sv) (0 + (assert_(!SvREADONLY(sv)) (sv)->sv_u.svu_pv))
1295 #  else
1296 #  define SvPVX(sv) SvPVX_mutable(sv)
1297 #  endif
1298 #  define SvCUR(sv) (0 + ((XPV*) SvANY(sv))->xpv_cur)
1299 #  define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
1300 #  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1301
1302 #  define SvMAGIC(sv)   (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*)  SvANY(sv))->xmg_u.xmg_magic))
1303 #  define SvSTASH(sv)   (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*)  SvANY(sv))->xmg_stash))
1304 #else   /* Below is not PERL_DEBUG_COW */
1305 # ifdef PERL_CORE
1306 #  define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
1307 # else
1308 #  define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
1309 # endif
1310 #  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1311
1312 #  if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
1313 /* These get expanded inside other macros that already use a variable _sv  */
1314 #    define SvPVX(sv)                                                   \
1315         (*({ SV *const _svpvx = MUTABLE_SV(sv);                         \
1316             assert(PL_valid_types_PVX[SvTYPE(_svpvx) & SVt_MASK]);      \
1317             assert(!isGV_with_GP(_svpvx));                              \
1318             assert(!(SvTYPE(_svpvx) == SVt_PVIO                         \
1319                      && !(IoFLAGS(_svpvx) & IOf_FAKE_DIRP)));           \
1320             &((_svpvx)->sv_u.svu_pv);                                   \
1321          }))
1322 #   ifdef PERL_CORE
1323 #    define SvCUR(sv)                                                   \
1324         ({ const SV *const _svcur = (const SV *)(sv);                   \
1325             assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]);      \
1326             assert(!isGV_with_GP(_svcur));                              \
1327             assert(!(SvTYPE(_svcur) == SVt_PVIO                         \
1328                      && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP)));           \
1329             (((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur);             \
1330          })
1331 #   else
1332 #    define SvCUR(sv)                                                   \
1333         (*({ const SV *const _svcur = (const SV *)(sv);                 \
1334             assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]);      \
1335             assert(!isGV_with_GP(_svcur));                              \
1336             assert(!(SvTYPE(_svcur) == SVt_PVIO                         \
1337                      && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP)));           \
1338             &(((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur);            \
1339          }))
1340 #   endif
1341 #    define SvIVX(sv)                                                   \
1342         (*({ const SV *const _svivx = (const SV *)(sv);                 \
1343             assert(PL_valid_types_IVX[SvTYPE(_svivx) & SVt_MASK]);      \
1344             assert(!isGV_with_GP(_svivx));                              \
1345             &(((XPVIV*) MUTABLE_PTR(SvANY(_svivx)))->xiv_iv);           \
1346          }))
1347 #    define SvUVX(sv)                                                   \
1348         (*({ const SV *const _svuvx = (const SV *)(sv);                 \
1349             assert(PL_valid_types_IVX[SvTYPE(_svuvx) & SVt_MASK]);      \
1350             assert(!isGV_with_GP(_svuvx));                              \
1351             &(((XPVUV*) MUTABLE_PTR(SvANY(_svuvx)))->xuv_uv);           \
1352          }))
1353 #    define SvNVX(sv)                                                   \
1354         (*({ const SV *const _svnvx = (const SV *)(sv);                 \
1355             assert(PL_valid_types_NVX[SvTYPE(_svnvx) & SVt_MASK]);      \
1356             assert(!isGV_with_GP(_svnvx));                              \
1357             &(((XPVNV*) MUTABLE_PTR(SvANY(_svnvx)))->xnv_u.xnv_nv);     \
1358          }))
1359 #    define SvRV(sv)                                                    \
1360         (*({ SV *const _svrv = MUTABLE_SV(sv);                          \
1361             assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]);        \
1362             assert(!isGV_with_GP(_svrv));                               \
1363             assert(!(SvTYPE(_svrv) == SVt_PVIO                          \
1364                      && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP)));            \
1365             &((_svrv)->sv_u.svu_rv);                                    \
1366          }))
1367 #    define SvRV_const(sv)                                              \
1368         ({ const SV *const _svrv = (const SV *)(sv);                    \
1369             assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]);        \
1370             assert(!isGV_with_GP(_svrv));                               \
1371             assert(!(SvTYPE(_svrv) == SVt_PVIO                          \
1372                      && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP)));            \
1373             (_svrv)->sv_u.svu_rv;                                       \
1374          })
1375 #    define SvMAGIC(sv)                                                 \
1376         (*({ const SV *const _svmagic = (const SV *)(sv);               \
1377             assert(SvTYPE(_svmagic) >= SVt_PVMG);                       \
1378             &(((XPVMG*) MUTABLE_PTR(SvANY(_svmagic)))->xmg_u.xmg_magic); \
1379           }))
1380 #    define SvSTASH(sv)                                                 \
1381         (*({ const SV *const _svstash = (const SV *)(sv);               \
1382             assert(SvTYPE(_svstash) >= SVt_PVMG);                       \
1383             &(((XPVMG*) MUTABLE_PTR(SvANY(_svstash)))->xmg_stash);      \
1384           }))
1385 #  else     /* Below is not DEBUGGING or can't use brace groups */
1386 #    define SvPVX(sv) ((sv)->sv_u.svu_pv)
1387 #    define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
1388 #    define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
1389 #    define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
1390 #    define SvNVX(sv) ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv
1391 #    define SvRV(sv) ((sv)->sv_u.svu_rv)
1392 #    define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1393 #    define SvMAGIC(sv) ((XPVMG*)  SvANY(sv))->xmg_u.xmg_magic
1394 #    define SvSTASH(sv) ((XPVMG*)  SvANY(sv))->xmg_stash
1395 #  endif
1396 #endif
1397
1398 #ifndef PERL_POISON
1399 /* Given that these two are new, there can't be any existing code using them
1400  *  as LVALUEs, so prevent that from happening  */
1401 #  define SvPVX_mutable(sv)     ((char *)((sv)->sv_u.svu_pv))
1402 #  define SvPVX_const(sv)       ((const char*)((sv)->sv_u.svu_pv))
1403 #else
1404 /* Except for the poison code, which uses & to scribble over the pointer after
1405    free() is called.  */
1406 #  define SvPVX_mutable(sv)     ((sv)->sv_u.svu_pv)
1407 #  define SvPVX_const(sv)       ((const char*)((sv)->sv_u.svu_pv))
1408 #endif
1409
1410 #define SvIVXx(sv) SvIVX(sv)
1411 #define SvUVXx(sv) SvUVX(sv)
1412 #define SvNVXx(sv) SvNVX(sv)
1413 #define SvPVXx(sv) SvPVX(sv)
1414 #define SvLENx(sv) SvLEN(sv)
1415 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
1416
1417
1418 /* Ask a scalar nicely to try to become an IV, if possible.
1419    Not guaranteed to stay returning void */
1420 /* Macro won't actually call sv_2iv if already IOK */
1421 #define SvIV_please(sv) \
1422         STMT_START {                                                        \
1423             SV * sv_ = MUTABLE_SV(sv);                                      \
1424             if (!SvIOKp(sv_) && (SvFLAGS(sv_) & (SVf_NOK|SVf_POK)))         \
1425                 (void) SvIV(sv_);                                           \
1426         } STMT_END
1427 #define SvIV_please_nomg(sv) \
1428         (!(SvFLAGS(sv) & (SVf_IOK|SVp_IOK)) && (SvFLAGS(sv) & (SVf_NOK|SVf_POK)) \
1429             ? (sv_2iv_flags(sv, 0), SvIOK(sv))    \
1430             : SvIOK(sv))
1431
1432 #define SvIV_set(sv, val) \
1433         STMT_START { \
1434                 SV * sv_ = MUTABLE_SV(sv);                                  \
1435                 assert(PL_valid_types_IV_set[SvTYPE(sv_) & SVt_MASK]);      \
1436                 assert(!isGV_with_GP(sv_));                                 \
1437                 (((XPVIV*)  SvANY(sv_))->xiv_iv = (val));                   \
1438         } STMT_END
1439
1440 #define SvNV_set(sv, val) \
1441         STMT_START { \
1442                 SV * sv_ = MUTABLE_SV(sv);                                  \
1443                 assert(PL_valid_types_NV_set[SvTYPE(sv_) & SVt_MASK]);      \
1444                 assert(!isGV_with_GP(sv_));                                 \
1445                 (((XPVNV*)SvANY(sv_))->xnv_u.xnv_nv = (val));               \
1446         } STMT_END
1447
1448 #define SvPV_set(sv, val) \
1449         STMT_START { \
1450                 SV * sv_ = MUTABLE_SV(sv);                                  \
1451                 assert(PL_valid_types_PVX[SvTYPE(sv_) & SVt_MASK]);         \
1452                 assert(!isGV_with_GP(sv_));                                 \
1453                 assert(!(SvTYPE(sv_) == SVt_PVIO                            \
1454                      && !(IoFLAGS(sv_) & IOf_FAKE_DIRP)));                  \
1455                 ((sv_)->sv_u.svu_pv = (val));                               \
1456         } STMT_END
1457
1458 #define SvUV_set(sv, val) \
1459         STMT_START { \
1460                 SV * sv_ = MUTABLE_SV(sv);                                  \
1461                 assert(PL_valid_types_IV_set[SvTYPE(sv_) & SVt_MASK]);      \
1462                 assert(!isGV_with_GP(sv_));                                 \
1463                 (((XPVUV*)SvANY(sv_))->xuv_uv = (val));                     \
1464         } STMT_END
1465
1466 #define SvRV_set(sv, val) \
1467         STMT_START { \
1468                 SV * sv_ = MUTABLE_SV(sv);                                  \
1469                 assert(PL_valid_types_RV[SvTYPE(sv_) & SVt_MASK]);          \
1470                 assert(!isGV_with_GP(sv_));                                 \
1471                 assert(!(SvTYPE(sv_) == SVt_PVIO                            \
1472                      && !(IoFLAGS(sv_) & IOf_FAKE_DIRP)));                  \
1473                 ((sv_)->sv_u.svu_rv = (val));                               \
1474         } STMT_END
1475 #define SvMAGIC_set(sv, val) \
1476         STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1477                 (((XPVMG*)SvANY(sv))->xmg_u.xmg_magic = (val)); } STMT_END
1478 #define SvSTASH_set(sv, val) \
1479         STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1480                 (((XPVMG*)  SvANY(sv))->xmg_stash = (val)); } STMT_END
1481 #define SvCUR_set(sv, val) \
1482         STMT_START { \
1483                 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]);      \
1484                 assert(!isGV_with_GP(sv));              \
1485                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1486                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1487                 (((XPV*)  SvANY(sv))->xpv_cur = (val)); } STMT_END
1488 #define SvLEN_set(sv, val) \
1489         STMT_START { \
1490                 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]);      \
1491                 assert(!isGV_with_GP(sv));      \
1492                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1493                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1494                 (((XPV*)  SvANY(sv))->xpv_len = (val)); } STMT_END
1495 #define SvEND_set(sv, val) \
1496         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
1497                 SvCUR_set(sv, (val) - SvPVX(sv)); } STMT_END
1498
1499 /*
1500 =for apidoc Am|void|SvPV_renew|SV* sv|STRLEN len
1501 Low level micro optimization of C<L</SvGROW>>.  It is generally better to use
1502 C<SvGROW> instead.  This is because C<SvPV_renew> ignores potential issues that
1503 C<SvGROW> handles.  C<sv> needs to have a real C<PV> that is unencombered by
1504 things like COW.  Using C<SV_CHECK_THINKFIRST> or
1505 C<SV_CHECK_THINKFIRST_COW_DROP> before calling this should clean it up, but
1506 why not just use C<SvGROW> if you're not sure about the provenance?
1507
1508 =cut
1509 */
1510 #define SvPV_renew(sv,n) \
1511         STMT_START { SvLEN_set(sv, n); \
1512                 SvPV_set((sv), (MEM_WRAP_CHECK_(n,char)                 \
1513                                 (char*)saferealloc((Malloc_t)SvPVX(sv), \
1514                                                    (MEM_SIZE)((n)))));  \
1515                  } STMT_END
1516 /*
1517 =for apidoc Am|void|SvPV_shrink_to_cur|SV* sv
1518
1519 Trim any trailing unused memory in the PV of C<sv>, which needs to have a real
1520 C<PV> that is unencombered by things like COW.  Think first before using this
1521 functionality.  Is the space saving really worth giving up COW?  Will the
1522 needed size of C<sv> stay the same?
1523
1524 If the answers are both yes, then use L</C<SV_CHECK_THINKFIRST>> or
1525 L</C<SV_CHECK_THINKFIRST_COW_DROP>> before calling this.
1526
1527 =cut
1528 */
1529
1530 #define SvPV_shrink_to_cur(sv) STMT_START { \
1531                    const STRLEN _lEnGtH = SvCUR(sv) + 1; \
1532                    SvPV_renew(sv, _lEnGtH); \
1533                  } STMT_END
1534
1535 /*
1536 =for apidoc Am|void|SvPV_free|SV * sv
1537
1538 Frees the PV buffer in C<sv>, leaving things in a precarious state, so should
1539 only be used as part of a larger operation
1540
1541 =cut
1542 */
1543 #define SvPV_free(sv)                                                   \
1544     STMT_START {                                                        \
1545                      assert(SvTYPE(sv) >= SVt_PV);                      \
1546                      if (SvLEN(sv)) {                                   \
1547                          assert(!SvROK(sv));                            \
1548                          if(UNLIKELY(SvOOK(sv))) {                      \
1549                              STRLEN zok;                                \
1550                              SvOOK_offset(sv, zok);                     \
1551                              SvPV_set(sv, SvPVX_mutable(sv) - zok);     \
1552                              SvFLAGS(sv) &= ~SVf_OOK;                   \
1553                          }                                              \
1554                          Safefree(SvPVX(sv));                           \
1555                      }                                                  \
1556                  } STMT_END
1557
1558 #ifdef PERL_CORE
1559 /* Code that crops up in three places to take a scalar and ready it to hold
1560    a reference */
1561 #  define prepare_SV_for_RV(sv)                                         \
1562     STMT_START {                                                        \
1563                     if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV)    \
1564                         sv_upgrade(sv, SVt_IV);                         \
1565                     else if (SvTYPE(sv) >= SVt_PV) {                    \
1566                         SvPV_free(sv);                                  \
1567                         SvLEN_set(sv, 0);                               \
1568                         SvCUR_set(sv, 0);                               \
1569                     }                                                   \
1570                  } STMT_END
1571 #endif
1572
1573 #ifndef PERL_CORE
1574 #  define BmFLAGS(sv)           (SvTAIL(sv) ? FBMcf_TAIL : 0)
1575 #endif
1576
1577 #if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
1578 #  define BmUSEFUL(sv)                                                  \
1579         (*({ SV *const _bmuseful = MUTABLE_SV(sv);                      \
1580             assert(SvTYPE(_bmuseful) >= SVt_PVIV);                      \
1581             assert(SvVALID(_bmuseful));                                 \
1582             assert(!SvIOK(_bmuseful));                                  \
1583             &(((XPVIV*) SvANY(_bmuseful))->xiv_u.xivu_iv);              \
1584          }))
1585 #else
1586 #  define BmUSEFUL(sv)          ((XPVIV*) SvANY(sv))->xiv_u.xivu_iv
1587
1588 #endif
1589
1590 #ifndef PERL_CORE
1591 # define BmRARE(sv)     0
1592 # define BmPREVIOUS(sv) 0
1593 #endif
1594
1595 #define FmLINES(sv)     ((XPVIV*)  SvANY(sv))->xiv_iv
1596
1597 #define LvTYPE(sv)      ((XPVLV*)  SvANY(sv))->xlv_type
1598 #define LvTARG(sv)      ((XPVLV*)  SvANY(sv))->xlv_targ
1599 #define LvTARGOFF(sv)   ((XPVLV*)  SvANY(sv))->xlv_targoff
1600 #define LvSTARGOFF(sv)  ((XPVLV*)  SvANY(sv))->xlv_targoff_u.xlvu_stargoff
1601 #define LvTARGLEN(sv)   ((XPVLV*)  SvANY(sv))->xlv_targlen
1602 #define LvFLAGS(sv)     ((XPVLV*)  SvANY(sv))->xlv_flags
1603
1604 #define LVf_NEG_OFF      0x1
1605 #define LVf_NEG_LEN      0x2
1606 #define LVf_OUT_OF_RANGE 0x4
1607
1608 #define IoIFP(sv)       (sv)->sv_u.svu_fp
1609 #define IoOFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ofp
1610 #define IoDIRP(sv)      ((XPVIO*)  SvANY(sv))->xio_dirp
1611 #define IoANY(sv)       ((XPVIO*)  SvANY(sv))->xio_any
1612 #define IoLINES(sv)     ((XPVIO*)  SvANY(sv))->xiv_u.xivu_iv
1613 #define IoPAGE(sv)      ((XPVIO*)  SvANY(sv))->xio_page
1614 #define IoPAGE_LEN(sv)  ((XPVIO*)  SvANY(sv))->xio_page_len
1615 #define IoLINES_LEFT(sv)((XPVIO*)  SvANY(sv))->xio_lines_left
1616 #define IoTOP_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_top_name
1617 #define IoTOP_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_top_gv
1618 #define IoFMT_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_fmt_name
1619 #define IoFMT_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_fmt_gv
1620 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
1621 #define IoBOTTOM_GV(sv) ((XPVIO*)  SvANY(sv))->xio_bottom_gv
1622 #define IoTYPE(sv)      ((XPVIO*)  SvANY(sv))->xio_type
1623 #define IoFLAGS(sv)     ((XPVIO*)  SvANY(sv))->xio_flags
1624
1625 /* IoTYPE(sv) is a single character telling the type of I/O connection. */
1626 #define IoTYPE_RDONLY           '<'
1627 #define IoTYPE_WRONLY           '>'
1628 #define IoTYPE_RDWR             '+'
1629 #define IoTYPE_APPEND           'a'
1630 #define IoTYPE_PIPE             '|'
1631 #define IoTYPE_STD              '-'     /* stdin or stdout */
1632 #define IoTYPE_SOCKET           's'
1633 #define IoTYPE_CLOSED           ' '
1634 #define IoTYPE_IMPLICIT         'I'     /* stdin or stdout or stderr */
1635 #define IoTYPE_NUMERIC          '#'     /* fdopen */
1636
1637 /*
1638 =for apidoc_section $tainting
1639 =for apidoc Am|bool|SvTAINTED|SV* sv
1640 Checks to see if an SV is tainted.  Returns TRUE if it is, FALSE if
1641 not.
1642
1643 =for apidoc Am|void|SvTAINTED_on|SV* sv
1644 Marks an SV as tainted if tainting is enabled.
1645
1646 =for apidoc Am|void|SvTAINTED_off|SV* sv
1647 Untaints an SV.  Be I<very> careful with this routine, as it short-circuits
1648 some of Perl's fundamental security features.  XS module authors should not
1649 use this function unless they fully understand all the implications of
1650 unconditionally untainting the value.  Untainting should be done in the
1651 standard perl fashion, via a carefully crafted regexp, rather than directly
1652 untainting variables.
1653
1654 =for apidoc Am|void|SvTAINT|SV* sv
1655 Taints an SV if tainting is enabled, and if some input to the current
1656 expression is tainted--usually a variable, but possibly also implicit
1657 inputs such as locale settings.  C<SvTAINT> propagates that taintedness to
1658 the outputs of an expression in a pessimistic fashion; i.e., without paying
1659 attention to precisely which outputs are influenced by which inputs.
1660
1661 =cut
1662 */
1663
1664 #define sv_taint(sv)      sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)
1665
1666 #ifdef NO_TAINT_SUPPORT
1667 #   define SvTAINTED(sv) 0
1668 #else
1669 #   define SvTAINTED(sv)          (SvMAGICAL(sv) && sv_tainted(sv))
1670 #endif
1671 #define SvTAINTED_on(sv)  STMT_START{ if(UNLIKELY(TAINTING_get)){sv_taint(sv);}   }STMT_END
1672 #define SvTAINTED_off(sv) STMT_START{ if(UNLIKELY(TAINTING_get)){sv_untaint(sv);} }STMT_END
1673
1674 #define SvTAINT(sv)                     \
1675     STMT_START {                        \
1676         assert(TAINTING_get || !TAINT_get); \
1677         if (UNLIKELY(TAINT_get))        \
1678             SvTAINTED_on(sv);           \
1679     } STMT_END
1680
1681 /*
1682 =for apidoc_section $SV
1683 =for apidoc Am|char*|SvPV_force              |SV* sv|STRLEN len
1684 =for apidoc_item   ||SvPV_force_flags        |SV * sv|STRLEN len|U32 flags
1685 =for apidoc_item   ||SvPV_force_flags_mutable|SV * sv|STRLEN len|U32 flags
1686 =for apidoc_item   ||SvPV_force_flags_nolen  |SV * sv           |U32 flags
1687 =for apidoc_item   ||SvPV_force_mutable      |SV * sv|STRLEN len
1688 =for apidoc_item   ||SvPV_force_nolen        |SV* sv
1689 =for apidoc_item   ||SvPV_force_nomg         |SV* sv|STRLEN len
1690 =for apidoc_item   ||SvPV_force_nomg_nolen   |SV * sv
1691 =for apidoc_item   ||SvPVbyte_force          |SV * sv|STRLEN len
1692 =for apidoc_item   ||SvPVbytex_force         |SV * sv|STRLEN len
1693 =for apidoc_item   ||SvPVutf8_force          |SV * sv|STRLEN len
1694 =for apidoc_item   ||SvPVutf8x_force         |SV * sv|STRLEN len
1695 =for apidoc_item   ||SvPVx_force             |SV* sv|STRLEN len
1696
1697 These are like C<L</SvPV>>, returning the string in the SV, but will force the
1698 SV into containing a string (C<L</SvPOK>>), and only a string
1699 (C<L</SvPOK_only>>), by hook or by crook.  You need to use one of these
1700 C<force> routines if you are going to update the C<L</SvPVX>> directly.
1701
1702 Note that coercing an arbitrary scalar into a plain PV will potentially
1703 strip useful data from it.  For example if the SV was C<SvROK>, then the
1704 referent will have its reference count decremented, and the SV itself may
1705 be converted to an C<SvPOK> scalar with a string buffer containing a value
1706 such as C<"ARRAY(0x1234)">.
1707
1708 The differences between the forms are:
1709
1710 The forms with C<flags> in their names allow you to use the C<flags> parameter
1711 to specify to perform 'get' magic (by setting the C<SV_GMAGIC> flag) or to skip
1712 'get' magic (by clearing it).  The other forms do perform 'get' magic, except
1713 for the ones with C<nomg> in their names, which skip 'get' magic.
1714
1715 The forms that take a C<len> parameter will set that variable to the byte
1716 length of the resultant string (these are macros, so don't use C<&len>).
1717
1718 The forms with C<nolen> in their names indicate they don't have a C<len>
1719 parameter.  They should be used only when it is known that the PV is a C
1720 string, terminated by a NUL byte, and without intermediate NUL characters; or
1721 when you don't care about its length.
1722
1723 The forms with C<mutable> in their names are effectively the same as those without,
1724 but the name emphasizes that the string is modifiable by the caller, which it is
1725 in all the forms.
1726
1727 C<SvPVutf8_force> is like C<SvPV_force>, but converts C<sv> to UTF-8 first if
1728 not already UTF-8.
1729
1730 C<SvPVutf8x_force> is like C<SvPVutf8_force>, but guarantees to evaluate C<sv>
1731 only once; use the more efficient C<SvPVutf8_force> otherwise.
1732
1733 C<SvPVbyte_force> is like C<SvPV_force>, but converts C<sv> to byte
1734 representation first if currently encoded as UTF-8.  If the SV cannot be
1735 downgraded from UTF-8, this croaks.
1736
1737 C<SvPVbytex_force> is like C<SvPVbyte_force>, but guarantees to evaluate C<sv>
1738 only once; use the more efficient C<SvPVbyte_force> otherwise.
1739
1740 =for apidoc Am   |      char*|SvPV                 |SV* sv|STRLEN len
1741 =for apidoc_item |const char*|SvPV_const           |SV* sv|STRLEN len
1742 =for apidoc_item |      char*|SvPV_flags           |SV* sv|STRLEN len|U32 flags
1743 =for apidoc_item |const char*|SvPV_flags_const     |SV* sv|STRLEN len|U32 flags
1744 =for apidoc_item |      char*|SvPV_flags_mutable   |SV* sv|STRLEN len|U32 flags
1745 =for apidoc_item |      char*|SvPV_mutable         |SV* sv|STRLEN len
1746 =for apidoc_item |      char*|SvPV_nolen           |SV* sv
1747 =for apidoc_item |const char*|SvPV_nolen_const     |SV* sv
1748 =for apidoc_item |      char*|SvPV_nomg            |SV* sv|STRLEN len
1749 =for apidoc_item |const char*|SvPV_nomg_const      |SV* sv|STRLEN len
1750 =for apidoc_item |const char*|SvPV_nomg_const_nolen|SV* sv
1751 =for apidoc_item |      char*|SvPV_nomg_nolen      |SV* sv
1752 =for apidoc_item |      char*|SvPVbyte             |SV* sv|STRLEN len
1753 =for apidoc_item |      char*|SvPVbyte_nolen       |SV* sv
1754 =for apidoc_item |      char*|SvPVbyte_nomg        |SV* sv|STRLEN len
1755 =for apidoc_item |      char*|SvPVbyte_or_null     |SV* sv|STRLEN len
1756 =for apidoc_item |      char*|SvPVbyte_or_null_nomg|SV* sv|STRLEN len
1757 =for apidoc_item |      char*|SvPVbytex            |SV* sv|STRLEN len
1758 =for apidoc_item |      char*|SvPVbytex_nolen      |SV* sv
1759 =for apidoc_item |      char*|SvPVutf8             |SV* sv|STRLEN len
1760 =for apidoc_item |      char*|SvPVutf8_nolen       |SV* sv
1761 =for apidoc_item |      char*|SvPVutf8_nomg        |SV* sv|STRLEN len
1762 =for apidoc_item |      char*|SvPVutf8_or_null     |SV* sv|STRLEN len
1763 =for apidoc_item |      char*|SvPVutf8_or_null_nomg|SV* sv|STRLEN len
1764 =for apidoc_item |      char*|SvPVutf8x            |SV* sv|STRLEN len
1765 =for apidoc_item |      char*|SvPVx                |SV* sv|STRLEN len
1766 =for apidoc_item |const char*|SvPVx_const          |SV* sv|STRLEN len
1767 =for apidoc_item |      char*|SvPVx_nolen          |SV* sv
1768 =for apidoc_item |const char*|SvPVx_nolen_const    |SV* sv
1769
1770 These each return a pointer to the string in C<sv>, or a stringified form of
1771 C<sv> if it does not contain a string.  The SV may cache the stringified
1772 version becoming C<SvPOK>.
1773
1774 This is a very basic and common operation, so there are lots of slightly
1775 different versions of it.
1776
1777 Note that there is no guarantee that the return value of C<SvPV(sv)>, for
1778 example, is equal to C<SvPVX(sv)>, or that C<SvPVX(sv)> contains valid data, or
1779 that successive calls to C<SvPV(sv)> (or another of these forms) will return
1780 the same pointer value each time.  This is due to the way that things like
1781 overloading and Copy-On-Write are handled.  In these cases, the return value
1782 may point to a temporary buffer or similar.  If you absolutely need the
1783 C<SvPVX> field to be valid (for example, if you intend to write to it), then
1784 see C<L</SvPV_force>>.
1785
1786 The differences between the forms are:
1787
1788 The forms with neither C<byte> nor C<utf8> in their names (e.g., C<SvPV> or
1789 C<SvPV_nolen>) can expose the SV's internal string buffer. If
1790 that buffer consists entirely of bytes 0-255 and includes any bytes above
1791 127, then you B<MUST> consult C<SvUTF8> to determine the actual code points
1792 the string is meant to contain. Generally speaking, it is probably safer to
1793 prefer C<SvPVbyte>, C<SvPVutf8>, and the like. See
1794 L<perlguts/How do I pass a Perl string to a C library?> for more details.
1795
1796 The forms with C<flags> in their names allow you to use the C<flags> parameter
1797 to specify to process 'get' magic (by setting the C<SV_GMAGIC> flag) or to skip
1798 'get' magic (by clearing it).  The other forms process 'get' magic, except for
1799 the ones with C<nomg> in their names, which skip 'get' magic.
1800
1801 The forms that take a C<len> parameter will set that variable to the byte
1802 length of the resultant string (these are macros, so don't use C<&len>).
1803
1804 The forms with C<nolen> in their names indicate they don't have a C<len>
1805 parameter.  They should be used only when it is known that the PV is a C
1806 string, terminated by a NUL byte, and without intermediate NUL characters; or
1807 when you don't care about its length.
1808
1809 The forms with C<const> in their names return S<C<const char *>> so that the
1810 compiler will hopefully complain if you were to try to modify the contents of
1811 the string (unless you cast away const yourself).
1812
1813 The other forms return a mutable pointer so that the string is modifiable by
1814 the caller; this is emphasized for the ones with C<mutable> in their names.
1815
1816 As of 5.38, all forms are guaranteed to evaluate C<sv> exactly once.  For
1817 earlier Perls, use a form whose name ends with C<x> for single evaluation.
1818
1819 C<SvPVutf8> is like C<SvPV>, but converts C<sv> to UTF-8 first if not already
1820 UTF-8.  Similiarly, the other forms with C<utf8> in their names correspond to
1821 their respective forms without.
1822
1823 C<SvPVutf8_or_null> and C<SvPVutf8_or_null_nomg> don't have corresponding
1824 non-C<utf8> forms.  Instead they are like C<SvPVutf8_nomg>, but when C<sv> is
1825 undef, they return C<NULL>.
1826
1827 C<SvPVbyte> is like C<SvPV>, but converts C<sv> to byte representation first if
1828 currently encoded as UTF-8.  If C<sv> cannot be downgraded from UTF-8, it
1829 croaks.  Similiarly, the other forms with C<byte> in their names correspond to
1830 their respective forms without.
1831
1832 C<SvPVbyte_or_null> doesn't have a corresponding non-C<byte> form.  Instead it
1833 is like C<SvPVbyte>, but when C<sv> is undef, it returns C<NULL>.
1834
1835 =for apidoc      SvTRUE
1836 =for apidoc_item SvTRUE_NN
1837 =for apidoc_item SvTRUE_nomg
1838 =for apidoc_item SvTRUE_nomg_NN
1839 =for apidoc_item SvTRUEx
1840
1841 These return a boolean indicating whether Perl would evaluate the SV as true or
1842 false.  See C<L</SvOK>> for a defined/undefined test.
1843
1844 As of Perl 5.32, all are guaranteed to evaluate C<sv> only once.  Prior to that
1845 release, only C<SvTRUEx> guaranteed single evaluation; now C<SvTRUEx> is
1846 identical to C<SvTRUE>.
1847
1848 C<SvTRUE_nomg> and C<TRUE_nomg_NN> do not perform 'get' magic; the others do
1849 unless the scalar is already C<SvPOK>, C<SvIOK>, or C<SvNOK> (the public, not
1850 the private flags).
1851
1852 C<SvTRUE_NN> is like C<L</SvTRUE>>, but C<sv> is assumed to be
1853 non-null (NN).  If there is a possibility that it is NULL, use plain
1854 C<SvTRUE>.
1855
1856 C<SvTRUE_nomg_NN> is like C<L</SvTRUE_nomg>>, but C<sv> is assumed to be
1857 non-null (NN).  If there is a possibility that it is NULL, use plain
1858 C<SvTRUE_nomg>.
1859
1860 =for apidoc Am|U32|SvIsCOW|SV* sv
1861 Returns a U32 value indicating whether the SV is Copy-On-Write (either shared
1862 hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
1863 COW).
1864
1865 =for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
1866 Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
1867 scalar.
1868
1869 =cut
1870 */
1871
1872 /* To pass the action to the functions called by the following macros */
1873 typedef enum {
1874     SvPVutf8_type_,
1875     SvPVbyte_type_,
1876     SvPVnormal_type_,
1877     SvPVforce_type_,
1878     SvPVutf8_pure_type_,
1879     SvPVbyte_pure_type_
1880 } PL_SvPVtype;
1881
1882 START_EXTERN_C
1883
1884 /* When this code was written, embed.fnc could not handle function pointer
1885  * parameters; perhaps it still can't */
1886 #ifndef PERL_NO_INLINE_FUNCTIONS
1887 PERL_STATIC_INLINE char*
1888 Perl_SvPV_helper(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags, const PL_SvPVtype type, char * (*non_trivial)(pTHX_ SV *, STRLEN * const, const U32), const bool or_null, const U32 return_flags);
1889 #endif
1890
1891 END_EXTERN_C
1892
1893 /* This test is "is there a cached PV that we can use directly?"
1894  * We can if
1895  * a) SVf_POK is true and there's definitely no get magic on the scalar
1896  * b) SVp_POK is true, there's no get magic, and we know that the cached PV
1897  *    came from an IV conversion.
1898  * For the latter case, we don't set SVf_POK so that we can distinguish whether
1899  * the value originated as a string or as an integer, before we cached the
1900  * second representation. */
1901 #define SvPOK_or_cached_IV(sv) \
1902     (((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK) || ((SvFLAGS(sv) & (SVf_IOK|SVp_POK|SVs_GMG)) == (SVf_IOK|SVp_POK)))
1903
1904 #define SvPV_flags(sv, len, flags)                                          \
1905    Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_,                \
1906                     Perl_sv_2pv_flags, FALSE, 0)
1907 #define SvPV_flags_const(sv, len, flags)                                    \
1908    ((const char*) Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_, \
1909                                    Perl_sv_2pv_flags, FALSE,                \
1910                                    SV_CONST_RETURN))
1911 #define SvPV_flags_const_nolen(sv, flags)                                   \
1912    ((const char*) Perl_SvPV_helper(aTHX_ sv, NULL, flags, SvPVnormal_type_, \
1913                                    Perl_sv_2pv_flags, FALSE,                \
1914                                    SV_CONST_RETURN))
1915 #define SvPV_flags_mutable(sv, len, flags)                                  \
1916     Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_,               \
1917                      Perl_sv_2pv_flags, FALSE, SV_MUTABLE_RETURN)
1918
1919 #define SvPV_nolen(sv)                                                      \
1920     Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVnormal_type_,           \
1921                      Perl_sv_2pv_flags, FALSE, 0)
1922
1923 #define SvPV_nolen_const(sv)  SvPV_flags_const_nolen(sv, SV_GMAGIC)
1924
1925 #define SvPV(sv, len)               SvPV_flags(sv, len, SV_GMAGIC)
1926 #define SvPV_const(sv, len)         SvPV_flags_const(sv, len, SV_GMAGIC)
1927 #define SvPV_mutable(sv, len)       SvPV_flags_mutable(sv, len, SV_GMAGIC)
1928
1929 #define SvPV_nomg_nolen(sv)                                                 \
1930     Perl_SvPV_helper(aTHX_ sv, NULL, 0, SvPVnormal_type_,Perl_sv_2pv_flags, \
1931                      FALSE, 0)
1932 #define SvPV_nomg(sv, len)          SvPV_flags(sv, len, 0)
1933 #define SvPV_nomg_const(sv, len)    SvPV_flags_const(sv, len, 0)
1934 #define SvPV_nomg_const_nolen(sv)   SvPV_flags_const_nolen(sv, 0)
1935
1936 #define SvPV_force_flags(sv, len, flags)                                    \
1937     Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVforce_type_,                \
1938                      Perl_sv_pvn_force_flags, FALSE, 0)
1939 #define SvPV_force_flags_nolen(sv, flags)                                   \
1940     Perl_SvPV_helper(aTHX_ sv, NULL, flags, SvPVforce_type_,                \
1941                      Perl_sv_pvn_force_flags, FALSE, 0)
1942 #define SvPV_force_flags_mutable(sv, len, flags)                            \
1943     Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVforce_type_,                \
1944                      Perl_sv_pvn_force_flags, FALSE, SV_MUTABLE_RETURN)
1945
1946 #define SvPV_force(sv, len)         SvPV_force_flags(sv, len, SV_GMAGIC)
1947 #define SvPV_force_nolen(sv)        SvPV_force_flags_nolen(sv, SV_GMAGIC)
1948 #define SvPV_force_mutable(sv, len) SvPV_force_flags_mutable(sv, len, SV_GMAGIC)
1949
1950 /* "_nomg" in these defines means no mg_get() */
1951 #define SvPV_force_nomg(sv, len)    SvPV_force_flags(sv, len, 0)
1952 #define SvPV_force_nomg_nolen(sv)   SvPV_force_flags_nolen(sv, 0)
1953
1954 #define SvPVutf8(sv, len)                                                   \
1955     Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVutf8_type_,             \
1956                      Perl_sv_2pvutf8_flags, FALSE, 0)
1957 #define SvPVutf8_nomg(sv, len)                                              \
1958     Perl_SvPV_helper(aTHX_ sv, NULL, 0, SvPVutf8_type_,                     \
1959                      Perl_sv_2pvutf8_flags, FALSE, 0)
1960 #define SvPVutf8_nolen(sv)                                                  \
1961     Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVutf8_type_,             \
1962                      Perl_sv_2pvutf8_flags, FALSE, 0)
1963 #define SvPVutf8_or_null(sv, len)                                           \
1964     Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVutf8_type_,             \
1965                      Perl_sv_2pvutf8_flags, TRUE, 0)
1966 #define SvPVutf8_or_null_nomg(sv, len)                                      \
1967     Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVutf8_type_,                     \
1968                      Perl_sv_2pvutf8_flags, TRUE, 0)
1969
1970 #define SvPVbyte(sv, len)                                                   \
1971     Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVbyte_type_,             \
1972                      Perl_sv_2pvbyte_flags, FALSE, 0)
1973 #define SvPVbyte_nomg(sv, len)                                              \
1974     Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_type_,                     \
1975                      Perl_sv_2pvbyte_flags, FALSE, 0)
1976 #define SvPVbyte_nolen(sv)                                                  \
1977     Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVbyte_type_,             \
1978                      Perl_sv_2pvbyte_flags, FALSE, 0)
1979 #define SvPVbyte_or_null(sv, len)                                           \
1980     Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVbyte_type_,             \
1981                      Perl_sv_2pvbyte_flags, TRUE, 0)
1982 #define SvPVbyte_or_null_nomg(sv, len)                                      \
1983     Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_type_,                     \
1984                      Perl_sv_2pvbyte_flags, TRUE, 0)
1985
1986 #define SvPVutf8_force(sv, len)                                             \
1987     Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVutf8_pure_type_,                \
1988                      Perl_sv_pvutf8n_force_wrapper, FALSE, 0)
1989
1990 #define SvPVbyte_force(sv, len)                                             \
1991     Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_pure_type_,                \
1992                      Perl_sv_pvbyten_force_wrapper, FALSE, 0)
1993
1994 /* define FOOx(): Before FOO(x) was inlined, these were idempotent versions of
1995  * FOO(). */
1996
1997 #define SvPVx_force(sv, len) sv_pvn_force(sv, &len)
1998 #define SvPVutf8x_force(sv, len) sv_pvutf8n_force(sv, &len)
1999 #define SvPVbytex_force(sv, len) sv_pvbyten_force(sv, &len)
2000
2001 #define SvTRUEx(sv)        SvTRUE(sv)
2002 #define SvTRUEx_nomg(sv)   SvTRUE_nomg(sv)
2003 #define SvTRUE_nomg_NN(sv) SvTRUE_common(sv, TRUE)
2004
2005 #  define SvIVx(sv) SvIV(sv)
2006 #  define SvUVx(sv) SvUV(sv)
2007 #  define SvNVx(sv) SvNV(sv)
2008
2009 #if defined(PERL_USE_GCC_BRACE_GROUPS)
2010
2011 #  define SvPVx(sv, len) ({SV *_sv = (sv); SvPV(_sv, len); })
2012 #  define SvPVx_const(sv, len) ({SV *_sv = (sv); SvPV_const(_sv, len); })
2013 #  define SvPVx_nolen(sv) ({SV *_sv = (sv); SvPV_nolen(_sv); })
2014 #  define SvPVx_nolen_const(sv) ({SV *_sv = (sv); SvPV_nolen_const(_sv); })
2015 #  define SvPVutf8x(sv, len) ({SV *_sv = (sv); SvPVutf8(_sv, len); })
2016 #  define SvPVbytex(sv, len) ({SV *_sv = (sv); SvPVbyte(_sv, len); })
2017 #  define SvPVbytex_nolen(sv) ({SV *_sv = (sv); SvPVbyte_nolen(_sv); })
2018
2019 #else /* __GNUC__ */
2020
2021 /* These inlined macros use globals, which will require a thread
2022  * declaration in user code, so we avoid them under threads */
2023
2024 #  define SvPVx(sv, len) ((PL_Sv = (sv)), SvPV(PL_Sv, len))
2025 #  define SvPVx_const(sv, len) ((PL_Sv = (sv)), SvPV_const(PL_Sv, len))
2026 #  define SvPVx_nolen(sv) ((PL_Sv = (sv)), SvPV_nolen(PL_Sv))
2027 #  define SvPVx_nolen_const(sv) ((PL_Sv = (sv)), SvPV_nolen_const(PL_Sv))
2028 #  define SvPVutf8x(sv, len) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, len))
2029 #  define SvPVbytex(sv, len) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, len))
2030 #  define SvPVbytex_nolen(sv) ((PL_Sv = (sv)), SvPVbyte_nolen(PL_Sv))
2031 #endif /* __GNU__ */
2032
2033 #define SvIsCOW(sv)              (SvFLAGS(sv) & SVf_IsCOW)
2034 #define SvIsCOW_on(sv)           (SvFLAGS(sv) |= SVf_IsCOW)
2035 #define SvIsCOW_off(sv)          (SvFLAGS(sv) &= ~(SVf_IsCOW|SVppv_STATIC))
2036 #define SvIsCOW_shared_hash(sv)  ((SvFLAGS(sv) & (SVf_IsCOW|SVppv_STATIC)) == (SVf_IsCOW) && SvLEN(sv) == 0)
2037 #define SvIsCOW_static(sv)       ((SvFLAGS(sv) & (SVf_IsCOW|SVppv_STATIC)) == (SVf_IsCOW|SVppv_STATIC))
2038
2039 #define SvSHARED_HEK_FROM_PV(pvx) \
2040         ((struct hek*)(pvx - STRUCT_OFFSET(struct hek, hek_key)))
2041 /*
2042 =for apidoc Am|struct hek*|SvSHARED_HASH|SV * sv
2043 Returns the hash for C<sv> created by C<L</newSVpvn_share>>.
2044
2045 =cut
2046 */
2047 #define SvSHARED_HASH(sv) (0 + SvSHARED_HEK_FROM_PV(SvPVX_const(sv))->hek_hash)
2048
2049 /* flag values for sv_*_flags functions */
2050 #define SV_UTF8_NO_ENCODING     0       /* No longer used */
2051
2052 /*
2053 =for apidoc AmnhD||SV_UTF8_NO_ENCODING
2054
2055 =cut
2056 */
2057
2058 #define SV_IMMEDIATE_UNREF      1
2059 #define SV_GMAGIC               2
2060 #define SV_COW_DROP_PV          4
2061 #define SV_NOSTEAL              16
2062 #define SV_CONST_RETURN         32
2063 #define SV_MUTABLE_RETURN       64
2064 #define SV_SMAGIC               128
2065 #define SV_HAS_TRAILING_NUL     256
2066 #define SV_COW_SHARED_HASH_KEYS 512
2067 /* This one is only enabled for PERL_OLD_COPY_ON_WRITE */
2068 /* XXX This flag actually enabled for any COW.  But it appears not to do
2069        anything.  Can we just remove it?  Or will it serve some future
2070        purpose.  */
2071 #define SV_COW_OTHER_PVS        1024
2072 /* Make sv_2pv_flags return NULL if something is undefined.  */
2073 #define SV_UNDEF_RETURNS_NULL   2048
2074 /* Tell sv_utf8_upgrade() to not check to see if an upgrade is really needed.
2075  * This is used when the caller has already determined it is, and avoids
2076  * redundant work */
2077 #define SV_FORCE_UTF8_UPGRADE   4096
2078 /* if (after resolving magic etc), the SV is found to be overloaded,
2079  * don't call the overload magic, just return as-is */
2080 #define SV_SKIP_OVERLOAD        8192
2081 #define SV_CATBYTES             16384
2082 #define SV_CATUTF8              32768
2083
2084 /* The core is safe for this COW optimisation. XS code on CPAN may not be.
2085    So only default to doing the COW setup if we're in the core.
2086  */
2087 #ifdef PERL_CORE
2088 #  ifndef SV_DO_COW_SVSETSV
2089 #    define SV_DO_COW_SVSETSV   SV_COW_SHARED_HASH_KEYS|SV_COW_OTHER_PVS
2090 #  endif
2091 #endif
2092
2093 #ifndef SV_DO_COW_SVSETSV
2094 #  define SV_DO_COW_SVSETSV     0
2095 #endif
2096
2097
2098 #define sv_unref(sv)            sv_unref_flags(sv, 0)
2099 #define sv_force_normal(sv)     sv_force_normal_flags(sv, 0)
2100 #define sv_usepvn(sv, p, l)     sv_usepvn_flags(sv, p, l, 0)
2101 #define sv_usepvn_mg(sv, p, l)  sv_usepvn_flags(sv, p, l, SV_SMAGIC)
2102
2103 /*
2104 =for apidoc Am|void|SV_CHECK_THINKFIRST_COW_DROP|SV * sv
2105
2106 Call this when you are about to replace the PV value in C<sv>, which is
2107 potentially copy-on-write.  It stops any sharing with other SVs, so that no
2108 Copy on Write (COW) actually happens.  This COW would be useless, as it would
2109 immediately get changed to something else.  This function also removes any
2110 other encumbrances that would be problematic when changing C<sv>.
2111
2112 =cut
2113 */
2114
2115 #define SV_CHECK_THINKFIRST_COW_DROP(sv) if (SvTHINKFIRST(sv)) \
2116                                     sv_force_normal_flags(sv, SV_COW_DROP_PV)
2117
2118 #ifdef PERL_COPY_ON_WRITE
2119 #   define SvCANCOW(sv)                                     \
2120         (SvIsCOW(sv)                                         \
2121          ? SvLEN(sv) ? CowREFCNT(sv) != SV_COW_REFCNT_MAX : 1 \
2122          : (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS       \
2123                             && SvCUR(sv)+1 < SvLEN(sv))
2124    /* Note: To allow 256 COW "copies", a refcnt of 0 means 1. */
2125 #   define CowREFCNT(sv)        (*(U8 *)(SvPVX(sv)+SvLEN(sv)-1))
2126 #   define SV_COW_REFCNT_MAX    nBIT_UMAX(sizeof(U8) * CHARBITS)
2127 #   define CAN_COW_MASK (SVf_POK|SVf_ROK|SVp_POK|SVf_FAKE| \
2128                          SVf_OOK|SVf_BREAK|SVf_READONLY|SVf_PROTECT)
2129 #endif
2130
2131 #define CAN_COW_FLAGS   (SVp_POK|SVf_POK)
2132
2133 /*
2134 =for apidoc Am|void|SV_CHECK_THINKFIRST|SV * sv
2135
2136 Remove any encumbrances from C<sv>, that need to be taken care of before it
2137 is modifiable.  For example if it is Copy on Write (COW), now is the time to
2138 make that copy.
2139
2140 If you know that you are about to change the PV value of C<sv>, instead use
2141 L</C<SV_CHECK_THINKFIRST_COW_DROP>> to avoid the write that would be
2142 immediately written again.
2143
2144 =cut
2145 */
2146 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) \
2147                                     sv_force_normal_flags(sv, 0)
2148
2149
2150 /* all these 'functions' are now just macros */
2151
2152 #define sv_pv(sv) SvPV_nolen(sv)
2153 #define sv_pvutf8(sv) SvPVutf8_nolen(sv)
2154 #define sv_pvbyte(sv) SvPVbyte_nolen(sv)
2155
2156 #define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
2157 #define sv_utf8_upgrade_flags(sv, flags) sv_utf8_upgrade_flags_grow(sv, flags, 0)
2158 #define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
2159 #define sv_utf8_downgrade(sv, fail_ok) sv_utf8_downgrade_flags(sv, fail_ok, SV_GMAGIC)
2160 #define sv_utf8_downgrade_nomg(sv, fail_ok) sv_utf8_downgrade_flags(sv, fail_ok, 0)
2161 #define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
2162 #define sv_catpv_nomg(dsv, sstr) sv_catpv_flags(dsv, sstr, 0)
2163 #define sv_setsv(dsv, ssv) \
2164         sv_setsv_flags(dsv, ssv, SV_GMAGIC|SV_DO_COW_SVSETSV)
2165 #define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_DO_COW_SVSETSV)
2166 #define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
2167 #define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
2168 #define sv_catsv_mg(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC|SV_SMAGIC)
2169 #define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
2170 #define sv_catpvn_mg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC|SV_SMAGIC);
2171 #define sv_copypv(dsv, ssv) sv_copypv_flags(dsv, ssv, SV_GMAGIC)
2172 #define sv_copypv_nomg(dsv, ssv) sv_copypv_flags(dsv, ssv, 0)
2173 #define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
2174 #define sv_2pv_nolen(sv) sv_2pv(sv, 0)
2175 #define sv_2pvbyte(sv, lp) sv_2pvbyte_flags(sv, lp, SV_GMAGIC)
2176 #define sv_2pvbyte_nolen(sv) sv_2pvbyte(sv, 0)
2177 #define sv_2pvutf8(sv, lp) sv_2pvutf8_flags(sv, lp, SV_GMAGIC)
2178 #define sv_2pvutf8_nolen(sv) sv_2pvutf8(sv, 0)
2179 #define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
2180 #define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
2181 #define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
2182 #define sv_2iv(sv) sv_2iv_flags(sv, SV_GMAGIC)
2183 #define sv_2uv(sv) sv_2uv_flags(sv, SV_GMAGIC)
2184 #define sv_2nv(sv) sv_2nv_flags(sv, SV_GMAGIC)
2185 #define sv_eq(sv1, sv2) sv_eq_flags(sv1, sv2, SV_GMAGIC)
2186 #define sv_cmp(sv1, sv2) sv_cmp_flags(sv1, sv2, SV_GMAGIC)
2187 #define sv_cmp_locale(sv1, sv2) sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC)
2188 #define sv_numeq(sv1, sv2) sv_numeq_flags(sv1, sv2, SV_GMAGIC)
2189 #define sv_streq(sv1, sv2) sv_streq_flags(sv1, sv2, SV_GMAGIC)
2190 #define sv_collxfrm(sv, nxp) sv_collxfrm_flags(sv, nxp, SV_GMAGIC)
2191 #define sv_2bool(sv) sv_2bool_flags(sv, SV_GMAGIC)
2192 #define sv_2bool_nomg(sv) sv_2bool_flags(sv, 0)
2193 #define sv_insert(bigstr, offset, len, little, littlelen)               \
2194         Perl_sv_insert_flags(aTHX_ (bigstr),(offset), (len), (little),  \
2195                              (littlelen), SV_GMAGIC)
2196 #define sv_mortalcopy(sv) \
2197         Perl_sv_mortalcopy_flags(aTHX_ sv, SV_GMAGIC|SV_DO_COW_SVSETSV)
2198 #define sv_cathek(sv,hek)                                           \
2199         STMT_START {                                                 \
2200             HEK * const bmxk = hek;                                   \
2201             sv_catpvn_flags(sv, HEK_KEY(bmxk), HEK_LEN(bmxk),          \
2202                             HEK_UTF8(bmxk) ? SV_CATUTF8 : SV_CATBYTES); \
2203         } STMT_END
2204
2205 /* Should be named SvCatPVN_utf8_upgrade? */
2206 #define sv_catpvn_nomg_utf8_upgrade(dsv, sstr, slen, nsv)       \
2207         STMT_START {                                    \
2208             if (!(nsv))                                 \
2209                 nsv = newSVpvn_flags(sstr, slen, SVs_TEMP);     \
2210             else                                        \
2211                 sv_setpvn(nsv, sstr, slen);             \
2212             SvUTF8_off(nsv);                            \
2213             sv_utf8_upgrade(nsv);                       \
2214             sv_catsv_nomg(dsv, nsv);                    \
2215         } STMT_END
2216 #define sv_catpvn_nomg_maybeutf8(dsv, sstr, len, is_utf8) \
2217         sv_catpvn_flags(dsv, sstr, len, (is_utf8)?SV_CATUTF8:SV_CATBYTES)
2218
2219 #if defined(PERL_CORE) || defined(PERL_EXT)
2220 # define sv_or_pv_len_utf8(sv, pv, bytelen)           \
2221     (SvGAMAGIC(sv)                                     \
2222         ? utf8_length((U8 *)(pv), (U8 *)(pv)+(bytelen)) \
2223         : sv_len_utf8(sv))
2224 #endif
2225
2226 /*
2227 =for apidoc newRV
2228 =for apidoc_item ||newRV_inc|
2229
2230 These are identical.  They create an RV wrapper for an SV.  The reference count
2231 for the original SV is incremented.
2232
2233 =cut
2234 */
2235
2236 #define newRV_inc(sv)   newRV(sv)
2237
2238 /* the following macros update any magic values this C<sv> is associated with */
2239
2240 /*
2241 =for apidoc_section $SV
2242
2243 =for apidoc Am|void|SvSETMAGIC|SV* sv
2244 Invokes C<L</mg_set>> on an SV if it has 'set' magic.  This is necessary
2245 after modifying a scalar, in case it is a magical variable like C<$|>
2246 or a tied variable (it calls C<STORE>).  This macro evaluates its
2247 argument more than once.
2248
2249 =for apidoc Am|void|SvSetMagicSV|SV* dsv|SV* ssv
2250 =for apidoc_item    SvSetMagicSV_nosteal
2251 =for apidoc_item    SvSetSV
2252 =for apidoc_item    SvSetSV_nosteal
2253
2254 if C<dsv> is the same as C<ssv>, these do nothing.  Otherwise they all call
2255 some form of C<L</sv_setsv>>.  They may evaluate their arguments more than
2256 once.
2257
2258 The only differences are:
2259
2260 C<SvSetMagicSV> and C<SvSetMagicSV_nosteal> perform any required 'set' magic
2261 afterwards on the destination SV; C<SvSetSV> and C<SvSetSV_nosteal> do not.
2262
2263 C<SvSetSV_nosteal> C<SvSetMagicSV_nosteal> call a non-destructive version of
2264 C<sv_setsv>.
2265
2266 =for apidoc Am|void|SvSHARE|SV* sv
2267 Arranges for C<sv> to be shared between threads if a suitable module
2268 has been loaded.
2269
2270 =for apidoc Am|void|SvLOCK|SV* sv
2271 Arranges for a mutual exclusion lock to be obtained on C<sv> if a suitable module
2272 has been loaded.
2273
2274 =for apidoc Am|void|SvUNLOCK|SV* sv
2275 Releases a mutual exclusion lock on C<sv> if a suitable module
2276 has been loaded.
2277
2278 =for apidoc_section $SV
2279
2280 =for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
2281 Expands the character buffer in the SV so that it has room for the
2282 indicated number of bytes (remember to reserve space for an extra trailing
2283 C<NUL> character).  Calls C<sv_grow> to perform the expansion if necessary.
2284 Returns a pointer to the character
2285 buffer.  SV must be of type >= C<SVt_PV>.  One
2286 alternative is to call C<sv_grow> if you are not sure of the type of SV.
2287
2288 You might mistakenly think that C<len> is the number of bytes to add to the
2289 existing size, but instead it is the total size C<sv> should be.
2290
2291 =for apidoc Am|char *|SvPVCLEAR|SV* sv
2292 Ensures that sv is a SVt_PV and that its SvCUR is 0, and that it is
2293 properly null terminated. Equivalent to sv_setpvs(""), but more efficient.
2294
2295 =cut
2296 */
2297
2298 #define SvPVCLEAR(sv) sv_setpv_bufsize(sv,0,0)
2299 #define SvSHARE(sv) PL_sharehook(aTHX_ sv)
2300 #define SvLOCK(sv) PL_lockhook(aTHX_ sv)
2301 #define SvUNLOCK(sv) PL_unlockhook(aTHX_ sv)
2302 #define SvDESTROYABLE(sv) PL_destroyhook(aTHX_ sv)
2303
2304 #define SvSETMAGIC(x) STMT_START { if (UNLIKELY(SvSMAGICAL(x))) mg_set(x); } STMT_END
2305
2306 #define SvSetSV_and(dst,src,finally) \
2307         STMT_START {                                    \
2308             SV * src_ = src;                            \
2309             SV * dst_ = dst;                            \
2310             if (LIKELY((dst_) != (src_))) {             \
2311                 sv_setsv(dst_, src_);                   \
2312                 finally;                                \
2313             }                                           \
2314         } STMT_END
2315
2316 #define SvSetSV_nosteal_and(dst,src,finally) \
2317         STMT_START {                                    \
2318             SV * src_ = src;                            \
2319             SV * dst_ = dst;                            \
2320             if (LIKELY((dst_) != (src_))) {             \
2321                 sv_setsv_flags(dst_, src_,              \
2322                         SV_GMAGIC                       \
2323                       | SV_NOSTEAL                      \
2324                       | SV_DO_COW_SVSETSV);             \
2325                 finally;                                \
2326             }                                           \
2327         } STMT_END
2328
2329 #define SvSetSV(dst,src) \
2330                 SvSetSV_and(dst,src,/*nothing*/;)
2331 #define SvSetSV_nosteal(dst,src) \
2332                 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
2333
2334 #define SvSetMagicSV(dst,src) \
2335                 SvSetSV_and(dst,src,SvSETMAGIC(dst))
2336 #define SvSetMagicSV_nosteal(dst,src) \
2337                 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
2338
2339
2340 #if !defined(SKIP_DEBUGGING)
2341 #define SvPEEK(sv) sv_peek(sv)
2342 #else
2343 #define SvPEEK(sv) ""
2344 #endif
2345
2346 /* Is this a per-interpreter immortal SV (rather than global)?
2347  * These should either occupy adjacent entries in the interpreter struct
2348  * (MULTIPLICITY) or adjacent elements of PL_sv_immortals[] otherwise.
2349  * The unsigned (Size_t) cast avoids the need for a second < 0 condition.
2350  */
2351 #define SvIMMORTAL_INTERP(sv) ((Size_t)((sv) - &PL_sv_yes) < 4)
2352
2353 /* Does this immortal have a true value? Currently only PL_sv_yes does. */
2354 #define SvIMMORTAL_TRUE(sv)   ((sv) == &PL_sv_yes)
2355
2356 /* the SvREADONLY() test is to quickly reject most SVs */
2357 #define SvIMMORTAL(sv) \
2358                 (  SvREADONLY(sv) \
2359                 && (SvIMMORTAL_INTERP(sv) || (sv) == &PL_sv_placeholder))
2360
2361 #ifdef DEBUGGING
2362    /* exercise the immortal resurrection code in sv_free2() */
2363 #  define SvREFCNT_IMMORTAL 1000
2364 #else
2365 #  define SvREFCNT_IMMORTAL ((~(U32)0)/2)
2366 #endif
2367
2368 /*
2369 =for apidoc Am|SV *|boolSV|bool b
2370
2371 Returns a true SV if C<b> is a true value, or a false SV if C<b> is 0.
2372
2373 See also C<L</PL_sv_yes>> and C<L</PL_sv_no>>.
2374
2375 =cut
2376 */
2377
2378 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
2379
2380 /*
2381 =for apidoc Am|void|sv_setbool|SV *sv|bool b
2382 =for apidoc_item |void|sv_setbool_mg|SV *sv|bool b
2383
2384 These set an SV to a true or false boolean value, upgrading first if necessary.
2385
2386 They differ only in that C<sv_setbool_mg> handles 'set' magic; C<sv_setbool>
2387 does not.
2388
2389 =cut
2390 */
2391
2392 #define sv_setbool(sv, b)     sv_setsv(sv, boolSV(b))
2393 #define sv_setbool_mg(sv, b)  sv_setsv_mg(sv, boolSV(b))
2394
2395 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
2396 /* If I give every macro argument a different name, then there won't be bugs
2397    where nested macros get confused. Been there, done that.  */
2398 /*
2399 =for apidoc Am|bool|isGV_with_GP|SV * sv
2400 Returns a boolean as to whether or not C<sv> is a GV with a pointer to a GP
2401 (glob pointer).
2402
2403 =cut
2404 */
2405 #define isGV_with_GP(pwadak) \
2406         (((SvFLAGS(pwadak) & (SVp_POK|SVpgv_GP)) == SVpgv_GP)   \
2407         && (SvTYPE(pwadak) == SVt_PVGV || SvTYPE(pwadak) == SVt_PVLV))
2408
2409 #define isGV_with_GP_on(sv)                                            \
2410     STMT_START {                                                       \
2411         SV * sv_ = MUTABLE_SV(sv);                                     \
2412         assert (SvTYPE(sv_) == SVt_PVGV || SvTYPE(sv_) == SVt_PVLV);   \
2413         assert (!SvPOKp(sv_));                                         \
2414         assert (!SvIOKp(sv_));                                         \
2415         (SvFLAGS(sv_) |= SVpgv_GP);                                    \
2416     } STMT_END
2417
2418 #define isGV_with_GP_off(sv)                                           \
2419     STMT_START {                                                       \
2420         SV * sv_ = MUTABLE_SV(sv);                                     \
2421         assert (SvTYPE(sv_) == SVt_PVGV || SvTYPE(sv_) == SVt_PVLV);   \
2422         assert (!SvPOKp(sv_));                                         \
2423         assert (!SvIOKp(sv_));                                         \
2424         (SvFLAGS(sv_) &= ~SVpgv_GP);                                   \
2425     } STMT_END
2426
2427 #ifdef PERL_CORE
2428 # define isGV_or_RVCV(kadawp) \
2429     (isGV(kadawp) || (SvROK(kadawp) && SvTYPE(SvRV(kadawp)) == SVt_PVCV))
2430 #endif
2431 #define isREGEXP(sv) \
2432     (SvTYPE(sv) == SVt_REGEXP                                 \
2433      || (SvFLAGS(sv) & (SVTYPEMASK|SVpgv_GP|SVf_FAKE))        \
2434          == (SVt_PVLV|SVf_FAKE))
2435
2436
2437 #ifdef PERL_ANY_COW
2438 # define SvGROW(sv,len) \
2439         (SvIsCOW(sv) || SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
2440 #else
2441 # define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
2442 #endif
2443 #define SvGROW_mutable(sv,len) \
2444     (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
2445 #define Sv_Grow sv_grow
2446
2447 #define CLONEf_COPY_STACKS 1
2448 #define CLONEf_KEEP_PTR_TABLE 2
2449 #define CLONEf_CLONE_HOST 4
2450 #define CLONEf_JOIN_IN 8
2451
2452 struct clone_params {
2453   AV* stashes;
2454   UV  flags;
2455   PerlInterpreter *proto_perl;
2456   PerlInterpreter *new_perl;
2457   AV *unreferenced;
2458 };
2459
2460 /* SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
2461    with SvTEMP_off and SvTEMP_on round a call to sv_setsv.  */
2462 #define newSVsv(sv) newSVsv_flags((sv), SV_GMAGIC|SV_NOSTEAL)
2463 #define newSVsv_nomg(sv) newSVsv_flags((sv), SV_NOSTEAL)
2464
2465 /*
2466 =for apidoc Am|SV*|newSVpvn_utf8|const char* s|STRLEN len|U32 utf8
2467
2468 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
2469 characters) into it.  If C<utf8> is true, calls
2470 C<SvUTF8_on> on the new SV.  Implemented as a wrapper around C<newSVpvn_flags>.
2471
2472 =cut
2473 */
2474
2475 #define newSVpvn_utf8(s, len, u) newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
2476
2477 /*
2478 =for apidoc Amx|SV*|newSVpadname|PADNAME *pn
2479
2480 Creates a new SV containing the pad name.
2481
2482 =cut
2483 */
2484
2485 #define newSVpadname(pn) newSVpvn_utf8(PadnamePV(pn), PadnameLEN(pn), TRUE)
2486
2487 /*
2488 =for apidoc Am|void|SvOOK_offset|SV*sv|STRLEN len
2489
2490 Reads into C<len> the offset from C<SvPVX> back to the true start of the
2491 allocated buffer, which will be non-zero if C<sv_chop> has been used to
2492 efficiently remove characters from start of the buffer.  Implemented as a
2493 macro, which takes the address of C<len>, which must be of type C<STRLEN>.
2494 Evaluates C<sv> more than once.  Sets C<len> to 0 if C<SvOOK(sv)> is false.
2495
2496 =cut
2497 */
2498
2499 #ifdef DEBUGGING
2500 /* Does the bot know something I don't?
2501 10:28 <@Nicholas> metabatman
2502 10:28 <+meta> Nicholas: crash
2503 */
2504 #  define SvOOK_offset(sv, offset) STMT_START {                         \
2505         STATIC_ASSERT_STMT(sizeof(offset) == sizeof(STRLEN));           \
2506         if (SvOOK(sv)) {                                                \
2507             const U8 *_crash = (U8*)SvPVX_const(sv);                    \
2508             (offset) = *--_crash;                                       \
2509             if (!(offset)) {                                            \
2510                 _crash -= sizeof(STRLEN);                               \
2511                 Copy(_crash, (U8 *)&(offset), sizeof(STRLEN), U8);      \
2512             }                                                           \
2513             {                                                           \
2514                 /* Validate the preceding buffer's sentinels to         \
2515                    verify that no-one is using it.  */                  \
2516                 const U8 *const _bonk = (U8*)SvPVX_const(sv) - (offset);\
2517                 while (_crash > _bonk) {                                \
2518                     --_crash;                                           \
2519                     assert (*_crash == (U8)PTR2UV(_crash));             \
2520                 }                                                       \
2521             }                                                           \
2522         } else {                                                        \
2523             (offset) = 0;                                               \
2524         }                                                               \
2525     } STMT_END
2526 #else
2527     /* This is the same code, but avoids using any temporary variables:  */
2528 #  define SvOOK_offset(sv, offset) STMT_START {                         \
2529         STATIC_ASSERT_STMT(sizeof(offset) == sizeof(STRLEN));           \
2530         if (SvOOK(sv)) {                                                \
2531             (offset) = ((U8*)SvPVX_const(sv))[-1];                      \
2532             if (!(offset)) {                                            \
2533                 Copy(SvPVX_const(sv) - 1 - sizeof(STRLEN),              \
2534                      (U8*)&(offset), sizeof(STRLEN), U8);               \
2535             }                                                           \
2536         } else {                                                        \
2537             (offset) = 0;                                               \
2538         }                                                               \
2539     } STMT_END
2540 #endif
2541
2542 /*
2543 =for apidoc_section $io
2544 =for apidoc newIO
2545
2546 Create a new IO, setting the reference count to 1.
2547
2548 =cut
2549 */
2550 #define newIO() MUTABLE_IO(newSV_type(SVt_PVIO))
2551
2552 #if defined(PERL_CORE) || defined(PERL_EXT)
2553
2554 #  define SV_CONST(name) \
2555         PL_sv_consts[SV_CONST_##name] \
2556                 ? PL_sv_consts[SV_CONST_##name] \
2557                 : (PL_sv_consts[SV_CONST_##name] = newSVpv_share(#name, 0))
2558
2559 #  define SV_CONST_TIESCALAR 0
2560 #  define SV_CONST_TIEARRAY 1
2561 #  define SV_CONST_TIEHASH 2
2562 #  define SV_CONST_TIEHANDLE 3
2563
2564 #  define SV_CONST_FETCH 4
2565 #  define SV_CONST_FETCHSIZE 5
2566 #  define SV_CONST_STORE 6
2567 #  define SV_CONST_STORESIZE 7
2568 #  define SV_CONST_EXISTS 8
2569
2570 #  define SV_CONST_PUSH 9
2571 #  define SV_CONST_POP 10
2572 #  define SV_CONST_SHIFT 11
2573 #  define SV_CONST_UNSHIFT 12
2574 #  define SV_CONST_SPLICE 13
2575 #  define SV_CONST_EXTEND 14
2576
2577 #  define SV_CONST_FIRSTKEY 15
2578 #  define SV_CONST_NEXTKEY 16
2579 #  define SV_CONST_SCALAR 17
2580
2581 #  define SV_CONST_OPEN 18
2582 #  define SV_CONST_WRITE 19
2583 #  define SV_CONST_PRINT 20
2584 #  define SV_CONST_PRINTF 21
2585 #  define SV_CONST_READ 22
2586 #  define SV_CONST_READLINE 23
2587 #  define SV_CONST_GETC 24
2588 #  define SV_CONST_SEEK 25
2589 #  define SV_CONST_TELL 26
2590 #  define SV_CONST_EOF 27
2591 #  define SV_CONST_BINMODE 28
2592 #  define SV_CONST_FILENO 29
2593 #  define SV_CONST_CLOSE 30
2594
2595 #  define SV_CONST_DELETE 31
2596 #  define SV_CONST_CLEAR 32
2597 #  define SV_CONST_UNTIE 33
2598 #  define SV_CONST_DESTROY 34
2599 #endif
2600
2601 #define SV_CONSTS_COUNT 35
2602
2603 /*
2604  * Bodyless IVs and NVs!
2605  *
2606  * Since 5.9.2, we can avoid allocating a body for SVt_IV-type SVs.
2607  * Since the larger IV-holding variants of SVs store their integer
2608  * values in their respective bodies, the family of SvIV() accessor
2609  * macros would  naively have to branch on the SV type to find the
2610  * integer value either in the HEAD or BODY. In order to avoid this
2611  * expensive branch, a clever soul has deployed a great hack:
2612  * We set up the SvANY pointer such that instead of pointing to a
2613  * real body, it points into the memory before the location of the
2614  * head. We compute this pointer such that the location of
2615  * the integer member of the hypothetical body struct happens to
2616  * be the same as the location of the integer member of the bodyless
2617  * SV head. This now means that the SvIV() family of accessors can
2618  * always read from the (hypothetical or real) body via SvANY.
2619  *
2620  * Since the 5.21 dev series, we employ the same trick for NVs
2621  * if the architecture can support it (NVSIZE <= IVSIZE).
2622  */
2623
2624 /* The following two macros compute the necessary offsets for the above
2625  * trick and store them in SvANY for SvIV() (and friends) to use. */
2626
2627 #  define SET_SVANY_FOR_BODYLESS_IV(sv) \
2628     STMT_START {                                            \
2629         SV * sv_ = MUTABLE_SV(sv);                          \
2630         SvANY(sv_) =   (XPVIV*)((char*)&(sv_->sv_u.svu_iv)  \
2631                     - STRUCT_OFFSET(XPVIV, xiv_iv));        \
2632     } STMT_END
2633
2634 #  define SET_SVANY_FOR_BODYLESS_NV(sv) \
2635     STMT_START {                                            \
2636         SV * sv_ = MUTABLE_SV(sv);                          \
2637         SvANY(sv_) =   (XPVNV*)((char*)&(sv_->sv_u.svu_nv)  \
2638                     - STRUCT_OFFSET(XPVNV, xnv_u.xnv_nv));  \
2639     } STMT_END
2640
2641 /*
2642  * ex: set ts=8 sts=4 sw=4 et:
2643  */