Commit | Line | Data |
a0d0e21e |
1 | /* gv.c |
79072805 |
2 | * |
9607fc9c |
3 | * Copyright (c) 1991-1997, Larry Wall |
79072805 |
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 | * |
a0d0e21e |
8 | */ |
9 | |
10 | /* |
11 | * 'Mercy!' cried Gandalf. 'If the giving of information is to be the cure |
12 | * of your inquisitiveness, I shall spend all the rest of my days answering |
13 | * you. What more do you want to know?' |
14 | * 'The names of all the stars, and of all living things, and the whole |
15 | * history of Middle-earth and Over-heaven and of the Sundering Seas,' |
16 | * laughed Pippin. |
79072805 |
17 | */ |
18 | |
19 | #include "EXTERN.h" |
20 | #include "perl.h" |
21 | |
22 | GV * |
8ac85365 |
23 | gv_AVadd(register GV *gv) |
79072805 |
24 | { |
a0d0e21e |
25 | if (!gv || SvTYPE((SV*)gv) != SVt_PVGV) |
26 | croak("Bad symbol for array"); |
79072805 |
27 | if (!GvAV(gv)) |
28 | GvAV(gv) = newAV(); |
29 | return gv; |
30 | } |
31 | |
32 | GV * |
8ac85365 |
33 | gv_HVadd(register GV *gv) |
79072805 |
34 | { |
a0d0e21e |
35 | if (!gv || SvTYPE((SV*)gv) != SVt_PVGV) |
36 | croak("Bad symbol for hash"); |
79072805 |
37 | if (!GvHV(gv)) |
463ee0b2 |
38 | GvHV(gv) = newHV(); |
79072805 |
39 | return gv; |
40 | } |
41 | |
42 | GV * |
8ac85365 |
43 | gv_IOadd(register GV *gv) |
a0d0e21e |
44 | { |
45 | if (!gv || SvTYPE((SV*)gv) != SVt_PVGV) |
46 | croak("Bad symbol for filehandle"); |
47 | if (!GvIOp(gv)) |
48 | GvIOp(gv) = newIO(); |
49 | return gv; |
50 | } |
51 | |
52 | GV * |
8ac85365 |
53 | gv_fetchfile(char *name) |
79072805 |
54 | { |
e858de61 |
55 | dTHR; |
53d95988 |
56 | char smallbuf[256]; |
57 | char *tmpbuf; |
8ebc5c01 |
58 | STRLEN tmplen; |
79072805 |
59 | GV *gv; |
60 | |
53d95988 |
61 | tmplen = strlen(name) + 2; |
62 | if (tmplen < sizeof smallbuf) |
63 | tmpbuf = smallbuf; |
64 | else |
65 | New(603, tmpbuf, tmplen + 1, char); |
66 | tmpbuf[0] = '_'; |
67 | tmpbuf[1] = '<'; |
68 | strcpy(tmpbuf + 2, name); |
3280af22 |
69 | gv = *(GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, TRUE); |
8ebc5c01 |
70 | if (!isGV(gv)) |
3280af22 |
71 | gv_init(gv, PL_defstash, tmpbuf, tmplen, FALSE); |
53d95988 |
72 | if (tmpbuf != smallbuf) |
73 | Safefree(tmpbuf); |
79072805 |
74 | sv_setpv(GvSV(gv), name); |
8ebc5c01 |
75 | if (*name == '/' && (instr(name, "/lib/") || instr(name, ".pm"))) |
a5f75d66 |
76 | GvMULTI_on(gv); |
84902520 |
77 | if (PERLDB_LINE) |
93a17b20 |
78 | hv_magic(GvHVn(gv_AVadd(gv)), gv, 'L'); |
79072805 |
79 | return gv; |
80 | } |
81 | |
463ee0b2 |
82 | void |
8ac85365 |
83 | gv_init(GV *gv, HV *stash, char *name, STRLEN len, int multi) |
463ee0b2 |
84 | { |
0f15f207 |
85 | dTHR; |
463ee0b2 |
86 | register GP *gp; |
55d729e4 |
87 | bool doproto = SvTYPE(gv) > SVt_NULL; |
88 | char *proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL; |
463ee0b2 |
89 | |
dc437b57 |
90 | sv_upgrade((SV*)gv, SVt_PVGV); |
55d729e4 |
91 | if (SvLEN(gv)) { |
92 | if (proto) { |
93 | SvPVX(gv) = NULL; |
94 | SvLEN(gv) = 0; |
95 | SvPOK_off(gv); |
96 | } else |
97 | Safefree(SvPVX(gv)); |
98 | } |
44a8e56a |
99 | Newz(602, gp, 1, GP); |
8990e307 |
100 | GvGP(gv) = gp_ref(gp); |
463ee0b2 |
101 | GvSV(gv) = NEWSV(72,0); |
3280af22 |
102 | GvLINE(gv) = PL_curcop->cop_line; |
103 | GvFILEGV(gv) = PL_curcop->cop_filegv; |
005a453c |
104 | GvCVGEN(gv) = 0; |
463ee0b2 |
105 | GvEGV(gv) = gv; |
106 | sv_magic((SV*)gv, (SV*)gv, '*', name, len); |
85aff577 |
107 | GvSTASH(gv) = (HV*)SvREFCNT_inc(stash); |
a0d0e21e |
108 | GvNAME(gv) = savepvn(name, len); |
463ee0b2 |
109 | GvNAMELEN(gv) = len; |
110 | if (multi) |
a5f75d66 |
111 | GvMULTI_on(gv); |
55d729e4 |
112 | if (doproto) { /* Replicate part of newSUB here. */ |
57ff9a15 |
113 | SvIOK_off(gv); |
55d729e4 |
114 | ENTER; |
115 | start_subparse(0,0); /* Create CV in compcv. */ |
3280af22 |
116 | GvCV(gv) = PL_compcv; |
55d729e4 |
117 | LEAVE; |
118 | |
3280af22 |
119 | PL_sub_generation++; |
55d729e4 |
120 | CvGV(GvCV(gv)) = (GV*)SvREFCNT_inc(gv); |
3280af22 |
121 | CvFILEGV(GvCV(gv)) = PL_curcop->cop_filegv; |
122 | CvSTASH(GvCV(gv)) = PL_curstash; |
55d729e4 |
123 | #ifdef USE_THREADS |
124 | CvOWNER(GvCV(gv)) = 0; |
b0a484d2 |
125 | if (!CvMUTEXP(GvCV(gv))) |
126 | New(666, CvMUTEXP(GvCV(gv)), 1, perl_mutex); |
55d729e4 |
127 | MUTEX_INIT(CvMUTEXP(GvCV(gv))); |
128 | #endif /* USE_THREADS */ |
129 | if (proto) { |
130 | sv_setpv((SV*)GvCV(gv), proto); |
131 | Safefree(proto); |
132 | } |
133 | } |
463ee0b2 |
134 | } |
135 | |
76e3520e |
136 | STATIC void |
8ac85365 |
137 | gv_init_sv(GV *gv, I32 sv_type) |
a0d0e21e |
138 | { |
139 | switch (sv_type) { |
140 | case SVt_PVIO: |
141 | (void)GvIOn(gv); |
142 | break; |
143 | case SVt_PVAV: |
144 | (void)GvAVn(gv); |
145 | break; |
146 | case SVt_PVHV: |
147 | (void)GvHVn(gv); |
148 | break; |
149 | } |
150 | } |
151 | |
79072805 |
152 | GV * |
8ac85365 |
153 | gv_fetchmeth(HV *stash, char *name, STRLEN len, I32 level) |
79072805 |
154 | { |
155 | AV* av; |
463ee0b2 |
156 | GV* topgv; |
79072805 |
157 | GV* gv; |
463ee0b2 |
158 | GV** gvp; |
748a9306 |
159 | CV* cv; |
a0d0e21e |
160 | |
161 | if (!stash) |
162 | return 0; |
44a8e56a |
163 | if ((level > 100) || (level < -100)) |
3e0ccd42 |
164 | croak("Recursive inheritance detected while looking for method '%s' in package '%s'", |
165 | name, HvNAME(stash)); |
463ee0b2 |
166 | |
463ee0b2 |
167 | DEBUG_o( deb("Looking for method %s in package %s\n",name,HvNAME(stash)) ); |
44a8e56a |
168 | |
169 | gvp = (GV**)hv_fetch(stash, name, len, (level >= 0)); |
170 | if (!gvp) |
171 | topgv = Nullgv; |
172 | else { |
173 | topgv = *gvp; |
174 | if (SvTYPE(topgv) != SVt_PVGV) |
175 | gv_init(topgv, stash, name, len, TRUE); |
176 | if (cv = GvCV(topgv)) { |
177 | /* If genuine method or valid cache entry, use it */ |
3280af22 |
178 | if (!GvCVGEN(topgv) || GvCVGEN(topgv) == PL_sub_generation) |
7a4c00b4 |
179 | return topgv; |
44a8e56a |
180 | /* Stale cached entry: junk it */ |
181 | SvREFCNT_dec(cv); |
182 | GvCV(topgv) = cv = Nullcv; |
183 | GvCVGEN(topgv) = 0; |
748a9306 |
184 | } |
3280af22 |
185 | else if (GvCVGEN(topgv) == PL_sub_generation) |
005a453c |
186 | return 0; /* cache indicates sub doesn't exist */ |
463ee0b2 |
187 | } |
79072805 |
188 | |
9607fc9c |
189 | gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE); |
3280af22 |
190 | av = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : Nullav; |
9607fc9c |
191 | |
fb73857a |
192 | /* create and re-create @.*::SUPER::ISA on demand */ |
193 | if (!av || !SvMAGIC(av)) { |
9607fc9c |
194 | char* packname = HvNAME(stash); |
195 | STRLEN packlen = strlen(packname); |
196 | |
197 | if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) { |
198 | HV* basestash; |
199 | |
200 | packlen -= 7; |
201 | basestash = gv_stashpvn(packname, packlen, TRUE); |
202 | gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE); |
3280af22 |
203 | if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) { |
e858de61 |
204 | dTHR; /* just for SvREFCNT_dec */ |
9607fc9c |
205 | gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE); |
206 | if (!gvp || !(gv = *gvp)) |
207 | croak("Cannot create %s::ISA", HvNAME(stash)); |
208 | if (SvTYPE(gv) != SVt_PVGV) |
209 | gv_init(gv, stash, "ISA", 3, TRUE); |
210 | SvREFCNT_dec(GvAV(gv)); |
211 | GvAV(gv) = (AV*)SvREFCNT_inc(av); |
212 | } |
213 | } |
214 | } |
215 | |
216 | if (av) { |
79072805 |
217 | SV** svp = AvARRAY(av); |
93965878 |
218 | /* NOTE: No support for tied ISA */ |
219 | I32 items = AvFILLp(av) + 1; |
79072805 |
220 | while (items--) { |
79072805 |
221 | SV* sv = *svp++; |
a0d0e21e |
222 | HV* basestash = gv_stashsv(sv, FALSE); |
9bbf4081 |
223 | if (!basestash) { |
3280af22 |
224 | if (PL_dowarn) |
a0d0e21e |
225 | warn("Can't locate package %s for @%s::ISA", |
463ee0b2 |
226 | SvPVX(sv), HvNAME(stash)); |
79072805 |
227 | continue; |
228 | } |
44a8e56a |
229 | gv = gv_fetchmeth(basestash, name, len, |
230 | (level >= 0) ? level + 1 : level - 1); |
231 | if (gv) |
232 | goto gotcha; |
79072805 |
233 | } |
234 | } |
a0d0e21e |
235 | |
9607fc9c |
236 | /* if at top level, try UNIVERSAL */ |
237 | |
44a8e56a |
238 | if (level == 0 || level == -1) { |
9607fc9c |
239 | HV* lastchance; |
240 | |
dc437b57 |
241 | if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) { |
44a8e56a |
242 | if (gv = gv_fetchmeth(lastchance, name, len, |
243 | (level >= 0) ? level + 1 : level - 1)) { |
244 | gotcha: |
dc848c6f |
245 | /* |
246 | * Cache method in topgv if: |
247 | * 1. topgv has no synonyms (else inheritance crosses wires) |
248 | * 2. method isn't a stub (else AUTOLOAD fails spectacularly) |
249 | */ |
250 | if (topgv && |
251 | GvREFCNT(topgv) == 1 && |
252 | (cv = GvCV(gv)) && |
253 | (CvROOT(cv) || CvXSUB(cv))) |
254 | { |
44a8e56a |
255 | if (cv = GvCV(topgv)) |
256 | SvREFCNT_dec(cv); |
257 | GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv)); |
3280af22 |
258 | GvCVGEN(topgv) = PL_sub_generation; |
44a8e56a |
259 | } |
a0d0e21e |
260 | return gv; |
261 | } |
005a453c |
262 | else if (topgv && GvREFCNT(topgv) == 1) { |
263 | /* cache the fact that the method is not defined */ |
3280af22 |
264 | GvCVGEN(topgv) = PL_sub_generation; |
005a453c |
265 | } |
a0d0e21e |
266 | } |
267 | } |
268 | |
79072805 |
269 | return 0; |
270 | } |
271 | |
272 | GV * |
8ac85365 |
273 | gv_fetchmethod(HV *stash, char *name) |
463ee0b2 |
274 | { |
dc848c6f |
275 | return gv_fetchmethod_autoload(stash, name, TRUE); |
276 | } |
277 | |
278 | GV * |
8ac85365 |
279 | gv_fetchmethod_autoload(HV *stash, char *name, I32 autoload) |
dc848c6f |
280 | { |
0f15f207 |
281 | dTHR; |
463ee0b2 |
282 | register char *nend; |
a0d0e21e |
283 | char *nsplit = 0; |
284 | GV* gv; |
463ee0b2 |
285 | |
286 | for (nend = name; *nend; nend++) { |
9607fc9c |
287 | if (*nend == '\'') |
a0d0e21e |
288 | nsplit = nend; |
9607fc9c |
289 | else if (*nend == ':' && *(nend + 1) == ':') |
290 | nsplit = ++nend; |
a0d0e21e |
291 | } |
292 | if (nsplit) { |
a0d0e21e |
293 | char *origname = name; |
294 | name = nsplit + 1; |
a0d0e21e |
295 | if (*nsplit == ':') |
296 | --nsplit; |
9607fc9c |
297 | if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) { |
298 | /* ->SUPER::method should really be looked up in original stash */ |
fc36a67e |
299 | SV *tmpstr = sv_2mortal(newSVpvf("%s::SUPER", |
3280af22 |
300 | HvNAME(PL_curcop->cop_stash))); |
9607fc9c |
301 | stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE); |
302 | DEBUG_o( deb("Treating %s as %s::%s\n", |
303 | origname, HvNAME(stash), name) ); |
4633a7c4 |
304 | } |
9607fc9c |
305 | else |
306 | stash = gv_stashpvn(origname, nsplit - origname, TRUE); |
4633a7c4 |
307 | } |
308 | |
9607fc9c |
309 | gv = gv_fetchmeth(stash, name, nend - name, 0); |
a0d0e21e |
310 | if (!gv) { |
702887b4 |
311 | if (strEQ(name,"import")) |
3280af22 |
312 | gv = (GV*)&PL_sv_yes; |
dc848c6f |
313 | else if (autoload) |
54310121 |
314 | gv = gv_autoload4(stash, name, nend - name, TRUE); |
463ee0b2 |
315 | } |
dc848c6f |
316 | else if (autoload) { |
317 | CV* cv = GvCV(gv); |
09280a33 |
318 | if (!CvROOT(cv) && !CvXSUB(cv)) { |
319 | GV* stubgv; |
320 | GV* autogv; |
321 | |
322 | if (CvANON(cv)) |
323 | stubgv = gv; |
324 | else { |
325 | stubgv = CvGV(cv); |
326 | if (GvCV(stubgv) != cv) /* orphaned import */ |
327 | stubgv = gv; |
328 | } |
329 | autogv = gv_autoload4(GvSTASH(stubgv), |
330 | GvNAME(stubgv), GvNAMELEN(stubgv), TRUE); |
dc848c6f |
331 | if (autogv) |
332 | gv = autogv; |
333 | } |
334 | } |
44a8e56a |
335 | |
336 | return gv; |
337 | } |
338 | |
339 | GV* |
8ac85365 |
340 | gv_autoload4(HV *stash, char *name, STRLEN len, I32 method) |
44a8e56a |
341 | { |
342 | static char autoload[] = "AUTOLOAD"; |
343 | static STRLEN autolen = 8; |
344 | GV* gv; |
345 | CV* cv; |
346 | HV* varstash; |
347 | GV* vargv; |
348 | SV* varsv; |
349 | |
350 | if (len == autolen && strnEQ(name, autoload, autolen)) |
351 | return Nullgv; |
dc848c6f |
352 | if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE))) |
353 | return Nullgv; |
354 | cv = GvCV(gv); |
355 | |
356 | /* |
357 | * Inheriting AUTOLOAD for non-methods works ... for now. |
358 | */ |
3280af22 |
359 | if (PL_dowarn && !method && (GvCVGEN(gv) || GvSTASH(gv) != stash)) |
dc848c6f |
360 | warn( |
361 | "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated", |
362 | HvNAME(stash), (int)len, name); |
44a8e56a |
363 | |
364 | /* |
365 | * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name. |
366 | * The subroutine's original name may not be "AUTOLOAD", so we don't |
367 | * use that, but for lack of anything better we will use the sub's |
368 | * original package to look up $AUTOLOAD. |
369 | */ |
370 | varstash = GvSTASH(CvGV(cv)); |
371 | vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE); |
372 | if (!isGV(vargv)) |
373 | gv_init(vargv, varstash, autoload, autolen, FALSE); |
374 | varsv = GvSV(vargv); |
375 | sv_setpv(varsv, HvNAME(stash)); |
376 | sv_catpvn(varsv, "::", 2); |
377 | sv_catpvn(varsv, name, len); |
378 | SvTAINTED_off(varsv); |
a0d0e21e |
379 | return gv; |
380 | } |
381 | |
382 | HV* |
8ac85365 |
383 | gv_stashpv(char *name, I32 create) |
a0d0e21e |
384 | { |
dc437b57 |
385 | return gv_stashpvn(name, strlen(name), create); |
386 | } |
387 | |
388 | HV* |
8ac85365 |
389 | gv_stashpvn(char *name, U32 namelen, I32 create) |
dc437b57 |
390 | { |
46fc3d4c |
391 | char smallbuf[256]; |
392 | char *tmpbuf; |
a0d0e21e |
393 | HV *stash; |
394 | GV *tmpgv; |
dc437b57 |
395 | |
46fc3d4c |
396 | if (namelen + 3 < sizeof smallbuf) |
397 | tmpbuf = smallbuf; |
398 | else |
399 | New(606, tmpbuf, namelen + 3, char); |
dc437b57 |
400 | Copy(name,tmpbuf,namelen,char); |
401 | tmpbuf[namelen++] = ':'; |
402 | tmpbuf[namelen++] = ':'; |
403 | tmpbuf[namelen] = '\0'; |
46fc3d4c |
404 | tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV); |
405 | if (tmpbuf != smallbuf) |
406 | Safefree(tmpbuf); |
a0d0e21e |
407 | if (!tmpgv) |
408 | return 0; |
409 | if (!GvHV(tmpgv)) |
410 | GvHV(tmpgv) = newHV(); |
411 | stash = GvHV(tmpgv); |
412 | if (!HvNAME(stash)) |
413 | HvNAME(stash) = savepv(name); |
414 | return stash; |
463ee0b2 |
415 | } |
416 | |
a0d0e21e |
417 | HV* |
8ac85365 |
418 | gv_stashsv(SV *sv, I32 create) |
a0d0e21e |
419 | { |
dc437b57 |
420 | register char *ptr; |
421 | STRLEN len; |
422 | ptr = SvPV(sv,len); |
423 | return gv_stashpvn(ptr, len, create); |
a0d0e21e |
424 | } |
425 | |
426 | |
463ee0b2 |
427 | GV * |
8ac85365 |
428 | gv_fetchpv(char *nambeg, I32 add, I32 sv_type) |
79072805 |
429 | { |
11343788 |
430 | dTHR; |
463ee0b2 |
431 | register char *name = nambeg; |
432 | register GV *gv = 0; |
79072805 |
433 | GV**gvp; |
79072805 |
434 | I32 len; |
435 | register char *namend; |
463ee0b2 |
436 | HV *stash = 0; |
9607fc9c |
437 | U32 add_gvflags = 0; |
79072805 |
438 | |
c07a80fd |
439 | if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */ |
440 | name++; |
441 | |
79072805 |
442 | for (namend = name; *namend; namend++) { |
463ee0b2 |
443 | if ((*namend == '\'' && namend[1]) || |
444 | (*namend == ':' && namend[1] == ':')) |
445 | { |
463ee0b2 |
446 | if (!stash) |
3280af22 |
447 | stash = PL_defstash; |
dc437b57 |
448 | if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */ |
a0d0e21e |
449 | return Nullgv; |
463ee0b2 |
450 | |
85e6fe83 |
451 | len = namend - name; |
452 | if (len > 0) { |
3c78fafa |
453 | char smallbuf[256]; |
62b57502 |
454 | char *tmpbuf; |
62b57502 |
455 | |
3c78fafa |
456 | if (len + 3 < sizeof smallbuf) |
457 | tmpbuf = smallbuf; |
62b57502 |
458 | else |
459 | New(601, tmpbuf, len+3, char); |
a0d0e21e |
460 | Copy(name, tmpbuf, len, char); |
461 | tmpbuf[len++] = ':'; |
462 | tmpbuf[len++] = ':'; |
463 | tmpbuf[len] = '\0'; |
463ee0b2 |
464 | gvp = (GV**)hv_fetch(stash,tmpbuf,len,add); |
6fa846a0 |
465 | gv = gvp ? *gvp : Nullgv; |
3280af22 |
466 | if (gv && gv != (GV*)&PL_sv_undef) { |
6fa846a0 |
467 | if (SvTYPE(gv) != SVt_PVGV) |
0f303493 |
468 | gv_init(gv, stash, tmpbuf, len, (add & GV_ADDMULTI)); |
6fa846a0 |
469 | else |
470 | GvMULTI_on(gv); |
471 | } |
3c78fafa |
472 | if (tmpbuf != smallbuf) |
62b57502 |
473 | Safefree(tmpbuf); |
3280af22 |
474 | if (!gv || gv == (GV*)&PL_sv_undef) |
a0d0e21e |
475 | return Nullgv; |
85e6fe83 |
476 | |
463ee0b2 |
477 | if (!(stash = GvHV(gv))) |
478 | stash = GvHV(gv) = newHV(); |
85e6fe83 |
479 | |
463ee0b2 |
480 | if (!HvNAME(stash)) |
a0d0e21e |
481 | HvNAME(stash) = savepvn(nambeg, namend - nambeg); |
463ee0b2 |
482 | } |
483 | |
484 | if (*namend == ':') |
485 | namend++; |
486 | namend++; |
487 | name = namend; |
488 | if (!*name) |
3280af22 |
489 | return gv ? gv : (GV*)*hv_fetch(PL_defstash, "main::", 6, TRUE); |
79072805 |
490 | } |
79072805 |
491 | } |
a0d0e21e |
492 | len = namend - name; |
493 | if (!len) |
494 | len = 1; |
463ee0b2 |
495 | |
496 | /* No stash in name, so see how we can default */ |
497 | |
498 | if (!stash) { |
499 | if (isIDFIRST(*name)) { |
9607fc9c |
500 | bool global = FALSE; |
501 | |
463ee0b2 |
502 | if (isUPPER(*name)) { |
9d116dd7 |
503 | if (*name == 'S' && ( |
504 | strEQ(name, "SIG") || |
505 | strEQ(name, "STDIN") || |
506 | strEQ(name, "STDOUT") || |
507 | strEQ(name, "STDERR"))) |
508 | global = TRUE; |
509 | else if (*name == 'I' && strEQ(name, "INC")) |
510 | global = TRUE; |
511 | else if (*name == 'E' && strEQ(name, "ENV")) |
512 | global = TRUE; |
463ee0b2 |
513 | else if (*name == 'A' && ( |
514 | strEQ(name, "ARGV") || |
9d116dd7 |
515 | strEQ(name, "ARGVOUT"))) |
463ee0b2 |
516 | global = TRUE; |
517 | } |
518 | else if (*name == '_' && !name[1]) |
519 | global = TRUE; |
9607fc9c |
520 | |
463ee0b2 |
521 | if (global) |
3280af22 |
522 | stash = PL_defstash; |
523 | else if ((COP*)PL_curcop == &PL_compiling) { |
524 | stash = PL_curstash; |
525 | if (add && (PL_hints & HINT_STRICT_VARS) && |
748a9306 |
526 | sv_type != SVt_PVCV && |
527 | sv_type != SVt_PVGV && |
4633a7c4 |
528 | sv_type != SVt_PVFM && |
c07a80fd |
529 | sv_type != SVt_PVIO && |
377b8fbc |
530 | !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) ) |
748a9306 |
531 | { |
4633a7c4 |
532 | gvp = (GV**)hv_fetch(stash,name,len,0); |
533 | if (!gvp || |
3280af22 |
534 | *gvp == (GV*)&PL_sv_undef || |
a5f75d66 |
535 | SvTYPE(*gvp) != SVt_PVGV) |
536 | { |
4633a7c4 |
537 | stash = 0; |
a5f75d66 |
538 | } |
539 | else if (sv_type == SVt_PV && !GvIMPORTED_SV(*gvp) || |
540 | sv_type == SVt_PVAV && !GvIMPORTED_AV(*gvp) || |
541 | sv_type == SVt_PVHV && !GvIMPORTED_HV(*gvp) ) |
4633a7c4 |
542 | { |
a5f75d66 |
543 | warn("Variable \"%c%s\" is not imported", |
4633a7c4 |
544 | sv_type == SVt_PVAV ? '@' : |
545 | sv_type == SVt_PVHV ? '%' : '$', |
546 | name); |
8ebc5c01 |
547 | if (GvCVu(*gvp)) |
4633a7c4 |
548 | warn("(Did you mean &%s instead?)\n", name); |
a0d0e21e |
549 | stash = 0; |
4633a7c4 |
550 | } |
a0d0e21e |
551 | } |
85e6fe83 |
552 | } |
463ee0b2 |
553 | else |
3280af22 |
554 | stash = PL_curcop->cop_stash; |
463ee0b2 |
555 | } |
556 | else |
3280af22 |
557 | stash = PL_defstash; |
463ee0b2 |
558 | } |
559 | |
560 | /* By this point we should have a stash and a name */ |
561 | |
a0d0e21e |
562 | if (!stash) { |
93233ece |
563 | if (!add) |
a0d0e21e |
564 | return Nullgv; |
0f303493 |
565 | if (add & ~GV_ADDMULTI) { |
2c4aebbd |
566 | char sv_type_char = ((sv_type == SVt_PV) ? '$' |
567 | : (sv_type == SVt_PVAV) ? '@' |
568 | : (sv_type == SVt_PVHV) ? '%' |
569 | : 0); |
570 | if (sv_type_char) |
571 | warn("Global symbol \"%c%s\" requires explicit package name", |
572 | sv_type_char, name); |
573 | else |
93233ece |
574 | warn("Global symbol \"%s\" requires explicit package name", |
575 | name); |
a0d0e21e |
576 | } |
3280af22 |
577 | ++PL_error_count; |
578 | stash = PL_curstash ? PL_curstash : PL_defstash; /* avoid core dumps */ |
93233ece |
579 | add_gvflags = ((sv_type == SVt_PV) ? GVf_IMPORTED_SV |
580 | : (sv_type == SVt_PVAV) ? GVf_IMPORTED_AV |
581 | : (sv_type == SVt_PVHV) ? GVf_IMPORTED_HV |
582 | : 0); |
a0d0e21e |
583 | } |
584 | |
585 | if (!SvREFCNT(stash)) /* symbol table under destruction */ |
586 | return Nullgv; |
587 | |
79072805 |
588 | gvp = (GV**)hv_fetch(stash,name,len,add); |
3280af22 |
589 | if (!gvp || *gvp == (GV*)&PL_sv_undef) |
79072805 |
590 | return Nullgv; |
591 | gv = *gvp; |
592 | if (SvTYPE(gv) == SVt_PVGV) { |
a0d0e21e |
593 | if (add) { |
a5f75d66 |
594 | GvMULTI_on(gv); |
a0d0e21e |
595 | gv_init_sv(gv, sv_type); |
596 | } |
79072805 |
597 | return gv; |
55d729e4 |
598 | } else if (add & GV_NOINIT) { |
599 | return gv; |
79072805 |
600 | } |
93a17b20 |
601 | |
602 | /* Adding a new symbol */ |
603 | |
55d729e4 |
604 | if (add & GV_ADDWARN) |
a0d0e21e |
605 | warn("Had to create %s unexpectedly", nambeg); |
55d729e4 |
606 | gv_init(gv, stash, name, len, add & GV_ADDMULTI); |
a0d0e21e |
607 | gv_init_sv(gv, sv_type); |
9607fc9c |
608 | GvFLAGS(gv) |= add_gvflags; |
93a17b20 |
609 | |
610 | /* set up magic where warranted */ |
611 | switch (*name) { |
a0d0e21e |
612 | case 'A': |
613 | if (strEQ(name, "ARGV")) { |
614 | IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START; |
615 | } |
616 | break; |
617 | |
ed6116ce |
618 | case 'a': |
619 | case 'b': |
620 | if (len == 1) |
a5f75d66 |
621 | GvMULTI_on(gv); |
ed6116ce |
622 | break; |
a0d0e21e |
623 | case 'E': |
624 | if (strnEQ(name, "EXPORT", 6)) |
a5f75d66 |
625 | GvMULTI_on(gv); |
a0d0e21e |
626 | break; |
463ee0b2 |
627 | case 'I': |
628 | if (strEQ(name, "ISA")) { |
629 | AV* av = GvAVn(gv); |
a5f75d66 |
630 | GvMULTI_on(gv); |
a0d0e21e |
631 | sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0); |
93965878 |
632 | /* NOTE: No support for tied ISA */ |
55d729e4 |
633 | if ((add & GV_ADDMULTI) && strEQ(nambeg,"AnyDBM_File::ISA") |
634 | && AvFILLp(av) == -1) |
85e6fe83 |
635 | { |
a0d0e21e |
636 | char *pname; |
637 | av_push(av, newSVpv(pname = "NDBM_File",0)); |
dc437b57 |
638 | gv_stashpvn(pname, 9, TRUE); |
a0d0e21e |
639 | av_push(av, newSVpv(pname = "DB_File",0)); |
dc437b57 |
640 | gv_stashpvn(pname, 7, TRUE); |
a0d0e21e |
641 | av_push(av, newSVpv(pname = "GDBM_File",0)); |
dc437b57 |
642 | gv_stashpvn(pname, 9, TRUE); |
a0d0e21e |
643 | av_push(av, newSVpv(pname = "SDBM_File",0)); |
dc437b57 |
644 | gv_stashpvn(pname, 9, TRUE); |
a0d0e21e |
645 | av_push(av, newSVpv(pname = "ODBM_File",0)); |
dc437b57 |
646 | gv_stashpvn(pname, 9, TRUE); |
85e6fe83 |
647 | } |
463ee0b2 |
648 | } |
649 | break; |
a0d0e21e |
650 | #ifdef OVERLOAD |
651 | case 'O': |
652 | if (strEQ(name, "OVERLOAD")) { |
653 | HV* hv = GvHVn(gv); |
a5f75d66 |
654 | GvMULTI_on(gv); |
ea0efc06 |
655 | hv_magic(hv, gv, 'A'); |
a0d0e21e |
656 | } |
657 | break; |
658 | #endif /* OVERLOAD */ |
93a17b20 |
659 | case 'S': |
660 | if (strEQ(name, "SIG")) { |
661 | HV *hv; |
dc437b57 |
662 | I32 i; |
3280af22 |
663 | PL_siggv = gv; |
664 | GvMULTI_on(PL_siggv); |
665 | hv = GvHVn(PL_siggv); |
666 | hv_magic(hv, PL_siggv, 'S'); |
dc437b57 |
667 | for(i=1;sig_name[i];i++) { |
668 | SV ** init; |
669 | init=hv_fetch(hv,sig_name[i],strlen(sig_name[i]),1); |
670 | if(init) |
3280af22 |
671 | sv_setsv(*init,&PL_sv_undef); |
dc437b57 |
672 | psig_ptr[i] = 0; |
673 | psig_name[i] = 0; |
674 | } |
93a17b20 |
675 | } |
676 | break; |
677 | |
678 | case '&': |
463ee0b2 |
679 | if (len > 1) |
680 | break; |
3280af22 |
681 | PL_ampergv = gv; |
682 | PL_sawampersand = TRUE; |
a0d0e21e |
683 | goto ro_magicalize; |
93a17b20 |
684 | |
685 | case '`': |
463ee0b2 |
686 | if (len > 1) |
687 | break; |
3280af22 |
688 | PL_leftgv = gv; |
689 | PL_sawampersand = TRUE; |
a0d0e21e |
690 | goto ro_magicalize; |
93a17b20 |
691 | |
692 | case '\'': |
463ee0b2 |
693 | if (len > 1) |
694 | break; |
3280af22 |
695 | PL_rightgv = gv; |
696 | PL_sawampersand = TRUE; |
a0d0e21e |
697 | goto ro_magicalize; |
93a17b20 |
698 | |
699 | case ':': |
463ee0b2 |
700 | if (len > 1) |
701 | break; |
3280af22 |
702 | sv_setpv(GvSV(gv),PL_chopset); |
93a17b20 |
703 | goto magicalize; |
704 | |
ff0cee69 |
705 | case '?': |
706 | if (len > 1) |
707 | break; |
708 | #ifdef COMPLEX_STATUS |
709 | sv_upgrade(GvSV(gv), SVt_PVLV); |
710 | #endif |
711 | goto magicalize; |
712 | |
067391ea |
713 | case '!': |
4318d5a0 |
714 | if (len > 1) |
067391ea |
715 | break; |
3280af22 |
716 | if (sv_type > SVt_PV && PL_curcop != &PL_compiling) { |
067391ea |
717 | HV* stash = gv_stashpvn("Errno",5,FALSE); |
718 | if(!stash || !(gv_fetchmethod(stash, "TIEHASH"))) { |
719 | dSP; |
720 | PUTBACK; |
721 | perl_require_pv("Errno.pm"); |
722 | SPAGAIN; |
723 | stash = gv_stashpvn("Errno",5,FALSE); |
724 | if (!stash || !(gv_fetchmethod(stash, "TIEHASH"))) |
1d2dff63 |
725 | croak("Can't use %%! because Errno.pm is not available"); |
067391ea |
726 | } |
727 | } |
728 | goto magicalize; |
93a17b20 |
729 | case '#': |
a0d0e21e |
730 | case '*': |
3280af22 |
731 | if (PL_dowarn && len == 1 && sv_type == SVt_PV) |
a0d0e21e |
732 | warn("Use of $%s is deprecated", name); |
733 | /* FALL THROUGH */ |
734 | case '[': |
93a17b20 |
735 | case '^': |
736 | case '~': |
737 | case '=': |
738 | case '-': |
739 | case '%': |
740 | case '.': |
93a17b20 |
741 | case '(': |
742 | case ')': |
743 | case '<': |
744 | case '>': |
745 | case ',': |
746 | case '\\': |
747 | case '/': |
93a17b20 |
748 | case '|': |
748a9306 |
749 | case '\001': |
93a17b20 |
750 | case '\004': |
ad8898e0 |
751 | case '\005': |
748a9306 |
752 | case '\006': |
a0d0e21e |
753 | case '\010': |
9d116dd7 |
754 | case '\011': /* NOT \t in EBCDIC */ |
ad8898e0 |
755 | case '\017': |
93a17b20 |
756 | case '\020': |
757 | case '\024': |
758 | case '\027': |
463ee0b2 |
759 | if (len > 1) |
760 | break; |
761 | goto magicalize; |
762 | |
a0d0e21e |
763 | case '+': |
463ee0b2 |
764 | case '1': |
765 | case '2': |
766 | case '3': |
767 | case '4': |
768 | case '5': |
769 | case '6': |
770 | case '7': |
771 | case '8': |
772 | case '9': |
fb73857a |
773 | case '\023': |
a0d0e21e |
774 | ro_magicalize: |
775 | SvREADONLY_on(GvSV(gv)); |
93a17b20 |
776 | magicalize: |
463ee0b2 |
777 | sv_magic(GvSV(gv), (SV*)gv, 0, name, len); |
93a17b20 |
778 | break; |
779 | |
780 | case '\014': |
463ee0b2 |
781 | if (len > 1) |
782 | break; |
93a17b20 |
783 | sv_setpv(GvSV(gv),"\f"); |
3280af22 |
784 | PL_formfeed = GvSV(gv); |
93a17b20 |
785 | break; |
786 | case ';': |
463ee0b2 |
787 | if (len > 1) |
788 | break; |
93a17b20 |
789 | sv_setpv(GvSV(gv),"\034"); |
790 | break; |
463ee0b2 |
791 | case ']': |
792 | if (len == 1) { |
f86702cc |
793 | SV *sv = GvSV(gv); |
93a17b20 |
794 | sv_upgrade(sv, SVt_PVNV); |
3280af22 |
795 | sv_setpv(sv, PL_patchlevel); |
f86702cc |
796 | (void)sv_2nv(sv); |
797 | SvREADONLY_on(sv); |
93a17b20 |
798 | } |
799 | break; |
79072805 |
800 | } |
93a17b20 |
801 | return gv; |
79072805 |
802 | } |
803 | |
804 | void |
8ac85365 |
805 | gv_fullname3(SV *sv, GV *gv, char *prefix) |
79072805 |
806 | { |
807 | HV *hv = GvSTASH(gv); |
f967eb5f |
808 | if (!hv) { |
809 | SvOK_off(sv); |
79072805 |
810 | return; |
f967eb5f |
811 | } |
812 | sv_setpv(sv, prefix ? prefix : ""); |
79072805 |
813 | sv_catpv(sv,HvNAME(hv)); |
463ee0b2 |
814 | sv_catpvn(sv,"::", 2); |
79072805 |
815 | sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv)); |
816 | } |
817 | |
818 | void |
8ac85365 |
819 | gv_efullname3(SV *sv, GV *gv, char *prefix) |
79072805 |
820 | { |
f967eb5f |
821 | GV *egv = GvEGV(gv); |
748a9306 |
822 | if (!egv) |
823 | egv = gv; |
f6aff53a |
824 | gv_fullname3(sv, egv, prefix); |
825 | } |
826 | |
827 | /* XXX compatibility with versions <= 5.003. */ |
828 | void |
8ac85365 |
829 | gv_fullname(SV *sv, GV *gv) |
f6aff53a |
830 | { |
831 | gv_fullname3(sv, gv, sv == (SV*)gv ? "*" : ""); |
832 | } |
833 | |
834 | /* XXX compatibility with versions <= 5.003. */ |
835 | void |
8ac85365 |
836 | gv_efullname(SV *sv, GV *gv) |
f6aff53a |
837 | { |
838 | gv_efullname3(sv, gv, sv == (SV*)gv ? "*" : ""); |
79072805 |
839 | } |
840 | |
841 | IO * |
8ac85365 |
842 | newIO(void) |
79072805 |
843 | { |
11343788 |
844 | dTHR; |
79072805 |
845 | IO *io; |
8990e307 |
846 | GV *iogv; |
847 | |
848 | io = (IO*)NEWSV(0,0); |
a0d0e21e |
849 | sv_upgrade((SV *)io,SVt_PVIO); |
8990e307 |
850 | SvREFCNT(io) = 1; |
851 | SvOBJECT_on(io); |
c9de509e |
852 | iogv = gv_fetchpv("FileHandle::", FALSE, SVt_PVHV); |
853 | if (!iogv) |
854 | iogv = gv_fetchpv("IO::Handle::", TRUE, SVt_PVHV); |
8990e307 |
855 | SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv)); |
79072805 |
856 | return io; |
857 | } |
858 | |
859 | void |
8ac85365 |
860 | gv_check(HV *stash) |
79072805 |
861 | { |
11343788 |
862 | dTHR; |
79072805 |
863 | register HE *entry; |
864 | register I32 i; |
865 | register GV *gv; |
463ee0b2 |
866 | HV *hv; |
a0d0e21e |
867 | GV *filegv; |
463ee0b2 |
868 | |
8990e307 |
869 | if (!HvARRAY(stash)) |
870 | return; |
a0d0e21e |
871 | for (i = 0; i <= (I32) HvMAX(stash); i++) { |
dc437b57 |
872 | for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) { |
873 | if (HeKEY(entry)[HeKLEN(entry)-1] == ':' && |
874 | (gv = (GV*)HeVAL(entry)) && (hv = GvHV(gv)) && HvNAME(hv)) |
a0d0e21e |
875 | { |
3280af22 |
876 | if (hv != PL_defstash) |
a0d0e21e |
877 | gv_check(hv); /* nested package */ |
878 | } |
dc437b57 |
879 | else if (isALPHA(*HeKEY(entry))) { |
880 | gv = (GV*)HeVAL(entry); |
55d729e4 |
881 | if (SvTYPE(gv) != SVt_PVGV || GvMULTI(gv)) |
463ee0b2 |
882 | continue; |
3280af22 |
883 | PL_curcop->cop_line = GvLINE(gv); |
a0d0e21e |
884 | filegv = GvFILEGV(gv); |
3280af22 |
885 | PL_curcop->cop_filegv = filegv; |
a5f75d66 |
886 | if (filegv && GvMULTI(filegv)) /* Filename began with slash */ |
8990e307 |
887 | continue; |
dc437b57 |
888 | warn("Name \"%s::%s\" used only once: possible typo", |
a0d0e21e |
889 | HvNAME(stash), GvNAME(gv)); |
463ee0b2 |
890 | } |
79072805 |
891 | } |
892 | } |
893 | } |
894 | |
895 | GV * |
8ac85365 |
896 | newGVgen(char *pack) |
79072805 |
897 | { |
3280af22 |
898 | return gv_fetchpv(form("%s::_GEN_%ld", pack, (long)PL_gensym++), |
46fc3d4c |
899 | TRUE, SVt_PVGV); |
79072805 |
900 | } |
901 | |
902 | /* hopefully this is only called on local symbol table entries */ |
903 | |
904 | GP* |
8ac85365 |
905 | gp_ref(GP *gp) |
79072805 |
906 | { |
907 | gp->gp_refcnt++; |
44a8e56a |
908 | if (gp->gp_cv) { |
909 | if (gp->gp_cvgen) { |
910 | /* multi-named GPs cannot be used for method cache */ |
911 | SvREFCNT_dec(gp->gp_cv); |
912 | gp->gp_cv = Nullcv; |
913 | gp->gp_cvgen = 0; |
914 | } |
915 | else { |
916 | /* Adding a new name to a subroutine invalidates method cache */ |
3280af22 |
917 | PL_sub_generation++; |
44a8e56a |
918 | } |
919 | } |
79072805 |
920 | return gp; |
79072805 |
921 | } |
922 | |
923 | void |
8ac85365 |
924 | gp_free(GV *gv) |
79072805 |
925 | { |
79072805 |
926 | GP* gp; |
377b8fbc |
927 | CV* cv; |
79072805 |
928 | |
929 | if (!gv || !(gp = GvGP(gv))) |
930 | return; |
931 | if (gp->gp_refcnt == 0) { |
932 | warn("Attempt to free unreferenced glob pointers"); |
933 | return; |
934 | } |
44a8e56a |
935 | if (gp->gp_cv) { |
936 | /* Deleting the name of a subroutine invalidates method cache */ |
3280af22 |
937 | PL_sub_generation++; |
44a8e56a |
938 | } |
748a9306 |
939 | if (--gp->gp_refcnt > 0) { |
940 | if (gp->gp_egv == gv) |
941 | gp->gp_egv = 0; |
79072805 |
942 | return; |
748a9306 |
943 | } |
79072805 |
944 | |
8990e307 |
945 | SvREFCNT_dec(gp->gp_sv); |
946 | SvREFCNT_dec(gp->gp_av); |
947 | SvREFCNT_dec(gp->gp_hv); |
377b8fbc |
948 | SvREFCNT_dec(gp->gp_io); |
a6006777 |
949 | SvREFCNT_dec(gp->gp_cv); |
748a9306 |
950 | SvREFCNT_dec(gp->gp_form); |
951 | |
79072805 |
952 | Safefree(gp); |
953 | GvGP(gv) = 0; |
954 | } |
955 | |
956 | #if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286)) |
957 | #define MICROPORT |
958 | #endif |
959 | |
960 | #ifdef MICROPORT /* Microport 2.4 hack */ |
961 | AV *GvAVn(gv) |
962 | register GV *gv; |
963 | { |
964 | if (GvGP(gv)->gp_av) |
965 | return GvGP(gv)->gp_av; |
966 | else |
967 | return GvGP(gv_AVadd(gv))->gp_av; |
968 | } |
969 | |
970 | HV *GvHVn(gv) |
971 | register GV *gv; |
972 | { |
973 | if (GvGP(gv)->gp_hv) |
974 | return GvGP(gv)->gp_hv; |
975 | else |
976 | return GvGP(gv_HVadd(gv))->gp_hv; |
977 | } |
978 | #endif /* Microport 2.4 hack */ |
a0d0e21e |
979 | |
980 | #ifdef OVERLOAD |
981 | /* Updates and caches the CV's */ |
982 | |
983 | bool |
8ac85365 |
984 | Gv_AMupdate(HV *stash) |
a0d0e21e |
985 | { |
11343788 |
986 | dTHR; |
a0d0e21e |
987 | GV** gvp; |
988 | HV* hv; |
989 | GV* gv; |
990 | CV* cv; |
991 | MAGIC* mg=mg_find((SV*)stash,'c'); |
8ac85365 |
992 | AMT *amtp = (mg) ? (AMT*)mg->mg_ptr: (AMT *) NULL; |
a6006777 |
993 | AMT amt; |
a0d0e21e |
994 | |
3280af22 |
995 | if (mg && amtp->was_ok_am == PL_amagic_generation |
996 | && amtp->was_ok_sub == PL_sub_generation) |
a6006777 |
997 | return AMT_AMAGIC(amtp); |
998 | if (amtp && AMT_AMAGIC(amtp)) { /* Have table. */ |
748a9306 |
999 | int i; |
a6006777 |
1000 | for (i=1; i<NofAMmeth; i++) { |
748a9306 |
1001 | if (amtp->table[i]) { |
1002 | SvREFCNT_dec(amtp->table[i]); |
1003 | } |
1004 | } |
1005 | } |
a0d0e21e |
1006 | sv_unmagic((SV*)stash, 'c'); |
1007 | |
1008 | DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) ); |
1009 | |
3280af22 |
1010 | amt.was_ok_am = PL_amagic_generation; |
1011 | amt.was_ok_sub = PL_sub_generation; |
a6006777 |
1012 | amt.fallback = AMGfallNO; |
1013 | amt.flags = 0; |
1014 | |
1015 | #ifdef OVERLOAD_VIA_HASH |
1016 | gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE); /* A shortcut */ |
3280af22 |
1017 | if (gvp && ((gv = *gvp) != (GV*)&PL_sv_undef && (hv = GvHV(gv)))) { |
a0d0e21e |
1018 | int filled=0; |
1019 | int i; |
1020 | char *cp; |
a0d0e21e |
1021 | SV* sv; |
1022 | SV** svp; |
a0d0e21e |
1023 | |
1024 | /* Work with "fallback" key, which we assume to be first in AMG_names */ |
1025 | |
a6006777 |
1026 | if (( cp = (char *)AMG_names[0] ) && |
1027 | (svp = (SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) { |
a0d0e21e |
1028 | if (SvTRUE(sv)) amt.fallback=AMGfallYES; |
1029 | else if (SvOK(sv)) amt.fallback=AMGfallNEVER; |
1030 | } |
a6006777 |
1031 | for (i = 1; i < NofAMmeth; i++) { |
1032 | cv = 0; |
1033 | cp = (char *)AMG_names[i]; |
1034 | |
1035 | svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE); |
3280af22 |
1036 | if (svp && ((sv = *svp) != &PL_sv_undef)) { |
a0d0e21e |
1037 | switch (SvTYPE(sv)) { |
1038 | default: |
1039 | if (!SvROK(sv)) { |
1040 | if (!SvOK(sv)) break; |
6b88bc9c |
1041 | gv = gv_fetchmethod(stash, SvPV(sv, PL_na)); |
a0d0e21e |
1042 | if (gv) cv = GvCV(gv); |
1043 | break; |
1044 | } |
1045 | cv = (CV*)SvRV(sv); |
1046 | if (SvTYPE(cv) == SVt_PVCV) |
1047 | break; |
1048 | /* FALL THROUGH */ |
1049 | case SVt_PVHV: |
1050 | case SVt_PVAV: |
a6006777 |
1051 | croak("Not a subroutine reference in overload table"); |
a0d0e21e |
1052 | return FALSE; |
1053 | case SVt_PVCV: |
8ebc5c01 |
1054 | cv = (CV*)sv; |
1055 | break; |
a0d0e21e |
1056 | case SVt_PVGV: |
8ebc5c01 |
1057 | if (!(cv = GvCVu((GV*)sv))) |
1058 | cv = sv_2cv(sv, &stash, &gv, TRUE); |
1059 | break; |
a0d0e21e |
1060 | } |
1061 | if (cv) filled=1; |
1062 | else { |
a6006777 |
1063 | croak("Method for operation %s not found in package %.256s during blessing\n", |
a0d0e21e |
1064 | cp,HvNAME(stash)); |
1065 | return FALSE; |
1066 | } |
1067 | } |
a6006777 |
1068 | #else |
1069 | { |
1070 | int filled = 0; |
1071 | int i; |
9607fc9c |
1072 | const char *cp; |
a6006777 |
1073 | SV* sv = NULL; |
1074 | SV** svp; |
1075 | |
1076 | /* Work with "fallback" key, which we assume to be first in AMG_names */ |
1077 | |
9607fc9c |
1078 | if ( cp = AMG_names[0] ) { |
a6006777 |
1079 | /* Try to find via inheritance. */ |
774d564b |
1080 | gv = gv_fetchmeth(stash, "()", 2, -1); /* A cookie: "()". */ |
a6006777 |
1081 | if (gv) sv = GvSV(gv); |
1082 | |
774d564b |
1083 | if (!gv) goto no_table; |
a6006777 |
1084 | else if (SvTRUE(sv)) amt.fallback=AMGfallYES; |
1085 | else if (SvOK(sv)) amt.fallback=AMGfallNEVER; |
1086 | } |
1087 | |
1088 | for (i = 1; i < NofAMmeth; i++) { |
46fc3d4c |
1089 | SV *cookie = sv_2mortal(newSVpvf("(%s", cp = AMG_names[i])); |
44a8e56a |
1090 | DEBUG_o( deb("Checking overloading of `%s' in package `%.256s'\n", |
1091 | cp, HvNAME(stash)) ); |
46fc3d4c |
1092 | /* don't fill the cache while looking up! */ |
1093 | gv = gv_fetchmeth(stash, SvPVX(cookie), SvCUR(cookie), -1); |
1094 | cv = 0; |
44a8e56a |
1095 | if(gv && (cv = GvCV(gv))) { |
44a8e56a |
1096 | if (GvNAMELEN(CvGV(cv)) == 3 && strEQ(GvNAME(CvGV(cv)), "nil") |
1097 | && strEQ(HvNAME(GvSTASH(CvGV(cv))), "overload")) { |
1098 | /* GvSV contains the name of the method. */ |
1099 | GV *ngv; |
1100 | |
1101 | DEBUG_o( deb("Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n", |
3280af22 |
1102 | SvPV(GvSV(gv), PL_na), cp, HvNAME(stash)) ); |
dc848c6f |
1103 | if (!SvPOK(GvSV(gv)) |
1104 | || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)), |
1105 | FALSE))) |
1106 | { |
44a8e56a |
1107 | /* Can be an import stub (created by `can'). */ |
1108 | if (GvCVGEN(gv)) { |
1109 | croak("Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'", |
1110 | (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ), |
1111 | cp, HvNAME(stash)); |
1112 | } else |
1113 | croak("Cannot resolve method `%.256s' overloading `%s' in package `%.256s'", |
1114 | (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ), |
1115 | cp, HvNAME(stash)); |
1116 | } |
dc848c6f |
1117 | cv = GvCV(gv = ngv); |
44a8e56a |
1118 | } |
1119 | DEBUG_o( deb("Overloading `%s' in package `%.256s' via `%.256s::%.256s' \n", |
1120 | cp, HvNAME(stash), HvNAME(GvSTASH(CvGV(cv))), |
1121 | GvNAME(CvGV(cv))) ); |
1122 | filled = 1; |
1123 | } |
a6006777 |
1124 | #endif |
1125 | amt.table[i]=(CV*)SvREFCNT_inc(cv); |
a0d0e21e |
1126 | } |
a0d0e21e |
1127 | if (filled) { |
a6006777 |
1128 | AMT_AMAGIC_on(&amt); |
1129 | sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMT)); |
a0d0e21e |
1130 | return TRUE; |
1131 | } |
1132 | } |
a6006777 |
1133 | /* Here we have no table: */ |
774d564b |
1134 | no_table: |
a6006777 |
1135 | AMT_AMAGIC_off(&amt); |
1136 | sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(AMTS)); |
a0d0e21e |
1137 | return FALSE; |
1138 | } |
1139 | |
a0d0e21e |
1140 | SV* |
8ac85365 |
1141 | amagic_call(SV *left, SV *right, int method, int flags) |
a0d0e21e |
1142 | { |
11343788 |
1143 | dTHR; |
a0d0e21e |
1144 | MAGIC *mg; |
1145 | CV *cv; |
1146 | CV **cvp=NULL, **ocvp=NULL; |
1147 | AMT *amtp, *oamtp; |
1148 | int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0; |
ee239bfe |
1149 | int postpr = 0, force_cpy = 0, assignshift = assign ? 1 : 0; |
a0d0e21e |
1150 | HV* stash; |
1151 | if (!(AMGf_noleft & flags) && SvAMAGIC(left) |
1152 | && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c')) |
a6006777 |
1153 | && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr) |
1154 | ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table |
8ac85365 |
1155 | : (CV **) NULL)) |
748a9306 |
1156 | && ((cv = cvp[off=method+assignshift]) |
1157 | || (assign && amtp->fallback > AMGfallNEVER && /* fallback to |
1158 | * usual method */ |
1159 | (fl = 1, cv = cvp[off=method])))) { |
a0d0e21e |
1160 | lr = -1; /* Call method for left argument */ |
1161 | } else { |
1162 | if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) { |
1163 | int logic; |
1164 | |
1165 | /* look for substituted methods */ |
ee239bfe |
1166 | /* In all the covered cases we should be called with assign==0. */ |
a0d0e21e |
1167 | switch (method) { |
1168 | case inc_amg: |
ee239bfe |
1169 | force_cpy = 1; |
1170 | if ((cv = cvp[off=add_ass_amg]) |
1171 | || ((cv = cvp[off = add_amg]) && (force_cpy = 0, postpr = 1))) { |
3280af22 |
1172 | right = &PL_sv_yes; lr = -1; assign = 1; |
a0d0e21e |
1173 | } |
1174 | break; |
1175 | case dec_amg: |
ee239bfe |
1176 | force_cpy = 1; |
1177 | if ((cv = cvp[off = subtr_ass_amg]) |
1178 | || ((cv = cvp[off = subtr_amg]) && (force_cpy = 0, postpr=1))) { |
3280af22 |
1179 | right = &PL_sv_yes; lr = -1; assign = 1; |
a0d0e21e |
1180 | } |
1181 | break; |
1182 | case bool__amg: |
1183 | (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg])); |
1184 | break; |
1185 | case numer_amg: |
1186 | (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg])); |
1187 | break; |
1188 | case string_amg: |
1189 | (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg])); |
1190 | break; |
dc437b57 |
1191 | case not_amg: |
1192 | (void)((cv = cvp[off=bool__amg]) |
1193 | || (cv = cvp[off=numer_amg]) |
1194 | || (cv = cvp[off=string_amg])); |
1195 | postpr = 1; |
1196 | break; |
748a9306 |
1197 | case copy_amg: |
1198 | { |
76e3520e |
1199 | /* |
1200 | * SV* ref causes confusion with the interpreter variable of |
1201 | * the same name |
1202 | */ |
1203 | SV* tmpRef=SvRV(left); |
1204 | if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) { |
fc36a67e |
1205 | /* |
1206 | * Just to be extra cautious. Maybe in some |
1207 | * additional cases sv_setsv is safe, too. |
1208 | */ |
76e3520e |
1209 | SV* newref = newSVsv(tmpRef); |
748a9306 |
1210 | SvOBJECT_on(newref); |
76e3520e |
1211 | SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef)); |
748a9306 |
1212 | return newref; |
1213 | } |
1214 | } |
1215 | break; |
a0d0e21e |
1216 | case abs_amg: |
748a9306 |
1217 | if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg]) |
a0d0e21e |
1218 | && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) { |
748a9306 |
1219 | SV* nullsv=sv_2mortal(newSViv(0)); |
a0d0e21e |
1220 | if (off1==lt_amg) { |
748a9306 |
1221 | SV* lessp = amagic_call(left,nullsv, |
a0d0e21e |
1222 | lt_amg,AMGf_noright); |
1223 | logic = SvTRUE(lessp); |
1224 | } else { |
748a9306 |
1225 | SV* lessp = amagic_call(left,nullsv, |
a0d0e21e |
1226 | ncmp_amg,AMGf_noright); |
1227 | logic = (SvNV(lessp) < 0); |
1228 | } |
1229 | if (logic) { |
1230 | if (off==subtr_amg) { |
1231 | right = left; |
748a9306 |
1232 | left = nullsv; |
a0d0e21e |
1233 | lr = 1; |
1234 | } |
1235 | } else { |
1236 | return left; |
1237 | } |
1238 | } |
1239 | break; |
1240 | case neg_amg: |
1241 | if (cv = cvp[off=subtr_amg]) { |
1242 | right = left; |
1243 | left = sv_2mortal(newSViv(0)); |
1244 | lr = 1; |
1245 | } |
1246 | break; |
1247 | default: |
1248 | goto not_found; |
1249 | } |
1250 | if (!cv) goto not_found; |
1251 | } else if (!(AMGf_noright & flags) && SvAMAGIC(right) |
1252 | && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c')) |
a6006777 |
1253 | && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr) |
1254 | ? (amtp = (AMT*)mg->mg_ptr)->table |
8ac85365 |
1255 | : (CV **) NULL)) |
a0d0e21e |
1256 | && (cv = cvp[off=method])) { /* Method for right |
1257 | * argument found */ |
1258 | lr=1; |
748a9306 |
1259 | } else if (((ocvp && oamtp->fallback > AMGfallNEVER |
4633a7c4 |
1260 | && (cvp=ocvp) && (lr = -1)) |
a0d0e21e |
1261 | || (cvp && amtp->fallback > AMGfallNEVER && (lr=1))) |
1262 | && !(flags & AMGf_unary)) { |
1263 | /* We look for substitution for |
1264 | * comparison operations and |
fc36a67e |
1265 | * concatenation */ |
a0d0e21e |
1266 | if (method==concat_amg || method==concat_ass_amg |
1267 | || method==repeat_amg || method==repeat_ass_amg) { |
1268 | return NULL; /* Delegate operation to string conversion */ |
1269 | } |
1270 | off = -1; |
1271 | switch (method) { |
1272 | case lt_amg: |
1273 | case le_amg: |
1274 | case gt_amg: |
1275 | case ge_amg: |
1276 | case eq_amg: |
1277 | case ne_amg: |
1278 | postpr = 1; off=ncmp_amg; break; |
1279 | case slt_amg: |
1280 | case sle_amg: |
1281 | case sgt_amg: |
1282 | case sge_amg: |
1283 | case seq_amg: |
1284 | case sne_amg: |
1285 | postpr = 1; off=scmp_amg; break; |
1286 | } |
1287 | if (off != -1) cv = cvp[off]; |
1288 | if (!cv) { |
1289 | goto not_found; |
1290 | } |
1291 | } else { |
a6006777 |
1292 | not_found: /* No method found, either report or croak */ |
a0d0e21e |
1293 | if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */ |
1294 | notfound = 1; lr = -1; |
1295 | } else if (cvp && (cv=cvp[nomethod_amg])) { |
1296 | notfound = 1; lr = 1; |
1297 | } else { |
46fc3d4c |
1298 | SV *msg; |
774d564b |
1299 | if (off==-1) off=method; |
46fc3d4c |
1300 | msg = sv_2mortal(newSVpvf( |
1301 | "Operation `%s': no method found,%sargument %s%s%s%s", |
a6006777 |
1302 | AMG_names[method + assignshift], |
e7ea3e70 |
1303 | (flags & AMGf_unary ? " " : "\n\tleft "), |
a0d0e21e |
1304 | SvAMAGIC(left)? |
1305 | "in overloaded package ": |
1306 | "has no overloaded magic", |
1307 | SvAMAGIC(left)? |
1308 | HvNAME(SvSTASH(SvRV(left))): |
1309 | "", |
1310 | SvAMAGIC(right)? |
e7ea3e70 |
1311 | ",\n\tright argument in overloaded package ": |
1312 | (flags & AMGf_unary |
1313 | ? "" |
1314 | : ",\n\tright argument has no overloaded magic"), |
a0d0e21e |
1315 | SvAMAGIC(right)? |
1316 | HvNAME(SvSTASH(SvRV(right))): |
46fc3d4c |
1317 | "")); |
a0d0e21e |
1318 | if (amtp && amtp->fallback >= AMGfallYES) { |
46fc3d4c |
1319 | DEBUG_o( deb("%s", SvPVX(msg)) ); |
a0d0e21e |
1320 | } else { |
fc36a67e |
1321 | croak("%_", msg); |
a0d0e21e |
1322 | } |
1323 | return NULL; |
1324 | } |
ee239bfe |
1325 | force_cpy = force_cpy || assign; |
a0d0e21e |
1326 | } |
1327 | } |
1328 | if (!notfound) { |
774d564b |
1329 | DEBUG_o( deb( |
46fc3d4c |
1330 | "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n", |
a6006777 |
1331 | AMG_names[off], |
748a9306 |
1332 | method+assignshift==off? "" : |
1333 | " (initially `", |
1334 | method+assignshift==off? "" : |
a6006777 |
1335 | AMG_names[method+assignshift], |
748a9306 |
1336 | method+assignshift==off? "" : "')", |
1337 | flags & AMGf_unary? "" : |
1338 | lr==1 ? " for right argument": " for left argument", |
1339 | flags & AMGf_unary? " for argument" : "", |
a0d0e21e |
1340 | HvNAME(stash), |
1341 | fl? ",\n\tassignment variant used": "") ); |
ee239bfe |
1342 | } |
748a9306 |
1343 | /* Since we use shallow copy during assignment, we need |
1344 | * to dublicate the contents, probably calling user-supplied |
1345 | * version of copy operator |
1346 | */ |
ee239bfe |
1347 | /* We need to copy in following cases: |
1348 | * a) Assignment form was called. |
1349 | * assignshift==1, assign==T, method + 1 == off |
1350 | * b) Increment or decrement, called directly. |
1351 | * assignshift==0, assign==0, method + 0 == off |
1352 | * c) Increment or decrement, translated to assignment add/subtr. |
1353 | * assignshift==0, assign==T, |
1354 | * force_cpy == T |
1355 | * d) Increment or decrement, translated to nomethod. |
1356 | * assignshift==0, assign==0, |
1357 | * force_cpy == T |
1358 | * e) Assignment form translated to nomethod. |
1359 | * assignshift==1, assign==T, method + 1 != off |
1360 | * force_cpy == T |
1361 | */ |
1362 | /* off is method, method+assignshift, or a result of opcode substitution. |
1363 | * In the latter case assignshift==0, so only notfound case is important. |
1364 | */ |
1365 | if (( (method + assignshift == off) |
1366 | && (assign || (method == inc_amg) || (method == dec_amg))) |
1367 | || force_cpy) |
1368 | RvDEEPCP(left); |
a0d0e21e |
1369 | { |
1370 | dSP; |
1371 | BINOP myop; |
1372 | SV* res; |
54310121 |
1373 | bool oldcatch = CATCH_GET; |
a0d0e21e |
1374 | |
54310121 |
1375 | CATCH_SET(TRUE); |
a0d0e21e |
1376 | Zero(&myop, 1, BINOP); |
1377 | myop.op_last = (OP *) &myop; |
1378 | myop.op_next = Nullop; |
54310121 |
1379 | myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED; |
a0d0e21e |
1380 | |
e788e7d3 |
1381 | PUSHSTACKi(PERLSI_OVERLOAD); |
a0d0e21e |
1382 | ENTER; |
462e5cf6 |
1383 | SAVEOP(); |
533c011a |
1384 | PL_op = (OP *) &myop; |
3280af22 |
1385 | if (PERLDB_SUB && PL_curstash != PL_debstash) |
533c011a |
1386 | PL_op->op_private |= OPpENTERSUB_DB; |
a0d0e21e |
1387 | PUTBACK; |
11343788 |
1388 | pp_pushmark(ARGS); |
a0d0e21e |
1389 | |
924508f0 |
1390 | EXTEND(SP, notfound + 5); |
a0d0e21e |
1391 | PUSHs(lr>0? right: left); |
1392 | PUSHs(lr>0? left: right); |
3280af22 |
1393 | PUSHs( lr > 0 ? &PL_sv_yes : ( assign ? &PL_sv_undef : &PL_sv_no )); |
a0d0e21e |
1394 | if (notfound) { |
a6006777 |
1395 | PUSHs( sv_2mortal(newSVpv((char *)AMG_names[method + assignshift],0)) ); |
a0d0e21e |
1396 | } |
1397 | PUSHs((SV*)cv); |
1398 | PUTBACK; |
1399 | |
533c011a |
1400 | if (PL_op = pp_entersub(ARGS)) |
76e3520e |
1401 | CALLRUNOPS(); |
a0d0e21e |
1402 | LEAVE; |
1403 | SPAGAIN; |
1404 | |
1405 | res=POPs; |
d3acc0f7 |
1406 | POPSTACK; |
54310121 |
1407 | CATCH_SET(oldcatch); |
a0d0e21e |
1408 | |
a0d0e21e |
1409 | if (postpr) { |
1410 | int ans; |
1411 | switch (method) { |
1412 | case le_amg: |
1413 | case sle_amg: |
1414 | ans=SvIV(res)<=0; break; |
1415 | case lt_amg: |
1416 | case slt_amg: |
1417 | ans=SvIV(res)<0; break; |
1418 | case ge_amg: |
1419 | case sge_amg: |
1420 | ans=SvIV(res)>=0; break; |
1421 | case gt_amg: |
1422 | case sgt_amg: |
1423 | ans=SvIV(res)>0; break; |
1424 | case eq_amg: |
1425 | case seq_amg: |
1426 | ans=SvIV(res)==0; break; |
1427 | case ne_amg: |
1428 | case sne_amg: |
1429 | ans=SvIV(res)!=0; break; |
1430 | case inc_amg: |
1431 | case dec_amg: |
bbce6d69 |
1432 | SvSetSV(left,res); return left; |
dc437b57 |
1433 | case not_amg: |
44a8e56a |
1434 | ans=!SvOK(res); break; |
a0d0e21e |
1435 | } |
54310121 |
1436 | return boolSV(ans); |
748a9306 |
1437 | } else if (method==copy_amg) { |
1438 | if (!SvROK(res)) { |
a6006777 |
1439 | croak("Copy method did not return a reference"); |
748a9306 |
1440 | } |
1441 | return SvREFCNT_inc(SvRV(res)); |
a0d0e21e |
1442 | } else { |
1443 | return res; |
1444 | } |
1445 | } |
1446 | } |
1447 | #endif /* OVERLOAD */ |
4e35701f |
1448 | |