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;
24 stashname = HvNAME(stash);
25 stashname_len = strlen(stashname);
28 "Can't linearize anonymous symbol table");
31 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
35 cache = sv_2mortal(newHV());
39 SV** cache_entry = hv_fetch(cache, stashname, stashname_len, 0);
41 return (AV*)SvREFCNT_inc(*cache_entry);
45 /* not in cache, make a new one */
47 retval = (AV*)sv_2mortal((SV*)newAV());
48 av_push(retval, newSVpvn(stashname, stashname_len)); /* us first */
50 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
51 isa = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : NULL;
53 if(isa && AvFILLp(isa) >= 0) {
56 HV* tails = (HV*)sv_2mortal((SV*)newHV());
57 AV* seqs = (AV*)sv_2mortal((SV*)newAV());
58 I32 items = AvFILLp(isa) + 1;
59 SV** isa_ptr = AvARRAY(isa);
62 SV* isa_item = *isa_ptr++;
63 HV* isa_item_stash = gv_stashsv(isa_item, 0);
66 av_push(isa_lin, newSVsv(isa_item));
69 isa_lin = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(isa_item_stash, cache, level + 1)); /* recursion */
71 av_push(seqs, (SV*)av_make(AvFILLp(isa_lin)+1, AvARRAY(isa_lin)));
73 av_push(seqs, (SV*)av_make(AvFILLp(isa)+1, AvARRAY(isa)));
75 seqs_ptr = AvARRAY(seqs);
76 seqs_items = AvFILLp(seqs) + 1;
78 AV* seq = (AV*)*seqs_ptr++;
79 I32 seq_items = AvFILLp(seq);
81 SV** seq_ptr = AvARRAY(seq) + 1;
83 SV* seqitem = *seq_ptr++;
84 HE* he = hv_fetch_ent(tails, seqitem, 0, 0);
86 hv_store_ent(tails, seqitem, newSViv(1), 0);
103 SV** avptr = AvARRAY(seqs);
104 items = AvFILLp(seqs)+1;
108 if(AvFILLp(seq) < 0) continue;
109 svp = av_fetch(seq, 0, 0);
113 if((tail_entry = hv_fetch_ent(tails, cand, 0, 0))
114 && (val = HeVAL(tail_entry))
117 winner = newSVsv(cand);
118 av_push(retval, winner);
120 if(!sv_cmp(seqhead, winner)) {
122 /* this is basically shift(@seq) in void context */
123 SvREFCNT_dec(*AvARRAY(seq));
124 *AvARRAY(seq) = &PL_sv_undef;
125 AvARRAY(seq) = AvARRAY(seq) + 1;
129 if(AvFILLp(seq) < 0) continue;
130 svp = av_fetch(seq, 0, 0);
132 tail_entry = hv_fetch_ent(tails, seqhead, 0, 0);
133 val = HeVAL(tail_entry);
139 Perl_croak(aTHX_ "Inconsistent hierarchy during C3 merge of class '%s': "
140 "merging failed on parent '%s'", stashname, SvPV_nolen(cand));
144 SvREADONLY_on(retval);
145 SvREFCNT_inc(retval); /* for cache storage */
146 SvREFCNT_inc(retval); /* for return */
147 /* hv_store(cache, stashname, stashname_len, (SV*)retval, 0); */
152 __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) {
154 for (i = startingblock; i >= 0; i--) {
155 if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i;
161 __nextcan(pTHX_ SV* self, I32 throw_nomethod)
164 register const PERL_CONTEXT *ccstack = cxstack;
165 const PERL_SI *top_si = PL_curstackinfo;
169 const char *fq_subname;
171 STRLEN fq_subname_len;
172 STRLEN stashname_len;
180 GV* candidate = NULL;
184 struct mro_meta* selfmeta;
188 if(sv_isobject(self))
189 selfstash = SvSTASH(SvRV(self));
191 selfstash = gv_stashsv(self, 0);
195 hvname = HvNAME(selfstash);
197 croak("Can't use anonymous symbol table for method lookup");
199 cxix = __dopoptosub_at(cxstack, cxstack_ix);
201 /* This block finds the contextually-enclosing fully-qualified subname,
202 much like looking at (caller($i))[3] until you find a real sub that
205 /* we may be in a higher stacklevel, so dig down deeper */
207 if(top_si->si_type == PERLSI_MAIN)
208 croak("next::method/next::can/maybe::next::method must be used in method context");
209 top_si = top_si->si_prev;
210 ccstack = top_si->si_cxstack;
211 cxix = __dopoptosub_at(ccstack, top_si->si_cxix);
214 if(CxTYPE((PERL_CONTEXT*)(&ccstack[cxix])) != CXt_SUB
215 || (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) {
216 cxix = __dopoptosub_at(ccstack, cxix - 1);
221 const I32 dbcxix = __dopoptosub_at(ccstack, cxix - 1);
222 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) {
223 if(CxTYPE((PERL_CONTEXT*)(&ccstack[dbcxix])) != CXt_SUB) {
230 cvgv = CvGV(ccstack[cxix].blk_sub.cv);
233 cxix = __dopoptosub_at(ccstack, cxix - 1);
237 /* we found a real sub here */
238 sv = sv_2mortal(newSV(0));
240 gv_efullname3(sv, cvgv, NULL);
242 fq_subname = SvPVX(sv);
243 fq_subname_len = SvCUR(sv);
245 subname = strrchr(fq_subname, ':');
247 croak("next::method/next::can/maybe::next::method cannot find enclosing method");
250 subname_len = fq_subname_len - (subname - fq_subname);
251 if(subname_len == 8 && strEQ(subname, "__ANON__")) {
252 cxix = __dopoptosub_at(ccstack, cxix - 1);
258 /* If we made it to here, we found our context */
261 XXX check %next::METHOD_CACHE
263 selfmeta = HvMROMETA(selfstash);
264 if(!(nmcache = selfmeta->mro_nextmethod)) {
265 nmcache = selfmeta->mro_nextmethod = newHV();
268 if((cache_entry = hv_fetch_ent(nmcache, sv, 0, 0))) {
269 SV* val = HeVAL(cache_entry);
270 if(val == &PL_sv_undef) {
272 croak("No next::method '%s' found for %s", subname, hvname);
275 return SvREFCNT_inc(val);
279 /* beyond here is just for cache misses, so perf isn't as critical */
281 stashname_len = subname - fq_subname - 2;
282 stashname = sv_2mortal(newSVpvn(fq_subname, stashname_len));
284 linear_av = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(selfstash, NULL, 0));
286 linear_svp = AvARRAY(linear_av);
287 items = AvFILLp(linear_av) + 1;
290 linear_sv = *linear_svp++;
292 if(sv_eq(linear_sv, stashname))
297 SV* sub_sv = sv_2mortal(newSVpv(subname, subname_len));
298 HV* cc3_mro = get_hv("Class::C3::MRO", 0);
301 linear_sv = *linear_svp++;
305 HE* he_cc3_mro_class = hv_fetch_ent(cc3_mro, linear_sv, 0, 0);
306 if(he_cc3_mro_class) {
307 HV* cc3_mro_class = (HV*)SvRV(HeVAL(he_cc3_mro_class));
308 SV** svp_cc3_mro_class_methods = hv_fetch(cc3_mro_class, "methods", 7, 0);
309 if(svp_cc3_mro_class_methods) {
310 HV* cc3_mro_class_methods = (HV*)SvRV(*svp_cc3_mro_class_methods);
311 if(hv_exists_ent(cc3_mro_class_methods, sub_sv, 0))
317 cstash = gv_stashsv(linear_sv, FALSE);
320 if (ckWARN(WARN_MISC))
321 Perl_warner(aTHX_ packWARN(WARN_MISC), "Can't locate package %"SVf" for @%s::ISA",
322 (void*)linear_sv, hvname);
328 gvp = (GV**)hv_fetch(cstash, subname, subname_len, 0);
334 if (SvTYPE(candidate) != SVt_PVGV)
335 gv_init(candidate, cstash, subname, subname_len, TRUE);
336 if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
337 SvREFCNT_inc((SV*)cand_cv);
338 /* XXX store result in cache */
339 /* hv_store_ent(nmcache, newSVsv(sv), (SV*)cand_cv, 0); */
345 /* XXX store undef in cache */
346 /* hv_store_ent(nmcache, newSVsv(sv), &PL_sv_undef, 0); */
348 croak("No next::method '%s' found for %s", subname, hvname);
352 XS(XS_Class_C3_XS_calculateMRO);
353 XS(XS_Class_C3_XS_calculateMRO)
369 if(items < 1 || items > 2)
370 croak("Usage: calculateMRO(classname[, cache])");
373 if(items == 2) cache = (HV*)ST(1);
375 class_stash = gv_stashsv(classname, 0);
376 if(!class_stash) croak("No such class: '%s'!", SvPV_nolen(classname));
378 res = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(class_stash, cache, 0));
380 res_items = ret_items = AvFILLp(res) + 1;
381 res_ptr = AvARRAY(res);
386 SV* res_item = *res_ptr++;
405 SV* methcv = __nextcan(self, 0);
407 PERL_UNUSED_VAR(items);
409 if(methcv == &PL_sv_undef) {
410 ST(0) = &PL_sv_undef;
413 ST(0) = sv_2mortal(newRV_inc(methcv));
425 SV* methcv = __nextcan(self, 1);
428 call_sv(methcv, GIMME_V);
431 XS(XS_maybe_next_method);
432 XS(XS_maybe_next_method)
437 SV* methcv = __nextcan(self, 0);
439 if(methcv == &PL_sv_undef) {
440 ST(0) = &PL_sv_undef;
445 call_sv(methcv, GIMME_V);
448 MODULE = Class::C3::XS PACKAGE = Class::C3::XS
451 newXS("Class::C3::XS::calculateMRO", XS_Class_C3_XS_calculateMRO, __FILE__);
452 newXS("next::can", XS_next_can, __FILE__);
453 newXS("next::method", XS_next_method, __FILE__);
454 newXS("maybe::next::method", XS_maybe_next_method, __FILE__);