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