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
24 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK);
31 use options
qw($options);
32 use output qw($output);
37 # Defined a couple common regexp tidbits
38 my $CALL_CONVENTION="__cdecl|__stdcall|" .
39 "__RPC_API|__RPC_STUB|__RPC_USER|" .
40 "CALLBACK|CDECL|NTAPI|PASCAL|RPC_ENTRY|RPC_VAR_ENTRY|" .
41 "SEC_ENTRY|VFWAPI|VFWAPIV|WINGDIPAPI|WMIAPI|WINAPI|WINAPIV|APIENTRY|";
44 sub parse_c_function($$$$$);
45 sub parse_c_function_call($$$$$$$$);
46 sub parse_c_preprocessor($$$$);
47 sub parse_c_statements($$$$);
48 sub parse_c_tuple($$$$$$$);
49 sub parse_c_type($$$$$);
50 sub parse_c_typedef($$$$);
51 sub parse_c_variable($$$$$$$);
56 my ($proto, $filename) = @_;
57 my $class = ref($proto) || $proto;
58 my $self = {FILE => $filename,
59 CREATE_FUNCTION => sub { return new c_function; },
60 CREATE_TYPE => sub { return new c_type; },
61 FOUND_COMMENT => sub { return 1; },
62 FOUND_DECLARATION => sub { return 1; },
63 FOUND_FUNCTION => sub { return 1; },
64 FOUND_FUNCTION_CALL => sub { return 1; },
65 FOUND_LINE => sub { return 1; },
66 FOUND_PREPROCESSOR => sub { return 1; },
67 FOUND_STATEMENT => sub { return 1; },
68 FOUND_TYPE => sub { return 1; },
69 FOUND_VARIABLE => sub { return 1; }
71 bless ($self, $class);
80 sub set_found_comment_callback($$)
82 my ($self, $found_comment) = @_;
83 $self->{FOUND_COMMENT} = $found_comment;
86 sub set_found_declaration_callback($$)
88 my ($self, $found_declaration) = @_;
89 $self->{FOUND_DEClARATION} = $found_declaration;
92 sub set_found_function_callback($$)
94 my ($self, $found_function) = @_;
95 $self->{FOUND_FUNCTION} = $found_function;
98 sub set_found_function_call_callback($$)
100 my ($self, $found_function_call) = @_;
101 $self->{FOUND_FUNCTION_CALL} = $found_function_call;
104 sub set_found_line_callback($$)
106 my ($self, $found_line) = @_;
107 $self->{FOUND_LINE} = $found_line;
110 sub set_found_preprocessor_callback($$)
112 my ($self, $found_preprocessor) = @_;
113 $self->{FOUND_PREPROCESSOR} = $found_preprocessor;
116 sub set_found_statement_callback($$)
118 my ($self, $found_statement) = @_;
119 $self->{FOUND_STATEMENT} = $found_statement;
122 sub set_found_type_callback($$)
124 my ($self, $found_type) = @_;
125 $self->{FOUND_TYPE} = $found_type;
128 sub set_found_variable_callback($$)
130 my ($self, $found_variable) = @_;
131 $self->{FOUND_VARIABLE} = $found_variable;
135 ########################################################################
137 sub _format_c_type($$)
139 my ($self, $type) = @_;
141 $type =~ s/^\s*(.*?)\s*$/$1/;
143 if ($type =~ /^(\w+(?:\s*\*)*)\s*\(\s*\*\s*\)\s*\(\s*(.*?)\s*\)$/s) {
144 my $return_type = $1;
145 my @arguments = split(/\s*,\s*/, $2);
146 foreach my $argument (@arguments) {
147 if ($argument =~ s/^(\w+(?:\s*\*)*)\s*\w+$/$1/) {
148 $argument =~ s/\s+/ /g;
149 $argument =~ s/\s*\*\s*/*/g;
150 $argument =~ s/(\*+)$/ $1/;
154 $type = "$return_type (*)(" . join(", ", @arguments) . ")";
161 ########################################################################
164 # FIXME: Use caller (See man perlfunc)
165 sub _parse_c_warning($$$$$$)
167 my ($self, $curlines, $line, $column, $context, $message) = @_;
169 $message = "warning" if !$message;
173 my @lines = split(/\n/, $curlines);
175 $current .= $lines[0] . "\n" if $lines[0];
176 $current .= $lines[1] . "\n" if $lines[1];
180 $output->write("$self->{FILE}:$line." . ($column + 1) . ": $context: $message: \\\n$current");
182 $output->write("$self->{FILE}:$line." . ($column + 1) . ": $context: $message\n");
186 ########################################################################
189 sub _parse_c_error($$$$$$)
191 my ($self, $curlines, $line, $column, $context, $message) = @_;
193 $message = "parse error" if !$message;
196 if($output->prefix) {
197 # $output->write("\n");
201 $self->_parse_c_warning($curlines, $line, $column, $context, $message);
206 ########################################################################
209 sub _update_c_position($$$$)
211 my ($self, $source, $refline, $refcolumn) = @_;
212 my $line = $$refline;
213 my $column = $$refcolumn;
217 if ($source =~ s/^[^\n\t\'\"]*//s)
219 $column += length($&);
222 if ($source =~ s/^\'//)
225 while ($source =~ /^./ && $source !~ s/^\'//)
227 $source =~ s/^([^\'\\]*)//s;
228 $column += length($1);
229 if ($source =~ s/^\\//)
232 if ($source =~ s/^(.)//s)
234 $column += length($1);
237 $source =~ s/^(\d{0,3})//s;
238 $column += length($1);
245 elsif ($source =~ s/^\"//)
248 while ($source =~ /^./ && $source !~ s/^\"//)
250 $source =~ s/^([^\"\\]*)//s;
251 $column += length($1);
252 if ($source =~ s/^\\//)
255 if ($source =~ s/^(.)//s)
257 $column += length($1);
260 $source =~ s/^(\d{0,3})//s;
261 $column += length($1);
268 elsif ($source =~ s/^\n//)
273 elsif ($source =~ s/^\t//)
275 $column = $column + 8 - $column % 8;
280 $$refcolumn = $column;
283 ########################################################################
284 # __parse_c_until_one_of
286 sub __parse_c_until_one_of($$$$$$$) {
289 my $characters = shift;
290 my $on_same_level = shift;
291 my $refcurrent = shift;
293 my $refcolumn = shift;
296 local $_ = $$refcurrent;
297 my $line = $$refline;
298 my $column = $$refcolumn;
301 if(!defined($match)) {
303 $match = \$blackhole;
308 while(/^[^$characters]/s || $level > 0) {
312 if(s/^[^\(\)\[\]\{\}\n\t\'\"]*//s) {
315 } elsif ($on_same_level) {
316 if(s/^[^$characters\(\)\[\]\{\}\n\t\'\"]*//s) {
320 if(s/^[^$characters\n\t\'\"]*//s) {
327 while(/^./ && !s/^\'//) {
343 $$match .= $submatch;
344 $column += length($submatch);
347 while(/^./ && !s/^\"//) {
363 $$match .= $submatch;
364 $column += length($submatch);
365 } elsif($on_same_level && s/^[\(\[\{]//) {
369 $$match .= $submatch;
371 } elsif($on_same_level && s/^[\)\]\}]//) {
376 $$match .= $submatch;
380 $$match .= $submatch;
386 $$match .= $submatch;
392 $$match .= $submatch;
393 $column = $column + 8 - $column % 8;
395 $$match .= $submatch;
396 $column += length($submatch);
402 $$refcolumn = $column;
406 sub _parse_c_until_one_of($$$$$$)
408 my ($self, $characters, $refcurrent, $refline, $refcolumn, $match) = @_;
409 return $self->__parse_c_until_one_of($characters, 0, $refcurrent, $refline, $refcolumn, $match);
412 sub _parse_c_on_same_level_until_one_of($$$$$$)
414 my ($self, $characters, $refcurrent, $refline, $refcolumn, $match) = @_;
415 return $self->__parse_c_until_one_of($characters, 1, $refcurrent, $refline, $refcolumn, $match);
418 ########################################################################
421 sub parse_c_block($$$$$$$) {
424 my $refcurrent = shift;
426 my $refcolumn = shift;
428 my $refstatements = shift;
429 my $refstatements_line = shift;
430 my $refstatements_column = shift;
432 local $_ = $$refcurrent;
433 my $line = $$refline;
434 my $column = $$refcolumn;
436 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
446 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
448 my $statements_line = $line;
449 my $statements_column = $column;
454 $self->_parse_c_until_one_of("\\{\\}", \$_, \$line, \$column, \$match);
458 $statements .= $match;
474 $$refcolumn = $column;
475 $$refstatements = $statements;
476 $$refstatements_line = $statements_line;
477 $$refstatements_column = $statements_column;
482 sub parse_c_declaration($$$$)
484 my ($self, $refcurrent, $refline, $refcolumn) = @_;
486 local $_ = $$refcurrent;
487 my $line = $$refline;
488 my $column = $$refcolumn;
490 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
492 my $begin_line = $line;
493 my $begin_column = $column + 1;
495 my $end_line = $begin_line;
496 my $end_column = $begin_column;
497 $self->_update_c_position($_, \$end_line, \$end_column);
499 if(!$self->{FOUND_DECLARATION}($begin_line, $begin_column, $end_line, $end_column, $_)) {
507 my ($linkage, $type, $name);
509 if(s/^WINE_(?:DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\(\s*(\w+)\s*\)\s*//s) { # FIXME: Wine specific kludge
510 $self->_update_c_position($&, \$line, \$column);
511 } elsif(s/^__ASM_GLOBAL_FUNC\(\s*(\w+)\s*,\s*//s) { # FIXME: Wine specific kludge
512 $self->_update_c_position($&, \$line, \$column);
513 $self->_parse_c_until_one_of("\)", \$_, \$line, \$column);
517 } elsif(s/^__ASM_STDCALL_FUNC\(\s*(\w+)\s*,\s*\d+\s*,\s*//s) { # FIXME: Wine specific kludge
518 $self->_update_c_position($&, \$line, \$column);
519 $self->_parse_c_until_one_of("\)", \$_, \$line, \$column);
523 } elsif(s/^(?:DEFINE_AVIGUID|DEFINE_OLEGUID)\s*(?=\()//s) { # FIXME: Wine specific kludge
524 $self->_update_c_position($&, \$line, \$column);
528 my @argument_columns;
530 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
533 } elsif(s/^DEFINE_COMMON_NOTIFICATIONS\(\s*(\w+)\s*,\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
534 $self->_update_c_position($&, \$line, \$column);
535 } elsif(s/^MAKE_FUNCPTR\(\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
536 $self->_update_c_position($&, \$line, \$column);
537 } elsif(s/^START_TEST\(\s*(\w+)\s*\)\s*{//s) { # FIXME: Wine specific kludge
538 $self->_update_c_position($&, \$line, \$column);
539 } elsif(s/^int\s*_FUNCTION_\s*{//s) { # FIXME: Wine specific kludge
540 $self->_update_c_position($&, \$line, \$column);
541 } elsif(s/^(?:jump|strong)_alias//s) { # FIXME: GNU C library specific kludge
542 $self->_update_c_position($&, \$line, \$column);
543 } elsif(s/^(?:__asm__|asm)\s*\(//) {
544 $self->_update_c_position($&, \$line, \$column);
545 } elsif($self->parse_c_typedef(\$_, \$line, \$column)) {
547 } elsif($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type, \$name)) {
549 } elsif($self->parse_c_function(\$_, \$line, \$column, \$function)) {
550 if($self->{FOUND_FUNCTION}($function))
552 my $statements = $function->statements;
553 my $statements_line = $function->statements_line;
554 my $statements_column = $function->statements_column;
556 if(defined($statements)) {
557 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
563 $self->_parse_c_error($_, $line, $column, "declaration");
568 $$refcolumn = $column;
575 my ($self, $pattern, $refcurrent, $refline, $refcolumn, $refmatch) = @_;
577 local $_ = $$refcurrent;
578 my $line = $$refline;
579 my $column = $$refcolumn;
582 if(s/^(?:$pattern)//s) {
583 $self->_update_c_position($&, \$line, \$column);
589 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
593 $$refcolumn = $column;
600 sub parse_c_enum($$$$)
602 my ($self, $refcurrent, $refline, $refcolumn) = @_;
604 local $_ = $$refcurrent;
605 my $line = $$refline;
606 my $column = $$refcolumn;
608 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
610 if (!s/^enum\s+((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
613 my $_name = $1 || "";
615 $self->_update_c_position($&, \$line, \$column);
620 while ($self->_parse_c_on_same_level_until_one_of(',', \$_, \$line, \$column, \$match)) {
622 if ($match !~ /^(\w+)\s*(?:=\s*(.*?)\s*)?$/) {
623 $self->_parse_c_error($_, $line, $column, "enum");
626 my $enum_value = $2 || "";
628 # $output->write("enum:$_name:$enum_name:$enum_value\n");
631 if ($self->_parse_c(',', \$_, \$line, \$column)) {
633 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
636 my $tuple_line = $line;
637 my $tuple_column = $column - 1;
641 my @argument_columns;
643 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
644 \@arguments, \@argument_lines, \@argument_columns))
646 $self->_parse_c_error($_, $line, $column, "enum");
650 if ($#arguments >= 0) {
651 $name = $arguments[0];
656 $self->_parse_c_error($_, $line, $column, "enum");
660 $self->_update_c_position($_, \$line, \$column);
664 $$refcolumn = $column;
667 sub parse_c_expression($$$$)
669 my ($self, $refcurrent, $refline, $refcolumn) = @_;
671 local $_ = $$refcurrent;
672 my $line = $$refline;
673 my $column = $$refcolumn;
675 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
678 if(s/^(.*?)(\w+\s*\()/$2/s) {
679 $self->_update_c_position($1, \$line, \$column);
681 my $begin_line = $line;
682 my $begin_column = $column + 1;
687 my @argument_columns;
688 if(!$self->parse_c_function_call(\$_, \$line, \$column, \$name, \@arguments, \@argument_lines, \@argument_columns)) {
692 if($self->{FOUND_FUNCTION_CALL}($begin_line, $begin_column, $line, $column, $name, \@arguments))
694 while(defined(my $argument = shift @arguments) &&
695 defined(my $argument_line = shift @argument_lines) &&
696 defined(my $argument_column = shift @argument_columns))
698 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
706 $self->_update_c_position($_, \$line, \$column);
710 $$refcolumn = $column;
715 sub parse_c_file($$$$)
717 my ($self, $refcurrent, $refline, $refcolumn) = @_;
719 local $_ = $$refcurrent;
720 my $line = $$refline;
721 my $column = $$refcolumn;
723 my $declaration = "";
724 my $declaration_line = $line;
725 my $declaration_column = $column;
727 my $previous_line = 0;
728 my $previous_column = -1;
730 my $preprocessor_condition = "";
737 while($plevel > 0 || $blevel > 0) {
739 $self->_parse_c_until_one_of("#/\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
741 if($line != $previous_line) {
742 $self->{FOUND_LINE}($line);
744 # $self->{FOUND_LINE}("$line.$column");
746 $previous_line = $line;
747 $previous_column = $column;
749 if($match !~ /^\s+$/s && $options->debug) {
750 $self->_parse_c_warning($_, $line, $column, "file", "$plevel $blevel: '$declaration' '$match'");
753 if(!$declaration && $match =~ s/^\s+//s) {
754 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
758 $declaration .= $match;
761 if ($declaration =~ s/^extern\s*\"C\"//s) {
763 $self->_update_c_position($&, \$line, \$column);
765 $declaration_line = $line;
766 $declaration_column = $column;
771 } elsif ($extern_c && $blevel == 1 && $plevel == 1 && !$declaration) {
773 $self->_update_c_position($&, \$line, \$column);
775 $declaration_line = $line;
776 $declaration_column = $column;
781 } elsif($declaration =~ s/^(?:__DEFINE_(?:GET|SET)_SEG|OUR_GUID_ENTRY)\s*(?=\()//sx) { # FIXME: Wine specific kludge
783 if ($plevel > 2 || !s/^\)//) {
784 $declaration = "$prefix$declaration";
787 $self->_update_c_position($&, \$line, \$column);
792 my @argument_columns;
794 if(!$self->parse_c_tuple(\$declaration, \$declaration_line, \$declaration_column,
795 \@arguments, \@argument_lines, \@argument_columns))
797 $self->_parse_c_error($declaration, $declaration_line, $declaration_column, "file", "tuple expected");
801 $declaration_line = $line;
802 $declaration_column = $column;
806 } elsif ($declaration =~ s/^(?:DECL_WINELIB_TYPE_AW|DECLARE_HANDLE(?:16)?|TYPE_MARSHAL)\(\s*(\w+)\s*\)\s*//s) {
807 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
813 while(s/^.*?\n//) { $blank_lines++; }
816 $declaration_line = $line;
817 $declaration_column = $column;
819 $declaration .= "\n" x $blank_lines;
827 my $preprocessor_line = $line;
828 my $preprocessor_column = $column;
830 my $preprocessor = $&;
831 while(s/^(.*?)\\\s*\n//) {
833 $preprocessor .= "$1\n";
835 if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
838 $preprocessor .= "$1$3";
842 } elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
852 if($preprocessor =~ /^\#\s*if/) {
853 if($preprocessor =~ /^\#\s*if\s*0/) {
858 if($preprocessor =~ /^\#\s*ifdef\s+WORDS_BIGENDIAN$/) {
859 $preprocessor_condition = "defined(WORD_BIGENDIAN)";
860 # $output->write("'$preprocessor_condition':'$declaration'\n")
862 $preprocessor_condition = "";
865 } elsif($preprocessor =~ /^\#\s*else/) {
866 if ($preprocessor_condition ne "") {
867 $preprocessor_condition =~ "!$preprocessor_condition";
868 $preprocessor_condition =~ s/^!!/!/;
869 # $output->write("'$preprocessor_condition':'$declaration'\n")
871 } elsif($preprocessor =~ /^\#\s*endif/) {
879 if ($preprocessor_condition ne "") {
880 # $output->write("'$preprocessor_condition':'$declaration'\n");
881 $preprocessor_condition = "";
886 if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
891 if(s/^\/\*.*?\*\///s) {
892 $self->{FOUND_COMMENT}($line, $column + 1, $&);
898 $column += length($_);
900 } elsif(s/^\/\/(.*?)\n//) {
901 $self->{FOUND_COMMENT}($line, $column + 1, $&);
910 $line += $blank_lines;
911 if($blank_lines > 0) {
916 $declaration_line = $line;
917 $declaration_column = $column;
918 } elsif($blank_lines > 0) {
919 $declaration .= "\n" x $blank_lines;
941 $self->_parse_c_error($_, $line, $column, "file", ") without (");
944 if($plevel == 1 && $declaration =~ /^(__ASM_GLOBAL_FUNC|__ASM_STDCALL_FUNC)/) {
945 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
948 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
950 $declaration_line = $line;
951 $declaration_column = $column;
959 $self->_parse_c_error($_, $line, $column, "file", "} without {");
964 if($declaration =~ /^typedef/s ||
965 $declaration =~ /^(?:const\s+|extern\s+|static\s+|volatile\s+)*(?:interface|struct|union)(?:\s+\w+)?\s*\{/s)
968 } elsif($plevel == 1 && $blevel == 1) {
969 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
972 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
974 $declaration_line = $line;
975 $declaration_column = $column;
976 } elsif($column == 1 && !$extern_c) {
977 $self->_parse_c_warning("", $line, $column, "file", "inner } ends on column 1");
981 if($plevel == 1 && $blevel == 1) {
982 $declaration =~ s/\s*;$//;
983 if($declaration && !$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
986 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
988 $declaration_line = $line;
989 $declaration_column = $column;
991 } elsif(/^\s*$/ && $declaration =~ /^\s*$/ && $match =~ /^\s*$/) {
995 $self->_parse_c_error($_, $line, $column, "file", "parse error: '$declaration' '$match'");
1001 $$refcolumn = $column;
1006 sub parse_c_function($$$$$)
1008 my ($self, $refcurrent, $refline, $refcolumn, $reffunction) = @_;
1010 local $_ = $$refcurrent;
1011 my $line = $$refline;
1012 my $column = $$refcolumn;
1015 my $calling_convention = "";
1020 my @argument_columns;
1022 my $statements_line;
1023 my $statements_column;
1025 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1027 my $begin_line = $line;
1028 my $begin_column = $column + 1;
1030 if($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
1034 # $self->_parse_c_warning($_, $line, $column, "function", "");
1037 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1038 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1039 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1040 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1041 \$_, \$line, \$column, \$match))
1043 if($match =~ /^(?:extern|static)$/) {
1050 if($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1052 } elsif($self->_parse_c('WINE_EXCEPTION_FILTER\(\w+\)', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1055 if(!$self->parse_c_type(\$_, \$line, \$column, \$return_type)) {
1059 $self->_parse_c('inline|FAR', \$_, \$line, \$column);
1061 $self->_parse_c($CALL_CONVENTION,
1062 \$_, \$line, \$column, \$calling_convention);
1065 # FIXME: ???: Old variant of __attribute((const))
1066 $self->_parse_c('(?:const|volatile)', \$_, \$line, \$column);
1068 if(!$self->_parse_c('(?:operator\s*!=|(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)', \$_, \$line, \$column, \$name)) {
1074 $self->_update_c_position($&, \$line, \$column);
1078 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1084 $self->_update_c_position($&, \$line, \$column);
1086 $self->_parse_c_error($_, $line, $column, "function");
1092 if($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
1097 # FIXME: Implement proper handling of K&R C functions
1098 $self->_parse_c_until_one_of("{", \$_, \$line, \$column, $kar);
1101 $output->write("K&R: $kar\n");
1104 if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1108 my $end_line = $line;
1109 my $end_column = $column;
1113 $$refcolumn = $column;
1115 my $function = $self->{CREATE_FUNCTION}();
1117 $function->file($self->{FILE});
1118 $function->begin_line($begin_line);
1119 $function->begin_column($begin_column);
1120 $function->end_line($end_line);
1121 $function->end_column($end_column);
1122 $function->linkage($linkage);
1123 $function->return_type($return_type);
1124 $function->calling_convention($calling_convention);
1125 $function->name($name);
1126 # if(defined($argument_types)) {
1127 # $function->argument_types([@$argument_types]);
1129 # if(defined($argument_names)) {
1130 # $function->argument_names([@$argument_names]);
1132 $function->statements_line($statements_line);
1133 $function->statements_column($statements_column);
1134 $function->statements($statements);
1136 $$reffunction = $function;
1141 sub parse_c_function_call($$$$$$$$)
1143 my ($self, $refcurrent, $refline, $refcolumn, $refname, $refarguments, $refargument_lines, $refargument_columns) = @_;
1145 local $_ = $$refcurrent;
1146 my $line = $$refline;
1147 my $column = $$refcolumn;
1152 my @argument_columns;
1154 if(s/^(\w+)(\s*)(?=\()//s) {
1155 $self->_update_c_position($&, \$line, \$column);
1159 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1168 $$refcolumn = $column;
1171 @$refarguments = @arguments;
1172 @$refargument_lines = @argument_lines;
1173 @$refargument_columns = @argument_columns;
1179 sub parse_c_preprocessor($$$$)
1181 my ($self, $refcurrent, $refline, $refcolumn) = @_;
1183 local $_ = $$refcurrent;
1184 my $line = $$refline;
1185 my $column = $$refcolumn;
1187 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1189 my $begin_line = $line;
1190 my $begin_column = $column + 1;
1192 if(!$self->{FOUND_PREPROCESSOR}($begin_line, $begin_column, "$_")) {
1196 if(/^\#\s*define\s*(.*?)$/s) {
1197 $self->_update_c_position($_, \$line, \$column);
1198 } elsif(/^\#\s*else/s) {
1199 $self->_update_c_position($_, \$line, \$column);
1200 } elsif(/^\#\s*endif/s) {
1201 $self->_update_c_position($_, \$line, \$column);
1202 } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1203 $self->_update_c_position($_, \$line, \$column);
1204 } elsif(/^\#\s*include\s+(.*?)$/s) {
1205 $self->_update_c_position($_, \$line, \$column);
1206 } elsif(/^\#\s*undef\s+(.*?)$/s) {
1207 $self->_update_c_position($_, \$line, \$column);
1209 $self->_parse_c_error($_, $line, $column, "preprocessor");
1214 $$refcolumn = $column;
1219 sub parse_c_statement($$$$)
1221 my ($self, $refcurrent, $refline, $refcolumn) = @_;
1223 local $_ = $$refcurrent;
1224 my $line = $$refline;
1225 my $column = $$refcolumn;
1227 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1229 $self->_parse_c('(?:case\s+)?(\w+)\s*:\s*', \$_, \$line, \$column);
1231 # $output->write("$line.$column: statement: '$_'\n");
1237 my $statements_line;
1238 my $statements_column;
1239 if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1242 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1245 } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
1246 $self->_update_c_position($&, \$line, \$column);
1252 my @argument_columns;
1253 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1257 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1258 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1261 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1263 while(defined(my $argument = shift @arguments) &&
1264 defined(my $argument_line = shift @argument_lines) &&
1265 defined(my $argument_column = shift @argument_columns))
1267 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1269 } elsif(s/^else//) {
1270 $self->_update_c_position($&, \$line, \$column);
1271 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1274 } elsif(s/^return//) {
1275 $self->_update_c_position($&, \$line, \$column);
1276 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1277 if(!$self->parse_c_expression(\$_, \$line, \$column)) {
1280 } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1283 # $self->_parse_c_error($_, $line, $column, "statement");
1286 $self->_update_c_position($_, \$line, \$column);
1290 $$refcolumn = $column;
1295 sub parse_c_statements($$$$)
1297 my ($self, $refcurrent, $refline, $refcolumn) = @_;
1299 local $_ = $$refcurrent;
1300 my $line = $$refline;
1301 my $column = $$refcolumn;
1303 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1305 # $output->write("$line.$column: statements: '$_'\n");
1308 my $statement_line = $line;
1309 my $statement_column = $column;
1311 my $previous_line = -1;
1312 my $previous_column = -1;
1316 while($plevel > 0 || $blevel > 0) {
1318 $self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1320 if($previous_line == $line && $previous_column == $column) {
1321 $self->_parse_c_error($_, $line, $column, "statements", "no progress");
1323 $previous_line = $line;
1324 $previous_column = $column;
1326 # $output->write("'$match' '$_'\n");
1328 $statement .= $match;
1333 } elsif(s/^[\)\]]//) {
1336 $self->_parse_c_error($_, $line, $column, "statements");
1346 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1349 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1351 $statement_line = $line;
1352 $statement_column = $column;
1355 if($plevel == 1 && $blevel == 1) {
1356 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1360 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1362 $statement_line = $line;
1363 $statement_column = $column;
1367 } elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
1371 $self->_parse_c_error($_, $line, $column, "statements");
1375 $self->_update_c_position($_, \$line, \$column);
1379 $$refcolumn = $column;
1384 sub parse_c_struct_union($$$$$$$$$)
1386 my ($self, $refcurrent, $refline, $refcolumn, $refkind, $ref_name, $reffield_type_names, $reffield_names, $refnames) = @_;
1388 local $_ = $$refcurrent;
1389 my $line = $$refline;
1390 my $column = $$refcolumn;
1394 my @field_type_names = ();
1395 my @field_names = ();
1398 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1400 if (!s/^(interface|struct|union)(\s+((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+))?\s*\{\s*//s) {
1406 $self->_update_c_position($&, \$line, \$column);
1409 while ($_ && $self->_parse_c_on_same_level_until_one_of(';', \$_, \$line, \$column, \$match))
1412 my $field_type_name;
1415 if ($self->parse_c_variable(\$match, \$line, \$column, \$field_linkage, \$field_type_name, \$field_name)) {
1416 $field_type_name =~ s/\s+/ /g;
1418 push @field_type_names, $field_type_name;
1419 push @field_names, $field_name;
1420 # $output->write("$kind:$_name:$field_type_name:$field_name\n");
1422 $self->_parse_c_error($_, $line, $column, "typedef $kind: '$match'");
1425 if ($self->_parse_c(';', \$_, \$line, \$column)) {
1427 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1430 my $tuple_line = $line;
1431 my $tuple_column = $column - 1;
1435 my @argument_columns;
1437 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1438 \@arguments, \@argument_lines, \@argument_columns))
1440 $self->_parse_c_error($_, $line, $column, "$kind");
1443 foreach my $argument (@arguments) {
1444 my $name = $argument;
1451 $self->_parse_c_error($_, $line, $column, "$kind");
1457 $$refcolumn = $column;
1460 $$ref_name = $_name;
1461 @$reffield_type_names = @field_type_names;
1462 @$reffield_names = @field_names;
1463 @$refnames = @names;
1468 sub parse_c_tuple($$$$$$$)
1470 my ($self, $refcurrent, $refline, $refcolumn,
1471 # FIXME: Should not write directly
1472 $items, $item_lines, $item_columns) = @_;
1474 local $_ = $$refcurrent;
1476 my $line = $$refline;
1477 my $column = $$refcolumn;
1487 my $item_line = $line;
1488 my $item_column = $column + 1;
1491 while($plevel > 0) {
1493 $self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1501 push @$item_lines, $item_line;
1502 push @$item_columns, $item_column;
1503 push @$items, $item;
1513 push @$item_lines, $item_line;
1514 push @$item_columns, $item_column;
1515 push @$items, $item;
1516 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1518 $item_column = $column + 1;
1530 $$refcolumn = $column;
1535 sub parse_c_type($$$$$)
1537 my ($self, $refcurrent, $refline, $refcolumn, $reftype) = @_;
1539 local $_ = $$refcurrent;
1540 my $line = $$refline;
1541 my $column = $$refcolumn;
1545 $self->_parse_c("(?:const|volatile)", \$_, \$line, \$column);
1547 if($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1549 } elsif($self->_parse_c('(?:enum\s+|interface\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1550 \$_, \$line, \$column, \$type))
1560 $$refcolumn = $column;
1567 sub parse_c_typedef($$$$)
1569 my ($self, $refcurrent, $refline, $refcolumn) = @_;
1571 local $_ = $$refcurrent;
1572 my $line = $$refline;
1573 my $column = $$refcolumn;
1575 if (!$self->_parse_c("typedef", \$_, \$line, \$column)) {
1579 my ($kind, $name, @field_type_names, @field_names, @names);
1580 my ($linkage, $type_name);
1581 if ($self->parse_c_enum(\$_, \$line, \$column))
1585 elsif ($self->parse_c_struct_union(\$_, \$line, \$column,
1586 \$kind, \$name, \@field_type_names, \@field_names, \@names))
1589 foreach my $_name (@names)
1591 if ($_name =~ /^\w+$/)
1593 $base_name = $_name;
1597 $base_name="$kind $name" if (!defined $base_name and defined $name);
1598 $base_name=$kind if (!defined $base_name);
1599 foreach my $_name (@names) {
1600 if ($_name =~ /^\w+$/) {
1601 my $type = $self->{CREATE_TYPE}();
1604 $type->_name($name);
1605 $type->name($_name);
1606 $type->field_type_names([@field_type_names]);
1607 $type->field_names([@field_names]);
1609 $self->{FOUND_TYPE}($type);
1610 } elsif ($_name =~ /^(\*+)\s*(?:RESTRICTED_POINTER\s+)?(\w+)$/) {
1611 my $type_name = "$base_name $1";
1614 my $type = $self->{CREATE_TYPE}();
1617 $type->name($_name);
1618 $type->field_type_names([$type_name]);
1619 $type->field_names([""]);
1621 $self->{FOUND_TYPE}($type);
1623 $self->_parse_c_error($_, $line, $column, "typedef 2");
1627 elsif ($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type_name, \$name))
1629 $type_name =~ s/\s+/ /g;
1631 if(defined($type_name) && defined($name)) {
1632 my $type = $self->{CREATE_TYPE}();
1634 if (length($name) == 0) {
1635 $self->_parse_c_error($_, $line, $column, "typedef");
1640 $type->field_type_names([$type_name]);
1641 $type->field_names([""]);
1643 $self->{FOUND_TYPE}($type);
1646 $self->_parse_c_error($_, $line, $column, "typedef");
1651 $$refcolumn = $column;
1656 sub parse_c_variable($$$$$$$)
1658 my ($self, $refcurrent, $refline, $refcolumn, $reflinkage, $reftype, $refname) = @_;
1660 local $_ = $$refcurrent;
1661 my $line = $$refline;
1662 my $column = $$refcolumn;
1664 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1666 my $begin_line = $line;
1667 my $begin_column = $column + 1;
1674 # $self->_parse_c_warning($_, $line, $column, "variable");
1677 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1678 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1679 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1680 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1681 \$_, \$line, \$column, \$match))
1683 if ($match =~ /^(?:extern|static)$/) {
1687 $self->_parse_c_warning($_, $line, $column, "repeated linkage (ignored): $match");
1689 } elsif ($match =~ /^(?:signed|unsigned)$/) {
1693 $self->_parse_c_warning($_, $line, $column, "repeated sign (ignored): $match");
1702 if (s/^(enum\s+|interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1705 $self->_update_c_position($&, \$line, \$column);
1707 if(defined($_name)) {
1708 $type = "$kind $_name { }";
1710 $type = "$kind { }";
1714 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s+DECLSPEC_ALIGN\(.*?\)|\s*(?:const\s*|volatile\s*)?\*)*)\s*(\w+)\s*(\[.*?\]$|:\s*(\d+)$|\{)?//s) {
1730 $type = $self->_format_c_type($type);
1733 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*)\s*:\s*(\d+)$//s) {
1734 $type = "$sign$1:$2";
1736 $type = $self->_format_c_type($type);
1739 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*\s*\(\s*(?:$CALL_CONVENTION)?(?:\s+DECLSPEC_[A-Z]+)?(?:\s*\*)*)\s*(\w+)\s*(\)\s*\(.*?\))$//s) {
1740 $type = $self->_format_c_type("$sign$1$3");
1744 } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
1748 $self->_parse_c_warning($_, $line, $column, "variable", "'$_'");
1752 if($self->_parse_c('SEQ_DEFINEBUF', \$_, \$line, \$column, \$match)) { # Linux specific
1755 } elsif($self->_parse_c('DEFINE_REGS_ENTRYPOINT_\w+|DPQ_DECL_\w+|HANDLER_DEF|IX86_ONLY', # Wine specific
1756 \$_, \$line, \$column, \$match))
1760 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
1763 } elsif(s/^(enum|interface|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*//s) {
1766 $self->_update_c_position($&, \$line, \$column);
1768 if(defined($_name)) {
1769 $type = "struct $_name { }";
1771 $type = "struct { }";
1773 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
1780 # $output->write("*** $type: '$_'\n");
1782 # $self->_parse_c_warning($_, $line, $column, "variable2", "");
1784 if(s/^WINAPI\s*//) {
1785 $self->_update_c_position($&, \$line, \$column);
1788 if(s/^(\((?:$CALL_CONVENTION)?\s*\*?\s*(?:$CALL_CONVENTION)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
1789 $self->_update_c_position($&, \$line, \$column);
1794 $self->_parse_c_until_one_of("\\)", \$_, \$line, \$column);
1795 if(s/^\)//) { $column++; }
1796 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1798 if(!s/^(?:=\s*|,\s*|$)//) {
1801 } elsif(s/^(?:\*\s*)*(?:const\s+|volatile\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
1802 $self->_update_c_position($&, \$line, \$column);
1814 # $output->write("$type: $name: '$_'\n");
1818 $$refcolumn = $column;
1820 $$reflinkage = $linkage;
1824 $self->{FOUND_VARIABLE}($begin_line, $begin_column, $linkage, $type, $name);