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