This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.0 alpha 2
[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 {
58 ANY sv_any; /* pointer to something */
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 {
67 ANY sv_any; /* pointer to something */
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 {
76 ANY sv_any; /* pointer to something */
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 {
85 ANY sv_any; /* pointer to something */
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 {
94 ANY sv_any; /* pointer to something */
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
102#define SvANY(sv) (sv)->sv_any.any_ptr
103#define SvANYI32(sv) (sv)->sv_any.any_i32
104#define SvTYPE(sv) (sv)->sv_type
105#define SvREFCNT(sv) (sv)->sv_refcnt
106#define SvFLAGS(sv) (sv)->sv_flags
107#define SvSTORAGE(sv) (sv)->sv_storage
108#define SvPRIVATE(sv) (sv)->sv_private
109
110#define SvUPGRADE(sv, mt) (SvTYPE(sv) >= mt || sv_upgrade(sv, mt))
111
112#define SVf_IOK 1 /* has valid integer value */
113#define SVf_NOK 2 /* has valid numeric value */
114#define SVf_POK 4 /* has valid pointer value */
115#define SVf_OOK 8 /* has valid offset value */
116#define SVf_MAGICAL 16 /* has special methods */
117#define SVf_SCREAM 32 /* eventually in sv_private? */
118#define SVf_TEMP 64 /* eventually in sv_private? */
119#define SVf_READONLY 128 /* may not be modified */
120
121#define SVp_TAINTED 128 /* is a security risk */
122
123#define SVpfm_COMPILED 1
124
125#define SVpbm_TAIL 1
126#define SVpbm_CASEFOLD 2
127#define SVpbm_VALID 4
128
129#define SVpgv_MULTI 1
130
131struct xpv {
132 char * xpv_pv; /* pointer to malloced string */
133 STRLEN xpv_cur; /* length of xpv_pv as a C string */
134 STRLEN xpv_len; /* allocated size */
135};
136
137struct xpviv {
138 char * xpv_pv; /* pointer to malloced string */
139 STRLEN xpv_cur; /* length of xpv_pv as a C string */
140 STRLEN xpv_len; /* allocated size */
141 I32 xiv_iv; /* integer value or pv offset */
142};
143
144struct xpvnv {
145 char * xpv_pv; /* pointer to malloced string */
146 STRLEN xpv_cur; /* length of xpv_pv as a C string */
147 STRLEN xpv_len; /* allocated size */
148 I32 xiv_iv; /* integer value or pv offset */
149 double xnv_nv; /* numeric value, if any */
150};
151
152struct xpvmg {
153 char * xpv_pv; /* pointer to malloced string */
154 STRLEN xpv_cur; /* length of xpv_pv as a C string */
155 STRLEN xpv_len; /* allocated size */
156 I32 xiv_iv; /* integer value or pv offset */
157 double xnv_nv; /* numeric value, if any */
158 MAGIC* xmg_magic; /* linked list of magicalness */
159 HV* xmg_stash; /* class package */
160};
161
162struct xpvlv {
163 char * xpv_pv; /* pointer to malloced string */
164 STRLEN xpv_cur; /* length of xpv_pv as a C string */
165 STRLEN xpv_len; /* allocated size */
166 I32 xiv_iv; /* integer value or pv offset */
167 double xnv_nv; /* numeric value, if any */
168 MAGIC* xmg_magic; /* linked list of magicalness */
169 HV* xmg_stash; /* class package */
170 STRLEN xlv_targoff;
171 STRLEN xlv_targlen;
172 SV* xlv_targ;
173 char xlv_type;
174};
175
176struct xpvgv {
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 */
180 I32 xiv_iv; /* integer value or pv offset */
181 double xnv_nv; /* numeric value, if any */
182 MAGIC* xmg_magic; /* linked list of magicalness */
183 HV* xmg_stash; /* class package */
184 GP* xgv_gp;
185 char* xgv_name;
186 STRLEN xgv_namelen;
187 HV* xgv_stash;
188};
189
190struct xpvbm {
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 */
194 I32 xiv_iv; /* integer value or pv offset */
195 double xnv_nv; /* numeric value, if any */
196 MAGIC* xmg_magic; /* linked list of magicalness */
197 HV* xmg_stash; /* class package */
198 I32 xbm_useful; /* is this constant pattern being useful? */
199 U16 xbm_previous; /* how many characters in string before rare? */
200 U8 xbm_rare; /* rarest character in string */
201};
202
203struct xpvfm {
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 */
207 I32 xiv_iv; /* integer value or pv offset */
208 double xnv_nv; /* numeric value, if any */
209 MAGIC* xmg_magic; /* linked list of magicalness */
210 HV* xmg_stash; /* class package */
211 HV * xcv_stash;
212 OP * xcv_start;
213 OP * xcv_root;
214 I32 (*xcv_usersub)();
215 I32 xcv_userindex;
216 GV * xcv_filegv;
217 long xcv_depth; /* >= 2 indicates recursive call */
218 AV * xcv_padlist;
219 bool xcv_deleted;
220 I32 xfm_lines;
221};
222
223/* XXX need to write custom routines for some of these */
224#define new_SV() (void*)malloc(sizeof(SV))
225#define del_SV(p) free((char*)p)
226
227#define new_XIV() (void*)malloc(sizeof(XPVIV))
228#define del_XIV(p) free((char*)p)
229
230#define new_XNV() (void*)malloc(sizeof(XPVNV))
231#define del_XNV(p) free((char*)p)
232
233#define new_XPV() (void*)malloc(sizeof(XPV))
234#define del_XPV(p) free((char*)p)
235
236#define new_XPVIV() (void*)malloc(sizeof(XPVIV))
237#define del_XPVIV(p) free((char*)p)
238
239#define new_XPVNV() (void*)malloc(sizeof(XPVNV))
240#define del_XPVNV(p) free((char*)p)
241
242#define new_XPVMG() (void*)malloc(sizeof(XPVMG))
243#define del_XPVMG(p) free((char*)p)
244
245#define new_XPVLV() (void*)malloc(sizeof(XPVLV))
246#define del_XPVLV(p) free((char*)p)
247
248#define new_XPVAV() (void*)malloc(sizeof(XPVAV))
249#define del_XPVAV(p) free((char*)p)
250
251#define new_XPVHV() (void*)malloc(sizeof(XPVHV))
252#define del_XPVHV(p) free((char*)p)
253
254#define new_XPVCV() (void*)malloc(sizeof(XPVCV))
255#define del_XPVCV(p) free((char*)p)
256
257#define new_XPVGV() (void*)malloc(sizeof(XPVGV))
258#define del_XPVGV(p) free((char*)p)
259
260#define new_XPVBM() (void*)malloc(sizeof(XPVBM))
261#define del_XPVBM(p) free((char*)p)
262
263#define new_XPVFM() (void*)malloc(sizeof(XPVFM))
264#define del_XPVFM(p) free((char*)p)
265
266#define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
267
268#define SvOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK))
269#define SvOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK), \
270 SvOOK_off(sv))
271
272#define SvIOK(sv) (SvFLAGS(sv) & SVf_IOK)
273#define SvIOK_on(sv) (SvOOK_off(sv), SvFLAGS(sv) |= SVf_IOK)
274#define SvIOK_off(sv) (SvFLAGS(sv) &= ~SVf_IOK)
275#define SvIOK_only(sv) (SvOK_off(sv), SvFLAGS(sv) |= SVf_IOK)
276
277#define SvNOK(sv) (SvFLAGS(sv) & SVf_NOK)
278#define SvNOK_on(sv) (SvFLAGS(sv) |= SVf_NOK)
279#define SvNOK_off(sv) (SvFLAGS(sv) &= ~SVf_NOK)
280#define SvNOK_only(sv) (SvOK_off(sv), SvFLAGS(sv) |= SVf_NOK)
281
282#define SvPOK(sv) (SvFLAGS(sv) & SVf_POK)
283#define SvPOK_on(sv) (SvFLAGS(sv) |= SVf_POK)
284#define SvPOK_off(sv) (SvFLAGS(sv) &= ~SVf_POK)
285#define SvPOK_only(sv) (SvOK_off(sv), SvFLAGS(sv) |= SVf_POK)
286
287#define SvOOK(sv) (SvFLAGS(sv) & SVf_OOK)
288#define SvOOK_on(sv) (SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
289#define SvOOK_off(sv) (SvOOK(sv) && sv_backoff(sv))
290#define SvOOK_only(sv) (SvOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
291
292#define SvREADONLY(sv) (SvFLAGS(sv) & SVf_READONLY)
293#define SvREADONLY_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
294#define SvREADONLY_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
295
296#define SvMAGICAL(sv) (SvFLAGS(sv) & SVf_MAGICAL)
297#define SvMAGICAL_on(sv) (SvFLAGS(sv) |= SVf_MAGICAL)
298#define SvMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVf_MAGICAL)
299
300#define SvSCREAM(sv) (SvFLAGS(sv) & SVf_SCREAM)
301#define SvSCREAM_on(sv) (SvFLAGS(sv) |= SVf_SCREAM)
302#define SvSCREAM_off(sv) (SvFLAGS(sv) &= ~SVf_SCREAM)
303
304#define SvTEMP(sv) (SvFLAGS(sv) & SVf_TEMP)
305#define SvTEMP_on(sv) (SvFLAGS(sv) |= SVf_TEMP)
306#define SvTEMP_off(sv) (SvFLAGS(sv) &= ~SVf_TEMP)
307
308#define SvTAINTED(sv) (SvPRIVATE(sv) & SVp_TAINTED)
309#define SvTAINTED_on(sv) (SvPRIVATE(sv) |= SVp_TAINTED)
310#define SvTAINTED_off(sv) (SvPRIVATE(sv) &= ~SVp_TAINTED)
311
312#define SvCOMPILED(sv) (SvPRIVATE(sv) & SVpfm_COMPILED)
313#define SvCOMPILED_on(sv) (SvPRIVATE(sv) |= SVpfm_COMPILED)
314#define SvCOMPILED_off(sv) (SvPRIVATE(sv) &= ~SVpfm_COMPILED)
315
316#define SvTAIL(sv) (SvPRIVATE(sv) & SVpbm_TAIL)
317#define SvTAIL_on(sv) (SvPRIVATE(sv) |= SVpbm_TAIL)
318#define SvTAIL_off(sv) (SvPRIVATE(sv) &= ~SVpbm_TAIL)
319
320#define SvCASEFOLD(sv) (SvPRIVATE(sv) & SVpbm_CASEFOLD)
321#define SvCASEFOLD_on(sv) (SvPRIVATE(sv) |= SVpbm_CASEFOLD)
322#define SvCASEFOLD_off(sv) (SvPRIVATE(sv) &= ~SVpbm_CASEFOLD)
323
324#define SvVALID(sv) (SvPRIVATE(sv) & SVpbm_VALID)
325#define SvVALID_on(sv) (SvPRIVATE(sv) |= SVpbm_VALID)
326#define SvVALID_off(sv) (SvPRIVATE(sv) &= ~SVpbm_VALID)
327
328#define SvMULTI(sv) (SvPRIVATE(sv) & SVpgv_MULTI)
329#define SvMULTI_on(sv) (SvPRIVATE(sv) |= SVpgv_MULTI)
330#define SvMULTI_off(sv) (SvPRIVATE(sv) &= ~SVpgv_MULTI)
331
332#define SvIV(sv) ((XPVIV*) SvANY(sv))->xiv_iv
333#define SvIVx(sv) SvIV(sv)
334#define SvNV(sv) ((XPVNV*)SvANY(sv))->xnv_nv
335#define SvNVx(sv) SvNV(sv)
336#define SvPV(sv) ((XPV*) SvANY(sv))->xpv_pv
337#define SvPVx(sv) SvPV(sv)
338#define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
339#define SvCURx(sv) SvCUR(sv)
340#define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
341#define SvLENx(sv) SvLEN(sv)
342#define SvEND(sv)(((XPV*) SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
343#define SvENDx(sv) ((Sv = sv), SvEND(Sv))
344#define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_magic
345#define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
346
347#define SvIV_set(sv, val) \
348 do { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
349 (((XPVIV*) SvANY(sv))->xiv_iv = val); } while (0)
350#define SvNV_set(sv, val) \
351 do { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
352 (((XPVNV*) SvANY(sv))->xnv_nv = val); } while (0)
353#define SvPV_set(sv, val) \
354 do { assert(SvTYPE(sv) >= SVt_PV); \
355 (((XPV*) SvANY(sv))->xpv_pv = val); } while (0)
356#define SvCUR_set(sv, val) \
357 do { assert(SvTYPE(sv) >= SVt_PV); \
358 (((XPV*) SvANY(sv))->xpv_cur = val); } while (0)
359#define SvLEN_set(sv, val) \
360 do { assert(SvTYPE(sv) >= SVt_PV); \
361 (((XPV*) SvANY(sv))->xpv_len = val); } while (0)
362#define SvEND_set(sv, val) \
363 do { assert(SvTYPE(sv) >= SVt_PV); \
364 (((XPV*) SvANY(sv))->xpv_cur = val - SvPV(sv)); } while (0)
365
366#define BmRARE(sv) ((XPVBM*) SvANY(sv))->xbm_rare
367#define BmUSEFUL(sv) ((XPVBM*) SvANY(sv))->xbm_useful
368#define BmPREVIOUS(sv) ((XPVBM*) SvANY(sv))->xbm_previous
369
370#define FmLINES(sv) ((XPVFM*) SvANY(sv))->xfm_lines
371
372#define LvTYPE(sv) ((XPVLV*) SvANY(sv))->xlv_type
373#define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
374#define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
375#define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
376
377#ifdef TAINT
378#define SvTUP(sv) (tainted |= (SvPRIVATE(sv) & SVp_TAINTED))
379#define SvTUPc(sv) (tainted |= (SvPRIVATE(sv) & SVp_TAINTED)),
380#define SvTDOWN(sv) (SvPRIVATE(sv) |= tainted ? SVp_TAINTED : 0)
381#define SvTDOWNc(sv) (SvPRIVATE(sv) |= tainted ? SVp_TAINTED : 0),
382#else
383#define SvTUP(sv)
384#define SvTUPc(sv)
385#define SvTDOWN(sv)
386#define SvTDOWNc(sv)
387#endif
388
389#ifdef CRIPPLED_CC
390
391double SvIVn();
392double SvNVn();
393char *SvPVn();
394I32 SvTRUE();
395
396#define SvIVnx(sv) SvIVn(sv)
397#define SvNVnx(sv) SvNVn(sv)
398#define SvPVnx(sv) SvPVn(sv)
399#define SvTRUEx(sv) SvTRUE(sv)
400
401#else /* !CRIPPLED_CC */
402
403#define SvIVn(sv) (SvTUPc(sv) (SvMAGICAL(sv) && mg_get(sv)), \
404 SvIOK(sv) ? SvIV(sv) : sv_2iv(sv))
405
406#define SvNVn(sv) (SvTUPc(sv) (SvMAGICAL(sv) && mg_get(sv)), \
407 SvNOK(sv) ? SvNV(sv) : sv_2nv(sv))
408
409#define SvPVn(sv) (SvTUPc(sv) (SvMAGICAL(sv) && mg_get(sv)), \
410 SvPOK(sv) ? SvPV(sv) : sv_2pv(sv))
411
412#define SvTRUE(sv) ((SvMAGICAL(sv) && mg_get(sv)), \
413 SvPOK(sv) \
414 ? ((Xpv = (XPV*)SvANY(sv)) && \
415 (*Xpv->xpv_pv > '0' || \
416 Xpv->xpv_cur > 1 || \
417 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')) \
418 ? 1 \
419 : 0) \
420 : \
421 SvIOK(sv) \
422 ? SvIV(sv) != 0 \
423 : SvNOK(sv) \
424 ? SvNV(sv) != 0.0 \
425 : 0 )
426
427#define SvIVnx(sv) ((Sv = sv), SvIVn(Sv))
428#define SvNVnx(sv) ((Sv = sv), SvNVn(Sv))
429#define SvPVnx(sv) ((Sv = sv), SvPVn(Sv))
430#define SvTRUEx(sv) ((Sv = sv), SvTRUE(Sv))
431
432#endif /* CRIPPLED_CC */
433
434/* the following macro updates any magic values this sv is associated with */
435
436#define SvGETMAGIC(x) \
437 SvTUP(x); \
438 if (SvMAGICAL(x)) mg_get(x)
439
440#define SvSETMAGIC(x) \
441 SvTDOWN(x); \
442 if (SvMAGICAL(x)) \
443 mg_set(x)
444
445#define SvSetSV(dst,src) if (dst != src) sv_setsv(dst,src)
446
447#define SvPEEK(sv) sv_peek(sv)
448
449#define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
450
451#define GROWSTR(pp,lp,len) if (*(lp) < (len)) pv_grow(pp, lp, (len) * 3 / 2)
452
453#ifndef DOSISH
454# define SvGROW(sv,len) if (SvLEN(sv) < (len)) sv_grow(sv,len)
455# define Sv_Grow sv_grow
456#else
457 /* extra parentheses intentionally NOT placed around "len"! */
458# define SvGROW(sv,len) if (SvLEN(sv) < (unsigned long)len) \
459 sv_grow(sv,(unsigned long)len)
460# define Sv_Grow(sv,len) sv_grow(sv,(unsigned long)(len))
461#endif /* DOSISH */
462