3 * Copyright (c) 2007 Brandon L Black
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.
11 * "Which order shall we go in?" said Frodo. "Eldest first, or quickest first?
12 * You'll be last either way, Master Peregrin."
18 These functions are related to the method resolution order of perl classes
29 AV *(*resolve)(pTHX_ HV* stash, I32 level);
32 /* First one is the default */
33 static struct mro_alg mros[] = {
34 {"dfs", S_mro_get_linear_isa_dfs},
35 {"c3", S_mro_get_linear_isa_c3}
38 #define NUMBER_OF_MROS (sizeof(mros)/sizeof(struct mro_alg))
40 static const struct mro_alg *
41 S_get_mro_from_name(pTHX_ const char *const name) {
42 const struct mro_alg *algo = mros;
43 const struct mro_alg *const end = mros + NUMBER_OF_MROS;
45 if(strEQ(name, algo->name))
53 Perl_mro_meta_init(pTHX_ HV* stash)
55 struct mro_meta* newmeta;
57 PERL_ARGS_ASSERT_MRO_META_INIT;
59 assert(!(HvAUX(stash)->xhv_mro_meta));
60 Newxz(newmeta, 1, struct mro_meta);
61 HvAUX(stash)->xhv_mro_meta = newmeta;
62 newmeta->cache_gen = 1;
64 newmeta->mro_which = mros;
69 #if defined(USE_ITHREADS)
71 /* for sv_dup on new threads */
73 Perl_mro_meta_dup(pTHX_ struct mro_meta* smeta, CLONE_PARAMS* param)
75 struct mro_meta* newmeta;
77 PERL_ARGS_ASSERT_MRO_META_DUP;
79 Newx(newmeta, 1, struct mro_meta);
80 Copy(smeta, newmeta, 1, struct mro_meta);
82 if (newmeta->mro_linear_dfs)
83 newmeta->mro_linear_dfs
84 = (AV*) SvREFCNT_inc(sv_dup((SV*)newmeta->mro_linear_dfs, param));
85 if (newmeta->mro_linear_c3)
86 newmeta->mro_linear_c3
87 = (AV*) SvREFCNT_inc(sv_dup((SV*)newmeta->mro_linear_c3, param));
88 if (newmeta->mro_nextmethod)
89 newmeta->mro_nextmethod
90 = (HV*) SvREFCNT_inc(sv_dup((SV*)newmeta->mro_nextmethod, param));
95 #endif /* USE_ITHREADS */
98 =for apidoc mro_get_linear_isa_dfs
100 Returns the Depth-First Search linearization of @ISA
101 the given stash. The return value is a read-only AV*.
102 C<level> should be 0 (it is used internally in this
103 function's recursion).
105 You are responsible for C<SvREFCNT_inc()> on the
106 return value if you plan to store it anywhere
107 semi-permanently (otherwise it might be deleted
108 out from under you the next time the cache is
114 S_mro_get_linear_isa_dfs(pTHX_ HV *stash, I32 level)
121 struct mro_meta* meta;
123 PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA_DFS;
124 assert(HvAUX(stash));
126 stashhek = HvNAME_HEK(stash);
128 Perl_croak(aTHX_ "Can't linearize anonymous symbol table");
131 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
134 meta = HvMROMETA(stash);
136 /* return cache if valid */
137 if((retval = meta->mro_linear_dfs)) {
141 /* not in cache, make a new one */
143 retval = (AV*)sv_2mortal((SV *)newAV());
144 av_push(retval, newSVhek(stashhek)); /* add ourselves at the top */
147 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
148 av = (gvp && (gv = *gvp) && isGV_with_GP(gv)) ? GvAV(gv) : NULL;
150 if(av && AvFILLp(av) >= 0) {
152 /* "stored" is used to keep track of all of the classnames
153 we have added to the MRO so far, so we can do a quick
154 exists check and avoid adding duplicate classnames to
157 HV* const stored = (HV*)sv_2mortal((SV*)newHV());
158 SV **svp = AvARRAY(av);
159 I32 items = AvFILLp(av) + 1;
163 SV* const sv = *svp++;
164 HV* const basestash = gv_stashsv(sv, 0);
169 /* if no stash exists for this @ISA member,
170 simply add it to the MRO and move on */
175 /* otherwise, recurse into ourselves for the MRO
176 of this @ISA member, and append their MRO to ours.
177 The recursive call could throw an exception, which
178 has memory management implications here, hence the use of
180 const AV *const subrv
181 = mro_get_linear_isa_dfs(basestash, level + 1);
183 subrv_p = AvARRAY(subrv);
184 subrv_items = AvFILLp(subrv) + 1;
186 while(subrv_items--) {
187 SV *const subsv = *subrv_p++;
188 /* LVALUE fetch will create a new undefined SV if necessary
190 HE *const he = hv_fetch_ent(stored, subsv, 1, 0);
192 if(HeVAL(he) != &PL_sv_undef) {
193 /* It was newly created. Steal it for our new SV, and
194 replace it in the hash with the "real" thing. */
195 SV *const val = HeVAL(he);
197 HeVAL(he) = &PL_sv_undef;
198 sv_setsv(val, subsv);
199 av_push(retval, val);
205 /* now that we're past the exception dangers, grab our own reference to
206 the AV we're about to use for the result. The reference owned by the
207 mortals' stack will be released soon, so everything will balance. */
208 SvREFCNT_inc_simple_void_NN(retval);
211 /* we don't want anyone modifying the cache entry but us,
212 and we do so by replacing it completely */
213 SvREADONLY_on(retval);
215 meta->mro_linear_dfs = retval;
220 =for apidoc mro_get_linear_isa_c3
222 Returns the C3 linearization of @ISA
223 the given stash. The return value is a read-only AV*.
224 C<level> should be 0 (it is used internally in this
225 function's recursion).
227 You are responsible for C<SvREFCNT_inc()> on the
228 return value if you plan to store it anywhere
229 semi-permanently (otherwise it might be deleted
230 out from under you the next time the cache is
237 S_mro_get_linear_isa_c3(pTHX_ HV* stash, I32 level)
244 struct mro_meta* meta;
246 PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA_C3;
247 assert(HvAUX(stash));
249 stashhek = HvNAME_HEK(stash);
251 Perl_croak(aTHX_ "Can't linearize anonymous symbol table");
254 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
257 meta = HvMROMETA(stash);
259 /* return cache if valid */
260 if((retval = meta->mro_linear_c3)) {
264 /* not in cache, make a new one */
266 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
267 isa = (gvp && (gv = *gvp) && isGV_with_GP(gv)) ? GvAV(gv) : NULL;
269 /* For a better idea how the rest of this works, see the much clearer
270 pure perl version in Algorithm::C3 0.01:
271 http://search.cpan.org/src/STEVAN/Algorithm-C3-0.01/lib/Algorithm/C3.pm
272 (later versions go about it differently than this code for speed reasons)
275 if(isa && AvFILLp(isa) >= 0) {
278 HV* const tails = (HV*)sv_2mortal((SV*)newHV());
279 AV* const seqs = (AV*)sv_2mortal((SV*)newAV());
282 /* This builds @seqs, which is an array of arrays.
283 The members of @seqs are the MROs of
284 the members of @ISA, followed by @ISA itself.
286 I32 items = AvFILLp(isa) + 1;
287 SV** isa_ptr = AvARRAY(isa);
289 SV* const isa_item = *isa_ptr++;
290 HV* const isa_item_stash = gv_stashsv(isa_item, 0);
291 if(!isa_item_stash) {
292 /* if no stash, make a temporary fake MRO
293 containing just itself */
294 AV* const isa_lin = newAV();
295 av_push(isa_lin, newSVsv(isa_item));
296 av_push(seqs, (SV*)isa_lin);
300 AV* const isa_lin = mro_get_linear_isa_c3(isa_item_stash, level + 1);
301 av_push(seqs, SvREFCNT_inc_simple_NN((SV*)isa_lin));
304 av_push(seqs, SvREFCNT_inc_simple_NN((SV*)isa));
306 /* This builds "heads", which as an array of integer array
307 indices, one per seq, which point at the virtual "head"
308 of the seq (initially zero) */
309 Newxz(heads, AvFILLp(seqs)+1, I32);
311 /* This builds %tails, which has one key for every class
312 mentioned in the tail of any sequence in @seqs (tail meaning
313 everything after the first class, the "head"). The value
314 is how many times this key appears in the tails of @seqs.
316 seqs_ptr = AvARRAY(seqs);
317 seqs_items = AvFILLp(seqs) + 1;
318 while(seqs_items--) {
319 AV* const seq = (AV*)*seqs_ptr++;
320 I32 seq_items = AvFILLp(seq);
322 SV** seq_ptr = AvARRAY(seq) + 1;
324 SV* const seqitem = *seq_ptr++;
325 /* LVALUE fetch will create a new undefined SV if necessary
327 HE* const he = hv_fetch_ent(tails, seqitem, 1, 0);
329 SV* const val = HeVAL(he);
330 /* This will increment undef to 1, which is what we
331 want for a newly created entry. */
338 /* Initialize retval to build the return value in */
340 av_push(retval, newSVhek(stashhek)); /* us first */
342 /* This loop won't terminate until we either finish building
343 the MRO, or get an exception. */
349 /* "foreach $seq (@seqs)" */
350 SV** const avptr = AvARRAY(seqs);
351 for(s = 0; s <= AvFILLp(seqs); s++) {
353 AV * const seq = (AV*)(avptr[s]);
355 if(!seq) continue; /* skip empty seqs */
356 svp = av_fetch(seq, heads[s], 0);
357 seqhead = *svp; /* seqhead = head of this seq */
361 /* if we haven't found a winner for this round yet,
362 and this seqhead is not in tails (or the count
363 for it in tails has dropped to zero), then this
364 seqhead is our new winner, and is added to the
365 final MRO immediately */
367 if((tail_entry = hv_fetch_ent(tails, cand, 0, 0))
368 && (val = HeVAL(tail_entry))
371 winner = newSVsv(cand);
372 av_push(retval, winner);
373 /* note however that even when we find a winner,
374 we continue looping over @seqs to do housekeeping */
376 if(!sv_cmp(seqhead, winner)) {
377 /* Once we have a winner (including the iteration
378 where we first found him), inc the head ptr
379 for any seq which had the winner as a head,
380 NULL out any seq which is now empty,
381 and adjust tails for consistency */
383 const int new_head = ++heads[s];
384 if(new_head > AvFILLp(seq)) {
385 SvREFCNT_dec(avptr[s]);
391 /* Because we know this new seqhead used to be
392 a tail, we can assume it is in tails and has
393 a positive value, which we need to dec */
394 svp = av_fetch(seq, new_head, 0);
396 tail_entry = hv_fetch_ent(tails, seqhead, 0, 0);
397 val = HeVAL(tail_entry);
403 /* if we found no candidates, we are done building the MRO.
404 !cand means no seqs have any entries left to check */
410 /* If we had candidates, but nobody won, then the @ISA
411 hierarchy is not C3-incompatible */
413 /* we have to do some cleanup before we croak */
415 SvREFCNT_dec(retval);
418 Perl_croak(aTHX_ "Inconsistent hierarchy during C3 merge of class '%s': "
419 "merging failed on parent '%"SVf"'", HEK_KEY(stashhek), SVfARG(cand));
423 else { /* @ISA was undefined or empty */
424 /* build a retval containing only ourselves */
426 av_push(retval, newSVhek(stashhek));
429 /* we don't want anyone modifying the cache entry but us,
430 and we do so by replacing it completely */
431 SvREADONLY_on(retval);
433 meta->mro_linear_c3 = retval;
438 =for apidoc mro_get_linear_isa
440 Returns either C<mro_get_linear_isa_c3> or
441 C<mro_get_linear_isa_dfs> for the given stash,
442 dependant upon which MRO is in effect
443 for that stash. The return value is a
446 You are responsible for C<SvREFCNT_inc()> on the
447 return value if you plan to store it anywhere
448 semi-permanently (otherwise it might be deleted
449 out from under you the next time the cache is
455 Perl_mro_get_linear_isa(pTHX_ HV *stash)
457 struct mro_meta* meta;
459 PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA;
461 Perl_croak(aTHX_ "Can't linearize anonymous symbol table");
463 meta = HvMROMETA(stash);
464 if (!meta->mro_which)
465 Perl_croak(aTHX_ "panic: invalid MRO!");
466 return meta->mro_which->resolve(aTHX_ stash, 0);
470 =for apidoc mro_isa_changed_in
472 Takes the necessary steps (cache invalidations, mostly)
473 when the @ISA of the given package has changed. Invoked
474 by the C<setisa> magic, should not need to invoke directly.
479 Perl_mro_isa_changed_in(pTHX_ HV* stash)
488 struct mro_meta * meta;
490 const char * const stashname = HvNAME_get(stash);
491 const STRLEN stashname_len = HvNAMELEN_get(stash);
493 PERL_ARGS_ASSERT_MRO_ISA_CHANGED_IN;
496 Perl_croak(aTHX_ "Can't call mro_isa_changed_in() on anonymous symbol table");
498 /* wipe out the cached linearizations for this stash */
499 meta = HvMROMETA(stash);
500 SvREFCNT_dec((SV*)meta->mro_linear_dfs);
501 SvREFCNT_dec((SV*)meta->mro_linear_c3);
502 meta->mro_linear_dfs = NULL;
503 meta->mro_linear_c3 = NULL;
505 /* Inc the package generation, since our @ISA changed */
508 /* Wipe the global method cache if this package
509 is UNIVERSAL or one of its parents */
511 svp = hv_fetch(PL_isarev, stashname, stashname_len, 0);
512 isarev = svp ? (HV*)*svp : NULL;
514 if((stashname_len == 9 && strEQ(stashname, "UNIVERSAL"))
515 || (isarev && hv_exists(isarev, "UNIVERSAL", 9))) {
519 else { /* Wipe the local method cache otherwise */
521 is_universal = FALSE;
524 /* wipe next::method cache too */
525 if(meta->mro_nextmethod) hv_clear(meta->mro_nextmethod);
527 /* Iterate the isarev (classes that are our children),
528 wiping out their linearization and method caches */
531 while((iter = hv_iternext(isarev))) {
533 const char* const revkey = hv_iterkey(iter, &len);
534 HV* revstash = gv_stashpvn(revkey, len, 0);
535 struct mro_meta* revmeta;
537 if(!revstash) continue;
538 revmeta = HvMROMETA(revstash);
539 SvREFCNT_dec((SV*)revmeta->mro_linear_dfs);
540 SvREFCNT_dec((SV*)revmeta->mro_linear_c3);
541 revmeta->mro_linear_dfs = NULL;
542 revmeta->mro_linear_c3 = NULL;
544 revmeta->cache_gen++;
545 if(revmeta->mro_nextmethod)
546 hv_clear(revmeta->mro_nextmethod);
550 /* Now iterate our MRO (parents), and do a few things:
551 1) instantiate with the "fake" flag if they don't exist
552 2) flag them as universal if we are universal
553 3) Add everything from our isarev to their isarev
556 /* We're starting at the 2nd element, skipping ourselves here */
557 linear_mro = mro_get_linear_isa(stash);
558 svp = AvARRAY(linear_mro) + 1;
559 items = AvFILLp(linear_mro);
562 SV* const sv = *svp++;
565 HE *he = hv_fetch_ent(PL_isarev, sv, TRUE, 0);
567 /* That fetch should not fail. But if it had to create a new SV for
568 us, then will need to upgrade it to an HV (which sv_upgrade() can
571 mroisarev = (HV*)HeVAL(he);
573 SvUPGRADE((SV*)mroisarev, SVt_PVHV);
575 /* This hash only ever contains PL_sv_yes. Storing it over itself is
576 almost as cheap as calling hv_exists, so on aggregate we expect to
577 save time by not making two calls to the common HV code for the
578 case where it doesn't exist. */
580 (void)hv_store(mroisarev, stashname, stashname_len, &PL_sv_yes, 0);
584 while((iter = hv_iternext(isarev))) {
586 char* const revkey = hv_iterkey(iter, &revkeylen);
587 (void)hv_store(mroisarev, revkey, revkeylen, &PL_sv_yes, 0);
594 =for apidoc mro_method_changed_in
596 Invalidates method caching on any child classes
597 of the given stash, so that they might notice
598 the changes in this one.
600 Ideally, all instances of C<PL_sub_generation++> in
601 perl source outside of C<mro.c> should be
602 replaced by calls to this.
604 Perl automatically handles most of the common
605 ways a method might be redefined. However, there
606 are a few ways you could change a method in a stash
607 without the cache code noticing, in which case you
608 need to call this method afterwards:
610 1) Directly manipulating the stash HV entries from
613 2) Assigning a reference to a readonly scalar
614 constant into a stash entry in order to create
615 a constant subroutine (like constant.pm
618 This same method is available from pure perl
619 via, C<mro::method_changed_in(classname)>.
624 Perl_mro_method_changed_in(pTHX_ HV *stash)
626 const char * const stashname = HvNAME_get(stash);
627 const STRLEN stashname_len = HvNAMELEN_get(stash);
629 SV ** const svp = hv_fetch(PL_isarev, stashname, stashname_len, 0);
630 HV * const isarev = svp ? (HV*)*svp : NULL;
632 PERL_ARGS_ASSERT_MRO_METHOD_CHANGED_IN;
635 Perl_croak(aTHX_ "Can't call mro_method_changed_in() on anonymous symbol table");
637 /* Inc the package generation, since a local method changed */
638 HvMROMETA(stash)->pkg_gen++;
640 /* If stash is UNIVERSAL, or one of UNIVERSAL's parents,
641 invalidate all method caches globally */
642 if((stashname_len == 9 && strEQ(stashname, "UNIVERSAL"))
643 || (isarev && hv_exists(isarev, "UNIVERSAL", 9))) {
648 /* else, invalidate the method caches of all child classes,
654 while((iter = hv_iternext(isarev))) {
656 const char* const revkey = hv_iterkey(iter, &len);
657 HV* const revstash = gv_stashpvn(revkey, len, 0);
658 struct mro_meta* mrometa;
660 if(!revstash) continue;
661 mrometa = HvMROMETA(revstash);
662 mrometa->cache_gen++;
663 if(mrometa->mro_nextmethod)
664 hv_clear(mrometa->mro_nextmethod);
669 /* These two are static helpers for next::method and friends,
670 and re-implement a bunch of the code from pp_caller() in
671 a more efficient manner for this particular usage.
675 __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) {
677 for (i = startingblock; i >= 0; i--) {
678 if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i;
685 XS(XS_mro_get_linear_isa);
688 XS(XS_mro_get_isarev);
689 XS(XS_mro_is_universal);
690 XS(XS_mro_invalidate_method_caches);
691 XS(XS_mro_method_changed_in);
692 XS(XS_mro_get_pkg_gen);
696 Perl_boot_core_mro(pTHX)
699 static const char file[] = __FILE__;
701 newXSproto("mro::get_linear_isa", XS_mro_get_linear_isa, file, "$;$");
702 newXSproto("mro::set_mro", XS_mro_set_mro, file, "$$");
703 newXSproto("mro::get_mro", XS_mro_get_mro, file, "$");
704 newXSproto("mro::get_isarev", XS_mro_get_isarev, file, "$");
705 newXSproto("mro::is_universal", XS_mro_is_universal, file, "$");
706 newXSproto("mro::invalidate_all_method_caches", XS_mro_invalidate_method_caches, file, "");
707 newXSproto("mro::method_changed_in", XS_mro_method_changed_in, file, "$");
708 newXSproto("mro::get_pkg_gen", XS_mro_get_pkg_gen, file, "$");
709 newXS("mro::_nextcan", XS_mro_nextcan, file);
712 XS(XS_mro_get_linear_isa) {
719 if(items < 1 || items > 2)
720 croak_xs_usage(cv, "classname [, type ]");
723 class_stash = gv_stashsv(classname, 0);
726 /* No stash exists yet, give them just the classname */
727 AV* isalin = newAV();
728 av_push(isalin, newSVsv(classname));
729 ST(0) = sv_2mortal(newRV_noinc((SV*)isalin));
733 const char* const which = SvPV_nolen(ST(1));
734 const struct mro_alg *const algo = S_get_mro_from_name(aTHX_ which);
736 Perl_croak(aTHX_ "Invalid mro name: '%s'", which);
737 RETVAL = algo->resolve(aTHX_ class_stash, 0);
740 RETVAL = mro_get_linear_isa(class_stash);
743 ST(0) = newRV_inc((SV*)RETVAL);
753 const char* whichstr;
754 const struct mro_alg *which;
756 struct mro_meta* meta;
759 croak_xs_usage(cv, "classname, type");
762 whichstr = SvPV_nolen(ST(1));
763 class_stash = gv_stashsv(classname, GV_ADD);
764 if(!class_stash) Perl_croak(aTHX_ "Cannot create class: '%"SVf"'!", SVfARG(classname));
765 meta = HvMROMETA(class_stash);
767 which = S_get_mro_from_name(aTHX_ whichstr);
769 Perl_croak(aTHX_ "Invalid mro name: '%s'", whichstr);
771 if(meta->mro_which != which) {
772 meta->mro_which = which;
773 /* Only affects local method cache, not
774 even child classes */
776 if(meta->mro_nextmethod)
777 hv_clear(meta->mro_nextmethod);
792 croak_xs_usage(cv, "classname");
795 class_stash = gv_stashsv(classname, 0);
797 ST(0) = sv_2mortal(newSVpv(class_stash
798 ? HvMROMETA(class_stash)->mro_which->name
803 XS(XS_mro_get_isarev)
813 croak_xs_usage(cv, "classname");
820 he = hv_fetch_ent(PL_isarev, classname, 0, 0);
821 isarev = he ? (HV*)HeVAL(he) : NULL;
827 while((iter = hv_iternext(isarev)))
828 av_push(ret_array, newSVsv(hv_iterkeysv(iter)));
830 mXPUSHs(newRV_noinc((SV*)ret_array));
836 XS(XS_mro_is_universal)
843 STRLEN classname_len;
847 croak_xs_usage(cv, "classname");
851 classname_pv = SvPV(classname,classname_len);
853 he = hv_fetch_ent(PL_isarev, classname, 0, 0);
854 isarev = he ? (HV*)HeVAL(he) : NULL;
856 if((classname_len == 9 && strEQ(classname_pv, "UNIVERSAL"))
857 || (isarev && hv_exists(isarev, "UNIVERSAL", 9)))
863 XS(XS_mro_invalidate_method_caches)
869 croak_xs_usage(cv, "");
876 XS(XS_mro_method_changed_in)
884 croak_xs_usage(cv, "classname");
888 class_stash = gv_stashsv(classname, 0);
889 if(!class_stash) Perl_croak(aTHX_ "No such class: '%"SVf"'!", SVfARG(classname));
891 mro_method_changed_in(class_stash);
896 XS(XS_mro_get_pkg_gen)
904 croak_xs_usage(cv, "classname");
908 class_stash = gv_stashsv(classname, 0);
912 mXPUSHi(class_stash ? HvMROMETA(class_stash)->pkg_gen : 0);
923 const I32 throw_nomethod = SvIVX(ST(1));
924 register I32 cxix = cxstack_ix;
925 register const PERL_CONTEXT *ccstack = cxstack;
926 const PERL_SI *top_si = PL_curstackinfo;
929 const char *fq_subname;
931 STRLEN stashname_len;
939 struct mro_meta* selfmeta;
947 if(sv_isobject(self))
948 selfstash = SvSTASH(SvRV(self));
950 selfstash = gv_stashsv(self, GV_ADD);
954 hvname = HvNAME_get(selfstash);
956 Perl_croak(aTHX_ "Can't use anonymous symbol table for method lookup");
958 /* This block finds the contextually-enclosing fully-qualified subname,
959 much like looking at (caller($i))[3] until you find a real sub that
960 isn't ANON, etc (also skips over pureperl next::method, etc) */
961 for(i = 0; i < 2; i++) {
962 cxix = __dopoptosub_at(ccstack, cxix);
965 STRLEN fq_subname_len;
967 /* we may be in a higher stacklevel, so dig down deeper */
969 if(top_si->si_type == PERLSI_MAIN)
970 Perl_croak(aTHX_ "next::method/next::can/maybe::next::method must be used in method context");
971 top_si = top_si->si_prev;
972 ccstack = top_si->si_cxstack;
973 cxix = __dopoptosub_at(ccstack, top_si->si_cxix);
976 if(CxTYPE((PERL_CONTEXT*)(&ccstack[cxix])) != CXt_SUB
977 || (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) {
978 cxix = __dopoptosub_at(ccstack, cxix - 1);
983 const I32 dbcxix = __dopoptosub_at(ccstack, cxix - 1);
984 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) {
985 if(CxTYPE((PERL_CONTEXT*)(&ccstack[dbcxix])) != CXt_SUB) {
992 cvgv = CvGV(ccstack[cxix].blk_sub.cv);
995 cxix = __dopoptosub_at(ccstack, cxix - 1);
999 /* we found a real sub here */
1000 sv = sv_2mortal(newSV(0));
1002 gv_efullname3(sv, cvgv, NULL);
1004 fq_subname = SvPVX(sv);
1005 fq_subname_len = SvCUR(sv);
1007 subname = strrchr(fq_subname, ':');
1009 Perl_croak(aTHX_ "next::method/next::can/maybe::next::method cannot find enclosing method");
1012 subname_len = fq_subname_len - (subname - fq_subname);
1013 if(subname_len == 8 && strEQ(subname, "__ANON__")) {
1014 cxix = __dopoptosub_at(ccstack, cxix - 1);
1022 /* If we made it to here, we found our context */
1024 /* Initialize the next::method cache for this stash
1026 selfmeta = HvMROMETA(selfstash);
1027 if(!(nmcache = selfmeta->mro_nextmethod)) {
1028 nmcache = selfmeta->mro_nextmethod = newHV();
1030 else { /* Use the cached coderef if it exists */
1031 HE* cache_entry = hv_fetch_ent(nmcache, sv, 0, 0);
1033 SV* const val = HeVAL(cache_entry);
1034 if(val == &PL_sv_undef) {
1036 Perl_croak(aTHX_ "No next::method '%s' found for %s", subname, hvname);
1039 mXPUSHs(newRV_inc(val));
1044 /* beyond here is just for cache misses, so perf isn't as critical */
1046 stashname_len = subname - fq_subname - 2;
1047 stashname = newSVpvn_flags(fq_subname, stashname_len, SVs_TEMP);
1049 linear_av = mro_get_linear_isa_c3(selfstash, 0); /* has ourselves at the top of the list */
1051 linear_svp = AvARRAY(linear_av);
1052 entries = AvFILLp(linear_av) + 1;
1054 /* Walk down our MRO, skipping everything up
1055 to the contextually enclosing class */
1057 SV * const linear_sv = *linear_svp++;
1059 if(sv_eq(linear_sv, stashname))
1063 /* Now search the remainder of the MRO for the
1064 same method name as the contextually enclosing
1068 SV * const linear_sv = *linear_svp++;
1074 curstash = gv_stashsv(linear_sv, FALSE);
1077 if (ckWARN(WARN_SYNTAX))
1078 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Can't locate package %"SVf" for @%s::ISA",
1079 (void*)linear_sv, hvname);
1085 gvp = (GV**)hv_fetch(curstash, subname, subname_len, 0);
1091 if (SvTYPE(candidate) != SVt_PVGV)
1092 gv_init(candidate, curstash, subname, subname_len, TRUE);
1094 /* Notably, we only look for real entries, not method cache
1095 entries, because in C3 the method cache of a parent is not
1096 valid for the child */
1097 if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
1098 SvREFCNT_inc_simple_void_NN((SV*)cand_cv);
1099 (void)hv_store_ent(nmcache, newSVsv(sv), (SV*)cand_cv, 0);
1100 mXPUSHs(newRV_inc((SV*)cand_cv));
1106 (void)hv_store_ent(nmcache, newSVsv(sv), &PL_sv_undef, 0);
1108 Perl_croak(aTHX_ "No next::method '%s' found for %s", subname, hvname);
1114 * c-indentation-style: bsd
1116 * indent-tabs-mode: t
1119 * ex: set ts=8 sts=4 sw=4 noet: