Teach winapi_check about other calling convention shorthands (they all
[wine/multimedia.git] / tools / winapi_check / winapi_parser.pm
blob731ab881fdd70aff2ad777976ccf1629450217c1
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 readmore: 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 my $prefix="";
195 while ($_ ne "")
197 if (s/^([^\"\/]+|\"(?:[^\\\"]*|\\.)*\")//)
199 $prefix.=$1;
201 elsif (/^\/\*/)
203 # remove C comments
204 if(s/^(\/\*.*?\*\/)//s) {
205 my @lines = split(/\n/, $1);
206 push @comment_lines, $.;
207 push @comments, $1;
208 &$c_comment_found_callback($. - $#lines, $., $1);
209 if($#lines <= 0) {
210 $_ = "$prefix $_";
211 } else {
212 $_ = $prefix . ("\n" x $#lines) . $_;
214 $again = 1;
215 } else {
216 $_ = "$prefix$_";
217 $lookahead = 1;
219 next readmore;
221 elsif (s/^(\/\/.*)$//)
223 # remove C++ comments
224 &$cplusplus_comment_found_callback($., $1);
225 $again = 1;
227 elsif (s/^(.)//)
229 $prefix.=$1;
232 $_=$prefix;
234 # remove preprocessor directives
235 if(s/^\s*\#/\#/s) {
236 if(/^(\#.*?)\\$/s) {
237 $_ = "$1\n";
238 $lookahead = 1;
239 next;
240 } elsif(s/^\#\s*(\w+)((?:\s+(.*?))?\s*)$//s) {
241 my @lines = split(/\n/, $2);
242 if($#lines > 0) {
243 $_ = "\n" x $#lines;
245 if(defined($3)) {
246 &$preprocessor_found_callback($1, $3);
247 } else {
248 &$preprocessor_found_callback($1, "");
250 $again = 1;
251 next;
255 # Remove extern "C"
256 if(s/^\s*extern\s+"C"\s+\{//m) {
257 $extern_c = 1;
258 $again = 1;
259 next;
262 my $documentation_line;
263 my $documentation;
264 my @argument_documentations = ();
266 my $n = $#comments;
267 while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
268 $comments[$n] =~ /^\/\*\*+\/$/))
270 $n--;
273 if(defined($comments[$n]) && $n >= 0) {
274 my @lines = split(/\n/, $comments[$n]);
276 $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
277 $documentation = $comments[$n];
279 for(my $m=$n+1; $m <= $#comments; $m++) {
280 if($comments[$m] =~ /^\/\*\*+\/$/ ||
281 $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
283 @argument_documentations = ();
284 next;
286 push @argument_documentations, $comments[$m];
288 } else {
289 $documentation = "";
293 if($level > 0)
295 my $line = "";
296 while(/^[^\{\}]/) {
297 s/^([^\{\}\'\"]*)//s;
298 $line .= $1;
299 if(s/^\'//) {
300 $line .= "\'";
301 while(/^./ && !s/^\'//) {
302 s/^([^\'\\]*)//s;
303 $line .= $1;
304 if(s/^\\//) {
305 $line .= "\\";
306 if(s/^(.)//s) {
307 $line .= $1;
308 if($1 eq "0") {
309 s/^(\d{0,3})//s;
310 $line .= $1;
315 $line .= "\'";
316 } elsif(s/^\"//) {
317 $line .= "\"";
318 while(/^./ && !s/^\"//) {
319 s/^([^\"\\]*)//s;
320 $line .= $1;
321 if(s/^\\//) {
322 $line .= "\\";
323 if(s/^(.)//s) {
324 $line .= $1;
325 if($1 eq "0") {
326 s/^(\d{0,3})//s;
327 $line .= $1;
332 $line .= "\"";
336 if(s/^\{//) {
337 $_ = $'; $again = 1;
338 $line .= "{";
339 print "+1: \{$_\n" if $options->debug >= 2;
340 $level++;
341 $statements .= $line;
342 } elsif(s/^\}//) {
343 $_ = $'; $again = 1;
344 $line .= "}" if $level > 1;
345 print "-1: \}$_\n" if $options->debug >= 2;
346 $level--;
347 if($level == -1 && $extern_c) {
348 $extern_c = 0;
349 $level = 0;
351 $statements .= $line;
352 } else {
353 $statements .= "$line\n";
356 if($level == 0) {
357 if($in_function) {
358 &$function_end($statements_line, $statements);
359 $statements = undef;
360 } elsif($in_type) {
361 if(/^\s*(?:WINE_PACKED\s+)?((?:\*\s*)?\w+\s*(?:\s*,\s*(?:\*+\s*)?\w+)*\s*);/s) {
362 my @parts = split(/\s*,\s*/, $1);
363 &$type_end([@parts]);
364 } elsif(/;/s) {
365 die "$file: $.: syntax error: '$_'\n";
366 } else {
367 $lookahead = 1;
371 next;
372 } elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
373 ((__cdecl|__stdcall|CDECL|NET_API_FUNCTION|RPC_ENTRY|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
374 (\w+(\(\w+\))?)\s*\((.*?)\)\s*(\{|\;)/sx)
376 my @lines = split(/\n/, $&);
377 my $function_line = $. - scalar(@lines) + 1;
379 $_ = $'; $again = 1;
381 if($11 eq "{") {
382 $level++;
385 my $linkage = $1;
386 my $return_type = $2;
387 my $calling_convention = $7;
388 my $name = $8;
389 my $arguments = $10;
391 if(!defined($linkage)) {
392 $linkage = "";
395 if(!defined($calling_convention)) {
396 $calling_convention = "";
399 $linkage =~ s/\s*$//;
401 $return_type =~ s/\s*$//;
402 $return_type =~ s/\s*\*\s*/*/g;
403 $return_type =~ s/(\*+)/ $1/g;
405 if($regs_entrypoints{$name}) {
406 $name = $regs_entrypoints{$name};
409 $arguments =~ y/\t\n/ /;
410 $arguments =~ s/^\s*(.*?)\s*$/$1/;
411 if($arguments eq "") { $arguments = "..." }
413 my @argument_types;
414 my @argument_names;
415 my @arguments;
416 my $n = 0;
417 while ($arguments =~ s/^((?:[^,\(\)]*|(?:\([^\)]*\))?)+)(?:,|$)// && $1) {
418 my $argument = $1;
419 push @arguments, $argument;
421 my $argument_type = "";
422 my $argument_name = "";
424 $argument =~ s/^\s*(.*?)\s*$/$1/;
425 # print " " . ($n + 1) . ": '$argument'\n";
426 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
427 $argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
428 if($argument =~ /^\.\.\.$/) {
429 $argument_type = "...";
430 $argument_name = "...";
431 } elsif($argument =~ /^
432 ((?:struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
433 (?:short\s+(?=int)|long\s+(?=int))?)?\w+|ElfW\(\w+\))\s*
434 ((?:__RPC_FAR|const|CONST)?\s*(?:\*\s*(?:__RPC_FAR|const|CONST)?\s*?)*)\s*
435 (?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL|\s+WINE_UNUSED)?$/x)
437 $argument_type = $1;
438 if ($2) {
439 $argument_type .= " $2";
441 $argument_name = $3;
442 } elsif ($argument =~ /^
443 ((?:struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
444 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
445 ((?:const)?\s*(?:\*\s*(?:const)?\s*?)*)\s*
446 (?:__cdecl\s+|__stdcall\s+|CALLBACK\s+|CDECL\s+|NET_API_FUNCTION\s+|RPC_ENTRY\s+|VFWAPIV\s+|VFWAPI\s+|WINAPIV\s+|WINAPI\s+)?
447 \(\s*(?:__cdecl|__stdcall|CALLBACK|CDECL|NET_API_FUNCTION|RPC_ENTRY|VFWAPIV|VFWAPI|WINAPIV|WINAPI)?\s*\*\s*((?:\w+)?)\s*\)\s*
448 \(\s*(.*?)\s*\)$/x)
450 my $return_type = $1;
451 if($2) {
452 $return_type .= " $2";
454 $argument_name = $3;
455 my $arguments = $4;
457 $return_type =~ s/\s+/ /g;
458 $arguments =~ s/\s*,\s*/,/g;
460 $argument_type = "$return_type (*)($arguments)";
461 } elsif ($argument =~ /^
462 ((?:struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
463 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
464 ((?:const)?\s*(?:\*\s*(?:const)?\s*?)*)\s*
465 (\w+)\s*\[\s*(.*?)\s*\](?:\[\s*(.*?)\s*\])?$/x)
467 my $return_type = $1;
468 if($2) {
469 $return_type .= " $2";
471 $argument_name = $3;
473 $argument_type = "$return_type\[$4\]";
475 if (defined($5)) {
476 $argument_type .= "\[$5\]";
479 # die "$file: $.: syntax error: '$argument_type':'$argument_name'\n";
480 } else {
481 die "$file: $.: syntax error: '$argument'\n";
484 $argument_type =~ s/\s*const\s*/ /g; # Remove const
485 $argument_type =~ s/([^\*\(\s])\*/$1 \*/g; # Assure whitespace between non-* and *
486 $argument_type =~ s/,([^\s])/, $1/g; # Assure whitespace after ,
487 $argument_type =~ s/\*\s+\*/\*\*/g; # Remove whitespace between * and *
488 $argument_type =~ s/([\(\[])\s+/$1/g; # Remove whitespace after ( and [
489 $argument_type =~ s/\s+([\)\]])/$1/g; # Remove whitespace before ] and )
490 $argument_type =~ s/\s+/ /; # Remove multiple whitespace
491 $argument_type =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
493 $argument_name =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
495 $argument_types[$n] = $argument_type;
496 $argument_names[$n] = $argument_name;
497 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
499 $n++;
501 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
502 $#argument_types = -1;
503 $#argument_names = -1;
506 if($options->debug) {
507 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
510 &$function_begin($documentation_line, $documentation,
511 $function_line, $linkage, $return_type, $calling_convention, $name,
512 \@argument_types,\@argument_names,\@argument_documentations);
513 if($level == 0) {
514 &$function_end(undef, undef);
516 $statements_line = $.;
517 $statements = "";
518 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
519 my @lines = split(/\n/, $&);
520 my $function_line = $. - scalar(@lines) + 1;
522 $_ = $'; $again = 1;
524 &$function_begin($documentation_line, $documentation,
525 $function_line, "", "void", "__asm", $1);
526 &$function_end($., "");
527 } elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
528 my @lines = split(/\n/, $&);
529 my $function_line = $. - scalar(@lines) + 1;
531 $_ = $'; $again = 1;
532 my @arguments16 = ("HWAVEIN16");
533 my @arguments32 = ("HWAVEIN");
534 &$function_begin($documentation_line, $documentation,
535 $function_line, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
536 &$function_end($., "");
537 &$function_begin($documentation_line, $documentation,
538 $function_line, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
539 &$function_end($., "");
540 } elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
541 my @lines = split(/\n/, $&);
542 my $function_line = $. - scalar(@lines) + 1;
544 $_ = $'; $again = 1;
546 my @arguments16 = ("HWAVEOUT16");
547 my @arguments32 = ("HWAVEOUT");
548 &$function_begin($documentation_line, $documentation,
549 $function_line, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
550 &$function_end($., "");
551 &$function_begin($documentation_line, $documentation,
552 $function_line, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
553 &$function_end($., "");
554 } elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
555 my @lines = split(/\n/, $&);
556 my $function_line = $. - scalar(@lines) + 1;
558 $_ = $'; $again = 1;
560 if($1 eq "1") {
561 my @arguments16 = ("HWAVEOUT16", $4);
562 my @arguments32 = ("HWAVEOUT", $4);
563 &$function_begin($documentation_line, $documentation,
564 $function_line, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
565 &$function_end($function_line, "");
566 &$function_begin($documentation_line, $documentation,
567 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
568 &$function_end($function_line, "");
569 } elsif($1 eq 2) {
570 my @arguments16 = ("UINT16", $4);
571 my @arguments32 = ("UINT", $4);
572 &$function_begin($documentation_line, $documentation,
573 $function_line, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
574 &$function_end($function_line, "");
575 &$function_begin($documentation_line, $documentation,
576 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
577 &$function_end($function_line, "");
579 } elsif(/DEFINE_THISCALL_WRAPPER\((\S*)\)/s) {
580 my @lines = split(/\n/, $&);
581 my $function_line = $. - scalar(@lines) + 1;
583 $_ = $'; $again = 1;
585 &$function_begin($documentation_line, $documentation,
586 $function_line, "", "void", "", "__thiscall_" . $1, \());
587 &$function_end($function_line, "");
588 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
589 $_ = $'; $again = 1;
590 $regs_entrypoints{$2} = $1;
591 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
592 $_ = $'; $again = 1;
593 unshift @$debug_channels, $1;
594 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
595 $_ = $'; $again = 1;
596 push @$debug_channels, $1;
597 } elsif(/typedef\s+(enum|struct|union)(?:\s+(\w+))?\s*\{/s) {
598 $_ = $'; $again = 1;
599 $level++;
600 my $type = $1;
601 if(defined($2)) {
602 $type .= " $2";
604 &$type_begin($type);
605 } elsif(/typedef\s+
606 ((?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
607 (\w+)
608 (?:\s+const)?
609 ((?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])*
610 (?:\s*,\s*(?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
611 \s*;/sx)
613 $_ = $'; $again = 1;
615 my $type = "$1 $2";
617 my @names;
618 my @parts = split(/\s*,\s*/, $2);
619 foreach my $part (@parts) {
620 if($part =~ /(?:\s*(\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
621 my $name = $2;
622 if(defined($1)) {
623 $name = "$1$2";
625 if(defined($3)) {
626 $name .= $3;
628 push @names, $name;
631 &$type_begin($type);
632 &$type_end([@names]);
633 } elsif(/typedef\s+
634 (?:(?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
635 (\w+(?:\s*\*+\s*)?)\s+
636 (?:(\w+)\s*)?
637 \((?:(\w+)\s*)?\s*\*\s*(\w+)\s*\)\s*
638 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
640 $_ = $'; $again = 1;
641 my $type;
642 if(defined($2) || defined($3)) {
643 my $cc = $2 || $3;
644 if(defined($5)) {
645 $type = "$1 ($cc *)($5)";
646 } else {
647 $type = "$1 ($cc *)[$6]";
649 } else {
650 if(defined($5)) {
651 $type = "$1 (*)($5)";
652 } else {
653 $type = "$1 (*)[$6]";
656 my $name = $4;
657 &$type_begin($type);
658 &$type_end([$name]);
659 } elsif(/typedef[^\{;]*;/s) {
660 $_ = $'; $again = 1;
661 $output->write("$file: $.: can't parse: '$&'\n");
662 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
663 $_ = $'; $again = 1;
664 $output->write("$file: $.: can't parse: '$&'\n");
665 } elsif(/\'[^\']*\'/s) {
666 $_ = $'; $again = 1;
667 } elsif(/\"(?:[^\\\"]*|\\.)*\"/s) {
668 $_ = $'; $again = 1;
669 } elsif(/;/s) {
670 $_ = $'; $again = 1;
671 } elsif(/extern\s+"C"\s+{/s) {
672 $_ = $'; $again = 1;
673 } elsif(/\{/s) {
674 $_ = $'; $again = 1;
675 print "+1: $_\n" if $options->debug >= 2;
676 $level++;
677 } else {
678 $lookahead = 1;
681 close(IN);
682 print STDERR "done\n" if $options->verbose;
683 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;