This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
902cae7176341b457b4b4cad4b0e6ec62c1a4d68
[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 =head1 SV Flags
17
18 =for apidoc AmU||svtype
19 An enum of flags for Perl types.  These are found in the file B<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_BIND (unused)
26     SVt_IV
27     SVt_NV
28     SVt_RV
29     SVt_PV
30     SVt_PVIV
31     SVt_PVNV
32     SVt_PVMG
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 SVt_PVIO is for I/O objects, SVt_PVFM for formats, SVt_PVCV for
45 subroutines, SVt_PVHV for hashes and 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 SVt_PVGV represents a typeglob.  If !SvFAKE(sv), then it is a real,
55 incoercible typeglob.  If 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.  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. SVt_REGEXP is for regular expressions.
61
62 SVt_PVMG represents a "normal" scalar (not a typeglob, regular expression,
63 or delegate).  Since most scalars do not need all the internal fields of a
64 PVMG, we save memory by allocating smaller structs when possible.  All the
65 other types are just simpler forms of SVt_PVMG, with fewer internal fields.
66  SVt_NULL can only hold undef.  SVt_IV can hold undef, an integer, or a
67 reference.  (SVt_RV is an alias for SVt_IV, which exists for backward
68 compatibility.)  SVt_NV can hold any of those or a double.  SVt_PV can only
69 hold undef or a string.  SVt_PVIV is a superset of SVt_PV and SVt_IV.
70 SVt_PVNV is similar.  SVt_PVMG can hold anything SVt_PVNV can hold, but it
71 can, but does not have to, be blessed or magical.
72
73 =for apidoc AmU||SVt_NULL
74 Type flag for scalars.  See L</svtype>.
75
76 =for apidoc AmU||SVt_IV
77 Type flag for scalars.  See L</svtype>.
78
79 =for apidoc AmU||SVt_NV
80 Type flag for scalars.  See L</svtype>.
81
82 =for apidoc AmU||SVt_PV
83 Type flag for scalars.  See L</svtype>.
84
85 =for apidoc AmU||SVt_PVIV
86 Type flag for scalars.  See L</svtype>.
87
88 =for apidoc AmU||SVt_PVNV
89 Type flag for scalars.  See L</svtype>.
90
91 =for apidoc AmU||SVt_PVMG
92 Type flag for scalars.  See L</svtype>.
93
94 =for apidoc AmU||SVt_REGEXP
95 Type flag for regular expressions.  See L</svtype>.
96
97 =for apidoc AmU||SVt_PVGV
98 Type flag for typeglobs.  See L</svtype>.
99
100 =for apidoc AmU||SVt_PVLV
101 Type flag for scalars.  See L</svtype>.
102
103 =for apidoc AmU||SVt_PVAV
104 Type flag for arrays.  See L</svtype>.
105
106 =for apidoc AmU||SVt_PVHV
107 Type flag for hashes.  See L</svtype>.
108
109 =for apidoc AmU||SVt_PVCV
110 Type flag for subroutines.  See L</svtype>.
111
112 =for apidoc AmU||SVt_PVFM
113 Type flag for formats.  See L</svtype>.
114
115 =for apidoc AmU||SVt_PVIO
116 Type flag for I/O objects.  See L</svtype>.
117
118 =cut
119 */
120
121 typedef enum {
122         SVt_NULL,       /* 0 */
123         SVt_BIND,       /* 1 */
124         SVt_IV,         /* 2 */
125         SVt_NV,         /* 3 */
126         /* RV was here, before it was merged with IV.  */
127         SVt_PV,         /* 4 */
128         SVt_PVIV,       /* 5 */
129         SVt_PVNV,       /* 6 */
130         SVt_PVMG,       /* 7 */
131         SVt_REGEXP,     /* 8 */
132         /* PVBM was here, before BIND replaced it.  */
133         SVt_PVGV,       /* 9 */
134         SVt_PVLV,       /* 10 */
135         SVt_PVAV,       /* 11 */
136         SVt_PVHV,       /* 12 */
137         SVt_PVCV,       /* 13 */
138         SVt_PVFM,       /* 14 */
139         SVt_PVIO,       /* 15 */
140         SVt_LAST        /* keep last in enum. used to size arrays */
141 } svtype;
142
143 /* *** any alterations to the SV types above need to be reflected in
144  * SVt_MASK and the various PL_valid_types_* tables */
145
146 #define SVt_MASK 0xf    /* smallest bitmask that covers all types */
147
148 #ifndef PERL_CORE
149 /* Although Fast Boyer Moore tables are now being stored in PVGVs, for most
150    purposes eternal code wanting to consider PVBM probably needs to think of
151    PVMG instead.  */
152 #  define SVt_PVBM      SVt_PVMG
153 /* Anything wanting to create a reference from clean should ensure that it has
154    a scalar of type SVt_IV now:  */
155 #  define SVt_RV        SVt_IV
156 #endif
157
158 /* There is collusion here with sv_clear - sv_clear exits early for SVt_NULL
159    so never reaches the clause at the end that uses sv_type_details->body_size
160    to determine whether to call safefree(). Hence body_size can be set
161    non-zero to record the size of HEs, without fear of bogus frees.  */
162 #if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
163 #define HE_SVSLOT       SVt_NULL
164 #endif
165
166 #define PERL_ARENA_ROOTS_SIZE   (SVt_LAST)
167
168 /* typedefs to eliminate some typing */
169 typedef struct he HE;
170 typedef struct hek HEK;
171
172 /* Using C's structural equivalence to help emulate C++ inheritance here... */
173
174 /* start with 2 sv-head building blocks */
175 #define _SV_HEAD(ptrtype) \
176     ptrtype     sv_any;         /* pointer to body */   \
177     U32         sv_refcnt;      /* how many references to us */ \
178     U32         sv_flags        /* what we are */
179
180 #define _SV_HEAD_UNION \
181     union {                             \
182         char*   svu_pv;         /* pointer to malloced string */        \
183         IV      svu_iv;                 \
184         UV      svu_uv;                 \
185         SV*     svu_rv;         /* pointer to another SV */             \
186         struct regexp* svu_rx;          \
187         SV**    svu_array;              \
188         HE**    svu_hash;               \
189         GP*     svu_gp;                 \
190         PerlIO *svu_fp;                 \
191     }   sv_u
192
193
194 struct STRUCT_SV {              /* struct sv { */
195     _SV_HEAD(void*);
196     _SV_HEAD_UNION;
197 #ifdef DEBUG_LEAKING_SCALARS
198     PERL_BITFIELD32 sv_debug_optype:9;  /* the type of OP that allocated us */
199     PERL_BITFIELD32 sv_debug_inpad:1;   /* was allocated in a pad for an OP */
200     PERL_BITFIELD32 sv_debug_line:16;   /* the line where we were allocated */
201     UV              sv_debug_serial;    /* serial number of sv allocation   */
202     char *          sv_debug_file;      /* the file where we were allocated */
203     SV *            sv_debug_parent;    /* what we were cloned from (ithreads)*/
204 #endif
205 };
206
207 struct gv {
208     _SV_HEAD(XPVGV*);           /* pointer to xpvgv body */
209     _SV_HEAD_UNION;
210 };
211
212 struct cv {
213     _SV_HEAD(XPVCV*);           /* pointer to xpvcv body */
214     _SV_HEAD_UNION;
215 };
216
217 struct av {
218     _SV_HEAD(XPVAV*);           /* pointer to xpvav body */
219     _SV_HEAD_UNION;
220 };
221
222 struct hv {
223     _SV_HEAD(XPVHV*);           /* pointer to xpvhv body */
224     _SV_HEAD_UNION;
225 };
226
227 struct io {
228     _SV_HEAD(XPVIO*);           /* pointer to xpvio body */
229     _SV_HEAD_UNION;
230 };
231
232 struct p5rx {
233     _SV_HEAD(struct regexp*);   /* pointer to regexp body */
234     _SV_HEAD_UNION;
235 };
236
237 #undef _SV_HEAD
238 #undef _SV_HEAD_UNION           /* ensure no pollution */
239
240 /*
241 =head1 SV Manipulation Functions
242
243 =for apidoc Am|U32|SvREFCNT|SV* sv
244 Returns the value of the object's reference count.
245
246 =for apidoc Am|SV*|SvREFCNT_inc|SV* sv
247 Increments the reference count of the given SV.
248
249 All of the following SvREFCNT_inc* macros are optimized versions of
250 SvREFCNT_inc, and can be replaced with SvREFCNT_inc.
251
252 =for apidoc Am|SV*|SvREFCNT_inc_NN|SV* sv
253 Same as SvREFCNT_inc, but can only be used if you know I<sv>
254 is not NULL.  Since we don't have to check the NULLness, it's faster
255 and smaller.
256
257 =for apidoc Am|void|SvREFCNT_inc_void|SV* sv
258 Same as SvREFCNT_inc, but can only be used if you don't need the
259 return value.  The macro doesn't need to return a meaningful value.
260
261 =for apidoc Am|void|SvREFCNT_inc_void_NN|SV* sv
262 Same as SvREFCNT_inc, but can only be used if you don't need the return
263 value, and you know that I<sv> is not NULL.  The macro doesn't need
264 to return a meaningful value, or check for NULLness, so it's smaller
265 and faster.
266
267 =for apidoc Am|SV*|SvREFCNT_inc_simple|SV* sv
268 Same as SvREFCNT_inc, but can only be used with expressions without side
269 effects.  Since we don't have to store a temporary value, it's faster.
270
271 =for apidoc Am|SV*|SvREFCNT_inc_simple_NN|SV* sv
272 Same as SvREFCNT_inc_simple, but can only be used if you know I<sv>
273 is not NULL.  Since we don't have to check the NULLness, it's faster
274 and smaller.
275
276 =for apidoc Am|void|SvREFCNT_inc_simple_void|SV* sv
277 Same as SvREFCNT_inc_simple, but can only be used if you don't need the
278 return value.  The macro doesn't need to return a meaningful value.
279
280 =for apidoc Am|void|SvREFCNT_inc_simple_void_NN|SV* sv
281 Same as SvREFCNT_inc, but can only be used if you don't need the return
282 value, and you know that I<sv> is not NULL.  The macro doesn't need
283 to return a meaningful value, or check for NULLness, so it's smaller
284 and faster.
285
286 =for apidoc Am|void|SvREFCNT_dec|SV* sv
287 Decrements the reference count of the given SV.
288
289 =for apidoc Am|svtype|SvTYPE|SV* sv
290 Returns the type of the SV.  See C<svtype>.
291
292 =for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
293 Used to upgrade an SV to a more complex form.  Uses C<sv_upgrade> to
294 perform the upgrade if necessary.  See C<svtype>.
295
296 =cut
297 */
298
299 #define SvANY(sv)       (sv)->sv_any
300 #define SvFLAGS(sv)     (sv)->sv_flags
301 #define SvREFCNT(sv)    (sv)->sv_refcnt
302
303 #define SvREFCNT_inc(sv)                S_SvREFCNT_inc(MUTABLE_SV(sv))
304 #define SvREFCNT_inc_simple(sv)         SvREFCNT_inc(sv)
305 #define SvREFCNT_inc_NN(sv)             S_SvREFCNT_inc_NN(MUTABLE_SV(sv))
306 #define SvREFCNT_inc_void(sv)           S_SvREFCNT_inc_void(MUTABLE_SV(sv))
307
308 /* These guys don't need the curly blocks */
309 #define SvREFCNT_inc_simple_void(sv)    STMT_START { if (sv) SvREFCNT(sv)++; } STMT_END
310 #define SvREFCNT_inc_simple_NN(sv)      (++(SvREFCNT(sv)),MUTABLE_SV(sv))
311 #define SvREFCNT_inc_void_NN(sv)        (void)(++SvREFCNT(MUTABLE_SV(sv)))
312 #define SvREFCNT_inc_simple_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
313
314 #define SvREFCNT_dec(sv)        S_SvREFCNT_dec(aTHX_ MUTABLE_SV(sv))
315
316 #define SVTYPEMASK      0xff
317 #define SvTYPE(sv)      ((svtype)((sv)->sv_flags & SVTYPEMASK))
318
319 /* Sadly there are some parts of the core that have pointers to already-freed
320    SV heads, and rely on being able to tell that they are now free. So mark
321    them all by using a consistent macro.  */
322 #define SvIS_FREED(sv)  ((sv)->sv_flags == SVTYPEMASK)
323
324 #define SvUPGRADE(sv, mt) (SvTYPE(sv) >= (mt) || (sv_upgrade(sv, mt), 1))
325
326 #define SVf_IOK         0x00000100  /* has valid public integer value */
327 #define SVf_NOK         0x00000200  /* has valid public numeric value */
328 #define SVf_POK         0x00000400  /* has valid public pointer value */
329 #define SVf_ROK         0x00000800  /* has a valid reference pointer */
330
331 #define SVp_IOK         0x00001000  /* has valid non-public integer value */
332 #define SVp_NOK         0x00002000  /* has valid non-public numeric value */
333 #define SVp_POK         0x00004000  /* has valid non-public pointer value */
334 #define SVp_SCREAM      0x00008000  /* method name is DOES */
335 #define SVphv_CLONEABLE SVp_SCREAM  /* PVHV (stashes) clone its objects */
336 #define SVpgv_GP        SVp_SCREAM  /* GV has a valid GP */
337 #define SVprv_PCS_IMPORTED  SVp_SCREAM  /* RV is a proxy for a constant
338                                        subroutine in another package. Set the
339                                        GvIMPORTED_CV_on() if it needs to be
340                                        expanded to a real GV */
341 #define SVf_IsCOW       0x00010000  /* copy on write (shared hash key if
342                                        SvLEN == 0) */
343 #define SVs_PADTMP      0x00020000  /* in use as tmp; only if ! SVs_PADMY */
344 #define SVs_PADSTALE    0x00020000  /* lexical has gone out of scope;
345                                         only valid for SVs_PADMY */
346 #define SVpad_TYPED     0x00020000  /* pad name is a Typed Lexical */
347 #define SVs_PADMY       0x00040000  /* in use a "my" variable */
348 #define SVpad_OUR       0x00040000  /* pad name is "our" instead of "my" */
349 #define SVs_TEMP        0x00080000  /* string is stealable? */
350 #define SVs_OBJECT      0x00100000  /* is "blessed" */
351 #define SVs_GMG         0x00200000  /* has magical get method */
352 #define SVs_SMG         0x00400000  /* has magical set method */
353 #define SVs_RMG         0x00800000  /* has random magical methods */
354
355 #define SVf_FAKE        0x01000000  /* 0: glob is just a copy
356                                        1: SV head arena wasn't malloc()ed
357                                        2: For PVCV, whether CvUNIQUE(cv)
358                                           refers to an eval or once only
359                                           [CvEVAL(cv), CvSPECIAL(cv)]
360                                        3: On a pad name SV, that slot in the
361                                           frame AV is a REFCNT'ed reference
362                                           to a lexical from "outside". */
363 #define SVf_OOK         0x02000000  /* has valid offset value. For a PVHV this
364                                        means that a hv_aux struct is present
365                                        after the main array */
366 #define SVf_BREAK       0x04000000  /* refcnt is artificially low - used by
367                                        SVs in final arena cleanup.
368                                        Set in S_regtry on PL_reg_curpm, so that
369                                        perl_destruct will skip it. */
370 #define SVf_READONLY    0x08000000  /* may not be modified */
371
372
373
374
375 #define SVf_THINKFIRST  (SVf_READONLY|SVf_ROK|SVf_FAKE|SVs_RMG|SVf_IsCOW)
376
377 #define SVf_OK          (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
378                          SVp_IOK|SVp_NOK|SVp_POK|SVpgv_GP)
379
380 #define PRIVSHIFT 4     /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
381
382 #define SVf_AMAGIC      0x10000000  /* has magical overloaded methods */
383
384 /* Ensure this value does not clash with the GV_ADD* flags in gv.h: */
385 #define SVf_UTF8        0x20000000  /* SvPV is UTF-8 encoded
386                                        This is also set on RVs whose overloaded
387                                        stringification is UTF-8. This might
388                                        only happen as a side effect of SvPV() */
389                                            
390
391 /* Some private flags. */
392
393 /* PVAV could probably use 0x2000000 without conflict. I assume that PVFM can
394    be UTF-8 encoded, and PVCVs could well have UTF-8 prototypes. PVIOs haven't
395    been restructured, so sometimes get used as string buffers.  */
396
397 /* PVHV */
398 #define SVphv_SHAREKEYS 0x20000000  /* PVHV keys live on shared string table */
399
400 /* PVNV, PVMG only, and only used in pads. Should be safe to test on any scalar
401    SV, as the core is careful to avoid setting both.
402
403    SVf_POK, SVp_POK also set:
404    0x00004400   Normal
405    0x0000C400   method name for DOES (SvSCREAM)
406    0x40004400   FBM compiled (SvVALID)
407    0x4000C400   pad name.
408
409    0x00008000   GV with GP
410    0x00008800   RV with PCS imported
411 */
412 #define SVpad_NAME      (SVp_SCREAM|SVpbm_VALID)
413                                     /* This SV is a name in the PAD, so
414                                        SVpad_TYPED, SVpad_OUR and SVpad_STATE
415                                        apply */
416 /* PVAV */
417 #define SVpav_REAL      0x40000000  /* free old entries */
418 /* PVHV */
419 #define SVphv_LAZYDEL   0x40000000  /* entry in xhv_eiter must be deleted */
420 /* This is only set true on a PVGV when it's playing "PVBM", but is tested for
421    on any regular scalar (anything <= PVLV) */
422 #define SVpbm_VALID     0x40000000
423 /* Only used in toke.c on an SV stored in PL_lex_repl */
424 #define SVrepl_EVAL     0x40000000  /* Replacement part of s///e */
425
426 /* IV, PVIV, PVNV, PVMG, PVGV and (I assume) PVLV  */
427 #define SVf_IVisUV      0x80000000  /* use XPVUV instead of XPVIV */
428 /* PVAV */
429 #define SVpav_REIFY     0x80000000  /* can become real */
430 /* PVHV */
431 #define SVphv_HASKFLAGS 0x80000000  /* keys have flag byte after hash */
432 /* PVGV when SVpbm_VALID is true */
433 #define SVpbm_TAIL      0x80000000
434 /* RV upwards. However, SVf_ROK and SVp_IOK are exclusive  */
435 #define SVprv_WEAKREF   0x80000000  /* Weak reference */
436 /* pad name vars only */
437 #define SVpad_STATE     0x80000000  /* pad name is a "state" var */
438
439 #define _XPV_HEAD                                                       \
440     HV*         xmg_stash;      /* class package */                     \
441     union _xmgu xmg_u;                                                  \
442     STRLEN      xpv_cur;        /* length of svu_pv as a C string */    \
443     union {                                                             \
444         STRLEN  xpvlenu_len;    /* allocated size */                    \
445         char *  xpvlenu_pv;     /* regexp string */                     \
446     } xpv_len_u 
447
448 #define xpv_len xpv_len_u.xpvlenu_len
449
450 union _xnvu {
451     NV      xnv_nv;             /* numeric value, if any */
452     HV *    xgv_stash;
453     struct {
454         U32 xlow;
455         U32 xhigh;
456     }       xpad_cop_seq;       /* used by pad.c for cop_sequence */
457     struct {
458         I32 xbm_useful;
459         U8  xbm_rare;           /* rarest character in string */
460     }       xbm_s;              /* fields from PVBM */
461 };
462
463 union _xivu {
464     IV      xivu_iv;            /* integer value */
465     UV      xivu_uv;
466     HEK *   xivu_namehek;       /* xpvlv, xpvgv: GvNAME */
467 };
468
469 union _xmgu {
470     MAGIC*  xmg_magic;          /* linked list of magicalness */
471     HV*     xmg_ourstash;       /* Stash for our (when SvPAD_OUR is true) */
472     STRLEN  xmg_hash_index;     /* used while freeing hash entries */
473 };
474
475 struct xpv {
476     _XPV_HEAD;
477 };
478
479 struct xpviv {
480     _XPV_HEAD;
481     union _xivu xiv_u;
482 };
483
484 #define xiv_iv xiv_u.xivu_iv
485
486 struct xpvuv {
487     _XPV_HEAD;
488     union _xivu xuv_u;
489 };
490
491 #define xuv_uv xuv_u.xivu_uv
492
493 struct xpvnv {
494     _XPV_HEAD;
495     union _xivu xiv_u;
496     union _xnvu xnv_u;
497 };
498
499 /* This structure must match the beginning of struct xpvhv in hv.h. */
500 struct xpvmg {
501     _XPV_HEAD;
502     union _xivu xiv_u;
503     union _xnvu xnv_u;
504 };
505
506 struct xpvlv {
507     _XPV_HEAD;
508     union _xivu xiv_u;
509     union _xnvu xnv_u;
510     STRLEN      xlv_targoff;
511     STRLEN      xlv_targlen;
512     SV*         xlv_targ;
513     char        xlv_type;       /* k=keys .=pos x=substr v=vec /=join/re
514                                  * y=alem/helem/iter t=tie T=tied HE */
515     char        xlv_flags;      /* 1 = negative offset  2 = negative len */
516 };
517
518 /* This structure works in 3 ways - regular scalar, GV with GP, or fast
519    Boyer-Moore.  */
520 struct xpvgv {
521     _XPV_HEAD;
522     union _xivu xiv_u;
523     union _xnvu xnv_u;
524 };
525
526 typedef U16 cv_flags_t;
527
528 #define _XPVCV_COMMON                                                           \
529     HV *        xcv_stash;                                                      \
530     union {                                                                     \
531         OP *    xcv_start;                                                      \
532         ANY     xcv_xsubany;                                                    \
533     }           xcv_start_u;                                                    \
534     union {                                                                     \
535         OP *    xcv_root;                                                       \
536         void    (*xcv_xsub) (pTHX_ CV*);                                        \
537     }           xcv_root_u;                                                     \
538     union {                                                             \
539         GV *    xcv_gv;                                                 \
540         HEK *   xcv_hek;                                                \
541     }           xcv_gv_u;                                               \
542     char *      xcv_file;                                                       \
543     PADLIST *   xcv_padlist;                                                    \
544     CV *        xcv_outside;                                                    \
545     U32         xcv_outside_seq; /* the COP sequence (at the point of our       \
546                                   * compilation) in the lexically enclosing     \
547                                   * sub */                                      \
548     cv_flags_t  xcv_flags;                                              \
549     I32 xcv_depth       /* >= 2 indicates recursive call */
550
551 /* This structure must match XPVCV in cv.h */
552
553 struct xpvfm {
554     _XPV_HEAD;
555     _XPVCV_COMMON;
556 };
557
558
559 struct xpvio {
560     _XPV_HEAD;
561     union _xivu xiv_u;
562     /* ifp and ofp are normally the same, but sockets need separate streams */
563     PerlIO *    xio_ofp;
564     /* Cray addresses everything by word boundaries (64 bits) and
565      * code and data pointers cannot be mixed (which is exactly what
566      * Perl_filter_add() tries to do with the dirp), hence the
567      *  following union trick (as suggested by Gurusamy Sarathy).
568      * For further information see Geir Johansen's problem report
569      * titled [ID 20000612.002] Perl problem on Cray system
570      * The any pointer (known as IoANY()) will also be a good place
571      * to hang any IO disciplines to.
572      */
573     union {
574         DIR *   xiou_dirp;      /* for opendir, readdir, etc */
575         void *  xiou_any;       /* for alignment */
576     } xio_dirpu;
577     /* IV xio_lines is now in IVX  $. */
578     IV          xio_page;       /* $% */
579     IV          xio_page_len;   /* $= */
580     IV          xio_lines_left; /* $- */
581     char *      xio_top_name;   /* $^ */
582     GV *        xio_top_gv;     /* $^ */
583     char *      xio_fmt_name;   /* $~ */
584     GV *        xio_fmt_gv;     /* $~ */
585     char *      xio_bottom_name;/* $^B */
586     GV *        xio_bottom_gv;  /* $^B */
587     char        xio_type;
588     U8          xio_flags;
589 };
590
591 #define xio_dirp        xio_dirpu.xiou_dirp
592 #define xio_any         xio_dirpu.xiou_any
593
594 #define IOf_ARGV        1       /* this fp iterates over ARGV */
595 #define IOf_START       2       /* check for null ARGV and substitute '-' */
596 #define IOf_FLUSH       4       /* this fp wants a flush after write op */
597 #define IOf_DIDTOP      8       /* just did top of form */
598 #define IOf_UNTAINT     16      /* consider this fp (and its data) "safe" */
599 #define IOf_NOLINE      32      /* slurped a pseudo-line from empty file */
600 #define IOf_FAKE_DIRP   64      /* xio_dirp is fake (source filters kludge)
601                                    Also, when this is set, SvPVX() is valid */
602
603 /* The following macros define implementation-independent predicates on SVs. */
604
605 /*
606 =for apidoc Am|U32|SvNIOK|SV* sv
607 Returns a U32 value indicating whether the SV contains a number, integer or
608 double.
609
610 =for apidoc Am|U32|SvNIOKp|SV* sv
611 Returns a U32 value indicating whether the SV contains a number, integer or
612 double.  Checks the B<private> setting.  Use C<SvNIOK> instead.
613
614 =for apidoc Am|void|SvNIOK_off|SV* sv
615 Unsets the NV/IV status of an SV.
616
617 =for apidoc Am|U32|SvOK|SV* sv
618 Returns a U32 value indicating whether the value is defined. This is
619 only meaningful for scalars.
620
621 =for apidoc Am|U32|SvIOKp|SV* sv
622 Returns a U32 value indicating whether the SV contains an integer.  Checks
623 the B<private> setting.  Use C<SvIOK> instead.
624
625 =for apidoc Am|U32|SvNOKp|SV* sv
626 Returns a U32 value indicating whether the SV contains a double.  Checks the
627 B<private> setting.  Use C<SvNOK> instead.
628
629 =for apidoc Am|U32|SvPOKp|SV* sv
630 Returns a U32 value indicating whether the SV contains a character string.
631 Checks the B<private> setting.  Use C<SvPOK> instead.
632
633 =for apidoc Am|U32|SvIOK|SV* sv
634 Returns a U32 value indicating whether the SV contains an integer.
635
636 =for apidoc Am|void|SvIOK_on|SV* sv
637 Tells an SV that it is an integer.
638
639 =for apidoc Am|void|SvIOK_off|SV* sv
640 Unsets the IV status of an SV.
641
642 =for apidoc Am|void|SvIOK_only|SV* sv
643 Tells an SV that it is an integer and disables all other OK bits.
644
645 =for apidoc Am|void|SvIOK_only_UV|SV* sv
646 Tells and SV that it is an unsigned integer and disables all other OK bits.
647
648 =for apidoc Am|bool|SvIOK_UV|SV* sv
649 Returns a boolean indicating whether the SV contains an unsigned integer.
650
651 =for apidoc Am|bool|SvUOK|SV* sv
652 Returns a boolean indicating whether the SV contains an unsigned integer.
653
654 =for apidoc Am|bool|SvIOK_notUV|SV* sv
655 Returns a boolean indicating whether the SV contains a signed integer.
656
657 =for apidoc Am|U32|SvNOK|SV* sv
658 Returns a U32 value indicating whether the SV contains a double.
659
660 =for apidoc Am|void|SvNOK_on|SV* sv
661 Tells an SV that it is a double.
662
663 =for apidoc Am|void|SvNOK_off|SV* sv
664 Unsets the NV status of an SV.
665
666 =for apidoc Am|void|SvNOK_only|SV* sv
667 Tells an SV that it is a double and disables all other OK bits.
668
669 =for apidoc Am|U32|SvPOK|SV* sv
670 Returns a U32 value indicating whether the SV contains a character
671 string.
672
673 =for apidoc Am|void|SvPOK_on|SV* sv
674 Tells an SV that it is a string.
675
676 =for apidoc Am|void|SvPOK_off|SV* sv
677 Unsets the PV status of an SV.
678
679 =for apidoc Am|void|SvPOK_only|SV* sv
680 Tells an SV that it is a string and disables all other OK bits.
681 Will also turn off the UTF-8 status.
682
683 =for apidoc Am|bool|SvVOK|SV* sv
684 Returns a boolean indicating whether the SV contains a v-string.
685
686 =for apidoc Am|U32|SvOOK|SV* sv
687 Returns a U32 indicating whether the pointer to the string buffer is offset.
688 This hack is used internally to speed up removal of characters from the
689 beginning of a SvPV.  When SvOOK is true, then the start of the
690 allocated string buffer is actually C<SvOOK_offset()> bytes before SvPVX.
691 This offset used to be stored in SvIVX, but is now stored within the spare
692 part of the buffer.
693
694 =for apidoc Am|U32|SvROK|SV* sv
695 Tests if the SV is an RV.
696
697 =for apidoc Am|void|SvROK_on|SV* sv
698 Tells an SV that it is an RV.
699
700 =for apidoc Am|void|SvROK_off|SV* sv
701 Unsets the RV status of an SV.
702
703 =for apidoc Am|SV*|SvRV|SV* sv
704 Dereferences an RV to return the SV.
705
706 =for apidoc Am|IV|SvIVX|SV* sv
707 Returns the raw value in the SV's IV slot, without checks or conversions.
708 Only use when you are sure SvIOK is true.  See also C<SvIV()>.
709
710 =for apidoc Am|UV|SvUVX|SV* sv
711 Returns the raw value in the SV's UV slot, without checks or conversions.
712 Only use when you are sure SvIOK is true.  See also C<SvUV()>.
713
714 =for apidoc Am|NV|SvNVX|SV* sv
715 Returns the raw value in the SV's NV slot, without checks or conversions.
716 Only use when you are sure SvNOK is true.  See also C<SvNV()>.
717
718 =for apidoc Am|char*|SvPVX|SV* sv
719 Returns a pointer to the physical string in the SV.  The SV must contain a
720 string.
721
722 This is also used to store the name of an autoloaded subroutine in an XS
723 AUTOLOAD routine.  See L<perlguts/Autoloading with XSUBs>.
724
725 =for apidoc Am|STRLEN|SvCUR|SV* sv
726 Returns the length of the string which is in the SV.  See C<SvLEN>.
727
728 =for apidoc Am|STRLEN|SvLEN|SV* sv
729 Returns the size of the string buffer in the SV, not including any part
730 attributable to C<SvOOK>.  See C<SvCUR>.
731
732 =for apidoc Am|char*|SvEND|SV* sv
733 Returns a pointer to the spot just after the last character in
734 the string which is in the SV, where there is usually a trailing
735 null (even though Perl scalars do not strictly require it).
736 See C<SvCUR>.  Access the character as *(SvEND(sv)).
737
738 Warning: If C<SvCUR> is equal to C<SvLEN>, then C<SvEND> points to
739 unallocated memory.
740
741 =for apidoc Am|HV*|SvSTASH|SV* sv
742 Returns the stash of the SV.
743
744 =for apidoc Am|void|SvIV_set|SV* sv|IV val
745 Set the value of the IV pointer in sv to val.  It is possible to perform
746 the same function of this macro with an lvalue assignment to C<SvIVX>.
747 With future Perls, however, it will be more efficient to use 
748 C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.
749
750 =for apidoc Am|void|SvNV_set|SV* sv|NV val
751 Set the value of the NV pointer in sv to val.  See C<SvIV_set>.
752
753 =for apidoc Am|void|SvPV_set|SV* sv|char* val
754 Set the value of the PV pointer in sv to val.  See C<SvIV_set>.
755
756 =for apidoc Am|void|SvUV_set|SV* sv|UV val
757 Set the value of the UV pointer in sv to val.  See C<SvIV_set>.
758
759 =for apidoc Am|void|SvRV_set|SV* sv|SV* val
760 Set the value of the RV pointer in sv to val.  See C<SvIV_set>.
761
762 =for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
763 Set the value of the MAGIC pointer in sv to val.  See C<SvIV_set>.
764
765 =for apidoc Am|void|SvSTASH_set|SV* sv|HV* val
766 Set the value of the STASH pointer in sv to val.  See C<SvIV_set>.
767
768 =for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
769 Set the current length of the string which is in the SV.  See C<SvCUR>
770 and C<SvIV_set>.
771
772 =for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
773 Set the actual length of the string which is in the SV.  See C<SvIV_set>.
774
775 =cut
776 */
777
778 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
779 #define SvNIOKp(sv)             (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
780 #define SvNIOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
781                                                   SVp_IOK|SVp_NOK|SVf_IVisUV))
782
783 #define assert_not_ROK(sv)      assert_(!SvROK(sv) || !SvRV(sv))
784 #define assert_not_glob(sv)     assert_(!isGV_with_GP(sv))
785
786 #define SvOK(sv)                ((SvTYPE(sv) == SVt_BIND)               \
787                                  ? (SvFLAGS(SvRV(sv)) & SVf_OK          \
788                                         || isREGEXP(SvRV(sv)))          \
789                                  : (SvFLAGS(sv) & SVf_OK                \
790                                          || isREGEXP(sv)))
791 #define SvOK_off(sv)            (assert_not_ROK(sv) assert_not_glob(sv) \
792                                  SvFLAGS(sv) &= ~(SVf_OK|               \
793                                                   SVf_IVisUV|SVf_UTF8), \
794                                                         SvOOK_off(sv))
795 #define SvOK_off_exc_UV(sv)     (assert_not_ROK(sv)                     \
796                                  SvFLAGS(sv) &= ~(SVf_OK|               \
797                                                   SVf_UTF8),            \
798                                                         SvOOK_off(sv))
799
800 #define SvOKp(sv)               (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
801 #define SvIOKp(sv)              (SvFLAGS(sv) & SVp_IOK)
802 #define SvIOKp_on(sv)           (assert_not_glob(sv) SvRELEASE_IVX_(sv) \
803                                     SvFLAGS(sv) |= SVp_IOK)
804 #define SvNOKp(sv)              (SvFLAGS(sv) & SVp_NOK)
805 #define SvNOKp_on(sv)           (assert_not_glob(sv) SvFLAGS(sv) |= SVp_NOK)
806 #define SvPOKp(sv)              (SvFLAGS(sv) & SVp_POK)
807 #define SvPOKp_on(sv)           (assert_not_ROK(sv) assert_not_glob(sv) \
808                                  SvFLAGS(sv) |= SVp_POK)
809
810 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
811 #define SvIOK_on(sv)            (assert_not_glob(sv) SvRELEASE_IVX_(sv) \
812                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
813 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
814 #define SvIOK_only(sv)          (SvOK_off(sv), \
815                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
816 #define SvIOK_only_UV(sv)       (assert_not_glob(sv) SvOK_off_exc_UV(sv), \
817                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
818
819 #define SvIOK_UV(sv)            ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
820                                  == (SVf_IOK|SVf_IVisUV))
821 #define SvUOK(sv)               SvIOK_UV(sv)
822 #define SvIOK_notUV(sv)         ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
823                                  == SVf_IOK)
824
825 #define SvIsUV(sv)              (SvFLAGS(sv) & SVf_IVisUV)
826 #define SvIsUV_on(sv)           (SvFLAGS(sv) |= SVf_IVisUV)
827 #define SvIsUV_off(sv)          (SvFLAGS(sv) &= ~SVf_IVisUV)
828
829 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
830 #define SvNOK_on(sv)            (assert_not_glob(sv) \
831                                  SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
832 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
833 #define SvNOK_only(sv)          (SvOK_off(sv), \
834                                     SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
835
836 /*
837 =for apidoc Am|U32|SvUTF8|SV* sv
838 Returns a U32 value indicating the UTF-8 status of an SV.  If things are set-up
839 properly, this indicates whether or not the SV contains UTF-8 encoded data.
840 Call this after SvPV() in case any call to string overloading updates the
841 internal flag.
842
843 =for apidoc Am|void|SvUTF8_on|SV *sv
844 Turn on the UTF-8 status of an SV (the data is not changed, just the flag).
845 Do not use frivolously.
846
847 =for apidoc Am|void|SvUTF8_off|SV *sv
848 Unsets the UTF-8 status of an SV (the data is not changed, just the flag).
849 Do not use frivolously.
850
851 =for apidoc Am|void|SvPOK_only_UTF8|SV* sv
852 Tells an SV that it is a string and disables all other OK bits,
853 and leaves the UTF-8 status as it was.
854
855 =cut
856  */
857
858 /* Ensure the return value of this macro does not clash with the GV_ADD* flags
859 in gv.h: */
860 #define SvUTF8(sv)              (SvFLAGS(sv) & SVf_UTF8)
861 #define SvUTF8_on(sv)           (SvFLAGS(sv) |= (SVf_UTF8))
862 #define SvUTF8_off(sv)          (SvFLAGS(sv) &= ~(SVf_UTF8))
863
864 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
865 #define SvPOK_on(sv)            (assert_not_ROK(sv) assert_not_glob(sv) \
866                                  SvFLAGS(sv) |= (SVf_POK|SVp_POK))
867 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
868 #define SvPOK_only(sv)          (assert_not_ROK(sv) assert_not_glob(sv) \
869                                  SvFLAGS(sv) &= ~(SVf_OK|               \
870                                                   SVf_IVisUV|SVf_UTF8), \
871                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
872 #define SvPOK_only_UTF8(sv)     (assert_not_ROK(sv) assert_not_glob(sv) \
873                                  SvFLAGS(sv) &= ~(SVf_OK|               \
874                                                   SVf_IVisUV),          \
875                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
876
877 #define SvVOK(sv)               (SvMAGICAL(sv)                          \
878                                  && mg_find(sv,PERL_MAGIC_vstring))
879 /* returns the vstring magic, if any */
880 #define SvVSTRING_mg(sv)        (SvMAGICAL(sv) \
881                                  ? mg_find(sv,PERL_MAGIC_vstring) : NULL)
882
883 #define SvOOK(sv)               (SvFLAGS(sv) & SVf_OOK)
884 #define SvOOK_on(sv)            (SvFLAGS(sv) |= SVf_OOK)
885 #define SvOOK_off(sv)           ((void)(SvOOK(sv) && sv_backoff(sv)))
886
887 #define SvFAKE(sv)              (SvFLAGS(sv) & SVf_FAKE)
888 #define SvFAKE_on(sv)           (SvFLAGS(sv) |= SVf_FAKE)
889 #define SvFAKE_off(sv)          (SvFLAGS(sv) &= ~SVf_FAKE)
890
891 #define SvROK(sv)               (SvFLAGS(sv) & SVf_ROK)
892 #define SvROK_on(sv)            (SvFLAGS(sv) |= SVf_ROK)
893 #define SvROK_off(sv)           (SvFLAGS(sv) &= ~(SVf_ROK))
894
895 #define SvMAGICAL(sv)           (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
896 #define SvMAGICAL_on(sv)        (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
897 #define SvMAGICAL_off(sv)       (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
898
899 #define SvGMAGICAL(sv)          (SvFLAGS(sv) & SVs_GMG)
900 #define SvGMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_GMG)
901 #define SvGMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_GMG)
902
903 #define SvSMAGICAL(sv)          (SvFLAGS(sv) & SVs_SMG)
904 #define SvSMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_SMG)
905 #define SvSMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_SMG)
906
907 #define SvRMAGICAL(sv)          (SvFLAGS(sv) & SVs_RMG)
908 #define SvRMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_RMG)
909 #define SvRMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_RMG)
910
911 #define SvAMAGIC(sv)            (SvROK(sv) && SvOBJECT(SvRV(sv)) &&     \
912                                  HvAMAGIC(SvSTASH(SvRV(sv))))
913
914 /* To be used on the stashes themselves: */
915 #define HvAMAGIC(hv)            (SvFLAGS(hv) & SVf_AMAGIC)
916 #define HvAMAGIC_on(hv)         (SvFLAGS(hv) |= SVf_AMAGIC)
917 #define HvAMAGIC_off(hv)        (SvFLAGS(hv) &=~ SVf_AMAGIC)
918
919
920 #define SvPOK_nog(sv)           ((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK)
921 #define SvIOK_nog(sv)           ((SvFLAGS(sv) & (SVf_IOK|SVs_GMG)) == SVf_IOK)
922 #define SvUOK_nog(sv)           ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
923 #define SvNOK_nog(sv)           ((SvFLAGS(sv) & (SVf_NOK|SVs_GMG)) == SVf_NOK)
924 #define SvNIOK_nog(sv)          (SvNIOK(sv) && !(SvFLAGS(sv) & SVs_GMG))
925
926 #define SvPOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
927 #define SvIOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_IOK|SVf_THINKFIRST|SVs_GMG)) == SVf_IOK)
928 #define SvUOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVf_THINKFIRST|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
929 #define SvNOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_NOK|SVf_THINKFIRST|SVs_GMG)) == SVf_NOK)
930 #define SvNIOK_nogthink(sv)     (SvNIOK(sv) && !(SvFLAGS(sv) & (SVf_THINKFIRST|SVs_GMG)))
931
932 #define SvPOK_utf8_nog(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == (SVf_POK|SVf_UTF8))
933 #define SvPOK_utf8_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
934
935 #define SvPOK_byte_nog(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == SVf_POK)
936 #define SvPOK_byte_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
937
938 #define SvPOK_pure_nogthink(sv) \
939     ((SvFLAGS(sv) & (SVf_POK|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
940 #define SvPOK_utf8_pure_nogthink(sv) \
941     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
942 #define SvPOK_byte_pure_nogthink(sv) \
943     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
944
945 /*
946 =for apidoc Am|U32|SvGAMAGIC|SV* sv
947
948 Returns true if the SV has get magic or
949 overloading.  If either is true then
950 the scalar is active data, and has the potential to return a new value every
951 time it is accessed.  Hence you must be careful to
952 only read it once per user logical operation and work
953 with that returned value.  If neither is true then
954 the scalar's value cannot change unless written to.
955
956 =cut
957 */
958
959 #define SvGAMAGIC(sv)           (SvGMAGICAL(sv) || SvAMAGIC(sv))
960
961 #define Gv_AMG(stash) \
962         (HvNAME(stash) && Gv_AMupdate(stash,FALSE) \
963             ? 1                                     \
964             : (HvAMAGIC_off(stash), 0))
965
966 #define SvWEAKREF(sv)           ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
967                                   == (SVf_ROK|SVprv_WEAKREF))
968 #define SvWEAKREF_on(sv)        (SvFLAGS(sv) |=  (SVf_ROK|SVprv_WEAKREF))
969 #define SvWEAKREF_off(sv)       (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
970
971 #define SvPCS_IMPORTED(sv)      ((SvFLAGS(sv) & (SVf_ROK|SVprv_PCS_IMPORTED)) \
972                                  == (SVf_ROK|SVprv_PCS_IMPORTED))
973 #define SvPCS_IMPORTED_on(sv)   (SvFLAGS(sv) |=  (SVf_ROK|SVprv_PCS_IMPORTED))
974 #define SvPCS_IMPORTED_off(sv)  (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_PCS_IMPORTED))
975
976 /*
977 =for apidoc m|U32|SvTHINKFIRST|SV *sv
978
979 A quick flag check to see whether an sv should be passed to sv_force_normal
980 to be "downgraded" before SvIVX or SvPVX can be modified directly.
981
982 For example, if your scalar is a reference and you want to modify the SvIVX
983 slot, you can't just do SvROK_off, as that will leak the referent.
984
985 This is used internally by various sv-modifying functions, such as
986 sv_setsv, sv_setiv and sv_pvn_force.
987
988 One case that this does not handle is a gv without SvFAKE set.  After
989
990     if (SvTHINKFIRST(gv)) sv_force_normal(gv);
991
992 it will still be a gv.
993
994 SvTHINKFIRST sometimes produces false positives.  In those cases
995 sv_force_normal does nothing.
996
997 =cut
998 */
999
1000 #define SvTHINKFIRST(sv)        (SvFLAGS(sv) & SVf_THINKFIRST)
1001
1002 #define SvPADMY(sv)             (SvFLAGS(sv) & SVs_PADMY)
1003 #define SvPADMY_on(sv)          (SvFLAGS(sv) |= SVs_PADMY)
1004
1005 /* SVs_PADTMP and SVs_PADSTALE share the same bit, mediated by SVs_PADMY */
1006
1007 #define SvPADTMP(sv)    ((SvFLAGS(sv) & (SVs_PADMY|SVs_PADTMP)) == SVs_PADTMP)
1008 #define SvPADSTALE(sv)  ((SvFLAGS(sv) & (SVs_PADMY|SVs_PADSTALE)) \
1009                                     == (SVs_PADMY|SVs_PADSTALE))
1010
1011 #define SvPADTMP_on(sv)         S_SvPADTMP_on(MUTABLE_SV(sv))
1012 #define SvPADTMP_off(sv)        S_SvPADTMP_off(MUTABLE_SV(sv))
1013 #define SvPADSTALE_on(sv)       S_SvPADSTALE_on(MUTABLE_SV(sv))
1014 #define SvPADSTALE_off(sv)      S_SvPADSTALE_off(MUTABLE_SV(sv))
1015
1016 #define SvTEMP(sv)              (SvFLAGS(sv) & SVs_TEMP)
1017 #define SvTEMP_on(sv)           (SvFLAGS(sv) |= SVs_TEMP)
1018 #define SvTEMP_off(sv)          (SvFLAGS(sv) &= ~SVs_TEMP)
1019
1020 #define SvOBJECT(sv)            (SvFLAGS(sv) & SVs_OBJECT)
1021 #define SvOBJECT_on(sv)         (SvFLAGS(sv) |= SVs_OBJECT)
1022 #define SvOBJECT_off(sv)        (SvFLAGS(sv) &= ~SVs_OBJECT)
1023
1024 #define SvREADONLY(sv)          (SvFLAGS(sv) & SVf_READONLY)
1025 #define SvREADONLY_on(sv)       (SvFLAGS(sv) |= SVf_READONLY)
1026 #define SvREADONLY_off(sv)      (SvFLAGS(sv) &= ~SVf_READONLY)
1027
1028 #define SvSCREAM(sv) ((SvFLAGS(sv) & (SVp_SCREAM|SVp_POK)) == (SVp_SCREAM|SVp_POK))
1029 #define SvSCREAM_on(sv)         (SvFLAGS(sv) |= SVp_SCREAM)
1030 #define SvSCREAM_off(sv)        (SvFLAGS(sv) &= ~SVp_SCREAM)
1031
1032 #ifndef PERL_CORE
1033 #  define SvCOMPILED(sv)        0
1034 #  define SvCOMPILED_on(sv)
1035 #  define SvCOMPILED_off(sv)
1036 #endif
1037
1038 #define SvEVALED(sv)            (SvFLAGS(sv) & SVrepl_EVAL)
1039 #define SvEVALED_on(sv)         (SvFLAGS(sv) |= SVrepl_EVAL)
1040 #define SvEVALED_off(sv)        (SvFLAGS(sv) &= ~SVrepl_EVAL)
1041
1042 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1043 #  define SvVALID(sv)           ({ const SV *const _svvalid = (const SV*)(sv); \
1044                                    if (SvFLAGS(_svvalid) & SVpbm_VALID) \
1045                                        assert(!isGV_with_GP(_svvalid)); \
1046                                    (SvFLAGS(_svvalid) & SVpbm_VALID);   \
1047                                 })
1048 #  define SvVALID_on(sv)        ({ SV *const _svvalid = MUTABLE_SV(sv); \
1049                                    assert(!isGV_with_GP(_svvalid));     \
1050                                    (SvFLAGS(_svvalid) |= SVpbm_VALID);  \
1051                                 })
1052 #  define SvVALID_off(sv)       ({ SV *const _svvalid = MUTABLE_SV(sv); \
1053                                    assert(!isGV_with_GP(_svvalid));     \
1054                                    (SvFLAGS(_svvalid) &= ~SVpbm_VALID); \
1055                                 })
1056
1057 #  define SvTAIL(sv)    ({ const SV *const _svtail = (const SV *)(sv);  \
1058                             assert(SvTYPE(_svtail) != SVt_PVAV);                \
1059                             assert(SvTYPE(_svtail) != SVt_PVHV);                \
1060                             (SvFLAGS(sv) & (SVpbm_TAIL|SVpbm_VALID))    \
1061                                 == (SVpbm_TAIL|SVpbm_VALID);            \
1062                         })
1063 #else
1064 #  define SvVALID(sv)           (SvFLAGS(sv) & SVpbm_VALID)
1065 #  define SvVALID_on(sv)        (SvFLAGS(sv) |= SVpbm_VALID)
1066 #  define SvVALID_off(sv)       (SvFLAGS(sv) &= ~SVpbm_VALID)
1067 #  define SvTAIL(sv)        ((SvFLAGS(sv) & (SVpbm_TAIL|SVpbm_VALID))   \
1068                              == (SVpbm_TAIL|SVpbm_VALID))
1069
1070 #endif
1071 #define SvTAIL_on(sv)           (SvFLAGS(sv) |= SVpbm_TAIL)
1072 #define SvTAIL_off(sv)          (SvFLAGS(sv) &= ~SVpbm_TAIL)
1073
1074
1075 #define SvPAD_TYPED(sv) \
1076         ((SvFLAGS(sv) & (SVpad_NAME|SVpad_TYPED)) == (SVpad_NAME|SVpad_TYPED))
1077
1078 #define SvPAD_OUR(sv)   \
1079         ((SvFLAGS(sv) & (SVpad_NAME|SVpad_OUR)) == (SVpad_NAME|SVpad_OUR))
1080
1081 #define SvPAD_STATE(sv) \
1082         ((SvFLAGS(sv) & (SVpad_NAME|SVpad_STATE)) == (SVpad_NAME|SVpad_STATE))
1083
1084 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1085 #  define SvPAD_TYPED_on(sv)    ({                                      \
1086             SV *const _svpad = MUTABLE_SV(sv);                          \
1087             assert(SvTYPE(_svpad) == SVt_PVMG);                         \
1088             (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_TYPED);                \
1089         })
1090 #define SvPAD_OUR_on(sv)        ({                                      \
1091             SV *const _svpad = MUTABLE_SV(sv);                          \
1092             assert(SvTYPE(_svpad) == SVt_PVMG);                         \
1093             (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_OUR);                  \
1094         })
1095 #define SvPAD_STATE_on(sv)      ({                                      \
1096             SV *const _svpad = MUTABLE_SV(sv);                          \
1097             assert(SvTYPE(_svpad) == SVt_PVNV || SvTYPE(_svpad) == SVt_PVMG); \
1098             (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_STATE);                \
1099         })
1100 #else
1101 #  define SvPAD_TYPED_on(sv)    (SvFLAGS(sv) |= SVpad_NAME|SVpad_TYPED)
1102 #  define SvPAD_OUR_on(sv)      (SvFLAGS(sv) |= SVpad_NAME|SVpad_OUR)
1103 #  define SvPAD_STATE_on(sv)    (SvFLAGS(sv) |= SVpad_NAME|SVpad_STATE)
1104 #endif
1105
1106 #define SvOURSTASH(sv)  \
1107         (SvPAD_OUR(sv) ? ((XPVMG*) SvANY(sv))->xmg_u.xmg_ourstash : NULL)
1108 #define SvOURSTASH_set(sv, st)                                  \
1109         STMT_START {                                            \
1110             assert(SvTYPE(sv) == SVt_PVMG);                     \
1111             ((XPVMG*) SvANY(sv))->xmg_u.xmg_ourstash = st;      \
1112         } STMT_END
1113
1114 #define SvRVx(sv) SvRV(sv)
1115
1116 #ifdef PERL_DEBUG_COW
1117 /* Need -0.0 for SvNVX to preserve IEEE FP "negative zero" because
1118    +0.0 + -0.0 => +0.0 but -0.0 + -0.0 => -0.0 */
1119 #  define SvIVX(sv) (0 + ((XPVIV*) SvANY(sv))->xiv_iv)
1120 #  define SvUVX(sv) (0 + ((XPVUV*) SvANY(sv))->xuv_uv)
1121 #  define SvNVX(sv) (-0.0 + ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv)
1122 #  define SvRV(sv) (0 + (sv)->sv_u.svu_rv)
1123 #  define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1124 /* Don't test the core XS code yet.  */
1125 #  if defined (PERL_CORE) && PERL_DEBUG_COW > 1
1126 #    define SvPVX(sv) (0 + (assert_(!SvREADONLY(sv)) (sv)->sv_u.svu_pv))
1127 #  else
1128 #  define SvPVX(sv) SvPVX_mutable(sv)
1129 #  endif
1130 #  define SvCUR(sv) (0 + ((XPV*) SvANY(sv))->xpv_cur)
1131 #  define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
1132 #  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1133
1134 #  define SvMAGIC(sv)   (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*)  SvANY(sv))->xmg_u.xmg_magic))
1135 #  define SvSTASH(sv)   (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*)  SvANY(sv))->xmg_stash))
1136 #else
1137 #  define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
1138 #  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1139
1140 #  if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1141 /* These get expanded inside other macros that already use a variable _sv  */
1142 #    define SvPVX(sv)                                                   \
1143         (*({ SV *const _svpvx = MUTABLE_SV(sv);                         \
1144             assert(PL_valid_types_PVX[SvTYPE(_svpvx) & SVt_MASK]);      \
1145             assert(!isGV_with_GP(_svpvx));                              \
1146             assert(!(SvTYPE(_svpvx) == SVt_PVIO                         \
1147                      && !(IoFLAGS(_svpvx) & IOf_FAKE_DIRP)));           \
1148             &((_svpvx)->sv_u.svu_pv);                                   \
1149          }))
1150 #    define SvCUR(sv)                                                   \
1151         (*({ const SV *const _svcur = (const SV *)(sv);                 \
1152             assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]        \
1153                 || SvTYPE(_svcur) == SVt_REGEXP);                       \
1154             assert(!isGV_with_GP(_svcur));                              \
1155             assert(!(SvTYPE(_svcur) == SVt_PVIO                         \
1156                      && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP)));           \
1157             &(((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur);            \
1158          }))
1159 #    define SvIVX(sv)                                                   \
1160         (*({ const SV *const _svivx = (const SV *)(sv);                 \
1161             assert(PL_valid_types_IVX[SvTYPE(_svivx) & SVt_MASK]);      \
1162             assert(!isGV_with_GP(_svivx));                              \
1163             &(((XPVIV*) MUTABLE_PTR(SvANY(_svivx)))->xiv_iv);           \
1164          }))
1165 #    define SvUVX(sv)                                                   \
1166         (*({ const SV *const _svuvx = (const SV *)(sv);                 \
1167             assert(PL_valid_types_IVX[SvTYPE(_svuvx) & SVt_MASK]);      \
1168             assert(!isGV_with_GP(_svuvx));                              \
1169             &(((XPVUV*) MUTABLE_PTR(SvANY(_svuvx)))->xuv_uv);           \
1170          }))
1171 #    define SvNVX(sv)                                                   \
1172         (*({ const SV *const _svnvx = (const SV *)(sv);                 \
1173             assert(PL_valid_types_NVX[SvTYPE(_svnvx) & SVt_MASK]);      \
1174             assert(!isGV_with_GP(_svnvx));                              \
1175             &(((XPVNV*) MUTABLE_PTR(SvANY(_svnvx)))->xnv_u.xnv_nv);     \
1176          }))
1177 #    define SvRV(sv)                                                    \
1178         (*({ SV *const _svrv = MUTABLE_SV(sv);                          \
1179             assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]);        \
1180             assert(!isGV_with_GP(_svrv));                               \
1181             assert(!(SvTYPE(_svrv) == SVt_PVIO                          \
1182                      && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP)));            \
1183             &((_svrv)->sv_u.svu_rv);                                    \
1184          }))
1185 #    define SvRV_const(sv)                                              \
1186         ({ const SV *const _svrv = (const SV *)(sv);                    \
1187             assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]);        \
1188             assert(!isGV_with_GP(_svrv));                               \
1189             assert(!(SvTYPE(_svrv) == SVt_PVIO                          \
1190                      && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP)));            \
1191             (_svrv)->sv_u.svu_rv;                                       \
1192          })
1193 #    define SvMAGIC(sv)                                                 \
1194         (*({ const SV *const _svmagic = (const SV *)(sv);               \
1195             assert(SvTYPE(_svmagic) >= SVt_PVMG);                       \
1196             if(SvTYPE(_svmagic) == SVt_PVMG)                            \
1197                 assert(!SvPAD_OUR(_svmagic));                           \
1198             &(((XPVMG*) MUTABLE_PTR(SvANY(_svmagic)))->xmg_u.xmg_magic); \
1199           }))
1200 #    define SvSTASH(sv)                                                 \
1201         (*({ const SV *const _svstash = (const SV *)(sv);               \
1202             assert(SvTYPE(_svstash) >= SVt_PVMG);                       \
1203             &(((XPVMG*) MUTABLE_PTR(SvANY(_svstash)))->xmg_stash);      \
1204           }))
1205 #  else
1206 #    define SvPVX(sv) ((sv)->sv_u.svu_pv)
1207 #    define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
1208 #    define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
1209 #    define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
1210 #    define SvNVX(sv) ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv
1211 #    define SvRV(sv) ((sv)->sv_u.svu_rv)
1212 #    define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1213 #    define SvMAGIC(sv) ((XPVMG*)  SvANY(sv))->xmg_u.xmg_magic
1214 #    define SvSTASH(sv) ((XPVMG*)  SvANY(sv))->xmg_stash
1215 #  endif
1216 #endif
1217
1218 #ifndef PERL_POISON
1219 /* Given that these two are new, there can't be any existing code using them
1220  *  as LVALUEs  */
1221 #  define SvPVX_mutable(sv)     (0 + (sv)->sv_u.svu_pv)
1222 #  define SvPVX_const(sv)       ((const char*)(0 + (sv)->sv_u.svu_pv))
1223 #else
1224 /* Except for the poison code, which uses & to scribble over the pointer after
1225    free() is called.  */
1226 #  define SvPVX_mutable(sv)     ((sv)->sv_u.svu_pv)
1227 #  define SvPVX_const(sv)       ((const char*)((sv)->sv_u.svu_pv))
1228 #endif
1229
1230 #define SvIVXx(sv) SvIVX(sv)
1231 #define SvUVXx(sv) SvUVX(sv)
1232 #define SvNVXx(sv) SvNVX(sv)
1233 #define SvPVXx(sv) SvPVX(sv)
1234 #define SvLENx(sv) SvLEN(sv)
1235 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
1236
1237
1238 /* Ask a scalar nicely to try to become an IV, if possible.
1239    Not guaranteed to stay returning void */
1240 /* Macro won't actually call sv_2iv if already IOK */
1241 #define SvIV_please(sv) \
1242         STMT_START {if (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv))) \
1243                 (void) SvIV(sv); } STMT_END
1244 #define SvIV_please_nomg(sv) \
1245         (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv)) \
1246             ? (SvIV_nomg(sv), SvIOK(sv))          \
1247             : SvIOK(sv))
1248 #define SvIV_set(sv, val) \
1249         STMT_START { \
1250                 assert(PL_valid_types_IV_set[SvTYPE(sv) & SVt_MASK]);   \
1251                 assert(!isGV_with_GP(sv));              \
1252                 (((XPVIV*)  SvANY(sv))->xiv_iv = (val)); } STMT_END
1253 #define SvNV_set(sv, val) \
1254         STMT_START { \
1255                 assert(PL_valid_types_NV_set[SvTYPE(sv) & SVt_MASK]);   \
1256                 assert(!isGV_with_GP(sv));              \
1257                 (((XPVNV*)SvANY(sv))->xnv_u.xnv_nv = (val)); } STMT_END
1258 #define SvPV_set(sv, val) \
1259         STMT_START { \
1260                 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]);      \
1261                 assert(!isGV_with_GP(sv));              \
1262                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1263                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1264                 ((sv)->sv_u.svu_pv = (val)); } STMT_END
1265 #define SvUV_set(sv, val) \
1266         STMT_START { \
1267                 assert(PL_valid_types_IV_set[SvTYPE(sv) & SVt_MASK]);   \
1268                 assert(!isGV_with_GP(sv));              \
1269                 (((XPVUV*)SvANY(sv))->xuv_uv = (val)); } STMT_END
1270 #define SvRV_set(sv, val) \
1271         STMT_START { \
1272                 assert(PL_valid_types_RV[SvTYPE(sv) & SVt_MASK]);       \
1273                 assert(!isGV_with_GP(sv));              \
1274                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1275                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1276                 ((sv)->sv_u.svu_rv = (val)); } STMT_END
1277 #define SvMAGIC_set(sv, val) \
1278         STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1279                 (((XPVMG*)SvANY(sv))->xmg_u.xmg_magic = (val)); } STMT_END
1280 #define SvSTASH_set(sv, val) \
1281         STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1282                 (((XPVMG*)  SvANY(sv))->xmg_stash = (val)); } STMT_END
1283 #define SvCUR_set(sv, val) \
1284         STMT_START { \
1285                 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]        \
1286                         || SvTYPE(sv) == SVt_REGEXP);   \
1287                 assert(!isGV_with_GP(sv));              \
1288                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1289                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1290                 (((XPV*)  SvANY(sv))->xpv_cur = (val)); } STMT_END
1291 #define SvLEN_set(sv, val) \
1292         STMT_START { \
1293                 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]);      \
1294                 assert(!isGV_with_GP(sv));      \
1295                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1296                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1297                 (((XPV*)  SvANY(sv))->xpv_len = (val)); } STMT_END
1298 #define SvEND_set(sv, val) \
1299         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
1300                 SvCUR_set(sv, (val) - SvPVX(sv)); } STMT_END
1301
1302 #define SvPV_renew(sv,n) \
1303         STMT_START { SvLEN_set(sv, n); \
1304                 SvPV_set((sv), (MEM_WRAP_CHECK_(n,char)                 \
1305                                 (char*)saferealloc((Malloc_t)SvPVX(sv), \
1306                                                    (MEM_SIZE)((n)))));  \
1307                  } STMT_END
1308
1309 #define SvPV_shrink_to_cur(sv) STMT_START { \
1310                    const STRLEN _lEnGtH = SvCUR(sv) + 1; \
1311                    SvPV_renew(sv, _lEnGtH); \
1312                  } STMT_END
1313
1314 #define SvPV_free(sv)                                                   \
1315     STMT_START {                                                        \
1316                      assert(SvTYPE(sv) >= SVt_PV);                      \
1317                      if (SvLEN(sv)) {                                   \
1318                          assert(!SvROK(sv));                            \
1319                          if(SvOOK(sv)) {                                \
1320                              STRLEN zok;                                \
1321                              SvOOK_offset(sv, zok);                     \
1322                              SvPV_set(sv, SvPVX_mutable(sv) - zok);     \
1323                              SvFLAGS(sv) &= ~SVf_OOK;                   \
1324                          }                                              \
1325                          Safefree(SvPVX(sv));                           \
1326                      }                                                  \
1327                  } STMT_END
1328
1329 #ifdef PERL_CORE
1330 /* Code that crops up in three places to take a scalar and ready it to hold
1331    a reference */
1332 #  define prepare_SV_for_RV(sv)                                         \
1333     STMT_START {                                                        \
1334                     if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV)    \
1335                         sv_upgrade(sv, SVt_IV);                         \
1336                     else if (SvTYPE(sv) >= SVt_PV) {                    \
1337                         SvPV_free(sv);                                  \
1338                         SvLEN_set(sv, 0);                               \
1339                         SvCUR_set(sv, 0);                               \
1340                     }                                                   \
1341                  } STMT_END
1342 #endif
1343
1344 #ifndef PERL_CORE
1345 #  define BmFLAGS(sv)           (SvTAIL(sv) ? FBMcf_TAIL : 0)
1346 #endif
1347
1348 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1349 #  define BmRARE(sv)                                                    \
1350         (*({ SV *const _bmrare = MUTABLE_SV(sv);                        \
1351                 assert(SvTYPE(_bmrare) == SVt_PVMG);                    \
1352                 assert(SvVALID(_bmrare));                               \
1353             &(((XPVMG*) SvANY(_bmrare))->xnv_u.xbm_s.xbm_rare);         \
1354          }))
1355 #  define BmUSEFUL(sv)                                                  \
1356         (*({ SV *const _bmuseful = MUTABLE_SV(sv);                      \
1357             assert(SvTYPE(_bmuseful) == SVt_PVMG);                      \
1358             assert(SvVALID(_bmuseful));                                 \
1359             assert(!SvIOK(_bmuseful));                                  \
1360             &(((XPVMG*) SvANY(_bmuseful))->xnv_u.xbm_s.xbm_useful);     \
1361          }))
1362 #  define BmPREVIOUS(sv)                                                \
1363     (*({ SV *const _bmprevious = MUTABLE_SV(sv);                        \
1364                 assert(SvTYPE(_bmprevious) == SVt_PVMG);                \
1365                 assert(SvVALID(_bmprevious));                           \
1366             &(((XPVMG*) SvANY(_bmprevious))->xiv_u.xivu_uv);            \
1367          }))
1368 #else
1369 #  define BmRARE(sv)            ((XPVMG*) SvANY(sv))->xnv_u.xbm_s.xbm_rare
1370 #  define BmUSEFUL(sv)          ((XPVMG*) SvANY(sv))->xnv_u.xbm_s.xbm_useful
1371 #  define BmPREVIOUS(sv)        ((XPVMG*) SvANY(sv))->xiv_u.xivu_uv
1372
1373 #endif
1374
1375 #define FmLINES(sv)     ((XPVIV*)  SvANY(sv))->xiv_iv
1376
1377 #define LvTYPE(sv)      ((XPVLV*)  SvANY(sv))->xlv_type
1378 #define LvTARG(sv)      ((XPVLV*)  SvANY(sv))->xlv_targ
1379 #define LvTARGOFF(sv)   ((XPVLV*)  SvANY(sv))->xlv_targoff
1380 #define LvTARGLEN(sv)   ((XPVLV*)  SvANY(sv))->xlv_targlen
1381 #define LvFLAGS(sv)     ((XPVLV*)  SvANY(sv))->xlv_flags
1382
1383 #define IoIFP(sv)       (sv)->sv_u.svu_fp
1384 #define IoOFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ofp
1385 #define IoDIRP(sv)      ((XPVIO*)  SvANY(sv))->xio_dirp
1386 #define IoANY(sv)       ((XPVIO*)  SvANY(sv))->xio_any
1387 #define IoLINES(sv)     ((XPVIO*)  SvANY(sv))->xiv_u.xivu_iv
1388 #define IoPAGE(sv)      ((XPVIO*)  SvANY(sv))->xio_page
1389 #define IoPAGE_LEN(sv)  ((XPVIO*)  SvANY(sv))->xio_page_len
1390 #define IoLINES_LEFT(sv)((XPVIO*)  SvANY(sv))->xio_lines_left
1391 #define IoTOP_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_top_name
1392 #define IoTOP_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_top_gv
1393 #define IoFMT_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_fmt_name
1394 #define IoFMT_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_fmt_gv
1395 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
1396 #define IoBOTTOM_GV(sv) ((XPVIO*)  SvANY(sv))->xio_bottom_gv
1397 #define IoTYPE(sv)      ((XPVIO*)  SvANY(sv))->xio_type
1398 #define IoFLAGS(sv)     ((XPVIO*)  SvANY(sv))->xio_flags
1399
1400 /* IoTYPE(sv) is a single character telling the type of I/O connection. */
1401 #define IoTYPE_RDONLY           '<'
1402 #define IoTYPE_WRONLY           '>'
1403 #define IoTYPE_RDWR             '+'
1404 #define IoTYPE_APPEND           'a'
1405 #define IoTYPE_PIPE             '|'
1406 #define IoTYPE_STD              '-'     /* stdin or stdout */
1407 #define IoTYPE_SOCKET           's'
1408 #define IoTYPE_CLOSED           ' '
1409 #define IoTYPE_IMPLICIT         'I'     /* stdin or stdout or stderr */
1410 #define IoTYPE_NUMERIC          '#'     /* fdopen */
1411
1412 /*
1413 =for apidoc Am|bool|SvTAINTED|SV* sv
1414 Checks to see if an SV is tainted.  Returns TRUE if it is, FALSE if
1415 not.
1416
1417 =for apidoc Am|void|SvTAINTED_on|SV* sv
1418 Marks an SV as tainted if tainting is enabled.
1419
1420 =for apidoc Am|void|SvTAINTED_off|SV* sv
1421 Untaints an SV.  Be I<very> careful with this routine, as it short-circuits
1422 some of Perl's fundamental security features.  XS module authors should not
1423 use this function unless they fully understand all the implications of
1424 unconditionally untainting the value. Untainting should be done in the
1425 standard perl fashion, via a carefully crafted regexp, rather than directly
1426 untainting variables.
1427
1428 =for apidoc Am|void|SvTAINT|SV* sv
1429 Taints an SV if tainting is enabled, and if some input to the current
1430 expression is tainted--usually a variable, but possibly also implicit
1431 inputs such as locale settings.  C<SvTAINT> propagates that taintedness to
1432 the outputs of an expression in a pessimistic fashion; i.e., without paying
1433 attention to precisely which outputs are influenced by which inputs.
1434
1435 =cut
1436 */
1437
1438 #define sv_taint(sv)      sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)
1439
1440 #if NO_TAINT_SUPPORT
1441 #   define SvTAINTED(sv) 0
1442 #else
1443 #   define SvTAINTED(sv)          (SvMAGICAL(sv) && sv_tainted(sv))
1444 #endif
1445 #define SvTAINTED_on(sv)  STMT_START{ if(TAINTING_get){sv_taint(sv);}   }STMT_END
1446 #define SvTAINTED_off(sv) STMT_START{ if(TAINTING_get){sv_untaint(sv);} }STMT_END
1447
1448 #define SvTAINT(sv)                     \
1449     STMT_START {                        \
1450         if (TAINTING_get) {             \
1451             if (TAINT_get)              \
1452                 SvTAINTED_on(sv);       \
1453         }                               \
1454     } STMT_END
1455
1456 /*
1457 =for apidoc Am|char*|SvPV_force|SV* sv|STRLEN len
1458 Like C<SvPV> but will force the SV into containing a string (C<SvPOK>), and
1459 only a string (C<SvPOK_only>), by hook or by crook.  You want force if you are
1460 going to update the C<SvPVX> directly.  Processes get magic.
1461
1462 =for apidoc Am|char*|SvPV_force_nomg|SV* sv|STRLEN len
1463 Like C<SvPV> but will force the SV into containing a string (C<SvPOK>), and
1464 only a string (C<SvPOK_only>), by hook or by crook.  You want force if you are
1465 going to update the C<SvPVX> directly.  Doesn't process get magic.
1466
1467 =for apidoc Am|char*|SvPV|SV* sv|STRLEN len
1468 Returns a pointer to the string in the SV, or a stringified form of
1469 the SV if the SV does not contain a string.  The SV may cache the
1470 stringified version becoming C<SvPOK>.  Handles 'get' magic.  See also
1471 C<SvPVx> for a version which guarantees to evaluate sv only once.
1472
1473 =for apidoc Am|char*|SvPVx|SV* sv|STRLEN len
1474 A version of C<SvPV> which guarantees to evaluate C<sv> only once.
1475 Only use this if C<sv> is an expression with side effects, otherwise use the
1476 more efficient C<SvPV>.
1477
1478 =for apidoc Am|char*|SvPV_nomg|SV* sv|STRLEN len
1479 Like C<SvPV> but doesn't process magic.
1480
1481 =for apidoc Am|char*|SvPV_nolen|SV* sv
1482 Returns a pointer to the string in the SV, or a stringified form of
1483 the SV if the SV does not contain a string.  The SV may cache the
1484 stringified form becoming C<SvPOK>.  Handles 'get' magic.
1485
1486 =for apidoc Am|char*|SvPV_nomg_nolen|SV* sv
1487 Like C<SvPV_nolen> but doesn't process magic.
1488
1489 =for apidoc Am|IV|SvIV|SV* sv
1490 Coerces the given SV to an integer and returns it.  See C<SvIVx> for a
1491 version which guarantees to evaluate sv only once.
1492
1493 =for apidoc Am|IV|SvIV_nomg|SV* sv
1494 Like C<SvIV> but doesn't process magic.
1495
1496 =for apidoc Am|IV|SvIVx|SV* sv
1497 Coerces the given SV to an integer and returns it.
1498 Guarantees to evaluate C<sv> only once.  Only use
1499 this if C<sv> is an expression with side effects,
1500 otherwise use the more efficient C<SvIV>.
1501
1502 =for apidoc Am|NV|SvNV|SV* sv
1503 Coerce the given SV to a double and return it.  See C<SvNVx> for a version
1504 which guarantees to evaluate sv only once.
1505
1506 =for apidoc Am|NV|SvNV_nomg|SV* sv
1507 Like C<SvNV> but doesn't process magic.
1508
1509 =for apidoc Am|NV|SvNVx|SV* sv
1510 Coerces the given SV to a double and returns it.
1511 Guarantees to evaluate C<sv> only once.  Only use
1512 this if C<sv> is an expression with side effects,
1513 otherwise use the more efficient C<SvNV>.
1514
1515 =for apidoc Am|UV|SvUV|SV* sv
1516 Coerces the given SV to an unsigned integer and returns it.  See C<SvUVx>
1517 for a version which guarantees to evaluate sv only once.
1518
1519 =for apidoc Am|UV|SvUV_nomg|SV* sv
1520 Like C<SvUV> but doesn't process magic.
1521
1522 =for apidoc Am|UV|SvUVx|SV* sv
1523 Coerces the given SV to an unsigned integer and
1524 returns it.  Guarantees to C<sv> only once.  Only
1525 use this if C<sv> is an expression with side effects,
1526 otherwise use the more efficient C<SvUV>.
1527
1528 =for apidoc Am|bool|SvTRUE|SV* sv
1529 Returns a boolean indicating whether Perl would evaluate the SV as true or
1530 false.  See SvOK() for a defined/undefined test.  Handles 'get' magic
1531 unless the scalar is already SvPOK, SvIOK or SvNOK (the public, not the
1532 private flags).
1533
1534 =for apidoc Am|bool|SvTRUE_nomg|SV* sv
1535 Returns a boolean indicating whether Perl would evaluate the SV as true or
1536 false.  See SvOK() for a defined/undefined test.  Does not handle 'get' magic.
1537
1538 =for apidoc Am|char*|SvPVutf8_force|SV* sv|STRLEN len
1539 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
1540
1541 =for apidoc Am|char*|SvPVutf8|SV* sv|STRLEN len
1542 Like C<SvPV>, but converts sv to utf8 first if necessary.
1543
1544 =for apidoc Am|char*|SvPVutf8_nolen|SV* sv
1545 Like C<SvPV_nolen>, but converts sv to utf8 first if necessary.
1546
1547 =for apidoc Am|char*|SvPVbyte_force|SV* sv|STRLEN len
1548 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
1549
1550 =for apidoc Am|char*|SvPVbyte|SV* sv|STRLEN len
1551 Like C<SvPV>, but converts sv to byte representation first if necessary.
1552
1553 =for apidoc Am|char*|SvPVbyte_nolen|SV* sv
1554 Like C<SvPV_nolen>, but converts sv to byte representation first if necessary.
1555
1556 =for apidoc Am|char*|SvPVutf8x_force|SV* sv|STRLEN len
1557 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
1558 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8_force>
1559 otherwise.
1560
1561 =for apidoc Am|char*|SvPVutf8x|SV* sv|STRLEN len
1562 Like C<SvPV>, but converts sv to utf8 first if necessary.
1563 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8>
1564 otherwise.
1565
1566 =for apidoc Am|char*|SvPVbytex_force|SV* sv|STRLEN len
1567 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
1568 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte_force>
1569 otherwise.
1570
1571 =for apidoc Am|char*|SvPVbytex|SV* sv|STRLEN len
1572 Like C<SvPV>, but converts sv to byte representation first if necessary.
1573 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte>
1574 otherwise.
1575
1576 =for apidoc Am|bool|SvIsCOW|SV* sv
1577 Returns a boolean indicating whether the SV is Copy-On-Write (either shared
1578 hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
1579 COW).
1580
1581 =for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
1582 Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
1583 scalar.
1584
1585 =for apidoc Am|void|sv_catpvn_nomg|SV* sv|const char* ptr|STRLEN len
1586 Like C<sv_catpvn> but doesn't process magic.
1587
1588 =for apidoc Am|void|sv_catpv_nomg|SV* sv|const char* ptr
1589 Like C<sv_catpv> but doesn't process magic.
1590
1591 =for apidoc Am|void|sv_setsv_nomg|SV* dsv|SV* ssv
1592 Like C<sv_setsv> but doesn't process magic.
1593
1594 =for apidoc Am|void|sv_catsv_nomg|SV* dsv|SV* ssv
1595 Like C<sv_catsv> but doesn't process magic.
1596
1597 =for apidoc Amdb|STRLEN|sv_utf8_upgrade_nomg|NN SV *sv
1598
1599 Like sv_utf8_upgrade, but doesn't do magic on C<sv>.
1600
1601 =cut
1602 */
1603
1604 /* Let us hope that bitmaps for UV and IV are the same */
1605 #define SvIV(sv) (SvIOK_nog(sv) ? SvIVX(sv) : sv_2iv(sv))
1606 #define SvUV(sv) (SvUOK_nog(sv) ? SvUVX(sv) : sv_2uv(sv))
1607 #define SvNV(sv) (SvNOK_nog(sv) ? SvNVX(sv) : sv_2nv(sv))
1608
1609 #define SvIV_nomg(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv_flags(sv, 0))
1610 #define SvUV_nomg(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv_flags(sv, 0))
1611 #define SvNV_nomg(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv_flags(sv, 0))
1612
1613 /* ----*/
1614
1615 #define SvPV(sv, lp)         SvPV_flags(sv, lp, SV_GMAGIC)
1616 #define SvPV_const(sv, lp)   SvPV_flags_const(sv, lp, SV_GMAGIC)
1617 #define SvPV_mutable(sv, lp) SvPV_flags_mutable(sv, lp, SV_GMAGIC)
1618
1619 #define SvPV_flags(sv, lp, flags) \
1620     (SvPOK_nog(sv) \
1621      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv_flags(sv, &lp, flags))
1622 #define SvPV_flags_const(sv, lp, flags) \
1623     (SvPOK_nog(sv) \
1624      ? ((lp = SvCUR(sv)), SvPVX_const(sv)) : \
1625      (const char*) sv_2pv_flags(sv, &lp, flags|SV_CONST_RETURN))
1626 #define SvPV_flags_const_nolen(sv, flags) \
1627     (SvPOK_nog(sv) \
1628      ? SvPVX_const(sv) : \
1629      (const char*) sv_2pv_flags(sv, 0, flags|SV_CONST_RETURN))
1630 #define SvPV_flags_mutable(sv, lp, flags) \
1631     (SvPOK_nog(sv) \
1632      ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) : \
1633      sv_2pv_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
1634
1635 #define SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
1636 #define SvPV_force_nolen(sv) SvPV_force_flags_nolen(sv, SV_GMAGIC)
1637 #define SvPV_force_mutable(sv, lp) SvPV_force_flags_mutable(sv, lp, SV_GMAGIC)
1638
1639 #define SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
1640 #define SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)
1641
1642 #define SvPV_force_flags(sv, lp, flags) \
1643     (SvPOK_pure_nogthink(sv) \
1644      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))
1645
1646 #define SvPV_force_flags_nolen(sv, flags) \
1647     (SvPOK_pure_nogthink(sv) \
1648      ? SvPVX(sv) : sv_pvn_force_flags(sv, 0, flags))
1649
1650 #define SvPV_force_flags_mutable(sv, lp, flags) \
1651     (SvPOK_pure_nogthink(sv) \
1652      ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) \
1653      : sv_pvn_force_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
1654
1655 #define SvPV_nolen(sv) \
1656     (SvPOK_nog(sv) \
1657      ? SvPVX(sv) : sv_2pv_flags(sv, 0, SV_GMAGIC))
1658
1659 #define SvPV_nomg_nolen(sv) \
1660     (SvPOK_nog(sv) \
1661      ? SvPVX(sv) : sv_2pv_flags(sv, 0, 0))
1662
1663 #define SvPV_nolen_const(sv) \
1664     (SvPOK_nog(sv) \
1665      ? SvPVX_const(sv) : sv_2pv_flags(sv, 0, SV_GMAGIC|SV_CONST_RETURN))
1666
1667 #define SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
1668 #define SvPV_nomg_const(sv, lp) SvPV_flags_const(sv, lp, 0)
1669 #define SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
1670
1671 /* ----*/
1672
1673 #define SvPVutf8(sv, lp) \
1674     (SvPOK_utf8_nog(sv) \
1675      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvutf8(sv, &lp))
1676
1677 #define SvPVutf8_force(sv, lp) \
1678     (SvPOK_utf8_pure_nogthink(sv) \
1679      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
1680
1681 #define SvPVutf8_nolen(sv) \
1682     (SvPOK_utf8_nog(sv) \
1683      ? SvPVX(sv) : sv_2pvutf8(sv, 0))
1684
1685 /* ----*/
1686
1687 #define SvPVbyte(sv, lp) \
1688     (SvPOK_byte_nog(sv) \
1689      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))
1690
1691 #define SvPVbyte_force(sv, lp) \
1692     (SvPOK_byte_pure_nogthink(sv) \
1693      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvbyten_force(sv, &lp))
1694
1695 #define SvPVbyte_nolen(sv) \
1696     (SvPOK_byte_nog(sv) \
1697      ? SvPVX(sv) : sv_2pvbyte(sv, 0))
1698
1699     
1700 /* define FOOx(): idempotent versions of FOO(). If possible, use a local
1701  * var to evaluate the arg once; failing that, use a global if possible;
1702  * failing that, call a function to do the work
1703  */
1704
1705 #define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
1706 #define SvPVutf8x_force(sv, lp) sv_pvutf8n_force(sv, &lp)
1707 #define SvPVbytex_force(sv, lp) sv_pvbyten_force(sv, &lp)
1708
1709 #define SvTRUE(sv)        ((sv) && (SvGMAGICAL(sv) ? sv_2bool(sv) : SvTRUE_common(sv, sv_2bool_nomg(sv))))
1710 #define SvTRUE_nomg(sv)   ((sv) && (                                SvTRUE_common(sv, sv_2bool_nomg(sv))))
1711 #define SvTRUE_common(sv,fallback) (                    \
1712       !SvOK(sv)                                         \
1713         ? 0                                             \
1714     : (SvFLAGS(sv) & (SVf_POK|SVf_IOK|SVf_NOK))         \
1715         ? (   (SvPOK(sv) && SvPVXtrue(sv))              \
1716            || (SvIOK(sv) && SvIVX(sv) != 0)             \
1717            || (SvNOK(sv) && SvNVX(sv) != 0.0))          \
1718     : (fallback))
1719
1720 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1721
1722 #  define SvIVx(sv) ({SV *_sv = MUTABLE_SV(sv); SvIV(_sv); })
1723 #  define SvUVx(sv) ({SV *_sv = MUTABLE_SV(sv); SvUV(_sv); })
1724 #  define SvNVx(sv) ({SV *_sv = MUTABLE_SV(sv); SvNV(_sv); })
1725 #  define SvPVx(sv, lp) ({SV *_sv = (sv); SvPV(_sv, lp); })
1726 #  define SvPVx_const(sv, lp) ({SV *_sv = (sv); SvPV_const(_sv, lp); })
1727 #  define SvPVx_nolen(sv) ({SV *_sv = (sv); SvPV_nolen(_sv); })
1728 #  define SvPVx_nolen_const(sv) ({SV *_sv = (sv); SvPV_nolen_const(_sv); })
1729 #  define SvPVutf8x(sv, lp) ({SV *_sv = (sv); SvPVutf8(_sv, lp); })
1730 #  define SvPVbytex(sv, lp) ({SV *_sv = (sv); SvPVbyte(_sv, lp); })
1731 #  define SvPVbytex_nolen(sv) ({SV *_sv = (sv); SvPVbyte_nolen(_sv); })
1732 #  define SvTRUEx(sv)      ({SV *_sv = (sv); SvTRUE(_sv); })
1733 #  define SvTRUEx_nomg(sv) ({SV *_sv = (sv); SvTRUE_nomg(_sv); })
1734 #  define SvPVXtrue(sv)                                         \
1735     ({XPV *nxpv;                                                \
1736      (nxpv = (XPV*)SvANY(sv))                                   \
1737       && (nxpv->xpv_cur > 1                                     \
1738           || (nxpv->xpv_cur && *(sv)->sv_u.svu_pv != '0'));})
1739
1740 #else /* __GNUC__ */
1741
1742 /* These inlined macros use globals, which will require a thread
1743  * declaration in user code, so we avoid them under threads */
1744
1745 #  define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
1746 #  define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
1747 #  define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
1748 #  define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
1749 #  define SvPVx_const(sv, lp) ((PL_Sv = (sv)), SvPV_const(PL_Sv, lp))
1750 #  define SvPVx_nolen(sv) ((PL_Sv = (sv)), SvPV_nolen(PL_Sv))
1751 #  define SvPVx_nolen_const(sv) ((PL_Sv = (sv)), SvPV_nolen_const(PL_Sv))
1752 #  define SvPVutf8x(sv, lp) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, lp))
1753 #  define SvPVbytex(sv, lp) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, lp))
1754 #  define SvPVbytex_nolen(sv) ((PL_Sv = (sv)), SvPVbyte_nolen(PL_Sv))
1755 #  define SvTRUEx(sv)      ((PL_Sv = (sv)), SvTRUE(PL_Sv))
1756 #  define SvTRUEx_nomg(sv) ((PL_Sv = (sv)), SvTRUE_nomg(PL_Sv))
1757 #  define SvPVXtrue(sv)                                         \
1758     ((PL_Xpv = (XPV*)SvANY(PL_Sv = (sv)))                       \
1759      && (PL_Xpv->xpv_cur > 1                                    \
1760          || (PL_Xpv->xpv_cur && *PL_Sv->sv_u.svu_pv != '0')))
1761 #endif /* __GNU__ */
1762
1763 #define SvIsCOW(sv)             (SvFLAGS(sv) & SVf_IsCOW)
1764 #define SvIsCOW_on(sv)          (SvFLAGS(sv) |= SVf_IsCOW)
1765 #define SvIsCOW_off(sv)         (SvFLAGS(sv) &= ~SVf_IsCOW)
1766 #define SvIsCOW_shared_hash(sv) (SvIsCOW(sv) && SvLEN(sv) == 0)
1767
1768 #define SvSHARED_HEK_FROM_PV(pvx) \
1769         ((struct hek*)(pvx - STRUCT_OFFSET(struct hek, hek_key)))
1770 #define SvSHARED_HASH(sv) (0 + SvSHARED_HEK_FROM_PV(SvPVX_const(sv))->hek_hash)
1771
1772 /* flag values for sv_*_flags functions */
1773 #define SV_IMMEDIATE_UNREF      1
1774 #define SV_GMAGIC               2
1775 #define SV_COW_DROP_PV          4
1776 #define SV_UTF8_NO_ENCODING     8
1777 #define SV_NOSTEAL              16
1778 #define SV_CONST_RETURN         32
1779 #define SV_MUTABLE_RETURN       64
1780 #define SV_SMAGIC               128
1781 #define SV_HAS_TRAILING_NUL     256
1782 #define SV_COW_SHARED_HASH_KEYS 512
1783 /* This one is only enabled for PERL_OLD_COPY_ON_WRITE */
1784 #define SV_COW_OTHER_PVS        1024
1785 /* Make sv_2pv_flags return NULL if something is undefined.  */
1786 #define SV_UNDEF_RETURNS_NULL   2048
1787 /* Tell sv_utf8_upgrade() to not check to see if an upgrade is really needed.
1788  * This is used when the caller has already determined it is, and avoids
1789  * redundant work */
1790 #define SV_FORCE_UTF8_UPGRADE   4096
1791 /* if (after resolving magic etc), the SV is found to be overloaded,
1792  * don't call the overload magic, just return as-is */
1793 #define SV_SKIP_OVERLOAD        8192
1794 /* It is not yet clear whether we want this as an API, or what the
1795  * constants should be named. */
1796 #ifdef PERL_CORE
1797 # define SV_CATBYTES            16384
1798 # define SV_CATUTF8             32768
1799 #endif
1800
1801 /* The core is safe for this COW optimisation. XS code on CPAN may not be.
1802    So only default to doing the COW setup if we're in the core.
1803  */
1804 #ifdef PERL_CORE
1805 #  ifndef SV_DO_COW_SVSETSV
1806 #    define SV_DO_COW_SVSETSV   SV_COW_SHARED_HASH_KEYS|SV_COW_OTHER_PVS
1807 #  endif
1808 #endif
1809
1810 #ifndef SV_DO_COW_SVSETSV
1811 #  define SV_DO_COW_SVSETSV     0
1812 #endif
1813
1814
1815 #define sv_unref(sv)            sv_unref_flags(sv, 0)
1816 #define sv_force_normal(sv)     sv_force_normal_flags(sv, 0)
1817 #define sv_usepvn(sv, p, l)     sv_usepvn_flags(sv, p, l, 0)
1818 #define sv_usepvn_mg(sv, p, l)  sv_usepvn_flags(sv, p, l, SV_SMAGIC)
1819
1820 /* We are about to replace the SV's current value. So if it's copy on write
1821    we need to normalise it. Use the SV_COW_DROP_PV flag hint to say that
1822    the value is about to get thrown away, so drop the PV rather than go to
1823    the effort of making a read-write copy only for it to get immediately
1824    discarded.  */
1825
1826 #define SV_CHECK_THINKFIRST_COW_DROP(sv) if (SvTHINKFIRST(sv)) \
1827                                     sv_force_normal_flags(sv, SV_COW_DROP_PV)
1828
1829 #ifdef PERL_OLD_COPY_ON_WRITE
1830 #define SvRELEASE_IVX(sv)   \
1831     ((SvIsCOW(sv) ? sv_force_normal_flags(sv, 0) : (void) 0), 0)
1832 #  define SvIsCOW_normal(sv)    (SvIsCOW(sv) && SvLEN(sv))
1833 #  define SvRELEASE_IVX_(sv)    SvRELEASE_IVX(sv),
1834 #else
1835 #  define SvRELEASE_IVX(sv)   0
1836 /* This little game brought to you by the need to shut this warning up:
1837 mg.c: In function 'Perl_magic_get':
1838 mg.c:1024: warning: left-hand operand of comma expression has no effect
1839 */
1840 #  define SvRELEASE_IVX_(sv)  /**/
1841 #endif /* PERL_OLD_COPY_ON_WRITE */
1842
1843 #define CAN_COW_MASK    (SVs_OBJECT|SVs_GMG|SVs_SMG|SVs_RMG|SVf_IOK|SVf_NOK| \
1844                          SVf_POK|SVf_ROK|SVp_IOK|SVp_NOK|SVp_POK|SVf_FAKE| \
1845                          SVf_OOK|SVf_BREAK|SVf_READONLY)
1846 #define CAN_COW_FLAGS   (SVp_POK|SVf_POK)
1847
1848 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) \
1849                                     sv_force_normal_flags(sv, 0)
1850
1851
1852 /* all these 'functions' are now just macros */
1853
1854 #define sv_pv(sv) SvPV_nolen(sv)
1855 #define sv_pvutf8(sv) SvPVutf8_nolen(sv)
1856 #define sv_pvbyte(sv) SvPVbyte_nolen(sv)
1857
1858 #define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
1859 #define sv_utf8_upgrade_flags(sv, flags) sv_utf8_upgrade_flags_grow(sv, flags, 0)
1860 #define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
1861 #define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
1862 #define sv_catpv_nomg(dsv, sstr) sv_catpv_flags(dsv, sstr, 0)
1863 #define sv_setsv(dsv, ssv) \
1864         sv_setsv_flags(dsv, ssv, SV_GMAGIC|SV_DO_COW_SVSETSV)
1865 #define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_DO_COW_SVSETSV)
1866 #define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
1867 #define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
1868 #define sv_catsv_mg(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC|SV_SMAGIC)
1869 #define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
1870 #define sv_catpvn_mg(sv, sstr, slen) sv_catpvn_flags(sv, sstr, slen, SV_GMAGIC|SV_SMAGIC);
1871 #define sv_copypv(dsv, ssv) sv_copypv_flags(dsv, ssv, SV_GMAGIC)
1872 #define sv_copypv_nomg(dsv, ssv) sv_copypv_flags(dsv, ssv, 0)
1873 #define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
1874 #define sv_2pv_nolen(sv) sv_2pv(sv, 0)
1875 #define sv_2pvbyte_nolen(sv) sv_2pvbyte(sv, 0)
1876 #define sv_2pvutf8_nolen(sv) sv_2pvutf8(sv, 0)
1877 #define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
1878 #define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
1879 #define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
1880 #define sv_2iv(sv) sv_2iv_flags(sv, SV_GMAGIC)
1881 #define sv_2uv(sv) sv_2uv_flags(sv, SV_GMAGIC)
1882 #define sv_2nv(sv) sv_2nv_flags(sv, SV_GMAGIC)
1883 #define sv_eq(sv1, sv2) sv_eq_flags(sv1, sv2, SV_GMAGIC)
1884 #define sv_cmp(sv1, sv2) sv_cmp_flags(sv1, sv2, SV_GMAGIC)
1885 #define sv_cmp_locale(sv1, sv2) sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC)
1886 #define sv_collxfrm(sv, nxp) sv_cmp_flags(sv, nxp, SV_GMAGIC)
1887 #define sv_2bool(sv) sv_2bool_flags(sv, SV_GMAGIC)
1888 #define sv_2bool_nomg(sv) sv_2bool_flags(sv, 0)
1889 #define sv_insert(bigstr, offset, len, little, littlelen)               \
1890         Perl_sv_insert_flags(aTHX_ (bigstr),(offset), (len), (little),  \
1891                              (littlelen), SV_GMAGIC)
1892 #define sv_mortalcopy(sv) \
1893         Perl_sv_mortalcopy_flags(aTHX_ sv, SV_GMAGIC|SV_DO_COW_SVSETSV)
1894
1895 /* Should be named SvCatPVN_utf8_upgrade? */
1896 #define sv_catpvn_nomg_utf8_upgrade(dsv, sstr, slen, nsv)       \
1897         STMT_START {                                    \
1898             if (!(nsv))                                 \
1899                 nsv = newSVpvn_flags(sstr, slen, SVs_TEMP);     \
1900             else                                        \
1901                 sv_setpvn(nsv, sstr, slen);             \
1902             SvUTF8_off(nsv);                            \
1903             sv_utf8_upgrade(nsv);                       \
1904             sv_catsv_nomg(dsv, nsv);                    \
1905         } STMT_END
1906 #define sv_catpvn_nomg_maybeutf8(dsv, sstr, slen, is_utf8) \
1907         sv_catpvn_flags(dsv, sstr, slen, (is_utf8)?SV_CATUTF8:SV_CATBYTES)
1908
1909 #ifdef PERL_CORE
1910 # define sv_or_pv_len_utf8(sv, pv, bytelen)           \
1911     (SvGAMAGIC(sv)                                     \
1912         ? utf8_length((U8 *)(pv), (U8 *)(pv)+(bytelen)) \
1913         : sv_len_utf8(sv))
1914 # define sv_or_pv_pos_u2b(sv,s,p,lp) S_sv_or_pv_pos_u2b(aTHX_ sv,s,p,lp)
1915 #endif
1916
1917 /*
1918 =for apidoc Am|SV*|newRV_inc|SV* sv
1919
1920 Creates an RV wrapper for an SV.  The reference count for the original SV is
1921 incremented.
1922
1923 =cut
1924 */
1925
1926 #define newRV_inc(sv)   newRV(sv)
1927
1928 /* the following macros update any magic values this sv is associated with */
1929
1930 /*
1931 =head1 Magical Functions
1932
1933 =for apidoc Am|void|SvGETMAGIC|SV* sv
1934 Invokes C<mg_get> on an SV if it has 'get' magic.  For example, this
1935 will call C<FETCH> on a tied variable.  This macro evaluates its
1936 argument more than once.
1937
1938 =for apidoc Am|void|SvSETMAGIC|SV* sv
1939 Invokes C<mg_set> on an SV if it has 'set' magic.  This is necessary
1940 after modifying a scalar, in case it is a magical variable like C<$|>
1941 or a tied variable (it calls C<STORE>).  This macro evaluates its
1942 argument more than once.
1943
1944 =for apidoc Am|void|SvSetSV|SV* dsb|SV* ssv
1945 Calls C<sv_setsv> if dsv is not the same as ssv.  May evaluate arguments
1946 more than once.
1947
1948 =for apidoc Am|void|SvSetSV_nosteal|SV* dsv|SV* ssv
1949 Calls a non-destructive version of C<sv_setsv> if dsv is not the same as
1950 ssv.  May evaluate arguments more than once.
1951
1952 =for apidoc Am|void|SvSetMagicSV|SV* dsb|SV* ssv
1953 Like C<SvSetSV>, but does any set magic required afterwards.
1954
1955 =for apidoc Am|void|SvSetMagicSV_nosteal|SV* dsv|SV* ssv
1956 Like C<SvSetSV_nosteal>, but does any set magic required afterwards.
1957
1958 =for apidoc Am|void|SvSHARE|SV* sv
1959 Arranges for sv to be shared between threads if a suitable module
1960 has been loaded.
1961
1962 =for apidoc Am|void|SvLOCK|SV* sv
1963 Arranges for a mutual exclusion lock to be obtained on sv if a suitable module
1964 has been loaded.
1965
1966 =for apidoc Am|void|SvUNLOCK|SV* sv
1967 Releases a mutual exclusion lock on sv if a suitable module
1968 has been loaded.
1969
1970 =head1 SV Manipulation Functions
1971
1972 =for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
1973 Expands the character buffer in the SV so that it has room for the
1974 indicated number of bytes (remember to reserve space for an extra trailing
1975 NUL character).  Calls C<sv_grow> to perform the expansion if necessary.
1976 Returns a pointer to the character buffer.
1977
1978 =cut
1979 */
1980
1981 #define SvSHARE(sv) PL_sharehook(aTHX_ sv)
1982 #define SvLOCK(sv) PL_lockhook(aTHX_ sv)
1983 #define SvUNLOCK(sv) PL_unlockhook(aTHX_ sv)
1984 #define SvDESTROYABLE(sv) PL_destroyhook(aTHX_ sv)
1985
1986 #define SvGETMAGIC(x) ((void)(SvGMAGICAL(x) && mg_get(x)))
1987 #define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
1988
1989 #define SvSetSV_and(dst,src,finally) \
1990         STMT_START {                                    \
1991             if ((dst) != (src)) {                       \
1992                 sv_setsv(dst, src);                     \
1993                 finally;                                \
1994             }                                           \
1995         } STMT_END
1996 #define SvSetSV_nosteal_and(dst,src,finally) \
1997         STMT_START {                                    \
1998             if ((dst) != (src)) {                       \
1999                 sv_setsv_flags(dst, src, SV_GMAGIC | SV_NOSTEAL | SV_DO_COW_SVSETSV);   \
2000                 finally;                                \
2001             }                                           \
2002         } STMT_END
2003
2004 #define SvSetSV(dst,src) \
2005                 SvSetSV_and(dst,src,/*nothing*/;)
2006 #define SvSetSV_nosteal(dst,src) \
2007                 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
2008
2009 #define SvSetMagicSV(dst,src) \
2010                 SvSetSV_and(dst,src,SvSETMAGIC(dst))
2011 #define SvSetMagicSV_nosteal(dst,src) \
2012                 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
2013
2014
2015 #if !defined(SKIP_DEBUGGING)
2016 #define SvPEEK(sv) sv_peek(sv)
2017 #else
2018 #define SvPEEK(sv) ""
2019 #endif
2020
2021 #define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no || (sv)==&PL_sv_placeholder)
2022
2023 /*
2024 =for apidoc Am|SV *|boolSV|bool b
2025
2026 Returns a true SV if C<b> is a true value, or a false SV if C<b> is 0.
2027
2028 See also C<PL_sv_yes> and C<PL_sv_no>.
2029
2030 =cut
2031 */
2032
2033 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
2034
2035 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
2036 /* If I give every macro argument a different name, then there won't be bugs
2037    where nested macros get confused. Been there, done that.  */
2038 #define isGV_with_GP(pwadak) \
2039         (((SvFLAGS(pwadak) & (SVp_POK|SVpgv_GP)) == SVpgv_GP)   \
2040         && (SvTYPE(pwadak) == SVt_PVGV || SvTYPE(pwadak) == SVt_PVLV))
2041 #define isGV_with_GP_on(sv)     STMT_START {                           \
2042         assert (SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV); \
2043         assert (!SvPOKp(sv));                                          \
2044         assert (!SvIOKp(sv));                                          \
2045         (SvFLAGS(sv) |= SVpgv_GP);                                     \
2046     } STMT_END
2047 #define isGV_with_GP_off(sv)    STMT_START {                           \
2048         assert (SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV); \
2049         assert (!SvPOKp(sv));                                          \
2050         assert (!SvIOKp(sv));                                          \
2051         (SvFLAGS(sv) &= ~SVpgv_GP);                                    \
2052     } STMT_END
2053 #define isREGEXP(sv) \
2054     (SvTYPE(sv) == SVt_REGEXP                                 \
2055      || (SvFLAGS(sv) & (SVTYPEMASK|SVp_POK|SVpgv_GP|SVf_FAKE)) \
2056          == (SVt_PVLV|SVf_FAKE))
2057
2058
2059 #define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
2060 #define SvGROW_mutable(sv,len) \
2061     (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
2062 #define Sv_Grow sv_grow
2063
2064 #define CLONEf_COPY_STACKS 1
2065 #define CLONEf_KEEP_PTR_TABLE 2
2066 #define CLONEf_CLONE_HOST 4
2067 #define CLONEf_JOIN_IN 8
2068
2069 struct clone_params {
2070   AV* stashes;
2071   UV  flags;
2072   PerlInterpreter *proto_perl;
2073   PerlInterpreter *new_perl;
2074   AV *unreferenced;
2075 };
2076
2077 /*
2078 =for apidoc Am|SV*|newSVpvn_utf8|NULLOK const char* s|STRLEN len|U32 utf8
2079
2080 Creates a new SV and copies a string into it.  If utf8 is true, calls
2081 C<SvUTF8_on> on the new SV.  Implemented as a wrapper around C<newSVpvn_flags>.
2082
2083 =cut
2084 */
2085
2086 #define newSVpvn_utf8(s, len, u) newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
2087
2088 /*
2089 =for apidoc Amx|SV*|newSVpadname|PADNAME *pn
2090
2091 Creates a new SV containing the pad name.  This is currently identical
2092 to C<newSVsv>, but pad names may cease being SVs at some point, so
2093 C<newSVpadname> is preferable.
2094
2095 =cut
2096 */
2097
2098 #define newSVpadname(pn) newSVsv(pn)
2099
2100 /*
2101 =for apidoc Am|void|SvOOK_offset|NN SV*sv|STRLEN len
2102
2103 Reads into I<len> the offset from SvPVX back to the true start of the
2104 allocated buffer, which will be non-zero if C<sv_chop> has been used to
2105 efficiently remove characters from start of the buffer.  Implemented as a
2106 macro, which takes the address of I<len>, which must be of type C<STRLEN>.
2107 Evaluates I<sv> more than once.  Sets I<len> to 0 if C<SvOOK(sv)> is false.
2108
2109 =cut
2110 */
2111
2112 #ifdef DEBUGGING
2113 /* Does the bot know something I don't?
2114 10:28 <@Nicholas> metabatman
2115 10:28 <+meta> Nicholas: crash
2116 */
2117 #  define SvOOK_offset(sv, offset) STMT_START {                         \
2118         assert(sizeof(offset) == sizeof(STRLEN));                       \
2119         if (SvOOK(sv)) {                                                \
2120             const U8 *_crash = (U8*)SvPVX_const(sv);                    \
2121             (offset) = *--_crash;                                       \
2122             if (!(offset)) {                                            \
2123                 _crash -= sizeof(STRLEN);                               \
2124                 Copy(_crash, (U8 *)&(offset), sizeof(STRLEN), U8);      \
2125             }                                                           \
2126             {                                                           \
2127                 /* Validate the preceding buffer's sentinels to         \
2128                    verify that no-one is using it.  */                  \
2129                 const U8 *const _bonk = (U8*)SvPVX_const(sv) - (offset);\
2130                 while (_crash > _bonk) {                                \
2131                     --_crash;                                           \
2132                     assert (*_crash == (U8)PTR2UV(_crash));             \
2133                 }                                                       \
2134             }                                                           \
2135         } else {                                                        \
2136             (offset) = 0;                                               \
2137         }                                                               \
2138     } STMT_END
2139 #else
2140     /* This is the same code, but avoids using any temporary variables:  */
2141 #  define SvOOK_offset(sv, offset) STMT_START {                         \
2142         assert(sizeof(offset) == sizeof(STRLEN));                       \
2143         if (SvOOK(sv)) {                                                \
2144             (offset) = ((U8*)SvPVX_const(sv))[-1];                      \
2145             if (!(offset)) {                                            \
2146                 Copy(SvPVX_const(sv) - 1 - sizeof(STRLEN),              \
2147                      (U8*)&(offset), sizeof(STRLEN), U8);               \
2148             }                                                           \
2149         } else {                                                        \
2150             (offset) = 0;                                               \
2151         }                                                               \
2152     } STMT_END
2153 #endif
2154
2155 #define newIO() MUTABLE_IO(newSV_type(SVt_PVIO))
2156
2157 /*
2158  * Local variables:
2159  * c-indentation-style: bsd
2160  * c-basic-offset: 4
2161  * indent-tabs-mode: nil
2162  * End:
2163  *
2164  * ex: set ts=8 sts=4 sw=4 et:
2165  */