This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add lvref magic type
[perl5.git] / regen / mg_vtable.pl
CommitLineData
8b09643d
NC
1#!/usr/bin/perl -w
2#
3# Regenerate (overwriting only if changed):
4#
130e6ef5
FC
5# mg_names.c
6# mg_raw.h
8b09643d 7# mg_vtable.h
1083e5c1 8# pod/perlguts.pod
8b09643d 9#
1083e5c1
FC
10# from information stored in this file. pod/perlguts.pod is not completely
11# regenerated. Only the magic table is replaced; the other parts remain
12# untouched.
8b09643d
NC
13#
14# Accepts the standard regen_lib -q and -v args.
15#
16# This script is normally invoked from regen.pl.
17
18use strict;
19require 5.004;
20
21BEGIN {
22 # Get function prototypes
23 require 'regen/regen_lib.pl';
24}
25
23cfd2fc 26my %mg =
6f83ef0e 27 (
9cce4f9a 28 sv => { char => "\0", vtable => 'sv', readonly_acceptable => 1,
82ff486e 29 desc => 'Special scalar variable' },
bd6e6c12
FC
30 # overload, or type "A" magic, used to be here. Hence overloaded is
31 # often called AMAGIC internally, even though it does not use "A"
32 # magic any more.
6f83ef0e
NC
33 overload_table => { char => 'c', vtable => 'ovrld',
34 desc => 'Holds overload table (AMT) on stash' },
e0a73de4 35 bm => { char => 'B', vtable => 'regexp', value_magic => 1,
82ff486e 36 readonly_acceptable => 1,
6f83ef0e
NC
37 desc => 'Boyer-Moore (fast string search)' },
38 regdata => { char => 'D', vtable => 'regdata',
abf9aa7a 39 desc => "Regex match position data\n(\@+ and \@- vars)" },
6f83ef0e
NC
40 regdatum => { char => 'd', vtable => 'regdatum',
41 desc => 'Regex match position data element' },
42 env => { char => 'E', vtable => 'env', desc => '%ENV hash' },
43 envelem => { char => 'e', vtable => 'envelem',
44 desc => '%ENV hash element' },
eccba044 45 fm => { char => 'f', vtable => 'regexp', value_magic => 1,
82ff486e 46 readonly_acceptable => 1, desc => "Formline ('compiled' format)" },
e0a73de4 47 regex_global => { char => 'g', vtable => 'mglob', value_magic => 1,
0177730e 48 readonly_acceptable => 1, desc => 'm//g target' },
6f83ef0e
NC
49 hints => { char => 'H', vtable => 'hints', desc => '%^H hash' },
50 hintselem => { char => 'h', vtable => 'hintselem',
51 desc => '%^H hash element' },
52 isa => { char => 'I', vtable => 'isa', desc => '@ISA array' },
53 isaelem => { char => 'i', vtable => 'isaelem',
54 desc => '@ISA array element' },
e0a73de4 55 nkeys => { char => 'k', vtable => 'nkeys', value_magic => 1,
6f83ef0e 56 desc => 'scalar(keys()) lvalue' },
f34d1562 57 dbfile => { char => 'L',
6f83ef0e 58 desc => 'Debugger %_<filename' },
f34d1562
FC
59 dbline => { char => 'l', vtable => 'dbline',
60 desc => 'Debugger %_<filename element' },
6f83ef0e
NC
61 shared => { char => 'N', desc => 'Shared between threads',
62 unknown_to_sv_magic => 1 },
63 shared_scalar => { char => 'n', desc => 'Shared between threads',
64 unknown_to_sv_magic => 1 },
e0a73de4 65 collxfrm => { char => 'o', vtable => 'collxfrm', value_magic => 1,
6f83ef0e 66 desc => 'Locale transformation' },
e0a73de4
NC
67 tied => { char => 'P', vtable => 'pack',
68 value_magic => 1, # treat as value, so 'local @tied' isn't tied
69 desc => 'Tied array or hash' },
6f83ef0e
NC
70 tiedelem => { char => 'p', vtable => 'packelem',
71 desc => 'Tied array or hash element' },
72 tiedscalar => { char => 'q', vtable => 'packelem',
73 desc => 'Tied scalar or handle' },
e0a73de4 74 qr => { char => 'r', vtable => 'regexp', value_magic => 1,
237da807 75 readonly_acceptable => 1, desc => 'precompiled qr// regex' },
6f83ef0e
NC
76 sig => { char => 'S', desc => '%SIG hash' },
77 sigelem => { char => 's', vtable => 'sigelem',
78 desc => '%SIG hash element' },
e0a73de4
NC
79 taint => { char => 't', vtable => 'taint', value_magic => 1,
80 desc => 'Taintedness' },
6f83ef0e
NC
81 uvar => { char => 'U', vtable => 'uvar',
82 desc => 'Available for use by extensions' },
83 uvar_elem => { char => 'u', desc => 'Reserved for use by extensions',
84 unknown_to_sv_magic => 1 },
e0a73de4
NC
85 vec => { char => 'v', vtable => 'vec', value_magic => 1,
86 desc => 'vec() lvalue' },
4499db73 87 vstring => { char => 'V', value_magic => 1,
e0a73de4
NC
88 desc => 'SV was vstring literal' },
89 utf8 => { char => 'w', vtable => 'utf8', value_magic => 1,
6f83ef0e 90 desc => 'Cached UTF-8 information' },
e0a73de4
NC
91 substr => { char => 'x', vtable => 'substr', value_magic => 1,
92 desc => 'substr() lvalue' },
93 defelem => { char => 'y', vtable => 'defelem', value_magic => 1,
abf9aa7a 94 desc => "Shadow \"foreach\" iterator variable /\nsmart parameter vivification" },
e0a73de4 95 arylen => { char => '#', vtable => 'arylen', value_magic => 1,
6f83ef0e 96 desc => 'Array length ($#ary)' },
2a388207 97 proto => { char => '&', desc => 'my sub prototype CV' },
e0a73de4
NC
98 pos => { char => '.', vtable => 'pos', value_magic => 1,
99 desc => 'pos() lvalue' },
100 backref => { char => '<', vtable => 'backref', value_magic => 1,
82ff486e 101 readonly_acceptable => 1, desc => 'for weak ref data' },
e0a73de4
NC
102 symtab => { char => ':', value_magic => 1,
103 desc => 'extra data for symbol tables' },
104 rhash => { char => '%', value_magic => 1,
105 desc => 'extra data for restricted hashes' },
106 arylen_p => { char => '@', value_magic => 1,
107 desc => 'to move arylen out of XPVAV' },
6f83ef0e 108 ext => { char => '~', desc => 'Available for use by extensions' },
09fb282d 109 checkcall => { char => ']', value_magic => 1, vtable => 'checkcall',
e0a73de4 110 desc => 'inlining/mutation of call to this CV'},
a6d69523
TC
111 debugvar => { char => '*', desc => '$DB::single, signal, trace vars',
112 vtable => 'debugvar' },
9cce4f9a
FC
113 lvref => { char => '\\', vtable => 'lvref',
114 desc => "Lvalue reference in list assignment" },
6f83ef0e
NC
115);
116
0a1f728a 117# These have a subtly different "namespace" from the magic types.
23cfd2fc 118my %sig =
8b09643d 119 (
9bb29b68 120 'sv' => {get => 'get', set => 'set'},
8b09643d
NC
121 'env' => {set => 'set_all_env', clear => 'clear_all_env'},
122 'envelem' => {set => 'setenv', clear => 'clearenv'},
123 'sigelem' => {get => 'getsig', set => 'setsig', clear => 'clearsig',
124 cond => '#ifndef PERL_MICRO'},
125 'pack' => {len => 'sizepack', clear => 'wipepack'},
126 'packelem' => {get => 'getpack', set => 'setpack', clear => 'clearpack'},
127 'dbline' => {set => 'setdbline'},
128 'isa' => {set => 'setisa', clear => 'clearisa'},
129 'isaelem' => {set => 'setisa'},
130 'arylen' => {get => 'getarylen', set => 'setarylen', const => 1},
83f29afa 131 'arylen_p' => {clear => 'cleararylen_p', free => 'freearylen_p'},
8b09643d
NC
132 'mglob' => {set => 'setmglob'},
133 'nkeys' => {get => 'getnkeys', set => 'setnkeys'},
134 'taint' => {get => 'gettaint', set => 'settaint'},
135 'substr' => {get => 'getsubstr', set => 'setsubstr'},
136 'vec' => {get => 'getvec', set => 'setvec'},
137 'pos' => {get => 'getpos', set => 'setpos'},
8b09643d
NC
138 'uvar' => {get => 'getuvar', set => 'setuvar'},
139 'defelem' => {get => 'getdefelem', set => 'setdefelem'},
b2e9fc6f 140 'regexp' => {set => 'setregexp', alias => [qw(bm fm)]},
8b09643d
NC
141 'regdata' => {len => 'regdata_cnt'},
142 'regdatum' => {get => 'regdatum_get', set => 'regdatum_set'},
8b09643d
NC
143 'backref' => {free => 'killbackrefs'},
144 'ovrld' => {free => 'freeovrld'},
145 'utf8' => {set => 'setutf8'},
146 'collxfrm' => {set => 'setcollxfrm',
147 cond => '#ifdef USE_LOCALE_COLLATE'},
148 'hintselem' => {set => 'sethint', clear => 'clearhint'},
149 'hints' => {clear => 'clearhints'},
09fb282d 150 'checkcall' => {copy => 'copycallchecker'},
a6d69523 151 'debugvar' => { set => 'setdebugvar', get => 'getdebugvar' },
9cce4f9a 152 'lvref' => {set => 'setlvref'},
8b09643d
NC
153);
154
52f49505 155my ($vt, $raw, $names) = map {
6f83ef0e
NC
156 open_new($_, '>',
157 { by => 'regen/mg_vtable.pl', file => $_, style => '*' });
52f49505 158} 'mg_vtable.h', 'mg_raw.h', 'mg_names.c';
f1f5ddd7 159my $guts = open_new("pod/perlguts.pod", ">");
6f83ef0e 160
abf9aa7a
NC
161print $vt <<'EOH';
162/* These constants should be used in preference to raw characters
163 * when using magic. Note that some perl guts still assume
164 * certain character properties of these constants, namely that
165 * isUPPER() and toLOWER() may do useful mappings.
166 */
167
168EOH
169
6f83ef0e
NC
170# Of course, it would be *much* easier if we could output this table directly
171# here and now. However, for our sins, we try to support EBCDIC, which wouldn't
172# be *so* bad, except that there are (at least) 3 EBCDIC charset variants, and
173# they don't agree on the code point for '~'. Which we use. Great.
174# So we have to get the local build runtime to sort our table in character order
175# (And of course, just to be helpful, in POSIX BC '~' is \xFF, so we can't even
176# simplify the C code by assuming that the last element of the array is
177# predictable)
178
179{
e972d315
NC
180 my $longest = 0;
181 foreach (keys %mg) {
182 $longest = length $_ if length $_ > $longest;
183 }
184
52f49505
NC
185 my $longest_p1 = $longest + 1;
186
23cfd2fc
NC
187 my %mg_order;
188 while (my ($name, $data) = each %mg) {
9cce4f9a
FC
189 my $byte = $data->{char};
190 if ($byte =~ /[[:print:]]/) {
191 $data->{r_char} = $byte; # readable char
192 ($data->{c_char} = $byte) =~ s/([\\"])/\\$1/g; # for C strings
193 }
194 else {
195 $data->{c_char} = $data->{r_char} = '\\'.ord $byte;
196 }
23cfd2fc
NC
197 $mg_order{(uc $byte) . $byte} = $name;
198 }
63e77aaf 199 my @rows;
23cfd2fc
NC
200 foreach (sort keys %mg_order) {
201 my $name = $mg_order{$_};
202 my $data = $mg{$name};
9cce4f9a 203 my $i = ord $data->{char};
6f83ef0e 204 unless ($data->{unknown_to_sv_magic}) {
e0a73de4 205 my $value = $data->{vtable}
6f83ef0e 206 ? "want_vtbl_$data->{vtable}" : 'magic_vtable_max';
82ff486e
NC
207 $value .= ' | PERL_MAGIC_READONLY_ACCEPTABLE'
208 if $data->{readonly_acceptable};
e0a73de4 209 $value .= ' | PERL_MAGIC_VALUE_MAGIC' if $data->{value_magic};
9cce4f9a 210 my $comment = "/* $name '$data->{r_char}' $data->{desc} */";
6f83ef0e 211 $comment =~ s/([\\"])/\\$1/g;
abf9aa7a 212 $comment =~ tr/\n/ /;
9cce4f9a 213 print $raw qq{ { '$data->{c_char}', "$value",\n "$comment" },\n};
6f83ef0e 214 }
abf9aa7a
NC
215
216 my $comment = $data->{desc};
217 my $leader = ' ' x ($longest + 27);
218 $comment =~ s/\n/\n$leader/s;
219 printf $vt "#define PERL_MAGIC_%-${longest}s '%s' /* %s */\n",
9cce4f9a 220 $name, $data->{c_char}, $comment;
52f49505 221
9cce4f9a 222 my $char = $data->{r_char};
52f49505
NC
223 $char =~ s/([\\"])/\\$1/g;
224 printf $names qq[\t{ PERL_MAGIC_%-${longest_p1}s "%s(%s)" },\n],
225 "$name,", $name, $char;
63e77aaf 226
9cce4f9a 227 push @rows, [(sprintf "%-2s PERL_MAGIC_%s", $data->{r_char},$name),
63e77aaf
NC
228 $data->{vtable} ? "vtbl_$data->{vtable}" : '(none)',
229 $data->{desc}];
230 }
f1f5ddd7 231 select +(select($guts), do {
63e77aaf
NC
232 my @header = ('(old-style char and macro)', 'MGVTBL', 'Type of magic');
233 my @widths = (0, 0);
234 foreach my $row (@rows) {
235 for (0, 1) {
236 $widths[$_] = length $row->[$_]
237 if length $row->[$_] > $widths[$_];
238 }
239 }
f1f5ddd7 240 my $indent = ' ';
63e77aaf
NC
241 my $format
242 = sprintf "$indent%%-%ds%%-%ds%%s\n", $widths[0] + 1, $widths[1] + 1;
f1f5ddd7
FC
243 my $desc_wrap =
244 79 - 7 - (length $indent) - $widths[0] - $widths[1] - 2;
245
246 open my $oldguts, "<", "pod/perlguts.pod"
247 or die "$0 cannot open pod/perlguts.pod for reading: $!";
248 while (<$oldguts>) {
249 print;
250 last if /^=for mg_vtable.pl begin/
251 }
63e77aaf 252
f1f5ddd7 253 print "\n", $indent . "mg_type\n";
63e77aaf
NC
254 printf $format, @header;
255 printf $format, map {'-' x length $_} @header;
256 foreach (@rows) {
257 my ($type, $vtbl, $desc) = @$_;
258 $desc =~ tr/\n/ /;
259 my @cont;
260 if (length $desc > $desc_wrap) {
261 # If it's too long, first split on '(', if there.
262 # [Which, if there, is always short enough, currently.
263 # Make this more robust if that changes]
264 ($desc, @cont) = split /(?=\()/, $desc;
265 if (!@cont) {
266 ($desc, @cont) = $desc =~ /(.{1,$desc_wrap})(?: |\z)/g
267 }
268 }
269 printf $format, $type, $vtbl, $desc;
270 printf $format, '', '', $_ foreach @cont;
271 }
f1f5ddd7
FC
272 print "\n";
273
274 while (<$oldguts>) {
275 last if /^=for mg_vtable.pl end/;
276 }
277 do { print } while <$oldguts>;
278 })[0];
6f83ef0e 279}
8b09643d 280
23cfd2fc 281my @names = sort keys %sig;
ca298f7d 282{
2d1f1fe5
NC
283 my $want = join ",\n ", (map {"want_vtbl_$_"} @names), 'magic_vtable_max';
284 my $names = join qq{",\n "}, @names;
285
6f83ef0e 286 print $vt <<"EOH";
abf9aa7a 287
ca298f7d 288enum { /* pass one of these to get_vtbl */
2d1f1fe5
NC
289 $want
290};
291
292#ifdef DOINIT
bfd14e52 293EXTCONST char * const PL_magic_vtable_names[magic_vtable_max] = {
2d1f1fe5 294 "$names"
ca298f7d 295};
2d1f1fe5 296#else
bfd14e52 297EXTCONST char * const PL_magic_vtable_names[magic_vtable_max];
2d1f1fe5 298#endif
ca298f7d
NC
299
300EOH
301}
302
6f83ef0e 303print $vt <<'EOH';
8b09643d
NC
304/* These all need to be 0, not NULL, as NULL can be (void*)0, which is a
305 * pointer to data, whereas we're assigning pointers to functions, which are
306 * not the same beast. ANSI doesn't allow the assignment from one to the other.
307 * (although most, but not all, compilers are prepared to do it)
308 */
309
0ffb5b03 310/* order is:
8b09643d
NC
311 get
312 set
313 len
314 clear
315 free
316 copy
317 dup
318 local
319*/
320
b7b5e578 321#ifdef DOINIT
c7fdacb9 322EXT_MGVTBL PL_magic_vtables[magic_vtable_max] = {
8b09643d
NC
323EOH
324
b7b5e578 325my @vtable_names;
b2e9fc6f 326my @aliases;
b7b5e578 327
23cfd2fc
NC
328while (my $name = shift @names) {
329 my $data = $sig{$name};
b7b5e578 330 push @vtable_names, $name;
0a1f728a 331 my @funcs = map {
8b09643d
NC
332 $data->{$_} ? "Perl_magic_$data->{$_}" : 0;
333 } qw(get set len clear free copy dup local);
334
0a1f728a 335 $funcs[0] = "(int (*)(pTHX_ SV *, MAGIC *))" . $funcs[0] if $data->{const};
0ffb5b03 336 my $funcs = join ", ", @funcs;
8b09643d 337
b7b5e578 338 # Because we can't have a , after the last {...}
23cfd2fc 339 my $comma = @names ? ',' : '';
b7b5e578 340
6f83ef0e
NC
341 print $vt "$data->{cond}\n" if $data->{cond};
342 print $vt " { $funcs }$comma\n";
343 print $vt <<"EOH" if $data->{cond};
0ffb5b03 344#else
b7b5e578 345 { 0, 0, 0, 0, 0, 0, 0, 0 }$comma
0ffb5b03 346#endif
b7b5e578 347EOH
b2e9fc6f
NC
348 foreach(@{$data->{alias}}) {
349 push @aliases, "#define want_vtbl_$_ want_vtbl_$name\n";
350 push @vtable_names, $_;
351 }
8b09643d
NC
352}
353
6f83ef0e 354print $vt <<'EOH';
b7b5e578
NC
355};
356#else
c7fdacb9 357EXT_MGVTBL PL_magic_vtables[magic_vtable_max];
b7b5e578
NC
358#endif
359
360EOH
361
6f83ef0e 362print $vt (sort @aliases), "\n";
b7b5e578 363
6f83ef0e 364print $vt "#define PL_vtbl_$_ PL_magic_vtables[want_vtbl_$_]\n"
b7b5e578
NC
365 foreach sort @vtable_names;
366
e0a73de4
NC
367# 63, not 64, As we rely on the last possible value to mean "NULL vtable"
368die "Too many vtable names" if @vtable_names > 63;
369
52f49505 370read_only_bottom_close_and_rename($_) foreach $vt, $raw, $names;
f1f5ddd7 371 close_and_rename($guts);