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 $output->write("$$file:$line." . ($column + 1) . ": $context: $message: \\\n$current");
248 $output->write("$$file:$line." . ($column + 1) . ": $context: $message\n");
252 ########################################################################
255 sub _parse_c_error($$$$$$) {
264 $message = "parse error" if !$message;
267 if($output->prefix) {
268 # $output->write("\n");
272 $self->_parse_c_warning($_, $line, $column, $context, $message);
277 ########################################################################
280 sub _update_c_position($$$$) {
285 my $refcolumn = shift;
287 my $line = $$refline;
288 my $column = $$refcolumn;
291 if(s/^[^\n\t\'\"]*//s) {
292 $column += length($&);
297 while(/^./ && !s/^\'//) {
299 $column += length($1);
303 $column += length($1);
306 $column += length($1);
314 while(/^./ && !s/^\"//) {
316 $column += length($1);
320 $column += length($1);
323 $column += length($1);
333 $column = $column + 8 - $column % 8;
338 $$refcolumn = $column;
341 ########################################################################
342 # __parse_c_until_one_of
344 sub __parse_c_until_one_of($$$$$$$) {
347 my $characters = shift;
348 my $on_same_level = shift;
349 my $refcurrent = shift;
351 my $refcolumn = shift;
354 local $_ = $$refcurrent;
355 my $line = $$refline;
356 my $column = $$refcolumn;
359 if(!defined($match)) {
361 $match = \$blackhole;
366 while(/^[^$characters]/s || $level > 0) {
370 if(s/^[^\(\)\[\]\{\}\n\t\'\"]*//s) {
373 } elsif ($on_same_level) {
374 if(s/^[^$characters\(\)\[\]\{\}\n\t\'\"]*//s) {
378 if(s/^[^$characters\n\t\'\"]*//s) {
385 while(/^./ && !s/^\'//) {
401 $$match .= $submatch;
402 $column += length($submatch);
405 while(/^./ && !s/^\"//) {
421 $$match .= $submatch;
422 $column += length($submatch);
423 } elsif($on_same_level && s/^[\(\[\{]//) {
427 $$match .= $submatch;
429 } elsif($on_same_level && s/^[\)\]\}]//) {
434 $$match .= $submatch;
438 $$match .= $submatch;
444 $$match .= $submatch;
450 $$match .= $submatch;
451 $column = $column + 8 - $column % 8;
453 $$match .= $submatch;
454 $column += length($submatch);
460 $$refcolumn = $column;
464 ########################################################################
465 # _parse_c_until_one_of
467 sub _parse_c_until_one_of($$$$$$) {
470 my $characters = shift;
471 my $refcurrent = shift;
473 my $refcolumn = shift;
476 return $self->__parse_c_until_one_of($characters, 0, $refcurrent, $refline, $refcolumn, $match);
479 ########################################################################
480 # _parse_c_on_same_level_until_one_of
482 sub _parse_c_on_same_level_until_one_of($$$$$$) {
485 my $characters = shift;
486 my $refcurrent = shift;
488 my $refcolumn = shift;
491 return $self->__parse_c_until_one_of($characters, 1, $refcurrent, $refline, $refcolumn, $match);
494 ########################################################################
497 sub parse_c_block($$$$$$$) {
500 my $refcurrent = shift;
502 my $refcolumn = shift;
504 my $refstatements = shift;
505 my $refstatements_line = shift;
506 my $refstatements_column = shift;
508 local $_ = $$refcurrent;
509 my $line = $$refline;
510 my $column = $$refcolumn;
512 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
522 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
524 my $statements_line = $line;
525 my $statements_column = $column;
530 $self->_parse_c_until_one_of("\\{\\}", \$_, \$line, \$column, \$match);
534 $statements .= $match;
550 $$refcolumn = $column;
551 $$refstatements = $statements;
552 $$refstatements_line = $statements_line;
553 $$refstatements_column = $statements_column;
558 ########################################################################
559 # parse_c_declaration
561 sub parse_c_declaration($$$$)
563 my ($self, $refcurrent, $refline, $refcolumn) = @_;
565 my $found_declaration = \${$self->{FOUND_DECLARATION}};
566 my $found_function = \${$self->{FOUND_FUNCTION}};
568 local $_ = $$refcurrent;
569 my $line = $$refline;
570 my $column = $$refcolumn;
572 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
574 my $begin_line = $line;
575 my $begin_column = $column + 1;
577 my $end_line = $begin_line;
578 my $end_column = $begin_column;
579 $self->_update_c_position($_, \$end_line, \$end_column);
581 if(!&$$found_declaration($begin_line, $begin_column, $end_line, $end_column, $_)) {
589 my ($linkage, $type, $name);
591 if(s/^WINE_(?:DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\(\s*(\w+)\s*\)\s*//s) { # FIXME: Wine specific kludge
592 $self->_update_c_position($&, \$line, \$column);
593 } elsif(s/^__ASM_GLOBAL_FUNC\(\s*(\w+)\s*,\s*//s) { # FIXME: Wine specific kludge
594 $self->_update_c_position($&, \$line, \$column);
595 $self->_parse_c_until_one_of("\)", \$_, \$line, \$column);
599 } elsif(s/^(?:DEFINE_AVIGUID|DEFINE_OLEGUID)\s*(?=\()//s) { # FIXME: Wine specific kludge
600 $self->_update_c_position($&, \$line, \$column);
604 my @argument_columns;
606 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
609 } elsif(s/^DEFINE_COMMON_NOTIFICATIONS\(\s*(\w+)\s*,\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
610 $self->_update_c_position($&, \$line, \$column);
611 } elsif(s/^MAKE_FUNCPTR\(\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
612 $self->_update_c_position($&, \$line, \$column);
613 } elsif(s/^START_TEST\(\s*(\w+)\s*\)\s*{//s) { # FIXME: Wine specific kludge
614 $self->_update_c_position($&, \$line, \$column);
615 } elsif(s/^int\s*_FUNCTION_\s*{//s) { # FIXME: Wine specific kludge
616 $self->_update_c_position($&, \$line, \$column);
617 } elsif(s/^(?:jump|strong)_alias//s) { # FIXME: GNU C library specific kludge
618 $self->_update_c_position($&, \$line, \$column);
619 } elsif(s/^(?:__asm__|asm)\s*\(//) {
620 $self->_update_c_position($&, \$line, \$column);
621 } elsif($self->parse_c_typedef(\$_, \$line, \$column)) {
623 } elsif($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type, \$name)) {
625 } elsif($self->parse_c_function(\$_, \$line, \$column, \$function)) {
626 if(&$$found_function($function))
628 my $statements = $function->statements;
629 my $statements_line = $function->statements_line;
630 my $statements_column = $function->statements_column;
632 if(defined($statements)) {
633 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
639 $self->_parse_c_error($_, $line, $column, "declaration");
644 $$refcolumn = $column;
649 ########################################################################
652 sub _parse_c($$$$$$) {
656 my $refcurrent = shift;
658 my $refcolumn = shift;
660 my $refmatch = shift;
662 local $_ = $$refcurrent;
663 my $line = $$refline;
664 my $column = $$refcolumn;
667 if(s/^(?:$pattern)//s) {
668 $self->_update_c_position($&, \$line, \$column);
674 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
678 $$refcolumn = $column;
685 ########################################################################
688 sub parse_c_enum($$$$) {
691 my $refcurrent = shift;
693 my $refcolumn = shift;
695 local $_ = $$refcurrent;
696 my $line = $$refline;
697 my $column = $$refcolumn;
699 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
701 if (!s/^enum\s+((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
704 my $_name = $1 || "";
706 $self->_update_c_position($&, \$line, \$column);
711 while ($self->_parse_c_on_same_level_until_one_of(',', \$_, \$line, \$column, \$match)) {
713 if ($match !~ /^(\w+)\s*(?:=\s*(.*?)\s*)?$/) {
714 $self->_parse_c_error($_, $line, $column, "enum");
717 my $enum_value = $2 || "";
719 # $output->write("enum:$_name:$enum_name:$enum_value\n");
722 if ($self->_parse_c(',', \$_, \$line, \$column)) {
724 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
727 my $tuple_line = $line;
728 my $tuple_column = $column - 1;
732 my @argument_columns;
734 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
735 \@arguments, \@argument_lines, \@argument_columns))
737 $self->_parse_c_error($_, $line, $column, "enum");
741 if ($#arguments >= 0) {
742 $name = $arguments[0];
747 $self->_parse_c_error($_, $line, $column, "enum");
751 $self->_update_c_position($_, \$line, \$column);
755 $$refcolumn = $column;
759 ########################################################################
762 sub parse_c_expression($$$$) {
765 my $refcurrent = shift;
767 my $refcolumn = shift;
769 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
771 local $_ = $$refcurrent;
772 my $line = $$refline;
773 my $column = $$refcolumn;
775 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
778 if(s/^(.*?)(\w+\s*\()/$2/s) {
779 $self->_update_c_position($1, \$line, \$column);
781 my $begin_line = $line;
782 my $begin_column = $column + 1;
787 my @argument_columns;
788 if(!$self->parse_c_function_call(\$_, \$line, \$column, \$name, \@arguments, \@argument_lines, \@argument_columns)) {
792 if(&$$found_function_call($begin_line, $begin_column, $line, $column, $name, \@arguments))
794 while(defined(my $argument = shift @arguments) &&
795 defined(my $argument_line = shift @argument_lines) &&
796 defined(my $argument_column = shift @argument_columns))
798 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
806 $self->_update_c_position($_, \$line, \$column);
810 $$refcolumn = $column;
815 ########################################################################
818 sub parse_c_file($$$$) {
821 my $found_comment = \${$self->{FOUND_COMMENT}};
822 my $found_line = \${$self->{FOUND_LINE}};
824 my $refcurrent = shift;
826 my $refcolumn = shift;
828 local $_ = $$refcurrent;
829 my $line = $$refline;
830 my $column = $$refcolumn;
832 my $declaration = "";
833 my $declaration_line = $line;
834 my $declaration_column = $column;
836 my $previous_line = 0;
837 my $previous_column = -1;
839 my $preprocessor_condition;
846 while($plevel > 0 || $blevel > 0) {
848 $self->_parse_c_until_one_of("#/\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
850 if($line != $previous_line) {
851 &$$found_line($line);
853 # &$$found_line("$line.$column");
855 $previous_line = $line;
856 $previous_column = $column;
858 if($match !~ /^\s+$/s && $options->debug) {
859 $self->_parse_c_warning($_, $line, $column, "file", "$plevel $blevel: '$declaration' '$match'");
862 if(!$declaration && $match =~ s/^\s+//s) {
863 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
867 $declaration .= $match;
870 if ($declaration =~ s/^extern\s*\"C\"//s) {
872 $self->_update_c_position($&, \$line, \$column);
874 $declaration_line = $line;
875 $declaration_column = $column;
880 } elsif ($extern_c && $blevel == 1 && $plevel == 1 && !$declaration) {
882 $self->_update_c_position($&, \$line, \$column);
884 $declaration_line = $line;
885 $declaration_column = $column;
890 } elsif($declaration =~ s/^(?:__DEFINE_(?:GET|SET)_SEG|OUR_GUID_ENTRY)\s*(?=\()//sx) { # FIXME: Wine specific kludge
892 if ($plevel > 2 || !s/^\)//) {
893 $declaration = "$prefix$declaration";
896 $self->_update_c_position($&, \$line, \$column);
901 my @argument_columns;
903 if(!$self->parse_c_tuple(\$declaration, \$declaration_line, \$declaration_column,
904 \@arguments, \@argument_lines, \@argument_columns))
906 $self->_parse_c_error($declaration, $declaration_line, $declaration_column, "file", "tuple expected");
910 $declaration_line = $line;
911 $declaration_column = $column;
915 } elsif ($declaration =~ s/^(?:DEFINE_SHLGUID)\s*\(.*?\)//s) {
916 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
917 } elsif ($declaration =~ s/^(?:DECL_WINELIB_TYPE_AW|DECLARE_HANDLE(?:16)?|TYPE_MARSHAL)\(\s*(\w+)\s*\)\s*//s) {
918 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
919 } elsif ($declaration =~ s/^ICOM_DEFINE\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*//s) {
920 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
926 while(s/^.*?\n//) { $blank_lines++; }
929 $declaration_line = $line;
930 $declaration_column = $column;
932 $declaration .= "\n" x $blank_lines;
940 my $preprocessor_line = $line;
941 my $preprocessor_column = $column;
943 my $preprocessor = $&;
944 while(s/^(.*?)\\\s*\n//) {
946 $preprocessor .= "$1\n";
948 if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
951 $preprocessor .= "$1$3";
955 } elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
965 if($preprocessor =~ /^\#\s*if/) {
966 if($preprocessor =~ /^\#\s*if\s*0/) {
971 if($preprocessor =~ /^\#\s*ifdef\s+WORDS_BIGENDIAN$/) {
972 $preprocessor_condition = "defined(WORD_BIGENDIAN)";
973 # $output->write("'$preprocessor_condition':'$declaration'\n")
975 $preprocessor_condition = "";
978 } elsif($preprocessor =~ /^\#\s*else/) {
979 if ($preprocessor_condition ne "") {
980 $preprocessor_condition =~ "!$preprocessor_condition";
981 $preprocessor_condition =~ s/^!!/!/;
982 # $output->write("'$preprocessor_condition':'$declaration'\n")
984 } elsif($preprocessor =~ /^\#\s*endif/) {
992 if ($preprocessor_condition ne "") {
993 # $output->write("'$preprocessor_condition':'$declaration'\n");
994 $preprocessor_condition = "";
999 if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
1004 if(s/^\/\*.*?\*\///s) {
1005 &$$found_comment($line, $column + 1, $&);
1011 $column += length($_);
1013 } elsif(s/^\/\/(.*?)\n//) {
1014 &$$found_comment($line, $column + 1, $&);
1023 $line += $blank_lines;
1024 if($blank_lines > 0) {
1029 $declaration_line = $line;
1030 $declaration_column = $column;
1031 } elsif($blank_lines > 0) {
1032 $declaration .= "\n" x $blank_lines;
1054 $self->_parse_c_error($_, $line, $column, "file", ") without (");
1057 if($plevel == 1 && $declaration =~ /^__ASM_GLOBAL_FUNC/) {
1058 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1061 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1063 $declaration_line = $line;
1064 $declaration_column = $column;
1072 $self->_parse_c_error($_, $line, $column, "file", "} without {");
1077 if($declaration =~ /^typedef/s ||
1078 $declaration =~ /^(?:const\s+|extern\s+|static\s+|volatile\s+)*(?:interface|struct|union)(?:\s+\w+)?\s*\{/s)
1081 } elsif($plevel == 1 && $blevel == 1) {
1082 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1085 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1087 $declaration_line = $line;
1088 $declaration_column = $column;
1089 } elsif($column == 1 && !$extern_c) {
1090 $self->_parse_c_error("", $line, $column, "file", "inner } ends on column 1");
1094 if($plevel == 1 && $blevel == 1) {
1095 $declaration =~ s/\s*;$//;
1096 if($declaration && !$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1099 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1101 $declaration_line = $line;
1102 $declaration_column = $column;
1104 } elsif(/^\s*$/ && $declaration =~ /^\s*$/ && $match =~ /^\s*$/) {
1108 $self->_parse_c_error($_, $line, $column, "file", "parse error: '$declaration' '$match'");
1114 $$refcolumn = $column;
1119 ########################################################################
1122 sub parse_c_function($$$$$) {
1125 my $file = \${$self->{FILE}};
1126 my $create_function = \${$self->{CREATE_FUNCTION}};
1128 my $refcurrent = shift;
1129 my $refline = shift;
1130 my $refcolumn = shift;
1132 my $reffunction = shift;
1134 local $_ = $$refcurrent;
1135 my $line = $$refline;
1136 my $column = $$refcolumn;
1139 my $calling_convention = "";
1144 my @argument_columns;
1146 my $statements_line;
1147 my $statements_column;
1149 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1151 my $begin_line = $line;
1152 my $begin_column = $column + 1;
1154 if($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
1158 # $self->_parse_c_warning($_, $line, $column, "function", "");
1161 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1162 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1163 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1164 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1165 \$_, \$line, \$column, \$match))
1167 if($match =~ /^(?:extern|static)$/) {
1174 if($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1176 } elsif($self->_parse_c('WINE_EXCEPTION_FILTER\(\w+\)', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1179 if(!$self->parse_c_type(\$_, \$line, \$column, \$return_type)) {
1183 $self->_parse_c('inline|FAR', \$_, \$line, \$column);
1185 $self->_parse_c($CALL_CONVENTION,
1186 \$_, \$line, \$column, \$calling_convention);
1189 # FIXME: ???: Old variant of __attribute((const))
1190 $self->_parse_c('(?:const|volatile)', \$_, \$line, \$column);
1192 if(!$self->_parse_c('(?:operator\s*!=|(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)', \$_, \$line, \$column, \$name)) {
1198 $self->_update_c_position($&, \$line, \$column);
1202 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1208 $self->_update_c_position($&, \$line, \$column);
1210 $self->_parse_c_error($_, $line, $column, "function");
1216 if($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
1221 # FIXME: Implement proper handling of K&R C functions
1222 $self->_parse_c_until_one_of("{", \$_, \$line, \$column, $kar);
1225 $output->write("K&R: $kar\n");
1228 if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1232 my $end_line = $line;
1233 my $end_column = $column;
1237 $$refcolumn = $column;
1239 my $function = &$$create_function;
1241 $function->file($$file);
1242 $function->begin_line($begin_line);
1243 $function->begin_column($begin_column);
1244 $function->end_line($end_line);
1245 $function->end_column($end_column);
1246 $function->linkage($linkage);
1247 $function->return_type($return_type);
1248 $function->calling_convention($calling_convention);
1249 $function->name($name);
1250 # if(defined($argument_types)) {
1251 # $function->argument_types([@$argument_types]);
1253 # if(defined($argument_names)) {
1254 # $function->argument_names([@$argument_names]);
1256 $function->statements_line($statements_line);
1257 $function->statements_column($statements_column);
1258 $function->statements($statements);
1260 $$reffunction = $function;
1265 ########################################################################
1266 # parse_c_function_call
1268 sub parse_c_function_call($$$$$$$$) {
1271 my $refcurrent = shift;
1272 my $refline = shift;
1273 my $refcolumn = shift;
1275 my $refname = shift;
1276 my $refarguments = shift;
1277 my $refargument_lines = shift;
1278 my $refargument_columns = shift;
1280 local $_ = $$refcurrent;
1281 my $line = $$refline;
1282 my $column = $$refcolumn;
1287 my @argument_columns;
1289 if(s/^(\w+)(\s*)(?=\()//s) {
1290 $self->_update_c_position($&, \$line, \$column);
1294 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1303 $$refcolumn = $column;
1306 @$refarguments = @arguments;
1307 @$refargument_lines = @argument_lines;
1308 @$refargument_columns = @argument_columns;
1313 ########################################################################
1314 # parse_c_preprocessor
1316 sub parse_c_preprocessor($$$$) {
1319 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
1321 my $refcurrent = shift;
1322 my $refline = shift;
1323 my $refcolumn = shift;
1325 local $_ = $$refcurrent;
1326 my $line = $$refline;
1327 my $column = $$refcolumn;
1329 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1331 my $begin_line = $line;
1332 my $begin_column = $column + 1;
1334 if(!&$$found_preprocessor($begin_line, $begin_column, "$_")) {
1338 if(/^\#\s*define\s*(.*?)$/s) {
1339 $self->_update_c_position($_, \$line, \$column);
1340 } elsif(/^\#\s*else/s) {
1341 $self->_update_c_position($_, \$line, \$column);
1342 } elsif(/^\#\s*endif/s) {
1343 $self->_update_c_position($_, \$line, \$column);
1344 } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1345 $self->_update_c_position($_, \$line, \$column);
1346 } elsif(/^\#\s*include\s+(.*?)$/s) {
1347 $self->_update_c_position($_, \$line, \$column);
1348 } elsif(/^\#\s*undef\s+(.*?)$/s) {
1349 $self->_update_c_position($_, \$line, \$column);
1351 $self->_parse_c_error($_, $line, $column, "preprocessor");
1356 $$refcolumn = $column;
1361 ########################################################################
1364 sub parse_c_statement($$$$) {
1367 my $refcurrent = shift;
1368 my $refline = shift;
1369 my $refcolumn = shift;
1371 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1373 local $_ = $$refcurrent;
1374 my $line = $$refline;
1375 my $column = $$refcolumn;
1377 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1379 $self->_parse_c('(?:case\s+)?(\w+)\s*:\s*', \$_, \$line, \$column);
1381 # $output->write("$line.$column: statement: '$_'\n");
1387 my $statements_line;
1388 my $statements_column;
1389 if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1392 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1395 } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
1396 $self->_update_c_position($&, \$line, \$column);
1402 my @argument_columns;
1403 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1407 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1408 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1411 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1413 while(defined(my $argument = shift @arguments) &&
1414 defined(my $argument_line = shift @argument_lines) &&
1415 defined(my $argument_column = shift @argument_columns))
1417 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1419 } elsif(s/^else//) {
1420 $self->_update_c_position($&, \$line, \$column);
1421 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1424 } elsif(s/^return//) {
1425 $self->_update_c_position($&, \$line, \$column);
1426 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1427 if(!$self->parse_c_expression(\$_, \$line, \$column)) {
1430 } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1433 # $self->_parse_c_error($_, $line, $column, "statement");
1436 $self->_update_c_position($_, \$line, \$column);
1440 $$refcolumn = $column;
1445 ########################################################################
1446 # parse_c_statements
1448 sub parse_c_statements($$$$) {
1451 my $refcurrent = shift;
1452 my $refline = shift;
1453 my $refcolumn = shift;
1455 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1457 local $_ = $$refcurrent;
1458 my $line = $$refline;
1459 my $column = $$refcolumn;
1461 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1463 # $output->write("$line.$column: statements: '$_'\n");
1466 my $statement_line = $line;
1467 my $statement_column = $column;
1469 my $previous_line = -1;
1470 my $previous_column = -1;
1474 while($plevel > 0 || $blevel > 0) {
1476 $self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1478 if($previous_line == $line && $previous_column == $column) {
1479 $self->_parse_c_error($_, $line, $column, "statements", "no progress");
1481 $previous_line = $line;
1482 $previous_column = $column;
1484 # $output->write("'$match' '$_'\n");
1486 $statement .= $match;
1491 } elsif(s/^[\)\]]//) {
1494 $self->_parse_c_error($_, $line, $column, "statements");
1504 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1507 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1509 $statement_line = $line;
1510 $statement_column = $column;
1513 if($plevel == 1 && $blevel == 1) {
1514 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1518 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1520 $statement_line = $line;
1521 $statement_column = $column;
1525 } elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
1529 $self->_parse_c_error($_, $line, $column, "statements");
1533 $self->_update_c_position($_, \$line, \$column);
1537 $$refcolumn = $column;
1542 ########################################################################
1543 # parse_c_struct_union
1545 sub parse_c_struct_union($$$$$$$$$) {
1548 my $refcurrent = shift;
1549 my $refline = shift;
1550 my $refcolumn = shift;
1552 my $refkind = shift;
1553 my $ref_name = shift;
1554 my $reffield_type_names = shift;
1555 my $reffield_names = shift;
1556 my $refnames = shift;
1558 local $_ = $$refcurrent;
1559 my $line = $$refline;
1560 my $column = $$refcolumn;
1564 my @field_type_names = ();
1565 my @field_names = ();
1568 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1570 if (!s/^(interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1576 $self->_update_c_position($&, \$line, \$column);
1581 while ($_ && $self->_parse_c_on_same_level_until_one_of(';', \$_, \$line, \$column, \$match))
1584 my $field_type_name;
1587 if ($self->parse_c_variable(\$match, \$line, \$column, \$field_linkage, \$field_type_name, \$field_name)) {
1588 $field_type_name =~ s/\s+/ /g;
1590 push @field_type_names, $field_type_name;
1591 push @field_names, $field_name;
1592 # $output->write("$kind:$_name:$field_type_name:$field_name\n");
1594 $self->_parse_c_error($_, $line, $column, "typedef $kind: '$match'");
1597 if ($self->_parse_c(';', \$_, \$line, \$column)) {
1599 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1602 my $tuple_line = $line;
1603 my $tuple_column = $column - 1;
1607 my @argument_columns;
1609 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1610 \@arguments, \@argument_lines, \@argument_columns))
1612 $self->_parse_c_error($_, $line, $column, "$kind");
1615 foreach my $argument (@arguments) {
1616 my $name = $argument;
1623 $self->_parse_c_error($_, $line, $column, "$kind");
1629 $$refcolumn = $column;
1632 $$ref_name = $_name;
1633 @$reffield_type_names = @field_type_names;
1634 @$reffield_names = @field_names;
1635 @$refnames = @names;
1640 ########################################################################
1643 sub parse_c_tuple($$$$$$$) {
1646 my $refcurrent = shift;
1647 my $refline = shift;
1648 my $refcolumn = shift;
1650 # FIXME: Should not write directly
1652 my $item_lines = shift;
1653 my $item_columns = shift;
1655 local $_ = $$refcurrent;
1657 my $line = $$refline;
1658 my $column = $$refcolumn;
1668 my $item_line = $line;
1669 my $item_column = $column + 1;
1672 while($plevel > 0) {
1674 $self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1682 push @$item_lines, $item_line;
1683 push @$item_columns, $item_column;
1684 push @$items, $item;
1694 push @$item_lines, $item_line;
1695 push @$item_columns, $item_column;
1696 push @$items, $item;
1697 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1699 $item_column = $column + 1;
1711 $$refcolumn = $column;
1716 ########################################################################
1719 sub parse_c_type($$$$$) {
1722 my $refcurrent = shift;
1723 my $refline = shift;
1724 my $refcolumn = shift;
1726 my $reftype = shift;
1728 local $_ = $$refcurrent;
1729 my $line = $$refline;
1730 my $column = $$refcolumn;
1734 $self->_parse_c("(?:const|volatile)", \$_, \$line, \$column);
1736 if($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1738 } elsif($self->_parse_c('(?:enum\s+|interface\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1739 \$_, \$line, \$column, \$type))
1749 $$refcolumn = $column;
1756 ########################################################################
1759 sub parse_c_typedef($$$$) {
1762 my $create_type = \${$self->{CREATE_TYPE}};
1763 my $found_type = \${$self->{FOUND_TYPE}};
1764 my $preprocessor_condition = \${$self->{PREPROCESSOR_CONDITION}};
1766 my $refcurrent = shift;
1767 my $refline = shift;
1768 my $refcolumn = shift;
1770 local $_ = $$refcurrent;
1771 my $line = $$refline;
1772 my $column = $$refcolumn;
1776 if (!$self->_parse_c("typedef", \$_, \$line, \$column)) {
1784 } elsif ($self->parse_c_enum(\$_, \$line, \$column)) {
1790 my @field_type_names;
1795 } elsif ($self->parse_c_struct_union(\$_, \$line, \$column,
1796 \$kind, \$_name, \@field_type_names, \@field_names, \@names))
1799 foreach my $name (@names)
1801 if ($name =~ /^\w+$/)
1807 $base_name="$kind $_name" if (!defined $base_name and defined $_name);
1808 $base_name=$kind if (!defined $base_name);
1809 foreach my $name (@names) {
1810 if ($name =~ /^\w+$/) {
1811 my $type = &$$create_type();
1814 $type->_name($_name);
1816 $type->field_type_names([@field_type_names]);
1817 $type->field_names([@field_names]);
1819 &$$found_type($type);
1820 } elsif ($name =~ /^(\*+)\s*(?:RESTRICTED_POINTER\s+)?(\w+)$/) {
1821 my $type_name = "$base_name $1";
1824 my $type = &$$create_type();
1828 $type->field_type_names([$type_name]);
1829 $type->field_names([""]);
1831 &$$found_type($type);
1833 $self->_parse_c_error($_, $line, $column, "typedef 2");
1845 } elsif ($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type_name, \$name)) {
1846 $type_name =~ s/\s+/ /g;
1848 if(defined($type_name) && defined($name)) {
1849 my $type = &$$create_type();
1851 if (length($name) == 0) {
1852 $self->_parse_c_error($_, $line, $column, "typedef");
1857 $type->field_type_names([$type_name]);
1858 $type->field_names([""]);
1860 &$$found_type($type);
1863 $self->_parse_c_error($_, $line, $column, "typedef");
1868 $$refcolumn = $column;
1873 ########################################################################
1876 sub parse_c_variable($$$$$$$) {
1879 my $found_variable = \${$self->{FOUND_VARIABLE}};
1881 my $refcurrent = shift;
1882 my $refline = shift;
1883 my $refcolumn = shift;
1885 my $reflinkage = shift;
1886 my $reftype = shift;
1887 my $refname = shift;
1889 local $_ = $$refcurrent;
1890 my $line = $$refline;
1891 my $column = $$refcolumn;
1893 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1895 my $begin_line = $line;
1896 my $begin_column = $column + 1;
1903 # $self->_parse_c_warning($_, $line, $column, "variable");
1906 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1907 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1908 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1909 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1910 \$_, \$line, \$column, \$match))
1912 if ($match =~ /^(?:extern|static)$/) {
1916 $self->_parse_c_warning($_, $line, $column, "repeated linkage (ignored): $match");
1918 } elsif ($match =~ /^(?:signed|unsigned)$/) {
1922 $self->_parse_c_warning($_, $line, $column, "repeated sign (ignored): $match");
1933 } elsif (s/^(enum\s+|interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1936 $self->_update_c_position($&, \$line, \$column);
1938 if(defined($_name)) {
1939 $type = "$kind $_name { }";
1941 $type = "$kind { }";
1945 } 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) {
1961 $type = $self->_format_c_type($type);
1964 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*)\s*:\s*(\d+)$//s) {
1965 $type = "$sign$1:$2";
1967 $type = $self->_format_c_type($type);
1970 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*\s*\(\s*(?:$CALL_CONVENTION)?(?:\s*\*)*)\s*(\w+)\s*(\)\s*\(.*?\))$//s) {
1971 $type = $self->_format_c_type("$sign$1$3");
1975 } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
1979 $self->_parse_c_warning($_, $line, $column, "variable", "'$_'");
1985 } elsif($self->_parse_c('SEQ_DEFINEBUF', \$_, \$line, \$column, \$match)) { # Linux specific
1988 } elsif($self->_parse_c('DEFINE_REGS_ENTRYPOINT_\w+|DPQ_DECL_\w+|HANDLER_DEF|IX86_ONLY', # Wine specific
1989 \$_, \$line, \$column, \$match))
1993 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
1996 } elsif(s/^(enum|interface|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*//s) {
1999 $self->_update_c_position($&, \$line, \$column);
2001 if(defined($_name)) {
2002 $type = "struct $_name { }";
2004 $type = "struct { }";
2006 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
2013 # $output->write("*** $type: '$_'\n");
2015 # $self->_parse_c_warning($_, $line, $column, "variable2", "");
2019 } elsif(s/^WINAPI\s*//) {
2020 $self->_update_c_position($&, \$line, \$column);
2025 } elsif(s/^(\((?:$CALL_CONVENTION)?\s*\*?\s*(?:$CALL_CONVENTION)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
2026 $self->_update_c_position($&, \$line, \$column);
2031 $self->_parse_c_until_one_of("\\)", \$_, \$line, \$column);
2032 if(s/^\)//) { $column++; }
2033 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
2035 if(!s/^(?:=\s*|,\s*|$)//) {
2038 } elsif(s/^(?:\*\s*)*(?:const\s+|volatile\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
2039 $self->_update_c_position($&, \$line, \$column);
2049 # $output->write("$type: $name: '$_'\n");
2053 $$refcolumn = $column;
2055 $$reflinkage = $linkage;
2059 if(&$$found_variable($begin_line, $begin_column, $linkage, $type, $name))