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