This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.002gamma: utils/h2xs.PL
[perl5.git] / gv.c
CommitLineData
a0d0e21e 1/* gv.c
79072805 2 *
a0d0e21e 3 * Copyright (c) 1991-1994, Larry Wall
79072805
LW
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
LW
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
LW
17 */
18
19#include "EXTERN.h"
20#include "perl.h"
21
463ee0b2 22extern char rcsid[];
93a17b20 23
79072805
LW
24GV *
25gv_AVadd(gv)
26register GV *gv;
27{
a0d0e21e
LW
28 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
29 croak("Bad symbol for array");
79072805
LW
30 if (!GvAV(gv))
31 GvAV(gv) = newAV();
32 return gv;
33}
34
35GV *
36gv_HVadd(gv)
37register GV *gv;
38{
a0d0e21e
LW
39 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
40 croak("Bad symbol for hash");
79072805 41 if (!GvHV(gv))
463ee0b2 42 GvHV(gv) = newHV();
79072805
LW
43 return gv;
44}
45
46GV *
a0d0e21e
LW
47gv_IOadd(gv)
48register GV *gv;
49{
50 if (!gv || SvTYPE((SV*)gv) != SVt_PVGV)
51 croak("Bad symbol for filehandle");
52 if (!GvIOp(gv))
53 GvIOp(gv) = newIO();
54 return gv;
55}
56
57GV *
79072805
LW
58gv_fetchfile(name)
59char *name;
60{
61 char tmpbuf[1200];
62 GV *gv;
63
8990e307 64 sprintf(tmpbuf,"::_<%s", name);
85e6fe83 65 gv = gv_fetchpv(tmpbuf, TRUE, SVt_PVGV);
79072805 66 sv_setpv(GvSV(gv), name);
a0d0e21e 67 if (*name == '/' && (instr(name,"/lib/") || instr(name,".pm")))
8990e307 68 SvMULTI_on(gv);
79072805 69 if (perldb)
93a17b20 70 hv_magic(GvHVn(gv_AVadd(gv)), gv, 'L');
79072805
LW
71 return gv;
72}
73
463ee0b2
LW
74void
75gv_init(gv, stash, name, len, multi)
76GV *gv;
77HV *stash;
78char *name;
79STRLEN len;
80int multi;
81{
82 register GP *gp;
83
84 sv_upgrade(gv, SVt_PVGV);
85 if (SvLEN(gv))
86 Safefree(SvPVX(gv));
87 Newz(602,gp, 1, GP);
8990e307 88 GvGP(gv) = gp_ref(gp);
463ee0b2
LW
89 GvREFCNT(gv) = 1;
90 GvSV(gv) = NEWSV(72,0);
91 GvLINE(gv) = curcop->cop_line;
8990e307 92 GvFILEGV(gv) = curcop->cop_filegv;
463ee0b2
LW
93 GvEGV(gv) = gv;
94 sv_magic((SV*)gv, (SV*)gv, '*', name, len);
95 GvSTASH(gv) = stash;
a0d0e21e 96 GvNAME(gv) = savepvn(name, len);
463ee0b2
LW
97 GvNAMELEN(gv) = len;
98 if (multi)
99 SvMULTI_on(gv);
100}
101
a0d0e21e
LW
102static void
103gv_init_sv(gv, sv_type)
104GV* gv;
105I32 sv_type;
106{
107 switch (sv_type) {
108 case SVt_PVIO:
109 (void)GvIOn(gv);
110 break;
111 case SVt_PVAV:
112 (void)GvAVn(gv);
113 break;
114 case SVt_PVHV:
115 (void)GvHVn(gv);
116 break;
117 }
118}
119
79072805 120GV *
a0d0e21e 121gv_fetchmeth(stash, name, len, level)
79072805
LW
122HV* stash;
123char* name;
463ee0b2 124STRLEN len;
a0d0e21e 125I32 level;
79072805
LW
126{
127 AV* av;
463ee0b2 128 GV* topgv;
79072805 129 GV* gv;
463ee0b2 130 GV** gvp;
a0d0e21e 131 HV* lastchance;
748a9306 132 CV* cv;
a0d0e21e
LW
133
134 if (!stash)
135 return 0;
136 if (level > 100)
137 croak("Recursive inheritance detected");
463ee0b2
LW
138
139 gvp = (GV**)hv_fetch(stash, name, len, TRUE);
140
141 DEBUG_o( deb("Looking for method %s in package %s\n",name,HvNAME(stash)) );
142 topgv = *gvp;
143 if (SvTYPE(topgv) != SVt_PVGV)
144 gv_init(topgv, stash, name, len, TRUE);
145
748a9306
LW
146 if (cv=GvCV(topgv)) {
147 if (GvCVGEN(topgv) >= sub_generation)
148 return topgv; /* valid cached inheritance */
149 if (!GvCVGEN(topgv)) { /* not an inheritance cache */
16d20bd9 150 return topgv;
748a9306
LW
151 }
152 else {
153 /* stale cached entry, just junk it */
154 GvCV(topgv) = cv = 0;
155 GvCVGEN(topgv) = 0;
156 }
463ee0b2 157 }
748a9306 158 /* if cv is still set, we have to free it if we find something to cache */
79072805
LW
159
160 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
161 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) {
162 SV** svp = AvARRAY(av);
163 I32 items = AvFILL(av) + 1;
164 while (items--) {
79072805 165 SV* sv = *svp++;
a0d0e21e 166 HV* basestash = gv_stashsv(sv, FALSE);
9bbf4081 167 if (!basestash) {
79072805 168 if (dowarn)
a0d0e21e 169 warn("Can't locate package %s for @%s::ISA",
463ee0b2 170 SvPVX(sv), HvNAME(stash));
79072805
LW
171 continue;
172 }
a0d0e21e 173 gv = gv_fetchmeth(basestash, name, len, level + 1);
463ee0b2 174 if (gv) {
748a9306
LW
175 if (cv) { /* junk old undef */
176 assert(SvREFCNT(topgv) > 1);
177 SvREFCNT_dec(topgv);
178 SvREFCNT_dec(cv);
179 }
463ee0b2
LW
180 GvCV(topgv) = GvCV(gv); /* cache the CV */
181 GvCVGEN(topgv) = sub_generation; /* valid for now */
79072805 182 return gv;
463ee0b2 183 }
79072805
LW
184 }
185 }
a0d0e21e
LW
186
187 if (!level) {
188 if (lastchance = gv_stashpv("UNIVERSAL", FALSE)) {
189 if (gv = gv_fetchmeth(lastchance, name, len, level + 1)) {
748a9306
LW
190 if (cv) { /* junk old undef */
191 assert(SvREFCNT(topgv) > 1);
192 SvREFCNT_dec(topgv);
193 SvREFCNT_dec(cv);
194 }
a0d0e21e
LW
195 GvCV(topgv) = GvCV(gv); /* cache the CV */
196 GvCVGEN(topgv) = sub_generation; /* valid for now */
197 return gv;
198 }
199 }
200 }
201
79072805
LW
202 return 0;
203}
204
205GV *
463ee0b2
LW
206gv_fetchmethod(stash, name)
207HV* stash;
208char* name;
209{
210 register char *nend;
a0d0e21e
LW
211 char *nsplit = 0;
212 GV* gv;
463ee0b2
LW
213
214 for (nend = name; *nend; nend++) {
a0d0e21e
LW
215 if (*nend == ':' || *nend == '\'')
216 nsplit = nend;
217 }
218 if (nsplit) {
219 char ch;
220 char *origname = name;
221 name = nsplit + 1;
222 ch = *nsplit;
223 if (*nsplit == ':')
224 --nsplit;
225 *nsplit = '\0';
4633a7c4
LW
226 if (strEQ(origname,"SUPER")) {
227 /* Degenerate case ->SUPER::method should really lookup in original stash */
37120919 228 SV *tmpstr = sv_2mortal(newSVpv(HvNAME(curcop->cop_stash),0));
4633a7c4
LW
229 sv_catpvn(tmpstr, "::SUPER", 7);
230 stash = gv_stashpv(SvPV(tmpstr,na),TRUE);
231 *nsplit = ch;
232 DEBUG_o( deb("Treating %s as %s::%s\n",origname,HvNAME(stash),name) );
233 } else {
234 stash = gv_stashpv(origname,TRUE);
235 *nsplit = ch;
236 }
a0d0e21e
LW
237 }
238 gv = gv_fetchmeth(stash, name, nend - name, 0);
4633a7c4
LW
239
240 if (!gv) {
241 /* Failed obvious case - look for SUPER as last element of stash's name */
242 char *packname = HvNAME(stash);
243 STRLEN len = strlen(packname);
244 if (len >= 7 && strEQ(packname+len-7,"::SUPER")) {
245 /* Now look for @.*::SUPER::ISA */
246 GV** gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
247 if (!gvp || (gv = *gvp) == (GV*)&sv_undef || !GvAV(gv)) {
248 /* No @ISA in package ending in ::SUPER - drop suffix
249 and see if there is an @ISA there
250 */
251 HV *basestash;
252 char ch = packname[len-7];
253 AV *av;
254 packname[len-7] = '\0';
255 basestash = gv_stashpv(packname, TRUE);
256 packname[len-7] = ch;
257 gvp = (GV**)hv_fetch(basestash,"ISA",3,FALSE);
258 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) {
259 /* Okay found @ISA after dropping the SUPER, alias it */
260 SV *tmpstr = sv_2mortal(newSVpv(HvNAME(stash),0));
261 sv_catpvn(tmpstr, "::ISA", 5);
262 gv = gv_fetchpv(SvPV(tmpstr,na),TRUE,SVt_PVGV);
263 if (gv) {
264 GvAV(gv) = (AV*)SvREFCNT_inc(av);
265 /* ... and re-try lookup */
266 gv = gv_fetchmeth(stash, name, nend - name, 0);
267 } else {
268 croak("Cannot create %s::ISA",HvNAME(stash));
269 }
270 }
271 }
272 }
273 }
274
a0d0e21e
LW
275 if (!gv) {
276 CV* cv;
277
278 if (strEQ(name,"import") || strEQ(name,"unimport"))
279 gv = &sv_yes;
280 else if (strNE(name, "AUTOLOAD")) {
281 gv = gv_fetchmeth(stash, "AUTOLOAD", 8, 0);
282 if (gv && (cv = GvCV(gv))) { /* One more chance... */
748a9306 283 SV *tmpstr = sv_2mortal(newSVpv(HvNAME(stash),0));
a0d0e21e
LW
284 sv_catpvn(tmpstr,"::", 2);
285 sv_catpvn(tmpstr, name, nend - name);
286 sv_setsv(GvSV(CvGV(cv)), tmpstr);
c07a80fd
PP
287 if (tainting)
288 sv_unmagic(GvSV(CvGV(cv)), 't');
a0d0e21e 289 }
463ee0b2
LW
290 }
291 }
a0d0e21e
LW
292 return gv;
293}
294
295HV*
296gv_stashpv(name,create)
297char *name;
298I32 create;
299{
300 char tmpbuf[1234];
301 HV *stash;
302 GV *tmpgv;
06b43eb7
PP
303 /* Use strncpy to avoid bug in VMS sprintf */
304 /* sprintf(tmpbuf,"%.*s::",1200,name); */
305 strncpy(tmpbuf, name, 1200);
306 tmpbuf[1200] = '\0'; /* just in case . . . */
307 strcat(tmpbuf, "::");
a0d0e21e
LW
308 tmpgv = gv_fetchpv(tmpbuf,create, SVt_PVHV);
309 if (!tmpgv)
310 return 0;
311 if (!GvHV(tmpgv))
312 GvHV(tmpgv) = newHV();
313 stash = GvHV(tmpgv);
314 if (!HvNAME(stash))
315 HvNAME(stash) = savepv(name);
316 return stash;
463ee0b2
LW
317}
318
a0d0e21e
LW
319HV*
320gv_stashsv(sv,create)
321SV *sv;
322I32 create;
323{
324 return gv_stashpv(SvPV(sv,na), create);
325}
326
327
463ee0b2 328GV *
a0d0e21e 329gv_fetchpv(nambeg,add,sv_type)
463ee0b2 330char *nambeg;
79072805 331I32 add;
a0d0e21e 332I32 sv_type;
79072805 333{
463ee0b2
LW
334 register char *name = nambeg;
335 register GV *gv = 0;
79072805 336 GV**gvp;
79072805
LW
337 I32 len;
338 register char *namend;
463ee0b2 339 HV *stash = 0;
79072805 340 bool global = FALSE;
85e6fe83 341 char *tmpbuf;
79072805 342
c07a80fd
PP
343 if (*name == '*' && isALPHA(name[1])) /* accidental stringify on a GV? */
344 name++;
345
79072805 346 for (namend = name; *namend; namend++) {
463ee0b2
LW
347 if ((*namend == '\'' && namend[1]) ||
348 (*namend == ':' && namend[1] == ':'))
349 {
463ee0b2
LW
350 if (!stash)
351 stash = defstash;
a0d0e21e
LW
352 if (!SvREFCNT(stash)) /* symbol table under destruction */
353 return Nullgv;
463ee0b2 354
85e6fe83
LW
355 len = namend - name;
356 if (len > 0) {
a0d0e21e
LW
357 New(601, tmpbuf, len+3, char);
358 Copy(name, tmpbuf, len, char);
359 tmpbuf[len++] = ':';
360 tmpbuf[len++] = ':';
361 tmpbuf[len] = '\0';
463ee0b2 362 gvp = (GV**)hv_fetch(stash,tmpbuf,len,add);
85e6fe83 363 Safefree(tmpbuf);
463ee0b2
LW
364 if (!gvp || *gvp == (GV*)&sv_undef)
365 return Nullgv;
366 gv = *gvp;
85e6fe83 367
463ee0b2
LW
368 if (SvTYPE(gv) == SVt_PVGV)
369 SvMULTI_on(gv);
a0d0e21e
LW
370 else if (!add)
371 return Nullgv;
463ee0b2
LW
372 else
373 gv_init(gv, stash, nambeg, namend - nambeg, (add & 2));
85e6fe83 374
463ee0b2
LW
375 if (!(stash = GvHV(gv)))
376 stash = GvHV(gv) = newHV();
85e6fe83 377
463ee0b2 378 if (!HvNAME(stash))
a0d0e21e 379 HvNAME(stash) = savepvn(nambeg, namend - nambeg);
463ee0b2
LW
380 }
381
382 if (*namend == ':')
383 namend++;
384 namend++;
385 name = namend;
386 if (!*name)
a0d0e21e 387 return gv ? gv : *hv_fetch(defstash, "main::", 6, TRUE);
79072805 388 }
79072805 389 }
a0d0e21e
LW
390 len = namend - name;
391 if (!len)
392 len = 1;
463ee0b2
LW
393
394 /* No stash in name, so see how we can default */
395
396 if (!stash) {
397 if (isIDFIRST(*name)) {
398 if (isUPPER(*name)) {
399 if (*name > 'I') {
400 if (*name == 'S' && (
401 strEQ(name, "SIG") ||
402 strEQ(name, "STDIN") ||
403 strEQ(name, "STDOUT") ||
404 strEQ(name, "STDERR") ))
405 global = TRUE;
406 }
407 else if (*name > 'E') {
408 if (*name == 'I' && strEQ(name, "INC"))
409 global = TRUE;
410 }
411 else if (*name > 'A') {
412 if (*name == 'E' && strEQ(name, "ENV"))
413 global = TRUE;
414 }
415 else if (*name == 'A' && (
416 strEQ(name, "ARGV") ||
417 strEQ(name, "ARGVOUT") ))
418 global = TRUE;
419 }
420 else if (*name == '_' && !name[1])
421 global = TRUE;
422 if (global)
423 stash = defstash;
85e6fe83 424 else if ((COP*)curcop == &compiling) {
a0d0e21e 425 stash = curstash;
748a9306
LW
426 if (add && (hints & HINT_STRICT_VARS) &&
427 sv_type != SVt_PVCV &&
428 sv_type != SVt_PVGV &&
4633a7c4 429 sv_type != SVt_PVFM &&
c07a80fd 430 sv_type != SVt_PVIO &&
377b8fbc 431 !(len == 1 && sv_type == SVt_PV && strchr("ab",*name)) )
748a9306 432 {
4633a7c4
LW
433 gvp = (GV**)hv_fetch(stash,name,len,0);
434 if (!gvp ||
435 *gvp == (GV*)&sv_undef ||
436 SvTYPE(*gvp) != SVt_PVGV ||
437 !(GvFLAGS(*gvp) & GVf_IMPORTED))
438 stash = 0;
439 else if (sv_type == SVt_PVAV && !GvAV(*gvp) ||
440 sv_type == SVt_PVHV && !GvHV(*gvp) ||
c07a80fd 441 sv_type == SVt_PV && !GvSV(*gvp) )
4633a7c4
LW
442 {
443 warn("Variable \"%c%s\" is not exported",
444 sv_type == SVt_PVAV ? '@' :
445 sv_type == SVt_PVHV ? '%' : '$',
446 name);
447 if (GvCV(*gvp))
448 warn("(Did you mean &%s instead?)\n", name);
a0d0e21e 449 stash = 0;
4633a7c4 450 }
a0d0e21e 451 }
85e6fe83 452 }
463ee0b2
LW
453 else
454 stash = curcop->cop_stash;
455 }
456 else
457 stash = defstash;
458 }
459
460 /* By this point we should have a stash and a name */
461
a0d0e21e
LW
462 if (!stash) {
463 if (add) {
464 warn("Global symbol \"%s\" requires explicit package name", name);
465 ++error_count;
466 stash = curstash ? curstash : defstash; /* avoid core dumps */
467 }
468 else
469 return Nullgv;
470 }
471
472 if (!SvREFCNT(stash)) /* symbol table under destruction */
473 return Nullgv;
474
79072805
LW
475 gvp = (GV**)hv_fetch(stash,name,len,add);
476 if (!gvp || *gvp == (GV*)&sv_undef)
477 return Nullgv;
478 gv = *gvp;
479 if (SvTYPE(gv) == SVt_PVGV) {
a0d0e21e
LW
480 if (add) {
481 SvMULTI_on(gv);
482 gv_init_sv(gv, sv_type);
483 }
79072805
LW
484 return gv;
485 }
93a17b20
LW
486
487 /* Adding a new symbol */
488
a0d0e21e
LW
489 if (add & 4)
490 warn("Had to create %s unexpectedly", nambeg);
463ee0b2 491 gv_init(gv, stash, name, len, add & 2);
a0d0e21e 492 gv_init_sv(gv, sv_type);
93a17b20
LW
493
494 /* set up magic where warranted */
495 switch (*name) {
a0d0e21e
LW
496 case 'A':
497 if (strEQ(name, "ARGV")) {
498 IoFLAGS(GvIOn(gv)) |= IOf_ARGV|IOf_START;
499 }
500 break;
501
ed6116ce
LW
502 case 'a':
503 case 'b':
504 if (len == 1)
505 SvMULTI_on(gv);
506 break;
a0d0e21e
LW
507 case 'E':
508 if (strnEQ(name, "EXPORT", 6))
509 SvMULTI_on(gv);
510 break;
463ee0b2
LW
511 case 'I':
512 if (strEQ(name, "ISA")) {
513 AV* av = GvAVn(gv);
8990e307 514 SvMULTI_on(gv);
a0d0e21e
LW
515 sv_magic((SV*)av, (SV*)gv, 'I', Nullch, 0);
516 if (add & 2 && strEQ(nambeg,"AnyDBM_File::ISA") && AvFILL(av) == -1)
85e6fe83 517 {
a0d0e21e
LW
518 char *pname;
519 av_push(av, newSVpv(pname = "NDBM_File",0));
520 gv_stashpv(pname, TRUE);
521 av_push(av, newSVpv(pname = "DB_File",0));
522 gv_stashpv(pname, TRUE);
523 av_push(av, newSVpv(pname = "GDBM_File",0));
524 gv_stashpv(pname, TRUE);
525 av_push(av, newSVpv(pname = "SDBM_File",0));
526 gv_stashpv(pname, TRUE);
527 av_push(av, newSVpv(pname = "ODBM_File",0));
528 gv_stashpv(pname, TRUE);
85e6fe83 529 }
463ee0b2
LW
530 }
531 break;
a0d0e21e
LW
532#ifdef OVERLOAD
533 case 'O':
534 if (strEQ(name, "OVERLOAD")) {
535 HV* hv = GvHVn(gv);
536 SvMULTI_on(gv);
537 sv_magic((SV*)hv, (SV*)gv, 'A', 0, 0);
538 }
539 break;
540#endif /* OVERLOAD */
93a17b20
LW
541 case 'S':
542 if (strEQ(name, "SIG")) {
543 HV *hv;
544 siggv = gv;
545 SvMULTI_on(siggv);
546 hv = GvHVn(siggv);
547 hv_magic(hv, siggv, 'S');
548
549 /* initialize signal stack */
550 signalstack = newAV();
93a17b20 551 AvREAL_off(signalstack);
a0d0e21e
LW
552 av_extend(signalstack, 30);
553 av_fill(signalstack, 0);
93a17b20
LW
554 }
555 break;
556
557 case '&':
463ee0b2
LW
558 if (len > 1)
559 break;
93a17b20
LW
560 ampergv = gv;
561 sawampersand = TRUE;
a0d0e21e 562 goto ro_magicalize;
93a17b20
LW
563
564 case '`':
463ee0b2
LW
565 if (len > 1)
566 break;
93a17b20
LW
567 leftgv = gv;
568 sawampersand = TRUE;
a0d0e21e 569 goto ro_magicalize;
93a17b20
LW
570
571 case '\'':
463ee0b2
LW
572 if (len > 1)
573 break;
93a17b20
LW
574 rightgv = gv;
575 sawampersand = TRUE;
a0d0e21e 576 goto ro_magicalize;
93a17b20
LW
577
578 case ':':
463ee0b2
LW
579 if (len > 1)
580 break;
93a17b20
LW
581 sv_setpv(GvSV(gv),chopset);
582 goto magicalize;
583
93a17b20 584 case '#':
a0d0e21e
LW
585 case '*':
586 if (dowarn && len == 1 && sv_type == SVt_PV)
587 warn("Use of $%s is deprecated", name);
588 /* FALL THROUGH */
589 case '[':
590 case '!':
93a17b20
LW
591 case '?':
592 case '^':
593 case '~':
594 case '=':
595 case '-':
596 case '%':
597 case '.':
93a17b20
LW
598 case '(':
599 case ')':
600 case '<':
601 case '>':
602 case ',':
603 case '\\':
604 case '/':
93a17b20 605 case '|':
748a9306 606 case '\001':
93a17b20 607 case '\004':
748a9306 608 case '\006':
a0d0e21e 609 case '\010':
93a17b20
LW
610 case '\t':
611 case '\020':
612 case '\024':
613 case '\027':
463ee0b2
LW
614 if (len > 1)
615 break;
616 goto magicalize;
617
a0d0e21e 618 case '+':
463ee0b2
LW
619 case '1':
620 case '2':
621 case '3':
622 case '4':
623 case '5':
624 case '6':
625 case '7':
626 case '8':
627 case '9':
a0d0e21e
LW
628 ro_magicalize:
629 SvREADONLY_on(GvSV(gv));
93a17b20 630 magicalize:
463ee0b2 631 sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
93a17b20
LW
632 break;
633
634 case '\014':
463ee0b2
LW
635 if (len > 1)
636 break;
93a17b20
LW
637 sv_setpv(GvSV(gv),"\f");
638 formfeed = GvSV(gv);
639 break;
640 case ';':
463ee0b2
LW
641 if (len > 1)
642 break;
93a17b20
LW
643 sv_setpv(GvSV(gv),"\034");
644 break;
463ee0b2
LW
645 case ']':
646 if (len == 1) {
93a17b20
LW
647 SV *sv;
648 sv = GvSV(gv);
649 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 650 sv_setpv(sv, patchlevel);
93a17b20
LW
651 }
652 break;
79072805 653 }
93a17b20 654 return gv;
79072805
LW
655}
656
657void
658gv_fullname(sv,gv)
659SV *sv;
660GV *gv;
661{
662 HV *hv = GvSTASH(gv);
663
664 if (!hv)
665 return;
666 sv_setpv(sv, sv == (SV*)gv ? "*" : "");
667 sv_catpv(sv,HvNAME(hv));
463ee0b2 668 sv_catpvn(sv,"::", 2);
79072805
LW
669 sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
670}
671
672void
673gv_efullname(sv,gv)
674SV *sv;
675GV *gv;
676{
677 GV* egv = GvEGV(gv);
748a9306
LW
678 HV *hv;
679
680 if (!egv)
681 egv = gv;
682 hv = GvSTASH(egv);
79072805
LW
683 if (!hv)
684 return;
748a9306 685
79072805
LW
686 sv_setpv(sv, sv == (SV*)gv ? "*" : "");
687 sv_catpv(sv,HvNAME(hv));
463ee0b2 688 sv_catpvn(sv,"::", 2);
79072805
LW
689 sv_catpvn(sv,GvNAME(egv),GvNAMELEN(egv));
690}
691
692IO *
693newIO()
694{
695 IO *io;
8990e307
LW
696 GV *iogv;
697
698 io = (IO*)NEWSV(0,0);
a0d0e21e 699 sv_upgrade((SV *)io,SVt_PVIO);
8990e307
LW
700 SvREFCNT(io) = 1;
701 SvOBJECT_on(io);
85e6fe83 702 iogv = gv_fetchpv("FileHandle::", TRUE, SVt_PVIO);
8990e307 703 SvSTASH(io) = (HV*)SvREFCNT_inc(GvHV(iogv));
79072805
LW
704 return io;
705}
706
707void
463ee0b2
LW
708gv_check(stash)
709HV* stash;
79072805
LW
710{
711 register HE *entry;
712 register I32 i;
713 register GV *gv;
463ee0b2 714 HV *hv;
a0d0e21e 715 GV *filegv;
463ee0b2 716
8990e307
LW
717 if (!HvARRAY(stash))
718 return;
a0d0e21e 719 for (i = 0; i <= (I32) HvMAX(stash); i++) {
463ee0b2 720 for (entry = HvARRAY(stash)[i]; entry; entry = entry->hent_next) {
a0d0e21e
LW
721 if (entry->hent_key[entry->hent_klen-1] == ':' &&
722 (gv = (GV*)entry->hent_val) && (hv = GvHV(gv)) && HvNAME(hv))
723 {
724 if (hv != defstash)
725 gv_check(hv); /* nested package */
726 }
727 else if (isALPHA(*entry->hent_key)) {
463ee0b2
LW
728 gv = (GV*)entry->hent_val;
729 if (SvMULTI(gv))
730 continue;
731 curcop->cop_line = GvLINE(gv);
a0d0e21e
LW
732 filegv = GvFILEGV(gv);
733 curcop->cop_filegv = filegv;
734 if (filegv && SvMULTI(filegv)) /* Filename began with slash */
8990e307 735 continue;
a0d0e21e
LW
736 warn("Identifier \"%s::%s\" used only once: possible typo",
737 HvNAME(stash), GvNAME(gv));
463ee0b2 738 }
79072805
LW
739 }
740 }
741}
742
743GV *
a0d0e21e
LW
744newGVgen(pack)
745char *pack;
79072805 746{
a0d0e21e 747 (void)sprintf(tokenbuf,"%s::_GEN_%ld",pack,(long)gensym++);
85e6fe83 748 return gv_fetchpv(tokenbuf,TRUE, SVt_PVGV);
79072805
LW
749}
750
751/* hopefully this is only called on local symbol table entries */
752
753GP*
754gp_ref(gp)
755GP* gp;
756{
757 gp->gp_refcnt++;
758 return gp;
759
760}
761
762void
763gp_free(gv)
764GV* gv;
765{
79072805 766 GP* gp;
377b8fbc 767 CV* cv;
79072805
LW
768
769 if (!gv || !(gp = GvGP(gv)))
770 return;
771 if (gp->gp_refcnt == 0) {
772 warn("Attempt to free unreferenced glob pointers");
773 return;
774 }
748a9306
LW
775 if (--gp->gp_refcnt > 0) {
776 if (gp->gp_egv == gv)
777 gp->gp_egv = 0;
79072805 778 return;
748a9306 779 }
79072805 780
8990e307
LW
781 SvREFCNT_dec(gp->gp_sv);
782 SvREFCNT_dec(gp->gp_av);
783 SvREFCNT_dec(gp->gp_hv);
377b8fbc 784 SvREFCNT_dec(gp->gp_io);
8990e307
LW
785 if ((cv = gp->gp_cv) && !GvCVGEN(gv))
786 SvREFCNT_dec(cv);
748a9306
LW
787 SvREFCNT_dec(gp->gp_form);
788
79072805
LW
789 Safefree(gp);
790 GvGP(gv) = 0;
791}
792
793#if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
794#define MICROPORT
795#endif
796
797#ifdef MICROPORT /* Microport 2.4 hack */
798AV *GvAVn(gv)
799register GV *gv;
800{
801 if (GvGP(gv)->gp_av)
802 return GvGP(gv)->gp_av;
803 else
804 return GvGP(gv_AVadd(gv))->gp_av;
805}
806
807HV *GvHVn(gv)
808register GV *gv;
809{
810 if (GvGP(gv)->gp_hv)
811 return GvGP(gv)->gp_hv;
812 else
813 return GvGP(gv_HVadd(gv))->gp_hv;
814}
815#endif /* Microport 2.4 hack */
a0d0e21e
LW
816
817#ifdef OVERLOAD
818/* Updates and caches the CV's */
819
820bool
821Gv_AMupdate(stash)
822HV* stash;
823{
824 GV** gvp;
825 HV* hv;
826 GV* gv;
827 CV* cv;
828 MAGIC* mg=mg_find((SV*)stash,'c');
748a9306 829 AMT *amtp=mg ? (AMT*)mg->mg_ptr: NULL;
a0d0e21e
LW
830
831 if (mg && (amtp=((AMT*)(mg->mg_ptr)))->was_ok_am == amagic_generation &&
832 amtp->was_ok_sub == sub_generation)
833 return HV_AMAGIC(stash)? TRUE: FALSE;
834 gvp=(GV**)hv_fetch(stash,"OVERLOAD",8,FALSE);
748a9306
LW
835 if (amtp && amtp->table) {
836 int i;
837 for (i=1;i<NofAMmeth*2;i++) {
838 if (amtp->table[i]) {
839 SvREFCNT_dec(amtp->table[i]);
840 }
841 }
842 }
a0d0e21e
LW
843 sv_unmagic((SV*)stash, 'c');
844
845 DEBUG_o( deb("Recalcing overload magic in package %s\n",HvNAME(stash)) );
846
847 if (gvp && ((gv = *gvp) != (GV*)&sv_undef && (hv = GvHV(gv)))) {
848 int filled=0;
849 int i;
850 char *cp;
851 AMT amt;
852 SV* sv;
853 SV** svp;
854
855/* if (*(svp)==(SV*)amagic_generation && *(svp+1)==(SV*)sub_generation) {
856 DEBUG_o( deb("Overload magic in package %s up-to-date\n",HvNAME(stash))
857);
858 return HV_AMAGIC(stash)? TRUE: FALSE;
859 }*/
860
861 amt.was_ok_am=amagic_generation;
862 amt.was_ok_sub=sub_generation;
863 amt.fallback=AMGfallNO;
864
865 /* Work with "fallback" key, which we assume to be first in AMG_names */
866
867 if ((cp=((char**)(*AMG_names))[0]) &&
868 (svp=(SV**)hv_fetch(hv,cp,strlen(cp),FALSE)) && (sv = *svp)) {
869 if (SvTRUE(sv)) amt.fallback=AMGfallYES;
870 else if (SvOK(sv)) amt.fallback=AMGfallNEVER;
871 }
872
873 for (i=1;i<NofAMmeth*2;i++) {
874 cv=0;
875
876 if ( (cp=((char**)(*AMG_names))[i]) ) {
877 svp=(SV**)hv_fetch(hv,cp,strlen(cp),FALSE);
878 if (svp && ((sv = *svp) != (GV*)&sv_undef)) {
879 switch (SvTYPE(sv)) {
880 default:
881 if (!SvROK(sv)) {
882 if (!SvOK(sv)) break;
748a9306 883 gv = gv_fetchmethod(stash, SvPV(sv, na));
a0d0e21e
LW
884 if (gv) cv = GvCV(gv);
885 break;
886 }
887 cv = (CV*)SvRV(sv);
888 if (SvTYPE(cv) == SVt_PVCV)
889 break;
890 /* FALL THROUGH */
891 case SVt_PVHV:
892 case SVt_PVAV:
893 die("Not a subroutine reference in %%OVERLOAD");
894 return FALSE;
895 case SVt_PVCV:
896 cv = (CV*)sv;
897 break;
898 case SVt_PVGV:
899 if (!(cv = GvCV((GV*)sv)))
900 cv = sv_2cv(sv, &stash, &gv, TRUE);
901 break;
902 }
903 if (cv) filled=1;
904 else {
06b43eb7 905 die("Method for operation %s not found in package %.256s during blessing\n",
a0d0e21e
LW
906 cp,HvNAME(stash));
907 return FALSE;
908 }
909 }
910 }
748a9306 911 amt.table[i]=(CV*)SvREFCNT_inc(cv);
a0d0e21e
LW
912 }
913 sv_magic((SV*)stash, 0, 'c', (char*)&amt, sizeof(amt));
914 if (filled) {
915/* HV_badAMAGIC_off(stash);*/
916 HV_AMAGIC_on(stash);
917 return TRUE;
918 }
919 }
920/*HV_badAMAGIC_off(stash);*/
921 HV_AMAGIC_off(stash);
922 return FALSE;
923}
924
925/* During call to this subroutine stack can be reallocated. It is
926 * advised to call SPAGAIN macro in your code after call */
927
928SV*
929amagic_call(left,right,method,flags)
930SV* left;
931SV* right;
932int method;
933int flags;
934{
935 MAGIC *mg;
936 CV *cv;
937 CV **cvp=NULL, **ocvp=NULL;
938 AMT *amtp, *oamtp;
939 int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0;
748a9306 940 int postpr=0, inc_dec_ass=0, assignshift=assign?1:0;
a0d0e21e
LW
941 HV* stash;
942 if (!(AMGf_noleft & flags) && SvAMAGIC(left)
943 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c'))
944 && (ocvp = cvp = ((oamtp=amtp=(AMT*)mg->mg_ptr)->table))
748a9306
LW
945 && ((cv = cvp[off=method+assignshift])
946 || (assign && amtp->fallback > AMGfallNEVER && /* fallback to
947 * usual method */
948 (fl = 1, cv = cvp[off=method])))) {
a0d0e21e
LW
949 lr = -1; /* Call method for left argument */
950 } else {
951 if (cvp && amtp->fallback > AMGfallNEVER && flags & AMGf_unary) {
952 int logic;
953
954 /* look for substituted methods */
955 switch (method) {
956 case inc_amg:
748a9306 957 if (((cv = cvp[off=add_ass_amg]) && (inc_dec_ass=1))
a0d0e21e
LW
958 || ((cv = cvp[off=add_amg]) && (postpr=1))) {
959 right = &sv_yes; lr = -1; assign = 1;
960 }
961 break;
962 case dec_amg:
748a9306 963 if (((cv = cvp[off=subtr_ass_amg]) && (inc_dec_ass=1))
a0d0e21e
LW
964 || ((cv = cvp[off=subtr_amg]) && (postpr=1))) {
965 right = &sv_yes; lr = -1; assign = 1;
966 }
967 break;
968 case bool__amg:
969 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg]));
970 break;
971 case numer_amg:
972 (void)((cv = cvp[off=string_amg]) || (cv = cvp[off=bool__amg]));
973 break;
974 case string_amg:
975 (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg]));
976 break;
748a9306
LW
977 case copy_amg:
978 {
979 SV* ref=SvRV(left);
980 if (!SvROK(ref) && SvTYPE(ref) <= SVt_PVMG) { /* Just to be
981 * extra
982 * causious,
983 * maybe in some
984 * additional
985 * cases sv_setsv
986 * is safe too */
987 SV* newref = newSVsv(ref);
988 SvOBJECT_on(newref);
989 SvSTASH(newref) = (HV*)SvREFCNT_inc(SvSTASH(ref));
990 return newref;
991 }
992 }
993 break;
a0d0e21e 994 case abs_amg:
748a9306 995 if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg])
a0d0e21e 996 && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) {
748a9306 997 SV* nullsv=sv_2mortal(newSViv(0));
a0d0e21e 998 if (off1==lt_amg) {
748a9306 999 SV* lessp = amagic_call(left,nullsv,
a0d0e21e
LW
1000 lt_amg,AMGf_noright);
1001 logic = SvTRUE(lessp);
1002 } else {
748a9306 1003 SV* lessp = amagic_call(left,nullsv,
a0d0e21e
LW
1004 ncmp_amg,AMGf_noright);
1005 logic = (SvNV(lessp) < 0);
1006 }
1007 if (logic) {
1008 if (off==subtr_amg) {
1009 right = left;
748a9306 1010 left = nullsv;
a0d0e21e
LW
1011 lr = 1;
1012 }
1013 } else {
1014 return left;
1015 }
1016 }
1017 break;
1018 case neg_amg:
1019 if (cv = cvp[off=subtr_amg]) {
1020 right = left;
1021 left = sv_2mortal(newSViv(0));
1022 lr = 1;
1023 }
1024 break;
1025 default:
1026 goto not_found;
1027 }
1028 if (!cv) goto not_found;
1029 } else if (!(AMGf_noright & flags) && SvAMAGIC(right)
1030 && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c'))
1031 && (cvp = ((amtp=(AMT*)mg->mg_ptr)->table))
1032 && (cv = cvp[off=method])) { /* Method for right
1033 * argument found */
1034 lr=1;
748a9306 1035 } else if (((ocvp && oamtp->fallback > AMGfallNEVER
4633a7c4 1036 && (cvp=ocvp) && (lr = -1))
a0d0e21e
LW
1037 || (cvp && amtp->fallback > AMGfallNEVER && (lr=1)))
1038 && !(flags & AMGf_unary)) {
1039 /* We look for substitution for
1040 * comparison operations and
1041 * concatendation */
1042 if (method==concat_amg || method==concat_ass_amg
1043 || method==repeat_amg || method==repeat_ass_amg) {
1044 return NULL; /* Delegate operation to string conversion */
1045 }
1046 off = -1;
1047 switch (method) {
1048 case lt_amg:
1049 case le_amg:
1050 case gt_amg:
1051 case ge_amg:
1052 case eq_amg:
1053 case ne_amg:
1054 postpr = 1; off=ncmp_amg; break;
1055 case slt_amg:
1056 case sle_amg:
1057 case sgt_amg:
1058 case sge_amg:
1059 case seq_amg:
1060 case sne_amg:
1061 postpr = 1; off=scmp_amg; break;
1062 }
1063 if (off != -1) cv = cvp[off];
1064 if (!cv) {
1065 goto not_found;
1066 }
1067 } else {
1068 not_found: /* No method found, either report or die */
1069 if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */
1070 notfound = 1; lr = -1;
1071 } else if (cvp && (cv=cvp[nomethod_amg])) {
1072 notfound = 1; lr = 1;
1073 } else {
748a9306 1074 if (off==-1) off=method;
06b43eb7 1075 sprintf(buf, "Operation `%s': no method found,\n\tleft argument %s%.256s,\n\tright argument %s%.256s",
c07a80fd 1076 ((char**)AMG_names)[method + assignshift],
a0d0e21e
LW
1077 SvAMAGIC(left)?
1078 "in overloaded package ":
1079 "has no overloaded magic",
1080 SvAMAGIC(left)?
1081 HvNAME(SvSTASH(SvRV(left))):
1082 "",
1083 SvAMAGIC(right)?
1084 "in overloaded package ":
1085 "has no overloaded magic",
1086 SvAMAGIC(right)?
1087 HvNAME(SvSTASH(SvRV(right))):
1088 "");
1089 if (amtp && amtp->fallback >= AMGfallYES) {
06b43eb7 1090 DEBUG_o( deb(buf) );
a0d0e21e 1091 } else {
06b43eb7 1092 die(buf);
a0d0e21e
LW
1093 }
1094 return NULL;
1095 }
1096 }
1097 }
1098 if (!notfound) {
06b43eb7 1099 DEBUG_o( deb("Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %.256s%s\n",
a0d0e21e 1100 ((char**)AMG_names)[off],
748a9306
LW
1101 method+assignshift==off? "" :
1102 " (initially `",
1103 method+assignshift==off? "" :
1104 ((char**)AMG_names)[method+assignshift],
1105 method+assignshift==off? "" : "')",
1106 flags & AMGf_unary? "" :
1107 lr==1 ? " for right argument": " for left argument",
1108 flags & AMGf_unary? " for argument" : "",
a0d0e21e
LW
1109 HvNAME(stash),
1110 fl? ",\n\tassignment variant used": "") );
748a9306
LW
1111 /* Since we use shallow copy during assignment, we need
1112 * to dublicate the contents, probably calling user-supplied
1113 * version of copy operator
1114 */
c07a80fd 1115 if ((method + assignshift==off
748a9306
LW
1116 && (assign || method==inc_amg || method==dec_amg))
1117 || inc_dec_ass) RvDEEPCP(left);
a0d0e21e
LW
1118 }
1119 {
1120 dSP;
1121 BINOP myop;
1122 SV* res;
1123
1124 Zero(&myop, 1, BINOP);
1125 myop.op_last = (OP *) &myop;
1126 myop.op_next = Nullop;
1127 myop.op_flags = OPf_KNOW|OPf_STACKED;
1128
1129 ENTER;
1130 SAVESPTR(op);
1131 op = (OP *) &myop;
1132 PUTBACK;
1133 pp_pushmark();
1134
1135 EXTEND(sp, notfound + 5);
1136 PUSHs(lr>0? right: left);
1137 PUSHs(lr>0? left: right);
1138 PUSHs( assign ? &sv_undef : (lr>0? &sv_yes: &sv_no));
1139 if (notfound) {
c07a80fd 1140 PUSHs( sv_2mortal(newSVpv(((char**)AMG_names)[method + assignshift],0)) );
a0d0e21e
LW
1141 }
1142 PUSHs((SV*)cv);
1143 PUTBACK;
1144
1145 if (op = pp_entersub())
1146 run();
1147 LEAVE;
1148 SPAGAIN;
1149
1150 res=POPs;
1151 PUTBACK;
1152
1153 if (notfound) {
1154 /* sv_2mortal(res); */
1155 return NULL;
1156 }
1157
1158 if (postpr) {
1159 int ans;
1160 switch (method) {
1161 case le_amg:
1162 case sle_amg:
1163 ans=SvIV(res)<=0; break;
1164 case lt_amg:
1165 case slt_amg:
1166 ans=SvIV(res)<0; break;
1167 case ge_amg:
1168 case sge_amg:
1169 ans=SvIV(res)>=0; break;
1170 case gt_amg:
1171 case sgt_amg:
1172 ans=SvIV(res)>0; break;
1173 case eq_amg:
1174 case seq_amg:
1175 ans=SvIV(res)==0; break;
1176 case ne_amg:
1177 case sne_amg:
1178 ans=SvIV(res)!=0; break;
1179 case inc_amg:
1180 case dec_amg:
1181 SvSetSV(left,res); return res; break;
1182 }
1183 return ans? &sv_yes: &sv_no;
748a9306
LW
1184 } else if (method==copy_amg) {
1185 if (!SvROK(res)) {
1186 die("Copy method did not return a reference");
1187 }
1188 return SvREFCNT_inc(SvRV(res));
a0d0e21e
LW
1189 } else {
1190 return res;
1191 }
1192 }
1193}
1194#endif /* OVERLOAD */