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