Fixed one more HeapReAlloc call.
[wine] / tools / winapi_check / winapi_parser.pm
1 #
2 # Copyright 1999, 2000, 2001 Patrik Stridvall
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 #
18
19 package winapi_parser;
20
21 use strict;
22
23 use output qw($output);
24 use options qw($options);
25
26 sub parse_c_file {
27     my $file = shift;
28     my $callbacks = shift;
29
30     my $empty_callback = sub { };
31
32     my $c_comment_found_callback = $$callbacks{c_comment_found} || $empty_callback;
33     my $cplusplus_comment_found_callback = $$callbacks{cplusplus_comment_found} || $empty_callback;
34     my $function_create_callback = $$callbacks{function_create} || $empty_callback;
35     my $function_found_callback = $$callbacks{function_found} || $empty_callback;
36     my $type_create_callback = $$callbacks{type_create} || $empty_callback;
37     my $type_found_callback = $$callbacks{type_found} || $empty_callback;
38     my $preprocessor_found_callback = $$callbacks{preprocessor_found} || $empty_callback;
39
40     # global
41     my $debug_channels = [];
42
43     my $in_function = 0;
44     my $function_begin;
45     my $function_end;
46     {
47         my $documentation_line;
48         my $documentation;
49         my $function_line;
50         my $linkage;
51         my $return_type;
52         my $calling_convention;
53         my $internal_name = "";
54         my $argument_types;
55         my $argument_names;
56         my $argument_documentations;
57         my $statements_line;
58         my $statements;
59
60         $function_begin = sub {
61             $documentation_line = shift;
62             $documentation = shift;
63             $function_line = shift;
64             $linkage = shift;
65             $return_type= shift;
66             $calling_convention = shift;
67             $internal_name = shift;
68             $argument_types = shift;
69             $argument_names = shift;
70             $argument_documentations = shift;
71
72             if(defined($argument_names) && defined($argument_types) &&
73                $#$argument_names == -1)
74             {
75                 foreach my $n (0..$#$argument_types) {
76                     push @$argument_names, "";
77                 }
78             }
79
80             if(defined($argument_documentations) &&
81                $#$argument_documentations == -1)
82             {
83                 foreach my $n (0..$#$argument_documentations) {
84                     push @$argument_documentations, "";
85                 }
86             }
87
88             $in_function = 1;
89         };
90
91         $function_end = sub {
92             $statements_line = shift;
93             $statements = shift;
94
95             my $function = &$function_create_callback();
96
97             if(!defined($documentation_line)) {
98                 $documentation_line = 0;
99             }
100
101             $function->file($file);
102             $function->debug_channels([@$debug_channels]);
103             $function->documentation_line($documentation_line);
104             $function->documentation($documentation);
105             $function->function_line($function_line);
106             $function->linkage($linkage);
107             $function->return_type($return_type);
108             $function->calling_convention($calling_convention);
109             $function->internal_name($internal_name);
110             if(defined($argument_types)) {
111                 $function->argument_types([@$argument_types]);
112             }
113             if(defined($argument_names)) {
114                 $function->argument_names([@$argument_names]);
115             }
116             if(defined($argument_documentations)) {
117                 $function->argument_documentations([@$argument_documentations]);
118             }
119             $function->statements_line($statements_line);
120             $function->statements($statements);
121
122             &$function_found_callback($function);
123
124             $in_function = 0;
125         };
126     }
127
128     my $in_type = 0;
129     my $type_begin;
130     my $type_end;
131     {
132         my $type;
133
134         $type_begin = sub {
135             $type = shift;
136             $in_type = 1;
137         };
138
139         $type_end = sub {
140             my $names = shift;
141
142             foreach my $name (@$names) {
143                 if($type =~ /^(?:struct|enum)/) {
144                     # $output->write("typedef $type {\n");
145                     # $output->write("} $name;\n");
146                 } else {
147                     # $output->write("typedef $type $name;\n");
148                 }
149             }
150             $in_type = 0;
151         };
152     }
153
154     my %regs_entrypoints;
155     my @comment_lines = ();
156     my @comments = ();
157     my $statements_line;
158     my $statements;
159     my $level = 0;
160     my $extern_c = 0;
161     my $again = 0;
162     my $lookahead = 0;
163     my $lookahead_count = 0;
164
165     print STDERR "Processing file '$file' ... " if $options->verbose;
166     open(IN, "< $file") || die "<internal>: $file: $!\n";
167     local $_ = "";
168     while($again || defined(my $line = <IN>)) {
169         $_ = "" if !defined($_);
170         if(!$again) {
171             chomp $line;
172
173             if($lookahead) {
174                 $lookahead = 0;
175                 $_ .= "\n" . $line;
176                 $lookahead_count++;
177             } else {
178                 $_ = $line;
179                 $lookahead_count = 0;
180             }
181             $output->write(" $level($lookahead_count): $line\n") if $options->debug >= 2;
182             $output->write("*** $_\n") if $options->debug >= 3;
183         } else {
184             $lookahead_count = 0;
185             $again = 0;
186         }
187
188         # CVS merge conflicts in file?
189         if(/^(<<<<<<<|=======|>>>>>>>)/) {
190             $output->write("$file: merge conflicts in file\n");
191             last;
192         }
193
194         # remove C comments
195         if(s/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)(?=\/\*)//s) {
196             my $prefix = $1;
197             if(s/^(\/\*.*?\*\/)//s) {
198                 my @lines = split(/\n/, $1);
199                 push @comment_lines, $.;
200                 push @comments, $1;
201                 &$c_comment_found_callback($. - $#lines, $., $1);
202                 if($#lines <= 0) {
203                     $_ = "$prefix $_";
204                 } else {
205                     $_ = $prefix . ("\n" x $#lines) . $_;
206                 }
207                 $again = 1;
208             } else {
209                 $_ = "$prefix$_";
210                 $lookahead = 1;
211             }
212             next;
213         }
214
215         # remove C++ comments
216         while(s/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)(\/\/.*?)$/$1/s) {
217             &$cplusplus_comment_found_callback($., $2);
218             $again = 1;
219         }
220         if($again) { next; }
221
222         # remove preprocessor directives
223         if(s/^\s*\#/\#/s) {
224             if(/^(\#.*?)\\$/s) {
225                 $_ = "$1\n";
226                 $lookahead = 1;
227                 next;
228             } elsif(s/^\#\s*(\w+)((?:\s+(.*?))?\s*)$//s) {
229                 my @lines = split(/\n/, $2);
230                 if($#lines > 0) {
231                     $_ = "\n" x $#lines;
232                 }
233                 if(defined($3)) {
234                     &$preprocessor_found_callback($1, $3);
235                 } else {
236                     &$preprocessor_found_callback($1, "");
237                 }
238                 $again = 1;
239                 next;
240             }
241         }
242
243         # Remove extern "C"
244         if(s/^\s*extern\s+"C"\s+\{//m) {
245             $extern_c = 1;
246             $again = 1;
247             next;
248         }
249
250         my $documentation_line;
251         my $documentation;
252         my @argument_documentations = ();
253         {
254             my $n = $#comments;
255             while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
256                               $comments[$n] =~ /^\/\*\*+\/$/))
257             {
258                 $n--;
259             }
260
261             if(defined($comments[$n]) && $n >= 0) {
262                 my @lines = split(/\n/, $comments[$n]);
263
264                 $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
265                 $documentation = $comments[$n];
266
267                 for(my $m=$n+1; $m <= $#comments; $m++) {
268                     if($comments[$m] =~ /^\/\*\*+\/$/ ||
269                        $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
270                     {
271                         @argument_documentations = ();
272                         next;
273                     }
274                     push @argument_documentations, $comments[$m];
275                 }
276             } else {
277                 $documentation = "";
278             }
279         }
280
281         if($level > 0)
282         {
283             my $line = "";
284             while(/^[^\{\}]/) {
285                 s/^([^\{\}\'\"]*)//s;
286                 $line .= $1;
287                 if(s/^\'//) {
288                     $line .= "\'";
289                     while(/^./ && !s/^\'//) {
290                         s/^([^\'\\]*)//s;
291                         $line .= $1;
292                         if(s/^\\//) {
293                             $line .= "\\";
294                             if(s/^(.)//s) {
295                                 $line .= $1;
296                                 if($1 eq "0") {
297                                     s/^(\d{0,3})//s;
298                                     $line .= $1;
299                                 }
300                             }
301                         }
302                     }
303                     $line .= "\'";
304                 } elsif(s/^\"//) {
305                     $line .= "\"";
306                     while(/^./ && !s/^\"//) {
307                         s/^([^\"\\]*)//s;
308                         $line .= $1;
309                         if(s/^\\//) {
310                             $line .= "\\";
311                             if(s/^(.)//s) {
312                                 $line .= $1;
313                                 if($1 eq "0") {
314                                     s/^(\d{0,3})//s;
315                                     $line .= $1;
316                                 }
317                             }
318                         }
319                     }
320                     $line .= "\"";
321                 }
322             }
323
324             if(s/^\{//) {
325                 $_ = $'; $again = 1;
326                 $line .= "{";
327                 print "+1: \{$_\n" if $options->debug >= 2;
328                 $level++;
329                 $statements .= $line;
330             } elsif(s/^\}//) {
331                 $_ = $'; $again = 1;
332                 $line .= "}" if $level > 1;
333                 print "-1: \}$_\n" if $options->debug >= 2;
334                 $level--;
335                 if($level == -1 && $extern_c) {
336                     $extern_c = 0;
337                     $level = 0;
338                 }
339                 $statements .= $line;
340             } else {
341                 $statements .= "$line\n";
342             }
343
344             if($level == 0) {
345                 if($in_function) {
346                     &$function_end($statements_line, $statements);
347                     $statements = undef;
348                 } elsif($in_type) {
349                     if(/^\s*(?:WINE_PACKED\s+)?((?:\*\s*)?\w+\s*(?:\s*,\s*(?:\*+\s*)?\w+)*\s*);/s) {
350                         my @parts = split(/\s*,\s*/, $1);
351                         &$type_end([@parts]);
352                     } elsif(/;/s) {
353                         die "$file: $.: syntax error: '$_'\n";
354                     } else {
355                         $lookahead = 1;
356                     }
357                 }
358             }
359             next;
360         } elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
361             ((__cdecl|__stdcall|CDECL|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
362             (\w+(\(\w+\))?)\s*\(([^\)]*)\)\s*(\{|\;)/sx)
363         {
364             my @lines = split(/\n/, $&);
365             my $function_line = $. - scalar(@lines) + 1;
366
367             $_ = $'; $again = 1;
368
369             if($11 eq "{")  {
370                 $level++;
371             }
372
373             my $linkage = $1;
374             my $return_type = $2;
375             my $calling_convention = $7;
376             my $name = $8;
377             my $arguments = $10;
378
379             if(!defined($linkage)) {
380                 $linkage = "";
381             }
382
383             if(!defined($calling_convention)) {
384                 $calling_convention = "";
385             }
386
387             $linkage =~ s/\s*$//;
388
389             $return_type =~ s/\s*$//;
390             $return_type =~ s/\s*\*\s*/*/g;
391             $return_type =~ s/(\*+)/ $1/g;
392
393             if($regs_entrypoints{$name}) {
394                 $name = $regs_entrypoints{$name};
395             }
396
397             $arguments =~ y/\t\n/  /;
398             $arguments =~ s/^\s*(.*?)\s*$/$1/;
399             if($arguments eq "") { $arguments = "..." }
400
401             my @argument_types;
402             my @argument_names;
403             my @arguments = split(/,/, $arguments);
404             foreach my $n (0..$#arguments) {
405                 my $argument_type = "";
406                 my $argument_name = "";
407                 my $argument = $arguments[$n];
408                 $argument =~ s/^\s*(.*?)\s*$/$1/;
409                 # print "  " . ($n + 1) . ": '$argument'\n";
410                 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
411                 $argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
412                 if($argument =~ /^\.\.\.$/) {
413                     $argument_type = "...";
414                     $argument_name = "...";
415                 } elsif($argument =~ /^
416                         ((?:struct\s+|union\s+|enum\s+|(?:signed\s+|unsigned\s+)
417                           (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
418                         ((?:const)?\s*(?:\*\s*?)*)\s*
419                         (?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL)?/x)
420                 {
421                     $argument_type = "$1";
422                     if($2 ne "") {
423                         $argument_type .= " $2";
424                     }
425                     $argument_name = $3;
426
427                     $argument_type =~ s/\s*const\s*/ /;
428                     $argument_type =~ s/^\s*(.*?)\s*$/$1/;
429
430                     $argument_name =~ s/^\s*(.*?)\s*$/$1/;
431                 } else {
432                     die "$file: $.: syntax error: '$argument'\n";
433                 }
434                 $argument_types[$n] = $argument_type;
435                 $argument_names[$n] = $argument_name;
436                 # print "  " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
437             }
438             if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
439                 $#argument_types = -1;
440                 $#argument_names = -1;
441             }
442
443             if($options->debug) {
444                 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
445             }
446
447             &$function_begin($documentation_line, $documentation,
448                              $function_line, $linkage, $return_type, $calling_convention, $name,
449                              \@argument_types,\@argument_names,\@argument_documentations);
450             if($level == 0) {
451                 &$function_end(undef, undef);
452             }
453             $statements_line = $.;
454             $statements = "";
455         } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
456             my @lines = split(/\n/, $&);
457             my $function_line = $. - scalar(@lines) + 1;
458
459             $_ = $'; $again = 1;
460
461             &$function_begin($documentation_line, $documentation,
462                              $function_line, "", "void", "__asm", $1);
463             &$function_end($., "");
464         } elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
465             my @lines = split(/\n/, $&);
466             my $function_line = $. - scalar(@lines) + 1;
467
468             $_ = $'; $again = 1;
469             my @arguments16 = ("HWAVEIN16");
470             my @arguments32 = ("HWAVEIN");
471             &$function_begin($documentation_line, $documentation,
472                              $function_line,  "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
473             &$function_end($., "");
474             &$function_begin($documentation_line, $documentation,
475                              $function_line, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
476             &$function_end($., "");
477         } elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
478             my @lines = split(/\n/, $&);
479             my $function_line = $. - scalar(@lines) + 1;
480
481             $_ = $'; $again = 1;
482
483             my @arguments16 = ("HWAVEOUT16");
484             my @arguments32 = ("HWAVEOUT");
485             &$function_begin($documentation_line, $documentation,
486                              $function_line, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
487             &$function_end($., "");
488             &$function_begin($documentation_line, $documentation,
489                              $function_line, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
490             &$function_end($., "");
491         } elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
492             my @lines = split(/\n/, $&);
493             my $function_line = $. - scalar(@lines) + 1;
494
495             $_ = $'; $again = 1;
496
497             if($1 eq "1") {
498                 my @arguments16 = ("HWAVEOUT16", $4);
499                 my @arguments32 = ("HWAVEOUT", $4);
500                 &$function_begin($documentation_line, $documentation,
501                                  $function_line, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
502                 &$function_end($., "");
503                 &$function_begin($documentation_line, $documentation,
504                                  $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
505                 &$function_end($., "");
506             } elsif($1 eq 2) {
507                 my @arguments16 = ("UINT16", $4);
508                 my @arguments32 = ("UINT", $4);
509                 &$function_begin($documentation_line, $documentation,
510                                  $function_line, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
511                 &$function_end($., "");
512                 &$function_begin($documentation_line, $documentation,
513                                  $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
514                 &$function_end($., "");
515             }
516         } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
517             $_ = $'; $again = 1;
518             $regs_entrypoints{$2} = $1;
519         } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
520             $_ = $'; $again = 1;
521             unshift @$debug_channels, $1;
522         } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
523             $_ = $'; $again = 1;
524             push @$debug_channels, $1;
525         } elsif(/typedef\s+(enum|struct|union)(?:\s+(\w+))?\s*\{/s) {
526             $_ = $'; $again = 1;
527             $level++;
528             my $type = $1;
529             if(defined($2)) {
530                $type .= " $2";
531             }
532             &$type_begin($type);
533         } elsif(/typedef\s+
534                 ((?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
535                 (\w+)
536                 (?:\s+const)?
537                 ((?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])*
538                 (?:\s*,\s*(?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
539                 \s*;/sx)
540         {
541             $_ = $'; $again = 1;
542
543             my $type = "$1 $2";
544
545             my @names;
546             my @parts = split(/\s*,\s*/, $2);
547             foreach my $part (@parts) {
548                 if($part =~ /(?:\s*(\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
549                     my $name = $2;
550                     if(defined($1)) {
551                         $name = "$1$2";
552                     }
553                     if(defined($3)) {
554                         $name .= $3;
555                     }
556                     push @names, $name;
557                 }
558             }
559             &$type_begin($type);
560             &$type_end([@names]);
561         } elsif(/typedef\s+
562                 (?:(?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
563                 (\w+(?:\s*\*+\s*)?)\s+
564                 (?:(\w+)\s*)?
565                 \((?:(\w+)\s*)?\s*\*\s*(\w+)\s*\)\s*
566                 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
567         {
568             $_ = $'; $again = 1;
569             my $type;
570             if(defined($2) || defined($3)) {
571                 my $cc = $2 || $3;
572                 if(defined($5)) {
573                     $type = "$1 ($cc *)($5)";
574                 } else {
575                     $type = "$1 ($cc *)[$6]";
576                 }
577             } else {
578                 if(defined($5)) {
579                     $type = "$1 (*)($5)";
580                 } else {
581                     $type = "$1 (*)[$6]";
582                 }
583             }
584             my $name = $4;
585             &$type_begin($type);
586             &$type_end([$name]);
587         } elsif(/typedef[^\{;]*;/s) {
588             $_ = $'; $again = 1;
589             $output->write("$file: $.: can't parse: '$&'\n");
590         } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
591             $_ = $'; $again = 1;
592             $output->write("$file: $.: can't parse: '$&'\n");
593         } elsif(/\'[^\']*\'/s) {
594             $_ = $'; $again = 1;
595         } elsif(/\"[^\"]*\"/s) {
596             $_ = $'; $again = 1;
597         } elsif(/;/s) {
598             $_ = $'; $again = 1;
599         } elsif(/extern\s+"C"\s+{/s) {
600             $_ = $'; $again = 1;
601         } elsif(/\{/s) {
602             $_ = $'; $again = 1;
603             print "+1: $_\n" if $options->debug >= 2;
604             $level++;
605         } else {
606             $lookahead = 1;
607         }
608     }
609     close(IN);
610     print STDERR "done\n" if $options->verbose;
611     $output->write("$file: not at toplevel at end of file\n") unless $level == 0;
612 }
613
614 1;