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;
607 if(s/^WINE_(?:DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\(\s*(\w+)\s*\)\s*//s) { # FIXME: Wine specific kludge
608 $self->_update_c_position($&, \$line, \$column);
609 } elsif(s/^__ASM_GLOBAL_FUNC\(\s*(\w+)\s*,\s*//s) { # FIXME: Wine specific kludge
610 $self->_update_c_position($&, \$line, \$column);
611 $self->_parse_c_until_one_of("\)", \$_, \$line, \$column);
615 } elsif(s/^(?:DEFINE_AVIGUID|DEFINE_OLEGUID)\s*(?=\()//s) { # FIXME: Wine specific kludge
616 $self->_update_c_position($&, \$line, \$column);
620 my @argument_columns;
622 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
625 } elsif(s/^DEFINE_COMMON_NOTIFICATIONS\(\s*(\w+)\s*,\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
626 $self->_update_c_position($&, \$line, \$column);
627 } elsif(s/^MAKE_FUNCPTR\(\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
628 $self->_update_c_position($&, \$line, \$column);
629 } elsif(s/^START_TEST\(\s*(\w+)\s*\)\s*{//s) { # FIXME: Wine specific kludge
630 $self->_update_c_position($&, \$line, \$column);
631 } elsif(s/^int\s*_FUNCTION_\s*{//s) { # FIXME: Wine specific kludge
632 $self->_update_c_position($&, \$line, \$column);
633 } elsif(s/^(?:jump|strong)_alias//s) { # FIXME: GNU C library specific kludge
634 $self->_update_c_position($&, \$line, \$column);
635 } elsif(s/^(?:__asm__|asm)\s*\(//) {
636 $self->_update_c_position($&, \$line, \$column);
637 } elsif($self->parse_c_typedef(\$_, \$line, \$column)) {
639 } elsif($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type, \$name)) {
641 } elsif($self->parse_c_function(\$_, \$line, \$column, \$function)) {
642 if(&$$found_function($function))
644 my $statements = $function->statements;
645 my $statements_line = $function->statements_line;
646 my $statements_column = $function->statements_column;
648 if(defined($statements)) {
649 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
655 $self->_parse_c_error($_, $line, $column, "declaration");
660 $$refcolumn = $column;
665 ########################################################################
666 # parse_c_declarations
668 sub parse_c_declarations($$$$) {
671 my $refcurrent = shift;
673 my $refcolumn = shift;
678 ########################################################################
681 sub _parse_c($$$$$$) {
685 my $refcurrent = shift;
687 my $refcolumn = shift;
689 my $refmatch = shift;
691 local $_ = $$refcurrent;
692 my $line = $$refline;
693 my $column = $$refcolumn;
696 if(s/^(?:$pattern)//s) {
697 $self->_update_c_position($&, \$line, \$column);
703 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
707 $$refcolumn = $column;
714 ########################################################################
717 sub parse_c_enum($$$$) {
720 my $refcurrent = shift;
722 my $refcolumn = shift;
724 local $_ = $$refcurrent;
725 my $line = $$refline;
726 my $column = $$refcolumn;
728 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
730 if (!s/^enum\s+((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
733 my $_name = $1 || "";
735 $self->_update_c_position($&, \$line, \$column);
740 while ($self->_parse_c_on_same_level_until_one_of(',', \$_, \$line, \$column, \$match)) {
742 if ($match !~ /^(\w+)\s*(?:=\s*(.*?)\s*)?$/) {
743 $self->_parse_c_error($_, $line, $column, "enum");
746 my $enum_value = $2 || "";
748 # $output->write("enum:$_name:$enum_name:$enum_value\n");
751 if ($self->_parse_c(',', \$_, \$line, \$column)) {
753 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
756 my $tuple_line = $line;
757 my $tuple_column = $column - 1;
761 my @argument_columns;
763 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
764 \@arguments, \@argument_lines, \@argument_columns))
766 $self->_parse_c_error($_, $line, $column, "enum");
770 if ($#arguments >= 0) {
771 $name = $arguments[0];
776 $self->_parse_c_error($_, $line, $column, "enum");
780 $self->_update_c_position($_, \$line, \$column);
784 $$refcolumn = $column;
788 ########################################################################
791 sub parse_c_expression($$$$) {
794 my $refcurrent = shift;
796 my $refcolumn = shift;
798 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
800 local $_ = $$refcurrent;
801 my $line = $$refline;
802 my $column = $$refcolumn;
804 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
807 if(s/^(.*?)(\w+\s*\()/$2/s) {
808 $self->_update_c_position($1, \$line, \$column);
810 my $begin_line = $line;
811 my $begin_column = $column + 1;
816 my @argument_columns;
817 if(!$self->parse_c_function_call(\$_, \$line, \$column, \$name, \@arguments, \@argument_lines, \@argument_columns)) {
821 if(&$$found_function_call($begin_line, $begin_column, $line, $column, $name, \@arguments))
823 while(defined(my $argument = shift @arguments) &&
824 defined(my $argument_line = shift @argument_lines) &&
825 defined(my $argument_column = shift @argument_columns))
827 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
835 $self->_update_c_position($_, \$line, \$column);
839 $$refcolumn = $column;
844 ########################################################################
847 sub parse_c_file($$$$) {
850 my $found_comment = \${$self->{FOUND_COMMENT}};
851 my $found_line = \${$self->{FOUND_LINE}};
853 my $refcurrent = shift;
855 my $refcolumn = shift;
857 local $_ = $$refcurrent;
858 my $line = $$refline;
859 my $column = $$refcolumn;
861 my $declaration = "";
862 my $declaration_line = $line;
863 my $declaration_column = $column;
865 my $previous_line = 0;
866 my $previous_column = -1;
868 my $preprocessor_condition;
875 while($plevel > 0 || $blevel > 0) {
877 $self->_parse_c_until_one_of("#/\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
879 if($line != $previous_line) {
880 &$$found_line($line);
881 } elsif(0 && $column == $previous_column) {
882 $self->_parse_c_error($_, $line, $column, "file", "no progress");
884 # &$$found_line("$line.$column");
886 $previous_line = $line;
887 $previous_column = $column;
889 if($match !~ /^\s+$/s && $options->debug) {
890 $self->_parse_c_warning($_, $line, $column, "file", "$plevel $blevel: '$declaration' '$match'");
893 if(!$declaration && $match =~ s/^\s+//s) {
894 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
898 $declaration .= $match;
901 if ($declaration =~ s/^extern\s*\"C\"//s) {
903 $self->_update_c_position($&, \$line, \$column);
905 $declaration_line = $line;
906 $declaration_column = $column;
911 } elsif ($extern_c && $blevel == 1 && $plevel == 1 && !$declaration) {
913 $self->_update_c_position($&, \$line, \$column);
915 $declaration_line = $line;
916 $declaration_column = $column;
921 } elsif($declaration =~ s/^(?:__DEFINE_(?:GET|SET)_SEG|OUR_GUID_ENTRY)\s*(?=\()//sx) { # FIXME: Wine specific kludge
923 if ($plevel > 2 || !s/^\)//) {
924 $declaration = "$prefix$declaration";
927 $self->_update_c_position($&, \$line, \$column);
932 my @argument_columns;
934 if(!$self->parse_c_tuple(\$declaration, \$declaration_line, \$declaration_column,
935 \@arguments, \@argument_lines, \@argument_columns))
937 $self->_parse_c_error($declaration, $declaration_line, $declaration_column, "file", "tuple expected");
941 $declaration_line = $line;
942 $declaration_column = $column;
946 } elsif ($declaration =~ s/^(?:DEFINE_SHLGUID)\s*\(.*?\)//s) {
947 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
948 } elsif ($declaration =~ s/^(?:DECL_WINELIB_TYPE_AW|DECLARE_HANDLE(?:16)?|TYPE_MARSHAL)\(\s*(\w+)\s*\)\s*//s) {
949 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
950 } elsif ($declaration =~ s/^ICOM_DEFINE\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*//s) {
951 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
957 while(s/^.*?\n//) { $blank_lines++; }
960 $declaration_line = $line;
961 $declaration_column = $column;
963 $declaration .= "\n" x $blank_lines;
971 my $preprocessor_line = $line;
972 my $preprocessor_column = $column;
974 my $preprocessor = $&;
975 while(s/^(.*?)\\\s*\n//) {
977 $preprocessor .= "$1\n";
979 if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
982 $preprocessor .= "$1$3";
986 } elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
996 if($preprocessor =~ /^\#\s*if/) {
997 if($preprocessor =~ /^\#\s*if\s*0/) {
1002 if($preprocessor =~ /^\#\s*ifdef\s+WORDS_BIGENDIAN$/) {
1003 $preprocessor_condition = "defined(WORD_BIGENDIAN)";
1004 # $output->write("'$preprocessor_condition':'$declaration'\n")
1006 $preprocessor_condition = "";
1009 } elsif($preprocessor =~ /^\#\s*else/) {
1010 if ($preprocessor_condition ne "") {
1011 $preprocessor_condition =~ "!$preprocessor_condition";
1012 $preprocessor_condition =~ s/^!!/!/;
1013 # $output->write("'$preprocessor_condition':'$declaration'\n")
1015 } elsif($preprocessor =~ /^\#\s*endif/) {
1023 if ($preprocessor_condition ne "") {
1024 # $output->write("'$preprocessor_condition':'$declaration'\n");
1025 $preprocessor_condition = "";
1030 if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
1035 if(s/^\/\*.*?\*\///s) {
1036 &$$found_comment($line, $column + 1, $&);
1042 $column += length($_);
1044 } elsif(s/^\/\/(.*?)\n//) {
1045 &$$found_comment($line, $column + 1, $&);
1054 $line += $blank_lines;
1055 if($blank_lines > 0) {
1060 $declaration_line = $line;
1061 $declaration_column = $column;
1062 } elsif($blank_lines > 0) {
1063 $declaration .= "\n" x $blank_lines;
1085 $self->_parse_c_error($_, $line, $column, "file", ") without (");
1088 if($plevel == 1 && $declaration =~ /^__ASM_GLOBAL_FUNC/) {
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;
1103 $self->_parse_c_error($_, $line, $column, "file", "} without {");
1108 if($declaration =~ /^typedef/s ||
1109 $declaration =~ /^(?:const\s+|extern\s+|static\s+|volatile\s+)*(?:interface|struct|union)(?:\s+\w+)?\s*\{/s)
1112 } elsif($plevel == 1 && $blevel == 1) {
1113 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1116 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1118 $declaration_line = $line;
1119 $declaration_column = $column;
1120 } elsif($column == 1 && !$extern_c) {
1121 $self->_parse_c_error("", $line, $column, "file", "inner } ends on column 1");
1125 if(0 && $blevel == 1 &&
1126 $declaration !~ /^typedef/ &&
1127 $declaration !~ /^(?:const\s+|extern\s+|static\s+|volatile\s+)?(?:interface|struct|union)(?:\s+\w+)?\s*\{/s &&
1128 $declaration =~ /^(?:\w+(?:\s*\*)*\s+)*(\w+)\s*\(\s*(?:(?:\w+\s*,\s*)*(\w+))?\s*\)\s*(.*?);$/s &&
1129 $1 ne "ICOM_VTABLE" && defined($2) && $2 ne "void" && $3) # K&R
1131 $self->_parse_c_warning("", $line, $column, "file", "function $1: warning: function has K&R format");
1132 } elsif($plevel == 1 && $blevel == 1) {
1133 $declaration =~ s/\s*;$//;
1134 if($declaration && !$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1137 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1139 $declaration_line = $line;
1140 $declaration_column = $column;
1142 } elsif(/^\s*$/ && $declaration =~ /^\s*$/ && $match =~ /^\s*$/) {
1146 $self->_parse_c_error($_, $line, $column, "file", "parse error: '$declaration' '$match'");
1152 $$refcolumn = $column;
1157 ########################################################################
1160 sub parse_c_function($$$$$) {
1163 my $file = \${$self->{FILE}};
1164 my $create_function = \${$self->{CREATE_FUNCTION}};
1166 my $refcurrent = shift;
1167 my $refline = shift;
1168 my $refcolumn = shift;
1170 my $reffunction = shift;
1172 local $_ = $$refcurrent;
1173 my $line = $$refline;
1174 my $column = $$refcolumn;
1177 my $calling_convention = "";
1182 my @argument_columns;
1184 my $statements_line;
1185 my $statements_column;
1187 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1189 my $begin_line = $line;
1190 my $begin_column = $column + 1;
1192 if($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
1196 # $self->_parse_c_warning($_, $line, $column, "function", "");
1199 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1200 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1201 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1202 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1203 \$_, \$line, \$column, \$match))
1205 if($match =~ /^(?:extern|static)$/) {
1212 if($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1214 } elsif($self->_parse_c('WINE_EXCEPTION_FILTER\(\w+\)', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1217 if(!$self->parse_c_type(\$_, \$line, \$column, \$return_type)) {
1221 $self->_parse_c('inline|FAR', \$_, \$line, \$column);
1223 $self->_parse_c($CALL_CONVENTION,
1224 \$_, \$line, \$column, \$calling_convention);
1227 # FIXME: ???: Old variant of __attribute((const))
1228 $self->_parse_c('(?:const|volatile)', \$_, \$line, \$column);
1230 if(!$self->_parse_c('(?:operator\s*!=|(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)', \$_, \$line, \$column, \$name)) {
1236 $self->_update_c_position($&, \$line, \$column);
1240 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1246 $self->_update_c_position($&, \$line, \$column);
1248 $self->_parse_c_error($_, $line, $column, "function");
1254 if($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
1259 # FIXME: Implement proper handling of K&R C functions
1260 $self->_parse_c_until_one_of("{", \$_, \$line, \$column, $kar);
1263 $output->write("K&R: $kar\n");
1266 if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1270 my $end_line = $line;
1271 my $end_column = $column;
1275 $$refcolumn = $column;
1277 my $function = &$$create_function;
1279 $function->file($$file);
1280 $function->begin_line($begin_line);
1281 $function->begin_column($begin_column);
1282 $function->end_line($end_line);
1283 $function->end_column($end_column);
1284 $function->linkage($linkage);
1285 $function->return_type($return_type);
1286 $function->calling_convention($calling_convention);
1287 $function->name($name);
1288 # if(defined($argument_types)) {
1289 # $function->argument_types([@$argument_types]);
1291 # if(defined($argument_names)) {
1292 # $function->argument_names([@$argument_names]);
1294 $function->statements_line($statements_line);
1295 $function->statements_column($statements_column);
1296 $function->statements($statements);
1298 $$reffunction = $function;
1303 ########################################################################
1304 # parse_c_function_call
1306 sub parse_c_function_call($$$$$$$$) {
1309 my $refcurrent = shift;
1310 my $refline = shift;
1311 my $refcolumn = shift;
1313 my $refname = shift;
1314 my $refarguments = shift;
1315 my $refargument_lines = shift;
1316 my $refargument_columns = shift;
1318 local $_ = $$refcurrent;
1319 my $line = $$refline;
1320 my $column = $$refcolumn;
1325 my @argument_columns;
1327 if(s/^(\w+)(\s*)(?=\()//s) {
1328 $self->_update_c_position($&, \$line, \$column);
1332 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1341 $$refcolumn = $column;
1344 @$refarguments = @arguments;
1345 @$refargument_lines = @argument_lines;
1346 @$refargument_columns = @argument_columns;
1351 ########################################################################
1352 # parse_c_preprocessor
1354 sub parse_c_preprocessor($$$$) {
1357 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
1359 my $refcurrent = shift;
1360 my $refline = shift;
1361 my $refcolumn = shift;
1363 local $_ = $$refcurrent;
1364 my $line = $$refline;
1365 my $column = $$refcolumn;
1367 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1369 my $begin_line = $line;
1370 my $begin_column = $column + 1;
1372 if(!&$$found_preprocessor($begin_line, $begin_column, "$_")) {
1376 if(/^\#\s*define\s*(.*?)$/s) {
1377 $self->_update_c_position($_, \$line, \$column);
1378 } elsif(/^\#\s*else/s) {
1379 $self->_update_c_position($_, \$line, \$column);
1380 } elsif(/^\#\s*endif/s) {
1381 $self->_update_c_position($_, \$line, \$column);
1382 } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1383 $self->_update_c_position($_, \$line, \$column);
1384 } elsif(/^\#\s*include\s+(.*?)$/s) {
1385 $self->_update_c_position($_, \$line, \$column);
1386 } elsif(/^\#\s*undef\s+(.*?)$/s) {
1387 $self->_update_c_position($_, \$line, \$column);
1389 $self->_parse_c_error($_, $line, $column, "preprocessor");
1394 $$refcolumn = $column;
1399 ########################################################################
1402 sub parse_c_statement($$$$) {
1405 my $refcurrent = shift;
1406 my $refline = shift;
1407 my $refcolumn = shift;
1409 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1411 local $_ = $$refcurrent;
1412 my $line = $$refline;
1413 my $column = $$refcolumn;
1415 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1417 $self->_parse_c('(?:case\s+)?(\w+)\s*:\s*', \$_, \$line, \$column);
1419 # $output->write("$line.$column: statement: '$_'\n");
1425 my $statements_line;
1426 my $statements_column;
1427 if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1430 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1433 } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
1434 $self->_update_c_position($&, \$line, \$column);
1440 my @argument_columns;
1441 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1445 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1446 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1449 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1451 while(defined(my $argument = shift @arguments) &&
1452 defined(my $argument_line = shift @argument_lines) &&
1453 defined(my $argument_column = shift @argument_columns))
1455 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1457 } elsif(s/^else//) {
1458 $self->_update_c_position($&, \$line, \$column);
1459 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1462 } elsif(s/^return//) {
1463 $self->_update_c_position($&, \$line, \$column);
1464 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1465 if(!$self->parse_c_expression(\$_, \$line, \$column)) {
1468 } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1471 # $self->_parse_c_error($_, $line, $column, "statement");
1474 $self->_update_c_position($_, \$line, \$column);
1478 $$refcolumn = $column;
1483 ########################################################################
1484 # parse_c_statements
1486 sub parse_c_statements($$$$) {
1489 my $refcurrent = shift;
1490 my $refline = shift;
1491 my $refcolumn = shift;
1493 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1495 local $_ = $$refcurrent;
1496 my $line = $$refline;
1497 my $column = $$refcolumn;
1499 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1501 # $output->write("$line.$column: statements: '$_'\n");
1504 my $statement_line = $line;
1505 my $statement_column = $column;
1507 my $previous_line = -1;
1508 my $previous_column = -1;
1512 while($plevel > 0 || $blevel > 0) {
1514 $self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1516 if($previous_line == $line && $previous_column == $column) {
1517 $self->_parse_c_error($_, $line, $column, "statements", "no progress");
1519 $previous_line = $line;
1520 $previous_column = $column;
1522 # $output->write("'$match' '$_'\n");
1524 $statement .= $match;
1529 } elsif(s/^[\)\]]//) {
1532 $self->_parse_c_error($_, $line, $column, "statements");
1542 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1545 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1547 $statement_line = $line;
1548 $statement_column = $column;
1551 if($plevel == 1 && $blevel == 1) {
1552 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1556 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1558 $statement_line = $line;
1559 $statement_column = $column;
1563 } elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
1567 $self->_parse_c_error($_, $line, $column, "statements");
1571 $self->_update_c_position($_, \$line, \$column);
1575 $$refcolumn = $column;
1580 ########################################################################
1581 # parse_c_struct_union
1583 sub parse_c_struct_union($$$$$$$$$) {
1586 my $refcurrent = shift;
1587 my $refline = shift;
1588 my $refcolumn = shift;
1590 my $refkind = shift;
1591 my $ref_name = shift;
1592 my $reffield_type_names = shift;
1593 my $reffield_names = shift;
1594 my $refnames = shift;
1596 local $_ = $$refcurrent;
1597 my $line = $$refline;
1598 my $column = $$refcolumn;
1602 my @field_type_names = ();
1603 my @field_names = ();
1606 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1608 if (!s/^(interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1614 $self->_update_c_position($&, \$line, \$column);
1619 while ($_ && $self->_parse_c_on_same_level_until_one_of(';', \$_, \$line, \$column, \$match))
1622 my $field_type_name;
1625 if ($self->parse_c_variable(\$match, \$line, \$column, \$field_linkage, \$field_type_name, \$field_name)) {
1626 $field_type_name =~ s/\s+/ /g;
1628 push @field_type_names, $field_type_name;
1629 push @field_names, $field_name;
1630 # $output->write("$kind:$_name:$field_type_name:$field_name\n");
1632 $self->_parse_c_error($_, $line, $column, "typedef $kind: '$match'");
1635 if ($self->_parse_c(';', \$_, \$line, \$column)) {
1637 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1640 my $tuple_line = $line;
1641 my $tuple_column = $column - 1;
1645 my @argument_columns;
1647 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1648 \@arguments, \@argument_lines, \@argument_columns))
1650 $self->_parse_c_error($_, $line, $column, "$kind");
1653 foreach my $argument (@arguments) {
1654 my $name = $argument;
1661 $self->_parse_c_error($_, $line, $column, "$kind");
1667 $$refcolumn = $column;
1670 $$ref_name = $_name;
1671 @$reffield_type_names = @field_type_names;
1672 @$reffield_names = @field_names;
1673 @$refnames = @names;
1678 ########################################################################
1681 sub parse_c_tuple($$$$$$$) {
1684 my $refcurrent = shift;
1685 my $refline = shift;
1686 my $refcolumn = shift;
1688 # FIXME: Should not write directly
1690 my $item_lines = shift;
1691 my $item_columns = shift;
1693 local $_ = $$refcurrent;
1695 my $line = $$refline;
1696 my $column = $$refcolumn;
1706 my $item_line = $line;
1707 my $item_column = $column + 1;
1710 while($plevel > 0) {
1712 $self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1720 push @$item_lines, $item_line;
1721 push @$item_columns, $item_column;
1722 push @$items, $item;
1732 push @$item_lines, $item_line;
1733 push @$item_columns, $item_column;
1734 push @$items, $item;
1735 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1737 $item_column = $column + 1;
1749 $$refcolumn = $column;
1754 ########################################################################
1757 sub parse_c_type($$$$$) {
1760 my $refcurrent = shift;
1761 my $refline = shift;
1762 my $refcolumn = shift;
1764 my $reftype = shift;
1766 local $_ = $$refcurrent;
1767 my $line = $$refline;
1768 my $column = $$refcolumn;
1772 $self->_parse_c("(?:const|volatile)", \$_, \$line, \$column);
1774 if($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1776 } elsif($self->_parse_c('(?:enum\s+|interface\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1777 \$_, \$line, \$column, \$type))
1787 $$refcolumn = $column;
1794 ########################################################################
1797 sub parse_c_typedef($$$$) {
1800 my $create_type = \${$self->{CREATE_TYPE}};
1801 my $found_type = \${$self->{FOUND_TYPE}};
1802 my $preprocessor_condition = \${$self->{PREPROCESSOR_CONDITION}};
1804 my $refcurrent = shift;
1805 my $refline = shift;
1806 my $refcolumn = shift;
1808 local $_ = $$refcurrent;
1809 my $line = $$refline;
1810 my $column = $$refcolumn;
1814 if (!$self->_parse_c("typedef", \$_, \$line, \$column)) {
1822 } elsif ($self->parse_c_enum(\$_, \$line, \$column)) {
1828 my @field_type_names;
1833 } elsif ($self->parse_c_struct_union(\$_, \$line, \$column,
1834 \$kind, \$_name, \@field_type_names, \@field_names, \@names))
1837 foreach my $name (@names)
1839 if ($name =~ /^\w+$/)
1845 $base_name="$kind $_name" if (!defined $base_name and defined $_name);
1846 $base_name=$kind if (!defined $base_name);
1847 foreach my $name (@names) {
1848 if ($name =~ /^\w+$/) {
1849 my $type = &$$create_type();
1852 $type->_name($_name);
1854 $type->field_type_names([@field_type_names]);
1855 $type->field_names([@field_names]);
1857 &$$found_type($type);
1858 } elsif ($name =~ /^(\*+)\s*(?:RESTRICTED_POINTER\s+)?(\w+)$/) {
1859 my $type_name = "$base_name $1";
1862 my $type = &$$create_type();
1866 $type->field_type_names([$type_name]);
1867 $type->field_names([""]);
1869 &$$found_type($type);
1871 $self->_parse_c_error($_, $line, $column, "typedef 2");
1883 } elsif ($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type_name, \$name)) {
1884 $type_name =~ s/\s+/ /g;
1886 if(defined($type_name) && defined($name)) {
1887 my $type = &$$create_type();
1889 if (length($name) == 0) {
1890 $self->_parse_c_error($_, $line, $column, "typedef");
1895 $type->field_type_names([$type_name]);
1896 $type->field_names([""]);
1898 &$$found_type($type);
1901 if (0 && $_ && !/^,/) {
1902 $self->_parse_c_error($_, $line, $column, "typedef");
1905 $self->_parse_c_error($_, $line, $column, "typedef");
1910 $$refcolumn = $column;
1915 ########################################################################
1918 sub parse_c_variable($$$$$$$) {
1921 my $found_variable = \${$self->{FOUND_VARIABLE}};
1923 my $refcurrent = shift;
1924 my $refline = shift;
1925 my $refcolumn = shift;
1927 my $reflinkage = shift;
1928 my $reftype = shift;
1929 my $refname = shift;
1931 local $_ = $$refcurrent;
1932 my $line = $$refline;
1933 my $column = $$refcolumn;
1935 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1937 my $begin_line = $line;
1938 my $begin_column = $column + 1;
1945 # $self->_parse_c_warning($_, $line, $column, "variable");
1948 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1949 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1950 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1951 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1952 \$_, \$line, \$column, \$match))
1954 if ($match =~ /^(?:extern|static)$/) {
1958 $self->_parse_c_warning($_, $line, $column, "repeated linkage (ignored): $match");
1960 } elsif ($match =~ /^(?:signed|unsigned)$/) {
1964 $self->_parse_c_warning($_, $line, $column, "repeated sign (ignored): $match");
1975 } elsif (s/^(enum\s+|interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1978 $self->_update_c_position($&, \$line, \$column);
1980 if(defined($_name)) {
1981 $type = "$kind $_name { }";
1983 $type = "$kind { }";
1987 } 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) {
2003 $type = $self->_format_c_type($type);
2006 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*)\s*:\s*(\d+)$//s) {
2007 $type = "$sign$1:$2";
2009 $type = $self->_format_c_type($type);
2012 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*\s*\(\s*(?:$CALL_CONVENTION)?(?:\s*\*)*)\s*(\w+)\s*(\)\s*\(.*?\))$//s) {
2013 $type = $self->_format_c_type("$sign$1$3");
2017 } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
2021 $self->_parse_c_warning($_, $line, $column, "variable", "'$_'");
2027 } elsif($self->_parse_c('SEQ_DEFINEBUF', \$_, \$line, \$column, \$match)) { # Linux specific
2030 } elsif($self->_parse_c('DEFINE_REGS_ENTRYPOINT_\w+|DPQ_DECL_\w+|HANDLER_DEF|IX86_ONLY', # Wine specific
2031 \$_, \$line, \$column, \$match))
2035 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
2038 } elsif(s/^(enum|interface|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*//s) {
2041 $self->_update_c_position($&, \$line, \$column);
2043 if(defined($_name)) {
2044 $type = "struct $_name { }";
2046 $type = "struct { }";
2048 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
2055 # $output->write("*** $type: '$_'\n");
2057 # $self->_parse_c_warning($_, $line, $column, "variable2", "");
2061 } elsif(s/^WINAPI\s*//) {
2062 $self->_update_c_position($&, \$line, \$column);
2067 } elsif(s/^(\((?:$CALL_CONVENTION)?\s*\*?\s*(?:$CALL_CONVENTION)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
2068 $self->_update_c_position($&, \$line, \$column);
2073 $self->_parse_c_until_one_of("\\)", \$_, \$line, \$column);
2074 if(s/^\)//) { $column++; }
2075 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
2077 if(!s/^(?:=\s*|,\s*|$)//) {
2080 } elsif(s/^(?:\*\s*)*(?:const\s+|volatile\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
2081 $self->_update_c_position($&, \$line, \$column);
2091 # $output->write("$type: $name: '$_'\n");
2095 $$refcolumn = $column;
2097 $$reflinkage = $linkage;
2101 if(&$$found_variable($begin_line, $begin_column, $linkage, $type, $name))