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