xref: /openbsd/gnu/usr.bin/perl/gv.h (revision e0680481)
1 /*    gv.h
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4  *    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10 
11 struct gp {
12     SV *	gp_sv;		/* scalar value */
13     struct io *	gp_io;		/* filehandle value */
14     CV *	gp_cv;		/* subroutine value */
15     U32		gp_cvgen;	/* generational validity of cached gp_cv */
16     U32		gp_refcnt;	/* how many globs point to this? */
17     HV *	gp_hv;		/* hash value */
18     AV *	gp_av;		/* array value */
19     CV *	gp_form;	/* format value */
20     GV *	gp_egv;		/* effective gv, if *glob */
21     PERL_BITFIELD32 gp_line:31;	/* line first declared at (for -w) */
22     PERL_BITFIELD32 gp_flags:1;
23     HEK *	gp_file_hek;	/* file first declared in (for -w) */
24 };
25 
26 #define GvXPVGV(gv)	((XPVGV*)SvANY(gv))
27 
28 
29 #if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS) && !defined(__INTEL_COMPILER)
30 #  define GvGP(gv)							\
31         ((GP *)(*({GV *const _gvgp = (GV *) (gv);				\
32             assert(SvTYPE(_gvgp) == SVt_PVGV || SvTYPE(_gvgp) == SVt_PVLV); \
33             assert(isGV_with_GP(_gvgp));				\
34             &((_gvgp)->sv_u.svu_gp);})))
35 #  define GvGP_set(gv,gp)						\
36         {GV *const _gvgp = (GV *) (gv);				\
37             assert(SvTYPE(_gvgp) == SVt_PVGV || SvTYPE(_gvgp) == SVt_PVLV); \
38             assert(isGV_with_GP(_gvgp));				\
39             (_gvgp)->sv_u.svu_gp = (gp); }
40 #  define GvFLAGS(gv)							\
41         (*({GV *const _gvflags = (GV *) (gv);				\
42             assert(SvTYPE(_gvflags) == SVt_PVGV || SvTYPE(_gvflags) == SVt_PVLV); \
43             assert(isGV_with_GP(_gvflags));				\
44             &(GvXPVGV(_gvflags)->xpv_cur);}))
45 #  define GvSTASH(gv)							\
46         (*({ GV * const _gvstash = (GV *) (gv);				\
47             assert(isGV_with_GP(_gvstash));				\
48             assert(SvTYPE(_gvstash) == SVt_PVGV || SvTYPE(_gvstash) >= SVt_PVLV); \
49             &(GvXPVGV(_gvstash)->xnv_u.xgv_stash);			\
50          }))
51 #  define GvNAME_HEK(gv)						\
52     (*({ GV * const _gvname_hek = (GV *) (gv);				\
53            assert(isGV_with_GP(_gvname_hek));				\
54            assert(SvTYPE(_gvname_hek) == SVt_PVGV || SvTYPE(_gvname_hek) >= SVt_PVLV); \
55            &(GvXPVGV(_gvname_hek)->xiv_u.xivu_namehek);			\
56          }))
57 #  define GvNAME_get(gv)	({ assert(GvNAME_HEK(gv)); (char *)HEK_KEY(GvNAME_HEK(gv)); })
58 #  define GvNAMELEN_get(gv)	({ assert(GvNAME_HEK(gv)); HEK_LEN(GvNAME_HEK(gv)); })
59 #  define GvNAMEUTF8(gv)	({ assert(GvNAME_HEK(gv)); HEK_UTF8(GvNAME_HEK(gv)); })
60 #else
61 #  define GvGP(gv)	(0+(gv)->sv_u.svu_gp)
62 #  define GvGP_set(gv,gp)	((gv)->sv_u.svu_gp = (gp))
63 #  define GvFLAGS(gv)	(GvXPVGV(gv)->xpv_cur)
64 #  define GvSTASH(gv)	(GvXPVGV(gv)->xnv_u.xgv_stash)
65 #  define GvNAME_HEK(gv)	(GvXPVGV(gv)->xiv_u.xivu_namehek)
66 #  define GvNAME_get(gv)	HEK_KEY(GvNAME_HEK(gv))
67 #  define GvNAMELEN_get(gv)	HEK_LEN(GvNAME_HEK(gv))
68 #  define GvNAMEUTF8(gv)	HEK_UTF8(GvNAME_HEK(gv))
69 #endif
70 
71 #define GvNAME(gv)	GvNAME_get(gv)
72 #define GvNAMELEN(gv)	GvNAMELEN_get(gv)
73 
74 /*
75 =for apidoc Am|SV*|GvSV|GV* gv
76 
77 Return the SV from the GV.
78 
79 Prior to Perl v5.9.3, this would add a scalar if none existed.  Nowadays, use
80 C<L</GvSVn>> for that, or compile perl with S<C<-DPERL_CREATE_GVSV>>.  See
81 L<perl5100delta>.
82 
83 =for apidoc Am|SV*|GvSVn|GV* gv
84 Like C<L</GvSV>>, but creates an empty scalar if none already exists.
85 
86 =for apidoc Am|AV*|GvAV|GV* gv
87 
88 Return the AV from the GV.
89 
90 =for apidoc Am|HV*|GvHV|GV* gv
91 
92 Return the HV from the GV.
93 
94 =for apidoc Am|CV*|GvCV|GV* gv
95 
96 Return the CV from the GV.
97 
98 =cut
99 */
100 
101 #define GvSV(gv)	(GvGP(gv)->gp_sv)
102 #ifdef PERL_DONT_CREATE_GVSV
103 #define GvSVn(gv)	(*(GvGP(gv)->gp_sv ? \
104                          &(GvGP(gv)->gp_sv) : \
105                          &(GvGP(gv_SVadd(gv))->gp_sv)))
106 #else
107 #define GvSVn(gv)	GvSV(gv)
108 #endif
109 
110 #define GvREFCNT(gv)	(GvGP(gv)->gp_refcnt)
111 #define GvIO(gv)                         \
112  (                                        \
113      (gv)                                  \
114   && (                                      \
115          SvTYPE((const SV*)(gv)) == SVt_PVGV \
116       || SvTYPE((const SV*)(gv)) == SVt_PVLV  \
117      )                                         \
118   && GvGP(gv)                                   \
119    ? GvIOp(gv)                                   \
120    : NULL                                         \
121  )
122 #define GvIOp(gv)	(GvGP(gv)->gp_io)
123 #define GvIOn(gv)	(GvIO(gv) ? GvIOp(gv) : GvIOp(gv_IOadd(gv)))
124 
125 #define GvFORM(gv)	(GvGP(gv)->gp_form)
126 #define GvAV(gv)	(GvGP(gv)->gp_av)
127 
128 #define GvAVn(gv)	(GvGP(gv)->gp_av ? \
129                          GvGP(gv)->gp_av : \
130                          GvGP(gv_AVadd(gv))->gp_av)
131 #define GvHV(gv)	((GvGP(gv))->gp_hv)
132 
133 #define GvHVn(gv)	(GvGP(gv)->gp_hv ? \
134                          GvGP(gv)->gp_hv : \
135                          GvGP(gv_HVadd(gv))->gp_hv)
136 
137 #define GvCV(gv)	((CV*)GvGP(gv)->gp_cv)
138 #define GvCV_set(gv,cv)	(GvGP(gv)->gp_cv = (cv))
139 #define GvCVGEN(gv)	(GvGP(gv)->gp_cvgen)
140 #define GvCVu(gv)	(GvGP(gv)->gp_cvgen ? NULL : GvGP(gv)->gp_cv)
141 
142 #define GvGPFLAGS(gv)	(GvGP(gv)->gp_flags)
143 
144 #define GvLINE(gv)	(GvGP(gv)->gp_line)
145 #define GvFILE_HEK(gv)	(GvGP(gv)->gp_file_hek)
146 #define GvFILEx(gv)	HEK_KEY(GvFILE_HEK(gv))
147 #define GvFILE(gv)	(GvFILE_HEK(gv) ? GvFILEx(gv) : NULL)
148 #define GvFILEGV(gv)	(GvFILE_HEK(gv) ? gv_fetchfile(GvFILEx(gv)) : NULL)
149 
150 #define GvEGV(gv)	(GvGP(gv)->gp_egv)
151 #define GvEGVx(gv)	(isGV_with_GP(gv) ? GvEGV(gv) : NULL)
152 #define GvENAME(gv)	GvNAME(GvEGV(gv) ? GvEGV(gv) : gv)
153 #define GvENAMELEN(gv)  GvNAMELEN(GvEGV(gv) ? GvEGV(gv) : gv)
154 #define GvENAMEUTF8(gv) GvNAMEUTF8(GvEGV(gv) ? GvEGV(gv) : gv)
155 #define GvENAME_HEK(gv) GvNAME_HEK(GvEGV(gv) ? GvEGV(gv) : gv)
156 #define GvESTASH(gv)	GvSTASH(GvEGV(gv) ? GvEGV(gv) : gv)
157 
158 /* GVf_INTRO is one-shot flag which indicates that the next assignment
159    of a reference to the glob is to be localised; it distinguishes
160    'local *g = $ref' from '*g = $ref'.
161 */
162 #define GVf_INTRO	0x01
163 #define GVf_MULTI	0x02
164 #define GVf_ASSUMECV	0x04
165 #define GVf_RESERVED    0x08   /* unused */
166 #define GVf_IMPORTED	0xF0
167 #define GVf_IMPORTED_SV	  0x10
168 #define GVf_IMPORTED_AV	  0x20
169 #define GVf_IMPORTED_HV	  0x40
170 #define GVf_IMPORTED_CV	  0x80
171 
172 #define GvINTRO(gv)		(GvFLAGS(gv) & GVf_INTRO)
173 #define GvINTRO_on(gv)		(GvFLAGS(gv) |= GVf_INTRO)
174 #define GvINTRO_off(gv)		(GvFLAGS(gv) &= ~GVf_INTRO)
175 
176 #define GvMULTI(gv)		(GvFLAGS(gv) & GVf_MULTI)
177 #define GvMULTI_on(gv)		(GvFLAGS(gv) |= GVf_MULTI)
178 #define GvMULTI_off(gv)		(GvFLAGS(gv) &= ~GVf_MULTI)
179 
180 #define GvASSUMECV(gv)		(GvFLAGS(gv) & GVf_ASSUMECV)
181 #define GvASSUMECV_on(gv)	(GvFLAGS(gv) |= GVf_ASSUMECV)
182 #define GvASSUMECV_off(gv)	(GvFLAGS(gv) &= ~GVf_ASSUMECV)
183 
184 #define GvIMPORTED(gv)		(GvFLAGS(gv) & GVf_IMPORTED)
185 #define GvIMPORTED_on(gv)	(GvFLAGS(gv) |= GVf_IMPORTED)
186 #define GvIMPORTED_off(gv)	(GvFLAGS(gv) &= ~GVf_IMPORTED)
187 
188 #define GvIMPORTED_SV(gv)	(GvFLAGS(gv) & GVf_IMPORTED_SV)
189 #define GvIMPORTED_SV_on(gv)	(GvFLAGS(gv) |= GVf_IMPORTED_SV)
190 #define GvIMPORTED_SV_off(gv)	(GvFLAGS(gv) &= ~GVf_IMPORTED_SV)
191 
192 #define GvIMPORTED_AV(gv)	(GvFLAGS(gv) & GVf_IMPORTED_AV)
193 #define GvIMPORTED_AV_on(gv)	(GvFLAGS(gv) |= GVf_IMPORTED_AV)
194 #define GvIMPORTED_AV_off(gv)	(GvFLAGS(gv) &= ~GVf_IMPORTED_AV)
195 
196 #define GvIMPORTED_HV(gv)	(GvFLAGS(gv) & GVf_IMPORTED_HV)
197 #define GvIMPORTED_HV_on(gv)	(GvFLAGS(gv) |= GVf_IMPORTED_HV)
198 #define GvIMPORTED_HV_off(gv)	(GvFLAGS(gv) &= ~GVf_IMPORTED_HV)
199 
200 #define GvIMPORTED_CV(gv)	(GvFLAGS(gv) & GVf_IMPORTED_CV)
201 #define GvIMPORTED_CV_on(gv)	(GvFLAGS(gv) |= GVf_IMPORTED_CV)
202 #define GvIMPORTED_CV_off(gv)	(GvFLAGS(gv) &= ~GVf_IMPORTED_CV)
203 
204 #ifndef PERL_CORE
205 #  define GvIN_PAD(gv)		0
206 #  define GvIN_PAD_on(gv)	NOOP
207 #  define GvIN_PAD_off(gv)	NOOP
208 #  define Nullgv Null(GV*)
209 #endif
210 
211 #define DM_RUID      0x001
212 #define DM_EUID      0x002
213 #define DM_UID       (DM_RUID|DM_EUID)
214 #define DM_ARRAY_ISA 0x004
215 #define DM_RGID      0x010
216 #define DM_EGID      0x020
217 #define DM_GID       (DM_RGID|DM_EGID)
218 #define DM_DELAY     0x100
219 
220 /*
221  * symbol creation flags, for use in gv_fetchpv() and get_*v()
222  */
223 #define GV_ADD		0x01	/* add, if symbol not already there
224                                    For gv_name_set, adding a HEK for the first
225                                    time, so don't try to free what's there.  */
226 #define GV_ADDMULTI	0x02	/* add, pretending it has been added
227                                    already; used also by gv_init_* */
228 #define GV_ADDWARN	0x04	/* add, but warn if symbol wasn't already there */
229                 /*	0x08	   UNUSED */
230 #define GV_NOINIT	0x10	/* add, but don't init symbol, if type != PVGV */
231 /* This is used by toke.c to avoid turing placeholder constants in the symbol
232    table into full PVGVs with attached constant subroutines.  */
233 #define GV_NOADD_NOINIT	0x20	/* Don't add the symbol if it's not there.
234                                    Don't init it if it is there but ! PVGV */
235 #define GV_NOEXPAND	0x40	/* Don't expand SvOK() entries to PVGV */
236 #define GV_NOTQUAL	0x80	/* A plain symbol name, not qualified with a
237                                    package (so skip checks for :: and ')  */
238 #define GV_AUTOLOAD	0x100	/* gv_fetchmethod_flags() should AUTOLOAD  */
239 #define GV_CROAK	0x200	/* gv_fetchmethod_flags() should croak  */
240 #define GV_ADDMG	0x400	/* add if magical */
241 #define GV_NO_SVGMAGIC	0x800	/* Skip get-magic on an SV argument;
242                                    used only by gv_fetchsv(_nomg) */
243 #define GV_CACHE_ONLY	0x1000  /* return stash only if found in cache;
244                                    used only in flags parameter to gv_stash* family */
245 
246 /* Flags for gv_fetchmeth_pvn and gv_autoload_pvn*/
247 #define GV_SUPER	0x1000	/* SUPER::method */
248 #define GV_NOUNIVERSAL  0x2000  /* Skip UNIVERSAL lookup */
249 
250 /* Flags for gv_autoload_*/
251 #define GV_AUTOLOAD_ISMETHOD 1	/* autoloading a method? */
252 
253 /*      SVf_UTF8 (more accurately the return value from SvUTF8) is also valid
254         as a flag to various gv_* functions, so ensure it lies
255         outside this range.
256 */
257 
258 #define GV_NOADD_MASK \
259   (SVf_UTF8|GV_NOADD_NOINIT|GV_NOEXPAND|GV_NOTQUAL|GV_ADDMG|GV_NO_SVGMAGIC)
260 /* The bit flags that don't cause gv_fetchpv() to add a symbol if not
261    found (with the exception GV_ADDMG, which *might* cause the symbol
262    to be added) */
263 
264 /* gv_fetchfile_flags() */
265 #define GVF_NOADD       0x01    /* don't add the glob if it doesn't exist */
266 
267 #define gv_fullname3(sv,gv,prefix) gv_fullname4(sv,gv,prefix,TRUE)
268 #define gv_efullname3(sv,gv,prefix) gv_efullname4(sv,gv,prefix,TRUE)
269 #define gv_fetchmethod(stash, name) gv_fetchmethod_autoload(stash, name, TRUE)
270 #define gv_fetchsv_nomg(n,f,t) gv_fetchsv(n,(f)|GV_NO_SVGMAGIC,t)
271 #define gv_init(gv,stash,name,len,multi) \
272         gv_init_pvn(gv,stash,name,len,GV_ADDMULTI*cBOOL(multi))
273 #define gv_fetchmeth(stash,name,len,level) gv_fetchmeth_pvn(stash, name, len, level, 0)
274 #define gv_fetchmeth_autoload(stash,name,len,level) gv_fetchmeth_pvn_autoload(stash, name, len, level, 0)
275 #define gv_fetchmethod_flags(stash,name,flags) gv_fetchmethod_pv_flags(stash, name, flags)
276 
277 /*
278 =for apidoc gv_autoload4
279 Equivalent to C<L</gv_autoload_pvn>>.
280 
281 =cut
282 */
283 #define gv_autoload4(stash, name, len, autoload) \
284         gv_autoload_pvn(stash, name, len, cBOOL(autoload))
285 #define newGVgen(pack)  newGVgen_flags(pack, 0)
286 #define gv_method_changed(gv)		    \
287     (					     \
288         assert_(isGV_with_GP(gv))	      \
289         GvREFCNT(gv) > 1		       \
290             ? (void)++PL_sub_generation		\
291             : mro_method_changed_in(GvSTASH(gv)) \
292     )
293 
294 /*
295 =for apidoc      gv_AVadd
296 =for apidoc_item gv_HVadd
297 =for apidoc_item gv_IOadd
298 =for apidoc_item gv_SVadd
299 
300 Make sure there is a slot of the given type (AV, HV, IO, SV) in the GV C<gv>.
301 
302 =cut
303 */
304 
305 #define gv_AVadd(gv) gv_add_by_type((gv), SVt_PVAV)
306 #define gv_HVadd(gv) gv_add_by_type((gv), SVt_PVHV)
307 #define gv_IOadd(gv) gv_add_by_type((gv), SVt_PVIO)
308 #define gv_SVadd(gv) gv_add_by_type((gv), SVt_NULL)
309 
310 /*
311  * ex: set ts=8 sts=4 sw=4 et:
312  */
313