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