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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 package winapi_parser
;
23 use output
qw($output);
24 use options qw($options);
26 # Defined a couple common regexp tidbits
27 my $CALL_CONVENTION="__cdecl|__stdcall|" .
28 "__RPC_API|__RPC_STUB|__RPC_USER|RPC_ENTRY|" .
29 "RPC_VAR_ENTRY|STDMETHODCALLTYPE|NET_API_FUNCTION|" .
30 "CALLBACK|CDECL|NTAPI|PASCAL|APIENTRY|" .
31 "VFWAPI|VFWAPIV|WINAPI|WINAPIV|";
33 sub parse_c_file($$) {
35 my $callbacks = shift;
37 my $empty_callback = sub { };
39 my $c_comment_found_callback = $$callbacks{c_comment_found} || $empty_callback;
40 my $cplusplus_comment_found_callback = $$callbacks{cplusplus_comment_found} || $empty_callback;
41 my $function_create_callback = $$callbacks{function_create} || $empty_callback;
42 my $function_found_callback = $$callbacks{function_found} || $empty_callback;
43 my $type_create_callback = $$callbacks{type_create} || $empty_callback;
44 my $type_found_callback = $$callbacks{type_found} || $empty_callback;
45 my $preprocessor_found_callback = $$callbacks{preprocessor_found} || $empty_callback;
48 my $debug_channels = [];
54 my $documentation_line;
59 my $calling_convention;
60 my $internal_name = "";
63 my $argument_documentations;
67 $function_begin = sub {
68 $documentation_line = shift;
69 $documentation = shift;
70 $function_line = shift;
73 $calling_convention = shift;
74 $internal_name = shift;
75 $argument_types = shift;
76 $argument_names = shift;
77 $argument_documentations = shift;
79 if(defined($argument_names) && defined($argument_types) &&
80 $#$argument_names == -1)
82 foreach my $n (0..$#$argument_types) {
83 push @$argument_names, "";
87 if(defined($argument_documentations) &&
88 $#$argument_documentations == -1)
90 foreach my $n (0..$#$argument_documentations) {
91 push @$argument_documentations, "";
99 $statements_line = shift;
102 my $function = &$function_create_callback();
104 if(!defined($documentation_line)) {
105 $documentation_line = 0;
108 $function->file($file);
109 $function->debug_channels([@$debug_channels]);
110 $function->documentation_line($documentation_line);
111 $function->documentation($documentation);
112 $function->function_line($function_line);
113 $function->linkage($linkage);
114 $function->return_type($return_type);
115 $function->calling_convention($calling_convention);
116 $function->internal_name($internal_name);
117 if(defined($argument_types)) {
118 $function->argument_types([@$argument_types]);
120 if(defined($argument_names)) {
121 $function->argument_names([@$argument_names]);
123 if(defined($argument_documentations)) {
124 $function->argument_documentations([@$argument_documentations]);
126 $function->statements_line($statements_line);
127 $function->statements($statements);
129 &$function_found_callback($function);
149 foreach my $name (@$names) {
150 if($type =~ /^(?:enum|interface|struct|union)/) {
151 # $output->write("typedef $type {\n");
152 # $output->write("} $name;\n");
154 # $output->write("typedef $type $name;\n");
161 my %regs_entrypoints;
162 my @comment_lines = ();
170 my $lookahead_count = 0;
172 print STDERR "Processing file '$file' ... " if $options->verbose;
173 open(IN, "< $file") || die "<internal>: $file: $!\n";
175 readmore: while($again || defined(my $line = <IN>)) {
176 $_ = "" if !defined($_);
186 $lookahead_count = 0;
188 $output->write(" $level($lookahead_count): $line\n") if $options->debug >= 2;
189 $output->write("*** $_\n") if $options->debug >= 3;
191 $lookahead_count = 0;
195 # CVS merge conflicts in file?
196 if(/^(<<<<<<<|=======|>>>>>>>)/) {
197 $output->write("$file: merge conflicts in file\n");
204 if (s/^([^\"\/]+|\"(?:[^\\\"]*|\\.)*\")//)
211 if(s/^(\/\*.*?\*\/)//s) {
212 my @lines = split(/\n/, $1);
213 push @comment_lines, $.;
215 &$c_comment_found_callback($. - $#lines, $., $1);
219 $_ = $prefix . ("\n" x $#lines) . $_;
228 elsif (s/^(\/\/.*)$//)
230 # remove C++ comments
231 &$cplusplus_comment_found_callback($., $1);
241 # remove preprocessor directives
247 } elsif(s/^\#\s*(\w+)((?:\s+(.*?))?\s*)$//s) {
248 my @lines = split(/\n/, $2);
253 &$preprocessor_found_callback($1, $3);
255 &$preprocessor_found_callback($1, "");
263 if(s/^\s*extern\s+"C"\s+\{//m) {
269 my $documentation_line;
271 my @argument_documentations = ();
274 while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
275 $comments[$n] =~ /^\/\*\*+\/$/))
280 if(defined($comments[$n]) && $n >= 0) {
281 my @lines = split(/\n/, $comments[$n]);
283 $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
284 $documentation = $comments[$n];
286 for(my $m=$n+1; $m <= $#comments; $m++) {
287 if($comments[$m] =~ /^\/\*\*+\/$/ ||
288 $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
290 @argument_documentations = ();
293 push @argument_documentations, $comments[$m];
304 s/^([^\{\}\'\"]*)//s;
308 while(/^./ && !s/^\'//) {
325 while(/^./ && !s/^\"//) {
346 print "+1: \{$_\n" if $options->debug >= 2;
348 $statements .= $line;
351 $line .= "}" if $level > 1;
352 print "-1: \}$_\n" if $options->debug >= 2;
354 if($level == -1 && $extern_c) {
358 $statements .= $line;
360 $statements .= "$line\n";
365 &$function_end($statements_line, $statements);
368 if(/^\s*((?:(?:FAR\s*)?\*\s*(?:RESTRICTED_POINTER\s+)?)?
371 (?:\s*,\s*(?:(?:FAR\s*)?\*+\s*(?:RESTRICTED_POINTER\s+)?)?(?:volatile\s+)?(?:\w+|WS\(\w+\)))*\s*);/sx) {
372 my @parts = split(/\s*,\s*/, $1);
373 &$type_end([@parts]);
375 die "$file: $.: syntax error: '$_'\n";
382 } elsif(/(extern\s+|static\s+)?((interface\s+|struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
383 (($CALL_CONVENTION)\s+)?
384 (\w+(\(\w+\))?)\s*\((.*?)\)\s*(\{|\;)/sx)
386 my @lines = split(/\n/, $&);
387 my $function_line = $. - scalar(@lines) + 1;
396 my $return_type = $2;
397 my $calling_convention = $7;
401 if(!defined($linkage)) {
405 if(!defined($calling_convention)) {
406 $calling_convention = "";
409 $linkage =~ s/\s*$//;
411 $return_type =~ s/\s*$//;
412 $return_type =~ s/\s*\*\s*/*/g;
413 $return_type =~ s/(\*+)/ $1/g;
415 if($regs_entrypoints{$name}) {
416 $name = $regs_entrypoints{$name};
419 $arguments =~ y/\t\n/ /;
420 $arguments =~ s/^\s*(.*?)\s*$/$1/;
421 if($arguments eq "") { $arguments = "..." }
427 while ($arguments =~ s/^((?:[^,\(\)]*|(?:\([^\)]*\))?)+)(?:,|$)// && $1) {
429 push @arguments, $argument;
431 my $argument_type = "";
432 my $argument_name = "";
434 $argument =~ s/^\s*(.*?)\s*$/$1/;
435 # print " " . ($n + 1) . ": '$argument'\n";
436 $argument =~ s/^(?:IN OUT|IN|OUT)?\s+//;
437 $argument =~ s/^(?:const|CONST|GDIPCONST|volatile)?\s+//;
438 if($argument =~ /^\.\.\.$/) {
439 $argument_type = "...";
440 $argument_name = "...";
441 } elsif($argument =~ /^
442 ((?:interface\s+|struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)?
443 (?:short\s+(?=int)|long\s+(?=int))?)?(?:\w+|ElfW\(\w+\)|WS\(\w+\)))\s*
444 ((?:__RPC_FAR|const|CONST|GDIPCONST|volatile)?\s*(?:\*\s*(?:__RPC_FAR|const|CONST|volatile)?\s*?)*)\s*
445 (\w*)\s*(\[\])?(?:\s+OPTIONAL)?$/x)
449 $argument_type .= " $2";
452 $argument_type .= "$4";
455 } elsif ($argument =~ /^
456 ((?:interface\s+|struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)?
457 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
458 ((?:const|volatile)?\s*(?:\*\s*(?:const|volatile)?\s*?)*)\s*
459 (?:(?:$CALL_CONVENTION)\s+)?
460 \(\s*(?:$CALL_CONVENTION)?\s*\*\s*((?:\w+)?)\s*\)\s*
463 my $return_type = $1;
465 $return_type .= " $2";
470 $return_type =~ s/\s+/ /g;
471 $arguments =~ s/\s*,\s*/,/g;
473 $argument_type = "$return_type (*)($arguments)";
474 } elsif ($argument =~ /^
475 ((?:interface\s+|struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
476 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
477 ((?:const|volatile)?\s*(?:\*\s*(?:const|volatile)?\s*?)*)\s*
478 (\w+)\s*\[\s*(.*?)\s*\](?:\[\s*(.*?)\s*\])?$/x)
480 my $return_type = $1;
482 $return_type .= " $2";
486 $argument_type = "$return_type\[$4\]";
489 $argument_type .= "\[$5\]";
492 # die "$file: $.: syntax error: '$argument_type':'$argument_name'\n";
494 die "$file: $.: syntax error: '$argument'\n";
497 $argument_type =~ s/\s*(?:const|volatile)\s*/ /g; # Remove const/volatile
498 $argument_type =~ s/([^\*\(\s])\*/$1 \*/g; # Assure whitespace between non-* and *
499 $argument_type =~ s/,([^\s])/, $1/g; # Assure whitespace after ,
500 $argument_type =~ s/\*\s+\*/\*\*/g; # Remove whitespace between * and *
501 $argument_type =~ s/([\(\[])\s+/$1/g; # Remove whitespace after ( and [
502 $argument_type =~ s/\s+([\)\]])/$1/g; # Remove whitespace before ] and )
503 $argument_type =~ s/\s+/ /; # Remove multiple whitespace
504 $argument_type =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
506 $argument_name =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
508 $argument_types[$n] = $argument_type;
509 $argument_names[$n] = $argument_name;
510 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
514 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
515 $#argument_types = -1;
516 $#argument_names = -1;
519 if($options->debug) {
520 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
523 &$function_begin($documentation_line, $documentation,
524 $function_line, $linkage, $return_type, $calling_convention, $name,
525 \@argument_types,\@argument_names,\@argument_documentations);
527 &$function_end(undef, undef);
529 $statements_line = $.;
531 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
532 my @lines = split(/\n/, $&);
533 my $function_line = $. - scalar(@lines) + 1;
537 &$function_begin($documentation_line, $documentation,
538 $function_line, "", "void", "__asm", $1);
539 &$function_end($., "");
540 } elsif(/DEFINE_THISCALL_WRAPPER\((\S*)\)/s) {
541 my @lines = split(/\n/, $&);
542 my $function_line = $. - scalar(@lines) + 1;
546 &$function_begin($documentation_line, $documentation,
547 $function_line, "", "void", "", "__thiscall_" . $1, \());
548 &$function_end($function_line, "");
549 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
551 $regs_entrypoints{$2} = $1;
552 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
554 unshift @$debug_channels, $1;
555 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
557 push @$debug_channels, $1;
558 } elsif(/typedef\s+(enum|interface|struct|union)(?:\s+(\w+))?\s*\{/s) {
567 ((?:const\s+|CONST\s+|enum\s+|interface\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+|volatile\s+)*?)
569 (?:\s+const|\s+volatile)?
570 ((?:\s*(?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+\s*|\s+)(?:volatile\s+|DECLSPEC_ALIGN\(\d+\)\s+)?\w+\s*(?:\[[^\]]*\])*
571 (?:\s*,\s*(?:\s*(?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
579 my @parts = split(/\s*,\s*/, $2);
580 foreach my $part (@parts) {
581 if($part =~ /(?:\s*((?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
593 &$type_end([@names]);
595 (?:(?:const\s+|enum\s+|interface\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+|volatile\s+)*?)
596 (\w+(?:\s*\*+\s*)?)\s*
598 \((?:(\w+)\s*)?\s*(?:\*\s*(\w+)|_ATL_CATMAPFUNC)\s*\)\s*
599 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
603 if(defined($2) || defined($3)) {
606 $type = "$1 ($cc *)($5)";
608 $type = "$1 ($cc *)[$6]";
612 $type = "$1 (*)($5)";
614 $type = "$1 (*)[$6]";
620 } elsif(/typedef[^\{;]*;/s) {
622 $output->write("$file: $.: can't parse: '$&'\n");
623 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
625 $output->write("$file: $.: can't parse: '$&'\n");
626 } elsif(/\'[^\']*\'/s) {
628 } elsif(/\"(?:[^\\\"]*|\\.)*\"/s) {
632 } elsif(/extern\s+"C"\s+{/s) {
636 print "+1: $_\n" if $options->debug >= 2;
643 print STDERR "done\n" if $options->verbose;
644 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;