1 #define PERL_NO_GET_CONTEXT
8 S_mro_get_linear_isa_c3(pTHX_ HV* stash, U32 level);
10 static const struct mro_alg c3_alg =
11 {S_mro_get_linear_isa_c3, "c3", 2, 0, 0};
14 =for apidoc mro_get_linear_isa_c3
16 Returns the C3 linearization of @ISA
17 the given stash. The return value is a read-only AV*.
18 C<level> should be 0 (it is used internally in this
19 function's recursion).
21 You are responsible for C<SvREFCNT_inc()> on the
22 return value if you plan to store it anywhere
23 semi-permanently (otherwise it might be deleted
24 out from under you the next time the cache is
31 S_mro_get_linear_isa_c3(pTHX_ HV* stash, U32 level)
38 struct mro_meta* meta;
42 stashhek = HvENAME_HEK(stash);
43 if (!stashhek) stashhek = HvNAME_HEK(stash);
45 Perl_croak(aTHX_ "Can't linearize anonymous symbol table");
48 Perl_croak(aTHX_ "Recursive inheritance detected in package '%"SVf"'",
49 SVfARG(sv_2mortal(newSVhek(stashhek))));
51 meta = HvMROMETA(stash);
53 /* return cache if valid */
54 if((retval = MUTABLE_AV(MRO_GET_PRIVATE_DATA(meta, &c3_alg)))) {
58 /* not in cache, make a new one */
60 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
61 isa = (gvp && (gv = *gvp) && isGV_with_GP(gv)) ? GvAV(gv) : NULL;
63 /* For a better idea how the rest of this works, see the much clearer
64 pure perl version in Algorithm::C3 0.01:
65 http://search.cpan.org/src/STEVAN/Algorithm-C3-0.01/lib/Algorithm/C3.pm
66 (later versions go about it differently than this code for speed reasons)
69 if(isa && AvFILLp(isa) >= 0) {
73 AV *const seqs = MUTABLE_AV(sv_2mortal(MUTABLE_SV(newAV())));
76 /* This builds @seqs, which is an array of arrays.
77 The members of @seqs are the MROs of
78 the members of @ISA, followed by @ISA itself.
80 I32 items = AvFILLp(isa) + 1;
81 SV** isa_ptr = AvARRAY(isa);
83 SV* const isa_item = *isa_ptr++;
84 HV* const isa_item_stash = gv_stashsv(isa_item, 0);
86 /* if no stash, make a temporary fake MRO
87 containing just itself */
88 AV* const isa_lin = newAV();
89 av_push(isa_lin, newSVsv(isa_item));
90 av_push(seqs, MUTABLE_SV(isa_lin));
95 = S_mro_get_linear_isa_c3(aTHX_ isa_item_stash, level + 1);
97 if(items == 0 && AvFILLp(seqs) == -1) {
98 /* Only one parent class. For this case, the C3
99 linearisation is this class followed by the parent's
100 linearisation, so don't bother with the expensive
103 I32 subrv_items = AvFILLp(isa_lin) + 1;
104 SV *const *subrv_p = AvARRAY(isa_lin);
106 /* Hijack the allocated but unused array seqs to be the
107 return value. It's currently mortalised. */
111 av_extend(retval, subrv_items);
112 AvFILLp(retval) = subrv_items;
113 svp = AvARRAY(retval);
115 /* First entry is this class. We happen to make a shared
116 hash key scalar because it's the cheapest and fastest
118 *svp++ = newSVhek(stashhek);
120 while(subrv_items--) {
121 /* These values are unlikely to be shared hash key
122 scalars, so no point in adding code to optimising
123 for a case that is unlikely to be true.
124 (Or prove me wrong and do it.) */
126 SV *const val = *subrv_p++;
127 *svp++ = newSVsv(val);
130 SvREFCNT_inc(retval);
134 av_push(seqs, SvREFCNT_inc_simple_NN(MUTABLE_SV(isa_lin)));
137 av_push(seqs, SvREFCNT_inc_simple_NN(MUTABLE_SV(isa)));
138 tails = MUTABLE_HV(sv_2mortal(MUTABLE_SV(newHV())));
140 /* This builds "heads", which as an array of integer array
141 indices, one per seq, which point at the virtual "head"
142 of the seq (initially zero) */
143 Newxz(heads, AvFILLp(seqs)+1, I32);
145 /* This builds %tails, which has one key for every class
146 mentioned in the tail of any sequence in @seqs (tail meaning
147 everything after the first class, the "head"). The value
148 is how many times this key appears in the tails of @seqs.
150 seqs_ptr = AvARRAY(seqs);
151 seqs_items = AvFILLp(seqs) + 1;
152 while(seqs_items--) {
153 AV *const seq = MUTABLE_AV(*seqs_ptr++);
154 I32 seq_items = AvFILLp(seq);
156 SV** seq_ptr = AvARRAY(seq) + 1;
158 SV* const seqitem = *seq_ptr++;
159 /* LVALUE fetch will create a new undefined SV if necessary
161 HE* const he = hv_fetch_ent(tails, seqitem, 1, 0);
163 SV* const val = HeVAL(he);
164 /* For 5.8.0 and later, sv_inc() with increment undef to
165 an IV of 1, which is what we want for a newly created
166 entry. However, for 5.6.x it will become an NV of
167 1.0, which confuses the SvIVX() checks above. */
169 SvIV_set(val, SvIVX(val) + 1);
178 /* Initialize retval to build the return value in */
180 av_push(retval, newSVhek(stashhek)); /* us first */
182 /* This loop won't terminate until we either finish building
183 the MRO, or get an exception. */
189 /* "foreach $seq (@seqs)" */
190 SV** const avptr = AvARRAY(seqs);
191 for(s = 0; s <= AvFILLp(seqs); s++) {
193 AV * const seq = MUTABLE_AV(avptr[s]);
195 if(!seq) continue; /* skip empty seqs */
196 svp = av_fetch(seq, heads[s], 0);
197 seqhead = *svp; /* seqhead = head of this seq */
201 /* if we haven't found a winner for this round yet,
202 and this seqhead is not in tails (or the count
203 for it in tails has dropped to zero), then this
204 seqhead is our new winner, and is added to the
205 final MRO immediately */
207 if((tail_entry = hv_fetch_ent(tails, cand, 0, 0))
208 && (val = HeVAL(tail_entry))
211 winner = newSVsv(cand);
212 av_push(retval, winner);
213 /* note however that even when we find a winner,
214 we continue looping over @seqs to do housekeeping */
216 if(!sv_cmp(seqhead, winner)) {
217 /* Once we have a winner (including the iteration
218 where we first found him), inc the head ptr
219 for any seq which had the winner as a head,
220 NULL out any seq which is now empty,
221 and adjust tails for consistency */
223 const int new_head = ++heads[s];
224 if(new_head > AvFILLp(seq)) {
225 SvREFCNT_dec(avptr[s]);
231 /* Because we know this new seqhead used to be
232 a tail, we can assume it is in tails and has
233 a positive value, which we need to dec */
234 svp = av_fetch(seq, new_head, 0);
236 tail_entry = hv_fetch_ent(tails, seqhead, 0, 0);
237 val = HeVAL(tail_entry);
243 /* if we found no candidates, we are done building the MRO.
244 !cand means no seqs have any entries left to check */
250 /* If we had candidates, but nobody won, then the @ISA
251 hierarchy is not C3-incompatible */
257 "Inconsistent hierarchy during C3 merge of class '%"SVf"':\n\t"
258 "current merge results [\n",
259 SVfARG(sv_2mortal(newSVhek(stashhek))));
260 for (i = 0; i <= av_len(retval); i++) {
261 SV **elem = av_fetch(retval, i, 0);
262 sv_catpvf(errmsg, "\t\t%"SVf",\n", SVfARG(*elem));
264 sv_catpvf(errmsg, "\t]\n\tmerging failed on '%"SVf"'", SVfARG(cand));
266 /* we have to do some cleanup before we croak */
268 SvREFCNT_dec(retval);
271 Perl_croak(aTHX_ "%"SVf, SVfARG(errmsg));
275 else { /* @ISA was undefined or empty */
276 /* build a retval containing only ourselves */
278 av_push(retval, newSVhek(stashhek));
282 /* we don't want anyone modifying the cache entry but us,
283 and we do so by replacing it completely */
284 SvREADONLY_on(retval);
286 return MUTABLE_AV(Perl_mro_set_private_data(aTHX_ meta, &c3_alg,
287 MUTABLE_SV(retval)));
291 /* These two are static helpers for next::method and friends,
292 and re-implement a bunch of the code from pp_caller() in
293 a more efficient manner for this particular usage.
297 __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) {
299 for (i = startingblock; i >= 0; i--) {
300 if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i;
305 MODULE = mro PACKAGE = mro PREFIX = mro_
308 mro_get_linear_isa(...)
315 if(items < 1 || items > 2)
316 croak_xs_usage(cv, "classname [, type ]");
319 class_stash = gv_stashsv(classname, 0);
322 /* No stash exists yet, give them just the classname */
323 AV* isalin = newAV();
324 av_push(isalin, newSVsv(classname));
325 ST(0) = sv_2mortal(newRV_noinc(MUTABLE_SV(isalin)));
329 const struct mro_alg *const algo = Perl_mro_get_from_name(aTHX_ ST(1));
331 Perl_croak(aTHX_ "Invalid mro name: '%"SVf"'", ST(1));
332 RETVAL = algo->resolve(aTHX_ class_stash, 0);
335 RETVAL = mro_get_linear_isa(class_stash);
337 ST(0) = newRV_inc(MUTABLE_SV(RETVAL));
347 struct mro_meta* meta;
350 croak_xs_usage(cv, "classname, type");
353 class_stash = gv_stashsv(classname, GV_ADD);
354 if(!class_stash) Perl_croak(aTHX_ "Cannot create class: '%"SVf"'!", SVfARG(classname));
355 meta = HvMROMETA(class_stash);
357 Perl_mro_set_mro(aTHX_ meta, ST(1));
369 croak_xs_usage(cv, "classname");
372 class_stash = gv_stashsv(classname, 0);
375 const struct mro_alg *const meta = HvMROMETA(class_stash)->mro_which;
376 ST(0) = newSVpvn_flags(meta->name, meta->length,
378 | ((meta->kflags & HVhek_UTF8) ? SVf_UTF8 : 0));
380 ST(0) = newSVpvn_flags("dfs", 3, SVs_TEMP);
394 croak_xs_usage(cv, "classname");
398 he = hv_fetch_ent(PL_isarev, classname, 0, 0);
399 isarev = he ? MUTABLE_HV(HeVAL(he)) : NULL;
405 while((iter = hv_iternext(isarev)))
406 av_push(ret_array, newSVsv(hv_iterkeysv(iter)));
408 mXPUSHs(newRV_noinc(MUTABLE_SV(ret_array)));
413 mro_is_universal(...)
419 STRLEN classname_len;
423 croak_xs_usage(cv, "classname");
427 classname_pv = SvPV(classname,classname_len);
429 he = hv_fetch_ent(PL_isarev, classname, 0, 0);
430 isarev = he ? MUTABLE_HV(HeVAL(he)) : NULL;
432 if((classname_len == 9 && strEQ(classname_pv, "UNIVERSAL"))
433 || (isarev && hv_exists(isarev, "UNIVERSAL", 9)))
440 mro_invalidate_all_method_caches(...)
444 croak_xs_usage(cv, "");
458 croak_xs_usage(cv, "classname");
462 class_stash = gv_stashsv(classname, 0);
464 mXPUSHi(class_stash ? HvMROMETA(class_stash)->pkg_gen : 0);
472 const I32 throw_nomethod = SvIVX(ST(1));
473 register I32 cxix = cxstack_ix;
474 register const PERL_CONTEXT *ccstack = cxstack;
475 const PERL_SI *top_si = PL_curstackinfo;
478 const char *fq_subname;
480 bool subname_utf8 = 0;
481 STRLEN stashname_len;
489 struct mro_meta* selfmeta;
495 if(sv_isobject(self))
496 selfstash = SvSTASH(SvRV(self));
498 selfstash = gv_stashsv(self, GV_ADD);
502 hvname = HvNAME_get(selfstash);
504 Perl_croak(aTHX_ "Can't use anonymous symbol table for method lookup");
506 /* This block finds the contextually-enclosing fully-qualified subname,
507 much like looking at (caller($i))[3] until you find a real sub that
508 isn't ANON, etc (also skips over pureperl next::method, etc) */
509 for(i = 0; i < 2; i++) {
510 cxix = __dopoptosub_at(ccstack, cxix);
513 STRLEN fq_subname_len;
515 /* we may be in a higher stacklevel, so dig down deeper */
517 if(top_si->si_type == PERLSI_MAIN)
518 Perl_croak(aTHX_ "next::method/next::can/maybe::next::method must be used in method context");
519 top_si = top_si->si_prev;
520 ccstack = top_si->si_cxstack;
521 cxix = __dopoptosub_at(ccstack, top_si->si_cxix);
524 if(CxTYPE((PERL_CONTEXT*)(&ccstack[cxix])) != CXt_SUB
525 || (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) {
526 cxix = __dopoptosub_at(ccstack, cxix - 1);
531 const I32 dbcxix = __dopoptosub_at(ccstack, cxix - 1);
532 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) {
533 if(CxTYPE((PERL_CONTEXT*)(&ccstack[dbcxix])) != CXt_SUB) {
540 cvgv = CvGV(ccstack[cxix].blk_sub.cv);
543 cxix = __dopoptosub_at(ccstack, cxix - 1);
547 /* we found a real sub here */
550 gv_efullname3(sv, cvgv, NULL);
553 fq_subname = SvPVX(sv);
554 fq_subname_len = SvCUR(sv);
556 subname_utf8 = SvUTF8(sv) ? 1 : 0;
557 subname = strrchr(fq_subname, ':');
563 Perl_croak(aTHX_ "next::method/next::can/maybe::next::method cannot find enclosing method");
566 subname_len = fq_subname_len - (subname - fq_subname);
567 if(subname_len == 8 && strEQ(subname, "__ANON__")) {
568 cxix = __dopoptosub_at(ccstack, cxix - 1);
576 /* If we made it to here, we found our context */
578 /* Initialize the next::method cache for this stash
580 selfmeta = HvMROMETA(selfstash);
581 if(!(nmcache = selfmeta->mro_nextmethod)) {
582 nmcache = selfmeta->mro_nextmethod = newHV();
584 else { /* Use the cached coderef if it exists */
585 HE* cache_entry = hv_fetch_ent(nmcache, sv, 0, 0);
587 SV* const val = HeVAL(cache_entry);
588 if(val == &PL_sv_undef) {
590 Perl_croak(aTHX_ "No next::method '%"SVf"' found for %"SVf,
591 SVfARG(newSVpvn_flags(subname, subname_len,
592 SVs_TEMP | ( subname_utf8 ? SVf_UTF8 : 0 ) )),
593 SVfARG(sv_2mortal(newSVhek( HvNAME_HEK(selfstash) ))));
596 mXPUSHs(newRV_inc(val));
601 /* beyond here is just for cache misses, so perf isn't as critical */
603 stashname_len = subname - fq_subname - 2;
604 stashname = newSVpvn_flags(fq_subname, stashname_len,
605 SVs_TEMP | (subname_utf8 ? SVf_UTF8 : 0));
607 /* has ourselves at the top of the list */
608 linear_av = S_mro_get_linear_isa_c3(aTHX_ selfstash, 0);
610 linear_svp = AvARRAY(linear_av);
611 entries = AvFILLp(linear_av) + 1;
613 /* Walk down our MRO, skipping everything up
614 to the contextually enclosing class */
616 SV * const linear_sv = *linear_svp++;
618 if(sv_eq(linear_sv, stashname))
622 /* Now search the remainder of the MRO for the
623 same method name as the contextually enclosing
627 SV * const linear_sv = *linear_svp++;
633 curstash = gv_stashsv(linear_sv, FALSE);
636 if (ckWARN(WARN_SYNTAX))
637 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Can't locate package %"SVf" for @%"SVf"::ISA",
639 SVfARG(sv_2mortal(newSVhek( HvNAME_HEK(selfstash) ))));
645 gvp = (GV**)hv_fetch(curstash, subname,
646 subname_utf8 ? -subname_len : subname_len, 0);
652 if (SvTYPE(candidate) != SVt_PVGV)
653 gv_init_pvn(candidate, curstash, subname, subname_len,
654 GV_ADDMULTI|(subname_utf8 ? SVf_UTF8 : 0));
656 /* Notably, we only look for real entries, not method cache
657 entries, because in C3 the method cache of a parent is not
658 valid for the child */
659 if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
660 SvREFCNT_inc_simple_void_NN(MUTABLE_SV(cand_cv));
661 (void)hv_store_ent(nmcache, sv, MUTABLE_SV(cand_cv), 0);
662 mXPUSHs(newRV_inc(MUTABLE_SV(cand_cv)));
668 (void)hv_store_ent(nmcache, sv, &PL_sv_undef, 0);
670 Perl_croak(aTHX_ "No next::method '%"SVf"' found for %"SVf,
671 SVfARG(newSVpvn_flags(subname, subname_len,
672 SVs_TEMP | ( subname_utf8 ? SVf_UTF8 : 0 ) )),
673 SVfARG(sv_2mortal(newSVhek( HvNAME_HEK(selfstash) ))));
677 Perl_mro_register(aTHX_ &c3_alg);