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