+ * So we get it from the gv. But, since the gv may no longer be in the
+ * symbol table, we check that first. The only reliable way to tell is
+ * to see whether its stash has an effective name and whether the gv
+ * resides in that stash under its name. That effective name may be
+ * different from what gv_fullname4 would use.
+ * If flags & 1, the caller has asked us to skip the check.
+ */
+ if(!(flags & 1)) {
+ SV **svp;
+ if(
+ !GvSTASH(gv) || !HvENAME(GvSTASH(gv)) ||
+ !(svp = hv_fetchhek(GvSTASH(gv), GvNAME_HEK(gv), 0)) ||
+ *svp != (SV *)gv
+ ) return;
+ }
+ assert(SvOOK(GvSTASH(gv)));
+ assert(GvNAMELEN(gv));
+ assert(GvNAME(gv)[GvNAMELEN(gv) - 1] == ':');
+ assert(GvNAMELEN(gv) == 1 || GvNAME(gv)[GvNAMELEN(gv) - 2] == ':');
+ name_count = HvAUX(GvSTASH(gv))->xhv_name_count;
+ if (!name_count) {
+ name_count = 1;
+ namep = &HvAUX(GvSTASH(gv))->xhv_name_u.xhvnameu_name;
+ }
+ else {
+ namep = HvAUX(GvSTASH(gv))->xhv_name_u.xhvnameu_names;
+ if (name_count < 0) ++namep, name_count = -name_count - 1;
+ }
+ if (name_count == 1) {
+ if (HEK_LEN(*namep) == 4 && strnEQ(HEK_KEY(*namep), "main", 4)) {
+ namesv = GvNAMELEN(gv) == 1
+ ? newSVpvs_flags(":", SVs_TEMP)
+ : newSVpvs_flags("", SVs_TEMP);
+ }
+ else {
+ namesv = sv_2mortal(newSVhek(*namep));
+ if (GvNAMELEN(gv) == 1) sv_catpvs(namesv, ":");
+ else sv_catpvs(namesv, "::");
+ }
+ if (GvNAMELEN(gv) != 1) {
+ sv_catpvn_flags(
+ namesv, GvNAME(gv), GvNAMELEN(gv) - 2,
+ /* skip trailing :: */
+ GvNAMEUTF8(gv) ? SV_CATUTF8 : SV_CATBYTES
+ );
+ }
+ }
+ else {
+ SV *aname;
+ namesv = sv_2mortal((SV *)newAV());
+ while (name_count--) {
+ if(HEK_LEN(*namep) == 4 && strnEQ(HEK_KEY(*namep), "main", 4)){
+ aname = GvNAMELEN(gv) == 1
+ ? newSVpvs(":")
+ : newSVpvs("");
+ namep++;
+ }
+ else {
+ aname = newSVhek(*namep++);
+ if (GvNAMELEN(gv) == 1) sv_catpvs(aname, ":");
+ else sv_catpvs(aname, "::");
+ }
+ if (GvNAMELEN(gv) != 1) {
+ sv_catpvn_flags(
+ aname, GvNAME(gv), GvNAMELEN(gv) - 2,
+ /* skip trailing :: */
+ GvNAMEUTF8(gv) ? SV_CATUTF8 : SV_CATBYTES
+ );
+ }
+ av_push((AV *)namesv, aname);
+ }
+ }
+
+ /* Get a list of all the affected classes. */
+ /* We cannot simply pass them all to mro_isa_changed_in to avoid
+ the list, as that function assumes that only one package has
+ changed. It does not work with:
+
+ @foo::ISA = qw( B B::B );
+ *B:: = delete $::{"A::"};
+
+ as neither B nor B::B can be updated before the other, since they
+ will reset caches on foo, which will see either B or B::B with the
+ wrong name. The names must be set on *all* affected stashes before
+ we do anything else. (And linearisations must be cleared, too.)
+ */
+ stashes = (HV *) sv_2mortal((SV *)newHV());
+ mro_gather_and_rename(
+ stashes, (HV *) sv_2mortal((SV *)newHV()),
+ stash, oldstash, namesv
+ );
+
+ /* Once the caches have been wiped on all the classes, call
+ mro_isa_changed_in on each. */
+ hv_iterinit(stashes);
+ while((iter = hv_iternext(stashes))) {
+ HV * const stash = *(HV **)HEK_KEY(HeKEY_hek(iter));
+ if(HvENAME(stash)) {
+ /* We have to restore the original meta->isa (that
+ mro_gather_and_rename set aside for us) this way, in case
+ one class in this list is a superclass of a another class
+ that we have already encountered. In such a case, meta->isa
+ will have been overwritten without old entries being deleted
+ from PL_isarev. */
+ struct mro_meta * const meta = HvMROMETA(stash);
+ if(meta->isa != (HV *)HeVAL(iter)){
+ SvREFCNT_dec(meta->isa);
+ meta->isa
+ = HeVAL(iter) == &PL_sv_yes
+ ? NULL
+ : (HV *)HeVAL(iter);
+ HeVAL(iter) = NULL; /* We donated our reference count. */
+ }
+ mro_isa_changed_in(stash);
+ }
+ }
+}
+
+STATIC void
+S_mro_gather_and_rename(pTHX_ HV * const stashes, HV * const seen_stashes,
+ HV *stash, HV *oldstash, SV *namesv)
+{
+ XPVHV* xhv;
+ HE *entry;
+ I32 riter = -1;
+ I32 items = 0;
+ const bool stash_had_name = stash && HvENAME(stash);
+ bool fetched_isarev = FALSE;
+ HV *seen = NULL;
+ HV *isarev = NULL;
+ SV **svp = NULL;
+
+ PERL_ARGS_ASSERT_MRO_GATHER_AND_RENAME;
+
+ /* We use the seen_stashes hash to keep track of which packages have
+ been encountered so far. This must be separate from the main list of
+ stashes, as we need to distinguish between stashes being assigned
+ and stashes being replaced/deleted. (A nested stash can be on both
+ sides of an assignment. We cannot simply skip iterating through a
+ stash on the right if we have seen it on the left, as it will not
+ get its ename assigned to it.)
+
+ To avoid allocating extra SVs, instead of a bitfield we can make
+ bizarre use of immortals:
+
+ &PL_sv_undef: seen on the left (oldstash)
+ &PL_sv_no : seen on the right (stash)
+ &PL_sv_yes : seen on both sides
+