This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In PL_magic_data flag whether magic can be added to a readonly value.
[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
6f83ef0e
NC
21my @mg =
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',
34 desc => 'Regex match position data (@+ and @- vars)' },
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,
6f83ef0e 89 desc => 'Shadow "foreach" iterator variable / smart 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.
8b09643d
NC
108my @sig =
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
6f83ef0e
NC
144my ($vt, $raw) = map {
145 open_new($_, '>',
146 { by => 'regen/mg_vtable.pl', file => $_, style => '*' });
147} 'mg_vtable.h', 'mg_raw.h';
148
149# Of course, it would be *much* easier if we could output this table directly
150# here and now. However, for our sins, we try to support EBCDIC, which wouldn't
151# be *so* bad, except that there are (at least) 3 EBCDIC charset variants, and
152# they don't agree on the code point for '~'. Which we use. Great.
153# So we have to get the local build runtime to sort our table in character order
154# (And of course, just to be helpful, in POSIX BC '~' is \xFF, so we can't even
155# simplify the C code by assuming that the last element of the array is
156# predictable)
157
158{
159 while (my ($name, $data) = splice @mg, 0, 2) {
160 my $i = ord eval qq{"$data->{char}"};
161 unless ($data->{unknown_to_sv_magic}) {
e0a73de4 162 my $value = $data->{vtable}
6f83ef0e 163 ? "want_vtbl_$data->{vtable}" : 'magic_vtable_max';
82ff486e
NC
164 $value .= ' | PERL_MAGIC_READONLY_ACCEPTABLE'
165 if $data->{readonly_acceptable};
e0a73de4 166 $value .= ' | PERL_MAGIC_VALUE_MAGIC' if $data->{value_magic};
6f83ef0e
NC
167 my $comment = "/* $name '$data->{char}' $data->{desc} */";
168 $comment =~ s/([\\"])/\\$1/g;
e0a73de4 169 print $raw qq{ { '$data->{char}', "$value",\n "$comment" },\n};
6f83ef0e
NC
170 }
171 }
172}
8b09643d 173
ca298f7d 174{
2d1f1fe5
NC
175 my @names = grep {!ref $_} @sig;
176 my $want = join ",\n ", (map {"want_vtbl_$_"} @names), 'magic_vtable_max';
177 my $names = join qq{",\n "}, @names;
178
6f83ef0e 179 print $vt <<"EOH";
ca298f7d 180enum { /* pass one of these to get_vtbl */
2d1f1fe5
NC
181 $want
182};
183
184#ifdef DOINIT
185EXTCONST char *PL_magic_vtable_names[magic_vtable_max] = {
186 "$names"
ca298f7d 187};
2d1f1fe5
NC
188#else
189EXTCONST char *PL_magic_vtable_names[magic_vtable_max];
190#endif
ca298f7d
NC
191
192EOH
193}
194
6f83ef0e 195print $vt <<'EOH';
8b09643d
NC
196/* These all need to be 0, not NULL, as NULL can be (void*)0, which is a
197 * pointer to data, whereas we're assigning pointers to functions, which are
198 * not the same beast. ANSI doesn't allow the assignment from one to the other.
199 * (although most, but not all, compilers are prepared to do it)
200 */
201
0ffb5b03 202/* order is:
8b09643d
NC
203 get
204 set
205 len
206 clear
207 free
208 copy
209 dup
210 local
211*/
212
b7b5e578 213#ifdef DOINIT
c7fdacb9 214EXT_MGVTBL PL_magic_vtables[magic_vtable_max] = {
8b09643d
NC
215EOH
216
b7b5e578 217my @vtable_names;
b2e9fc6f 218my @aliases;
b7b5e578 219
8b09643d 220while (my ($name, $data) = splice @sig, 0, 2) {
b7b5e578 221 push @vtable_names, $name;
0a1f728a 222 my @funcs = map {
8b09643d
NC
223 $data->{$_} ? "Perl_magic_$data->{$_}" : 0;
224 } qw(get set len clear free copy dup local);
225
0a1f728a 226 $funcs[0] = "(int (*)(pTHX_ SV *, MAGIC *))" . $funcs[0] if $data->{const};
0ffb5b03 227 my $funcs = join ", ", @funcs;
8b09643d 228
b7b5e578
NC
229 # Because we can't have a , after the last {...}
230 my $comma = @sig ? ',' : '';
231
6f83ef0e
NC
232 print $vt "$data->{cond}\n" if $data->{cond};
233 print $vt " { $funcs }$comma\n";
234 print $vt <<"EOH" if $data->{cond};
0ffb5b03 235#else
b7b5e578 236 { 0, 0, 0, 0, 0, 0, 0, 0 }$comma
0ffb5b03 237#endif
b7b5e578 238EOH
b2e9fc6f
NC
239 foreach(@{$data->{alias}}) {
240 push @aliases, "#define want_vtbl_$_ want_vtbl_$name\n";
241 push @vtable_names, $_;
242 }
8b09643d
NC
243}
244
6f83ef0e 245print $vt <<'EOH';
b7b5e578
NC
246};
247#else
c7fdacb9 248EXT_MGVTBL PL_magic_vtables[magic_vtable_max];
b7b5e578
NC
249#endif
250
251EOH
252
6f83ef0e 253print $vt (sort @aliases), "\n";
b7b5e578 254
6f83ef0e 255print $vt "#define PL_vtbl_$_ PL_magic_vtables[want_vtbl_$_]\n"
b7b5e578
NC
256 foreach sort @vtable_names;
257
e0a73de4
NC
258# 63, not 64, As we rely on the last possible value to mean "NULL vtable"
259die "Too many vtable names" if @vtable_names > 63;
260
6f83ef0e 261read_only_bottom_close_and_rename($_) foreach $vt, $raw;