This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Promote v5.36 usage and feature bundles doc
[perl5.git] / sv.h
CommitLineData
a0d0e21e 1/* sv.h
79072805 2 *
1129b882 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
83706693 4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
79072805
LW
9 */
10
a0d0e21e
LW
11#ifdef sv_flags
12#undef sv_flags /* Convex has this in <signal.h> for sigvec() */
13#endif
14
954c1994 15/*
3f620621 16=for apidoc_section $SV_flags
ccfc67b7 17
e17573c0 18=for apidoc Ay||svtype
75af9d73 19An enum of flags for Perl types. These are found in the file F<sv.h>
954c1994
GS
20in the C<svtype> enum. Test these flags with the C<SvTYPE> macro.
21
a5e62da0
FC
22The types are:
23
24 SVt_NULL
a5e62da0
FC
25 SVt_IV
26 SVt_NV
27 SVt_RV
28 SVt_PV
29 SVt_PVIV
30 SVt_PVNV
31 SVt_PVMG
e94d9b54 32 SVt_INVLIST
a5e62da0
FC
33 SVt_REGEXP
34 SVt_PVGV
35 SVt_PVLV
36 SVt_PVAV
37 SVt_PVHV
38 SVt_PVCV
39 SVt_PVFM
40 SVt_PVIO
41
42These are most easily explained from the bottom up.
43
796b6530
KW
44C<SVt_PVIO> is for I/O objects, C<SVt_PVFM> for formats, C<SVt_PVCV> for
45subroutines, C<SVt_PVHV> for hashes and C<SVt_PVAV> for arrays.
a5e62da0
FC
46
47All the others are scalar types, that is, things that can be bound to a
48C<$> variable. For these, the internal types are mostly orthogonal to
49types in the Perl language.
50
51Hence, checking C<< SvTYPE(sv) < SVt_PVAV >> is the best way to see whether
52something is a scalar.
53
796b6530
KW
54C<SVt_PVGV> represents a typeglob. If C<!SvFAKE(sv)>, then it is a real,
55incoercible typeglob. If C<SvFAKE(sv)>, then it is a scalar to which a
a5e62da0 56typeglob has been assigned. Assigning to it again will stop it from being
796b6530 57a typeglob. C<SVt_PVLV> represents a scalar that delegates to another scalar
a5e62da0
FC
58behind the scenes. It is used, e.g., for the return value of C<substr> and
59for tied hash and array elements. It can hold any scalar value, including
796b6530
KW
60a typeglob. C<SVt_REGEXP> is for regular
61expressions. C<SVt_INVLIST> is for Perl
e94d9b54 62core internal use only.
a5e62da0 63
796b6530 64C<SVt_PVMG> represents a "normal" scalar (not a typeglob, regular expression,
a5e62da0
FC
65or delegate). Since most scalars do not need all the internal fields of a
66PVMG, we save memory by allocating smaller structs when possible. All the
796b6530
KW
67other types are just simpler forms of C<SVt_PVMG>, with fewer internal fields.
68C<SVt_NULL> can only hold undef. C<SVt_IV> can hold undef, an integer, or a
69reference. (C<SVt_RV> is an alias for C<SVt_IV>, which exists for backward
70compatibility.) C<SVt_NV> can hold any of those or a double. C<SVt_PV> can only
71hold C<undef> or a string. C<SVt_PVIV> is a superset of C<SVt_PV> and C<SVt_IV>.
72C<SVt_PVNV> is similar. C<SVt_PVMG> can hold anything C<SVt_PVNV> can hold, but it
a5e62da0
FC
73can, but does not have to, be blessed or magical.
74
78342678 75=for apidoc AmnU||SVt_NULL
a5e62da0 76Type flag for scalars. See L</svtype>.
954c1994 77
78342678 78=for apidoc AmnU||SVt_IV
a5e62da0 79Type flag for scalars. See L</svtype>.
954c1994 80
78342678 81=for apidoc AmnU||SVt_NV
a5e62da0
FC
82Type flag for scalars. See L</svtype>.
83
78342678 84=for apidoc AmnU||SVt_PV
a5e62da0
FC
85Type flag for scalars. See L</svtype>.
86
78342678 87=for apidoc AmnU||SVt_PVIV
a5e62da0
FC
88Type flag for scalars. See L</svtype>.
89
78342678 90=for apidoc AmnU||SVt_PVNV
a5e62da0 91Type flag for scalars. See L</svtype>.
954c1994 92
78342678 93=for apidoc AmnU||SVt_PVMG
a5e62da0
FC
94Type flag for scalars. See L</svtype>.
95
f85ae787
KW
96=for apidoc CmnU||SVt_INVLIST
97Type flag for scalars. See L<perlapi/svtype>.
e94d9b54 98
78342678 99=for apidoc AmnU||SVt_REGEXP
a5e62da0
FC
100Type flag for regular expressions. See L</svtype>.
101
78342678 102=for apidoc AmnU||SVt_PVGV
a5e62da0
FC
103Type flag for typeglobs. See L</svtype>.
104
78342678 105=for apidoc AmnU||SVt_PVLV
a5e62da0 106Type flag for scalars. See L</svtype>.
954c1994 107
78342678 108=for apidoc AmnU||SVt_PVAV
a5e62da0 109Type flag for arrays. See L</svtype>.
954c1994 110
78342678 111=for apidoc AmnU||SVt_PVHV
a5e62da0 112Type flag for hashes. See L</svtype>.
954c1994 113
78342678 114=for apidoc AmnU||SVt_PVCV
a5e62da0
FC
115Type flag for subroutines. See L</svtype>.
116
78342678 117=for apidoc AmnU||SVt_PVFM
a5e62da0
FC
118Type flag for formats. See L</svtype>.
119
78342678 120=for apidoc AmnU||SVt_PVIO
a5e62da0 121Type flag for I/O objects. See L</svtype>.
954c1994
GS
122
123=cut
1078c6a2
KW
124
125 These are ordered so that the simpler types have a lower value; SvUPGRADE
126 doesn't allow you to upgrade from a higher numbered type to a lower numbered
127 one; also there is code that assumes that anything that has as a PV component
128 has a type numbered >= SVt_PV.
954c1994
GS
129*/
130
1078c6a2 131
79072805 132typedef enum {
1f4fbd3b
MS
133 SVt_NULL, /* 0 */
134 /* BIND was here, before INVLIST replaced it. */
135 SVt_IV, /* 1 */
136 SVt_NV, /* 2 */
137 /* RV was here, before it was merged with IV. */
138 SVt_PV, /* 3 */
139 SVt_INVLIST, /* 4, implemented as a PV */
140 SVt_PVIV, /* 5 */
141 SVt_PVNV, /* 6 */
142 SVt_PVMG, /* 7 */
143 SVt_REGEXP, /* 8 */
144 /* PVBM was here, before BIND replaced it. */
145 SVt_PVGV, /* 9 */
146 SVt_PVLV, /* 10 */
147 SVt_PVAV, /* 11 */
148 SVt_PVHV, /* 12 */
149 SVt_PVCV, /* 13 */
150 SVt_PVFM, /* 14 */
151 SVt_PVIO, /* 15 */
90ba01ad
KW
152 /* 16-31: Unused, though one should be reserved for a
153 * freed sv, if the other 3 bits below the flags ones
154 * get allocated */
1f4fbd3b 155 SVt_LAST /* keep last in enum. used to size arrays */
79072805
LW
156} svtype;
157
f1fb8741 158/* *** any alterations to the SV types above need to be reflected in
e94d9b54
KW
159 * SVt_MASK and the various PL_valid_types_* tables. As of this writing those
160 * tables are in perl.h. There are also two affected names tables in dump.c,
e22533f8 161 * one in B.xs, and 'bodies_by_type[]' in sv_inline.h.
f40abc99 162 *
90ba01ad
KW
163 * The bits that match 0xe0 are CURRENTLY UNUSED
164 * The bits above that are for flags, like SVf_IOK */
f1fb8741 165
90ba01ad 166#define SVt_MASK 0x1f /* smallest bitmask that covers all types */
f1fb8741 167
cecf5685 168#ifndef PERL_CORE
a5c7cb08 169/* Fast Boyer Moore tables are now stored in magic attached to PVMGs */
cecf5685 170# define SVt_PVBM SVt_PVMG
4df7f6af
NC
171/* Anything wanting to create a reference from clean should ensure that it has
172 a scalar of type SVt_IV now: */
173# define SVt_RV SVt_IV
cecf5685
NC
174#endif
175
caf0b9e5 176/* The array of arena roots for SV bodies is indexed by SvTYPE. SVt_NULL doesn't
94ee6ed7
NC
177 * use a body, so that arena root is re-used for HEs. SVt_IV also doesn't, so
178 * that arena root is used for HVs with struct xpvhv_aux. */
75acd14e 179
6a93a7e5 180#if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
caf0b9e5 181# define HE_ARENA_ROOT_IX SVt_NULL
6a93a7e5 182#endif
94ee6ed7
NC
183#if defined(PERL_IN_HV_C) || defined(PERL_IN_SV_C)
184# define HVAUX_ARENA_ROOT_IX SVt_IV
185#endif
ff121dc6
DD
186#ifdef PERL_IN_SV_C
187# define SVt_FIRST SVt_NULL /* the type of SV that new_SV() in sv.c returns */
188#endif
43e6e717 189
232d1c15
NC
190#define PERL_ARENA_ROOTS_SIZE (SVt_LAST)
191
43e6e717
NC
192/* typedefs to eliminate some typing */
193typedef struct he HE;
194typedef struct hek HEK;
195
79072805
LW
196/* Using C's structural equivalence to help emulate C++ inheritance here... */
197
30153bd2
JC
198/* start with 2 sv-head building blocks */
199#define _SV_HEAD(ptrtype) \
200 ptrtype sv_any; /* pointer to body */ \
201 U32 sv_refcnt; /* how many references to us */ \
202 U32 sv_flags /* what we are */
203
5b306eef
DD
204#if NVSIZE <= IVSIZE
205# define _NV_BODYLESS_UNION NV svu_nv;
206#else
207# define _NV_BODYLESS_UNION
208#endif
209
30153bd2
JC
210#define _SV_HEAD_UNION \
211 union { \
1f4fbd3b
MS
212 char* svu_pv; /* pointer to malloced string */ \
213 IV svu_iv; \
214 UV svu_uv; \
215 _NV_BODYLESS_UNION \
216 SV* svu_rv; /* pointer to another SV */ \
217 SV** svu_array; \
218 HE** svu_hash; \
219 GP* svu_gp; \
220 PerlIO *svu_fp; \
bb3b7d7b
DD
221 } sv_u \
222 _SV_HEAD_DEBUG
30153bd2 223
bb3b7d7b
DD
224#ifdef DEBUG_LEAKING_SCALARS
225#define _SV_HEAD_DEBUG ;\
226 PERL_BITFIELD32 sv_debug_optype:9; /* the type of OP that allocated us */ \
227 PERL_BITFIELD32 sv_debug_inpad:1; /* was allocated in a pad for an OP */ \
228 PERL_BITFIELD32 sv_debug_line:16; /* the line where we were allocated */ \
229 UV sv_debug_serial; /* serial number of sv allocation */ \
230 char * sv_debug_file; /* the file where we were allocated */ \
231 SV * sv_debug_parent /* what we were cloned from (ithreads)*/
232#else
233#define _SV_HEAD_DEBUG
234#endif
30153bd2 235
5e045b90 236struct STRUCT_SV { /* struct sv { */
30153bd2
JC
237 _SV_HEAD(void*);
238 _SV_HEAD_UNION;
79072805
LW
239};
240
241struct gv {
30153bd2
JC
242 _SV_HEAD(XPVGV*); /* pointer to xpvgv body */
243 _SV_HEAD_UNION;
79072805
LW
244};
245
246struct cv {
30153bd2
JC
247 _SV_HEAD(XPVCV*); /* pointer to xpvcv body */
248 _SV_HEAD_UNION;
79072805
LW
249};
250
251struct av {
0f7b1ae0 252 _SV_HEAD(XPVAV*); /* pointer to xpvav body */
30153bd2 253 _SV_HEAD_UNION;
79072805
LW
254};
255
256struct hv {
30153bd2
JC
257 _SV_HEAD(XPVHV*); /* pointer to xpvhv body */
258 _SV_HEAD_UNION;
8990e307
LW
259};
260
261struct io {
30153bd2
JC
262 _SV_HEAD(XPVIO*); /* pointer to xpvio body */
263 _SV_HEAD_UNION;
79072805
LW
264};
265
d2f13c59
NC
266struct p5rx {
267 _SV_HEAD(struct regexp*); /* pointer to regexp body */
268 _SV_HEAD_UNION;
269};
270
47e6c6d9
N
271struct invlist {
272 _SV_HEAD(XINVLIST*); /* pointer to xpvinvlist body */
273 _SV_HEAD_UNION;
274};
275
30153bd2 276#undef _SV_HEAD
0f7b1ae0 277#undef _SV_HEAD_UNION /* ensure no pollution */
30153bd2 278
954c1994 279/*
3f620621 280=for apidoc_section $SV
ccfc67b7 281
954c1994 282=for apidoc Am|U32|SvREFCNT|SV* sv
cb198164
YO
283Returns the value of the object's reference count. Exposed
284to perl code via Internals::SvREFCNT().
954c1994 285
1607e393
KW
286=for apidoc SvREFCNT_inc
287=for apidoc_item SvREFCNT_inc_NN
288=for apidoc_item |SV* |SvREFCNT_inc_simple|SV* sv
289=for apidoc_item |SV* |SvREFCNT_inc_simple_NN|SV* sv
52f0fcf7
KW
290=for apidoc_item |void|SvREFCNT_inc_simple_void|SV* sv
291=for apidoc_item |void|SvREFCNT_inc_simple_void_NN|SV* sv
1607e393
KW
292=for apidoc_item SvREFCNT_inc_void
293=for apidoc_item |void|SvREFCNT_inc_void_NN|SV* sv
954c1994 294
52f0fcf7
KW
295These all increment the reference count of the given SV.
296The ones without C<void> in their names return the SV.
b37c2d43 297
52f0fcf7
KW
298C<SvREFCNT_inc> is the base operation; the rest are optimizations if various
299input constraints are known to be true; hence, all can be replaced with
300C<SvREFCNT_inc>.
301
302C<SvREFCNT_inc_NN> can only be used if you know C<sv> is not C<NULL>. Since we
303don't have to check the NULLness, it's faster and smaller.
b37c2d43 304
52f0fcf7 305C<SvREFCNT_inc_void> can only be used if you don't need the
b37c2d43
AL
306return value. The macro doesn't need to return a meaningful value.
307
52f0fcf7
KW
308C<SvREFCNT_inc_void_NN> can only be used if you both don't need the return
309value, and you know that C<sv> is not C<NULL>. The macro doesn't need to
310return a meaningful value, or check for NULLness, so it's smaller and faster.
b37c2d43 311
52f0fcf7 312C<SvREFCNT_inc_simple> can only be used with expressions without side
4ea561bc 313effects. Since we don't have to store a temporary value, it's faster.
b37c2d43 314
52f0fcf7
KW
315C<SvREFCNT_inc_simple_NN> can only be used with expressions without side
316effects and you know C<sv> is not C<NULL>. Since we don't have to store a
317temporary value, nor check for NULLness, it's faster and smaller.
b37c2d43 318
52f0fcf7
KW
319C<SvREFCNT_inc_simple_void> can only be used with expressions without side
320effects and you don't need the return value.
b37c2d43 321
52f0fcf7
KW
322C<SvREFCNT_inc_simple_void_NN> can only be used with expressions without side
323effects, you don't need the return value, and you know C<sv> is not C<NULL>.
b37c2d43 324
3f2f854a 325=for apidoc SvREFCNT_dec
b29c03ae 326=for apidoc_item SvREFCNT_dec_NN
954c1994 327
b29c03ae
KW
328These decrement the reference count of the given SV.
329
330C<SvREFCNT_dec_NN> may only be used when C<sv> is known to not be C<NULL>.
4a9a56a7 331
954c1994 332=for apidoc Am|svtype|SvTYPE|SV* sv
fbe13c60 333Returns the type of the SV. See C<L</svtype>>.
954c1994
GS
334
335=for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
336Used to upgrade an SV to a more complex form. Uses C<sv_upgrade> to
fbe13c60 337perform the upgrade if necessary. See C<L</svtype>>.
954c1994
GS
338
339=cut
340*/
341
463ee0b2 342#define SvANY(sv) (sv)->sv_any
79072805 343#define SvFLAGS(sv) (sv)->sv_flags
aeea060c 344#define SvREFCNT(sv) (sv)->sv_refcnt
a863c7d1 345
c9182d9c 346#define SvREFCNT_inc(sv) Perl_SvREFCNT_inc(MUTABLE_SV(sv))
27669aa4 347#define SvREFCNT_inc_simple(sv) SvREFCNT_inc(sv)
c9182d9c
KW
348#define SvREFCNT_inc_NN(sv) Perl_SvREFCNT_inc_NN(MUTABLE_SV(sv))
349#define SvREFCNT_inc_void(sv) Perl_SvREFCNT_inc_void(MUTABLE_SV(sv))
8990e307 350
b37c2d43 351/* These guys don't need the curly blocks */
fa50d782
KW
352#define SvREFCNT_inc_simple_void(sv) \
353 STMT_START { \
354 SV * sv_ = MUTABLE_SV(sv); \
355 if (sv_) \
356 SvREFCNT(sv_)++; \
357 } STMT_END
358
b1bc3f34
NC
359#define SvREFCNT_inc_simple_NN(sv) (++(SvREFCNT(sv)),MUTABLE_SV(sv))
360#define SvREFCNT_inc_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
361#define SvREFCNT_inc_simple_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
b37c2d43 362
c9182d9c
KW
363#define SvREFCNT_dec(sv) Perl_SvREFCNT_dec(aTHX_ MUTABLE_SV(sv))
364#define SvREFCNT_dec_NN(sv) Perl_SvREFCNT_dec_NN(aTHX_ MUTABLE_SV(sv))
a863c7d1 365
8990e307 366#define SVTYPEMASK 0xff
1fef4616 367#define SvTYPE(sv) ((svtype)((sv)->sv_flags & SVTYPEMASK))
79072805 368
0565a181
NC
369/* Sadly there are some parts of the core that have pointers to already-freed
370 SV heads, and rely on being able to tell that they are now free. So mark
371 them all by using a consistent macro. */
7667e7f1 372#define SvIS_FREED(sv) UNLIKELY(((sv)->sv_flags == SVTYPEMASK))
0565a181 373
24e08842
DM
374/* this is defined in this peculiar way to avoid compiler warnings.
375 * See the <20121213131428.GD1842@iabyn.com> thread in p5p */
463ea229 376#define SvUPGRADE(sv, mt) \
24e08842 377 ((void)(SvTYPE(sv) >= (mt) || (sv_upgrade(sv, mt),1)))
79072805 378
3f7ef1d4
NC
379#define SVf_IOK 0x00000100 /* has valid public integer value */
380#define SVf_NOK 0x00000200 /* has valid public numeric value */
381#define SVf_POK 0x00000400 /* has valid public pointer value */
382#define SVf_ROK 0x00000800 /* has a valid reference pointer */
383
384#define SVp_IOK 0x00001000 /* has valid non-public integer value */
385#define SVp_NOK 0x00002000 /* has valid non-public numeric value */
386#define SVp_POK 0x00004000 /* has valid non-public pointer value */
9a70c74b 387#define SVp_SCREAM 0x00008000 /* currently unused on plain scalars */
2e5b91de
NC
388#define SVphv_CLONEABLE SVp_SCREAM /* PVHV (stashes) clone its objects */
389#define SVpgv_GP SVp_SCREAM /* GV has a valid GP */
1ccdb730 390#define SVprv_PCS_IMPORTED SVp_SCREAM /* RV is a proxy for a constant
1f4fbd3b
MS
391 subroutine in another package. Set the
392 GvIMPORTED_CV_on() if it needs to be
393 expanded to a real GV */
cc25b9e2
DM
394
395/* SVf_PROTECT is what SVf_READONLY should have been: i.e. modifying
396 * this SV is completely illegal. However, SVf_READONLY (via
397 * Internals::SvREADONLY()) has come to be seen as a flag that can be
398 * temporarily set and unset by the user to indicate e.g. whether a hash
399 * is "locked". Now, Hash::Util et al only set SVf_READONLY, while core
400 * sets both (SVf_READONLY|SVf_PROTECT) to indicate both to core and user
401 * code that this SV should not be messed with.
402 */
fd01b4b7 403#define SVf_PROTECT 0x00010000 /* very read-only */
c0683843 404#define SVs_PADTMP 0x00020000 /* in use as tmp */
c0683843 405#define SVs_PADSTALE 0x00040000 /* lexical has gone out of scope;
1f4fbd3b 406 only used when !PADTMP */
2c39754a 407#define SVs_TEMP 0x00080000 /* mortal (implies string is stealable) */
3f7ef1d4
NC
408#define SVs_OBJECT 0x00100000 /* is "blessed" */
409#define SVs_GMG 0x00200000 /* has magical get method */
410#define SVs_SMG 0x00400000 /* has magical set method */
411#define SVs_RMG 0x00800000 /* has random magical methods */
412
fea3622c 413#define SVf_FAKE 0x01000000 /* 0: glob is just a copy
1f4fbd3b
MS
414 1: SV head arena wasn't malloc()ed
415 2: For PVCV, whether CvUNIQUE(cv)
416 refers to an eval or once only
417 [CvEVAL(cv), CvSPECIAL(cv)]
0f94cb1f 418 3: HV: informally reserved by DAPM
df6b4bd5
DM
419 for vtables
420 4: Together with other flags (or
421 lack thereof) indicates a regex,
422 including PVLV-as-regex. See
423 isREGEXP().
424 */
922562d1
PE
425#define SVf_OOK 0x02000000 /* has valid offset value */
426#define SVphv_HasAUX SVf_OOK /* PVHV has an additional hv_aux struct */
3f7ef1d4 427#define SVf_BREAK 0x04000000 /* refcnt is artificially low - used by
1f4fbd3b
MS
428 SVs in final arena cleanup.
429 Set in S_regtry on PL_reg_curpm, so that
430 perl_destruct will skip it.
a5f48505
DM
431 Used for mark and sweep by OP_AASSIGN
432 */
3f7ef1d4 433#define SVf_READONLY 0x08000000 /* may not be modified */
8990e307 434
a0d0e21e 435
8990e307 436
5bc28da9 437
a623f893 438#define SVf_THINKFIRST (SVf_READONLY|SVf_PROTECT|SVf_ROK|SVf_FAKE \
1f4fbd3b 439 |SVs_RMG|SVf_IsCOW)
5bc28da9 440
a0d0e21e 441#define SVf_OK (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
1f4fbd3b 442 SVp_IOK|SVp_NOK|SVp_POK|SVpgv_GP)
a0d0e21e 443
7a77f1af 444#define PRIVSHIFT 4 /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
8990e307 445
a3815e44 446/* SVf_AMAGIC means that the stash *may* have overload methods. It's
d3d326f2
DM
447 * set each time a function is compiled into a stash, and is reset by the
448 * overload code when called for the first time and finds that there are
449 * no overload methods. Note that this used to be set on the object; but
450 * is now only set on stashes.
451 */
cd028baa 452#define SVf_AMAGIC 0x10000000 /* has magical overloaded methods */
45eaf8af 453#define SVf_IsCOW 0x10000000 /* copy on write (shared hash key if
1f4fbd3b 454 SvLEN == 0) */
70bc21b7 455
44b2d6d6
FC
456/* Ensure this value does not clash with the GV_ADD* flags in gv.h, or the
457 CV_CKPROTO_* flags in op.c, or the padadd_* flags in pad.h: */
cd028baa 458#define SVf_UTF8 0x20000000 /* SvPV is UTF-8 encoded
1f4fbd3b
MS
459 This is also set on RVs whose overloaded
460 stringification is UTF-8. This might
461 only happen as a side effect of SvPV() */
70bc21b7
DM
462/* PVHV */
463#define SVphv_SHAREKEYS 0x20000000 /* PVHV keys live on shared string table */
8990e307 464
2e9f07a2
NC
465/* PVAV could probably use 0x2000000 without conflict. I assume that PVFM can
466 be UTF-8 encoded, and PVCVs could well have UTF-8 prototypes. PVIOs haven't
467 been restructured, so sometimes get used as string buffers. */
468
70bc21b7
DM
469
470/* Some private flags. */
471
e1dcbbca 472
914bb574
PE
473/* scalar SVs with SVp_POK */
474#define SVppv_STATIC 0x40000000 /* PV is pointer to static const; must be set with SVf_IsCOW */
26a32e18 475/* PVAV */
3f7ef1d4 476#define SVpav_REAL 0x40000000 /* free old entries */
26a32e18 477/* PVHV */
3f7ef1d4 478#define SVphv_LAZYDEL 0x40000000 /* entry in xhv_eiter must be deleted */
25da4f38 479
26a32e18 480/* IV, PVIV, PVNV, PVMG, PVGV and (I assume) PVLV */
3f7ef1d4 481#define SVf_IVisUV 0x80000000 /* use XPVUV instead of XPVIV */
26a32e18 482/* PVAV */
3f7ef1d4 483#define SVpav_REIFY 0x80000000 /* can become real */
26a32e18 484/* PVHV */
3f7ef1d4 485#define SVphv_HASKFLAGS 0x80000000 /* keys have flag byte after hash */
26a32e18 486/* RV upwards. However, SVf_ROK and SVp_IOK are exclusive */
3f7ef1d4 487#define SVprv_WEAKREF 0x80000000 /* Weak reference */
d3ce79bd 488/* pad name vars only */
810b8aa5 489
6e128786
NC
490#define _XPV_HEAD \
491 HV* xmg_stash; /* class package */ \
492 union _xmgu xmg_u; \
8de47657 493 STRLEN xpv_cur; /* length of svu_pv as a C string */ \
8d919b0a 494 union { \
1f4fbd3b 495 STRLEN xpvlenu_len; /* allocated size */ \
df6b4bd5 496 struct regexp* xpvlenu_rx; /* regex when SV body is XPVLV */ \
1f4fbd3b 497 } xpv_len_u
8d919b0a
FC
498
499#define xpv_len xpv_len_u.xpvlenu_len
bb02a38f 500
20f4945e
MHM
501union _xnvu {
502 NV xnv_nv; /* numeric value, if any */
503 HV * xgv_stash;
9420b268 504 line_t xnv_lines; /* used internally by S_scan_subst() */
b4204fb6 505 bool xnv_bm_tail; /* an SvVALID (BM) SV has an implicit "\n" */
20f4945e
MHM
506};
507
508union _xivu {
509 IV xivu_iv; /* integer value */
20f4945e 510 UV xivu_uv;
20f4945e 511 HEK * xivu_namehek; /* xpvlv, xpvgv: GvNAME */
6432a58a 512 bool xivu_eval_seen; /* used internally by S_scan_subst() */
b4204fb6 513
20f4945e
MHM
514};
515
516union _xmgu {
517 MAGIC* xmg_magic; /* linked list of magicalness */
5bec93be 518 STRLEN xmg_hash_index; /* used while freeing hash entries */
9b7476d7 519};
11ca45c0 520
79072805 521struct xpv {
20f4945e 522 _XPV_HEAD;
79072805
LW
523};
524
525struct xpviv {
20f4945e
MHM
526 _XPV_HEAD;
527 union _xivu xiv_u;
79072805
LW
528};
529
311a25d9
NC
530#define xiv_iv xiv_u.xivu_iv
531
ff68c719 532struct xpvuv {
20f4945e
MHM
533 _XPV_HEAD;
534 union _xivu xuv_u;
ff68c719 535};
536
20f4945e 537#define xuv_uv xuv_u.xivu_uv
311a25d9 538
79072805 539struct xpvnv {
20f4945e
MHM
540 _XPV_HEAD;
541 union _xivu xiv_u;
6e128786 542 union _xnvu xnv_u;
79072805
LW
543};
544
3cd3be43 545/* This structure must match the beginning of struct xpvhv in hv.h. */
79072805 546struct xpvmg {
20f4945e 547 _XPV_HEAD;
6e128786
NC
548 union _xivu xiv_u;
549 union _xnvu xnv_u;
79072805
LW
550};
551
552struct xpvlv {
20f4945e 553 _XPV_HEAD;
6e128786
NC
554 union _xivu xiv_u;
555 union _xnvu xnv_u;
bbfdc870 556 union {
1f4fbd3b
MS
557 STRLEN xlvu_targoff;
558 SSize_t xlvu_stargoff;
bbfdc870 559 } xlv_targoff_u;
79072805
LW
560 STRLEN xlv_targlen;
561 SV* xlv_targ;
dd28f7bb 562 char xlv_type; /* k=keys .=pos x=substr v=vec /=join/re
1f4fbd3b 563 * y=alem/helem/iter t=tie T=tied HE */
1b92e694
DM
564 char xlv_flags; /* 1 = negative offset 2 = negative len
565 4 = out of range (vec) */
79072805
LW
566};
567
bbfdc870
FC
568#define xlv_targoff xlv_targoff_u.xlvu_targoff
569
d361b004
KW
570struct xpvinvlist {
571 _XPV_HEAD;
da62e549
KW
572 IV prev_index; /* caches result of previous invlist_search() */
573 STRLEN iterator; /* Stores where we are in iterating */
574 bool is_offset; /* The data structure for all inversion lists
575 begins with an element for code point U+0000.
576 If this bool is set, the actual list contains
577 that 0; otherwise, the list actually begins
578 with the following element. Thus to invert
579 the list, merely toggle this flag */
d361b004
KW
580};
581
a5c7cb08
DM
582/* This structure works in 2 ways - regular scalar, or GV with GP */
583
79072805 584struct xpvgv {
20f4945e 585 _XPV_HEAD;
6e128786
NC
586 union _xivu xiv_u;
587 union _xnvu xnv_u;
79072805
LW
588};
589
51c78f1b 590typedef U32 cv_flags_t;
77a005ab 591
20f4945e
MHM
592#define _XPVCV_COMMON \
593 HV * xcv_stash; \
594 union { \
1f4fbd3b
MS
595 OP * xcv_start; \
596 ANY xcv_xsubany; \
20f4945e
MHM
597 } xcv_start_u; \
598 union { \
1f4fbd3b
MS
599 OP * xcv_root; \
600 void (*xcv_xsub) (pTHX_ CV*); \
20f4945e 601 } xcv_root_u; \
b290562e 602 union { \
1f4fbd3b
MS
603 GV * xcv_gv; \
604 HEK * xcv_hek; \
b290562e 605 } xcv_gv_u; \
20f4945e 606 char * xcv_file; \
eacbb379 607 union { \
1f4fbd3b
MS
608 PADLIST * xcv_padlist; \
609 void * xcv_hscxt; \
eacbb379 610 } xcv_padlist_u; \
20f4945e
MHM
611 CV * xcv_outside; \
612 U32 xcv_outside_seq; /* the COP sequence (at the point of our \
1f4fbd3b
MS
613 * compilation) in the lexically enclosing \
614 * sub */ \
8de47657
FC
615 cv_flags_t xcv_flags; \
616 I32 xcv_depth /* >= 2 indicates recursive call */
8990e307 617
0d856a3a
FC
618/* This structure must match XPVCV in cv.h */
619
20f4945e
MHM
620struct xpvfm {
621 _XPV_HEAD;
20f4945e 622 _XPVCV_COMMON;
79072805
LW
623};
624
167f2c4d 625
8990e307 626struct xpvio {
20f4945e 627 _XPV_HEAD;
6e128786 628 union _xivu xiv_u;
6f7e8353
NC
629 /* ifp and ofp are normally the same, but sockets need separate streams */
630 PerlIO * xio_ofp;
c951e2ad
NC
631 /* Cray addresses everything by word boundaries (64 bits) and
632 * code and data pointers cannot be mixed (which is exactly what
633 * Perl_filter_add() tries to do with the dirp), hence the
634 * following union trick (as suggested by Gurusamy Sarathy).
635 * For further information see Geir Johansen's problem report
4fe48737 636 * titled [ID 20000612.002 (#3366)] Perl problem on Cray system
c951e2ad
NC
637 * The any pointer (known as IoANY()) will also be a good place
638 * to hang any IO disciplines to.
639 */
640 union {
1f4fbd3b
MS
641 DIR * xiou_dirp; /* for opendir, readdir, etc */
642 void * xiou_any; /* for alignment */
c951e2ad
NC
643 } xio_dirpu;
644 /* IV xio_lines is now in IVX $. */
645 IV xio_page; /* $% */
646 IV xio_page_len; /* $= */
647 IV xio_lines_left; /* $- */
648 char * xio_top_name; /* $^ */
649 GV * xio_top_gv; /* $^ */
650 char * xio_fmt_name; /* $~ */
651 GV * xio_fmt_gv; /* $~ */
652 char * xio_bottom_name;/* $^B */
653 GV * xio_bottom_gv; /* $^B */
654 char xio_type;
655 U8 xio_flags;
8990e307 656};
167f2c4d 657
4755096e
GS
658#define xio_dirp xio_dirpu.xiou_dirp
659#define xio_any xio_dirpu.xiou_any
8990e307 660
e0c19803
GS
661#define IOf_ARGV 1 /* this fp iterates over ARGV */
662#define IOf_START 2 /* check for null ARGV and substitute '-' */
663#define IOf_FLUSH 4 /* this fp wants a flush after write op */
664#define IOf_DIDTOP 8 /* just did top of form */
665#define IOf_UNTAINT 16 /* consider this fp (and its data) "safe" */
666#define IOf_NOLINE 32 /* slurped a pseudo-line from empty file */
5bb89d25 667#define IOf_FAKE_DIRP 64 /* xio_dirp is fake (source filters kludge)
1f4fbd3b 668 Also, when this is set, SvPVX() is valid */
8990e307 669
ed6116ce
LW
670/* The following macros define implementation-independent predicates on SVs. */
671
954c1994 672/*
fbf9f983
RGS
673=for apidoc Am|U32|SvNIOK|SV* sv
674Returns a U32 value indicating whether the SV contains a number, integer or
954c1994
GS
675double.
676
fbf9f983
RGS
677=for apidoc Am|U32|SvNIOKp|SV* sv
678Returns a U32 value indicating whether the SV contains a number, integer or
fe749c9a 679double. Checks the B<private> setting. Use C<SvNIOK> instead.
954c1994
GS
680
681=for apidoc Am|void|SvNIOK_off|SV* sv
682Unsets the NV/IV status of an SV.
683
fbf9f983 684=for apidoc Am|U32|SvOK|SV* sv
72d33970 685Returns a U32 value indicating whether the value is defined. This is
c0b6140e 686only meaningful for scalars.
954c1994 687
fbf9f983
RGS
688=for apidoc Am|U32|SvIOKp|SV* sv
689Returns a U32 value indicating whether the SV contains an integer. Checks
fe749c9a 690the B<private> setting. Use C<SvIOK> instead.
954c1994 691
fbf9f983
RGS
692=for apidoc Am|U32|SvNOKp|SV* sv
693Returns a U32 value indicating whether the SV contains a double. Checks the
fe749c9a 694B<private> setting. Use C<SvNOK> instead.
954c1994 695
fbf9f983
RGS
696=for apidoc Am|U32|SvPOKp|SV* sv
697Returns a U32 value indicating whether the SV contains a character string.
fe749c9a 698Checks the B<private> setting. Use C<SvPOK> instead.
954c1994 699
fbf9f983
RGS
700=for apidoc Am|U32|SvIOK|SV* sv
701Returns a U32 value indicating whether the SV contains an integer.
954c1994
GS
702
703=for apidoc Am|void|SvIOK_on|SV* sv
704Tells an SV that it is an integer.
705
706=for apidoc Am|void|SvIOK_off|SV* sv
707Unsets the IV status of an SV.
708
709=for apidoc Am|void|SvIOK_only|SV* sv
796b6530 710Tells an SV that it is an integer and disables all other C<OK> bits.
954c1994 711
e331fc52 712=for apidoc Am|void|SvIOK_only_UV|SV* sv
796b6530 713Tells an SV that it is an unsigned integer and disables all other C<OK> bits.
e331fc52 714
12fa07df 715=for apidoc Am|bool|SvIOK_UV|SV* sv
a6ceea06
KW
716Returns a boolean indicating whether the SV contains an integer that must be
717interpreted as unsigned. A non-negative integer whose value is within the
a3815e44 718range of both an IV and a UV may be flagged as either C<SvUOK> or C<SvIOK>.
e331fc52 719
fbf9f983 720=for apidoc Am|bool|SvUOK|SV* sv
a6ceea06
KW
721Returns a boolean indicating whether the SV contains an integer that must be
722interpreted as unsigned. A non-negative integer whose value is within the
a3815e44 723range of both an IV and a UV may be flagged as either C<SvUOK> or C<SvIOK>.
28e5dec8 724
12fa07df 725=for apidoc Am|bool|SvIOK_notUV|SV* sv
d1be9408 726Returns a boolean indicating whether the SV contains a signed integer.
e331fc52 727
fbf9f983
RGS
728=for apidoc Am|U32|SvNOK|SV* sv
729Returns a U32 value indicating whether the SV contains a double.
954c1994
GS
730
731=for apidoc Am|void|SvNOK_on|SV* sv
732Tells an SV that it is a double.
733
734=for apidoc Am|void|SvNOK_off|SV* sv
735Unsets the NV status of an SV.
736
737=for apidoc Am|void|SvNOK_only|SV* sv
738Tells an SV that it is a double and disables all other OK bits.
739
fbf9f983
RGS
740=for apidoc Am|U32|SvPOK|SV* sv
741Returns a U32 value indicating whether the SV contains a character
954c1994
GS
742string.
743
744=for apidoc Am|void|SvPOK_on|SV* sv
745Tells an SV that it is a string.
746
747=for apidoc Am|void|SvPOK_off|SV* sv
748Unsets the PV status of an SV.
749
750=for apidoc Am|void|SvPOK_only|SV* sv
796b6530 751Tells an SV that it is a string and disables all other C<OK> bits.
1e54db1a 752Will also turn off the UTF-8 status.
954c1994 753
fff0089c
YO
754=for apidoc Am|U32|SvBoolFlagsOK|SV* sv
755Returns a bool indicating whether the SV has the right flags set such
756that it is safe to call C<BOOL_INTERNALS_sv_isbool()> or
757C<BOOL_INTERNALS_sv_isbool_true()> or
758C<BOOL_INTERNALS_sv_isbool_false()>. Currently equivalent to
759C<SvIandPOK(sv)> or C<SvIOK(sv) && SvPOK(sv)>. Serialization may want to
760unroll this check. If so you are strongly recommended to add code like
761C<assert(SvBoolFlagsOK(sv));> B<before> calling using any of the
762BOOL_INTERNALS macros.
763
764=for apidoc Am|U32|SvIandPOK|SV* sv
765Returns a bool indicating whether the SV is both C<SvPOK()> and
766C<SvIOK()> at the same time. Equivalent to C<SvIOK(sv) && SvPOK(sv)> but
767more efficient.
768
769=for apidoc Am|void|SvIandPOK_on|SV* sv
770Tells an SV that is a string and a number in one operation. Equivalent
771to C<SvIOK_on(sv); SvPOK_on(sv);> but more efficient.
772
773=for apidoc Am|void|SvIandPOK_off|SV* sv
774Unsets the PV and IV status of an SV in one operation. Equivalent to
775C<SvIOK_off(sv); SvPK_off(v);> but more efficient.
776
777=for apidoc Am|bool|BOOL_INTERNALS_sv_isbool|SV* sv
778Checks if a C<SvBoolFlagsOK()> sv is a bool. B<Note> that it is the
779caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()> before
780calling this. This is only useful in specialized logic like
781serialization code where performance is critical and the flags have
782already been checked to be correct. Almost always you should be using
783C<sv_isbool(sv)> instead.
784
785=for apidoc Am|bool|BOOL_INTERNALS_sv_isbool_true|SV* sv
786Checks if a C<SvBoolFlagsOK()> sv is a true bool. B<Note> that it is
787the caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()>
788before calling this. This is only useful in specialized logic like
789serialization code where performance is critical and the flags have
790already been checked to be correct. This is B<NOT> what you should use
791to check if an SV is "true", for that you should be using
792C<SvTRUE(sv)> instead.
793
794=for apidoc Am|bool|BOOL_INTERNALS_sv_isbool_false|SV* sv
795Checks if a C<SvBoolFlagsOK()> sv is a false bool. B<Note> that it is
796the caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()>
797before calling this. This is only useful in specialized logic like
798serialization code where performance is critical and the flags have
799already been checked to be correct. This is B<NOT> what you should use
800to check if an SV is "false", for that you should be using
801C<!SvTRUE(sv)> instead.
802
b0f01acb
JP
803=for apidoc Am|bool|SvVOK|SV* sv
804Returns a boolean indicating whether the SV contains a v-string.
805
fbf9f983 806=for apidoc Am|U32|SvOOK|SV* sv
69240efd
NC
807Returns a U32 indicating whether the pointer to the string buffer is offset.
808This hack is used internally to speed up removal of characters from the
de343f44 809beginning of a C<L</SvPV>>. When C<SvOOK> is true, then the start of the
796b6530
KW
810allocated string buffer is actually C<SvOOK_offset()> bytes before C<SvPVX>.
811This offset used to be stored in C<SvIVX>, but is now stored within the spare
69240efd 812part of the buffer.
954c1994 813
fbf9f983 814=for apidoc Am|U32|SvROK|SV* sv
954c1994
GS
815Tests if the SV is an RV.
816
817=for apidoc Am|void|SvROK_on|SV* sv
818Tells an SV that it is an RV.
819
820=for apidoc Am|void|SvROK_off|SV* sv
821Unsets the RV status of an SV.
822
823=for apidoc Am|SV*|SvRV|SV* sv
824Dereferences an RV to return the SV.
825
826=for apidoc Am|IV|SvIVX|SV* sv
645c22ef 827Returns the raw value in the SV's IV slot, without checks or conversions.
796b6530 828Only use when you are sure C<SvIOK> is true. See also C<L</SvIV>>.
954c1994
GS
829
830=for apidoc Am|UV|SvUVX|SV* sv
645c22ef 831Returns the raw value in the SV's UV slot, without checks or conversions.
796b6530 832Only use when you are sure C<SvIOK> is true. See also C<L</SvUV>>.
954c1994 833
b7822a6d
KW
834=for apidoc AmD|UV|SvUVXx|SV* sv
835This is an unnecessary synonym for L</SvUVX>
836
954c1994 837=for apidoc Am|NV|SvNVX|SV* sv
645c22ef 838Returns the raw value in the SV's NV slot, without checks or conversions.
796b6530 839Only use when you are sure C<SvNOK> is true. See also C<L</SvNV>>.
954c1994 840
1607e393 841=for apidoc Am |char* |SvPVX|SV* sv
c2b527b3 842=for apidoc_item |const char*|SvPVX_const|SV* sv
1607e393
KW
843=for apidoc_item |char* |SvPVX_mutable|SV* sv
844=for apidoc_item |char* |SvPVXx|SV* sv
c2b527b3
KW
845
846These return a pointer to the physical string in the SV. The SV must contain a
847string. Prior to 5.9.3 it is not safe to execute these unless the SV's
796b6530 848type >= C<SVt_PV>.
954c1994 849
c2b527b3 850These are also used to store the name of an autoloaded subroutine in an XS
5b36e945 851AUTOLOAD routine. See L<perlguts/Autoloading with XSUBs>.
89fc9e2a 852
c2b527b3
KW
853C<SvPVXx> is identical to C<SvPVX>.
854
855C<SvPVX_mutable> is merely a synonym for C<SvPVX>, but its name emphasizes that
856the string is modifiable by the caller.
857
858C<SvPVX_const> differs in that the return value has been cast so that the
859compiler will complain if you were to try to modify the contents of the string,
860(unless you cast away const yourself).
861
954c1994 862=for apidoc Am|STRLEN|SvCUR|SV* sv
3c3f883d
FG
863Returns the length, in bytes, of the PV inside the SV.
864Note that this may not match Perl's C<length>; for that, use
865C<sv_len_utf8(sv)>. See C<L</SvLEN>> also.
954c1994
GS
866
867=for apidoc Am|STRLEN|SvLEN|SV* sv
659239a4 868Returns the size of the string buffer in the SV, not including any part
fbe13c60 869attributable to C<SvOOK>. See C<L</SvCUR>>.
954c1994
GS
870
871=for apidoc Am|char*|SvEND|SV* sv
f990ea3e
FC
872Returns a pointer to the spot just after the last character in
873the string which is in the SV, where there is usually a trailing
417b32ce 874C<NUL> character (even though Perl scalars do not strictly require it).
796b6530 875See C<L</SvCUR>>. Access the character as C<*(SvEND(sv))>.
954c1994 876
f990ea3e
FC
877Warning: If C<SvCUR> is equal to C<SvLEN>, then C<SvEND> points to
878unallocated memory.
879
954c1994
GS
880=for apidoc Am|HV*|SvSTASH|SV* sv
881Returns the stash of the SV.
882
672994ce 883=for apidoc Am|void|SvIV_set|SV* sv|IV val
20799e15
SP
884Set the value of the IV pointer in sv to val. It is possible to perform
885the same function of this macro with an lvalue assignment to C<SvIVX>.
1f4fbd3b 886With future Perls, however, it will be more efficient to use
20799e15 887C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.
672994ce
SP
888
889=for apidoc Am|void|SvNV_set|SV* sv|NV val
796b6530 890Set the value of the NV pointer in C<sv> to val. See C<L</SvIV_set>>.
672994ce
SP
891
892=for apidoc Am|void|SvPV_set|SV* sv|char* val
f2979eac
DD
893This is probably not what you want to use, you probably wanted
894L</sv_usepvn_flags> or L</sv_setpvn> or L</sv_setpvs>.
235fc044 895
f2979eac 896Set the value of the PV pointer in C<sv> to the Perl allocated
fbe13c60 897C<NUL>-terminated string C<val>. See also C<L</SvIV_set>>.
f2979eac
DD
898
899Remember to free the previous PV buffer. There are many things to check.
235fc044
FC
900Beware that the existing pointer may be involved in copy-on-write or other
901mischief, so do C<SvOOK_off(sv)> and use C<sv_force_normal> or
796b6530 902C<SvPV_force> (or check the C<SvIsCOW> flag) first to make sure this
d54acd4f
KW
903modification is safe. Then finally, if it is not a COW, call
904C<L</SvPV_free>> to free the previous PV buffer.
672994ce
SP
905
906=for apidoc Am|void|SvUV_set|SV* sv|UV val
796b6530 907Set the value of the UV pointer in C<sv> to val. See C<L</SvIV_set>>.
672994ce
SP
908
909=for apidoc Am|void|SvRV_set|SV* sv|SV* val
796b6530 910Set the value of the RV pointer in C<sv> to val. See C<L</SvIV_set>>.
672994ce
SP
911
912=for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
796b6530 913Set the value of the MAGIC pointer in C<sv> to val. See C<L</SvIV_set>>.
672994ce 914
b6f4de24 915=for apidoc Am|void|SvSTASH_set|SV* sv|HV* val
796b6530 916Set the value of the STASH pointer in C<sv> to val. See C<L</SvIV_set>>.
672994ce 917
954c1994 918=for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
3c3f883d
FG
919Sets the current length, in bytes, of the C string which is in the SV.
920See C<L</SvCUR>> and C<SvIV_set>>.
672994ce
SP
921
922=for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
0ff57aee 923Set the size of the string buffer for the SV. See C<L</SvLEN>>.
954c1994
GS
924
925=cut
926*/
927
79072805 928#define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
748a9306 929#define SvNIOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
a0d0e21e 930#define SvNIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
1f4fbd3b 931 SVp_IOK|SVp_NOK|SVf_IVisUV))
79072805 932
c7724415
FC
933#define assert_not_ROK(sv) assert_(!SvROK(sv) || !SvRV(sv))
934#define assert_not_glob(sv) assert_(!isGV_with_GP(sv))
906ed6d6 935
df6b4bd5 936#define SvOK(sv) (SvFLAGS(sv) & SVf_OK)
f7877b28 937#define SvOK_off(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1f4fbd3b
MS
938 SvFLAGS(sv) &= ~(SVf_OK| \
939 SVf_IVisUV|SVf_UTF8), \
940 SvOOK_off(sv))
54866b45 941#define SvOK_off_exc_UV(sv) (assert_not_ROK(sv) \
1f4fbd3b
MS
942 SvFLAGS(sv) &= ~(SVf_OK| \
943 SVf_UTF8), \
944 SvOOK_off(sv))
79072805 945
8990e307
LW
946#define SvOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
947#define SvIOKp(sv) (SvFLAGS(sv) & SVp_IOK)
b326ddff 948#define SvIOKp_on(sv) (assert_not_glob(sv) \
1f4fbd3b 949 SvFLAGS(sv) |= SVp_IOK)
8990e307 950#define SvNOKp(sv) (SvFLAGS(sv) & SVp_NOK)
f7877b28 951#define SvNOKp_on(sv) (assert_not_glob(sv) SvFLAGS(sv) |= SVp_NOK)
8990e307 952#define SvPOKp(sv) (SvFLAGS(sv) & SVp_POK)
f7877b28 953#define SvPOKp_on(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1f4fbd3b 954 SvFLAGS(sv) |= SVp_POK)
463ee0b2 955
79072805 956#define SvIOK(sv) (SvFLAGS(sv) & SVf_IOK)
b326ddff 957#define SvIOK_on(sv) (assert_not_glob(sv) \
1f4fbd3b 958 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
25da4f38 959#define SvIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
0c34ef67 960#define SvIOK_only(sv) (SvOK_off(sv), \
1f4fbd3b 961 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
f7877b28 962#define SvIOK_only_UV(sv) (assert_not_glob(sv) SvOK_off_exc_UV(sv), \
1f4fbd3b 963 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
70401c6b 964
25da4f38 965#define SvIOK_UV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
1f4fbd3b 966 == (SVf_IOK|SVf_IVisUV))
28e5dec8 967#define SvUOK(sv) SvIOK_UV(sv)
25da4f38 968#define SvIOK_notUV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
1f4fbd3b 969 == SVf_IOK)
25da4f38 970
fff0089c
YO
971#define SvIandPOK(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_POK)) == (SVf_IOK|SVf_POK))
972#define SvIandPOK_on(sv) (assert_not_glob(sv) \
973 (SvFLAGS(sv) |= (SVf_IOK|SVp_IOK|SVf_POK|SVp_POK)))
974#define SvIandPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_POK|SVp_POK))
975
976#define SvBoolFlagsOK(sv) SvIandPOK(sv)
977
978#define BOOL_INTERNALS_sv_isbool(sv) (SvIsCOW_static(sv) && \
979 (SvPVX_const(sv) == PL_Yes || SvPVX_const(sv) == PL_No))
980#define BOOL_INTERNALS_sv_isbool_true(sv) (SvIsCOW_static(sv) && \
981 (SvPVX_const(sv) == PL_Yes))
982#define BOOL_INTERNALS_sv_isbool_false(sv) (SvIsCOW_static(sv) && \
983 (SvPVX_const(sv) == PL_No))
984
25da4f38
IZ
985#define SvIsUV(sv) (SvFLAGS(sv) & SVf_IVisUV)
986#define SvIsUV_on(sv) (SvFLAGS(sv) |= SVf_IVisUV)
987#define SvIsUV_off(sv) (SvFLAGS(sv) &= ~SVf_IVisUV)
79072805
LW
988
989#define SvNOK(sv) (SvFLAGS(sv) & SVf_NOK)
f7877b28 990#define SvNOK_on(sv) (assert_not_glob(sv) \
1f4fbd3b 991 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
8990e307 992#define SvNOK_off(sv) (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
0c34ef67 993#define SvNOK_only(sv) (SvOK_off(sv), \
1f4fbd3b 994 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
79072805 995
914184e1 996/*
fbf9f983 997=for apidoc Am|U32|SvUTF8|SV* sv
3c813ed0
KW
998Returns a U32 value indicating the UTF-8 status of an SV. If things are set-up
999properly, this indicates whether or not the SV contains UTF-8 encoded data.
de343f44 1000You should use this I<after> a call to C<L</SvPV>> or one of its variants, in
fd142383 1001case any call to string overloading updates the internal flag.
914184e1 1002
119bc988
KW
1003If you want to take into account the L<bytes> pragma, use C<L</DO_UTF8>>
1004instead.
1005
914184e1 1006=for apidoc Am|void|SvUTF8_on|SV *sv
1e54db1a 1007Turn on the UTF-8 status of an SV (the data is not changed, just the flag).
d5ce4a7c 1008Do not use frivolously.
914184e1
JH
1009
1010=for apidoc Am|void|SvUTF8_off|SV *sv
3c813ed0
KW
1011Unsets the UTF-8 status of an SV (the data is not changed, just the flag).
1012Do not use frivolously.
914184e1
JH
1013
1014=for apidoc Am|void|SvPOK_only_UTF8|SV* sv
796b6530 1015Tells an SV that it is a string and disables all other C<OK> bits,
1e54db1a 1016and leaves the UTF-8 status as it was.
f1a1024e 1017
914184e1
JH
1018=cut
1019 */
1020
7a5fd60d
NC
1021/* Ensure the return value of this macro does not clash with the GV_ADD* flags
1022in gv.h: */
a7cb1f99
GS
1023#define SvUTF8(sv) (SvFLAGS(sv) & SVf_UTF8)
1024#define SvUTF8_on(sv) (SvFLAGS(sv) |= (SVf_UTF8))
1025#define SvUTF8_off(sv) (SvFLAGS(sv) &= ~(SVf_UTF8))
5bc28da9 1026
79072805 1027#define SvPOK(sv) (SvFLAGS(sv) & SVf_POK)
f7877b28 1028#define SvPOK_on(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1f4fbd3b 1029 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
8990e307 1030#define SvPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
f7877b28 1031#define SvPOK_only(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1f4fbd3b
MS
1032 SvFLAGS(sv) &= ~(SVf_OK| \
1033 SVf_IVisUV|SVf_UTF8), \
1034 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
f7877b28 1035#define SvPOK_only_UTF8(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1f4fbd3b
MS
1036 SvFLAGS(sv) &= ~(SVf_OK| \
1037 SVf_IVisUV), \
1038 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
79072805 1039
4f2da183 1040#define SvVOK(sv) (SvMAGICAL(sv) \
1f4fbd3b 1041 && mg_find(sv,PERL_MAGIC_vstring))
c4d5dfab
KW
1042/*
1043=for apidoc Am|MAGIC*|SvVSTRING_mg|SV * sv
1044
1045Returns the vstring magic, or NULL if none
1046
1047=cut
1048*/
b0a11fe1 1049#define SvVSTRING_mg(sv) (SvMAGICAL(sv) \
1f4fbd3b 1050 ? mg_find(sv,PERL_MAGIC_vstring) : NULL)
b0a11fe1 1051
79072805 1052#define SvOOK(sv) (SvFLAGS(sv) & SVf_OOK)
a4f658f7 1053#define SvOOK_on(sv) (SvFLAGS(sv) |= SVf_OOK)
13cc62f5
KW
1054
1055
1056/*
1057=for apidoc Am|void|SvOOK_off|SV * sv
1058
1059Remove any string offset.
1060
1061=cut
1062*/
1063
fa7a1e49 1064#define SvOOK_off(sv) ((void)(SvOOK(sv) && (sv_backoff(sv),0)))
79072805 1065
a0d0e21e
LW
1066#define SvFAKE(sv) (SvFLAGS(sv) & SVf_FAKE)
1067#define SvFAKE_on(sv) (SvFLAGS(sv) |= SVf_FAKE)
1068#define SvFAKE_off(sv) (SvFLAGS(sv) &= ~SVf_FAKE)
1069
ed6116ce 1070#define SvROK(sv) (SvFLAGS(sv) & SVf_ROK)
a0d0e21e 1071#define SvROK_on(sv) (SvFLAGS(sv) |= SVf_ROK)
dd2eae66 1072#define SvROK_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK))
79072805 1073
8990e307
LW
1074#define SvMAGICAL(sv) (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
1075#define SvMAGICAL_on(sv) (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
1076#define SvMAGICAL_off(sv) (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
79072805 1077
8990e307
LW
1078#define SvGMAGICAL(sv) (SvFLAGS(sv) & SVs_GMG)
1079#define SvGMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_GMG)
1080#define SvGMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_GMG)
ed6116ce 1081
8990e307
LW
1082#define SvSMAGICAL(sv) (SvFLAGS(sv) & SVs_SMG)
1083#define SvSMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_SMG)
1084#define SvSMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_SMG)
ed6116ce 1085
8990e307
LW
1086#define SvRMAGICAL(sv) (SvFLAGS(sv) & SVs_RMG)
1087#define SvRMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_RMG)
1088#define SvRMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_RMG)
ed6116ce 1089
acd60c2b
KW
1090/*
1091=for apidoc Am|bool|SvAMAGIC|SV * sv
1092
1093Returns a boolean as to whether C<sv> has overloading (active magic) enabled or
1094not.
1095
1096=cut
1097*/
1098
a1cd65be 1099#define SvAMAGIC(sv) (SvROK(sv) && SvOBJECT(SvRV(sv)) && \
1f4fbd3b 1100 HvAMAGIC(SvSTASH(SvRV(sv))))
a0d0e21e 1101
0adc25b0
FC
1102/* To be used on the stashes themselves: */
1103#define HvAMAGIC(hv) (SvFLAGS(hv) & SVf_AMAGIC)
1104#define HvAMAGIC_on(hv) (SvFLAGS(hv) |= SVf_AMAGIC)
1105#define HvAMAGIC_off(hv) (SvFLAGS(hv) &=~ SVf_AMAGIC)
1106
4bac9ae4 1107
1078c6a2 1108/* "nog" means "doesn't have get magic" */
4bac9ae4
CS
1109#define SvPOK_nog(sv) ((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK)
1110#define SvIOK_nog(sv) ((SvFLAGS(sv) & (SVf_IOK|SVs_GMG)) == SVf_IOK)
1111#define SvUOK_nog(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
1112#define SvNOK_nog(sv) ((SvFLAGS(sv) & (SVf_NOK|SVs_GMG)) == SVf_NOK)
1113#define SvNIOK_nog(sv) (SvNIOK(sv) && !(SvFLAGS(sv) & SVs_GMG))
1114
1115#define SvPOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1116#define SvIOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_THINKFIRST|SVs_GMG)) == SVf_IOK)
1117#define SvUOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVf_THINKFIRST|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
1118#define SvNOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_NOK|SVf_THINKFIRST|SVs_GMG)) == SVf_NOK)
1119#define SvNIOK_nogthink(sv) (SvNIOK(sv) && !(SvFLAGS(sv) & (SVf_THINKFIRST|SVs_GMG)))
1120
1121#define SvPOK_utf8_nog(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1122#define SvPOK_utf8_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1123
1124#define SvPOK_byte_nog(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == SVf_POK)
1125#define SvPOK_byte_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1126
8bb025ae
CS
1127#define SvPOK_pure_nogthink(sv) \
1128 ((SvFLAGS(sv) & (SVf_POK|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1129#define SvPOK_utf8_pure_nogthink(sv) \
1130 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1131#define SvPOK_byte_pure_nogthink(sv) \
1132 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1133
d34786ba 1134/*
62a72503 1135=for apidoc Am|bool|SvIsBOOL|SV* sv
1d0d673f
PE
1136
1137Returns true if the SV is one of the special boolean constants (PL_sv_yes or
1138PL_sv_no), or is a regular SV whose last assignment stored a copy of one.
1139
1140=cut
1141*/
1142
1143#define SvIsBOOL(sv) Perl_sv_isbool(aTHX_ sv)
1144
1145/*
458b44e7 1146=for apidoc Am|U32|SvGAMAGIC|SV* sv
d34786ba 1147
8554e7a0
FC
1148Returns true if the SV has get magic or
1149overloading. If either is true then
d34786ba 1150the scalar is active data, and has the potential to return a new value every
8554e7a0
FC
1151time it is accessed. Hence you must be careful to
1152only read it once per user logical operation and work
1153with that returned value. If neither is true then
d34786ba
NC
1154the scalar's value cannot change unless written to.
1155
1156=cut
1157*/
1158
dd2eae66 1159#define SvGAMAGIC(sv) (SvGMAGICAL(sv) || SvAMAGIC(sv))
1426bbf4 1160
9299023c 1161#define Gv_AMG(stash) \
1f4fbd3b
MS
1162 (HvNAME(stash) && Gv_AMupdate(stash,FALSE) \
1163 ? 1 \
1164 : (HvAMAGIC_off(stash), 0))
a0d0e21e 1165
810b8aa5 1166#define SvWEAKREF(sv) ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
1f4fbd3b 1167 == (SVf_ROK|SVprv_WEAKREF))
810b8aa5
GS
1168#define SvWEAKREF_on(sv) (SvFLAGS(sv) |= (SVf_ROK|SVprv_WEAKREF))
1169#define SvWEAKREF_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
1170
1ccdb730 1171#define SvPCS_IMPORTED(sv) ((SvFLAGS(sv) & (SVf_ROK|SVprv_PCS_IMPORTED)) \
1f4fbd3b 1172 == (SVf_ROK|SVprv_PCS_IMPORTED))
1ccdb730
NC
1173#define SvPCS_IMPORTED_on(sv) (SvFLAGS(sv) |= (SVf_ROK|SVprv_PCS_IMPORTED))
1174#define SvPCS_IMPORTED_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_PCS_IMPORTED))
1175
a11eaecb
FC
1176/*
1177=for apidoc m|U32|SvTHINKFIRST|SV *sv
1178
796b6530
KW
1179A quick flag check to see whether an C<sv> should be passed to C<sv_force_normal>
1180to be "downgraded" before C<SvIVX> or C<SvPVX> can be modified directly.
a11eaecb 1181
796b6530
KW
1182For example, if your scalar is a reference and you want to modify the C<SvIVX>
1183slot, you can't just do C<SvROK_off>, as that will leak the referent.
a11eaecb
FC
1184
1185This is used internally by various sv-modifying functions, such as
ef058b33 1186C<sv_setsv>, C<sv_setiv> and C<sv_pvn_force>.
a11eaecb
FC
1187
1188One case that this does not handle is a gv without SvFAKE set. After
1189
1190 if (SvTHINKFIRST(gv)) sv_force_normal(gv);
1191
1192it will still be a gv.
1193
796b6530
KW
1194C<SvTHINKFIRST> sometimes produces false positives. In those cases
1195C<sv_force_normal> does nothing.
a11eaecb
FC
1196
1197=cut
1198*/
1199
a0d0e21e 1200#define SvTHINKFIRST(sv) (SvFLAGS(sv) & SVf_THINKFIRST)
ed6116ce 1201
c0683843
FC
1202#define SVs_PADMY 0
1203#define SvPADMY(sv) !(SvFLAGS(sv) & SVs_PADTMP)
145bf8ee
FC
1204#ifndef PERL_CORE
1205# define SvPADMY_on(sv) SvPADTMP_off(sv)
1206#endif
d9d18af6 1207
c0683843
FC
1208#define SvPADTMP(sv) (SvFLAGS(sv) & (SVs_PADTMP))
1209#define SvPADSTALE(sv) (SvFLAGS(sv) & (SVs_PADSTALE))
62bb6514 1210
145bf8ee
FC
1211#define SvPADTMP_on(sv) (SvFLAGS(sv) |= SVs_PADTMP)
1212#define SvPADTMP_off(sv) (SvFLAGS(sv) &= ~SVs_PADTMP)
c9182d9c
KW
1213#define SvPADSTALE_on(sv) Perl_SvPADSTALE_on(MUTABLE_SV(sv))
1214#define SvPADSTALE_off(sv) Perl_SvPADSTALE_off(MUTABLE_SV(sv))
ed6116ce 1215
8990e307
LW
1216#define SvTEMP(sv) (SvFLAGS(sv) & SVs_TEMP)
1217#define SvTEMP_on(sv) (SvFLAGS(sv) |= SVs_TEMP)
1218#define SvTEMP_off(sv) (SvFLAGS(sv) &= ~SVs_TEMP)
79072805 1219
8990e307
LW
1220#define SvOBJECT(sv) (SvFLAGS(sv) & SVs_OBJECT)
1221#define SvOBJECT_on(sv) (SvFLAGS(sv) |= SVs_OBJECT)
1222#define SvOBJECT_off(sv) (SvFLAGS(sv) &= ~SVs_OBJECT)
79072805 1223
cb198164
YO
1224/*
1225=for apidoc Am|U32|SvREADONLY|SV* sv
1226Returns true if the argument is readonly, otherwise returns false.
a3815e44 1227Exposed to perl code via Internals::SvREADONLY().
cb198164
YO
1228
1229=for apidoc Am|U32|SvREADONLY_on|SV* sv
1230Mark an object as readonly. Exactly what this means depends on the object
1231type. Exposed to perl code via Internals::SvREADONLY().
1232
1233=for apidoc Am|U32|SvREADONLY_off|SV* sv
1234Mark an object as not-readonly. Exactly what this mean depends on the
1235object type. Exposed to perl code via Internals::SvREADONLY().
1236
1237=cut
1238*/
1239
a623f893
FC
1240#define SvREADONLY(sv) (SvFLAGS(sv) & (SVf_READONLY|SVf_PROTECT))
1241#ifdef PERL_CORE
1242# define SvREADONLY_on(sv) (SvFLAGS(sv) |= (SVf_READONLY|SVf_PROTECT))
1243# define SvREADONLY_off(sv) (SvFLAGS(sv) &=~(SVf_READONLY|SVf_PROTECT))
1244#else
1245# define SvREADONLY_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
1246# define SvREADONLY_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
1247#endif
79072805 1248
180488f8 1249#define SvSCREAM(sv) ((SvFLAGS(sv) & (SVp_SCREAM|SVp_POK)) == (SVp_SCREAM|SVp_POK))
8990e307
LW
1250#define SvSCREAM_on(sv) (SvFLAGS(sv) |= SVp_SCREAM)
1251#define SvSCREAM_off(sv) (SvFLAGS(sv) &= ~SVp_SCREAM)
79072805 1252
74e0ddf7
NC
1253#ifndef PERL_CORE
1254# define SvCOMPILED(sv) 0
1255# define SvCOMPILED_on(sv)
1256# define SvCOMPILED_off(sv)
1257#endif
79072805 1258
85ffbbb0 1259
041c1a23 1260#if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
e9726144 1261# define SvTAIL(sv) ({ const SV *const _svtail = (const SV *)(sv); \
1f4fbd3b
MS
1262 assert(SvTYPE(_svtail) != SVt_PVAV); \
1263 assert(SvTYPE(_svtail) != SVt_PVHV); \
1264 assert(!(SvFLAGS(_svtail) & (SVf_NOK|SVp_NOK))); \
1265 assert(SvVALID(_svtail)); \
b4204fb6 1266 ((XPVNV*)SvANY(_svtail))->xnv_u.xnv_bm_tail; \
1f4fbd3b 1267 })
85efc3a5 1268#else
b4204fb6 1269# define SvTAIL(_svtail) (((XPVNV*)SvANY(_svtail))->xnv_u.xnv_bm_tail)
85efc3a5 1270#endif
8990e307 1271
a5c7cb08
DM
1272/* Does the SV have a Boyer-Moore table attached as magic?
1273 * 'VALID' is a poor name, but is kept for historical reasons. */
1274#define SvVALID(_svvalid) ( \
4e8879f3
DM
1275 SvPOKp(_svvalid) \
1276 && SvSMAGICAL(_svvalid) \
a5c7cb08
DM
1277 && SvMAGIC(_svvalid) \
1278 && (SvMAGIC(_svvalid)->mg_type == PERL_MAGIC_bm \
1279 || mg_find(_svvalid, PERL_MAGIC_bm)) \
1280 )
1281
ed6116ce
LW
1282#define SvRVx(sv) SvRV(sv)
1283
f599b64b 1284#ifdef PERL_DEBUG_COW
8ed30cc1
DD
1285/* Need -0.0 for SvNVX to preserve IEEE FP "negative zero" because
1286 +0.0 + -0.0 => +0.0 but -0.0 + -0.0 => -0.0 */
771ba71a
NC
1287# define SvIVX(sv) (0 + ((XPVIV*) SvANY(sv))->xiv_iv)
1288# define SvUVX(sv) (0 + ((XPVUV*) SvANY(sv))->xuv_uv)
58430790 1289# define SvNVX(sv) (-0.0 + ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv)
ac09da3b 1290# define SvRV(sv) (0 + (sv)->sv_u.svu_rv)
f19a12a3 1291# define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
c0e1089a 1292/* Don't test the core XS code yet. */
771ba71a 1293# if defined (PERL_CORE) && PERL_DEBUG_COW > 1
c7724415 1294# define SvPVX(sv) (0 + (assert_(!SvREADONLY(sv)) (sv)->sv_u.svu_pv))
771ba71a
NC
1295# else
1296# define SvPVX(sv) SvPVX_mutable(sv)
1297# endif
771ba71a
NC
1298# define SvCUR(sv) (0 + ((XPV*) SvANY(sv))->xpv_cur)
1299# define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
1300# define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1301
c7724415
FC
1302# define SvMAGIC(sv) (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*) SvANY(sv))->xmg_u.xmg_magic))
1303# define SvSTASH(sv) (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*) SvANY(sv))->xmg_stash))
f3476b0f 1304#else /* Below is not PERL_DEBUG_COW */
2324bdb9
DIM
1305# ifdef PERL_CORE
1306# define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
1307# else
771ba71a 1308# define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
2324bdb9 1309# endif
771ba71a
NC
1310# define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1311
041c1a23 1312# if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
05ef5f8f 1313/* These get expanded inside other macros that already use a variable _sv */
f7877b28 1314# define SvPVX(sv) \
1f4fbd3b
MS
1315 (*({ SV *const _svpvx = MUTABLE_SV(sv); \
1316 assert(PL_valid_types_PVX[SvTYPE(_svpvx) & SVt_MASK]); \
1317 assert(!isGV_with_GP(_svpvx)); \
1318 assert(!(SvTYPE(_svpvx) == SVt_PVIO \
1319 && !(IoFLAGS(_svpvx) & IOf_FAKE_DIRP))); \
1320 &((_svpvx)->sv_u.svu_pv); \
1321 }))
2324bdb9
DIM
1322# ifdef PERL_CORE
1323# define SvCUR(sv) \
1f4fbd3b
MS
1324 ({ const SV *const _svcur = (const SV *)(sv); \
1325 assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]); \
1326 assert(!isGV_with_GP(_svcur)); \
1327 assert(!(SvTYPE(_svcur) == SVt_PVIO \
1328 && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP))); \
1329 (((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur); \
1330 })
2324bdb9 1331# else
08002bbf 1332# define SvCUR(sv) \
1f4fbd3b
MS
1333 (*({ const SV *const _svcur = (const SV *)(sv); \
1334 assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]); \
1335 assert(!isGV_with_GP(_svcur)); \
1336 assert(!(SvTYPE(_svcur) == SVt_PVIO \
1337 && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP))); \
1338 &(((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur); \
1339 }))
2324bdb9 1340# endif
05ef5f8f 1341# define SvIVX(sv) \
1f4fbd3b
MS
1342 (*({ const SV *const _svivx = (const SV *)(sv); \
1343 assert(PL_valid_types_IVX[SvTYPE(_svivx) & SVt_MASK]); \
1344 assert(!isGV_with_GP(_svivx)); \
1345 &(((XPVIV*) MUTABLE_PTR(SvANY(_svivx)))->xiv_iv); \
1346 }))
05ef5f8f 1347# define SvUVX(sv) \
1f4fbd3b
MS
1348 (*({ const SV *const _svuvx = (const SV *)(sv); \
1349 assert(PL_valid_types_IVX[SvTYPE(_svuvx) & SVt_MASK]); \
1350 assert(!isGV_with_GP(_svuvx)); \
1351 &(((XPVUV*) MUTABLE_PTR(SvANY(_svuvx)))->xuv_uv); \
1352 }))
05ef5f8f 1353# define SvNVX(sv) \
1f4fbd3b
MS
1354 (*({ const SV *const _svnvx = (const SV *)(sv); \
1355 assert(PL_valid_types_NVX[SvTYPE(_svnvx) & SVt_MASK]); \
1356 assert(!isGV_with_GP(_svnvx)); \
1357 &(((XPVNV*) MUTABLE_PTR(SvANY(_svnvx)))->xnv_u.xnv_nv); \
1358 }))
ac09da3b 1359# define SvRV(sv) \
1f4fbd3b
MS
1360 (*({ SV *const _svrv = MUTABLE_SV(sv); \
1361 assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]); \
1362 assert(!isGV_with_GP(_svrv)); \
1363 assert(!(SvTYPE(_svrv) == SVt_PVIO \
1364 && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP))); \
1365 &((_svrv)->sv_u.svu_rv); \
1366 }))
f19a12a3 1367# define SvRV_const(sv) \
1f4fbd3b
MS
1368 ({ const SV *const _svrv = (const SV *)(sv); \
1369 assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]); \
1370 assert(!isGV_with_GP(_svrv)); \
1371 assert(!(SvTYPE(_svrv) == SVt_PVIO \
1372 && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP))); \
1373 (_svrv)->sv_u.svu_rv; \
1374 })
05ef5f8f 1375# define SvMAGIC(sv) \
1f4fbd3b
MS
1376 (*({ const SV *const _svmagic = (const SV *)(sv); \
1377 assert(SvTYPE(_svmagic) >= SVt_PVMG); \
1378 &(((XPVMG*) MUTABLE_PTR(SvANY(_svmagic)))->xmg_u.xmg_magic); \
1379 }))
05ef5f8f 1380# define SvSTASH(sv) \
1f4fbd3b
MS
1381 (*({ const SV *const _svstash = (const SV *)(sv); \
1382 assert(SvTYPE(_svstash) >= SVt_PVMG); \
1383 &(((XPVMG*) MUTABLE_PTR(SvANY(_svstash)))->xmg_stash); \
1384 }))
f3476b0f 1385# else /* Below is not DEBUGGING or can't use brace groups */
08002bbf
NC
1386# define SvPVX(sv) ((sv)->sv_u.svu_pv)
1387# define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
05ef5f8f
NC
1388# define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
1389# define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
58430790 1390# define SvNVX(sv) ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv
ac09da3b 1391# define SvRV(sv) ((sv)->sv_u.svu_rv)
f19a12a3 1392# define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
e736a858 1393# define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_u.xmg_magic
771ba71a 1394# define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
03687789 1395# endif
62703e72
NC
1396#endif
1397
06c0cc96 1398#ifndef PERL_POISON
bb496845 1399/* Given that these two are new, there can't be any existing code using them
3968dd31 1400 * as LVALUEs, so prevent that from happening */
a8b89476
TC
1401# define SvPVX_mutable(sv) ((char *)((sv)->sv_u.svu_pv))
1402# define SvPVX_const(sv) ((const char*)((sv)->sv_u.svu_pv))
06c0cc96
NC
1403#else
1404/* Except for the poison code, which uses & to scribble over the pointer after
1405 free() is called. */
1406# define SvPVX_mutable(sv) ((sv)->sv_u.svu_pv)
1407# define SvPVX_const(sv) ((const char*)((sv)->sv_u.svu_pv))
1408#endif
bb496845 1409
62703e72
NC
1410#define SvIVXx(sv) SvIVX(sv)
1411#define SvUVXx(sv) SvUVX(sv)
1412#define SvNVXx(sv) SvNVX(sv)
1413#define SvPVXx(sv) SvPVX(sv)
1414#define SvLENx(sv) SvLEN(sv)
1415#define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
1416
1417
28e5dec8
JH
1418/* Ask a scalar nicely to try to become an IV, if possible.
1419 Not guaranteed to stay returning void */
1420/* Macro won't actually call sv_2iv if already IOK */
1421#define SvIV_please(sv) \
51c4bdf3
KW
1422 STMT_START { \
1423 SV * sv_ = MUTABLE_SV(sv); \
1424 if (!SvIOKp(sv_) && (SvFLAGS(sv_) & (SVf_NOK|SVf_POK))) \
1425 (void) SvIV(sv_); \
1426 } STMT_END
6f1401dc 1427#define SvIV_please_nomg(sv) \
1f4fbd3b
MS
1428 (!(SvFLAGS(sv) & (SVf_IOK|SVp_IOK)) && (SvFLAGS(sv) & (SVf_NOK|SVf_POK)) \
1429 ? (sv_2iv_flags(sv, 0), SvIOK(sv)) \
1430 : SvIOK(sv))
8c981be1 1431
79072805 1432#define SvIV_set(sv, val) \
1f4fbd3b 1433 STMT_START { \
8c981be1
KW
1434 SV * sv_ = MUTABLE_SV(sv); \
1435 assert(PL_valid_types_IV_set[SvTYPE(sv_) & SVt_MASK]); \
1436 assert(!isGV_with_GP(sv_)); \
1437 (((XPVIV*) SvANY(sv_))->xiv_iv = (val)); \
1438 } STMT_END
1439
79072805 1440#define SvNV_set(sv, val) \
1f4fbd3b 1441 STMT_START { \
8c981be1
KW
1442 SV * sv_ = MUTABLE_SV(sv); \
1443 assert(PL_valid_types_NV_set[SvTYPE(sv_) & SVt_MASK]); \
1444 assert(!isGV_with_GP(sv_)); \
1445 (((XPVNV*)SvANY(sv_))->xnv_u.xnv_nv = (val)); \
1446 } STMT_END
1447
79072805 1448#define SvPV_set(sv, val) \
1f4fbd3b 1449 STMT_START { \
8c981be1
KW
1450 SV * sv_ = MUTABLE_SV(sv); \
1451 assert(PL_valid_types_PVX[SvTYPE(sv_) & SVt_MASK]); \
1452 assert(!isGV_with_GP(sv_)); \
1453 assert(!(SvTYPE(sv_) == SVt_PVIO \
1454 && !(IoFLAGS(sv_) & IOf_FAKE_DIRP))); \
1455 ((sv_)->sv_u.svu_pv = (val)); \
1456 } STMT_END
1457
607fa7f2 1458#define SvUV_set(sv, val) \
1f4fbd3b 1459 STMT_START { \
8c981be1
KW
1460 SV * sv_ = MUTABLE_SV(sv); \
1461 assert(PL_valid_types_IV_set[SvTYPE(sv_) & SVt_MASK]); \
1462 assert(!isGV_with_GP(sv_)); \
1463 (((XPVUV*)SvANY(sv_))->xuv_uv = (val)); \
1464 } STMT_END
1465
b162af07 1466#define SvRV_set(sv, val) \
f1fb8741 1467 STMT_START { \
8c981be1
KW
1468 SV * sv_ = MUTABLE_SV(sv); \
1469 assert(PL_valid_types_RV[SvTYPE(sv_) & SVt_MASK]); \
1470 assert(!isGV_with_GP(sv_)); \
1471 assert(!(SvTYPE(sv_) == SVt_PVIO \
1472 && !(IoFLAGS(sv_) & IOf_FAKE_DIRP))); \
1473 ((sv_)->sv_u.svu_rv = (val)); \
1474 } STMT_END
b162af07
SP
1475#define SvMAGIC_set(sv, val) \
1476 STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
e736a858 1477 (((XPVMG*)SvANY(sv))->xmg_u.xmg_magic = (val)); } STMT_END
b162af07
SP
1478#define SvSTASH_set(sv, val) \
1479 STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1480 (((XPVMG*) SvANY(sv))->xmg_stash = (val)); } STMT_END
79072805 1481#define SvCUR_set(sv, val) \
1f4fbd3b
MS
1482 STMT_START { \
1483 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]); \
1484 assert(!isGV_with_GP(sv)); \
1485 assert(!(SvTYPE(sv) == SVt_PVIO \
1486 && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1487 (((XPV*) SvANY(sv))->xpv_cur = (val)); } STMT_END
79072805 1488#define SvLEN_set(sv, val) \
1f4fbd3b
MS
1489 STMT_START { \
1490 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]); \
1491 assert(!isGV_with_GP(sv)); \
1492 assert(!(SvTYPE(sv) == SVt_PVIO \
1493 && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1494 (((XPV*) SvANY(sv))->xpv_len = (val)); } STMT_END
7ae8ee9e 1495#define SvEND_set(sv, val) \
1f4fbd3b
MS
1496 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
1497 SvCUR_set(sv, (val) - SvPVX(sv)); } STMT_END
79072805 1498
cd188aff
KW
1499/*
1500=for apidoc Am|void|SvPV_renew|SV* sv|STRLEN len
1501Low level micro optimization of C<L</SvGROW>>. It is generally better to use
1502C<SvGROW> instead. This is because C<SvPV_renew> ignores potential issues that
1503C<SvGROW> handles. C<sv> needs to have a real C<PV> that is unencombered by
cbcd1db3
KW
1504things like COW. Using C<SV_CHECK_THINKFIRST> or
1505C<SV_CHECK_THINKFIRST_COW_DROP> before calling this should clean it up, but
cd188aff
KW
1506why not just use C<SvGROW> if you're not sure about the provenance?
1507
1508=cut
1509*/
b7e9a5c2 1510#define SvPV_renew(sv,n) \
1f4fbd3b
MS
1511 STMT_START { SvLEN_set(sv, n); \
1512 SvPV_set((sv), (MEM_WRAP_CHECK_(n,char) \
1513 (char*)saferealloc((Malloc_t)SvPVX(sv), \
1514 (MEM_SIZE)((n))))); \
1515 } STMT_END
e76506c9
KW
1516/*
1517=for apidoc Am|void|SvPV_shrink_to_cur|SV* sv
1518
1519Trim any trailing unused memory in the PV of C<sv>, which needs to have a real
1520C<PV> that is unencombered by things like COW. Think first before using this
1521functionality. Is the space saving really worth giving up COW? Will the
1522needed size of C<sv> stay the same?
1523
af46018b
KW
1524If the answers are both yes, then use L</C<SV_CHECK_THINKFIRST>> or
1525L</C<SV_CHECK_THINKFIRST_COW_DROP>> before calling this.
e76506c9
KW
1526
1527=cut
1528*/
1da4ca5f
NC
1529
1530#define SvPV_shrink_to_cur(sv) STMT_START { \
1f4fbd3b
MS
1531 const STRLEN _lEnGtH = SvCUR(sv) + 1; \
1532 SvPV_renew(sv, _lEnGtH); \
1533 } STMT_END
b7e9a5c2 1534
d54acd4f
KW
1535/*
1536=for apidoc Am|void|SvPV_free|SV * sv
1537
1538Frees the PV buffer in C<sv>, leaving things in a precarious state, so should
1539only be used as part of a larger operation
1540
1541=cut
1542*/
771ba71a
NC
1543#define SvPV_free(sv) \
1544 STMT_START { \
1f4fbd3b
MS
1545 assert(SvTYPE(sv) >= SVt_PV); \
1546 if (SvLEN(sv)) { \
1547 assert(!SvROK(sv)); \
1548 if(UNLIKELY(SvOOK(sv))) { \
1549 STRLEN zok; \
1550 SvOOK_offset(sv, zok); \
1551 SvPV_set(sv, SvPVX_mutable(sv) - zok); \
1552 SvFLAGS(sv) &= ~SVf_OOK; \
1553 } \
1554 Safefree(SvPVX(sv)); \
1555 } \
1556 } STMT_END
8bd4d4c5 1557
43230e26
NC
1558#ifdef PERL_CORE
1559/* Code that crops up in three places to take a scalar and ready it to hold
1560 a reference */
1561# define prepare_SV_for_RV(sv) \
1562 STMT_START { \
1f4fbd3b
MS
1563 if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV) \
1564 sv_upgrade(sv, SVt_IV); \
1565 else if (SvTYPE(sv) >= SVt_PV) { \
1566 SvPV_free(sv); \
1567 SvLEN_set(sv, 0); \
43230e26 1568 SvCUR_set(sv, 0); \
1f4fbd3b
MS
1569 } \
1570 } STMT_END
43230e26 1571#endif
373b357f 1572
cffe132d
NC
1573#ifndef PERL_CORE
1574# define BmFLAGS(sv) (SvTAIL(sv) ? FBMcf_TAIL : 0)
1575#endif
6976cee3 1576
041c1a23 1577#if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
60c8298a 1578# define BmUSEFUL(sv) \
1f4fbd3b
MS
1579 (*({ SV *const _bmuseful = MUTABLE_SV(sv); \
1580 assert(SvTYPE(_bmuseful) >= SVt_PVIV); \
1581 assert(SvVALID(_bmuseful)); \
1582 assert(!SvIOK(_bmuseful)); \
1583 &(((XPVIV*) SvANY(_bmuseful))->xiv_u.xivu_iv); \
1584 }))
60c8298a 1585#else
50c7be83 1586# define BmUSEFUL(sv) ((XPVIV*) SvANY(sv))->xiv_u.xivu_iv
b4f6c04b 1587
60c8298a 1588#endif
79072805 1589
8922e438
FC
1590#ifndef PERL_CORE
1591# define BmRARE(sv) 0
1592# define BmPREVIOUS(sv) 0
1593#endif
1594
f2da823f 1595#define FmLINES(sv) ((XPVIV*) SvANY(sv))->xiv_iv
79072805
LW
1596
1597#define LvTYPE(sv) ((XPVLV*) SvANY(sv))->xlv_type
1598#define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
1599#define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
bbfdc870 1600#define LvSTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff_u.xlvu_stargoff
79072805 1601#define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
83f78d1a 1602#define LvFLAGS(sv) ((XPVLV*) SvANY(sv))->xlv_flags
79072805 1603
b063b0a8
DIM
1604#define LVf_NEG_OFF 0x1
1605#define LVf_NEG_LEN 0x2
1606#define LVf_OUT_OF_RANGE 0x4
1607
6f7e8353 1608#define IoIFP(sv) (sv)->sv_u.svu_fp
8990e307
LW
1609#define IoOFP(sv) ((XPVIO*) SvANY(sv))->xio_ofp
1610#define IoDIRP(sv) ((XPVIO*) SvANY(sv))->xio_dirp
4755096e 1611#define IoANY(sv) ((XPVIO*) SvANY(sv))->xio_any
c490b21c 1612#define IoLINES(sv) ((XPVIO*) SvANY(sv))->xiv_u.xivu_iv
8990e307
LW
1613#define IoPAGE(sv) ((XPVIO*) SvANY(sv))->xio_page
1614#define IoPAGE_LEN(sv) ((XPVIO*) SvANY(sv))->xio_page_len
1615#define IoLINES_LEFT(sv)((XPVIO*) SvANY(sv))->xio_lines_left
1616#define IoTOP_NAME(sv) ((XPVIO*) SvANY(sv))->xio_top_name
1617#define IoTOP_GV(sv) ((XPVIO*) SvANY(sv))->xio_top_gv
1618#define IoFMT_NAME(sv) ((XPVIO*) SvANY(sv))->xio_fmt_name
1619#define IoFMT_GV(sv) ((XPVIO*) SvANY(sv))->xio_fmt_gv
1620#define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
1621#define IoBOTTOM_GV(sv) ((XPVIO*) SvANY(sv))->xio_bottom_gv
8990e307
LW
1622#define IoTYPE(sv) ((XPVIO*) SvANY(sv))->xio_type
1623#define IoFLAGS(sv) ((XPVIO*) SvANY(sv))->xio_flags
1624
50952442 1625/* IoTYPE(sv) is a single character telling the type of I/O connection. */
3b6c1aba
JH
1626#define IoTYPE_RDONLY '<'
1627#define IoTYPE_WRONLY '>'
1628#define IoTYPE_RDWR '+'
1629#define IoTYPE_APPEND 'a'
1630#define IoTYPE_PIPE '|'
1631#define IoTYPE_STD '-' /* stdin or stdout */
1632#define IoTYPE_SOCKET 's'
1633#define IoTYPE_CLOSED ' '
1634#define IoTYPE_IMPLICIT 'I' /* stdin or stdout or stderr */
1635#define IoTYPE_NUMERIC '#' /* fdopen */
03fcf2fc
GS
1636
1637/*
d8e799d8 1638=for apidoc_section $tainting
954c1994 1639=for apidoc Am|bool|SvTAINTED|SV* sv
8554e7a0 1640Checks to see if an SV is tainted. Returns TRUE if it is, FALSE if
954c1994
GS
1641not.
1642
1643=for apidoc Am|void|SvTAINTED_on|SV* sv
c55831ac 1644Marks an SV as tainted if tainting is enabled.
954c1994
GS
1645
1646=for apidoc Am|void|SvTAINTED_off|SV* sv
8554e7a0
FC
1647Untaints an SV. Be I<very> careful with this routine, as it short-circuits
1648some of Perl's fundamental security features. XS module authors should not
954c1994 1649use this function unless they fully understand all the implications of
72d33970 1650unconditionally untainting the value. Untainting should be done in the
954c1994
GS
1651standard perl fashion, via a carefully crafted regexp, rather than directly
1652untainting variables.
1653
1654=for apidoc Am|void|SvTAINT|SV* sv
c98cdb04
FC
1655Taints an SV if tainting is enabled, and if some input to the current
1656expression is tainted--usually a variable, but possibly also implicit
1657inputs such as locale settings. C<SvTAINT> propagates that taintedness to
1658the outputs of an expression in a pessimistic fashion; i.e., without paying
1659attention to precisely which outputs are influenced by which inputs.
954c1994
GS
1660
1661=cut
1662*/
1663
a0714e2c 1664#define sv_taint(sv) sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)
aae9cea0 1665
dc6d7f5c 1666#ifdef NO_TAINT_SUPPORT
284167a5
S
1667# define SvTAINTED(sv) 0
1668#else
1669# define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv))
1670#endif
5d9574c1
DM
1671#define SvTAINTED_on(sv) STMT_START{ if(UNLIKELY(TAINTING_get)){sv_taint(sv);} }STMT_END
1672#define SvTAINTED_off(sv) STMT_START{ if(UNLIKELY(TAINTING_get)){sv_untaint(sv);} }STMT_END
bbce6d69 1673
c6ee37c5
MB
1674#define SvTAINT(sv) \
1675 STMT_START { \
d48c660d
DM
1676 assert(TAINTING_get || !TAINT_get); \
1677 if (UNLIKELY(TAINT_get)) \
1678 SvTAINTED_on(sv); \
c6ee37c5 1679 } STMT_END
79072805 1680
954c1994 1681/*
d8e799d8 1682=for apidoc_section $SV
1607e393
KW
1683=for apidoc Am|char*|SvPV_force |SV* sv|STRLEN len
1684=for apidoc_item ||SvPV_force_flags |SV * sv|STRLEN len|U32 flags
1685=for apidoc_item ||SvPV_force_flags_mutable|SV * sv|STRLEN len|U32 flags
1686=for apidoc_item ||SvPV_force_flags_nolen |SV * sv |U32 flags
1687=for apidoc_item ||SvPV_force_mutable |SV * sv|STRLEN len
1688=for apidoc_item ||SvPV_force_nolen |SV* sv
1689=for apidoc_item ||SvPV_force_nomg |SV* sv|STRLEN len
1690=for apidoc_item ||SvPV_force_nomg_nolen |SV * sv
1691=for apidoc_item ||SvPVbyte_force |SV * sv|STRLEN len
1692=for apidoc_item ||SvPVbytex_force |SV * sv|STRLEN len
1693=for apidoc_item ||SvPVutf8_force |SV * sv|STRLEN len
1694=for apidoc_item ||SvPVutf8x_force |SV * sv|STRLEN len
1695=for apidoc_item ||SvPVx_force |SV* sv|STRLEN len
26d1d7c7 1696
e97ba882
KW
1697These are like C<L</SvPV>>, returning the string in the SV, but will force the
1698SV into containing a string (C<L</SvPOK>>), and only a string
1699(C<L</SvPOK_only>>), by hook or by crook. You need to use one of these
1700C<force> routines if you are going to update the C<L</SvPVX>> directly.
954c1994 1701
fd142383 1702Note that coercing an arbitrary scalar into a plain PV will potentially
72d33970 1703strip useful data from it. For example if the SV was C<SvROK>, then the
fd142383
DM
1704referent will have its reference count decremented, and the SV itself may
1705be converted to an C<SvPOK> scalar with a string buffer containing a value
1706such as C<"ARRAY(0x1234)">.
1707
26d1d7c7
KW
1708The differences between the forms are:
1709
1710The forms with C<flags> in their names allow you to use the C<flags> parameter
1711to specify to perform 'get' magic (by setting the C<SV_GMAGIC> flag) or to skip
1712'get' magic (by clearing it). The other forms do perform 'get' magic, except
1713for the ones with C<nomg> in their names, which skip 'get' magic.
1714
e97ba882
KW
1715The forms that take a C<len> parameter will set that variable to the byte
1716length of the resultant string (these are macros, so don't use C<&len>).
1717
1718The forms with C<nolen> in their names indicate they don't have a C<len>
1719parameter. They should be used only when it is known that the PV is a C
1720string, terminated by a NUL byte, and without intermediate NUL characters; or
1721when you don't care about its length.
26d1d7c7
KW
1722
1723The forms with C<mutable> in their names are effectively the same as those without,
1724but the name emphasizes that the string is modifiable by the caller, which it is
1725in all the forms.
1726
1727C<SvPVutf8_force> is like C<SvPV_force>, but converts C<sv> to UTF-8 first if
1728not already UTF-8.
1729
1730C<SvPVutf8x_force> is like C<SvPVutf8_force>, but guarantees to evaluate C<sv>
1731only once; use the more efficient C<SvPVutf8_force> otherwise.
1732
1733C<SvPVbyte_force> is like C<SvPV_force>, but converts C<sv> to byte
1734representation first if currently encoded as UTF-8. If the SV cannot be
1735downgraded from UTF-8, this croaks.
1736
1737C<SvPVbytex_force> is like C<SvPVbyte_force>, but guarantees to evaluate C<sv>
1738only once; use the more efficient C<SvPVbyte_force> otherwise.
645c22ef 1739
1607e393
KW
1740=for apidoc Am | char*|SvPV |SV* sv|STRLEN len
1741=for apidoc_item |const char*|SvPV_const |SV* sv|STRLEN len
1742=for apidoc_item | char*|SvPV_flags |SV* sv|STRLEN len|U32 flags
1743=for apidoc_item |const char*|SvPV_flags_const |SV* sv|STRLEN len|U32 flags
1744=for apidoc_item | char*|SvPV_flags_mutable |SV* sv|STRLEN len|U32 flags
1745=for apidoc_item | char*|SvPV_mutable |SV* sv|STRLEN len
1746=for apidoc_item | char*|SvPV_nolen |SV* sv
1747=for apidoc_item |const char*|SvPV_nolen_const |SV* sv
1748=for apidoc_item | char*|SvPV_nomg |SV* sv|STRLEN len
1749=for apidoc_item |const char*|SvPV_nomg_const |SV* sv|STRLEN len
1e0eb5c5 1750=for apidoc_item |const char*|SvPV_nomg_const_nolen|SV* sv
1607e393
KW
1751=for apidoc_item | char*|SvPV_nomg_nolen |SV* sv
1752=for apidoc_item | char*|SvPVbyte |SV* sv|STRLEN len
1753=for apidoc_item | char*|SvPVbyte_nolen |SV* sv
1754=for apidoc_item | char*|SvPVbyte_nomg |SV* sv|STRLEN len
1755=for apidoc_item | char*|SvPVbyte_or_null |SV* sv|STRLEN len
1756=for apidoc_item | char*|SvPVbyte_or_null_nomg|SV* sv|STRLEN len
1757=for apidoc_item | char*|SvPVbytex |SV* sv|STRLEN len
1758=for apidoc_item | char*|SvPVbytex_nolen |SV* sv
1759=for apidoc_item | char*|SvPVutf8 |SV* sv|STRLEN len
1760=for apidoc_item | char*|SvPVutf8_nolen |SV* sv
1761=for apidoc_item | char*|SvPVutf8_nomg |SV* sv|STRLEN len
1762=for apidoc_item | char*|SvPVutf8_or_null |SV* sv|STRLEN len
1763=for apidoc_item | char*|SvPVutf8_or_null_nomg|SV* sv|STRLEN len
1764=for apidoc_item | char*|SvPVutf8x |SV* sv|STRLEN len
1765=for apidoc_item | char*|SvPVx |SV* sv|STRLEN len
1766=for apidoc_item |const char*|SvPVx_const |SV* sv|STRLEN len
1767=for apidoc_item | char*|SvPVx_nolen |SV* sv
1768=for apidoc_item |const char*|SvPVx_nolen_const |SV* sv
1e0eb5c5 1769
1ef9039b 1770These each return a pointer to the string in C<sv>, or a stringified form of
1e0eb5c5
KW
1771C<sv> if it does not contain a string. The SV may cache the stringified
1772version becoming C<SvPOK>.
1773
1774This is a very basic and common operation, so there are lots of slightly
1775different versions of it.
1776
1777Note that there is no guarantee that the return value of C<SvPV(sv)>, for
1778example, is equal to C<SvPVX(sv)>, or that C<SvPVX(sv)> contains valid data, or
1779that successive calls to C<SvPV(sv)> (or another of these forms) will return
1780the same pointer value each time. This is due to the way that things like
1781overloading and Copy-On-Write are handled. In these cases, the return value
1782may point to a temporary buffer or similar. If you absolutely need the
1783C<SvPVX> field to be valid (for example, if you intend to write to it), then
1784see C<L</SvPV_force>>.
1785
1786The differences between the forms are:
1787
3c3f883d
FG
1788The forms with neither C<byte> nor C<utf8> in their names (e.g., C<SvPV> or
1789C<SvPV_nolen>) can expose the SV's internal string buffer. If
1790that buffer consists entirely of bytes 0-255 and includes any bytes above
1791127, then you B<MUST> consult C<SvUTF8> to determine the actual code points
1792the string is meant to contain. Generally speaking, it is probably safer to
1793prefer C<SvPVbyte>, C<SvPVutf8>, and the like. See
1794L<perlguts/How do I pass a Perl string to a C library?> for more details.
1795
1e0eb5c5
KW
1796The forms with C<flags> in their names allow you to use the C<flags> parameter
1797to specify to process 'get' magic (by setting the C<SV_GMAGIC> flag) or to skip
1798'get' magic (by clearing it). The other forms process 'get' magic, except for
1799the ones with C<nomg> in their names, which skip 'get' magic.
1800
1801The forms that take a C<len> parameter will set that variable to the byte
1802length of the resultant string (these are macros, so don't use C<&len>).
1803
1804The forms with C<nolen> in their names indicate they don't have a C<len>
1805parameter. They should be used only when it is known that the PV is a C
1806string, terminated by a NUL byte, and without intermediate NUL characters; or
1807when you don't care about its length.
1808
1809The forms with C<const> in their names return S<C<const char *>> so that the
1810compiler will hopefully complain if you were to try to modify the contents of
1811the string (unless you cast away const yourself).
1812
1813The other forms return a mutable pointer so that the string is modifiable by
1814the caller; this is emphasized for the ones with C<mutable> in their names.
1815
1ef9039b
KW
1816As of 5.38, all forms are guaranteed to evaluate C<sv> exactly once. For
1817earlier Perls, use a form whose name ends with C<x> for single evaluation.
1e0eb5c5
KW
1818
1819C<SvPVutf8> is like C<SvPV>, but converts C<sv> to UTF-8 first if not already
1820UTF-8. Similiarly, the other forms with C<utf8> in their names correspond to
1821their respective forms without.
1822
1823C<SvPVutf8_or_null> and C<SvPVutf8_or_null_nomg> don't have corresponding
1824non-C<utf8> forms. Instead they are like C<SvPVutf8_nomg>, but when C<sv> is
1825undef, they return C<NULL>.
1826
1827C<SvPVbyte> is like C<SvPV>, but converts C<sv> to byte representation first if
1828currently encoded as UTF-8. If C<sv> cannot be downgraded from UTF-8, it
1829croaks. Similiarly, the other forms with C<byte> in their names correspond to
1830their respective forms without.
1831
1832C<SvPVbyte_or_null> doesn't have a corresponding non-C<byte> form. Instead it
1833is like C<SvPVbyte>, but when C<sv> is undef, it returns C<NULL>.
71eb6d8c 1834
1607e393 1835=for apidoc SvTRUE
4eff5eb8 1836=for apidoc_item SvTRUE_NN
1607e393 1837=for apidoc_item SvTRUE_nomg
4eff5eb8 1838=for apidoc_item SvTRUE_nomg_NN
1607e393 1839=for apidoc_item SvTRUEx
06c841cf 1840
4eff5eb8
KW
1841These return a boolean indicating whether Perl would evaluate the SV as true or
1842false. See C<L</SvOK>> for a defined/undefined test.
3127113c 1843
4eff5eb8
KW
1844As of Perl 5.32, all are guaranteed to evaluate C<sv> only once. Prior to that
1845release, only C<SvTRUEx> guaranteed single evaluation; now C<SvTRUEx> is
1846identical to C<SvTRUE>.
954c1994 1847
4eff5eb8
KW
1848C<SvTRUE_nomg> and C<TRUE_nomg_NN> do not perform 'get' magic; the others do
1849unless the scalar is already C<SvPOK>, C<SvIOK>, or C<SvNOK> (the public, not
1850the private flags).
1851
1852C<SvTRUE_NN> is like C<L</SvTRUE>>, but C<sv> is assumed to be
1853non-null (NN). If there is a possibility that it is NULL, use plain
1854C<SvTRUE>.
1855
1856C<SvTRUE_nomg_NN> is like C<L</SvTRUE_nomg>>, but C<sv> is assumed to be
1857non-null (NN). If there is a possibility that it is NULL, use plain
1858C<SvTRUE_nomg>.
1859
df8dcb28
DD
1860=for apidoc Am|U32|SvIsCOW|SV* sv
1861Returns a U32 value indicating whether the SV is Copy-On-Write (either shared
19dbb8f1 1862hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
8554e7a0 1863COW).
19dbb8f1
NC
1864
1865=for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
1866Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
1867scalar.
645c22ef 1868
954c1994
GS
1869=cut
1870*/
1871
1ef9039b
KW
1872/* To pass the action to the functions called by the following macros */
1873typedef enum {
1874 SvPVutf8_type_,
1875 SvPVbyte_type_,
1876 SvPVnormal_type_,
1877 SvPVforce_type_,
1878 SvPVutf8_pure_type_,
1879 SvPVbyte_pure_type_
1880} PL_SvPVtype;
1881
1882START_EXTERN_C
1883
1884/* When this code was written, embed.fnc could not handle function pointer
1885 * parameters; perhaps it still can't */
1886#ifndef PERL_NO_INLINE_FUNCTIONS
1887PERL_STATIC_INLINE char*
1888Perl_SvPV_helper(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags, const PL_SvPVtype type, char * (*non_trivial)(pTHX_ SV *, STRLEN * const, const U32), const bool or_null, const U32 return_flags);
1889#endif
1890
1891END_EXTERN_C
8d6d96c1 1892
cff76435
NC
1893/* This test is "is there a cached PV that we can use directly?"
1894 * We can if
1895 * a) SVf_POK is true and there's definitely no get magic on the scalar
1896 * b) SVp_POK is true, there's no get magic, and we know that the cached PV
1897 * came from an IV conversion.
1898 * For the latter case, we don't set SVf_POK so that we can distinguish whether
1899 * the value originated as a string or as an integer, before we cached the
1900 * second representation. */
1901#define SvPOK_or_cached_IV(sv) \
1902 (((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK) || ((SvFLAGS(sv) & (SVf_IOK|SVp_POK|SVs_GMG)) == (SVf_IOK|SVp_POK)))
1903
1ef9039b
KW
1904#define SvPV_flags(sv, len, flags) \
1905 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_, \
1906 Perl_sv_2pv_flags, FALSE, 0)
1907#define SvPV_flags_const(sv, len, flags) \
1908 ((const char*) Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_, \
1909 Perl_sv_2pv_flags, FALSE, \
1910 SV_CONST_RETURN))
1911#define SvPV_flags_const_nolen(sv, flags) \
1912 ((const char*) Perl_SvPV_helper(aTHX_ sv, NULL, flags, SvPVnormal_type_, \
1913 Perl_sv_2pv_flags, FALSE, \
1914 SV_CONST_RETURN))
1915#define SvPV_flags_mutable(sv, len, flags) \
1916 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_, \
1917 Perl_sv_2pv_flags, FALSE, SV_MUTABLE_RETURN)
1918
1919#define SvPV_nolen(sv) \
1920 Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVnormal_type_, \
1921 Perl_sv_2pv_flags, FALSE, 0)
1922
1923#define SvPV_nolen_const(sv) SvPV_flags_const_nolen(sv, SV_GMAGIC)
1924
1925#define SvPV(sv, len) SvPV_flags(sv, len, SV_GMAGIC)
1926#define SvPV_const(sv, len) SvPV_flags_const(sv, len, SV_GMAGIC)
1927#define SvPV_mutable(sv, len) SvPV_flags_mutable(sv, len, SV_GMAGIC)
1928
1929#define SvPV_nomg_nolen(sv) \
1930 Perl_SvPV_helper(aTHX_ sv, NULL, 0, SvPVnormal_type_,Perl_sv_2pv_flags, \
1931 FALSE, 0)
1932#define SvPV_nomg(sv, len) SvPV_flags(sv, len, 0)
1933#define SvPV_nomg_const(sv, len) SvPV_flags_const(sv, len, 0)
1934#define SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
1935
1936#define SvPV_force_flags(sv, len, flags) \
1937 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVforce_type_, \
1938 Perl_sv_pvn_force_flags, FALSE, 0)
1939#define SvPV_force_flags_nolen(sv, flags) \
1940 Perl_SvPV_helper(aTHX_ sv, NULL, flags, SvPVforce_type_, \
1941 Perl_sv_pvn_force_flags, FALSE, 0)
1942#define SvPV_force_flags_mutable(sv, len, flags) \
1943 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVforce_type_, \
1944 Perl_sv_pvn_force_flags, FALSE, SV_MUTABLE_RETURN)
1945
1946#define SvPV_force(sv, len) SvPV_force_flags(sv, len, SV_GMAGIC)
1947#define SvPV_force_nolen(sv) SvPV_force_flags_nolen(sv, SV_GMAGIC)
3629b4ec 1948#define SvPV_force_mutable(sv, len) SvPV_force_flags_mutable(sv, len, SV_GMAGIC)
baca2b92 1949
0334097d 1950/* "_nomg" in these defines means no mg_get() */
1ef9039b
KW
1951#define SvPV_force_nomg(sv, len) SvPV_force_flags(sv, len, 0)
1952#define SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)
1953
1954#define SvPVutf8(sv, len) \
1955 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVutf8_type_, \
1956 Perl_sv_2pvutf8_flags, FALSE, 0)
1957#define SvPVutf8_nomg(sv, len) \
1958 Perl_SvPV_helper(aTHX_ sv, NULL, 0, SvPVutf8_type_, \
1959 Perl_sv_2pvutf8_flags, FALSE, 0)
1960#define SvPVutf8_nolen(sv) \
1961 Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVutf8_type_, \
1962 Perl_sv_2pvutf8_flags, FALSE, 0)
1963#define SvPVutf8_or_null(sv, len) \
1964 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVutf8_type_, \
1965 Perl_sv_2pvutf8_flags, TRUE, 0)
1966#define SvPVutf8_or_null_nomg(sv, len) \
1967 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVutf8_type_, \
1968 Perl_sv_2pvutf8_flags, TRUE, 0)
1969
1970#define SvPVbyte(sv, len) \
1971 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVbyte_type_, \
1972 Perl_sv_2pvbyte_flags, FALSE, 0)
1973#define SvPVbyte_nomg(sv, len) \
1974 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_type_, \
1975 Perl_sv_2pvbyte_flags, FALSE, 0)
1976#define SvPVbyte_nolen(sv) \
1977 Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVbyte_type_, \
1978 Perl_sv_2pvbyte_flags, FALSE, 0)
1979#define SvPVbyte_or_null(sv, len) \
1980 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVbyte_type_, \
1981 Perl_sv_2pvbyte_flags, TRUE, 0)
1982#define SvPVbyte_or_null_nomg(sv, len) \
1983 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_type_, \
1984 Perl_sv_2pvbyte_flags, TRUE, 0)
1985
1986#define SvPVutf8_force(sv, len) \
1987 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVutf8_pure_type_, \
1988 Perl_sv_pvutf8n_force_wrapper, FALSE, 0)
1989
1990#define SvPVbyte_force(sv, len) \
1991 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_pure_type_, \
1992 Perl_sv_pvbyten_force_wrapper, FALSE, 0)
1993
1994/* define FOOx(): Before FOO(x) was inlined, these were idempotent versions of
1995 * FOO(). */
baca2b92 1996
3629b4ec
KW
1997#define SvPVx_force(sv, len) sv_pvn_force(sv, &len)
1998#define SvPVutf8x_force(sv, len) sv_pvutf8n_force(sv, &len)
1999#define SvPVbytex_force(sv, len) sv_pvbyten_force(sv, &len)
baca2b92 2000
7358a4d1 2001#define SvTRUEx(sv) SvTRUE(sv)
4eff5eb8
KW
2002#define SvTRUEx_nomg(sv) SvTRUE_nomg(sv)
2003#define SvTRUE_nomg_NN(sv) SvTRUE_common(sv, TRUE)
4bac9ae4 2004
dae8ab81
KW
2005# define SvIVx(sv) SvIV(sv)
2006# define SvUVx(sv) SvUV(sv)
2007# define SvNVx(sv) SvNV(sv)
2008
041c1a23 2009#if defined(PERL_USE_GCC_BRACE_GROUPS)
baca2b92 2010
3629b4ec
KW
2011# define SvPVx(sv, len) ({SV *_sv = (sv); SvPV(_sv, len); })
2012# define SvPVx_const(sv, len) ({SV *_sv = (sv); SvPV_const(_sv, len); })
002e4c74 2013# define SvPVx_nolen(sv) ({SV *_sv = (sv); SvPV_nolen(_sv); })
9ce348e8 2014# define SvPVx_nolen_const(sv) ({SV *_sv = (sv); SvPV_nolen_const(_sv); })
3629b4ec
KW
2015# define SvPVutf8x(sv, len) ({SV *_sv = (sv); SvPVutf8(_sv, len); })
2016# define SvPVbytex(sv, len) ({SV *_sv = (sv); SvPVbyte(_sv, len); })
002e4c74 2017# define SvPVbytex_nolen(sv) ({SV *_sv = (sv); SvPVbyte_nolen(_sv); })
baca2b92 2018
a80f87c4 2019#else /* __GNUC__ */
baca2b92 2020
a80f87c4
GS
2021/* These inlined macros use globals, which will require a thread
2022 * declaration in user code, so we avoid them under threads */
2023
3629b4ec
KW
2024# define SvPVx(sv, len) ((PL_Sv = (sv)), SvPV(PL_Sv, len))
2025# define SvPVx_const(sv, len) ((PL_Sv = (sv)), SvPV_const(PL_Sv, len))
002e4c74 2026# define SvPVx_nolen(sv) ((PL_Sv = (sv)), SvPV_nolen(PL_Sv))
5f1478c3 2027# define SvPVx_nolen_const(sv) ((PL_Sv = (sv)), SvPV_nolen_const(PL_Sv))
3629b4ec
KW
2028# define SvPVutf8x(sv, len) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, len))
2029# define SvPVbytex(sv, len) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, len))
002e4c74 2030# define SvPVbytex_nolen(sv) ((PL_Sv = (sv)), SvPVbyte_nolen(PL_Sv))
baca2b92
DM
2031#endif /* __GNU__ */
2032
914bb574
PE
2033#define SvIsCOW(sv) (SvFLAGS(sv) & SVf_IsCOW)
2034#define SvIsCOW_on(sv) (SvFLAGS(sv) |= SVf_IsCOW)
2035#define SvIsCOW_off(sv) (SvFLAGS(sv) &= ~(SVf_IsCOW|SVppv_STATIC))
2036#define SvIsCOW_shared_hash(sv) ((SvFLAGS(sv) & (SVf_IsCOW|SVppv_STATIC)) == (SVf_IsCOW) && SvLEN(sv) == 0)
2037#define SvIsCOW_static(sv) ((SvFLAGS(sv) & (SVf_IsCOW|SVppv_STATIC)) == (SVf_IsCOW|SVppv_STATIC))
baca2b92 2038
bdd68bc3 2039#define SvSHARED_HEK_FROM_PV(pvx) \
1f4fbd3b 2040 ((struct hek*)(pvx - STRUCT_OFFSET(struct hek, hek_key)))
fb857162
KW
2041/*
2042=for apidoc Am|struct hek*|SvSHARED_HASH|SV * sv
2043Returns the hash for C<sv> created by C<L</newSVpvn_share>>.
2044
2045=cut
2046*/
0a356b31 2047#define SvSHARED_HASH(sv) (0 + SvSHARED_HEK_FROM_PV(SvPVX_const(sv))->hek_hash)
c158a4fd 2048
baca2b92 2049/* flag values for sv_*_flags functions */
5dfbfbd5 2050#define SV_UTF8_NO_ENCODING 0 /* No longer used */
49e7c422
KW
2051
2052/*
2053=for apidoc AmnhD||SV_UTF8_NO_ENCODING
2054
2055=cut
2056*/
2057
baca2b92
DM
2058#define SV_IMMEDIATE_UNREF 1
2059#define SV_GMAGIC 2
765f542d 2060#define SV_COW_DROP_PV 4
5fcdf167 2061#define SV_NOSTEAL 16
32a5c6ec
NC
2062#define SV_CONST_RETURN 32
2063#define SV_MUTABLE_RETURN 64
bddd5118 2064#define SV_SMAGIC 128
c1c21316 2065#define SV_HAS_TRAILING_NUL 256
34482cd6 2066#define SV_COW_SHARED_HASH_KEYS 512
cb23d5b1 2067/* This one is only enabled for PERL_OLD_COPY_ON_WRITE */
b7adcee4
FC
2068/* XXX This flag actually enabled for any COW. But it appears not to do
2069 anything. Can we just remove it? Or will it serve some future
2070 purpose. */
cb23d5b1 2071#define SV_COW_OTHER_PVS 1024
9f621bb0
NC
2072/* Make sv_2pv_flags return NULL if something is undefined. */
2073#define SV_UNDEF_RETURNS_NULL 2048
b3ab6785
KW
2074/* Tell sv_utf8_upgrade() to not check to see if an upgrade is really needed.
2075 * This is used when the caller has already determined it is, and avoids
2076 * redundant work */
2077#define SV_FORCE_UTF8_UPGRADE 4096
aee036bb
DM
2078/* if (after resolving magic etc), the SV is found to be overloaded,
2079 * don't call the overload magic, just return as-is */
2080#define SV_SKIP_OVERLOAD 8192
20c88bf7
FC
2081#define SV_CATBYTES 16384
2082#define SV_CATUTF8 32768
34482cd6
NC
2083
2084/* The core is safe for this COW optimisation. XS code on CPAN may not be.
2085 So only default to doing the COW setup if we're in the core.
2086 */
2087#ifdef PERL_CORE
2088# ifndef SV_DO_COW_SVSETSV
cb23d5b1 2089# define SV_DO_COW_SVSETSV SV_COW_SHARED_HASH_KEYS|SV_COW_OTHER_PVS
34482cd6
NC
2090# endif
2091#endif
2092
2093#ifndef SV_DO_COW_SVSETSV
2094# define SV_DO_COW_SVSETSV 0
2095#endif
2096
765f542d 2097
5abc721d
NC
2098#define sv_unref(sv) sv_unref_flags(sv, 0)
2099#define sv_force_normal(sv) sv_force_normal_flags(sv, 0)
47518d95
NC
2100#define sv_usepvn(sv, p, l) sv_usepvn_flags(sv, p, l, 0)
2101#define sv_usepvn_mg(sv, p, l) sv_usepvn_flags(sv, p, l, SV_SMAGIC)
174c73e3 2102
2bf695be
KW
2103/*
2104=for apidoc Am|void|SV_CHECK_THINKFIRST_COW_DROP|SV * sv
2105
2106Call this when you are about to replace the PV value in C<sv>, which is
2107potentially copy-on-write. It stops any sharing with other SVs, so that no
2108Copy on Write (COW) actually happens. This COW would be useless, as it would
2109immediately get changed to something else. This function also removes any
2110other encumbrances that would be problematic when changing C<sv>.
2111
2112=cut
2113*/
765f542d
NC
2114
2115#define SV_CHECK_THINKFIRST_COW_DROP(sv) if (SvTHINKFIRST(sv)) \
1f4fbd3b 2116 sv_force_normal_flags(sv, SV_COW_DROP_PV)
46187eeb 2117
93c10d60 2118#ifdef PERL_COPY_ON_WRITE
db2c6cb3 2119# define SvCANCOW(sv) \
1f4fbd3b
MS
2120 (SvIsCOW(sv) \
2121 ? SvLEN(sv) ? CowREFCNT(sv) != SV_COW_REFCNT_MAX : 1 \
2122 : (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS \
2123 && SvCUR(sv)+1 < SvLEN(sv))
db2c6cb3
FC
2124 /* Note: To allow 256 COW "copies", a refcnt of 0 means 1. */
2125# define CowREFCNT(sv) (*(U8 *)(SvPVX(sv)+SvLEN(sv)-1))
1381ccb1 2126# define SV_COW_REFCNT_MAX nBIT_UMAX(sizeof(U8) * CHARBITS)
f7a8268c 2127# define CAN_COW_MASK (SVf_POK|SVf_ROK|SVp_POK|SVf_FAKE| \
1f4fbd3b 2128 SVf_OOK|SVf_BREAK|SVf_READONLY|SVf_PROTECT)
b7adcee4 2129#endif
ed252734 2130
ed252734
NC
2131#define CAN_COW_FLAGS (SVp_POK|SVf_POK)
2132
2bf695be
KW
2133/*
2134=for apidoc Am|void|SV_CHECK_THINKFIRST|SV * sv
2135
2136Remove any encumbrances from C<sv>, that need to be taken care of before it
2137is modifiable. For example if it is Copy on Write (COW), now is the time to
2138make that copy.
2139
2140If you know that you are about to change the PV value of C<sv>, instead use
2141L</C<SV_CHECK_THINKFIRST_COW_DROP>> to avoid the write that would be
2142immediately written again.
2143
2144=cut
2145*/
765f542d 2146#define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) \
1f4fbd3b 2147 sv_force_normal_flags(sv, 0)
765f542d
NC
2148
2149
baca2b92
DM
2150/* all these 'functions' are now just macros */
2151
2152#define sv_pv(sv) SvPV_nolen(sv)
2153#define sv_pvutf8(sv) SvPVutf8_nolen(sv)
2154#define sv_pvbyte(sv) SvPVbyte_nolen(sv)
2155
2156#define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
b3ab6785 2157#define sv_utf8_upgrade_flags(sv, flags) sv_utf8_upgrade_flags_grow(sv, flags, 0)
baca2b92 2158#define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
423ce623
P
2159#define sv_utf8_downgrade(sv, fail_ok) sv_utf8_downgrade_flags(sv, fail_ok, SV_GMAGIC)
2160#define sv_utf8_downgrade_nomg(sv, fail_ok) sv_utf8_downgrade_flags(sv, fail_ok, 0)
baca2b92 2161#define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
9dcc53ea 2162#define sv_catpv_nomg(dsv, sstr) sv_catpv_flags(dsv, sstr, 0)
34482cd6 2163#define sv_setsv(dsv, ssv) \
1f4fbd3b 2164 sv_setsv_flags(dsv, ssv, SV_GMAGIC|SV_DO_COW_SVSETSV)
34482cd6 2165#define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_DO_COW_SVSETSV)
baca2b92
DM
2166#define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
2167#define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
b347df82 2168#define sv_catsv_mg(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC|SV_SMAGIC)
baca2b92 2169#define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
37ee558d 2170#define sv_catpvn_mg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC|SV_SMAGIC);
4bac9ae4
CS
2171#define sv_copypv(dsv, ssv) sv_copypv_flags(dsv, ssv, SV_GMAGIC)
2172#define sv_copypv_nomg(dsv, ssv) sv_copypv_flags(dsv, ssv, 0)
baca2b92 2173#define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
cb2f1b7b 2174#define sv_2pv_nolen(sv) sv_2pv(sv, 0)
757fc329 2175#define sv_2pvbyte(sv, lp) sv_2pvbyte_flags(sv, lp, SV_GMAGIC)
cb2f1b7b 2176#define sv_2pvbyte_nolen(sv) sv_2pvbyte(sv, 0)
757fc329 2177#define sv_2pvutf8(sv, lp) sv_2pvutf8_flags(sv, lp, SV_GMAGIC)
cb2f1b7b 2178#define sv_2pvutf8_nolen(sv) sv_2pvutf8(sv, 0)
baca2b92
DM
2179#define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
2180#define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
2181#define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
891f9566
YST
2182#define sv_2iv(sv) sv_2iv_flags(sv, SV_GMAGIC)
2183#define sv_2uv(sv) sv_2uv_flags(sv, SV_GMAGIC)
39d5de13 2184#define sv_2nv(sv) sv_2nv_flags(sv, SV_GMAGIC)
078504b2 2185#define sv_eq(sv1, sv2) sv_eq_flags(sv1, sv2, SV_GMAGIC)
e8c9c9d4
NC
2186#define sv_cmp(sv1, sv2) sv_cmp_flags(sv1, sv2, SV_GMAGIC)
2187#define sv_cmp_locale(sv1, sv2) sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC)
1dd43bce 2188#define sv_numeq(sv1, sv2) sv_numeq_flags(sv1, sv2, SV_GMAGIC)
e269455b 2189#define sv_streq(sv1, sv2) sv_streq_flags(sv1, sv2, SV_GMAGIC)
e17dadf3 2190#define sv_collxfrm(sv, nxp) sv_collxfrm_flags(sv, nxp, SV_GMAGIC)
06c841cf 2191#define sv_2bool(sv) sv_2bool_flags(sv, SV_GMAGIC)
4bac9ae4 2192#define sv_2bool_nomg(sv) sv_2bool_flags(sv, 0)
84335ee9 2193#define sv_insert(bigstr, offset, len, little, littlelen) \
1f4fbd3b
MS
2194 Perl_sv_insert_flags(aTHX_ (bigstr),(offset), (len), (little), \
2195 (littlelen), SV_GMAGIC)
108cb980 2196#define sv_mortalcopy(sv) \
1f4fbd3b 2197 Perl_sv_mortalcopy_flags(aTHX_ sv, SV_GMAGIC|SV_DO_COW_SVSETSV)
f34d8cdd 2198#define sv_cathek(sv,hek) \
1f4fbd3b
MS
2199 STMT_START { \
2200 HEK * const bmxk = hek; \
2201 sv_catpvn_flags(sv, HEK_KEY(bmxk), HEK_LEN(bmxk), \
2202 HEK_UTF8(bmxk) ? SV_CATUTF8 : SV_CATBYTES); \
2203 } STMT_END
baca2b92 2204
db79b45b 2205/* Should be named SvCatPVN_utf8_upgrade? */
4bac9ae4 2206#define sv_catpvn_nomg_utf8_upgrade(dsv, sstr, slen, nsv) \
1f4fbd3b
MS
2207 STMT_START { \
2208 if (!(nsv)) \
2209 nsv = newSVpvn_flags(sstr, slen, SVs_TEMP); \
2210 else \
2211 sv_setpvn(nsv, sstr, slen); \
2212 SvUTF8_off(nsv); \
2213 sv_utf8_upgrade(nsv); \
2214 sv_catsv_nomg(dsv, nsv); \
2215 } STMT_END
bcef955a
KW
2216#define sv_catpvn_nomg_maybeutf8(dsv, sstr, len, is_utf8) \
2217 sv_catpvn_flags(dsv, sstr, len, (is_utf8)?SV_CATUTF8:SV_CATBYTES)
79072805 2218
25fdce4a 2219#if defined(PERL_CORE) || defined(PERL_EXT)
aec43834
FC
2220# define sv_or_pv_len_utf8(sv, pv, bytelen) \
2221 (SvGAMAGIC(sv) \
1f4fbd3b
MS
2222 ? utf8_length((U8 *)(pv), (U8 *)(pv)+(bytelen)) \
2223 : sv_len_utf8(sv))
aec43834
FC
2224#endif
2225
954c1994 2226/*
ac950c89
KW
2227=for apidoc newRV
2228=for apidoc_item ||newRV_inc|
954c1994 2229
ac950c89
KW
2230These are identical. They create an RV wrapper for an SV. The reference count
2231for the original SV is incremented.
954c1994
GS
2232
2233=cut
2234*/
2235
5f05dabc 2236#define newRV_inc(sv) newRV(sv)
5f05dabc 2237
796b6530 2238/* the following macros update any magic values this C<sv> is associated with */
79072805 2239
954c1994 2240/*
574e093e 2241=for apidoc_section $SV
ccfc67b7 2242
954c1994 2243=for apidoc Am|void|SvSETMAGIC|SV* sv
8890fded 2244Invokes C<L</mg_set>> on an SV if it has 'set' magic. This is necessary
f7e029ab
FC
2245after modifying a scalar, in case it is a magical variable like C<$|>
2246or a tied variable (it calls C<STORE>). This macro evaluates its
954c1994
GS
2247argument more than once.
2248
1607e393
KW
2249=for apidoc Am|void|SvSetMagicSV|SV* dsv|SV* ssv
2250=for apidoc_item SvSetMagicSV_nosteal
2251=for apidoc_item SvSetSV
2252=for apidoc_item SvSetSV_nosteal
574e093e
KW
2253
2254if C<dsv> is the same as C<ssv>, these do nothing. Otherwise they all call
2255some form of C<L</sv_setsv>>. They may evaluate their arguments more than
2256once.
954c1994 2257
574e093e 2258The only differences are:
954c1994 2259
574e093e
KW
2260C<SvSetMagicSV> and C<SvSetMagicSV_nosteal> perform any required 'set' magic
2261afterwards on the destination SV; C<SvSetSV> and C<SvSetSV_nosteal> do not.
645c22ef 2262
574e093e
KW
2263C<SvSetSV_nosteal> C<SvSetMagicSV_nosteal> call a non-destructive version of
2264C<sv_setsv>.
645c22ef 2265
68795e93 2266=for apidoc Am|void|SvSHARE|SV* sv
796b6530 2267Arranges for C<sv> to be shared between threads if a suitable module
68795e93
NIS
2268has been loaded.
2269
2270=for apidoc Am|void|SvLOCK|SV* sv
796b6530 2271Arranges for a mutual exclusion lock to be obtained on C<sv> if a suitable module
68795e93
NIS
2272has been loaded.
2273
2274=for apidoc Am|void|SvUNLOCK|SV* sv
796b6530 2275Releases a mutual exclusion lock on C<sv> if a suitable module
68795e93
NIS
2276has been loaded.
2277
3f620621 2278=for apidoc_section $SV
ccfc67b7 2279
679ac26e 2280=for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
954c1994
GS
2281Expands the character buffer in the SV so that it has room for the
2282indicated number of bytes (remember to reserve space for an extra trailing
6602b933 2283C<NUL> character). Calls C<sv_grow> to perform the expansion if necessary.
72d33970 2284Returns a pointer to the character
796b6530 2285buffer. SV must be of type >= C<SVt_PV>. One
4b1e7f06 2286alternative is to call C<sv_grow> if you are not sure of the type of SV.
954c1994 2287
d8732c60
KW
2288You might mistakenly think that C<len> is the number of bytes to add to the
2289existing size, but instead it is the total size C<sv> should be.
2290
45661033
YO
2291=for apidoc Am|char *|SvPVCLEAR|SV* sv
2292Ensures that sv is a SVt_PV and that its SvCUR is 0, and that it is
2293properly null terminated. Equivalent to sv_setpvs(""), but more efficient.
2294
954c1994
GS
2295=cut
2296*/
2297
45661033 2298#define SvPVCLEAR(sv) sv_setpv_bufsize(sv,0,0)
16c91539
BM
2299#define SvSHARE(sv) PL_sharehook(aTHX_ sv)
2300#define SvLOCK(sv) PL_lockhook(aTHX_ sv)
2301#define SvUNLOCK(sv) PL_unlockhook(aTHX_ sv)
2302#define SvDESTROYABLE(sv) PL_destroyhook(aTHX_ sv)
68795e93 2303
5d9574c1 2304#define SvSETMAGIC(x) STMT_START { if (UNLIKELY(SvSMAGICAL(x))) mg_set(x); } STMT_END
79072805 2305
54310121 2306#define SvSetSV_and(dst,src,finally) \
1f4fbd3b 2307 STMT_START { \
c1bee391
KW
2308 SV * src_ = src; \
2309 SV * dst_ = dst; \
2310 if (LIKELY((dst_) != (src_))) { \
2311 sv_setsv(dst_, src_); \
1f4fbd3b
MS
2312 finally; \
2313 } \
2314 } STMT_END
c1bee391 2315
54310121 2316#define SvSetSV_nosteal_and(dst,src,finally) \
1f4fbd3b 2317 STMT_START { \
c1bee391
KW
2318 SV * src_ = src; \
2319 SV * dst_ = dst; \
2320 if (LIKELY((dst_) != (src_))) { \
2321 sv_setsv_flags(dst_, src_, \
2322 SV_GMAGIC \
2323 | SV_NOSTEAL \
2324 | SV_DO_COW_SVSETSV); \
1f4fbd3b
MS
2325 finally; \
2326 } \
2327 } STMT_END
79072805 2328
54310121 2329#define SvSetSV(dst,src) \
1f4fbd3b 2330 SvSetSV_and(dst,src,/*nothing*/;)
54310121 2331#define SvSetSV_nosteal(dst,src) \
1f4fbd3b 2332 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
54310121 2333
2334#define SvSetMagicSV(dst,src) \
1f4fbd3b 2335 SvSetSV_and(dst,src,SvSETMAGIC(dst))
54310121 2336#define SvSetMagicSV_nosteal(dst,src) \
1f4fbd3b 2337 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
54310121 2338
db79b45b 2339
1045810a 2340#if !defined(SKIP_DEBUGGING)
79072805 2341#define SvPEEK(sv) sv_peek(sv)
3967c732
JD
2342#else
2343#define SvPEEK(sv) ""
2344#endif
79072805 2345
7c123f9d
DM
2346/* Is this a per-interpreter immortal SV (rather than global)?
2347 * These should either occupy adjacent entries in the interpreter struct
2348 * (MULTIPLICITY) or adjacent elements of PL_sv_immortals[] otherwise.
2349 * The unsigned (Size_t) cast avoids the need for a second < 0 condition.
2350 */
2351#define SvIMMORTAL_INTERP(sv) ((Size_t)((sv) - &PL_sv_yes) < 4)
2352
2353/* Does this immortal have a true value? Currently only PL_sv_yes does. */
2354#define SvIMMORTAL_TRUE(sv) ((sv) == &PL_sv_yes)
2355
2356/* the SvREADONLY() test is to quickly reject most SVs */
2357#define SvIMMORTAL(sv) \
2358 ( SvREADONLY(sv) \
2359 && (SvIMMORTAL_INTERP(sv) || (sv) == &PL_sv_placeholder))
36477c24 2360
fe54beba
DM
2361#ifdef DEBUGGING
2362 /* exercise the immortal resurrection code in sv_free2() */
2363# define SvREFCNT_IMMORTAL 1000
2364#else
2365# define SvREFCNT_IMMORTAL ((~(U32)0)/2)
2366#endif
2367
567f3b56
TC
2368/*
2369=for apidoc Am|SV *|boolSV|bool b
2370
2371Returns a true SV if C<b> is a true value, or a false SV if C<b> is 0.
2372
fbe13c60 2373See also C<L</PL_sv_yes>> and C<L</PL_sv_no>>.
567f3b56
TC
2374
2375=cut
2376*/
2377
3280af22 2378#define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
54310121 2379
1d0d673f
PE
2380/*
2381=for apidoc Am|void|sv_setbool|SV *sv|bool b
2382=for apidoc_item |void|sv_setbool_mg|SV *sv|bool b
2383
2384These set an SV to a true or false boolean value, upgrading first if necessary.
2385
2386They differ only in that C<sv_setbool_mg> handles 'set' magic; C<sv_setbool>
2387does not.
2388
2389=cut
2390*/
2391
2392#define sv_setbool(sv, b) sv_setsv(sv, boolSV(b))
2393#define sv_setbool_mg(sv, b) sv_setsv_mg(sv, boolSV(b))
2394
79072805 2395#define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
f7877b28
NC
2396/* If I give every macro argument a different name, then there won't be bugs
2397 where nested macros get confused. Been there, done that. */
1d31276d
KW
2398/*
2399=for apidoc Am|bool|isGV_with_GP|SV * sv
2400Returns a boolean as to whether or not C<sv> is a GV with a pointer to a GP
2401(glob pointer).
2402
2403=cut
2404*/
f7877b28 2405#define isGV_with_GP(pwadak) \
1f4fbd3b
MS
2406 (((SvFLAGS(pwadak) & (SVp_POK|SVpgv_GP)) == SVpgv_GP) \
2407 && (SvTYPE(pwadak) == SVt_PVGV || SvTYPE(pwadak) == SVt_PVLV))
41bd242f
KW
2408
2409#define isGV_with_GP_on(sv) \
2410 STMT_START { \
2411 SV * sv_ = MUTABLE_SV(sv); \
2412 assert (SvTYPE(sv_) == SVt_PVGV || SvTYPE(sv_) == SVt_PVLV); \
2413 assert (!SvPOKp(sv_)); \
2414 assert (!SvIOKp(sv_)); \
2415 (SvFLAGS(sv_) |= SVpgv_GP); \
2e5b91de 2416 } STMT_END
41bd242f
KW
2417
2418#define isGV_with_GP_off(sv) \
2419 STMT_START { \
2420 SV * sv_ = MUTABLE_SV(sv); \
2421 assert (SvTYPE(sv_) == SVt_PVGV || SvTYPE(sv_) == SVt_PVLV); \
2422 assert (!SvPOKp(sv_)); \
2423 assert (!SvIOKp(sv_)); \
2424 (SvFLAGS(sv_) &= ~SVpgv_GP); \
2e5b91de 2425 } STMT_END
41bd242f 2426
f5b4df4d
FC
2427#ifdef PERL_CORE
2428# define isGV_or_RVCV(kadawp) \
2429 (isGV(kadawp) || (SvROK(kadawp) && SvTYPE(SvRV(kadawp)) == SVt_PVCV))
2430#endif
8d919b0a
FC
2431#define isREGEXP(sv) \
2432 (SvTYPE(sv) == SVt_REGEXP \
df6b4bd5 2433 || (SvFLAGS(sv) & (SVTYPEMASK|SVpgv_GP|SVf_FAKE)) \
1f4fbd3b 2434 == (SVt_PVLV|SVf_FAKE))
2e5b91de 2435
79072805 2436
bba82a09 2437#ifdef PERL_ANY_COW
db2c6cb3 2438# define SvGROW(sv,len) \
1f4fbd3b 2439 (SvIsCOW(sv) || SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
db2c6cb3 2440#else
6bbd724f 2441# define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
db2c6cb3 2442#endif
6bbd724f
DM
2443#define SvGROW_mutable(sv,len) \
2444 (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
933fea7f 2445#define Sv_Grow sv_grow
3d35f11b 2446
a0739874
DM
2447#define CLONEf_COPY_STACKS 1
2448#define CLONEf_KEEP_PTR_TABLE 2
c43294b8 2449#define CLONEf_CLONE_HOST 4
0405e91e 2450#define CLONEf_JOIN_IN 8
a0739874 2451
8cf8f3d1 2452struct clone_params {
d2d73c3e
AB
2453 AV* stashes;
2454 UV flags;
59b40662 2455 PerlInterpreter *proto_perl;
1db366cc 2456 PerlInterpreter *new_perl;
d08d57ef 2457 AV *unreferenced;
8cf8f3d1 2458};
102d3b8a 2459
238f2c13
P
2460/* SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
2461 with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */
2462#define newSVsv(sv) newSVsv_flags((sv), SV_GMAGIC|SV_NOSTEAL)
2463#define newSVsv_nomg(sv) newSVsv_flags((sv), SV_NOSTEAL)
2464
102d3b8a 2465/*
d106cb46 2466=for apidoc Am|SV*|newSVpvn_utf8|const char* s|STRLEN len|U32 utf8
740cce10 2467
30a15352 2468Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
796b6530 2469characters) into it. If C<utf8> is true, calls
740cce10
NC
2470C<SvUTF8_on> on the new SV. Implemented as a wrapper around C<newSVpvn_flags>.
2471
2472=cut
2473*/
2474
2475#define newSVpvn_utf8(s, len, u) newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
2476
2477/*
35e035cc
FC
2478=for apidoc Amx|SV*|newSVpadname|PADNAME *pn
2479
0f94cb1f 2480Creates a new SV containing the pad name.
35e035cc
FC
2481
2482=cut
2483*/
2484
0f94cb1f 2485#define newSVpadname(pn) newSVpvn_utf8(PadnamePV(pn), PadnameLEN(pn), TRUE)
35e035cc
FC
2486
2487/*
d106cb46 2488=for apidoc Am|void|SvOOK_offset|SV*sv|STRLEN len
69240efd 2489
796b6530 2490Reads into C<len> the offset from C<SvPVX> back to the true start of the
69240efd 2491allocated buffer, which will be non-zero if C<sv_chop> has been used to
8554e7a0 2492efficiently remove characters from start of the buffer. Implemented as a
2d7f6611
KW
2493macro, which takes the address of C<len>, which must be of type C<STRLEN>.
2494Evaluates C<sv> more than once. Sets C<len> to 0 if C<SvOOK(sv)> is false.
69240efd
NC
2495
2496=cut
2497*/
2498
2499#ifdef DEBUGGING
2500/* Does the bot know something I don't?
250110:28 <@Nicholas> metabatman
250210:28 <+meta> Nicholas: crash
2503*/
2504# define SvOOK_offset(sv, offset) STMT_START { \
1f4fbd3b
MS
2505 STATIC_ASSERT_STMT(sizeof(offset) == sizeof(STRLEN)); \
2506 if (SvOOK(sv)) { \
2507 const U8 *_crash = (U8*)SvPVX_const(sv); \
2508 (offset) = *--_crash; \
2509 if (!(offset)) { \
2510 _crash -= sizeof(STRLEN); \
2511 Copy(_crash, (U8 *)&(offset), sizeof(STRLEN), U8); \
2512 } \
2513 { \
2514 /* Validate the preceding buffer's sentinels to \
2515 verify that no-one is using it. */ \
2516 const U8 *const _bonk = (U8*)SvPVX_const(sv) - (offset);\
2517 while (_crash > _bonk) { \
2518 --_crash; \
2519 assert (*_crash == (U8)PTR2UV(_crash)); \
2520 } \
2521 } \
2522 } else { \
2523 (offset) = 0; \
2524 } \
69240efd
NC
2525 } STMT_END
2526#else
2527 /* This is the same code, but avoids using any temporary variables: */
2528# define SvOOK_offset(sv, offset) STMT_START { \
1f4fbd3b
MS
2529 STATIC_ASSERT_STMT(sizeof(offset) == sizeof(STRLEN)); \
2530 if (SvOOK(sv)) { \
2531 (offset) = ((U8*)SvPVX_const(sv))[-1]; \
2532 if (!(offset)) { \
2533 Copy(SvPVX_const(sv) - 1 - sizeof(STRLEN), \
2534 (U8*)&(offset), sizeof(STRLEN), U8); \
2535 } \
2536 } else { \
2537 (offset) = 0; \
2538 } \
69240efd
NC
2539 } STMT_END
2540#endif
85dca89a 2541
9287e2bf 2542/*
0c8e4929 2543=for apidoc_section $io
9287e2bf
KW
2544=for apidoc newIO
2545
2546Create a new IO, setting the reference count to 1.
2547
2548=cut
2549*/
85dca89a
NC
2550#define newIO() MUTABLE_IO(newSV_type(SVt_PVIO))
2551
d5722fbc
KW
2552#if defined(PERL_CORE) || defined(PERL_EXT)
2553
2554# define SV_CONST(name) \
1f4fbd3b
MS
2555 PL_sv_consts[SV_CONST_##name] \
2556 ? PL_sv_consts[SV_CONST_##name] \
2557 : (PL_sv_consts[SV_CONST_##name] = newSVpv_share(#name, 0))
a38ab475 2558
d5722fbc
KW
2559# define SV_CONST_TIESCALAR 0
2560# define SV_CONST_TIEARRAY 1
2561# define SV_CONST_TIEHASH 2
2562# define SV_CONST_TIEHANDLE 3
2563
2564# define SV_CONST_FETCH 4
2565# define SV_CONST_FETCHSIZE 5
2566# define SV_CONST_STORE 6
2567# define SV_CONST_STORESIZE 7
2568# define SV_CONST_EXISTS 8
2569
2570# define SV_CONST_PUSH 9
2571# define SV_CONST_POP 10
2572# define SV_CONST_SHIFT 11
2573# define SV_CONST_UNSHIFT 12
2574# define SV_CONST_SPLICE 13
2575# define SV_CONST_EXTEND 14
2576
2577# define SV_CONST_FIRSTKEY 15
2578# define SV_CONST_NEXTKEY 16
2579# define SV_CONST_SCALAR 17
2580
2581# define SV_CONST_OPEN 18
2582# define SV_CONST_WRITE 19
2583# define SV_CONST_PRINT 20
2584# define SV_CONST_PRINTF 21
2585# define SV_CONST_READ 22
2586# define SV_CONST_READLINE 23
2587# define SV_CONST_GETC 24
2588# define SV_CONST_SEEK 25
2589# define SV_CONST_TELL 26
2590# define SV_CONST_EOF 27
2591# define SV_CONST_BINMODE 28
2592# define SV_CONST_FILENO 29
2593# define SV_CONST_CLOSE 30
2594
2595# define SV_CONST_DELETE 31
2596# define SV_CONST_CLEAR 32
2597# define SV_CONST_UNTIE 33
2598# define SV_CONST_DESTROY 34
2599#endif
a38ab475
RZ
2600
2601#define SV_CONSTS_COUNT 35
2602
69240efd 2603/*
82fce3d0
DM
2604 * Bodyless IVs and NVs!
2605 *
2606 * Since 5.9.2, we can avoid allocating a body for SVt_IV-type SVs.
2607 * Since the larger IV-holding variants of SVs store their integer
2608 * values in their respective bodies, the family of SvIV() accessor
2609 * macros would naively have to branch on the SV type to find the
2610 * integer value either in the HEAD or BODY. In order to avoid this
2611 * expensive branch, a clever soul has deployed a great hack:
2612 * We set up the SvANY pointer such that instead of pointing to a
2613 * real body, it points into the memory before the location of the
2614 * head. We compute this pointer such that the location of
2615 * the integer member of the hypothetical body struct happens to
2616 * be the same as the location of the integer member of the bodyless
2617 * SV head. This now means that the SvIV() family of accessors can
2618 * always read from the (hypothetical or real) body via SvANY.
2619 *
2620 * Since the 5.21 dev series, we employ the same trick for NVs
2621 * if the architecture can support it (NVSIZE <= IVSIZE).
2622 */
2623
2624/* The following two macros compute the necessary offsets for the above
2625 * trick and store them in SvANY for SvIV() (and friends) to use. */
2626
82fce3d0 2627# define SET_SVANY_FOR_BODYLESS_IV(sv) \
fecef291
KW
2628 STMT_START { \
2629 SV * sv_ = MUTABLE_SV(sv); \
2630 SvANY(sv_) = (XPVIV*)((char*)&(sv_->sv_u.svu_iv) \
2631 - STRUCT_OFFSET(XPVIV, xiv_iv)); \
2632 } STMT_END
82fce3d0
DM
2633
2634# define SET_SVANY_FOR_BODYLESS_NV(sv) \
fecef291
KW
2635 STMT_START { \
2636 SV * sv_ = MUTABLE_SV(sv); \
2637 SvANY(sv_) = (XPVNV*)((char*)&(sv_->sv_u.svu_nv) \
2638 - STRUCT_OFFSET(XPVNV, xnv_u.xnv_nv)); \
2639 } STMT_END
82fce3d0
DM
2640
2641/*
14d04a33 2642 * ex: set ts=8 sts=4 sw=4 et:
102d3b8a 2643 */