5ebb337888e0df306c8f10a1e75f626bd225dc92
[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_IV,
33         SVt_NV,
34         SVt_RV,
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_ROK         16              /* has a valid reference pointer */
116 #define SVf_OK          32              /* has defined value */
117 #define SVf_MAGICAL     64              /* has special methods */
118 #define SVf_THINKFIRST  128             /* may not be changed without thought */
119
120 #define SVs_PADBUSY     1               /* reserved for tmp or my already */
121 #define SVs_PADTMP      2               /* in use as tmp */
122 #define SVs_PADMY       4               /* in use a "my" variable */
123 #define SVs_8           8
124 #define SVs_16          16
125 #define SVs_TEMP        32              /* string is stealable? */
126 #define SVs_OBJECT      64              /* is "blessed" */
127 #define SVs_READONLY    128             /* may not be modified */
128
129 #define SVp_IOK         1               /* has valid non-public integer value */
130 #define SVp_NOK         2               /* has valid non-public numeric value */
131 #define SVp_POK         4               /* has valid non-public pointer value */
132 #define SVp_SCREAM      8               /* has been studied? */
133 #define SVp_TAINTEDDIR  16              /* PATH component is a security risk */
134
135 #define SVpfm_COMPILED  128
136
137 #define SVpbm_VALID     128
138 #define SVpbm_CASEFOLD  64
139 #define SVpbm_TAIL      32
140
141 #define SVpgv_MULTI     128
142
143 struct xrv {
144     SV *        xrv_rv;         /* pointer to another SV */
145 };
146
147 struct xpv {
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 };
152
153 struct xpviv {
154     char *      xpv_pv;         /* pointer to malloced string */
155     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
156     STRLEN      xpv_len;        /* allocated size */
157     I32         xiv_iv;         /* integer value or pv offset */
158 };
159
160 struct xpvnv {
161     char *      xpv_pv;         /* pointer to malloced string */
162     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
163     STRLEN      xpv_len;        /* allocated size */
164     I32         xiv_iv;         /* integer value or pv offset */
165     double      xnv_nv;         /* numeric value, if any */
166 };
167
168 struct xpvmg {
169     char *      xpv_pv;         /* pointer to malloced string */
170     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
171     STRLEN      xpv_len;        /* allocated size */
172     I32         xiv_iv;         /* integer value or pv offset */
173     double      xnv_nv;         /* numeric value, if any */
174     MAGIC*      xmg_magic;      /* linked list of magicalness */
175     HV*         xmg_stash;      /* class package */
176 };
177
178 struct xpvlv {
179     char *      xpv_pv;         /* pointer to malloced string */
180     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
181     STRLEN      xpv_len;        /* allocated size */
182     I32         xiv_iv;         /* integer value or pv offset */
183     double      xnv_nv;         /* numeric value, if any */
184     MAGIC*      xmg_magic;      /* linked list of magicalness */
185     HV*         xmg_stash;      /* class package */
186     STRLEN      xlv_targoff;
187     STRLEN      xlv_targlen;
188     SV*         xlv_targ;
189     char        xlv_type;
190 };
191
192 struct xpvgv {
193     char *      xpv_pv;         /* pointer to malloced string */
194     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
195     STRLEN      xpv_len;        /* allocated size */
196     I32         xiv_iv;         /* integer value or pv offset */
197     double      xnv_nv;         /* numeric value, if any */
198     MAGIC*      xmg_magic;      /* linked list of magicalness */
199     HV*         xmg_stash;      /* class package */
200     GP*         xgv_gp;
201     char*       xgv_name;
202     STRLEN      xgv_namelen;
203     HV*         xgv_stash;
204 };
205
206 struct xpvbm {
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     I32         xbm_useful;     /* is this constant pattern being useful? */
215     U16         xbm_previous;   /* how many characters in string before rare? */
216     U8          xbm_rare;       /* rarest character in string */
217 };
218
219 struct xpvfm {
220     char *      xpv_pv;         /* pointer to malloced string */
221     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
222     STRLEN      xpv_len;        /* allocated size */
223     I32         xiv_iv;         /* integer value or pv offset */
224     double      xnv_nv;         /* numeric value, if any */
225     MAGIC*      xmg_magic;      /* linked list of magicalness */
226     HV*         xmg_stash;      /* class package */
227     HV *        xcv_stash;
228     OP *        xcv_start;
229     OP *        xcv_root;
230     I32       (*xcv_usersub)();
231     I32         xcv_userindex;
232     GV *        xcv_filegv;
233     long        xcv_depth;              /* >= 2 indicates recursive call */
234     AV *        xcv_padlist;
235     bool        xcv_deleted;
236     I32         xfm_lines;
237 };
238
239 /* The following macros define implementation-independent predicates on SVs. */
240
241 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
242
243 #define SvOK(sv)                (SvFLAGS(sv) & SVf_OK)
244 #define SvOK_on(sv)             (SvFLAGS(sv) |= SVf_OK)
245 #define SvOK_off(sv)            (SvFLAGS(sv) &=                            \
246                                         ~(SVf_IOK|SVf_NOK|SVf_POK|SVf_OK), \
247                                         SvOOK_off(sv))
248
249 #define SvOKp(sv)               (SvPRIVATE(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
250 #define SvIOKp(sv)              (SvPRIVATE(sv) & SVp_IOK)
251 #define SvIOKp_on(sv)           (SvOOK_off(sv), SvPRIVATE(sv) |= SVp_IOK)
252 #define SvNOKp(sv)              (SvPRIVATE(sv) & SVp_NOK)
253 #define SvNOKp_on(sv)           (SvPRIVATE(sv) |= SVp_NOK)
254 #define SvPOKp(sv)              (SvPRIVATE(sv) & SVp_POK)
255 #define SvPOKp_on(sv)           (SvPRIVATE(sv) |= SVp_POK)
256
257 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
258 #define SvIOK_on(sv)            (SvOOK_off(sv), SvFLAGS(sv) |= (SVf_IOK|SVf_OK))
259 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~SVf_IOK)
260 #define SvIOK_only(sv)          (SvOK_off(sv), SvFLAGS(sv) |= (SVf_IOK|SVf_OK))
261
262 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
263 #define SvNOK_on(sv)            (SvFLAGS(sv) |= (SVf_NOK|SVf_OK))
264 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~SVf_NOK)
265 #define SvNOK_only(sv)          (SvOK_off(sv), SvFLAGS(sv) |= (SVf_NOK|SVf_OK))
266
267 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
268 #define SvPOK_on(sv)            (SvFLAGS(sv) |= (SVf_POK|SVf_OK))
269 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~SVf_POK)
270 #define SvPOK_only(sv)          (SvOK_off(sv), SvFLAGS(sv) |= (SVf_POK|SVf_OK))
271
272 #define SvOOK(sv)               (SvFLAGS(sv) & SVf_OOK)
273 #define SvOOK_on(sv)            (SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
274 #define SvOOK_off(sv)           (SvOOK(sv) && sv_backoff(sv))
275
276 #define SvROK(sv)               (SvFLAGS(sv) & SVf_ROK)
277 #define SvROK_on(sv)            (SvFLAGS(sv) |= SVf_ROK|SVf_THINKFIRST|SVf_OK)
278 #define SvROK_off(sv)           (SvFLAGS(sv) &= ~SVf_ROK)
279
280 #define SvMAGICAL(sv)           (SvFLAGS(sv) & SVf_MAGICAL)
281 #define SvMAGICAL_on(sv)        (SvFLAGS(sv) |= SVf_MAGICAL)
282 #define SvMAGICAL_off(sv)       (SvFLAGS(sv) &= ~SVf_MAGICAL)
283
284 #define SvTHINKFIRST(sv)        (SvFLAGS(sv) & SVf_THINKFIRST)
285 #define SvTHINKFIRST_on(sv)     (SvFLAGS(sv) |= SVf_THINKFIRST)
286 #define SvTHINKFIRST_off(sv)    (SvFLAGS(sv) &= ~SVf_THINKFIRST)
287
288 #define SvPADBUSY(sv)           (SvSTORAGE(sv) & SVs_PADBUSY)
289
290 #define SvPADTMP(sv)            (SvSTORAGE(sv) & SVs_PADTMP)
291 #define SvPADTMP_on(sv)         (SvSTORAGE(sv) |= SVs_PADTMP|SVs_PADBUSY)
292 #define SvPADTMP_off(sv)        (SvSTORAGE(sv) &= ~SVs_PADTMP)
293
294 #define SvPADMY(sv)             (SvSTORAGE(sv) & SVs_PADMY)
295 #define SvPADMY_on(sv)          (SvSTORAGE(sv) |= SVs_PADMY|SVs_PADBUSY)
296
297 #define SvTEMP(sv)              (SvSTORAGE(sv) & SVs_TEMP)
298 #define SvTEMP_on(sv)           (SvSTORAGE(sv) |= SVs_TEMP)
299 #define SvTEMP_off(sv)          (SvSTORAGE(sv) &= ~SVs_TEMP)
300
301 #define SvOBJECT(sv)            (SvSTORAGE(sv) & SVs_OBJECT)
302 #define SvOBJECT_on(sv)         (SvSTORAGE(sv) |= SVs_OBJECT)
303 #define SvOBJECT_off(sv)        (SvSTORAGE(sv) &= ~SVs_OBJECT)
304
305 #define SvREADONLY(sv)          (SvSTORAGE(sv) & SVs_READONLY)
306 #define SvREADONLY_on(sv)       (SvSTORAGE(sv) |= SVs_READONLY, \
307                                         SvTHINKFIRST_on(sv))
308 #define SvREADONLY_off(sv)      (SvSTORAGE(sv) &= ~SVs_READONLY)
309
310 #define SvSCREAM(sv)            (SvPRIVATE(sv) & SVp_SCREAM)
311 #define SvSCREAM_on(sv)         (SvPRIVATE(sv) |= SVp_SCREAM)
312 #define SvSCREAM_off(sv)        (SvPRIVATE(sv) &= ~SVp_SCREAM)
313
314 #define SvCOMPILED(sv)          (SvPRIVATE(sv) & SVpfm_COMPILED)
315 #define SvCOMPILED_on(sv)       (SvPRIVATE(sv) |= SVpfm_COMPILED)
316 #define SvCOMPILED_off(sv)      (SvPRIVATE(sv) &= ~SVpfm_COMPILED)
317
318 #define SvTAIL(sv)              (SvPRIVATE(sv) & SVpbm_TAIL)
319 #define SvTAIL_on(sv)           (SvPRIVATE(sv) |= SVpbm_TAIL)
320 #define SvTAIL_off(sv)          (SvPRIVATE(sv) &= ~SVpbm_TAIL)
321
322 #define SvCASEFOLD(sv)          (SvPRIVATE(sv) & SVpbm_CASEFOLD)
323 #define SvCASEFOLD_on(sv)       (SvPRIVATE(sv) |= SVpbm_CASEFOLD)
324 #define SvCASEFOLD_off(sv)      (SvPRIVATE(sv) &= ~SVpbm_CASEFOLD)
325
326 #define SvVALID(sv)             (SvPRIVATE(sv) & SVpbm_VALID)
327 #define SvVALID_on(sv)          (SvPRIVATE(sv) |= SVpbm_VALID)
328 #define SvVALID_off(sv)         (SvPRIVATE(sv) &= ~SVpbm_VALID)
329
330 #define SvMULTI(sv)             (SvPRIVATE(sv) & SVpgv_MULTI)
331 #define SvMULTI_on(sv)          (SvPRIVATE(sv) |= SVpgv_MULTI)
332 #define SvMULTI_off(sv)         (SvPRIVATE(sv) &= ~SVpgv_MULTI)
333
334 #define SvRV(sv) ((XRV*)  SvANY(sv))->xrv_rv
335 #define SvRVx(sv) SvRV(sv)
336
337 #define SvIVX(sv) ((XPVIV*)  SvANY(sv))->xiv_iv
338 #define SvIVXx(sv) SvIVX(sv)
339 #define SvNVX(sv)  ((XPVNV*)SvANY(sv))->xnv_nv
340 #define SvNVXx(sv) SvNVX(sv)
341 #define SvPVX(sv)  ((XPV*)  SvANY(sv))->xpv_pv
342 #define SvPVXx(sv) SvPVX(sv)
343 #define SvCUR(sv) ((XPV*)  SvANY(sv))->xpv_cur
344 #define SvLEN(sv) ((XPV*)  SvANY(sv))->xpv_len
345 #define SvLENx(sv) SvLEN(sv)
346 #define SvEND(sv)(((XPV*)  SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
347 #define SvENDx(sv) ((Sv = sv), SvEND(Sv))
348 #define SvMAGIC(sv)     ((XPVMG*)  SvANY(sv))->xmg_magic
349 #define SvSTASH(sv)     ((XPVMG*)  SvANY(sv))->xmg_stash
350
351 #define SvIV_set(sv, val) \
352         do { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
353                 (((XPVIV*)  SvANY(sv))->xiv_iv = val); } while (0)
354 #define SvNV_set(sv, val) \
355         do { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
356                 (((XPVNV*)  SvANY(sv))->xnv_nv = val); } while (0)
357 #define SvPV_set(sv, val) \
358         do { assert(SvTYPE(sv) >= SVt_PV); \
359                 (((XPV*)  SvANY(sv))->xpv_pv = val); } while (0)
360 #define SvCUR_set(sv, val) \
361         do { assert(SvTYPE(sv) >= SVt_PV); \
362                 (((XPV*)  SvANY(sv))->xpv_cur = val); } while (0)
363 #define SvLEN_set(sv, val) \
364         do { assert(SvTYPE(sv) >= SVt_PV); \
365                 (((XPV*)  SvANY(sv))->xpv_len = val); } while (0)
366 #define SvEND_set(sv, val) \
367         do { assert(SvTYPE(sv) >= SVt_PV); \
368                 (((XPV*)  SvANY(sv))->xpv_cur = val - SvPVX(sv)); } while (0)
369
370 #define SvCUROK(sv) (SvPOK(sv) ? SvCUR(sv) : 0)
371
372 #define BmRARE(sv)      ((XPVBM*)  SvANY(sv))->xbm_rare
373 #define BmUSEFUL(sv)    ((XPVBM*)  SvANY(sv))->xbm_useful
374 #define BmPREVIOUS(sv)  ((XPVBM*)  SvANY(sv))->xbm_previous
375
376 #define FmLINES(sv)     ((XPVFM*)  SvANY(sv))->xfm_lines
377
378 #define LvTYPE(sv)      ((XPVLV*)  SvANY(sv))->xlv_type
379 #define LvTARG(sv)      ((XPVLV*)  SvANY(sv))->xlv_targ
380 #define LvTARGOFF(sv)   ((XPVLV*)  SvANY(sv))->xlv_targoff
381 #define LvTARGLEN(sv)   ((XPVLV*)  SvANY(sv))->xlv_targlen
382
383 #define SvTAINT(sv) if (tainting && tainted) sv_magic(sv, 0, 't', 0, 0)
384
385 #ifdef CRIPPLED_CC
386
387 double SvIV();
388 double SvNV();
389 #define SvPV(sv, lp) sv_pvn(sv, &lp)
390 char *sv_pvn();
391 I32 SvTRUE();
392
393 #define SvIVx(sv) SvIV(sv)
394 #define SvNVx(sv) SvNV(sv)
395 #define SvPVx(sv, lp) sv_pvn(sv, &lp)
396 #define SvTRUEx(sv) SvTRUE(sv)
397
398 #else /* !CRIPPLED_CC */
399
400 #define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
401
402 #define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
403
404 #define SvPV(sv, lp) (SvPOK(sv) ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv(sv, &lp))
405
406 #define SvTRUE(sv) (                                            \
407         SvPOK(sv)                                               \
408         ?   ((Xpv = (XPV*)SvANY(sv)) &&                         \
409              (*Xpv->xpv_pv > '0' ||                             \
410               Xpv->xpv_cur > 1 ||                               \
411               (Xpv->xpv_cur && *Xpv->xpv_pv != '0'))            \
412              ? 1                                                \
413              : 0)                                               \
414         :                                                       \
415             SvIOK(sv)                                           \
416             ? SvIVX(sv) != 0                                    \
417             :   SvNOK(sv)                                       \
418                 ? SvNVX(sv) != 0.0                              \
419                 : sv_2bool(sv) )
420
421 #define SvIVx(sv) ((Sv = sv), SvIV(Sv))
422 #define SvNVx(sv) ((Sv = sv), SvNV(Sv))
423 #define SvPVx(sv, lp) ((Sv = sv), SvPV(Sv, lp))
424 #define SvTRUEx(sv) ((Sv = sv), SvTRUE(Sv))
425
426 #endif /* CRIPPLED_CC */
427
428 /* the following macro updates any magic values this sv is associated with */
429
430 #define SvSETMAGIC(x) if (SvMAGICAL(x)) mg_set(x)
431
432 #define SvSetSV(dst,src) if (dst != src) sv_setsv(dst,src)
433
434 #define SvPEEK(sv) sv_peek(sv)
435
436 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
437
438 #define GROWSTR(pp,lp,len) if (*(lp) < (len)) pv_grow(pp, lp, (len) * 3 / 2)
439
440 #ifndef DOSISH
441 #  define SvGROW(sv,len) if (SvLEN(sv) < (len)) sv_grow(sv,len)
442 #  define Sv_Grow sv_grow
443 #else
444     /* extra parentheses intentionally NOT placed around "len"! */
445 #  define SvGROW(sv,len) if (SvLEN(sv) < (unsigned long)len) \
446                 sv_grow(sv,(unsigned long)len)
447 #  define Sv_Grow(sv,len) sv_grow(sv,(unsigned long)(len))
448 #endif /* DOSISH */