2 # Copyright 1999, 2000, 2001 Patrik Stridvall
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK);
30 use options
qw($options);
31 use output qw($output);
36 ########################################################################
41 my $class = ref($proto) || $proto;
43 bless ($self, $class);
45 my $file = \${$self->{FILE}};
46 my $create_function = \${$self->{CREATE_FUNCTION}};
47 my $create_type = \${$self->{CREATE_TYPE}};
48 my $found_comment = \${$self->{FOUND_COMMENT}};
49 my $found_declaration = \${$self->{FOUND_DECLARATION}};
50 my $found_function = \${$self->{FOUND_FUNCTION}};
51 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
52 my $found_line = \${$self->{FOUND_LINE}};
53 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
54 my $found_statement = \${$self->{FOUND_STATEMENT}};
55 my $found_type = \${$self->{FOUND_TYPE}};
56 my $found_variable = \${$self->{FOUND_VARIABLE}};
60 $$create_function = sub { return new c_function; };
61 $$create_type = sub { return new c_type; };
62 $$found_comment = sub { return 1; };
63 $$found_declaration = sub { return 1; };
64 $$found_function = sub { return 1; };
65 $$found_function_call = sub { return 1; };
66 $$found_line = sub { return 1; };
67 $$found_preprocessor = sub { return 1; };
68 $$found_statement = sub { return 1; };
69 $$found_type = sub { return 1; };
70 $$found_variable = sub { return 1; };
75 ########################################################################
76 # set_found_comment_callback
78 sub set_found_comment_callback {
81 my $found_comment = \${$self->{FOUND_COMMENT}};
83 $$found_comment = shift;
86 ########################################################################
87 # set_found_declaration_callback
89 sub set_found_declaration_callback {
92 my $found_declaration = \${$self->{FOUND_DECLARATION}};
94 $$found_declaration = shift;
97 ########################################################################
98 # set_found_function_callback
100 sub set_found_function_callback {
103 my $found_function = \${$self->{FOUND_FUNCTION}};
105 $$found_function = shift;
108 ########################################################################
109 # set_found_function_call_callback
111 sub set_found_function_call_callback {
114 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
116 $$found_function_call = shift;
119 ########################################################################
120 # set_found_line_callback
122 sub set_found_line_callback {
125 my $found_line = \${$self->{FOUND_LINE}};
127 $$found_line = shift;
130 ########################################################################
131 # set_found_preprocessor_callback
133 sub set_found_preprocessor_callback {
136 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
138 $$found_preprocessor = shift;
141 ########################################################################
142 # set_found_statement_callback
144 sub set_found_statement_callback {
147 my $found_statement = \${$self->{FOUND_STATEMENT}};
149 $$found_statement = shift;
152 ########################################################################
153 # set_found_type_callback
155 sub set_found_type_callback {
158 my $found_type = \${$self->{FOUND_TYPE}};
160 $$found_type = shift;
163 ########################################################################
164 # set_found_variable_callback
166 sub set_found_variable_callback {
169 my $found_variable = \${$self->{FOUND_VARIABLE}};
171 $$found_variable = shift;
175 ########################################################################
184 if (/^(\w+(?:\s*\*)*)\s*\(\s*\*\s*\)\s*\(\s*(.*?)\s*\)$/s) {
185 my $return_type = $1;
186 my @arguments = split(/\s*,\s*/, $2);
187 foreach my $argument (@arguments) {
188 if ($argument =~ s/^(\w+(?:\s*\*)*)\s*\w+$/$1/) {
189 $argument =~ s/\s+/ /g;
190 $argument =~ s/\s*\*\s*/*/g;
191 $argument =~ s/(\*+)$/ $1/;
195 $_ = "$return_type (*)(" . join(", ", @arguments) . ")";
202 ########################################################################
209 my $refcurrent = shift;
211 my $refcolumn = shift;
213 my $refmatch = shift;
215 local $_ = $$refcurrent;
216 my $line = $$refline;
217 my $column = $$refcolumn;
220 if(s/^(?:$pattern)//s) {
221 $self->_update_c_position($&, \$line, \$column);
227 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
231 $$refcolumn = $column;
238 ########################################################################
250 $message = "parse error" if !$message;
253 if($output->prefix) {
254 # $output->write("\n");
258 $self->_parse_c_warning($_, $line, $column, $context, $message);
263 ########################################################################
266 # FIXME: Use caller (See man perlfunc)
268 sub _parse_c_warning {
277 my $file = \${$self->{FILE}};
279 $message = "warning" if !$message;
283 my @lines = split(/\n/, $_);
285 $current .= $lines[0] . "\n" if $lines[0];
286 $current .= $lines[1] . "\n" if $lines[1];
290 (my $package, my $filename, my $line) = caller(0);
291 $output->write("*** caller ***: $filename:$line\n");
295 $output->write("$$file:$line." . ($column + 1) . ": $context: $message: \\\n$current");
297 $output->write("$$file:$line." . ($column + 1) . ": $context: $message\n");
301 ########################################################################
302 # __parse_c_until_one_of
304 sub __parse_c_until_one_of {
307 my $characters = shift;
308 my $on_same_level = shift;
309 my $refcurrent = shift;
311 my $refcolumn = shift;
314 local $_ = $$refcurrent;
315 my $line = $$refline;
316 my $column = $$refcolumn;
319 if(!defined($match)) {
321 $match = \$blackhole;
326 while(/^[^$characters]/s || $level > 0) {
330 if(s/^[^\(\)\[\]\{\}\n\t\'\"]*//s) {
333 } elsif ($on_same_level) {
334 if(s/^[^$characters\(\)\[\]\{\}\n\t\'\"]*//s) {
338 if(s/^[^$characters\n\t\'\"]*//s) {
345 while(/^./ && !s/^\'//) {
361 $$match .= $submatch;
362 $column += length($submatch);
365 while(/^./ && !s/^\"//) {
381 $$match .= $submatch;
382 $column += length($submatch);
383 } elsif($on_same_level && s/^[\(\[\{]//) {
387 $$match .= $submatch;
389 } elsif($on_same_level && s/^[\)\]\}]//) {
394 $$match .= $submatch;
398 $$match .= $submatch;
404 $$match .= $submatch;
410 $$match .= $submatch;
411 $column = $column + 8 - $column % 8;
413 $$match .= $submatch;
414 $column += length($submatch);
420 $$refcolumn = $column;
424 ########################################################################
425 # _parse_c_until_one_of
427 sub _parse_c_until_one_of {
430 my $characters = shift;
431 my $refcurrent = shift;
433 my $refcolumn = shift;
436 return $self->__parse_c_until_one_of($characters, 0, $refcurrent, $refline, $refcolumn, $match);
439 ########################################################################
440 # _parse_c_on_same_level_until_one_of
442 sub _parse_c_on_same_level_until_one_of {
445 my $characters = shift;
446 my $refcurrent = shift;
448 my $refcolumn = shift;
451 return $self->__parse_c_until_one_of($characters, 1, $refcurrent, $refline, $refcolumn, $match);
454 ########################################################################
457 sub _update_c_position {
462 my $refcolumn = shift;
464 my $line = $$refline;
465 my $column = $$refcolumn;
468 if(s/^[^\n\t\'\"]*//s) {
469 $column += length($&);
474 while(/^./ && !s/^\'//) {
476 $column += length($1);
480 $column += length($1);
483 $column += length($1);
491 while(/^./ && !s/^\"//) {
493 $column += length($1);
497 $column += length($1);
500 $column += length($1);
510 $column = $column + 8 - $column % 8;
515 $$refcolumn = $column;
518 ########################################################################
524 my $refcurrent = shift;
526 my $refcolumn = shift;
528 my $refstatements = shift;
529 my $refstatements_line = shift;
530 my $refstatements_column = shift;
532 local $_ = $$refcurrent;
533 my $line = $$refline;
534 my $column = $$refcolumn;
536 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
546 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
548 my $statements_line = $line;
549 my $statements_column = $column;
554 $self->_parse_c_until_one_of("\\{\\}", \$_, \$line, \$column, \$match);
558 $statements .= $match;
574 $$refcolumn = $column;
575 $$refstatements = $statements;
576 $$refstatements_line = $statements_line;
577 $$refstatements_column = $statements_column;
582 ########################################################################
583 # parse_c_declaration
585 sub parse_c_declaration {
588 my $found_declaration = \${$self->{FOUND_DECLARATION}};
589 my $found_function = \${$self->{FOUND_FUNCTION}};
591 my $refcurrent = shift;
593 my $refcolumn = shift;
595 local $_ = $$refcurrent;
596 my $line = $$refline;
597 my $column = $$refcolumn;
599 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
601 my $begin_line = $line;
602 my $begin_column = $column + 1;
604 my $end_line = $begin_line;
605 my $end_column = $begin_column;
606 $self->_update_c_position($_, \$end_line, \$end_column);
608 if(!&$$found_declaration($begin_line, $begin_column, $end_line, $end_column, $_)) {
613 my $function = shift;
616 my $calling_convention = shift;
617 my $return_type = shift;
619 my @arguments = shift;
620 my @argument_lines = shift;
621 my @argument_columns = shift;
628 } elsif(s/^WINE_(?:DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\(\s*(\w+)\s*\)\s*//s) { # FIXME: Wine specific kludge
629 $self->_update_c_position($&, \$line, \$column);
630 } elsif(s/^__ASM_GLOBAL_FUNC\(\s*(\w+)\s*,\s*//s) { # FIXME: Wine specific kludge
631 $self->_update_c_position($&, \$line, \$column);
632 $self->_parse_c_until_one_of("\)", \$_, \$line, \$column);
636 } elsif(s/^(?:DECLARE_OLD_HANDLE|DEFINE_AVIGUID|DEFINE_OLEGUID)\s*(?=\()//s) { # FIXME: Wine specific kludge
637 $self->_update_c_position($&, \$line, \$column);
641 my @argument_columns;
643 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
646 } elsif(s/^DEFINE_COMMON_NOTIFICATIONS\(\s*(\w+)\s*,\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
647 $self->_update_c_position($&, \$line, \$column);
648 } elsif(s/^MAKE_FUNCPTR\(\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
649 $self->_update_c_position($&, \$line, \$column);
650 } elsif(s/^START_TEST\(\s*(\w+)\s*\)\s*{//s) { # FIXME: Wine specific kludge
651 $self->_update_c_position($&, \$line, \$column);
652 } elsif(s/^int\s*_FUNCTION_\s*{//s) { # FIXME: Wine specific kludge
653 $self->_update_c_position($&, \$line, \$column);
654 } elsif(s/^(?:jump|strong)_alias//s) { # FIXME: GNU C library specific kludge
655 $self->_update_c_position($&, \$line, \$column);
656 } elsif(s/^(?:__asm__|asm)\s*\(//) {
657 $self->_update_c_position($&, \$line, \$column);
658 } elsif($self->parse_c_typedef(\$_, \$line, \$column)) {
660 } elsif($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type, \$name)) {
662 } elsif($self->parse_c_function(\$_, \$line, \$column, \$function)) {
663 if(&$$found_function($function))
665 my $statements = $function->statements;
666 my $statements_line = $function->statements_line;
667 my $statements_column = $function->statements_column;
669 if(defined($statements)) {
670 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
676 $self->_parse_c_error($_, $line, $column, "declaration");
681 $$refcolumn = $column;
686 ########################################################################
687 # parse_c_declarations
689 sub parse_c_declarations {
692 my $refcurrent = shift;
694 my $refcolumn = shift;
699 ########################################################################
705 my $refcurrent = shift;
707 my $refcolumn = shift;
709 local $_ = $$refcurrent;
710 my $line = $$refline;
711 my $column = $$refcolumn;
713 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
715 if (!s/^enum\s+((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
718 my $_name = $1 || "";
720 $self->_update_c_position($&, \$line, \$column);
725 while ($self->_parse_c_on_same_level_until_one_of(',', \$_, \$line, \$column, \$match)) {
727 if ($match !~ /^(\w+)\s*(?:=\s*(.*?)\s*)?$/) {
728 $self->_parse_c_error($_, $line, $column, "enum");
731 my $enum_value = $2 || "";
733 # $output->write("enum:$_name:$enum_name:$enum_value\n");
736 if ($self->_parse_c(',', \$_, \$line, \$column)) {
738 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
741 my $tuple_line = $line;
742 my $tuple_column = $column - 1;
746 my @argument_columns;
748 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
749 \@arguments, \@argument_lines, \@argument_columns))
751 $self->_parse_c_error($_, $line, $column, "enum");
755 if ($#arguments >= 0) {
756 $name = $arguments[0];
761 $self->_parse_c_error($_, $line, $column, "enum");
765 $self->_update_c_position($_, \$line, \$column);
769 $$refcolumn = $column;
773 ########################################################################
776 sub parse_c_expression {
779 my $refcurrent = shift;
781 my $refcolumn = shift;
783 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
785 local $_ = $$refcurrent;
786 my $line = $$refline;
787 my $column = $$refcolumn;
789 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
792 if(s/^(.*?)(\w+\s*\()/$2/s) {
793 $self->_update_c_position($1, \$line, \$column);
795 my $begin_line = $line;
796 my $begin_column = $column + 1;
801 my @argument_columns;
802 if(!$self->parse_c_function_call(\$_, \$line, \$column, \$name, \@arguments, \@argument_lines, \@argument_columns)) {
806 if(&$$found_function_call($begin_line, $begin_column, $line, $column, $name, \@arguments))
808 while(defined(my $argument = shift @arguments) &&
809 defined(my $argument_line = shift @argument_lines) &&
810 defined(my $argument_column = shift @argument_columns))
812 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
820 $self->_update_c_position($_, \$line, \$column);
824 $$refcolumn = $column;
829 ########################################################################
835 my $found_comment = \${$self->{FOUND_COMMENT}};
836 my $found_line = \${$self->{FOUND_LINE}};
838 my $refcurrent = shift;
840 my $refcolumn = shift;
842 local $_ = $$refcurrent;
843 my $line = $$refline;
844 my $column = $$refcolumn;
846 my $declaration = "";
847 my $declaration_line = $line;
848 my $declaration_column = $column;
850 my $previous_line = 0;
851 my $previous_column = -1;
853 my $preprocessor_condition;
860 while($plevel > 0 || $blevel > 0) {
862 $self->_parse_c_until_one_of("#/\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
864 if($line != $previous_line) {
865 &$$found_line($line);
866 } elsif(0 && $column == $previous_column) {
867 $self->_parse_c_error($_, $line, $column, "file", "no progress");
869 # &$$found_line("$line.$column");
871 $previous_line = $line;
872 $previous_column = $column;
874 if($match !~ /^\s+$/s && $options->debug) {
875 $self->_parse_c_warning($_, $line, $column, "file", "$plevel $blevel: '$declaration' '$match'");
878 if(!$declaration && $match =~ s/^\s+//s) {
879 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
883 $declaration .= $match;
886 if ($declaration =~ s/^extern\s*\"C\"//s) {
888 $self->_update_c_position($&, \$line, \$column);
890 $declaration_line = $line;
891 $declaration_column = $column;
896 } elsif ($extern_c && $blevel == 1 && $plevel == 1 && !$declaration) {
898 $self->_update_c_position($&, \$line, \$column);
900 $declaration_line = $line;
901 $declaration_column = $column;
906 } elsif($declaration =~ s/^(?:__DEFINE_(?:GET|SET)_SEG|OUR_GUID_ENTRY)\s*(?=\()//sx) { # FIXME: Wine specific kludge
908 if ($plevel > 2 || !s/^\)//) {
909 $declaration = "$prefix$declaration";
912 $self->_update_c_position($&, \$line, \$column);
917 my @argument_columns;
919 if(!$self->parse_c_tuple(\$declaration, \$declaration_line, \$declaration_column,
920 \@arguments, \@argument_lines, \@argument_columns))
922 $self->_parse_c_error($declaration, $declaration_line, $declaration_column, "file", "tuple expected");
926 $declaration_line = $line;
927 $declaration_column = $column;
931 } elsif ($declaration =~ s/^(?:DEFINE_SHLGUID)\s*\(.*?\)//s) {
932 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
933 } elsif ($declaration =~ s/^(?:DECL_WINELIB_TYPE_AW|DECLARE_HANDLE(?:16)?|TYPE_MARSHAL)\(\s*(\w+)\s*\)\s*//s) {
934 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
935 } elsif ($declaration =~ s/^ICOM_DEFINE\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*//s) {
936 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
942 while(s/^.*?\n//) { $blank_lines++; }
945 $declaration_line = $line;
946 $declaration_column = $column;
948 $declaration .= "\n" x $blank_lines;
956 my $preprocessor_line = $line;
957 my $preprocessor_column = $column;
959 my $preprocessor = $&;
960 while(s/^(.*?)\\\s*\n//) {
962 $preprocessor .= "$1\n";
964 if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
967 $preprocessor .= "$1$3";
971 } elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
983 } elsif($preprocessor =~ /^\#\s*if/) {
984 if($preprocessor =~ /^\#\s*if\s*0/) {
989 if($preprocessor =~ /^\#\s*ifdef\s+WORDS_BIGENDIAN$/) {
990 $preprocessor_condition = "defined(WORD_BIGENDIAN)";
991 # $output->write("'$preprocessor_condition':'$declaration'\n")
993 $preprocessor_condition = "";
996 } elsif($preprocessor =~ /^\#\s*else/) {
997 if ($preprocessor_condition ne "") {
998 $preprocessor_condition =~ "!$preprocessor_condition";
999 $preprocessor_condition =~ s/^!!/!/;
1000 # $output->write("'$preprocessor_condition':'$declaration'\n")
1002 } elsif($preprocessor =~ /^\#\s*endif/) {
1010 if ($preprocessor_condition ne "") {
1011 # $output->write("'$preprocessor_condition':'$declaration'\n");
1012 $preprocessor_condition = "";
1017 if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
1022 if(s/^\/\*.*?\*\///s) {
1023 &$$found_comment($line, $column + 1, $&);
1029 $column += length($_);
1031 } elsif(s/^\/\/(.*?)\n//) {
1032 &$$found_comment($line, $column + 1, $&);
1041 $line += $blank_lines;
1042 if($blank_lines > 0) {
1047 $declaration_line = $line;
1048 $declaration_column = $column;
1049 } elsif($blank_lines > 0) {
1050 $declaration .= "\n" x $blank_lines;
1072 $self->_parse_c_error($_, $line, $column, "file", ") without (");
1075 if($plevel == 1 && $declaration =~ /^__ASM_GLOBAL_FUNC/) {
1076 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1079 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1081 $declaration_line = $line;
1082 $declaration_column = $column;
1090 $self->_parse_c_error($_, $line, $column, "file", "} without {");
1095 if($declaration =~ /^typedef/s ||
1096 $declaration =~ /^(?:const\s+|extern\s+|static\s+)*(?:struct|union)(?:\s+\w+)?\s*\{/s)
1099 } elsif($plevel == 1 && $blevel == 1) {
1100 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1103 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1105 $declaration_line = $line;
1106 $declaration_column = $column;
1107 } elsif($column == 1 && !$extern_c) {
1108 $self->_parse_c_error("", $line, $column, "file", "inner } ends on column 1");
1112 if(0 && $blevel == 1 &&
1113 $declaration !~ /^typedef/ &&
1114 $declaration !~ /^(?:const\s+|extern\s+|static\s+)?(?:struct|union)(?:\s+\w+)?\s*\{/s &&
1115 $declaration =~ /^(?:\w+(?:\s*\*)*\s+)*(\w+)\s*\(\s*(?:(?:\w+\s*,\s*)*(\w+))?\s*\)\s*(.*?);$/s &&
1116 $1 ne "ICOM_VTABLE" && defined($2) && $2 ne "void" && $3) # K&R
1118 $self->_parse_c_warning("", $line, $column, "file", "function $1: warning: function has K&R format");
1119 } elsif($plevel == 1 && $blevel == 1) {
1120 $declaration =~ s/\s*;$//;
1121 if($declaration && !$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1124 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1126 $declaration_line = $line;
1127 $declaration_column = $column;
1129 } elsif(/^\s*$/ && $declaration =~ /^\s*$/ && $match =~ /^\s*$/) {
1133 $self->_parse_c_error($_, $line, $column, "file", "parse error: '$declaration' '$match'");
1139 $$refcolumn = $column;
1144 ########################################################################
1147 sub parse_c_function {
1150 my $file = \${$self->{FILE}};
1151 my $create_function = \${$self->{CREATE_FUNCTION}};
1153 my $refcurrent = shift;
1154 my $refline = shift;
1155 my $refcolumn = shift;
1157 my $reffunction = shift;
1159 local $_ = $$refcurrent;
1160 my $line = $$refline;
1161 my $column = $$refcolumn;
1164 my $calling_convention = "";
1169 my @argument_columns;
1171 my $statements_line;
1172 my $statements_column;
1174 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1176 my $begin_line = $line;
1177 my $begin_column = $column + 1;
1181 } elsif($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
1185 # $self->_parse_c_warning($_, $line, $column, "function", "");
1188 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1189 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1190 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1191 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1192 \$_, \$line, \$column, \$match))
1194 if($match =~ /^extern|static$/) {
1203 } elsif($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1205 } elsif($self->_parse_c('WINE_EXCEPTION_FILTER\(\w+\)', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1208 if(!$self->parse_c_type(\$_, \$line, \$column, \$return_type)) {
1212 $self->_parse_c('inline|FAR', \$_, \$line, \$column);
1214 $self->_parse_c("__cdecl|__stdcall|__RPC_STUB|" .
1215 "CALLBACK|CDECL|PASCAL|" .
1216 "RPC_ENTRY|RPC_VAR_ENTRY|" .
1217 "VFWAPIV|VFWAPI|WINAPIV|WINAPI|" .
1219 \$_, \$line, \$column, \$calling_convention);
1222 # FIXME: ???: Old variant of __attribute((const))
1223 $self->_parse_c('const', \$_, \$line, \$column);
1225 if(!$self->_parse_c('(?:operator\s*!=|(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)', \$_, \$line, \$column, \$name)) {
1231 $self->_update_c_position($&, \$line, \$column);
1235 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1241 $self->_update_c_position($&, \$line, \$column);
1243 $self->_parse_c_error($_, $line, $column, "function");
1251 } elsif($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
1256 # FIXME: Implement proper handling of K&R C functions
1257 $self->_parse_c_until_one_of("{", \$_, \$line, \$column, $kar);
1260 $output->write("K&R: $kar\n");
1263 if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1267 my $end_line = $line;
1268 my $end_column = $column;
1272 $$refcolumn = $column;
1274 my $function = &$$create_function;
1276 $function->file($$file);
1277 $function->begin_line($begin_line);
1278 $function->begin_column($begin_column);
1279 $function->end_line($end_line);
1280 $function->end_column($end_column);
1281 $function->linkage($linkage);
1282 $function->return_type($return_type);
1283 $function->calling_convention($calling_convention);
1284 $function->name($name);
1285 # if(defined($argument_types)) {
1286 # $function->argument_types([@$argument_types]);
1288 # if(defined($argument_names)) {
1289 # $function->argument_names([@$argument_names]);
1291 $function->statements_line($statements_line);
1292 $function->statements_column($statements_column);
1293 $function->statements($statements);
1295 $$reffunction = $function;
1300 ########################################################################
1301 # parse_c_function_call
1303 sub parse_c_function_call {
1306 my $refcurrent = shift;
1307 my $refline = shift;
1308 my $refcolumn = shift;
1310 my $refname = shift;
1311 my $refarguments = shift;
1312 my $refargument_lines = shift;
1313 my $refargument_columns = shift;
1315 local $_ = $$refcurrent;
1316 my $line = $$refline;
1317 my $column = $$refcolumn;
1322 my @argument_columns;
1324 if(s/^(\w+)(\s*)(?=\()//s) {
1325 $self->_update_c_position($&, \$line, \$column);
1329 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1338 $$refcolumn = $column;
1341 @$refarguments = @arguments;
1342 @$refargument_lines = @argument_lines;
1343 @$refargument_columns = @argument_columns;
1348 ########################################################################
1349 # parse_c_preprocessor
1351 sub parse_c_preprocessor {
1354 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
1356 my $refcurrent = shift;
1357 my $refline = shift;
1358 my $refcolumn = shift;
1360 local $_ = $$refcurrent;
1361 my $line = $$refline;
1362 my $column = $$refcolumn;
1364 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1366 my $begin_line = $line;
1367 my $begin_column = $column + 1;
1369 if(!&$$found_preprocessor($begin_line, $begin_column, "$_")) {
1375 } elsif(/^\#\s*define\s*(.*?)$/s) {
1376 $self->_update_c_position($_, \$line, \$column);
1377 } elsif(/^\#\s*else/s) {
1378 $self->_update_c_position($_, \$line, \$column);
1379 } elsif(/^\#\s*endif/s) {
1380 $self->_update_c_position($_, \$line, \$column);
1381 } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1382 $self->_update_c_position($_, \$line, \$column);
1383 } elsif(/^\#\s*include\s+(.*?)$/s) {
1384 $self->_update_c_position($_, \$line, \$column);
1385 } elsif(/^\#\s*undef\s+(.*?)$/s) {
1386 $self->_update_c_position($_, \$line, \$column);
1388 $self->_parse_c_error($_, $line, $column, "preprocessor");
1393 $$refcolumn = $column;
1398 ########################################################################
1401 sub parse_c_statement {
1404 my $refcurrent = shift;
1405 my $refline = shift;
1406 my $refcolumn = shift;
1408 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1410 local $_ = $$refcurrent;
1411 my $line = $$refline;
1412 my $column = $$refcolumn;
1414 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1416 $self->_parse_c('(?:case\s+)?(\w+)\s*:\s*', \$_, \$line, \$column);
1418 # $output->write("$line.$column: statement: '$_'\n");
1424 my $statements_line;
1425 my $statements_column;
1426 if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1429 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1432 } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
1433 $self->_update_c_position($&, \$line, \$column);
1439 my @argument_columns;
1440 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1444 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1445 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1448 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1450 while(defined(my $argument = shift @arguments) &&
1451 defined(my $argument_line = shift @argument_lines) &&
1452 defined(my $argument_column = shift @argument_columns))
1454 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1456 } elsif(s/^else//) {
1457 $self->_update_c_position($&, \$line, \$column);
1458 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1461 } elsif(s/^return//) {
1462 $self->_update_c_position($&, \$line, \$column);
1463 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1464 if(!$self->parse_c_expression(\$_, \$line, \$column)) {
1467 } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1470 # $self->_parse_c_error($_, $line, $column, "statement");
1473 $self->_update_c_position($_, \$line, \$column);
1477 $$refcolumn = $column;
1482 ########################################################################
1483 # parse_c_statements
1485 sub parse_c_statements {
1488 my $refcurrent = shift;
1489 my $refline = shift;
1490 my $refcolumn = shift;
1492 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1494 local $_ = $$refcurrent;
1495 my $line = $$refline;
1496 my $column = $$refcolumn;
1498 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1500 # $output->write("$line.$column: statements: '$_'\n");
1503 my $statement_line = $line;
1504 my $statement_column = $column;
1506 my $previous_line = -1;
1507 my $previous_column = -1;
1511 while($plevel > 0 || $blevel > 0) {
1513 $self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1515 if($previous_line == $line && $previous_column == $column) {
1516 $self->_parse_c_error($_, $line, $column, "statements", "no progress");
1518 $previous_line = $line;
1519 $previous_column = $column;
1521 # $output->write("'$match' '$_'\n");
1523 $statement .= $match;
1528 } elsif(s/^[\)\]]//) {
1531 $self->_parse_c_error($_, $line, $column, "statements");
1541 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1544 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1546 $statement_line = $line;
1547 $statement_column = $column;
1550 if($plevel == 1 && $blevel == 1) {
1551 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1555 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1557 $statement_line = $line;
1558 $statement_column = $column;
1562 } elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
1566 $self->_parse_c_error($_, $line, $column, "statements");
1570 $self->_update_c_position($_, \$line, \$column);
1574 $$refcolumn = $column;
1579 ########################################################################
1580 # parse_c_struct_union
1582 sub parse_c_struct_union {
1585 my $refcurrent = shift;
1586 my $refline = shift;
1587 my $refcolumn = shift;
1589 my $refkind = shift;
1590 my $ref_name = shift;
1591 my $reffield_type_names = shift;
1592 my $reffield_names = shift;
1593 my $refnames = shift;
1595 local $_ = $$refcurrent;
1596 my $line = $$refline;
1597 my $column = $$refcolumn;
1601 my @field_type_names = ();
1602 my @field_names = ();
1605 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1607 if (!s/^(struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1613 $self->_update_c_position($&, \$line, \$column);
1618 while ($_ && $self->_parse_c_on_same_level_until_one_of(';', \$_, \$line, \$column, \$match))
1621 my $field_type_name;
1624 if ($self->parse_c_variable(\$match, \$line, \$column, \$field_linkage, \$field_type_name, \$field_name)) {
1625 $field_type_name =~ s/\s+/ /g;
1627 push @field_type_names, $field_type_name;
1628 push @field_names, $field_name;
1629 # $output->write("$kind:$_name:$field_type_name:$field_name\n");
1631 $self->_parse_c_error($_, $line, $column, "typedef $kind: '$match'");
1634 if ($self->_parse_c(';', \$_, \$line, \$column)) {
1636 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1639 my $tuple_line = $line;
1640 my $tuple_column = $column - 1;
1644 my @argument_columns;
1646 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1647 \@arguments, \@argument_lines, \@argument_columns))
1649 $self->_parse_c_error($_, $line, $column, "$kind");
1652 foreach my $argument (@arguments) {
1653 my $name = $argument;
1660 $self->_parse_c_error($_, $line, $column, "$kind");
1666 $$refcolumn = $column;
1669 $$ref_name = $_name;
1670 @$reffield_type_names = @field_type_names;
1671 @$reffield_names = @field_names;
1672 @$refnames = @names;
1677 ########################################################################
1683 my $refcurrent = shift;
1684 my $refline = shift;
1685 my $refcolumn = shift;
1687 # FIXME: Should not write directly
1689 my $item_lines = shift;
1690 my $item_columns = shift;
1692 local $_ = $$refcurrent;
1694 my $line = $$refline;
1695 my $column = $$refcolumn;
1705 my $item_line = $line;
1706 my $item_column = $column + 1;
1709 while($plevel > 0) {
1711 $self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1719 push @$item_lines, $item_line;
1720 push @$item_columns, $item_column;
1721 push @$items, $item;
1731 push @$item_lines, $item_line;
1732 push @$item_columns, $item_column;
1733 push @$items, $item;
1734 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1736 $item_column = $column + 1;
1748 $$refcolumn = $column;
1753 ########################################################################
1759 my $refcurrent = shift;
1760 my $refline = shift;
1761 my $refcolumn = shift;
1763 my $reftype = shift;
1765 local $_ = $$refcurrent;
1766 my $line = $$refline;
1767 my $column = $$refcolumn;
1771 $self->_parse_c("const", \$_, \$line, \$column);
1775 } elsif($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1777 } elsif($self->_parse_c('(?:enum\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1778 \$_, \$line, \$column, \$type))
1788 $$refcolumn = $column;
1795 ########################################################################
1798 sub parse_c_typedef {
1801 my $create_type = \${$self->{CREATE_TYPE}};
1802 my $found_type = \${$self->{FOUND_TYPE}};
1803 my $preprocessor_condition = \${$self->{PREPROCESSOR_CONDITION}};
1805 my $refcurrent = shift;
1806 my $refline = shift;
1807 my $refcolumn = shift;
1809 local $_ = $$refcurrent;
1810 my $line = $$refline;
1811 my $column = $$refcolumn;
1815 if (!$self->_parse_c("typedef", \$_, \$line, \$column)) {
1823 } elsif ($self->parse_c_enum(\$_, \$line, \$column)) {
1829 my @field_type_names;
1834 } elsif ($self->parse_c_struct_union(\$_, \$line, \$column,
1835 \$kind, \$_name, \@field_type_names, \@field_names, \@names))
1838 foreach my $name (@names) {
1839 if ($name =~ /^\w+$/) {
1840 my $type = &$$create_type();
1843 $type->_name($_name);
1845 $type->field_type_names([@field_type_names]);
1846 $type->field_names([@field_names]);
1848 &$$found_type($type);
1851 } elsif (!defined($base_name)) {
1852 $self->_parse_c_error($_, $line, $column, "typedef 1");
1853 } elsif ($name =~ /^(\*+)\s*(?:RESTRICTED_POINTER\s+)?(\w+)$/) {
1854 my $type_name = "$base_name $1";
1857 my $type = &$$create_type();
1861 $type->field_type_names([$type_name]);
1862 $type->field_names([""]);
1864 &$$found_type($type);
1866 $self->_parse_c_error($_, $line, $column, "typedef 2");
1878 } elsif ($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type_name, \$name)) {
1879 $type_name =~ s/\s+/ /g;
1881 if(defined($type_name) && defined($name)) {
1882 my $type = &$$create_type();
1884 if (length($name) == 0) {
1885 $self->_parse_c_error($_, $line, $column, "typedef");
1890 $type->field_type_names([$type_name]);
1891 $type->field_names([""]);
1893 &$$found_type($type);
1896 if (0 && $_ && !/^,/) {
1897 $self->_parse_c_error($_, $line, $column, "typedef");
1900 $self->_parse_c_error($_, $line, $column, "typedef");
1905 $$refcolumn = $column;
1910 ########################################################################
1913 sub parse_c_variable {
1916 my $found_variable = \${$self->{FOUND_VARIABLE}};
1918 my $refcurrent = shift;
1919 my $refline = shift;
1920 my $refcolumn = shift;
1922 my $reflinkage = shift;
1923 my $reftype = shift;
1924 my $refname = shift;
1926 local $_ = $$refcurrent;
1927 my $line = $$refline;
1928 my $column = $$refcolumn;
1930 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1932 my $begin_line = $line;
1933 my $begin_column = $column + 1;
1940 # $self->_parse_c_warning($_, $line, $column, "variable");
1943 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1944 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1945 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1946 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1947 \$_, \$line, \$column, \$match))
1949 if ($match =~ /^extern|static$/) {
1953 $self->_parse_c_warning($_, $line, $column, "repeated linkage (ignored): $match");
1955 } elsif ($match =~ /^signed|unsigned$/) {
1959 $self->_parse_c_warning($_, $line, $column, "repeated sign (ignored): $match");
1970 } elsif (s/^(enum\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1973 $self->_update_c_position($&, \$line, \$column);
1975 if(defined($_name)) {
1976 $type = "$kind $_name { }";
1978 $type = "$kind { }";
1982 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*)\s*(\w+)\s*(\[.*?\]$|:\s*(\d+)$|\{)?//s) {
1998 $type = $self->_format_c_type($type);
2001 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*\s*\((?:\s*CALLBACK|\s*NTAPI|\s*WINAPI)?(?:\s*\*)*)\s*(\w+)\s*(\)\s*\(.*?\))$//s) {
2002 $type = $self->_format_c_type("$sign$1$3");
2006 } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
2010 $self->_parse_c_warning($_, $line, $column, "variable", "'$_'");
2016 } elsif($self->_parse_c('SEQ_DEFINEBUF', \$_, \$line, \$column, \$match)) { # Linux specific
2019 } elsif($self->_parse_c('DEFINE_REGS_ENTRYPOINT_\w+|DPQ_DECL_\w+|HANDLER_DEF|IX86_ONLY', # Wine specific
2020 \$_, \$line, \$column, \$match))
2024 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
2027 } elsif(s/^(enum|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*//s) {
2030 $self->_update_c_position($&, \$line, \$column);
2032 if(defined($_name)) {
2033 $type = "struct $_name { }";
2035 $type = "struct { }";
2037 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
2044 # $output->write("*** $type: '$_'\n");
2046 # $self->_parse_c_warning($_, $line, $column, "variable2", "");
2050 } elsif(s/^WINAPI\s*//) {
2051 $self->_update_c_position($&, \$line, \$column);
2052 } elsif(s/^WINE_UNUSED\s*//) {
2053 $self->_update_c_position($&, \$line, \$column);
2058 } elsif(s/^(\((?:__cdecl|PASCAL|WINAPI)?\s*\*?\s*(?:__cdecl|PASCAL|WINAPI)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
2059 $self->_update_c_position($&, \$line, \$column);
2064 $self->_parse_c_until_one_of("\\)", \$_, \$line, \$column);
2065 if(s/^\)//) { $column++; }
2066 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
2068 if(!s/^(?:=\s*|,\s*|$)//) {
2071 } elsif(s/^(?:\*\s*)*(?:const\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
2072 $self->_update_c_position($&, \$line, \$column);
2082 # $output->write("$type: $name: '$_'\n");
2084 if(1 || $finished) {
2086 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(.*?\)', \$_, \$line, \$column, \$match)) {
2089 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+)\s*
2090 (?:\*\s*)*(\w+|\s*\*?\s*\w+\s*\))\s*(?:\[[^\]]*\]|\([^\)]*\))?
2091 (?:,\s*(?:\*\s*)*(\w+)\s*(?:\[[^\]]*\])?)*
2094 $self->_update_c_position($&, \$line, \$column);
2100 $type =~ s/^struct/struct /;
2101 } elsif(/^(enum|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*((?:\*\s*)*)(\w+)\s*(?:=|$)/s) {
2102 $self->_update_c_position($&, \$line, \$column);
2109 if(defined($_name)) {
2110 $type = "struct $_name { }";
2112 $type = "struct { }";
2125 $$refcolumn = $column;
2127 $$reflinkage = $linkage;
2131 if(&$$found_variable($begin_line, $begin_column, $linkage, $type, $name))