6 /* Most of this code is backported from the bleadperl patch's
7 mro.c, and then modified to work with Class::C3's
12 __mro_linear_isa_c3(pTHX_ HV* stash, HV* cache, I32 level)
18 const char* stashname;
23 stashname = HvNAME(stash);
24 stashname_len = strlen(stashname);
27 "Can't linearize anonymous symbol table");
30 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
34 cache = (HV*)sv_2mortal((SV*)newHV());
37 SV** cache_entry = hv_fetch(cache, stashname, stashname_len, 0);
39 return (AV*)SvREFCNT_inc(*cache_entry);
42 /* not in cache, make a new one */
45 av_push(retval, newSVpvn(stashname, stashname_len)); /* us first */
47 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
48 isa = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : NULL;
50 if(isa && AvFILLp(isa) >= 0) {
53 HV* tails = (HV*)sv_2mortal((SV*)newHV());
54 AV* seqs = (AV*)sv_2mortal((SV*)newAV());
55 I32 items = AvFILLp(isa) + 1;
56 SV** isa_ptr = AvARRAY(isa);
59 SV* isa_item = *isa_ptr++;
60 HV* isa_item_stash = gv_stashsv(isa_item, 0);
63 av_push(isa_lin, newSVsv(isa_item));
66 isa_lin = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(aTHX_ isa_item_stash, cache, level + 1)); /* recursion */
68 av_push(seqs, (SV*)av_make(AvFILLp(isa_lin)+1, AvARRAY(isa_lin)));
70 av_push(seqs, (SV*)av_make(AvFILLp(isa)+1, AvARRAY(isa)));
72 seqs_ptr = AvARRAY(seqs);
73 seqs_items = AvFILLp(seqs) + 1;
75 AV* seq = (AV*)*seqs_ptr++;
76 I32 seq_items = AvFILLp(seq);
78 SV** seq_ptr = AvARRAY(seq) + 1;
80 SV* seqitem = *seq_ptr++;
81 HE* he = hv_fetch_ent(tails, seqitem, 0, 0);
83 hv_store_ent(tails, seqitem, newSViv(1), 0);
100 SV** avptr = AvARRAY(seqs);
101 items = AvFILLp(seqs)+1;
105 if(AvFILLp(seq) < 0) continue;
106 svp = av_fetch(seq, 0, 0);
110 if((tail_entry = hv_fetch_ent(tails, cand, 0, 0))
111 && (val = HeVAL(tail_entry))
114 winner = newSVsv(cand);
115 av_push(retval, winner);
117 if(!sv_cmp(seqhead, winner)) {
118 sv_2mortal(av_shift(seq));
119 if(AvFILLp(seq) < 0) continue;
120 svp = av_fetch(seq, 0, 0);
122 tail_entry = hv_fetch_ent(tails, seqhead, 0, 0);
123 val = HeVAL(tail_entry);
129 SvREFCNT_dec(retval);
130 Perl_croak(aTHX_ "Inconsistent hierarchy during C3 merge of class '%s': "
131 "merging failed on parent '%s'", stashname, SvPV_nolen(cand));
136 SvREADONLY_on(retval);
137 hv_store(cache, stashname, stashname_len, (SV*)retval, 0);
138 return (AV*)SvREFCNT_inc(retval);
142 __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) {
144 for (i = startingblock; i >= 0; i--) {
145 if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i;
151 __nextcan(pTHX_ SV* self, I32 throw_nomethod)
154 register const PERL_CONTEXT *ccstack = cxstack;
155 const PERL_SI *top_si = PL_curstackinfo;
159 const char *fq_subname;
161 STRLEN fq_subname_len;
162 STRLEN stashname_len;
170 GV* candidate = NULL;
178 if(sv_isobject(self))
179 selfstash = SvSTASH(SvRV(self));
181 selfstash = gv_stashsv(self, 0);
185 hvname = HvNAME(selfstash);
187 croak("Can't use anonymous symbol table for method lookup");
189 cxix = __dopoptosub_at(cxstack, cxstack_ix);
191 /* This block finds the contextually-enclosing fully-qualified subname,
192 much like looking at (caller($i))[3] until you find a real sub that
195 /* we may be in a higher stacklevel, so dig down deeper */
197 if(top_si->si_type == PERLSI_MAIN)
198 croak("next::method/next::can/maybe::next::method must be used in method context");
199 top_si = top_si->si_prev;
200 ccstack = top_si->si_cxstack;
201 cxix = __dopoptosub_at(ccstack, top_si->si_cxix);
204 if(CxTYPE((PERL_CONTEXT*)(&ccstack[cxix])) != CXt_SUB
205 || (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) {
206 cxix = __dopoptosub_at(ccstack, cxix - 1);
211 const I32 dbcxix = __dopoptosub_at(ccstack, cxix - 1);
212 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) {
213 if(CxTYPE((PERL_CONTEXT*)(&ccstack[dbcxix])) != CXt_SUB) {
220 cvgv = CvGV(ccstack[cxix].blk_sub.cv);
223 cxix = __dopoptosub_at(ccstack, cxix - 1);
227 /* we found a real sub here */
228 sv = sv_2mortal(newSV(0));
230 gv_efullname3(sv, cvgv, NULL);
232 fq_subname = SvPVX(sv);
233 fq_subname_len = SvCUR(sv);
235 subname = strrchr(fq_subname, ':');
237 croak("next::method/next::can/maybe::next::method cannot find enclosing method");
240 subname_len = fq_subname_len - (subname - fq_subname);
241 if(subname_len == 8 && strEQ(subname, "__ANON__")) {
242 cxix = __dopoptosub_at(ccstack, cxix - 1);
248 /* If we made it to here, we found our context */
250 /* cachekey = "objpkg|context::method::name" */
251 cachekey = sv_2mortal(newSVpv(hvname, 0));
252 sv_catpvn(cachekey, "|", 1);
253 sv_catsv(cachekey, sv);
255 nmcache = get_hv("next::METHOD_CACHE", 1);
256 if((cache_entry = hv_fetch_ent(nmcache, cachekey, 0, 0))) {
257 SV* val = HeVAL(cache_entry);
258 if(val == &PL_sv_undef) {
260 croak("No next::method '%s' found for %s", subname, hvname);
263 return SvREFCNT_inc(val);
266 /* beyond here is just for cache misses, so perf isn't as critical */
268 stashname_len = subname - fq_subname - 2;
269 stashname = sv_2mortal(newSVpvn(fq_subname, stashname_len));
271 linear_av = __mro_linear_isa_c3(aTHX_ selfstash, NULL, 0);
273 linear_svp = AvARRAY(linear_av);
274 items = AvFILLp(linear_av) + 1;
277 linear_sv = *linear_svp++;
279 if(sv_eq(linear_sv, stashname))
284 SV* sub_sv = sv_2mortal(newSVpv(subname, subname_len));
285 HV* cc3_mro = get_hv("Class::C3::MRO", 0);
288 linear_sv = *linear_svp++;
292 HE* he_cc3_mro_class = hv_fetch_ent(cc3_mro, linear_sv, 0, 0);
293 if(he_cc3_mro_class) {
294 SV* cc3_mro_class_sv = HeVAL(he_cc3_mro_class);
295 if(SvROK(cc3_mro_class_sv)) {
296 HV* cc3_mro_class = (HV*)SvRV(cc3_mro_class_sv);
297 SV** svp_cc3_mro_class_methods = hv_fetch(cc3_mro_class, "methods", 7, 0);
298 if(svp_cc3_mro_class_methods) {
299 SV* cc3_mro_class_methods_sv = *svp_cc3_mro_class_methods;
300 if(SvROK(cc3_mro_class_methods_sv)) {
301 HV* cc3_mro_class_methods = (HV*)SvRV(cc3_mro_class_methods_sv);
302 if(hv_exists_ent(cc3_mro_class_methods, sub_sv, 0))
310 cstash = gv_stashsv(linear_sv, FALSE);
313 if (ckWARN(WARN_MISC))
314 Perl_warner(aTHX_ packWARN(WARN_MISC), "Can't locate package %"SVf" for @%s::ISA",
315 (void*)linear_sv, hvname);
321 gvp = (GV**)hv_fetch(cstash, subname, subname_len, 0);
327 if (SvTYPE(candidate) != SVt_PVGV)
328 gv_init(candidate, cstash, subname, subname_len, TRUE);
329 if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
330 SvREFCNT_dec(linear_av);
331 SvREFCNT_inc((SV*)cand_cv);
332 hv_store_ent(nmcache, newSVsv(cachekey), (SV*)cand_cv, 0);
338 SvREFCNT_dec(linear_av);
339 hv_store_ent(nmcache, newSVsv(cachekey), &PL_sv_undef, 0);
341 croak("No next::method '%s' found for %s", subname, hvname);
345 XS(XS_Class_C3_XS_calculateMRO);
346 XS(XS_Class_C3_XS_calculateMRO)
362 if(items < 1 || items > 2)
363 croak("Usage: calculateMRO(classname[, cache])");
366 if(items == 2) cache = (HV*)SvRV(ST(1));
368 class_stash = gv_stashsv(classname, 0);
369 if(!class_stash) croak("No such class: '%s'!", SvPV_nolen(classname));
371 res = __mro_linear_isa_c3(aTHX_ class_stash, cache, 0);
373 res_items = ret_items = AvFILLp(res) + 1;
374 res_ptr = AvARRAY(res);
379 SV* res_item = *res_ptr++;
380 XPUSHs(sv_2mortal(newSVsv(res_item)));
399 SV* methcv = __nextcan(aTHX_ self, 0);
401 PERL_UNUSED_VAR(items);
403 if(methcv == &PL_sv_undef) {
404 ST(0) = &PL_sv_undef;
407 ST(0) = sv_2mortal(newRV_inc(methcv));
419 SV* methcv = __nextcan(aTHX_ self, 1);
422 call_sv(methcv, GIMME_V);
425 XS(XS_maybe_next_method);
426 XS(XS_maybe_next_method)
431 SV* methcv = __nextcan(aTHX_ self, 0);
433 if(methcv == &PL_sv_undef) {
434 ST(0) = &PL_sv_undef;
439 call_sv(methcv, GIMME_V);
442 MODULE = Class::C3::XS PACKAGE = Class::C3::XS
445 newXS("Class::C3::XS::calculateMRO", XS_Class_C3_XS_calculateMRO, __FILE__);
446 newXS("next::can", XS_next_can, __FILE__);
447 newXS("next::method", XS_next_method, __FILE__);
448 newXS("maybe::next::method", XS_maybe_next_method, __FILE__);