Get rid of the WIN_SetRectangles export from user32.
[wine.git] / tools / winapi_check / winapi_parser.pm
blob412328dab5f83324e565a0bbd5a55538c8455362
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 $_ = "$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;
233 if(defined($3)) {
234 &$preprocessor_found_callback($1, $3);
235 } else {
236 &$preprocessor_found_callback($1, "");
238 $again = 1;
239 next;
243 # Remove extern "C"
244 if(s/^\s*extern\s+"C"\s+\{//m) {
245 $extern_c = 1;
246 $again = 1;
247 next;
250 my $documentation_line;
251 my $documentation;
252 my @argument_documentations = ();
254 my $n = $#comments;
255 while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
256 $comments[$n] =~ /^\/\*\*+\/$/))
258 $n--;
261 if(defined($comments[$n]) && $n >= 0) {
262 my @lines = split(/\n/, $comments[$n]);
264 $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
265 $documentation = $comments[$n];
267 for(my $m=$n+1; $m <= $#comments; $m++) {
268 if($comments[$m] =~ /^\/\*\*+\/$/ ||
269 $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
271 @argument_documentations = ();
272 next;
274 push @argument_documentations, $comments[$m];
276 } else {
277 $documentation = "";
281 if($level > 0)
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;
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;
320 $line .= "\"";
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;
339 $statements .= $line;
340 } else {
341 $statements .= "$line\n";
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;
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)
364 my @lines = split(/\n/, $&);
365 my $function_line = $. - scalar(@lines) + 1;
367 $_ = $'; $again = 1;
369 if($11 eq "{") {
370 $level++;
373 my $linkage = $1;
374 my $return_type = $2;
375 my $calling_convention = $7;
376 my $name = $8;
377 my $arguments = $10;
379 if(!defined($linkage)) {
380 $linkage = "";
383 if(!defined($calling_convention)) {
384 $calling_convention = "";
387 $linkage =~ s/\s*$//;
389 $return_type =~ s/\s*$//;
390 $return_type =~ s/\s*\*\s*/*/g;
391 $return_type =~ s/(\*+)/ $1/g;
393 if($regs_entrypoints{$name}) {
394 $name = $regs_entrypoints{$name};
397 $arguments =~ y/\t\n/ /;
398 $arguments =~ s/^\s*(.*?)\s*$/$1/;
399 if($arguments eq "") { $arguments = "..." }
401 my @argument_types;
402 my @argument_names;
403 my @arguments;
404 my $n = 0;
405 while ($arguments =~ s/^((?:[^,\(\)]*|(?:\([^\)]*\))?)+)(?:,|$)// && $1) {
406 my $argument = $1;
407 push @arguments, $argument;
409 my $argument_type = "";
410 my $argument_name = "";
412 $argument =~ s/^\s*(.*?)\s*$/$1/;
413 # print " " . ($n + 1) . ": '$argument'\n";
414 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
415 $argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
416 if($argument =~ /^\.\.\.$/) {
417 $argument_type = "...";
418 $argument_name = "...";
419 } elsif($argument =~ /^
420 ((?:struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
421 (?:short\s+(?=int)|long\s+(?=int))?)?\w+|ElfW\(\w+\))\s*
422 ((?:__RPC_FAR|const|CONST)?\s*(?:\*\s*(?:__RPC_FAR|const|CONST)?\s*?)*)\s*
423 (?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL|\s+WINE_UNUSED)?$/x)
425 $argument_type = $1;
426 if ($2) {
427 $argument_type .= " $2";
429 $argument_name = $3;
430 } elsif ($argument =~ /^
431 ((?:struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
432 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
433 ((?:const)?\s*(?:\*\s*(?:const)?\s*?)*)\s*
434 (?:__cdecl\s+|__stdcall\s+|CALLBACK\s+|WINAPI\s+)?
435 \(\s*(?:__cdecl|__stdcall|CALLBACK|WINAPI)?\s*\*\s*((?:\w+)?)\s*\)\s*
436 \(\s*(.*?)\s*\)$/x)
438 my $return_type = $1;
439 if($2) {
440 $return_type .= " $2";
442 $argument_name = $3;
443 my $arguments = $4;
445 $return_type =~ s/\s+/ /g;
446 $arguments =~ s/\s*,\s*/,/g;
448 $argument_type = "$return_type (*)($arguments)";
449 } elsif ($argument =~ /^
450 ((?:struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
451 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
452 ((?:const)?\s*(?:\*\s*(?:const)?\s*?)*)\s*
453 (\w+)\s*\[\s*(.*?)\s*\](?:\[\s*(.*?)\s*\])?$/x)
455 my $return_type = $1;
456 if($2) {
457 $return_type .= " $2";
459 $argument_name = $3;
461 $argument_type = "$return_type\[$4\]";
463 if (defined($5)) {
464 $argument_type .= "\[$5\]";
467 # die "$file: $.: syntax error: '$argument_type':'$argument_name'\n";
468 } else {
469 die "$file: $.: syntax error: '$argument'\n";
472 $argument_type =~ s/\s*const\s*/ /g; # Remove const
473 $argument_type =~ s/([^\*\(\s])\*/$1 \*/g; # Assure whitespace between non-* and *
474 $argument_type =~ s/,([^\s])/, $1/g; # Assure whitespace after ,
475 $argument_type =~ s/\*\s+\*/\*\*/g; # Remove whitespace between * and *
476 $argument_type =~ s/([\(\[])\s+/$1/g; # Remove whitespace after ( and [
477 $argument_type =~ s/\s+([\)\]])/$1/g; # Remove whitespace before ] and )
478 $argument_type =~ s/\s+/ /; # Remove multiple whitespace
479 $argument_type =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
481 $argument_name =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
483 $argument_types[$n] = $argument_type;
484 $argument_names[$n] = $argument_name;
485 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
487 $n++;
489 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
490 $#argument_types = -1;
491 $#argument_names = -1;
494 if($options->debug) {
495 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
498 &$function_begin($documentation_line, $documentation,
499 $function_line, $linkage, $return_type, $calling_convention, $name,
500 \@argument_types,\@argument_names,\@argument_documentations);
501 if($level == 0) {
502 &$function_end(undef, undef);
504 $statements_line = $.;
505 $statements = "";
506 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
507 my @lines = split(/\n/, $&);
508 my $function_line = $. - scalar(@lines) + 1;
510 $_ = $'; $again = 1;
512 &$function_begin($documentation_line, $documentation,
513 $function_line, "", "void", "__asm", $1);
514 &$function_end($., "");
515 } elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
516 my @lines = split(/\n/, $&);
517 my $function_line = $. - scalar(@lines) + 1;
519 $_ = $'; $again = 1;
520 my @arguments16 = ("HWAVEIN16");
521 my @arguments32 = ("HWAVEIN");
522 &$function_begin($documentation_line, $documentation,
523 $function_line, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
524 &$function_end($., "");
525 &$function_begin($documentation_line, $documentation,
526 $function_line, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
527 &$function_end($., "");
528 } elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
529 my @lines = split(/\n/, $&);
530 my $function_line = $. - scalar(@lines) + 1;
532 $_ = $'; $again = 1;
534 my @arguments16 = ("HWAVEOUT16");
535 my @arguments32 = ("HWAVEOUT");
536 &$function_begin($documentation_line, $documentation,
537 $function_line, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
538 &$function_end($., "");
539 &$function_begin($documentation_line, $documentation,
540 $function_line, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
541 &$function_end($., "");
542 } elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
543 my @lines = split(/\n/, $&);
544 my $function_line = $. - scalar(@lines) + 1;
546 $_ = $'; $again = 1;
548 if($1 eq "1") {
549 my @arguments16 = ("HWAVEOUT16", $4);
550 my @arguments32 = ("HWAVEOUT", $4);
551 &$function_begin($documentation_line, $documentation,
552 $function_line, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
553 &$function_end($function_line, "");
554 &$function_begin($documentation_line, $documentation,
555 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
556 &$function_end($function_line, "");
557 } elsif($1 eq 2) {
558 my @arguments16 = ("UINT16", $4);
559 my @arguments32 = ("UINT", $4);
560 &$function_begin($documentation_line, $documentation,
561 $function_line, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
562 &$function_end($function_line, "");
563 &$function_begin($documentation_line, $documentation,
564 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
565 &$function_end($function_line, "");
567 } elsif(/DEFINE_THISCALL_WRAPPER\((\S*)\)/s) {
568 my @lines = split(/\n/, $&);
569 my $function_line = $. - scalar(@lines) + 1;
571 $_ = $'; $again = 1;
573 &$function_begin($documentation_line, $documentation,
574 $function_line, "", "void", "", "__thiscall_" . $1, \());
575 &$function_end($function_line, "");
576 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
577 $_ = $'; $again = 1;
578 $regs_entrypoints{$2} = $1;
579 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
580 $_ = $'; $again = 1;
581 unshift @$debug_channels, $1;
582 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
583 $_ = $'; $again = 1;
584 push @$debug_channels, $1;
585 } elsif(/typedef\s+(enum|struct|union)(?:\s+(\w+))?\s*\{/s) {
586 $_ = $'; $again = 1;
587 $level++;
588 my $type = $1;
589 if(defined($2)) {
590 $type .= " $2";
592 &$type_begin($type);
593 } elsif(/typedef\s+
594 ((?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
595 (\w+)
596 (?:\s+const)?
597 ((?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])*
598 (?:\s*,\s*(?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
599 \s*;/sx)
601 $_ = $'; $again = 1;
603 my $type = "$1 $2";
605 my @names;
606 my @parts = split(/\s*,\s*/, $2);
607 foreach my $part (@parts) {
608 if($part =~ /(?:\s*(\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
609 my $name = $2;
610 if(defined($1)) {
611 $name = "$1$2";
613 if(defined($3)) {
614 $name .= $3;
616 push @names, $name;
619 &$type_begin($type);
620 &$type_end([@names]);
621 } elsif(/typedef\s+
622 (?:(?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
623 (\w+(?:\s*\*+\s*)?)\s+
624 (?:(\w+)\s*)?
625 \((?:(\w+)\s*)?\s*\*\s*(\w+)\s*\)\s*
626 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
628 $_ = $'; $again = 1;
629 my $type;
630 if(defined($2) || defined($3)) {
631 my $cc = $2 || $3;
632 if(defined($5)) {
633 $type = "$1 ($cc *)($5)";
634 } else {
635 $type = "$1 ($cc *)[$6]";
637 } else {
638 if(defined($5)) {
639 $type = "$1 (*)($5)";
640 } else {
641 $type = "$1 (*)[$6]";
644 my $name = $4;
645 &$type_begin($type);
646 &$type_end([$name]);
647 } elsif(/typedef[^\{;]*;/s) {
648 $_ = $'; $again = 1;
649 $output->write("$file: $.: can't parse: '$&'\n");
650 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
651 $_ = $'; $again = 1;
652 $output->write("$file: $.: can't parse: '$&'\n");
653 } elsif(/\'[^\']*\'/s) {
654 $_ = $'; $again = 1;
655 } elsif(/\"[^\"]*\"/s) {
656 $_ = $'; $again = 1;
657 } elsif(/;/s) {
658 $_ = $'; $again = 1;
659 } elsif(/extern\s+"C"\s+{/s) {
660 $_ = $'; $again = 1;
661 } elsif(/\{/s) {
662 $_ = $'; $again = 1;
663 print "+1: $_\n" if $options->debug >= 2;
664 $level++;
665 } else {
666 $lookahead = 1;
669 close(IN);
670 print STDERR "done\n" if $options->verbose;
671 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;