This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade Encode from version 2.78 to 2.79
[perl5.git] / mro_core.c
CommitLineData
8b371338 1/* mro_core.c
e1a479c5
BB
2 *
3 * Copyright (c) 2007 Brandon L Black
2eee27d7 4 * Copyright (c) 2007, 2008, 2009, 2010, 2011 Larry Wall and others
e1a479c5
BB
5 *
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.
8 *
8b371338
KW
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)
e1a479c5
BB
12 */
13
14/*
4ac71550
TC
15 * 'Which order shall we go in?' said Frodo. 'Eldest first, or quickest first?
16 * You'll be last either way, Master Peregrin.'
17 *
18 * [p.101 of _The Lord of the Rings_, I/iii: "A Conspiracy Unmasked"]
cac98860
RGS
19 */
20
21/*
e1a479c5 22=head1 MRO Functions
e1a479c5
BB
23These functions are related to the method resolution order of perl classes
24
25=cut
26*/
27
28#include "EXTERN.h"
4befac30 29#define PERL_IN_MRO_C
e1a479c5
BB
30#include "perl.h"
31
a3e6e81e
NC
32static const struct mro_alg dfs_alg =
33 {S_mro_get_linear_isa_dfs, "dfs", 3, 0, 0};
3d76853f 34
fa60396f
NC
35SV *
36Perl_mro_get_private_data(pTHX_ struct mro_meta *const smeta,
37 const struct mro_alg *const which)
38{
39 SV **data;
40 PERL_ARGS_ASSERT_MRO_GET_PRIVATE_DATA;
41
5844ac76
NC
42 data = (SV **)Perl_hv_common(aTHX_ smeta->mro_linear_all, NULL,
43 which->name, which->length, which->kflags,
44 HV_FETCH_JUST_SV, NULL, which->hash);
fa60396f
NC
45 if (!data)
46 return NULL;
47
48 /* If we've been asked to look up the private data for the current MRO, then
49 cache it. */
50 if (smeta->mro_which == which)
3a6fa573 51 smeta->mro_linear_current = *data;
fa60396f
NC
52
53 return *data;
54}
55
56SV *
57Perl_mro_set_private_data(pTHX_ struct mro_meta *const smeta,
58 const struct mro_alg *const which, SV *const data)
59{
60 PERL_ARGS_ASSERT_MRO_SET_PRIVATE_DATA;
61
9953ff72 62 if (!smeta->mro_linear_all) {
553e831a
NC
63 if (smeta->mro_which == which) {
64 /* If all we need to store is the current MRO's data, then don't use
65 memory on a hash with 1 element - store it direct, and signal
66 this by leaving the would-be-hash NULL. */
3a6fa573 67 smeta->mro_linear_current = data;
553e831a
NC
68 return data;
69 } else {
70 HV *const hv = newHV();
71 /* Start with 2 buckets. It's unlikely we'll need more. */
8b371338 72 HvMAX(hv) = 1;
9953ff72 73 smeta->mro_linear_all = hv;
553e831a 74
3a6fa573 75 if (smeta->mro_linear_current) {
553e831a
NC
76 /* If we were storing something directly, put it in the hash
77 before we lose it. */
8b371338 78 Perl_mro_set_private_data(aTHX_ smeta, smeta->mro_which,
3a6fa573 79 smeta->mro_linear_current);
553e831a
NC
80 }
81 }
82 }
83
84 /* We get here if we're storing more than one linearisation for this stash,
85 or the linearisation we are storing is not that if its current MRO. */
86
87 if (smeta->mro_which == which) {
88 /* If we've been asked to store the private data for the current MRO,
89 then cache it. */
3a6fa573 90 smeta->mro_linear_current = data;
fa60396f
NC
91 }
92
9953ff72 93 if (!Perl_hv_common(aTHX_ smeta->mro_linear_all, NULL,
fa60396f
NC
94 which->name, which->length, which->kflags,
95 HV_FETCH_ISSTORE, data, which->hash)) {
96 Perl_croak(aTHX_ "panic: hv_store() failed in set_mro_private_data() "
97 "for '%.*s' %d", (int) which->length, which->name,
98 which->kflags);
99 }
100
101 return data;
102}
103
a3e6e81e
NC
104const struct mro_alg *
105Perl_mro_get_from_name(pTHX_ SV *name) {
106 SV **data;
107
108 PERL_ARGS_ASSERT_MRO_GET_FROM_NAME;
109
5844ac76
NC
110 data = (SV **)Perl_hv_common(aTHX_ PL_registered_mros, name, NULL, 0, 0,
111 HV_FETCH_JUST_SV, NULL, 0);
a3e6e81e
NC
112 if (!data)
113 return NULL;
114 assert(SvTYPE(*data) == SVt_IV);
115 assert(SvIOK(*data));
116 return INT2PTR(const struct mro_alg *, SvUVX(*data));
117}
118
c145ee24
FC
119/*
120=for apidoc mro_register
121Registers a custom mro plugin. See L<perlmroapi> for details.
164df45a
FC
122
123=cut
c145ee24
FC
124*/
125
a3e6e81e
NC
126void
127Perl_mro_register(pTHX_ const struct mro_alg *mro) {
128 SV *wrapper = newSVuv(PTR2UV(mro));
129
130 PERL_ARGS_ASSERT_MRO_REGISTER;
131
8b371338 132
a3e6e81e
NC
133 if (!Perl_hv_common(aTHX_ PL_registered_mros, NULL,
134 mro->name, mro->length, mro->kflags,
135 HV_FETCH_ISSTORE, wrapper, mro->hash)) {
06059157 136 SvREFCNT_dec_NN(wrapper);
a3e6e81e
NC
137 Perl_croak(aTHX_ "panic: hv_store() failed in mro_register() "
138 "for '%.*s' %d", (int) mro->length, mro->name, mro->kflags);
3d76853f 139 }
3d76853f
NC
140}
141
e1a479c5
BB
142struct mro_meta*
143Perl_mro_meta_init(pTHX_ HV* stash)
144{
9fe4aecf 145 struct mro_meta* newmeta;
e1a479c5 146
7918f24d 147 PERL_ARGS_ASSERT_MRO_META_INIT;
dc3bf405 148 PERL_UNUSED_CONTEXT;
e1a479c5
BB
149 assert(HvAUX(stash));
150 assert(!(HvAUX(stash)->xhv_mro_meta));
183086be 151 Newxz(newmeta, 1, struct mro_meta);
9fe4aecf 152 HvAUX(stash)->xhv_mro_meta = newmeta;
dd69841b 153 newmeta->cache_gen = 1;
70cd14a1 154 newmeta->pkg_gen = 1;
a3e6e81e 155 newmeta->mro_which = &dfs_alg;
e1a479c5
BB
156
157 return newmeta;
158}
159
160#if defined(USE_ITHREADS)
161
162/* for sv_dup on new threads */
163struct mro_meta*
164Perl_mro_meta_dup(pTHX_ struct mro_meta* smeta, CLONE_PARAMS* param)
165{
e1a479c5
BB
166 struct mro_meta* newmeta;
167
7918f24d 168 PERL_ARGS_ASSERT_MRO_META_DUP;
e1a479c5 169
33e12d9d
NC
170 Newx(newmeta, 1, struct mro_meta);
171 Copy(smeta, newmeta, 1, struct mro_meta);
172
9953ff72
NC
173 if (newmeta->mro_linear_all) {
174 newmeta->mro_linear_all
a09252eb 175 = MUTABLE_HV(sv_dup_inc((const SV *)newmeta->mro_linear_all, param));
553e831a
NC
176 /* This is just acting as a shortcut pointer, and will be automatically
177 updated on the first get. */
3a6fa573
NC
178 newmeta->mro_linear_current = NULL;
179 } else if (newmeta->mro_linear_current) {
553e831a 180 /* Only the current MRO is stored, so this owns the data. */
3a6fa573 181 newmeta->mro_linear_current
a09252eb 182 = sv_dup_inc((const SV *)newmeta->mro_linear_current, param);
553e831a
NC
183 }
184
33e12d9d
NC
185 if (newmeta->mro_nextmethod)
186 newmeta->mro_nextmethod
a09252eb 187 = MUTABLE_HV(sv_dup_inc((const SV *)newmeta->mro_nextmethod, param));
a49ba3fc
NC
188 if (newmeta->isa)
189 newmeta->isa
a09252eb 190 = MUTABLE_HV(sv_dup_inc((const SV *)newmeta->isa, param));
e1a479c5 191
1a33a059
FC
192 newmeta->super = NULL;
193
e1a479c5
BB
194 return newmeta;
195}
196
197#endif /* USE_ITHREADS */
198
199/*
200=for apidoc mro_get_linear_isa_dfs
201
796b6530 202Returns the Depth-First Search linearization of C<@ISA>
e1a479c5
BB
203the given stash. The return value is a read-only AV*.
204C<level> should be 0 (it is used internally in this
205function's recursion).
206
1c908217
RGS
207You are responsible for C<SvREFCNT_inc()> on the
208return value if you plan to store it anywhere
209semi-permanently (otherwise it might be deleted
210out from under you the next time the cache is
211invalidated).
212
e1a479c5
BB
213=cut
214*/
4befac30 215static AV*
94d1e706 216S_mro_get_linear_isa_dfs(pTHX_ HV *stash, U32 level)
e1a479c5
BB
217{
218 AV* retval;
219 GV** gvp;
220 GV* gv;
221 AV* av;
190d0b22 222 const HEK* stashhek;
e1a479c5 223 struct mro_meta* meta;
a49ba3fc 224 SV *our_name;
73519bd0 225 HV *stored = NULL;
e1a479c5 226
7918f24d 227 PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA_DFS;
e1a479c5
BB
228 assert(HvAUX(stash));
229
00169e2c 230 stashhek
15d9236d 231 = HvAUX(stash)->xhv_name_u.xhvnameu_name && HvENAME_HEK_NN(stash)
00169e2c
FC
232 ? HvENAME_HEK_NN(stash)
233 : HvNAME_HEK(stash);
234
190d0b22 235 if (!stashhek)
1e05feb3 236 Perl_croak(aTHX_ "Can't linearize anonymous symbol table");
e1a479c5
BB
237
238 if (level > 100)
d0c0e7dd
FC
239 Perl_croak(aTHX_
240 "Recursive inheritance detected in package '%"HEKf"'",
241 HEKfARG(stashhek));
e1a479c5
BB
242
243 meta = HvMROMETA(stash);
1c908217
RGS
244
245 /* return cache if valid */
a3e6e81e 246 if((retval = MUTABLE_AV(MRO_GET_PRIVATE_DATA(meta, &dfs_alg)))) {
e1a479c5
BB
247 return retval;
248 }
249
250 /* not in cache, make a new one */
1c908217 251
ad64d0ec 252 retval = MUTABLE_AV(sv_2mortal(MUTABLE_SV(newAV())));
a49ba3fc
NC
253 /* We use this later in this function, but don't need a reference to it
254 beyond the end of this function, so reference count is fine. */
255 our_name = newSVhek(stashhek);
256 av_push(retval, our_name); /* add ourselves at the top */
e1a479c5 257
1c908217 258 /* fetch our @ISA */
e1a479c5
BB
259 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
260 av = (gvp && (gv = *gvp) && isGV_with_GP(gv)) ? GvAV(gv) : NULL;
261
a49ba3fc
NC
262 /* "stored" is used to keep track of all of the classnames we have added to
263 the MRO so far, so we can do a quick exists check and avoid adding
264 duplicate classnames to the MRO as we go.
265 It's then retained to be re-used as a fast lookup for ->isa(), by adding
266 our own name and "UNIVERSAL" to it. */
267
a49ba3fc 268 if(av && AvFILLp(av) >= 0) {
1c908217 269
ffd8da72
NC
270 SV **svp = AvARRAY(av);
271 I32 items = AvFILLp(av) + 1;
1c908217
RGS
272
273 /* foreach(@ISA) */
e1a479c5 274 while (items--) {
3df5c4b5 275 SV* const sv = *svp ? *svp : &PL_sv_undef;
e1a479c5 276 HV* const basestash = gv_stashsv(sv, 0);
ffd8da72
NC
277 SV *const *subrv_p;
278 I32 subrv_items;
3df5c4b5 279 svp++;
e1a479c5
BB
280
281 if (!basestash) {
1c908217
RGS
282 /* if no stash exists for this @ISA member,
283 simply add it to the MRO and move on */
ffd8da72
NC
284 subrv_p = &sv;
285 subrv_items = 1;
e1a479c5
BB
286 }
287 else {
1c908217 288 /* otherwise, recurse into ourselves for the MRO
b1d0c68a
NC
289 of this @ISA member, and append their MRO to ours.
290 The recursive call could throw an exception, which
291 has memory management implications here, hence the use of
292 the mortal. */
ffd8da72
NC
293 const AV *const subrv
294 = mro_get_linear_isa_dfs(basestash, level + 1);
295
296 subrv_p = AvARRAY(subrv);
297 subrv_items = AvFILLp(subrv) + 1;
298 }
73519bd0
NC
299 if (stored) {
300 while(subrv_items--) {
301 SV *const subsv = *subrv_p++;
302 /* LVALUE fetch will create a new undefined SV if necessary
303 */
304 HE *const he = hv_fetch_ent(stored, subsv, 1, 0);
305 assert(he);
306 if(HeVAL(he) != &PL_sv_undef) {
307 /* It was newly created. Steal it for our new SV, and
308 replace it in the hash with the "real" thing. */
309 SV *const val = HeVAL(he);
310 HEK *const key = HeKEY_hek(he);
311
312 HeVAL(he) = &PL_sv_undef;
b69f5762 313 sv_sethek(val, key);
73519bd0
NC
314 av_push(retval, val);
315 }
ffd8da72 316 }
73519bd0
NC
317 } else {
318 /* We are the first (or only) parent. We can short cut the
319 complexity above, because our @ISA is simply us prepended
320 to our parent's @ISA, and our ->isa cache is simply our
321 parent's, with our name added. */
322 /* newSVsv() is slow. This code is only faster if we can avoid
323 it by ensuring that SVs in the arrays are shared hash key
324 scalar SVs, because we can "copy" them very efficiently.
325 Although to be fair, we can't *ensure* this, as a reference
326 to the internal array is returned by mro::get_linear_isa(),
327 so we'll have to be defensive just in case someone faffed
328 with it. */
329 if (basestash) {
330 SV **svp;
17eef65c 331 stored = MUTABLE_HV(sv_2mortal((SV*)newHVhv(HvMROMETA(basestash)->isa)));
73519bd0
NC
332 av_extend(retval, subrv_items);
333 AvFILLp(retval) = subrv_items;
334 svp = AvARRAY(retval);
335 while(subrv_items--) {
336 SV *const val = *subrv_p++;
337 *++svp = SvIsCOW_shared_hash(val)
338 ? newSVhek(SvSHARED_HEK_FROM_PV(SvPVX(val)))
339 : newSVsv(val);
340 }
341 } else {
342 /* They have no stash. So create ourselves an ->isa cache
343 as if we'd copied it from what theirs should be. */
344 stored = MUTABLE_HV(sv_2mortal(MUTABLE_SV(newHV())));
345 (void) hv_store(stored, "UNIVERSAL", 9, &PL_sv_undef, 0);
346 av_push(retval,
347 newSVhek(HeKEY_hek(hv_store_ent(stored, sv,
348 &PL_sv_undef, 0))));
349 }
350 }
e1a479c5 351 }
73519bd0
NC
352 } else {
353 /* We have no parents. */
354 stored = MUTABLE_HV(sv_2mortal(MUTABLE_SV(newHV())));
355 (void) hv_store(stored, "UNIVERSAL", 9, &PL_sv_undef, 0);
e1a479c5
BB
356 }
357
ed09b296 358 (void) hv_store_ent(stored, our_name, &PL_sv_undef, 0);
ed09b296
NC
359
360 SvREFCNT_inc_simple_void_NN(stored);
361 SvTEMP_off(stored);
362 SvREADONLY_on(stored);
363
364 meta->isa = stored;
365
0fd7ece8
NC
366 /* now that we're past the exception dangers, grab our own reference to
367 the AV we're about to use for the result. The reference owned by the
368 mortals' stack will be released soon, so everything will balance. */
369 SvREFCNT_inc_simple_void_NN(retval);
370 SvTEMP_off(retval);
fdef73f9 371
1c908217
RGS
372 /* we don't want anyone modifying the cache entry but us,
373 and we do so by replacing it completely */
e1a479c5 374 SvREADONLY_on(retval);
1c908217 375
a3e6e81e 376 return MUTABLE_AV(Perl_mro_set_private_data(aTHX_ meta, &dfs_alg,
fa60396f 377 MUTABLE_SV(retval)));
e1a479c5
BB
378}
379
380/*
e1a479c5
BB
381=for apidoc mro_get_linear_isa
382
0b31f535 383Returns the mro linearisation for the given stash. By default, this
006d9e7b
FC
384will be whatever C<mro_get_linear_isa_dfs> returns unless some
385other MRO is in effect for the stash. The return value is a
e1a479c5
BB
386read-only AV*.
387
1c908217
RGS
388You are responsible for C<SvREFCNT_inc()> on the
389return value if you plan to store it anywhere
390semi-permanently (otherwise it might be deleted
391out from under you the next time the cache is
392invalidated).
393
e1a479c5
BB
394=cut
395*/
396AV*
397Perl_mro_get_linear_isa(pTHX_ HV *stash)
398{
399 struct mro_meta* meta;
7311f41d 400 AV *isa;
2c7f4b87 401
7918f24d 402 PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA;
2c7f4b87
BB
403 if(!SvOOK(stash))
404 Perl_croak(aTHX_ "Can't linearize anonymous symbol table");
e1a479c5
BB
405
406 meta = HvMROMETA(stash);
3d76853f 407 if (!meta->mro_which)
14f97ce6 408 Perl_croak(aTHX_ "panic: invalid MRO!");
7311f41d
FC
409 isa = meta->mro_which->resolve(aTHX_ stash, 0);
410
02cab674
FC
411 if (meta->mro_which != &dfs_alg) { /* skip for dfs, for speed */
412 SV * const namesv =
413 (HvENAME(stash)||HvNAME(stash))
414 ? newSVhek(HvENAME_HEK(stash)
415 ? HvENAME_HEK(stash)
416 : HvNAME_HEK(stash))
417 : NULL;
418
419 if(namesv && (AvFILLp(isa) == -1 || !sv_eq(*AvARRAY(isa), namesv)))
420 {
421 AV * const old = isa;
422 SV **svp;
423 SV **ovp = AvARRAY(old);
424 SV * const * const oend = ovp + AvFILLp(old) + 1;
425 isa = (AV *)sv_2mortal((SV *)newAV());
426 av_extend(isa, AvFILLp(isa) = AvFILLp(old)+1);
427 *AvARRAY(isa) = namesv;
428 svp = AvARRAY(isa)+1;
429 while (ovp < oend) *svp++ = SvREFCNT_inc(*ovp++);
430 }
431 else SvREFCNT_dec(namesv);
432 }
433
7311f41d
FC
434 if (!meta->isa) {
435 HV *const isa_hash = newHV();
436 /* Linearisation didn't build it for us, so do it here. */
437 SV *const *svp = AvARRAY(isa);
438 SV *const *const svp_end = svp + AvFILLp(isa) + 1;
439 const HEK *canon_name = HvENAME_HEK(stash);
440 if (!canon_name) canon_name = HvNAME_HEK(stash);
441
442 while (svp < svp_end) {
443 (void) hv_store_ent(isa_hash, *svp++, &PL_sv_undef, 0);
444 }
445
446 (void) hv_common(isa_hash, NULL, HEK_KEY(canon_name),
447 HEK_LEN(canon_name), HEK_FLAGS(canon_name),
448 HV_FETCH_ISSTORE, &PL_sv_undef,
449 HEK_HASH(canon_name));
450 (void) hv_store(isa_hash, "UNIVERSAL", 9, &PL_sv_undef, 0);
451
452 SvREADONLY_on(isa_hash);
453
454 meta->isa = isa_hash;
455 }
456
457 return isa;
e1a479c5
BB
458}
459
460/*
461=for apidoc mro_isa_changed_in
462
1c908217 463Takes the necessary steps (cache invalidations, mostly)
796b6530 464when the C<@ISA> of the given package has changed. Invoked
e1a479c5
BB
465by the C<setisa> magic, should not need to invoke directly.
466
467=cut
468*/
f7afb547 469
beeda143 470/* Macro to avoid repeating the code five times. */
f7afb547
FC
471#define CLEAR_LINEAR(mEta) \
472 if (mEta->mro_linear_all) { \
473 SvREFCNT_dec(MUTABLE_SV(mEta->mro_linear_all)); \
474 mEta->mro_linear_all = NULL; \
475 /* This is just acting as a shortcut pointer. */ \
476 mEta->mro_linear_current = NULL; \
477 } else if (mEta->mro_linear_current) { \
478 /* Only the current MRO is stored, so this owns the data. */ \
479 SvREFCNT_dec(mEta->mro_linear_current); \
480 mEta->mro_linear_current = NULL; \
481 }
482
e1a479c5 483void
4df35a07 484Perl_mro_isa_changed_in(pTHX_ HV* stash)
e1a479c5 485{
e1a479c5
BB
486 HV* isarev;
487 AV* linear_mro;
488 HE* iter;
489 SV** svp;
490 I32 items;
1e05feb3 491 bool is_universal;
4df35a07 492 struct mro_meta * meta;
80ebaca2 493 HV *isa = NULL;
e1a479c5 494
3386c920 495 const HEK * const stashhek = HvENAME_HEK(stash);
4df35a07
FC
496 const char * const stashname = HvENAME_get(stash);
497 const STRLEN stashname_len = HvENAMELEN_get(stash);
498
499 PERL_ARGS_ASSERT_MRO_ISA_CHANGED_IN;
7918f24d 500
2c7f4b87
BB
501 if(!stashname)
502 Perl_croak(aTHX_ "Can't call mro_isa_changed_in() on anonymous symbol table");
503
4df35a07
FC
504
505 /* wipe out the cached linearizations for this stash */
506 meta = HvMROMETA(stash);
f7afb547 507 CLEAR_LINEAR(meta);
4df35a07 508 if (meta->isa) {
80ebaca2
FC
509 /* Steal it for our own purposes. */
510 isa = (HV *)sv_2mortal((SV *)meta->isa);
5782d502 511 meta->isa = NULL;
6f86b615 512 }
70cd14a1 513
4df35a07
FC
514 /* Inc the package generation, since our @ISA changed */
515 meta->pkg_gen++;
516
e1a479c5
BB
517 /* Wipe the global method cache if this package
518 is UNIVERSAL or one of its parents */
dd69841b 519
3386c920 520 svp = hv_fetchhek(PL_isarev, stashhek, 0);
85fbaab2 521 isarev = svp ? MUTABLE_HV(*svp) : NULL;
dd69841b
BB
522
523 if((stashname_len == 9 && strEQ(stashname, "UNIVERSAL"))
524 || (isarev && hv_exists(isarev, "UNIVERSAL", 9))) {
e1a479c5 525 PL_sub_generation++;
dd69841b
BB
526 is_universal = TRUE;
527 }
1e05feb3 528 else { /* Wipe the local method cache otherwise */
4df35a07 529 meta->cache_gen++;
1e05feb3
AL
530 is_universal = FALSE;
531 }
e1a479c5
BB
532
533 /* wipe next::method cache too */
4df35a07 534 if(meta->mro_nextmethod) hv_clear(meta->mro_nextmethod);
1e05feb3 535
9cbd5ca3
FC
536 /* Changes to @ISA might turn overloading on */
537 HvAMAGIC_on(stash);
3d147ac2
DM
538 /* pessimise derefs for now. Will get recalculated by Gv_AMupdate() */
539 HvAUX(stash)->xhv_aux_flags &= ~HvAUXf_NO_DEREF;
9cbd5ca3 540
8c34e50d
FC
541 /* DESTROY can be cached in SvSTASH. */
542 if (!SvOBJECT(stash)) SvSTASH(stash) = NULL;
543
e1a479c5 544 /* Iterate the isarev (classes that are our children),
80ebaca2
FC
545 wiping out their linearization, method and isa caches
546 and upating PL_isarev. */
dd69841b 547 if(isarev) {
80ebaca2
FC
548 HV *isa_hashes = NULL;
549
550 /* We have to iterate through isarev twice to avoid a chicken and
551 * egg problem: if A inherits from B and both are in isarev, A might
e1fa07e3 552 * be processed before B and use B's previous linearisation.
80ebaca2
FC
553 */
554
555 /* First iteration: Wipe everything, but stash away the isa hashes
556 * since we still need them for updating PL_isarev.
557 */
558
559 if(hv_iterinit(isarev)) {
560 /* Only create the hash if we need it; i.e., if isarev has
561 any elements. */
562 isa_hashes = (HV *)sv_2mortal((SV *)newHV());
563 }
e1a479c5 564 while((iter = hv_iternext(isarev))) {
204e6232 565 HV* revstash = gv_stashsv(hv_iterkeysv(iter), 0);
bc2cbbac
BB
566 struct mro_meta* revmeta;
567
568 if(!revstash) continue;
569 revmeta = HvMROMETA(revstash);
f7afb547 570 CLEAR_LINEAR(revmeta);
dd69841b
BB
571 if(!is_universal)
572 revmeta->cache_gen++;
e1a479c5
BB
573 if(revmeta->mro_nextmethod)
574 hv_clear(revmeta->mro_nextmethod);
c07f9fb2 575 if (!SvOBJECT(revstash)) SvSTASH(revstash) = NULL;
80ebaca2
FC
576
577 (void)
578 hv_store(
579 isa_hashes, (const char*)&revstash, sizeof(HV *),
580 revmeta->isa ? (SV *)revmeta->isa : &PL_sv_undef, 0
581 );
582 revmeta->isa = NULL;
583 }
584
585 /* Second pass: Update PL_isarev. We can just use isa_hashes to
586 * avoid another round of stash lookups. */
587
588 /* isarev might be deleted from PL_isarev during this loop, so hang
589 * on to it. */
590 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)isarev));
591
592 if(isa_hashes) {
593 hv_iterinit(isa_hashes);
594 while((iter = hv_iternext(isa_hashes))) {
595 HV* const revstash = *(HV **)HEK_KEY(HeKEY_hek(iter));
596 HV * const isa = (HV *)HeVAL(iter);
597 const HEK *namehek;
598
80ebaca2
FC
599 /* We're starting at the 2nd element, skipping revstash */
600 linear_mro = mro_get_linear_isa(revstash);
601 svp = AvARRAY(linear_mro) + 1;
602 items = AvFILLp(linear_mro);
603
604 namehek = HvENAME_HEK(revstash);
605 if (!namehek) namehek = HvNAME_HEK(revstash);
606
607 while (items--) {
608 SV* const sv = *svp++;
609 HV* mroisarev;
610
611 HE *he = hv_fetch_ent(PL_isarev, sv, TRUE, 0);
612
613 /* That fetch should not fail. But if it had to create
614 a new SV for us, then will need to upgrade it to an
615 HV (which sv_upgrade() can now do for us). */
616
617 mroisarev = MUTABLE_HV(HeVAL(he));
618
619 SvUPGRADE(MUTABLE_SV(mroisarev), SVt_PVHV);
620
621 /* This hash only ever contains PL_sv_yes. Storing it
622 over itself is almost as cheap as calling hv_exists,
623 so on aggregate we expect to save time by not making
624 two calls to the common HV code for the case where
625 it doesn't exist. */
8b371338 626
80ebaca2 627 (void)
3386c920 628 hv_storehek(mroisarev, namehek, &PL_sv_yes);
80ebaca2
FC
629 }
630
316ebaf2
JH
631 if ((SV *)isa != &PL_sv_undef) {
632 assert(namehek);
80ebaca2
FC
633 mro_clean_isarev(
634 isa, HEK_KEY(namehek), HEK_LEN(namehek),
3386c920
FC
635 HvMROMETA(revstash)->isa, HEK_HASH(namehek),
636 HEK_UTF8(namehek)
80ebaca2 637 );
316ebaf2 638 }
80ebaca2 639 }
e1a479c5
BB
640 }
641 }
642
c9143745
FC
643 /* Now iterate our MRO (parents), adding ourselves and everything from
644 our isarev to their isarev.
1c908217
RGS
645 */
646
647 /* We're starting at the 2nd element, skipping ourselves here */
e1a479c5
BB
648 linear_mro = mro_get_linear_isa(stash);
649 svp = AvARRAY(linear_mro) + 1;
650 items = AvFILLp(linear_mro);
1c908217 651
e1a479c5
BB
652 while (items--) {
653 SV* const sv = *svp++;
e1a479c5
BB
654 HV* mroisarev;
655
117b69ca
NC
656 HE *he = hv_fetch_ent(PL_isarev, sv, TRUE, 0);
657
658 /* That fetch should not fail. But if it had to create a new SV for
4ea50411
NC
659 us, then will need to upgrade it to an HV (which sv_upgrade() can
660 now do for us. */
117b69ca 661
85fbaab2 662 mroisarev = MUTABLE_HV(HeVAL(he));
e1a479c5 663
ad64d0ec 664 SvUPGRADE(MUTABLE_SV(mroisarev), SVt_PVHV);
117b69ca 665
25270bc0
NC
666 /* This hash only ever contains PL_sv_yes. Storing it over itself is
667 almost as cheap as calling hv_exists, so on aggregate we expect to
668 save time by not making two calls to the common HV code for the
669 case where it doesn't exist. */
8b371338 670
3386c920 671 (void)hv_storehek(mroisarev, stashhek, &PL_sv_yes);
80ebaca2
FC
672 }
673
e1fa07e3 674 /* Delete our name from our former parents' isarevs. */
80ebaca2 675 if(isa && HvARRAY(isa))
204e6232 676 mro_clean_isarev(isa, stashname, stashname_len, meta->isa,
3386c920 677 HEK_HASH(stashhek), HEK_UTF8(stashhek));
80ebaca2
FC
678}
679
680/* Deletes name from all the isarev entries listed in isa */
681STATIC void
682S_mro_clean_isarev(pTHX_ HV * const isa, const char * const name,
3386c920
FC
683 const STRLEN len, HV * const exceptions, U32 hash,
684 U32 flags)
80ebaca2
FC
685{
686 HE* iter;
e1a479c5 687
80ebaca2
FC
688 PERL_ARGS_ASSERT_MRO_CLEAN_ISAREV;
689
e1fa07e3 690 /* Delete our name from our former parents' isarevs. */
3dc78631 691 if(HvARRAY(isa) && hv_iterinit(isa)) {
80ebaca2
FC
692 SV **svp;
693 while((iter = hv_iternext(isa))) {
694 I32 klen;
695 const char * const key = hv_iterkey(iter, &klen);
204e6232
BF
696 if(exceptions && hv_exists(exceptions, key, HeKUTF8(iter) ? -klen : klen))
697 continue;
698 svp = hv_fetch(PL_isarev, key, HeKUTF8(iter) ? -klen : klen, 0);
80ebaca2
FC
699 if(svp) {
700 HV * const isarev = (HV *)*svp;
3386c920
FC
701 (void)hv_common(isarev, NULL, name, len, flags,
702 G_DISCARD|HV_DELETE, NULL, hash);
1b95d04f 703 if(!HvARRAY(isarev) || !HvUSEDKEYS(isarev))
204e6232
BF
704 (void)hv_delete(PL_isarev, key,
705 HeKUTF8(iter) ? -klen : klen, G_DISCARD);
e1a479c5
BB
706 }
707 }
708 }
709}
710
711/*
c8bbf675
FC
712=for apidoc mro_package_moved
713
d056e33c 714Call this function to signal to a stash that it has been assigned to
61daec80 715another spot in the stash hierarchy. C<stash> is the stash that has been
154e47c8 716assigned. C<oldstash> is the stash it replaces, if any. C<gv> is the glob
afdbe55d 717that is actually being assigned to.
d056e33c 718
35759254 719This can also be called with a null first argument to
d056e33c
FC
720indicate that C<oldstash> has been deleted.
721
722This function invalidates isa caches on the old stash, on all subpackages
723nested inside it, and on the subclasses of all those, including
724non-existent packages that have corresponding entries in C<stash>.
c8bbf675 725
80ebaca2
FC
726It also sets the effective names (C<HvENAME>) on all the stashes as
727appropriate.
728
0290c710 729If the C<gv> is present and is not in the symbol table, then this function
61daec80 730simply returns. This checked will be skipped if C<flags & 1>.
0290c710 731
c8bbf675
FC
732=cut
733*/
734void
35759254 735Perl_mro_package_moved(pTHX_ HV * const stash, HV * const oldstash,
afdbe55d 736 const GV * const gv, U32 flags)
c8bbf675 737{
d7879cf0
FC
738 SV *namesv;
739 HEK **namep;
740 I32 name_count;
80ebaca2
FC
741 HV *stashes;
742 HE* iter;
62c1e33f 743
afdbe55d 744 PERL_ARGS_ASSERT_MRO_PACKAGE_MOVED;
35759254 745 assert(stash || oldstash);
35759254 746
d7879cf0 747 /* Determine the name(s) of the location that stash was assigned to
35759254
FC
748 * or from which oldstash was removed.
749 *
928eb066 750 * We cannot reliably use the name in oldstash, because it may have
35759254
FC
751 * been deleted from the location in the symbol table that its name
752 * suggests, as in this case:
753 *
754 * $globref = \*foo::bar::;
755 * Symbol::delete_package("foo");
756 * *$globref = \%baz::;
757 * *$globref = *frelp::;
758 * # calls mro_package_moved(%frelp::, %baz::, *$globref, NULL, 0)
759 *
d7879cf0
FC
760 * So we get it from the gv. But, since the gv may no longer be in the
761 * symbol table, we check that first. The only reliable way to tell is
762 * to see whether its stash has an effective name and whether the gv
763 * resides in that stash under its name. That effective name may be
764 * different from what gv_fullname4 would use.
765 * If flags & 1, the caller has asked us to skip the check.
35759254 766 */
d7879cf0
FC
767 if(!(flags & 1)) {
768 SV **svp;
769 if(
770 !GvSTASH(gv) || !HvENAME(GvSTASH(gv)) ||
3386c920 771 !(svp = hv_fetchhek(GvSTASH(gv), GvNAME_HEK(gv), 0)) ||
d7879cf0
FC
772 *svp != (SV *)gv
773 ) return;
774 }
775 assert(SvOOK(GvSTASH(gv)));
1f656fcf 776 assert(GvNAMELEN(gv));
d7879cf0 777 assert(GvNAME(gv)[GvNAMELEN(gv) - 1] == ':');
1f656fcf 778 assert(GvNAMELEN(gv) == 1 || GvNAME(gv)[GvNAMELEN(gv) - 2] == ':');
d7879cf0
FC
779 name_count = HvAUX(GvSTASH(gv))->xhv_name_count;
780 if (!name_count) {
781 name_count = 1;
15d9236d 782 namep = &HvAUX(GvSTASH(gv))->xhv_name_u.xhvnameu_name;
d7879cf0
FC
783 }
784 else {
15d9236d 785 namep = HvAUX(GvSTASH(gv))->xhv_name_u.xhvnameu_names;
d7879cf0
FC
786 if (name_count < 0) ++namep, name_count = -name_count - 1;
787 }
788 if (name_count == 1) {
789 if (HEK_LEN(*namep) == 4 && strnEQ(HEK_KEY(*namep), "main", 4)) {
1f656fcf
FC
790 namesv = GvNAMELEN(gv) == 1
791 ? newSVpvs_flags(":", SVs_TEMP)
792 : newSVpvs_flags("", SVs_TEMP);
d7879cf0
FC
793 }
794 else {
795 namesv = sv_2mortal(newSVhek(*namep));
1f656fcf
FC
796 if (GvNAMELEN(gv) == 1) sv_catpvs(namesv, ":");
797 else sv_catpvs(namesv, "::");
d7879cf0 798 }
204e6232 799 if (GvNAMELEN(gv) != 1) {
c682ebef
FC
800 sv_catpvn_flags(
801 namesv, GvNAME(gv), GvNAMELEN(gv) - 2,
d7879cf0 802 /* skip trailing :: */
c682ebef
FC
803 GvNAMEUTF8(gv) ? SV_CATUTF8 : SV_CATBYTES
804 );
204e6232 805 }
d7879cf0
FC
806 }
807 else {
808 SV *aname;
809 namesv = sv_2mortal((SV *)newAV());
810 while (name_count--) {
811 if(HEK_LEN(*namep) == 4 && strnEQ(HEK_KEY(*namep), "main", 4)){
1f656fcf
FC
812 aname = GvNAMELEN(gv) == 1
813 ? newSVpvs(":")
814 : newSVpvs("");
815 namep++;
d7879cf0
FC
816 }
817 else {
818 aname = newSVhek(*namep++);
1f656fcf
FC
819 if (GvNAMELEN(gv) == 1) sv_catpvs(aname, ":");
820 else sv_catpvs(aname, "::");
d7879cf0 821 }
204e6232 822 if (GvNAMELEN(gv) != 1) {
c682ebef
FC
823 sv_catpvn_flags(
824 aname, GvNAME(gv), GvNAMELEN(gv) - 2,
d7879cf0 825 /* skip trailing :: */
c682ebef
FC
826 GvNAMEUTF8(gv) ? SV_CATUTF8 : SV_CATBYTES
827 );
204e6232 828 }
d7879cf0
FC
829 av_push((AV *)namesv, aname);
830 }
831 }
c8bbf675 832
80ebaca2
FC
833 /* Get a list of all the affected classes. */
834 /* We cannot simply pass them all to mro_isa_changed_in to avoid
835 the list, as that function assumes that only one package has
836 changed. It does not work with:
837
838 @foo::ISA = qw( B B::B );
839 *B:: = delete $::{"A::"};
840
841 as neither B nor B::B can be updated before the other, since they
842 will reset caches on foo, which will see either B or B::B with the
843 wrong name. The names must be set on *all* affected stashes before
beeda143 844 we do anything else. (And linearisations must be cleared, too.)
80ebaca2
FC
845 */
846 stashes = (HV *) sv_2mortal((SV *)newHV());
b89cdb22
FC
847 mro_gather_and_rename(
848 stashes, (HV *) sv_2mortal((SV *)newHV()),
d7879cf0 849 stash, oldstash, namesv
b89cdb22 850 );
80ebaca2 851
80ebaca2
FC
852 /* Once the caches have been wiped on all the classes, call
853 mro_isa_changed_in on each. */
854 hv_iterinit(stashes);
855 while((iter = hv_iternext(stashes))) {
90ba1f34
FC
856 HV * const stash = *(HV **)HEK_KEY(HeKEY_hek(iter));
857 if(HvENAME(stash)) {
858 /* We have to restore the original meta->isa (that
859 mro_gather_and_rename set aside for us) this way, in case
860 one class in this list is a superclass of a another class
861 that we have already encountered. In such a case, meta->isa
8b371338 862
90ba1f34
FC
863 from PL_isarev. */
864 struct mro_meta * const meta = HvMROMETA(stash);
865 if(meta->isa != (HV *)HeVAL(iter)){
866 SvREFCNT_dec(meta->isa);
867 meta->isa
868 = HeVAL(iter) == &PL_sv_yes
869 ? NULL
870 : (HV *)HeVAL(iter);
871 HeVAL(iter) = NULL; /* We donated our reference count. */
872 }
873 mro_isa_changed_in(stash);
874 }
80ebaca2
FC
875 }
876}
877
aeba7c97 878STATIC void
b89cdb22 879S_mro_gather_and_rename(pTHX_ HV * const stashes, HV * const seen_stashes,
d7879cf0 880 HV *stash, HV *oldstash, SV *namesv)
80ebaca2 881{
eb578fdb
KW
882 XPVHV* xhv;
883 HE *entry;
80ebaca2 884 I32 riter = -1;
4c916935 885 I32 items = 0;
80ebaca2 886 const bool stash_had_name = stash && HvENAME(stash);
d7879cf0 887 bool fetched_isarev = FALSE;
80ebaca2
FC
888 HV *seen = NULL;
889 HV *isarev = NULL;
d7879cf0 890 SV **svp = NULL;
80ebaca2
FC
891
892 PERL_ARGS_ASSERT_MRO_GATHER_AND_RENAME;
893
b89cdb22
FC
894 /* We use the seen_stashes hash to keep track of which packages have
895 been encountered so far. This must be separate from the main list of
896 stashes, as we need to distinguish between stashes being assigned
897 and stashes being replaced/deleted. (A nested stash can be on both
898 sides of an assignment. We cannot simply skip iterating through a
899 stash on the right if we have seen it on the left, as it will not
900 get its ename assigned to it.)
901
902 To avoid allocating extra SVs, instead of a bitfield we can make
903 bizarre use of immortals:
904
905 &PL_sv_undef: seen on the left (oldstash)
906 &PL_sv_no : seen on the right (stash)
907 &PL_sv_yes : seen on both sides
908
909 */
910
80ebaca2
FC
911 if(oldstash) {
912 /* Add to the big list. */
90ba1f34 913 struct mro_meta * meta;
80ebaca2
FC
914 HE * const entry
915 = (HE *)
916 hv_common(
b89cdb22 917 seen_stashes, NULL, (const char *)&oldstash, sizeof(HV *), 0,
df5f182b 918 HV_FETCH_LVALUE|HV_FETCH_EMPTY_HE, NULL, 0
80ebaca2 919 );
b89cdb22 920 if(HeVAL(entry) == &PL_sv_undef || HeVAL(entry) == &PL_sv_yes) {
80ebaca2
FC
921 oldstash = NULL;
922 goto check_stash;
923 }
b89cdb22
FC
924 HeVAL(entry)
925 = HeVAL(entry) == &PL_sv_no ? &PL_sv_yes : &PL_sv_undef;
90ba1f34 926 meta = HvMROMETA(oldstash);
b89cdb22
FC
927 (void)
928 hv_store(
929 stashes, (const char *)&oldstash, sizeof(HV *),
90ba1f34
FC
930 meta->isa
931 ? SvREFCNT_inc_simple_NN((SV *)meta->isa)
932 : &PL_sv_yes,
933 0
b89cdb22 934 );
beeda143 935 CLEAR_LINEAR(meta);
80ebaca2
FC
936
937 /* Update the effective name. */
938 if(HvENAME_get(oldstash)) {
d7879cf0
FC
939 const HEK * const enamehek = HvENAME_HEK(oldstash);
940 if(SvTYPE(namesv) == SVt_PVAV) {
941 items = AvFILLp((AV *)namesv) + 1;
942 svp = AvARRAY((AV *)namesv);
943 }
944 else {
945 items = 1;
946 svp = &namesv;
947 }
948 while (items--) {
204e6232 949 const U32 name_utf8 = SvUTF8(*svp);
d7879cf0 950 STRLEN len;
103f5a36
NC
951 const char *name = SvPVx_const(*svp, len);
952 if(PL_stashcache) {
953 DEBUG_o(Perl_deb(aTHX_ "mro_gather_and_rename clearing PL_stashcache for '%"SVf"'\n",
c1f6cd39 954 SVfARG(*svp)));
c60dbbc3 955 (void)hv_delete(PL_stashcache, name, name_utf8 ? -(I32)len : (I32)len, G_DISCARD);
103f5a36
NC
956 }
957 ++svp;
204e6232 958 hv_ename_delete(oldstash, name, len, name_utf8);
d7879cf0
FC
959
960 if (!fetched_isarev) {
961 /* If the name deletion caused a name change, then we
962 * are not going to call mro_isa_changed_in with this
963 * name (and not at all if it has become anonymous) so
964 * we need to delete old isarev entries here, both
e1fa07e3 965 * those in the superclasses and this class's own list
d7879cf0
FC
966 * of subclasses. We simply delete the latter from
967 * PL_isarev, since we still need it. hv_delete morti-
968 * fies it for us, so sv_2mortal is not necessary. */
969 if(HvENAME_HEK(oldstash) != enamehek) {
970 if(meta->isa && HvARRAY(meta->isa))
3386c920
FC
971 mro_clean_isarev(meta->isa, name, len, 0, 0,
972 name_utf8 ? HVhek_UTF8 : 0);
204e6232 973 isarev = (HV *)hv_delete(PL_isarev, name,
c60dbbc3 974 name_utf8 ? -(I32)len : (I32)len, 0);
d7879cf0
FC
975 fetched_isarev=TRUE;
976 }
977 }
978 }
80ebaca2 979 }
d7fbb1de 980 }
80ebaca2 981 check_stash:
8b9e80a3 982 if(stash) {
d7879cf0
FC
983 if(SvTYPE(namesv) == SVt_PVAV) {
984 items = AvFILLp((AV *)namesv) + 1;
985 svp = AvARRAY((AV *)namesv);
986 }
987 else {
988 items = 1;
989 svp = &namesv;
990 }
991 while (items--) {
204e6232 992 const U32 name_utf8 = SvUTF8(*svp);
d7879cf0
FC
993 STRLEN len;
994 const char *name = SvPVx_const(*svp++, len);
204e6232 995 hv_ename_add(stash, name, len, name_utf8);
d7879cf0 996 }
80ebaca2 997
b89cdb22
FC
998 /* Add it to the big list if it needs
999 * mro_isa_changed_in called on it. That happens if it was
80ebaca2 1000 * detached from the symbol table (so it had no HvENAME) before
9404893f 1001 * being assigned to the spot named by the 'name' variable, because
486ec47a 1002 * its cached isa linearisation is now stale (the effective name
80ebaca2
FC
1003 * having changed), and subclasses will then use that cache when
1004 * mro_package_moved calls mro_isa_changed_in. (See
8b9e80a3 1005 * [perl #77358].)
80ebaca2 1006 *
8b9e80a3 1007 * If it did have a name, then its previous name is still
80ebaca2
FC
1008 * used in isa caches, and there is no need for
1009 * mro_package_moved to call mro_isa_changed_in.
8b9e80a3 1010 */
80ebaca2
FC
1011
1012 entry
1013 = (HE *)
1014 hv_common(
b89cdb22 1015 seen_stashes, NULL, (const char *)&stash, sizeof(HV *), 0,
df5f182b 1016 HV_FETCH_LVALUE|HV_FETCH_EMPTY_HE, NULL, 0
80ebaca2 1017 );
b89cdb22 1018 if(HeVAL(entry) == &PL_sv_yes || HeVAL(entry) == &PL_sv_no)
80ebaca2 1019 stash = NULL;
b89cdb22
FC
1020 else {
1021 HeVAL(entry)
1022 = HeVAL(entry) == &PL_sv_undef ? &PL_sv_yes : &PL_sv_no;
1023 if(!stash_had_name)
90ba1f34
FC
1024 {
1025 struct mro_meta * const meta = HvMROMETA(stash);
b89cdb22
FC
1026 (void)
1027 hv_store(
1028 stashes, (const char *)&stash, sizeof(HV *),
90ba1f34
FC
1029 meta->isa
1030 ? SvREFCNT_inc_simple_NN((SV *)meta->isa)
1031 : &PL_sv_yes,
1032 0
b89cdb22 1033 );
beeda143 1034 CLEAR_LINEAR(meta);
90ba1f34 1035 }
b89cdb22 1036 }
8b9e80a3 1037 }
d7fbb1de 1038
80ebaca2
FC
1039 if(!stash && !oldstash)
1040 /* Both stashes have been encountered already. */
1041 return;
1042
1043 /* Add all the subclasses to the big list. */
d7879cf0
FC
1044 if(!fetched_isarev) {
1045 /* If oldstash is not null, then we can use its HvENAME to look up
1046 the isarev hash, since all its subclasses will be listed there.
06f3ce86
FC
1047 It will always have an HvENAME. It the HvENAME was removed
1048 above, then fetch_isarev will be true, and this code will not be
1049 reached.
1050
1051 If oldstash is null, then this is an empty spot with no stash in
1052 it, so subclasses could be listed in isarev hashes belonging to
1053 any of the names, so we have to check all of them.
1054 */
e0aa2606 1055 assert(!oldstash || HvENAME(oldstash));
06f3ce86
FC
1056 if (oldstash) {
1057 /* Extra variable to avoid a compiler warning */
3386c920 1058 const HEK * const hvename = HvENAME_HEK(oldstash);
d7879cf0 1059 fetched_isarev = TRUE;
3386c920 1060 svp = hv_fetchhek(PL_isarev, hvename, 0);
d7879cf0
FC
1061 if (svp) isarev = MUTABLE_HV(*svp);
1062 }
1063 else if(SvTYPE(namesv) == SVt_PVAV) {
1064 items = AvFILLp((AV *)namesv) + 1;
1065 svp = AvARRAY((AV *)namesv);
1066 }
1067 else {
1068 items = 1;
1069 svp = &namesv;
1070 }
1071 }
80ebaca2 1072 if(
d7879cf0 1073 isarev || !fetched_isarev
80ebaca2 1074 ) {
d7879cf0 1075 while (fetched_isarev || items--) {
80ebaca2 1076 HE *iter;
d7879cf0
FC
1077
1078 if (!fetched_isarev) {
1079 HE * const he = hv_fetch_ent(PL_isarev, *svp++, 0, 0);
1080 if (!he || !(isarev = MUTABLE_HV(HeVAL(he)))) continue;
1081 }
1082
80ebaca2
FC
1083 hv_iterinit(isarev);
1084 while((iter = hv_iternext(isarev))) {
204e6232 1085 HV* revstash = gv_stashsv(hv_iterkeysv(iter), 0);
90ba1f34 1086 struct mro_meta * meta;
80ebaca2
FC
1087
1088 if(!revstash) continue;
90ba1f34 1089 meta = HvMROMETA(revstash);
b89cdb22
FC
1090 (void)
1091 hv_store(
1092 stashes, (const char *)&revstash, sizeof(HV *),
90ba1f34
FC
1093 meta->isa
1094 ? SvREFCNT_inc_simple_NN((SV *)meta->isa)
1095 : &PL_sv_yes,
1096 0
b89cdb22 1097 );
beeda143 1098 CLEAR_LINEAR(meta);
80ebaca2 1099 }
d7879cf0
FC
1100
1101 if (fetched_isarev) break;
1102 }
80ebaca2 1103 }
d056e33c 1104
c8bbf675
FC
1105 /* This is partly based on code in hv_iternext_flags. We are not call-
1106 ing that here, as we want to avoid resetting the hash iterator. */
1107
d056e33c 1108 /* Skip the entire loop if the hash is empty. */
8b371338 1109 if(oldstash && HvUSEDKEYS(oldstash)) {
d056e33c 1110 xhv = (XPVHV*)SvANY(oldstash);
dbe2fffc 1111 seen = (HV *) sv_2mortal((SV *)newHV());
d056e33c 1112
80ebaca2
FC
1113 /* Iterate through entries in the oldstash, adding them to the
1114 list, meanwhile doing the equivalent of $seen{$key} = 1.
d056e33c
FC
1115 */
1116
1117 while (++riter <= (I32)xhv->xhv_max) {
1118 entry = (HvARRAY(oldstash))[riter];
1119
1120 /* Iterate through the entries in this list */
1121 for(; entry; entry = HeNEXT(entry)) {
1122 const char* key;
090c3c37 1123 I32 len;
d056e33c
FC
1124
1125 /* If this entry is not a glob, ignore it.
1126 Try the next. */
1127 if (!isGV(HeVAL(entry))) continue;
1128
090c3c37 1129 key = hv_iterkey(entry, &len);
1f656fcf
FC
1130 if ((len > 1 && key[len-2] == ':' && key[len-1] == ':')
1131 || (len == 1 && key[0] == ':')) {
d056e33c
FC
1132 HV * const oldsubstash = GvHV(HeVAL(entry));
1133 SV ** const stashentry
090c3c37 1134 = stash ? hv_fetch(stash, key, HeUTF8(entry) ? -(I32)len : (I32)len, 0) : NULL;
35759254 1135 HV *substash = NULL;
62c1e33f
FC
1136
1137 /* Avoid main::main::main::... */
1138 if(oldsubstash == oldstash) continue;
1139
d056e33c 1140 if(
35759254 1141 (
3d8812a2 1142 stashentry && *stashentry && isGV(*stashentry)
35759254
FC
1143 && (substash = GvHV(*stashentry))
1144 )
00169e2c 1145 || (oldsubstash && HvENAME_get(oldsubstash))
d056e33c 1146 )
35759254
FC
1147 {
1148 /* Add :: and the key (minus the trailing ::)
d7879cf0
FC
1149 to each name. */
1150 SV *subname;
1151 if(SvTYPE(namesv) == SVt_PVAV) {
1152 SV *aname;
1153 items = AvFILLp((AV *)namesv) + 1;
1154 svp = AvARRAY((AV *)namesv);
1155 subname = sv_2mortal((SV *)newAV());
1156 while (items--) {
1157 aname = newSVsv(*svp++);
1f656fcf
FC
1158 if (len == 1)
1159 sv_catpvs(aname, ":");
1160 else {
1161 sv_catpvs(aname, "::");
c682ebef
FC
1162 sv_catpvn_flags(
1163 aname, key, len-2,
090c3c37 1164 HeUTF8(entry)
c682ebef
FC
1165 ? SV_CATUTF8 : SV_CATBYTES
1166 );
1f656fcf 1167 }
d7879cf0
FC
1168 av_push((AV *)subname, aname);
1169 }
80ebaca2 1170 }
d7879cf0
FC
1171 else {
1172 subname = sv_2mortal(newSVsv(namesv));
1f656fcf
FC
1173 if (len == 1) sv_catpvs(subname, ":");
1174 else {
1175 sv_catpvs(subname, "::");
c682ebef
FC
1176 sv_catpvn_flags(
1177 subname, key, len-2,
090c3c37 1178 HeUTF8(entry) ? SV_CATUTF8 : SV_CATBYTES
c682ebef 1179 );
1f656fcf 1180 }
d7879cf0
FC
1181 }
1182 mro_gather_and_rename(
1183 stashes, seen_stashes,
1184 substash, oldsubstash, subname
1185 );
35759254 1186 }
d056e33c 1187
090c3c37 1188 (void)hv_store(seen, key, HeUTF8(entry) ? -(I32)len : (I32)len, &PL_sv_yes, 0);
d056e33c
FC
1189 }
1190 }
1191 }
1192 }
c8bbf675
FC
1193
1194 /* Skip the entire loop if the hash is empty. */
d056e33c
FC
1195 if (stash && HvUSEDKEYS(stash)) {
1196 xhv = (XPVHV*)SvANY(stash);
b89cdb22 1197 riter = -1;
d056e33c
FC
1198
1199 /* Iterate through the new stash, skipping $seen{$key} items,
b89cdb22 1200 calling mro_gather_and_rename(stashes,seen,entry,NULL, ...). */
c8bbf675
FC
1201 while (++riter <= (I32)xhv->xhv_max) {
1202 entry = (HvARRAY(stash))[riter];
1203
1204 /* Iterate through the entries in this list */
1205 for(; entry; entry = HeNEXT(entry)) {
1206 const char* key;
090c3c37 1207 I32 len;
c8bbf675
FC
1208
1209 /* If this entry is not a glob, ignore it.
1210 Try the next. */
1211 if (!isGV(HeVAL(entry))) continue;
1212
090c3c37 1213 key = hv_iterkey(entry, &len);
1f656fcf
FC
1214 if ((len > 1 && key[len-2] == ':' && key[len-1] == ':')
1215 || (len == 1 && key[0] == ':')) {
d056e33c
FC
1216 HV *substash;
1217
1218 /* If this entry was seen when we iterated through the
1219 oldstash, skip it. */
090c3c37 1220 if(seen && hv_exists(seen, key, HeUTF8(entry) ? -(I32)len : (I32)len)) continue;
d056e33c
FC
1221
1222 /* We get here only if this stash has no corresponding
1223 entry in the stash being replaced. */
1224
1225 substash = GvHV(HeVAL(entry));
35759254 1226 if(substash) {
d7879cf0 1227 SV *subname;
62c1e33f
FC
1228
1229 /* Avoid checking main::main::main::... */
1230 if(substash == stash) continue;
1231
d056e33c 1232 /* Add :: and the key (minus the trailing ::)
d7879cf0
FC
1233 to each name. */
1234 if(SvTYPE(namesv) == SVt_PVAV) {
1235 SV *aname;
1236 items = AvFILLp((AV *)namesv) + 1;
1237 svp = AvARRAY((AV *)namesv);
1238 subname = sv_2mortal((SV *)newAV());
1239 while (items--) {
1240 aname = newSVsv(*svp++);
1f656fcf
FC
1241 if (len == 1)
1242 sv_catpvs(aname, ":");
1243 else {
1244 sv_catpvs(aname, "::");
c682ebef
FC
1245 sv_catpvn_flags(
1246 aname, key, len-2,
090c3c37 1247 HeUTF8(entry)
c682ebef
FC
1248 ? SV_CATUTF8 : SV_CATBYTES
1249 );
1f656fcf 1250 }
d7879cf0
FC
1251 av_push((AV *)subname, aname);
1252 }
1253 }
1254 else {
1255 subname = sv_2mortal(newSVsv(namesv));
1f656fcf
FC
1256 if (len == 1) sv_catpvs(subname, ":");
1257 else {
1258 sv_catpvs(subname, "::");
c682ebef
FC
1259 sv_catpvn_flags(
1260 subname, key, len-2,
090c3c37 1261 HeUTF8(entry) ? SV_CATUTF8 : SV_CATBYTES
c682ebef 1262 );
1f656fcf 1263 }
d7879cf0 1264 }
80ebaca2 1265 mro_gather_and_rename(
b89cdb22 1266 stashes, seen_stashes,
d7879cf0 1267 substash, NULL, subname
d056e33c
FC
1268 );
1269 }
c8bbf675
FC
1270 }
1271 }
1272 }
1273 }
1274}
1275
1276/*
e1a479c5
BB
1277=for apidoc mro_method_changed_in
1278
47c9dd14
BB
1279Invalidates method caching on any child classes
1280of the given stash, so that they might notice
1281the changes in this one.
e1a479c5
BB
1282
1283Ideally, all instances of C<PL_sub_generation++> in
25d585df 1284perl source outside of F<mro.c> should be
dd69841b
BB
1285replaced by calls to this.
1286
1287Perl automatically handles most of the common
1288ways a method might be redefined. However, there
1289are a few ways you could change a method in a stash
1290without the cache code noticing, in which case you
1291need to call this method afterwards:
e1a479c5 1292
dd69841b
BB
12931) Directly manipulating the stash HV entries from
1294XS code.
e1a479c5 1295
dd69841b
BB
12962) Assigning a reference to a readonly scalar
1297constant into a stash entry in order to create
75af9d73 1298a constant subroutine (like F<constant.pm>
dd69841b
BB
1299does).
1300
1301This same method is available from pure perl
1302via, C<mro::method_changed_in(classname)>.
e1a479c5
BB
1303
1304=cut
1305*/
1306void
1307Perl_mro_method_changed_in(pTHX_ HV *stash)
1308{
00169e2c
FC
1309 const char * const stashname = HvENAME_get(stash);
1310 const STRLEN stashname_len = HvENAMELEN_get(stash);
dd69841b 1311
3386c920 1312 SV ** const svp = hv_fetchhek(PL_isarev, HvENAME_HEK(stash), 0);
85fbaab2 1313 HV * const isarev = svp ? MUTABLE_HV(*svp) : NULL;
e1a479c5 1314
7918f24d
NC
1315 PERL_ARGS_ASSERT_MRO_METHOD_CHANGED_IN;
1316
2c7f4b87
BB
1317 if(!stashname)
1318 Perl_croak(aTHX_ "Can't call mro_method_changed_in() on anonymous symbol table");
1319
70cd14a1
CB
1320 /* Inc the package generation, since a local method changed */
1321 HvMROMETA(stash)->pkg_gen++;
1322
8c34e50d
FC
1323 /* DESTROY can be cached in SvSTASH. */
1324 if (!SvOBJECT(stash)) SvSTASH(stash) = NULL;
1325
e1a479c5
BB
1326 /* If stash is UNIVERSAL, or one of UNIVERSAL's parents,
1327 invalidate all method caches globally */
dd69841b
BB
1328 if((stashname_len == 9 && strEQ(stashname, "UNIVERSAL"))
1329 || (isarev && hv_exists(isarev, "UNIVERSAL", 9))) {
e1a479c5
BB
1330 PL_sub_generation++;
1331 return;
1332 }
1333
1334 /* else, invalidate the method caches of all child classes,
1335 but not itself */
dd69841b 1336 if(isarev) {
1e05feb3
AL
1337 HE* iter;
1338
e1a479c5
BB
1339 hv_iterinit(isarev);
1340 while((iter = hv_iternext(isarev))) {
204e6232 1341 HV* const revstash = gv_stashsv(hv_iterkeysv(iter), 0);
bc2cbbac
BB
1342 struct mro_meta* mrometa;
1343
1344 if(!revstash) continue;
1345 mrometa = HvMROMETA(revstash);
dd69841b 1346 mrometa->cache_gen++;
e1a479c5
BB
1347 if(mrometa->mro_nextmethod)
1348 hv_clear(mrometa->mro_nextmethod);
c07f9fb2 1349 if (!SvOBJECT(revstash)) SvSTASH(revstash) = NULL;
e1a479c5
BB
1350 }
1351 }
234df27b
FC
1352
1353 /* The method change may be due to *{$package . "::()"} = \&nil; in
1354 overload.pm. */
1355 HvAMAGIC_on(stash);
3d147ac2
DM
1356 /* pessimise derefs for now. Will get recalculated by Gv_AMupdate() */
1357 HvAUX(stash)->xhv_aux_flags &= ~HvAUXf_NO_DEREF;
e1a479c5
BB
1358}
1359
31b9005d
NC
1360void
1361Perl_mro_set_mro(pTHX_ struct mro_meta *const meta, SV *const name)
1362{
1363 const struct mro_alg *const which = Perl_mro_get_from_name(aTHX_ name);
8b371338 1364
31b9005d
NC
1365 PERL_ARGS_ASSERT_MRO_SET_MRO;
1366
1367 if (!which)
1368 Perl_croak(aTHX_ "Invalid mro name: '%"SVf"'", name);
1369
1370 if(meta->mro_which != which) {
9953ff72 1371 if (meta->mro_linear_current && !meta->mro_linear_all) {
31b9005d
NC
1372 /* If we were storing something directly, put it in the hash before
1373 we lose it. */
8b371338 1374 Perl_mro_set_private_data(aTHX_ meta, meta->mro_which,
3a6fa573 1375 MUTABLE_SV(meta->mro_linear_current));
31b9005d
NC
1376 }
1377 meta->mro_which = which;
1378 /* Scrub our cached pointer to the private data. */
3a6fa573 1379 meta->mro_linear_current = NULL;
31b9005d
NC
1380 /* Only affects local method cache, not
1381 even child classes */
1382 meta->cache_gen++;
1383 if(meta->mro_nextmethod)
1384 hv_clear(meta->mro_nextmethod);
1385 }
1386}
1387
e1a479c5
BB
1388#include "XSUB.h"
1389
e1a479c5 1390XS(XS_mro_method_changed_in);
e1a479c5
BB
1391
1392void
1393Perl_boot_core_mro(pTHX)
1394{
e1a479c5
BB
1395 static const char file[] = __FILE__;
1396
a3e6e81e 1397 Perl_mro_register(aTHX_ &dfs_alg);
a3e6e81e 1398
e1a479c5 1399 newXSproto("mro::method_changed_in", XS_mro_method_changed_in, file, "$");
e1a479c5
BB
1400}
1401
e1a479c5
BB
1402XS(XS_mro_method_changed_in)
1403{
e1a479c5
BB
1404 dXSARGS;
1405 SV* classname;
1406 HV* class_stash;
1407
e1a479c5 1408 if(items != 1)
afa74d42 1409 croak_xs_usage(cv, "classname");
8b371338 1410
e1a479c5
BB
1411 classname = ST(0);
1412
1413 class_stash = gv_stashsv(classname, 0);
1414 if(!class_stash) Perl_croak(aTHX_ "No such class: '%"SVf"'!", SVfARG(classname));
1415
1416 mro_method_changed_in(class_stash);
1417
1418 XSRETURN_EMPTY;
1419}
1420
e1a479c5 1421/*
14d04a33 1422 * ex: set ts=8 sts=4 sw=4 et:
e1a479c5 1423 */