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($$$$$$$$$$$$) {
569 my $found_declaration = \${$self->{FOUND_DECLARATION}};
570 my $found_function = \${$self->{FOUND_FUNCTION}};
572 my $refcurrent = shift;
574 my $refcolumn = shift;
576 local $_ = $$refcurrent;
577 my $line = $$refline;
578 my $column = $$refcolumn;
580 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
582 my $begin_line = $line;
583 my $begin_column = $column + 1;
585 my $end_line = $begin_line;
586 my $end_column = $begin_column;
587 $self->_update_c_position($_, \$end_line, \$end_column);
589 if(!&$$found_declaration($begin_line, $begin_column, $end_line, $end_column, $_)) {
594 my $function = shift;
597 my $calling_convention = shift;
598 my $return_type = shift;
600 my @arguments = shift;
601 my @argument_lines = shift;
602 my @argument_columns = shift;
609 } elsif(s/^WINE_(?:DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\(\s*(\w+)\s*\)\s*//s) { # FIXME: Wine specific kludge
610 $self->_update_c_position($&, \$line, \$column);
611 } elsif(s/^__ASM_GLOBAL_FUNC\(\s*(\w+)\s*,\s*//s) { # FIXME: Wine specific kludge
612 $self->_update_c_position($&, \$line, \$column);
613 $self->_parse_c_until_one_of("\)", \$_, \$line, \$column);
617 } elsif(s/^(?:DEFINE_AVIGUID|DEFINE_OLEGUID)\s*(?=\()//s) { # FIXME: Wine specific kludge
618 $self->_update_c_position($&, \$line, \$column);
622 my @argument_columns;
624 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
627 } elsif(s/^DEFINE_COMMON_NOTIFICATIONS\(\s*(\w+)\s*,\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
628 $self->_update_c_position($&, \$line, \$column);
629 } elsif(s/^MAKE_FUNCPTR\(\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
630 $self->_update_c_position($&, \$line, \$column);
631 } elsif(s/^START_TEST\(\s*(\w+)\s*\)\s*{//s) { # FIXME: Wine specific kludge
632 $self->_update_c_position($&, \$line, \$column);
633 } elsif(s/^int\s*_FUNCTION_\s*{//s) { # FIXME: Wine specific kludge
634 $self->_update_c_position($&, \$line, \$column);
635 } elsif(s/^(?:jump|strong)_alias//s) { # FIXME: GNU C library specific kludge
636 $self->_update_c_position($&, \$line, \$column);
637 } elsif(s/^(?:__asm__|asm)\s*\(//) {
638 $self->_update_c_position($&, \$line, \$column);
639 } elsif($self->parse_c_typedef(\$_, \$line, \$column)) {
641 } elsif($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type, \$name)) {
643 } elsif($self->parse_c_function(\$_, \$line, \$column, \$function)) {
644 if(&$$found_function($function))
646 my $statements = $function->statements;
647 my $statements_line = $function->statements_line;
648 my $statements_column = $function->statements_column;
650 if(defined($statements)) {
651 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
657 $self->_parse_c_error($_, $line, $column, "declaration");
662 $$refcolumn = $column;
667 ########################################################################
668 # parse_c_declarations
670 sub parse_c_declarations($$$$) {
673 my $refcurrent = shift;
675 my $refcolumn = shift;
680 ########################################################################
683 sub _parse_c($$$$$$) {
687 my $refcurrent = shift;
689 my $refcolumn = shift;
691 my $refmatch = shift;
693 local $_ = $$refcurrent;
694 my $line = $$refline;
695 my $column = $$refcolumn;
698 if(s/^(?:$pattern)//s) {
699 $self->_update_c_position($&, \$line, \$column);
705 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
709 $$refcolumn = $column;
716 ########################################################################
719 sub parse_c_enum($$$$) {
722 my $refcurrent = shift;
724 my $refcolumn = shift;
726 local $_ = $$refcurrent;
727 my $line = $$refline;
728 my $column = $$refcolumn;
730 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
732 if (!s/^enum\s+((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
735 my $_name = $1 || "";
737 $self->_update_c_position($&, \$line, \$column);
742 while ($self->_parse_c_on_same_level_until_one_of(',', \$_, \$line, \$column, \$match)) {
744 if ($match !~ /^(\w+)\s*(?:=\s*(.*?)\s*)?$/) {
745 $self->_parse_c_error($_, $line, $column, "enum");
748 my $enum_value = $2 || "";
750 # $output->write("enum:$_name:$enum_name:$enum_value\n");
753 if ($self->_parse_c(',', \$_, \$line, \$column)) {
755 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
758 my $tuple_line = $line;
759 my $tuple_column = $column - 1;
763 my @argument_columns;
765 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
766 \@arguments, \@argument_lines, \@argument_columns))
768 $self->_parse_c_error($_, $line, $column, "enum");
772 if ($#arguments >= 0) {
773 $name = $arguments[0];
778 $self->_parse_c_error($_, $line, $column, "enum");
782 $self->_update_c_position($_, \$line, \$column);
786 $$refcolumn = $column;
790 ########################################################################
793 sub parse_c_expression($$$$) {
796 my $refcurrent = shift;
798 my $refcolumn = shift;
800 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
802 local $_ = $$refcurrent;
803 my $line = $$refline;
804 my $column = $$refcolumn;
806 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
809 if(s/^(.*?)(\w+\s*\()/$2/s) {
810 $self->_update_c_position($1, \$line, \$column);
812 my $begin_line = $line;
813 my $begin_column = $column + 1;
818 my @argument_columns;
819 if(!$self->parse_c_function_call(\$_, \$line, \$column, \$name, \@arguments, \@argument_lines, \@argument_columns)) {
823 if(&$$found_function_call($begin_line, $begin_column, $line, $column, $name, \@arguments))
825 while(defined(my $argument = shift @arguments) &&
826 defined(my $argument_line = shift @argument_lines) &&
827 defined(my $argument_column = shift @argument_columns))
829 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
837 $self->_update_c_position($_, \$line, \$column);
841 $$refcolumn = $column;
846 ########################################################################
849 sub parse_c_file($$$$) {
852 my $found_comment = \${$self->{FOUND_COMMENT}};
853 my $found_line = \${$self->{FOUND_LINE}};
855 my $refcurrent = shift;
857 my $refcolumn = shift;
859 local $_ = $$refcurrent;
860 my $line = $$refline;
861 my $column = $$refcolumn;
863 my $declaration = "";
864 my $declaration_line = $line;
865 my $declaration_column = $column;
867 my $previous_line = 0;
868 my $previous_column = -1;
870 my $preprocessor_condition;
877 while($plevel > 0 || $blevel > 0) {
879 $self->_parse_c_until_one_of("#/\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
881 if($line != $previous_line) {
882 &$$found_line($line);
883 } elsif(0 && $column == $previous_column) {
884 $self->_parse_c_error($_, $line, $column, "file", "no progress");
886 # &$$found_line("$line.$column");
888 $previous_line = $line;
889 $previous_column = $column;
891 if($match !~ /^\s+$/s && $options->debug) {
892 $self->_parse_c_warning($_, $line, $column, "file", "$plevel $blevel: '$declaration' '$match'");
895 if(!$declaration && $match =~ s/^\s+//s) {
896 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
900 $declaration .= $match;
903 if ($declaration =~ s/^extern\s*\"C\"//s) {
905 $self->_update_c_position($&, \$line, \$column);
907 $declaration_line = $line;
908 $declaration_column = $column;
913 } elsif ($extern_c && $blevel == 1 && $plevel == 1 && !$declaration) {
915 $self->_update_c_position($&, \$line, \$column);
917 $declaration_line = $line;
918 $declaration_column = $column;
923 } elsif($declaration =~ s/^(?:__DEFINE_(?:GET|SET)_SEG|OUR_GUID_ENTRY)\s*(?=\()//sx) { # FIXME: Wine specific kludge
925 if ($plevel > 2 || !s/^\)//) {
926 $declaration = "$prefix$declaration";
929 $self->_update_c_position($&, \$line, \$column);
934 my @argument_columns;
936 if(!$self->parse_c_tuple(\$declaration, \$declaration_line, \$declaration_column,
937 \@arguments, \@argument_lines, \@argument_columns))
939 $self->_parse_c_error($declaration, $declaration_line, $declaration_column, "file", "tuple expected");
943 $declaration_line = $line;
944 $declaration_column = $column;
948 } elsif ($declaration =~ s/^(?:DEFINE_SHLGUID)\s*\(.*?\)//s) {
949 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
950 } elsif ($declaration =~ s/^(?:DECL_WINELIB_TYPE_AW|DECLARE_HANDLE(?:16)?|TYPE_MARSHAL)\(\s*(\w+)\s*\)\s*//s) {
951 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
952 } elsif ($declaration =~ s/^ICOM_DEFINE\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*//s) {
953 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
959 while(s/^.*?\n//) { $blank_lines++; }
962 $declaration_line = $line;
963 $declaration_column = $column;
965 $declaration .= "\n" x $blank_lines;
973 my $preprocessor_line = $line;
974 my $preprocessor_column = $column;
976 my $preprocessor = $&;
977 while(s/^(.*?)\\\s*\n//) {
979 $preprocessor .= "$1\n";
981 if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
984 $preprocessor .= "$1$3";
988 } elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
1000 } elsif($preprocessor =~ /^\#\s*if/) {
1001 if($preprocessor =~ /^\#\s*if\s*0/) {
1006 if($preprocessor =~ /^\#\s*ifdef\s+WORDS_BIGENDIAN$/) {
1007 $preprocessor_condition = "defined(WORD_BIGENDIAN)";
1008 # $output->write("'$preprocessor_condition':'$declaration'\n")
1010 $preprocessor_condition = "";
1013 } elsif($preprocessor =~ /^\#\s*else/) {
1014 if ($preprocessor_condition ne "") {
1015 $preprocessor_condition =~ "!$preprocessor_condition";
1016 $preprocessor_condition =~ s/^!!/!/;
1017 # $output->write("'$preprocessor_condition':'$declaration'\n")
1019 } elsif($preprocessor =~ /^\#\s*endif/) {
1027 if ($preprocessor_condition ne "") {
1028 # $output->write("'$preprocessor_condition':'$declaration'\n");
1029 $preprocessor_condition = "";
1034 if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
1039 if(s/^\/\*.*?\*\///s) {
1040 &$$found_comment($line, $column + 1, $&);
1046 $column += length($_);
1048 } elsif(s/^\/\/(.*?)\n//) {
1049 &$$found_comment($line, $column + 1, $&);
1058 $line += $blank_lines;
1059 if($blank_lines > 0) {
1064 $declaration_line = $line;
1065 $declaration_column = $column;
1066 } elsif($blank_lines > 0) {
1067 $declaration .= "\n" x $blank_lines;
1089 $self->_parse_c_error($_, $line, $column, "file", ") without (");
1092 if($plevel == 1 && $declaration =~ /^__ASM_GLOBAL_FUNC/) {
1093 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1096 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1098 $declaration_line = $line;
1099 $declaration_column = $column;
1107 $self->_parse_c_error($_, $line, $column, "file", "} without {");
1112 if($declaration =~ /^typedef/s ||
1113 $declaration =~ /^(?:const\s+|extern\s+|static\s+|volatile\s+)*(?:interface|struct|union)(?:\s+\w+)?\s*\{/s)
1116 } elsif($plevel == 1 && $blevel == 1) {
1117 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1120 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1122 $declaration_line = $line;
1123 $declaration_column = $column;
1124 } elsif($column == 1 && !$extern_c) {
1125 $self->_parse_c_error("", $line, $column, "file", "inner } ends on column 1");
1129 if(0 && $blevel == 1 &&
1130 $declaration !~ /^typedef/ &&
1131 $declaration !~ /^(?:const\s+|extern\s+|static\s+|volatile\s+)?(?:interface|struct|union)(?:\s+\w+)?\s*\{/s &&
1132 $declaration =~ /^(?:\w+(?:\s*\*)*\s+)*(\w+)\s*\(\s*(?:(?:\w+\s*,\s*)*(\w+))?\s*\)\s*(.*?);$/s &&
1133 $1 ne "ICOM_VTABLE" && defined($2) && $2 ne "void" && $3) # K&R
1135 $self->_parse_c_warning("", $line, $column, "file", "function $1: warning: function has K&R format");
1136 } elsif($plevel == 1 && $blevel == 1) {
1137 $declaration =~ s/\s*;$//;
1138 if($declaration && !$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1141 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1143 $declaration_line = $line;
1144 $declaration_column = $column;
1146 } elsif(/^\s*$/ && $declaration =~ /^\s*$/ && $match =~ /^\s*$/) {
1150 $self->_parse_c_error($_, $line, $column, "file", "parse error: '$declaration' '$match'");
1156 $$refcolumn = $column;
1161 ########################################################################
1164 sub parse_c_function($$$$$) {
1167 my $file = \${$self->{FILE}};
1168 my $create_function = \${$self->{CREATE_FUNCTION}};
1170 my $refcurrent = shift;
1171 my $refline = shift;
1172 my $refcolumn = shift;
1174 my $reffunction = shift;
1176 local $_ = $$refcurrent;
1177 my $line = $$refline;
1178 my $column = $$refcolumn;
1181 my $calling_convention = "";
1186 my @argument_columns;
1188 my $statements_line;
1189 my $statements_column;
1191 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1193 my $begin_line = $line;
1194 my $begin_column = $column + 1;
1198 } elsif($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
1202 # $self->_parse_c_warning($_, $line, $column, "function", "");
1205 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1206 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1207 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1208 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1209 \$_, \$line, \$column, \$match))
1211 if($match =~ /^(?:extern|static)$/) {
1220 } elsif($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1222 } elsif($self->_parse_c('WINE_EXCEPTION_FILTER\(\w+\)', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1225 if(!$self->parse_c_type(\$_, \$line, \$column, \$return_type)) {
1229 $self->_parse_c('inline|FAR', \$_, \$line, \$column);
1231 $self->_parse_c($CALL_CONVENTION,
1232 \$_, \$line, \$column, \$calling_convention);
1235 # FIXME: ???: Old variant of __attribute((const))
1236 $self->_parse_c('(?:const|volatile)', \$_, \$line, \$column);
1238 if(!$self->_parse_c('(?:operator\s*!=|(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)', \$_, \$line, \$column, \$name)) {
1244 $self->_update_c_position($&, \$line, \$column);
1248 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1254 $self->_update_c_position($&, \$line, \$column);
1256 $self->_parse_c_error($_, $line, $column, "function");
1264 } elsif($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
1269 # FIXME: Implement proper handling of K&R C functions
1270 $self->_parse_c_until_one_of("{", \$_, \$line, \$column, $kar);
1273 $output->write("K&R: $kar\n");
1276 if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1280 my $end_line = $line;
1281 my $end_column = $column;
1285 $$refcolumn = $column;
1287 my $function = &$$create_function;
1289 $function->file($$file);
1290 $function->begin_line($begin_line);
1291 $function->begin_column($begin_column);
1292 $function->end_line($end_line);
1293 $function->end_column($end_column);
1294 $function->linkage($linkage);
1295 $function->return_type($return_type);
1296 $function->calling_convention($calling_convention);
1297 $function->name($name);
1298 # if(defined($argument_types)) {
1299 # $function->argument_types([@$argument_types]);
1301 # if(defined($argument_names)) {
1302 # $function->argument_names([@$argument_names]);
1304 $function->statements_line($statements_line);
1305 $function->statements_column($statements_column);
1306 $function->statements($statements);
1308 $$reffunction = $function;
1313 ########################################################################
1314 # parse_c_function_call
1316 sub parse_c_function_call($$$$$$$$) {
1319 my $refcurrent = shift;
1320 my $refline = shift;
1321 my $refcolumn = shift;
1323 my $refname = shift;
1324 my $refarguments = shift;
1325 my $refargument_lines = shift;
1326 my $refargument_columns = shift;
1328 local $_ = $$refcurrent;
1329 my $line = $$refline;
1330 my $column = $$refcolumn;
1335 my @argument_columns;
1337 if(s/^(\w+)(\s*)(?=\()//s) {
1338 $self->_update_c_position($&, \$line, \$column);
1342 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1351 $$refcolumn = $column;
1354 @$refarguments = @arguments;
1355 @$refargument_lines = @argument_lines;
1356 @$refargument_columns = @argument_columns;
1361 ########################################################################
1362 # parse_c_preprocessor
1364 sub parse_c_preprocessor($$$$) {
1367 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
1369 my $refcurrent = shift;
1370 my $refline = shift;
1371 my $refcolumn = shift;
1373 local $_ = $$refcurrent;
1374 my $line = $$refline;
1375 my $column = $$refcolumn;
1377 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1379 my $begin_line = $line;
1380 my $begin_column = $column + 1;
1382 if(!&$$found_preprocessor($begin_line, $begin_column, "$_")) {
1388 } elsif(/^\#\s*define\s*(.*?)$/s) {
1389 $self->_update_c_position($_, \$line, \$column);
1390 } elsif(/^\#\s*else/s) {
1391 $self->_update_c_position($_, \$line, \$column);
1392 } elsif(/^\#\s*endif/s) {
1393 $self->_update_c_position($_, \$line, \$column);
1394 } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1395 $self->_update_c_position($_, \$line, \$column);
1396 } elsif(/^\#\s*include\s+(.*?)$/s) {
1397 $self->_update_c_position($_, \$line, \$column);
1398 } elsif(/^\#\s*undef\s+(.*?)$/s) {
1399 $self->_update_c_position($_, \$line, \$column);
1401 $self->_parse_c_error($_, $line, $column, "preprocessor");
1406 $$refcolumn = $column;
1411 ########################################################################
1414 sub parse_c_statement($$$$) {
1417 my $refcurrent = shift;
1418 my $refline = shift;
1419 my $refcolumn = shift;
1421 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1423 local $_ = $$refcurrent;
1424 my $line = $$refline;
1425 my $column = $$refcolumn;
1427 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1429 $self->_parse_c('(?:case\s+)?(\w+)\s*:\s*', \$_, \$line, \$column);
1431 # $output->write("$line.$column: statement: '$_'\n");
1437 my $statements_line;
1438 my $statements_column;
1439 if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1442 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1445 } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
1446 $self->_update_c_position($&, \$line, \$column);
1452 my @argument_columns;
1453 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1457 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1458 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1461 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1463 while(defined(my $argument = shift @arguments) &&
1464 defined(my $argument_line = shift @argument_lines) &&
1465 defined(my $argument_column = shift @argument_columns))
1467 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1469 } elsif(s/^else//) {
1470 $self->_update_c_position($&, \$line, \$column);
1471 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1474 } elsif(s/^return//) {
1475 $self->_update_c_position($&, \$line, \$column);
1476 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1477 if(!$self->parse_c_expression(\$_, \$line, \$column)) {
1480 } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1483 # $self->_parse_c_error($_, $line, $column, "statement");
1486 $self->_update_c_position($_, \$line, \$column);
1490 $$refcolumn = $column;
1495 ########################################################################
1496 # parse_c_statements
1498 sub parse_c_statements($$$$) {
1501 my $refcurrent = shift;
1502 my $refline = shift;
1503 my $refcolumn = shift;
1505 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1507 local $_ = $$refcurrent;
1508 my $line = $$refline;
1509 my $column = $$refcolumn;
1511 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1513 # $output->write("$line.$column: statements: '$_'\n");
1516 my $statement_line = $line;
1517 my $statement_column = $column;
1519 my $previous_line = -1;
1520 my $previous_column = -1;
1524 while($plevel > 0 || $blevel > 0) {
1526 $self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1528 if($previous_line == $line && $previous_column == $column) {
1529 $self->_parse_c_error($_, $line, $column, "statements", "no progress");
1531 $previous_line = $line;
1532 $previous_column = $column;
1534 # $output->write("'$match' '$_'\n");
1536 $statement .= $match;
1541 } elsif(s/^[\)\]]//) {
1544 $self->_parse_c_error($_, $line, $column, "statements");
1554 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1557 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1559 $statement_line = $line;
1560 $statement_column = $column;
1563 if($plevel == 1 && $blevel == 1) {
1564 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1568 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1570 $statement_line = $line;
1571 $statement_column = $column;
1575 } elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
1579 $self->_parse_c_error($_, $line, $column, "statements");
1583 $self->_update_c_position($_, \$line, \$column);
1587 $$refcolumn = $column;
1592 ########################################################################
1593 # parse_c_struct_union
1595 sub parse_c_struct_union($$$$$$$$$) {
1598 my $refcurrent = shift;
1599 my $refline = shift;
1600 my $refcolumn = shift;
1602 my $refkind = shift;
1603 my $ref_name = shift;
1604 my $reffield_type_names = shift;
1605 my $reffield_names = shift;
1606 my $refnames = shift;
1608 local $_ = $$refcurrent;
1609 my $line = $$refline;
1610 my $column = $$refcolumn;
1614 my @field_type_names = ();
1615 my @field_names = ();
1618 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1620 if (!s/^(interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1626 $self->_update_c_position($&, \$line, \$column);
1631 while ($_ && $self->_parse_c_on_same_level_until_one_of(';', \$_, \$line, \$column, \$match))
1634 my $field_type_name;
1637 if ($self->parse_c_variable(\$match, \$line, \$column, \$field_linkage, \$field_type_name, \$field_name)) {
1638 $field_type_name =~ s/\s+/ /g;
1640 push @field_type_names, $field_type_name;
1641 push @field_names, $field_name;
1642 # $output->write("$kind:$_name:$field_type_name:$field_name\n");
1644 $self->_parse_c_error($_, $line, $column, "typedef $kind: '$match'");
1647 if ($self->_parse_c(';', \$_, \$line, \$column)) {
1649 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1652 my $tuple_line = $line;
1653 my $tuple_column = $column - 1;
1657 my @argument_columns;
1659 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1660 \@arguments, \@argument_lines, \@argument_columns))
1662 $self->_parse_c_error($_, $line, $column, "$kind");
1665 foreach my $argument (@arguments) {
1666 my $name = $argument;
1673 $self->_parse_c_error($_, $line, $column, "$kind");
1679 $$refcolumn = $column;
1682 $$ref_name = $_name;
1683 @$reffield_type_names = @field_type_names;
1684 @$reffield_names = @field_names;
1685 @$refnames = @names;
1690 ########################################################################
1693 sub parse_c_tuple($$$$$$$) {
1696 my $refcurrent = shift;
1697 my $refline = shift;
1698 my $refcolumn = shift;
1700 # FIXME: Should not write directly
1702 my $item_lines = shift;
1703 my $item_columns = shift;
1705 local $_ = $$refcurrent;
1707 my $line = $$refline;
1708 my $column = $$refcolumn;
1718 my $item_line = $line;
1719 my $item_column = $column + 1;
1722 while($plevel > 0) {
1724 $self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1732 push @$item_lines, $item_line;
1733 push @$item_columns, $item_column;
1734 push @$items, $item;
1744 push @$item_lines, $item_line;
1745 push @$item_columns, $item_column;
1746 push @$items, $item;
1747 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1749 $item_column = $column + 1;
1761 $$refcolumn = $column;
1766 ########################################################################
1769 sub parse_c_type($$$$$) {
1772 my $refcurrent = shift;
1773 my $refline = shift;
1774 my $refcolumn = shift;
1776 my $reftype = shift;
1778 local $_ = $$refcurrent;
1779 my $line = $$refline;
1780 my $column = $$refcolumn;
1784 $self->_parse_c("(?:const|volatile)", \$_, \$line, \$column);
1788 } elsif($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1790 } elsif($self->_parse_c('(?:enum\s+|interface\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1791 \$_, \$line, \$column, \$type))
1801 $$refcolumn = $column;
1808 ########################################################################
1811 sub parse_c_typedef($$$$) {
1814 my $create_type = \${$self->{CREATE_TYPE}};
1815 my $found_type = \${$self->{FOUND_TYPE}};
1816 my $preprocessor_condition = \${$self->{PREPROCESSOR_CONDITION}};
1818 my $refcurrent = shift;
1819 my $refline = shift;
1820 my $refcolumn = shift;
1822 local $_ = $$refcurrent;
1823 my $line = $$refline;
1824 my $column = $$refcolumn;
1828 if (!$self->_parse_c("typedef", \$_, \$line, \$column)) {
1836 } elsif ($self->parse_c_enum(\$_, \$line, \$column)) {
1842 my @field_type_names;
1847 } elsif ($self->parse_c_struct_union(\$_, \$line, \$column,
1848 \$kind, \$_name, \@field_type_names, \@field_names, \@names))
1851 foreach my $name (@names)
1853 if ($name =~ /^\w+$/)
1859 $base_name="$kind $_name" if (!defined $base_name and defined $_name);
1860 $base_name=$kind if (!defined $base_name);
1861 foreach my $name (@names) {
1862 if ($name =~ /^\w+$/) {
1863 my $type = &$$create_type();
1866 $type->_name($_name);
1868 $type->field_type_names([@field_type_names]);
1869 $type->field_names([@field_names]);
1871 &$$found_type($type);
1872 } elsif ($name =~ /^(\*+)\s*(?:RESTRICTED_POINTER\s+)?(\w+)$/) {
1873 my $type_name = "$base_name $1";
1876 my $type = &$$create_type();
1880 $type->field_type_names([$type_name]);
1881 $type->field_names([""]);
1883 &$$found_type($type);
1885 $self->_parse_c_error($_, $line, $column, "typedef 2");
1897 } elsif ($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type_name, \$name)) {
1898 $type_name =~ s/\s+/ /g;
1900 if(defined($type_name) && defined($name)) {
1901 my $type = &$$create_type();
1903 if (length($name) == 0) {
1904 $self->_parse_c_error($_, $line, $column, "typedef");
1909 $type->field_type_names([$type_name]);
1910 $type->field_names([""]);
1912 &$$found_type($type);
1915 if (0 && $_ && !/^,/) {
1916 $self->_parse_c_error($_, $line, $column, "typedef");
1919 $self->_parse_c_error($_, $line, $column, "typedef");
1924 $$refcolumn = $column;
1929 ########################################################################
1932 sub parse_c_variable($$$$$$$) {
1935 my $found_variable = \${$self->{FOUND_VARIABLE}};
1937 my $refcurrent = shift;
1938 my $refline = shift;
1939 my $refcolumn = shift;
1941 my $reflinkage = shift;
1942 my $reftype = shift;
1943 my $refname = shift;
1945 local $_ = $$refcurrent;
1946 my $line = $$refline;
1947 my $column = $$refcolumn;
1949 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1951 my $begin_line = $line;
1952 my $begin_column = $column + 1;
1959 # $self->_parse_c_warning($_, $line, $column, "variable");
1962 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1963 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1964 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1965 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1966 \$_, \$line, \$column, \$match))
1968 if ($match =~ /^(?:extern|static)$/) {
1972 $self->_parse_c_warning($_, $line, $column, "repeated linkage (ignored): $match");
1974 } elsif ($match =~ /^(?:signed|unsigned)$/) {
1978 $self->_parse_c_warning($_, $line, $column, "repeated sign (ignored): $match");
1989 } elsif (s/^(enum\s+|interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1992 $self->_update_c_position($&, \$line, \$column);
1994 if(defined($_name)) {
1995 $type = "$kind $_name { }";
1997 $type = "$kind { }";
2001 } 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) {
2017 $type = $self->_format_c_type($type);
2020 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*)\s*:\s*(\d+)$//s) {
2021 $type = "$sign$1:$2";
2023 $type = $self->_format_c_type($type);
2026 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*\s*\(\s*(?:$CALL_CONVENTION)?(?:\s*\*)*)\s*(\w+)\s*(\)\s*\(.*?\))$//s) {
2027 $type = $self->_format_c_type("$sign$1$3");
2031 } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
2035 $self->_parse_c_warning($_, $line, $column, "variable", "'$_'");
2041 } elsif($self->_parse_c('SEQ_DEFINEBUF', \$_, \$line, \$column, \$match)) { # Linux specific
2044 } elsif($self->_parse_c('DEFINE_REGS_ENTRYPOINT_\w+|DPQ_DECL_\w+|HANDLER_DEF|IX86_ONLY', # Wine specific
2045 \$_, \$line, \$column, \$match))
2049 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
2052 } elsif(s/^(enum|interface|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*//s) {
2055 $self->_update_c_position($&, \$line, \$column);
2057 if(defined($_name)) {
2058 $type = "struct $_name { }";
2060 $type = "struct { }";
2062 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
2069 # $output->write("*** $type: '$_'\n");
2071 # $self->_parse_c_warning($_, $line, $column, "variable2", "");
2075 } elsif(s/^WINAPI\s*//) {
2076 $self->_update_c_position($&, \$line, \$column);
2081 } elsif(s/^(\((?:$CALL_CONVENTION)?\s*\*?\s*(?:$CALL_CONVENTION)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
2082 $self->_update_c_position($&, \$line, \$column);
2087 $self->_parse_c_until_one_of("\\)", \$_, \$line, \$column);
2088 if(s/^\)//) { $column++; }
2089 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
2091 if(!s/^(?:=\s*|,\s*|$)//) {
2094 } elsif(s/^(?:\*\s*)*(?:const\s+|volatile\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
2095 $self->_update_c_position($&, \$line, \$column);
2105 # $output->write("$type: $name: '$_'\n");
2107 if(1 || $finished) {
2109 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(.*?\)', \$_, \$line, \$column, \$match)) {
2112 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+)\s*
2113 (?:\*\s*)*(\w+|\s*\*?\s*\w+\s*\))\s*(?:\[[^\]]*\]|\([^\)]*\))?
2114 (?:,\s*(?:\*\s*)*(\w+)\s*(?:\[[^\]]*\])?)*
2117 $self->_update_c_position($&, \$line, \$column);
2123 $type =~ s/^struct/struct /;
2124 } elsif(/^(enum|interface|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*((?:\*\s*)*)(\w+)\s*(?:=|$)/s) {
2125 $self->_update_c_position($&, \$line, \$column);
2132 if(defined($_name)) {
2133 $type = "struct $_name { }";
2135 $type = "struct { }";
2148 $$refcolumn = $column;
2150 $$reflinkage = $linkage;
2154 if(&$$found_variable($begin_line, $begin_column, $linkage, $type, $name))