c_parser.pm 51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# Copyright 1999, 2000, 2001 Patrik Stridvall
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# 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
17 18
#

19 20 21 22
package c_parser;

use strict;

23 24 25 26 27 28 29
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;

@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw();

30 31 32
use options qw($options);
use output qw($output);

33
use c_function;
34
use c_type;
35

36 37 38 39
# Defined a couple common regexp tidbits
my $CALL_CONVENTION="__cdecl|__stdcall|" .
                    "__RPC_API|__RPC_STUB|__RPC_USER|" .
		    "CALLBACK|CDECL|NTAPI|PASCAL|RPC_ENTRY|RPC_VAR_ENTRY|" .
40
		    "VFWAPI|VFWAPIV|WINAPI|WINAPIV|APIENTRY|";
41 42


43 44 45 46 47 48 49 50 51 52
sub parse_c_function($$$$$);
sub parse_c_function_call($$$$$$$$);
sub parse_c_preprocessor($$$$);
sub parse_c_statements($$$$);
sub parse_c_tuple($$$$$$$);
sub parse_c_type($$$$$);
sub parse_c_typedef($$$$);
sub parse_c_variable($$$$$$$);


53 54 55
########################################################################
# new
#
56
sub new($$) {
57 58 59 60 61 62
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self  = {};
    bless ($self, $class);

    my $file = \${$self->{FILE}};
63 64
    my $create_function = \${$self->{CREATE_FUNCTION}};
    my $create_type = \${$self->{CREATE_TYPE}};
65 66 67 68
    my $found_comment = \${$self->{FOUND_COMMENT}};
    my $found_declaration = \${$self->{FOUND_DECLARATION}};
    my $found_function = \${$self->{FOUND_FUNCTION}};
    my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};
69
    my $found_line = \${$self->{FOUND_LINE}};
70 71
    my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};
    my $found_statement = \${$self->{FOUND_STATEMENT}};
72
    my $found_type = \${$self->{FOUND_TYPE}};
73 74 75 76
    my $found_variable = \${$self->{FOUND_VARIABLE}};

    $$file = shift;

77 78
    $$create_function = sub { return new c_function; };
    $$create_type = sub { return new c_type; };
79 80 81 82
    $$found_comment = sub { return 1; };
    $$found_declaration = sub { return 1; };
    $$found_function = sub { return 1; };
    $$found_function_call = sub { return 1; };
83
    $$found_line = sub { return 1; };
84 85
    $$found_preprocessor = sub { return 1; };
    $$found_statement = sub { return 1; };
86
    $$found_type = sub { return 1; };
87 88 89 90
    $$found_variable = sub { return 1; };

    return $self;
}
91

92 93 94
########################################################################
# set_found_comment_callback
#
95
sub set_found_comment_callback($$) {
96
    my $self = shift;
97

98
    my $found_comment = \${$self->{FOUND_COMMENT}};
99

100 101
    $$found_comment = shift;
}
102

103 104 105
########################################################################
# set_found_declaration_callback
#
106
sub set_found_declaration_callback($$) {
107 108 109 110 111
    my $self = shift;

    my $found_declaration = \${$self->{FOUND_DECLARATION}};

    $$found_declaration = shift;
112 113
}

114 115 116
########################################################################
# set_found_function_callback
#
117
sub set_found_function_callback($$) {
118 119 120 121 122 123 124 125 126 127
    my $self = shift;

    my $found_function = \${$self->{FOUND_FUNCTION}};

    $$found_function = shift;
}

########################################################################
# set_found_function_call_callback
#
128
sub set_found_function_call_callback($$) {
129 130 131 132 133 134 135
    my $self = shift;

    my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};

    $$found_function_call = shift;
}

136 137 138
########################################################################
# set_found_line_callback
#
139
sub set_found_line_callback($$) {
140 141 142 143 144 145 146
    my $self = shift;

    my $found_line = \${$self->{FOUND_LINE}};

    $$found_line = shift;
}

147 148 149
########################################################################
# set_found_preprocessor_callback
#
150
sub set_found_preprocessor_callback($$) {
151 152 153 154 155 156 157 158 159 160
    my $self = shift;

    my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};

    $$found_preprocessor = shift;
}

########################################################################
# set_found_statement_callback
#
161
sub set_found_statement_callback($$) {
162 163 164 165 166 167 168
    my $self = shift;

    my $found_statement = \${$self->{FOUND_STATEMENT}};

    $$found_statement = shift;
}

169 170 171
########################################################################
# set_found_type_callback
#
172
sub set_found_type_callback($$) {
173 174 175 176 177 178 179
    my $self = shift;

    my $found_type = \${$self->{FOUND_TYPE}};

    $$found_type = shift;
}

180 181 182
########################################################################
# set_found_variable_callback
#
183
sub set_found_variable_callback($$) {
184 185 186 187 188 189 190
    my $self = shift;

    my $found_variable = \${$self->{FOUND_VARIABLE}};

    $$found_variable = shift;
}

191 192 193 194

########################################################################
# _format_c_type

195
sub _format_c_type($$) {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    my $self = shift;

    local $_ = shift;
    s/^\s*(.*?)\s*$/$1/;

    if (/^(\w+(?:\s*\*)*)\s*\(\s*\*\s*\)\s*\(\s*(.*?)\s*\)$/s) {
	my $return_type = $1;
	my @arguments = split(/\s*,\s*/, $2);
	foreach my $argument (@arguments) {
	    if ($argument =~ s/^(\w+(?:\s*\*)*)\s*\w+$/$1/) { 
		$argument =~ s/\s+/ /g;
		$argument =~ s/\s*\*\s*/*/g;
		$argument =~ s/(\*+)$/ $1/;
	    }
	}

	$_ = "$return_type (*)(" . join(", ", @arguments) . ")";
    }
    
    return $_;
}


219
########################################################################
220 221 222
# _parse_c_warning
#
# FIXME: Use caller (See man perlfunc)
223

224
sub _parse_c_warning($$$$$$) {
225 226
    my $self = shift;

227 228 229 230 231
    local $_ = shift;
    my $line = shift;
    my $column = shift;
    my $context = shift;
    my $message = shift;
232

233
    my $file = \${$self->{FILE}};
234

235
    $message = "warning" if !$message;
236

237 238 239
    my $current = "";
    if($_) {
	my @lines = split(/\n/, $_);
240

241 242 243
	$current .= $lines[0] . "\n" if $lines[0];
        $current .= $lines[1] . "\n" if $lines[1];
    }
244

245 246 247 248
    if (0) {
	(my $package, my $filename, my $line) = caller(0);
	$output->write("*** caller ***: $filename:$line\n");
    }
249

250 251 252 253 254
    if($current) {
	$output->write("$$file:$line." . ($column + 1) . ": $context: $message: \\\n$current");
    } else {
	$output->write("$$file:$line." . ($column + 1) . ": $context: $message\n");
    }
255 256
}

257 258 259
########################################################################
# _parse_c_error

260
sub _parse_c_error($$$$$$) {
261 262 263 264 265 266
    my $self = shift;

    local $_ = shift;
    my $line = shift;
    my $column = shift;
    my $context = shift;
267
    my $message = shift;
268

269
    $message = "parse error" if !$message;
270

271
    # Why did I do this?
272
    if($output->prefix) {
273
	# $output->write("\n");
274 275
	$output->prefix("");
    }
276

277
    $self->_parse_c_warning($_, $line, $column, $context, $message);
278 279 280 281 282

    exit 1;
}

########################################################################
283
# _update_c_position
284

285
sub _update_c_position($$$$) {
286 287
    my $self = shift;

288
    local $_ = shift;
289 290
    my $refline = shift;
    my $refcolumn = shift;
291

292 293
    my $line = $$refline;
    my $column = $$refcolumn;
294

295 296 297 298
    while($_) {
	if(s/^[^\n\t\'\"]*//s) {
	    $column += length($&);
	}
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	if(s/^\'//) {
	    $column++;
	    while(/^./ && !s/^\'//) {
		s/^([^\'\\]*)//s;
		$column += length($1);
		if(s/^\\//) {
		    $column++;
		    if(s/^(.)//s) {
			$column += length($1);
			if($1 eq "0") {
			    s/^(\d{0,3})//s;
			    $column += length($1);
			}
		    }
		}
	    }
	    $column++;
	} elsif(s/^\"//) {
	    $column++;
	    while(/^./ && !s/^\"//) {
		s/^([^\"\\]*)//s;
		$column += length($1);
		if(s/^\\//) {
		    $column++;
		    if(s/^(.)//s) {
			$column += length($1);
			if($1 eq "0") {
			    s/^(\d{0,3})//s;
			    $column += length($1);
			}
		    }
		}
	    }
	    $column++;
	} elsif(s/^\n//) {
	    $line++;
	    $column = 0;
	} elsif(s/^\t//) {
	    $column = $column + 8 - $column % 8;
	}
340 341
    }

342 343
    $$refline = $line;
    $$refcolumn = $column;
344 345 346
}

########################################################################
347
# __parse_c_until_one_of
348

349
sub __parse_c_until_one_of($$$$$$$) {
350 351
    my $self = shift;

352
    my $characters = shift;
353
    my $on_same_level = shift;
354 355 356 357 358 359 360 361 362
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;
    my $match = shift;

    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

363

364 365 366 367 368
    if(!defined($match)) {
	my $blackhole;
	$match = \$blackhole;
    }

369
    my $level = 0;
370
    $$match = "";
371
    while(/^[^$characters]/s || $level > 0) {
372 373
	my $submatch = "";

374 375 376 377 378 379 380 381 382 383 384 385
	if ($level > 0) {
	    if(s/^[^\(\)\[\]\{\}\n\t\'\"]*//s) {
		$submatch .= $&;
	    }
	} elsif ($on_same_level) {
	    if(s/^[^$characters\(\)\[\]\{\}\n\t\'\"]*//s) {
		$submatch .= $&;
	    }
	} else {
	    if(s/^[^$characters\n\t\'\"]*//s) {
		$submatch .= $&;
	    }
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
	}

	if(s/^\'//) {
	    $submatch .= "\'";
	    while(/^./ && !s/^\'//) {
		s/^([^\'\\]*)//s;
		$submatch .= $1;
		if(s/^\\//) {
		    $submatch .= "\\";
		    if(s/^(.)//s) {
			$submatch .= $1;
			if($1 eq "0") {
			    s/^(\d{0,3})//s;
			    $submatch .= $1;
			}
		    }
		}
	    }
	    $submatch .= "\'";

	    $$match .= $submatch;
	    $column += length($submatch);
	} elsif(s/^\"//) {
	    $submatch .= "\"";
	    while(/^./ && !s/^\"//) {
		s/^([^\"\\]*)//s;
		$submatch .= $1;
		if(s/^\\//) {
		    $submatch .= "\\";
		    if(s/^(.)//s) {
			$submatch .= $1;
			if($1 eq "0") {
			    s/^(\d{0,3})//s;
			    $submatch .= $1;
			}
		    }
		}
	    }
	    $submatch .= "\"";

	    $$match .= $submatch;
	    $column += length($submatch);
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	} elsif($on_same_level && s/^[\(\[\{]//) {
	    $level++;

	    $submatch .= $&;
	    $$match .= $submatch;
	    $column++;
	} elsif($on_same_level && s/^[\)\]\}]//) {
	    if ($level > 0) {
		$level--;
		
		$submatch .= $&;
		$$match .= $submatch;
		$column++;
	    } else {
		$_ = "$&$_";
		$$match .= $submatch;
		last;
	    }
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
	} elsif(s/^\n//) {
	    $submatch .= "\n";

	    $$match .= $submatch;
	    $line++;
	    $column = 0;
	} elsif(s/^\t//) {
	    $submatch .= "\t";

	    $$match .= $submatch;
	    $column = $column + 8 - $column % 8;
	} else {
	    $$match .= $submatch;
	    $column += length($submatch);
	}
    }

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;
    return 1;
}

469 470 471
########################################################################
# _parse_c_until_one_of

472
sub _parse_c_until_one_of($$$$$$) {
473 474 475 476 477 478 479 480 481 482 483 484 485 486
    my $self = shift;

    my $characters = shift;
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;
    my $match = shift;

    return $self->__parse_c_until_one_of($characters, 0, $refcurrent, $refline, $refcolumn, $match);
}

########################################################################
# _parse_c_on_same_level_until_one_of

487
sub _parse_c_on_same_level_until_one_of($$$$$$) {
488 489 490 491 492 493 494 495 496 497 498
    my $self = shift;

    my $characters = shift;
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;
    my $match = shift;

    return $self->__parse_c_until_one_of($characters, 1, $refcurrent, $refline, $refcolumn, $match);
}

499 500 501
########################################################################
# parse_c_block

502
sub parse_c_block($$$$$$$) {
503 504
    my $self = shift;

505 506 507
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;
508

509 510 511 512 513 514 515 516
    my $refstatements = shift;
    my $refstatements_line = shift;
    my $refstatements_column = shift;

    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

517 518
    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);

519 520 521 522 523 524 525 526
    my $statements;
    if(s/^\{//) {
	$column++;
	$statements = "";
    } else {
	return 0;
    }

527
    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
528 529 530 531 532 533 534

    my $statements_line = $line;
    my $statements_column = $column;

    my $plevel = 1;
    while($plevel > 0) {
	my $match;
535
	$self->_parse_c_until_one_of("\\{\\}", \$_, \$line, \$column, \$match);
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

	$column++;

	$statements .= $match;
	if(s/^\}//) {
	    $plevel--;
	    if($plevel > 0) {
		$statements .= "}";
	    }
	} elsif(s/^\{//) {
	    $plevel++;
	    $statements .= "{";
	} else {
	    return 0;
	}
    }

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;
    $$refstatements = $statements;
    $$refstatements_line = $statements_line;
    $$refstatements_column = $statements_column;

    return 1;
}

563 564 565
########################################################################
# parse_c_declaration

566
sub parse_c_declaration($$$$$$$$$$$$) {
567 568 569 570 571
    my $self = shift;

    my $found_declaration = \${$self->{FOUND_DECLARATION}};
    my $found_function = \${$self->{FOUND_FUNCTION}};

572 573 574 575
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

576
    local $_ = $$refcurrent;
577 578 579
    my $line = $$refline;
    my $column = $$refcolumn;

580
    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
581

582 583
    my $begin_line = $line;
    my $begin_column = $column + 1;
584

585 586 587
    my $end_line = $begin_line;
    my $end_column = $begin_column;
    $self->_update_c_position($_, \$end_line, \$end_column);
588

589 590 591
    if(!&$$found_declaration($begin_line, $begin_column, $end_line, $end_column, $_)) {
	return 1;
    }
592

593 594
    # Function
    my $function = shift;
595

596 597 598 599 600 601 602
    my $linkage = shift;
    my $calling_convention = shift;
    my $return_type = shift;
    my $name = shift;
    my @arguments = shift;
    my @argument_lines = shift;
    my @argument_columns = shift;
603

604 605 606
    # Variable
    my $type;

607
    if(s/^WINE_(?:DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\(\s*(\w+)\s*\)\s*//s) { # FIXME: Wine specific kludge
608
	$self->_update_c_position($&, \$line, \$column);
609
    } elsif(s/^__ASM_GLOBAL_FUNC\(\s*(\w+)\s*,\s*//s) { # FIXME: Wine specific kludge
610
	$self->_update_c_position($&, \$line, \$column);
611 612 613
	$self->_parse_c_until_one_of("\)", \$_, \$line, \$column);
	if(s/\)//) {
	    $column++;
614
	}
615
    } elsif(s/^(?:DEFINE_AVIGUID|DEFINE_OLEGUID)\s*(?=\()//s) { # FIXME: Wine specific kludge
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
	$self->_update_c_position($&, \$line, \$column);

	my @arguments;
	my @argument_lines;
	my @argument_columns;

	if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
	    return 0;
	}
    } elsif(s/^DEFINE_COMMON_NOTIFICATIONS\(\s*(\w+)\s*,\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
	$self->_update_c_position($&, \$line, \$column);
    } elsif(s/^MAKE_FUNCPTR\(\s*(\w+)\s*\)//s) { # FIXME: Wine specific kludge
	$self->_update_c_position($&, \$line, \$column);
    } elsif(s/^START_TEST\(\s*(\w+)\s*\)\s*{//s) { # FIXME: Wine specific kludge
	$self->_update_c_position($&, \$line, \$column);
    } elsif(s/^int\s*_FUNCTION_\s*{//s) { # FIXME: Wine specific kludge
	$self->_update_c_position($&, \$line, \$column);
633 634 635 636 637 638 639 640
    } elsif(s/^(?:jump|strong)_alias//s) { # FIXME: GNU C library specific kludge
	$self->_update_c_position($&, \$line, \$column);
    } elsif(s/^(?:__asm__|asm)\s*\(//) {
	$self->_update_c_position($&, \$line, \$column);
    } elsif($self->parse_c_typedef(\$_, \$line, \$column)) {
	# Nothing
    } elsif($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type, \$name)) {
	# Nothing
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    } elsif($self->parse_c_function(\$_, \$line, \$column, \$function)) {
	if(&$$found_function($function))
	{
	    my $statements = $function->statements;
	    my $statements_line = $function->statements_line;
	    my $statements_column = $function->statements_column;

	    if(defined($statements)) {
		if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
		    return 0;
		}
	    }
	}
    } else {
	$self->_parse_c_error($_, $line, $column, "declaration");
    }
657

658 659 660
    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;
661

662 663 664 665 666 667
    return 1;
}

########################################################################
# parse_c_declarations

668 669 670 671 672 673 674 675 676 677 678 679 680 681
sub parse_c_declarations($$$$) {
    my $self = shift;

    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    return 1;
}

########################################################################
# _parse_c

sub _parse_c($$$$$$) {
682 683
    my $self = shift;

684
    my $pattern = shift;
685 686 687
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;
688

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
    my $refmatch = shift;

    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

    my $match;
    if(s/^(?:$pattern)//s) {
	$self->_update_c_position($&, \$line, \$column);
	$match = $&;
    } else {
	return 0;
    }

    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    $$refmatch = $match;

711 712 713
    return 1;
}

714 715 716
########################################################################
# parse_c_enum

717
sub parse_c_enum($$$$) {
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
    my $self = shift;

    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);

    if (!s/^enum\s+((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
	return 0;
    }
    my $_name = $1 || "";

    $self->_update_c_position($&, \$line, \$column);

    my $name = "";
    
    my $match;
    while ($self->_parse_c_on_same_level_until_one_of(',', \$_, \$line, \$column, \$match)) {
	if ($match) {
	    if ($match !~ /^(\w+)\s*(?:=\s*(.*?)\s*)?$/) {
		$self->_parse_c_error($_, $line, $column, "enum");
	    }
	    my $enum_name = $1;
	    my $enum_value = $2 || "";

	    # $output->write("enum:$_name:$enum_name:$enum_value\n");
	}

	if ($self->_parse_c(',', \$_, \$line, \$column)) {
	    next;
	} elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
	    # FIXME: Kludge
	    my $tuple = "($_)";
	    my $tuple_line = $line;
	    my $tuple_column = $column - 1;
	    
	    my @arguments;
	    my @argument_lines;
		    my @argument_columns;
	    
	    if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
				     \@arguments, \@argument_lines, \@argument_columns)) 
	    {
		$self->_parse_c_error($_, $line, $column, "enum");
	    }
	    
	    # FIXME: Kludge
	    if ($#arguments >= 0) {
		$name = $arguments[0];
	    }
	    
	    last;
	} else {
	    $self->_parse_c_error($_, $line, $column, "enum");
	}
    }

    $self->_update_c_position($_, \$line, \$column);

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;
}


788 789 790
########################################################################
# parse_c_expression

791
sub parse_c_expression($$$$) {
792 793 794 795 796 797 798 799 800 801 802 803 804 805
    my $self = shift;

    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};

    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);

806 807 808 809 810 811
    while($_) {
	if(s/^(.*?)(\w+\s*\()/$2/s) {
	    $self->_update_c_position($1, \$line, \$column);

	    my $begin_line = $line;
	    my $begin_column = $column + 1;
812

813 814 815 816 817 818 819
	    my $name;
	    my @arguments;
	    my @argument_lines;
	    my @argument_columns;
	    if(!$self->parse_c_function_call(\$_, \$line, \$column, \$name, \@arguments, \@argument_lines, \@argument_columns)) {
		return 0;
	    }
820

821
	    if(&$$found_function_call($begin_line, $begin_column, $line, $column, $name, \@arguments))
822
	    {
823 824 825 826 827 828
		while(defined(my $argument = shift @arguments) &&
		      defined(my $argument_line = shift @argument_lines) &&
		      defined(my $argument_column = shift @argument_columns))
		{
		    $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
		}
829
	    }
830 831
	} else {
	    $_ = "";
832 833 834
	}
    }

835
    $self->_update_c_position($_, \$line, \$column);
836 837 838 839 840 841 842 843

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    return 1;
}

844 845 846
########################################################################
# parse_c_file

847
sub parse_c_file($$$$) {
848 849 850
    my $self = shift;

    my $found_comment = \${$self->{FOUND_COMMENT}};
851
    my $found_line = \${$self->{FOUND_LINE}};
852 853 854 855

    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;
856

857 858 859 860 861 862 863 864 865 866 867
    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

    my $declaration = "";
    my $declaration_line = $line;
    my $declaration_column = $column;

    my $previous_line = 0;
    my $previous_column = -1;

868
    my $preprocessor_condition;
869 870
    my $if = 0;
    my $if0 = 0;
871
    my $extern_c = 0;
872

873 874 875 876 877
    my $blevel = 1;
    my $plevel = 1;
    while($plevel > 0 || $blevel > 0) {
	my $match;
	$self->_parse_c_until_one_of("#/\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
878

879 880
	if($line != $previous_line) {
	    &$$found_line($line);
881
	} elsif(0 && $column == $previous_column) {
882 883 884
	    $self->_parse_c_error($_, $line, $column, "file", "no progress");
	} else {
	    # &$$found_line("$line.$column");
885 886 887 888
	}
	$previous_line = $line;
	$previous_column = $column;

889 890 891
	if($match !~ /^\s+$/s && $options->debug) {
	    $self->_parse_c_warning($_, $line, $column, "file", "$plevel $blevel: '$declaration' '$match'");
	}
892 893 894 895

	if(!$declaration && $match =~ s/^\s+//s) {
	    $self->_update_c_position($&, \$declaration_line, \$declaration_column);
	}
896

897 898
	if(!$if0) {
	    $declaration .= $match;
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952

	    # FIXME: Kludge
	    if ($declaration =~ s/^extern\s*\"C\"//s) {
		if (s/^\{//) {
		    $self->_update_c_position($&, \$line, \$column);
		    $declaration = "";
		    $declaration_line = $line;
		    $declaration_column = $column;

		    $extern_c = 1;
		    next;
		}
	    } elsif ($extern_c && $blevel == 1 && $plevel == 1 && !$declaration) {
		if (s/^\}//) {
		    $self->_update_c_position($&, \$line, \$column);
		    $declaration = "";
		    $declaration_line = $line;
		    $declaration_column = $column;
		    
		    $extern_c = 0;
		    next;
		}
	    } elsif($declaration =~ s/^(?:__DEFINE_(?:GET|SET)_SEG|OUR_GUID_ENTRY)\s*(?=\()//sx) { # FIXME: Wine specific kludge
		my $prefix = $&;
		if ($plevel > 2 || !s/^\)//) {
		    $declaration = "$prefix$declaration";
		} else {
		    $plevel--;
		    $self->_update_c_position($&, \$line, \$column);
		    $declaration .= $&;

		    my @arguments;
		    my @argument_lines;
		    my @argument_columns;

		    if(!$self->parse_c_tuple(\$declaration, \$declaration_line, \$declaration_column,
					     \@arguments, \@argument_lines, \@argument_columns)) 
		    {
			$self->_parse_c_error($declaration, $declaration_line, $declaration_column, "file", "tuple expected");
		    }

		    $declaration = "";
		    $declaration_line = $line;
		    $declaration_column = $column;
		    
		    next;
		}
	    } elsif ($declaration =~ s/^(?:DEFINE_SHLGUID)\s*\(.*?\)//s) {
		$self->_update_c_position($&, \$declaration_line, \$declaration_column);
	    } elsif ($declaration =~ s/^(?:DECL_WINELIB_TYPE_AW|DECLARE_HANDLE(?:16)?|TYPE_MARSHAL)\(\s*(\w+)\s*\)\s*//s) {
		$self->_update_c_position($&, \$declaration_line, \$declaration_column);
	    } elsif ($declaration =~ s/^ICOM_DEFINE\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*//s) {
		$self->_update_c_position($&, \$declaration_line, \$declaration_column);
	    }
953 954 955 956 957 958 959 960 961 962 963 964 965 966
	} else {
	    my $blank_lines = 0;

	    local $_ = $match;
	    while(s/^.*?\n//) { $blank_lines++; }

	    if(!$declaration) {
		$declaration_line = $line;
		$declaration_column = $column;
	    } else {
		$declaration .= "\n" x $blank_lines;
	    }

	}
967 968 969 970 971 972 973 974 975 976 977 978

	if(/^[\#\/]/) {
	    my $blank_lines = 0;
	    if(s/^\#\s*//) {
		my $preprocessor_line = $line;
		my $preprocessor_column = $column;

		my $preprocessor = $&;
		while(s/^(.*?)\\\s*\n//) {
		    $blank_lines++;
		    $preprocessor .= "$1\n";
		}
979 980 981 982 983 984 985 986
		if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
		    $_ = "$2\n$_";
		    if(defined($3)) {
			$preprocessor .= "$1$3";
		    } else {
			$preprocessor .= $1;
		    }
		} elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
987 988 989 990 991 992 993 994
		    if(defined($2)) {
			$_ = "$2\n$_";
		    } else {
			$blank_lines++;
		    }
		    $preprocessor .= $1;
		}

995

996
		if($preprocessor =~ /^\#\s*if/) {
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
		    if($preprocessor =~ /^\#\s*if\s*0/) {
			$if0++;
		    } elsif($if0 > 0) {
			$if++;
		    } else {
			if($preprocessor =~ /^\#\s*ifdef\s+WORDS_BIGENDIAN$/) {
			    $preprocessor_condition = "defined(WORD_BIGENDIAN)";
			    # $output->write("'$preprocessor_condition':'$declaration'\n")
			} else {
			    $preprocessor_condition = "";
			}
		    }
		} elsif($preprocessor =~ /^\#\s*else/) {
		    if ($preprocessor_condition ne "") {
			$preprocessor_condition =~ "!$preprocessor_condition";
			$preprocessor_condition =~ s/^!!/!/;
			# $output->write("'$preprocessor_condition':'$declaration'\n")
		    }
		} elsif($preprocessor =~ /^\#\s*endif/) {
1016 1017 1018 1019 1020 1021
		    if($if0 > 0) {
			if($if > 0) {
			    $if--;
			} else {
			    $if0--;
			}
1022 1023 1024 1025 1026
		    } else {
			if ($preprocessor_condition ne "") {
			    # $output->write("'$preprocessor_condition':'$declaration'\n");
			    $preprocessor_condition = "";
			}
1027 1028 1029
		    }
		}

1030
		if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
1031 1032 1033 1034 1035 1036 1037 1038
		     return 0;
		}
	    }

	    if(s/^\/\*.*?\*\///s) {
		&$$found_comment($line, $column + 1, $&);
	        local $_ = $&;
		while(s/^.*?\n//) {
1039
		    $blank_lines++;
1040
		}
1041 1042
		if($_) {
		    $column += length($_);
1043 1044
		}
	    } elsif(s/^\/\/(.*?)\n//) {
1045
		&$$found_comment($line, $column + 1, $&);
1046 1047
		$blank_lines++;
	    } elsif(s/^\///) {
1048 1049 1050 1051
		if(!$if0) {
		    $declaration .= $&;
		    $column++;
		}
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	    }

	    $line += $blank_lines;
	    if($blank_lines > 0) {
		$column = 0;
	    }

	    if(!$declaration) {
		$declaration_line = $line;
		$declaration_column = $column;
1062
	    } elsif($blank_lines > 0) {
1063 1064 1065 1066
		$declaration .= "\n" x $blank_lines;
	    }

	    next;
1067
	}
1068 1069

	$column++;
1070 1071 1072 1073 1074 1075

	if($if0) {
	    s/^.//;
	    next;
	}

1076 1077 1078
	if(s/^[\(\[]//) {
	    $plevel++;
	    $declaration .= $&;
1079 1080 1081 1082
	} elsif(s/^\]//) {
	    $plevel--;
	    $declaration .= $&;
	} elsif(s/^\)//) {
1083
	    $plevel--;
1084 1085 1086
	    if($blevel <= 0) {
		$self->_parse_c_error($_, $line, $column, "file", ") without (");
	    }
1087
	    $declaration .= $&;
1088 1089 1090 1091 1092 1093 1094 1095 1096
	    if($plevel == 1 && $declaration =~ /^__ASM_GLOBAL_FUNC/) {
		if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
		    return 0;
		}
		$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
		$declaration = "";
		$declaration_line = $line;
		$declaration_column = $column;
	    }
1097 1098 1099 1100 1101
	} elsif(s/^\{//) {
	    $blevel++;
	    $declaration .= $&;
	} elsif(s/^\}//) {
	    $blevel--;
1102 1103 1104 1105
	    if($blevel <= 0) {
		$self->_parse_c_error($_, $line, $column, "file", "} without {");
	    }

1106
	    $declaration .= $&;
1107

1108
	    if($declaration =~ /^typedef/s ||
1109
	       $declaration =~ /^(?:const\s+|extern\s+|static\s+|volatile\s+)*(?:interface|struct|union)(?:\s+\w+)?\s*\{/s)
1110 1111 1112
	    {
		# Nothing
	    } elsif($plevel == 1 && $blevel == 1) {
1113 1114 1115 1116 1117 1118 1119
		if(!$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
		    return 0;
		}
		$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
		$declaration = "";
		$declaration_line = $line;
		$declaration_column = $column;
1120
	    } elsif($column == 1 && !$extern_c) {
1121
		$self->_parse_c_error("", $line, $column, "file", "inner } ends on column 1");
1122 1123
	    }
	} elsif(s/^;//) {
1124
	    $declaration .= $&;
1125
	    if(0 && $blevel == 1 &&
1126
	       $declaration !~ /^typedef/ &&
1127
	       $declaration !~ /^(?:const\s+|extern\s+|static\s+|volatile\s+)?(?:interface|struct|union)(?:\s+\w+)?\s*\{/s &&
1128 1129
	       $declaration =~ /^(?:\w+(?:\s*\*)*\s+)*(\w+)\s*\(\s*(?:(?:\w+\s*,\s*)*(\w+))?\s*\)\s*(.*?);$/s &&
	       $1 ne "ICOM_VTABLE" && defined($2) && $2 ne "void" && $3) # K&R
1130
	    {
1131
		$self->_parse_c_warning("", $line, $column, "file", "function $1: warning: function has K&R format");
1132 1133
	    } elsif($plevel == 1 && $blevel == 1) {
		$declaration =~ s/\s*;$//;
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
		if($declaration && !$self->parse_c_declaration(\$declaration, \$declaration_line, \$declaration_column)) {
		    return 0;
		}
		$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
		$declaration = "";
		$declaration_line = $line;
		$declaration_column = $column;
	    }
	} elsif(/^\s*$/ && $declaration =~ /^\s*$/ && $match =~ /^\s*$/) {
	    $plevel = 0;
	    $blevel = 0;
	} else {
1146
	    $self->_parse_c_error($_, $line, $column, "file", "parse error: '$declaration' '$match'");
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	}
    }

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    return 1;
}

########################################################################
# parse_c_function

1160
sub parse_c_function($$$$$) {
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    my $self = shift;

    my $file = \${$self->{FILE}};
    my $create_function = \${$self->{CREATE_FUNCTION}};

    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    my $reffunction = shift;
1171

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

    my $linkage = "";
    my $calling_convention = "";
    my $return_type;
    my $name;
    my @arguments;
    my @argument_lines;
    my @argument_columns;
    my $statements;
    my $statements_line;
    my $statements_column;

    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);

    my $begin_line = $line;
    my $begin_column = $column + 1;

1192
    if($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
1193 1194 1195 1196 1197
	# Nothing
    }

    # $self->_parse_c_warning($_, $line, $column, "function", "");

1198
    my $match;
1199
    while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1200 1201
			  'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
			  'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1202
			  'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1203
			  \$_, \$line, \$column, \$match))
1204
    {
1205
	if($match =~ /^(?:extern|static)$/) {
1206 1207 1208 1209
	    if(!$linkage) {
		$linkage = $match;
	    }
	}
1210 1211
    }

1212
    if($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
1213 1214 1215 1216 1217 1218 1219 1220
	# Nothing
    } elsif($self->_parse_c('WINE_EXCEPTION_FILTER\(\w+\)', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
	# Nothing
    } else {
	if(!$self->parse_c_type(\$_, \$line, \$column, \$return_type)) {
	    return 0;
	}

1221 1222
	$self->_parse_c('inline|FAR', \$_, \$line, \$column);

1223
	$self->_parse_c($CALL_CONVENTION,
1224
			\$_, \$line, \$column, \$calling_convention);
1225

1226 1227

	# FIXME: ???: Old variant of __attribute((const))
1228
	$self->_parse_c('(?:const|volatile)', \$_, \$line, \$column);
1229 1230

	if(!$self->_parse_c('(?:operator\s*!=|(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)', \$_, \$line, \$column, \$name)) {
1231
	    return 0;
1232 1233
	}

1234 1235 1236 1237 1238 1239
	my $p = 0;
	if(s/^__P\s*\(//) {
	    $self->_update_c_position($&, \$line, \$column);
	    $p = 1;
	}

1240
	if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1241
	    return 0;
1242
	}
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253

	if($p) {
	    if (s/^\)//) {
		$self->_update_c_position($&, \$line, \$column);
	    } else {
		$self->_parse_c_error($_, $line, $column, "function");
	    }
	}
    }


1254
    if($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
1255
	# Nothing
1256
    }
1257 1258 1259 1260 1261 1262 1263

    my $kar;
    # FIXME: Implement proper handling of K&R C functions
    $self->_parse_c_until_one_of("{", \$_, \$line, \$column, $kar);

    if($kar) {
	$output->write("K&R: $kar\n");
1264
    }
1265

1266 1267 1268 1269 1270 1271
    if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
	return 0;
    }

    my $end_line = $line;
    my $end_column = $column;
1272

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    my $function = &$$create_function;

    $function->file($$file);
    $function->begin_line($begin_line);
    $function->begin_column($begin_column);
    $function->end_line($end_line);
    $function->end_column($end_column);
    $function->linkage($linkage);
1285
    $function->return_type($return_type);
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
    $function->calling_convention($calling_convention);
    $function->name($name);
    # if(defined($argument_types)) {
    #     $function->argument_types([@$argument_types]);
    # }
    # if(defined($argument_names)) {
    #     $function->argument_names([@$argument_names]);
    # }
    $function->statements_line($statements_line);
    $function->statements_column($statements_column);
    $function->statements($statements);

    $$reffunction = $function;

    return 1;
}

########################################################################
# parse_c_function_call

1306
sub parse_c_function_call($$$$$$$$) {
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
    my $self = shift;

    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    my $refname = shift;
    my $refarguments = shift;
    my $refargument_lines = shift;
    my $refargument_columns = shift;

    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

    my $name;
    my @arguments;
    my @argument_lines;
    my @argument_columns;

1327 1328
    if(s/^(\w+)(\s*)(?=\()//s) {
	$self->_update_c_position($&, \$line, \$column);
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353

	$name = $1;

	if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
	    return 0;
	}
    } else {
	return 0;
    }

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    $$refname = $name;
    @$refarguments = @arguments;
    @$refargument_lines = @argument_lines;
    @$refargument_columns = @argument_columns;

    return 1;
}

########################################################################
# parse_c_preprocessor

1354
sub parse_c_preprocessor($$$$) {
1355 1356 1357 1358
    my $self = shift;

    my $found_preprocessor = \${$self->{FOUND_PREPROCESSOR}};

1359 1360 1361 1362
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

1363
    local $_ = $$refcurrent;
1364 1365 1366
    my $line = $$refline;
    my $column = $$refcolumn;

1367 1368 1369 1370 1371 1372 1373 1374
    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);

    my $begin_line = $line;
    my $begin_column = $column + 1;

    if(!&$$found_preprocessor($begin_line, $begin_column, "$_")) {
	return 1;
    }
1375

1376
    if(/^\#\s*define\s*(.*?)$/s) {
1377 1378 1379 1380 1381
	$self->_update_c_position($_, \$line, \$column);
    } elsif(/^\#\s*else/s) {
	$self->_update_c_position($_, \$line, \$column);
    } elsif(/^\#\s*endif/s) {
	$self->_update_c_position($_, \$line, \$column);
1382
    } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
	$self->_update_c_position($_, \$line, \$column);
    } elsif(/^\#\s*include\s+(.*?)$/s) {
	$self->_update_c_position($_, \$line, \$column);
    } elsif(/^\#\s*undef\s+(.*?)$/s) {
	$self->_update_c_position($_, \$line, \$column);
    } else {
	$self->_parse_c_error($_, $line, $column, "preprocessor");
    }

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    return 1;
}

########################################################################
# parse_c_statement

1402
sub parse_c_statement($$$$) {
1403 1404 1405 1406 1407 1408 1409 1410
    my $self = shift;

    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};

1411
    local $_ = $$refcurrent;
1412 1413
    my $line = $$refline;
    my $column = $$refcolumn;
1414

1415
    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1416

1417
    $self->_parse_c('(?:case\s+)?(\w+)\s*:\s*', \$_, \$line, \$column);
1418

1419
    # $output->write("$line.$column: statement: '$_'\n");
1420 1421 1422 1423 1424 1425 1426

    if(/^$/) {
	# Nothing
    } elsif(/^\{/) {
	my $statements;
	my $statements_line;
	my $statements_column;
1427
	if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1428 1429
	    return 0;
	}
1430
	if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1431 1432
	    return 0;
	}
1433 1434
    } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
	$self->_update_c_position($&, \$line, \$column);
1435

1436
	my $name = $1;
1437 1438 1439 1440

	my @arguments;
	my @argument_lines;
	my @argument_columns;
1441
	if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1442 1443 1444
	    return 0;
	}

1445 1446
	$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
	if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1447 1448
	    return 0;
	}
1449
	$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1450 1451 1452 1453 1454

	while(defined(my $argument = shift @arguments) &&
	      defined(my $argument_line = shift @argument_lines) &&
	      defined(my $argument_column = shift @argument_columns))
	{
1455
	    $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1456 1457
	}
    } elsif(s/^else//) {
1458
	$self->_update_c_position($&, \$line, \$column);
1459
	if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1460 1461
	    return 0;
	}
1462 1463 1464 1465 1466 1467
    } elsif(s/^return//) {
	$self->_update_c_position($&, \$line, \$column);
	$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
	if(!$self->parse_c_expression(\$_, \$line, \$column)) {
	    return 0;
	}
1468
    } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1469 1470
	# Nothing
    } else {
1471
	# $self->_parse_c_error($_, $line, $column, "statement");
1472 1473
    }

1474
    $self->_update_c_position($_, \$line, \$column);
1475 1476 1477 1478 1479 1480 1481 1482

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    return 1;
}

1483 1484 1485
########################################################################
# parse_c_statements

1486
sub parse_c_statements($$$$) {
1487 1488
    my $self = shift;

1489 1490 1491 1492
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

1493 1494 1495
    my $found_function_call = \${$self->{FOUND_FUNCTION_CALL}};

    local $_ = $$refcurrent;
1496 1497 1498
    my $line = $$refline;
    my $column = $$refcolumn;

1499
    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1500

1501 1502
    # $output->write("$line.$column: statements: '$_'\n");

1503 1504 1505 1506
    my $statement = "";
    my $statement_line = $line;
    my $statement_column = $column;

1507 1508 1509
    my $previous_line = -1;
    my $previous_column = -1;

1510 1511 1512 1513
    my $blevel = 1;
    my $plevel = 1;
    while($plevel > 0 || $blevel > 0) {
	my $match;
1514
	$self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1515

1516 1517 1518 1519 1520 1521
	if($previous_line == $line && $previous_column == $column) {
	    $self->_parse_c_error($_, $line, $column, "statements", "no progress");
	}
	$previous_line = $line;
	$previous_column = $column;

1522 1523 1524
	# $output->write("'$match' '$_'\n");

	$statement .= $match;
1525
	$column++;
1526 1527 1528 1529 1530 1531
	if(s/^[\(\[]//) {
	    $plevel++;
	    $statement .= $&;
	} elsif(s/^[\)\]]//) {
	    $plevel--;
	    if($plevel <= 0) {
1532
		$self->_parse_c_error($_, $line, $column, "statements");
1533 1534 1535 1536 1537 1538 1539 1540 1541
	    }
	    $statement .= $&;
	} elsif(s/^\{//) {
	    $blevel++;
	    $statement .= $&;
	} elsif(s/^\}//) {
	    $blevel--;
	    $statement .= $&;
	    if($blevel == 1) {
1542
		if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1543 1544
		    return 0;
		}
1545
		$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1546 1547 1548 1549 1550 1551
		$statement = "";
		$statement_line = $line;
		$statement_column = $column;
	    }
	} elsif(s/^;//) {
	    if($plevel == 1 && $blevel == 1) {
1552
		if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1553 1554 1555
		    return 0;
		}

1556
		$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
		$statement = "";
		$statement_line = $line;
		$statement_column = $column;
	    } else {
		$statement .= $&;
	    }
	} elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
	    $plevel = 0;
	    $blevel = 0;
	} else {
1567
	    $self->_parse_c_error($_, $line, $column, "statements");
1568 1569 1570
	}
    }

1571
    $self->_update_c_position($_, \$line, \$column);
1572 1573 1574 1575 1576 1577 1578 1579

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    return 1;
}

1580 1581 1582
########################################################################
# parse_c_struct_union

1583
sub parse_c_struct_union($$$$$$$$$) {
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
    my $self = shift;

    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    my $refkind = shift;
    my $ref_name = shift;
    my $reffield_type_names = shift;
    my $reffield_names = shift;
    my $refnames = shift;

    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

    my $kind;
    my $_name;
    my @field_type_names = ();
    my @field_names = ();
    my @names = ();

    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);

1608
    if (!s/^(interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
	return 0;
    }
    $kind = $1;
    $_name = $2 || "";

    $self->_update_c_position($&, \$line, \$column);
    
    $kind =~ s/\s+//g;

    my $match;
    while ($_ && $self->_parse_c_on_same_level_until_one_of(';', \$_, \$line, \$column, \$match))
    {
	my $field_linkage;
	my $field_type_name;
	my $field_name;
	
	if ($self->parse_c_variable(\$match, \$line, \$column, \$field_linkage, \$field_type_name, \$field_name)) {
	    $field_type_name =~ s/\s+/ /g;
	    
	    push @field_type_names, $field_type_name;
	    push @field_names, $field_name;
	    # $output->write("$kind:$_name:$field_type_name:$field_name\n");
	} elsif ($match) {
	    $self->_parse_c_error($_, $line, $column, "typedef $kind: '$match'");
	}
	
	if ($self->_parse_c(';', \$_, \$line, \$column)) {
	    next;
	} elsif ($self->_parse_c('}', \$_, \$line, \$column)) {
	    # FIXME: Kludge
	    my $tuple = "($_)";
	    my $tuple_line = $line;
	    my $tuple_column = $column - 1;
	    
	    my @arguments;
	    my @argument_lines;
	    my @argument_columns;
	    
	    if(!$self->parse_c_tuple(\$tuple, \$tuple_line, \$tuple_column,
				     \@arguments, \@argument_lines, \@argument_columns)) 
	    {
		$self->_parse_c_error($_, $line, $column, "$kind");
	    }

	    foreach my $argument (@arguments) {
		my $name = $argument;

		push @names, $name;
	    }

	    last;
	} else {
	    $self->_parse_c_error($_, $line, $column, "$kind");
	}
    }

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    $$refkind = $kind;
    $$ref_name = $_name;
    @$reffield_type_names = @field_type_names;
    @$reffield_names = @field_names;
    @$refnames = @names;

    return 1;
}

1678 1679 1680
########################################################################
# parse_c_tuple

1681
sub parse_c_tuple($$$$$$$) {
1682 1683
    my $self = shift;

1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    # FIXME: Should not write directly
    my $items = shift;
    my $item_lines = shift;
    my $item_columns = shift;

    local $_ = $$refcurrent;

    my $line = $$refline;
    my $column = $$refcolumn;

    my $item;
    if(s/^\(//) {
	$column++;
	$item = "";
    } else {
	return 0;
    }

    my $item_line = $line;
    my $item_column = $column + 1;

    my $plevel = 1;
    while($plevel > 0) {
	my $match;
1712
	$self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734

	$column++;

	$item .= $match;
	if(s/^\)//) {
	    $plevel--;
	    if($plevel == 0) {
		push @$item_lines, $item_line;
		push @$item_columns, $item_column;
		push @$items, $item;
		$item = "";
	    } else {
		$item .= ")";
	    }
	} elsif(s/^\(//) {
	    $plevel++;
	    $item .= "(";
	} elsif(s/^,//) {
	    if($plevel == 1) {
		push @$item_lines, $item_line;
		push @$item_columns, $item_column;
		push @$items, $item;
1735
		$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
		$item_line = $line;
		$item_column = $column + 1;
		$item = "";
	    } else {
		$item .= ",";
	    }
	} else {
	    return 0;
	}
    }

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    return 1;
}

1754 1755 1756
########################################################################
# parse_c_type

1757
sub parse_c_type($$$$$) {
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
    my $self = shift;

    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    my $reftype = shift;

    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

    my $type;

1772
    $self->_parse_c("(?:const|volatile)", \$_, \$line, \$column);
1773

1774
    if($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1775
	# Nothing
1776
    } elsif($self->_parse_c('(?:enum\s+|interface\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1777 1778
			    \$_, \$line, \$column, \$type))
    {
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
	# Nothing
    } else {
	return 0;
    }
    $type =~ s/\s//g;

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    $$reftype = $type;

    return 1;
}

########################################################################
# parse_c_typedef

1797
sub parse_c_typedef($$$$) {
1798 1799
    my $self = shift;

1800 1801
    my $create_type = \${$self->{CREATE_TYPE}};
    my $found_type = \${$self->{FOUND_TYPE}};
1802
    my $preprocessor_condition = \${$self->{PREPROCESSOR_CONDITION}};
1803

1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

    my $type;

1814 1815 1816
    if (!$self->_parse_c("typedef", \$_, \$line, \$column)) {
	return 0;
    }
1817

1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
    my $finished = 0;
    
    if ($finished) {
	# Nothing
    } elsif ($self->parse_c_enum(\$_, \$line, \$column)) {
	$finished = 1;
    } 

    my $kind;
    my $_name;
    my @field_type_names;
    my @field_names;
    my @names;
    if ($finished) {
	# Nothing
    } elsif ($self->parse_c_struct_union(\$_, \$line, \$column,
					 \$kind, \$_name, \@field_type_names, \@field_names, \@names))
    {
	my $base_name;
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
        foreach my $name (@names)
        {
            if ($name =~ /^\w+$/)
            {
                $base_name = $name;
                last;
            }
        }
        $base_name="$kind $_name" if (!defined $base_name and defined $_name);
        $base_name=$kind if (!defined $base_name);
1847 1848 1849 1850 1851 1852 1853 1854 1855
	foreach my $name (@names) {
	    if ($name =~ /^\w+$/) {
		my $type = &$$create_type();
		
		$type->kind($kind);
		$type->_name($_name);
		$type->name($name);
		$type->field_type_names([@field_type_names]);
		$type->field_names([@field_names]);
1856

1857 1858 1859 1860
		&$$found_type($type);
	    } elsif ($name =~ /^(\*+)\s*(?:RESTRICTED_POINTER\s+)?(\w+)$/) {
		my $type_name = "$base_name $1";
		$name = $2;
1861 1862 1863 1864 1865 1866 1867 1868

		my $type = &$$create_type();

		$type->kind("");
		$type->name($name);
		$type->field_type_names([$type_name]);
		$type->field_names([""]);

1869 1870 1871
		&$$found_type($type);		
	    } else {
		$self->_parse_c_error($_, $line, $column, "typedef 2");
1872
	    }
1873 1874 1875 1876
	}
	
	$finished = 1;
    }
1877

1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
    my $linkage;
    my $type_name;
    my $name;
    if ($finished) {
	# Nothing
    } elsif ($self->parse_c_variable(\$_, \$line, \$column, \$linkage, \$type_name, \$name)) {
	$type_name =~ s/\s+/ /g;
	
	if(defined($type_name) && defined($name)) {
	    my $type = &$$create_type();
	    
	    if (length($name) == 0) {
1890
		$self->_parse_c_error($_, $line, $column, "typedef");
1891
	    }
1892

1893 1894 1895 1896 1897 1898 1899
	    $type->kind("");
	    $type->name($name);
	    $type->field_type_names([$type_name]);
	    $type->field_names([""]);
	    
	    &$$found_type($type);
	}
1900

1901 1902 1903
	if (0 && $_ && !/^,/) {
	    $self->_parse_c_error($_, $line, $column, "typedef");
	}   
1904
    } else {
1905
	$self->_parse_c_error($_, $line, $column, "typedef");
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
    }

    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    return 1;
}

########################################################################
# parse_c_variable

1918
sub parse_c_variable($$$$$$$) {
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
    my $self = shift;

    my $found_variable = \${$self->{FOUND_VARIABLE}};

    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

    my $reflinkage = shift;
    my $reftype = shift;
    my $refname = shift;

    local $_ = $$refcurrent;
    my $line = $$refline;
    my $column = $$refcolumn;

    $self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);

    my $begin_line = $line;
    my $begin_column = $column + 1;

    my $linkage = "";
1941
    my $sign = "";
1942 1943 1944
    my $type = "";
    my $name = "";

1945 1946
    # $self->_parse_c_warning($_, $line, $column, "variable");

1947
    my $match;
1948
    while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1949 1950
			  'signed(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
			  'unsigned(?=\s+__int(?:8|16|32|64)\b|\s+char\b|\s+int\b|\s+long(?:\s+long)?\b|\s+short\b)|' .
1951
			  'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1952
			  \$_, \$line, \$column, \$match))
1953
    {
1954
	if ($match =~ /^(?:extern|static)$/) {
1955
	    if (!$linkage) {
1956
		$linkage = $match;
1957 1958 1959
	    } else {
		$self->_parse_c_warning($_, $line, $column, "repeated linkage (ignored): $match");
	    }
1960
	} elsif ($match =~ /^(?:signed|unsigned)$/) {
1961 1962 1963 1964
	    if (!$sign) {
		$sign = "$match ";
	    } else {
		$self->_parse_c_warning($_, $line, $column, "repeated sign (ignored): $match");
1965 1966 1967 1968 1969 1970
	    }
	}
    }

    my $finished = 0;

1971 1972
    if($finished) {
	# Nothing
1973 1974
    } elsif(/^$/) {
	return 0;
1975
    } elsif (s/^(enum\s+|interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
	my $kind = $1;
	my $_name = $2;
	$self->_update_c_position($&, \$line, \$column);

	if(defined($_name)) {
	    $type = "$kind $_name { }";
	} else {
	    $type = "$kind { }";
	}

	$finished = 1;
1987
    } 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) {
1988
	$type = "$sign$1";
1989 1990
	$name = $2;

1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
	if (defined($3)) {
	    my $bits = $4;
	    local $_ = $3;
	    if (/^\[/) {
		$type .= $_;
	    } elsif (/^:/) {
		$type .= ":$bits";
	    } elsif (/^\{/) {
		# Nothing
	    }
	}

	$type = $self->_format_c_type($type);

2005
	$finished = 1;
2006
    } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*)\s*:\s*(\d+)$//s) {
2007 2008 2009 2010
	$type = "$sign$1:$2";
	$name = "";
	$type = $self->_format_c_type($type);

2011
	$finished = 1;
2012
    } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*\s*\(\s*(?:$CALL_CONVENTION)?(?:\s*\*)*)\s*(\w+)\s*(\)\s*\(.*?\))$//s) {
2013
	$type = $self->_format_c_type("$sign$1$3");
2014 2015
	$name = $2;

2016 2017 2018
	$finished = 1;
    } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
	$type = $match;
2019
	$finished = 1;
2020 2021
    } else {
	$self->_parse_c_warning($_, $line, $column, "variable", "'$_'");
2022
	$finished = 1;
2023 2024
    }

2025 2026 2027 2028 2029 2030 2031
    if($finished) {
	# Nothing
    } elsif($self->_parse_c('SEQ_DEFINEBUF', \$_, \$line, \$column, \$match)) { # Linux specific
	$type = $match;
	$finished = 1;
    } elsif($self->_parse_c('DEFINE_REGS_ENTRYPOINT_\w+|DPQ_DECL_\w+|HANDLER_DEF|IX86_ONLY', # Wine specific
			    \$_, \$line, \$column, \$match))
2032
    {
2033 2034 2035 2036 2037
	$type = $match;
	$finished = 1;
    } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
	$type = $match;
	$finished = 1;
2038
    } elsif(s/^(enum|interface|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*//s) {
2039 2040
	my $kind = $1;
	my $_name = $2;
2041 2042
	$self->_update_c_position($&, \$line, \$column);

2043 2044
	if(defined($_name)) {
	    $type = "struct $_name { }";
2045 2046 2047
	} else {
	    $type = "struct { }";
	}
2048
    } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
2049 2050 2051 2052 2053 2054
	$type = $&;
	$type =~ s/\s//g;
    } else {
	return 0;
    }

2055 2056 2057
    # $output->write("*** $type: '$_'\n");

    # $self->_parse_c_warning($_, $line, $column, "variable2", "");
2058 2059 2060 2061 2062 2063 2064 2065 2066

    if($finished) {
	# Nothing
    } elsif(s/^WINAPI\s*//) {
	$self->_update_c_position($&, \$line, \$column);
    }

    if($finished) {
	# Nothing
2067
    } elsif(s/^(\((?:$CALL_CONVENTION)?\s*\*?\s*(?:$CALL_CONVENTION)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
	$self->_update_c_position($&, \$line, \$column);

	$name = $1;
	$name =~ s/\s//g;

	$self->_parse_c_until_one_of("\\)", \$_, \$line, \$column);
	if(s/^\)//) { $column++; }
	$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);

        if(!s/^(?:=\s*|,\s*|$)//) {
	    return 0;
	}
2080
    } elsif(s/^(?:\*\s*)*(?:const\s+|volatile\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
2081 2082 2083 2084 2085 2086 2087 2088 2089
	$self->_update_c_position($&, \$line, \$column);

	$name = $1;
	$name =~ s/\s//g;
    } elsif(/^$/) {
	$name = "";
    } else {
	return 0;
    }
2090

2091 2092
    # $output->write("$type: $name: '$_'\n");

2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

    $$reflinkage = $linkage;
    $$reftype = $type;
    $$refname = $name;

    if(&$$found_variable($begin_line, $begin_column, $linkage, $type, $name))
    {
	# Nothing
    }

    return 1;
}

2109
1;