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 "SEC_ENTRY|VFWAPI|VFWAPIV|WINGDIPAPI|WMIAPI|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 (?:DECLSPEC_HOTPATCH\s+)?
385 (\w+(\(\w+\))?)\s*\((.*?)\)\s*(\{|\;)/sx)
387 my @lines = split(/\n/, $&);
388 my $function_line = $. - scalar(@lines) + 1;
397 my $return_type = $2;
398 my $calling_convention = $7;
402 if(!defined($linkage)) {
406 if(!defined($calling_convention)) {
407 $calling_convention = "";
410 $linkage =~ s/\s*$//;
412 $return_type =~ s/\s*$//;
413 $return_type =~ s/\s*\*\s*/*/g;
414 $return_type =~ s/(\*+)/ $1/g;
416 if($regs_entrypoints{$name}) {
417 $name = $regs_entrypoints{$name};
420 $arguments =~ y/\t\n/ /;
421 $arguments =~ s/^\s*(.*?)\s*$/$1/;
422 if($arguments eq "") { $arguments = "..." }
428 while ($arguments =~ s/^((?:[^,\(\)]*|(?:\([^\)]*\))?)+)(?:,|$)// && $1) {
430 push @arguments, $argument;
432 my $argument_type = "";
433 my $argument_name = "";
435 $argument =~ s/^\s*(.*?)\s*$/$1/;
436 # print " " . ($n + 1) . ": '$argument'\n";
437 $argument =~ s/^(?:IN OUT|IN|OUT)?\s+//;
438 $argument =~ s/^(?:const|CONST|GDIPCONST|volatile)?\s+//;
439 if($argument =~ /^\.\.\.$/) {
440 $argument_type = "...";
441 $argument_name = "...";
442 } elsif($argument =~ /^
443 ((?:interface\s+|struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)?
444 (?:short\s+(?=int)|long\s+(?=int))?)?(?:\w+|ElfW\(\w+\)|WS\(\w+\)))\s*
445 ((?:__RPC_FAR|const|CONST|GDIPCONST|volatile)?\s*(?:\*\s*(?:__RPC_FAR|const|CONST|volatile)?\s*?)*)\s*
446 (\w*)\s*(\[\])?(?:\s+OPTIONAL)?$/x)
450 $argument_type .= " $2";
453 $argument_type .= "$4";
456 } elsif ($argument =~ /^
457 ((?:interface\s+|struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)?
458 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
459 ((?:const|volatile)?\s*(?:\*\s*(?:const|volatile)?\s*?)*)\s*
460 (?:(?:$CALL_CONVENTION)\s+)?
461 \(\s*(?:$CALL_CONVENTION)?\s*\*\s*((?:\w+)?)\s*\)\s*
464 my $return_type = $1;
466 $return_type .= " $2";
471 $return_type =~ s/\s+/ /g;
472 $arguments =~ s/\s*,\s*/,/g;
474 $argument_type = "$return_type (*)($arguments)";
475 } elsif ($argument =~ /^
476 ((?:interface\s+|struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
477 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
478 ((?:const|volatile)?\s*(?:\*\s*(?:const|volatile)?\s*?)*)\s*
479 (\w+)\s*\[\s*(.*?)\s*\](?:\[\s*(.*?)\s*\])?$/x)
481 my $return_type = $1;
483 $return_type .= " $2";
487 $argument_type = "$return_type\[$4\]";
490 $argument_type .= "\[$5\]";
493 # die "$file: $.: syntax error: '$argument_type':'$argument_name'\n";
495 die "$file: $.: syntax error: '$argument'\n";
498 $argument_type =~ s/\s*(?:const|volatile)\s*/ /g; # Remove const/volatile
499 $argument_type =~ s/([^\*\(\s])\*/$1 \*/g; # Assure whitespace between non-* and *
500 $argument_type =~ s/,([^\s])/, $1/g; # Assure whitespace after ,
501 $argument_type =~ s/\*\s+\*/\*\*/g; # Remove whitespace between * and *
502 $argument_type =~ s/([\(\[])\s+/$1/g; # Remove whitespace after ( and [
503 $argument_type =~ s/\s+([\)\]])/$1/g; # Remove whitespace before ] and )
504 $argument_type =~ s/\s+/ /; # Remove multiple whitespace
505 $argument_type =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
507 $argument_name =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
509 $argument_types[$n] = $argument_type;
510 $argument_names[$n] = $argument_name;
511 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
515 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
516 $#argument_types = -1;
517 $#argument_names = -1;
520 if($options->debug) {
521 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
524 &$function_begin($documentation_line, $documentation,
525 $function_line, $linkage, $return_type, $calling_convention, $name,
526 \@argument_types,\@argument_names,\@argument_documentations);
528 &$function_end(undef, undef);
530 $statements_line = $.;
532 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
533 my @lines = split(/\n/, $&);
534 my $function_line = $. - scalar(@lines) + 1;
538 &$function_begin($documentation_line, $documentation,
539 $function_line, "", "void", "__asm", $1);
540 &$function_end($., "");
541 } elsif(/DEFINE_THISCALL_WRAPPER\((\S*)\)/s) {
542 my @lines = split(/\n/, $&);
543 my $function_line = $. - scalar(@lines) + 1;
547 &$function_begin($documentation_line, $documentation,
548 $function_line, "", "void", "", "__thiscall_" . $1, \());
549 &$function_end($function_line, "");
550 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
552 $regs_entrypoints{$2} = $1;
553 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
555 unshift @$debug_channels, $1;
556 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
558 push @$debug_channels, $1;
559 } elsif(/typedef\s+(enum|interface|struct|union)(?:\s+(\w+))?\s*\{/s) {
568 ((?:const\s+|CONST\s+|enum\s+|interface\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+|volatile\s+)*?)
570 (?:\s+const|\s+volatile)?
571 ((?:\s*(?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+\s*|\s+)(?:volatile\s+|DECLSPEC_ALIGN\(\d+\)\s+)?\w+\s*(?:\[[^\]]*\])*
572 (?:\s*,\s*(?:\s*(?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
580 my @parts = split(/\s*,\s*/, $2);
581 foreach my $part (@parts) {
582 if($part =~ /(?:\s*((?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
594 &$type_end([@names]);
596 (?:(?:const\s+|enum\s+|interface\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+|volatile\s+)*?)
597 (\w+(?:\s*\*+\s*)?)\s*
599 \((?:(\w+)\s*)?\s*(?:\*\s*(\w+)|_ATL_CATMAPFUNC)\s*\)\s*
600 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
604 if(defined($2) || defined($3)) {
607 $type = "$1 ($cc *)($5)";
609 $type = "$1 ($cc *)[$6]";
613 $type = "$1 (*)($5)";
615 $type = "$1 (*)[$6]";
621 } elsif(/typedef[^\{;]*;/s) {
623 $output->write("$file: $.: can't parse: '$&'\n");
624 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
626 $output->write("$file: $.: can't parse: '$&'\n");
627 } elsif(/\'[^\']*\'/s) {
629 } elsif(/\"(?:[^\\\"]*|\\.)*\"/s) {
633 } elsif(/extern\s+"C"\s+{/s) {
637 print "+1: $_\n" if $options->debug >= 2;
644 print STDERR "done\n" if $options->verbose;
645 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;