5 use output qw($output);
6 use options qw($options);
10 my $function_create_callback = shift;
11 my $function_found_callback = shift;
12 my $type_create_callback = shift;
13 my $type_found_callback = shift;
14 my $preprocessor_found_callback = shift;
17 my $debug_channels = [];
23 my $documentation_line;
28 my $calling_convention;
29 my $internal_name = "";
32 my $argument_documentations;
36 $function_begin = sub {
37 $documentation_line = shift;
38 $documentation = shift;
39 $function_line = shift;
42 $calling_convention = shift;
43 $internal_name = shift;
44 $argument_types = shift;
45 $argument_names = shift;
46 $argument_documentations = shift;
48 if(defined($argument_names) && defined($argument_types) &&
49 $#$argument_names == -1)
51 foreach my $n (0..$#$argument_types) {
52 push @$argument_names, "";
56 if(defined($argument_documentations) &&
57 $#$argument_documentations == -1)
59 foreach my $n (0..$#$argument_documentations) {
60 push @$argument_documentations, "";
68 $statements_line = shift;
71 my $function = &$function_create_callback();
73 if(!defined($documentation_line)) {
74 $documentation_line = 0;
77 $function->file($file);
78 $function->debug_channels([@$debug_channels]);
79 $function->documentation_line($documentation_line);
80 $function->documentation($documentation);
81 $function->function_line($function_line);
82 $function->linkage($linkage);
83 $function->return_type($return_type);
84 $function->calling_convention($calling_convention);
85 $function->internal_name($internal_name);
86 if(defined($argument_types)) {
87 $function->argument_types([@$argument_types]);
89 if(defined($argument_names)) {
90 $function->argument_names([@$argument_names]);
92 if(defined($argument_documentations)) {
93 $function->argument_documentations([@$argument_documentations]);
95 $function->statements_line($statements_line);
96 $function->statements($statements);
98 &$function_found_callback($function);
118 foreach my $name (@$names) {
119 if($type =~ /^(?:struct|enum)/) {
120 # $output->write("typedef $type {\n");
121 # $output->write("} $name;\n");
123 # $output->write("typedef $type $name;\n");
130 my %regs_entrypoints;
131 my @comment_lines = ();
139 my $lookahead_count = 0;
141 print STDERR "Processing file '$file' ... " if $options->verbose;
142 open(IN, "< $file") || die "<internal>: $file: $!\n";
144 while($again || defined(my $line = <IN>)) {
154 $lookahead_count = 0;
156 print " $level($lookahead_count): $line\n" if $options->debug >= 2;
157 print "*** $_\n" if $options->debug >= 3;
159 $lookahead_count = 0;
163 # CVS merge conflicts in file?
164 if(/^(<<<<<<<|=======|>>>>>>>)/) {
165 $output->write("$file: merge conflicts in file\n");
170 if(/^(.*?)(\/\*(.*?)\*\/)(.*)$/s) {
171 my @lines = split(/\n/, $2);
172 push @comment_lines, $.;
177 $_ = $1 . ("\n" x $#lines) . $4;
187 # remove C++ comments
188 while(s/^(.*?)\/\/.*?$/$1/s) { $again = 1 }
191 # remove preprocessor directives
196 } elsif(s/^\#\s*(\w+)((?:\s+(.*?))?\s*)$//s) {
197 my @lines = split(/\n/, $2);
202 &$preprocessor_found_callback($1, $3);
204 &$preprocessor_found_callback($1, "");
212 if(s/^\s*extern\s+"C"\s+\{//m) {
218 my $documentation_line;
220 my @argument_documentations = ();
223 while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
224 $comments[$n] =~ /^\/\*\*+\/$/))
229 if(defined($comments[$n]) && $n >= 0) {
230 my @lines = split(/\n/, $comments[$n]);
232 $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
233 $documentation = $comments[$n];
235 for(my $m=$n+1; $m <= $#comments; $m++) {
236 if($comments[$m] =~ /^\/\*\*+\/$/ ||
237 $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
239 @argument_documentations = ();
242 push @argument_documentations, $comments[$m];
253 s/^([^\{\}\'\"]*)//s;
257 while(/^./ && !s/^\'//) {
274 while(/^./ && !s/^\"//) {
295 print "+1: \{$_\n" if $options->debug >= 2;
297 $statements .= $line;
300 $line .= "}" if $level > 1;
301 print "-1: \}$_\n" if $options->debug >= 2;
303 if($level == -1 && $extern_c) {
307 $statements .= $line;
309 $statements .= "$line\n";
314 &$function_end($statements_line, $statements);
317 if(/^\s*(?:WINE_PACKED\s+)?((?:\*\s*)?\w+\s*(?:\s*,\s*(?:\*+\s*)?\w+)*\s*);/s) {
318 my @parts = split(/\s*,\s*/, $1);
319 &$type_end([@parts]);
321 die "$file: $.: syntax error: '$_'\n";
328 } elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
329 ((__cdecl|__stdcall|CDECL|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
330 (\w+(\(\w+\))?)\s*\(([^\)]*)\)\s*(\{|\;)/sx)
332 my @lines = split(/\n/, $&);
333 my $function_line = $. - scalar(@lines) + 1;
342 my $return_type = $2;
343 my $calling_convention = $7;
347 if(!defined($linkage)) {
351 if(!defined($calling_convention)) {
352 $calling_convention = "";
355 $linkage =~ s/\s*$//;
357 $return_type =~ s/\s*$//;
358 $return_type =~ s/\s*\*\s*/*/g;
359 $return_type =~ s/(\*+)/ $1/g;
361 if($regs_entrypoints{$name}) {
362 $name = $regs_entrypoints{$name};
365 $arguments =~ y/\t\n/ /;
366 $arguments =~ s/^\s*(.*?)\s*$/$1/;
367 if($arguments eq "") { $arguments = "..." }
371 my @arguments = split(/,/, $arguments);
372 foreach my $n (0..$#arguments) {
373 my $argument_type = "";
374 my $argument_name = "";
375 my $argument = $arguments[$n];
376 $argument =~ s/^\s*(.*?)\s*$/$1/;
377 # print " " . ($n + 1) . ": '$argument'\n";
378 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
379 $argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
380 if($argument =~ /^\.\.\.$/) {
381 $argument_type = "...";
382 $argument_name = "...";
383 } elsif($argument =~ /^
384 ((?:struct\s+|union\s+|enum\s+|(?:signed\s+|unsigned\s+)
385 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
386 ((?:const)?\s*(?:\*\s*?)*)\s*
387 (?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL)?/x)
389 $argument_type = "$1";
391 $argument_type .= " $2";
395 $argument_type =~ s/\s*const\s*/ /;
396 $argument_type =~ s/^\s*(.*?)\s*$/$1/;
398 $argument_name =~ s/^\s*(.*?)\s*$/$1/;
400 die "$file: $.: syntax error: '$argument'\n";
402 $argument_types[$n] = $argument_type;
403 $argument_names[$n] = $argument_name;
404 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
406 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
407 $#argument_types = -1;
408 $#argument_names = -1;
411 if($options->debug) {
412 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
415 &$function_begin($documentation_line, $documentation,
416 $function_line, $linkage, $return_type, $calling_convention, $name,
417 \@argument_types,\@argument_names,\@argument_documentations);
419 &$function_end(undef, undef);
421 $statements_line = $.;
423 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
424 my @lines = split(/\n/, $&);
425 my $function_line = $. - scalar(@lines) + 1;
429 &$function_begin($documentation_line, $documentation,
430 $function_line, "", "void", "__asm", $1);
431 &$function_end($., "");
432 } elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
433 my @lines = split(/\n/, $&);
434 my $function_line = $. - scalar(@lines) + 1;
437 my @arguments16 = ("HWAVEIN16");
438 my @arguments32 = ("HWAVEIN");
439 &$function_begin($documentation_line, $documentation,
440 $function_line, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
441 &$function_end($., "");
442 &$function_begin($documentation_line, $documentation,
443 $function_line, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
444 &$function_end($., "");
445 } elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
446 my @lines = split(/\n/, $&);
447 my $function_line = $. - scalar(@lines) + 1;
451 my @arguments16 = ("HWAVEOUT16");
452 my @arguments32 = ("HWAVEOUT");
453 &$function_begin($documentation_line, $documentation,
454 $function_line, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
455 &$function_end($., "");
456 &$function_begin($documentation_line, $documentation,
457 $function_line, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
458 &$function_end($., "");
459 } elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
460 my @lines = split(/\n/, $&);
461 my $function_line = $. - scalar(@lines) + 1;
466 my @arguments16 = ("HWAVEOUT16", $4);
467 my @arguments32 = ("HWAVEOUT", $4);
468 &$function_begin($documentation_line, $documentation,
469 $function_line, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
470 &$function_end($., "");
471 &$function_begin($documentation_line, $documentation,
472 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
473 &$function_end($., "");
475 my @arguments16 = ("UINT16", $4);
476 my @arguments32 = ("UINT", $4);
477 &$function_begin($documentation_line, $documentation,
478 $function_line, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
479 &$function_end($., "");
480 &$function_begin($documentation_line, $documentation,
481 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
482 &$function_end($., "");
484 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
486 $regs_entrypoints{$2} = $1;
487 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
489 unshift @$debug_channels, $1;
490 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
492 push @$debug_channels, $1;
493 } elsif(/typedef\s+(enum|struct|union)(?:\s+(\w+))?\s*\{/s) {
502 ((?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
505 ((?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?
506 (?:\s*,\s*(?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
514 my @parts = split(/\s*,\s*/, $2);
515 foreach my $part (@parts) {
516 if($part =~ /(?:\s*(\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
528 &$type_end([@names]);
530 (?:(?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
533 \((?:(\w+)\s+)?\s*\*\s*(\w+)\s*\)\s*
534 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
538 if(defined($2) || defined($3)) {
541 $type = "$1 ($cc *)($5)";
543 $type = "$1 ($cc *)[$6]";
547 $type = "$1 (*)($5)";
549 $type = "$1 (*)[$6]";
555 } elsif(/typedef[^\{;]*;/s) {
557 $output->write("$file: $.: can't parse: '$&'\n");
558 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
560 $output->write("$file: $.: can't parse: '$&'\n");
561 } elsif(/\'[^\']*\'/s) {
563 } elsif(/\"[^\"]*\"/s) {
567 } elsif(/extern\s+"C"\s+{/s) {
571 print "+1: $_\n" if $options->debug >= 2;
578 print STDERR "done\n" if $options->verbose;
579 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;