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