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