This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #122445] use magic on $DB::single etc to avoid overload issues
[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 (
82ff486e
NC
28 sv => { char => '\0', vtable => 'sv', readonly_acceptable => 1,
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' },
6f83ef0e
NC
113);
114
0a1f728a 115# These have a subtly different "namespace" from the magic types.
23cfd2fc 116my %sig =
8b09643d 117 (
9bb29b68 118 'sv' => {get => 'get', set => 'set'},
8b09643d
NC
119 'env' => {set => 'set_all_env', clear => 'clear_all_env'},
120 'envelem' => {set => 'setenv', clear => 'clearenv'},
121 'sigelem' => {get => 'getsig', set => 'setsig', clear => 'clearsig',
122 cond => '#ifndef PERL_MICRO'},
123 'pack' => {len => 'sizepack', clear => 'wipepack'},
124 'packelem' => {get => 'getpack', set => 'setpack', clear => 'clearpack'},
125 'dbline' => {set => 'setdbline'},
126 'isa' => {set => 'setisa', clear => 'clearisa'},
127 'isaelem' => {set => 'setisa'},
128 'arylen' => {get => 'getarylen', set => 'setarylen', const => 1},
83f29afa 129 'arylen_p' => {clear => 'cleararylen_p', free => 'freearylen_p'},
8b09643d
NC
130 'mglob' => {set => 'setmglob'},
131 'nkeys' => {get => 'getnkeys', set => 'setnkeys'},
132 'taint' => {get => 'gettaint', set => 'settaint'},
133 'substr' => {get => 'getsubstr', set => 'setsubstr'},
134 'vec' => {get => 'getvec', set => 'setvec'},
135 'pos' => {get => 'getpos', set => 'setpos'},
8b09643d
NC
136 'uvar' => {get => 'getuvar', set => 'setuvar'},
137 'defelem' => {get => 'getdefelem', set => 'setdefelem'},
b2e9fc6f 138 'regexp' => {set => 'setregexp', alias => [qw(bm fm)]},
8b09643d
NC
139 'regdata' => {len => 'regdata_cnt'},
140 'regdatum' => {get => 'regdatum_get', set => 'regdatum_set'},
8b09643d
NC
141 'backref' => {free => 'killbackrefs'},
142 'ovrld' => {free => 'freeovrld'},
143 'utf8' => {set => 'setutf8'},
144 'collxfrm' => {set => 'setcollxfrm',
145 cond => '#ifdef USE_LOCALE_COLLATE'},
146 'hintselem' => {set => 'sethint', clear => 'clearhint'},
147 'hints' => {clear => 'clearhints'},
09fb282d 148 'checkcall' => {copy => 'copycallchecker'},
a6d69523 149 'debugvar' => { set => 'setdebugvar', get => 'getdebugvar' },
8b09643d
NC
150);
151
52f49505 152my ($vt, $raw, $names) = map {
6f83ef0e
NC
153 open_new($_, '>',
154 { by => 'regen/mg_vtable.pl', file => $_, style => '*' });
52f49505 155} 'mg_vtable.h', 'mg_raw.h', 'mg_names.c';
f1f5ddd7 156my $guts = open_new("pod/perlguts.pod", ">");
6f83ef0e 157
abf9aa7a
NC
158print $vt <<'EOH';
159/* These constants should be used in preference to raw characters
160 * when using magic. Note that some perl guts still assume
161 * certain character properties of these constants, namely that
162 * isUPPER() and toLOWER() may do useful mappings.
163 */
164
165EOH
166
6f83ef0e
NC
167# Of course, it would be *much* easier if we could output this table directly
168# here and now. However, for our sins, we try to support EBCDIC, which wouldn't
169# be *so* bad, except that there are (at least) 3 EBCDIC charset variants, and
170# they don't agree on the code point for '~'. Which we use. Great.
171# So we have to get the local build runtime to sort our table in character order
172# (And of course, just to be helpful, in POSIX BC '~' is \xFF, so we can't even
173# simplify the C code by assuming that the last element of the array is
174# predictable)
175
176{
e972d315
NC
177 my $longest = 0;
178 foreach (keys %mg) {
179 $longest = length $_ if length $_ > $longest;
180 }
181
52f49505
NC
182 my $longest_p1 = $longest + 1;
183
23cfd2fc
NC
184 my %mg_order;
185 while (my ($name, $data) = each %mg) {
186 my $byte = eval qq{"$data->{char}"};
187 $data->{byte} = $byte;
188 $mg_order{(uc $byte) . $byte} = $name;
189 }
63e77aaf 190 my @rows;
23cfd2fc
NC
191 foreach (sort keys %mg_order) {
192 my $name = $mg_order{$_};
193 my $data = $mg{$name};
194 my $i = ord $data->{byte};
6f83ef0e 195 unless ($data->{unknown_to_sv_magic}) {
e0a73de4 196 my $value = $data->{vtable}
6f83ef0e 197 ? "want_vtbl_$data->{vtable}" : 'magic_vtable_max';
82ff486e
NC
198 $value .= ' | PERL_MAGIC_READONLY_ACCEPTABLE'
199 if $data->{readonly_acceptable};
e0a73de4 200 $value .= ' | PERL_MAGIC_VALUE_MAGIC' if $data->{value_magic};
6f83ef0e
NC
201 my $comment = "/* $name '$data->{char}' $data->{desc} */";
202 $comment =~ s/([\\"])/\\$1/g;
abf9aa7a 203 $comment =~ tr/\n/ /;
e0a73de4 204 print $raw qq{ { '$data->{char}', "$value",\n "$comment" },\n};
6f83ef0e 205 }
abf9aa7a
NC
206
207 my $comment = $data->{desc};
208 my $leader = ' ' x ($longest + 27);
209 $comment =~ s/\n/\n$leader/s;
210 printf $vt "#define PERL_MAGIC_%-${longest}s '%s' /* %s */\n",
211 $name, $data->{char}, $comment;
52f49505
NC
212
213 my $char = $data->{char};
214 $char =~ s/([\\"])/\\$1/g;
215 printf $names qq[\t{ PERL_MAGIC_%-${longest_p1}s "%s(%s)" },\n],
216 "$name,", $name, $char;
63e77aaf
NC
217
218 push @rows, [(sprintf "%-2s PERL_MAGIC_%s", $data->{char}, $name),
219 $data->{vtable} ? "vtbl_$data->{vtable}" : '(none)',
220 $data->{desc}];
221 }
f1f5ddd7 222 select +(select($guts), do {
63e77aaf
NC
223 my @header = ('(old-style char and macro)', 'MGVTBL', 'Type of magic');
224 my @widths = (0, 0);
225 foreach my $row (@rows) {
226 for (0, 1) {
227 $widths[$_] = length $row->[$_]
228 if length $row->[$_] > $widths[$_];
229 }
230 }
f1f5ddd7 231 my $indent = ' ';
63e77aaf
NC
232 my $format
233 = sprintf "$indent%%-%ds%%-%ds%%s\n", $widths[0] + 1, $widths[1] + 1;
f1f5ddd7
FC
234 my $desc_wrap =
235 79 - 7 - (length $indent) - $widths[0] - $widths[1] - 2;
236
237 open my $oldguts, "<", "pod/perlguts.pod"
238 or die "$0 cannot open pod/perlguts.pod for reading: $!";
239 while (<$oldguts>) {
240 print;
241 last if /^=for mg_vtable.pl begin/
242 }
63e77aaf 243
f1f5ddd7 244 print "\n", $indent . "mg_type\n";
63e77aaf
NC
245 printf $format, @header;
246 printf $format, map {'-' x length $_} @header;
247 foreach (@rows) {
248 my ($type, $vtbl, $desc) = @$_;
249 $desc =~ tr/\n/ /;
250 my @cont;
251 if (length $desc > $desc_wrap) {
252 # If it's too long, first split on '(', if there.
253 # [Which, if there, is always short enough, currently.
254 # Make this more robust if that changes]
255 ($desc, @cont) = split /(?=\()/, $desc;
256 if (!@cont) {
257 ($desc, @cont) = $desc =~ /(.{1,$desc_wrap})(?: |\z)/g
258 }
259 }
260 printf $format, $type, $vtbl, $desc;
261 printf $format, '', '', $_ foreach @cont;
262 }
f1f5ddd7
FC
263 print "\n";
264
265 while (<$oldguts>) {
266 last if /^=for mg_vtable.pl end/;
267 }
268 do { print } while <$oldguts>;
269 })[0];
6f83ef0e 270}
8b09643d 271
23cfd2fc 272my @names = sort keys %sig;
ca298f7d 273{
2d1f1fe5
NC
274 my $want = join ",\n ", (map {"want_vtbl_$_"} @names), 'magic_vtable_max';
275 my $names = join qq{",\n "}, @names;
276
6f83ef0e 277 print $vt <<"EOH";
abf9aa7a 278
ca298f7d 279enum { /* pass one of these to get_vtbl */
2d1f1fe5
NC
280 $want
281};
282
283#ifdef DOINIT
bfd14e52 284EXTCONST char * const PL_magic_vtable_names[magic_vtable_max] = {
2d1f1fe5 285 "$names"
ca298f7d 286};
2d1f1fe5 287#else
bfd14e52 288EXTCONST char * const PL_magic_vtable_names[magic_vtable_max];
2d1f1fe5 289#endif
ca298f7d
NC
290
291EOH
292}
293
6f83ef0e 294print $vt <<'EOH';
8b09643d
NC
295/* These all need to be 0, not NULL, as NULL can be (void*)0, which is a
296 * pointer to data, whereas we're assigning pointers to functions, which are
297 * not the same beast. ANSI doesn't allow the assignment from one to the other.
298 * (although most, but not all, compilers are prepared to do it)
299 */
300
0ffb5b03 301/* order is:
8b09643d
NC
302 get
303 set
304 len
305 clear
306 free
307 copy
308 dup
309 local
310*/
311
b7b5e578 312#ifdef DOINIT
c7fdacb9 313EXT_MGVTBL PL_magic_vtables[magic_vtable_max] = {
8b09643d
NC
314EOH
315
b7b5e578 316my @vtable_names;
b2e9fc6f 317my @aliases;
b7b5e578 318
23cfd2fc
NC
319while (my $name = shift @names) {
320 my $data = $sig{$name};
b7b5e578 321 push @vtable_names, $name;
0a1f728a 322 my @funcs = map {
8b09643d
NC
323 $data->{$_} ? "Perl_magic_$data->{$_}" : 0;
324 } qw(get set len clear free copy dup local);
325
0a1f728a 326 $funcs[0] = "(int (*)(pTHX_ SV *, MAGIC *))" . $funcs[0] if $data->{const};
0ffb5b03 327 my $funcs = join ", ", @funcs;
8b09643d 328
b7b5e578 329 # Because we can't have a , after the last {...}
23cfd2fc 330 my $comma = @names ? ',' : '';
b7b5e578 331
6f83ef0e
NC
332 print $vt "$data->{cond}\n" if $data->{cond};
333 print $vt " { $funcs }$comma\n";
334 print $vt <<"EOH" if $data->{cond};
0ffb5b03 335#else
b7b5e578 336 { 0, 0, 0, 0, 0, 0, 0, 0 }$comma
0ffb5b03 337#endif
b7b5e578 338EOH
b2e9fc6f
NC
339 foreach(@{$data->{alias}}) {
340 push @aliases, "#define want_vtbl_$_ want_vtbl_$name\n";
341 push @vtable_names, $_;
342 }
8b09643d
NC
343}
344
6f83ef0e 345print $vt <<'EOH';
b7b5e578
NC
346};
347#else
c7fdacb9 348EXT_MGVTBL PL_magic_vtables[magic_vtable_max];
b7b5e578
NC
349#endif
350
351EOH
352
6f83ef0e 353print $vt (sort @aliases), "\n";
b7b5e578 354
6f83ef0e 355print $vt "#define PL_vtbl_$_ PL_magic_vtables[want_vtbl_$_]\n"
b7b5e578
NC
356 foreach sort @vtable_names;
357
e0a73de4
NC
358# 63, not 64, As we rely on the last possible value to mean "NULL vtable"
359die "Too many vtable names" if @vtable_names > 63;
360
52f49505 361read_only_bottom_close_and_rename($_) foreach $vt, $raw, $names;
f1f5ddd7 362 close_and_rename($guts);