forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpad.c
2547 lines (2143 loc) · 71.6 KB
/
pad.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
/* pad.c
*
* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
* 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.
*/
/*
* 'Anyway: there was this Mr. Frodo left an orphan and stranded, as you
* might say, among those queer Bucklanders, being brought up anyhow in
* Brandy Hall. A regular warren, by all accounts. Old Master Gorbadoc
* never had fewer than a couple of hundred relations in the place.
* Mr. Bilbo never did a kinder deed than when he brought the lad back
* to live among decent folk.' --the Gaffer
*
* [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
*/
/* XXX DAPM
* As of Sept 2002, this file is new and may be in a state of flux for
* a while. I've marked things I intent to come back and look at further
* with an 'XXX DAPM' comment.
*/
/*
=head1 Pad Data Structures
=for apidoc Amx|PADLIST *|CvPADLIST|CV *cv
CV's can have CvPADLIST(cv) set to point to a PADLIST. This is the CV's
scratchpad, which stores lexical variables and opcode temporary and
per-thread values.
For these purposes "formats" are a kind-of CV; eval""s are too (except they're
not callable at will and are always thrown away after the eval"" is done
executing). Require'd files are simply evals without any outer lexical
scope.
XSUBs don't have CvPADLIST set - dXSTARG fetches values from PL_curpad,
but that is really the callers pad (a slot of which is allocated by
every entersub).
The PADLIST has a C array where pads are stored.
The 0th entry of the PADLIST is a PADNAMELIST (which is actually just an
AV, but that may change) which represents the "names" or rather
the "static type information" for lexicals. The individual elements of a
PADNAMELIST are PADNAMEs (just SVs; but, again, that may change). Future
refactorings might stop the PADNAMELIST from being stored in the PADLIST's
array, so don't rely on it. See L</PadlistNAMES>.
The CvDEPTH'th entry of a PADLIST is a PAD (an AV) which is the stack frame
at that depth of recursion into the CV. The 0th slot of a frame AV is an
AV which is @_. Other entries are storage for variables and op targets.
Iterating over the PADNAMELIST iterates over all possible pad
items. Pad slots for targets (SVs_PADTMP) and GVs end up having &PL_sv_no
"names", while slots for constants have &PL_sv_no "names" (see
pad_alloc()). That &PL_sv_no is used is an implementation detail subject
to change. To test for it, use C<PadnamePV(name) && !PadnameLEN(name)>.
Only my/our variable (SvPADMY/PADNAME_isOUR) slots get valid names.
The rest are op targets/GVs/constants which are statically allocated
or resolved at compile time. These don't have names by which they
can be looked up from Perl code at run time through eval"" the way
my/our variables can be. Since they can't be looked up by "name"
but only by their index allocated at compile time (which is usually
in PL_op->op_targ), wasting a name SV for them doesn't make sense.
The SVs in the names AV have their PV being the name of the variable.
xlow+1..xhigh inclusive in the NV union is a range of cop_seq numbers for
which the name is valid (accessed through the macros COP_SEQ_RANGE_LOW and
_HIGH). During compilation, these fields may hold the special value
PERL_PADSEQ_INTRO to indicate various stages:
COP_SEQ_RANGE_LOW _HIGH
----------------- -----
PERL_PADSEQ_INTRO 0 variable not yet introduced: { my ($x
valid-seq# PERL_PADSEQ_INTRO variable in scope: { my ($x)
valid-seq# valid-seq# compilation of scope complete: { my ($x) }
For typed lexicals name SV is SVt_PVMG and SvSTASH
points at the type. For C<our> lexicals, the type is also SVt_PVMG, with the
SvOURSTASH slot pointing at the stash of the associated global (so that
duplicate C<our> declarations in the same package can be detected). SvUVX is
sometimes hijacked to store the generation number during compilation.
If PADNAME_OUTER (SvFAKE) is set on the
name SV, then that slot in the frame AV is
a REFCNT'ed reference to a lexical from "outside". In this case,
the name SV does not use xlow and xhigh to store a cop_seq range, since it is
in scope throughout. Instead xhigh stores some flags containing info about
the real lexical (is it declared in an anon, and is it capable of being
instantiated multiple times?), and for fake ANONs, xlow contains the index
within the parent's pad where the lexical's value is stored, to make
cloning quicker.
If the 'name' is '&' the corresponding entry in the PAD
is a CV representing a possible closure.
(PADNAME_OUTER and name of '&' is not a
meaningful combination currently but could
become so if C<my sub foo {}> is implemented.)
Note that formats are treated as anon subs, and are cloned each time
write is called (if necessary).
The flag SVs_PADSTALE is cleared on lexicals each time the my() is executed,
and set on scope exit. This allows the
'Variable $x is not available' warning
to be generated in evals, such as
{ my $x = 1; sub f { eval '$x'} } f();
For state vars, SVs_PADSTALE is overloaded to mean 'not yet initialised'.
=for apidoc AmxU|PADNAMELIST *|PL_comppad_name
During compilation, this points to the array containing the names part
of the pad for the currently-compiling code.
=for apidoc AmxU|PAD *|PL_comppad
During compilation, this points to the array containing the values
part of the pad for the currently-compiling code. (At runtime a CV may
have many such value arrays; at compile time just one is constructed.)
At runtime, this points to the array containing the currently-relevant
values for the pad for the currently-executing code.
=for apidoc AmxU|SV **|PL_curpad
Points directly to the body of the L</PL_comppad> array.
(I.e., this is C<PAD_ARRAY(PL_comppad)>.)
=cut
*/
#include "EXTERN.h"
#define PERL_IN_PAD_C
#include "perl.h"
#include "keywords.h"
#define COP_SEQ_RANGE_LOW_set(sv,val) \
STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xlow = (val); } STMT_END
#define COP_SEQ_RANGE_HIGH_set(sv,val) \
STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xhigh = (val); } STMT_END
#define PARENT_PAD_INDEX_set(sv,val) \
STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xlow = (val); } STMT_END
#define PARENT_FAKELEX_FLAGS_set(sv,val) \
STMT_START { ((XPVNV*)SvANY(sv))->xnv_u.xpad_cop_seq.xhigh = (val); } STMT_END
/*
=for apidoc mx|void|pad_peg|const char *s
When PERL_MAD is enabled, this is a small no-op function that gets called
at the start of each pad-related function. It can be breakpointed to
track all pad operations. The parameter is a string indicating the type
of pad operation being performed.
=cut
*/
#ifdef PERL_MAD
void pad_peg(const char* s) {
static int pegcnt; /* XXX not threadsafe */
PERL_UNUSED_ARG(s);
PERL_ARGS_ASSERT_PAD_PEG;
pegcnt++;
}
#endif
/*
This is basically sv_eq_flags() in sv.c, but we avoid the magic
and bytes checking.
*/
static bool
sv_eq_pvn_flags(pTHX_ const SV *sv, const char* pv, const STRLEN pvlen, const U32 flags) {
if ( (SvUTF8(sv) & SVf_UTF8 ) != (flags & SVf_UTF8) ) {
const char *pv1 = SvPVX_const(sv);
STRLEN cur1 = SvCUR(sv);
const char *pv2 = pv;
STRLEN cur2 = pvlen;
if (PL_encoding) {
SV* svrecode = NULL;
if (SvUTF8(sv)) {
svrecode = newSVpvn(pv2, cur2);
sv_recode_to_utf8(svrecode, PL_encoding);
pv2 = SvPV_const(svrecode, cur2);
}
else {
svrecode = newSVpvn(pv1, cur1);
sv_recode_to_utf8(svrecode, PL_encoding);
pv1 = SvPV_const(svrecode, cur1);
}
SvREFCNT_dec_NN(svrecode);
}
if (flags & SVf_UTF8)
return (bytes_cmp_utf8(
(const U8*)pv1, cur1,
(const U8*)pv2, cur2) == 0);
else
return (bytes_cmp_utf8(
(const U8*)pv2, cur2,
(const U8*)pv1, cur1) == 0);
}
else
return ((SvPVX_const(sv) == pv)
|| memEQ(SvPVX_const(sv), pv, pvlen));
}
/*
=for apidoc Am|PADLIST *|pad_new|int flags
Create a new padlist, updating the global variables for the
currently-compiling padlist to point to the new padlist. The following
flags can be OR'ed together:
padnew_CLONE this pad is for a cloned CV
padnew_SAVE save old globals on the save stack
padnew_SAVESUB also save extra stuff for start of sub
=cut
*/
PADLIST *
Perl_pad_new(pTHX_ int flags)
{
dVAR;
PADLIST *padlist;
PAD *padname, *pad;
PAD **ary;
ASSERT_CURPAD_LEGAL("pad_new");
/* XXX DAPM really need a new SAVEt_PAD which restores all or most
* vars (based on flags) rather than storing vals + addresses for
* each individually. Also see pad_block_start.
* XXX DAPM Try to see whether all these conditionals are required
*/
/* save existing state, ... */
if (flags & padnew_SAVE) {
SAVECOMPPAD();
if (! (flags & padnew_CLONE)) {
SAVESPTR(PL_comppad_name);
SAVEI32(PL_padix);
SAVEI32(PL_comppad_name_fill);
SAVEI32(PL_min_intro_pending);
SAVEI32(PL_max_intro_pending);
SAVEBOOL(PL_cv_has_eval);
if (flags & padnew_SAVESUB) {
SAVEBOOL(PL_pad_reset_pending);
}
}
}
/* XXX DAPM interestingly, PL_comppad_name_floor never seems to be
* saved - check at some pt that this is okay */
/* ... create new pad ... */
Newxz(padlist, 1, PADLIST);
pad = newAV();
if (flags & padnew_CLONE) {
/* XXX DAPM I dont know why cv_clone needs it
* doing differently yet - perhaps this separate branch can be
* dispensed with eventually ???
*/
AV * const a0 = newAV(); /* will be @_ */
av_store(pad, 0, MUTABLE_SV(a0));
AvREIFY_only(a0);
padname = (PAD *)SvREFCNT_inc_simple_NN(PL_comppad_name);
}
else {
av_store(pad, 0, NULL);
padname = newAV();
AvPAD_NAMELIST_on(padname);
av_store(padname, 0, &PL_sv_undef);
}
/* Most subroutines never recurse, hence only need 2 entries in the padlist
array - names, and depth=1. The default for av_store() is to allocate
0..3, and even an explicit call to av_extend() with <3 will be rounded
up, so we inline the allocation of the array here. */
Newx(ary, 2, PAD *);
PadlistMAX(padlist) = 1;
PadlistARRAY(padlist) = ary;
ary[0] = padname;
ary[1] = pad;
/* ... then update state variables */
PL_comppad = pad;
PL_curpad = AvARRAY(pad);
if (! (flags & padnew_CLONE)) {
PL_comppad_name = padname;
PL_comppad_name_fill = 0;
PL_min_intro_pending = 0;
PL_padix = 0;
PL_cv_has_eval = 0;
}
DEBUG_X(PerlIO_printf(Perl_debug_log,
"Pad 0x%"UVxf"[0x%"UVxf"] new: compcv=0x%"UVxf
" name=0x%"UVxf" flags=0x%"UVxf"\n",
PTR2UV(PL_comppad), PTR2UV(PL_curpad), PTR2UV(PL_compcv),
PTR2UV(padname), (UV)flags
)
);
return (PADLIST*)padlist;
}
/*
=head1 Embedding Functions
=for apidoc cv_undef
Clear out all the active components of a CV. This can happen either
by an explicit C<undef &foo>, or by the reference count going to zero.
In the former case, we keep the CvOUTSIDE pointer, so that any anonymous
children can still follow the full lexical scope chain.
=cut
*/
void
Perl_cv_undef(pTHX_ CV *cv)
{
dVAR;
const PADLIST *padlist = CvPADLIST(cv);
bool const slabbed = !!CvSLABBED(cv);
PERL_ARGS_ASSERT_CV_UNDEF;
DEBUG_X(PerlIO_printf(Perl_debug_log,
"CV undef: cv=0x%"UVxf" comppad=0x%"UVxf"\n",
PTR2UV(cv), PTR2UV(PL_comppad))
);
if (CvFILE(cv) && CvDYNFILE(cv)) {
Safefree(CvFILE(cv));
}
CvFILE(cv) = NULL;
CvSLABBED_off(cv);
if (!CvISXSUB(cv) && CvROOT(cv)) {
if (SvTYPE(cv) == SVt_PVCV && CvDEPTH(cv))
Perl_croak(aTHX_ "Can't undef active subroutine");
ENTER;
PAD_SAVE_SETNULLPAD();
if (slabbed) OpslabREFCNT_dec_padok(OpSLAB(CvROOT(cv)));
op_free(CvROOT(cv));
CvROOT(cv) = NULL;
CvSTART(cv) = NULL;
LEAVE;
}
else if (slabbed && CvSTART(cv)) {
ENTER;
PAD_SAVE_SETNULLPAD();
/* discard any leaked ops */
if (PL_parser)
parser_free_nexttoke_ops(PL_parser, (OPSLAB *)CvSTART(cv));
opslab_force_free((OPSLAB *)CvSTART(cv));
CvSTART(cv) = NULL;
LEAVE;
}
#ifdef DEBUGGING
else if (slabbed) Perl_warn(aTHX_ "Slab leaked from cv %p", cv);
#endif
SvPOK_off(MUTABLE_SV(cv)); /* forget prototype */
sv_unmagic((SV *)cv, PERL_MAGIC_checkcall);
if (CvNAMED(cv)) CvNAME_HEK_set(cv, NULL);
else CvGV_set(cv, NULL);
/* This statement and the subsequence if block was pad_undef(). */
pad_peg("pad_undef");
if (padlist) {
I32 ix;
/* Free the padlist associated with a CV.
If parts of it happen to be current, we null the relevant PL_*pad*
global vars so that we don't have any dangling references left.
We also repoint the CvOUTSIDE of any about-to-be-orphaned inner
subs to the outer of this cv. */
DEBUG_X(PerlIO_printf(Perl_debug_log,
"Pad undef: cv=0x%"UVxf" padlist=0x%"UVxf" comppad=0x%"UVxf"\n",
PTR2UV(cv), PTR2UV(padlist), PTR2UV(PL_comppad))
);
/* detach any '&' anon children in the pad; if afterwards they
* are still live, fix up their CvOUTSIDEs to point to our outside,
* bypassing us. */
/* XXX DAPM for efficiency, we should only do this if we know we have
* children, or integrate this loop with general cleanup */
if (PL_phase != PERL_PHASE_DESTRUCT) { /* don't bother during global destruction */
CV * const outercv = CvOUTSIDE(cv);
const U32 seq = CvOUTSIDE_SEQ(cv);
PAD * const comppad_name = PadlistARRAY(padlist)[0];
SV ** const namepad = AvARRAY(comppad_name);
PAD * const comppad = PadlistARRAY(padlist)[1];
SV ** const curpad = AvARRAY(comppad);
for (ix = AvFILLp(comppad_name); ix > 0; ix--) {
SV * const namesv = namepad[ix];
if (namesv && namesv != &PL_sv_undef
&& *SvPVX_const(namesv) == '&')
{
CV * const innercv = MUTABLE_CV(curpad[ix]);
U32 inner_rc = SvREFCNT(innercv);
assert(inner_rc);
assert(SvTYPE(innercv) != SVt_PVFM);
if (SvREFCNT(comppad) < 2) { /* allow for /(?{ sub{} })/ */
curpad[ix] = NULL;
SvREFCNT_dec_NN(innercv);
inner_rc--;
}
/* in use, not just a prototype */
if (inner_rc && (CvOUTSIDE(innercv) == cv)) {
assert(CvWEAKOUTSIDE(innercv));
/* don't relink to grandfather if he's being freed */
if (outercv && SvREFCNT(outercv)) {
CvWEAKOUTSIDE_off(innercv);
CvOUTSIDE(innercv) = outercv;
CvOUTSIDE_SEQ(innercv) = seq;
SvREFCNT_inc_simple_void_NN(outercv);
}
else {
CvOUTSIDE(innercv) = NULL;
}
}
}
}
}
ix = PadlistMAX(padlist);
while (ix > 0) {
PAD * const sv = PadlistARRAY(padlist)[ix--];
if (sv) {
if (sv == PL_comppad) {
PL_comppad = NULL;
PL_curpad = NULL;
}
SvREFCNT_dec_NN(sv);
}
}
{
PAD * const sv = PadlistARRAY(padlist)[0];
if (sv == PL_comppad_name && SvREFCNT(sv) == 1)
PL_comppad_name = NULL;
SvREFCNT_dec(sv);
}
if (PadlistARRAY(padlist)) Safefree(PadlistARRAY(padlist));
Safefree(padlist);
CvPADLIST(cv) = NULL;
}
/* remove CvOUTSIDE unless this is an undef rather than a free */
if (!SvREFCNT(cv) && CvOUTSIDE(cv)) {
if (!CvWEAKOUTSIDE(cv))
SvREFCNT_dec(CvOUTSIDE(cv));
CvOUTSIDE(cv) = NULL;
}
if (CvCONST(cv)) {
SvREFCNT_dec(MUTABLE_SV(CvXSUBANY(cv).any_ptr));
CvCONST_off(cv);
}
if (CvISXSUB(cv) && CvXSUB(cv)) {
CvXSUB(cv) = NULL;
}
/* delete all flags except WEAKOUTSIDE and CVGV_RC, which indicate the
* ref status of CvOUTSIDE and CvGV, and ANON, which pp_entersub uses
* to choose an error message */
CvFLAGS(cv) &= (CVf_WEAKOUTSIDE|CVf_CVGV_RC|CVf_ANON);
}
/*
=for apidoc cv_forget_slab
When a CV has a reference count on its slab (CvSLABBED), it is responsible
for making sure it is freed. (Hence, no two CVs should ever have a
reference count on the same slab.) The CV only needs to reference the slab
during compilation. Once it is compiled and CvROOT attached, it has
finished its job, so it can forget the slab.
=cut
*/
void
Perl_cv_forget_slab(pTHX_ CV *cv)
{
const bool slabbed = !!CvSLABBED(cv);
OPSLAB *slab = NULL;
PERL_ARGS_ASSERT_CV_FORGET_SLAB;
if (!slabbed) return;
CvSLABBED_off(cv);
if (CvROOT(cv)) slab = OpSLAB(CvROOT(cv));
else if (CvSTART(cv)) slab = (OPSLAB *)CvSTART(cv);
#ifdef DEBUGGING
else if (slabbed) Perl_warn(aTHX_ "Slab leaked from cv %p", cv);
#endif
if (slab) {
#ifdef PERL_DEBUG_READONLY_OPS
const size_t refcnt = slab->opslab_refcnt;
#endif
OpslabREFCNT_dec(slab);
#ifdef PERL_DEBUG_READONLY_OPS
if (refcnt > 1) Slab_to_ro(slab);
#endif
}
}
/*
=for apidoc m|PADOFFSET|pad_alloc_name|SV *namesv|U32 flags|HV *typestash|HV *ourstash
Allocates a place in the currently-compiling
pad (via L<perlapi/pad_alloc>) and
then stores a name for that entry. I<namesv> is adopted and becomes the
name entry; it must already contain the name string and be sufficiently
upgraded. I<typestash> and I<ourstash> and the C<padadd_STATE> flag get
added to I<namesv>. None of the other
processing of L<perlapi/pad_add_name_pvn>
is done. Returns the offset of the allocated pad slot.
=cut
*/
static PADOFFSET
S_pad_alloc_name(pTHX_ SV *namesv, U32 flags, HV *typestash, HV *ourstash)
{
dVAR;
const PADOFFSET offset = pad_alloc(OP_PADSV, SVs_PADMY);
PERL_ARGS_ASSERT_PAD_ALLOC_NAME;
ASSERT_CURPAD_ACTIVE("pad_alloc_name");
if (typestash) {
assert(SvTYPE(namesv) == SVt_PVMG);
SvPAD_TYPED_on(namesv);
SvSTASH_set(namesv, MUTABLE_HV(SvREFCNT_inc_simple_NN(MUTABLE_SV(typestash))));
}
if (ourstash) {
SvPAD_OUR_on(namesv);
SvOURSTASH_set(namesv, ourstash);
SvREFCNT_inc_simple_void_NN(ourstash);
}
else if (flags & padadd_STATE) {
SvPAD_STATE_on(namesv);
}
av_store(PL_comppad_name, offset, namesv);
PadnamelistMAXNAMED(PL_comppad_name) = offset;
return offset;
}
/*
=for apidoc Am|PADOFFSET|pad_add_name_pvn|const char *namepv|STRLEN namelen|U32 flags|HV *typestash|HV *ourstash
Allocates a place in the currently-compiling pad for a named lexical
variable. Stores the name and other metadata in the name part of the
pad, and makes preparations to manage the variable's lexical scoping.
Returns the offset of the allocated pad slot.
I<namepv>/I<namelen> specify the variable's name, including leading sigil.
If I<typestash> is non-null, the name is for a typed lexical, and this
identifies the type. If I<ourstash> is non-null, it's a lexical reference
to a package variable, and this identifies the package. The following
flags can be OR'ed together:
padadd_OUR redundantly specifies if it's a package var
padadd_STATE variable will retain value persistently
padadd_NO_DUP_CHECK skip check for lexical shadowing
=cut
*/
PADOFFSET
Perl_pad_add_name_pvn(pTHX_ const char *namepv, STRLEN namelen,
U32 flags, HV *typestash, HV *ourstash)
{
dVAR;
PADOFFSET offset;
SV *namesv;
bool is_utf8;
PERL_ARGS_ASSERT_PAD_ADD_NAME_PVN;
if (flags & ~(padadd_OUR|padadd_STATE|padadd_NO_DUP_CHECK|padadd_UTF8_NAME))
Perl_croak(aTHX_ "panic: pad_add_name_pvn illegal flag bits 0x%" UVxf,
(UV)flags);
namesv = newSV_type((ourstash || typestash) ? SVt_PVMG : SVt_PVNV);
if ((is_utf8 = ((flags & padadd_UTF8_NAME) != 0))) {
namepv = (const char*)bytes_from_utf8((U8*)namepv, &namelen, &is_utf8);
}
sv_setpvn(namesv, namepv, namelen);
if (is_utf8) {
flags |= padadd_UTF8_NAME;
SvUTF8_on(namesv);
}
else
flags &= ~padadd_UTF8_NAME;
if ((flags & padadd_NO_DUP_CHECK) == 0) {
ENTER;
SAVEFREESV(namesv); /* in case of fatal warnings */
/* check for duplicate declaration */
pad_check_dup(namesv, flags & padadd_OUR, ourstash);
SvREFCNT_inc_simple_void_NN(namesv);
LEAVE;
}
offset = pad_alloc_name(namesv, flags & ~padadd_UTF8_NAME, typestash, ourstash);
/* not yet introduced */
COP_SEQ_RANGE_LOW_set(namesv, PERL_PADSEQ_INTRO);
COP_SEQ_RANGE_HIGH_set(namesv, 0);
if (!PL_min_intro_pending)
PL_min_intro_pending = offset;
PL_max_intro_pending = offset;
/* if it's not a simple scalar, replace with an AV or HV */
assert(SvTYPE(PL_curpad[offset]) == SVt_NULL);
assert(SvREFCNT(PL_curpad[offset]) == 1);
if (namelen != 0 && *namepv == '@')
sv_upgrade(PL_curpad[offset], SVt_PVAV);
else if (namelen != 0 && *namepv == '%')
sv_upgrade(PL_curpad[offset], SVt_PVHV);
else if (namelen != 0 && *namepv == '&')
sv_upgrade(PL_curpad[offset], SVt_PVCV);
assert(SvPADMY(PL_curpad[offset]));
DEBUG_Xv(PerlIO_printf(Perl_debug_log,
"Pad addname: %ld \"%s\" new lex=0x%"UVxf"\n",
(long)offset, SvPVX(namesv),
PTR2UV(PL_curpad[offset])));
return offset;
}
/*
=for apidoc Am|PADOFFSET|pad_add_name_pv|const char *name|U32 flags|HV *typestash|HV *ourstash
Exactly like L</pad_add_name_pvn>, but takes a nul-terminated string
instead of a string/length pair.
=cut
*/
PADOFFSET
Perl_pad_add_name_pv(pTHX_ const char *name,
const U32 flags, HV *typestash, HV *ourstash)
{
PERL_ARGS_ASSERT_PAD_ADD_NAME_PV;
return pad_add_name_pvn(name, strlen(name), flags, typestash, ourstash);
}
/*
=for apidoc Am|PADOFFSET|pad_add_name_sv|SV *name|U32 flags|HV *typestash|HV *ourstash
Exactly like L</pad_add_name_pvn>, but takes the name string in the form
of an SV instead of a string/length pair.
=cut
*/
PADOFFSET
Perl_pad_add_name_sv(pTHX_ SV *name, U32 flags, HV *typestash, HV *ourstash)
{
char *namepv;
STRLEN namelen;
PERL_ARGS_ASSERT_PAD_ADD_NAME_SV;
namepv = SvPV(name, namelen);
if (SvUTF8(name))
flags |= padadd_UTF8_NAME;
return pad_add_name_pvn(namepv, namelen, flags, typestash, ourstash);
}
/*
=for apidoc Amx|PADOFFSET|pad_alloc|I32 optype|U32 tmptype
Allocates a place in the currently-compiling pad,
returning the offset of the allocated pad slot.
No name is initially attached to the pad slot.
I<tmptype> is a set of flags indicating the kind of pad entry required,
which will be set in the value SV for the allocated pad entry:
SVs_PADMY named lexical variable ("my", "our", "state")
SVs_PADTMP unnamed temporary store
SVf_READONLY constant shared between recursion levels
C<SVf_READONLY> has been supported here only since perl 5.20. To work with
earlier versions as well, use C<SVf_READONLY|SVs_PADTMP>. C<SVf_READONLY>
does not cause the SV in the pad slot to be marked read-only, but simply
tells C<pad_alloc> that it I<will> be made read-only (by the caller), or at
least should be treated as such.
I<optype> should be an opcode indicating the type of operation that the
pad entry is to support. This doesn't affect operational semantics,
but is used for debugging.
=cut
*/
/* XXX DAPM integrate alloc(), add_name() and add_anon(),
* or at least rationalise ??? */
PADOFFSET
Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
{
dVAR;
SV *sv;
I32 retval;
PERL_UNUSED_ARG(optype);
ASSERT_CURPAD_ACTIVE("pad_alloc");
if (AvARRAY(PL_comppad) != PL_curpad)
Perl_croak(aTHX_ "panic: pad_alloc, %p!=%p",
AvARRAY(PL_comppad), PL_curpad);
if (PL_pad_reset_pending)
pad_reset();
if (tmptype & SVs_PADMY) {
/* For a my, simply push a null SV onto the end of PL_comppad. */
sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
retval = AvFILLp(PL_comppad);
}
else {
/* For a tmp, scan the pad from PL_padix upwards
* for a slot which has no name and no active value.
*/
SV * const * const names = AvARRAY(PL_comppad_name);
const SSize_t names_fill = AvFILLp(PL_comppad_name);
for (;;) {
/*
* Entries that close over unavailable variables
* in outer subs contain values not marked PADMY.
* Thus we must skip, not just pad values that are
* marked as current pad values, but also those with names.
*/
if (++PL_padix <= names_fill &&
(sv = names[PL_padix]) && sv != &PL_sv_undef)
continue;
sv = *av_fetch(PL_comppad, PL_padix, TRUE);
if (!(SvFLAGS(sv) & (SVs_PADTMP | SVs_PADMY)) &&
!IS_PADGV(sv))
break;
}
if (tmptype & SVf_READONLY) {
av_store(PL_comppad_name, PL_padix, &PL_sv_no);
tmptype &= ~SVf_READONLY;
tmptype |= SVs_PADTMP;
}
retval = PL_padix;
}
SvFLAGS(sv) |= tmptype;
PL_curpad = AvARRAY(PL_comppad);
DEBUG_X(PerlIO_printf(Perl_debug_log,
"Pad 0x%"UVxf"[0x%"UVxf"] alloc: %ld for %s\n",
PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long) retval,
PL_op_name[optype]));
#ifdef DEBUG_LEAKING_SCALARS
sv->sv_debug_optype = optype;
sv->sv_debug_inpad = 1;
#endif
return (PADOFFSET)retval;
}
/*
=for apidoc Am|PADOFFSET|pad_add_anon|CV *func|I32 optype
Allocates a place in the currently-compiling pad (via L</pad_alloc>)
for an anonymous function that is lexically scoped inside the
currently-compiling function.
The function I<func> is linked into the pad, and its C<CvOUTSIDE> link
to the outer scope is weakened to avoid a reference loop.
One reference count is stolen, so you may need to do C<SvREFCNT_inc(func)>.
I<optype> should be an opcode indicating the type of operation that the
pad entry is to support. This doesn't affect operational semantics,
but is used for debugging.
=cut
*/
PADOFFSET
Perl_pad_add_anon(pTHX_ CV* func, I32 optype)
{
dVAR;
PADOFFSET ix;
SV* const name = newSV_type(SVt_PVNV);
PERL_ARGS_ASSERT_PAD_ADD_ANON;
pad_peg("add_anon");
sv_setpvs(name, "&");
/* These two aren't used; just make sure they're not equal to
* PERL_PADSEQ_INTRO */
COP_SEQ_RANGE_LOW_set(name, 0);
COP_SEQ_RANGE_HIGH_set(name, 0);
ix = pad_alloc(optype, SVs_PADMY);
av_store(PL_comppad_name, ix, name);
/* XXX DAPM use PL_curpad[] ? */
if (SvTYPE(func) == SVt_PVCV || !CvOUTSIDE(func))
av_store(PL_comppad, ix, (SV*)func);
else {
SV *rv = newRV_noinc((SV *)func);
sv_rvweaken(rv);
assert (SvTYPE(func) == SVt_PVFM);
av_store(PL_comppad, ix, rv);
}
SvPADMY_on((SV*)func);
/* to avoid ref loops, we never have parent + child referencing each
* other simultaneously */
if (CvOUTSIDE(func) && SvTYPE(func) == SVt_PVCV) {
assert(!CvWEAKOUTSIDE(func));
CvWEAKOUTSIDE_on(func);
SvREFCNT_dec_NN(CvOUTSIDE(func));
}
return ix;
}
/*
=for apidoc pad_check_dup
Check for duplicate declarations: report any of:
* a my in the current scope with the same name;
* an our (anywhere in the pad) with the same name and the
same stash as C<ourstash>
C<is_our> indicates that the name to check is an 'our' declaration.
=cut
*/
STATIC void
S_pad_check_dup(pTHX_ SV *name, U32 flags, const HV *ourstash)
{
dVAR;
SV **svp;
PADOFFSET top, off;
const U32 is_our = flags & padadd_OUR;
PERL_ARGS_ASSERT_PAD_CHECK_DUP;
ASSERT_CURPAD_ACTIVE("pad_check_dup");
assert((flags & ~padadd_OUR) == 0);
if (AvFILLp(PL_comppad_name) < 0 || !ckWARN(WARN_MISC))
return; /* nothing to check */
svp = AvARRAY(PL_comppad_name);
top = AvFILLp(PL_comppad_name);
/* check the current scope */
/* XXX DAPM - why the (I32) cast - shouldn't we ensure they're the same
* type ? */
for (off = top; (I32)off > PL_comppad_name_floor; off--) {
SV * const sv = svp[off];
if (sv
&& PadnameLEN(sv)
&& !SvFAKE(sv)
&& ( COP_SEQ_RANGE_LOW(sv) == PERL_PADSEQ_INTRO
|| COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO)
&& sv_eq(name, sv))
{
if (is_our && (SvPAD_OUR(sv)))
break; /* "our" masking "our" */
/* diag_listed_as: "%s" variable %s masks earlier declaration in same %s */
Perl_warner(aTHX_ packWARN(WARN_MISC),
"\"%s\" %s %"SVf" masks earlier declaration in same %s",
(is_our ? "our" : PL_parser->in_my == KEY_my ? "my" : "state"),
*SvPVX(sv) == '&' ? "subroutine" : "variable",
sv,
(COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO
? "scope" : "statement"));
--off;
break;
}
}
/* check the rest of the pad */
if (is_our) {
while (off > 0) {
SV * const sv = svp[off];
if (sv
&& PadnameLEN(sv)
&& !SvFAKE(sv)
&& ( COP_SEQ_RANGE_LOW(sv) == PERL_PADSEQ_INTRO
|| COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO)
&& SvOURSTASH(sv) == ourstash
&& sv_eq(name, sv))
{
Perl_warner(aTHX_ packWARN(WARN_MISC),
"\"our\" variable %"SVf" redeclared", sv);
if ((I32)off <= PL_comppad_name_floor)
Perl_warner(aTHX_ packWARN(WARN_MISC),
"\t(Did you mean \"local\" instead of \"our\"?)\n");
break;
}
--off;
}
}
}
/*
=for apidoc Am|PADOFFSET|pad_findmy_pvn|const char *namepv|STRLEN namelen|U32 flags
Given the name of a lexical variable, find its position in the
currently-compiling pad.
I<namepv>/I<namelen> specify the variable's name, including leading sigil.
I<flags> is reserved and must be zero.
If it is not in the current pad but appears in the pad of any lexically
enclosing scope, then a pseudo-entry for it is added in the current pad.
Returns the offset in the current pad,
or C<NOT_IN_PAD> if no such lexical is in scope.
=cut
*/
PADOFFSET
Perl_pad_findmy_pvn(pTHX_ const char *namepv, STRLEN namelen, U32 flags)
{
dVAR;
SV *out_sv;
int out_flags;
I32 offset;
const AV *nameav;
SV **name_svp;
PERL_ARGS_ASSERT_PAD_FINDMY_PVN;
pad_peg("pad_findmy_pvn");
if (flags & ~padadd_UTF8_NAME)
Perl_croak(aTHX_ "panic: pad_findmy_pvn illegal flag bits 0x%" UVxf,
(UV)flags);
if (flags & padadd_UTF8_NAME) {
bool is_utf8 = TRUE;
namepv = (const char*)bytes_from_utf8((U8*)namepv, &namelen, &is_utf8);
if (is_utf8)
flags |= padadd_UTF8_NAME;
else
flags &= ~padadd_UTF8_NAME;
}
offset = pad_findlex(namepv, namelen, flags,
PL_compcv, PL_cop_seqmax, 1, NULL, &out_sv, &out_flags);
if ((PADOFFSET)offset != NOT_IN_PAD)
return offset;
/* look for an our that's being introduced; this allows
* our $foo = 0 unless defined $foo;
* to not give a warning. (Yes, this is a hack) */
nameav = PadlistARRAY(CvPADLIST(PL_compcv))[0];
name_svp = AvARRAY(nameav);
for (offset = AvFILLp(nameav); offset > 0; offset--) {
const SV * const namesv = name_svp[offset];
if (namesv && PadnameLEN(namesv) == namelen
&& !SvFAKE(namesv)
&& (SvPAD_OUR(namesv))
&& sv_eq_pvn_flags(aTHX_ namesv, namepv, namelen,
flags & padadd_UTF8_NAME ? SVf_UTF8 : 0 )
&& COP_SEQ_RANGE_LOW(namesv) == PERL_PADSEQ_INTRO