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