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 ########################################################################
702 sub parse_c_expression {
705 my $refcurrent = shift;
707 my $refcolumn = shift;
709 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
711 local $_ = $$refcurrent;
712 my $line = $$refline;
713 my $column = $$refcolumn;
715 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
718 if(s/^(.*?)(\w+\s*\()/$2/s) {
719 $self->_update_c_position($1, \$line, \$column);
721 my $begin_line = $line;
722 my $begin_column = $column + 1;
727 my @argument_columns;
728 if(!$self->parse_c_function_call(\$_, \$line, \$column, \$name, \@arguments, \@argument_lines, \@argument_columns)) {
732 if(&$$found_function_call($begin_line, $begin_column, $line, $column, $name, \@arguments))
734 while(defined(my $argument = shift @arguments) &&
735 defined(my $argument_line = shift @argument_lines) &&
736 defined(my $argument_column = shift @argument_columns))
738 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
746 $self->_update_c_position($_, \$line, \$column);
750 $$refcolumn = $column;
755 ########################################################################
761 my $found_comment = \${$self->{FOUND_COMMENT}};
762 my $found_line = \${$self->{FOUND_LINE}};
764 my $refcurrent = shift;
766 my $refcolumn = shift;
768 local $_ = $$refcurrent;
769 my $line = $$refline;
770 my $column = $$refcolumn;
772 my $declaration = "";
773 my $declaration_line = $line;
774 my $declaration_column = $column;
776 my $previous_line = 0;
777 my $previous_column = -1;
779 my $preprocessor_condition;
786 while($plevel > 0 || $blevel > 0) {
788 $self->_parse_c_until_one_of("#/\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
790 if($line != $previous_line) {
791 &$$found_line($line);
792 } elsif(0 && $column == $previous_column) {
793 $self->_parse_c_error($_, $line, $column, "file", "no progress");
795 # &$$found_line("$line.$column");
797 $previous_line = $line;
798 $previous_column = $column;
800 if($match !~ /^\s+$/s && $options->debug) {
801 $self->_parse_c_warning($_, $line, $column, "file", "$plevel $blevel: '$declaration' '$match'");
804 if(!$declaration && $match =~ s/^\s+//s) {
805 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
809 $declaration .= $match;
812 if ($declaration =~ s/^extern\s*\"C\"//s) {
814 $self->_update_c_position($&, \$line, \$column);
816 $declaration_line = $line;
817 $declaration_column = $column;
822 } elsif ($extern_c && $blevel == 1 && $plevel == 1 && !$declaration) {
824 $self->_update_c_position($&, \$line, \$column);
826 $declaration_line = $line;
827 $declaration_column = $column;
832 } elsif($declaration =~ s/^(?:__DEFINE_(?:GET|SET)_SEG|OUR_GUID_ENTRY)\s*(?=\()//sx) { # FIXME: Wine specific kludge
834 if ($plevel > 2 || !s/^\)//) {
835 $declaration = "$prefix$declaration";
838 $self->_update_c_position($&, \$line, \$column);
843 my @argument_columns;
845 if(!$self->parse_c_tuple(\$declaration, \$declaration_line, \$declaration_column,
846 \@arguments, \@argument_lines, \@argument_columns))
848 $self->_parse_c_error($declaration, $declaration_line, $declaration_column, "file", "tuple expected");
852 $declaration_line = $line;
853 $declaration_column = $column;
857 } elsif ($declaration =~ s/^(?:DEFINE_SHLGUID)\s*\(.*?\)//s) {
858 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
859 } elsif ($declaration =~ s/^(?:DECL_WINELIB_TYPE_AW|DECLARE_HANDLE(?:16)?|TYPE_MARSHAL)\(\s*(\w+)\s*\)\s*//s) {
860 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
861 } elsif ($declaration =~ s/^ICOM_DEFINE\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*//s) {
862 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
868 while(s/^.*?\n//) { $blank_lines++; }
871 $declaration_line = $line;
872 $declaration_column = $column;
874 $declaration .= "\n" x $blank_lines;
882 my $preprocessor_line = $line;
883 my $preprocessor_column = $column;
885 my $preprocessor = $&;
886 while(s/^(.*?)\\\s*\n//) {
888 $preprocessor .= "$1\n";
890 if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
893 $preprocessor .= "$1$3";
897 } elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
909 } elsif($preprocessor =~ /^\#\s*if/) {
910 if($preprocessor =~ /^\#\s*if\s*0/) {
915 if($preprocessor =~ /^\#\s*ifdef\s+WORDS_BIGENDIAN$/) {
916 $preprocessor_condition = "defined(WORD_BIGENDIAN)";
917 # $output->write("'$preprocessor_condition':'$declaration'\n")
919 $preprocessor_condition = "";
922 } elsif($preprocessor =~ /^\#\s*else/) {
923 if ($preprocessor_condition ne "") {
924 $preprocessor_condition =~ "!$preprocessor_condition";
925 $preprocessor_condition =~ s/^!!/!/;
926 # $output->write("'$preprocessor_condition':'$declaration'\n")
928 } elsif($preprocessor =~ /^\#\s*endif/) {
936 if ($preprocessor_condition ne "") {
937 # $output->write("'$preprocessor_condition':'$declaration'\n");
938 $preprocessor_condition = "";
943 if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
948 if(s/^\/\*.*?\*\///s) {
949 &$$found_comment($line, $column + 1, $&);
955 $column += length($_);
957 } elsif(s/^\/\/(.*?)\n//) {
958 &$$found_comment($line, $column + 1, $&);
967 $line += $blank_lines;
968 if($blank_lines > 0) {
973 $declaration_line = $line;
974 $declaration_column = $column;
975 } elsif($blank_lines > 0) {
976 $declaration .= "\n" x $blank_lines;
998 $self->_parse_c_error($_, $line, $column, "file", ") without (");
1001 if($plevel == 1 && $declaration =~ /^__ASM_GLOBAL_FUNC/) {
1002 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1005 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1007 $declaration_line = $line;
1008 $declaration_column = $column;
1016 $self->_parse_c_error($_, $line, $column, "file", "} without {");
1021 if($declaration =~ /^typedef/s ||
1022 $declaration =~ /^(?:const\s+|extern\s+|static\s+)*(?:struct|union)(?:\s+\w+)?\s*\{/s)
1025 } elsif($plevel == 1 && $blevel == 1) {
1026 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1029 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1031 $declaration_line = $line;
1032 $declaration_column = $column;
1033 } elsif($column == 1 && !$extern_c) {
1034 $self->_parse_c_error("", $line, $column, "file", "inner } ends on column 1");
1038 if(0 && $blevel == 1 &&
1039 $declaration !~ /^typedef/ &&
1040 $declaration !~ /^(?:const\s+|extern\s+|static\s+)?(?:struct|union)(?:\s+\w+)?\s*\{/s &&
1041 $declaration =~ /^(?:\w+(?:\s*\*)*\s+)*(\w+)\s*\(\s*(?:(?:\w+\s*,\s*)*(\w+))?\s*\)\s*(.*?);$/s &&
1042 $1 ne "ICOM_VTABLE" && defined($2) && $2 ne "void" && $3) # K&R
1044 $self->_parse_c_warning("", $line, $column, "file", "function $1: warning: function has K&R format");
1045 } elsif($plevel == 1 && $blevel == 1) {
1046 $declaration =~ s/\s*;$//;
1047 if($declaration && !$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1050 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1052 $declaration_line = $line;
1053 $declaration_column = $column;
1055 } elsif(/^\s*$/ && $declaration =~ /^\s*$/ && $match =~ /^\s*$/) {
1059 $self->_parse_c_error($_, $line, $column, "file", "parse error: '$declaration' '$match'");
1065 $$refcolumn = $column;
1070 ########################################################################
1073 sub parse_c_function {
1076 my $file = \${$self->{FILE}};
1077 my $create_function = \${$self->{CREATE_FUNCTION}};
1079 my $refcurrent = shift;
1080 my $refline = shift;
1081 my $refcolumn = shift;
1083 my $reffunction = shift;
1085 local $_ = $$refcurrent;
1086 my $line = $$refline;
1087 my $column = $$refcolumn;
1090 my $calling_convention = "";
1095 my @argument_columns;
1097 my $statements_line;
1098 my $statements_column;
1100 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1102 my $begin_line = $line;
1103 my $begin_column = $column + 1;
1107 } elsif($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
1111 # $self->_parse_c_warning($_, $line, $column, "function", "");
1114 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1115 'signed(?=\s+__int64\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1116 'unsigned(?=\s+__int64\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1117 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1118 \$_, \$line, \$column, \$match))
1120 if($match =~ /^extern|static$/) {
1129 } elsif($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1131 } elsif($self->_parse_c('WINE_EXCEPTION_FILTER\(\w+\)', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1134 if(!$self->parse_c_type(\$_, \$line, \$column, \$return_type)) {
1138 $self->_parse_c('inline|FAR', \$_, \$line, \$column);
1140 $self->_parse_c("__cdecl|__stdcall|__RPC_STUB|" .
1141 "CALLBACK|CDECL|PASCAL|" .
1142 "RPC_ENTRY|RPC_VAR_ENTRY|" .
1143 "VFWAPIV|VFWAPI|WINAPIV|WINAPI|" .
1145 \$_, \$line, \$column, \$calling_convention);
1148 # FIXME: ???: Old variant of __attribute((const))
1149 $self->_parse_c('const', \$_, \$line, \$column);
1151 if(!$self->_parse_c('(?:operator\s*!=|(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)', \$_, \$line, \$column, \$name)) {
1157 $self->_update_c_position($&, \$line, \$column);
1161 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1167 $self->_update_c_position($&, \$line, \$column);
1169 $self->_parse_c_error($_, $line, $column, "function");
1177 } elsif($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
1182 # FIXME: Implement proper handling of K&R C functions
1183 $self->_parse_c_until_one_of("{", \$_, \$line, \$column, $kar);
1186 $output->write("K&R: $kar\n");
1189 if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1193 my $end_line = $line;
1194 my $end_column = $column;
1198 $$refcolumn = $column;
1200 my $function = &$$create_function;
1202 $function->file($$file);
1203 $function->begin_line($begin_line);
1204 $function->begin_column($begin_column);
1205 $function->end_line($end_line);
1206 $function->end_column($end_column);
1207 $function->linkage($linkage);
1208 $function->return_type($return_type);
1209 $function->calling_convention($calling_convention);
1210 $function->name($name);
1211 # if(defined($argument_types)) {
1212 # $function->argument_types([@$argument_types]);
1214 # if(defined($argument_names)) {
1215 # $function->argument_names([@$argument_names]);
1217 $function->statements_line($statements_line);
1218 $function->statements_column($statements_column);
1219 $function->statements($statements);
1221 $$reffunction = $function;
1226 ########################################################################
1227 # parse_c_function_call
1229 sub parse_c_function_call {
1232 my $refcurrent = shift;
1233 my $refline = shift;
1234 my $refcolumn = shift;
1236 my $refname = shift;
1237 my $refarguments = shift;
1238 my $refargument_lines = shift;
1239 my $refargument_columns = shift;
1241 local $_ = $$refcurrent;
1242 my $line = $$refline;
1243 my $column = $$refcolumn;
1248 my @argument_columns;
1250 if(s/^(\w+)(\s*)(?=\()//s) {
1251 $self->_update_c_position($&, \$line, \$column);
1255 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1264 $$refcolumn = $column;
1267 @$refarguments = @arguments;
1268 @$refargument_lines = @argument_lines;
1269 @$refargument_columns = @argument_columns;
1274 ########################################################################
1275 # parse_c_preprocessor
1277 sub parse_c_preprocessor {
1280 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
1282 my $refcurrent = shift;
1283 my $refline = shift;
1284 my $refcolumn = shift;
1286 local $_ = $$refcurrent;
1287 my $line = $$refline;
1288 my $column = $$refcolumn;
1290 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1292 my $begin_line = $line;
1293 my $begin_column = $column + 1;
1295 if(!&$$found_preprocessor($begin_line, $begin_column, "$_")) {
1301 } elsif(/^\#\s*define\s*(.*?)$/s) {
1302 $self->_update_c_position($_, \$line, \$column);
1303 } elsif(/^\#\s*else/s) {
1304 $self->_update_c_position($_, \$line, \$column);
1305 } elsif(/^\#\s*endif/s) {
1306 $self->_update_c_position($_, \$line, \$column);
1307 } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1308 $self->_update_c_position($_, \$line, \$column);
1309 } elsif(/^\#\s*include\s+(.*?)$/s) {
1310 $self->_update_c_position($_, \$line, \$column);
1311 } elsif(/^\#\s*undef\s+(.*?)$/s) {
1312 $self->_update_c_position($_, \$line, \$column);
1314 $self->_parse_c_error($_, $line, $column, "preprocessor");
1319 $$refcolumn = $column;
1324 ########################################################################
1327 sub parse_c_statement {
1330 my $refcurrent = shift;
1331 my $refline = shift;
1332 my $refcolumn = shift;
1334 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1336 local $_ = $$refcurrent;
1337 my $line = $$refline;
1338 my $column = $$refcolumn;
1340 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1342 $self->_parse_c('(?:case\s+)?(\w+)\s*:\s*', \$_, \$line, \$column);
1344 # $output->write("$line.$column: statement: '$_'\n");
1350 my $statements_line;
1351 my $statements_column;
1352 if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1355 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1358 } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
1359 $self->_update_c_position($&, \$line, \$column);
1365 my @argument_columns;
1366 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1370 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1371 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1374 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1376 while(defined(my $argument = shift @arguments) &&
1377 defined(my $argument_line = shift @argument_lines) &&
1378 defined(my $argument_column = shift @argument_columns))
1380 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1382 } elsif(s/^else//) {
1383 $self->_update_c_position($&, \$line, \$column);
1384 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1387 } elsif(s/^return//) {
1388 $self->_update_c_position($&, \$line, \$column);
1389 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1390 if(!$self->parse_c_expression(\$_, \$line, \$column)) {
1393 } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1396 # $self->_parse_c_error($_, $line, $column, "statement");
1399 $self->_update_c_position($_, \$line, \$column);
1403 $$refcolumn = $column;
1408 ########################################################################
1409 # parse_c_statements
1411 sub parse_c_statements {
1414 my $refcurrent = shift;
1415 my $refline = shift;
1416 my $refcolumn = shift;
1418 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1420 local $_ = $$refcurrent;
1421 my $line = $$refline;
1422 my $column = $$refcolumn;
1424 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1426 # $output->write("$line.$column: statements: '$_'\n");
1429 my $statement_line = $line;
1430 my $statement_column = $column;
1432 my $previous_line = -1;
1433 my $previous_column = -1;
1437 while($plevel > 0 || $blevel > 0) {
1439 $self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1441 if($previous_line == $line && $previous_column == $column) {
1442 $self->_parse_c_error($_, $line, $column, "statements", "no progress");
1444 $previous_line = $line;
1445 $previous_column = $column;
1447 # $output->write("'$match' '$_'\n");
1449 $statement .= $match;
1454 } elsif(s/^[\)\]]//) {
1457 $self->_parse_c_error($_, $line, $column, "statements");
1467 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1470 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1472 $statement_line = $line;
1473 $statement_column = $column;
1476 if($plevel == 1 && $blevel == 1) {
1477 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1481 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1483 $statement_line = $line;
1484 $statement_column = $column;
1488 } elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
1492 $self->_parse_c_error($_, $line, $column, "statements");
1496 $self->_update_c_position($_, \$line, \$column);
1500 $$refcolumn = $column;
1505 ########################################################################
1511 my $refcurrent = shift;
1512 my $refline = shift;
1513 my $refcolumn = shift;
1515 # FIXME: Should not write directly
1517 my $item_lines = shift;
1518 my $item_columns = shift;
1520 local $_ = $$refcurrent;
1522 my $line = $$refline;
1523 my $column = $$refcolumn;
1533 my $item_line = $line;
1534 my $item_column = $column + 1;
1537 while($plevel > 0) {
1539 $self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1547 push @$item_lines, $item_line;
1548 push @$item_columns, $item_column;
1549 push @$items, $item;
1559 push @$item_lines, $item_line;
1560 push @$item_columns, $item_column;
1561 push @$items, $item;
1562 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1564 $item_column = $column + 1;
1576 $$refcolumn = $column;
1581 ########################################################################
1587 my $refcurrent = shift;
1588 my $refline = shift;
1589 my $refcolumn = shift;
1591 my $reftype = shift;
1593 local $_ = $$refcurrent;
1594 my $line = $$refline;
1595 my $column = $$refcolumn;
1599 $self->_parse_c("const", \$_, \$line, \$column);
1603 } elsif($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1605 } elsif($self->_parse_c('(?:enum\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1606 \$_, \$line, \$column, \$type))
1616 $$refcolumn = $column;
1623 ########################################################################
1626 sub parse_c_typedef {
1629 my $create_type = \${$self->{CREATE_TYPE}};
1630 my $found_type = \${$self->{FOUND_TYPE}};
1631 my $preprocessor_condition = \${$self->{PREPROCESSOR_CONDITION}};
1633 my $refcurrent = shift;
1634 my $refline = shift;
1635 my $refcolumn = shift;
1637 my $reftype = shift;
1639 local $_ = $$refcurrent;
1640 my $line = $$refline;
1641 my $column = $$refcolumn;
1647 } elsif (s/^(?:typedef\s+)?(enum\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1648 $self->_update_c_position($&, \$line, \$column);
1651 my $_name = $2 || "";
1655 if ($kind =~ /^struct|union$/) {
1657 my @field_type_names = ();
1658 my @field_names = ();
1661 while ($_ && $self->_parse_c_on_same_level_until_one_of(';', \$_, \$line, \$column, \$match))
1664 my $field_type_name;
1667 if ($self->parse_c_variable(\$match, \$line, \$column, \$field_linkage, \$field_type_name, \$field_name)) {
1668 $field_type_name =~ s/\s+/ /g;
1670 push @field_type_names, $field_type_name;
1671 push @field_names, $field_name;
1672 # $output->write("$kind:$_name:$field_type_name:$field_name\n");
1674 $self->_parse_c_error($_, $line, $column, "typedef $kind: '$match'");
1677 if ($self->_parse_c(';', \$_, \$line, \$column)) {
1679 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1682 my $tuple_line = $line;
1683 my $tuple_column = $column - 1;
1687 my @argument_columns;
1689 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1690 \@arguments, \@argument_lines, \@argument_columns))
1692 $self->_parse_c_error($_, $line, $column, "typedef $kind");
1696 if ($#arguments >= 0) {
1697 $name = $arguments[0];
1702 $self->_parse_c_error($_, $line, $column, "typedef $kind");
1706 my $type = &$$create_type();
1709 $type->_name($_name);
1711 $type->field_type_names([@field_type_names]);
1712 $type->field_names([@field_names]);
1714 &$$found_type($type);
1719 while ($self->_parse_c_on_same_level_until_one_of(',', \$_, \$line, \$column, \$match)) {
1721 if ($match !~ /^(\w+)\s*(?:=\s*(.*?)\s*)?$/) {
1722 $self->_parse_c_error($_, $line, $column, "typedef $kind");
1725 my $enum_value = $2 || "";
1727 # $output->write("$kind:$_name:$enum_name:$enum_value\n");
1730 if ($self->_parse_c(',', \$_, \$line, \$column)) {
1732 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1735 my $tuple_line = $line;
1736 my $tuple_column = $column - 1;
1740 my @argument_columns;
1742 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1743 \@arguments, \@argument_lines, \@argument_columns))
1745 $self->_parse_c_error($_, $line, $column, "typedef $kind");
1749 if ($#arguments >= 0) {
1750 $name = $arguments[0];
1755 $self->_parse_c_error($_, $line, $column, "typedef $kind");
1759 # FIXME: Not correct
1760 # $output->write("typedef:$name:$_name\n");
1762 } elsif ($self->_parse_c("typedef", \$_, \$line, \$column)) {
1767 if ($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type_name, \$name)) {
1768 $type_name =~ s/\s+/ /g;
1770 if(defined($type_name) && defined($name)) {
1771 my $type = &$$create_type();
1773 if (length($name) == 0) {
1774 $self->_parse_c_error($_, $line, $column, "typedef");
1780 $type->field_type_names([$type_name]);
1781 $type->field_names([""]);
1783 &$$found_type($type);
1786 if (0 && $_ && !/^,/) {
1787 $self->_parse_c_error($_, $line, $column, "typedef");
1790 $self->_parse_c_error($_, $line, $column, "typedef");
1792 } elsif (0 && $self->_parse_c("typedef", \$_, \$line, \$column)) {
1794 $self->_parse_c('\w+', \$_, \$line, \$column, \$type_name);
1797 $self->_parse_c('\w+', \$_, \$line, \$column, \$name);
1805 $$refcolumn = $column;
1812 ########################################################################
1815 sub parse_c_variable {
1818 my $found_variable = \${$self->{FOUND_VARIABLE}};
1820 my $refcurrent = shift;
1821 my $refline = shift;
1822 my $refcolumn = shift;
1824 my $reflinkage = shift;
1825 my $reftype = shift;
1826 my $refname = shift;
1828 local $_ = $$refcurrent;
1829 my $line = $$refline;
1830 my $column = $$refcolumn;
1832 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1834 my $begin_line = $line;
1835 my $begin_column = $column + 1;
1841 # $self->_parse_c_warning($_, $line, $column, "variable");
1844 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1845 'signed(?=\s+__int64\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1846 'unsigned(?=\s+__int64\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1847 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1848 \$_, \$line, \$column, \$match))
1850 if($match =~ /^extern|static$/) {
1863 } elsif(s/^(enum|struct|union)(?:\s+(\w+))?\s*\{//s) {
1866 $self->_update_c_position($&, \$line, \$column);
1868 if(defined($_name)) {
1869 $type = "$kind $_name { }";
1871 $type = "$kind { }";
1875 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+(?:\s*\*)*)\s*(\w+)\s*(\[.*?\]$|:\s*(\d+)$|\{)?//s) {
1891 $type = $self->_format_c_type($type);
1894 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+(?:\s*\*)*\s*\(\s*(?:\*\s*)*)(\w+)\s*(\)\(.*?\))$//s) {
1895 $type = $self->_format_c_type("$1$3");
1900 $type = $self->_format_c_type("$1$3");
1905 $self->_parse_c_warning($_, $line, $column, "variable", "'$_'");
1910 } elsif($self->_parse_c('SEQ_DEFINEBUF', \$_, \$line, \$column, \$match)) { # Linux specific
1913 } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
1916 } elsif($self->_parse_c('DEFINE_REGS_ENTRYPOINT_\w+|DPQ_DECL_\w+|HANDLER_DEF|IX86_ONLY', # Wine specific
1917 \$_, \$line, \$column, \$match))
1921 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
1924 } elsif(s/^(enum|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*//s) {
1927 $self->_update_c_position($&, \$line, \$column);
1929 if(defined($_name)) {
1930 $type = "struct $_name { }";
1932 $type = "struct { }";
1934 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
1941 # $output->write("*** $type: '$_'\n");
1943 # $self->_parse_c_warning($_, $line, $column, "variable2", "");
1947 } elsif(s/^WINAPI\s*//) {
1948 $self->_update_c_position($&, \$line, \$column);
1949 } elsif(s/^WINE_UNUSED\s*//) {
1950 $self->_update_c_position($&, \$line, \$column);
1955 } elsif(s/^(\((?:__cdecl|PASCAL|WINAPI)?\s*\*?\s*(?:__cdecl|PASCAL|WINAPI)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
1956 $self->_update_c_position($&, \$line, \$column);
1961 $self->_parse_c_until_one_of("\\)", \$_, \$line, \$column);
1962 if(s/^\)//) { $column++; }
1963 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1965 if(!s/^(?:=\s*|,\s*|$)//) {
1968 } elsif(s/^(?:\*\s*)*(?:const\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
1969 $self->_update_c_position($&, \$line, \$column);
1979 # $output->write("$type: $name: '$_'\n");
1981 if(1 || $finished) {
1983 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(.*?\)', \$_, \$line, \$column, \$match)) {
1986 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+)\s*
1987 (?:\*\s*)*(\w+|\s*\*?\s*\w+\s*\))\s*(?:\[[^\]]*\]|\([^\)]*\))?
1988 (?:,\s*(?:\*\s*)*(\w+)\s*(?:\[[^\]]*\])?)*
1991 $self->_update_c_position($&, \$line, \$column);
1997 $type =~ s/^struct/struct /;
1998 } elsif(/^(enum|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*((?:\*\s*)*)(\w+)\s*(?:=|$)/s) {
1999 $self->_update_c_position($&, \$line, \$column);
2006 if(defined($_name)) {
2007 $type = "struct $_name { }";
2009 $type = "struct { }";
2022 $$refcolumn = $column;
2024 $$reflinkage = $linkage;
2028 if(&$$found_variable($begin_line, $begin_column, $linkage, $type, $name))