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