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;
785 while($plevel > 0 || $blevel > 0) {
787 $self->_parse_c_until_one_of("#/\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
789 if($line != $previous_line) {
790 &$$found_line($line);
791 } elsif(0 && $column == $previous_column) {
792 $self->_parse_c_error($_, $line, $column, "file", "no progress");
794 # &$$found_line("$line.$column");
796 $previous_line = $line;
797 $previous_column = $column;
799 if($match !~ /^\s+$/s && $options->debug) {
800 $self->_parse_c_warning($_, $line, $column, "file", "$plevel $blevel: '$declaration' '$match'");
803 if(!$declaration && $match =~ s/^\s+//s) {
804 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
808 $declaration .= $match;
811 if ($declaration =~ s/^extern\s*\"C\"//s) {
813 $self->_update_c_position($&, \$line, \$column);
815 $declaration_line = $line;
816 $declaration_column = $column;
821 } elsif ($extern_c && $blevel == 1 && $plevel == 1 && !$declaration) {
823 $self->_update_c_position($&, \$line, \$column);
825 $declaration_line = $line;
826 $declaration_column = $column;
831 } elsif($declaration =~ s/^(?:__DEFINE_(?:GET|SET)_SEG|OUR_GUID_ENTRY)\s*(?=\()//sx) { # FIXME: Wine specific kludge
833 if ($plevel > 2 || !s/^\)//) {
834 $declaration = "$prefix$declaration";
837 $self->_update_c_position($&, \$line, \$column);
842 my @argument_columns;
844 if(!$self->parse_c_tuple(\$declaration, \$declaration_line, \$declaration_column,
845 \@arguments, \@argument_lines, \@argument_columns))
847 $self->_parse_c_error($declaration, $declaration_line, $declaration_column, "file", "tuple expected");
851 $declaration_line = $line;
852 $declaration_column = $column;
856 } elsif ($declaration =~ s/^(?:DEFINE_SHLGUID)\s*\(.*?\)//s) {
857 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
858 } elsif ($declaration =~ s/^(?:DECL_WINELIB_TYPE_AW|DECLARE_HANDLE(?:16)?|TYPE_MARSHAL)\(\s*(\w+)\s*\)\s*//s) {
859 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
860 } elsif ($declaration =~ s/^ICOM_DEFINE\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*//s) {
861 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
867 while(s/^.*?\n//) { $blank_lines++; }
870 $declaration_line = $line;
871 $declaration_column = $column;
873 $declaration .= "\n" x $blank_lines;
881 my $preprocessor_line = $line;
882 my $preprocessor_column = $column;
884 my $preprocessor = $&;
885 while(s/^(.*?)\\\s*\n//) {
887 $preprocessor .= "$1\n";
889 if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
892 $preprocessor .= "$1$3";
896 } elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
905 if($if0 && $preprocessor =~ /^\#\s*endif/) {
913 } elsif($preprocessor =~ /^\#\s*if/) {
914 if($preprocessor =~ /^\#\s*if\s*0/) {
921 if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
926 if(s/^\/\*.*?\*\///s) {
927 &$$found_comment($line, $column + 1, $&);
933 $column += length($_);
935 } elsif(s/^\/\/(.*?)\n//) {
936 &$$found_comment($line, $column + 1, $&);
945 $line += $blank_lines;
946 if($blank_lines > 0) {
951 $declaration_line = $line;
952 $declaration_column = $column;
953 } elsif($blank_lines > 0) {
954 $declaration .= "\n" x $blank_lines;
976 $self->_parse_c_error($_, $line, $column, "file", ") without (");
979 if($plevel == 1 && $declaration =~ /^__ASM_GLOBAL_FUNC/) {
980 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
983 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
985 $declaration_line = $line;
986 $declaration_column = $column;
994 $self->_parse_c_error($_, $line, $column, "file", "} without {");
999 if($declaration =~ /^typedef/s ||
1000 $declaration =~ /^(?:const\s+|extern\s+|static\s+)*(?:struct|union)(?:\s+\w+)?\s*\{/s)
1003 } elsif($plevel == 1 && $blevel == 1) {
1004 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1007 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1009 $declaration_line = $line;
1010 $declaration_column = $column;
1011 } elsif($column == 1 && !$extern_c) {
1012 $self->_parse_c_error("", $line, $column, "file", "inner } ends on column 1");
1016 if(0 && $blevel == 1 &&
1017 $declaration !~ /^typedef/ &&
1018 $declaration !~ /^(?:const\s+|extern\s+|static\s+)?(?:struct|union)(?:\s+\w+)?\s*\{/s &&
1019 $declaration =~ /^(?:\w+(?:\s*\*)*\s+)*(\w+)\s*\(\s*(?:(?:\w+\s*,\s*)*(\w+))?\s*\)\s*(.*?);$/s &&
1020 $1 ne "ICOM_VTABLE" && defined($2) && $2 ne "void" && $3) # K&R
1022 $self->_parse_c_warning("", $line, $column, "file", "function $1: warning: function has K&R format");
1023 } elsif($plevel == 1 && $blevel == 1) {
1024 $declaration =~ s/\s*;$//;
1025 if($declaration && !$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1028 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1030 $declaration_line = $line;
1031 $declaration_column = $column;
1033 } elsif(/^\s*$/ && $declaration =~ /^\s*$/ && $match =~ /^\s*$/) {
1037 $self->_parse_c_error($_, $line, $column, "file", "parse error: '$declaration' '$match'");
1043 $$refcolumn = $column;
1048 ########################################################################
1051 sub parse_c_function {
1054 my $file = \${$self->{FILE}};
1055 my $create_function = \${$self->{CREATE_FUNCTION}};
1057 my $refcurrent = shift;
1058 my $refline = shift;
1059 my $refcolumn = shift;
1061 my $reffunction = shift;
1063 local $_ = $$refcurrent;
1064 my $line = $$refline;
1065 my $column = $$refcolumn;
1068 my $calling_convention = "";
1073 my @argument_columns;
1075 my $statements_line;
1076 my $statements_column;
1078 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1080 my $begin_line = $line;
1081 my $begin_column = $column + 1;
1085 } elsif($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
1089 # $self->_parse_c_warning($_, $line, $column, "function", "");
1092 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1093 'signed(?=\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1094 'unsigned(?=\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1095 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1096 \$_, \$line, \$column, \$match))
1098 if($match =~ /^extern|static$/) {
1107 } elsif($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1109 } elsif($self->_parse_c('WINE_EXCEPTION_FILTER\(\w+\)', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1112 if(!$self->parse_c_type(\$_, \$line, \$column, \$return_type)) {
1116 $self->_parse_c('inline|FAR', \$_, \$line, \$column);
1118 $self->_parse_c("__cdecl|__stdcall|__RPC_STUB|" .
1119 "CALLBACK|CDECL|PASCAL|" .
1120 "RPC_ENTRY|RPC_VAR_ENTRY|" .
1121 "VFWAPIV|VFWAPI|WINAPIV|WINAPI|" .
1123 \$_, \$line, \$column, \$calling_convention);
1126 # FIXME: ???: Old variant of __attribute((const))
1127 $self->_parse_c('const', \$_, \$line, \$column);
1129 if(!$self->_parse_c('(?:operator\s*!=|(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)', \$_, \$line, \$column, \$name)) {
1135 $self->_update_c_position($&, \$line, \$column);
1139 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1145 $self->_update_c_position($&, \$line, \$column);
1147 $self->_parse_c_error($_, $line, $column, "function");
1155 } elsif($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
1160 # FIXME: Implement proper handling of K&R C functions
1161 $self->_parse_c_until_one_of("{", \$_, \$line, \$column, $kar);
1164 $output->write("K&R: $kar\n");
1167 if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1171 my $end_line = $line;
1172 my $end_column = $column;
1176 $$refcolumn = $column;
1178 my $function = &$$create_function;
1180 $function->file($$file);
1181 $function->begin_line($begin_line);
1182 $function->begin_column($begin_column);
1183 $function->end_line($end_line);
1184 $function->end_column($end_column);
1185 $function->linkage($linkage);
1186 $function->return_type($return_type);
1187 $function->calling_convention($calling_convention);
1188 $function->name($name);
1189 # if(defined($argument_types)) {
1190 # $function->argument_types([@$argument_types]);
1192 # if(defined($argument_names)) {
1193 # $function->argument_names([@$argument_names]);
1195 $function->statements_line($statements_line);
1196 $function->statements_column($statements_column);
1197 $function->statements($statements);
1199 $$reffunction = $function;
1204 ########################################################################
1205 # parse_c_function_call
1207 sub parse_c_function_call {
1210 my $refcurrent = shift;
1211 my $refline = shift;
1212 my $refcolumn = shift;
1214 my $refname = shift;
1215 my $refarguments = shift;
1216 my $refargument_lines = shift;
1217 my $refargument_columns = shift;
1219 local $_ = $$refcurrent;
1220 my $line = $$refline;
1221 my $column = $$refcolumn;
1226 my @argument_columns;
1228 if(s/^(\w+)(\s*)(?=\()//s) {
1229 $self->_update_c_position($&, \$line, \$column);
1233 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1242 $$refcolumn = $column;
1245 @$refarguments = @arguments;
1246 @$refargument_lines = @argument_lines;
1247 @$refargument_columns = @argument_columns;
1252 ########################################################################
1253 # parse_c_preprocessor
1255 sub parse_c_preprocessor {
1258 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
1260 my $refcurrent = shift;
1261 my $refline = shift;
1262 my $refcolumn = shift;
1264 local $_ = $$refcurrent;
1265 my $line = $$refline;
1266 my $column = $$refcolumn;
1268 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1270 my $begin_line = $line;
1271 my $begin_column = $column + 1;
1273 if(!&$$found_preprocessor($begin_line, $begin_column, "$_")) {
1279 } elsif(/^\#\s*define\s*(.*?)$/s) {
1280 $self->_update_c_position($_, \$line, \$column);
1281 } elsif(/^\#\s*else/s) {
1282 $self->_update_c_position($_, \$line, \$column);
1283 } elsif(/^\#\s*endif/s) {
1284 $self->_update_c_position($_, \$line, \$column);
1285 } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1286 $self->_update_c_position($_, \$line, \$column);
1287 } elsif(/^\#\s*include\s+(.*?)$/s) {
1288 $self->_update_c_position($_, \$line, \$column);
1289 } elsif(/^\#\s*undef\s+(.*?)$/s) {
1290 $self->_update_c_position($_, \$line, \$column);
1292 $self->_parse_c_error($_, $line, $column, "preprocessor");
1297 $$refcolumn = $column;
1302 ########################################################################
1305 sub parse_c_statement {
1308 my $refcurrent = shift;
1309 my $refline = shift;
1310 my $refcolumn = shift;
1312 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1314 local $_ = $$refcurrent;
1315 my $line = $$refline;
1316 my $column = $$refcolumn;
1318 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1320 $self->_parse_c('(?:case\s+)?(\w+)\s*:\s*', \$_, \$line, \$column);
1322 # $output->write("$line.$column: statement: '$_'\n");
1328 my $statements_line;
1329 my $statements_column;
1330 if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1333 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1336 } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
1337 $self->_update_c_position($&, \$line, \$column);
1343 my @argument_columns;
1344 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1348 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1349 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1352 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1354 while(defined(my $argument = shift @arguments) &&
1355 defined(my $argument_line = shift @argument_lines) &&
1356 defined(my $argument_column = shift @argument_columns))
1358 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1360 } elsif(s/^else//) {
1361 $self->_update_c_position($&, \$line, \$column);
1362 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1365 } elsif(s/^return//) {
1366 $self->_update_c_position($&, \$line, \$column);
1367 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1368 if(!$self->parse_c_expression(\$_, \$line, \$column)) {
1371 } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1374 # $self->_parse_c_error($_, $line, $column, "statement");
1377 $self->_update_c_position($_, \$line, \$column);
1381 $$refcolumn = $column;
1386 ########################################################################
1387 # parse_c_statements
1389 sub parse_c_statements {
1392 my $refcurrent = shift;
1393 my $refline = shift;
1394 my $refcolumn = shift;
1396 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1398 local $_ = $$refcurrent;
1399 my $line = $$refline;
1400 my $column = $$refcolumn;
1402 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1404 # $output->write("$line.$column: statements: '$_'\n");
1407 my $statement_line = $line;
1408 my $statement_column = $column;
1410 my $previous_line = -1;
1411 my $previous_column = -1;
1415 while($plevel > 0 || $blevel > 0) {
1417 $self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1419 if($previous_line == $line && $previous_column == $column) {
1420 $self->_parse_c_error($_, $line, $column, "statements", "no progress");
1422 $previous_line = $line;
1423 $previous_column = $column;
1425 # $output->write("'$match' '$_'\n");
1427 $statement .= $match;
1432 } elsif(s/^[\)\]]//) {
1435 $self->_parse_c_error($_, $line, $column, "statements");
1445 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1448 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1450 $statement_line = $line;
1451 $statement_column = $column;
1454 if($plevel == 1 && $blevel == 1) {
1455 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1459 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1461 $statement_line = $line;
1462 $statement_column = $column;
1466 } elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
1470 $self->_parse_c_error($_, $line, $column, "statements");
1474 $self->_update_c_position($_, \$line, \$column);
1478 $$refcolumn = $column;
1483 ########################################################################
1489 my $refcurrent = shift;
1490 my $refline = shift;
1491 my $refcolumn = shift;
1493 # FIXME: Should not write directly
1495 my $item_lines = shift;
1496 my $item_columns = shift;
1498 local $_ = $$refcurrent;
1500 my $line = $$refline;
1501 my $column = $$refcolumn;
1511 my $item_line = $line;
1512 my $item_column = $column + 1;
1515 while($plevel > 0) {
1517 $self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1525 push @$item_lines, $item_line;
1526 push @$item_columns, $item_column;
1527 push @$items, $item;
1537 push @$item_lines, $item_line;
1538 push @$item_columns, $item_column;
1539 push @$items, $item;
1540 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1542 $item_column = $column + 1;
1554 $$refcolumn = $column;
1559 ########################################################################
1565 my $refcurrent = shift;
1566 my $refline = shift;
1567 my $refcolumn = shift;
1569 my $reftype = shift;
1571 local $_ = $$refcurrent;
1572 my $line = $$refline;
1573 my $column = $$refcolumn;
1577 $self->_parse_c("const", \$_, \$line, \$column);
1581 } elsif($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1583 } elsif($self->_parse_c('(?:enum\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1584 \$_, \$line, \$column, \$type))
1594 $$refcolumn = $column;
1601 ########################################################################
1604 sub parse_c_typedef {
1607 my $create_type = \${$self->{CREATE_TYPE}};
1608 my $found_type = \${$self->{FOUND_TYPE}};
1610 my $refcurrent = shift;
1611 my $refline = shift;
1612 my $refcolumn = shift;
1614 my $reftype = shift;
1616 local $_ = $$refcurrent;
1617 my $line = $$refline;
1618 my $column = $$refcolumn;
1624 } elsif (s/^(?:typedef\s+)?(enum\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1625 $self->_update_c_position($&, \$line, \$column);
1628 my $_name = $2 || "";
1632 if ($kind =~ /^struct|union$/) {
1634 my @field_types = ();
1635 my @field_names = ();
1638 while ($self->_parse_c_on_same_level_until_one_of(';', \$_, \$line, \$column, \$match))
1644 if ($self->parse_c_variable(\$match, \$line, \$column, \$field_linkage, \$field_type, \$field_name)) {
1645 $field_type =~ s/\s+/ /g;
1647 push @field_types, $field_type;
1648 push @field_names, $field_name;
1649 # $output->write("$kind:$_name:$field_type:$field_name\n");
1652 if ($self->_parse_c(';', \$_, \$line, \$column)) {
1654 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1657 my $tuple_line = $line;
1658 my $tuple_column = $column - 1;
1662 my @argument_columns;
1664 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1665 \@arguments, \@argument_lines, \@argument_columns))
1667 $self->_parse_c_error($_, $line, $column, "typedef $kind");
1671 if ($#arguments >= 0) {
1672 $name = $arguments[0];
1677 $self->_parse_c_error($_, $line, $column, "typedef $kind");
1681 my $type = &$$create_type();
1684 $type->_name($_name);
1686 $type->field_types([@field_types]);
1687 $type->field_names([@field_names]);
1689 &$$found_type($type);
1694 while ($self->_parse_c_on_same_level_until_one_of(',', \$_, \$line, \$column, \$match)) {
1696 if ($match !~ /^(\w+)\s*(?:=\s*(.*?)\s*)?$/) {
1697 $self->_parse_c_error($_, $line, $column, "typedef $kind");
1700 my $enum_value = $2 || "";
1702 # $output->write("$kind:$_name:$enum_name:$enum_value\n");
1705 if ($self->_parse_c(',', \$_, \$line, \$column)) {
1707 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1710 my $tuple_line = $line;
1711 my $tuple_column = $column - 1;
1715 my @argument_columns;
1717 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1718 \@arguments, \@argument_lines, \@argument_columns))
1720 $self->_parse_c_error($_, $line, $column, "typedef $kind");
1724 if ($#arguments >= 0) {
1725 $name = $arguments[0];
1730 $self->_parse_c_error($_, $line, $column, "typedef $kind");
1734 # FIXME: Not correct
1735 # $output->write("typedef:$name:$_name\n");
1738 } elsif ($self->_parse_c("typedef", \$_, \$line, \$column)) {
1746 $$refcolumn = $column;
1753 ########################################################################
1756 sub parse_c_variable {
1759 my $found_variable = \${$self->{FOUND_VARIABLE}};
1761 my $refcurrent = shift;
1762 my $refline = shift;
1763 my $refcolumn = shift;
1765 my $reflinkage = shift;
1766 my $reftype = shift;
1767 my $refname = shift;
1769 local $_ = $$refcurrent;
1770 my $line = $$refline;
1771 my $column = $$refcolumn;
1773 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1775 my $begin_line = $line;
1776 my $begin_column = $column + 1;
1782 # $self->_parse_c_warning($_, $line, $column, "variable");
1785 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1786 'signed(?=\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1787 'unsigned(?=\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1788 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1789 \$_, \$line, \$column, \$match))
1791 if($match =~ /^extern|static$/) {
1802 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+\s*(?:\*\s*)*)(\w+)$//s) {
1803 $type = $self->_format_c_type($1);
1807 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+\s*(?:\*\s*)*\(\s*(?:\*\s*)*)(\w+)\s*(\)\(.*?\))$//s) {
1808 $type = $self->_format_c_type("$1$3");
1816 } elsif($self->_parse_c('SEQ_DEFINEBUF', \$_, \$line, \$column, \$match)) { # Linux specific
1819 } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
1822 } elsif($self->_parse_c('DEFINE_REGS_ENTRYPOINT_\w+|DPQ_DECL_\w+|HANDLER_DEF|IX86_ONLY', # Wine specific
1823 \$_, \$line, \$column, \$match))
1827 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
1830 } elsif(s/^(?:enum\s+|struct\s+|union\s+)(\w+)?\s*\{.*?\}\s*//s) {
1831 $self->_update_c_position($&, \$line, \$column);
1834 $type = "struct $1 { }";
1836 $type = "struct { }";
1845 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
1852 # $output->write("*** $type: '$_'\n");
1854 # $self->_parse_c_warning($_, $line, $column, "variable2", "");
1858 } elsif(s/^WINAPI\s*//) {
1859 $self->_update_c_position($&, \$line, \$column);
1860 } elsif(s/^WINE_UNUSED\s*//) {
1861 $self->_update_c_position($&, \$line, \$column);
1866 } elsif(s/^(\((?:__cdecl|PASCAL|WINAPI)?\s*\*?\s*(?:__cdecl|PASCAL|WINAPI)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
1867 $self->_update_c_position($&, \$line, \$column);
1872 $self->_parse_c_until_one_of("\\)", \$_, \$line, \$column);
1873 if(s/^\)//) { $column++; }
1874 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1876 if(!s/^(?:=\s*|,\s*|$)//) {
1879 } elsif(s/^(?:\*\s*)*(?:const\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
1880 $self->_update_c_position($&, \$line, \$column);
1890 # $output->write("$type: $name: '$_'\n");
1894 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(.*?\)', \$_, \$line, \$column, \$match)) {
1897 } elsif(s/^((?:enum\s+|struct\s+|union\s+)?\w+)\s*
1898 (?:\*\s*)*(\w+|\s*\*?\s*\w+\s*\))\s*(?:\[[^\]]*\]|\([^\)]*\))?
1899 (?:,\s*(?:\*\s*)*(\w+)\s*(?:\[[^\]]*\])?)*
1902 $self->_update_c_position($&, \$line, \$column);
1908 $type =~ s/^struct/struct /;
1909 } elsif(/^(?:enum|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*((?:\*\s*)*)(\w+)\s*(?:=|$)/s) {
1910 $self->_update_c_position($&, \$line, \$column);
1913 $type = "struct $1 { }";
1915 $type = "struct { }";
1934 $$refcolumn = $column;
1936 $$reflinkage = $linkage;
1940 if(&$$found_variable($begin_line, $begin_column, $linkage, $type, $name))