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);
26 sub parse_c_file($$) {
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 readmore: 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");
197 if (s/^([^\"\/]+|\"(?:[^\\\"]*|\\.)*\")//)
204 if(s/^(\/\*.*?\*\/)//s) {
205 my @lines = split(/\n/, $1);
206 push @comment_lines, $.;
208 &$c_comment_found_callback($. - $#lines, $., $1);
212 $_ = $prefix . ("\n" x $#lines) . $_;
221 elsif (s/^(\/\/.*)$//)
223 # remove C++ comments
224 &$cplusplus_comment_found_callback($., $1);
234 # remove preprocessor directives
240 } elsif(s/^\#\s*(\w+)((?:\s+(.*?))?\s*)$//s) {
241 my @lines = split(/\n/, $2);
246 &$preprocessor_found_callback($1, $3);
248 &$preprocessor_found_callback($1, "");
256 if(s/^\s*extern\s+"C"\s+\{//m) {
262 my $documentation_line;
264 my @argument_documentations = ();
267 while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
268 $comments[$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 = ();
286 push @argument_documentations, $comments[$m];
297 s/^([^\{\}\'\"]*)//s;
301 while(/^./ && !s/^\'//) {
318 while(/^./ && !s/^\"//) {
339 print "+1: \{$_\n" if $options->debug >= 2;
341 $statements .= $line;
344 $line .= "}" if $level > 1;
345 print "-1: \}$_\n" if $options->debug >= 2;
347 if($level == -1 && $extern_c) {
351 $statements .= $line;
353 $statements .= "$line\n";
358 &$function_end($statements_line, $statements);
361 if(/^\s*((?:(?:FAR\s*)?\*\s*(?:RESTRICTED_POINTER\s+)?)?
363 (?:\s*,\s*(?:(?:FAR\s*)?\*+\s*(?:RESTRICTED_POINTER\s+)?)?(?:\w+|WS\(\w+\)))*\s*);/sx) {
364 my @parts = split(/\s*,\s*/, $1);
365 &$type_end([@parts]);
367 die "$file: $.: syntax error: '$_'\n";
374 } elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
375 ((__cdecl|__stdcall|CDECL|NET_API_FUNCTION|RPC_ENTRY|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
376 (\w+(\(\w+\))?)\s*\((.*?)\)\s*(\{|\;)/sx)
378 my @lines = split(/\n/, $&);
379 my $function_line = $. - scalar(@lines) + 1;
388 my $return_type = $2;
389 my $calling_convention = $7;
393 if(!defined($linkage)) {
397 if(!defined($calling_convention)) {
398 $calling_convention = "";
401 $linkage =~ s/\s*$//;
403 $return_type =~ s/\s*$//;
404 $return_type =~ s/\s*\*\s*/*/g;
405 $return_type =~ s/(\*+)/ $1/g;
407 if($regs_entrypoints{$name}) {
408 $name = $regs_entrypoints{$name};
411 $arguments =~ y/\t\n/ /;
412 $arguments =~ s/^\s*(.*?)\s*$/$1/;
413 if($arguments eq "") { $arguments = "..." }
419 while ($arguments =~ s/^((?:[^,\(\)]*|(?:\([^\)]*\))?)+)(?:,|$)// && $1) {
421 push @arguments, $argument;
423 my $argument_type = "";
424 my $argument_name = "";
426 $argument =~ s/^\s*(.*?)\s*$/$1/;
427 # print " " . ($n + 1) . ": '$argument'\n";
428 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
429 $argument =~ s/^(const(?=\s)|CONST(?=\s)|volatile(?=\s)|\s*)\s*//;
430 if($argument =~ /^\.\.\.$/) {
431 $argument_type = "...";
432 $argument_name = "...";
433 } elsif($argument =~ /^
434 ((?:struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
435 (?:short\s+(?=int)|long\s+(?=int))?)?(?:\w+|ElfW\(\w+\)|WS\(\w+\)))\s*
436 ((?:__RPC_FAR|const|CONST|volatile)?\s*(?:\*\s*(?:__RPC_FAR|const|CONST|volatile)?\s*?)*)\s*
437 (\w*)\s*(\[\])?(?:\s+OPTIONAL)?$/x)
441 $argument_type .= " $2";
444 $argument_type .= "$4";
447 } elsif ($argument =~ /^
448 ((?:struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
449 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
450 ((?:const|volatile)?\s*(?:\*\s*(?:const|volatile)?\s*?)*)\s*
451 (?:__cdecl\s+|__stdcall\s+|CALLBACK\s+|CDECL\s+|NET_API_FUNCTION\s+|RPC_ENTRY\s+|STDMETHODCALLTYPE\s+|VFWAPIV\s+|VFWAPI\s+|WINAPIV\s+|WINAPI\s+)?
452 \(\s*(?:__cdecl|__stdcall|CALLBACK|CDECL|NET_API_FUNCTION|RPC_ENTRY|STDMETHODCALLTYPE|VFWAPIV|VFWAPI|WINAPIV|WINAPI)?\s*\*\s*((?:\w+)?)\s*\)\s*
455 my $return_type = $1;
457 $return_type .= " $2";
462 $return_type =~ s/\s+/ /g;
463 $arguments =~ s/\s*,\s*/,/g;
465 $argument_type = "$return_type (*)($arguments)";
466 } elsif ($argument =~ /^
467 ((?:struct\s+|union\s+|enum\s+|register\s+|(?:signed\s+|unsigned\s+)
468 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
469 ((?:const|volatile)?\s*(?:\*\s*(?:const|volatile)?\s*?)*)\s*
470 (\w+)\s*\[\s*(.*?)\s*\](?:\[\s*(.*?)\s*\])?$/x)
472 my $return_type = $1;
474 $return_type .= " $2";
478 $argument_type = "$return_type\[$4\]";
481 $argument_type .= "\[$5\]";
484 # die "$file: $.: syntax error: '$argument_type':'$argument_name'\n";
486 die "$file: $.: syntax error: '$argument'\n";
489 $argument_type =~ s/\s*(?:const|volatile)\s*/ /g; # Remove const/volatile
490 $argument_type =~ s/([^\*\(\s])\*/$1 \*/g; # Assure whitespace between non-* and *
491 $argument_type =~ s/,([^\s])/, $1/g; # Assure whitespace after ,
492 $argument_type =~ s/\*\s+\*/\*\*/g; # Remove whitespace between * and *
493 $argument_type =~ s/([\(\[])\s+/$1/g; # Remove whitespace after ( and [
494 $argument_type =~ s/\s+([\)\]])/$1/g; # Remove whitespace before ] and )
495 $argument_type =~ s/\s+/ /; # Remove multiple whitespace
496 $argument_type =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
498 $argument_name =~ s/^\s*(.*?)\s*$/$1/; # Remove leading and trailing whitespace
500 $argument_types[$n] = $argument_type;
501 $argument_names[$n] = $argument_name;
502 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
506 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
507 $#argument_types = -1;
508 $#argument_names = -1;
511 if($options->debug) {
512 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
515 &$function_begin($documentation_line, $documentation,
516 $function_line, $linkage, $return_type, $calling_convention, $name,
517 \@argument_types,\@argument_names,\@argument_documentations);
519 &$function_end(undef, undef);
521 $statements_line = $.;
523 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
524 my @lines = split(/\n/, $&);
525 my $function_line = $. - scalar(@lines) + 1;
529 &$function_begin($documentation_line, $documentation,
530 $function_line, "", "void", "__asm", $1);
531 &$function_end($., "");
532 } elsif(/DEFINE_THISCALL_WRAPPER\((\S*)\)/s) {
533 my @lines = split(/\n/, $&);
534 my $function_line = $. - scalar(@lines) + 1;
538 &$function_begin($documentation_line, $documentation,
539 $function_line, "", "void", "", "__thiscall_" . $1, \());
540 &$function_end($function_line, "");
541 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
543 $regs_entrypoints{$2} = $1;
544 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
546 unshift @$debug_channels, $1;
547 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
549 push @$debug_channels, $1;
550 } elsif(/typedef\s+(enum|struct|union)(?:\s+(\w+))?\s*\{/s) {
559 ((?:const\s+|CONST\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+|volatile\s+)*?)
561 (?:\s+const|\s+volatile)?
562 ((?:\s*(?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+\s*|\s+)(?:volatile\s+|DECLSPEC_ALIGN\(\d+\)\s+)?\w+\s*(?:\[[^\]]*\])*
563 (?:\s*,\s*(?:\s*(?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
571 my @parts = split(/\s*,\s*/, $2);
572 foreach my $part (@parts) {
573 if($part =~ /(?:\s*((?:(?:FAR|__RPC_FAR|TW_HUGE)?\s*)?\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
585 &$type_end([@names]);
587 (?:(?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+|volatile\s+)*?)
588 (\w+(?:\s*\*+\s*)?)\s*
590 \((?:(\w+)\s*)?\s*(?:\*\s*(\w+)|_ATL_CATMAPFUNC)\s*\)\s*
591 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
595 if(defined($2) || defined($3)) {
598 $type = "$1 ($cc *)($5)";
600 $type = "$1 ($cc *)[$6]";
604 $type = "$1 (*)($5)";
606 $type = "$1 (*)[$6]";
612 } elsif(/typedef[^\{;]*;/s) {
614 $output->write("$file: $.: can't parse: '$&'\n");
615 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
617 $output->write("$file: $.: can't parse: '$&'\n");
618 } elsif(/\'[^\']*\'/s) {
620 } elsif(/\"(?:[^\\\"]*|\\.)*\"/s) {
624 } elsif(/extern\s+"C"\s+{/s) {
628 print "+1: $_\n" if $options->debug >= 2;
635 print STDERR "done\n" if $options->verbose;
636 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;