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
23 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK);
30 use options
qw($options);
31 use output qw($output);
36 # Defined a couple common regexp tidbits
37 my $CALL_CONVENTION="__cdecl|__stdcall|" .
38 "__RPC_API|__RPC_STUB|__RPC_USER|" .
39 "CALLBACK|CDECL|NTAPI|PASCAL|RPC_ENTRY|RPC_VAR_ENTRY|" .
40 "VFWAPI|VFWAPIV|WINAPI|WINAPIV|APIENTRY|";
43 sub parse_c_function($$$$$);
44 sub parse_c_function_call($$$$$$$$);
45 sub parse_c_preprocessor($$$$);
46 sub parse_c_statements($$$$);
47 sub parse_c_tuple($$$$$$$);
48 sub parse_c_type($$$$$);
49 sub parse_c_typedef($$$$);
50 sub parse_c_variable($$$$$$$);
53 ########################################################################
58 my $class = ref($proto) || $proto;
60 bless ($self, $class);
62 my $file = \${$self->{FILE}};
63 my $create_function = \${$self->{CREATE_FUNCTION}};
64 my $create_type = \${$self->{CREATE_TYPE}};
65 my $found_comment = \${$self->{FOUND_COMMENT}};
66 my $found_declaration = \${$self->{FOUND_DECLARATION}};
67 my $found_function = \${$self->{FOUND_FUNCTION}};
68 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
69 my $found_line = \${$self->{FOUND_LINE}};
70 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
71 my $found_statement = \${$self->{FOUND_STATEMENT}};
72 my $found_type = \${$self->{FOUND_TYPE}};
73 my $found_variable = \${$self->{FOUND_VARIABLE}};
77 $$create_function = sub { return new c_function; };
78 $$create_type = sub { return new c_type; };
79 $$found_comment = sub { return 1; };
80 $$found_declaration = sub { return 1; };
81 $$found_function = sub { return 1; };
82 $$found_function_call = sub { return 1; };
83 $$found_line = sub { return 1; };
84 $$found_preprocessor = sub { return 1; };
85 $$found_statement = sub { return 1; };
86 $$found_type = sub { return 1; };
87 $$found_variable = sub { return 1; };
92 ########################################################################
93 # set_found_comment_callback
95 sub set_found_comment_callback($$) {
98 my $found_comment = \${$self->{FOUND_COMMENT}};
100 $$found_comment = shift;
103 ########################################################################
104 # set_found_declaration_callback
106 sub set_found_declaration_callback($$) {
109 my $found_declaration = \${$self->{FOUND_DECLARATION}};
111 $$found_declaration = shift;
114 ########################################################################
115 # set_found_function_callback
117 sub set_found_function_callback($$) {
120 my $found_function = \${$self->{FOUND_FUNCTION}};
122 $$found_function = shift;
125 ########################################################################
126 # set_found_function_call_callback
128 sub set_found_function_call_callback($$) {
131 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
133 $$found_function_call = shift;
136 ########################################################################
137 # set_found_line_callback
139 sub set_found_line_callback($$) {
142 my $found_line = \${$self->{FOUND_LINE}};
144 $$found_line = shift;
147 ########################################################################
148 # set_found_preprocessor_callback
150 sub set_found_preprocessor_callback($$) {
153 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
155 $$found_preprocessor = shift;
158 ########################################################################
159 # set_found_statement_callback
161 sub set_found_statement_callback($$) {
164 my $found_statement = \${$self->{FOUND_STATEMENT}};
166 $$found_statement = shift;
169 ########################################################################
170 # set_found_type_callback
172 sub set_found_type_callback($$) {
175 my $found_type = \${$self->{FOUND_TYPE}};
177 $$found_type = shift;
180 ########################################################################
181 # set_found_variable_callback
183 sub set_found_variable_callback($$) {
186 my $found_variable = \${$self->{FOUND_VARIABLE}};
188 $$found_variable = shift;
192 ########################################################################
195 sub _format_c_type($$) {
201 if (/^(\w+(?:\s*\*)*)\s*\(\s*\*\s*\)\s*\(\s*(.*?)\s*\)$/s) {
202 my $return_type = $1;
203 my @arguments = split(/\s*,\s*/, $2);
204 foreach my $argument (@arguments) {
205 if ($argument =~ s/^(\w+(?:\s*\*)*)\s*\w+$/$1/) {
206 $argument =~ s/\s+/ /g;
207 $argument =~ s/\s*\*\s*/*/g;
208 $argument =~ s/(\*+)$/ $1/;
212 $_ = "$return_type (*)(" . join(", ", @arguments) . ")";
219 ########################################################################
222 # FIXME: Use caller (See man perlfunc)
224 sub _parse_c_warning($$$$$$) {
233 my $file = \${$self->{FILE}};
235 $message = "warning" if !$message;
239 my @lines = split(/\n/, $_);
241 $current .= $lines[0] . "\n" if $lines[0];
242 $current .= $lines[1] . "\n" if $lines[1];
246 (my $package, my $filename, my $line) = caller(0);
247 $output->write("*** caller ***: $filename:$line\n");
251 $output->write("$$file:$line." . ($column + 1) . ": $context: $message: \\\n$current");
253 $output->write("$$file:$line." . ($column + 1) . ": $context: $message\n");
257 ########################################################################
260 sub _parse_c_error($$$$$$) {
269 $message = "parse error" if !$message;
272 if($output->prefix) {
273 # $output->write("\n");
277 $self->_parse_c_warning($_, $line, $column, $context, $message);
282 ########################################################################
285 sub _update_c_position($$$$) {
290 my $refcolumn = shift;
292 my $line = $$refline;
293 my $column = $$refcolumn;
296 if(s/^[^\n\t\'\"]*//s) {
297 $column += length($&);
302 while(/^./ && !s/^\'//) {
304 $column += length($1);
308 $column += length($1);
311 $column += length($1);
319 while(/^./ && !s/^\"//) {
321 $column += length($1);
325 $column += length($1);
328 $column += length($1);
338 $column = $column + 8 - $column % 8;
343 $$refcolumn = $column;
346 ########################################################################
347 # __parse_c_until_one_of
349 sub __parse_c_until_one_of($$$$$$$) {
352 my $characters = shift;
353 my $on_same_level = shift;
354 my $refcurrent = shift;
356 my $refcolumn = shift;
359 local $_ = $$refcurrent;
360 my $line = $$refline;
361 my $column = $$refcolumn;
364 if(!defined($match)) {
366 $match = \$blackhole;
371 while(/^[^$characters]/s || $level > 0) {
375 if(s/^[^\(\)\[\]\{\}\n\t\'\"]*//s) {
378 } elsif ($on_same_level) {
379 if(s/^[^$characters\(\)\[\]\{\}\n\t\'\"]*//s) {
383 if(s/^[^$characters\n\t\'\"]*//s) {
390 while(/^./ && !s/^\'//) {
406 $$match .= $submatch;
407 $column += length($submatch);
410 while(/^./ && !s/^\"//) {
426 $$match .= $submatch;
427 $column += length($submatch);
428 } elsif($on_same_level && s/^[\(\[\{]//) {
432 $$match .= $submatch;
434 } elsif($on_same_level && s/^[\)\]\}]//) {
439 $$match .= $submatch;
443 $$match .= $submatch;
449 $$match .= $submatch;
455 $$match .= $submatch;
456 $column = $column + 8 - $column % 8;
458 $$match .= $submatch;
459 $column += length($submatch);
465 $$refcolumn = $column;
469 ########################################################################
470 # _parse_c_until_one_of
472 sub _parse_c_until_one_of($$$$$$) {
475 my $characters = shift;
476 my $refcurrent = shift;
478 my $refcolumn = shift;
481 return $self->__parse_c_until_one_of($characters, 0, $refcurrent, $refline, $refcolumn, $match);
484 ########################################################################
485 # _parse_c_on_same_level_until_one_of
487 sub _parse_c_on_same_level_until_one_of($$$$$$) {
490 my $characters = shift;
491 my $refcurrent = shift;
493 my $refcolumn = shift;
496 return $self->__parse_c_until_one_of($characters, 1, $refcurrent, $refline, $refcolumn, $match);
499 ########################################################################
502 sub parse_c_block($$$$$$$) {
505 my $refcurrent = shift;
507 my $refcolumn = shift;
509 my $refstatements = shift;
510 my $refstatements_line = shift;
511 my $refstatements_column = shift;
513 local $_ = $$refcurrent;
514 my $line = $$refline;
515 my $column = $$refcolumn;
517 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
527 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
529 my $statements_line = $line;
530 my $statements_column = $column;
535 $self->_parse_c_until_one_of("\\{\\}", \$_, \$line, \$column, \$match);
539 $statements .= $match;
555 $$refcolumn = $column;
556 $$refstatements = $statements;
557 $$refstatements_line = $statements_line;
558 $$refstatements_column = $statements_column;
563 ########################################################################
564 # parse_c_declaration
566 sub parse_c_declaration($$$$)
568 my ($self, $refcurrent, $refline, $refcolumn) = @_;
570 my $found_declaration = \${$self->{FOUND_DECLARATION}};
571 my $found_function = \${$self->{FOUND_FUNCTION}};
573 local $_ = $$refcurrent;
574 my $line = $$refline;
575 my $column = $$refcolumn;
577 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
579 my $begin_line = $line;
580 my $begin_column = $column + 1;
582 my $end_line = $begin_line;
583 my $end_column = $begin_column;
584 $self->_update_c_position($_, \$end_line, \$end_column);
586 if(!&$$found_declaration($begin_line, $begin_column, $end_line, $end_column, $_)) {
594 my ($linkage, $type, $name);
596 if(s/^WINE_(?:DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\(\s*(\w+)\s*\)\s*//s) { # FIXME: Wine specific kludge
597 $self->_update_c_position($&, \$line, \$column);
598 } elsif(s/^__ASM_GLOBAL_FUNC\(\s*(\w+)\s*,\s*//s) { # FIXME: Wine specific kludge
599 $self->_update_c_position($&, \$line, \$column);
600 $self->_parse_c_until_one_of("\)", \$_, \$line, \$column);
604 } elsif(s/^(?:DEFINE_AVIGUID|DEFINE_OLEGUID)\s*(?=\()//s) { # FIXME: Wine specific kludge
605 $self->_update_c_position($&, \$line, \$column);
609 my @argument_columns;
611 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
614 } elsif(s/^DEFINE_COMMON_NOTIFICATIONS\(\s*(\w+)\s*,\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
615 $self->_update_c_position($&, \$line, \$column);
616 } elsif(s/^MAKE_FUNCPTR\(\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
617 $self->_update_c_position($&, \$line, \$column);
618 } elsif(s/^START_TEST\(\s*(\w+)\s*\)\s*{//s) { # FIXME: Wine specific kludge
619 $self->_update_c_position($&, \$line, \$column);
620 } elsif(s/^int\s*_FUNCTION_\s*{//s) { # FIXME: Wine specific kludge
621 $self->_update_c_position($&, \$line, \$column);
622 } elsif(s/^(?:jump|strong)_alias//s) { # FIXME: GNU C library specific kludge
623 $self->_update_c_position($&, \$line, \$column);
624 } elsif(s/^(?:__asm__|asm)\s*\(//) {
625 $self->_update_c_position($&, \$line, \$column);
626 } elsif($self->parse_c_typedef(\$_, \$line, \$column)) {
628 } elsif($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type, \$name)) {
630 } elsif($self->parse_c_function(\$_, \$line, \$column, \$function)) {
631 if(&$$found_function($function))
633 my $statements = $function->statements;
634 my $statements_line = $function->statements_line;
635 my $statements_column = $function->statements_column;
637 if(defined($statements)) {
638 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
644 $self->_parse_c_error($_, $line, $column, "declaration");
649 $$refcolumn = $column;
654 ########################################################################
657 sub _parse_c($$$$$$) {
661 my $refcurrent = shift;
663 my $refcolumn = shift;
665 my $refmatch = shift;
667 local $_ = $$refcurrent;
668 my $line = $$refline;
669 my $column = $$refcolumn;
672 if(s/^(?:$pattern)//s) {
673 $self->_update_c_position($&, \$line, \$column);
679 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
683 $$refcolumn = $column;
690 ########################################################################
693 sub parse_c_enum($$$$) {
696 my $refcurrent = shift;
698 my $refcolumn = shift;
700 local $_ = $$refcurrent;
701 my $line = $$refline;
702 my $column = $$refcolumn;
704 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
706 if (!s/^enum\s+((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
709 my $_name = $1 || "";
711 $self->_update_c_position($&, \$line, \$column);
716 while ($self->_parse_c_on_same_level_until_one_of(',', \$_, \$line, \$column, \$match)) {
718 if ($match !~ /^(\w+)\s*(?:=\s*(.*?)\s*)?$/) {
719 $self->_parse_c_error($_, $line, $column, "enum");
722 my $enum_value = $2 || "";
724 # $output->write("enum:$_name:$enum_name:$enum_value\n");
727 if ($self->_parse_c(',', \$_, \$line, \$column)) {
729 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
732 my $tuple_line = $line;
733 my $tuple_column = $column - 1;
737 my @argument_columns;
739 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
740 \@arguments, \@argument_lines, \@argument_columns))
742 $self->_parse_c_error($_, $line, $column, "enum");
746 if ($#arguments >= 0) {
747 $name = $arguments[0];
752 $self->_parse_c_error($_, $line, $column, "enum");
756 $self->_update_c_position($_, \$line, \$column);
760 $$refcolumn = $column;
764 ########################################################################
767 sub parse_c_expression($$$$) {
770 my $refcurrent = shift;
772 my $refcolumn = shift;
774 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
776 local $_ = $$refcurrent;
777 my $line = $$refline;
778 my $column = $$refcolumn;
780 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
783 if(s/^(.*?)(\w+\s*\()/$2/s) {
784 $self->_update_c_position($1, \$line, \$column);
786 my $begin_line = $line;
787 my $begin_column = $column + 1;
792 my @argument_columns;
793 if(!$self->parse_c_function_call(\$_, \$line, \$column, \$name, \@arguments, \@argument_lines, \@argument_columns)) {
797 if(&$$found_function_call($begin_line, $begin_column, $line, $column, $name, \@arguments))
799 while(defined(my $argument = shift @arguments) &&
800 defined(my $argument_line = shift @argument_lines) &&
801 defined(my $argument_column = shift @argument_columns))
803 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
811 $self->_update_c_position($_, \$line, \$column);
815 $$refcolumn = $column;
820 ########################################################################
823 sub parse_c_file($$$$) {
826 my $found_comment = \${$self->{FOUND_COMMENT}};
827 my $found_line = \${$self->{FOUND_LINE}};
829 my $refcurrent = shift;
831 my $refcolumn = shift;
833 local $_ = $$refcurrent;
834 my $line = $$refline;
835 my $column = $$refcolumn;
837 my $declaration = "";
838 my $declaration_line = $line;
839 my $declaration_column = $column;
841 my $previous_line = 0;
842 my $previous_column = -1;
844 my $preprocessor_condition;
851 while($plevel > 0 || $blevel > 0) {
853 $self->_parse_c_until_one_of("#/\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
855 if($line != $previous_line) {
856 &$$found_line($line);
857 } elsif(0 && $column == $previous_column) {
858 $self->_parse_c_error($_, $line, $column, "file", "no progress");
860 # &$$found_line("$line.$column");
862 $previous_line = $line;
863 $previous_column = $column;
865 if($match !~ /^\s+$/s && $options->debug) {
866 $self->_parse_c_warning($_, $line, $column, "file", "$plevel $blevel: '$declaration' '$match'");
869 if(!$declaration && $match =~ s/^\s+//s) {
870 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
874 $declaration .= $match;
877 if ($declaration =~ s/^extern\s*\"C\"//s) {
879 $self->_update_c_position($&, \$line, \$column);
881 $declaration_line = $line;
882 $declaration_column = $column;
887 } elsif ($extern_c && $blevel == 1 && $plevel == 1 && !$declaration) {
889 $self->_update_c_position($&, \$line, \$column);
891 $declaration_line = $line;
892 $declaration_column = $column;
897 } elsif($declaration =~ s/^(?:__DEFINE_(?:GET|SET)_SEG|OUR_GUID_ENTRY)\s*(?=\()//sx) { # FIXME: Wine specific kludge
899 if ($plevel > 2 || !s/^\)//) {
900 $declaration = "$prefix$declaration";
903 $self->_update_c_position($&, \$line, \$column);
908 my @argument_columns;
910 if(!$self->parse_c_tuple(\$declaration, \$declaration_line, \$declaration_column,
911 \@arguments, \@argument_lines, \@argument_columns))
913 $self->_parse_c_error($declaration, $declaration_line, $declaration_column, "file", "tuple expected");
917 $declaration_line = $line;
918 $declaration_column = $column;
922 } elsif ($declaration =~ s/^(?:DEFINE_SHLGUID)\s*\(.*?\)//s) {
923 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
924 } elsif ($declaration =~ s/^(?:DECL_WINELIB_TYPE_AW|DECLARE_HANDLE(?:16)?|TYPE_MARSHAL)\(\s*(\w+)\s*\)\s*//s) {
925 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
926 } elsif ($declaration =~ s/^ICOM_DEFINE\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*//s) {
927 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
933 while(s/^.*?\n//) { $blank_lines++; }
936 $declaration_line = $line;
937 $declaration_column = $column;
939 $declaration .= "\n" x $blank_lines;
947 my $preprocessor_line = $line;
948 my $preprocessor_column = $column;
950 my $preprocessor = $&;
951 while(s/^(.*?)\\\s*\n//) {
953 $preprocessor .= "$1\n";
955 if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
958 $preprocessor .= "$1$3";
962 } elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
972 if($preprocessor =~ /^\#\s*if/) {
973 if($preprocessor =~ /^\#\s*if\s*0/) {
978 if($preprocessor =~ /^\#\s*ifdef\s+WORDS_BIGENDIAN$/) {
979 $preprocessor_condition = "defined(WORD_BIGENDIAN)";
980 # $output->write("'$preprocessor_condition':'$declaration'\n")
982 $preprocessor_condition = "";
985 } elsif($preprocessor =~ /^\#\s*else/) {
986 if ($preprocessor_condition ne "") {
987 $preprocessor_condition =~ "!$preprocessor_condition";
988 $preprocessor_condition =~ s/^!!/!/;
989 # $output->write("'$preprocessor_condition':'$declaration'\n")
991 } elsif($preprocessor =~ /^\#\s*endif/) {
999 if ($preprocessor_condition ne "") {
1000 # $output->write("'$preprocessor_condition':'$declaration'\n");
1001 $preprocessor_condition = "";
1006 if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
1011 if(s/^\/\*.*?\*\///s) {
1012 &$$found_comment($line, $column + 1, $&);
1018 $column += length($_);
1020 } elsif(s/^\/\/(.*?)\n//) {
1021 &$$found_comment($line, $column + 1, $&);
1030 $line += $blank_lines;
1031 if($blank_lines > 0) {
1036 $declaration_line = $line;
1037 $declaration_column = $column;
1038 } elsif($blank_lines > 0) {
1039 $declaration .= "\n" x $blank_lines;
1061 $self->_parse_c_error($_, $line, $column, "file", ") without (");
1064 if($plevel == 1 && $declaration =~ /^__ASM_GLOBAL_FUNC/) {
1065 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1068 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1070 $declaration_line = $line;
1071 $declaration_column = $column;
1079 $self->_parse_c_error($_, $line, $column, "file", "} without {");
1084 if($declaration =~ /^typedef/s ||
1085 $declaration =~ /^(?:const\s+|extern\s+|static\s+|volatile\s+)*(?:interface|struct|union)(?:\s+\w+)?\s*\{/s)
1088 } elsif($plevel == 1 && $blevel == 1) {
1089 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1092 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1094 $declaration_line = $line;
1095 $declaration_column = $column;
1096 } elsif($column == 1 && !$extern_c) {
1097 $self->_parse_c_error("", $line, $column, "file", "inner } ends on column 1");
1101 if(0 && $blevel == 1 &&
1102 $declaration !~ /^typedef/ &&
1103 $declaration !~ /^(?:const\s+|extern\s+|static\s+|volatile\s+)?(?:interface|struct|union)(?:\s+\w+)?\s*\{/s &&
1104 $declaration =~ /^(?:\w+(?:\s*\*)*\s+)*(\w+)\s*\(\s*(?:(?:\w+\s*,\s*)*(\w+))?\s*\)\s*(.*?);$/s &&
1105 $1 ne "ICOM_VTABLE" && defined($2) && $2 ne "void" && $3) # K&R
1107 $self->_parse_c_warning("", $line, $column, "file", "function $1: warning: function has K&R format");
1108 } elsif($plevel == 1 && $blevel == 1) {
1109 $declaration =~ s/\s*;$//;
1110 if($declaration && !$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1113 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1115 $declaration_line = $line;
1116 $declaration_column = $column;
1118 } elsif(/^\s*$/ && $declaration =~ /^\s*$/ && $match =~ /^\s*$/) {
1122 $self->_parse_c_error($_, $line, $column, "file", "parse error: '$declaration' '$match'");
1128 $$refcolumn = $column;
1133 ########################################################################
1136 sub parse_c_function($$$$$) {
1139 my $file = \${$self->{FILE}};
1140 my $create_function = \${$self->{CREATE_FUNCTION}};
1142 my $refcurrent = shift;
1143 my $refline = shift;
1144 my $refcolumn = shift;
1146 my $reffunction = shift;
1148 local $_ = $$refcurrent;
1149 my $line = $$refline;
1150 my $column = $$refcolumn;
1153 my $calling_convention = "";
1158 my @argument_columns;
1160 my $statements_line;
1161 my $statements_column;
1163 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1165 my $begin_line = $line;
1166 my $begin_column = $column + 1;
1168 if($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
1172 # $self->_parse_c_warning($_, $line, $column, "function", "");
1175 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1176 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1177 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1178 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1179 \$_, \$line, \$column, \$match))
1181 if($match =~ /^(?:extern|static)$/) {
1188 if($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1190 } elsif($self->_parse_c('WINE_EXCEPTION_FILTER\(\w+\)', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1193 if(!$self->parse_c_type(\$_, \$line, \$column, \$return_type)) {
1197 $self->_parse_c('inline|FAR', \$_, \$line, \$column);
1199 $self->_parse_c($CALL_CONVENTION,
1200 \$_, \$line, \$column, \$calling_convention);
1203 # FIXME: ???: Old variant of __attribute((const))
1204 $self->_parse_c('(?:const|volatile)', \$_, \$line, \$column);
1206 if(!$self->_parse_c('(?:operator\s*!=|(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)', \$_, \$line, \$column, \$name)) {
1212 $self->_update_c_position($&, \$line, \$column);
1216 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1222 $self->_update_c_position($&, \$line, \$column);
1224 $self->_parse_c_error($_, $line, $column, "function");
1230 if($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
1235 # FIXME: Implement proper handling of K&R C functions
1236 $self->_parse_c_until_one_of("{", \$_, \$line, \$column, $kar);
1239 $output->write("K&R: $kar\n");
1242 if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1246 my $end_line = $line;
1247 my $end_column = $column;
1251 $$refcolumn = $column;
1253 my $function = &$$create_function;
1255 $function->file($$file);
1256 $function->begin_line($begin_line);
1257 $function->begin_column($begin_column);
1258 $function->end_line($end_line);
1259 $function->end_column($end_column);
1260 $function->linkage($linkage);
1261 $function->return_type($return_type);
1262 $function->calling_convention($calling_convention);
1263 $function->name($name);
1264 # if(defined($argument_types)) {
1265 # $function->argument_types([@$argument_types]);
1267 # if(defined($argument_names)) {
1268 # $function->argument_names([@$argument_names]);
1270 $function->statements_line($statements_line);
1271 $function->statements_column($statements_column);
1272 $function->statements($statements);
1274 $$reffunction = $function;
1279 ########################################################################
1280 # parse_c_function_call
1282 sub parse_c_function_call($$$$$$$$) {
1285 my $refcurrent = shift;
1286 my $refline = shift;
1287 my $refcolumn = shift;
1289 my $refname = shift;
1290 my $refarguments = shift;
1291 my $refargument_lines = shift;
1292 my $refargument_columns = shift;
1294 local $_ = $$refcurrent;
1295 my $line = $$refline;
1296 my $column = $$refcolumn;
1301 my @argument_columns;
1303 if(s/^(\w+)(\s*)(?=\()//s) {
1304 $self->_update_c_position($&, \$line, \$column);
1308 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1317 $$refcolumn = $column;
1320 @$refarguments = @arguments;
1321 @$refargument_lines = @argument_lines;
1322 @$refargument_columns = @argument_columns;
1327 ########################################################################
1328 # parse_c_preprocessor
1330 sub parse_c_preprocessor($$$$) {
1333 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
1335 my $refcurrent = shift;
1336 my $refline = shift;
1337 my $refcolumn = shift;
1339 local $_ = $$refcurrent;
1340 my $line = $$refline;
1341 my $column = $$refcolumn;
1343 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1345 my $begin_line = $line;
1346 my $begin_column = $column + 1;
1348 if(!&$$found_preprocessor($begin_line, $begin_column, "$_")) {
1352 if(/^\#\s*define\s*(.*?)$/s) {
1353 $self->_update_c_position($_, \$line, \$column);
1354 } elsif(/^\#\s*else/s) {
1355 $self->_update_c_position($_, \$line, \$column);
1356 } elsif(/^\#\s*endif/s) {
1357 $self->_update_c_position($_, \$line, \$column);
1358 } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1359 $self->_update_c_position($_, \$line, \$column);
1360 } elsif(/^\#\s*include\s+(.*?)$/s) {
1361 $self->_update_c_position($_, \$line, \$column);
1362 } elsif(/^\#\s*undef\s+(.*?)$/s) {
1363 $self->_update_c_position($_, \$line, \$column);
1365 $self->_parse_c_error($_, $line, $column, "preprocessor");
1370 $$refcolumn = $column;
1375 ########################################################################
1378 sub parse_c_statement($$$$) {
1381 my $refcurrent = shift;
1382 my $refline = shift;
1383 my $refcolumn = shift;
1385 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1387 local $_ = $$refcurrent;
1388 my $line = $$refline;
1389 my $column = $$refcolumn;
1391 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1393 $self->_parse_c('(?:case\s+)?(\w+)\s*:\s*', \$_, \$line, \$column);
1395 # $output->write("$line.$column: statement: '$_'\n");
1401 my $statements_line;
1402 my $statements_column;
1403 if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1406 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1409 } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
1410 $self->_update_c_position($&, \$line, \$column);
1416 my @argument_columns;
1417 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1421 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1422 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1425 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1427 while(defined(my $argument = shift @arguments) &&
1428 defined(my $argument_line = shift @argument_lines) &&
1429 defined(my $argument_column = shift @argument_columns))
1431 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1433 } elsif(s/^else//) {
1434 $self->_update_c_position($&, \$line, \$column);
1435 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1438 } elsif(s/^return//) {
1439 $self->_update_c_position($&, \$line, \$column);
1440 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1441 if(!$self->parse_c_expression(\$_, \$line, \$column)) {
1444 } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1447 # $self->_parse_c_error($_, $line, $column, "statement");
1450 $self->_update_c_position($_, \$line, \$column);
1454 $$refcolumn = $column;
1459 ########################################################################
1460 # parse_c_statements
1462 sub parse_c_statements($$$$) {
1465 my $refcurrent = shift;
1466 my $refline = shift;
1467 my $refcolumn = shift;
1469 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1471 local $_ = $$refcurrent;
1472 my $line = $$refline;
1473 my $column = $$refcolumn;
1475 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1477 # $output->write("$line.$column: statements: '$_'\n");
1480 my $statement_line = $line;
1481 my $statement_column = $column;
1483 my $previous_line = -1;
1484 my $previous_column = -1;
1488 while($plevel > 0 || $blevel > 0) {
1490 $self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1492 if($previous_line == $line && $previous_column == $column) {
1493 $self->_parse_c_error($_, $line, $column, "statements", "no progress");
1495 $previous_line = $line;
1496 $previous_column = $column;
1498 # $output->write("'$match' '$_'\n");
1500 $statement .= $match;
1505 } elsif(s/^[\)\]]//) {
1508 $self->_parse_c_error($_, $line, $column, "statements");
1518 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1521 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1523 $statement_line = $line;
1524 $statement_column = $column;
1527 if($plevel == 1 && $blevel == 1) {
1528 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1532 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1534 $statement_line = $line;
1535 $statement_column = $column;
1539 } elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
1543 $self->_parse_c_error($_, $line, $column, "statements");
1547 $self->_update_c_position($_, \$line, \$column);
1551 $$refcolumn = $column;
1556 ########################################################################
1557 # parse_c_struct_union
1559 sub parse_c_struct_union($$$$$$$$$) {
1562 my $refcurrent = shift;
1563 my $refline = shift;
1564 my $refcolumn = shift;
1566 my $refkind = shift;
1567 my $ref_name = shift;
1568 my $reffield_type_names = shift;
1569 my $reffield_names = shift;
1570 my $refnames = shift;
1572 local $_ = $$refcurrent;
1573 my $line = $$refline;
1574 my $column = $$refcolumn;
1578 my @field_type_names = ();
1579 my @field_names = ();
1582 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1584 if (!s/^(interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1590 $self->_update_c_position($&, \$line, \$column);
1595 while ($_ && $self->_parse_c_on_same_level_until_one_of(';', \$_, \$line, \$column, \$match))
1598 my $field_type_name;
1601 if ($self->parse_c_variable(\$match, \$line, \$column, \$field_linkage, \$field_type_name, \$field_name)) {
1602 $field_type_name =~ s/\s+/ /g;
1604 push @field_type_names, $field_type_name;
1605 push @field_names, $field_name;
1606 # $output->write("$kind:$_name:$field_type_name:$field_name\n");
1608 $self->_parse_c_error($_, $line, $column, "typedef $kind: '$match'");
1611 if ($self->_parse_c(';', \$_, \$line, \$column)) {
1613 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1616 my $tuple_line = $line;
1617 my $tuple_column = $column - 1;
1621 my @argument_columns;
1623 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1624 \@arguments, \@argument_lines, \@argument_columns))
1626 $self->_parse_c_error($_, $line, $column, "$kind");
1629 foreach my $argument (@arguments) {
1630 my $name = $argument;
1637 $self->_parse_c_error($_, $line, $column, "$kind");
1643 $$refcolumn = $column;
1646 $$ref_name = $_name;
1647 @$reffield_type_names = @field_type_names;
1648 @$reffield_names = @field_names;
1649 @$refnames = @names;
1654 ########################################################################
1657 sub parse_c_tuple($$$$$$$) {
1660 my $refcurrent = shift;
1661 my $refline = shift;
1662 my $refcolumn = shift;
1664 # FIXME: Should not write directly
1666 my $item_lines = shift;
1667 my $item_columns = shift;
1669 local $_ = $$refcurrent;
1671 my $line = $$refline;
1672 my $column = $$refcolumn;
1682 my $item_line = $line;
1683 my $item_column = $column + 1;
1686 while($plevel > 0) {
1688 $self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1696 push @$item_lines, $item_line;
1697 push @$item_columns, $item_column;
1698 push @$items, $item;
1708 push @$item_lines, $item_line;
1709 push @$item_columns, $item_column;
1710 push @$items, $item;
1711 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1713 $item_column = $column + 1;
1725 $$refcolumn = $column;
1730 ########################################################################
1733 sub parse_c_type($$$$$) {
1736 my $refcurrent = shift;
1737 my $refline = shift;
1738 my $refcolumn = shift;
1740 my $reftype = shift;
1742 local $_ = $$refcurrent;
1743 my $line = $$refline;
1744 my $column = $$refcolumn;
1748 $self->_parse_c("(?:const|volatile)", \$_, \$line, \$column);
1750 if($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1752 } elsif($self->_parse_c('(?:enum\s+|interface\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1753 \$_, \$line, \$column, \$type))
1763 $$refcolumn = $column;
1770 ########################################################################
1773 sub parse_c_typedef($$$$) {
1776 my $create_type = \${$self->{CREATE_TYPE}};
1777 my $found_type = \${$self->{FOUND_TYPE}};
1778 my $preprocessor_condition = \${$self->{PREPROCESSOR_CONDITION}};
1780 my $refcurrent = shift;
1781 my $refline = shift;
1782 my $refcolumn = shift;
1784 local $_ = $$refcurrent;
1785 my $line = $$refline;
1786 my $column = $$refcolumn;
1790 if (!$self->_parse_c("typedef", \$_, \$line, \$column)) {
1798 } elsif ($self->parse_c_enum(\$_, \$line, \$column)) {
1804 my @field_type_names;
1809 } elsif ($self->parse_c_struct_union(\$_, \$line, \$column,
1810 \$kind, \$_name, \@field_type_names, \@field_names, \@names))
1813 foreach my $name (@names)
1815 if ($name =~ /^\w+$/)
1821 $base_name="$kind $_name" if (!defined $base_name and defined $_name);
1822 $base_name=$kind if (!defined $base_name);
1823 foreach my $name (@names) {
1824 if ($name =~ /^\w+$/) {
1825 my $type = &$$create_type();
1828 $type->_name($_name);
1830 $type->field_type_names([@field_type_names]);
1831 $type->field_names([@field_names]);
1833 &$$found_type($type);
1834 } elsif ($name =~ /^(\*+)\s*(?:RESTRICTED_POINTER\s+)?(\w+)$/) {
1835 my $type_name = "$base_name $1";
1838 my $type = &$$create_type();
1842 $type->field_type_names([$type_name]);
1843 $type->field_names([""]);
1845 &$$found_type($type);
1847 $self->_parse_c_error($_, $line, $column, "typedef 2");
1859 } elsif ($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type_name, \$name)) {
1860 $type_name =~ s/\s+/ /g;
1862 if(defined($type_name) && defined($name)) {
1863 my $type = &$$create_type();
1865 if (length($name) == 0) {
1866 $self->_parse_c_error($_, $line, $column, "typedef");
1871 $type->field_type_names([$type_name]);
1872 $type->field_names([""]);
1874 &$$found_type($type);
1877 $self->_parse_c_error($_, $line, $column, "typedef");
1882 $$refcolumn = $column;
1887 ########################################################################
1890 sub parse_c_variable($$$$$$$) {
1893 my $found_variable = \${$self->{FOUND_VARIABLE}};
1895 my $refcurrent = shift;
1896 my $refline = shift;
1897 my $refcolumn = shift;
1899 my $reflinkage = shift;
1900 my $reftype = shift;
1901 my $refname = shift;
1903 local $_ = $$refcurrent;
1904 my $line = $$refline;
1905 my $column = $$refcolumn;
1907 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1909 my $begin_line = $line;
1910 my $begin_column = $column + 1;
1917 # $self->_parse_c_warning($_, $line, $column, "variable");
1920 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1921 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1922 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1923 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1924 \$_, \$line, \$column, \$match))
1926 if ($match =~ /^(?:extern|static)$/) {
1930 $self->_parse_c_warning($_, $line, $column, "repeated linkage (ignored): $match");
1932 } elsif ($match =~ /^(?:signed|unsigned)$/) {
1936 $self->_parse_c_warning($_, $line, $column, "repeated sign (ignored): $match");
1947 } elsif (s/^(enum\s+|interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1950 $self->_update_c_position($&, \$line, \$column);
1952 if(defined($_name)) {
1953 $type = "$kind $_name { }";
1955 $type = "$kind { }";
1959 } 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) {
1975 $type = $self->_format_c_type($type);
1978 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*)\s*:\s*(\d+)$//s) {
1979 $type = "$sign$1:$2";
1981 $type = $self->_format_c_type($type);
1984 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*\s*\(\s*(?:$CALL_CONVENTION)?(?:\s*\*)*)\s*(\w+)\s*(\)\s*\(.*?\))$//s) {
1985 $type = $self->_format_c_type("$sign$1$3");
1989 } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
1993 $self->_parse_c_warning($_, $line, $column, "variable", "'$_'");
1999 } elsif($self->_parse_c('SEQ_DEFINEBUF', \$_, \$line, \$column, \$match)) { # Linux specific
2002 } elsif($self->_parse_c('DEFINE_REGS_ENTRYPOINT_\w+|DPQ_DECL_\w+|HANDLER_DEF|IX86_ONLY', # Wine specific
2003 \$_, \$line, \$column, \$match))
2007 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
2010 } elsif(s/^(enum|interface|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*//s) {
2013 $self->_update_c_position($&, \$line, \$column);
2015 if(defined($_name)) {
2016 $type = "struct $_name { }";
2018 $type = "struct { }";
2020 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
2027 # $output->write("*** $type: '$_'\n");
2029 # $self->_parse_c_warning($_, $line, $column, "variable2", "");
2033 } elsif(s/^WINAPI\s*//) {
2034 $self->_update_c_position($&, \$line, \$column);
2039 } elsif(s/^(\((?:$CALL_CONVENTION)?\s*\*?\s*(?:$CALL_CONVENTION)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
2040 $self->_update_c_position($&, \$line, \$column);
2045 $self->_parse_c_until_one_of("\\)", \$_, \$line, \$column);
2046 if(s/^\)//) { $column++; }
2047 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
2049 if(!s/^(?:=\s*|,\s*|$)//) {
2052 } elsif(s/^(?:\*\s*)*(?:const\s+|volatile\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
2053 $self->_update_c_position($&, \$line, \$column);
2063 # $output->write("$type: $name: '$_'\n");
2067 $$refcolumn = $column;
2069 $$reflinkage = $linkage;
2073 if(&$$found_variable($begin_line, $begin_column, $linkage, $type, $name))