Several bug fixes and additions.
[wine] / tools / winapi_check / winapi_parser.pm
1 package winapi_parser;
2
3 use strict;
4
5 sub parse_c_file {
6     my $options = shift;
7     my $output = shift;
8     my $file = shift;
9     my $function_found_callback = shift;
10     my $preprocessor_found_callback = shift;
11
12     # global
13     my $debug_channels = [];
14
15     # local
16     my $line_number = 0;
17     my $documentation;
18     my $linkage;
19     my $return_type;
20     my $calling_convention;
21     my $function = "";
22     my $argument_types;
23     my $argument_names;
24     my $argument_documentations;
25     my $statements;
26
27     my $function_begin = sub {
28         $documentation = shift;
29         $linkage = shift;
30         $return_type= shift;
31         $calling_convention = shift;
32         $function = shift;
33         $argument_types = shift;
34         $argument_names = shift;
35         $argument_documentations = shift;
36
37         if($#$argument_names == -1) {
38             foreach my $n (0..$#$argument_types) {
39                 push @$argument_names, "";
40             }
41         }
42
43         if($#$argument_documentations == -1) {
44             foreach my $n (0..$#$argument_documentations) {
45                 push @$argument_documentations, "";
46             }
47         }
48
49         $statements = undef;
50     };
51     my $function_end = sub {
52         &$function_found_callback($line_number,$debug_channels,$documentation,$linkage,$return_type,
53                                   $calling_convention,$function,$argument_types,
54                                   $argument_names,$argument_documentations,$statements);
55         $function = "";
56     };
57     my %regs_entrypoints;
58     my @comment_lines = ();
59     my @comments = ();
60     my $level = 0;
61     my $extern_c = 0;
62     my $again = 0;
63     my $lookahead = 0;
64     my $lookahead_count = 0;
65
66     print STDERR "Processing file '$file' ... " if $options->verbose;
67     open(IN, "< $file") || die "<internal>: $file: $!\n";
68     $/ = "\n";
69     while($again || defined(my $line = <IN>)) {
70         if(!$again) {
71             chomp $line;
72
73             if($lookahead) {
74                 $lookahead = 0;
75                 $_ .= "\n" . $line;
76                 $lookahead_count++;
77             } else {
78                 $_ = $line;
79                 $lookahead_count = 0;
80             }
81             print " $level($lookahead_count): $line\n" if $options->debug >= 2;
82             print "*** $_\n" if $options->debug >= 3;
83         } else {
84             $lookahead_count = 0;
85             $again = 0;
86         }
87
88         # Merge conflicts in file?
89         if(/^(<<<<<<<|=======|>>>>>>>)/) {
90             $output->write("$file: merge conflicts in file\n");
91             last;
92         }
93       
94         # remove C comments
95         if(s/^(.*?)(\/\*.*?\*\/)(.*)$/$1 $3/s) { 
96             push @comment_lines, $.; 
97             push @comments, $2; 
98             $again = 1; 
99             next;
100         }
101         if(/^(.*?)\/\*/s) {
102             $lookahead = 1;
103             next;
104         }
105
106         # remove C++ comments
107         while(s/^(.*?)\/\/.*?$/$1\n/s) { $again = 1 }
108         if($again) { next; }
109
110         # remove empty rows
111         if(/^\s*$/) { next; }
112
113         # remove preprocessor directives
114         if(s/^\s*\#/\#/m) {
115             if(/^\\#.*?\\$/m) {
116                 $lookahead = 1;
117                 next;
118             } elsif(s/^\#\s*(.*?)(\s+(.*?))?\s*$//m) {
119                 if(defined($3)) {
120                     &$preprocessor_found_callback($1, $3);
121                 } else {
122                     &$preprocessor_found_callback($1, "");
123                 }
124                 next;
125             }
126         }
127
128         # Remove extern "C"
129         if(s/^\s*extern\s+"C"\s+\{//m) { 
130             $extern_c = 1;
131             $again = 1;
132             next; 
133         }
134
135         my $documentation_line;
136         my $documentation;
137         my @argument_documentations = ();
138         {
139             my $n = $#comments;
140             while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
141                               $comments[$n] =~ /^\/\*\*+\/$/)) 
142             {
143                 $n--;
144             }
145
146             if(defined($comments[$n]) && $n >= 0) {
147                 my @lines = split(/\n/, $comments[$n]);
148
149                 $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
150                 $documentation = $comments[$n];
151
152                 for(my $m=$n+1; $m <= $#comments; $m++) {
153                     if($comments[$m] =~ /^\/\*\*+\/$/ ||
154                        $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
155                     {
156                         @argument_documentations = ();
157                         next;
158                     }
159                     push @argument_documentations, $comments[$m];
160                 }
161             } else {
162                 $documentation = "";
163             }
164         }
165
166         if($level > 0)
167         {
168             my $line = "";
169             while(/^[^\{\}]/) {
170                 s/^([^\{\}\'\"]*)//s;
171                 $line .= $1;
172                 if(s/^\'//) {
173                     $line .= "\'";
174                     while(/^./ && !s/^\'//) {
175                         s/^([^\'\\]*)//s;
176                         $line .= $1;
177                         if(s/^\\//) {
178                             $line .= "\\";
179                             if(s/^(.)//s) {
180                                 $line .= $1;
181                                 if($1 eq "0") {
182                                     s/^(\d{0,3})//s;
183                                     $line .= $1;
184                                 }
185                             }
186                         }
187                     }
188                     $line .= "\'";
189                 } elsif(s/^\"//) {
190                     $line .= "\"";
191                     while(/^./ && !s/^\"//) {
192                         s/^([^\"\\]*)//s;
193                         $line .= $1;
194                         if(s/^\\//) {
195                             $line .= "\\";
196                             if(s/^(.)//s) {
197                                 $line .= $1;
198                                 if($1 eq "0") {
199                                     s/^(\d{0,3})//s;
200                                     $line .= $1;
201                                 }
202                             }
203                         }
204                     }
205                     $line .= "\"";
206                 }
207             }
208
209             if(s/^\{//) {
210                 $_ = $'; $again = 1;
211                 $line .= "{";
212                 print "+1: \{$_\n" if $options->debug >= 2;
213                 $level++;
214             } elsif(s/^\}//) {
215                 $_ = $'; $again = 1;
216                 $line .= "}" if $level > 1;
217                 print "-1: \}$_\n" if $options->debug >= 2; 
218                 $level--;
219                 if($level == -1 && $extern_c) {
220                     $extern_c = 0;
221                     $level = 0;
222                 }
223             }
224
225             if(!defined($statements)) {
226                 $statements = "";
227             }
228
229             if($line !~ /^\s*$/) {
230                 $statements .= "$line\n";
231             }
232
233             if($function && $level == 0) {
234                 &$function_end;
235             }
236             next;           
237         } elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+)?\w+((\s*\*)+\s*|\s+))
238             ((__cdecl|__stdcall|CDECL|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
239             (\w+(\(\w+\))?)\s*\(([^\)]*)\)\s*(\{|\;)/sx)
240         {
241             my @lines = split(/\n/, $&);
242             my $function_line = $. - scalar(@lines) + 1;
243
244             # FIXME: Should be separate for documentation and function
245             $line_number = $documentation_line;
246
247             $_ = $'; $again = 1;
248
249             if($11 eq "{")  {
250                 $level++;
251             }
252
253             my $linkage = $1;
254             my $return_type = $2;
255             my $calling_convention = $7;
256             my $name = $8;
257             my $arguments = $10;
258
259             if(!defined($linkage)) {
260                 $linkage = "";
261             }
262
263             if(!defined($calling_convention)) {
264                 $calling_convention = "";
265             }
266
267             $linkage =~ s/\s*$//;
268
269             $return_type =~ s/\s*$//;
270             $return_type =~ s/\s*\*\s*/*/g;
271             $return_type =~ s/(\*+)/ $1/g;
272
273             if($regs_entrypoints{$name}) {
274                 $name = $regs_entrypoints{$name};
275             } 
276
277             $arguments =~ y/\t\n/  /;
278             $arguments =~ s/^\s*(.*?)\s*$/$1/;
279             if($arguments eq "") { $arguments = "..." }
280
281             my @argument_types;
282             my @argument_names;
283             my @arguments = split(/,/, $arguments);
284             foreach my $n (0..$#arguments) {
285                 my $argument_type = "";
286                 my $argument_name = "";
287                 my $argument = $arguments[$n];
288                 $argument =~ s/^\s*(.*?)\s*$/$1/;
289                 # print "  " . ($n + 1) . ": '$argument'\n";
290                 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
291                 $argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
292                 if($argument =~ /^\.\.\.$/) {
293                     $argument_type = "...";
294                     $argument_name = "...";
295                 } elsif($argument =~ /^
296                         ((?:struct\s+|union\s+|enum\s+|(?:signed\s+|unsigned\s+)
297                           (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
298                         ((?:const)?\s*(?:\*\s*?)*)\s*
299                         (?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL)?/x)
300                 {
301                     $argument_type = "$1";
302                     if($2 ne "") {
303                         $argument_type .= " $2";
304                     }
305                     $argument_name = $3;
306
307                     $argument_type =~ s/\s*const\s*/ /;
308                     $argument_type =~ s/^\s*(.*?)\s*$/$1/;
309
310                     $argument_name =~ s/^\s*(.*?)\s*$/$1/;
311                 } else {
312                     die "$file: $.: syntax error: '$argument'\n";
313                 }
314                 $argument_types[$n] = $argument_type;
315                 $argument_names[$n] = $argument_name;
316                 # print "  " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
317             }
318             if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
319                 $#argument_types = -1;
320                 $#argument_names = -1;  
321             }
322
323             if($options->debug) {
324                 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
325             }
326             
327             &$function_begin($documentation,$linkage,$return_type,$calling_convention,$name,\@argument_types,\@argument_names,\@argument_documentations);
328             if($level == 0) {
329                 &$function_end;
330             }
331         } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
332             $_ = $'; $again = 1;
333             my @arguments = ();
334             &$function_begin($documentation, "", "void", "__asm", $1, \@arguments);
335             &$function_end;
336         } elsif(/DC_(GET_X_Y|GET_VAL_16)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
337             $_ = $'; $again = 1;
338             my @arguments = ("HDC16");
339             &$function_begin($documentation, "", $2, "WINAPI", $3, \@arguments);
340             &$function_end;
341         } elsif(/DC_(GET_VAL)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,.*?\)/s) {
342             $_ = $'; $again = 1;
343             my $return16 = $3 . "16";
344             my $return32 = $3;
345             my $name16 = $2 . "16";
346             my $name32 = $2;
347             my @arguments16 = ("HDC16");
348             my @arguments32 = ("HDC");
349
350             if($name16 eq "COLORREF16") { $name16 = "COLORREF"; }
351
352             &$function_begin($documentation, "", $name16, "WINAPI", $return16, \@arguments16);
353             &$function_end;
354             &$function_begin($documentation, "", $name32, "WINAPI", $return32, \@arguments32);
355             &$function_end;
356         } elsif(/DC_(GET_VAL_EX)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
357             $_ = $'; $again = 1;
358             my @arguments16 = ("HDC16", "LP" . $5 . "16");
359             my @arguments32 = ("HDC", "LP" . $5);
360             &$function_begin($documentation, "", "BOOL16", "WINAPI", $2 . "16", \@arguments16);
361             &$function_end;
362             &$function_begin($documentation, "", "BOOL", "WINAPI", $2, \@arguments32);
363             &$function_end;
364         } elsif(/DC_(SET_MODE)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
365             $_ = $'; $again = 1;
366             my @arguments16 = ("HDC16", "INT16");
367             my @arguments32 = ("HDC", "INT");
368             &$function_begin($documentation, "", "INT16", "WINAPI", $2 . "16", \@arguments16);
369             &$function_end;
370             &$function_begin($documentation, "", "INT", "WINAPI", $2, \@arguments32);
371             &$function_end;
372         } elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
373             $_ = $'; $again = 1;
374             my @arguments16 = ("HWAVEIN16");
375             my @arguments32 = ("HWAVEIN");
376             &$function_begin($documentation, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
377             &$function_end;
378             &$function_begin($documentation, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
379             &$function_end;         
380         } elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
381             $_ = $'; $again = 1;
382             my @arguments16 = ("HWAVEOUT16");
383             my @arguments32 = ("HWAVEOUT");
384             &$function_begin($documentation, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
385             &$function_end;
386             &$function_begin($documentation, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);          
387             &$function_end;
388         } elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
389             $_ = $'; $again = 1;
390             if($1 eq "1") {
391                 my @arguments16 = ("HWAVEOUT16", $4);
392                 my @arguments32 = ("HWAVEOUT", $4);
393                 &$function_begin($documentation, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
394                 &$function_end;
395                 &$function_begin($documentation, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
396                 &$function_end;
397             } elsif($1 eq 2) {
398                 my @arguments16 = ("UINT16", $4);
399                 my @arguments32 = ("UINT", $4);
400                 &$function_begin($documentation, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
401                 &$function_end;
402                 &$function_begin($documentation, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
403                 &$function_end;
404             }
405         } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
406             $_ = $'; $again = 1;
407             $regs_entrypoints{$2} = $1;
408         } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
409             $_ = $'; $again = 1;
410             unshift @$debug_channels, $1;
411         } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
412             $_ = $'; $again = 1;
413             push @$debug_channels, $1;
414         } elsif(/\'[^\']*\'/s) {
415             $_ = $'; $again = 1;
416         } elsif(/\"[^\"]*\"/s) {
417             $_ = $'; $again = 1;
418         } elsif(/;/s) {
419             $_ = $'; $again = 1;
420         } elsif(/extern\s+"C"\s+{/s) {
421             $_ = $'; $again = 1;
422         } elsif(/\{/s) {
423             $_ = $'; $again = 1;
424             print "+1: $_\n" if $options->debug >= 2;
425             $level++;
426         } else {
427             $lookahead = 1;
428         }
429     }
430     close(IN);
431     print STDERR "done\n" if $options->verbose;
432     $output->write("$file: not at toplevel at end of file\n") unless $level == 0;
433 }
434
435 1;