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 = (HV*)sv_2mortal((SV*)newHV());
39 SV** cache_entry = hv_fetch(cache, stashname, stashname_len, 0);
41 return (AV*)SvREFCNT_inc(*cache_entry);
44 /* not in cache, make a new one */
46 retval = (AV*)sv_2mortal((SV*)newAV());
47 av_push(retval, newSVpvn(stashname, stashname_len)); /* us first */
49 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
50 isa = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : NULL;
52 if(isa && AvFILLp(isa) >= 0) {
55 HV* tails = (HV*)sv_2mortal((SV*)newHV());
56 AV* seqs = (AV*)sv_2mortal((SV*)newAV());
57 I32 items = AvFILLp(isa) + 1;
58 SV** isa_ptr = AvARRAY(isa);
61 SV* isa_item = *isa_ptr++;
62 HV* isa_item_stash = gv_stashsv(isa_item, 0);
65 av_push(isa_lin, newSVsv(isa_item));
68 isa_lin = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(isa_item_stash, cache, level + 1)); /* recursion */
70 av_push(seqs, (SV*)av_make(AvFILLp(isa_lin)+1, AvARRAY(isa_lin)));
72 av_push(seqs, (SV*)av_make(AvFILLp(isa)+1, AvARRAY(isa)));
74 seqs_ptr = AvARRAY(seqs);
75 seqs_items = AvFILLp(seqs) + 1;
77 AV* seq = (AV*)*seqs_ptr++;
78 I32 seq_items = AvFILLp(seq);
80 SV** seq_ptr = AvARRAY(seq) + 1;
82 SV* seqitem = *seq_ptr++;
83 HE* he = hv_fetch_ent(tails, seqitem, 0, 0);
85 hv_store_ent(tails, seqitem, newSViv(1), 0);
102 SV** avptr = AvARRAY(seqs);
103 items = AvFILLp(seqs)+1;
107 if(AvFILLp(seq) < 0) continue;
108 svp = av_fetch(seq, 0, 0);
112 if((tail_entry = hv_fetch_ent(tails, cand, 0, 0))
113 && (val = HeVAL(tail_entry))
116 winner = newSVsv(cand);
117 av_push(retval, winner);
119 if(!sv_cmp(seqhead, winner)) {
121 /* this is basically shift(@seq) in void context */
122 SvREFCNT_dec(*AvARRAY(seq));
123 *AvARRAY(seq) = &PL_sv_undef;
124 AvARRAY(seq) = AvARRAY(seq) + 1;
128 if(AvFILLp(seq) < 0) continue;
129 svp = av_fetch(seq, 0, 0);
131 tail_entry = hv_fetch_ent(tails, seqhead, 0, 0);
132 val = HeVAL(tail_entry);
138 Perl_croak(aTHX_ "Inconsistent hierarchy during C3 merge of class '%s': "
139 "merging failed on parent '%s'", stashname, SvPV_nolen(cand));
143 SvREADONLY_on(retval);
144 SvREFCNT_inc(retval); /* for cache storage */
145 SvREFCNT_inc(retval); /* for return */
146 hv_store(cache, stashname, stashname_len, (SV*)retval, 0);
151 __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) {
153 for (i = startingblock; i >= 0; i--) {
154 if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i;
160 __nextcan(pTHX_ SV* self, I32 throw_nomethod)
163 register const PERL_CONTEXT *ccstack = cxstack;
164 const PERL_SI *top_si = PL_curstackinfo;
168 const char *fq_subname;
170 STRLEN fq_subname_len;
171 STRLEN stashname_len;
179 GV* candidate = NULL;
183 struct mro_meta* selfmeta;
187 if(sv_isobject(self))
188 selfstash = SvSTASH(SvRV(self));
190 selfstash = gv_stashsv(self, 0);
194 hvname = HvNAME(selfstash);
196 croak("Can't use anonymous symbol table for method lookup");
198 cxix = __dopoptosub_at(cxstack, cxstack_ix);
200 /* This block finds the contextually-enclosing fully-qualified subname,
201 much like looking at (caller($i))[3] until you find a real sub that
204 /* we may be in a higher stacklevel, so dig down deeper */
206 if(top_si->si_type == PERLSI_MAIN)
207 croak("next::method/next::can/maybe::next::method must be used in method context");
208 top_si = top_si->si_prev;
209 ccstack = top_si->si_cxstack;
210 cxix = __dopoptosub_at(ccstack, top_si->si_cxix);
213 if(CxTYPE((PERL_CONTEXT*)(&ccstack[cxix])) != CXt_SUB
214 || (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) {
215 cxix = __dopoptosub_at(ccstack, cxix - 1);
220 const I32 dbcxix = __dopoptosub_at(ccstack, cxix - 1);
221 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) {
222 if(CxTYPE((PERL_CONTEXT*)(&ccstack[dbcxix])) != CXt_SUB) {
229 cvgv = CvGV(ccstack[cxix].blk_sub.cv);
232 cxix = __dopoptosub_at(ccstack, cxix - 1);
236 /* we found a real sub here */
237 sv = sv_2mortal(newSV(0));
239 gv_efullname3(sv, cvgv, NULL);
241 fq_subname = SvPVX(sv);
242 fq_subname_len = SvCUR(sv);
244 subname = strrchr(fq_subname, ':');
246 croak("next::method/next::can/maybe::next::method cannot find enclosing method");
249 subname_len = fq_subname_len - (subname - fq_subname);
250 if(subname_len == 8 && strEQ(subname, "__ANON__")) {
251 cxix = __dopoptosub_at(ccstack, cxix - 1);
257 /* If we made it to here, we found our context */
260 XXX check %next::METHOD_CACHE
262 selfmeta = HvMROMETA(selfstash);
263 if(!(nmcache = selfmeta->mro_nextmethod)) {
264 nmcache = selfmeta->mro_nextmethod = newHV();
267 if((cache_entry = hv_fetch_ent(nmcache, sv, 0, 0))) {
268 SV* val = HeVAL(cache_entry);
269 if(val == &PL_sv_undef) {
271 croak("No next::method '%s' found for %s", subname, hvname);
274 return SvREFCNT_inc(val);
278 /* beyond here is just for cache misses, so perf isn't as critical */
280 stashname_len = subname - fq_subname - 2;
281 stashname = sv_2mortal(newSVpvn(fq_subname, stashname_len));
283 linear_av = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(selfstash, NULL, 0));
285 linear_svp = AvARRAY(linear_av);
286 items = AvFILLp(linear_av) + 1;
289 linear_sv = *linear_svp++;
291 if(sv_eq(linear_sv, stashname))
296 SV* sub_sv = sv_2mortal(newSVpv(subname, subname_len));
297 HV* cc3_mro = get_hv("Class::C3::MRO", 0);
300 linear_sv = *linear_svp++;
304 HE* he_cc3_mro_class = hv_fetch_ent(cc3_mro, linear_sv, 0, 0);
305 if(he_cc3_mro_class) {
306 HV* cc3_mro_class = (HV*)SvRV(HeVAL(he_cc3_mro_class));
307 SV** svp_cc3_mro_class_methods = hv_fetch(cc3_mro_class, "methods", 7, 0);
308 if(svp_cc3_mro_class_methods) {
309 HV* cc3_mro_class_methods = (HV*)SvRV(*svp_cc3_mro_class_methods);
310 if(hv_exists_ent(cc3_mro_class_methods, sub_sv, 0))
316 cstash = gv_stashsv(linear_sv, FALSE);
319 if (ckWARN(WARN_MISC))
320 Perl_warner(aTHX_ packWARN(WARN_MISC), "Can't locate package %"SVf" for @%s::ISA",
321 (void*)linear_sv, hvname);
327 gvp = (GV**)hv_fetch(cstash, subname, subname_len, 0);
333 if (SvTYPE(candidate) != SVt_PVGV)
334 gv_init(candidate, cstash, subname, subname_len, TRUE);
335 if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
336 SvREFCNT_inc((SV*)cand_cv);
337 /* XXX store result in cache */
338 /* hv_store_ent(nmcache, newSVsv(sv), (SV*)cand_cv, 0); */
344 /* XXX store undef in cache */
345 /* hv_store_ent(nmcache, newSVsv(sv), &PL_sv_undef, 0); */
347 croak("No next::method '%s' found for %s", subname, hvname);
351 XS(XS_Class_C3_XS_calculateMRO);
352 XS(XS_Class_C3_XS_calculateMRO)
368 if(items < 1 || items > 2)
369 croak("Usage: calculateMRO(classname[, cache])");
372 if(items == 2) cache = (HV*)SvRV(ST(1));
374 class_stash = gv_stashsv(classname, 0);
375 if(!class_stash) croak("No such class: '%s'!", SvPV_nolen(classname));
377 res = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(class_stash, cache, 0));
379 res_items = ret_items = AvFILLp(res) + 1;
380 res_ptr = AvARRAY(res);
385 SV* res_item = *res_ptr++;
404 SV* methcv = __nextcan(self, 0);
406 PERL_UNUSED_VAR(items);
408 if(methcv == &PL_sv_undef) {
409 ST(0) = &PL_sv_undef;
412 ST(0) = sv_2mortal(newRV_inc(methcv));
424 SV* methcv = __nextcan(self, 1);
427 call_sv(methcv, GIMME_V);
430 XS(XS_maybe_next_method);
431 XS(XS_maybe_next_method)
436 SV* methcv = __nextcan(self, 0);
438 if(methcv == &PL_sv_undef) {
439 ST(0) = &PL_sv_undef;
444 call_sv(methcv, GIMME_V);
447 MODULE = Class::C3::XS PACKAGE = Class::C3::XS
450 newXS("Class::C3::XS::calculateMRO", XS_Class_C3_XS_calculateMRO, __FILE__);
451 newXS("next::can", XS_next_can, __FILE__);
452 newXS("next::method", XS_next_method, __FILE__);
453 newXS("maybe::next::method", XS_maybe_next_method, __FILE__);