3 * Copyright (c) 2007 Brandon L Black
4 * Copyright (c) 2007, 2008, 2009, 2010, 2011 Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
9 * This was 'mro.c', but changed because there is another mro.c in /ext, and
10 * the os390 loader can't cope with this situation (which involves the two
11 * files calling functions defined in the other)
15 * 'Which order shall we go in?' said Frodo. 'Eldest first, or quickest first?
16 * You'll be last either way, Master Peregrin.'
18 * [p.101 of _The Lord of the Rings_, I/iii: "A Conspiracy Unmasked"]
23 These functions are related to the method resolution order of perl classes
24 Also see L<perlmroapi>.
31 #define PERL_IN_MRO_CORE_C
34 static const struct mro_alg dfs_alg =
35 {S_mro_get_linear_isa_dfs, "dfs", 3, 0, 0};
38 Perl_mro_get_private_data(pTHX_ struct mro_meta *const smeta,
39 const struct mro_alg *const which)
42 PERL_ARGS_ASSERT_MRO_GET_PRIVATE_DATA;
44 data = (SV **)Perl_hv_common(aTHX_ smeta->mro_linear_all, NULL,
45 which->name, which->length, which->kflags,
46 HV_FETCH_JUST_SV, NULL, which->hash);
50 /* If we've been asked to look up the private data for the current MRO, then
52 if (smeta->mro_which == which)
53 smeta->mro_linear_current = *data;
59 Perl_mro_set_private_data(pTHX_ struct mro_meta *const smeta,
60 const struct mro_alg *const which, SV *const data)
62 PERL_ARGS_ASSERT_MRO_SET_PRIVATE_DATA;
64 if (!smeta->mro_linear_all) {
65 if (smeta->mro_which == which) {
66 /* If all we need to store is the current MRO's data, then don't use
67 memory on a hash with 1 element - store it direct, and signal
68 this by leaving the would-be-hash NULL. */
69 smeta->mro_linear_current = data;
72 HV *const hv = newHV();
73 /* Start with 2 buckets. It's unlikely we'll need more. */
75 smeta->mro_linear_all = hv;
77 if (smeta->mro_linear_current) {
78 /* If we were storing something directly, put it in the hash
80 Perl_mro_set_private_data(aTHX_ smeta, smeta->mro_which,
81 smeta->mro_linear_current);
86 /* We get here if we're storing more than one linearisation for this stash,
87 or the linearisation we are storing is not that if its current MRO. */
89 if (smeta->mro_which == which) {
90 /* If we've been asked to store the private data for the current MRO,
92 smeta->mro_linear_current = data;
95 if (!Perl_hv_common(aTHX_ smeta->mro_linear_all, NULL,
96 which->name, which->length, which->kflags,
97 HV_FETCH_ISSTORE, data, which->hash)) {
98 croak("panic: hv_store() failed in set_mro_private_data() "
99 "for '%.*s' %d", (int) which->length, which->name,
107 =for apidoc mro_get_from_name
109 Returns the previously registered mro with the given C<name>, or NULL if not
110 registered. See L</C<mro_register>>.
115 const struct mro_alg *
116 Perl_mro_get_from_name(pTHX_ SV *name) {
119 PERL_ARGS_ASSERT_MRO_GET_FROM_NAME;
121 data = (SV **)Perl_hv_common(aTHX_ PL_registered_mros, name, NULL, 0, 0,
122 HV_FETCH_JUST_SV, NULL, 0);
125 assert(SvTYPE(*data) == SVt_IV);
126 assert(SvIOK(*data));
127 return INT2PTR(const struct mro_alg *, SvUVX(*data));
131 =for apidoc mro_register
132 Registers a custom mro plugin. See L<perlmroapi> for details on this and other
139 Perl_mro_register(pTHX_ const struct mro_alg *mro) {
140 SV *wrapper = newSVuv(PTR2UV(mro));
142 PERL_ARGS_ASSERT_MRO_REGISTER;
145 if (!Perl_hv_common(aTHX_ PL_registered_mros, NULL,
146 mro->name, mro->length, mro->kflags,
147 HV_FETCH_ISSTORE, wrapper, mro->hash)) {
148 SvREFCNT_dec_NN(wrapper);
149 croak("panic: hv_store() failed in mro_register() "
150 "for '%.*s' %d", (int) mro->length, mro->name, mro->kflags);
155 Perl_mro_meta_init(pTHX_ HV* stash)
157 struct mro_meta* newmeta;
159 PERL_ARGS_ASSERT_MRO_META_INIT;
161 assert(HvAUX(stash));
162 assert(!(HvAUX(stash)->xhv_mro_meta));
163 Newxz(newmeta, 1, struct mro_meta);
164 HvAUX(stash)->xhv_mro_meta = newmeta;
165 newmeta->cache_gen = 1;
166 newmeta->pkg_gen = 1;
167 newmeta->mro_which = &dfs_alg;
172 #if defined(USE_ITHREADS)
174 /* for sv_dup on new threads */
176 Perl_mro_meta_dup(pTHX_ struct mro_meta* smeta, CLONE_PARAMS* param)
178 struct mro_meta* newmeta;
180 PERL_ARGS_ASSERT_MRO_META_DUP;
182 Newx(newmeta, 1, struct mro_meta);
183 Copy(smeta, newmeta, 1, struct mro_meta);
185 if (newmeta->mro_linear_all) {
186 newmeta->mro_linear_all
187 = MUTABLE_HV(sv_dup_inc((const SV *)newmeta->mro_linear_all, param));
188 /* This is just acting as a shortcut pointer, and will be automatically
189 updated on the first get. */
190 newmeta->mro_linear_current = NULL;
191 } else if (newmeta->mro_linear_current) {
192 /* Only the current MRO is stored, so this owns the data. */
193 newmeta->mro_linear_current
194 = sv_dup_inc((const SV *)newmeta->mro_linear_current, param);
197 if (newmeta->mro_nextmethod)
198 newmeta->mro_nextmethod
199 = MUTABLE_HV(sv_dup_inc((const SV *)newmeta->mro_nextmethod, param));
202 = MUTABLE_HV(sv_dup_inc((const SV *)newmeta->isa, param));
204 newmeta->super = NULL;
206 /* clear the destructor cache */
207 newmeta->destroy = NULL;
208 newmeta->destroy_gen = 0;
213 #endif /* USE_ITHREADS */
216 =for apidoc mro_get_linear_isa_dfs
218 Returns the Depth-First Search linearization of C<@ISA>
219 the given stash. The return value is a read-only AV*
220 whose elements are string SVs giving class names.
221 C<level> should be 0 (it is used internally in this
222 function's recursion).
224 You are responsible for C<SvREFCNT_inc()> on the
225 return value if you plan to store it anywhere
226 semi-permanently (otherwise it might be deleted
227 out from under you the next time the cache is
233 S_mro_get_linear_isa_dfs(pTHX_ HV *stash, U32 level)
240 struct mro_meta* meta;
244 PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA_DFS;
245 assert(HvAUX(stash));
248 = HvAUX(stash)->xhv_name_u.xhvnameu_name && HvENAME_HEK_NN(stash)
249 ? HvENAME_HEK_NN(stash)
253 croak("Can't linearize anonymous symbol table");
257 "Recursive inheritance detected in package '%" HEKf "'",
260 meta = HvMROMETA(stash);
262 /* return cache if valid */
263 if((retval = MUTABLE_AV(MRO_GET_PRIVATE_DATA(meta, &dfs_alg)))) {
267 /* not in cache, make a new one */
269 retval = newAV_mortal();
270 /* We use this later in this function, but don't need a reference to it
271 beyond the end of this function, so reference count is fine. */
272 our_name = newSVhek(stashhek);
273 av_push_simple(retval, our_name); /* add ourselves at the top */
276 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
277 av = (gvp && (gv = *gvp) && isGV_with_GP(gv)) ? GvAV(gv) : NULL;
279 /* "stored" is used to keep track of all of the classnames we have added to
280 the MRO so far, so we can do a quick exists check and avoid adding
281 duplicate classnames to the MRO as we go.
282 It's then retained to be re-used as a fast lookup for ->isa(), by adding
283 our own name and "UNIVERSAL" to it. */
285 if(av && AvFILLp(av) >= 0) {
287 SV **svp = AvARRAY(av);
288 I32 items = AvFILLp(av) + 1;
292 SV* const sv = *svp ? *svp : &PL_sv_undef;
293 HV* const basestash = gv_stashsv(sv, 0);
299 /* if no stash exists for this @ISA member,
300 simply add it to the MRO and move on */
305 /* otherwise, recurse into ourselves for the MRO
306 of this @ISA member, and append their MRO to ours.
307 The recursive call could throw an exception, which
308 has memory management implications here, hence the use of
310 const AV *const subrv
311 = mro_get_linear_isa_dfs(basestash, level + 1);
313 subrv_p = AvARRAY(subrv);
314 subrv_items = AvFILLp(subrv) + 1;
317 while(subrv_items--) {
318 SV *const subsv = *subrv_p++;
319 /* LVALUE fetch will create a new undefined SV if necessary
321 HE *const he = hv_fetch_ent(stored, subsv, 1, 0);
323 if(HeVAL(he) != &PL_sv_undef) {
324 /* It was newly created. Steal it for our new SV, and
325 replace it in the hash with the "real" thing. */
326 SV *const val = HeVAL(he);
327 HEK *const key = HeKEY_hek(he);
329 HeVAL(he) = &PL_sv_undef;
331 av_push_simple(retval, val);
335 /* We are the first (or only) parent. We can short cut the
336 complexity above, because our @ISA is simply us prepended
337 to our parent's @ISA, and our ->isa cache is simply our
338 parent's, with our name added. */
339 /* newSVsv() is slow. This code is only faster if we can avoid
340 it by ensuring that SVs in the arrays are shared hash key
341 scalar SVs, because we can "copy" them very efficiently.
342 Although to be fair, we can't *ensure* this, as a reference
343 to the internal array is returned by mro::get_linear_isa(),
344 so we'll have to be defensive just in case someone faffed
348 stored = MUTABLE_HV(sv_2mortal((SV*)newHVhv(HvMROMETA(basestash)->isa)));
349 av_extend(retval, subrv_items);
350 AvFILLp(retval) = subrv_items;
351 svp = AvARRAY(retval);
352 while(subrv_items--) {
353 SV *const val = *subrv_p++;
354 *++svp = SvIsCOW_shared_hash(val)
355 ? newSVhek(SvSHARED_HEK_FROM_PV(SvPVX(val)))
359 /* They have no stash. So create ourselves an ->isa cache
360 as if we'd copied it from what theirs should be. */
361 stored = MUTABLE_HV(newSV_type_mortal(SVt_PVHV));
362 (void) hv_stores(stored, "UNIVERSAL", &PL_sv_undef);
363 av_push_simple(retval,
364 newSVhek(HeKEY_hek(hv_store_ent(stored, sv,
370 /* We have no parents. */
371 stored = MUTABLE_HV(newSV_type_mortal(SVt_PVHV));
372 (void) hv_stores(stored, "UNIVERSAL", &PL_sv_undef);
375 (void) hv_store_ent(stored, our_name, &PL_sv_undef, 0);
377 SvREFCNT_inc_simple_void_NN(stored);
379 SvREADONLY_on(stored);
383 /* now that we're past the exception dangers, grab our own reference to
384 the AV we're about to use for the result. The reference owned by the
385 mortals' stack will be released soon, so everything will balance. */
386 SvREFCNT_inc_simple_void_NN(retval);
389 /* we don't want anyone modifying the cache entry but us,
390 and we do so by replacing it completely */
391 SvREADONLY_on(retval);
393 return MUTABLE_AV(Perl_mro_set_private_data(aTHX_ meta, &dfs_alg,
394 MUTABLE_SV(retval)));
398 =for apidoc mro_get_linear_isa
400 Returns the mro linearisation for the given stash. By default, this
401 will be whatever C<mro_get_linear_isa_dfs> returns unless some
402 other MRO is in effect for the stash. The return value is a
403 read-only AV* whose values are string SVs giving class names.
405 You are responsible for C<SvREFCNT_inc()> on the
406 return value if you plan to store it anywhere
407 semi-permanently (otherwise it might be deleted
408 out from under you the next time the cache is
414 Perl_mro_get_linear_isa(pTHX_ HV *stash)
416 struct mro_meta* meta;
419 PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA;
421 croak("Can't linearize anonymous symbol table");
423 meta = HvMROMETA(stash);
424 if (!meta->mro_which)
425 croak("panic: invalid MRO!");
426 isa = meta->mro_which->resolve(aTHX_ stash, 0);
428 if (meta->mro_which != &dfs_alg) { /* skip for dfs, for speed */
430 (HvHasENAME_HEK(stash) || HvHasNAME(stash))
431 ? newSVhek(HvHasENAME_HEK(stash)
436 if(namesv && (AvFILLp(isa) == -1 || !sv_eq(*AvARRAY(isa), namesv)))
438 AV * const old = isa;
440 SV **ovp = AvARRAY(old);
441 SV * const * const oend = ovp + AvFILLp(old) + 1;
442 isa = (AV *)newSV_type_mortal(SVt_PVAV);
443 av_extend(isa, AvFILLp(isa) = AvFILLp(old)+1);
444 *AvARRAY(isa) = namesv;
445 svp = AvARRAY(isa)+1;
446 while (ovp < oend) *svp++ = SvREFCNT_inc(*ovp++);
448 else SvREFCNT_dec(namesv);
452 HV *const isa_hash = newHV();
453 /* Linearisation didn't build it for us, so do it here. */
454 I32 count = AvFILLp(isa) + 1;
455 SV *const *svp = AvARRAY(isa);
456 SV *const *const svp_end = svp + count;
457 const HEK *canon_name = HvENAME_HEK(stash);
458 if (!canon_name) canon_name = HvNAME_HEK(stash);
460 if (count > PERL_HASH_DEFAULT_HvMAX) {
461 hv_ksplit(isa_hash, count);
464 while (svp < svp_end) {
465 (void) hv_store_ent(isa_hash, *svp++, &PL_sv_undef, 0);
468 (void) hv_common(isa_hash, NULL, HEK_KEY(canon_name),
469 HEK_LEN(canon_name), HEK_FLAGS(canon_name),
470 HV_FETCH_ISSTORE, &PL_sv_undef,
471 HEK_HASH(canon_name));
472 (void) hv_stores(isa_hash, "UNIVERSAL", &PL_sv_undef);
474 SvREADONLY_on(isa_hash);
476 meta->isa = isa_hash;
483 =for apidoc mro_isa_changed_in
485 Takes the necessary steps (cache invalidations, mostly)
486 when the C<@ISA> of the given package has changed. Invoked
487 by the C<setisa> magic, should not need to invoke directly.
492 /* Macro to avoid repeating the code five times. */
493 #define CLEAR_LINEAR(mEta) \
494 if (mEta->mro_linear_all) { \
495 SvREFCNT_dec(MUTABLE_SV(mEta->mro_linear_all)); \
496 mEta->mro_linear_all = NULL; \
497 /* This is just acting as a shortcut pointer. */ \
498 mEta->mro_linear_current = NULL; \
499 } else if (mEta->mro_linear_current) { \
500 /* Only the current MRO is stored, so this owns the data. */ \
501 SvREFCNT_dec(mEta->mro_linear_current); \
502 mEta->mro_linear_current = NULL; \
506 Perl_mro_isa_changed_in(pTHX_ HV* stash)
514 struct mro_meta * meta;
517 const HEK * const stashhek = HvENAME_HEK(stash);
518 const char * const stashname = HvENAME_get(stash);
519 const STRLEN stashname_len = HvENAMELEN_get(stash);
521 PERL_ARGS_ASSERT_MRO_ISA_CHANGED_IN;
524 croak("Can't call mro_isa_changed_in() on anonymous symbol table");
527 /* wipe out the cached linearizations for this stash */
528 meta = HvMROMETA(stash);
531 /* Steal it for our own purposes. */
532 isa = (HV *)sv_2mortal((SV *)meta->isa);
536 /* Inc the package generation, since our @ISA changed */
539 /* Wipe the global method cache if this package
540 is UNIVERSAL or one of its parents */
542 svp = hv_fetchhek(PL_isarev, stashhek, 0);
543 isarev = svp ? MUTABLE_HV(*svp) : NULL;
545 if((memEQs(stashname, stashname_len, "UNIVERSAL"))
546 || (isarev && hv_existss(isarev, "UNIVERSAL"))) {
550 else { /* Wipe the local method cache otherwise */
552 is_universal = FALSE;
555 /* wipe next::method cache too */
556 if(meta->mro_nextmethod) hv_clear(meta->mro_nextmethod);
558 /* Changes to @ISA might turn overloading on */
560 /* pessimise derefs for now. Will get recalculated by Gv_AMupdate() */
561 HvAUX(stash)->xhv_aux_flags &= ~HvAUXf_NO_DEREF;
563 /* DESTROY can be cached in meta. */
564 meta->destroy_gen = 0;
566 /* Iterate the isarev (classes that are our children),
567 wiping out their linearization, method and isa caches
568 and upating PL_isarev. */
570 HV *isa_hashes = NULL;
572 /* We have to iterate through isarev twice to avoid a chicken and
573 * egg problem: if A inherits from B and both are in isarev, A might
574 * be processed before B and use B's previous linearisation.
577 /* First iteration: Wipe everything, but stash away the isa hashes
578 * since we still need them for updating PL_isarev.
581 if(hv_iterinit(isarev)) {
582 /* Only create the hash if we need it; i.e., if isarev has
584 isa_hashes = (HV *)newSV_type_mortal(SVt_PVHV);
586 while((iter = hv_iternext(isarev))) {
587 HV* revstash = gv_stashsv(hv_iterkeysv(iter), 0);
588 struct mro_meta* revmeta;
590 if(!revstash) continue;
591 revmeta = HvMROMETA(revstash);
592 CLEAR_LINEAR(revmeta);
594 revmeta->cache_gen++;
595 if(revmeta->mro_nextmethod)
596 hv_clear(revmeta->mro_nextmethod);
597 if (!SvOBJECT(revstash)) SvSTASH(revstash) = NULL;
601 isa_hashes, (const char*)&revstash, sizeof(HV *),
602 revmeta->isa ? (SV *)revmeta->isa : &PL_sv_undef, 0
607 /* Second pass: Update PL_isarev. We can just use isa_hashes to
608 * avoid another round of stash lookups. */
610 /* isarev might be deleted from PL_isarev during this loop, so hang
612 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)isarev));
615 hv_iterinit(isa_hashes);
616 while((iter = hv_iternext(isa_hashes))) {
617 HV* const revstash = *(HV **)HEK_KEY(HeKEY_hek(iter));
618 HV * const isa = (HV *)HeVAL(iter);
621 /* We're starting at the 2nd element, skipping revstash */
622 linear_mro = mro_get_linear_isa(revstash);
623 svp = AvARRAY(linear_mro) + 1;
624 items = AvFILLp(linear_mro);
626 namehek = HvENAME_HEK(revstash);
627 if (!namehek) namehek = HvNAME_HEK(revstash);
630 SV* const sv = *svp++;
633 HE *he = hv_fetch_ent(PL_isarev, sv, TRUE, 0);
635 /* That fetch should not fail. But if it had to create
636 a new SV for us, then will need to upgrade it to an
637 HV (which sv_upgrade() can now do for us). */
639 mroisarev = MUTABLE_HV(HeVAL(he));
641 SvUPGRADE(MUTABLE_SV(mroisarev), SVt_PVHV);
643 /* This hash only ever contains PL_sv_yes. Storing it
644 over itself is almost as cheap as calling hv_exists,
645 so on aggregate we expect to save time by not making
646 two calls to the common HV code for the case where
650 hv_storehek(mroisarev, namehek, &PL_sv_yes);
653 if ((SV *)isa != &PL_sv_undef && HvTOTALKEYS(isa)) {
656 isa, HEK_KEY(namehek), HEK_LEN(namehek),
657 HvMROMETA(revstash)->isa, HEK_HASH(namehek),
665 /* Now iterate our MRO (parents), adding ourselves and everything from
666 our isarev to their isarev.
669 /* We're starting at the 2nd element, skipping ourselves here */
670 linear_mro = mro_get_linear_isa(stash);
671 svp = AvARRAY(linear_mro) + 1;
672 items = AvFILLp(linear_mro);
675 SV* const sv = *svp++;
678 HE *he = hv_fetch_ent(PL_isarev, sv, TRUE, 0);
680 /* That fetch should not fail. But if it had to create a new SV for
681 us, then will need to upgrade it to an HV (which sv_upgrade() can
684 mroisarev = MUTABLE_HV(HeVAL(he));
686 SvUPGRADE(MUTABLE_SV(mroisarev), SVt_PVHV);
688 /* This hash only ever contains PL_sv_yes. Storing it over itself is
689 almost as cheap as calling hv_exists, so on aggregate we expect to
690 save time by not making two calls to the common HV code for the
691 case where it doesn't exist. */
693 (void)hv_storehek(mroisarev, stashhek, &PL_sv_yes);
696 /* Delete our name from our former parents' isarevs. */
697 if(isa && HvTOTALKEYS(isa))
698 mro_clean_isarev(isa, stashname, stashname_len, meta->isa,
699 HEK_HASH(stashhek), HEK_UTF8(stashhek));
702 /* Deletes name from all the isarev entries listed in isa.
703 Don't call this if isa is already empty. */
705 S_mro_clean_isarev(pTHX_ HV * const isa, const char * const name,
706 const STRLEN len, HV * const exceptions, U32 hash,
711 PERL_ARGS_ASSERT_MRO_CLEAN_ISAREV;
713 assert(HvTOTALKEYS(isa));
714 /* Delete our name from our former parents' isarevs. */
717 while((iter = hv_iternext(isa))) {
719 HEK *key = HeKEY_hek(iter);
720 if(exceptions && hv_existshek(exceptions, key))
722 svp = hv_fetchhek(PL_isarev, key, 0);
724 HV * const isarev = (HV *)*svp;
725 (void)hv_common(isarev, NULL, name, len, flags,
726 G_DISCARD|HV_DELETE, NULL, hash);
727 if(!HvTOTALKEYS(isarev))
728 (void)hv_deletehek(PL_isarev, key, G_DISCARD);
734 =for apidoc mro_package_moved
736 Call this function to signal to a stash that it has been assigned to
737 another spot in the stash hierarchy. C<stash> is the stash that has been
738 assigned. C<oldstash> is the stash it replaces, if any. C<gv> is the glob
739 that is actually being assigned to.
741 This can also be called with a null first argument to
742 indicate that C<oldstash> has been deleted.
744 This function invalidates isa caches on the old stash, on all subpackages
745 nested inside it, and on the subclasses of all those, including
746 non-existent packages that have corresponding entries in C<stash>.
748 It also sets the effective names (C<HvENAME>) on all the stashes as
751 If the C<gv> is present and is not in the symbol table, then this function
752 simply returns. This checked will be skipped if C<flags & 1>.
757 Perl_mro_package_moved(pTHX_ HV * const stash, HV * const oldstash,
758 const GV * const gv, U32 flags)
766 PERL_ARGS_ASSERT_MRO_PACKAGE_MOVED;
767 assert(stash || oldstash);
769 /* Determine the name(s) of the location that stash was assigned to
770 * or from which oldstash was removed.
772 * We cannot reliably use the name in oldstash, because it may have
773 * been deleted from the location in the symbol table that its name
774 * suggests, as in this case:
776 * $globref = \*foo::bar::;
777 * Symbol::delete_package("foo");
778 * *$globref = \%baz::;
779 * *$globref = *frelp::;
780 * # calls mro_package_moved(%frelp::, %baz::, *$globref, NULL, 0)
782 * So we get it from the gv. But, since the gv may no longer be in the
783 * symbol table, we check that first. The only reliable way to tell is
784 * to see whether its stash has an effective name and whether the gv
785 * resides in that stash under its name. That effective name may be
786 * different from what gv_fullname4 would use.
787 * If flags & 1, the caller has asked us to skip the check.
792 !GvSTASH(gv) || !HvHasENAME(GvSTASH(gv)) ||
793 !(svp = hv_fetchhek(GvSTASH(gv), GvNAME_HEK(gv), 0)) ||
797 assert(HvHasAUX(GvSTASH(gv)));
798 assert(GvNAMELEN(gv));
799 assert(GvNAME(gv)[GvNAMELEN(gv) - 1] == ':');
800 assert(GvNAMELEN(gv) == 1 || GvNAME(gv)[GvNAMELEN(gv) - 2] == ':');
801 name_count = HvAUX(GvSTASH(gv))->xhv_name_count;
804 namep = &HvAUX(GvSTASH(gv))->xhv_name_u.xhvnameu_name;
807 namep = HvAUX(GvSTASH(gv))->xhv_name_u.xhvnameu_names;
808 if (name_count < 0) ++namep, name_count = -name_count - 1;
810 if (name_count == 1) {
811 if (memEQs(HEK_KEY(*namep), HEK_LEN(*namep), "main")) {
812 namesv = GvNAMELEN(gv) == 1
813 ? newSVpvs_flags(":", SVs_TEMP)
814 : newSVpvs_flags("", SVs_TEMP);
817 namesv = newSVhek_mortal(*namep);
818 if (GvNAMELEN(gv) == 1) sv_catpvs(namesv, ":");
819 else sv_catpvs(namesv, "::");
821 if (GvNAMELEN(gv) != 1) {
823 namesv, GvNAME(gv), GvNAMELEN(gv) - 2,
824 /* skip trailing :: */
825 GvNAMEUTF8(gv) ? SV_CATUTF8 : SV_CATBYTES
831 namesv = newSV_type_mortal(SVt_PVAV);
832 while (name_count--) {
833 if(memEQs(HEK_KEY(*namep), HEK_LEN(*namep), "main")){
834 aname = GvNAMELEN(gv) == 1
840 aname = newSVhek(*namep++);
841 if (GvNAMELEN(gv) == 1) sv_catpvs(aname, ":");
842 else sv_catpvs(aname, "::");
844 if (GvNAMELEN(gv) != 1) {
846 aname, GvNAME(gv), GvNAMELEN(gv) - 2,
847 /* skip trailing :: */
848 GvNAMEUTF8(gv) ? SV_CATUTF8 : SV_CATBYTES
851 av_push_simple((AV *)namesv, aname);
855 /* Get a list of all the affected classes. */
856 /* We cannot simply pass them all to mro_isa_changed_in to avoid
857 the list, as that function assumes that only one package has
858 changed. It does not work with:
860 @foo::ISA = qw( B B::B );
861 *B:: = delete $::{"A::"};
863 as neither B nor B::B can be updated before the other, since they
864 will reset caches on foo, which will see either B or B::B with the
865 wrong name. The names must be set on *all* affected stashes before
866 we do anything else. (And linearisations must be cleared, too.)
868 stashes = (HV *) newSV_type_mortal(SVt_PVHV);
869 mro_gather_and_rename(
870 stashes, (HV *) newSV_type_mortal(SVt_PVHV),
871 stash, oldstash, namesv
874 /* Once the caches have been wiped on all the classes, call
875 mro_isa_changed_in on each. */
876 hv_iterinit(stashes);
877 while((iter = hv_iternext(stashes))) {
878 HV * const this_stash = *(HV **)HEK_KEY(HeKEY_hek(iter));
879 if(HvENAME(this_stash)) {
880 /* We have to restore the original meta->isa (that
881 mro_gather_and_rename set aside for us) this way, in case
882 one class in this list is a superclass of a another class
883 that we have already encountered. In such a case, meta->isa
884 will have been overwritten without old entries being deleted
886 struct mro_meta * const meta = HvMROMETA(this_stash);
887 if(meta->isa != (HV *)HeVAL(iter)){
888 SvREFCNT_dec(meta->isa);
890 = HeVAL(iter) == &PL_sv_yes
893 HeVAL(iter) = NULL; /* We donated our reference count. */
895 mro_isa_changed_in(this_stash);
901 S_mro_gather_and_rename(pTHX_ HV * const stashes, HV * const seen_stashes,
902 HV *stash, HV *oldstash, SV *namesv)
908 const bool stash_had_name = stash && HvHasENAME(stash);
909 bool fetched_isarev = FALSE;
914 PERL_ARGS_ASSERT_MRO_GATHER_AND_RENAME;
916 /* We use the seen_stashes hash to keep track of which packages have
917 been encountered so far. This must be separate from the main list of
918 stashes, as we need to distinguish between stashes being assigned
919 and stashes being replaced/deleted. (A nested stash can be on both
920 sides of an assignment. We cannot simply skip iterating through a
921 stash on the right if we have seen it on the left, as it will not
922 get its ename assigned to it.)
924 To avoid allocating extra SVs, instead of a bitfield we can make
925 bizarre use of immortals:
927 &PL_sv_undef: seen on the left (oldstash)
928 &PL_sv_no : seen on the right (stash)
929 &PL_sv_yes : seen on both sides
934 /* Add to the big list. */
935 struct mro_meta * meta;
939 seen_stashes, NULL, (const char *)&oldstash, sizeof(HV *), 0,
940 HV_FETCH_LVALUE|HV_FETCH_EMPTY_HE, NULL, 0
942 if(HeVAL(entry) == &PL_sv_undef || HeVAL(entry) == &PL_sv_yes) {
947 = HeVAL(entry) == &PL_sv_no ? &PL_sv_yes : &PL_sv_undef;
948 meta = HvMROMETA(oldstash);
951 stashes, (const char *)&oldstash, sizeof(HV *),
953 ? SvREFCNT_inc_simple_NN((SV *)meta->isa)
959 /* Update the effective name. */
960 if(HvENAME_get(oldstash)) {
961 const HEK * const enamehek = HvENAME_HEK(oldstash);
962 if(SvTYPE(namesv) == SVt_PVAV) {
963 items = AvFILLp((AV *)namesv) + 1;
964 svp = AvARRAY((AV *)namesv);
971 const U32 name_utf8 = SvUTF8(*svp);
973 const char *name = SvPVx_const(*svp, len);
975 DEBUG_o(Perl_deb(aTHX_ "mro_gather_and_rename clearing PL_stashcache for '%" SVf "'\n",
977 (void)hv_delete_ent(PL_stashcache, *svp, G_DISCARD, 0);
979 hv_ename_delete(oldstash, name, len, name_utf8);
981 if (!fetched_isarev) {
982 /* If the name deletion caused a name change, then we
983 * are not going to call mro_isa_changed_in with this
984 * name (and not at all if it has become anonymous) so
985 * we need to delete old isarev entries here, both
986 * those in the superclasses and this class's own list
987 * of subclasses. We simply delete the latter from
988 * PL_isarev, since we still need it. hv_delete morti-
989 * fies it for us, so sv_2mortal is not necessary. */
990 if(HvENAME_HEK(oldstash) != enamehek) {
991 if(meta->isa && HvTOTALKEYS(meta->isa))
992 mro_clean_isarev(meta->isa, name, len, 0, 0,
993 name_utf8 ? HVhek_UTF8 : 0);
994 isarev = (HV *)hv_delete_ent(PL_isarev, *svp, 0, 0);
1005 if(SvTYPE(namesv) == SVt_PVAV) {
1006 items = AvFILLp((AV *)namesv) + 1;
1007 svp = AvARRAY((AV *)namesv);
1014 const U32 name_utf8 = SvUTF8(*svp);
1016 const char *name = SvPVx_const(*svp++, len);
1017 hv_ename_add(stash, name, len, name_utf8);
1020 /* Add it to the big list if it needs
1021 * mro_isa_changed_in called on it. That happens if it was
1022 * detached from the symbol table (so it had no HvENAME) before
1023 * being assigned to the spot named by the 'name' variable, because
1024 * its cached isa linearisation is now stale (the effective name
1025 * having changed), and subclasses will then use that cache when
1026 * mro_package_moved calls mro_isa_changed_in. (See
1029 * If it did have a name, then its previous name is still
1030 * used in isa caches, and there is no need for
1031 * mro_package_moved to call mro_isa_changed_in.
1037 seen_stashes, NULL, (const char *)&stash, sizeof(HV *), 0,
1038 HV_FETCH_LVALUE|HV_FETCH_EMPTY_HE, NULL, 0
1040 if(HeVAL(entry) == &PL_sv_yes || HeVAL(entry) == &PL_sv_no)
1044 = HeVAL(entry) == &PL_sv_undef ? &PL_sv_yes : &PL_sv_no;
1047 struct mro_meta * const meta = HvMROMETA(stash);
1050 stashes, (const char *)&stash, sizeof(HV *),
1052 ? SvREFCNT_inc_simple_NN((SV *)meta->isa)
1061 if(!stash && !oldstash)
1062 /* Both stashes have been encountered already. */
1065 /* Add all the subclasses to the big list. */
1066 if(!fetched_isarev) {
1067 /* If oldstash is not null, then we can use its HvENAME to look up
1068 the isarev hash, since all its subclasses will be listed there.
1069 It will always have an HvENAME. It the HvENAME was removed
1070 above, then fetch_isarev will be true, and this code will not be
1073 If oldstash is null, then this is an empty spot with no stash in
1074 it, so subclasses could be listed in isarev hashes belonging to
1075 any of the names, so we have to check all of them.
1077 assert(!oldstash || HvENAME(oldstash));
1079 /* Extra variable to avoid a compiler warning */
1080 const HEK * const hvename = HvENAME_HEK(oldstash);
1081 fetched_isarev = TRUE;
1082 svp = hv_fetchhek(PL_isarev, hvename, 0);
1083 if (svp) isarev = MUTABLE_HV(*svp);
1085 else if(SvTYPE(namesv) == SVt_PVAV) {
1086 items = AvFILLp((AV *)namesv) + 1;
1087 svp = AvARRAY((AV *)namesv);
1095 isarev || !fetched_isarev
1097 while (fetched_isarev || items--) {
1100 if (!fetched_isarev) {
1101 HE * const he = hv_fetch_ent(PL_isarev, *svp++, 0, 0);
1102 if (!he || !(isarev = MUTABLE_HV(HeVAL(he)))) continue;
1105 hv_iterinit(isarev);
1106 while((iter = hv_iternext(isarev))) {
1107 HV* revstash = gv_stashsv(hv_iterkeysv(iter), 0);
1108 struct mro_meta * meta;
1110 if(!revstash) continue;
1111 meta = HvMROMETA(revstash);
1114 stashes, (const char *)&revstash, sizeof(HV *),
1116 ? SvREFCNT_inc_simple_NN((SV *)meta->isa)
1123 if (fetched_isarev) break;
1127 /* This is partly based on code in hv_iternext_flags. We are not call-
1128 ing that here, as we want to avoid resetting the hash iterator. */
1130 /* Skip the entire loop if the hash is empty. */
1131 if(oldstash && HvTOTALKEYS(oldstash)) {
1132 xhv = (XPVHV*)SvANY(oldstash);
1133 seen = (HV *) newSV_type_mortal(SVt_PVHV);
1135 /* Iterate through entries in the oldstash, adding them to the
1136 list, meanwhile doing the equivalent of $seen{$key} = 1.
1139 while (++riter <= (I32)xhv->xhv_max) {
1140 entry = (HvARRAY(oldstash))[riter];
1142 /* Iterate through the entries in this list */
1143 for(; entry; entry = HeNEXT(entry)) {
1147 /* If this entry is not a glob, ignore it.
1149 if (!isGV(HeVAL(entry))) continue;
1151 key = hv_iterkey(entry, &len);
1152 if ((len > 1 && key[len-2] == ':' && key[len-1] == ':')
1153 || (len == 1 && key[0] == ':')) {
1154 HV * const oldsubstash = GvHV(HeVAL(entry));
1156 HV *substash = NULL;
1158 /* Avoid main::main::main::... */
1159 if(oldsubstash == oldstash) continue;
1161 stashentry = stash ? hv_fetchhek(stash, HeKEY_hek(entry), 0) : NULL;
1165 stashentry && *stashentry && isGV(*stashentry)
1166 && (substash = GvHV(*stashentry))
1168 || (oldsubstash && HvHasENAME(oldsubstash))
1171 /* Add :: and the key (minus the trailing ::)
1174 if(SvTYPE(namesv) == SVt_PVAV) {
1176 items = AvFILLp((AV *)namesv) + 1;
1177 svp = AvARRAY((AV *)namesv);
1178 subname = newSV_type_mortal(SVt_PVAV);
1180 aname = newSVsv(*svp++);
1182 sv_catpvs(aname, ":");
1184 sv_catpvs(aname, "::");
1188 ? SV_CATUTF8 : SV_CATBYTES
1191 av_push_simple((AV *)subname, aname);
1195 subname = sv_mortalcopy_flags(namesv, SV_GMAGIC|SV_NOSTEAL);
1196 if (len == 1) sv_catpvs(subname, ":");
1198 sv_catpvs(subname, "::");
1200 subname, key, len-2,
1201 HeUTF8(entry) ? SV_CATUTF8 : SV_CATBYTES
1205 mro_gather_and_rename(
1206 stashes, seen_stashes,
1207 substash, oldsubstash, subname
1211 (void)hv_storehek(seen, HeKEY_hek(entry), &PL_sv_yes);
1217 /* Skip the entire loop if the hash is empty. */
1218 if (stash && HvTOTALKEYS(stash)) {
1219 xhv = (XPVHV*)SvANY(stash);
1222 /* Iterate through the new stash, skipping $seen{$key} items,
1223 calling mro_gather_and_rename(stashes,seen,entry,NULL, ...). */
1224 while (++riter <= (I32)xhv->xhv_max) {
1225 entry = (HvARRAY(stash))[riter];
1227 /* Iterate through the entries in this list */
1228 for(; entry; entry = HeNEXT(entry)) {
1232 /* If this entry is not a glob, ignore it.
1234 if (!isGV(HeVAL(entry))) continue;
1236 key = hv_iterkey(entry, &len);
1237 if ((len > 1 && key[len-2] == ':' && key[len-1] == ':')
1238 || (len == 1 && key[0] == ':')) {
1241 /* If this entry was seen when we iterated through the
1242 oldstash, skip it. */
1243 if(seen && hv_existshek(seen, HeKEY_hek(entry))) continue;
1245 /* We get here only if this stash has no corresponding
1246 entry in the stash being replaced. */
1248 substash = GvHV(HeVAL(entry));
1252 /* Avoid checking main::main::main::... */
1253 if(substash == stash) continue;
1255 /* Add :: and the key (minus the trailing ::)
1257 if(SvTYPE(namesv) == SVt_PVAV) {
1259 items = AvFILLp((AV *)namesv) + 1;
1260 svp = AvARRAY((AV *)namesv);
1261 subname = newSV_type_mortal(SVt_PVAV);
1263 aname = newSVsv(*svp++);
1265 sv_catpvs(aname, ":");
1267 sv_catpvs(aname, "::");
1271 ? SV_CATUTF8 : SV_CATBYTES
1274 av_push_simple((AV *)subname, aname);
1278 subname = sv_mortalcopy_flags(namesv, SV_GMAGIC|SV_NOSTEAL);
1279 if (len == 1) sv_catpvs(subname, ":");
1281 sv_catpvs(subname, "::");
1283 subname, key, len-2,
1284 HeUTF8(entry) ? SV_CATUTF8 : SV_CATBYTES
1288 mro_gather_and_rename(
1289 stashes, seen_stashes,
1290 substash, NULL, subname
1300 =for apidoc mro_method_changed_in
1302 Invalidates method caching on any child classes
1303 of the given stash, so that they might notice
1304 the changes in this one.
1306 Ideally, all instances of C<PL_sub_generation++> in
1307 perl source outside of F<mro.c> should be
1308 replaced by calls to this.
1310 Perl automatically handles most of the common
1311 ways a method might be redefined. However, there
1312 are a few ways you could change a method in a stash
1313 without the cache code noticing, in which case you
1314 need to call this method afterwards:
1316 1) Directly manipulating the stash HV entries from
1319 2) Assigning a reference to a readonly scalar
1320 constant into a stash entry in order to create
1321 a constant subroutine (like F<constant.pm>
1324 This same method is available from pure perl
1325 via, C<mro::method_changed_in(classname)>.
1330 Perl_mro_method_changed_in(pTHX_ HV *stash)
1332 PERL_ARGS_ASSERT_MRO_METHOD_CHANGED_IN;
1334 const char * const stashname = HvENAME_get(stash);
1337 croak("Can't call mro_method_changed_in() on anonymous symbol table");
1339 const STRLEN stashname_len = HvENAMELEN_get(stash);
1341 SV ** const svp = hv_fetchhek(PL_isarev, HvENAME_HEK_NN(stash), 0);
1342 HV * const isarev = svp ? MUTABLE_HV(*svp) : NULL;
1344 /* Inc the package generation, since a local method changed */
1345 HvMROMETA(stash)->pkg_gen++;
1347 /* DESTROY can be cached in meta */
1348 HvMROMETA(stash)->destroy_gen = 0;
1350 /* If stash is UNIVERSAL, or one of UNIVERSAL's parents,
1351 invalidate all method caches globally */
1352 if((memEQs(stashname, stashname_len, "UNIVERSAL"))
1353 || (isarev && hv_existss(isarev, "UNIVERSAL"))) {
1354 PL_sub_generation++;
1358 /* else, invalidate the method caches of all child classes,
1363 hv_iterinit(isarev);
1364 while((iter = hv_iternext(isarev))) {
1365 HV* const revstash = gv_stashsv(hv_iterkeysv(iter), 0);
1366 struct mro_meta* mrometa;
1368 if(!revstash) continue;
1369 mrometa = HvMROMETA(revstash);
1370 mrometa->cache_gen++;
1371 if(mrometa->mro_nextmethod)
1372 hv_clear(mrometa->mro_nextmethod);
1373 mrometa->destroy_gen = 0;
1377 /* The method change may be due to *{$package . "::()"} = \&nil; in
1380 /* pessimise derefs for now. Will get recalculated by Gv_AMupdate() */
1381 HvAUX(stash)->xhv_aux_flags &= ~HvAUXf_NO_DEREF;
1385 =for apidoc mro_set_mro
1387 Set C<meta> to the value contained in the registered mro plugin whose name is
1390 Croaks if C<name> hasn't been registered
1396 Perl_mro_set_mro(pTHX_ struct mro_meta *const meta, SV *const name)
1398 const struct mro_alg *const which = Perl_mro_get_from_name(aTHX_ name);
1400 PERL_ARGS_ASSERT_MRO_SET_MRO;
1403 croak("Invalid mro name: '%" SVf "'", name);
1405 if(meta->mro_which != which) {
1406 if (meta->mro_linear_current && !meta->mro_linear_all) {
1407 /* If we were storing something directly, put it in the hash before
1409 Perl_mro_set_private_data(aTHX_ meta, meta->mro_which,
1410 MUTABLE_SV(meta->mro_linear_current));
1412 meta->mro_which = which;
1413 /* Scrub our cached pointer to the private data. */
1414 meta->mro_linear_current = NULL;
1415 /* Only affects local method cache, not
1416 even child classes */
1418 if(meta->mro_nextmethod)
1419 hv_clear(meta->mro_nextmethod);
1425 XS(XS_mro_method_changed_in);
1428 Perl_boot_core_mro(pTHX)
1430 static const char file[] = __FILE__;
1432 Perl_mro_register(aTHX_ &dfs_alg);
1434 newXSproto("mro::method_changed_in", XS_mro_method_changed_in, file, "$");
1437 XS(XS_mro_method_changed_in)
1444 croak_xs_usage(cv, "classname");
1448 class_stash = gv_stashsv(classname, 0);
1449 if(!class_stash) croak("No such class: '%" SVf "'!", SVfARG(classname));
1451 mro_method_changed_in(class_stash);
1457 * ex: set ts=8 sts=4 sw=4 et: