forked from schacon/perl
/
sv.c
12457 lines (10975 loc) · 309 KB
/
sv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* sv.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
* 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
* "I wonder what the Entish is for 'yes' and 'no'," he thought.
*
*
* This file contains the code that creates, manipulates and destroys
* scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
* structure of an SV, so their creation and destruction is handled
* here; higher-level functions are in av.c, hv.c, and so on. Opcode
* level functions (eg. substr, split, join) for each of the types are
* in the pp*.c files.
*/
#include "EXTERN.h"
#define PERL_IN_SV_C
#include "perl.h"
#include "regcomp.h"
#define FCALL *f
#ifdef __Lynx__
/* Missing proto on LynxOS */
char *gconvert(double, int, int, char *);
#endif
#ifdef PERL_UTF8_CACHE_ASSERT
/* The cache element 0 is the Unicode offset;
* the cache element 1 is the byte offset of the element 0;
* the cache element 2 is the Unicode length of the substring;
* the cache element 3 is the byte length of the substring;
* The checking of the substring side would be good
* but substr() has enough code paths to make my head spin;
* if adding more checks watch out for the following tests:
* t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
* lib/utf8.t lib/Unicode/Collate/t/index.t
* --jhi
*/
#define ASSERT_UTF8_CACHE(cache) \
STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); } } STMT_END
#else
#define ASSERT_UTF8_CACHE(cache) NOOP
#endif
#ifdef PERL_COPY_ON_WRITE
#define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
#define SV_COW_NEXT_SV_SET(current,next) SvUVX(current) = PTR2UV(next)
/* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
on-write. */
#endif
/* ============================================================================
=head1 Allocation and deallocation of SVs.
An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
av, hv...) contains type and reference count information, as well as a
pointer to the body (struct xrv, xpv, xpviv...), which contains fields
specific to each type.
Normally, this allocation is done using arenas, which are approximately
1K chunks of memory parcelled up into N heads or bodies. The first slot
in each arena is reserved, and is used to hold a link to the next arena.
In the case of heads, the unused first slot also contains some flags and
a note of the number of slots. Snaked through each arena chain is a
linked list of free items; when this becomes empty, an extra arena is
allocated and divided up into N items which are threaded into the free
list.
The following global variables are associated with arenas:
PL_sv_arenaroot pointer to list of SV arenas
PL_sv_root pointer to list of free SV structures
PL_foo_arenaroot pointer to list of foo arenas,
PL_foo_root pointer to list of free foo bodies
... for foo in xiv, xnv, xrv, xpv etc.
Note that some of the larger and more rarely used body types (eg xpvio)
are not allocated using arenas, but are instead just malloc()/free()ed as
required. Also, if PURIFY is defined, arenas are abandoned altogether,
with all items individually malloc()ed. In addition, a few SV heads are
not allocated from an arena, but are instead directly created as static
or auto variables, eg PL_sv_undef.
The SV arena serves the secondary purpose of allowing still-live SVs
to be located and destroyed during final cleanup.
At the lowest level, the macros new_SV() and del_SV() grab and free
an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
to return the SV to the free list with error checking.) new_SV() calls
more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
SVs in the free list have their SvTYPE field set to all ones.
Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
that allocate and return individual body types. Normally these are mapped
to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
instead mapped directly to malloc()/free() if PURIFY is defined. The
new/del functions remove from, or add to, the appropriate PL_foo_root
list, and call more_xiv() etc to add a new arena if the list is empty.
At the time of very final cleanup, sv_free_arenas() is called from
perl_destruct() to physically free all the arenas allocated since the
start of the interpreter. Note that this also clears PL_he_arenaroot,
which is otherwise dealt with in hv.c.
Manipulation of any of the PL_*root pointers is protected by enclosing
LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
if threads are enabled.
The function visit() scans the SV arenas list, and calls a specified
function for each SV it finds which is still live - ie which has an SvTYPE
other than all 1's, and a non-zero SvREFCNT. visit() is used by the
following functions (specified as [function that calls visit()] / [function
called by visit() for each SV]):
sv_report_used() / do_report_used()
dump all remaining SVs (debugging aid)
sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
Attempt to free all objects pointed to by RVs,
and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
try to do the same for all objects indirectly
referenced by typeglobs too. Called once from
perl_destruct(), prior to calling sv_clean_all()
below.
sv_clean_all() / do_clean_all()
SvREFCNT_dec(sv) each remaining SV, possibly
triggering an sv_free(). It also sets the
SVf_BREAK flag on the SV to indicate that the
refcnt has been artificially lowered, and thus
stopping sv_free() from giving spurious warnings
about SVs which unexpectedly have a refcnt
of zero. called repeatedly from perl_destruct()
until there are no SVs left.
=head2 Summary
Private API to rest of sv.c
new_SV(), del_SV(),
new_XIV(), del_XIV(),
new_XNV(), del_XNV(),
etc
Public API:
sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
=cut
============================================================================ */
/*
* "A time to plant, and a time to uproot what was planted..."
*/
#ifdef DEBUG_LEAKING_SCALARS
# ifdef NETWARE
# define FREE_SV_DEBUG_FILE(sv) PerlMemfree((sv)->sv_debug_file)
# else
# define FREE_SV_DEBUG_FILE(sv) PerlMemShared_free((sv)->sv_debug_file)
# endif
#else
# define FREE_SV_DEBUG_FILE(sv)
#endif
#define plant_SV(p) \
STMT_START { \
FREE_SV_DEBUG_FILE(p); \
SvANY(p) = (void *)PL_sv_root; \
SvFLAGS(p) = SVTYPEMASK; \
PL_sv_root = (p); \
--PL_sv_count; \
} STMT_END
/* sv_mutex must be held while calling uproot_SV() */
#define uproot_SV(p) \
STMT_START { \
(p) = PL_sv_root; \
PL_sv_root = (SV*)SvANY(p); \
++PL_sv_count; \
} STMT_END
/* new_SV(): return a new, empty SV head */
#ifdef DEBUG_LEAKING_SCALARS
/* provide a real function for a debugger to play with */
STATIC SV*
S_new_SV(pTHX)
{
SV* sv;
LOCK_SV_MUTEX;
if (PL_sv_root)
uproot_SV(sv);
else
sv = more_sv();
UNLOCK_SV_MUTEX;
SvANY(sv) = 0;
SvREFCNT(sv) = 1;
SvFLAGS(sv) = 0;
sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
sv->sv_debug_line = (U16) ((PL_copline == NOLINE) ?
(PL_curcop ? CopLINE(PL_curcop) : 0) : PL_copline);
sv->sv_debug_inpad = 0;
sv->sv_debug_cloned = 0;
# ifdef NETWARE
sv->sv_debug_file = PL_curcop ? savepv(CopFILE(PL_curcop)): NULL;
# else
sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
# endif
return sv;
}
# define new_SV(p) (p)=S_new_SV(aTHX)
#else
# define new_SV(p) \
STMT_START { \
LOCK_SV_MUTEX; \
if (PL_sv_root) \
uproot_SV(p); \
else \
(p) = more_sv(); \
UNLOCK_SV_MUTEX; \
SvANY(p) = 0; \
SvREFCNT(p) = 1; \
SvFLAGS(p) = 0; \
} STMT_END
#endif
/* del_SV(): return an empty SV head to the free list */
#ifdef DEBUGGING
#define del_SV(p) \
STMT_START { \
LOCK_SV_MUTEX; \
if (DEBUG_D_TEST) \
del_sv(p); \
else \
plant_SV(p); \
UNLOCK_SV_MUTEX; \
} STMT_END
STATIC void
S_del_sv(pTHX_ SV *p)
{
if (DEBUG_D_TEST) {
SV* sva;
SV* sv;
SV* svend;
int ok = 0;
for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
sv = sva + 1;
svend = &sva[SvREFCNT(sva)];
if (p >= sv && p < svend)
ok = 1;
}
if (!ok) {
if (ckWARN_d(WARN_INTERNAL))
Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
"Attempt to free non-arena SV: 0x%"UVxf
pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
return;
}
}
plant_SV(p);
}
#else /* ! DEBUGGING */
#define del_SV(p) plant_SV(p)
#endif /* DEBUGGING */
/*
=head1 SV Manipulation Functions
=for apidoc sv_add_arena
Given a chunk of memory, link it to the head of the list of arenas,
and split it into a list of free SVs.
=cut
*/
void
Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
{
SV* sva = (SV*)ptr;
register SV* sv;
register SV* svend;
/* The first SV in an arena isn't an SV. */
SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
SvFLAGS(sva) = flags; /* FAKE if not to be freed */
PL_sv_arenaroot = sva;
PL_sv_root = sva + 1;
svend = &sva[SvREFCNT(sva) - 1];
sv = sva + 1;
while (sv < svend) {
SvANY(sv) = (void *)(SV*)(sv + 1);
SvREFCNT(sv) = 0;
SvFLAGS(sv) = SVTYPEMASK;
sv++;
}
SvANY(sv) = 0;
SvFLAGS(sv) = SVTYPEMASK;
}
/* make some more SVs by adding another arena */
/* sv_mutex must be held while calling more_sv() */
STATIC SV*
S_more_sv(pTHX)
{
register SV* sv;
if (PL_nice_chunk) {
sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
PL_nice_chunk = Nullch;
PL_nice_chunk_size = 0;
}
else {
char *chunk; /* must use New here to match call to */
New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
sv_add_arena(chunk, 1008, 0);
}
uproot_SV(sv);
return sv;
}
/* visit(): call the named function for each non-free SV in the arenas
* whose flags field matches the flags/mask args. */
STATIC I32
S_visit(pTHX_ SVFUNC_t f, U32 flags, U32 mask)
{
SV* sva;
SV* sv;
register SV* svend;
I32 visited = 0;
for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
svend = &sva[SvREFCNT(sva)];
for (sv = sva + 1; sv < svend; ++sv) {
if (SvTYPE(sv) != SVTYPEMASK
&& (sv->sv_flags & mask) == flags
&& SvREFCNT(sv))
{
(FCALL)(aTHX_ sv);
++visited;
}
}
}
return visited;
}
#ifdef DEBUGGING
/* called by sv_report_used() for each live SV */
static void
do_report_used(pTHX_ SV *sv)
{
if (SvTYPE(sv) != SVTYPEMASK) {
PerlIO_printf(Perl_debug_log, "****\n");
sv_dump(sv);
}
}
#endif
/*
=for apidoc sv_report_used
Dump the contents of all SVs not yet freed. (Debugging aid).
=cut
*/
void
Perl_sv_report_used(pTHX)
{
#ifdef DEBUGGING
visit(do_report_used, 0, 0);
#endif
}
/* called by sv_clean_objs() for each live SV */
static void
do_clean_objs(pTHX_ SV *sv)
{
SV* rv;
if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
if (SvWEAKREF(sv)) {
sv_del_backref(sv);
SvWEAKREF_off(sv);
SvRV(sv) = 0;
} else {
SvROK_off(sv);
SvRV(sv) = 0;
SvREFCNT_dec(rv);
}
}
/* XXX Might want to check arrays, etc. */
}
/* called by sv_clean_objs() for each live SV */
#ifndef DISABLE_DESTRUCTOR_KLUDGE
static void
do_clean_named_objs(pTHX_ SV *sv)
{
if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
if ( SvOBJECT(GvSV(sv)) ||
(GvAV(sv) && SvOBJECT(GvAV(sv))) ||
(GvHV(sv) && SvOBJECT(GvHV(sv))) ||
(GvIO(sv) && SvOBJECT(GvIO(sv))) ||
(GvCV(sv) && SvOBJECT(GvCV(sv))) )
{
DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
SvFLAGS(sv) |= SVf_BREAK;
SvREFCNT_dec(sv);
}
}
}
#endif
/*
=for apidoc sv_clean_objs
Attempt to destroy all objects not yet freed
=cut
*/
void
Perl_sv_clean_objs(pTHX)
{
PL_in_clean_objs = TRUE;
visit(do_clean_objs, SVf_ROK, SVf_ROK);
#ifndef DISABLE_DESTRUCTOR_KLUDGE
/* some barnacles may yet remain, clinging to typeglobs */
visit(do_clean_named_objs, SVt_PVGV, SVTYPEMASK);
#endif
PL_in_clean_objs = FALSE;
}
/* called by sv_clean_all() for each live SV */
static void
do_clean_all(pTHX_ SV *sv)
{
DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
SvFLAGS(sv) |= SVf_BREAK;
if (PL_comppad == (AV*)sv) {
PL_comppad = Nullav;
PL_curpad = Null(SV**);
}
SvREFCNT_dec(sv);
}
/*
=for apidoc sv_clean_all
Decrement the refcnt of each remaining SV, possibly triggering a
cleanup. This function may have to be called multiple times to free
SVs which are in complex self-referential hierarchies.
=cut
*/
I32
Perl_sv_clean_all(pTHX)
{
I32 cleaned;
PL_in_clean_all = TRUE;
cleaned = visit(do_clean_all, 0,0);
PL_in_clean_all = FALSE;
return cleaned;
}
/*
=for apidoc sv_free_arenas
Deallocate the memory used by all arenas. Note that all the individual SV
heads and bodies within the arenas must already have been freed.
=cut
*/
void
Perl_sv_free_arenas(pTHX)
{
SV* sva;
SV* svanext;
XPV *arena, *arenanext;
/* Free arenas here, but be careful about fake ones. (We assume
contiguity of the fake ones with the corresponding real ones.) */
for (sva = PL_sv_arenaroot; sva; sva = svanext) {
svanext = (SV*) SvANY(sva);
while (svanext && SvFAKE(svanext))
svanext = (SV*) SvANY(svanext);
if (!SvFAKE(sva))
Safefree((void *)sva);
}
for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xiv_arenaroot = 0;
PL_xiv_root = 0;
for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xnv_arenaroot = 0;
PL_xnv_root = 0;
for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xrv_arenaroot = 0;
PL_xrv_root = 0;
for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xpv_arenaroot = 0;
PL_xpv_root = 0;
for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xpviv_arenaroot = 0;
PL_xpviv_root = 0;
for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xpvnv_arenaroot = 0;
PL_xpvnv_root = 0;
for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xpvcv_arenaroot = 0;
PL_xpvcv_root = 0;
for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xpvav_arenaroot = 0;
PL_xpvav_root = 0;
for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xpvhv_arenaroot = 0;
PL_xpvhv_root = 0;
for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xpvmg_arenaroot = 0;
PL_xpvmg_root = 0;
for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xpvlv_arenaroot = 0;
PL_xpvlv_root = 0;
for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_xpvbm_arenaroot = 0;
PL_xpvbm_root = 0;
for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
arenanext = (XPV*)arena->xpv_pv;
Safefree(arena);
}
PL_he_arenaroot = 0;
PL_he_root = 0;
if (PL_nice_chunk)
Safefree(PL_nice_chunk);
PL_nice_chunk = Nullch;
PL_nice_chunk_size = 0;
PL_sv_arenaroot = 0;
PL_sv_root = 0;
}
/* ---------------------------------------------------------------------
*
* support functions for report_uninit()
*/
/* the maxiumum size of array or hash where we will scan looking
* for the undefined element that triggered the warning */
#define FUV_MAX_SEARCH_SIZE 1000
/* Look for an entry in the hash whose value has the same SV as val;
* If so, return a mortal copy of the key. */
STATIC SV*
S_find_hash_subscript(pTHX_ HV *hv, SV* val)
{
register HE **array;
register HE *entry;
I32 i;
if (!hv || SvMAGICAL(hv) || !HvARRAY(hv) ||
(HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
return Nullsv;
array = HvARRAY(hv);
for (i=HvMAX(hv); i>0; i--) {
for (entry = array[i]; entry; entry = HeNEXT(entry)) {
if (HeVAL(entry) != val)
continue;
if ( HeVAL(entry) == &PL_sv_undef ||
HeVAL(entry) == &PL_sv_placeholder)
continue;
if (!HeKEY(entry))
return Nullsv;
if (HeKLEN(entry) == HEf_SVKEY)
return sv_mortalcopy(HeKEY_sv(entry));
return sv_2mortal(newSVpvn(HeKEY(entry), HeKLEN(entry)));
}
}
return Nullsv;
}
/* Look for an entry in the array whose value has the same SV as val;
* If so, return the index, otherwise return -1. */
STATIC I32
S_find_array_subscript(pTHX_ AV *av, SV* val)
{
SV** svp;
I32 i;
if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
(AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
return -1;
svp = AvARRAY(av);
for (i=AvFILLp(av); i>=0; i--) {
if (svp[i] == val && svp[i] != &PL_sv_undef)
return i;
}
return -1;
}
/* S_varname(): return the name of a variable, optionally with a subscript.
* If gv is non-zero, use the name of that global, along with gvtype (one
* of "$", "@", "%"); otherwise use the name of the lexical at pad offset
* targ. Depending on the value of the subscript_type flag, return:
*/
#define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
#define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
#define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
#define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
STATIC SV*
S_varname(pTHX_ GV *gv, const char *gvtype, PADOFFSET targ,
SV* keyname, I32 aindex, int subscript_type)
{
AV *av;
SV *sv, *name;
name = sv_newmortal();
if (gv) {
/* simulate gv_fullname4(), but add literal '^' for $^FOO names
* XXX get rid of all this if gv_fullnameX() ever supports this
* directly */
const char *p;
HV *hv = GvSTASH(gv);
sv_setpv(name, gvtype);
if (!hv)
p = "???";
else if (!(p=HvNAME(hv)))
p = "__ANON__";
if (strNE(p, "main")) {
sv_catpv(name,p);
sv_catpvn(name,"::", 2);
}
if (GvNAMELEN(gv)>= 1 &&
((unsigned int)*GvNAME(gv)) <= 26)
{ /* handle $^FOO */
Perl_sv_catpvf(aTHX_ name,"^%c", *GvNAME(gv) + 'A' - 1);
sv_catpvn(name,GvNAME(gv)+1,GvNAMELEN(gv)-1);
}
else
sv_catpvn(name,GvNAME(gv),GvNAMELEN(gv));
}
else {
U32 u;
CV *cv = find_runcv(&u);
if (!cv || !CvPADLIST(cv))
return Nullsv;;
av = (AV*)(*av_fetch(CvPADLIST(cv), 0, FALSE));
sv = *av_fetch(av, targ, FALSE);
/* SvLEN in a pad name is not to be trusted */
sv_setpv(name, SvPV_nolen(sv));
}
if (subscript_type == FUV_SUBSCRIPT_HASH) {
*SvPVX(name) = '$';
sv = NEWSV(0,0);
Perl_sv_catpvf(aTHX_ name, "{%s}",
pv_display(sv,SvPVX(keyname), SvCUR(keyname), 0, 32));
SvREFCNT_dec(sv);
}
else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
*SvPVX(name) = '$';
Perl_sv_catpvf(aTHX_ name, "[%"IVdf"]", (IV)aindex);
}
else if (subscript_type == FUV_SUBSCRIPT_WITHIN)
sv_insert(name, 0, 0, "within ", 7);
return name;
}
/*
=for apidoc find_uninit_var
Find the name of the undefined variable (if any) that caused the operator o
to issue a "Use of uninitialized value" warning.
If match is true, only return a name if it's value matches uninit_sv.
So roughly speaking, if a unary operator (such as OP_COS) generates a
warning, then following the direct child of the op may yield an
OP_PADSV or OP_GV that gives the name of the undefined variable. On the
other hand, with OP_ADD there are two branches to follow, so we only print
the variable name if we get an exact match.
The name is returned as a mortal SV.
Assumes that PL_op is the op that originally triggered the error, and that
PL_comppad/PL_curpad points to the currently executing pad.
=cut
*/
STATIC SV *
S_find_uninit_var(pTHX_ OP* obase, SV* uninit_sv, bool match)
{
SV *sv;
AV *av;
SV **svp;
GV *gv;
OP *o, *o2, *kid;
if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
uninit_sv == &PL_sv_placeholder)))
return Nullsv;
switch (obase->op_type) {
case OP_RV2AV:
case OP_RV2HV:
case OP_PADAV:
case OP_PADHV:
{
bool pad = (obase->op_type == OP_PADAV || obase->op_type == OP_PADHV);
bool hash = (obase->op_type == OP_PADHV || obase->op_type == OP_RV2HV);
I32 index = 0;
SV *keysv = Nullsv;
int subscript_type = FUV_SUBSCRIPT_WITHIN;
if (pad) { /* @lex, %lex */
sv = PAD_SVl(obase->op_targ);
gv = Nullgv;
}
else {
if (cUNOPx(obase)->op_first->op_type == OP_GV) {
/* @global, %global */
gv = cGVOPx_gv(cUNOPx(obase)->op_first);
if (!gv)
break;
sv = hash ? (SV*)GvHV(gv): (SV*)GvAV(gv);
}
else /* @{expr}, %{expr} */
return find_uninit_var(cUNOPx(obase)->op_first,
uninit_sv, match);
}
/* attempt to find a match within the aggregate */
if (hash) {
keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
if (keysv)
subscript_type = FUV_SUBSCRIPT_HASH;
}
else {
index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
if (index >= 0)
subscript_type = FUV_SUBSCRIPT_ARRAY;
}
if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
break;
return S_varname(aTHX_ gv, hash ? "%" : "@", obase->op_targ,
keysv, index, subscript_type);
}
case OP_PADSV:
if (match && PAD_SVl(obase->op_targ) != uninit_sv)
break;
return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
Nullsv, 0, FUV_SUBSCRIPT_NONE);
case OP_GVSV:
gv = cGVOPx_gv(obase);
if (!gv || (match && GvSV(gv) != uninit_sv))
break;
return S_varname(aTHX_ gv, "$", 0, Nullsv, 0, FUV_SUBSCRIPT_NONE);
case OP_AELEMFAST:
if (obase->op_flags & OPf_SPECIAL) { /* lexical array */
if (match) {
av = (AV*)PAD_SV(obase->op_targ);
if (!av || SvRMAGICAL(av))
break;
svp = av_fetch(av, (I32)obase->op_private, FALSE);
if (!svp || *svp != uninit_sv)
break;
}
return S_varname(aTHX_ Nullgv, "$", obase->op_targ,
Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
}
else {
gv = cGVOPx_gv(obase);
if (!gv)
break;
if (match) {
av = GvAV(gv);
if (!av || SvRMAGICAL(av))
break;
svp = av_fetch(av, (I32)obase->op_private, FALSE);
if (!svp || *svp != uninit_sv)
break;
}
return S_varname(aTHX_ gv, "$", 0,
Nullsv, (I32)obase->op_private, FUV_SUBSCRIPT_ARRAY);
}
break;
case OP_EXISTS:
o = cUNOPx(obase)->op_first;
if (!o || o->op_type != OP_NULL ||
! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
break;
return find_uninit_var(cBINOPo->op_last, uninit_sv, match);
case OP_AELEM:
case OP_HELEM:
if (PL_op == obase)
/* $a[uninit_expr] or $h{uninit_expr} */
return find_uninit_var(cBINOPx(obase)->op_last, uninit_sv, match);
gv = Nullgv;
o = cBINOPx(obase)->op_first;
kid = cBINOPx(obase)->op_last;
/* get the av or hv, and optionally the gv */
sv = Nullsv;
if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
sv = PAD_SV(o->op_targ);
}
else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
&& cUNOPo->op_first->op_type == OP_GV)
{
gv = cGVOPx_gv(cUNOPo->op_first);
if (!gv)
break;
sv = o->op_type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)GvAV(gv);
}
if (!sv)
break;
if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
/* index is constant */
if (match) {
if (SvMAGICAL(sv))
break;
if (obase->op_type == OP_HELEM) {
HE* he = hv_fetch_ent((HV*)sv, cSVOPx_sv(kid), 0, 0);
if (!he || HeVAL(he) != uninit_sv)
break;
}
else {
svp = av_fetch((AV*)sv, SvIV(cSVOPx_sv(kid)), FALSE);
if (!svp || *svp != uninit_sv)
break;
}
}
if (obase->op_type == OP_HELEM)
return S_varname(aTHX_ gv, "%", o->op_targ,
cSVOPx_sv(kid), 0, FUV_SUBSCRIPT_HASH);
else
return S_varname(aTHX_ gv, "@", o->op_targ, Nullsv,
SvIV(cSVOPx_sv(kid)), FUV_SUBSCRIPT_ARRAY);
;
}
else {
/* index is an expression;
* attempt to find a match within the aggregate */
if (obase->op_type == OP_HELEM) {
SV *keysv = S_find_hash_subscript(aTHX_ (HV*)sv, uninit_sv);
if (keysv)
return S_varname(aTHX_ gv, "%", o->op_targ,
keysv, 0, FUV_SUBSCRIPT_HASH);
}
else {
I32 index = S_find_array_subscript(aTHX_ (AV*)sv, uninit_sv);
if (index >= 0)
return S_varname(aTHX_ gv, "@", o->op_targ,
Nullsv, index, FUV_SUBSCRIPT_ARRAY);
}
if (match)
break;
return S_varname(aTHX_ gv,
(o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
? "@" : "%",
o->op_targ, Nullsv, 0, FUV_SUBSCRIPT_WITHIN);
}
break;
case OP_AASSIGN:
/* only examine RHS */
return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv, match);
case OP_OPEN:
o = cUNOPx(obase)->op_first;
if (o->op_type == OP_PUSHMARK)
o = o->op_sibling;
if (!o->op_sibling) {
/* one-arg version of open is highly magical */
if (o->op_type == OP_GV) { /* open FOO; */
gv = cGVOPx_gv(o);
if (match && GvSV(gv) != uninit_sv)
break;
return S_varname(aTHX_ gv, "$", 0,
Nullsv, 0, FUV_SUBSCRIPT_NONE);
}
/* other possibilities not handled are:
* open $x; or open my $x; should return '${*$x}'
* open expr; should return '$'.expr ideally
*/
break;
}