This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
integrate ansi branch to get s/foo/PL_foo/ changes
[perl5.git] / sv.h
CommitLineData
a0d0e21e 1/* sv.h
79072805 2 *
9607fc9c 3 * Copyright (c) 1991-1997, 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
MB
109# define SvREFCNT_inc(sv) \
110 ((Sv=(SV*)(sv)), (Sv && ATOMIC_INC(SvREFCNT(Sv))), (SV*)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
a0d0e21e
LW
140#define SVf_THINKFIRST (SVf_READONLY|SVf_ROK)
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
150#ifdef OVERLOAD
151#define SVf_AMAGIC 0x10000000 /* has magical overloaded methods */
827b7e14
GA
152#else
153#define SVf_AMAGIC 0 /* can be or-ed without effect */
a0d0e21e
LW
154#endif /* OVERLOAD */
155
8990e307
LW
156#define PRIVSHIFT 8
157
158/* Some private flags. */
159
160#define SVpfm_COMPILED 0x80000000
161
162#define SVpbm_VALID 0x80000000
bbce6d69 163#define SVpbm_TAIL 0x40000000
8990e307 164
bf6bd887 165#define SVphv_SHAREKEYS 0x20000000 /* keys live on shared string table */
51594c39 166#define SVphv_LAZYDEL 0x40000000 /* entry in xhv_eiter must be deleted */
bf6bd887 167
ed6116ce
LW
168struct xrv {
169 SV * xrv_rv; /* pointer to another SV */
170};
171
79072805 172struct xpv {
ed6116ce
LW
173 char * xpv_pv; /* pointer to malloced string */
174 STRLEN xpv_cur; /* length of xpv_pv as a C string */
175 STRLEN xpv_len; /* allocated size */
79072805
LW
176};
177
178struct xpviv {
ed6116ce
LW
179 char * xpv_pv; /* pointer to malloced string */
180 STRLEN xpv_cur; /* length of xpv_pv as a C string */
181 STRLEN xpv_len; /* allocated size */
a0d0e21e 182 IV xiv_iv; /* integer value or pv offset */
79072805
LW
183};
184
ff68c719
PP
185struct xpvuv {
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 */
189 UV xuv_uv; /* unsigned value or pv offset */
190};
191
79072805 192struct xpvnv {
ed6116ce
LW
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 */
a0d0e21e 196 IV xiv_iv; /* integer value or pv offset */
ed6116ce 197 double xnv_nv; /* numeric value, if any */
79072805
LW
198};
199
6ee623d5 200/* These structure must match the beginning of struct xpvhv in hv.h. */
79072805 201struct xpvmg {
ed6116ce
LW
202 char * xpv_pv; /* pointer to malloced string */
203 STRLEN xpv_cur; /* length of xpv_pv as a C string */
204 STRLEN xpv_len; /* allocated size */
a0d0e21e 205 IV xiv_iv; /* integer value or pv offset */
ed6116ce 206 double xnv_nv; /* numeric value, if any */
79072805
LW
207 MAGIC* xmg_magic; /* linked list of magicalness */
208 HV* xmg_stash; /* class package */
209};
210
211struct xpvlv {
ed6116ce
LW
212 char * xpv_pv; /* pointer to malloced string */
213 STRLEN xpv_cur; /* length of xpv_pv as a C string */
214 STRLEN xpv_len; /* allocated size */
a0d0e21e 215 IV xiv_iv; /* integer value or pv offset */
ed6116ce 216 double xnv_nv; /* numeric value, if any */
79072805
LW
217 MAGIC* xmg_magic; /* linked list of magicalness */
218 HV* xmg_stash; /* class package */
8990e307 219
79072805
LW
220 STRLEN xlv_targoff;
221 STRLEN xlv_targlen;
222 SV* xlv_targ;
223 char xlv_type;
224};
225
226struct xpvgv {
ed6116ce
LW
227 char * xpv_pv; /* pointer to malloced string */
228 STRLEN xpv_cur; /* length of xpv_pv as a C string */
229 STRLEN xpv_len; /* allocated size */
a0d0e21e 230 IV xiv_iv; /* integer value or pv offset */
ed6116ce 231 double xnv_nv; /* numeric value, if any */
79072805
LW
232 MAGIC* xmg_magic; /* linked list of magicalness */
233 HV* xmg_stash; /* class package */
8990e307 234
79072805
LW
235 GP* xgv_gp;
236 char* xgv_name;
237 STRLEN xgv_namelen;
238 HV* xgv_stash;
a5f75d66 239 U8 xgv_flags;
79072805
LW
240};
241
242struct xpvbm {
ed6116ce
LW
243 char * xpv_pv; /* pointer to malloced string */
244 STRLEN xpv_cur; /* length of xpv_pv as a C string */
245 STRLEN xpv_len; /* allocated size */
a0d0e21e 246 IV xiv_iv; /* integer value or pv offset */
ed6116ce 247 double xnv_nv; /* numeric value, if any */
79072805
LW
248 MAGIC* xmg_magic; /* linked list of magicalness */
249 HV* xmg_stash; /* class package */
8990e307 250
79072805
LW
251 I32 xbm_useful; /* is this constant pattern being useful? */
252 U16 xbm_previous; /* how many characters in string before rare? */
253 U8 xbm_rare; /* rarest character in string */
254};
255
44a8e56a
PP
256/* This structure much match XPVCV */
257
77a005ab
MB
258typedef U16 cv_flags_t;
259
79072805 260struct xpvfm {
ed6116ce
LW
261 char * xpv_pv; /* pointer to malloced string */
262 STRLEN xpv_cur; /* length of xpv_pv as a C string */
263 STRLEN xpv_len; /* allocated size */
a0d0e21e 264 IV xiv_iv; /* integer value or pv offset */
ed6116ce 265 double xnv_nv; /* numeric value, if any */
79072805
LW
266 MAGIC* xmg_magic; /* linked list of magicalness */
267 HV* xmg_stash; /* class package */
8990e307 268
79072805
LW
269 HV * xcv_stash;
270 OP * xcv_start;
271 OP * xcv_root;
e3b8966e 272 void (*xcv_xsub)_((CV* _CPERLproto));
a0d0e21e
LW
273 ANY xcv_xsubany;
274 GV * xcv_gv;
79072805
LW
275 GV * xcv_filegv;
276 long xcv_depth; /* >= 2 indicates recursive call */
277 AV * xcv_padlist;
748a9306 278 CV * xcv_outside;
e858de61 279#ifdef USE_THREADS
f6d98b14 280 perl_mutex *xcv_mutexp; /* protects xcv_owner */
52e1cb5e 281 struct perl_thread *xcv_owner; /* current owner thread */
e858de61 282#endif /* USE_THREADS */
77a005ab 283 cv_flags_t xcv_flags;
44a8e56a 284
79072805
LW
285 I32 xfm_lines;
286};
287
8990e307
LW
288struct xpvio {
289 char * xpv_pv; /* pointer to malloced string */
290 STRLEN xpv_cur; /* length of xpv_pv as a C string */
291 STRLEN xpv_len; /* allocated size */
a0d0e21e 292 IV xiv_iv; /* integer value or pv offset */
8990e307
LW
293 double xnv_nv; /* numeric value, if any */
294 MAGIC* xmg_magic; /* linked list of magicalness */
295 HV* xmg_stash; /* class package */
296
760ac839
LW
297 PerlIO * xio_ifp; /* ifp and ofp are normally the same */
298 PerlIO * xio_ofp; /* but sockets need separate streams */
8990e307
LW
299 DIR * xio_dirp; /* for opendir, readdir, etc */
300 long xio_lines; /* $. */
301 long xio_page; /* $% */
302 long xio_page_len; /* $= */
303 long xio_lines_left; /* $- */
304 char * xio_top_name; /* $^ */
305 GV * xio_top_gv; /* $^ */
306 char * xio_fmt_name; /* $~ */
307 GV * xio_fmt_gv; /* $~ */
308 char * xio_bottom_name;/* $^B */
309 GV * xio_bottom_gv; /* $^B */
310 short xio_subprocess; /* -| or |- */
311 char xio_type;
312 char xio_flags;
313};
314
315#define IOf_ARGV 1 /* this fp iterates over ARGV */
316#define IOf_START 2 /* check for null ARGV and substitute '-' */
317#define IOf_FLUSH 4 /* this fp wants a flush after write op */
748a9306 318#define IOf_DIDTOP 8 /* just did top of form */
51594c39 319#define IOf_UNTAINT 16 /* consider this fp (and it's data) "safe" */
8990e307 320
ed6116ce
LW
321/* The following macros define implementation-independent predicates on SVs. */
322
79072805 323#define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
748a9306 324#define SvNIOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
a0d0e21e
LW
325#define SvNIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
326 SVp_IOK|SVp_NOK))
79072805 327
463ee0b2 328#define SvOK(sv) (SvFLAGS(sv) & SVf_OK)
a0d0e21e
LW
329#define SvOK_off(sv) (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC), \
330 SvOOK_off(sv))
79072805 331
8990e307
LW
332#define SvOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
333#define SvIOKp(sv) (SvFLAGS(sv) & SVp_IOK)
334#define SvIOKp_on(sv) (SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
335#define SvNOKp(sv) (SvFLAGS(sv) & SVp_NOK)
336#define SvNOKp_on(sv) (SvFLAGS(sv) |= SVp_NOK)
337#define SvPOKp(sv) (SvFLAGS(sv) & SVp_POK)
338#define SvPOKp_on(sv) (SvFLAGS(sv) |= SVp_POK)
463ee0b2 339
79072805 340#define SvIOK(sv) (SvFLAGS(sv) & SVf_IOK)
8990e307 341#define SvIOK_on(sv) (SvOOK_off(sv), \
a0d0e21e 342 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
8990e307 343#define SvIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK))
827b7e14 344#define SvIOK_only(sv) (SvOK_off(sv), \
a0d0e21e 345 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
79072805
LW
346
347#define SvNOK(sv) (SvFLAGS(sv) & SVf_NOK)
a0d0e21e 348#define SvNOK_on(sv) (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
8990e307
LW
349#define SvNOK_off(sv) (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
350#define SvNOK_only(sv) (SvOK_off(sv), \
a0d0e21e 351 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
79072805
LW
352
353#define SvPOK(sv) (SvFLAGS(sv) & SVf_POK)
a0d0e21e 354#define SvPOK_on(sv) (SvFLAGS(sv) |= (SVf_POK|SVp_POK))
8990e307 355#define SvPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
827b7e14 356#define SvPOK_only(sv) (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC), \
a0d0e21e 357 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
79072805
LW
358
359#define SvOOK(sv) (SvFLAGS(sv) & SVf_OOK)
360#define SvOOK_on(sv) (SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
361#define SvOOK_off(sv) (SvOOK(sv) && sv_backoff(sv))
79072805 362
a0d0e21e
LW
363#define SvFAKE(sv) (SvFLAGS(sv) & SVf_FAKE)
364#define SvFAKE_on(sv) (SvFLAGS(sv) |= SVf_FAKE)
365#define SvFAKE_off(sv) (SvFLAGS(sv) &= ~SVf_FAKE)
366
ed6116ce 367#define SvROK(sv) (SvFLAGS(sv) & SVf_ROK)
a0d0e21e 368#define SvROK_on(sv) (SvFLAGS(sv) |= SVf_ROK)
a0d0e21e 369#define SvROK_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
79072805 370
8990e307
LW
371#define SvMAGICAL(sv) (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
372#define SvMAGICAL_on(sv) (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
373#define SvMAGICAL_off(sv) (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
79072805 374
8990e307
LW
375#define SvGMAGICAL(sv) (SvFLAGS(sv) & SVs_GMG)
376#define SvGMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_GMG)
377#define SvGMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_GMG)
ed6116ce 378
8990e307
LW
379#define SvSMAGICAL(sv) (SvFLAGS(sv) & SVs_SMG)
380#define SvSMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_SMG)
381#define SvSMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_SMG)
ed6116ce 382
8990e307
LW
383#define SvRMAGICAL(sv) (SvFLAGS(sv) & SVs_RMG)
384#define SvRMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_RMG)
385#define SvRMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_RMG)
ed6116ce 386
a0d0e21e
LW
387#ifdef OVERLOAD
388#define SvAMAGIC(sv) (SvFLAGS(sv) & SVf_AMAGIC)
389#define SvAMAGIC_on(sv) (SvFLAGS(sv) |= SVf_AMAGIC)
390#define SvAMAGIC_off(sv) (SvFLAGS(sv) &= ~SVf_AMAGIC)
391
392/*
393#define Gv_AMG(stash) \
394 (HV_AMAGICmb(stash) && \
395 ((!HV_AMAGICbad(stash) && HV_AMAGIC(stash)) || Gv_AMupdate(stash)))
396*/
3280af22 397#define Gv_AMG(stash) (PL_amagic_generation && Gv_AMupdate(stash))
a0d0e21e
LW
398#endif /* OVERLOAD */
399
400#define SvTHINKFIRST(sv) (SvFLAGS(sv) & SVf_THINKFIRST)
ed6116ce 401
8990e307 402#define SvPADBUSY(sv) (SvFLAGS(sv) & SVs_PADBUSY)
ed6116ce 403
8990e307
LW
404#define SvPADTMP(sv) (SvFLAGS(sv) & SVs_PADTMP)
405#define SvPADTMP_on(sv) (SvFLAGS(sv) |= SVs_PADTMP|SVs_PADBUSY)
406#define SvPADTMP_off(sv) (SvFLAGS(sv) &= ~SVs_PADTMP)
ed6116ce 407
8990e307
LW
408#define SvPADMY(sv) (SvFLAGS(sv) & SVs_PADMY)
409#define SvPADMY_on(sv) (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
ed6116ce 410
8990e307
LW
411#define SvTEMP(sv) (SvFLAGS(sv) & SVs_TEMP)
412#define SvTEMP_on(sv) (SvFLAGS(sv) |= SVs_TEMP)
413#define SvTEMP_off(sv) (SvFLAGS(sv) &= ~SVs_TEMP)
79072805 414
8990e307
LW
415#define SvOBJECT(sv) (SvFLAGS(sv) & SVs_OBJECT)
416#define SvOBJECT_on(sv) (SvFLAGS(sv) |= SVs_OBJECT)
417#define SvOBJECT_off(sv) (SvFLAGS(sv) &= ~SVs_OBJECT)
79072805 418
8990e307
LW
419#define SvREADONLY(sv) (SvFLAGS(sv) & SVf_READONLY)
420#define SvREADONLY_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
421#define SvREADONLY_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
79072805 422
8990e307
LW
423#define SvSCREAM(sv) (SvFLAGS(sv) & SVp_SCREAM)
424#define SvSCREAM_on(sv) (SvFLAGS(sv) |= SVp_SCREAM)
425#define SvSCREAM_off(sv) (SvFLAGS(sv) &= ~SVp_SCREAM)
79072805 426
8990e307
LW
427#define SvCOMPILED(sv) (SvFLAGS(sv) & SVpfm_COMPILED)
428#define SvCOMPILED_on(sv) (SvFLAGS(sv) |= SVpfm_COMPILED)
429#define SvCOMPILED_off(sv) (SvFLAGS(sv) &= ~SVpfm_COMPILED)
79072805 430
8990e307
LW
431#define SvTAIL(sv) (SvFLAGS(sv) & SVpbm_TAIL)
432#define SvTAIL_on(sv) (SvFLAGS(sv) |= SVpbm_TAIL)
433#define SvTAIL_off(sv) (SvFLAGS(sv) &= ~SVpbm_TAIL)
434
8990e307
LW
435#define SvVALID(sv) (SvFLAGS(sv) & SVpbm_VALID)
436#define SvVALID_on(sv) (SvFLAGS(sv) |= SVpbm_VALID)
437#define SvVALID_off(sv) (SvFLAGS(sv) &= ~SVpbm_VALID)
438
ed6116ce
LW
439#define SvRV(sv) ((XRV*) SvANY(sv))->xrv_rv
440#define SvRVx(sv) SvRV(sv)
441
463ee0b2
LW
442#define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
443#define SvIVXx(sv) SvIVX(sv)
ff68c719
PP
444#define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
445#define SvUVXx(sv) SvUVX(sv)
463ee0b2
LW
446#define SvNVX(sv) ((XPVNV*)SvANY(sv))->xnv_nv
447#define SvNVXx(sv) SvNVX(sv)
448#define SvPVX(sv) ((XPV*) SvANY(sv))->xpv_pv
449#define SvPVXx(sv) SvPVX(sv)
79072805 450#define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
79072805
LW
451#define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
452#define SvLENx(sv) SvLEN(sv)
453#define SvEND(sv)(((XPV*) SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
8990e307 454#define SvENDx(sv) ((Sv = (sv)), SvEND(Sv))
79072805
LW
455#define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_magic
456#define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
457
458#define SvIV_set(sv, val) \
80b92232
PP
459 STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
460 (((XPVIV*) SvANY(sv))->xiv_iv = val); } STMT_END
79072805 461#define SvNV_set(sv, val) \
80b92232
PP
462 STMT_START { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
463 (((XPVNV*) SvANY(sv))->xnv_nv = val); } STMT_END
79072805 464#define SvPV_set(sv, val) \
80b92232
PP
465 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
466 (((XPV*) SvANY(sv))->xpv_pv = val); } STMT_END
79072805 467#define SvCUR_set(sv, val) \
80b92232
PP
468 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
469 (((XPV*) SvANY(sv))->xpv_cur = val); } STMT_END
79072805 470#define SvLEN_set(sv, val) \
80b92232
PP
471 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
472 (((XPV*) SvANY(sv))->xpv_len = val); } STMT_END
79072805 473#define SvEND_set(sv, val) \
80b92232
PP
474 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
475 (((XPV*) SvANY(sv))->xpv_cur = val - SvPVX(sv)); } STMT_END
79072805
LW
476
477#define BmRARE(sv) ((XPVBM*) SvANY(sv))->xbm_rare
478#define BmUSEFUL(sv) ((XPVBM*) SvANY(sv))->xbm_useful
479#define BmPREVIOUS(sv) ((XPVBM*) SvANY(sv))->xbm_previous
480
481#define FmLINES(sv) ((XPVFM*) SvANY(sv))->xfm_lines
482
483#define LvTYPE(sv) ((XPVLV*) SvANY(sv))->xlv_type
484#define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
485#define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
486#define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
487
8990e307
LW
488#define IoIFP(sv) ((XPVIO*) SvANY(sv))->xio_ifp
489#define IoOFP(sv) ((XPVIO*) SvANY(sv))->xio_ofp
490#define IoDIRP(sv) ((XPVIO*) SvANY(sv))->xio_dirp
491#define IoLINES(sv) ((XPVIO*) SvANY(sv))->xio_lines
492#define IoPAGE(sv) ((XPVIO*) SvANY(sv))->xio_page
493#define IoPAGE_LEN(sv) ((XPVIO*) SvANY(sv))->xio_page_len
494#define IoLINES_LEFT(sv)((XPVIO*) SvANY(sv))->xio_lines_left
495#define IoTOP_NAME(sv) ((XPVIO*) SvANY(sv))->xio_top_name
496#define IoTOP_GV(sv) ((XPVIO*) SvANY(sv))->xio_top_gv
497#define IoFMT_NAME(sv) ((XPVIO*) SvANY(sv))->xio_fmt_name
498#define IoFMT_GV(sv) ((XPVIO*) SvANY(sv))->xio_fmt_gv
499#define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
500#define IoBOTTOM_GV(sv) ((XPVIO*) SvANY(sv))->xio_bottom_gv
501#define IoSUBPROCESS(sv)((XPVIO*) SvANY(sv))->xio_subprocess
502#define IoTYPE(sv) ((XPVIO*) SvANY(sv))->xio_type
503#define IoFLAGS(sv) ((XPVIO*) SvANY(sv))->xio_flags
504
bbce6d69 505#define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv))
3280af22
NIS
506#define SvTAINTED_on(sv) STMT_START{ if(PL_tainting){sv_taint(sv);} }STMT_END
507#define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
bbce6d69 508
c6ee37c5
MB
509#define SvTAINT(sv) \
510 STMT_START { \
3280af22 511 if (PL_tainting) { \
c6ee37c5 512 dTHR; \
3280af22 513 if (PL_tainted) \
c6ee37c5
MB
514 SvTAINTED_on(sv); \
515 } \
516 } STMT_END
79072805 517
a0d0e21e 518#define SvPV_force(sv, lp) sv_pvn_force(sv, &lp)
463ee0b2 519#define SvPV(sv, lp) sv_pvn(sv, &lp)
4e35701f
NIS
520#define SvIVx(sv) sv_iv(sv)
521#define SvUVx(sv) sv_uv(sv)
522#define SvNVx(sv) sv_nv(sv)
463ee0b2 523#define SvPVx(sv, lp) sv_pvn(sv, &lp)
a0d0e21e 524#define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
4e35701f
NIS
525#define SvTRUEx(sv) sv_true(sv)
526
527#define SvIV(sv) SvIVx(sv)
528#define SvNV(sv) SvNVx(sv)
529#define SvUV(sv) SvIVx(sv)
530#define SvTRUE(sv) SvTRUEx(sv)
79072805 531
a80f87c4
GS
532#ifndef CRIPPLED_CC
533/* redefine some things to more efficient inlined versions */
79072805 534
ff68c719 535#undef SvIV
463ee0b2 536#define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
79072805 537
ff68c719
PP
538#undef SvUV
539#define SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
540
541#undef SvNV
463ee0b2 542#define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
79072805 543
ff68c719
PP
544#undef SvPV
545#define SvPV(sv, lp) \
546 (SvPOK(sv) ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv(sv, &lp))
79072805 547
ff68c719
PP
548#undef SvPV_force
549#define SvPV_force(sv, lp) \
550 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
551 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force(sv, &lp))
a0d0e21e 552
a80f87c4
GS
553#ifdef __GNUC__
554# undef SvIVx
555# undef SvUVx
556# undef SvNVx
557# undef SvPVx
558# undef SvTRUE
559# undef SvTRUEx
560# define SvIVx(sv) ({SV *nsv = (SV*)(sv); SvIV(nsv); })
561# define SvUVx(sv) ({SV *nsv = (SV*)(sv); SvUV(nsv); })
562# define SvNVx(sv) ({SV *nsv = (SV*)(sv); SvNV(nsv); })
563# define SvPVx(sv, lp) ({SV *nsv = (sv); SvPV(nsv, lp); })
564# define SvTRUE(sv) ( \
8990e307
LW
565 !sv \
566 ? 0 \
567 : SvPOK(sv) \
a80f87c4
GS
568 ? (({XPV *nxpv = (XPV*)SvANY(sv); \
569 nxpv && \
570 (*nxpv->xpv_pv > '0' || \
571 nxpv->xpv_cur > 1 || \
572 (nxpv->xpv_cur && *nxpv->xpv_pv != '0')); }) \
79072805
LW
573 ? 1 \
574 : 0) \
575 : \
576 SvIOK(sv) \
463ee0b2 577 ? SvIVX(sv) != 0 \
79072805 578 : SvNOK(sv) \
463ee0b2
LW
579 ? SvNVX(sv) != 0.0 \
580 : sv_2bool(sv) )
a80f87c4
GS
581# define SvTRUEx(sv) ({SV *nsv = (sv); SvTRUE(nsv); })
582#else /* __GNUC__ */
583#ifndef USE_THREADS
584/* These inlined macros use globals, which will require a thread
585 * declaration in user code, so we avoid them under threads */
586
587# undef SvIVx
588# undef SvUVx
589# undef SvNVx
590# undef SvPVx
591# undef SvTRUE
592# undef SvTRUEx
aeea060c
NIS
593# define SvIVx(sv) ((Sv = (sv)), SvIV(Sv))
594# define SvUVx(sv) ((Sv = (sv)), SvUV(Sv))
595# define SvNVx(sv) ((Sv = (sv)), SvNV(Sv))
596# define SvPVx(sv, lp) ((Sv = (sv)), SvPV(Sv, lp))
a80f87c4
GS
597# define SvTRUE(sv) ( \
598 !sv \
599 ? 0 \
600 : SvPOK(sv) \
601 ? ((Xpv = (XPV*)SvANY(sv)) && \
602 (*Xpv->xpv_pv > '0' || \
603 Xpv->xpv_cur > 1 || \
604 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')) \
605 ? 1 \
606 : 0) \
607 : \
608 SvIOK(sv) \
609 ? SvIVX(sv) != 0 \
610 : SvNOK(sv) \
611 ? SvNVX(sv) != 0.0 \
612 : sv_2bool(sv) )
613# define SvTRUEx(sv) ((Sv = (sv)), SvTRUE(Sv))
614#endif /* !USE_THREADS */
615#endif /* !__GNU__ */
616#endif /* !CRIPPLED_CC */
79072805 617
5f05dabc 618#define newRV_inc(sv) newRV(sv)
5f05dabc 619
ef50df4b 620/* the following macros update any magic values this sv is associated with */
79072805 621
189b2af5
GS
622#define SvGETMAGIC(x) STMT_START { if (SvGMAGICAL(x)) mg_get(x); } STMT_END
623#define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
79072805 624
54310121 625#define SvSetSV_and(dst,src,finally) \
189b2af5 626 STMT_START { \
54310121
PP
627 if ((dst) != (src)) { \
628 sv_setsv(dst, src); \
629 finally; \
189b2af5
GS
630 } \
631 } STMT_END
54310121 632#define SvSetSV_nosteal_and(dst,src,finally) \
189b2af5 633 STMT_START { \
8ebc5c01
PP
634 if ((dst) != (src)) { \
635 U32 tMpF = SvFLAGS(src) & SVs_TEMP; \
636 SvTEMP_off(src); \
637 sv_setsv(dst, src); \
638 SvFLAGS(src) |= tMpF; \
54310121 639 finally; \
189b2af5
GS
640 } \
641 } STMT_END
79072805 642
54310121 643#define SvSetSV(dst,src) \
c9d716f7 644 SvSetSV_and(dst,src,/*nothing*/;)
54310121 645#define SvSetSV_nosteal(dst,src) \
c9d716f7 646 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
54310121
PP
647
648#define SvSetMagicSV(dst,src) \
649 SvSetSV_and(dst,src,SvSETMAGIC(dst))
650#define SvSetMagicSV_nosteal(dst,src) \
651 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
652
79072805
LW
653#define SvPEEK(sv) sv_peek(sv)
654
3280af22 655#define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no)
36477c24 656
3280af22 657#define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
54310121 658
79072805
LW
659#define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
660
79072805 661#ifndef DOSISH
a0d0e21e 662# define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
79072805
LW
663# define Sv_Grow sv_grow
664#else
665 /* extra parentheses intentionally NOT placed around "len"! */
a0d0e21e
LW
666# define SvGROW(sv,len) ((SvLEN(sv) < (unsigned long)len) \
667 ? sv_grow(sv,(unsigned long)len) : SvPVX(sv))
79072805
LW
668# define Sv_Grow(sv,len) sv_grow(sv,(unsigned long)(len))
669#endif /* DOSISH */