push e1d8a1293d44015bb0894687d02c5c53339996f7
[wine/hacks.git] / tools / winapi / c_parser.pm
blob1de62ee3afc08db5dee9c5610aeec2ad31d38bb3
2 # Copyright 1999, 2000, 2001 Patrik Stridvall
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 package c_parser;
21 use strict;
23 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
24 require Exporter;
26 @ISA = qw(Exporter);
27 @EXPORT = qw();
28 @EXPORT_OK = qw();
30 use options qw($options);
31 use output qw($output);
33 use c_function;
34 use c_type;
36 # Defined a couple common regexp tidbits
37 my $CALL_CONVENTION="__cdecl|__stdcall|" .
38 "__RPC_API|__RPC_STUB|__RPC_USER|" .
39 "CALLBACK|CDECL|NTAPI|PASCAL|RPC_ENTRY|RPC_VAR_ENTRY|" .
40 "VFWAPI|VFWAPIV|WINAPI|WINAPIV|APIENTRY|";
43 sub parse_c_function($$$$$);
44 sub parse_c_function_call($$$$$$$$);
45 sub parse_c_preprocessor($$$$);
46 sub parse_c_statements($$$$);
47 sub parse_c_tuple($$$$$$$);
48 sub parse_c_type($$$$$);
49 sub parse_c_typedef($$$$);
50 sub parse_c_variable($$$$$$$);
53 ########################################################################
54 # new
56 sub new($$) {
57 my $proto = shift;
58 my $class = ref($proto) || $proto;
59 my $self = {};
60 bless ($self, $class);
62 my $file = \${$self->{FILE}};
63 my $create_function = \${$self->{CREATE_FUNCTION}};
64 my $create_type = \${$self->{CREATE_TYPE}};
65 my $found_comment = \${$self->{FOUND_COMMENT}};
66 my $found_declaration = \${$self->{FOUND_DECLARATION}};
67 my $found_function = \${$self->{FOUND_FUNCTION}};
68 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
69 my $found_line = \${$self->{FOUND_LINE}};
70 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
71 my $found_statement = \${$self->{FOUND_STATEMENT}};
72 my $found_type = \${$self->{FOUND_TYPE}};
73 my $found_variable = \${$self->{FOUND_VARIABLE}};
75 $$file = shift;
77 $$create_function = sub { return new c_function; };
78 $$create_type = sub { return new c_type; };
79 $$found_comment = sub { return 1; };
80 $$found_declaration = sub { return 1; };
81 $$found_function = sub { return 1; };
82 $$found_function_call = sub { return 1; };
83 $$found_line = sub { return 1; };
84 $$found_preprocessor = sub { return 1; };
85 $$found_statement = sub { return 1; };
86 $$found_type = sub { return 1; };
87 $$found_variable = sub { return 1; };
89 return $self;
92 ########################################################################
93 # set_found_comment_callback
95 sub set_found_comment_callback($$) {
96 my $self = shift;
98 my $found_comment = \${$self->{FOUND_COMMENT}};
100 $$found_comment = shift;
103 ########################################################################
104 # set_found_declaration_callback
106 sub set_found_declaration_callback($$) {
107 my $self = shift;
109 my $found_declaration = \${$self->{FOUND_DECLARATION}};
111 $$found_declaration = shift;
114 ########################################################################
115 # set_found_function_callback
117 sub set_found_function_callback($$) {
118 my $self = shift;
120 my $found_function = \${$self->{FOUND_FUNCTION}};
122 $$found_function = shift;
125 ########################################################################
126 # set_found_function_call_callback
128 sub set_found_function_call_callback($$) {
129 my $self = shift;
131 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
133 $$found_function_call = shift;
136 ########################################################################
137 # set_found_line_callback
139 sub set_found_line_callback($$) {
140 my $self = shift;
142 my $found_line = \${$self->{FOUND_LINE}};
144 $$found_line = shift;
147 ########################################################################
148 # set_found_preprocessor_callback
150 sub set_found_preprocessor_callback($$) {
151 my $self = shift;
153 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
155 $$found_preprocessor = shift;
158 ########################################################################
159 # set_found_statement_callback
161 sub set_found_statement_callback($$) {
162 my $self = shift;
164 my $found_statement = \${$self->{FOUND_STATEMENT}};
166 $$found_statement = shift;
169 ########################################################################
170 # set_found_type_callback
172 sub set_found_type_callback($$) {
173 my $self = shift;
175 my $found_type = \${$self->{FOUND_TYPE}};
177 $$found_type = shift;
180 ########################################################################
181 # set_found_variable_callback
183 sub set_found_variable_callback($$) {
184 my $self = shift;
186 my $found_variable = \${$self->{FOUND_VARIABLE}};
188 $$found_variable = shift;
192 ########################################################################
193 # _format_c_type
195 sub _format_c_type($$) {
196 my $self = shift;
198 local $_ = shift;
199 s/^\s*(.*?)\s*$/$1/;
201 if (/^(\w+(?:\s*\*)*)\s*\(\s*\*\s*\)\s*\(\s*(.*?)\s*\)$/s) {
202 my $return_type = $1;
203 my @arguments = split(/\s*,\s*/, $2);
204 foreach my $argument (@arguments) {
205 if ($argument =~ s/^(\w+(?:\s*\*)*)\s*\w+$/$1/) {
206 $argument =~ s/\s+/ /g;
207 $argument =~ s/\s*\*\s*/*/g;
208 $argument =~ s/(\*+)$/ $1/;
212 $_ = "$return_type (*)(" . join(", ", @arguments) . ")";
215 return $_;
219 ########################################################################
220 # _parse_c_warning
222 # FIXME: Use caller (See man perlfunc)
224 sub _parse_c_warning($$$$$$) {
225 my $self = shift;
227 local $_ = shift;
228 my $line = shift;
229 my $column = shift;
230 my $context = shift;
231 my $message = shift;
233 my $file = \${$self->{FILE}};
235 $message = "warning" if !$message;
237 my $current = "";
238 if($_) {
239 my @lines = split(/\n/, $_);
241 $current .= $lines[0] . "\n" if $lines[0];
242 $current .= $lines[1] . "\n" if $lines[1];
245 if($current) {
246 $output->write("$$file:$line." . ($column + 1) . ": $context: $message: \\\n$current");
247 } else {
248 $output->write("$$file:$line." . ($column + 1) . ": $context: $message\n");
252 ########################################################################
253 # _parse_c_error
255 sub _parse_c_error($$$$$$) {
256 my $self = shift;
258 local $_ = shift;
259 my $line = shift;
260 my $column = shift;
261 my $context = shift;
262 my $message = shift;
264 $message = "parse error" if !$message;
266 # Why did I do this?
267 if($output->prefix) {
268 # $output->write("\n");
269 $output->prefix("");
272 $self->_parse_c_warning($_, $line, $column, $context, $message);
274 exit 1;
277 ########################################################################
278 # _update_c_position
280 sub _update_c_position($$$$) {
281 my $self = shift;
283 local $_ = shift;
284 my $refline = shift;
285 my $refcolumn = shift;
287 my $line = $$refline;
288 my $column = $$refcolumn;
290 while($_) {
291 if(s/^[^\n\t\'\"]*//s) {
292 $column += length($&);
295 if(s/^\'//) {
296 $column++;
297 while(/^./ && !s/^\'//) {
298 s/^([^\'\\]*)//s;
299 $column += length($1);
300 if(s/^\\//) {
301 $column++;
302 if(s/^(.)//s) {
303 $column += length($1);
304 if($1 eq "0") {
305 s/^(\d{0,3})//s;
306 $column += length($1);
311 $column++;
312 } elsif(s/^\"//) {
313 $column++;
314 while(/^./ && !s/^\"//) {
315 s/^([^\"\\]*)//s;
316 $column += length($1);
317 if(s/^\\//) {
318 $column++;
319 if(s/^(.)//s) {
320 $column += length($1);
321 if($1 eq "0") {
322 s/^(\d{0,3})//s;
323 $column += length($1);
328 $column++;
329 } elsif(s/^\n//) {
330 $line++;
331 $column = 0;
332 } elsif(s/^\t//) {
333 $column = $column + 8 - $column % 8;
337 $$refline = $line;
338 $$refcolumn = $column;
341 ########################################################################
342 # __parse_c_until_one_of
344 sub __parse_c_until_one_of($$$$$$$) {
345 my $self = shift;
347 my $characters = shift;
348 my $on_same_level = shift;
349 my $refcurrent = shift;
350 my $refline = shift;
351 my $refcolumn = shift;
352 my $match = shift;
354 local $_ = $$refcurrent;
355 my $line = $$refline;
356 my $column = $$refcolumn;
359 if(!defined($match)) {
360 my $blackhole;
361 $match = \$blackhole;
364 my $level = 0;
365 $$match = "";
366 while(/^[^$characters]/s || $level > 0) {
367 my $submatch = "";
369 if ($level > 0) {
370 if(s/^[^\(\)\[\]\{\}\n\t\'\"]*//s) {
371 $submatch .= $&;
373 } elsif ($on_same_level) {
374 if(s/^[^$characters\(\)\[\]\{\}\n\t\'\"]*//s) {
375 $submatch .= $&;
377 } else {
378 if(s/^[^$characters\n\t\'\"]*//s) {
379 $submatch .= $&;
383 if(s/^\'//) {
384 $submatch .= "\'";
385 while(/^./ && !s/^\'//) {
386 s/^([^\'\\]*)//s;
387 $submatch .= $1;
388 if(s/^\\//) {
389 $submatch .= "\\";
390 if(s/^(.)//s) {
391 $submatch .= $1;
392 if($1 eq "0") {
393 s/^(\d{0,3})//s;
394 $submatch .= $1;
399 $submatch .= "\'";
401 $$match .= $submatch;
402 $column += length($submatch);
403 } elsif(s/^\"//) {
404 $submatch .= "\"";
405 while(/^./ && !s/^\"//) {
406 s/^([^\"\\]*)//s;
407 $submatch .= $1;
408 if(s/^\\//) {
409 $submatch .= "\\";
410 if(s/^(.)//s) {
411 $submatch .= $1;
412 if($1 eq "0") {
413 s/^(\d{0,3})//s;
414 $submatch .= $1;
419 $submatch .= "\"";
421 $$match .= $submatch;
422 $column += length($submatch);
423 } elsif($on_same_level && s/^[\(\[\{]//) {
424 $level++;
426 $submatch .= $&;
427 $$match .= $submatch;
428 $column++;
429 } elsif($on_same_level && s/^[\)\]\}]//) {
430 if ($level > 0) {
431 $level--;
433 $submatch .= $&;
434 $$match .= $submatch;
435 $column++;
436 } else {
437 $_ = "$&$_";
438 $$match .= $submatch;
439 last;
441 } elsif(s/^\n//) {
442 $submatch .= "\n";
444 $$match .= $submatch;
445 $line++;
446 $column = 0;
447 } elsif(s/^\t//) {
448 $submatch .= "\t";
450 $$match .= $submatch;
451 $column = $column + 8 - $column % 8;
452 } else {
453 $$match .= $submatch;
454 $column += length($submatch);
458 $$refcurrent = $_;
459 $$refline = $line;
460 $$refcolumn = $column;
461 return 1;
464 ########################################################################
465 # _parse_c_until_one_of
467 sub _parse_c_until_one_of($$$$$$) {
468 my $self = shift;
470 my $characters = shift;
471 my $refcurrent = shift;
472 my $refline = shift;
473 my $refcolumn = shift;
474 my $match = shift;
476 return $self->__parse_c_until_one_of($characters, 0, $refcurrent, $refline, $refcolumn, $match);
479 ########################################################################
480 # _parse_c_on_same_level_until_one_of
482 sub _parse_c_on_same_level_until_one_of($$$$$$) {
483 my $self = shift;
485 my $characters = shift;
486 my $refcurrent = shift;
487 my $refline = shift;
488 my $refcolumn = shift;
489 my $match = shift;
491 return $self->__parse_c_until_one_of($characters, 1, $refcurrent, $refline, $refcolumn, $match);
494 ########################################################################
495 # parse_c_block
497 sub parse_c_block($$$$$$$) {
498 my $self = shift;
500 my $refcurrent = shift;
501 my $refline = shift;
502 my $refcolumn = shift;
504 my $refstatements = shift;
505 my $refstatements_line = shift;
506 my $refstatements_column = shift;
508 local $_ = $$refcurrent;
509 my $line = $$refline;
510 my $column = $$refcolumn;
512 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
514 my $statements;
515 if(s/^\{//) {
516 $column++;
517 $statements = "";
518 } else {
519 return 0;
522 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
524 my $statements_line = $line;
525 my $statements_column = $column;
527 my $plevel = 1;
528 while($plevel > 0) {
529 my $match;
530 $self->_parse_c_until_one_of("\\{\\}", \$_, \$line, \$column, \$match);
532 $column++;
534 $statements .= $match;
535 if(s/^\}//) {
536 $plevel--;
537 if($plevel > 0) {
538 $statements .= "}";
540 } elsif(s/^\{//) {
541 $plevel++;
542 $statements .= "{";
543 } else {
544 return 0;
548 $$refcurrent = $_;
549 $$refline = $line;
550 $$refcolumn = $column;
551 $$refstatements = $statements;
552 $$refstatements_line = $statements_line;
553 $$refstatements_column = $statements_column;
555 return 1;
558 ########################################################################
559 # parse_c_declaration
561 sub parse_c_declaration($$$$)
563 my ($self, $refcurrent, $refline, $refcolumn) = @_;
565 my $found_declaration = \${$self->{FOUND_DECLARATION}};
566 my $found_function = \${$self->{FOUND_FUNCTION}};
568 local $_ = $$refcurrent;
569 my $line = $$refline;
570 my $column = $$refcolumn;
572 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
574 my $begin_line = $line;
575 my $begin_column = $column + 1;
577 my $end_line = $begin_line;
578 my $end_column = $begin_column;
579 $self->_update_c_position($_, \$end_line, \$end_column);
581 if(!&$$found_declaration($begin_line, $begin_column, $end_line, $end_column, $_)) {
582 return 1;
585 # Function
586 my $function;
588 # Variable
589 my ($linkage, $type, $name);
591 if(s/^WINE_(?:DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\(\s*(\w+)\s*\)\s*//s) { # FIXME: Wine specific kludge
592 $self->_update_c_position($&, \$line, \$column);
593 } elsif(s/^__ASM_GLOBAL_FUNC\(\s*(\w+)\s*,\s*//s) { # FIXME: Wine specific kludge
594 $self->_update_c_position($&, \$line, \$column);
595 $self->_parse_c_until_one_of("\)", \$_, \$line, \$column);
596 if(s/\)//) {
597 $column++;
599 } elsif(s/^(?:DEFINE_AVIGUID|DEFINE_OLEGUID)\s*(?=\()//s) { # FIXME: Wine specific kludge
600 $self->_update_c_position($&, \$line, \$column);
602 my @arguments;
603 my @argument_lines;
604 my @argument_columns;
606 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
607 return 0;
609 } elsif(s/^DEFINE_COMMON_NOTIFICATIONS\(\s*(\w+)\s*,\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
610 $self->_update_c_position($&, \$line, \$column);
611 } elsif(s/^MAKE_FUNCPTR\(\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
612 $self->_update_c_position($&, \$line, \$column);
613 } elsif(s/^START_TEST\(\s*(\w+)\s*\)\s*{//s) { # FIXME: Wine specific kludge
614 $self->_update_c_position($&, \$line, \$column);
615 } elsif(s/^int\s*_FUNCTION_\s*{//s) { # FIXME: Wine specific kludge
616 $self->_update_c_position($&, \$line, \$column);
617 } elsif(s/^(?:jump|strong)_alias//s) { # FIXME: GNU C library specific kludge
618 $self->_update_c_position($&, \$line, \$column);
619 } elsif(s/^(?:__asm__|asm)\s*\(//) {
620 $self->_update_c_position($&, \$line, \$column);
621 } elsif($self->parse_c_typedef(\$_, \$line, \$column)) {
622 # Nothing
623 } elsif($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type, \$name)) {
624 # Nothing
625 } elsif($self->parse_c_function(\$_, \$line, \$column, \$function)) {
626 if(&$$found_function($function))
628 my $statements = $function->statements;
629 my $statements_line = $function->statements_line;
630 my $statements_column = $function->statements_column;
632 if(defined($statements)) {
633 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
634 return 0;
638 } else {
639 $self->_parse_c_error($_, $line, $column, "declaration");
642 $$refcurrent = $_;
643 $$refline = $line;
644 $$refcolumn = $column;
646 return 1;
649 ########################################################################
650 # _parse_c
652 sub _parse_c($$$$$$) {
653 my $self = shift;
655 my $pattern = shift;
656 my $refcurrent = shift;
657 my $refline = shift;
658 my $refcolumn = shift;
660 my $refmatch = shift;
662 local $_ = $$refcurrent;
663 my $line = $$refline;
664 my $column = $$refcolumn;
666 my $match;
667 if(s/^(?:$pattern)//s) {
668 $self->_update_c_position($&, \$line, \$column);
669 $match = $&;
670 } else {
671 return 0;
674 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
676 $$refcurrent = $_;
677 $$refline = $line;
678 $$refcolumn = $column;
680 $$refmatch = $match;
682 return 1;
685 ########################################################################
686 # parse_c_enum
688 sub parse_c_enum($$$$) {
689 my $self = shift;
691 my $refcurrent = shift;
692 my $refline = shift;
693 my $refcolumn = shift;
695 local $_ = $$refcurrent;
696 my $line = $$refline;
697 my $column = $$refcolumn;
699 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
701 if (!s/^enum\s+((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
702 return 0;
704 my $_name = $1 || "";
706 $self->_update_c_position($&, \$line, \$column);
708 my $name = "";
710 my $match;
711 while ($self->_parse_c_on_same_level_until_one_of(',', \$_, \$line, \$column, \$match)) {
712 if ($match) {
713 if ($match !~ /^(\w+)\s*(?:=\s*(.*?)\s*)?$/) {
714 $self->_parse_c_error($_, $line, $column, "enum");
716 my $enum_name = $1;
717 my $enum_value = $2 || "";
719 # $output->write("enum:$_name:$enum_name:$enum_value\n");
722 if ($self->_parse_c(',', \$_, \$line, \$column)) {
723 next;
724 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
725 # FIXME: Kludge
726 my $tuple = "($_)";
727 my $tuple_line = $line;
728 my $tuple_column = $column - 1;
730 my @arguments;
731 my @argument_lines;
732 my @argument_columns;
734 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
735 \@arguments, \@argument_lines, \@argument_columns))
737 $self->_parse_c_error($_, $line, $column, "enum");
740 # FIXME: Kludge
741 if ($#arguments >= 0) {
742 $name = $arguments[0];
745 last;
746 } else {
747 $self->_parse_c_error($_, $line, $column, "enum");
751 $self->_update_c_position($_, \$line, \$column);
753 $$refcurrent = $_;
754 $$refline = $line;
755 $$refcolumn = $column;
759 ########################################################################
760 # parse_c_expression
762 sub parse_c_expression($$$$) {
763 my $self = shift;
765 my $refcurrent = shift;
766 my $refline = shift;
767 my $refcolumn = shift;
769 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
771 local $_ = $$refcurrent;
772 my $line = $$refline;
773 my $column = $$refcolumn;
775 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
777 while($_) {
778 if(s/^(.*?)(\w+\s*\()/$2/s) {
779 $self->_update_c_position($1, \$line, \$column);
781 my $begin_line = $line;
782 my $begin_column = $column + 1;
784 my $name;
785 my @arguments;
786 my @argument_lines;
787 my @argument_columns;
788 if(!$self->parse_c_function_call(\$_, \$line, \$column, \$name, \@arguments, \@argument_lines, \@argument_columns)) {
789 return 0;
792 if(&$$found_function_call($begin_line, $begin_column, $line, $column, $name, \@arguments))
794 while(defined(my $argument = shift @arguments) &&
795 defined(my $argument_line = shift @argument_lines) &&
796 defined(my $argument_column = shift @argument_columns))
798 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
801 } else {
802 $_ = "";
806 $self->_update_c_position($_, \$line, \$column);
808 $$refcurrent = $_;
809 $$refline = $line;
810 $$refcolumn = $column;
812 return 1;
815 ########################################################################
816 # parse_c_file
818 sub parse_c_file($$$$) {
819 my $self = shift;
821 my $found_comment = \${$self->{FOUND_COMMENT}};
822 my $found_line = \${$self->{FOUND_LINE}};
824 my $refcurrent = shift;
825 my $refline = shift;
826 my $refcolumn = shift;
828 local $_ = $$refcurrent;
829 my $line = $$refline;
830 my $column = $$refcolumn;
832 my $declaration = "";
833 my $declaration_line = $line;
834 my $declaration_column = $column;
836 my $previous_line = 0;
837 my $previous_column = -1;
839 my $preprocessor_condition;
840 my $if = 0;
841 my $if0 = 0;
842 my $extern_c = 0;
844 my $blevel = 1;
845 my $plevel = 1;
846 while($plevel > 0 || $blevel > 0) {
847 my $match;
848 $self->_parse_c_until_one_of("#/\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
850 if($line != $previous_line) {
851 &$$found_line($line);
852 } else {
853 # &$$found_line("$line.$column");
855 $previous_line = $line;
856 $previous_column = $column;
858 if($match !~ /^\s+$/s && $options->debug) {
859 $self->_parse_c_warning($_, $line, $column, "file", "$plevel $blevel: '$declaration' '$match'");
862 if(!$declaration && $match =~ s/^\s+//s) {
863 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
866 if(!$if0) {
867 $declaration .= $match;
869 # FIXME: Kludge
870 if ($declaration =~ s/^extern\s*\"C\"//s) {
871 if (s/^\{//) {
872 $self->_update_c_position($&, \$line, \$column);
873 $declaration = "";
874 $declaration_line = $line;
875 $declaration_column = $column;
877 $extern_c = 1;
878 next;
880 } elsif ($extern_c && $blevel == 1 && $plevel == 1 && !$declaration) {
881 if (s/^\}//) {
882 $self->_update_c_position($&, \$line, \$column);
883 $declaration = "";
884 $declaration_line = $line;
885 $declaration_column = $column;
887 $extern_c = 0;
888 next;
890 } elsif($declaration =~ s/^(?:__DEFINE_(?:GET|SET)_SEG|OUR_GUID_ENTRY)\s*(?=\()//sx) { # FIXME: Wine specific kludge
891 my $prefix = $&;
892 if ($plevel > 2 || !s/^\)//) {
893 $declaration = "$prefix$declaration";
894 } else {
895 $plevel--;
896 $self->_update_c_position($&, \$line, \$column);
897 $declaration .= $&;
899 my @arguments;
900 my @argument_lines;
901 my @argument_columns;
903 if(!$self->parse_c_tuple(\$declaration, \$declaration_line, \$declaration_column,
904 \@arguments, \@argument_lines, \@argument_columns))
906 $self->_parse_c_error($declaration, $declaration_line, $declaration_column, "file", "tuple expected");
909 $declaration = "";
910 $declaration_line = $line;
911 $declaration_column = $column;
913 next;
915 } elsif ($declaration =~ s/^(?:DEFINE_SHLGUID)\s*\(.*?\)//s) {
916 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
917 } elsif ($declaration =~ s/^(?:DECL_WINELIB_TYPE_AW|DECLARE_HANDLE(?:16)?|TYPE_MARSHAL)\(\s*(\w+)\s*\)\s*//s) {
918 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
919 } elsif ($declaration =~ s/^ICOM_DEFINE\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*//s) {
920 $self->_update_c_position($&, \$declaration_line, \$declaration_column);
922 } else {
923 my $blank_lines = 0;
925 local $_ = $match;
926 while(s/^.*?\n//) { $blank_lines++; }
928 if(!$declaration) {
929 $declaration_line = $line;
930 $declaration_column = $column;
931 } else {
932 $declaration .= "\n" x $blank_lines;
937 if(/^[\#\/]/) {
938 my $blank_lines = 0;
939 if(s/^\#\s*//) {
940 my $preprocessor_line = $line;
941 my $preprocessor_column = $column;
943 my $preprocessor = $&;
944 while(s/^(.*?)\\\s*\n//) {
945 $blank_lines++;
946 $preprocessor .= "$1\n";
948 if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
949 $_ = "$2\n$_";
950 if(defined($3)) {
951 $preprocessor .= "$1$3";
952 } else {
953 $preprocessor .= $1;
955 } elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
956 if(defined($2)) {
957 $_ = "$2\n$_";
958 } else {
959 $blank_lines++;
961 $preprocessor .= $1;
965 if($preprocessor =~ /^\#\s*if/) {
966 if($preprocessor =~ /^\#\s*if\s*0/) {
967 $if0++;
968 } elsif($if0 > 0) {
969 $if++;
970 } else {
971 if($preprocessor =~ /^\#\s*ifdef\s+WORDS_BIGENDIAN$/) {
972 $preprocessor_condition = "defined(WORD_BIGENDIAN)";
973 # $output->write("'$preprocessor_condition':'$declaration'\n")
974 } else {
975 $preprocessor_condition = "";
978 } elsif($preprocessor =~ /^\#\s*else/) {
979 if ($preprocessor_condition ne "") {
980 $preprocessor_condition =~ "!$preprocessor_condition";
981 $preprocessor_condition =~ s/^!!/!/;
982 # $output->write("'$preprocessor_condition':'$declaration'\n")
984 } elsif($preprocessor =~ /^\#\s*endif/) {
985 if($if0 > 0) {
986 if($if > 0) {
987 $if--;
988 } else {
989 $if0--;
991 } else {
992 if ($preprocessor_condition ne "") {
993 # $output->write("'$preprocessor_condition':'$declaration'\n");
994 $preprocessor_condition = "";
999 if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
1000 return 0;
1004 if(s/^\/\*.*?\*\///s) {
1005 &$$found_comment($line, $column + 1, $&);
1006 local $_ = $&;
1007 while(s/^.*?\n//) {
1008 $blank_lines++;
1010 if($_) {
1011 $column += length($_);
1013 } elsif(s/^\/\/(.*?)\n//) {
1014 &$$found_comment($line, $column + 1, $&);
1015 $blank_lines++;
1016 } elsif(s/^\///) {
1017 if(!$if0) {
1018 $declaration .= $&;
1019 $column++;
1023 $line += $blank_lines;
1024 if($blank_lines > 0) {
1025 $column = 0;
1028 if(!$declaration) {
1029 $declaration_line = $line;
1030 $declaration_column = $column;
1031 } elsif($blank_lines > 0) {
1032 $declaration .= "\n" x $blank_lines;
1035 next;
1038 $column++;
1040 if($if0) {
1041 s/^.//;
1042 next;
1045 if(s/^[\(\[]//) {
1046 $plevel++;
1047 $declaration .= $&;
1048 } elsif(s/^\]//) {
1049 $plevel--;
1050 $declaration .= $&;
1051 } elsif(s/^\)//) {
1052 $plevel--;
1053 if($plevel <= 0) {
1054 $self->_parse_c_error($_, $line, $column, "file", ") without (");
1056 $declaration .= $&;
1057 if($plevel == 1 && $declaration =~ /^__ASM_GLOBAL_FUNC/) {
1058 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1059 return 0;
1061 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1062 $declaration = "";
1063 $declaration_line = $line;
1064 $declaration_column = $column;
1066 } elsif(s/^\{//) {
1067 $blevel++;
1068 $declaration .= $&;
1069 } elsif(s/^\}//) {
1070 $blevel--;
1071 if($blevel <= 0) {
1072 $self->_parse_c_error($_, $line, $column, "file", "} without {");
1075 $declaration .= $&;
1077 if($declaration =~ /^typedef/s ||
1078 $declaration =~ /^(?:const\s+|extern\s+|static\s+|volatile\s+)*(?:interface|struct|union)(?:\s+\w+)?\s*\{/s)
1080 # Nothing
1081 } elsif($plevel == 1 && $blevel == 1) {
1082 if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1083 return 0;
1085 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1086 $declaration = "";
1087 $declaration_line = $line;
1088 $declaration_column = $column;
1089 } elsif($column == 1 && !$extern_c) {
1090 $self->_parse_c_error("", $line, $column, "file", "inner } ends on column 1");
1092 } elsif(s/^;//) {
1093 $declaration .= $&;
1094 if($plevel == 1 && $blevel == 1) {
1095 $declaration =~ s/\s*;$//;
1096 if($declaration && !$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
1097 return 0;
1099 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1100 $declaration = "";
1101 $declaration_line = $line;
1102 $declaration_column = $column;
1104 } elsif(/^\s*$/ && $declaration =~ /^\s*$/ && $match =~ /^\s*$/) {
1105 $plevel = 0;
1106 $blevel = 0;
1107 } else {
1108 $self->_parse_c_error($_, $line, $column, "file", "parse error: '$declaration' '$match'");
1112 $$refcurrent = $_;
1113 $$refline = $line;
1114 $$refcolumn = $column;
1116 return 1;
1119 ########################################################################
1120 # parse_c_function
1122 sub parse_c_function($$$$$) {
1123 my $self = shift;
1125 my $file = \${$self->{FILE}};
1126 my $create_function = \${$self->{CREATE_FUNCTION}};
1128 my $refcurrent = shift;
1129 my $refline = shift;
1130 my $refcolumn = shift;
1132 my $reffunction = shift;
1134 local $_ = $$refcurrent;
1135 my $line = $$refline;
1136 my $column = $$refcolumn;
1138 my $linkage = "";
1139 my $calling_convention = "";
1140 my $return_type;
1141 my $name;
1142 my @arguments;
1143 my @argument_lines;
1144 my @argument_columns;
1145 my $statements;
1146 my $statements_line;
1147 my $statements_column;
1149 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1151 my $begin_line = $line;
1152 my $begin_column = $column + 1;
1154 if($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
1155 # Nothing
1158 # $self->_parse_c_warning($_, $line, $column, "function", "");
1160 my $match;
1161 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1162 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1163 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1164 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1165 \$_, \$line, \$column, \$match))
1167 if($match =~ /^(?:extern|static)$/) {
1168 if(!$linkage) {
1169 $linkage = $match;
1174 if($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1175 # Nothing
1176 } elsif($self->_parse_c('WINE_EXCEPTION_FILTER\(\w+\)', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1177 # Nothing
1178 } else {
1179 if(!$self->parse_c_type(\$_, \$line, \$column, \$return_type)) {
1180 return 0;
1183 $self->_parse_c('inline|FAR', \$_, \$line, \$column);
1185 $self->_parse_c($CALL_CONVENTION,
1186 \$_, \$line, \$column, \$calling_convention);
1189 # FIXME: ???: Old variant of __attribute((const))
1190 $self->_parse_c('(?:const|volatile)', \$_, \$line, \$column);
1192 if(!$self->_parse_c('(?:operator\s*!=|(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)', \$_, \$line, \$column, \$name)) {
1193 return 0;
1196 my $p = 0;
1197 if(s/^__P\s*\(//) {
1198 $self->_update_c_position($&, \$line, \$column);
1199 $p = 1;
1202 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1203 return 0;
1206 if($p) {
1207 if (s/^\)//) {
1208 $self->_update_c_position($&, \$line, \$column);
1209 } else {
1210 $self->_parse_c_error($_, $line, $column, "function");
1216 if($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
1217 # Nothing
1220 my $kar;
1221 # FIXME: Implement proper handling of K&R C functions
1222 $self->_parse_c_until_one_of("{", \$_, \$line, \$column, $kar);
1224 if($kar) {
1225 $output->write("K&R: $kar\n");
1228 if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1229 return 0;
1232 my $end_line = $line;
1233 my $end_column = $column;
1235 $$refcurrent = $_;
1236 $$refline = $line;
1237 $$refcolumn = $column;
1239 my $function = &$$create_function;
1241 $function->file($$file);
1242 $function->begin_line($begin_line);
1243 $function->begin_column($begin_column);
1244 $function->end_line($end_line);
1245 $function->end_column($end_column);
1246 $function->linkage($linkage);
1247 $function->return_type($return_type);
1248 $function->calling_convention($calling_convention);
1249 $function->name($name);
1250 # if(defined($argument_types)) {
1251 # $function->argument_types([@$argument_types]);
1253 # if(defined($argument_names)) {
1254 # $function->argument_names([@$argument_names]);
1256 $function->statements_line($statements_line);
1257 $function->statements_column($statements_column);
1258 $function->statements($statements);
1260 $$reffunction = $function;
1262 return 1;
1265 ########################################################################
1266 # parse_c_function_call
1268 sub parse_c_function_call($$$$$$$$) {
1269 my $self = shift;
1271 my $refcurrent = shift;
1272 my $refline = shift;
1273 my $refcolumn = shift;
1275 my $refname = shift;
1276 my $refarguments = shift;
1277 my $refargument_lines = shift;
1278 my $refargument_columns = shift;
1280 local $_ = $$refcurrent;
1281 my $line = $$refline;
1282 my $column = $$refcolumn;
1284 my $name;
1285 my @arguments;
1286 my @argument_lines;
1287 my @argument_columns;
1289 if(s/^(\w+)(\s*)(?=\()//s) {
1290 $self->_update_c_position($&, \$line, \$column);
1292 $name = $1;
1294 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1295 return 0;
1297 } else {
1298 return 0;
1301 $$refcurrent = $_;
1302 $$refline = $line;
1303 $$refcolumn = $column;
1305 $$refname = $name;
1306 @$refarguments = @arguments;
1307 @$refargument_lines = @argument_lines;
1308 @$refargument_columns = @argument_columns;
1310 return 1;
1313 ########################################################################
1314 # parse_c_preprocessor
1316 sub parse_c_preprocessor($$$$) {
1317 my $self = shift;
1319 my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
1321 my $refcurrent = shift;
1322 my $refline = shift;
1323 my $refcolumn = shift;
1325 local $_ = $$refcurrent;
1326 my $line = $$refline;
1327 my $column = $$refcolumn;
1329 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1331 my $begin_line = $line;
1332 my $begin_column = $column + 1;
1334 if(!&$$found_preprocessor($begin_line, $begin_column, "$_")) {
1335 return 1;
1338 if(/^\#\s*define\s*(.*?)$/s) {
1339 $self->_update_c_position($_, \$line, \$column);
1340 } elsif(/^\#\s*else/s) {
1341 $self->_update_c_position($_, \$line, \$column);
1342 } elsif(/^\#\s*endif/s) {
1343 $self->_update_c_position($_, \$line, \$column);
1344 } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1345 $self->_update_c_position($_, \$line, \$column);
1346 } elsif(/^\#\s*include\s+(.*?)$/s) {
1347 $self->_update_c_position($_, \$line, \$column);
1348 } elsif(/^\#\s*undef\s+(.*?)$/s) {
1349 $self->_update_c_position($_, \$line, \$column);
1350 } else {
1351 $self->_parse_c_error($_, $line, $column, "preprocessor");
1354 $$refcurrent = $_;
1355 $$refline = $line;
1356 $$refcolumn = $column;
1358 return 1;
1361 ########################################################################
1362 # parse_c_statement
1364 sub parse_c_statement($$$$) {
1365 my $self = shift;
1367 my $refcurrent = shift;
1368 my $refline = shift;
1369 my $refcolumn = shift;
1371 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1373 local $_ = $$refcurrent;
1374 my $line = $$refline;
1375 my $column = $$refcolumn;
1377 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1379 $self->_parse_c('(?:case\s+)?(\w+)\s*:\s*', \$_, \$line, \$column);
1381 # $output->write("$line.$column: statement: '$_'\n");
1383 if(/^$/) {
1384 # Nothing
1385 } elsif(/^\{/) {
1386 my $statements;
1387 my $statements_line;
1388 my $statements_column;
1389 if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1390 return 0;
1392 if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1393 return 0;
1395 } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
1396 $self->_update_c_position($&, \$line, \$column);
1398 my $name = $1;
1400 my @arguments;
1401 my @argument_lines;
1402 my @argument_columns;
1403 if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1404 return 0;
1407 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1408 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1409 return 0;
1411 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1413 while(defined(my $argument = shift @arguments) &&
1414 defined(my $argument_line = shift @argument_lines) &&
1415 defined(my $argument_column = shift @argument_columns))
1417 $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1419 } elsif(s/^else//) {
1420 $self->_update_c_position($&, \$line, \$column);
1421 if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1422 return 0;
1424 } elsif(s/^return//) {
1425 $self->_update_c_position($&, \$line, \$column);
1426 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1427 if(!$self->parse_c_expression(\$_, \$line, \$column)) {
1428 return 0;
1430 } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1431 # Nothing
1432 } else {
1433 # $self->_parse_c_error($_, $line, $column, "statement");
1436 $self->_update_c_position($_, \$line, \$column);
1438 $$refcurrent = $_;
1439 $$refline = $line;
1440 $$refcolumn = $column;
1442 return 1;
1445 ########################################################################
1446 # parse_c_statements
1448 sub parse_c_statements($$$$) {
1449 my $self = shift;
1451 my $refcurrent = shift;
1452 my $refline = shift;
1453 my $refcolumn = shift;
1455 my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
1457 local $_ = $$refcurrent;
1458 my $line = $$refline;
1459 my $column = $$refcolumn;
1461 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1463 # $output->write("$line.$column: statements: '$_'\n");
1465 my $statement = "";
1466 my $statement_line = $line;
1467 my $statement_column = $column;
1469 my $previous_line = -1;
1470 my $previous_column = -1;
1472 my $blevel = 1;
1473 my $plevel = 1;
1474 while($plevel > 0 || $blevel > 0) {
1475 my $match;
1476 $self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1478 if($previous_line == $line && $previous_column == $column) {
1479 $self->_parse_c_error($_, $line, $column, "statements", "no progress");
1481 $previous_line = $line;
1482 $previous_column = $column;
1484 # $output->write("'$match' '$_'\n");
1486 $statement .= $match;
1487 $column++;
1488 if(s/^[\(\[]//) {
1489 $plevel++;
1490 $statement .= $&;
1491 } elsif(s/^[\)\]]//) {
1492 $plevel--;
1493 if($plevel <= 0) {
1494 $self->_parse_c_error($_, $line, $column, "statements");
1496 $statement .= $&;
1497 } elsif(s/^\{//) {
1498 $blevel++;
1499 $statement .= $&;
1500 } elsif(s/^\}//) {
1501 $blevel--;
1502 $statement .= $&;
1503 if($blevel == 1) {
1504 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1505 return 0;
1507 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1508 $statement = "";
1509 $statement_line = $line;
1510 $statement_column = $column;
1512 } elsif(s/^;//) {
1513 if($plevel == 1 && $blevel == 1) {
1514 if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1515 return 0;
1518 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1519 $statement = "";
1520 $statement_line = $line;
1521 $statement_column = $column;
1522 } else {
1523 $statement .= $&;
1525 } elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
1526 $plevel = 0;
1527 $blevel = 0;
1528 } else {
1529 $self->_parse_c_error($_, $line, $column, "statements");
1533 $self->_update_c_position($_, \$line, \$column);
1535 $$refcurrent = $_;
1536 $$refline = $line;
1537 $$refcolumn = $column;
1539 return 1;
1542 ########################################################################
1543 # parse_c_struct_union
1545 sub parse_c_struct_union($$$$$$$$$) {
1546 my $self = shift;
1548 my $refcurrent = shift;
1549 my $refline = shift;
1550 my $refcolumn = shift;
1552 my $refkind = shift;
1553 my $ref_name = shift;
1554 my $reffield_type_names = shift;
1555 my $reffield_names = shift;
1556 my $refnames = shift;
1558 local $_ = $$refcurrent;
1559 my $line = $$refline;
1560 my $column = $$refcolumn;
1562 my $kind;
1563 my $_name;
1564 my @field_type_names = ();
1565 my @field_names = ();
1566 my @names = ();
1568 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1570 if (!s/^(interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1571 return 0;
1573 $kind = $1;
1574 $_name = $2 || "";
1576 $self->_update_c_position($&, \$line, \$column);
1578 $kind =~ s/\s+//g;
1580 my $match;
1581 while ($_ && $self->_parse_c_on_same_level_until_one_of(';', \$_, \$line, \$column, \$match))
1583 my $field_linkage;
1584 my $field_type_name;
1585 my $field_name;
1587 if ($self->parse_c_variable(\$match, \$line, \$column, \$field_linkage, \$field_type_name, \$field_name)) {
1588 $field_type_name =~ s/\s+/ /g;
1590 push @field_type_names, $field_type_name;
1591 push @field_names, $field_name;
1592 # $output->write("$kind:$_name:$field_type_name:$field_name\n");
1593 } elsif ($match) {
1594 $self->_parse_c_error($_, $line, $column, "typedef $kind: '$match'");
1597 if ($self->_parse_c(';', \$_, \$line, \$column)) {
1598 next;
1599 } elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
1600 # FIXME: Kludge
1601 my $tuple = "($_)";
1602 my $tuple_line = $line;
1603 my $tuple_column = $column - 1;
1605 my @arguments;
1606 my @argument_lines;
1607 my @argument_columns;
1609 if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
1610 \@arguments, \@argument_lines, \@argument_columns))
1612 $self->_parse_c_error($_, $line, $column, "$kind");
1615 foreach my $argument (@arguments) {
1616 my $name = $argument;
1618 push @names, $name;
1621 last;
1622 } else {
1623 $self->_parse_c_error($_, $line, $column, "$kind");
1627 $$refcurrent = $_;
1628 $$refline = $line;
1629 $$refcolumn = $column;
1631 $$refkind = $kind;
1632 $$ref_name = $_name;
1633 @$reffield_type_names = @field_type_names;
1634 @$reffield_names = @field_names;
1635 @$refnames = @names;
1637 return 1;
1640 ########################################################################
1641 # parse_c_tuple
1643 sub parse_c_tuple($$$$$$$) {
1644 my $self = shift;
1646 my $refcurrent = shift;
1647 my $refline = shift;
1648 my $refcolumn = shift;
1650 # FIXME: Should not write directly
1651 my $items = shift;
1652 my $item_lines = shift;
1653 my $item_columns = shift;
1655 local $_ = $$refcurrent;
1657 my $line = $$refline;
1658 my $column = $$refcolumn;
1660 my $item;
1661 if(s/^\(//) {
1662 $column++;
1663 $item = "";
1664 } else {
1665 return 0;
1668 my $item_line = $line;
1669 my $item_column = $column + 1;
1671 my $plevel = 1;
1672 while($plevel > 0) {
1673 my $match;
1674 $self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1676 $column++;
1678 $item .= $match;
1679 if(s/^\)//) {
1680 $plevel--;
1681 if($plevel == 0) {
1682 push @$item_lines, $item_line;
1683 push @$item_columns, $item_column;
1684 push @$items, $item;
1685 $item = "";
1686 } else {
1687 $item .= ")";
1689 } elsif(s/^\(//) {
1690 $plevel++;
1691 $item .= "(";
1692 } elsif(s/^,//) {
1693 if($plevel == 1) {
1694 push @$item_lines, $item_line;
1695 push @$item_columns, $item_column;
1696 push @$items, $item;
1697 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1698 $item_line = $line;
1699 $item_column = $column + 1;
1700 $item = "";
1701 } else {
1702 $item .= ",";
1704 } else {
1705 return 0;
1709 $$refcurrent = $_;
1710 $$refline = $line;
1711 $$refcolumn = $column;
1713 return 1;
1716 ########################################################################
1717 # parse_c_type
1719 sub parse_c_type($$$$$) {
1720 my $self = shift;
1722 my $refcurrent = shift;
1723 my $refline = shift;
1724 my $refcolumn = shift;
1726 my $reftype = shift;
1728 local $_ = $$refcurrent;
1729 my $line = $$refline;
1730 my $column = $$refcolumn;
1732 my $type;
1734 $self->_parse_c("(?:const|volatile)", \$_, \$line, \$column);
1736 if($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1737 # Nothing
1738 } elsif($self->_parse_c('(?:enum\s+|interface\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1739 \$_, \$line, \$column, \$type))
1741 # Nothing
1742 } else {
1743 return 0;
1745 $type =~ s/\s//g;
1747 $$refcurrent = $_;
1748 $$refline = $line;
1749 $$refcolumn = $column;
1751 $$reftype = $type;
1753 return 1;
1756 ########################################################################
1757 # parse_c_typedef
1759 sub parse_c_typedef($$$$) {
1760 my $self = shift;
1762 my $create_type = \${$self->{CREATE_TYPE}};
1763 my $found_type = \${$self->{FOUND_TYPE}};
1764 my $preprocessor_condition = \${$self->{PREPROCESSOR_CONDITION}};
1766 my $refcurrent = shift;
1767 my $refline = shift;
1768 my $refcolumn = shift;
1770 local $_ = $$refcurrent;
1771 my $line = $$refline;
1772 my $column = $$refcolumn;
1774 my $type;
1776 if (!$self->_parse_c("typedef", \$_, \$line, \$column)) {
1777 return 0;
1780 my $finished = 0;
1782 if ($finished) {
1783 # Nothing
1784 } elsif ($self->parse_c_enum(\$_, \$line, \$column)) {
1785 $finished = 1;
1788 my $kind;
1789 my $_name;
1790 my @field_type_names;
1791 my @field_names;
1792 my @names;
1793 if ($finished) {
1794 # Nothing
1795 } elsif ($self->parse_c_struct_union(\$_, \$line, \$column,
1796 \$kind, \$_name, \@field_type_names, \@field_names, \@names))
1798 my $base_name;
1799 foreach my $name (@names)
1801 if ($name =~ /^\w+$/)
1803 $base_name = $name;
1804 last;
1807 $base_name="$kind $_name" if (!defined $base_name and defined $_name);
1808 $base_name=$kind if (!defined $base_name);
1809 foreach my $name (@names) {
1810 if ($name =~ /^\w+$/) {
1811 my $type = &$$create_type();
1813 $type->kind($kind);
1814 $type->_name($_name);
1815 $type->name($name);
1816 $type->field_type_names([@field_type_names]);
1817 $type->field_names([@field_names]);
1819 &$$found_type($type);
1820 } elsif ($name =~ /^(\*+)\s*(?:RESTRICTED_POINTER\s+)?(\w+)$/) {
1821 my $type_name = "$base_name $1";
1822 $name = $2;
1824 my $type = &$$create_type();
1826 $type->kind("");
1827 $type->name($name);
1828 $type->field_type_names([$type_name]);
1829 $type->field_names([""]);
1831 &$$found_type($type);
1832 } else {
1833 $self->_parse_c_error($_, $line, $column, "typedef 2");
1837 $finished = 1;
1840 my $linkage;
1841 my $type_name;
1842 my $name;
1843 if ($finished) {
1844 # Nothing
1845 } elsif ($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type_name, \$name)) {
1846 $type_name =~ s/\s+/ /g;
1848 if(defined($type_name) && defined($name)) {
1849 my $type = &$$create_type();
1851 if (length($name) == 0) {
1852 $self->_parse_c_error($_, $line, $column, "typedef");
1855 $type->kind("");
1856 $type->name($name);
1857 $type->field_type_names([$type_name]);
1858 $type->field_names([""]);
1860 &$$found_type($type);
1862 } else {
1863 $self->_parse_c_error($_, $line, $column, "typedef");
1866 $$refcurrent = $_;
1867 $$refline = $line;
1868 $$refcolumn = $column;
1870 return 1;
1873 ########################################################################
1874 # parse_c_variable
1876 sub parse_c_variable($$$$$$$) {
1877 my $self = shift;
1879 my $found_variable = \${$self->{FOUND_VARIABLE}};
1881 my $refcurrent = shift;
1882 my $refline = shift;
1883 my $refcolumn = shift;
1885 my $reflinkage = shift;
1886 my $reftype = shift;
1887 my $refname = shift;
1889 local $_ = $$refcurrent;
1890 my $line = $$refline;
1891 my $column = $$refcolumn;
1893 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1895 my $begin_line = $line;
1896 my $begin_column = $column + 1;
1898 my $linkage = "";
1899 my $sign = "";
1900 my $type = "";
1901 my $name = "";
1903 # $self->_parse_c_warning($_, $line, $column, "variable");
1905 my $match;
1906 while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1907 'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1908 'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1909 'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1910 \$_, \$line, \$column, \$match))
1912 if ($match =~ /^(?:extern|static)$/) {
1913 if (!$linkage) {
1914 $linkage = $match;
1915 } else {
1916 $self->_parse_c_warning($_, $line, $column, "repeated linkage (ignored): $match");
1918 } elsif ($match =~ /^(?:signed|unsigned)$/) {
1919 if (!$sign) {
1920 $sign = "$match ";
1921 } else {
1922 $self->_parse_c_warning($_, $line, $column, "repeated sign (ignored): $match");
1927 my $finished = 0;
1929 if($finished) {
1930 # Nothing
1931 } elsif(/^$/) {
1932 return 0;
1933 } elsif (s/^(enum\s+|interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1934 my $kind = $1;
1935 my $_name = $2;
1936 $self->_update_c_position($&, \$line, \$column);
1938 if(defined($_name)) {
1939 $type = "$kind $_name { }";
1940 } else {
1941 $type = "$kind { }";
1944 $finished = 1;
1945 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s+DECLSPEC_ALIGN\(.*?\)|\s*(?:const\s*|volatile\s*)?\*)*)\s*(\w+)\s*(\[.*?\]$|:\s*(\d+)$|\{)?//s) {
1946 $type = "$sign$1";
1947 $name = $2;
1949 if (defined($3)) {
1950 my $bits = $4;
1951 local $_ = $3;
1952 if (/^\[/) {
1953 $type .= $_;
1954 } elsif (/^:/) {
1955 $type .= ":$bits";
1956 } elsif (/^\{/) {
1957 # Nothing
1961 $type = $self->_format_c_type($type);
1963 $finished = 1;
1964 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*)\s*:\s*(\d+)$//s) {
1965 $type = "$sign$1:$2";
1966 $name = "";
1967 $type = $self->_format_c_type($type);
1969 $finished = 1;
1970 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*\s*\(\s*(?:$CALL_CONVENTION)?(?:\s*\*)*)\s*(\w+)\s*(\)\s*\(.*?\))$//s) {
1971 $type = $self->_format_c_type("$sign$1$3");
1972 $name = $2;
1974 $finished = 1;
1975 } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
1976 $type = $match;
1977 $finished = 1;
1978 } else {
1979 $self->_parse_c_warning($_, $line, $column, "variable", "'$_'");
1980 $finished = 1;
1983 if($finished) {
1984 # Nothing
1985 } elsif($self->_parse_c('SEQ_DEFINEBUF', \$_, \$line, \$column, \$match)) { # Linux specific
1986 $type = $match;
1987 $finished = 1;
1988 } elsif($self->_parse_c('DEFINE_REGS_ENTRYPOINT_\w+|DPQ_DECL_\w+|HANDLER_DEF|IX86_ONLY', # Wine specific
1989 \$_, \$line, \$column, \$match))
1991 $type = $match;
1992 $finished = 1;
1993 } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
1994 $type = $match;
1995 $finished = 1;
1996 } elsif(s/^(enum|interface|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*//s) {
1997 my $kind = $1;
1998 my $_name = $2;
1999 $self->_update_c_position($&, \$line, \$column);
2001 if(defined($_name)) {
2002 $type = "struct $_name { }";
2003 } else {
2004 $type = "struct { }";
2006 } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
2007 $type = $&;
2008 $type =~ s/\s//g;
2009 } else {
2010 return 0;
2013 # $output->write("*** $type: '$_'\n");
2015 # $self->_parse_c_warning($_, $line, $column, "variable2", "");
2017 if($finished) {
2018 # Nothing
2019 } elsif(s/^WINAPI\s*//) {
2020 $self->_update_c_position($&, \$line, \$column);
2023 if($finished) {
2024 # Nothing
2025 } elsif(s/^(\((?:$CALL_CONVENTION)?\s*\*?\s*(?:$CALL_CONVENTION)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
2026 $self->_update_c_position($&, \$line, \$column);
2028 $name = $1;
2029 $name =~ s/\s//g;
2031 $self->_parse_c_until_one_of("\\)", \$_, \$line, \$column);
2032 if(s/^\)//) { $column++; }
2033 $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
2035 if(!s/^(?:=\s*|,\s*|$)//) {
2036 return 0;
2038 } elsif(s/^(?:\*\s*)*(?:const\s+|volatile\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
2039 $self->_update_c_position($&, \$line, \$column);
2041 $name = $1;
2042 $name =~ s/\s//g;
2043 } elsif(/^$/) {
2044 $name = "";
2045 } else {
2046 return 0;
2049 # $output->write("$type: $name: '$_'\n");
2051 $$refcurrent = $_;
2052 $$refline = $line;
2053 $$refcolumn = $column;
2055 $$reflinkage = $linkage;
2056 $$reftype = $type;
2057 $$refname = $name;
2059 if(&$$found_variable($begin_line, $begin_column, $linkage, $type, $name))
2061 # Nothing
2064 return 1;