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
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|" .
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($$$$$$$);
54 ########################################################################
59 my $class = ref($proto) || $proto;
61 bless ($self, $class);
63 my $file = \${$self->{FILE}};
64 my $create_function = \${$self->{CREATE_FUNCTION}};
65 my $create_type = \${$self->{CREATE_TYPE}};
66 my $found_comment = \${$self->{FOUND_COMMENT}};
67 my $found_declaration = \${$self->{FOUND_DECLARATION}};
68 my $found_function = \${$self->{FOUND_FUNCTION}};
69 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
70 my $found_line = \${$self->{FOUND_LINE}};
71 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
72 my $found_statement = \${$self->{FOUND_STATEMENT}};
73 my $found_type = \${$self->{FOUND_TYPE}};
74 my $found_variable = \${$self->{FOUND_VARIABLE}};
78 $$create_function = sub { return new c_function; };
79 $$create_type = sub { return new c_type; };
80 $$found_comment = sub { return 1; };
81 $$found_declaration = sub { return 1; };
82 $$found_function = sub { return 1; };
83 $$found_function_call = sub { return 1; };
84 $$found_line = sub { return 1; };
85 $$found_preprocessor = sub { return 1; };
86 $$found_statement = sub { return 1; };
87 $$found_type = sub { return 1; };
88 $$found_variable = sub { return 1; };
93 ########################################################################
94 # set_found_comment_callback
96 sub set_found_comment_callback($$) {
99 my $found_comment = \${$self->{FOUND_COMMENT}};
101 $$found_comment = shift;
104 ########################################################################
105 # set_found_declaration_callback
107 sub set_found_declaration_callback($$) {
110 my $found_declaration = \${$self->{FOUND_DECLARATION}};
112 $$found_declaration = shift;
115 ########################################################################
116 # set_found_function_callback
118 sub set_found_function_callback($$) {
121 my $found_function = \${$self->{FOUND_FUNCTION}};
123 $$found_function = shift;
126 ########################################################################
127 # set_found_function_call_callback
129 sub set_found_function_call_callback($$) {
132 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
134 $$found_function_call = shift;
137 ########################################################################
138 # set_found_line_callback
140 sub set_found_line_callback($$) {
143 my $found_line = \${$self->{FOUND_LINE}};
145 $$found_line = shift;
148 ########################################################################
149 # set_found_preprocessor_callback
151 sub set_found_preprocessor_callback($$) {
154 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
156 $$found_preprocessor = shift;
159 ########################################################################
160 # set_found_statement_callback
162 sub set_found_statement_callback($$) {
165 my $found_statement = \${$self->{FOUND_STATEMENT}};
167 $$found_statement = shift;
170 ########################################################################
171 # set_found_type_callback
173 sub set_found_type_callback($$) {
176 my $found_type = \${$self->{FOUND_TYPE}};
178 $$found_type = shift;
181 ########################################################################
182 # set_found_variable_callback
184 sub set_found_variable_callback($$) {
187 my $found_variable = \${$self->{FOUND_VARIABLE}};
189 $$found_variable = shift;
193 ########################################################################
196 sub _format_c_type($$) {
202 if (/^(\w+(?:\s*\*)*)\s*\(\s*\*\s*\)\s*\(\s*(.*?)\s*\)$/s) {
203 my $return_type = $1;
204 my @arguments = split(/\s*,\s*/, $2);
205 foreach my $argument (@arguments) {
206 if ($argument =~ s/^(\w+(?:\s*\*)*)\s*\w+$/$1/) {
207 $argument =~ s/\s+/ /g;
208 $argument =~ s/\s*\*\s*/*/g;
209 $argument =~ s/(\*+)$/ $1/;
213 $_ = "$return_type (*)(" . join(", ", @arguments) . ")";
220 ########################################################################
223 # FIXME: Use caller (See man perlfunc)
225 sub _parse_c_warning($$$$$$) {
234 my $file = \${$self->{FILE}};
236 $message = "warning" if !$message;
240 my @lines = split(/\n/, $_);
242 $current .= $lines[0] . "\n" if $lines[0];
243 $current .= $lines[1] . "\n" if $lines[1];
247 (my $package, my $filename, my $line) = caller(0);
248 $output->write("*** caller ***: $filename:$line\n");
252 $output->write("$$file:$line." . ($column + 1) . ": $context: $message: \\\n$current");
254 $output->write("$$file:$line." . ($column + 1) . ": $context: $message\n");
258 ########################################################################
261 sub _parse_c_error($$$$$$) {
270 $message = "parse error" if !$message;
273 if($output->prefix) {
274 # $output->write("\n");
278 $self->_parse_c_warning($_, $line, $column, $context, $message);
283 ########################################################################
286 sub _update_c_position($$$$) {
291 my $refcolumn = shift;
293 my $line = $$refline;
294 my $column = $$refcolumn;
297 if(s/^[^\n\t\'\"]*//s) {
298 $column += length($&);
303 while(/^./ && !s/^\'//) {
305 $column += length($1);
309 $column += length($1);
312 $column += length($1);
320 while(/^./ && !s/^\"//) {
322 $column += length($1);
326 $column += length($1);
329 $column += length($1);
339 $column = $column + 8 - $column % 8;
344 $$refcolumn = $column;
347 ########################################################################
348 # __parse_c_until_one_of
350 sub __parse_c_until_one_of($$$$$$$) {
353 my $characters = shift;
354 my $on_same_level = shift;
355 my $refcurrent = shift;
357 my $refcolumn = shift;
360 local $_ = $$refcurrent;
361 my $line = $$refline;
362 my $column = $$refcolumn;
365 if(!defined($match)) {
367 $match = \$blackhole;
372 while(/^[^$characters]/s || $level > 0) {
376 if(s/^[^\(\)\[\]\{\}\n\t\'\"]*//s) {
379 } elsif ($on_same_level) {
380 if(s/^[^$characters\(\)\[\]\{\}\n\t\'\"]*//s) {
384 if(s/^[^$characters\n\t\'\"]*//s) {
391 while(/^./ && !s/^\'//) {
407 $$match .= $submatch;
408 $column += length($submatch);
411 while(/^./ && !s/^\"//) {
427 $$match .= $submatch;
428 $column += length($submatch);
429 } elsif($on_same_level && s/^[\(\[\{]//) {
433 $$match .= $submatch;
435 } elsif($on_same_level && s/^[\)\]\}]//) {
440 $$match .= $submatch;
444 $$match .= $submatch;
450 $$match .= $submatch;
456 $$match .= $submatch;
457 $column = $column + 8 - $column % 8;
459 $$match .= $submatch;
460 $column += length($submatch);
466 $$refcolumn = $column;
470 ########################################################################
471 # _parse_c_until_one_of
473 sub _parse_c_until_one_of($$$$$$) {
476 my $characters = shift;
477 my $refcurrent = shift;
479 my $refcolumn = shift;
482 return $self->__parse_c_until_one_of($characters, 0, $refcurrent, $refline, $refcolumn, $match);
485 ########################################################################
486 # _parse_c_on_same_level_until_one_of
488 sub _parse_c_on_same_level_until_one_of($$$$$$) {
491 my $characters = shift;
492 my $refcurrent = shift;
494 my $refcolumn = shift;
497 return $self->__parse_c_until_one_of($characters, 1, $refcurrent, $refline, $refcolumn, $match);
500 ########################################################################
503 sub parse_c_block($$$$$$$) {
506 my $refcurrent = shift;
508 my $refcolumn = shift;
510 my $refstatements = shift;
511 my $refstatements_line = shift;
512 my $refstatements_column = shift;
514 local $_ = $$refcurrent;
515 my $line = $$refline;
516 my $column = $$refcolumn;
518 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
528 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
530 my $statements_line = $line;
531 my $statements_column = $column;
536 $self->_parse_c_until_one_of("\\{\\}", \$_, \$line, \$column, \$match);
540 $statements .= $match;
556 $$refcolumn = $column;
557 $$refstatements = $statements;
558 $$refstatements_line = $statements_line;
559 $$refstatements_column = $statements_column;
564 ########################################################################
565 # parse_c_declaration
567 sub parse_c_declaration($$$$$$$$$$$$) {
570 my $found_declaration = \${$self->{FOUND_DECLARATION}};
571 my $found_function = \${$self->{FOUND_FUNCTION}};
573 my $refcurrent = shift;
575 my $refcolumn = shift;
577 local $_ = $$refcurrent;
578 my $line = $$refline;
579 my $column = $$refcolumn;
581 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
583 my $begin_line = $line;
584 my $begin_column = $column + 1;
586 my $end_line = $begin_line;
587 my $end_column = $begin_column;
588 $self->_update_c_position($_, \$end_line, \$end_column);
590 if(!&$$found_declaration($begin_line, $begin_column, $end_line, $end_column, $_)) {
595 my $function = shift;
598 my $calling_convention = shift;
599 my $return_type = shift;
601 my @arguments = shift;
602 my @argument_lines = shift;
603 my @argument_columns = shift;
610 } elsif(s/^WINE_(?:DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\(\s*(\w+)\s*\)\s*//s) { # FIXME: Wine specific kludge
611 $self->_update_c_position($&, \$line, \$column);
612 } elsif(s/^__ASM_GLOBAL_FUNC\(\s*(\w+)\s*,\s*//s) { # FIXME: Wine specific kludge
613 $self->_update_c_position($&, \$line, \$column);
614 $self->_parse_c_until_one_of("\)", \$_, \$line, \$column);
618 } elsif(s/^(?:DEFINE_AVIGUID|DEFINE_OLEGUID)\s*(?=\()//s) { # FIXME: Wine specific kludge
619 $self->_update_c_position($&, \$line, \$column);
623 my @argument_columns;
625 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
628 } elsif(s/^DEFINE_COMMON_NOTIFICATIONS\(\s*(\w+)\s*,\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
629 $self->_update_c_position($&, \$line, \$column);
630 } elsif(s/^MAKE_FUNCPTR\(\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
631 $self->_update_c_position($&, \$line, \$column);
632 } elsif(s/^START_TEST\(\s*(\w+)\s*\)\s*{//s) { # FIXME: Wine specific kludge
633 $self->_update_c_position($&, \$line, \$column);
634 } elsif(s/^int\s*_FUNCTION_\s*{//s) { # FIXME: Wine specific kludge
635 $self->_update_c_position($&, \$line, \$column);
636 } elsif(s/^(?:jump|strong)_alias//s) { # FIXME: GNU C library specific kludge
637 $self->_update_c_position($&, \$line, \$column);
638 } elsif(s/^(?:__asm__|asm)\s*\(//) {
639 $self->_update_c_position($&, \$line, \$column);
640 } elsif($self->parse_c_typedef(\$_, \$line, \$column)) {
642 } elsif($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type, \$name)) {
644 } elsif($self->parse_c_function(\$_, \$line, \$column, \$function)) {
645 if(&$$found_function($function))
647 my $statements = $function->statements;
648 my $statements_line = $function->statements_line;
649 my $statements_column = $function->statements_column;
651 if(defined($statements)) {
652 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
658 $self->_parse_c_error($_, $line, $column, "declaration");
663 $$refcolumn = $column;
668 ########################################################################
669 # parse_c_declarations
671 sub parse_c_declarations($$$$) {
674 my $refcurrent = shift;
676 my $refcolumn = shift;
681 ########################################################################
684 sub _parse_c($$$$$$) {
688 my $refcurrent = shift;
690 my $refcolumn = shift;
692 my $refmatch = shift;
694 local $_ = $$refcurrent;
695 my $line = $$refline;
696 my $column = $$refcolumn;
699 if(s/^(?:$pattern)//s) {
700 $self->_update_c_position($&, \$line, \$column);
706 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
710 $$refcolumn = $column;
717 ########################################################################
720 sub parse_c_enum($$$$) {
723 my $refcurrent = shift;
725 my $refcolumn = shift;
727 local $_ = $$refcurrent;
728 my $line = $$refline;
729 my $column = $$refcolumn;
731 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
733 if (!s/^enum\s+((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
736 my $_name = $1 || "";
738 $self->_update_c_position($&, \$line, \$column);
743 while ($self->_parse_c_on_same_level_until_one_of(',', \$_, \$line, \$column, \$match)) {
745 if ($match !~ /^(\w+)\s*(?:=\s*(.*?)\s*)?$/) {
746 $self->_parse_c_error($_, $line, $column, "enum");
749 my $enum_value = $2 || "";
751 # $output->write("enum:$_name:$enum_name:$enum_value\n");
754 if ($self->_parse_c(',', \$_, \$line, \$column)) {
756 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
759 my $tuple_line = $line;
760 my $tuple_column = $column - 1;
764 my @argument_columns;
766 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
767 \@arguments, \@argument_lines, \@argument_columns))
769 $self->_parse_c_error($_, $line, $column, "enum");
773 if ($#arguments >= 0) {
774 $name = $arguments[0];
779 $self->_parse_c_error($_, $line, $column, "enum");
783 $self->_update_c_position($_, \$line, \$column);
787 $$refcolumn = $column;
791 ########################################################################
794 sub parse_c_expression($$$$) {
797 my $refcurrent = shift;
799 my $refcolumn = shift;
801 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
803 local $_ = $$refcurrent;
804 my $line = $$refline;
805 my $column = $$refcolumn;
807 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
810 if(s/^(.*?)(\w+\s*\()/$2/s) {
811 $self->_update_c_position($1, \$line, \$column);
813 my $begin_line = $line;
814 my $begin_column = $column + 1;
819 my @argument_columns;
820 if(!$self->parse_c_function_call(\$_, \$line, \$column, \$name, \@arguments, \@argument_lines, \@argument_columns)) {
824 if(&$$found_function_call($begin_line, $begin_column, $line, $column, $name, \@arguments))
826 while(defined(my $argument = shift @arguments) &&
827 defined(my $argument_line = shift @argument_lines) &&
828 defined(my $argument_column = shift @argument_columns))
830 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
838 $self->_update_c_position($_, \$line, \$column);
842 $$refcolumn = $column;
847 ########################################################################
850 sub parse_c_file($$$$) {
853 my $found_comment = \${$self->{FOUND_COMMENT}};
854 my $found_line = \${$self->{FOUND_LINE}};
856 my $refcurrent = shift;
858 my $refcolumn = shift;
860 local $_ = $$refcurrent;
861 my $line = $$refline;
862 my $column = $$refcolumn;
864 my $declaration = "";
865 my $declaration_line = $line;
866 my $declaration_column = $column;
868 my $previous_line = 0;
869 my $previous_column = -1;
871 my $preprocessor_condition;
878 while($plevel > 0 || $blevel > 0) {
880 $self->_parse_c_until_one_of("#/\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
882 if($line != $previous_line) {
883 &$$found_line($line);
884 } elsif(0 && $column == $previous_column) {
885 $self->_parse_c_error($_, $line, $column, "file", "no progress");
887 # &$$found_line("$line.$column");
889 $previous_line = $line;
890 $previous_column = $column;
892 if($match !~ /^\s+$/s && $options->debug) {
893 $self->_parse_c_warning($_, $line, $column, "file", "$plevel $blevel: '$declaration' '$match'");
896 if(!$declaration && $match =~ s/^\s+//s) {
897 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
901 $declaration .= $match;
904 if ($declaration =~ s/^extern\s*\"C\"//s) {
906 $self->_update_c_position($&, \$line, \$column);
908 $declaration_line = $line;
909 $declaration_column = $column;
914 } elsif ($extern_c && $blevel == 1 && $plevel == 1 && !$declaration) {
916 $self->_update_c_position($&, \$line, \$column);
918 $declaration_line = $line;
919 $declaration_column = $column;
924 } elsif($declaration =~ s/^(?:__DEFINE_(?:GET|SET)_SEG|OUR_GUID_ENTRY)\s*(?=\()//sx) { # FIXME: Wine specific kludge
926 if ($plevel > 2 || !s/^\)//) {
927 $declaration = "$prefix$declaration";
930 $self->_update_c_position($&, \$line, \$column);
935 my @argument_columns;
937 if(!$self->parse_c_tuple(\$declaration, \$declaration_line, \$declaration_column,
938 \@arguments, \@argument_lines, \@argument_columns))
940 $self->_parse_c_error($declaration, $declaration_line, $declaration_column, "file", "tuple expected");
944 $declaration_line = $line;
945 $declaration_column = $column;
949 } elsif ($declaration =~ s/^(?:DEFINE_SHLGUID)\s*\(.*?\)//s) {
950 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
951 } elsif ($declaration =~ s/^(?:DECL_WINELIB_TYPE_AW|DECLARE_HANDLE(?:16)?|TYPE_MARSHAL)\(\s*(\w+)\s*\)\s*//s) {
952 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
953 } elsif ($declaration =~ s/^ICOM_DEFINE\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*//s) {
954 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
960 while(s/^.*?\n//) { $blank_lines++; }
963 $declaration_line = $line;
964 $declaration_column = $column;
966 $declaration .= "\n" x $blank_lines;
974 my $preprocessor_line = $line;
975 my $preprocessor_column = $column;
977 my $preprocessor = $&;
978 while(s/^(.*?)\\\s*\n//) {
980 $preprocessor .= "$1\n";
982 if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
985 $preprocessor .= "$1$3";
989 } elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
1001 } elsif($preprocessor =~ /^\#\s*if/) {
1002 if($preprocessor =~ /^\#\s*if\s*0/) {
1007 if($preprocessor =~ /^\#\s*ifdef\s+WORDS_BIGENDIAN$/) {
1008 $preprocessor_condition = "defined(WORD_BIGENDIAN)";
1009 # $output->write("'$preprocessor_condition':'$declaration'\n")
1011 $preprocessor_condition = "";
1014 } elsif($preprocessor =~ /^\#\s*else/) {
1015 if ($preprocessor_condition ne "") {
1016 $preprocessor_condition =~ "!$preprocessor_condition";
1017 $preprocessor_condition =~ s/^!!/!/;
1018 # $output->write("'$preprocessor_condition':'$declaration'\n")
1020 } elsif($preprocessor =~ /^\#\s*endif/) {
1028 if ($preprocessor_condition ne "") {
1029 # $output->write("'$preprocessor_condition':'$declaration'\n");
1030 $preprocessor_condition = "";
1035 if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
1040 if(s/^\/\*.*?\*\///s) {
1041 &$$found_comment($line, $column + 1, $&);
1047 $column += length($_);
1049 } elsif(s/^\/\/(.*?)\n//) {
1050 &$$found_comment($line, $column + 1, $&);
1059 $line += $blank_lines;
1060 if($blank_lines > 0) {
1065 $declaration_line = $line;
1066 $declaration_column = $column;
1067 } elsif($blank_lines > 0) {
1068 $declaration .= "\n" x $blank_lines;
1090 $self->_parse_c_error($_, $line, $column, "file", ") without (");
1093 if($plevel == 1 && $declaration =~ /^__ASM_GLOBAL_FUNC/) {
1094 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1097 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1099 $declaration_line = $line;
1100 $declaration_column = $column;
1108 $self->_parse_c_error($_, $line, $column, "file", "} without {");
1113 if($declaration =~ /^typedef/s ||
1114 $declaration =~ /^(?:const\s+|extern\s+|static\s+)*(?:struct|union)(?:\s+\w+)?\s*\{/s)
1117 } elsif($plevel == 1 && $blevel == 1) {
1118 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1121 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1123 $declaration_line = $line;
1124 $declaration_column = $column;
1125 } elsif($column == 1 && !$extern_c) {
1126 $self->_parse_c_error("", $line, $column, "file", "inner } ends on column 1");
1130 if(0 && $blevel == 1 &&
1131 $declaration !~ /^typedef/ &&
1132 $declaration !~ /^(?:const\s+|extern\s+|static\s+)?(?:struct|union)(?:\s+\w+)?\s*\{/s &&
1133 $declaration =~ /^(?:\w+(?:\s*\*)*\s+)*(\w+)\s*\(\s*(?:(?:\w+\s*,\s*)*(\w+))?\s*\)\s*(.*?);$/s &&
1134 $1 ne "ICOM_VTABLE" && defined($2) && $2 ne "void" && $3) # K&R
1136 $self->_parse_c_warning("", $line, $column, "file", "function $1: warning: function has K&R format");
1137 } elsif($plevel == 1 && $blevel == 1) {
1138 $declaration =~ s/\s*;$//;
1139 if($declaration && !$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1142 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1144 $declaration_line = $line;
1145 $declaration_column = $column;
1147 } elsif(/^\s*$/ && $declaration =~ /^\s*$/ && $match =~ /^\s*$/) {
1151 $self->_parse_c_error($_, $line, $column, "file", "parse error: '$declaration' '$match'");
1157 $$refcolumn = $column;
1162 ########################################################################
1165 sub parse_c_function($$$$$) {
1168 my $file = \${$self->{FILE}};
1169 my $create_function = \${$self->{CREATE_FUNCTION}};
1171 my $refcurrent = shift;
1172 my $refline = shift;
1173 my $refcolumn = shift;
1175 my $reffunction = shift;
1177 local $_ = $$refcurrent;
1178 my $line = $$refline;
1179 my $column = $$refcolumn;
1182 my $calling_convention = "";
1187 my @argument_columns;
1189 my $statements_line;
1190 my $statements_column;
1192 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1194 my $begin_line = $line;
1195 my $begin_column = $column + 1;
1199 } elsif($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
1203 # $self->_parse_c_warning($_, $line, $column, "function", "");
1206 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1207 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1208 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1209 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1210 \$_, \$line, \$column, \$match))
1212 if($match =~ /^(?:extern|static)$/) {
1221 } elsif($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1223 } elsif($self->_parse_c('WINE_EXCEPTION_FILTER\(\w+\)', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1226 if(!$self->parse_c_type(\$_, \$line, \$column, \$return_type)) {
1230 $self->_parse_c('inline|FAR', \$_, \$line, \$column);
1232 $self->_parse_c($CALL_CONVENTION,
1233 \$_, \$line, \$column, \$calling_convention);
1236 # FIXME: ???: Old variant of __attribute((const))
1237 $self->_parse_c('const', \$_, \$line, \$column);
1239 if(!$self->_parse_c('(?:operator\s*!=|(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)', \$_, \$line, \$column, \$name)) {
1245 $self->_update_c_position($&, \$line, \$column);
1249 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1255 $self->_update_c_position($&, \$line, \$column);
1257 $self->_parse_c_error($_, $line, $column, "function");
1265 } elsif($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
1270 # FIXME: Implement proper handling of K&R C functions
1271 $self->_parse_c_until_one_of("{", \$_, \$line, \$column, $kar);
1274 $output->write("K&R: $kar\n");
1277 if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1281 my $end_line = $line;
1282 my $end_column = $column;
1286 $$refcolumn = $column;
1288 my $function = &$$create_function;
1290 $function->file($$file);
1291 $function->begin_line($begin_line);
1292 $function->begin_column($begin_column);
1293 $function->end_line($end_line);
1294 $function->end_column($end_column);
1295 $function->linkage($linkage);
1296 $function->return_type($return_type);
1297 $function->calling_convention($calling_convention);
1298 $function->name($name);
1299 # if(defined($argument_types)) {
1300 # $function->argument_types([@$argument_types]);
1302 # if(defined($argument_names)) {
1303 # $function->argument_names([@$argument_names]);
1305 $function->statements_line($statements_line);
1306 $function->statements_column($statements_column);
1307 $function->statements($statements);
1309 $$reffunction = $function;
1314 ########################################################################
1315 # parse_c_function_call
1317 sub parse_c_function_call($$$$$$$$) {
1320 my $refcurrent = shift;
1321 my $refline = shift;
1322 my $refcolumn = shift;
1324 my $refname = shift;
1325 my $refarguments = shift;
1326 my $refargument_lines = shift;
1327 my $refargument_columns = shift;
1329 local $_ = $$refcurrent;
1330 my $line = $$refline;
1331 my $column = $$refcolumn;
1336 my @argument_columns;
1338 if(s/^(\w+)(\s*)(?=\()//s) {
1339 $self->_update_c_position($&, \$line, \$column);
1343 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1352 $$refcolumn = $column;
1355 @$refarguments = @arguments;
1356 @$refargument_lines = @argument_lines;
1357 @$refargument_columns = @argument_columns;
1362 ########################################################################
1363 # parse_c_preprocessor
1365 sub parse_c_preprocessor($$$$) {
1368 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
1370 my $refcurrent = shift;
1371 my $refline = shift;
1372 my $refcolumn = shift;
1374 local $_ = $$refcurrent;
1375 my $line = $$refline;
1376 my $column = $$refcolumn;
1378 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1380 my $begin_line = $line;
1381 my $begin_column = $column + 1;
1383 if(!&$$found_preprocessor($begin_line, $begin_column, "$_")) {
1389 } elsif(/^\#\s*define\s*(.*?)$/s) {
1390 $self->_update_c_position($_, \$line, \$column);
1391 } elsif(/^\#\s*else/s) {
1392 $self->_update_c_position($_, \$line, \$column);
1393 } elsif(/^\#\s*endif/s) {
1394 $self->_update_c_position($_, \$line, \$column);
1395 } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1396 $self->_update_c_position($_, \$line, \$column);
1397 } elsif(/^\#\s*include\s+(.*?)$/s) {
1398 $self->_update_c_position($_, \$line, \$column);
1399 } elsif(/^\#\s*undef\s+(.*?)$/s) {
1400 $self->_update_c_position($_, \$line, \$column);
1402 $self->_parse_c_error($_, $line, $column, "preprocessor");
1407 $$refcolumn = $column;
1412 ########################################################################
1415 sub parse_c_statement($$$$) {
1418 my $refcurrent = shift;
1419 my $refline = shift;
1420 my $refcolumn = shift;
1422 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1424 local $_ = $$refcurrent;
1425 my $line = $$refline;
1426 my $column = $$refcolumn;
1428 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1430 $self->_parse_c('(?:case\s+)?(\w+)\s*:\s*', \$_, \$line, \$column);
1432 # $output->write("$line.$column: statement: '$_'\n");
1438 my $statements_line;
1439 my $statements_column;
1440 if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1443 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1446 } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
1447 $self->_update_c_position($&, \$line, \$column);
1453 my @argument_columns;
1454 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1458 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1459 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1462 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1464 while(defined(my $argument = shift @arguments) &&
1465 defined(my $argument_line = shift @argument_lines) &&
1466 defined(my $argument_column = shift @argument_columns))
1468 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1470 } elsif(s/^else//) {
1471 $self->_update_c_position($&, \$line, \$column);
1472 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1475 } elsif(s/^return//) {
1476 $self->_update_c_position($&, \$line, \$column);
1477 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1478 if(!$self->parse_c_expression(\$_, \$line, \$column)) {
1481 } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1484 # $self->_parse_c_error($_, $line, $column, "statement");
1487 $self->_update_c_position($_, \$line, \$column);
1491 $$refcolumn = $column;
1496 ########################################################################
1497 # parse_c_statements
1499 sub parse_c_statements($$$$) {
1502 my $refcurrent = shift;
1503 my $refline = shift;
1504 my $refcolumn = shift;
1506 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1508 local $_ = $$refcurrent;
1509 my $line = $$refline;
1510 my $column = $$refcolumn;
1512 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1514 # $output->write("$line.$column: statements: '$_'\n");
1517 my $statement_line = $line;
1518 my $statement_column = $column;
1520 my $previous_line = -1;
1521 my $previous_column = -1;
1525 while($plevel > 0 || $blevel > 0) {
1527 $self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1529 if($previous_line == $line && $previous_column == $column) {
1530 $self->_parse_c_error($_, $line, $column, "statements", "no progress");
1532 $previous_line = $line;
1533 $previous_column = $column;
1535 # $output->write("'$match' '$_'\n");
1537 $statement .= $match;
1542 } elsif(s/^[\)\]]//) {
1545 $self->_parse_c_error($_, $line, $column, "statements");
1555 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1558 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1560 $statement_line = $line;
1561 $statement_column = $column;
1564 if($plevel == 1 && $blevel == 1) {
1565 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1569 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1571 $statement_line = $line;
1572 $statement_column = $column;
1576 } elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
1580 $self->_parse_c_error($_, $line, $column, "statements");
1584 $self->_update_c_position($_, \$line, \$column);
1588 $$refcolumn = $column;
1593 ########################################################################
1594 # parse_c_struct_union
1596 sub parse_c_struct_union($$$$$$$$$) {
1599 my $refcurrent = shift;
1600 my $refline = shift;
1601 my $refcolumn = shift;
1603 my $refkind = shift;
1604 my $ref_name = shift;
1605 my $reffield_type_names = shift;
1606 my $reffield_names = shift;
1607 my $refnames = shift;
1609 local $_ = $$refcurrent;
1610 my $line = $$refline;
1611 my $column = $$refcolumn;
1615 my @field_type_names = ();
1616 my @field_names = ();
1619 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1621 if (!s/^(struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1627 $self->_update_c_position($&, \$line, \$column);
1632 while ($_ && $self->_parse_c_on_same_level_until_one_of(';', \$_, \$line, \$column, \$match))
1635 my $field_type_name;
1638 if ($self->parse_c_variable(\$match, \$line, \$column, \$field_linkage, \$field_type_name, \$field_name)) {
1639 $field_type_name =~ s/\s+/ /g;
1641 push @field_type_names, $field_type_name;
1642 push @field_names, $field_name;
1643 # $output->write("$kind:$_name:$field_type_name:$field_name\n");
1645 $self->_parse_c_error($_, $line, $column, "typedef $kind: '$match'");
1648 if ($self->_parse_c(';', \$_, \$line, \$column)) {
1650 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1653 my $tuple_line = $line;
1654 my $tuple_column = $column - 1;
1658 my @argument_columns;
1660 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1661 \@arguments, \@argument_lines, \@argument_columns))
1663 $self->_parse_c_error($_, $line, $column, "$kind");
1666 foreach my $argument (@arguments) {
1667 my $name = $argument;
1674 $self->_parse_c_error($_, $line, $column, "$kind");
1680 $$refcolumn = $column;
1683 $$ref_name = $_name;
1684 @$reffield_type_names = @field_type_names;
1685 @$reffield_names = @field_names;
1686 @$refnames = @names;
1691 ########################################################################
1694 sub parse_c_tuple($$$$$$$) {
1697 my $refcurrent = shift;
1698 my $refline = shift;
1699 my $refcolumn = shift;
1701 # FIXME: Should not write directly
1703 my $item_lines = shift;
1704 my $item_columns = shift;
1706 local $_ = $$refcurrent;
1708 my $line = $$refline;
1709 my $column = $$refcolumn;
1719 my $item_line = $line;
1720 my $item_column = $column + 1;
1723 while($plevel > 0) {
1725 $self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1733 push @$item_lines, $item_line;
1734 push @$item_columns, $item_column;
1735 push @$items, $item;
1745 push @$item_lines, $item_line;
1746 push @$item_columns, $item_column;
1747 push @$items, $item;
1748 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1750 $item_column = $column + 1;
1762 $$refcolumn = $column;
1767 ########################################################################
1770 sub parse_c_type($$$$$) {
1773 my $refcurrent = shift;
1774 my $refline = shift;
1775 my $refcolumn = shift;
1777 my $reftype = shift;
1779 local $_ = $$refcurrent;
1780 my $line = $$refline;
1781 my $column = $$refcolumn;
1785 $self->_parse_c("const", \$_, \$line, \$column);
1789 } elsif($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1791 } elsif($self->_parse_c('(?:enum\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1792 \$_, \$line, \$column, \$type))
1802 $$refcolumn = $column;
1809 ########################################################################
1812 sub parse_c_typedef($$$$) {
1815 my $create_type = \${$self->{CREATE_TYPE}};
1816 my $found_type = \${$self->{FOUND_TYPE}};
1817 my $preprocessor_condition = \${$self->{PREPROCESSOR_CONDITION}};
1819 my $refcurrent = shift;
1820 my $refline = shift;
1821 my $refcolumn = shift;
1823 local $_ = $$refcurrent;
1824 my $line = $$refline;
1825 my $column = $$refcolumn;
1829 if (!$self->_parse_c("typedef", \$_, \$line, \$column)) {
1837 } elsif ($self->parse_c_enum(\$_, \$line, \$column)) {
1843 my @field_type_names;
1848 } elsif ($self->parse_c_struct_union(\$_, \$line, \$column,
1849 \$kind, \$_name, \@field_type_names, \@field_names, \@names))
1852 foreach my $name (@names)
1854 if ($name =~ /^\w+$/)
1860 $base_name="$kind $_name" if (!defined $base_name and defined $_name);
1861 $base_name=$kind if (!defined $base_name);
1862 foreach my $name (@names) {
1863 if ($name =~ /^\w+$/) {
1864 my $type = &$$create_type();
1867 $type->_name($_name);
1869 $type->field_type_names([@field_type_names]);
1870 $type->field_names([@field_names]);
1872 &$$found_type($type);
1873 } elsif ($name =~ /^(\*+)\s*(?:RESTRICTED_POINTER\s+)?(\w+)$/) {
1874 my $type_name = "$base_name $1";
1877 my $type = &$$create_type();
1881 $type->field_type_names([$type_name]);
1882 $type->field_names([""]);
1884 &$$found_type($type);
1886 $self->_parse_c_error($_, $line, $column, "typedef 2");
1898 } elsif ($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type_name, \$name)) {
1899 $type_name =~ s/\s+/ /g;
1901 if(defined($type_name) && defined($name)) {
1902 my $type = &$$create_type();
1904 if (length($name) == 0) {
1905 $self->_parse_c_error($_, $line, $column, "typedef");
1910 $type->field_type_names([$type_name]);
1911 $type->field_names([""]);
1913 &$$found_type($type);
1916 if (0 && $_ && !/^,/) {
1917 $self->_parse_c_error($_, $line, $column, "typedef");
1920 $self->_parse_c_error($_, $line, $column, "typedef");
1925 $$refcolumn = $column;
1930 ########################################################################
1933 sub parse_c_variable($$$$$$$) {
1936 my $found_variable = \${$self->{FOUND_VARIABLE}};
1938 my $refcurrent = shift;
1939 my $refline = shift;
1940 my $refcolumn = shift;
1942 my $reflinkage = shift;
1943 my $reftype = shift;
1944 my $refname = shift;
1946 local $_ = $$refcurrent;
1947 my $line = $$refline;
1948 my $column = $$refcolumn;
1950 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1952 my $begin_line = $line;
1953 my $begin_column = $column + 1;
1960 # $self->_parse_c_warning($_, $line, $column, "variable");
1963 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1964 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1965 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1966 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1967 \$_, \$line, \$column, \$match))
1969 if ($match =~ /^(?:extern|static)$/) {
1973 $self->_parse_c_warning($_, $line, $column, "repeated linkage (ignored): $match");
1975 } elsif ($match =~ /^(?:signed|unsigned)$/) {
1979 $self->_parse_c_warning($_, $line, $column, "repeated sign (ignored): $match");
1990 } elsif (s/^(enum\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1993 $self->_update_c_position($&, \$line, \$column);
1995 if(defined($_name)) {
1996 $type = "$kind $_name { }";
1998 $type = "$kind { }";
2002 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+\b(?:\s+DECLSPEC_ALIGN\(.*?\)|\s*(?:const\s*)?\*)*)\s*(\w+)\s*(\[.*?\]$|:\s*(\d+)$|\{)?//s) {
2018 $type = $self->_format_c_type($type);
2021 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*)\s*:\s*(\d+)$//s) {
2022 $type = "$sign$1:$2";
2024 $type = $self->_format_c_type($type);
2027 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*\s*\(\s*(?:$CALL_CONVENTION)?(?:\s*\*)*)\s*(\w+)\s*(\)\s*\(.*?\))$//s) {
2028 $type = $self->_format_c_type("$sign$1$3");
2032 } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
2036 $self->_parse_c_warning($_, $line, $column, "variable", "'$_'");
2042 } elsif($self->_parse_c('SEQ_DEFINEBUF', \$_, \$line, \$column, \$match)) { # Linux specific
2045 } elsif($self->_parse_c('DEFINE_REGS_ENTRYPOINT_\w+|DPQ_DECL_\w+|HANDLER_DEF|IX86_ONLY', # Wine specific
2046 \$_, \$line, \$column, \$match))
2050 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
2053 } elsif(s/^(enum|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*//s) {
2056 $self->_update_c_position($&, \$line, \$column);
2058 if(defined($_name)) {
2059 $type = "struct $_name { }";
2061 $type = "struct { }";
2063 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
2070 # $output->write("*** $type: '$_'\n");
2072 # $self->_parse_c_warning($_, $line, $column, "variable2", "");
2076 } elsif(s/^WINAPI\s*//) {
2077 $self->_update_c_position($&, \$line, \$column);
2078 } elsif(s/^WINE_UNUSED\s*//) {
2079 $self->_update_c_position($&, \$line, \$column);
2084 } elsif(s/^(\((?:$CALL_CONVENTION)?\s*\*?\s*(?:$CALL_CONVENTION)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
2085 $self->_update_c_position($&, \$line, \$column);
2090 $self->_parse_c_until_one_of("\\)", \$_, \$line, \$column);
2091 if(s/^\)//) { $column++; }
2092 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
2094 if(!s/^(?:=\s*|,\s*|$)//) {
2097 } elsif(s/^(?:\*\s*)*(?:const\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
2098 $self->_update_c_position($&, \$line, \$column);
2108 # $output->write("$type: $name: '$_'\n");
2110 if(1 || $finished) {
2112 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(.*?\)', \$_, \$line, \$column, \$match)) {
2115 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+)\s*
2116 (?:\*\s*)*(\w+|\s*\*?\s*\w+\s*\))\s*(?:\[[^\]]*\]|\([^\)]*\))?
2117 (?:,\s*(?:\*\s*)*(\w+)\s*(?:\[[^\]]*\])?)*
2120 $self->_update_c_position($&, \$line, \$column);
2126 $type =~ s/^struct/struct /;
2127 } elsif(/^(enum|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*((?:\*\s*)*)(\w+)\s*(?:=|$)/s) {
2128 $self->_update_c_position($&, \$line, \$column);
2135 if(defined($_name)) {
2136 $type = "struct $_name { }";
2138 $type = "struct { }";
2151 $$refcolumn = $column;
2153 $$reflinkage = $linkage;
2157 if(&$$found_variable($begin_line, $begin_column, $linkage, $type, $name))