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 C<@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 '%" HEKf
52 meta = HvMROMETA(stash);
54 /* return cache if valid */
55 if((retval = MUTABLE_AV(MRO_GET_PRIVATE_DATA(meta, &c3_alg)))) {
59 /* not in cache, make a new one */
61 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
62 isa = (gvp && (gv = *gvp) && isGV_with_GP(gv)) ? GvAV(gv) : NULL;
64 /* For a better idea how the rest of this works, see the much clearer
65 pure perl version in Algorithm::C3 0.01:
66 http://search.cpan.org/src/STEVAN/Algorithm-C3-0.01/lib/Algorithm/C3.pm
67 (later versions go about it differently than this code for speed reasons)
70 if(isa && AvFILLp(isa) >= 0) {
74 AV *const seqs = MUTABLE_AV(sv_2mortal(MUTABLE_SV(newAV())));
77 /* This builds @seqs, which is an array of arrays.
78 The members of @seqs are the MROs of
79 the members of @ISA, followed by @ISA itself.
81 SSize_t items = AvFILLp(isa) + 1;
82 SV** isa_ptr = AvARRAY(isa);
84 SV* const isa_item = *isa_ptr ? *isa_ptr : &PL_sv_undef;
85 HV* const isa_item_stash = gv_stashsv(isa_item, 0);
88 /* if no stash, make a temporary fake MRO
89 containing just itself */
90 AV* const isa_lin = newAV();
91 av_push(isa_lin, newSVsv(isa_item));
92 av_push(seqs, MUTABLE_SV(isa_lin));
97 = S_mro_get_linear_isa_c3(aTHX_ isa_item_stash, level + 1);
99 if(items == 0 && AvFILLp(seqs) == -1) {
100 /* Only one parent class. For this case, the C3
101 linearisation is this class followed by the parent's
102 linearisation, so don't bother with the expensive
105 I32 subrv_items = AvFILLp(isa_lin) + 1;
106 SV *const *subrv_p = AvARRAY(isa_lin);
108 /* Hijack the allocated but unused array seqs to be the
109 return value. It's currently mortalised. */
113 av_extend(retval, subrv_items);
114 AvFILLp(retval) = subrv_items;
115 svp = AvARRAY(retval);
117 /* First entry is this class. We happen to make a shared
118 hash key scalar because it's the cheapest and fastest
120 *svp++ = newSVhek(stashhek);
122 while(subrv_items--) {
123 /* These values are unlikely to be shared hash key
124 scalars, so no point in adding code to optimising
125 for a case that is unlikely to be true.
126 (Or prove me wrong and do it.) */
128 SV *const val = *subrv_p++;
129 *svp++ = newSVsv(val);
132 SvREFCNT_inc(retval);
136 av_push(seqs, SvREFCNT_inc_simple_NN(MUTABLE_SV(isa_lin)));
139 av_push(seqs, SvREFCNT_inc_simple_NN(MUTABLE_SV(isa)));
140 tails = MUTABLE_HV(sv_2mortal(MUTABLE_SV(newHV())));
142 /* This builds "heads", which as an array of integer array
143 indices, one per seq, which point at the virtual "head"
144 of the seq (initially zero) */
145 Newxz(heads, AvFILLp(seqs)+1, I32);
147 /* This builds %tails, which has one key for every class
148 mentioned in the tail of any sequence in @seqs (tail meaning
149 everything after the first class, the "head"). The value
150 is how many times this key appears in the tails of @seqs.
152 seqs_ptr = AvARRAY(seqs);
153 seqs_items = AvFILLp(seqs) + 1;
154 while(seqs_items--) {
155 AV *const seq = MUTABLE_AV(*seqs_ptr++);
156 I32 seq_items = AvFILLp(seq);
158 SV** seq_ptr = AvARRAY(seq) + 1;
160 SV* const seqitem = *seq_ptr++;
161 /* LVALUE fetch will create a new undefined SV if necessary
163 HE* const he = hv_fetch_ent(tails, seqitem, 1, 0);
165 SV* const val = HeVAL(he);
166 /* For 5.8.0 and later, sv_inc() with increment undef to
167 an IV of 1, which is what we want for a newly created
168 entry. However, for 5.6.x it will become an NV of
169 1.0, which confuses the SvIVX() checks above. */
171 SvIV_set(val, SvIVX(val) + 1);
180 /* Initialize retval to build the return value in */
182 av_push(retval, newSVhek(stashhek)); /* us first */
184 /* This loop won't terminate until we either finish building
185 the MRO, or get an exception. */
191 /* "foreach $seq (@seqs)" */
192 SV** const avptr = AvARRAY(seqs);
193 for(s = 0; s <= AvFILLp(seqs); s++) {
195 AV * const seq = MUTABLE_AV(avptr[s]);
197 if(!seq) continue; /* skip empty seqs */
198 svp = av_fetch(seq, heads[s], 0);
199 seqhead = *svp; /* seqhead = head of this seq */
203 /* if we haven't found a winner for this round yet,
204 and this seqhead is not in tails (or the count
205 for it in tails has dropped to zero), then this
206 seqhead is our new winner, and is added to the
207 final MRO immediately */
209 if((tail_entry = hv_fetch_ent(tails, cand, 0, 0))
210 && (val = HeVAL(tail_entry))
213 winner = newSVsv(cand);
214 av_push(retval, winner);
215 /* note however that even when we find a winner,
216 we continue looping over @seqs to do housekeeping */
218 if(!sv_cmp(seqhead, winner)) {
219 /* Once we have a winner (including the iteration
220 where we first found him), inc the head ptr
221 for any seq which had the winner as a head,
222 NULL out any seq which is now empty,
223 and adjust tails for consistency */
225 const int new_head = ++heads[s];
226 if(new_head > AvFILLp(seq)) {
227 SvREFCNT_dec(avptr[s]);
233 /* Because we know this new seqhead used to be
234 a tail, we can assume it is in tails and has
235 a positive value, which we need to dec */
236 svp = av_fetch(seq, new_head, 0);
238 tail_entry = hv_fetch_ent(tails, seqhead, 0, 0);
239 val = HeVAL(tail_entry);
245 /* if we found no candidates, we are done building the MRO.
246 !cand means no seqs have any entries left to check */
252 /* If we had candidates, but nobody won, then the @ISA
253 hierarchy is not C3-incompatible */
259 "Inconsistent hierarchy during C3 merge of class '%" HEKf "':\n\t"
260 "current merge results [\n",
262 for (i = 0; i < av_count(retval); i++) {
263 SV **elem = av_fetch(retval, i, 0);
264 sv_catpvf(errmsg, "\t\t%" SVf ",\n", SVfARG(*elem));
266 sv_catpvf(errmsg, "\t]\n\tmerging failed on '%" SVf "'", SVfARG(cand));
268 /* we have to do some cleanup before we croak */
270 SvREFCNT_dec(retval);
273 Perl_croak(aTHX_ "%" SVf, SVfARG(errmsg));
277 else { /* @ISA was undefined or empty */
278 /* build a retval containing only ourselves */
280 av_push(retval, newSVhek(stashhek));
284 /* we don't want anyone modifying the cache entry but us,
285 and we do so by replacing it completely */
286 SvREADONLY_on(retval);
288 return MUTABLE_AV(Perl_mro_set_private_data(aTHX_ meta, &c3_alg,
289 MUTABLE_SV(retval)));
293 /* These two are static helpers for next::method and friends,
294 and re-implement a bunch of the code from pp_caller() in
295 a more efficient manner for this particular usage.
299 __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) {
301 for (i = startingblock; i >= 0; i--) {
302 if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i;
307 MODULE = mro PACKAGE = mro PREFIX = mro_
310 mro_get_linear_isa(...)
317 if(items < 1 || items > 2)
318 croak_xs_usage(cv, "classname [, type ]");
321 class_stash = gv_stashsv(classname, 0);
324 /* No stash exists yet, give them just the classname */
325 AV* isalin = newAV();
326 av_push(isalin, newSVsv(classname));
327 ST(0) = sv_2mortal(newRV_noinc(MUTABLE_SV(isalin)));
331 const struct mro_alg *const algo = Perl_mro_get_from_name(aTHX_ ST(1));
333 Perl_croak(aTHX_ "Invalid mro name: '%" SVf "'", ST(1));
334 RETVAL = algo->resolve(aTHX_ class_stash, 0);
337 RETVAL = mro_get_linear_isa(class_stash);
339 ST(0) = newRV_inc(MUTABLE_SV(RETVAL));
349 struct mro_meta* meta;
352 croak_xs_usage(cv, "classname, type");
355 class_stash = gv_stashsv(classname, GV_ADD);
356 if(!class_stash) Perl_croak(aTHX_ "Cannot create class: '%" SVf "'!", SVfARG(classname));
357 meta = HvMROMETA(class_stash);
359 Perl_mro_set_mro(aTHX_ meta, ST(1));
371 croak_xs_usage(cv, "classname");
374 class_stash = gv_stashsv(classname, 0);
377 const struct mro_alg *const meta = HvMROMETA(class_stash)->mro_which;
378 ST(0) = newSVpvn_flags(meta->name, meta->length,
380 | ((meta->kflags & HVhek_UTF8) ? SVf_UTF8 : 0));
382 ST(0) = newSVpvn_flags("dfs", 3, SVs_TEMP);
396 croak_xs_usage(cv, "classname");
400 he = hv_fetch_ent(PL_isarev, classname, 0, 0);
401 isarev = he ? MUTABLE_HV(HeVAL(he)) : NULL;
407 while((iter = hv_iternext(isarev)))
408 av_push(ret_array, newSVsv(hv_iterkeysv(iter)));
410 mXPUSHs(newRV_noinc(MUTABLE_SV(ret_array)));
415 mro_is_universal(...)
421 STRLEN classname_len;
425 croak_xs_usage(cv, "classname");
429 classname_pv = SvPV(classname,classname_len);
431 he = hv_fetch_ent(PL_isarev, classname, 0, 0);
432 isarev = he ? MUTABLE_HV(HeVAL(he)) : NULL;
434 if((memEQs(classname_pv, classname_len, "UNIVERSAL"))
435 || (isarev && hv_existss(isarev, "UNIVERSAL")))
442 mro_invalidate_all_method_caches(...)
446 croak_xs_usage(cv, "");
460 croak_xs_usage(cv, "classname");
464 class_stash = gv_stashsv(classname, 0);
466 mXPUSHi(class_stash ? HvMROMETA(class_stash)->pkg_gen : 0);
474 const I32 throw_nomethod = SvIVX(ST(1));
475 I32 cxix = cxstack_ix;
476 const PERL_CONTEXT *ccstack = cxstack;
477 const PERL_SI *top_si = PL_curstackinfo;
480 const char *fq_subname = NULL;
481 const char *subname = NULL;
482 bool subname_utf8 = 0;
483 STRLEN stashname_len;
491 struct mro_meta* selfmeta;
497 if(sv_isobject(self))
498 selfstash = SvSTASH(SvRV(self));
500 selfstash = gv_stashsv(self, GV_ADD);
504 hvname = HvNAME_get(selfstash);
506 Perl_croak(aTHX_ "Can't use anonymous symbol table for method lookup");
508 /* This block finds the contextually-enclosing fully-qualified subname,
509 much like looking at (caller($i))[3] until you find a real sub that
510 isn't ANON, etc (also skips over pureperl next::method, etc) */
511 for(i = 0; i < 2; i++) {
512 cxix = __dopoptosub_at(ccstack, cxix);
515 STRLEN fq_subname_len;
517 /* we may be in a higher stacklevel, so dig down deeper */
519 if(top_si->si_type == PERLSI_MAIN)
520 Perl_croak(aTHX_ "next::method/next::can/maybe::next::method must be used in method context");
521 top_si = top_si->si_prev;
522 ccstack = top_si->si_cxstack;
523 cxix = __dopoptosub_at(ccstack, top_si->si_cxix);
526 if(CxTYPE((PERL_CONTEXT*)(&ccstack[cxix])) != CXt_SUB
527 || (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) {
528 cxix = __dopoptosub_at(ccstack, cxix - 1);
533 const I32 dbcxix = __dopoptosub_at(ccstack, cxix - 1);
534 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) {
535 if(CxTYPE((PERL_CONTEXT*)(&ccstack[dbcxix])) != CXt_SUB) {
542 cvgv = CvGV(ccstack[cxix].blk_sub.cv);
545 cxix = __dopoptosub_at(ccstack, cxix - 1);
549 /* we found a real sub here */
552 gv_efullname3(sv, cvgv, NULL);
555 fq_subname = SvPVX(sv);
556 fq_subname_len = SvCUR(sv);
558 subname_utf8 = SvUTF8(sv) ? 1 : 0;
559 subname = strrchr(fq_subname, ':');
565 Perl_croak(aTHX_ "next::method/next::can/maybe::next::method cannot find enclosing method");
568 subname_len = fq_subname_len - (subname - fq_subname);
569 if(memEQs(subname, subname_len, "__ANON__")) {
570 cxix = __dopoptosub_at(ccstack, cxix - 1);
578 /* If we made it to here, we found our context */
580 /* Initialize the next::method cache for this stash
582 selfmeta = HvMROMETA(selfstash);
583 if(!(nmcache = selfmeta->mro_nextmethod)) {
584 nmcache = selfmeta->mro_nextmethod = newHV();
586 else { /* Use the cached coderef if it exists */
587 HE* cache_entry = hv_fetch_ent(nmcache, sv, 0, 0);
589 SV* const val = HeVAL(cache_entry);
590 if(val == &PL_sv_undef) {
593 "No next::method '%" SVf "' found for %" HEKf,
594 SVfARG(newSVpvn_flags(subname, subname_len,
595 SVs_TEMP | ( subname_utf8 ? SVf_UTF8 : 0 ) )),
596 HEKfARG( HvNAME_HEK(selfstash) ));
599 mXPUSHs(newRV_inc(val));
604 /* beyond here is just for cache misses, so perf isn't as critical */
606 stashname_len = subname - fq_subname - 2;
607 stashname = newSVpvn_flags(fq_subname, stashname_len,
608 SVs_TEMP | (subname_utf8 ? SVf_UTF8 : 0));
610 /* has ourselves at the top of the list */
611 linear_av = S_mro_get_linear_isa_c3(aTHX_ selfstash, 0);
613 linear_svp = AvARRAY(linear_av);
614 entries = AvFILLp(linear_av) + 1;
616 /* Walk down our MRO, skipping everything up
617 to the contextually enclosing class */
619 SV * const linear_sv = *linear_svp++;
621 if(sv_eq(linear_sv, stashname))
625 /* Now search the remainder of the MRO for the
626 same method name as the contextually enclosing
630 SV * const linear_sv = *linear_svp++;
636 curstash = gv_stashsv(linear_sv, FALSE);
639 if (ckWARN(WARN_SYNTAX))
640 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
641 "Can't locate package %" SVf " for @%" HEKf "::ISA",
643 HEKfARG( HvNAME_HEK(selfstash) ));
649 gvp = (GV**)hv_fetch(curstash, subname,
650 subname_utf8 ? -(I32)subname_len : (I32)subname_len, 0);
656 if (SvTYPE(candidate) != SVt_PVGV)
657 gv_init_pvn(candidate, curstash, subname, subname_len,
658 GV_ADDMULTI|(subname_utf8 ? SVf_UTF8 : 0));
660 /* Notably, we only look for real entries, not method cache
661 entries, because in C3 the method cache of a parent is not
662 valid for the child */
663 if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
664 SvREFCNT_inc_simple_void_NN(MUTABLE_SV(cand_cv));
665 (void)hv_store_ent(nmcache, sv, MUTABLE_SV(cand_cv), 0);
666 mXPUSHs(newRV_inc(MUTABLE_SV(cand_cv)));
672 (void)hv_store_ent(nmcache, sv, &PL_sv_undef, 0);
674 Perl_croak(aTHX_ "No next::method '%" SVf "' found for %" HEKf,
675 SVfARG(newSVpvn_flags(subname, subname_len,
676 SVs_TEMP | ( subname_utf8 ? SVf_UTF8 : 0 ) )),
677 HEKfARG( HvNAME_HEK(selfstash) ));
681 Perl_mro_register(aTHX_ &c3_alg);