This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In Perl_moreswitches(), avoid the strlen() inside sv_catpv() by moving
[perl5.git] / overload.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Generate overload.h
5 # This allows the order of overloading constants to be changed.
6
7
8 BEGIN {
9     # Get function prototypes
10     require 'regen_lib.pl';
11 }
12
13 use strict;
14
15 my (@enums, @names);
16 while (<DATA>) {
17   next if /^#/;
18   next if /^$/;
19   my ($enum, $name) = /^(\S+)\s+(\S+)/ or die "Can't parse $_";
20   push @enums, $enum;
21   push @names, $name;
22 }
23
24 safer_unlink ('overload.h', 'overload.c');
25 die "overload.h: $!" unless open(C, ">overload.c");
26 binmode C;
27 die "overload.h: $!" unless open(H, ">overload.h");
28 binmode H;
29
30 sub print_header {
31   my $file = shift;
32   print <<"EOF";
33 /* -*- buffer-read-only: t -*-
34  *
35  *    $file
36  *
37  *    Copyright (C) 1997, 1998, 2000, 2001, 2005, 2006, 2007 by Larry Wall
38  *    and others
39  *
40  *    You may distribute under the terms of either the GNU General Public
41  *    License or the Artistic License, as specified in the README file.
42  *
43  *  !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
44  *  This file is built by overload.pl
45  */
46 EOF
47 }
48
49 select C;
50 print_header('overload.c');
51
52 select H;
53 print_header('overload.h');
54 print <<'EOF';
55
56 enum {
57 EOF
58
59 print "    ${_}_amg,\n", foreach @enums;
60
61 print <<'EOF';
62     max_amg_code
63     /* Do not leave a trailing comma here.  C9X allows it, C89 doesn't. */
64 };
65
66 #define NofAMmeth max_amg_code
67
68 EOF
69
70 print C <<'EOF';
71
72 #define AMG_id2name(id) (PL_AMG_names[id]+1)
73
74 const U8 PL_AMG_namelens[NofAMmeth] = {
75 EOF
76
77 my $last = pop @names;
78
79 print C "    $_,\n" foreach map { length $_ } @names;
80
81 my $lastlen = length $last;
82 print C <<"EOT";
83     $lastlen
84 };
85
86 char * const PL_AMG_names[NofAMmeth] = {
87   /* Names kept in the symbol table.  fallback => "()", the rest has
88      "(" prepended.  The only other place in perl which knows about
89      this convention is AMG_id2name (used for debugging output and
90      'nomethod' only), the only other place which has it hardwired is
91      overload.pm.  */
92 EOT
93
94 print C "    \"$_\",\n" foreach map { s/(["\\"])/\\$1/g; $_ } @names;
95
96 print C <<"EOT";
97     "$last"
98 };
99 EOT
100
101 close H or die $!;
102 close C or die $!;
103
104 __DATA__
105 # Fallback should be the first
106 fallback        ()
107
108 # These 5 are the most common in the fallback switch statement in amagic_call
109 to_sv           (${}
110 to_av           (@{}
111 to_hv           (%{}
112 to_gv           (*{}
113 to_cv           (&{}
114
115 # These have non-default cases in that switch statement
116 inc             (++
117 dec             (--
118 bool_           (bool
119 numer           (0+
120 string          (""
121 not             (!
122 copy            (=
123 abs             (abs
124 neg             (neg
125 iter            (<>
126 int             (int
127
128 # These 12 feature in the next switch statement
129 lt              (<
130 le              (<=
131 gt              (>
132 ge              (>=
133 eq              (==
134 ne              (!=
135 slt             (lt
136 sle             (le
137 sgt             (gt
138 sge             (ge
139 seq             (eq
140 sne             (ne
141
142 nomethod        (nomethod
143 add             (+
144 add_ass         (+=
145 subtr           (-
146 subtr_ass       (-=
147 mult            (*
148 mult_ass        (*=
149 div             (/
150 div_ass         (/=
151 modulo          (%
152 modulo_ass      (%=
153 pow             (**
154 pow_ass         (**=
155 lshift          (<<
156 lshift_ass      (<<=
157 rshift          (>>
158 rshift_ass      (>>=
159 band            (&
160 band_ass        (&=
161 bor             (|
162 bor_ass         (|=
163 bxor            (^
164 bxor_ass        (^=
165 ncmp            (<=>
166 scmp            (cmp
167 compl           (~
168 atan2           (atan2
169 cos             (cos
170 sin             (sin
171 exp             (exp
172 log             (log
173 sqrt            (sqrt
174 repeat          (x
175 repeat_ass      (x=
176 concat          (.
177 concat_ass      (.=
178 smart           (~~
179 # Note: Perl_Gv_AMupdate() assumes that DESTROY is the last entry
180 DESTROY         DESTROY