Slight tweaks on #11213.
[p5sagit/p5-mst-13.2.git] / sv.h
1 /*    sv.h
2  *
3  *    Copyright (c) 1991-2001, Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 #ifdef sv_flags
11 #undef sv_flags         /* Convex has this in <signal.h> for sigvec() */
12 #endif
13
14 /*
15 =for apidoc AmU||svtype
16 An enum of flags for Perl types.  These are found in the file B<sv.h> 
17 in the C<svtype> enum.  Test these flags with the C<SvTYPE> macro.
18
19 =for apidoc AmU||SVt_PV
20 Pointer type flag for scalars.  See C<svtype>.
21
22 =for apidoc AmU||SVt_IV
23 Integer type flag for scalars.  See C<svtype>.
24
25 =for apidoc AmU||SVt_NV
26 Double type flag for scalars.  See C<svtype>.
27
28 =for apidoc AmU||SVt_PVMG
29 Type flag for blessed scalars.  See C<svtype>.
30
31 =for apidoc AmU||SVt_PVAV
32 Type flag for arrays.  See C<svtype>.
33
34 =for apidoc AmU||SVt_PVHV
35 Type flag for hashes.  See C<svtype>.
36
37 =for apidoc AmU||SVt_PVCV
38 Type flag for code refs.  See C<svtype>.
39
40 =cut
41 */
42
43 typedef enum {
44         SVt_NULL,       /* 0 */
45         SVt_IV,         /* 1 */
46         SVt_NV,         /* 2 */
47         SVt_RV,         /* 3 */
48         SVt_PV,         /* 4 */
49         SVt_PVIV,       /* 5 */
50         SVt_PVNV,       /* 6 */
51         SVt_PVMG,       /* 7 */
52         SVt_PVBM,       /* 8 */
53         SVt_PVLV,       /* 9 */
54         SVt_PVAV,       /* 10 */
55         SVt_PVHV,       /* 11 */
56         SVt_PVCV,       /* 12 */
57         SVt_PVGV,       /* 13 */
58         SVt_PVFM,       /* 14 */
59         SVt_PVIO        /* 15 */
60 } svtype;
61
62 /* Using C's structural equivalence to help emulate C++ inheritance here... */
63
64 struct STRUCT_SV {              /* struct sv { */
65     void*       sv_any;         /* pointer to something */
66     U32         sv_refcnt;      /* how many references to us */
67     U32         sv_flags;       /* what we are */
68 };
69
70 struct gv {
71     XPVGV*      sv_any;         /* pointer to something */
72     U32         sv_refcnt;      /* how many references to us */
73     U32         sv_flags;       /* what we are */
74 };
75
76 struct cv {
77     XPVCV*      sv_any;         /* pointer to something */
78     U32         sv_refcnt;      /* how many references to us */
79     U32         sv_flags;       /* what we are */
80 };
81
82 struct av {
83     XPVAV*      sv_any;         /* pointer to something */
84     U32         sv_refcnt;      /* how many references to us */
85     U32         sv_flags;       /* what we are */
86 };
87
88 struct hv {
89     XPVHV*      sv_any;         /* pointer to something */
90     U32         sv_refcnt;      /* how many references to us */
91     U32         sv_flags;       /* what we are */
92 };
93
94 struct io {
95     XPVIO*      sv_any;         /* pointer to something */
96     U32         sv_refcnt;      /* how many references to us */
97     U32         sv_flags;       /* what we are */
98 };
99
100 /*
101 =for apidoc Am|U32|SvREFCNT|SV* sv
102 Returns the value of the object's reference count.
103
104 =for apidoc Am|SV*|SvREFCNT_inc|SV* sv
105 Increments the reference count of the given SV.
106
107 =for apidoc Am|void|SvREFCNT_dec|SV* sv
108 Decrements the reference count of the given SV.
109
110 =for apidoc Am|svtype|SvTYPE|SV* sv
111 Returns the type of the SV.  See C<svtype>.
112
113 =for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
114 Used to upgrade an SV to a more complex form.  Uses C<sv_upgrade> to
115 perform the upgrade if necessary.  See C<svtype>.
116
117 =cut
118 */
119
120 #define SvANY(sv)       (sv)->sv_any
121 #define SvFLAGS(sv)     (sv)->sv_flags
122 #define SvREFCNT(sv)    (sv)->sv_refcnt
123
124 #ifdef USE_THREADS
125
126 #  if defined(VMS)
127 #    define ATOMIC_INC(count) __ATOMIC_INCREMENT_LONG(&count)
128 #    define ATOMIC_DEC_AND_TEST(res,count) res=(1==__ATOMIC_DECREMENT_LONG(&count))
129  #  else
130 #    ifdef EMULATE_ATOMIC_REFCOUNTS
131  #      define ATOMIC_INC(count) STMT_START {   \
132           MUTEX_LOCK(&PL_svref_mutex);          \
133           ++count;                              \
134           MUTEX_UNLOCK(&PL_svref_mutex);                \
135        } STMT_END
136 #      define ATOMIC_DEC_AND_TEST(res,count) STMT_START {       \
137           MUTEX_LOCK(&PL_svref_mutex);                  \
138           res = (--count == 0);                         \
139           MUTEX_UNLOCK(&PL_svref_mutex);                        \
140        } STMT_END
141 #    else
142 #      define ATOMIC_INC(count) atomic_inc(&count)
143 #      define ATOMIC_DEC_AND_TEST(res,count) (res = atomic_dec_and_test(&count))
144 #    endif /* EMULATE_ATOMIC_REFCOUNTS */
145 #  endif /* VMS */
146 #else
147 #  define ATOMIC_INC(count) (++count)
148 #  define ATOMIC_DEC_AND_TEST(res, count) (res = (--count == 0))
149 #endif /* USE_THREADS */
150
151 #ifdef __GNUC__
152 #  define SvREFCNT_inc(sv)              \
153     ({                                  \
154         SV *nsv = (SV*)(sv);            \
155         if (nsv)                        \
156              ATOMIC_INC(SvREFCNT(nsv)); \
157         nsv;                            \
158     })
159 #else
160 #  if defined(CRIPPLED_CC) || defined(USE_THREADS)
161 #    if defined(VMS) && defined(__ALPHA)
162 #      define SvREFCNT_inc(sv) \
163           (PL_Sv=(SV*)(sv), (PL_Sv && __ATOMIC_INCREMENT_LONG(&(SvREFCNT(PL_Sv)))), (SV *)PL_Sv)
164 #    else
165 #      define SvREFCNT_inc(sv) sv_newref((SV*)sv)
166 #    endif
167 #  else
168 #    define SvREFCNT_inc(sv)    \
169         ((PL_Sv=(SV*)(sv)), (PL_Sv && ATOMIC_INC(SvREFCNT(PL_Sv))), (SV*)PL_Sv)
170 #  endif
171 #endif
172
173 #define SvREFCNT_dec(sv)        sv_free((SV*)sv)
174
175 #define SVTYPEMASK      0xff
176 #define SvTYPE(sv)      ((sv)->sv_flags & SVTYPEMASK)
177
178 #define SvUPGRADE(sv, mt) (SvTYPE(sv) >= mt || sv_upgrade(sv, mt))
179
180 #define SVs_PADBUSY     0x00000100      /* reserved for tmp or my already */
181 #define SVs_PADTMP      0x00000200      /* in use as tmp */
182 #define SVs_PADMY       0x00000400      /* in use a "my" variable */
183 #define SVs_TEMP        0x00000800      /* string is stealable? */
184 #define SVs_OBJECT      0x00001000      /* is "blessed" */
185 #define SVs_GMG         0x00002000      /* has magical get method */
186 #define SVs_SMG         0x00004000      /* has magical set method */
187 #define SVs_RMG         0x00008000      /* has random magical methods */
188
189 #define SVf_IOK         0x00010000      /* has valid public integer value */
190 #define SVf_NOK         0x00020000      /* has valid public numeric value */
191 #define SVf_POK         0x00040000      /* has valid public pointer value */
192 #define SVf_ROK         0x00080000      /* has a valid reference pointer */
193
194 #define SVf_FAKE        0x00100000      /* glob or lexical is just a copy */
195 #define SVf_OOK         0x00200000      /* has valid offset value */
196 #define SVf_BREAK       0x00400000      /* refcnt is artificially low - used
197                                          * by SV's in final arena  cleanup */
198 #define SVf_READONLY    0x00800000      /* may not be modified */
199
200
201 #define SVp_IOK         0x01000000      /* has valid non-public integer value */
202 #define SVp_NOK         0x02000000      /* has valid non-public numeric value */
203 #define SVp_POK         0x04000000      /* has valid non-public pointer value */
204 #define SVp_SCREAM      0x08000000      /* has been studied? */
205
206 #define SVf_UTF8        0x20000000      /* SvPVX is UTF-8 encoded */
207
208 #define SVf_THINKFIRST  (SVf_READONLY|SVf_ROK|SVf_FAKE)
209
210 #define SVf_OK          (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
211                          SVp_IOK|SVp_NOK|SVp_POK)
212
213 #define SVf_AMAGIC      0x10000000      /* has magical overloaded methods */
214
215 #define PRIVSHIFT 8
216
217 /* Some private flags. */
218
219 /* SVpad_OUR may be set on SVt_PV{NV,MG,GV} types */
220 #define SVpad_OUR       0x80000000      /* pad name is "our" instead of "my" */
221 #define SVpad_TYPED     0x40000000      /* Typed Lexical */
222
223 #define SVf_IVisUV      0x80000000      /* use XPVUV instead of XPVIV */
224
225 #define SVpfm_COMPILED  0x80000000      /* FORMLINE is compiled */
226
227 #define SVpbm_VALID     0x80000000
228 #define SVpbm_TAIL      0x40000000
229
230 #define SVrepl_EVAL     0x40000000      /* Replacement part of s///e */
231
232 #define SVphv_SHAREKEYS 0x20000000      /* keys live on shared string table */
233 #define SVphv_LAZYDEL   0x40000000      /* entry in xhv_eiter must be deleted */
234
235 #define SVprv_WEAKREF   0x80000000      /* Weak reference */
236
237 struct xrv {
238     SV *        xrv_rv;         /* pointer to another SV */
239 };
240
241 struct xpv {
242     char *      xpv_pv;         /* pointer to malloced string */
243     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
244     STRLEN      xpv_len;        /* allocated size */
245 };
246
247 struct xpviv {
248     char *      xpv_pv;         /* pointer to malloced string */
249     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
250     STRLEN      xpv_len;        /* allocated size */
251     IV          xiv_iv;         /* integer value or pv offset */
252 };
253
254 struct xpvuv {
255     char *      xpv_pv;         /* pointer to malloced string */
256     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
257     STRLEN      xpv_len;        /* allocated size */
258     UV          xuv_uv;         /* unsigned value or pv offset */
259 };
260
261 struct xpvnv {
262     char *      xpv_pv;         /* pointer to malloced string */
263     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
264     STRLEN      xpv_len;        /* allocated size */
265     IV          xiv_iv;         /* integer value or pv offset */
266     NV          xnv_nv;         /* numeric value, if any */
267 };
268
269 /* These structure must match the beginning of struct xpvhv in hv.h. */
270 struct xpvmg {
271     char *      xpv_pv;         /* pointer to malloced string */
272     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
273     STRLEN      xpv_len;        /* allocated size */
274     IV          xiv_iv;         /* integer value or pv offset */
275     NV          xnv_nv;         /* numeric value, if any */
276     MAGIC*      xmg_magic;      /* linked list of magicalness */
277     HV*         xmg_stash;      /* class package */
278 };
279
280 struct xpvlv {
281     char *      xpv_pv;         /* pointer to malloced string */
282     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
283     STRLEN      xpv_len;        /* allocated size */
284     IV          xiv_iv;         /* integer value or pv offset */
285     NV          xnv_nv;         /* numeric value, if any */
286     MAGIC*      xmg_magic;      /* linked list of magicalness */
287     HV*         xmg_stash;      /* class package */
288
289     STRLEN      xlv_targoff;
290     STRLEN      xlv_targlen;
291     SV*         xlv_targ;
292     char        xlv_type;
293 };
294
295 struct xpvgv {
296     char *      xpv_pv;         /* pointer to malloced string */
297     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
298     STRLEN      xpv_len;        /* allocated size */
299     IV          xiv_iv;         /* integer value or pv offset */
300     NV          xnv_nv;         /* numeric value, if any */
301     MAGIC*      xmg_magic;      /* linked list of magicalness */
302     HV*         xmg_stash;      /* class package */
303
304     GP*         xgv_gp;
305     char*       xgv_name;
306     STRLEN      xgv_namelen;
307     HV*         xgv_stash;
308     U8          xgv_flags;
309 };
310
311 struct xpvbm {
312     char *      xpv_pv;         /* pointer to malloced string */
313     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
314     STRLEN      xpv_len;        /* allocated size */
315     IV          xiv_iv;         /* integer value or pv offset */
316     NV          xnv_nv;         /* numeric value, if any */
317     MAGIC*      xmg_magic;      /* linked list of magicalness */
318     HV*         xmg_stash;      /* class package */
319
320     I32         xbm_useful;     /* is this constant pattern being useful? */
321     U16         xbm_previous;   /* how many characters in string before rare? */
322     U8          xbm_rare;       /* rarest character in string */
323 };
324
325 /* This structure must match XPVCV in cv.h */
326
327 typedef U16 cv_flags_t;
328
329 struct xpvfm {
330     char *      xpv_pv;         /* pointer to malloced string */
331     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
332     STRLEN      xpv_len;        /* allocated size */
333     IV          xiv_iv;         /* integer value or pv offset */
334     NV          xnv_nv;         /* numeric value, if any */
335     MAGIC*      xmg_magic;      /* linked list of magicalness */
336     HV*         xmg_stash;      /* class package */
337
338     HV *        xcv_stash;
339     OP *        xcv_start;
340     OP *        xcv_root;
341     void      (*xcv_xsub)(pTHXo_ CV*);
342     ANY         xcv_xsubany;
343     GV *        xcv_gv;
344     char *      xcv_file;
345     long        xcv_depth;      /* >= 2 indicates recursive call */
346     AV *        xcv_padlist;
347     CV *        xcv_outside;
348 #ifdef USE_THREADS
349     perl_mutex *xcv_mutexp;     /* protects xcv_owner */
350     struct perl_thread *xcv_owner;      /* current owner thread */
351 #endif /* USE_THREADS */
352     cv_flags_t  xcv_flags;
353     HV *        xcv_defstash;
354
355     I32         xfm_lines;
356 };
357
358 struct xpvio {
359     char *      xpv_pv;         /* pointer to malloced string */
360     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
361     STRLEN      xpv_len;        /* allocated size */
362     IV          xiv_iv;         /* integer value or pv offset */
363     NV          xnv_nv;         /* numeric value, if any */
364     MAGIC*      xmg_magic;      /* linked list of magicalness */
365     HV*         xmg_stash;      /* class package */
366
367     PerlIO *    xio_ifp;        /* ifp and ofp are normally the same */
368     PerlIO *    xio_ofp;        /* but sockets need separate streams */
369     /* Cray addresses everything by word boundaries (64 bits) and
370      * code and data pointers cannot be mixed (which is exactly what
371      * Perl_filter_add() tries to do with the dirp), hence the following
372      * union trick (as suggested by Gurusamy Sarathy).
373      * For further information see Geir Johansen's problem report titled
374        [ID 20000612.002] Perl problem on Cray system
375      * The any pointer (known as IoANY()) will also be a good place
376      * to hang any IO disciplines to.
377      */
378     union {
379         DIR *   xiou_dirp;      /* for opendir, readdir, etc */
380         void *  xiou_any;       /* for alignment */
381     } xio_dirpu;
382     long        xio_lines;      /* $. */
383     long        xio_page;       /* $% */
384     long        xio_page_len;   /* $= */
385     long        xio_lines_left; /* $- */
386     char *      xio_top_name;   /* $^ */
387     GV *        xio_top_gv;     /* $^ */
388     char *      xio_fmt_name;   /* $~ */
389     GV *        xio_fmt_gv;     /* $~ */
390     char *      xio_bottom_name;/* $^B */
391     GV *        xio_bottom_gv;  /* $^B */
392     short       xio_subprocess; /* -| or |- */
393     char        xio_type;
394     char        xio_flags;
395 };
396 #define xio_dirp        xio_dirpu.xiou_dirp
397 #define xio_any         xio_dirpu.xiou_any
398
399 #define IOf_ARGV        1       /* this fp iterates over ARGV */
400 #define IOf_START       2       /* check for null ARGV and substitute '-' */
401 #define IOf_FLUSH       4       /* this fp wants a flush after write op */
402 #define IOf_DIDTOP      8       /* just did top of form */
403 #define IOf_UNTAINT     16      /* consider this fp (and its data) "safe" */
404 #define IOf_NOLINE      32      /* slurped a pseudo-line from empty file */
405 #define IOf_FAKE_DIRP   64      /* xio_dirp is fake (source filters kludge) */
406
407 /* The following macros define implementation-independent predicates on SVs. */
408
409 /*
410 =for apidoc Am|bool|SvNIOK|SV* sv
411 Returns a boolean indicating whether the SV contains a number, integer or
412 double.
413
414 =for apidoc Am|bool|SvNIOKp|SV* sv
415 Returns a boolean indicating whether the SV contains a number, integer or
416 double.  Checks the B<private> setting.  Use C<SvNIOK>.
417
418 =for apidoc Am|void|SvNIOK_off|SV* sv
419 Unsets the NV/IV status of an SV.
420
421 =for apidoc Am|bool|SvOK|SV* sv
422 Returns a boolean indicating whether the value is an SV.
423
424 =for apidoc Am|bool|SvIOKp|SV* sv
425 Returns a boolean indicating whether the SV contains an integer.  Checks
426 the B<private> setting.  Use C<SvIOK>.
427
428 =for apidoc Am|bool|SvNOKp|SV* sv
429 Returns a boolean indicating whether the SV contains a double.  Checks the
430 B<private> setting.  Use C<SvNOK>.
431
432 =for apidoc Am|bool|SvPOKp|SV* sv
433 Returns a boolean indicating whether the SV contains a character string.
434 Checks the B<private> setting.  Use C<SvPOK>.
435
436 =for apidoc Am|bool|SvIOK|SV* sv
437 Returns a boolean indicating whether the SV contains an integer.
438
439 =for apidoc Am|void|SvIOK_on|SV* sv
440 Tells an SV that it is an integer.
441
442 =for apidoc Am|void|SvIOK_off|SV* sv
443 Unsets the IV status of an SV.
444
445 =for apidoc Am|void|SvIOK_only|SV* sv
446 Tells an SV that it is an integer and disables all other OK bits.
447
448 =for apidoc Am|void|SvIOK_only_UV|SV* sv
449 Tells and SV that it is an unsigned integer and disables all other OK bits.
450
451 =for apidoc Am|void|SvIOK_UV|SV* sv
452 Returns a boolean indicating whether the SV contains an unsigned integer.
453
454 =for apidoc Am|void|SvUOK|SV* sv
455 Returns a boolean indicating whether the SV contains an unsigned integer.
456
457 =for apidoc Am|void|SvIOK_notUV|SV* sv
458 Returns a boolean indicating whether the SV contains an signed integer.
459
460 =for apidoc Am|bool|SvNOK|SV* sv
461 Returns a boolean indicating whether the SV contains a double.
462
463 =for apidoc Am|void|SvNOK_on|SV* sv
464 Tells an SV that it is a double.
465
466 =for apidoc Am|void|SvNOK_off|SV* sv
467 Unsets the NV status of an SV.
468
469 =for apidoc Am|void|SvNOK_only|SV* sv
470 Tells an SV that it is a double and disables all other OK bits.
471
472 =for apidoc Am|bool|SvPOK|SV* sv
473 Returns a boolean indicating whether the SV contains a character
474 string.
475
476 =for apidoc Am|void|SvPOK_on|SV* sv
477 Tells an SV that it is a string.
478
479 =for apidoc Am|void|SvPOK_off|SV* sv
480 Unsets the PV status of an SV.
481
482 =for apidoc Am|void|SvPOK_only|SV* sv
483 Tells an SV that it is a string and disables all other OK bits.
484 Will also turn off the UTF8 status.
485
486 =for apidoc Am|bool|SvOOK|SV* sv
487 Returns a boolean indicating whether the SvIVX is a valid offset value for
488 the SvPVX.  This hack is used internally to speed up removal of characters
489 from the beginning of a SvPV.  When SvOOK is true, then the start of the
490 allocated string buffer is really (SvPVX - SvIVX).
491
492 =for apidoc Am|bool|SvROK|SV* sv
493 Tests if the SV is an RV.
494
495 =for apidoc Am|void|SvROK_on|SV* sv
496 Tells an SV that it is an RV.
497
498 =for apidoc Am|void|SvROK_off|SV* sv
499 Unsets the RV status of an SV.
500
501 =for apidoc Am|SV*|SvRV|SV* sv
502 Dereferences an RV to return the SV.
503
504 =for apidoc Am|IV|SvIVX|SV* sv
505 Returns the raw value in the SV's IV slot, without checks or conversions.
506 Only use when you are sure SvIOK is true. See also C<SvIV()>.
507
508 =for apidoc Am|UV|SvUVX|SV* sv
509 Returns the raw value in the SV's UV slot, without checks or conversions.
510 Only use when you are sure SvIOK is true. See also C<SvUV()>.
511
512 =for apidoc Am|NV|SvNVX|SV* sv
513 Returns the raw value in the SV's NV slot, without checks or conversions.
514 Only use when you are sure SvNOK is true. See also C<SvNV()>.
515
516 =for apidoc Am|char*|SvPVX|SV* sv
517 Returns a pointer to the physical string in the SV.  The SV must contain a
518 string.
519
520 =for apidoc Am|STRLEN|SvCUR|SV* sv
521 Returns the length of the string which is in the SV.  See C<SvLEN>.
522
523 =for apidoc Am|STRLEN|SvLEN|SV* sv
524 Returns the size of the string buffer in the SV, not including any part
525 attributable to C<SvOOK>.  See C<SvCUR>.
526
527 =for apidoc Am|char*|SvEND|SV* sv
528 Returns a pointer to the last character in the string which is in the SV.
529 See C<SvCUR>.  Access the character as *(SvEND(sv)).
530
531 =for apidoc Am|HV*|SvSTASH|SV* sv
532 Returns the stash of the SV.
533
534 =for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
535 Set the length of the string which is in the SV.  See C<SvCUR>.
536
537 =cut
538 */
539
540 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
541 #define SvNIOKp(sv)             (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
542 #define SvNIOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
543                                                   SVp_IOK|SVp_NOK|SVf_IVisUV))
544
545 #define SvOK(sv)                (SvFLAGS(sv) & SVf_OK)
546 #define SvOK_off(sv)            (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
547                                                   SVf_IVisUV|SVf_UTF8), \
548                                                         SvOOK_off(sv))
549 #define SvOK_off_exc_UV(sv)     (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
550                                                   SVf_UTF8),            \
551                                                         SvOOK_off(sv))
552
553 #define SvOKp(sv)               (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
554 #define SvIOKp(sv)              (SvFLAGS(sv) & SVp_IOK)
555 #define SvIOKp_on(sv)           ((void)SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
556 #define SvNOKp(sv)              (SvFLAGS(sv) & SVp_NOK)
557 #define SvNOKp_on(sv)           (SvFLAGS(sv) |= SVp_NOK)
558 #define SvPOKp(sv)              (SvFLAGS(sv) & SVp_POK)
559 #define SvPOKp_on(sv)           (SvFLAGS(sv) |= SVp_POK)
560
561 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
562 #define SvIOK_on(sv)            ((void)SvOOK_off(sv), \
563                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
564 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
565 #define SvIOK_only(sv)          ((void)SvOK_off(sv), \
566                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
567 #define SvIOK_only_UV(sv)       ((void)SvOK_off_exc_UV(sv), \
568                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
569
570 #define SvIOK_UV(sv)            ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
571                                  == (SVf_IOK|SVf_IVisUV))
572 #define SvUOK(sv)               SvIOK_UV(sv)
573 #define SvIOK_notUV(sv)         ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
574                                  == SVf_IOK)
575
576 #define SvIsUV(sv)              (SvFLAGS(sv) & SVf_IVisUV)
577 #define SvIsUV_on(sv)           (SvFLAGS(sv) |= SVf_IVisUV)
578 #define SvIsUV_off(sv)          (SvFLAGS(sv) &= ~SVf_IVisUV)
579
580 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
581 #define SvNOK_on(sv)            (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
582 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
583 #define SvNOK_only(sv)          ((void)SvOK_off(sv), \
584                                     SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
585
586 /*
587 =for apidoc Am|void|SvUTF8|SV* sv
588 Returns a boolean indicating whether the SV contains UTF-8 encoded data.
589
590 =for apidoc Am|void|SvUTF8_on|SV *sv
591 Turn on the UTF8 status of an SV (the data is not changed, just the flag).
592 Do not use frivolously.
593
594 =for apidoc Am|void|SvUTF8_off|SV *sv
595 Unsets the UTF8 status of an SV.
596
597 =for apidoc Am|void|SvPOK_only_UTF8|SV* sv
598 Tells an SV that it is a string and disables all other OK bits,
599 and leaves the UTF8 status as it was.
600
601 =cut
602  */
603
604 #define SvUTF8(sv)              (SvFLAGS(sv) & SVf_UTF8)
605 #define SvUTF8_on(sv)           (SvFLAGS(sv) |= (SVf_UTF8))
606 #define SvUTF8_off(sv)          (SvFLAGS(sv) &= ~(SVf_UTF8))
607
608 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
609 #define SvPOK_on(sv)            (SvFLAGS(sv) |= (SVf_POK|SVp_POK))
610 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
611 #define SvPOK_only(sv)          (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
612                                                   SVf_IVisUV|SVf_UTF8), \
613                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
614 #define SvPOK_only_UTF8(sv)     (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
615                                                   SVf_IVisUV),          \
616                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
617
618 #define SvOOK(sv)               (SvFLAGS(sv) & SVf_OOK)
619 #define SvOOK_on(sv)            ((void)SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
620 #define SvOOK_off(sv)           (SvOOK(sv) && sv_backoff(sv))
621
622 #define SvFAKE(sv)              (SvFLAGS(sv) & SVf_FAKE)
623 #define SvFAKE_on(sv)           (SvFLAGS(sv) |= SVf_FAKE)
624 #define SvFAKE_off(sv)          (SvFLAGS(sv) &= ~SVf_FAKE)
625
626 #define SvROK(sv)               (SvFLAGS(sv) & SVf_ROK)
627 #define SvROK_on(sv)            (SvFLAGS(sv) |= SVf_ROK)
628 #define SvROK_off(sv)           (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
629
630 #define SvMAGICAL(sv)           (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
631 #define SvMAGICAL_on(sv)        (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
632 #define SvMAGICAL_off(sv)       (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
633
634 #define SvGMAGICAL(sv)          (SvFLAGS(sv) & SVs_GMG)
635 #define SvGMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_GMG)
636 #define SvGMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_GMG)
637
638 #define SvSMAGICAL(sv)          (SvFLAGS(sv) & SVs_SMG)
639 #define SvSMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_SMG)
640 #define SvSMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_SMG)
641
642 #define SvRMAGICAL(sv)          (SvFLAGS(sv) & SVs_RMG)
643 #define SvRMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_RMG)
644 #define SvRMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_RMG)
645
646 #define SvAMAGIC(sv)            (SvFLAGS(sv) & SVf_AMAGIC)
647 #define SvAMAGIC_on(sv)         (SvFLAGS(sv) |= SVf_AMAGIC)
648 #define SvAMAGIC_off(sv)        (SvFLAGS(sv) &= ~SVf_AMAGIC)
649
650 #define SvGAMAGIC(sv)           (SvFLAGS(sv) & (SVs_GMG|SVf_AMAGIC)) 
651
652 /*
653 #define Gv_AMG(stash) \
654         (HV_AMAGICmb(stash) && \
655          ((!HV_AMAGICbad(stash) && HV_AMAGIC(stash)) || Gv_AMupdate(stash)))
656 */
657 #define Gv_AMG(stash)           (PL_amagic_generation && Gv_AMupdate(stash))
658
659 #define SvWEAKREF(sv)           ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
660                                   == (SVf_ROK|SVprv_WEAKREF))
661 #define SvWEAKREF_on(sv)        (SvFLAGS(sv) |=  (SVf_ROK|SVprv_WEAKREF))
662 #define SvWEAKREF_off(sv)       (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
663
664 #define SvTHINKFIRST(sv)        (SvFLAGS(sv) & SVf_THINKFIRST)
665
666 #define SvPADBUSY(sv)           (SvFLAGS(sv) & SVs_PADBUSY)
667
668 #define SvPADTMP(sv)            (SvFLAGS(sv) & SVs_PADTMP)
669 #define SvPADTMP_on(sv)         (SvFLAGS(sv) |= SVs_PADTMP|SVs_PADBUSY)
670 #define SvPADTMP_off(sv)        (SvFLAGS(sv) &= ~SVs_PADTMP)
671
672 #define SvPADMY(sv)             (SvFLAGS(sv) & SVs_PADMY)
673 #define SvPADMY_on(sv)          (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
674
675 #define SvTEMP(sv)              (SvFLAGS(sv) & SVs_TEMP)
676 #define SvTEMP_on(sv)           (SvFLAGS(sv) |= SVs_TEMP)
677 #define SvTEMP_off(sv)          (SvFLAGS(sv) &= ~SVs_TEMP)
678
679 #define SvOBJECT(sv)            (SvFLAGS(sv) & SVs_OBJECT)
680 #define SvOBJECT_on(sv)         (SvFLAGS(sv) |= SVs_OBJECT)
681 #define SvOBJECT_off(sv)        (SvFLAGS(sv) &= ~SVs_OBJECT)
682
683 #define SvREADONLY(sv)          (SvFLAGS(sv) & SVf_READONLY)
684 #define SvREADONLY_on(sv)       (SvFLAGS(sv) |= SVf_READONLY)
685 #define SvREADONLY_off(sv)      (SvFLAGS(sv) &= ~SVf_READONLY)
686
687 #define SvSCREAM(sv)            (SvFLAGS(sv) & SVp_SCREAM)
688 #define SvSCREAM_on(sv)         (SvFLAGS(sv) |= SVp_SCREAM)
689 #define SvSCREAM_off(sv)        (SvFLAGS(sv) &= ~SVp_SCREAM)
690
691 #define SvCOMPILED(sv)          (SvFLAGS(sv) & SVpfm_COMPILED)
692 #define SvCOMPILED_on(sv)       (SvFLAGS(sv) |= SVpfm_COMPILED)
693 #define SvCOMPILED_off(sv)      (SvFLAGS(sv) &= ~SVpfm_COMPILED)
694
695 #define SvEVALED(sv)            (SvFLAGS(sv) & SVrepl_EVAL)
696 #define SvEVALED_on(sv)         (SvFLAGS(sv) |= SVrepl_EVAL)
697 #define SvEVALED_off(sv)        (SvFLAGS(sv) &= ~SVrepl_EVAL)
698
699 #define SvTAIL(sv)              (SvFLAGS(sv) & SVpbm_TAIL)
700 #define SvTAIL_on(sv)           (SvFLAGS(sv) |= SVpbm_TAIL)
701 #define SvTAIL_off(sv)          (SvFLAGS(sv) &= ~SVpbm_TAIL)
702
703 #define SvVALID(sv)             (SvFLAGS(sv) & SVpbm_VALID)
704 #define SvVALID_on(sv)          (SvFLAGS(sv) |= SVpbm_VALID)
705 #define SvVALID_off(sv)         (SvFLAGS(sv) &= ~SVpbm_VALID)
706
707 #define SvRV(sv) ((XRV*)  SvANY(sv))->xrv_rv
708 #define SvRVx(sv) SvRV(sv)
709
710 #define SvIVX(sv) ((XPVIV*)  SvANY(sv))->xiv_iv
711 #define SvIVXx(sv) SvIVX(sv)
712 #define SvUVX(sv) ((XPVUV*)  SvANY(sv))->xuv_uv
713 #define SvUVXx(sv) SvUVX(sv)
714 #define SvNVX(sv)  ((XPVNV*)SvANY(sv))->xnv_nv
715 #define SvNVXx(sv) SvNVX(sv)
716 #define SvPVX(sv)  ((XPV*)  SvANY(sv))->xpv_pv
717 #define SvPVXx(sv) SvPVX(sv)
718 #define SvCUR(sv) ((XPV*)  SvANY(sv))->xpv_cur
719 #define SvLEN(sv) ((XPV*)  SvANY(sv))->xpv_len
720 #define SvLENx(sv) SvLEN(sv)
721 #define SvEND(sv)(((XPV*)  SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
722 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
723 #define SvMAGIC(sv)     ((XPVMG*)  SvANY(sv))->xmg_magic
724 #define SvSTASH(sv)     ((XPVMG*)  SvANY(sv))->xmg_stash
725
726 /* Ask a scalar nicely to try to become an IV, if possible.
727    Not guaranteed to stay returning void */
728 /* Macro won't actually call sv_2iv if already IOK */
729 #define SvIV_please(sv) \
730         STMT_START {if (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv))) \
731                 (void) SvIV(sv); } STMT_END
732 #define SvIV_set(sv, val) \
733         STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
734                 (((XPVIV*)  SvANY(sv))->xiv_iv = val); } STMT_END
735 #define SvNV_set(sv, val) \
736         STMT_START { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
737                 (((XPVNV*)  SvANY(sv))->xnv_nv = val); } STMT_END
738 #define SvPV_set(sv, val) \
739         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
740                 (((XPV*)  SvANY(sv))->xpv_pv = val); } STMT_END
741 #define SvCUR_set(sv, val) \
742         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
743                 (((XPV*)  SvANY(sv))->xpv_cur = val); } STMT_END
744 #define SvLEN_set(sv, val) \
745         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
746                 (((XPV*)  SvANY(sv))->xpv_len = val); } STMT_END
747 #define SvEND_set(sv, val) \
748         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
749                 (((XPV*)  SvANY(sv))->xpv_cur = val - SvPVX(sv)); } STMT_END
750
751 #define BmRARE(sv)      ((XPVBM*)  SvANY(sv))->xbm_rare
752 #define BmUSEFUL(sv)    ((XPVBM*)  SvANY(sv))->xbm_useful
753 #define BmPREVIOUS(sv)  ((XPVBM*)  SvANY(sv))->xbm_previous
754
755 #define FmLINES(sv)     ((XPVFM*)  SvANY(sv))->xfm_lines
756
757 #define LvTYPE(sv)      ((XPVLV*)  SvANY(sv))->xlv_type
758 #define LvTARG(sv)      ((XPVLV*)  SvANY(sv))->xlv_targ
759 #define LvTARGOFF(sv)   ((XPVLV*)  SvANY(sv))->xlv_targoff
760 #define LvTARGLEN(sv)   ((XPVLV*)  SvANY(sv))->xlv_targlen
761
762 #define IoIFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ifp
763 #define IoOFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ofp
764 #define IoDIRP(sv)      ((XPVIO*)  SvANY(sv))->xio_dirp
765 #define IoANY(sv)       ((XPVIO*)  SvANY(sv))->xio_any
766 #define IoLINES(sv)     ((XPVIO*)  SvANY(sv))->xio_lines
767 #define IoPAGE(sv)      ((XPVIO*)  SvANY(sv))->xio_page
768 #define IoPAGE_LEN(sv)  ((XPVIO*)  SvANY(sv))->xio_page_len
769 #define IoLINES_LEFT(sv)((XPVIO*)  SvANY(sv))->xio_lines_left
770 #define IoTOP_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_top_name
771 #define IoTOP_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_top_gv
772 #define IoFMT_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_fmt_name
773 #define IoFMT_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_fmt_gv
774 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
775 #define IoBOTTOM_GV(sv) ((XPVIO*)  SvANY(sv))->xio_bottom_gv
776 #define IoSUBPROCESS(sv)((XPVIO*)  SvANY(sv))->xio_subprocess
777 #define IoTYPE(sv)      ((XPVIO*)  SvANY(sv))->xio_type
778 #define IoFLAGS(sv)     ((XPVIO*)  SvANY(sv))->xio_flags
779
780 /* IoTYPE(sv) is a single character telling the type of I/O connection. */
781 #define IoTYPE_RDONLY   '<'
782 #define IoTYPE_WRONLY   '>'
783 #define IoTYPE_RDWR     '+'
784 #define IoTYPE_APPEND   'a'
785 #define IoTYPE_PIPE     '|'
786 #define IoTYPE_STD      '-'     /* stdin or stdout */
787 #define IoTYPE_SOCKET   's'
788 #define IoTYPE_CLOSED   ' '
789
790 /*
791 =for apidoc Am|bool|SvTAINTED|SV* sv
792 Checks to see if an SV is tainted. Returns TRUE if it is, FALSE if
793 not.
794
795 =for apidoc Am|void|SvTAINTED_on|SV* sv
796 Marks an SV as tainted.
797
798 =for apidoc Am|void|SvTAINTED_off|SV* sv
799 Untaints an SV. Be I<very> careful with this routine, as it short-circuits
800 some of Perl's fundamental security features. XS module authors should not
801 use this function unless they fully understand all the implications of
802 unconditionally untainting the value. Untainting should be done in the
803 standard perl fashion, via a carefully crafted regexp, rather than directly
804 untainting variables.
805
806 =for apidoc Am|void|SvTAINT|SV* sv
807 Taints an SV if tainting is enabled
808
809 =cut
810 */
811
812 #define SvTAINTED(sv)     (SvMAGICAL(sv) && sv_tainted(sv))
813 #define SvTAINTED_on(sv)  STMT_START{ if(PL_tainting){sv_taint(sv);}   }STMT_END
814 #define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
815
816 #define SvTAINT(sv)                     \
817     STMT_START {                        \
818         if (PL_tainting) {              \
819             if (PL_tainted)             \
820                 SvTAINTED_on(sv);       \
821         }                               \
822     } STMT_END
823
824 /*
825 =for apidoc Am|char*|SvPV_force|SV* sv|STRLEN len
826 Like <SvPV> but will force the SV into becoming a string (SvPOK).  You want
827 force if you are going to update the SvPVX directly.
828
829 =for apidoc Am|char*|SvPV_force_nomg|SV* sv|STRLEN len
830 Like <SvPV> but will force the SV into becoming a string (SvPOK).  You want
831 force if you are going to update the SvPVX directly. Doesn't process magic.
832
833 =for apidoc Am|char*|SvPV|SV* sv|STRLEN len
834 Returns a pointer to the string in the SV, or a stringified form of the SV
835 if the SV does not contain a string.  Handles 'get' magic. See also
836 C<SvPVx> for a version which guarantees to evaluate sv only once.
837
838 =for apidoc Am|char*|SvPVx|SV* sv|STRLEN len
839 A version of C<SvPV> which guarantees to evaluate sv only once.
840
841 =for apidoc Am|char*|SvPV_nolen|SV* sv
842 Returns a pointer to the string in the SV, or a stringified form of the SV
843 if the SV does not contain a string.  Handles 'get' magic.
844
845 =for apidoc Am|IV|SvIV|SV* sv
846 Coerces the given SV to an integer and returns it. See  C<SvIVx> for a
847 version which guarantees to evaluate sv only once.
848
849 =for apidoc Am|IV|SvIVx|SV* sv
850 Coerces the given SV to an integer and returns it. Guarantees to evaluate
851 sv only once. Use the more efficent C<SvIV> otherwise.
852
853 =for apidoc Am|NV|SvNV|SV* sv
854 Coerce the given SV to a double and return it. See  C<SvNVx> for a version
855 which guarantees to evaluate sv only once.
856
857 =for apidoc Am|NV|SvNVx|SV* sv
858 Coerces the given SV to a double and returns it. Guarantees to evaluate
859 sv only once. Use the more efficent C<SvNV> otherwise.
860
861 =for apidoc Am|UV|SvUV|SV* sv
862 Coerces the given SV to an unsigned integer and returns it.  See C<SvUVx>
863 for a version which guarantees to evaluate sv only once.
864
865 =for apidoc Am|UV|SvUVx|SV* sv
866 Coerces the given SV to an unsigned integer and returns it. Guarantees to
867 evaluate sv only once. Use the more efficent C<SvUV> otherwise.
868
869 =for apidoc Am|bool|SvTRUE|SV* sv
870 Returns a boolean indicating whether Perl would evaluate the SV as true or
871 false, defined or undefined.  Does not handle 'get' magic.
872
873 =for apidoc Am|char*|SvPVutf8_force|SV* sv|STRLEN len
874 Like C<SvPV_force>, but converts sv to uft8 first if necessary.
875
876 =for apidoc Am|char*|SvPVutf8|SV* sv|STRLEN len
877 Like C<SvPV>, but converts sv to uft8 first if necessary.
878
879 =for apidoc Am|char*|SvPVutf8_nolen|SV* sv|STRLEN len
880 Like C<SvPV_nolen>, but converts sv to uft8 first if necessary.
881
882 =for apidoc Am|char*|SvPVbyte_force|SV* sv|STRLEN len
883 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
884
885 =for apidoc Am|char*|SvPVbyte|SV* sv|STRLEN len
886 Like C<SvPV>, but converts sv to byte representation first if necessary.
887
888 =for apidoc Am|char*|SvPVbyte_nolen|SV* sv|STRLEN len
889 Like C<SvPV_nolen>, but converts sv to byte representation first if necessary.
890
891 =for apidoc Am|char*|SvPVutf8x_force|SV* sv|STRLEN len
892 Like C<SvPV_force>, but converts sv to uft8 first if necessary.
893 Guarantees to evalute sv only once; use the more efficient C<SvPVutf8_force>
894 otherwise.
895
896 =for apidoc Am|char*|SvPVutf8x|SV* sv|STRLEN len
897 Like C<SvPV>, but converts sv to uft8 first if necessary.
898 Guarantees to evalute sv only once; use the more efficient C<SvPVutf8>
899 otherwise.
900
901 =for apidoc Am|char*|SvPVbytex_force|SV* sv|STRLEN len
902 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
903 Guarantees to evalute sv only once; use the more efficient C<SvPVbyte_force>
904 otherwise.
905
906 =for apidoc Am|char*|SvPVbytex|SV* sv|STRLEN len
907 Like C<SvPV>, but converts sv to byte representation first if necessary.
908 Guarantees to evalute sv only once; use the more efficient C<SvPVbyte>
909 otherwise.
910
911
912 =cut
913 */
914
915 #define SvPV_force(sv, lp) sv_pvn_force(sv, &lp)
916 #define SvPV(sv, lp) sv_pvn(sv, &lp)
917 #define SvPV_nolen(sv) sv_pv(sv)
918
919 #define SvPVutf8_force(sv, lp) sv_pvutf8n_force(sv, &lp)
920 #define SvPVutf8(sv, lp) sv_pvutf8n(sv, &lp)
921 #define SvPVutf8_nolen(sv) sv_pvutf8(sv)
922
923 #define SvPVbyte_force(sv, lp) sv_pvbyte_force(sv, &lp)
924 #define SvPVbyte(sv, lp) sv_pvbyten(sv, &lp)
925 #define SvPVbyte_nolen(sv) sv_pvbyte(sv)
926
927 #define SvPVx(sv, lp) sv_pvn(sv, &lp)
928 #define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
929 #define SvPVutf8x(sv, lp) sv_pvutf8n(sv, &lp)
930 #define SvPVutf8x_force(sv, lp) sv_pvutf8n_force(sv, &lp)
931 #define SvPVbytex(sv, lp) sv_pvbyten(sv, &lp)
932 #define SvPVbytex_force(sv, lp) sv_pvbyten_force(sv, &lp)
933
934 #define SvIVx(sv) sv_iv(sv)
935 #define SvUVx(sv) sv_uv(sv)
936 #define SvNVx(sv) sv_nv(sv)
937
938 #define SvTRUEx(sv) sv_true(sv)
939
940 #define SvIV(sv) SvIVx(sv)
941 #define SvNV(sv) SvNVx(sv)
942 #define SvUV(sv) SvUVx(sv)
943 #define SvTRUE(sv) SvTRUEx(sv)
944
945 #ifndef CRIPPLED_CC
946 /* redefine some things to more efficient inlined versions */
947
948 /* Let us hope that bitmaps for UV and IV are the same */
949 #undef SvIV
950 #define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
951
952 #undef SvUV
953 #define SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
954
955 #undef SvNV
956 #define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
957
958 /* flag values for sv_*_flags functions */
959 #define SV_IMMEDIATE_UNREF      1
960 #define SV_GMAGIC               2
961
962 #define sv_setsv_macro(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_GMAGIC)
963 #define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, 0)
964 #define sv_catsv_macro(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
965 #define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
966 #define sv_catpvn_macro(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
967 #define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
968 #define sv_2pv_macro(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
969 #define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
970 #define sv_pvn_force_macro(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
971 #define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
972 #define sv_utf8_upgrade_macro(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
973 #define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
974
975 /* function style also available for bincompat */
976 #define sv_setsv(dsv, ssv) sv_setsv_macro(dsv, ssv)
977 #define sv_catsv(dsv, ssv) sv_catsv_macro(dsv, ssv)
978 #define sv_catpvn(dsv, sstr, slen) sv_catpvn_macro(dsv, sstr, slen)
979 #define sv_2pv(sv, lp) sv_2pv_macro(sv, lp)
980 #define sv_pvn_force(sv, lp) sv_pvn_force_macro(sv, lp)
981 #define sv_utf8_upgrade(sv) sv_utf8_upgrade_macro(sv)
982
983 #undef SvPV
984 #define SvPV(sv, lp) SvPV_flags(sv, lp, SV_GMAGIC)
985
986 #undef SvPV_nomg
987 #define SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
988
989 #undef SvPV_flags
990 #define SvPV_flags(sv, lp, flags) \
991     ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
992      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv_flags(sv, &lp, flags))
993
994 #undef SvPV_force
995 #define SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
996 #undef SvPV_force_nomg
997 #define SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
998
999 #undef SvPV_force_flags
1000 #define SvPV_force_flags(sv, lp, flags) \
1001     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
1002     ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))
1003
1004 #undef SvPV_nolen
1005 #define SvPV_nolen(sv) \
1006     ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1007      ? SvPVX(sv) : sv_2pv_nolen(sv))
1008
1009 #undef SvPVutf8
1010 #define SvPVutf8(sv, lp) \
1011     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8) \
1012      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvutf8(sv, &lp))
1013
1014 #undef SvPVutf8_force
1015 #define SvPVutf8_force(sv, lp) \
1016     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == (SVf_POK|SVf_UTF8) \
1017      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
1018
1019 #undef SvPVutf8_nolen
1020 #define SvPVutf8_nolen(sv) \
1021     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8)\
1022      ? SvPVX(sv) : sv_2pvutf8_nolen(sv))
1023
1024 #undef SvPVutf8
1025 #define SvPVutf8(sv, lp) \
1026     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8) \
1027      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvutf8(sv, &lp))
1028
1029 #undef SvPVutf8_force
1030 #define SvPVutf8_force(sv, lp) \
1031     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == (SVf_POK|SVf_UTF8) \
1032      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
1033
1034 #undef SvPVutf8_nolen
1035 #define SvPVutf8_nolen(sv) \
1036     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8)\
1037      ? SvPVX(sv) : sv_2pvutf8_nolen(sv))
1038
1039 #undef SvPVbyte
1040 #define SvPVbyte(sv, lp) \
1041     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \
1042      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))
1043
1044 #undef SvPVbyte_force
1045 #define SvPVbyte_force(sv, lp) \
1046     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST)) == (SVf_POK) \
1047      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvbyte_force(sv, &lp))
1048
1049 #undef SvPVbyte_nolen
1050 #define SvPVbyte_nolen(sv) \
1051     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK)\
1052      ? SvPVX(sv) : sv_2pvbyte_nolen(sv))
1053
1054
1055 #ifdef __GNUC__
1056 #  undef SvIVx
1057 #  undef SvUVx
1058 #  undef SvNVx
1059 #  undef SvPVx
1060 #  undef SvPVutf8x
1061 #  undef SvPVbytex
1062 #  undef SvTRUE
1063 #  undef SvTRUEx
1064 #  define SvIVx(sv) ({SV *nsv = (SV*)(sv); SvIV(nsv); })
1065 #  define SvUVx(sv) ({SV *nsv = (SV*)(sv); SvUV(nsv); })
1066 #  define SvNVx(sv) ({SV *nsv = (SV*)(sv); SvNV(nsv); })
1067 #  define SvPVx(sv, lp) ({SV *nsv = (sv); SvPV(nsv, lp); })
1068 #  define SvPVutf8x(sv, lp) ({SV *nsv = (sv); SvPVutf8(nsv, lp); })
1069 #  define SvPVbytex(sv, lp) ({SV *nsv = (sv); SvPVbyte(nsv, lp); })
1070 #  define SvTRUE(sv) (                                          \
1071     !sv                                                         \
1072     ? 0                                                         \
1073     :    SvPOK(sv)                                              \
1074         ?   (({XPV *nxpv = (XPV*)SvANY(sv);                     \
1075              nxpv &&                                            \
1076              (nxpv->xpv_cur > 1 ||                              \
1077               (nxpv->xpv_cur && *nxpv->xpv_pv != '0')); })      \
1078              ? 1                                                \
1079              : 0)                                               \
1080         :                                                       \
1081             SvIOK(sv)                                           \
1082             ? SvIVX(sv) != 0                                    \
1083             :   SvNOK(sv)                                       \
1084                 ? SvNVX(sv) != 0.0                              \
1085                 : sv_2bool(sv) )
1086 #  define SvTRUEx(sv) ({SV *nsv = (sv); SvTRUE(nsv); })
1087 #else /* __GNUC__ */
1088 #ifndef USE_THREADS
1089 /* These inlined macros use globals, which will require a thread
1090  * declaration in user code, so we avoid them under threads */
1091
1092 #  undef SvIVx
1093 #  undef SvUVx
1094 #  undef SvNVx
1095 #  undef SvPVx
1096 #  undef SvPVutf8x
1097 #  undef SvPVbytex
1098 #  undef SvTRUE
1099 #  undef SvTRUEx
1100 #  define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
1101 #  define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
1102 #  define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
1103 #  define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
1104 #  define SvPVutf8x(sv, lp) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, lp))
1105 #  define SvPVbytex(sv, lp) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, lp))
1106 #  define SvTRUE(sv) (                                          \
1107     !sv                                                         \
1108     ? 0                                                         \
1109     :    SvPOK(sv)                                              \
1110         ?   ((PL_Xpv = (XPV*)SvANY(sv)) &&                      \
1111              (PL_Xpv->xpv_cur > 1 ||                            \
1112               (PL_Xpv->xpv_cur && *PL_Xpv->xpv_pv != '0'))      \
1113              ? 1                                                \
1114              : 0)                                               \
1115         :                                                       \
1116             SvIOK(sv)                                           \
1117             ? SvIVX(sv) != 0                                    \
1118             :   SvNOK(sv)                                       \
1119                 ? SvNVX(sv) != 0.0                              \
1120                 : sv_2bool(sv) )
1121 #  define SvTRUEx(sv) ((PL_Sv = (sv)), SvTRUE(PL_Sv))
1122 #endif /* !USE_THREADS */
1123 #endif /* !__GNU__ */
1124 #endif /* !CRIPPLED_CC */
1125
1126 /*
1127 =for apidoc Am|SV*|newRV_inc|SV* sv
1128
1129 Creates an RV wrapper for an SV.  The reference count for the original SV is
1130 incremented.
1131
1132 =cut
1133 */
1134
1135 #define newRV_inc(sv)   newRV(sv)
1136
1137 /* the following macros update any magic values this sv is associated with */
1138
1139 /*
1140 =for apidoc Am|void|SvGETMAGIC|SV* sv
1141 Invokes C<mg_get> on an SV if it has 'get' magic.  This macro evaluates its
1142 argument more than once.
1143
1144 =for apidoc Am|void|SvSETMAGIC|SV* sv
1145 Invokes C<mg_set> on an SV if it has 'set' magic.  This macro evaluates its
1146 argument more than once.
1147
1148 =for apidoc Am|void|SvSetSV|SV* dsb|SV* ssv
1149 Calls C<sv_setsv> if dsv is not the same as ssv.  May evaluate arguments
1150 more than once.
1151
1152 =for apidoc Am|void|SvSetSV_nosteal|SV* dsv|SV* ssv
1153 Calls a non-destructive version of C<sv_setsv> if dsv is not the same as
1154 ssv. May evaluate arguments more than once.
1155
1156 =for apidoc Am|void|SvSetMagicSV|SV* dsb|SV* ssv
1157 Like C<SvSetSV>, but does any set magic required afterwards.
1158
1159 =for apidoc Am|void|SvSetMagicSV_nosteal|SV* dsv|SV* ssv
1160 Like C<SvSetMagicSV>, but does any set magic required afterwards.
1161
1162 =for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
1163 Expands the character buffer in the SV so that it has room for the
1164 indicated number of bytes (remember to reserve space for an extra trailing
1165 NUL character).  Calls C<sv_grow> to perform the expansion if necessary. 
1166 Returns a pointer to the character buffer.
1167
1168 =cut
1169 */
1170
1171 #define SvGETMAGIC(x) STMT_START { if (SvGMAGICAL(x)) mg_get(x); } STMT_END
1172 #define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
1173
1174 #define SvSetSV_and(dst,src,finally) \
1175         STMT_START {                                    \
1176             if ((dst) != (src)) {                       \
1177                 sv_setsv(dst, src);                     \
1178                 finally;                                \
1179             }                                           \
1180         } STMT_END
1181 #define SvSetSV_nosteal_and(dst,src,finally) \
1182         STMT_START {                                    \
1183             if ((dst) != (src)) {                       \
1184                 U32 tMpF = SvFLAGS(src) & SVs_TEMP;     \
1185                 SvTEMP_off(src);                        \
1186                 sv_setsv(dst, src);                     \
1187                 SvFLAGS(src) |= tMpF;                   \
1188                 finally;                                \
1189             }                                           \
1190         } STMT_END
1191
1192 #define SvSetSV(dst,src) \
1193                 SvSetSV_and(dst,src,/*nothing*/;)
1194 #define SvSetSV_nosteal(dst,src) \
1195                 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
1196
1197 #define SvSetMagicSV(dst,src) \
1198                 SvSetSV_and(dst,src,SvSETMAGIC(dst))
1199 #define SvSetMagicSV_nosteal(dst,src) \
1200                 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
1201
1202 #ifdef DEBUGGING
1203 #define SvPEEK(sv) sv_peek(sv)
1204 #else
1205 #define SvPEEK(sv) ""
1206 #endif
1207
1208 #define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no)
1209
1210 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
1211
1212 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
1213
1214 #define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
1215 #define Sv_Grow sv_grow
1216
1217 #define CLONEf_COPY_STACKS 1
1218 #define CLONEf_KEEP_PTR_TABLE 2
1219 #define CLONEf_CLONE_HOST 4
1220
1221 typedef struct {
1222   AV* stashes;
1223   UV  flags;
1224 } clone_params;