This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
more cleanups for change#4539
[perl5.git] / sv.h
CommitLineData
a0d0e21e 1/* sv.h
79072805 2 *
4eb8286e 3 * Copyright (c) 1991-1999, Larry Wall
79072805
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
79072805
LW
8 */
9
a0d0e21e
LW
10#ifdef sv_flags
11#undef sv_flags /* Convex has this in <signal.h> for sigvec() */
12#endif
13
79072805 14typedef enum {
a0d0e21e
LW
15 SVt_NULL, /* 0 */
16 SVt_IV, /* 1 */
17 SVt_NV, /* 2 */
18 SVt_RV, /* 3 */
19 SVt_PV, /* 4 */
20 SVt_PVIV, /* 5 */
21 SVt_PVNV, /* 6 */
22 SVt_PVMG, /* 7 */
23 SVt_PVBM, /* 8 */
24 SVt_PVLV, /* 9 */
25 SVt_PVAV, /* 10 */
26 SVt_PVHV, /* 11 */
27 SVt_PVCV, /* 12 */
28 SVt_PVGV, /* 13 */
29 SVt_PVFM, /* 14 */
30 SVt_PVIO /* 15 */
79072805
LW
31} svtype;
32
79072805
LW
33/* Using C's structural equivalence to help emulate C++ inheritance here... */
34
35struct sv {
463ee0b2 36 void* sv_any; /* pointer to something */
79072805 37 U32 sv_refcnt; /* how many references to us */
8990e307 38 U32 sv_flags; /* what we are */
79072805
LW
39};
40
41struct gv {
463ee0b2 42 XPVGV* sv_any; /* pointer to something */
79072805 43 U32 sv_refcnt; /* how many references to us */
8990e307 44 U32 sv_flags; /* what we are */
79072805
LW
45};
46
47struct cv {
232e078e 48 XPVCV* sv_any; /* pointer to something */
79072805 49 U32 sv_refcnt; /* how many references to us */
8990e307 50 U32 sv_flags; /* what we are */
79072805
LW
51};
52
53struct av {
463ee0b2 54 XPVAV* sv_any; /* pointer to something */
79072805 55 U32 sv_refcnt; /* how many references to us */
8990e307 56 U32 sv_flags; /* what we are */
79072805
LW
57};
58
59struct hv {
463ee0b2 60 XPVHV* sv_any; /* pointer to something */
79072805 61 U32 sv_refcnt; /* how many references to us */
8990e307
LW
62 U32 sv_flags; /* what we are */
63};
64
65struct io {
66 XPVIO* sv_any; /* pointer to something */
67 U32 sv_refcnt; /* how many references to us */
68 U32 sv_flags; /* what we are */
79072805
LW
69};
70
463ee0b2 71#define SvANY(sv) (sv)->sv_any
79072805 72#define SvFLAGS(sv) (sv)->sv_flags
aeea060c 73#define SvREFCNT(sv) (sv)->sv_refcnt
a863c7d1 74
dce16143
MB
75#ifdef USE_THREADS
76
dce16143
MB
77# ifdef EMULATE_ATOMIC_REFCOUNTS
78# define ATOMIC_INC(count) STMT_START { \
533c011a 79 MUTEX_LOCK(&PL_svref_mutex); \
dce16143 80 ++count; \
533c011a 81 MUTEX_UNLOCK(&PL_svref_mutex); \
dce16143 82 } STMT_END
0bfcb09d 83# define ATOMIC_DEC_AND_TEST(res,count) STMT_START { \
533c011a 84 MUTEX_LOCK(&PL_svref_mutex); \
0bfcb09d 85 res = (--count == 0); \
533c011a 86 MUTEX_UNLOCK(&PL_svref_mutex); \
dce16143
MB
87 } STMT_END
88# else
89# define ATOMIC_INC(count) atomic_inc(&count)
90# define ATOMIC_DEC_AND_TEST(res,count) (res = atomic_dec_and_test(&count))
91# endif /* EMULATE_ATOMIC_REFCOUNTS */
92#else
93# define ATOMIC_INC(count) (++count)
83b0a010 94# define ATOMIC_DEC_AND_TEST(res, count) (res = (--count == 0))
dce16143
MB
95#endif /* USE_THREADS */
96
a863c7d1 97#ifdef __GNUC__
dce16143
MB
98# define SvREFCNT_inc(sv) \
99 ({ \
100 SV *nsv = (SV*)(sv); \
101 if (nsv) \
102 ATOMIC_INC(SvREFCNT(nsv)); \
103 nsv; \
104 })
8990e307 105#else
a863c7d1 106# if defined(CRIPPLED_CC) || defined(USE_THREADS)
199100c8 107# define SvREFCNT_inc(sv) sv_newref((SV*)sv)
a863c7d1 108# else
dce16143 109# define SvREFCNT_inc(sv) \
6b88bc9c 110 ((PL_Sv=(SV*)(sv)), (PL_Sv && ATOMIC_INC(SvREFCNT(PL_Sv))), (SV*)PL_Sv)
a863c7d1 111# endif
8990e307
LW
112#endif
113
a863c7d1
MB
114#define SvREFCNT_dec(sv) sv_free((SV*)sv)
115
8990e307
LW
116#define SVTYPEMASK 0xff
117#define SvTYPE(sv) ((sv)->sv_flags & SVTYPEMASK)
79072805
LW
118
119#define SvUPGRADE(sv, mt) (SvTYPE(sv) >= mt || sv_upgrade(sv, mt))
120
8990e307
LW
121#define SVs_PADBUSY 0x00000100 /* reserved for tmp or my already */
122#define SVs_PADTMP 0x00000200 /* in use as tmp */
123#define SVs_PADMY 0x00000400 /* in use a "my" variable */
124#define SVs_TEMP 0x00000800 /* string is stealable? */
125#define SVs_OBJECT 0x00001000 /* is "blessed" */
126#define SVs_GMG 0x00002000 /* has magical get method */
127#define SVs_SMG 0x00004000 /* has magical set method */
128#define SVs_RMG 0x00008000 /* has random magical methods */
129
130#define SVf_IOK 0x00010000 /* has valid public integer value */
131#define SVf_NOK 0x00020000 /* has valid public numeric value */
132#define SVf_POK 0x00040000 /* has valid public pointer value */
133#define SVf_ROK 0x00080000 /* has a valid reference pointer */
a0d0e21e 134
748a9306 135#define SVf_FAKE 0x00100000 /* glob or lexical is just a copy */
8990e307
LW
136#define SVf_OOK 0x00200000 /* has valid offset value */
137#define SVf_BREAK 0x00400000 /* refcnt is artificially low */
138#define SVf_READONLY 0x00800000 /* may not be modified */
139
6fc92669 140#define SVf_THINKFIRST (SVf_READONLY|SVf_ROK|SVf_FAKE)
a0d0e21e 141
8990e307
LW
142#define SVp_IOK 0x01000000 /* has valid non-public integer value */
143#define SVp_NOK 0x02000000 /* has valid non-public numeric value */
144#define SVp_POK 0x04000000 /* has valid non-public pointer value */
145#define SVp_SCREAM 0x08000000 /* has been studied? */
146
a0d0e21e
LW
147#define SVf_OK (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
148 SVp_IOK|SVp_NOK|SVp_POK)
149
9e7bc3e8 150#define SVf_AMAGIC 0x10000000 /* has magical overloaded methods */
a0d0e21e 151
8990e307
LW
152#define PRIVSHIFT 8
153
154/* Some private flags. */
155
77ca0c92
LW
156#define SVpad_OUR 0x80000000 /* pad name is "our" instead of "my" */
157
25da4f38
IZ
158#define SVf_IVisUV 0x80000000 /* use XPVUV instead of XPVIV */
159
160#define SVpfm_COMPILED 0x80000000 /* FORMLINE is compiled */
8990e307
LW
161
162#define SVpbm_VALID 0x80000000
bbce6d69 163#define SVpbm_TAIL 0x40000000
8990e307 164
25da4f38
IZ
165#define SVrepl_EVAL 0x40000000 /* Replacement part of s///e */
166
bf6bd887 167#define SVphv_SHAREKEYS 0x20000000 /* keys live on shared string table */
51594c39 168#define SVphv_LAZYDEL 0x40000000 /* entry in xhv_eiter must be deleted */
bf6bd887 169
810b8aa5
GS
170#define SVprv_WEAKREF 0x80000000 /* Weak reference */
171
ed6116ce
LW
172struct xrv {
173 SV * xrv_rv; /* pointer to another SV */
174};
175
79072805 176struct xpv {
ed6116ce
LW
177 char * xpv_pv; /* pointer to malloced string */
178 STRLEN xpv_cur; /* length of xpv_pv as a C string */
179 STRLEN xpv_len; /* allocated size */
79072805
LW
180};
181
182struct xpviv {
ed6116ce
LW
183 char * xpv_pv; /* pointer to malloced string */
184 STRLEN xpv_cur; /* length of xpv_pv as a C string */
185 STRLEN xpv_len; /* allocated size */
a0d0e21e 186 IV xiv_iv; /* integer value or pv offset */
79072805
LW
187};
188
ff68c719
PP
189struct xpvuv {
190 char * xpv_pv; /* pointer to malloced string */
191 STRLEN xpv_cur; /* length of xpv_pv as a C string */
192 STRLEN xpv_len; /* allocated size */
193 UV xuv_uv; /* unsigned value or pv offset */
194};
195
79072805 196struct xpvnv {
ed6116ce
LW
197 char * xpv_pv; /* pointer to malloced string */
198 STRLEN xpv_cur; /* length of xpv_pv as a C string */
199 STRLEN xpv_len; /* allocated size */
a0d0e21e 200 IV xiv_iv; /* integer value or pv offset */
65202027 201 NV xnv_nv; /* numeric value, if any */
79072805
LW
202};
203
6ee623d5 204/* These structure must match the beginning of struct xpvhv in hv.h. */
79072805 205struct xpvmg {
ed6116ce
LW
206 char * xpv_pv; /* pointer to malloced string */
207 STRLEN xpv_cur; /* length of xpv_pv as a C string */
208 STRLEN xpv_len; /* allocated size */
a0d0e21e 209 IV xiv_iv; /* integer value or pv offset */
65202027 210 NV xnv_nv; /* numeric value, if any */
79072805
LW
211 MAGIC* xmg_magic; /* linked list of magicalness */
212 HV* xmg_stash; /* class package */
213};
214
215struct xpvlv {
ed6116ce
LW
216 char * xpv_pv; /* pointer to malloced string */
217 STRLEN xpv_cur; /* length of xpv_pv as a C string */
218 STRLEN xpv_len; /* allocated size */
a0d0e21e 219 IV xiv_iv; /* integer value or pv offset */
65202027 220 NV xnv_nv; /* numeric value, if any */
79072805
LW
221 MAGIC* xmg_magic; /* linked list of magicalness */
222 HV* xmg_stash; /* class package */
8990e307 223
79072805
LW
224 STRLEN xlv_targoff;
225 STRLEN xlv_targlen;
226 SV* xlv_targ;
227 char xlv_type;
228};
229
230struct xpvgv {
ed6116ce
LW
231 char * xpv_pv; /* pointer to malloced string */
232 STRLEN xpv_cur; /* length of xpv_pv as a C string */
233 STRLEN xpv_len; /* allocated size */
a0d0e21e 234 IV xiv_iv; /* integer value or pv offset */
65202027 235 NV xnv_nv; /* numeric value, if any */
79072805
LW
236 MAGIC* xmg_magic; /* linked list of magicalness */
237 HV* xmg_stash; /* class package */
8990e307 238
79072805
LW
239 GP* xgv_gp;
240 char* xgv_name;
241 STRLEN xgv_namelen;
242 HV* xgv_stash;
a5f75d66 243 U8 xgv_flags;
79072805
LW
244};
245
246struct xpvbm {
ed6116ce
LW
247 char * xpv_pv; /* pointer to malloced string */
248 STRLEN xpv_cur; /* length of xpv_pv as a C string */
249 STRLEN xpv_len; /* allocated size */
a0d0e21e 250 IV xiv_iv; /* integer value or pv offset */
65202027 251 NV xnv_nv; /* numeric value, if any */
79072805
LW
252 MAGIC* xmg_magic; /* linked list of magicalness */
253 HV* xmg_stash; /* class package */
8990e307 254
79072805
LW
255 I32 xbm_useful; /* is this constant pattern being useful? */
256 U16 xbm_previous; /* how many characters in string before rare? */
257 U8 xbm_rare; /* rarest character in string */
258};
259
44a8e56a
PP
260/* This structure much match XPVCV */
261
77a005ab
MB
262typedef U16 cv_flags_t;
263
79072805 264struct xpvfm {
ed6116ce
LW
265 char * xpv_pv; /* pointer to malloced string */
266 STRLEN xpv_cur; /* length of xpv_pv as a C string */
267 STRLEN xpv_len; /* allocated size */
a0d0e21e 268 IV xiv_iv; /* integer value or pv offset */
65202027 269 NV xnv_nv; /* numeric value, if any */
79072805
LW
270 MAGIC* xmg_magic; /* linked list of magicalness */
271 HV* xmg_stash; /* class package */
8990e307 272
79072805
LW
273 HV * xcv_stash;
274 OP * xcv_start;
275 OP * xcv_root;
0cb96387 276 void (*xcv_xsub)(pTHXo_ CV*);
a0d0e21e
LW
277 ANY xcv_xsubany;
278 GV * xcv_gv;
b195d487
GS
279#if defined(PERL_BINCOMPAT_5005)
280 GV * xcv_filegv; /* XXX unused (and deprecated) */
281#endif
282 long xcv_depth; /* >= 2 indicates recursive call */
79072805 283 AV * xcv_padlist;
748a9306 284 CV * xcv_outside;
e858de61 285#ifdef USE_THREADS
f6d98b14 286 perl_mutex *xcv_mutexp; /* protects xcv_owner */
52e1cb5e 287 struct perl_thread *xcv_owner; /* current owner thread */
e858de61 288#endif /* USE_THREADS */
77a005ab 289 cv_flags_t xcv_flags;
44a8e56a 290
79072805
LW
291 I32 xfm_lines;
292};
293
8990e307
LW
294struct xpvio {
295 char * xpv_pv; /* pointer to malloced string */
296 STRLEN xpv_cur; /* length of xpv_pv as a C string */
297 STRLEN xpv_len; /* allocated size */
a0d0e21e 298 IV xiv_iv; /* integer value or pv offset */
65202027 299 NV xnv_nv; /* numeric value, if any */
8990e307
LW
300 MAGIC* xmg_magic; /* linked list of magicalness */
301 HV* xmg_stash; /* class package */
302
760ac839
LW
303 PerlIO * xio_ifp; /* ifp and ofp are normally the same */
304 PerlIO * xio_ofp; /* but sockets need separate streams */
8990e307
LW
305 DIR * xio_dirp; /* for opendir, readdir, etc */
306 long xio_lines; /* $. */
307 long xio_page; /* $% */
308 long xio_page_len; /* $= */
309 long xio_lines_left; /* $- */
310 char * xio_top_name; /* $^ */
311 GV * xio_top_gv; /* $^ */
312 char * xio_fmt_name; /* $~ */
313 GV * xio_fmt_gv; /* $~ */
314 char * xio_bottom_name;/* $^B */
315 GV * xio_bottom_gv; /* $^B */
316 short xio_subprocess; /* -| or |- */
317 char xio_type;
318 char xio_flags;
319};
320
e0c19803
GS
321#define IOf_ARGV 1 /* this fp iterates over ARGV */
322#define IOf_START 2 /* check for null ARGV and substitute '-' */
323#define IOf_FLUSH 4 /* this fp wants a flush after write op */
324#define IOf_DIDTOP 8 /* just did top of form */
325#define IOf_UNTAINT 16 /* consider this fp (and its data) "safe" */
326#define IOf_NOLINE 32 /* slurped a pseudo-line from empty file */
327#define IOf_FAKE_DIRP 64 /* xio_dirp is fake (source filters kludge) */
8990e307 328
ed6116ce
LW
329/* The following macros define implementation-independent predicates on SVs. */
330
79072805 331#define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
748a9306 332#define SvNIOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
a0d0e21e 333#define SvNIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
25da4f38 334 SVp_IOK|SVp_NOK|SVf_IVisUV))
79072805 335
463ee0b2 336#define SvOK(sv) (SvFLAGS(sv) & SVf_OK)
25da4f38
IZ
337#define SvOK_off(sv) (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
338 SVf_IVisUV), \
339 SvOOK_off(sv))
340#define SvOK_off_exc_UV(sv) (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC), \
a0d0e21e 341 SvOOK_off(sv))
79072805 342
8990e307
LW
343#define SvOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
344#define SvIOKp(sv) (SvFLAGS(sv) & SVp_IOK)
345#define SvIOKp_on(sv) (SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
346#define SvNOKp(sv) (SvFLAGS(sv) & SVp_NOK)
347#define SvNOKp_on(sv) (SvFLAGS(sv) |= SVp_NOK)
348#define SvPOKp(sv) (SvFLAGS(sv) & SVp_POK)
349#define SvPOKp_on(sv) (SvFLAGS(sv) |= SVp_POK)
463ee0b2 350
79072805 351#define SvIOK(sv) (SvFLAGS(sv) & SVf_IOK)
8990e307 352#define SvIOK_on(sv) (SvOOK_off(sv), \
a0d0e21e 353 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
25da4f38 354#define SvIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
827b7e14 355#define SvIOK_only(sv) (SvOK_off(sv), \
a0d0e21e 356 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
25da4f38
IZ
357#define SvIOK_only_UV(sv) (SvOK_off_exc_UV(sv), \
358 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
359
360#define SvIOK_UV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
361 == (SVf_IOK|SVf_IVisUV))
362#define SvIOK_notUV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
363 == SVf_IOK)
364
365#define SvIsUV(sv) (SvFLAGS(sv) & SVf_IVisUV)
366#define SvIsUV_on(sv) (SvFLAGS(sv) |= SVf_IVisUV)
367#define SvIsUV_off(sv) (SvFLAGS(sv) &= ~SVf_IVisUV)
79072805
LW
368
369#define SvNOK(sv) (SvFLAGS(sv) & SVf_NOK)
a0d0e21e 370#define SvNOK_on(sv) (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
8990e307
LW
371#define SvNOK_off(sv) (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
372#define SvNOK_only(sv) (SvOK_off(sv), \
a0d0e21e 373 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
79072805
LW
374
375#define SvPOK(sv) (SvFLAGS(sv) & SVf_POK)
a0d0e21e 376#define SvPOK_on(sv) (SvFLAGS(sv) |= (SVf_POK|SVp_POK))
8990e307 377#define SvPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
25da4f38 378#define SvPOK_only(sv) (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|SVf_IVisUV), \
a0d0e21e 379 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
79072805
LW
380
381#define SvOOK(sv) (SvFLAGS(sv) & SVf_OOK)
382#define SvOOK_on(sv) (SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
383#define SvOOK_off(sv) (SvOOK(sv) && sv_backoff(sv))
79072805 384
a0d0e21e
LW
385#define SvFAKE(sv) (SvFLAGS(sv) & SVf_FAKE)
386#define SvFAKE_on(sv) (SvFLAGS(sv) |= SVf_FAKE)
387#define SvFAKE_off(sv) (SvFLAGS(sv) &= ~SVf_FAKE)
388
ed6116ce 389#define SvROK(sv) (SvFLAGS(sv) & SVf_ROK)
a0d0e21e 390#define SvROK_on(sv) (SvFLAGS(sv) |= SVf_ROK)
a0d0e21e 391#define SvROK_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
79072805 392
8990e307
LW
393#define SvMAGICAL(sv) (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
394#define SvMAGICAL_on(sv) (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
395#define SvMAGICAL_off(sv) (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
79072805 396
8990e307
LW
397#define SvGMAGICAL(sv) (SvFLAGS(sv) & SVs_GMG)
398#define SvGMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_GMG)
399#define SvGMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_GMG)
ed6116ce 400
8990e307
LW
401#define SvSMAGICAL(sv) (SvFLAGS(sv) & SVs_SMG)
402#define SvSMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_SMG)
403#define SvSMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_SMG)
ed6116ce 404
8990e307
LW
405#define SvRMAGICAL(sv) (SvFLAGS(sv) & SVs_RMG)
406#define SvRMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_RMG)
407#define SvRMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_RMG)
ed6116ce 408
9e7bc3e8
JD
409#define SvAMAGIC(sv) (SvFLAGS(sv) & SVf_AMAGIC)
410#define SvAMAGIC_on(sv) (SvFLAGS(sv) |= SVf_AMAGIC)
411#define SvAMAGIC_off(sv) (SvFLAGS(sv) &= ~SVf_AMAGIC)
a0d0e21e
LW
412
413/*
414#define Gv_AMG(stash) \
415 (HV_AMAGICmb(stash) && \
416 ((!HV_AMAGICbad(stash) && HV_AMAGIC(stash)) || Gv_AMupdate(stash)))
417*/
3280af22 418#define Gv_AMG(stash) (PL_amagic_generation && Gv_AMupdate(stash))
a0d0e21e 419
810b8aa5
GS
420#define SvWEAKREF(sv) ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
421 == (SVf_ROK|SVprv_WEAKREF))
422#define SvWEAKREF_on(sv) (SvFLAGS(sv) |= (SVf_ROK|SVprv_WEAKREF))
423#define SvWEAKREF_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
424
a0d0e21e 425#define SvTHINKFIRST(sv) (SvFLAGS(sv) & SVf_THINKFIRST)
ed6116ce 426
8990e307 427#define SvPADBUSY(sv) (SvFLAGS(sv) & SVs_PADBUSY)
ed6116ce 428
8990e307
LW
429#define SvPADTMP(sv) (SvFLAGS(sv) & SVs_PADTMP)
430#define SvPADTMP_on(sv) (SvFLAGS(sv) |= SVs_PADTMP|SVs_PADBUSY)
431#define SvPADTMP_off(sv) (SvFLAGS(sv) &= ~SVs_PADTMP)
ed6116ce 432
8990e307
LW
433#define SvPADMY(sv) (SvFLAGS(sv) & SVs_PADMY)
434#define SvPADMY_on(sv) (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
ed6116ce 435
8990e307
LW
436#define SvTEMP(sv) (SvFLAGS(sv) & SVs_TEMP)
437#define SvTEMP_on(sv) (SvFLAGS(sv) |= SVs_TEMP)
438#define SvTEMP_off(sv) (SvFLAGS(sv) &= ~SVs_TEMP)
79072805 439
8990e307
LW
440#define SvOBJECT(sv) (SvFLAGS(sv) & SVs_OBJECT)
441#define SvOBJECT_on(sv) (SvFLAGS(sv) |= SVs_OBJECT)
442#define SvOBJECT_off(sv) (SvFLAGS(sv) &= ~SVs_OBJECT)
79072805 443
8990e307
LW
444#define SvREADONLY(sv) (SvFLAGS(sv) & SVf_READONLY)
445#define SvREADONLY_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
446#define SvREADONLY_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
79072805 447
8990e307
LW
448#define SvSCREAM(sv) (SvFLAGS(sv) & SVp_SCREAM)
449#define SvSCREAM_on(sv) (SvFLAGS(sv) |= SVp_SCREAM)
450#define SvSCREAM_off(sv) (SvFLAGS(sv) &= ~SVp_SCREAM)
79072805 451
8990e307
LW
452#define SvCOMPILED(sv) (SvFLAGS(sv) & SVpfm_COMPILED)
453#define SvCOMPILED_on(sv) (SvFLAGS(sv) |= SVpfm_COMPILED)
454#define SvCOMPILED_off(sv) (SvFLAGS(sv) &= ~SVpfm_COMPILED)
79072805 455
25da4f38
IZ
456#define SvEVALED(sv) (SvFLAGS(sv) & SVrepl_EVAL)
457#define SvEVALED_on(sv) (SvFLAGS(sv) |= SVrepl_EVAL)
458#define SvEVALED_off(sv) (SvFLAGS(sv) &= ~SVrepl_EVAL)
459
8990e307
LW
460#define SvTAIL(sv) (SvFLAGS(sv) & SVpbm_TAIL)
461#define SvTAIL_on(sv) (SvFLAGS(sv) |= SVpbm_TAIL)
462#define SvTAIL_off(sv) (SvFLAGS(sv) &= ~SVpbm_TAIL)
463
8990e307
LW
464#define SvVALID(sv) (SvFLAGS(sv) & SVpbm_VALID)
465#define SvVALID_on(sv) (SvFLAGS(sv) |= SVpbm_VALID)
466#define SvVALID_off(sv) (SvFLAGS(sv) &= ~SVpbm_VALID)
467
ed6116ce
LW
468#define SvRV(sv) ((XRV*) SvANY(sv))->xrv_rv
469#define SvRVx(sv) SvRV(sv)
470
463ee0b2
LW
471#define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
472#define SvIVXx(sv) SvIVX(sv)
ff68c719
PP
473#define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
474#define SvUVXx(sv) SvUVX(sv)
463ee0b2
LW
475#define SvNVX(sv) ((XPVNV*)SvANY(sv))->xnv_nv
476#define SvNVXx(sv) SvNVX(sv)
477#define SvPVX(sv) ((XPV*) SvANY(sv))->xpv_pv
478#define SvPVXx(sv) SvPVX(sv)
79072805 479#define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
79072805
LW
480#define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
481#define SvLENx(sv) SvLEN(sv)
482#define SvEND(sv)(((XPV*) SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
6b88bc9c 483#define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
79072805
LW
484#define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_magic
485#define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
486
487#define SvIV_set(sv, val) \
80b92232
PP
488 STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
489 (((XPVIV*) SvANY(sv))->xiv_iv = val); } STMT_END
79072805 490#define SvNV_set(sv, val) \
80b92232
PP
491 STMT_START { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
492 (((XPVNV*) SvANY(sv))->xnv_nv = val); } STMT_END
79072805 493#define SvPV_set(sv, val) \
80b92232
PP
494 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
495 (((XPV*) SvANY(sv))->xpv_pv = val); } STMT_END
79072805 496#define SvCUR_set(sv, val) \
80b92232
PP
497 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
498 (((XPV*) SvANY(sv))->xpv_cur = val); } STMT_END
79072805 499#define SvLEN_set(sv, val) \
80b92232
PP
500 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
501 (((XPV*) SvANY(sv))->xpv_len = val); } STMT_END
79072805 502#define SvEND_set(sv, val) \
80b92232
PP
503 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
504 (((XPV*) SvANY(sv))->xpv_cur = val - SvPVX(sv)); } STMT_END
79072805
LW
505
506#define BmRARE(sv) ((XPVBM*) SvANY(sv))->xbm_rare
507#define BmUSEFUL(sv) ((XPVBM*) SvANY(sv))->xbm_useful
508#define BmPREVIOUS(sv) ((XPVBM*) SvANY(sv))->xbm_previous
509
510#define FmLINES(sv) ((XPVFM*) SvANY(sv))->xfm_lines
511
512#define LvTYPE(sv) ((XPVLV*) SvANY(sv))->xlv_type
513#define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
514#define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
515#define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
516
8990e307
LW
517#define IoIFP(sv) ((XPVIO*) SvANY(sv))->xio_ifp
518#define IoOFP(sv) ((XPVIO*) SvANY(sv))->xio_ofp
519#define IoDIRP(sv) ((XPVIO*) SvANY(sv))->xio_dirp
520#define IoLINES(sv) ((XPVIO*) SvANY(sv))->xio_lines
521#define IoPAGE(sv) ((XPVIO*) SvANY(sv))->xio_page
522#define IoPAGE_LEN(sv) ((XPVIO*) SvANY(sv))->xio_page_len
523#define IoLINES_LEFT(sv)((XPVIO*) SvANY(sv))->xio_lines_left
524#define IoTOP_NAME(sv) ((XPVIO*) SvANY(sv))->xio_top_name
525#define IoTOP_GV(sv) ((XPVIO*) SvANY(sv))->xio_top_gv
526#define IoFMT_NAME(sv) ((XPVIO*) SvANY(sv))->xio_fmt_name
527#define IoFMT_GV(sv) ((XPVIO*) SvANY(sv))->xio_fmt_gv
528#define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
529#define IoBOTTOM_GV(sv) ((XPVIO*) SvANY(sv))->xio_bottom_gv
530#define IoSUBPROCESS(sv)((XPVIO*) SvANY(sv))->xio_subprocess
531#define IoTYPE(sv) ((XPVIO*) SvANY(sv))->xio_type
532#define IoFLAGS(sv) ((XPVIO*) SvANY(sv))->xio_flags
533
bbce6d69 534#define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv))
3280af22
NIS
535#define SvTAINTED_on(sv) STMT_START{ if(PL_tainting){sv_taint(sv);} }STMT_END
536#define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
bbce6d69 537
c6ee37c5
MB
538#define SvTAINT(sv) \
539 STMT_START { \
3280af22 540 if (PL_tainting) { \
c6ee37c5 541 dTHR; \
3280af22 542 if (PL_tainted) \
c6ee37c5
MB
543 SvTAINTED_on(sv); \
544 } \
545 } STMT_END
79072805 546
a0d0e21e 547#define SvPV_force(sv, lp) sv_pvn_force(sv, &lp)
463ee0b2 548#define SvPV(sv, lp) sv_pvn(sv, &lp)
1fa8b10d 549#define SvPV_nolen(sv) sv_pv(sv)
4e35701f
NIS
550#define SvIVx(sv) sv_iv(sv)
551#define SvUVx(sv) sv_uv(sv)
552#define SvNVx(sv) sv_nv(sv)
463ee0b2 553#define SvPVx(sv, lp) sv_pvn(sv, &lp)
a0d0e21e 554#define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
4e35701f
NIS
555#define SvTRUEx(sv) sv_true(sv)
556
557#define SvIV(sv) SvIVx(sv)
558#define SvNV(sv) SvNVx(sv)
25da4f38 559#define SvUV(sv) SvUVx(sv)
4e35701f 560#define SvTRUE(sv) SvTRUEx(sv)
79072805 561
a80f87c4
GS
562#ifndef CRIPPLED_CC
563/* redefine some things to more efficient inlined versions */
79072805 564
25da4f38 565/* Let us hope that bitmaps for UV and IV are the same */
ff68c719 566#undef SvIV
463ee0b2 567#define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
79072805 568
ff68c719
PP
569#undef SvUV
570#define SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
571
572#undef SvNV
463ee0b2 573#define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
79072805 574
ff68c719
PP
575#undef SvPV
576#define SvPV(sv, lp) \
577 (SvPOK(sv) ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv(sv, &lp))
79072805 578
ff68c719
PP
579#undef SvPV_force
580#define SvPV_force(sv, lp) \
581 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
582 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force(sv, &lp))
a0d0e21e 583
1fa8b10d
JD
584#undef SvPV_nolen
585#define SvPV_nolen(sv) \
586 (SvPOK(sv) ? SvPVX(sv) : sv_2pv_nolen(sv))
587
a80f87c4
GS
588#ifdef __GNUC__
589# undef SvIVx
590# undef SvUVx
591# undef SvNVx
592# undef SvPVx
593# undef SvTRUE
594# undef SvTRUEx
595# define SvIVx(sv) ({SV *nsv = (SV*)(sv); SvIV(nsv); })
596# define SvUVx(sv) ({SV *nsv = (SV*)(sv); SvUV(nsv); })
597# define SvNVx(sv) ({SV *nsv = (SV*)(sv); SvNV(nsv); })
598# define SvPVx(sv, lp) ({SV *nsv = (sv); SvPV(nsv, lp); })
599# define SvTRUE(sv) ( \
8990e307
LW
600 !sv \
601 ? 0 \
602 : SvPOK(sv) \
a80f87c4
GS
603 ? (({XPV *nxpv = (XPV*)SvANY(sv); \
604 nxpv && \
605 (*nxpv->xpv_pv > '0' || \
606 nxpv->xpv_cur > 1 || \
607 (nxpv->xpv_cur && *nxpv->xpv_pv != '0')); }) \
79072805
LW
608 ? 1 \
609 : 0) \
610 : \
611 SvIOK(sv) \
463ee0b2 612 ? SvIVX(sv) != 0 \
79072805 613 : SvNOK(sv) \
463ee0b2
LW
614 ? SvNVX(sv) != 0.0 \
615 : sv_2bool(sv) )
a80f87c4
GS
616# define SvTRUEx(sv) ({SV *nsv = (sv); SvTRUE(nsv); })
617#else /* __GNUC__ */
618#ifndef USE_THREADS
619/* These inlined macros use globals, which will require a thread
620 * declaration in user code, so we avoid them under threads */
621
622# undef SvIVx
623# undef SvUVx
624# undef SvNVx
625# undef SvPVx
626# undef SvTRUE
627# undef SvTRUEx
6b88bc9c
GS
628# define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
629# define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
630# define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
631# define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
a80f87c4
GS
632# define SvTRUE(sv) ( \
633 !sv \
634 ? 0 \
635 : SvPOK(sv) \
6b88bc9c
GS
636 ? ((PL_Xpv = (XPV*)SvANY(sv)) && \
637 (*PL_Xpv->xpv_pv > '0' || \
638 PL_Xpv->xpv_cur > 1 || \
639 (PL_Xpv->xpv_cur && *PL_Xpv->xpv_pv != '0')) \
a80f87c4
GS
640 ? 1 \
641 : 0) \
642 : \
643 SvIOK(sv) \
644 ? SvIVX(sv) != 0 \
645 : SvNOK(sv) \
646 ? SvNVX(sv) != 0.0 \
647 : sv_2bool(sv) )
6b88bc9c 648# define SvTRUEx(sv) ((PL_Sv = (sv)), SvTRUE(PL_Sv))
a80f87c4
GS
649#endif /* !USE_THREADS */
650#endif /* !__GNU__ */
651#endif /* !CRIPPLED_CC */
79072805 652
5f05dabc 653#define newRV_inc(sv) newRV(sv)
5f05dabc 654
ef50df4b 655/* the following macros update any magic values this sv is associated with */
79072805 656
189b2af5
GS
657#define SvGETMAGIC(x) STMT_START { if (SvGMAGICAL(x)) mg_get(x); } STMT_END
658#define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
79072805 659
54310121 660#define SvSetSV_and(dst,src,finally) \
189b2af5 661 STMT_START { \
54310121
PP
662 if ((dst) != (src)) { \
663 sv_setsv(dst, src); \
664 finally; \
189b2af5
GS
665 } \
666 } STMT_END
54310121 667#define SvSetSV_nosteal_and(dst,src,finally) \
189b2af5 668 STMT_START { \
8ebc5c01
PP
669 if ((dst) != (src)) { \
670 U32 tMpF = SvFLAGS(src) & SVs_TEMP; \
671 SvTEMP_off(src); \
672 sv_setsv(dst, src); \
673 SvFLAGS(src) |= tMpF; \
54310121 674 finally; \
189b2af5
GS
675 } \
676 } STMT_END
79072805 677
54310121 678#define SvSetSV(dst,src) \
c9d716f7 679 SvSetSV_and(dst,src,/*nothing*/;)
54310121 680#define SvSetSV_nosteal(dst,src) \
c9d716f7 681 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
54310121
PP
682
683#define SvSetMagicSV(dst,src) \
684 SvSetSV_and(dst,src,SvSETMAGIC(dst))
685#define SvSetMagicSV_nosteal(dst,src) \
686 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
687
3967c732 688#ifdef DEBUGGING
79072805 689#define SvPEEK(sv) sv_peek(sv)
3967c732
JD
690#else
691#define SvPEEK(sv) ""
692#endif
79072805 693
3280af22 694#define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no)
36477c24 695
3280af22 696#define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
54310121 697
79072805
LW
698#define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
699
933fea7f
GS
700#define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
701#define Sv_Grow sv_grow