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