c_parser.pm 52.1 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 607 608
    # Variable
    my $type;

    if(0) {
	# Nothing
609
    } elsif(s/^WINE_(?:DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\(\s*(\w+)\s*\)\s*//s) { # FIXME: Wine specific kludge
610
	$self->_update_c_position($&, \$line, \$column);
611
    } elsif(s/^__ASM_GLOBAL_FUNC\(\s*(\w+)\s*,\s*//s) { # FIXME: Wine specific kludge
612
	$self->_update_c_position($&, \$line, \$column);
613 614 615
	$self->_parse_c_until_one_of("\)", \$_, \$line, \$column);
	if(s/\)//) {
	    $column++;
616
	}
617
    } elsif(s/^(?:DEFINE_AVIGUID|DEFINE_OLEGUID)\s*(?=\()//s) { # FIXME: Wine specific kludge
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	$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);
635 636 637 638 639 640 641 642
    } 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
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
    } 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");
    }
659

660 661 662
    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;
663

664 665 666 667 668 669
    return 1;
}

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

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

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

    return 1;
}

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

sub _parse_c($$$$$$) {
684 685
    my $self = shift;

686
    my $pattern = shift;
687 688 689
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;
690

691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
    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;

713 714 715
    return 1;
}

716 717 718
########################################################################
# parse_c_enum

719
sub parse_c_enum($$$$) {
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 788 789
    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;
}


790 791 792
########################################################################
# parse_c_expression

793
sub parse_c_expression($$$$) {
794 795 796 797 798 799 800 801 802 803 804 805 806 807
    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);

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

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

815 816 817 818 819 820 821
	    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;
	    }
822

823
	    if(&$$found_function_call($begin_line, $begin_column, $line, $column, $name, \@arguments))
824
	    {
825 826 827 828 829 830
		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);
		}
831
	    }
832 833
	} else {
	    $_ = "";
834 835 836
	}
    }

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

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

    return 1;
}

846 847 848
########################################################################
# parse_c_file

849
sub parse_c_file($$$$) {
850 851 852
    my $self = shift;

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

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

859 860 861 862 863 864 865 866 867 868 869
    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;

870
    my $preprocessor_condition;
871 872
    my $if = 0;
    my $if0 = 0;
873
    my $extern_c = 0;
874

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

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

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

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

899 900
	if(!$if0) {
	    $declaration .= $match;
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 953 954

	    # 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);
	    }
955 956 957 958 959 960 961 962 963 964 965 966 967 968
	} 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;
	    }

	}
969 970 971 972 973 974 975 976 977 978 979 980

	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";
		}
981 982 983 984 985 986 987 988
		if(s/^(.*?)(\/\*.*?\*\/)(.*?)\n//) {
		    $_ = "$2\n$_";
		    if(defined($3)) {
			$preprocessor .= "$1$3";
		    } else {
			$preprocessor .= $1;
		    }
		} elsif(s/^(.*?)(\/[\*\/].*?)?\n//) {
989 990 991 992 993 994 995 996
		    if(defined($2)) {
			$_ = "$2\n$_";
		    } else {
			$blank_lines++;
		    }
		    $preprocessor .= $1;
		}

997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

		if (0) {
		    # Nothing
		} elsif($preprocessor =~ /^\#\s*if/) {
		    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/) {
1020 1021 1022 1023 1024 1025
		    if($if0 > 0) {
			if($if > 0) {
			    $if--;
			} else {
			    $if0--;
			}
1026 1027 1028 1029 1030
		    } else {
			if ($preprocessor_condition ne "") {
			    # $output->write("'$preprocessor_condition':'$declaration'\n");
			    $preprocessor_condition = "";
			}
1031 1032 1033
		    }
		}

1034
		if(!$self->parse_c_preprocessor(\$preprocessor, \$preprocessor_line, \$preprocessor_column)) {
1035 1036 1037 1038 1039 1040 1041 1042
		     return 0;
		}
	    }

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

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

	    if(!$declaration) {
		$declaration_line = $line;
		$declaration_column = $column;
1066
	    } elsif($blank_lines > 0) {
1067 1068 1069 1070
		$declaration .= "\n" x $blank_lines;
	    }

	    next;
1071
	}
1072 1073

	$column++;
1074 1075 1076 1077 1078 1079

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

1080 1081 1082
	if(s/^[\(\[]//) {
	    $plevel++;
	    $declaration .= $&;
1083 1084 1085 1086
	} elsif(s/^\]//) {
	    $plevel--;
	    $declaration .= $&;
	} elsif(s/^\)//) {
1087
	    $plevel--;
1088 1089 1090
	    if($blevel <= 0) {
		$self->_parse_c_error($_, $line, $column, "file", ") without (");
	    }
1091
	    $declaration .= $&;
1092 1093 1094 1095 1096 1097 1098 1099 1100
	    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;
	    }
1101 1102 1103 1104 1105
	} elsif(s/^\{//) {
	    $blevel++;
	    $declaration .= $&;
	} elsif(s/^\}//) {
	    $blevel--;
1106 1107 1108 1109
	    if($blevel <= 0) {
		$self->_parse_c_error($_, $line, $column, "file", "} without {");
	    }

1110
	    $declaration .= $&;
1111

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

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

    return 1;
}

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

1164
sub parse_c_function($$$$$) {
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
    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;
1175

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
    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;

1196 1197 1198 1199 1200 1201 1202 1203
    if(0) {
	# Nothing
    } elsif($self->_parse_c('__declspec\((?:dllexport|dllimport|naked)\)|INTERNETAPI|RPCRTAPI', \$_, \$line, \$column)) {
	# Nothing
    }

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

1204
    my $match;
1205
    while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1206 1207
			  '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)|' .
1208
			  'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1209
			  \$_, \$line, \$column, \$match))
1210
    {
1211
	if($match =~ /^(?:extern|static)$/) {
1212 1213 1214 1215
	    if(!$linkage) {
		$linkage = $match;
	    }
	}
1216 1217
    }

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
    if(0) {
	# Nothing
    } elsif($self->_parse_c('DECL_GLOBAL_CONSTRUCTOR', \$_, \$line, \$column, \$name)) { # FIXME: Wine specific kludge
	# 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;
	}

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

1231
	$self->_parse_c($CALL_CONVENTION,
1232
			\$_, \$line, \$column, \$calling_convention);
1233

1234 1235

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

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

1242 1243 1244 1245 1246 1247
	my $p = 0;
	if(s/^__P\s*\(//) {
	    $self->_update_c_position($&, \$line, \$column);
	    $p = 1;
	}

1248
	if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1249
	    return 0;
1250
	}
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265

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


    if (0) {
	# Nothing
    } elsif($self->_parse_c('__attribute__\s*\(\s*\(\s*(?:constructor|destructor)\s*\)\s*\)', \$_, \$line, \$column)) {
	# Nothing
1266
    }
1267 1268 1269 1270 1271 1272 1273

    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");
1274
    }
1275

1276 1277 1278 1279 1280 1281
    if($_ && !$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
	return 0;
    }

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

1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
    $$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);
1295
    $function->return_type($return_type);
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
    $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

1316
sub parse_c_function_call($$$$$$$$) {
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
    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;

1337 1338
    if(s/^(\w+)(\s*)(?=\()//s) {
	$self->_update_c_position($&, \$line, \$column);
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363

	$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

1364
sub parse_c_preprocessor($$$$) {
1365 1366 1367 1368
    my $self = shift;

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

1369 1370 1371 1372
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

1373
    local $_ = $$refcurrent;
1374 1375 1376
    my $line = $$refline;
    my $column = $$refcolumn;

1377 1378 1379 1380 1381 1382 1383 1384
    $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;
    }
1385

1386 1387
    if(0) {
	# Nothing
1388
    } elsif(/^\#\s*define\s*(.*?)$/s) {
1389 1390 1391 1392 1393
	$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);
1394
    } elsif(/^\#\s*(?:if|ifdef|ifndef)?\s*(.*?)$/s) {
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
	$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

1414
sub parse_c_statement($$$$) {
1415 1416 1417 1418 1419 1420 1421 1422
    my $self = shift;

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

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

1423
    local $_ = $$refcurrent;
1424 1425
    my $line = $$refline;
    my $column = $$refcolumn;
1426

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

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

1431
    # $output->write("$line.$column: statement: '$_'\n");
1432 1433 1434 1435 1436 1437 1438

    if(/^$/) {
	# Nothing
    } elsif(/^\{/) {
	my $statements;
	my $statements_line;
	my $statements_column;
1439
	if(!$self->parse_c_block(\$_, \$line, \$column, \$statements, \$statements_line, \$statements_column)) {
1440 1441
	    return 0;
	}
1442
	if(!$self->parse_c_statements(\$statements, \$statements_line, \$statements_column)) {
1443 1444
	    return 0;
	}
1445 1446
    } elsif(s/^(for|if|switch|while)\s*(?=\()//) {
	$self->_update_c_position($&, \$line, \$column);
1447

1448
	my $name = $1;
1449 1450 1451 1452

	my @arguments;
	my @argument_lines;
	my @argument_columns;
1453
	if(!$self->parse_c_tuple(\$_, \$line, \$column, \@arguments, \@argument_lines, \@argument_columns)) {
1454 1455 1456
	    return 0;
	}

1457 1458
	$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
	if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1459 1460
	    return 0;
	}
1461
	$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1462 1463 1464 1465 1466

	while(defined(my $argument = shift @arguments) &&
	      defined(my $argument_line = shift @argument_lines) &&
	      defined(my $argument_column = shift @argument_columns))
	{
1467
	    $self->parse_c_expression(\$argument, \$argument_line, \$argument_column);
1468 1469
	}
    } elsif(s/^else//) {
1470
	$self->_update_c_position($&, \$line, \$column);
1471
	if(!$self->parse_c_statement(\$_, \$line, \$column)) {
1472 1473
	    return 0;
	}
1474 1475 1476 1477 1478 1479
    } 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;
	}
1480
    } elsif($self->parse_c_expression(\$_, \$line, \$column)) {
1481 1482
	# Nothing
    } else {
1483
	# $self->_parse_c_error($_, $line, $column, "statement");
1484 1485
    }

1486
    $self->_update_c_position($_, \$line, \$column);
1487 1488 1489 1490 1491 1492 1493 1494

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

    return 1;
}

1495 1496 1497
########################################################################
# parse_c_statements

1498
sub parse_c_statements($$$$) {
1499 1500
    my $self = shift;

1501 1502 1503 1504
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

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

    local $_ = $$refcurrent;
1508 1509 1510
    my $line = $$refline;
    my $column = $$refcolumn;

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

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

1515 1516 1517 1518
    my $statement = "";
    my $statement_line = $line;
    my $statement_column = $column;

1519 1520 1521
    my $previous_line = -1;
    my $previous_column = -1;

1522 1523 1524 1525
    my $blevel = 1;
    my $plevel = 1;
    while($plevel > 0 || $blevel > 0) {
	my $match;
1526
	$self->_parse_c_until_one_of("\\(\\)\\[\\]\\{\\};", \$_, \$line, \$column, \$match);
1527

1528 1529 1530 1531 1532 1533
	if($previous_line == $line && $previous_column == $column) {
	    $self->_parse_c_error($_, $line, $column, "statements", "no progress");
	}
	$previous_line = $line;
	$previous_column = $column;

1534 1535 1536
	# $output->write("'$match' '$_'\n");

	$statement .= $match;
1537
	$column++;
1538 1539 1540 1541 1542 1543
	if(s/^[\(\[]//) {
	    $plevel++;
	    $statement .= $&;
	} elsif(s/^[\)\]]//) {
	    $plevel--;
	    if($plevel <= 0) {
1544
		$self->_parse_c_error($_, $line, $column, "statements");
1545 1546 1547 1548 1549 1550 1551 1552 1553
	    }
	    $statement .= $&;
	} elsif(s/^\{//) {
	    $blevel++;
	    $statement .= $&;
	} elsif(s/^\}//) {
	    $blevel--;
	    $statement .= $&;
	    if($blevel == 1) {
1554
		if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1555 1556
		    return 0;
		}
1557
		$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1558 1559 1560 1561 1562 1563
		$statement = "";
		$statement_line = $line;
		$statement_column = $column;
	    }
	} elsif(s/^;//) {
	    if($plevel == 1 && $blevel == 1) {
1564
		if(!$self->parse_c_statement(\$statement, \$statement_line, \$statement_column)) {
1565 1566 1567
		    return 0;
		}

1568
		$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
		$statement = "";
		$statement_line = $line;
		$statement_column = $column;
	    } else {
		$statement .= $&;
	    }
	} elsif(/^\s*$/ && $statement =~ /^\s*$/ && $match =~ /^\s*$/) {
	    $plevel = 0;
	    $blevel = 0;
	} else {
1579
	    $self->_parse_c_error($_, $line, $column, "statements");
1580 1581 1582
	}
    }

1583
    $self->_update_c_position($_, \$line, \$column);
1584 1585 1586 1587 1588 1589 1590 1591

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

    return 1;
}

1592 1593 1594
########################################################################
# parse_c_struct_union

1595
sub parse_c_struct_union($$$$$$$$$) {
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
    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);

1620
    if (!s/^(interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
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 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
	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;
}

1690 1691 1692
########################################################################
# parse_c_tuple

1693
sub parse_c_tuple($$$$$$$) {
1694 1695
    my $self = shift;

1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
    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;
1724
	$self->_parse_c_until_one_of("\\(,\\)", \$_, \$line, \$column, \$match);
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746

	$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;
1747
		$self->_parse_c_until_one_of("\\S", \$_, \$line, \$column);
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
		$item_line = $line;
		$item_column = $column + 1;
		$item = "";
	    } else {
		$item .= ",";
	    }
	} else {
	    return 0;
	}
    }

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

    return 1;
}

1766 1767 1768
########################################################################
# parse_c_type

1769
sub parse_c_type($$$$$) {
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
    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;

1784
    $self->_parse_c("(?:const|volatile)", \$_, \$line, \$column);
1785 1786 1787 1788

    if(0) {
	# Nothing
    } elsif($self->_parse_c('ICOM_VTABLE\(.*?\)', \$_, \$line, \$column, \$type)) {
1789
	# Nothing
1790
    } elsif($self->_parse_c('(?:enum\s+|interface\s+|struct\s+|union\s+)?(?:(?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)\s*(\*\s*)*',
1791 1792
			    \$_, \$line, \$column, \$type))
    {
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
	# Nothing
    } else {
	return 0;
    }
    $type =~ s/\s//g;

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

    $$reftype = $type;

    return 1;
}

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

1811
sub parse_c_typedef($$$$) {
1812 1813
    my $self = shift;

1814 1815
    my $create_type = \${$self->{CREATE_TYPE}};
    my $found_type = \${$self->{FOUND_TYPE}};
1816
    my $preprocessor_condition = \${$self->{PREPROCESSOR_CONDITION}};
1817

1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
    my $refcurrent = shift;
    my $refline = shift;
    my $refcolumn = shift;

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

    my $type;

1828 1829 1830
    if (!$self->_parse_c("typedef", \$_, \$line, \$column)) {
	return 0;
    }
1831

1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
    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;
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
        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);
1861 1862 1863 1864 1865 1866 1867 1868 1869
	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]);
1870

1871 1872 1873 1874
		&$$found_type($type);
	    } elsif ($name =~ /^(\*+)\s*(?:RESTRICTED_POINTER\s+)?(\w+)$/) {
		my $type_name = "$base_name $1";
		$name = $2;
1875 1876 1877 1878 1879 1880 1881 1882

		my $type = &$$create_type();

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

1883 1884 1885
		&$$found_type($type);		
	    } else {
		$self->_parse_c_error($_, $line, $column, "typedef 2");
1886
	    }
1887 1888 1889 1890
	}
	
	$finished = 1;
    }
1891

1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
    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) {
1904
		$self->_parse_c_error($_, $line, $column, "typedef");
1905
	    }
1906

1907 1908 1909 1910 1911 1912 1913
	    $type->kind("");
	    $type->name($name);
	    $type->field_type_names([$type_name]);
	    $type->field_names([""]);
	    
	    &$$found_type($type);
	}
1914

1915 1916 1917
	if (0 && $_ && !/^,/) {
	    $self->_parse_c_error($_, $line, $column, "typedef");
	}   
1918
    } else {
1919
	$self->_parse_c_error($_, $line, $column, "typedef");
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
    }

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

    return 1;
}

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

1932
sub parse_c_variable($$$$$$$) {
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
    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 = "";
1955
    my $sign = "";
1956 1957 1958
    my $type = "";
    my $name = "";

1959 1960
    # $self->_parse_c_warning($_, $line, $column, "variable");

1961
    my $match;
1962
    while($self->_parse_c('(?:const|inline|extern(?:\s+\"C\")?|EXTERN_C|static|volatile|' .
1963 1964
			  '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)|' .
1965
			  'long(?=\s+double\b|\s+int\b|\s+long\b))(?=\b)',
1966
			  \$_, \$line, \$column, \$match))
1967
    {
1968
	if ($match =~ /^(?:extern|static)$/) {
1969
	    if (!$linkage) {
1970
		$linkage = $match;
1971 1972 1973
	    } else {
		$self->_parse_c_warning($_, $line, $column, "repeated linkage (ignored): $match");
	    }
1974
	} elsif ($match =~ /^(?:signed|unsigned)$/) {
1975 1976 1977 1978
	    if (!$sign) {
		$sign = "$match ";
	    } else {
		$self->_parse_c_warning($_, $line, $column, "repeated sign (ignored): $match");
1979 1980 1981 1982 1983 1984
	    }
	}
    }

    my $finished = 0;

1985 1986
    if($finished) {
	# Nothing
1987 1988
    } elsif(/^$/) {
	return 0;
1989
    } elsif (s/^(enum\s+|interface\s+|struct\s+|union\s+)((?:MSVCRT|WS)\(\s*\w+\s*\)|\w+)?\s*\{\s*//s) {
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
	my $kind = $1;
	my $_name = $2;
	$self->_update_c_position($&, \$line, \$column);

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

	$finished = 1;
2001
    } 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) {
2002
	$type = "$sign$1";
2003 2004
	$name = $2;

2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
	if (defined($3)) {
	    my $bits = $4;
	    local $_ = $3;
	    if (/^\[/) {
		$type .= $_;
	    } elsif (/^:/) {
		$type .= ":$bits";
	    } elsif (/^\{/) {
		# Nothing
	    }
	}

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

2019
	$finished = 1;
2020
    } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*)\s*:\s*(\d+)$//s) {
2021 2022 2023 2024
	$type = "$sign$1:$2";
	$name = "";
	$type = $self->_format_c_type($type);

2025
	$finished = 1;
2026
    } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+\b(?:\s*\*)*\s*\(\s*(?:$CALL_CONVENTION)?(?:\s*\*)*)\s*(\w+)\s*(\)\s*\(.*?\))$//s) {
2027
	$type = $self->_format_c_type("$sign$1$3");
2028 2029
	$name = $2;

2030 2031 2032
	$finished = 1;
    } elsif($self->_parse_c('DEFINE_GUID', \$_, \$line, \$column, \$match)) { # Windows specific
	$type = $match;
2033
	$finished = 1;
2034 2035
    } else {
	$self->_parse_c_warning($_, $line, $column, "variable", "'$_'");
2036
	$finished = 1;
2037 2038
    }

2039 2040 2041 2042 2043 2044 2045
    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))
2046
    {
2047 2048 2049 2050 2051
	$type = $match;
	$finished = 1;
    } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(\w+\)', \$_, \$line, \$column, \$match)) {
	$type = $match;
	$finished = 1;
2052
    } elsif(s/^(enum|interface|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*//s) {
2053 2054
	my $kind = $1;
	my $_name = $2;
2055 2056
	$self->_update_c_position($&, \$line, \$column);

2057 2058
	if(defined($_name)) {
	    $type = "struct $_name { }";
2059 2060 2061
	} else {
	    $type = "struct { }";
	}
2062
    } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+)\s*(?:\*\s*)*//s) {
2063 2064 2065 2066 2067 2068
	$type = $&;
	$type =~ s/\s//g;
    } else {
	return 0;
    }

2069 2070 2071
    # $output->write("*** $type: '$_'\n");

    # $self->_parse_c_warning($_, $line, $column, "variable2", "");
2072 2073 2074 2075 2076 2077 2078 2079 2080

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

    if($finished) {
	# Nothing
2081
    } elsif(s/^(\((?:$CALL_CONVENTION)?\s*\*?\s*(?:$CALL_CONVENTION)?\w+\s*(?:\[[^\]]*\]\s*)*\))\s*\(//) {
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
	$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;
	}
2094
    } elsif(s/^(?:\*\s*)*(?:const\s+|volatile\s+)?(\w+)\s*(?:\[[^\]]*\]\s*)*\s*(?:=\s*|,\s*|$)//) {
2095 2096 2097 2098 2099 2100 2101 2102 2103
	$self->_update_c_position($&, \$line, \$column);

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

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

2107
    if(1 || $finished) {
2108 2109 2110 2111
	# Nothing
    } elsif($self->_parse_c('(?:struct\s+)?ICOM_VTABLE\s*\(.*?\)', \$_, \$line, \$column, \$match)) {
	$type = "<type>";
	$name = "<name>";
2112
    } elsif(s/^((?:enum\s+|interface\s+|struct\s+|union\s+)?\w+)\s*
2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
		(?:\*\s*)*(\w+|\s*\*?\s*\w+\s*\))\s*(?:\[[^\]]*\]|\([^\)]*\))?
		(?:,\s*(?:\*\s*)*(\w+)\s*(?:\[[^\]]*\])?)*
	    \s*(?:=|$)//sx)
    {
	$self->_update_c_position($&, \$line, \$column);

	$type = $1;
	$name = $2;

	$type =~ s/\s//g;
	$type =~ s/^struct/struct /;
2124
    } elsif(/^(enum|interface|struct|union)(?:\s+(\w+))?\s*\{.*?\}\s*((?:\*\s*)*)(\w+)\s*(?:=|$)/s) {
2125
	$self->_update_c_position($&, \$line, \$column);
2126

2127 2128 2129 2130 2131 2132 2133
	my $kind = $1;
	my $_name= $2;
	my $stars = $3;
	$name = $4;

	if(defined($_name)) {
	    $type = "struct $_name { }";
2134 2135 2136
	} else {
	    $type = "struct { }";
	}
2137

2138 2139 2140 2141 2142 2143 2144 2145
	$stars =~ s/\s//g;
	if($stars) {
	    $type .= " $type";
	}
    } else {
	return 0;
    }

2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
    $$refcurrent = $_;
    $$refline = $line;
    $$refcolumn = $column;

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

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

    return 1;
}

2162
1;