This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl5a5:pat/inherit.pat
[perl5.git] / sv.h
CommitLineData
79072805
LW
1/* $RCSfile: sv.h,v $$Revision: 4.1 $$Date: 92/08/07 18:26:57 $
2 *
3 * Copyright (c) 1991, Larry Wall
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 *
8 * $Log: sv.h,v $
9 * Revision 4.1 92/08/07 18:26:57 lwall
10 *
11 * Revision 4.0.1.4 92/06/08 15:41:45 lwall
12 * patch20: fixed confusion between a *var's real name and its effective name
13 * patch20: removed implicit int declarations on functions
14 *
15 * Revision 4.0.1.3 91/11/05 18:41:47 lwall
16 * patch11: random cleanup
17 * patch11: solitary subroutine references no longer trigger typo warnings
18 *
19 * Revision 4.0.1.2 91/06/07 11:58:33 lwall
20 * patch4: new copyright notice
21 *
22 * Revision 4.0.1.1 91/04/12 09:16:12 lwall
23 * patch1: you may now use "die" and "caller" in a signal handler
24 *
25 * Revision 4.0 91/03/20 01:40:04 lwall
26 * 4.0 baseline.
27 *
28 */
29
30typedef enum {
31 SVt_NULL,
32 SVt_REF,
33 SVt_IV,
34 SVt_NV,
35 SVt_PV,
36 SVt_PVIV,
37 SVt_PVNV,
38 SVt_PVMG,
39 SVt_PVLV,
40 SVt_PVAV,
41 SVt_PVHV,
42 SVt_PVCV,
43 SVt_PVGV,
44 SVt_PVBM,
45 SVt_PVFM,
46} svtype;
47
48/* Compensate for ANSI C misdesign... */
49#ifdef DEBUGGING
50#define SVTYPE svtype
51#else
52#define SVTYPE U8
53#endif
54
55/* Using C's structural equivalence to help emulate C++ inheritance here... */
56
57struct sv {
463ee0b2 58 void* sv_any; /* pointer to something */
79072805
LW
59 U32 sv_refcnt; /* how many references to us */
60 SVTYPE sv_type; /* what sort of thing pointer points to */
61 U8 sv_flags; /* extra flags, some depending on type */
62 U8 sv_storage; /* storage class */
63 U8 sv_private; /* extra value, depending on type */
64};
65
66struct gv {
463ee0b2 67 XPVGV* sv_any; /* pointer to something */
79072805
LW
68 U32 sv_refcnt; /* how many references to us */
69 SVTYPE sv_type; /* what sort of thing pointer points to */
70 U8 sv_flags; /* extra flags, some depending on type */
71 U8 sv_storage; /* storage class */
72 U8 sv_private; /* extra value, depending on type */
73};
74
75struct cv {
463ee0b2 76 XPVGV* sv_any; /* pointer to something */
79072805
LW
77 U32 sv_refcnt; /* how many references to us */
78 SVTYPE sv_type; /* what sort of thing pointer points to */
79 U8 sv_flags; /* extra flags, some depending on type */
80 U8 sv_storage; /* storage class */
81 U8 sv_private; /* extra value, depending on type */
82};
83
84struct av {
463ee0b2 85 XPVAV* sv_any; /* pointer to something */
79072805
LW
86 U32 sv_refcnt; /* how many references to us */
87 SVTYPE sv_type; /* what sort of thing pointer points to */
88 U8 sv_flags; /* extra flags, some depending on type */
89 U8 sv_storage; /* storage class */
90 U8 sv_private; /* extra value, depending on type */
91};
92
93struct hv {
463ee0b2 94 XPVHV* sv_any; /* pointer to something */
79072805
LW
95 U32 sv_refcnt; /* how many references to us */
96 SVTYPE sv_type; /* what sort of thing pointer points to */
97 U8 sv_flags; /* extra flags, some depending on type */
98 U8 sv_storage; /* storage class */
99 U8 sv_private; /* extra value, depending on type */
100};
101
463ee0b2 102#define SvANY(sv) (sv)->sv_any
79072805
LW
103#define SvTYPE(sv) (sv)->sv_type
104#define SvREFCNT(sv) (sv)->sv_refcnt
105#define SvFLAGS(sv) (sv)->sv_flags
106#define SvSTORAGE(sv) (sv)->sv_storage
107#define SvPRIVATE(sv) (sv)->sv_private
108
109#define SvUPGRADE(sv, mt) (SvTYPE(sv) >= mt || sv_upgrade(sv, mt))
110
111#define SVf_IOK 1 /* has valid integer value */
112#define SVf_NOK 2 /* has valid numeric value */
113#define SVf_POK 4 /* has valid pointer value */
114#define SVf_OOK 8 /* has valid offset value */
115#define SVf_MAGICAL 16 /* has special methods */
463ee0b2 116#define SVf_OK 32 /* has defined value */
79072805
LW
117#define SVf_TEMP 64 /* eventually in sv_private? */
118#define SVf_READONLY 128 /* may not be modified */
119
463ee0b2
LW
120#define SVp_IOK 1 /* has valid non-public integer value */
121#define SVp_NOK 2 /* has valid non-public numeric value */
122#define SVp_POK 4 /* has valid non-public pointer value */
123#define SVp_SCREAM 8 /* has been studied? */
124#define SVp_TAINTEDDIR 16 /* PATH component is a security risk */
79072805 125
463ee0b2 126#define SVpfm_COMPILED 128
79072805 127
463ee0b2
LW
128#define SVpbm_VALID 128
129#define SVpbm_CASEFOLD 64
130#define SVpbm_TAIL 32
79072805 131
463ee0b2 132#define SVpgv_MULTI 128
79072805
LW
133
134struct xpv {
135 char * xpv_pv; /* pointer to malloced string */
136 STRLEN xpv_cur; /* length of xpv_pv as a C string */
137 STRLEN xpv_len; /* allocated size */
138};
139
140struct xpviv {
141 char * xpv_pv; /* pointer to malloced string */
142 STRLEN xpv_cur; /* length of xpv_pv as a C string */
143 STRLEN xpv_len; /* allocated size */
144 I32 xiv_iv; /* integer value or pv offset */
145};
146
147struct xpvnv {
148 char * xpv_pv; /* pointer to malloced string */
149 STRLEN xpv_cur; /* length of xpv_pv as a C string */
150 STRLEN xpv_len; /* allocated size */
151 I32 xiv_iv; /* integer value or pv offset */
152 double xnv_nv; /* numeric value, if any */
153};
154
155struct xpvmg {
156 char * xpv_pv; /* pointer to malloced string */
157 STRLEN xpv_cur; /* length of xpv_pv as a C string */
158 STRLEN xpv_len; /* allocated size */
159 I32 xiv_iv; /* integer value or pv offset */
160 double xnv_nv; /* numeric value, if any */
161 MAGIC* xmg_magic; /* linked list of magicalness */
162 HV* xmg_stash; /* class package */
163};
164
165struct xpvlv {
166 char * xpv_pv; /* pointer to malloced string */
167 STRLEN xpv_cur; /* length of xpv_pv as a C string */
168 STRLEN xpv_len; /* allocated size */
169 I32 xiv_iv; /* integer value or pv offset */
170 double xnv_nv; /* numeric value, if any */
171 MAGIC* xmg_magic; /* linked list of magicalness */
172 HV* xmg_stash; /* class package */
173 STRLEN xlv_targoff;
174 STRLEN xlv_targlen;
175 SV* xlv_targ;
176 char xlv_type;
177};
178
179struct xpvgv {
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 */
183 I32 xiv_iv; /* integer value or pv offset */
184 double xnv_nv; /* numeric value, if any */
185 MAGIC* xmg_magic; /* linked list of magicalness */
186 HV* xmg_stash; /* class package */
187 GP* xgv_gp;
188 char* xgv_name;
189 STRLEN xgv_namelen;
190 HV* xgv_stash;
191};
192
193struct xpvbm {
194 char * xpv_pv; /* pointer to malloced string */
195 STRLEN xpv_cur; /* length of xpv_pv as a C string */
196 STRLEN xpv_len; /* allocated size */
197 I32 xiv_iv; /* integer value or pv offset */
198 double xnv_nv; /* numeric value, if any */
199 MAGIC* xmg_magic; /* linked list of magicalness */
200 HV* xmg_stash; /* class package */
201 I32 xbm_useful; /* is this constant pattern being useful? */
202 U16 xbm_previous; /* how many characters in string before rare? */
203 U8 xbm_rare; /* rarest character in string */
204};
205
206struct xpvfm {
207 char * xpv_pv; /* pointer to malloced string */
208 STRLEN xpv_cur; /* length of xpv_pv as a C string */
209 STRLEN xpv_len; /* allocated size */
210 I32 xiv_iv; /* integer value or pv offset */
211 double xnv_nv; /* numeric value, if any */
212 MAGIC* xmg_magic; /* linked list of magicalness */
213 HV* xmg_stash; /* class package */
214 HV * xcv_stash;
215 OP * xcv_start;
216 OP * xcv_root;
217 I32 (*xcv_usersub)();
218 I32 xcv_userindex;
219 GV * xcv_filegv;
220 long xcv_depth; /* >= 2 indicates recursive call */
221 AV * xcv_padlist;
222 bool xcv_deleted;
223 I32 xfm_lines;
224};
225
79072805
LW
226#define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
227
463ee0b2
LW
228#define SvOK(sv) (SvFLAGS(sv) & SVf_OK)
229#define SvOK_on(sv) (SvFLAGS(sv) |= SVf_OK)
230#define SvOK_off(sv) (SvFLAGS(sv) &= \
231 ~(SVf_IOK|SVf_NOK|SVf_POK|SVf_OK), \
79072805
LW
232 SvOOK_off(sv))
233
463ee0b2
LW
234#define SvOKp(sv) (SvPRIVATE(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
235#define SvIOKp(sv) (SvPRIVATE(sv) & SVp_IOK)
236#define SvIOKp_on(sv) (SvOOK_off(sv), SvPRIVATE(sv) |= SVp_IOK)
237#define SvNOKp(sv) (SvPRIVATE(sv) & SVp_NOK)
238#define SvNOKp_on(sv) (SvPRIVATE(sv) |= SVp_NOK)
239#define SvPOKp(sv) (SvPRIVATE(sv) & SVp_POK)
240#define SvPOKp_on(sv) (SvPRIVATE(sv) |= SVp_POK)
241
79072805 242#define SvIOK(sv) (SvFLAGS(sv) & SVf_IOK)
463ee0b2 243#define SvIOK_on(sv) (SvOOK_off(sv), SvFLAGS(sv) |= (SVf_IOK|SVf_OK))
79072805 244#define SvIOK_off(sv) (SvFLAGS(sv) &= ~SVf_IOK)
463ee0b2 245#define SvIOK_only(sv) (SvOK_off(sv), SvFLAGS(sv) |= (SVf_IOK|SVf_OK))
79072805
LW
246
247#define SvNOK(sv) (SvFLAGS(sv) & SVf_NOK)
463ee0b2 248#define SvNOK_on(sv) (SvFLAGS(sv) |= (SVf_NOK|SVf_OK))
79072805 249#define SvNOK_off(sv) (SvFLAGS(sv) &= ~SVf_NOK)
463ee0b2 250#define SvNOK_only(sv) (SvOK_off(sv), SvFLAGS(sv) |= (SVf_NOK|SVf_OK))
79072805
LW
251
252#define SvPOK(sv) (SvFLAGS(sv) & SVf_POK)
463ee0b2 253#define SvPOK_on(sv) (SvFLAGS(sv) |= (SVf_POK|SVf_OK))
79072805 254#define SvPOK_off(sv) (SvFLAGS(sv) &= ~SVf_POK)
463ee0b2 255#define SvPOK_only(sv) (SvOK_off(sv), SvFLAGS(sv) |= (SVf_POK|SVf_OK))
79072805
LW
256
257#define SvOOK(sv) (SvFLAGS(sv) & SVf_OOK)
258#define SvOOK_on(sv) (SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
259#define SvOOK_off(sv) (SvOOK(sv) && sv_backoff(sv))
79072805
LW
260
261#define SvREADONLY(sv) (SvFLAGS(sv) & SVf_READONLY)
262#define SvREADONLY_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
263#define SvREADONLY_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
264
265#define SvMAGICAL(sv) (SvFLAGS(sv) & SVf_MAGICAL)
266#define SvMAGICAL_on(sv) (SvFLAGS(sv) |= SVf_MAGICAL)
267#define SvMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVf_MAGICAL)
268
463ee0b2
LW
269#define SvSCREAM(sv) (SvPRIVATE(sv) & SVp_SCREAM)
270#define SvSCREAM_on(sv) (SvPRIVATE(sv) |= SVp_SCREAM)
271#define SvSCREAM_off(sv) (SvPRIVATE(sv) &= ~SVp_SCREAM)
79072805
LW
272
273#define SvTEMP(sv) (SvFLAGS(sv) & SVf_TEMP)
274#define SvTEMP_on(sv) (SvFLAGS(sv) |= SVf_TEMP)
275#define SvTEMP_off(sv) (SvFLAGS(sv) &= ~SVf_TEMP)
276
79072805
LW
277#define SvCOMPILED(sv) (SvPRIVATE(sv) & SVpfm_COMPILED)
278#define SvCOMPILED_on(sv) (SvPRIVATE(sv) |= SVpfm_COMPILED)
279#define SvCOMPILED_off(sv) (SvPRIVATE(sv) &= ~SVpfm_COMPILED)
280
281#define SvTAIL(sv) (SvPRIVATE(sv) & SVpbm_TAIL)
282#define SvTAIL_on(sv) (SvPRIVATE(sv) |= SVpbm_TAIL)
283#define SvTAIL_off(sv) (SvPRIVATE(sv) &= ~SVpbm_TAIL)
284
285#define SvCASEFOLD(sv) (SvPRIVATE(sv) & SVpbm_CASEFOLD)
286#define SvCASEFOLD_on(sv) (SvPRIVATE(sv) |= SVpbm_CASEFOLD)
287#define SvCASEFOLD_off(sv) (SvPRIVATE(sv) &= ~SVpbm_CASEFOLD)
288
289#define SvVALID(sv) (SvPRIVATE(sv) & SVpbm_VALID)
290#define SvVALID_on(sv) (SvPRIVATE(sv) |= SVpbm_VALID)
291#define SvVALID_off(sv) (SvPRIVATE(sv) &= ~SVpbm_VALID)
292
293#define SvMULTI(sv) (SvPRIVATE(sv) & SVpgv_MULTI)
294#define SvMULTI_on(sv) (SvPRIVATE(sv) |= SVpgv_MULTI)
295#define SvMULTI_off(sv) (SvPRIVATE(sv) &= ~SVpgv_MULTI)
296
463ee0b2
LW
297#define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
298#define SvIVXx(sv) SvIVX(sv)
299#define SvNVX(sv) ((XPVNV*)SvANY(sv))->xnv_nv
300#define SvNVXx(sv) SvNVX(sv)
301#define SvPVX(sv) ((XPV*) SvANY(sv))->xpv_pv
302#define SvPVXx(sv) SvPVX(sv)
79072805 303#define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
79072805
LW
304#define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
305#define SvLENx(sv) SvLEN(sv)
306#define SvEND(sv)(((XPV*) SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
307#define SvENDx(sv) ((Sv = sv), SvEND(Sv))
308#define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_magic
309#define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
310
311#define SvIV_set(sv, val) \
312 do { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
313 (((XPVIV*) SvANY(sv))->xiv_iv = val); } while (0)
314#define SvNV_set(sv, val) \
315 do { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
316 (((XPVNV*) SvANY(sv))->xnv_nv = val); } while (0)
317#define SvPV_set(sv, val) \
318 do { assert(SvTYPE(sv) >= SVt_PV); \
319 (((XPV*) SvANY(sv))->xpv_pv = val); } while (0)
320#define SvCUR_set(sv, val) \
321 do { assert(SvTYPE(sv) >= SVt_PV); \
322 (((XPV*) SvANY(sv))->xpv_cur = val); } while (0)
323#define SvLEN_set(sv, val) \
324 do { assert(SvTYPE(sv) >= SVt_PV); \
325 (((XPV*) SvANY(sv))->xpv_len = val); } while (0)
326#define SvEND_set(sv, val) \
327 do { assert(SvTYPE(sv) >= SVt_PV); \
463ee0b2 328 (((XPV*) SvANY(sv))->xpv_cur = val - SvPVX(sv)); } while (0)
79072805 329
93a17b20
LW
330#define SvCUROK(sv) (SvPOK(sv) ? SvCUR(sv) : 0)
331
79072805
LW
332#define BmRARE(sv) ((XPVBM*) SvANY(sv))->xbm_rare
333#define BmUSEFUL(sv) ((XPVBM*) SvANY(sv))->xbm_useful
334#define BmPREVIOUS(sv) ((XPVBM*) SvANY(sv))->xbm_previous
335
336#define FmLINES(sv) ((XPVFM*) SvANY(sv))->xfm_lines
337
338#define LvTYPE(sv) ((XPVLV*) SvANY(sv))->xlv_type
339#define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
340#define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
341#define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
342
463ee0b2 343#define SvTAINT(sv) if (tainting && tainted) sv_magic(sv, 0, 't', 0, 0)
79072805
LW
344
345#ifdef CRIPPLED_CC
346
463ee0b2
LW
347double SvIV();
348double SvNV();
349#define SvPV(sv, lp) sv_pvn(sv, &lp)
350char *sv_pvn();
79072805
LW
351I32 SvTRUE();
352
463ee0b2
LW
353#define SvIVx(sv) SvIV(sv)
354#define SvNVx(sv) SvNV(sv)
355#define SvPVx(sv, lp) sv_pvn(sv, &lp)
79072805
LW
356#define SvTRUEx(sv) SvTRUE(sv)
357
358#else /* !CRIPPLED_CC */
359
463ee0b2 360#define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
79072805 361
463ee0b2 362#define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
79072805 363
463ee0b2 364#define SvPV(sv, lp) (SvPOK(sv) ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv(sv, &lp))
79072805 365
463ee0b2 366#define SvTRUE(sv) ( \
79072805
LW
367 SvPOK(sv) \
368 ? ((Xpv = (XPV*)SvANY(sv)) && \
369 (*Xpv->xpv_pv > '0' || \
370 Xpv->xpv_cur > 1 || \
371 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')) \
372 ? 1 \
373 : 0) \
374 : \
375 SvIOK(sv) \
463ee0b2 376 ? SvIVX(sv) != 0 \
79072805 377 : SvNOK(sv) \
463ee0b2
LW
378 ? SvNVX(sv) != 0.0 \
379 : sv_2bool(sv) )
79072805 380
463ee0b2
LW
381#define SvIVx(sv) ((Sv = sv), SvIV(Sv))
382#define SvNVx(sv) ((Sv = sv), SvNV(Sv))
383#define SvPVx(sv, lp) ((Sv = sv), SvPV(Sv, lp))
79072805
LW
384#define SvTRUEx(sv) ((Sv = sv), SvTRUE(Sv))
385
386#endif /* CRIPPLED_CC */
387
388/* the following macro updates any magic values this sv is associated with */
389
463ee0b2 390#define SvSETMAGIC(x) if (SvMAGICAL(x)) mg_set(x)
79072805
LW
391
392#define SvSetSV(dst,src) if (dst != src) sv_setsv(dst,src)
393
394#define SvPEEK(sv) sv_peek(sv)
395
396#define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
397
398#define GROWSTR(pp,lp,len) if (*(lp) < (len)) pv_grow(pp, lp, (len) * 3 / 2)
399
400#ifndef DOSISH
401# define SvGROW(sv,len) if (SvLEN(sv) < (len)) sv_grow(sv,len)
402# define Sv_Grow sv_grow
403#else
404 /* extra parentheses intentionally NOT placed around "len"! */
405# define SvGROW(sv,len) if (SvLEN(sv) < (unsigned long)len) \
406 sv_grow(sv,(unsigned long)len)
407# define Sv_Grow(sv,len) sv_grow(sv,(unsigned long)(len))
408#endif /* DOSISH */