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
;
23 use output
qw($output);
24 use options qw($options);
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;
41 my $debug_channels = [];
47 my $documentation_line;
52 my $calling_convention;
53 my $internal_name = "";
56 my $argument_documentations;
60 $function_begin = sub {
61 $documentation_line = shift;
62 $documentation = shift;
63 $function_line = 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, "";
92 $statements_line = 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);
142 foreach my $name (@$names) {
143 if($type =~ /^(?:struct|enum)/) {
144 # $output->write("typedef $type {\n");
145 # $output->write("} $name;\n");
147 # $output->write("typedef $type $name;\n");
154 my %regs_entrypoints;
155 my @comment_lines = ();
163 my $lookahead_count = 0;
165 print STDERR "Processing file '$file' ... " if $options->verbose;
166 open(IN, "< $file") || die "<internal>: $file: $!\n";
168 while($again || defined(my $line = <IN>)) {
169 $_ = "" if !defined($_);
179 $lookahead_count = 0;
181 $output->write(" $level($lookahead_count): $line\n") if $options->debug >= 2;
182 $output->write("*** $_\n") if $options->debug >= 3;
184 $lookahead_count = 0;
188 # CVS merge conflicts in file?
189 if(/^(<<<<<<<|=======|>>>>>>>)/) {
190 $output->write("$file: merge conflicts in file\n");
195 if(s/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)(?=\/\*)//s) {
197 if(s/^(\/\*.*?\*\/)//s) {
198 my @lines = split(/\n/, $1);
199 push @comment_lines, $.;
201 &$c_comment_found_callback($. - $#lines, $., $1);
205 $_ = $prefix . ("\n" x $#lines) . $_;
215 # remove C++ comments
216 while(s/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)(\/\/.*?)$/$1/s) {
217 &$cplusplus_comment_found_callback($., $2);
222 # remove preprocessor directives
228 } elsif(s/^\#\s*(\w+)((?:\s+(.*?))?\s*)$//s) {
229 my @lines = split(/\n/, $2);
234 &$preprocessor_found_callback($1, $3);
236 &$preprocessor_found_callback($1, "");
244 if(s/^\s*extern\s+"C"\s+\{//m) {
250 my $documentation_line;
252 my @argument_documentations = ();
255 while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
256 $comments[$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 = ();
274 push @argument_documentations, $comments[$m];
285 s/^([^\{\}\'\"]*)//s;
289 while(/^./ && !s/^\'//) {
306 while(/^./ && !s/^\"//) {
327 print "+1: \{$_\n" if $options->debug >= 2;
329 $statements .= $line;
332 $line .= "}" if $level > 1;
333 print "-1: \}$_\n" if $options->debug >= 2;
335 if($level == -1 && $extern_c) {
339 $statements .= $line;
341 $statements .= "$line\n";
346 &$function_end($statements_line, $statements);
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]);
353 die "$file: $.: syntax error: '$_'\n";
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;
374 my $return_type = $2;
375 my $calling_convention = $7;
379 if(!defined($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 = "..." }
405 while ($arguments =~ s/^((?:[^,\(\)]*|(?:\([^\)]*\))?)+)(?:,|$)// && $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)
427 $argument_type .= " $2";
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*
438 my $return_type = $1;
440 $return_type .= " $2";
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;
457 $return_type .= " $2";
461 $argument_type = "$return_type\[$4\]";
464 $argument_type .= "\[$5\]";
467 # die "$file: $.: syntax error: '$argument_type':'$argument_name'\n";
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";
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);
502 &$function_end(undef, undef);
504 $statements_line = $.;
506 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
507 my @lines = split(/\n/, $&);
508 my $function_line = $. - scalar(@lines) + 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;
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;
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;
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, "");
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;
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) {
578 $regs_entrypoints{$2} = $1;
579 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
581 unshift @$debug_channels, $1;
582 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
584 push @$debug_channels, $1;
585 } elsif(/typedef\s+(enum|struct|union)(?:\s+(\w+))?\s*\{/s) {
594 ((?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
597 ((?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])*
598 (?:\s*,\s*(?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
606 my @parts = split(/\s*,\s*/, $2);
607 foreach my $part (@parts) {
608 if($part =~ /(?:\s*(\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
620 &$type_end([@names]);
622 (?:(?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
623 (\w+(?:\s*\*+\s*)?)\s+
625 \((?:(\w+)\s*)?\s*\*\s*(\w+)\s*\)\s*
626 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
630 if(defined($2) || defined($3)) {
633 $type = "$1 ($cc *)($5)";
635 $type = "$1 ($cc *)[$6]";
639 $type = "$1 (*)($5)";
641 $type = "$1 (*)[$6]";
647 } elsif(/typedef[^\{;]*;/s) {
649 $output->write("$file: $.: can't parse: '$&'\n");
650 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
652 $output->write("$file: $.: can't parse: '$&'\n");
653 } elsif(/\'[^\']*\'/s) {
655 } elsif(/\"[^\"]*\"/s) {
659 } elsif(/extern\s+"C"\s+{/s) {
663 print "+1: $_\n" if $options->debug >= 2;
670 print STDERR "done\n" if $options->verbose;
671 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;