perl 5.0 alpha 4
[p5sagit/p5-mst-13.2.git] / sv.h
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
30 typedef 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
57 struct sv {
58     void*       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
66 struct gv {
67     XPVGV*      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
75 struct cv {
76     XPVGV*      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
84 struct av {
85     XPVAV*      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
93 struct hv {
94     XPVHV*      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
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 */
116 #define SVf_OK          32              /* has defined value */
117 #define SVf_TEMP        64              /* eventually in sv_private? */
118 #define SVf_READONLY    128             /* may not be modified */
119
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 */
125
126 #define SVpfm_COMPILED  128
127
128 #define SVpbm_VALID     128
129 #define SVpbm_CASEFOLD  64
130 #define SVpbm_TAIL      32
131
132 #define SVpgv_MULTI     128
133
134 struct 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
140 struct 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
147 struct 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
155 struct 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
165 struct 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
179 struct 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
193 struct 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
206 struct 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
226 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
227
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), \
232                                         SvOOK_off(sv))
233
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
242 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
243 #define SvIOK_on(sv)            (SvOOK_off(sv), SvFLAGS(sv) |= (SVf_IOK|SVf_OK))
244 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~SVf_IOK)
245 #define SvIOK_only(sv)          (SvOK_off(sv), SvFLAGS(sv) |= (SVf_IOK|SVf_OK))
246
247 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
248 #define SvNOK_on(sv)            (SvFLAGS(sv) |= (SVf_NOK|SVf_OK))
249 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~SVf_NOK)
250 #define SvNOK_only(sv)          (SvOK_off(sv), SvFLAGS(sv) |= (SVf_NOK|SVf_OK))
251
252 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
253 #define SvPOK_on(sv)            (SvFLAGS(sv) |= (SVf_POK|SVf_OK))
254 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~SVf_POK)
255 #define SvPOK_only(sv)          (SvOK_off(sv), SvFLAGS(sv) |= (SVf_POK|SVf_OK))
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))
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
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)
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
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
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)
303 #define SvCUR(sv) ((XPV*)  SvANY(sv))->xpv_cur
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); \
328                 (((XPV*)  SvANY(sv))->xpv_cur = val - SvPVX(sv)); } while (0)
329
330 #define SvCUROK(sv) (SvPOK(sv) ? SvCUR(sv) : 0)
331
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
343 #define SvTAINT(sv) if (tainting && tainted) sv_magic(sv, 0, 't', 0, 0)
344
345 #ifdef CRIPPLED_CC
346
347 double SvIV();
348 double SvNV();
349 #define SvPV(sv, lp) sv_pvn(sv, &lp)
350 char *sv_pvn();
351 I32 SvTRUE();
352
353 #define SvIVx(sv) SvIV(sv)
354 #define SvNVx(sv) SvNV(sv)
355 #define SvPVx(sv, lp) sv_pvn(sv, &lp)
356 #define SvTRUEx(sv) SvTRUE(sv)
357
358 #else /* !CRIPPLED_CC */
359
360 #define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
361
362 #define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
363
364 #define SvPV(sv, lp) (SvPOK(sv) ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv(sv, &lp))
365
366 #define SvTRUE(sv) (                                            \
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)                                           \
376             ? SvIVX(sv) != 0                                    \
377             :   SvNOK(sv)                                       \
378                 ? SvNVX(sv) != 0.0                              \
379                 : sv_2bool(sv) )
380
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))
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
390 #define SvSETMAGIC(x) if (SvMAGICAL(x)) mg_set(x)
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 */