This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
424bea9737db2e20fc35fd3fd02b0d6c465d0123
[perl5.git] / ext / Devel / PPPort / parts / apicheck.pl
1 #!/usr/bin/perl -w
2 ################################################################################
3 #
4 #  apicheck.pl -- generate C source for automated API check
5 #
6 ################################################################################
7 #
8 #  $Revision: 21 $
9 #  $Author: mhx $
10 #  $Date: 2006/07/25 19:14:07 +0200 $
11 #
12 ################################################################################
13 #
14 #  Version 3.x, Copyright (C) 2004-2006, Marcus Holland-Moritz.
15 #  Version 2.x, Copyright (C) 2001, Paul Marquess.
16 #  Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
17 #
18 #  This program is free software; you can redistribute it and/or
19 #  modify it under the same terms as Perl itself.
20 #
21 ################################################################################
22
23 use strict;
24 require 'parts/ppptools.pl';
25
26 if (@ARGV) {
27   my $file = pop @ARGV;
28   open OUT, ">$file" or die "$file: $!\n";
29 }
30 else {
31   *OUT = \*STDOUT;
32 }
33
34 my @f = parse_embed(qw( parts/embed.fnc parts/apidoc.fnc ));
35
36 my %todo = %{&parse_todo};
37
38 my %tmap = (
39   void => 'int',
40 );
41
42 my %amap = (
43   SP   => 'SP',
44   type => 'int',
45   cast => 'int',
46 );
47
48 my %void = (
49   void     => 1,
50   Free_t   => 1,
51   Signal_t => 1,
52 );
53
54 my %castvoid = (
55   map { ($_ => 1) } qw(
56     Nullav
57     Nullcv
58     Nullhv
59     Nullch
60     Nullsv
61     HEf_SVKEY
62     SP
63     MARK
64     SVt_PV
65     SVt_IV
66     SVt_NV
67     SVt_PVMG
68     SVt_PVAV
69     SVt_PVHV
70     SVt_PVCV
71     SvUOK
72     G_SCALAR
73     G_ARRAY
74     G_VOID
75     G_DISCARD
76     G_EVAL
77     G_NOARGS
78     XS_VERSION
79   ),
80 );
81
82 my %ignorerv = (
83   map { ($_ => 1) } qw(
84     newCONSTSUB
85   ),
86 );
87
88 my %stack = (
89   ORIGMARK       => ['dORIGMARK;'],
90   POPpx          => ['STRLEN n_a;'],
91   POPpbytex      => ['STRLEN n_a;'],
92   PUSHp          => ['dTARG;'],
93   PUSHn          => ['dTARG;'],
94   PUSHi          => ['dTARG;'],
95   PUSHu          => ['dTARG;'],
96   XPUSHp         => ['dTARG;'],
97   XPUSHn         => ['dTARG;'],
98   XPUSHi         => ['dTARG;'],
99   XPUSHu         => ['dTARG;'],
100   UNDERBAR       => ['dUNDERBAR;'],
101   XCPT_TRY_START => ['dXCPT;'],
102   XCPT_TRY_END   => ['dXCPT;'],
103   XCPT_CATCH     => ['dXCPT;'],
104   XCPT_RETHROW   => ['dXCPT;'],
105 );
106
107 my %ignore = (
108   map { ($_ => 1) } qw(
109     svtype
110     items
111     ix
112     dXSI32
113     XS
114     CLASS
115     THIS
116     RETVAL
117     StructCopy
118   ),
119 );
120
121 print OUT <<HEAD;
122 /*
123  * !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
124  * This file is built by $0.
125  * Any changes made here will be lost!
126  */
127
128 #include "EXTERN.h"
129 #include "perl.h"
130
131 #define NO_XSLOCKS
132 #include "XSUB.h"
133
134 #ifdef DPPP_APICHECK_NO_PPPORT_H
135
136 /* This is just to avoid too many baseline failures with perls < 5.6.0 */
137
138 #ifndef dTHX
139 #  define dTHX extern int Perl___notused
140 #endif
141
142 #else
143
144 #define NEED_eval_pv
145 #define NEED_grok_bin
146 #define NEED_grok_hex
147 #define NEED_grok_number
148 #define NEED_grok_numeric_radix
149 #define NEED_grok_oct
150 #define NEED_my_snprintf
151 #define NEED_my_strlcat
152 #define NEED_my_strlcpy
153 #define NEED_newCONSTSUB
154 #define NEED_newRV_noinc
155 #define NEED_sv_2pv_nolen
156 #define NEED_sv_2pvbyte
157 #define NEED_sv_catpvf_mg
158 #define NEED_sv_catpvf_mg_nocontext
159 #define NEED_sv_setpvf_mg
160 #define NEED_sv_setpvf_mg_nocontext
161 #define NEED_vnewSVpvf
162 #define NEED_warner
163
164 #include "ppport.h"
165
166 #endif
167
168 static int    VARarg1;
169 static char  *VARarg2;
170 static double VARarg3;
171
172 HEAD
173
174 if (@ARGV) {
175   my %want = map { ($_ => 0) } @ARGV;
176   @f = grep { exists $want{$_->{name}} } @f;
177   for (@f) { $want{$_->{name}}++ }
178   for (keys %want) {
179     die "nothing found for '$_'\n" unless $want{$_};
180   }
181 }
182
183 my $f;
184 for $f (@f) {
185   $ignore{$f->{name}} and next;
186   $f->{flags}{A} or next;  # only public API members
187
188   $ignore{$f->{name}} = 1; # ignore duplicates
189
190   my $Perl_ = $f->{flags}{p} ? 'Perl_' : '';
191
192   my $stack = '';
193   my @arg;
194   my $aTHX = '';
195
196   my $i = 1;
197   my $ca;
198   my $varargs = 0;
199   for $ca (@{$f->{args}}) {
200     my $a = $ca->[0];
201     if ($a eq '...') {
202       $varargs = 1;
203       push @arg, qw(VARarg1 VARarg2 VARarg3);
204       last;
205     }
206     my($n, $p, $d) = $a =~ /^ (\w+(?:\s+\w+)*)\s*  # type name  => $n
207                               (\**)                # pointer    => $p
208                               (?:\s*const\s*)?     # const
209                               ((?:\[[^\]]*\])*)    # dimension  => $d
210                             $/x
211                      or die "$0 - cannot parse argument: [$a]\n";
212     if (exists $amap{$n}) {
213       push @arg, $amap{$n};
214       next;
215     }
216     $n = $tmap{$n} || $n;
217     if ($n eq 'const char' and $p eq '*' and !$f->{flags}{f}) {
218       push @arg, '"foo"';
219     }
220     else {
221       my $v = 'arg' . $i++;
222       push @arg, $v;
223       $stack .= "  static $n $p$v$d;\n";
224     }
225   }
226
227   unless ($f->{flags}{n} || $f->{flags}{'m'}) {
228     $stack = "  dTHX;\n$stack";
229     $aTHX = @arg ? 'aTHX_ ' : 'aTHX';
230   }
231
232   if ($stack{$f->{name}}) {
233     my $s = '';
234     for (@{$stack{$f->{name}}}) {
235       $s .= "  $_\n";
236     }
237     $stack = "$s$stack";
238   }
239
240   my $args = join ', ', @arg;
241   my $rvt = $f->{ret} || 'void';
242   my $ret;
243   if ($void{$rvt}) {
244     $ret = $castvoid{$f->{name}} ? '(void) ' : '';
245   }
246   else {
247     $stack .= "  $rvt rval;\n";
248     $ret = $ignorerv{$f->{name}} ? '(void) ' : "rval = ";
249   }
250   my $aTHX_args = "$aTHX$args";
251
252   unless ($f->{flags}{'m'} and @arg == 0) {
253     $args = "($args)";
254     $aTHX_args = "($aTHX_args)";
255   }
256
257   print OUT <<HEAD;
258 /******************************************************************************
259 *
260 *  $f->{name}
261 *
262 ******************************************************************************/
263
264 HEAD
265
266   if ($todo{$f->{name}}) {
267     my($ver,$sub) = $todo{$f->{name}} =~ /^5\.(\d{3})(\d{3})$/ or die;
268     for ($ver, $sub) {
269       s/^0+(\d)/$1/
270     }
271     if ($ver < 6 && $sub > 0) {
272       $sub =~ s/0$// or die;
273     }
274     print OUT "#if PERL_VERSION > $ver || (PERL_VERSION == $ver && PERL_SUBVERSION >= $sub) /* TODO */\n";
275   }
276
277   my $final = $varargs
278               ? "$Perl_$f->{name}$aTHX_args"
279               : "$f->{name}$args";
280
281   $f->{cond} and print OUT "#if $f->{cond}\n";
282
283   print OUT <<END;
284 void _DPPP_test_$f->{name} (void)
285 {
286   dXSARGS;
287 $stack
288   {
289 #ifdef $f->{name}
290     $ret$f->{name}$args;
291 #endif
292   }
293
294   {
295 #ifdef $f->{name}
296     $ret$final;
297 #else
298     $ret$Perl_$f->{name}$aTHX_args;
299 #endif
300   }
301 }
302 END
303
304   $f->{cond} and print OUT "#endif\n";
305   $todo{$f->{name}} and print OUT "#endif\n";
306
307   print OUT "\n";
308 }
309
310 @ARGV and close OUT;
311