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 | |
71be2cbc |
22 | EXT char rcsid[]; |
93a17b20 |
23 | |
79072805 |
24 | GV * |
8ac85365 |
25 | gv_AVadd(register GV *gv) |
79072805 |
26 | { |
a0d0e21e |
27 | if (!gv || SvTYPE((SV*)gv) != SVt_PVGV) |
28 | croak("Bad symbol for array"); |
79072805 |
29 | if (!GvAV(gv)) |
30 | GvAV(gv) = newAV(); |
31 | return gv; |
32 | } |
33 | |
34 | GV * |
8ac85365 |
35 | gv_HVadd(register GV *gv) |
79072805 |
36 | { |
a0d0e21e |
37 | if (!gv || SvTYPE((SV*)gv) != SVt_PVGV) |
38 | croak("Bad symbol for hash"); |
79072805 |
39 | if (!GvHV(gv)) |
463ee0b2 |
40 | GvHV(gv) = newHV(); |
79072805 |
41 | return gv; |
42 | } |
43 | |
44 | GV * |
8ac85365 |
45 | gv_IOadd(register GV *gv) |
a0d0e21e |
46 | { |
47 | if (!gv || SvTYPE((SV*)gv) != SVt_PVGV) |
48 | croak("Bad symbol for filehandle"); |
49 | if (!GvIOp(gv)) |
50 | GvIOp(gv) = newIO(); |
51 | return gv; |
52 | } |
53 | |
54 | GV * |
8ac85365 |
55 | gv_fetchfile(char *name) |
79072805 |
56 | { |
e858de61 |
57 | dTHR; |
53d95988 |
58 | char smallbuf[256]; |
59 | char *tmpbuf; |
8ebc5c01 |
60 | STRLEN tmplen; |
79072805 |
61 | GV *gv; |
62 | |
53d95988 |
63 | tmplen = strlen(name) + 2; |
64 | if (tmplen < sizeof smallbuf) |
65 | tmpbuf = smallbuf; |
66 | else |
67 | New(603, tmpbuf, tmplen + 1, char); |
68 | tmpbuf[0] = '_'; |
69 | tmpbuf[1] = '<'; |
70 | strcpy(tmpbuf + 2, name); |
8ebc5c01 |
71 | gv = *(GV**)hv_fetch(defstash, tmpbuf, tmplen, TRUE); |
72 | if (!isGV(gv)) |
73 | gv_init(gv, defstash, tmpbuf, tmplen, FALSE); |
53d95988 |
74 | if (tmpbuf != smallbuf) |
75 | Safefree(tmpbuf); |
79072805 |
76 | sv_setpv(GvSV(gv), name); |
8ebc5c01 |
77 | if (*name == '/' && (instr(name, "/lib/") || instr(name, ".pm"))) |
a5f75d66 |
78 | GvMULTI_on(gv); |
84902520 |
79 | if (PERLDB_LINE) |
93a17b20 |
80 | hv_magic(GvHVn(gv_AVadd(gv)), gv, 'L'); |
79072805 |
81 | return gv; |
82 | } |
83 | |
463ee0b2 |
84 | void |
8ac85365 |
85 | gv_init(GV *gv, HV *stash, char *name, STRLEN len, int multi) |
463ee0b2 |
86 | { |
0f15f207 |
87 | dTHR; |
463ee0b2 |
88 | register GP *gp; |
55d729e4 |
89 | bool doproto = SvTYPE(gv) > SVt_NULL; |
90 | char *proto = (doproto && SvPOK(gv)) ? SvPVX(gv) : NULL; |
463ee0b2 |
91 | |
dc437b57 |
92 | sv_upgrade((SV*)gv, SVt_PVGV); |
55d729e4 |
93 | if (SvLEN(gv)) { |
94 | if (proto) { |
95 | SvPVX(gv) = NULL; |
96 | SvLEN(gv) = 0; |
97 | SvPOK_off(gv); |
98 | } else |
99 | Safefree(SvPVX(gv)); |
100 | } |
44a8e56a |
101 | Newz(602, gp, 1, GP); |
8990e307 |
102 | GvGP(gv) = gp_ref(gp); |
463ee0b2 |
103 | GvSV(gv) = NEWSV(72,0); |
104 | GvLINE(gv) = curcop->cop_line; |
8990e307 |
105 | GvFILEGV(gv) = curcop->cop_filegv; |
463ee0b2 |
106 | GvEGV(gv) = gv; |
107 | sv_magic((SV*)gv, (SV*)gv, '*', name, len); |
85aff577 |
108 | GvSTASH(gv) = (HV*)SvREFCNT_inc(stash); |
a0d0e21e |
109 | GvNAME(gv) = savepvn(name, len); |
463ee0b2 |
110 | GvNAMELEN(gv) = len; |
111 | if (multi) |
a5f75d66 |
112 | GvMULTI_on(gv); |
55d729e4 |
113 | if (doproto) { /* Replicate part of newSUB here. */ |
57ff9a15 |
114 | SvIOK_off(gv); |
55d729e4 |
115 | ENTER; |
116 | start_subparse(0,0); /* Create CV in compcv. */ |
117 | GvCV(gv) = compcv; |
118 | LEAVE; |
119 | |
120 | GvCVGEN(gv) = 0; |
121 | sub_generation++; |
122 | CvGV(GvCV(gv)) = (GV*)SvREFCNT_inc(gv); |
123 | CvFILEGV(GvCV(gv)) = curcop->cop_filegv; |
124 | CvSTASH(GvCV(gv)) = curstash; |
125 | #ifdef USE_THREADS |
126 | CvOWNER(GvCV(gv)) = 0; |
127 | New(666, CvMUTEXP(GvCV(gv)), 1, perl_mutex); |
128 | MUTEX_INIT(CvMUTEXP(GvCV(gv))); |
129 | #endif /* USE_THREADS */ |
130 | if (proto) { |
131 | sv_setpv((SV*)GvCV(gv), proto); |
132 | Safefree(proto); |
133 | } |
134 | } |
463ee0b2 |
135 | } |
136 | |
76e3520e |
137 | STATIC void |
8ac85365 |
138 | gv_init_sv(GV *gv, I32 sv_type) |
a0d0e21e |
139 | { |
140 | switch (sv_type) { |
141 | case SVt_PVIO: |
142 | (void)GvIOn(gv); |
143 | break; |
144 | case SVt_PVAV: |
145 | (void)GvAVn(gv); |
146 | break; |
147 | case SVt_PVHV: |
148 | (void)GvHVn(gv); |
149 | break; |
150 | } |
151 | } |
152 | |
79072805 |
153 | GV * |
8ac85365 |
154 | gv_fetchmeth(HV *stash, char *name, STRLEN len, I32 level) |
79072805 |
155 | { |
156 | AV* av; |
463ee0b2 |
157 | GV* topgv; |
79072805 |
158 | GV* gv; |
463ee0b2 |
159 | GV** gvp; |
748a9306 |
160 | CV* cv; |
a0d0e21e |
161 | |
162 | if (!stash) |
163 | return 0; |
44a8e56a |
164 | if ((level > 100) || (level < -100)) |
a0d0e21e |
165 | croak("Recursive inheritance detected"); |
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 */ |
178 | if (!GvCVGEN(topgv) || GvCVGEN(topgv) >= 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 | } |
463ee0b2 |
185 | } |
79072805 |
186 | |
9607fc9c |
187 | gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE); |
188 | av = (gvp && (gv = *gvp) && gv != (GV*)&sv_undef) ? GvAV(gv) : Nullav; |
189 | |
fb73857a |
190 | /* create and re-create @.*::SUPER::ISA on demand */ |
191 | if (!av || !SvMAGIC(av)) { |
9607fc9c |
192 | char* packname = HvNAME(stash); |
193 | STRLEN packlen = strlen(packname); |
194 | |
195 | if (packlen >= 7 && strEQ(packname + packlen - 7, "::SUPER")) { |
196 | HV* basestash; |
197 | |
198 | packlen -= 7; |
199 | basestash = gv_stashpvn(packname, packlen, TRUE); |
200 | gvp = (GV**)hv_fetch(basestash, "ISA", 3, FALSE); |
201 | if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) { |
e858de61 |
202 | dTHR; /* just for SvREFCNT_dec */ |
9607fc9c |
203 | gvp = (GV**)hv_fetch(stash, "ISA", 3, TRUE); |
204 | if (!gvp || !(gv = *gvp)) |
205 | croak("Cannot create %s::ISA", HvNAME(stash)); |
206 | if (SvTYPE(gv) != SVt_PVGV) |
207 | gv_init(gv, stash, "ISA", 3, TRUE); |
208 | SvREFCNT_dec(GvAV(gv)); |
209 | GvAV(gv) = (AV*)SvREFCNT_inc(av); |
210 | } |
211 | } |
212 | } |
213 | |
214 | if (av) { |
79072805 |
215 | SV** svp = AvARRAY(av); |
93965878 |
216 | /* NOTE: No support for tied ISA */ |
217 | I32 items = AvFILLp(av) + 1; |
79072805 |
218 | while (items--) { |
79072805 |
219 | SV* sv = *svp++; |
a0d0e21e |
220 | HV* basestash = gv_stashsv(sv, FALSE); |
9bbf4081 |
221 | if (!basestash) { |
79072805 |
222 | if (dowarn) |
a0d0e21e |
223 | warn("Can't locate package %s for @%s::ISA", |
463ee0b2 |
224 | SvPVX(sv), HvNAME(stash)); |
79072805 |
225 | continue; |
226 | } |
44a8e56a |
227 | gv = gv_fetchmeth(basestash, name, len, |
228 | (level >= 0) ? level + 1 : level - 1); |
229 | if (gv) |
230 | goto gotcha; |
79072805 |
231 | } |
232 | } |
a0d0e21e |
233 | |
9607fc9c |
234 | /* if at top level, try UNIVERSAL */ |
235 | |
44a8e56a |
236 | if (level == 0 || level == -1) { |
9607fc9c |
237 | HV* lastchance; |
238 | |
dc437b57 |
239 | if (lastchance = gv_stashpvn("UNIVERSAL", 9, FALSE)) { |
44a8e56a |
240 | if (gv = gv_fetchmeth(lastchance, name, len, |
241 | (level >= 0) ? level + 1 : level - 1)) { |
242 | gotcha: |
dc848c6f |
243 | /* |
244 | * Cache method in topgv if: |
245 | * 1. topgv has no synonyms (else inheritance crosses wires) |
246 | * 2. method isn't a stub (else AUTOLOAD fails spectacularly) |
247 | */ |
248 | if (topgv && |
249 | GvREFCNT(topgv) == 1 && |
250 | (cv = GvCV(gv)) && |
251 | (CvROOT(cv) || CvXSUB(cv))) |
252 | { |
44a8e56a |
253 | if (cv = GvCV(topgv)) |
254 | SvREFCNT_dec(cv); |
255 | GvCV(topgv) = (CV*)SvREFCNT_inc(GvCV(gv)); |
256 | GvCVGEN(topgv) = sub_generation; |
257 | } |
a0d0e21e |
258 | return gv; |
259 | } |
260 | } |
261 | } |
262 | |
79072805 |
263 | return 0; |
264 | } |
265 | |
266 | GV * |
8ac85365 |
267 | gv_fetchmethod(HV *stash, char *name) |
463ee0b2 |
268 | { |
dc848c6f |
269 | return gv_fetchmethod_autoload(stash, name, TRUE); |
270 | } |
271 | |
272 | GV * |
8ac85365 |
273 | gv_fetchmethod_autoload(HV *stash, char *name, I32 autoload) |
dc848c6f |
274 | { |
0f15f207 |
275 | dTHR; |
463ee0b2 |
276 | register char *nend; |
a0d0e21e |
277 | char *nsplit = 0; |
278 | GV* gv; |
463ee0b2 |
279 | |
280 | for (nend = name; *nend; nend++) { |
9607fc9c |
281 | if (*nend == '\'') |
a0d0e21e |
282 | nsplit = nend; |
9607fc9c |
283 | else if (*nend == ':' && *(nend + 1) == ':') |
284 | nsplit = ++nend; |
a0d0e21e |
285 | } |
286 | if (nsplit) { |
a0d0e21e |
287 | char *origname = name; |
288 | name = nsplit + 1; |
a0d0e21e |
289 | if (*nsplit == ':') |
290 | --nsplit; |
9607fc9c |
291 | if ((nsplit - origname) == 5 && strnEQ(origname, "SUPER", 5)) { |
292 | /* ->SUPER::method should really be looked up in original stash */ |
fc36a67e |
293 | SV *tmpstr = sv_2mortal(newSVpvf("%s::SUPER", |
294 | HvNAME(curcop->cop_stash))); |
9607fc9c |
295 | stash = gv_stashpvn(SvPVX(tmpstr), SvCUR(tmpstr), TRUE); |
296 | DEBUG_o( deb("Treating %s as %s::%s\n", |
297 | origname, HvNAME(stash), name) ); |
4633a7c4 |
298 | } |
9607fc9c |
299 | else |
300 | stash = gv_stashpvn(origname, nsplit - origname, TRUE); |
4633a7c4 |
301 | } |
302 | |
9607fc9c |
303 | gv = gv_fetchmeth(stash, name, nend - name, 0); |
a0d0e21e |
304 | if (!gv) { |
702887b4 |
305 | if (strEQ(name,"import")) |
dc437b57 |
306 | gv = (GV*)&sv_yes; |
dc848c6f |
307 | else if (autoload) |
54310121 |
308 | gv = gv_autoload4(stash, name, nend - name, TRUE); |
463ee0b2 |
309 | } |
dc848c6f |
310 | else if (autoload) { |
311 | CV* cv = GvCV(gv); |
09280a33 |
312 | if (!CvROOT(cv) && !CvXSUB(cv)) { |
313 | GV* stubgv; |
314 | GV* autogv; |
315 | |
316 | if (CvANON(cv)) |
317 | stubgv = gv; |
318 | else { |
319 | stubgv = CvGV(cv); |
320 | if (GvCV(stubgv) != cv) /* orphaned import */ |
321 | stubgv = gv; |
322 | } |
323 | autogv = gv_autoload4(GvSTASH(stubgv), |
324 | GvNAME(stubgv), GvNAMELEN(stubgv), TRUE); |
dc848c6f |
325 | if (autogv) |
326 | gv = autogv; |
327 | } |
328 | } |
44a8e56a |
329 | |
330 | return gv; |
331 | } |
332 | |
333 | GV* |
8ac85365 |
334 | gv_autoload4(HV *stash, char *name, STRLEN len, I32 method) |
44a8e56a |
335 | { |
336 | static char autoload[] = "AUTOLOAD"; |
337 | static STRLEN autolen = 8; |
338 | GV* gv; |
339 | CV* cv; |
340 | HV* varstash; |
341 | GV* vargv; |
342 | SV* varsv; |
343 | |
344 | if (len == autolen && strnEQ(name, autoload, autolen)) |
345 | return Nullgv; |
dc848c6f |
346 | if (!(gv = gv_fetchmeth(stash, autoload, autolen, FALSE))) |
347 | return Nullgv; |
348 | cv = GvCV(gv); |
349 | |
350 | /* |
351 | * Inheriting AUTOLOAD for non-methods works ... for now. |
352 | */ |
353 | if (dowarn && !method && (GvCVGEN(gv) || GvSTASH(gv) != stash)) |
354 | warn( |
355 | "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated", |
356 | HvNAME(stash), (int)len, name); |
44a8e56a |
357 | |
358 | /* |
359 | * Given &FOO::AUTOLOAD, set $FOO::AUTOLOAD to desired function name. |
360 | * The subroutine's original name may not be "AUTOLOAD", so we don't |
361 | * use that, but for lack of anything better we will use the sub's |
362 | * original package to look up $AUTOLOAD. |
363 | */ |
364 | varstash = GvSTASH(CvGV(cv)); |
365 | vargv = *(GV**)hv_fetch(varstash, autoload, autolen, TRUE); |
366 | if (!isGV(vargv)) |
367 | gv_init(vargv, varstash, autoload, autolen, FALSE); |
368 | varsv = GvSV(vargv); |
369 | sv_setpv(varsv, HvNAME(stash)); |
370 | sv_catpvn(varsv, "::", 2); |
371 | sv_catpvn(varsv, name, len); |
372 | SvTAINTED_off(varsv); |
a0d0e21e |
373 | return gv; |
374 | } |
375 | |
376 | HV* |
8ac85365 |
377 | gv_stashpv(char *name, I32 create) |
a0d0e21e |
378 | { |
dc437b57 |
379 | return gv_stashpvn(name, strlen(name), create); |
380 | } |
381 | |
382 | HV* |
8ac85365 |
383 | gv_stashpvn(char *name, U32 namelen, I32 create) |
dc437b57 |
384 | { |
46fc3d4c |
385 | char smallbuf[256]; |
386 | char *tmpbuf; |
a0d0e21e |
387 | HV *stash; |
388 | GV *tmpgv; |
dc437b57 |
389 | |
46fc3d4c |
390 | if (namelen + 3 < sizeof smallbuf) |
391 | tmpbuf = smallbuf; |
392 | else |
393 | New(606, tmpbuf, namelen + 3, char); |
dc437b57 |
394 | Copy(name,tmpbuf,namelen,char); |
395 | tmpbuf[namelen++] = ':'; |
396 | tmpbuf[namelen++] = ':'; |
397 | tmpbuf[namelen] = '\0'; |
46fc3d4c |
398 | tmpgv = gv_fetchpv(tmpbuf, create, SVt_PVHV); |
399 | if (tmpbuf != smallbuf) |
400 | Safefree(tmpbuf); |
a0d0e21e |
401 | if (!tmpgv) |
402 | return 0; |
403 | if (!GvHV(tmpgv)) |
404 | GvHV(tmpgv) = newHV(); |
405 | stash = GvHV(tmpgv); |
406 | if (!HvNAME(stash)) |
407 | HvNAME(stash) = savepv(name); |
408 | return stash; |
463ee0b2 |
409 | } |
410 | |
a0d0e21e |
411 | HV* |
8ac85365 |
412 | gv_stashsv(SV *sv, I32 create) |
a0d0e21e |
413 | { |
dc437b57 |
414 | register char *ptr; |
415 | STRLEN len; |
416 | ptr = SvPV(sv,len); |
417 | return gv_stashpvn(ptr, len, create); |
a0d0e21e |
418 | } |
419 | |
420 | |
463ee0b2 |
421 | GV * |
8ac85365 |
422 | gv_fetchpv(char *nambeg, I32 add, I32 sv_type) |
79072805 |
423 | { |
11343788 |
424 | dTHR; |
463ee0b2 |
425 | register char *name = nambeg; |
426 | register GV *gv = 0; |
79072805 |
427 | GV**gvp; |
79072805 |
428 | I32 len; |
429 | register char *namend; |
463ee0b2 |
430 | HV *stash = 0; |
9607fc9c |
431 | U32 add_gvflags = 0; |
79072805 |
432 | |
c07a80fd |
433 | if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */ |
434 | name++; |
435 | |
79072805 |
436 | for (namend = name; *namend; namend++) { |
463ee0b2 |
437 | if ((*namend == '\'' && namend[1]) || |
438 | (*namend == ':' && namend[1] == ':')) |
439 | { |
463ee0b2 |
440 | if (!stash) |
441 | stash = defstash; |
dc437b57 |
442 | if (!stash || !SvREFCNT(stash)) /* symbol table under destruction */ |
a0d0e21e |
443 | return Nullgv; |
463ee0b2 |
444 | |
85e6fe83 |
445 | len = namend - name; |
446 | if (len > 0) { |
3c78fafa |
447 | char smallbuf[256]; |
62b57502 |
448 | char *tmpbuf; |
62b57502 |
449 | |
3c78fafa |
450 | if (len + 3 < sizeof smallbuf) |
451 | tmpbuf = smallbuf; |
62b57502 |
452 | else |
453 | New(601, tmpbuf, len+3, char); |
a0d0e21e |
454 | Copy(name, tmpbuf, len, char); |
455 | tmpbuf[len++] = ':'; |
456 | tmpbuf[len++] = ':'; |
457 | tmpbuf[len] = '\0'; |
463ee0b2 |
458 | gvp = (GV**)hv_fetch(stash,tmpbuf,len,add); |
6fa846a0 |
459 | gv = gvp ? *gvp : Nullgv; |
460 | if (gv && gv != (GV*)&sv_undef) { |
461 | if (SvTYPE(gv) != SVt_PVGV) |
462 | gv_init(gv, stash, tmpbuf, len, (add & 2)); |
463 | else |
464 | GvMULTI_on(gv); |
465 | } |
3c78fafa |
466 | if (tmpbuf != smallbuf) |
62b57502 |
467 | Safefree(tmpbuf); |
6fa846a0 |
468 | if (!gv || gv == (GV*)&sv_undef) |
a0d0e21e |
469 | return Nullgv; |
85e6fe83 |
470 | |
463ee0b2 |
471 | if (!(stash = GvHV(gv))) |
472 | stash = GvHV(gv) = newHV(); |
85e6fe83 |
473 | |
463ee0b2 |
474 | if (!HvNAME(stash)) |
a0d0e21e |
475 | HvNAME(stash) = savepvn(nambeg, namend - nambeg); |
463ee0b2 |
476 | } |
477 | |
478 | if (*namend == ':') |
479 | namend++; |
480 | namend++; |
481 | name = namend; |
482 | if (!*name) |
dc437b57 |
483 | return gv ? gv : (GV*)*hv_fetch(defstash, "main::", 6, TRUE); |
79072805 |
484 | } |
79072805 |
485 | } |
a0d0e21e |
486 | len = namend - name; |
487 | if (!len) |
488 | len = 1; |
463ee0b2 |
489 | |
490 | /* No stash in name, so see how we can default */ |
491 | |
492 | if (!stash) { |
493 | if (isIDFIRST(*name)) { |
9607fc9c |
494 | bool global = FALSE; |
495 | |
463ee0b2 |
496 | if (isUPPER(*name)) { |
497 | if (*name > 'I') { |
498 | if (*name == 'S' && ( |
499 | strEQ(name, "SIG") || |
500 | strEQ(name, "STDIN") || |
501 | strEQ(name, "STDOUT") || |
502 | strEQ(name, "STDERR") )) |
503 | global = TRUE; |
504 | } |
505 | else if (*name > 'E') { |
506 | if (*name == 'I' && strEQ(name, "INC")) |
507 | global = TRUE; |
508 | } |
509 | else if (*name > 'A') { |
510 | if (*name == 'E' && strEQ(name, "ENV")) |
511 | global = TRUE; |
512 | } |
513 | else if (*name == 'A' && ( |
514 | strEQ(name, "ARGV") || |
515 | strEQ(name, "ARGVOUT") )) |
516 | global = TRUE; |
517 | } |
518 | else if (*name == '_' && !name[1]) |
519 | global = TRUE; |
9607fc9c |
520 | |
463ee0b2 |
521 | if (global) |
522 | stash = defstash; |
85e6fe83 |
523 | else if ((COP*)curcop == &compiling) { |
a0d0e21e |
524 | stash = curstash; |
748a9306 |
525 | if (add && (hints & HINT_STRICT_VARS) && |
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 || |
a5f75d66 |
534 | *gvp == (GV*)&sv_undef || |
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 |
554 | stash = curcop->cop_stash; |
555 | } |
556 | else |
557 | stash = defstash; |
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; |
93233ece |
565 | if (add & ~2) { |
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 | } |
93233ece |
577 | ++error_count; |
578 | stash = curstash ? curstash : defstash; /* avoid core dumps */ |
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); |
589 | if (!gvp || *gvp == (GV*)&sv_undef) |
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; |
93a17b20 |
663 | siggv = gv; |
a5f75d66 |
664 | GvMULTI_on(siggv); |
93a17b20 |
665 | hv = GvHVn(siggv); |
666 | hv_magic(hv, 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) |
671 | sv_setsv(*init,&sv_undef); |
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; |
93a17b20 |
681 | ampergv = gv; |
682 | sawampersand = TRUE; |
a0d0e21e |
683 | goto ro_magicalize; |
93a17b20 |
684 | |
685 | case '`': |
463ee0b2 |
686 | if (len > 1) |
687 | break; |
93a17b20 |
688 | leftgv = gv; |
689 | sawampersand = TRUE; |
a0d0e21e |
690 | goto ro_magicalize; |
93a17b20 |
691 | |
692 | case '\'': |
463ee0b2 |
693 | if (len > 1) |
694 | break; |
93a17b20 |
695 | rightgv = gv; |
696 | sawampersand = TRUE; |
a0d0e21e |
697 | goto ro_magicalize; |
93a17b20 |
698 | |
699 | case ':': |
463ee0b2 |
700 | if (len > 1) |
701 | break; |
93a17b20 |
702 | sv_setpv(GvSV(gv),chopset); |
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 '!': |
714 | if(len > 1) |
715 | break; |
1d3434b8 |
716 | if(sv_type > SVt_PV) { |
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 '*': |
731 | if (dowarn && len == 1 && sv_type == SVt_PV) |
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': |
ad8898e0 |
754 | case '\017': |
93a17b20 |
755 | case '\t': |
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"); |
784 | formfeed = GvSV(gv); |
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); |
a0d0e21e |
795 | sv_setpv(sv, 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 | { |
876 | if (hv != defstash) |
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; |
883 | curcop->cop_line = GvLINE(gv); |
a0d0e21e |
884 | filegv = GvFILEGV(gv); |
885 | 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 | { |
46fc3d4c |
898 | return gv_fetchpv(form("%s::_GEN_%ld", pack, (long)gensym++), |
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 */ |
917 | sub_generation++; |
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 */ |
937 | sub_generation++; |
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 | |
e7ea3e70 |
995 | if (mg && amtp->was_ok_am == amagic_generation |
996 | && amtp->was_ok_sub == 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 | |
a6006777 |
1010 | amt.was_ok_am = amagic_generation; |
1011 | amt.was_ok_sub = sub_generation; |
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 */ |
a0d0e21e |
1017 | if (gvp && ((gv = *gvp) != (GV*)&sv_undef && (hv = GvHV(gv)))) { |
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); |
dc437b57 |
1036 | if (svp && ((sv = *svp) != &sv_undef)) { |
a0d0e21e |
1037 | switch (SvTYPE(sv)) { |
1038 | default: |
1039 | if (!SvROK(sv)) { |
1040 | if (!SvOK(sv)) break; |
748a9306 |
1041 | gv = gv_fetchmethod(stash, SvPV(sv, 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", |
1102 | SvPV(GvSV(gv), 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; |
748a9306 |
1149 | int postpr=0, inc_dec_ass=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 */ |
1166 | switch (method) { |
1167 | case inc_amg: |
748a9306 |
1168 | if (((cv = cvp[off=add_ass_amg]) && (inc_dec_ass=1)) |
a0d0e21e |
1169 | || ((cv = cvp[off=add_amg]) && (postpr=1))) { |
1170 | right = &sv_yes; lr = -1; assign = 1; |
1171 | } |
1172 | break; |
1173 | case dec_amg: |
748a9306 |
1174 | if (((cv = cvp[off=subtr_ass_amg]) && (inc_dec_ass=1)) |
a0d0e21e |
1175 | || ((cv = cvp[off=subtr_amg]) && (postpr=1))) { |
1176 | right = &sv_yes; lr = -1; assign = 1; |
1177 | } |
1178 | break; |
1179 | case bool__amg: |
1180 | (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg])); |
1181 | break; |
1182 | case numer_amg: |
1183 | (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg])); |
1184 | break; |
1185 | case string_amg: |
1186 | (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg])); |
1187 | break; |
dc437b57 |
1188 | case not_amg: |
1189 | (void)((cv = cvp[off=bool__amg]) |
1190 | || (cv = cvp[off=numer_amg]) |
1191 | || (cv = cvp[off=string_amg])); |
1192 | postpr = 1; |
1193 | break; |
748a9306 |
1194 | case copy_amg: |
1195 | { |
76e3520e |
1196 | /* |
1197 | * SV* ref causes confusion with the interpreter variable of |
1198 | * the same name |
1199 | */ |
1200 | SV* tmpRef=SvRV(left); |
1201 | if (!SvROK(tmpRef) && SvTYPE(tmpRef) <= SVt_PVMG) { |
fc36a67e |
1202 | /* |
1203 | * Just to be extra cautious. Maybe in some |
1204 | * additional cases sv_setsv is safe, too. |
1205 | */ |
76e3520e |
1206 | SV* newref = newSVsv(tmpRef); |
748a9306 |
1207 | SvOBJECT_on(newref); |
76e3520e |
1208 | SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(tmpRef)); |
748a9306 |
1209 | return newref; |
1210 | } |
1211 | } |
1212 | break; |
a0d0e21e |
1213 | case abs_amg: |
748a9306 |
1214 | if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg]) |
a0d0e21e |
1215 | && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) { |
748a9306 |
1216 | SV* nullsv=sv_2mortal(newSViv(0)); |
a0d0e21e |
1217 | if (off1==lt_amg) { |
748a9306 |
1218 | SV* lessp = amagic_call(left,nullsv, |
a0d0e21e |
1219 | lt_amg,AMGf_noright); |
1220 | logic = SvTRUE(lessp); |
1221 | } else { |
748a9306 |
1222 | SV* lessp = amagic_call(left,nullsv, |
a0d0e21e |
1223 | ncmp_amg,AMGf_noright); |
1224 | logic = (SvNV(lessp) < 0); |
1225 | } |
1226 | if (logic) { |
1227 | if (off==subtr_amg) { |
1228 | right = left; |
748a9306 |
1229 | left = nullsv; |
a0d0e21e |
1230 | lr = 1; |
1231 | } |
1232 | } else { |
1233 | return left; |
1234 | } |
1235 | } |
1236 | break; |
1237 | case neg_amg: |
1238 | if (cv = cvp[off=subtr_amg]) { |
1239 | right = left; |
1240 | left = sv_2mortal(newSViv(0)); |
1241 | lr = 1; |
1242 | } |
1243 | break; |
1244 | default: |
1245 | goto not_found; |
1246 | } |
1247 | if (!cv) goto not_found; |
1248 | } else if (!(AMGf_noright & flags) && SvAMAGIC(right) |
1249 | && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c')) |
a6006777 |
1250 | && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr) |
1251 | ? (amtp = (AMT*)mg->mg_ptr)->table |
8ac85365 |
1252 | : (CV **) NULL)) |
a0d0e21e |
1253 | && (cv = cvp[off=method])) { /* Method for right |
1254 | * argument found */ |
1255 | lr=1; |
748a9306 |
1256 | } else if (((ocvp && oamtp->fallback > AMGfallNEVER |
4633a7c4 |
1257 | && (cvp=ocvp) && (lr = -1)) |
a0d0e21e |
1258 | || (cvp && amtp->fallback > AMGfallNEVER && (lr=1))) |
1259 | && !(flags & AMGf_unary)) { |
1260 | /* We look for substitution for |
1261 | * comparison operations and |
fc36a67e |
1262 | * concatenation */ |
a0d0e21e |
1263 | if (method==concat_amg || method==concat_ass_amg |
1264 | || method==repeat_amg || method==repeat_ass_amg) { |
1265 | return NULL; /* Delegate operation to string conversion */ |
1266 | } |
1267 | off = -1; |
1268 | switch (method) { |
1269 | case lt_amg: |
1270 | case le_amg: |
1271 | case gt_amg: |
1272 | case ge_amg: |
1273 | case eq_amg: |
1274 | case ne_amg: |
1275 | postpr = 1; off=ncmp_amg; break; |
1276 | case slt_amg: |
1277 | case sle_amg: |
1278 | case sgt_amg: |
1279 | case sge_amg: |
1280 | case seq_amg: |
1281 | case sne_amg: |
1282 | postpr = 1; off=scmp_amg; break; |
1283 | } |
1284 | if (off != -1) cv = cvp[off]; |
1285 | if (!cv) { |
1286 | goto not_found; |
1287 | } |
1288 | } else { |
a6006777 |
1289 | not_found: /* No method found, either report or croak */ |
a0d0e21e |
1290 | if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */ |
1291 | notfound = 1; lr = -1; |
1292 | } else if (cvp && (cv=cvp[nomethod_amg])) { |
1293 | notfound = 1; lr = 1; |
1294 | } else { |
46fc3d4c |
1295 | SV *msg; |
774d564b |
1296 | if (off==-1) off=method; |
46fc3d4c |
1297 | msg = sv_2mortal(newSVpvf( |
1298 | "Operation `%s': no method found,%sargument %s%s%s%s", |
a6006777 |
1299 | AMG_names[method + assignshift], |
e7ea3e70 |
1300 | (flags & AMGf_unary ? " " : "\n\tleft "), |
a0d0e21e |
1301 | SvAMAGIC(left)? |
1302 | "in overloaded package ": |
1303 | "has no overloaded magic", |
1304 | SvAMAGIC(left)? |
1305 | HvNAME(SvSTASH(SvRV(left))): |
1306 | "", |
1307 | SvAMAGIC(right)? |
e7ea3e70 |
1308 | ",\n\tright argument in overloaded package ": |
1309 | (flags & AMGf_unary |
1310 | ? "" |
1311 | : ",\n\tright argument has no overloaded magic"), |
a0d0e21e |
1312 | SvAMAGIC(right)? |
1313 | HvNAME(SvSTASH(SvRV(right))): |
46fc3d4c |
1314 | "")); |
a0d0e21e |
1315 | if (amtp && amtp->fallback >= AMGfallYES) { |
46fc3d4c |
1316 | DEBUG_o( deb("%s", SvPVX(msg)) ); |
a0d0e21e |
1317 | } else { |
fc36a67e |
1318 | croak("%_", msg); |
a0d0e21e |
1319 | } |
1320 | return NULL; |
1321 | } |
1322 | } |
1323 | } |
1324 | if (!notfound) { |
774d564b |
1325 | DEBUG_o( deb( |
46fc3d4c |
1326 | "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n", |
a6006777 |
1327 | AMG_names[off], |
748a9306 |
1328 | method+assignshift==off? "" : |
1329 | " (initially `", |
1330 | method+assignshift==off? "" : |
a6006777 |
1331 | AMG_names[method+assignshift], |
748a9306 |
1332 | method+assignshift==off? "" : "')", |
1333 | flags & AMGf_unary? "" : |
1334 | lr==1 ? " for right argument": " for left argument", |
1335 | flags & AMGf_unary? " for argument" : "", |
a0d0e21e |
1336 | HvNAME(stash), |
1337 | fl? ",\n\tassignment variant used": "") ); |
748a9306 |
1338 | /* Since we use shallow copy during assignment, we need |
1339 | * to dublicate the contents, probably calling user-supplied |
1340 | * version of copy operator |
1341 | */ |
c07a80fd |
1342 | if ((method + assignshift==off |
748a9306 |
1343 | && (assign || method==inc_amg || method==dec_amg)) |
1344 | || inc_dec_ass) RvDEEPCP(left); |
a0d0e21e |
1345 | } |
1346 | { |
1347 | dSP; |
1348 | BINOP myop; |
1349 | SV* res; |
54310121 |
1350 | bool oldcatch = CATCH_GET; |
a0d0e21e |
1351 | |
54310121 |
1352 | CATCH_SET(TRUE); |
a0d0e21e |
1353 | Zero(&myop, 1, BINOP); |
1354 | myop.op_last = (OP *) &myop; |
1355 | myop.op_next = Nullop; |
54310121 |
1356 | myop.op_flags = OPf_WANT_SCALAR | OPf_STACKED; |
a0d0e21e |
1357 | |
e336de0d |
1358 | PUSHSTACK(SI_OVERLOAD); |
a0d0e21e |
1359 | ENTER; |
462e5cf6 |
1360 | SAVEOP(); |
a0d0e21e |
1361 | op = (OP *) &myop; |
84902520 |
1362 | if (PERLDB_SUB && curstash != debstash) |
dc437b57 |
1363 | op->op_private |= OPpENTERSUB_DB; |
a0d0e21e |
1364 | PUTBACK; |
11343788 |
1365 | pp_pushmark(ARGS); |
a0d0e21e |
1366 | |
924508f0 |
1367 | EXTEND(SP, notfound + 5); |
a0d0e21e |
1368 | PUSHs(lr>0? right: left); |
1369 | PUSHs(lr>0? left: right); |
5167a5b1 |
1370 | PUSHs( lr > 0 ? &sv_yes : ( assign ? &sv_undef : &sv_no )); |
a0d0e21e |
1371 | if (notfound) { |
a6006777 |
1372 | PUSHs( sv_2mortal(newSVpv((char *)AMG_names[method + assignshift],0)) ); |
a0d0e21e |
1373 | } |
1374 | PUSHs((SV*)cv); |
1375 | PUTBACK; |
1376 | |
11343788 |
1377 | if (op = pp_entersub(ARGS)) |
76e3520e |
1378 | CALLRUNOPS(); |
a0d0e21e |
1379 | LEAVE; |
1380 | SPAGAIN; |
1381 | |
1382 | res=POPs; |
e336de0d |
1383 | POPSTACK(); |
54310121 |
1384 | CATCH_SET(oldcatch); |
a0d0e21e |
1385 | |
a0d0e21e |
1386 | if (postpr) { |
1387 | int ans; |
1388 | switch (method) { |
1389 | case le_amg: |
1390 | case sle_amg: |
1391 | ans=SvIV(res)<=0; break; |
1392 | case lt_amg: |
1393 | case slt_amg: |
1394 | ans=SvIV(res)<0; break; |
1395 | case ge_amg: |
1396 | case sge_amg: |
1397 | ans=SvIV(res)>=0; break; |
1398 | case gt_amg: |
1399 | case sgt_amg: |
1400 | ans=SvIV(res)>0; break; |
1401 | case eq_amg: |
1402 | case seq_amg: |
1403 | ans=SvIV(res)==0; break; |
1404 | case ne_amg: |
1405 | case sne_amg: |
1406 | ans=SvIV(res)!=0; break; |
1407 | case inc_amg: |
1408 | case dec_amg: |
bbce6d69 |
1409 | SvSetSV(left,res); return left; |
dc437b57 |
1410 | case not_amg: |
44a8e56a |
1411 | ans=!SvOK(res); break; |
a0d0e21e |
1412 | } |
54310121 |
1413 | return boolSV(ans); |
748a9306 |
1414 | } else if (method==copy_amg) { |
1415 | if (!SvROK(res)) { |
a6006777 |
1416 | croak("Copy method did not return a reference"); |
748a9306 |
1417 | } |
1418 | return SvREFCNT_inc(SvRV(res)); |
a0d0e21e |
1419 | } else { |
1420 | return res; |
1421 | } |
1422 | } |
1423 | } |
1424 | #endif /* OVERLOAD */ |
4e35701f |
1425 | |