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 $function_create_callback = shift;
29 my $function_found_callback = shift;
30 my $type_create_callback = shift;
31 my $type_found_callback = shift;
32 my $preprocessor_found_callback = shift;
35 my $debug_channels = [];
41 my $documentation_line;
46 my $calling_convention;
47 my $internal_name = "";
50 my $argument_documentations;
54 $function_begin = sub {
55 $documentation_line = shift;
56 $documentation = shift;
57 $function_line = shift;
60 $calling_convention = shift;
61 $internal_name = shift;
62 $argument_types = shift;
63 $argument_names = shift;
64 $argument_documentations = shift;
66 if(defined($argument_names) && defined($argument_types) &&
67 $#$argument_names == -1)
69 foreach my $n (0..$#$argument_types) {
70 push @$argument_names, "";
74 if(defined($argument_documentations) &&
75 $#$argument_documentations == -1)
77 foreach my $n (0..$#$argument_documentations) {
78 push @$argument_documentations, "";
86 $statements_line = shift;
89 my $function = &$function_create_callback();
91 if(!defined($documentation_line)) {
92 $documentation_line = 0;
95 $function->file($file);
96 $function->debug_channels([@$debug_channels]);
97 $function->documentation_line($documentation_line);
98 $function->documentation($documentation);
99 $function->function_line($function_line);
100 $function->linkage($linkage);
101 $function->return_type($return_type);
102 $function->calling_convention($calling_convention);
103 $function->internal_name($internal_name);
104 if(defined($argument_types)) {
105 $function->argument_types([@$argument_types]);
107 if(defined($argument_names)) {
108 $function->argument_names([@$argument_names]);
110 if(defined($argument_documentations)) {
111 $function->argument_documentations([@$argument_documentations]);
113 $function->statements_line($statements_line);
114 $function->statements($statements);
116 &$function_found_callback($function);
136 foreach my $name (@$names) {
137 if($type =~ /^(?:struct|enum)/) {
138 # $output->write("typedef $type {\n");
139 # $output->write("} $name;\n");
141 # $output->write("typedef $type $name;\n");
148 my %regs_entrypoints;
149 my @comment_lines = ();
157 my $lookahead_count = 0;
159 print STDERR "Processing file '$file' ... " if $options->verbose;
160 open(IN, "< $file") || die "<internal>: $file: $!\n";
162 while($again || defined(my $line = <IN>)) {
172 $lookahead_count = 0;
174 print " $level($lookahead_count): $line\n" if $options->debug >= 2;
175 print "*** $_\n" if $options->debug >= 3;
177 $lookahead_count = 0;
181 # CVS merge conflicts in file?
182 if(/^(<<<<<<<|=======|>>>>>>>)/) {
183 $output->write("$file: merge conflicts in file\n");
188 if(/^(.*?)(\/\*(.*?)\*\/)(.*)$/s) {
189 my @lines = split(/\n/, $2);
190 push @comment_lines, $.;
195 $_ = $1 . ("\n" x $#lines) . $4;
205 # remove C++ comments
206 while(s/^(.*?)\/\/.*?$/$1/s) { $again = 1 }
209 # remove preprocessor directives
214 } elsif(s/^\#\s*(\w+)((?:\s+(.*?))?\s*)$//s) {
215 my @lines = split(/\n/, $2);
220 &$preprocessor_found_callback($1, $3);
222 &$preprocessor_found_callback($1, "");
230 if(s/^\s*extern\s+"C"\s+\{//m) {
236 my $documentation_line;
238 my @argument_documentations = ();
241 while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
242 $comments[$n] =~ /^\/\*\*+\/$/))
247 if(defined($comments[$n]) && $n >= 0) {
248 my @lines = split(/\n/, $comments[$n]);
250 $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
251 $documentation = $comments[$n];
253 for(my $m=$n+1; $m <= $#comments; $m++) {
254 if($comments[$m] =~ /^\/\*\*+\/$/ ||
255 $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
257 @argument_documentations = ();
260 push @argument_documentations, $comments[$m];
271 s/^([^\{\}\'\"]*)//s;
275 while(/^./ && !s/^\'//) {
292 while(/^./ && !s/^\"//) {
313 print "+1: \{$_\n" if $options->debug >= 2;
315 $statements .= $line;
318 $line .= "}" if $level > 1;
319 print "-1: \}$_\n" if $options->debug >= 2;
321 if($level == -1 && $extern_c) {
325 $statements .= $line;
327 $statements .= "$line\n";
332 &$function_end($statements_line, $statements);
335 if(/^\s*(?:WINE_PACKED\s+)?((?:\*\s*)?\w+\s*(?:\s*,\s*(?:\*+\s*)?\w+)*\s*);/s) {
336 my @parts = split(/\s*,\s*/, $1);
337 &$type_end([@parts]);
339 die "$file: $.: syntax error: '$_'\n";
346 } elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
347 ((__cdecl|__stdcall|CDECL|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
348 (\w+(\(\w+\))?)\s*\(([^\)]*)\)\s*(\{|\;)/sx)
350 my @lines = split(/\n/, $&);
351 my $function_line = $. - scalar(@lines) + 1;
360 my $return_type = $2;
361 my $calling_convention = $7;
365 if(!defined($linkage)) {
369 if(!defined($calling_convention)) {
370 $calling_convention = "";
373 $linkage =~ s/\s*$//;
375 $return_type =~ s/\s*$//;
376 $return_type =~ s/\s*\*\s*/*/g;
377 $return_type =~ s/(\*+)/ $1/g;
379 if($regs_entrypoints{$name}) {
380 $name = $regs_entrypoints{$name};
383 $arguments =~ y/\t\n/ /;
384 $arguments =~ s/^\s*(.*?)\s*$/$1/;
385 if($arguments eq "") { $arguments = "..." }
389 my @arguments = split(/,/, $arguments);
390 foreach my $n (0..$#arguments) {
391 my $argument_type = "";
392 my $argument_name = "";
393 my $argument = $arguments[$n];
394 $argument =~ s/^\s*(.*?)\s*$/$1/;
395 # print " " . ($n + 1) . ": '$argument'\n";
396 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
397 $argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
398 if($argument =~ /^\.\.\.$/) {
399 $argument_type = "...";
400 $argument_name = "...";
401 } elsif($argument =~ /^
402 ((?:struct\s+|union\s+|enum\s+|(?:signed\s+|unsigned\s+)
403 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
404 ((?:const)?\s*(?:\*\s*?)*)\s*
405 (?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL)?/x)
407 $argument_type = "$1";
409 $argument_type .= " $2";
413 $argument_type =~ s/\s*const\s*/ /;
414 $argument_type =~ s/^\s*(.*?)\s*$/$1/;
416 $argument_name =~ s/^\s*(.*?)\s*$/$1/;
418 die "$file: $.: syntax error: '$argument'\n";
420 $argument_types[$n] = $argument_type;
421 $argument_names[$n] = $argument_name;
422 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
424 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
425 $#argument_types = -1;
426 $#argument_names = -1;
429 if($options->debug) {
430 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
433 &$function_begin($documentation_line, $documentation,
434 $function_line, $linkage, $return_type, $calling_convention, $name,
435 \@argument_types,\@argument_names,\@argument_documentations);
437 &$function_end(undef, undef);
439 $statements_line = $.;
441 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
442 my @lines = split(/\n/, $&);
443 my $function_line = $. - scalar(@lines) + 1;
447 &$function_begin($documentation_line, $documentation,
448 $function_line, "", "void", "__asm", $1);
449 &$function_end($., "");
450 } elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
451 my @lines = split(/\n/, $&);
452 my $function_line = $. - scalar(@lines) + 1;
455 my @arguments16 = ("HWAVEIN16");
456 my @arguments32 = ("HWAVEIN");
457 &$function_begin($documentation_line, $documentation,
458 $function_line, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
459 &$function_end($., "");
460 &$function_begin($documentation_line, $documentation,
461 $function_line, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
462 &$function_end($., "");
463 } elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
464 my @lines = split(/\n/, $&);
465 my $function_line = $. - scalar(@lines) + 1;
469 my @arguments16 = ("HWAVEOUT16");
470 my @arguments32 = ("HWAVEOUT");
471 &$function_begin($documentation_line, $documentation,
472 $function_line, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
473 &$function_end($., "");
474 &$function_begin($documentation_line, $documentation,
475 $function_line, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
476 &$function_end($., "");
477 } elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
478 my @lines = split(/\n/, $&);
479 my $function_line = $. - scalar(@lines) + 1;
484 my @arguments16 = ("HWAVEOUT16", $4);
485 my @arguments32 = ("HWAVEOUT", $4);
486 &$function_begin($documentation_line, $documentation,
487 $function_line, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
488 &$function_end($., "");
489 &$function_begin($documentation_line, $documentation,
490 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
491 &$function_end($., "");
493 my @arguments16 = ("UINT16", $4);
494 my @arguments32 = ("UINT", $4);
495 &$function_begin($documentation_line, $documentation,
496 $function_line, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
497 &$function_end($., "");
498 &$function_begin($documentation_line, $documentation,
499 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
500 &$function_end($., "");
502 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
504 $regs_entrypoints{$2} = $1;
505 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
507 unshift @$debug_channels, $1;
508 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
510 push @$debug_channels, $1;
511 } elsif(/typedef\s+(enum|struct|union)(?:\s+(\w+))?\s*\{/s) {
520 ((?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
523 ((?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?
524 (?:\s*,\s*(?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
532 my @parts = split(/\s*,\s*/, $2);
533 foreach my $part (@parts) {
534 if($part =~ /(?:\s*(\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
546 &$type_end([@names]);
548 (?:(?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
549 (\w+(?:\s*\*+\s*)?)\s+
551 \((?:(\w+)\s+)?\s*\*\s*(\w+)\s*\)\s*
552 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
556 if(defined($2) || defined($3)) {
559 $type = "$1 ($cc *)($5)";
561 $type = "$1 ($cc *)[$6]";
565 $type = "$1 (*)($5)";
567 $type = "$1 (*)[$6]";
573 } elsif(/typedef[^\{;]*;/s) {
575 $output->write("$file: $.: can't parse: '$&'\n");
576 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
578 $output->write("$file: $.: can't parse: '$&'\n");
579 } elsif(/\'[^\']*\'/s) {
581 } elsif(/\"[^\"]*\"/s) {
585 } elsif(/extern\s+"C"\s+{/s) {
589 print "+1: $_\n" if $options->debug >= 2;
596 print STDERR "done\n" if $options->verbose;
597 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;