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