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