6 /* Most of this code is backported from bleadperl'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'",
38 /* XXX return cached entry for stashname if available */
41 /* not in cache, make a new one */
43 retval = (AV*)sv_2mortal((SV*)newAV());
44 av_push(retval, newSVpvn(stashname, stashname_len)); /* us first */
46 gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
47 isa = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : NULL;
49 if(isa && AvFILLp(isa) >= 0) {
52 HV* tails = (HV*)sv_2mortal((SV*)newHV());
53 AV* seqs = (AV*)sv_2mortal((SV*)newAV());
54 I32 items = AvFILLp(isa) + 1;
55 SV** isa_ptr = AvARRAY(isa);
58 SV* isa_item = *isa_ptr++;
59 HV* isa_item_stash = gv_stashsv(isa_item, 0);
62 av_push(isa_lin, newSVsv(isa_item));
65 isa_lin = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(isa_item_stash, cache, level + 1)); /* recursion */
67 av_push(seqs, (SV*)av_make(AvFILLp(isa_lin)+1, AvARRAY(isa_lin)));
69 av_push(seqs, (SV*)av_make(AvFILLp(isa)+1, AvARRAY(isa)));
71 seqs_ptr = AvARRAY(seqs);
72 seqs_items = AvFILLp(seqs) + 1;
74 AV* seq = (AV*)*seqs_ptr++;
75 I32 seq_items = AvFILLp(seq);
77 SV** seq_ptr = AvARRAY(seq) + 1;
79 SV* seqitem = *seq_ptr++;
80 HE* he = hv_fetch_ent(tails, seqitem, 0, 0);
82 hv_store_ent(tails, seqitem, newSViv(1), 0);
99 SV** avptr = AvARRAY(seqs);
100 items = AvFILLp(seqs)+1;
104 if(AvFILLp(seq) < 0) continue;
105 svp = av_fetch(seq, 0, 0);
109 if((tail_entry = hv_fetch_ent(tails, cand, 0, 0))
110 && (val = HeVAL(tail_entry))
113 winner = newSVsv(cand);
114 av_push(retval, winner);
116 if(!sv_cmp(seqhead, winner)) {
118 /* this is basically shift(@seq) in void context */
119 SvREFCNT_dec(*AvARRAY(seq));
120 *AvARRAY(seq) = &PL_sv_undef;
121 AvARRAY(seq) = AvARRAY(seq) + 1;
125 if(AvFILLp(seq) < 0) continue;
126 svp = av_fetch(seq, 0, 0);
128 tail_entry = hv_fetch_ent(tails, seqhead, 0, 0);
129 val = HeVAL(tail_entry);
135 Perl_croak(aTHX_ "Inconsistent hierarchy during C3 merge of class '%s': "
136 "merging failed on parent '%s'", stashname, SvPV_nolen(cand));
140 SvREADONLY_on(retval);
141 SvREFCNT_inc(retval); /* for cache storage */
142 SvREFCNT_inc(retval); /* for return to caller */
143 /* XXX store in cache storage */
148 __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) {
150 for (i = startingblock; i >= 0; i--) {
151 if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i;
157 __nextcan(pTHX_ SV* self, I32 throw_nomethod)
160 register const PERL_CONTEXT *ccstack = cxstack;
161 const PERL_SI *top_si = PL_curstackinfo;
165 const char *fq_subname;
167 STRLEN fq_subname_len;
168 STRLEN stashname_len;
176 GV* candidate = NULL;
180 struct mro_meta* selfmeta;
184 if(sv_isobject(self))
185 selfstash = SvSTASH(SvRV(self));
187 selfstash = gv_stashsv(self, 0);
191 hvname = HvNAME(selfstash);
193 croak("Can't use anonymous symbol table for method lookup");
195 cxix = __dopoptosub_at(cxstack, cxstack_ix);
197 /* This block finds the contextually-enclosing fully-qualified subname,
198 much like looking at (caller($i))[3] until you find a real sub that
201 /* we may be in a higher stacklevel, so dig down deeper */
203 if(top_si->si_type == PERLSI_MAIN)
204 croak("next::method/next::can/maybe::next::method must be used in method context");
205 top_si = top_si->si_prev;
206 ccstack = top_si->si_cxstack;
207 cxix = __dopoptosub_at(ccstack, top_si->si_cxix);
210 if(CxTYPE((PERL_CONTEXT*)(&ccstack[cxix])) != CXt_SUB
211 || (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) {
212 cxix = __dopoptosub_at(ccstack, cxix - 1);
217 const I32 dbcxix = __dopoptosub_at(ccstack, cxix - 1);
218 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) {
219 if(CxTYPE((PERL_CONTEXT*)(&ccstack[dbcxix])) != CXt_SUB) {
226 cvgv = CvGV(ccstack[cxix].blk_sub.cv);
229 cxix = __dopoptosub_at(ccstack, cxix - 1);
233 /* we found a real sub here */
234 sv = sv_2mortal(newSV(0));
236 gv_efullname3(sv, cvgv, NULL);
238 fq_subname = SvPVX(sv);
239 fq_subname_len = SvCUR(sv);
241 subname = strrchr(fq_subname, ':');
243 croak("next::method/next::can/maybe::next::method cannot find enclosing method");
246 subname_len = fq_subname_len - (subname - fq_subname);
247 if(subname_len == 8 && strEQ(subname, "__ANON__")) {
248 cxix = __dopoptosub_at(ccstack, cxix - 1);
254 /* If we made it to here, we found our context */
257 XXX check %next::METHOD_CACHE
259 selfmeta = HvMROMETA(selfstash);
260 if(!(nmcache = selfmeta->mro_nextmethod)) {
261 nmcache = selfmeta->mro_nextmethod = newHV();
264 if((cache_entry = hv_fetch_ent(nmcache, sv, 0, 0))) {
265 SV* val = HeVAL(cache_entry);
266 if(val == &PL_sv_undef) {
268 croak("No next::method '%s' found for %s", subname, hvname);
271 return SvREFCNT_inc(val);
275 /* beyond here is just for cache misses, so perf isn't as critical */
277 stashname_len = subname - fq_subname - 2;
278 stashname = sv_2mortal(newSVpvn(fq_subname, stashname_len));
280 linear_av = __mro_linear_isa_c3(selfstash, NULL, 0); /* has ourselves at the top of the list */
281 sv_2mortal((SV*)linear_av);
283 linear_svp = AvARRAY(linear_av);
284 items = AvFILLp(linear_av) + 1;
287 linear_sv = *linear_svp++;
289 if(sv_eq(linear_sv, stashname))
295 linear_sv = *linear_svp++;
297 cstash = gv_stashsv(linear_sv, FALSE);
300 if (ckWARN(WARN_MISC))
301 Perl_warner(aTHX_ packWARN(WARN_MISC), "Can't locate package %"SVf" for @%s::ISA",
302 (void*)linear_sv, hvname);
308 /* XXX we need to fetch from %Class::C3::MRO, not the real stash table */
310 gvp = (GV**)hv_fetch(cstash, subname, subname_len, 0);
316 if (SvTYPE(candidate) != SVt_PVGV)
317 gv_init(candidate, cstash, subname, subname_len, TRUE);
318 if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
319 SvREFCNT_inc((SV*)cand_cv);
320 /* XXX store result in cache */
321 /* hv_store_ent(nmcache, newSVsv(sv), (SV*)cand_cv, 0); */
327 /* XXX store undef in cache */
328 /* hv_store_ent(nmcache, newSVsv(sv), &PL_sv_undef, 0); */
330 croak("No next::method '%s' found for %s", subname, hvname);
334 XS(XS_Class_C3_XS_calculateMRO);
335 XS(XS_Class_C3_XS_calculateMRO)
351 if(items < 1 || items > 2)
352 croak("Usage: calculateMRO(classname[, cache])");
355 if(items == 2) cache = (HV*)ST(1);
357 class_stash = gv_stashsv(classname, 0);
358 if(!class_stash) croak("No such class: '%s'!", SvPV_nolen(classname));
360 res = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(class_stash, cache, 0));
363 res_items = ret_items = AvFILLp(res) + 1;
364 res_ptr = AvARRAY(res);
369 SV* res_item = *res_ptr++;
388 SV* methcv = __nextcan(self, 0);
390 PERL_UNUSED_VAR(items);
392 if(methcv == &PL_sv_undef) {
393 ST(0) = &PL_sv_undef;
396 ST(0) = sv_2mortal(newRV_inc(methcv));
408 SV* methcv = __nextcan(self, 1);
411 call_sv(methcv, GIMME_V);
414 XS(XS_maybe_next_method);
415 XS(XS_maybe_next_method)
420 SV* methcv = __nextcan(self, 0);
422 if(methcv == &PL_sv_undef) {
423 ST(0) = &PL_sv_undef;
428 call_sv(methcv, GIMME_V);
431 MODULE = Class::C3::XS PACKAGE = Class::C3::XS
434 newXS("Class::C3::XS::calculateMRO", XS_Class_C3_XS_calculateMRO, __FILE__);
435 newXS("next::can", XS_next_can, __FILE__);
436 newXS("next::method", XS_next_method, __FILE__);
437 newXS("maybe::next::method", XS_maybe_next_method, __FILE__);