Release 20030408.
[wine/gsoc-2012-control.git] / tools / winapi_check / winapi_parser.pm
blob01962dd84204dd4589303a754b359ff41200ddf6
2 # Copyright 1999, 2000, 2001 Patrik Stridvall
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.
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.
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
19 package winapi_parser;
21 use strict;
23 use output qw($output);
24 use options qw($options);
26 sub parse_c_file {
27 my $file = shift;
28 my $callbacks = shift;
30 my $empty_callback = sub { };
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;
40 # global
41 my $debug_channels = [];
43 my $in_function = 0;
44 my $function_begin;
45 my $function_end;
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;
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;
72 if(defined($argument_names) && defined($argument_types) &&
73 $#$argument_names == -1)
75 foreach my $n (0..$#$argument_types) {
76 push @$argument_names, "";
80 if(defined($argument_documentations) &&
81 $#$argument_documentations == -1)
83 foreach my $n (0..$#$argument_documentations) {
84 push @$argument_documentations, "";
88 $in_function = 1;
91 $function_end = sub {
92 $statements_line = shift;
93 $statements = shift;
95 my $function = &$function_create_callback();
97 if(!defined($documentation_line)) {
98 $documentation_line = 0;
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]);
113 if(defined($argument_names)) {
114 $function->argument_names([@$argument_names]);
116 if(defined($argument_documentations)) {
117 $function->argument_documentations([@$argument_documentations]);
119 $function->statements_line($statements_line);
120 $function->statements($statements);
122 &$function_found_callback($function);
124 $in_function = 0;
128 my $in_type = 0;
129 my $type_begin;
130 my $type_end;
132 my $type;
134 $type_begin = sub {
135 $type = shift;
136 $in_type = 1;
139 $type_end = sub {
140 my $names = shift;
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");
150 $in_type = 0;
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;
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;
173 if($lookahead) {
174 $lookahead = 0;
175 $_ .= "\n" . $line;
176 $lookahead_count++;
177 } else {
178 $_ = $line;
179 $lookahead_count = 0;
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;
188 # CVS merge conflicts in file?
189 if(/^(<<<<<<<|=======|>>>>>>>)/) {
190 $output->write("$file: merge conflicts in file\n");
191 last;
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) . $_;
207 $again = 1;
208 } else {
209 $_ = "$prefix$_";
210 $lookahead = 1;
212 next;
215 # remove C++ comments
216 while(s/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)(\/\/.*?)$/$1/s) {
217 &$cplusplus_comment_found_callback($., $2);
218 $again = 1;
220 if($again) { next; }
222 # remove preprocessor directives
223 if(s/^\s*\#/\#/s) {
224 if(/^\#.*?\\$/s) {
225 $lookahead = 1;
226 next;
227 } elsif(s/^\#\s*(\w+)((?:\s+(.*?))?\s*)$//s) {
228 my @lines = split(/\n/, $2);
229 if($#lines > 0) {
230 $_ = "\n" x $#lines;
232 if(defined($3)) {
233 &$preprocessor_found_callback($1, $3);
234 } else {
235 &$preprocessor_found_callback($1, "");
237 $again = 1;
238 next;
242 # Remove extern "C"
243 if(s/^\s*extern\s+"C"\s+\{//m) {
244 $extern_c = 1;
245 $again = 1;
246 next;
249 my $documentation_line;
250 my $documentation;
251 my @argument_documentations = ();
253 my $n = $#comments;
254 while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
255 $comments[$n] =~ /^\/\*\*+\/$/))
257 $n--;
260 if(defined($comments[$n]) && $n >= 0) {
261 my @lines = split(/\n/, $comments[$n]);
263 $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
264 $documentation = $comments[$n];
266 for(my $m=$n+1; $m <= $#comments; $m++) {
267 if($comments[$m] =~ /^\/\*\*+\/$/ ||
268 $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
270 @argument_documentations = ();
271 next;
273 push @argument_documentations, $comments[$m];
275 } else {
276 $documentation = "";
280 if($level > 0)
282 my $line = "";
283 while(/^[^\{\}]/) {
284 s/^([^\{\}\'\"]*)//s;
285 $line .= $1;
286 if(s/^\'//) {
287 $line .= "\'";
288 while(/^./ && !s/^\'//) {
289 s/^([^\'\\]*)//s;
290 $line .= $1;
291 if(s/^\\//) {
292 $line .= "\\";
293 if(s/^(.)//s) {
294 $line .= $1;
295 if($1 eq "0") {
296 s/^(\d{0,3})//s;
297 $line .= $1;
302 $line .= "\'";
303 } elsif(s/^\"//) {
304 $line .= "\"";
305 while(/^./ && !s/^\"//) {
306 s/^([^\"\\]*)//s;
307 $line .= $1;
308 if(s/^\\//) {
309 $line .= "\\";
310 if(s/^(.)//s) {
311 $line .= $1;
312 if($1 eq "0") {
313 s/^(\d{0,3})//s;
314 $line .= $1;
319 $line .= "\"";
323 if(s/^\{//) {
324 $_ = $'; $again = 1;
325 $line .= "{";
326 print "+1: \{$_\n" if $options->debug >= 2;
327 $level++;
328 $statements .= $line;
329 } elsif(s/^\}//) {
330 $_ = $'; $again = 1;
331 $line .= "}" if $level > 1;
332 print "-1: \}$_\n" if $options->debug >= 2;
333 $level--;
334 if($level == -1 && $extern_c) {
335 $extern_c = 0;
336 $level = 0;
338 $statements .= $line;
339 } else {
340 $statements .= "$line\n";
343 if($level == 0) {
344 if($in_function) {
345 &$function_end($statements_line, $statements);
346 $statements = undef;
347 } elsif($in_type) {
348 if(/^\s*(?:WINE_PACKED\s+)?((?:\*\s*)?\w+\s*(?:\s*,\s*(?:\*+\s*)?\w+)*\s*);/s) {
349 my @parts = split(/\s*,\s*/, $1);
350 &$type_end([@parts]);
351 } elsif(/;/s) {
352 die "$file: $.: syntax error: '$_'\n";
353 } else {
354 $lookahead = 1;
358 next;
359 } elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
360 ((__cdecl|__stdcall|CDECL|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
361 (\w+(\(\w+\))?)\s*\(([^\)]*)\)\s*(\{|\;)/sx)
363 my @lines = split(/\n/, $&);
364 my $function_line = $. - scalar(@lines) + 1;
366 $_ = $'; $again = 1;
368 if($11 eq "{") {
369 $level++;
372 my $linkage = $1;
373 my $return_type = $2;
374 my $calling_convention = $7;
375 my $name = $8;
376 my $arguments = $10;
378 if(!defined($linkage)) {
379 $linkage = "";
382 if(!defined($calling_convention)) {
383 $calling_convention = "";
386 $linkage =~ s/\s*$//;
388 $return_type =~ s/\s*$//;
389 $return_type =~ s/\s*\*\s*/*/g;
390 $return_type =~ s/(\*+)/ $1/g;
392 if($regs_entrypoints{$name}) {
393 $name = $regs_entrypoints{$name};
396 $arguments =~ y/\t\n/ /;
397 $arguments =~ s/^\s*(.*?)\s*$/$1/;
398 if($arguments eq "") { $arguments = "..." }
400 my @argument_types;
401 my @argument_names;
402 my @arguments = split(/,/, $arguments);
403 foreach my $n (0..$#arguments) {
404 my $argument_type = "";
405 my $argument_name = "";
406 my $argument = $arguments[$n];
407 $argument =~ s/^\s*(.*?)\s*$/$1/;
408 # print " " . ($n + 1) . ": '$argument'\n";
409 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
410 $argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
411 if($argument =~ /^\.\.\.$/) {
412 $argument_type = "...";
413 $argument_name = "...";
414 } elsif($argument =~ /^
415 ((?:struct\s+|union\s+|enum\s+|(?:signed\s+|unsigned\s+)
416 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
417 ((?:const)?\s*(?:\*\s*?)*)\s*
418 (?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL)?/x)
420 $argument_type = "$1";
421 if($2 ne "") {
422 $argument_type .= " $2";
424 $argument_name = $3;
426 $argument_type =~ s/\s*const\s*/ /;
427 $argument_type =~ s/^\s*(.*?)\s*$/$1/;
429 $argument_name =~ s/^\s*(.*?)\s*$/$1/;
430 } else {
431 die "$file: $.: syntax error: '$argument'\n";
433 $argument_types[$n] = $argument_type;
434 $argument_names[$n] = $argument_name;
435 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
437 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
438 $#argument_types = -1;
439 $#argument_names = -1;
442 if($options->debug) {
443 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
446 &$function_begin($documentation_line, $documentation,
447 $function_line, $linkage, $return_type, $calling_convention, $name,
448 \@argument_types,\@argument_names,\@argument_documentations);
449 if($level == 0) {
450 &$function_end(undef, undef);
452 $statements_line = $.;
453 $statements = "";
454 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
455 my @lines = split(/\n/, $&);
456 my $function_line = $. - scalar(@lines) + 1;
458 $_ = $'; $again = 1;
460 &$function_begin($documentation_line, $documentation,
461 $function_line, "", "void", "__asm", $1);
462 &$function_end($., "");
463 } elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
464 my @lines = split(/\n/, $&);
465 my $function_line = $. - scalar(@lines) + 1;
467 $_ = $'; $again = 1;
468 my @arguments16 = ("HWAVEIN16");
469 my @arguments32 = ("HWAVEIN");
470 &$function_begin($documentation_line, $documentation,
471 $function_line, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
472 &$function_end($., "");
473 &$function_begin($documentation_line, $documentation,
474 $function_line, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
475 &$function_end($., "");
476 } elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
477 my @lines = split(/\n/, $&);
478 my $function_line = $. - scalar(@lines) + 1;
480 $_ = $'; $again = 1;
482 my @arguments16 = ("HWAVEOUT16");
483 my @arguments32 = ("HWAVEOUT");
484 &$function_begin($documentation_line, $documentation,
485 $function_line, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
486 &$function_end($., "");
487 &$function_begin($documentation_line, $documentation,
488 $function_line, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
489 &$function_end($., "");
490 } elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
491 my @lines = split(/\n/, $&);
492 my $function_line = $. - scalar(@lines) + 1;
494 $_ = $'; $again = 1;
496 if($1 eq "1") {
497 my @arguments16 = ("HWAVEOUT16", $4);
498 my @arguments32 = ("HWAVEOUT", $4);
499 &$function_begin($documentation_line, $documentation,
500 $function_line, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
501 &$function_end($., "");
502 &$function_begin($documentation_line, $documentation,
503 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
504 &$function_end($., "");
505 } elsif($1 eq 2) {
506 my @arguments16 = ("UINT16", $4);
507 my @arguments32 = ("UINT", $4);
508 &$function_begin($documentation_line, $documentation,
509 $function_line, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
510 &$function_end($., "");
511 &$function_begin($documentation_line, $documentation,
512 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
513 &$function_end($., "");
515 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
516 $_ = $'; $again = 1;
517 $regs_entrypoints{$2} = $1;
518 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
519 $_ = $'; $again = 1;
520 unshift @$debug_channels, $1;
521 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
522 $_ = $'; $again = 1;
523 push @$debug_channels, $1;
524 } elsif(/typedef\s+(enum|struct|union)(?:\s+(\w+))?\s*\{/s) {
525 $_ = $'; $again = 1;
526 $level++;
527 my $type = $1;
528 if(defined($2)) {
529 $type .= " $2";
531 &$type_begin($type);
532 } elsif(/typedef\s+
533 ((?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
534 (\w+)
535 (?:\s+const)?
536 ((?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?
537 (?:\s*,\s*(?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
538 \s*;/sx)
540 $_ = $'; $again = 1;
542 my $type = "$1 $2";
544 my @names;
545 my @parts = split(/\s*,\s*/, $2);
546 foreach my $part (@parts) {
547 if($part =~ /(?:\s*(\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
548 my $name = $2;
549 if(defined($1)) {
550 $name = "$1$2";
552 if(defined($3)) {
553 $name .= $3;
555 push @names, $name;
558 &$type_begin($type);
559 &$type_end([@names]);
560 } elsif(/typedef\s+
561 (?:(?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
562 (\w+(?:\s*\*+\s*)?)\s+
563 (?:(\w+)\s*)?
564 \((?:(\w+)\s*)?\s*\*\s*(\w+)\s*\)\s*
565 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
567 $_ = $'; $again = 1;
568 my $type;
569 if(defined($2) || defined($3)) {
570 my $cc = $2 || $3;
571 if(defined($5)) {
572 $type = "$1 ($cc *)($5)";
573 } else {
574 $type = "$1 ($cc *)[$6]";
576 } else {
577 if(defined($5)) {
578 $type = "$1 (*)($5)";
579 } else {
580 $type = "$1 (*)[$6]";
583 my $name = $4;
584 &$type_begin($type);
585 &$type_end([$name]);
586 } elsif(/typedef[^\{;]*;/s) {
587 $_ = $'; $again = 1;
588 $output->write("$file: $.: can't parse: '$&'\n");
589 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
590 $_ = $'; $again = 1;
591 $output->write("$file: $.: can't parse: '$&'\n");
592 } elsif(/\'[^\']*\'/s) {
593 $_ = $'; $again = 1;
594 } elsif(/\"[^\"]*\"/s) {
595 $_ = $'; $again = 1;
596 } elsif(/;/s) {
597 $_ = $'; $again = 1;
598 } elsif(/extern\s+"C"\s+{/s) {
599 $_ = $'; $again = 1;
600 } elsif(/\{/s) {
601 $_ = $'; $again = 1;
602 print "+1: $_\n" if $options->debug >= 2;
603 $level++;
604 } else {
605 $lookahead = 1;
608 close(IN);
609 print STDERR "done\n" if $options->verbose;
610 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;