winapi.pm 28.4 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 winapi;

use strict;

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

@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw($win16api $win32api @winapis);

use vars qw($win16api $win32api @winapis);
31

32
use config qw($current_dir $wine_dir $winapi_dir);
33 34 35
use options qw($options);
use output qw($output);

36
use vars qw($modules);
37

38 39 40 41 42 43 44 45 46
sub found_shared_internal_function($$);
sub function_external_calling_convention_in_module($$$);
sub function_internal_module($$);
sub is_function_stub_in_module($$$);
sub new($$$);
sub parse_api_file($$);
sub parse_spec_file($$);

sub import(@) {
47
    $Exporter::ExportLevel++;
48
    Exporter::import(@_);
49
    $Exporter::ExportLevel--;
Patrik Stridvall's avatar
Patrik Stridvall committed
50

Patrik Stridvall's avatar
Patrik Stridvall committed
51 52 53 54
    if (defined($modules) && defined($win16api) && defined($win32api)) {
	return;
    }

55 56
    require modules;
    import modules qw($modules);
57

58 59
    my @spec_files16 = $modules->allowed_spec_files16;
    $win16api = 'winapi'->new("win16", \@spec_files16);
60

61 62
    my @spec_files32 = $modules->allowed_spec_files32;
    $win32api = 'winapi'->new("win32", \@spec_files32);
63

64 65 66 67 68 69 70 71 72 73 74 75
    @winapis = ($win16api, $win32api);

    for my $internal_name ($win32api->all_internal_functions) {
	my $module16 = $win16api->function_internal_module($internal_name);
	my $module32 = $win16api->function_internal_module($internal_name);
	if(defined($module16) &&
	   !$win16api->is_function_stub_in_module($module16, $internal_name) &&
	   !$win32api->is_function_stub_in_module($module32, $internal_name))
	{
	    $win16api->found_shared_internal_function($internal_name);
	    $win32api->found_shared_internal_function($internal_name);
	}
Patrik Stridvall's avatar
Patrik Stridvall committed
76
    }
77 78
}

79

80
sub new($$$) {
81 82 83 84 85
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self  = {};
    bless ($self, $class);

86
    my $name = \${$self->{NAME}};
Patrik Stridvall's avatar
Patrik Stridvall committed
87 88 89
    my $function_forward = \%{$self->{FUNCTION_FORWARD}};
    my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
    my $function_module = \%{$self->{FUNCTION_MODULE}};
90 91

    $$name = shift;
Patrik Stridvall's avatar
Patrik Stridvall committed
92 93 94 95 96
    my $refspec_files = shift;

    foreach my $file (@$refspec_files) {
	$self->parse_spec_file("$wine_dir/$file");
    }
97

98
    $self->parse_api_file("$$name.api");
99

100 101 102 103 104 105 106
    foreach my $module (sort(keys(%$function_forward))) {
	foreach my $external_name (sort(keys(%{$$function_forward{$module}}))) {
	    (my $forward_module, my $forward_external_name) = @{$$function_forward{$module}{$external_name}};
	    my $forward_internal_name = $$function_internal_name{$forward_external_name};
	    if(defined($forward_internal_name)) {
		$$function_module{$forward_internal_name} .= " & $module";
	    }
Patrik Stridvall's avatar
Patrik Stridvall committed
107 108
	}
    }
109 110 111 112

    return $self;
}

113
sub win16api() {
114 115 116
    return $win16api;
}

117
sub win32api() {
118 119 120
    return $win32api;
}

121
sub parse_api_file($$) {
122
    my $self = shift;
Patrik Stridvall's avatar
Patrik Stridvall committed
123

124 125 126
    my $allowed_kind = \%{$self->{ALLOWED_KIND}};
    my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
    my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
127
    my $allowed_modules_unlimited = \%{$self->{ALLOWED_MODULES_UNLIMITED}};
128
    my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
129
    my $type_format = \%{$self->{TYPE_FORMAT}};
130 131 132

    my $file = shift;

133
    my $module;
134
    my $kind;
135
    my $format;
136 137
    my $forbidden = 0;

138
    $output->lazy_progress("$file");
139

140
    open(IN, "< $winapi_dir/$file") || die "$winapi_dir/$file: $!\n";
141
    $/ = "\n";
142
    my $linenum=0;
143
    while(<IN>) {
144
        $linenum++;
145 146 147 148
	s/^\s*?(.*?)\s*$/$1/; # remove whitespace at begin and end of line
	s/^(.*?)\s*#.*$/$1/;  # remove comments
	/^$/ && next;         # skip empty lines

149 150 151 152 153 154
	if(/^%%(\S+)$/) {
	    $module = $1;
	    $module =~ s/\.dll$//; # FIXME: Kludge
	} elsif(!$modules->is_allowed_module($module)) {
	    # Nothing
	} elsif(s/^%(\S+)\s*//) {
155
	    $kind = $1;
156
	    $format = undef;
157 158 159
	    $forbidden = 0;

	    $$allowed_kind{$kind} = 1;
160
	    if(/^--forbidden/) {
161
		$forbidden = 1;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	    } elsif(/^--format=(\".*?\"|\S*)/) {
		$format = $1;
		$format =~ s/^\"(.*?)\"$/$1/;
	    }

	    if(!defined($format)) {
		if($kind eq "long") {
		    $format  = "%d|%u|%x|%X|";
		    $format .= "%hd|%hu|%hx|%hX|";
		    $format .= "%ld|%lu|%lx|%lX|";
		    $format .= "%04x|%04X|0x%04x|0x%04X|";
		    $format .= "%08x|%08X|0x%08x|0x%08X|";
                    $format .= "%08lx|%08lX|0x%08lx|0x%08lX";
		} elsif($kind eq "longlong") {
		    $format = "%lld";
		} elsif($kind eq "ptr") {
		    $format = "%p";
		} elsif($kind eq "segptr") {
		    $format = "%p";
		} elsif($kind eq "str") {
		    $format = "%p|%s";
		} elsif($kind eq "wstr") {
		    $format = "%p|%s";
		} elsif($kind eq "word") {
		    $format  = "%d|%u|%x|%X|";
		    $format .= "%hd|%hu|%hx|%hX|";
		    $format .= "%04x|%04X|0x%04x|0x%04X";
		} else {
		    $format = "<unknown>";
		}
192 193 194
	    }
	} elsif(defined($kind)) {
	    my $type = $_;
195
            if ($type =~ /\blong\b/)
196 197 198
            {
                $output->write("$file:$linenum: type ($type) is not Win64 compatible\n");
            }
199
	    if(!$forbidden) {
200 201
		if(defined($module)) {
		    if($$allowed_modules_unlimited{$type}) {
202
			$output->write("$file:$linenum: type ($type) already specified as an unlimited type\n");
203 204 205 206
		    } elsif(!$$allowed_modules{$type}{$module}) {
			$$allowed_modules{$type}{$module} = 1;
			$$allowed_modules_limited{$type} = 1;
		    } else {
207
			$output->write("$file:$linenum: type ($type) already specified\n");
208 209 210
		    }
		} else {
		    $$allowed_modules_unlimited{$type} = 1;
211 212 213 214
		}
	    } else {
		$$allowed_modules_limited{$type} = 1;
	    }
215
	    if(defined($$translate_argument{$type}) && $$translate_argument{$type} ne $kind) {
216
		$output->write("$file:$linenum: type ($type) respecified as different kind ($kind != $$translate_argument{$type})\n");
217 218 219
	    } else {
		$$translate_argument{$type} = $kind;
	    }
220

221
	    $$type_format{$module}{$type} = $format;
222
	} else {
223
	    $output->write("$file:$linenum: file must begin with %<type> statement\n");
224 225 226 227 228 229
	    exit 1;
	}
    }
    close(IN);
}

230
sub parse_spec_file($$) {
231
    my $self = shift;
232

233 234 235 236 237 238
    my $function_internal_arguments = \%{$self->{FUNCTION_INTERNAL_ARGUMENTS}};
    my $function_external_arguments = \%{$self->{FUNCTION_EXTERNAL_ARGUMENTS}};
    my $function_internal_ordinal = \%{$self->{FUNCTION_INTERNAL_ORDINAL}};
    my $function_external_ordinal = \%{$self->{FUNCTION_EXTERNAL_ORDINAL}};
    my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
    my $function_external_calling_convention = \%{$self->{FUNCTION_EXTERNAL_CALLING_CONVENTION}};
239
    my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
Patrik Stridvall's avatar
Patrik Stridvall committed
240
    my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
241
    my $function_forward = \%{$self->{FUNCTION_FORWARD}};
242 243
    my $function_internal_module = \%{$self->{FUNCTION_INTERNAL_MODULE}};
    my $function_external_module = \%{$self->{FUNCTION_EXTERNAL_MODULE}};
244
    my $function_wine_extension = \%{$self->{FUNCTION_WINE_EXTENSION}};
Patrik Stridvall's avatar
Patrik Stridvall committed
245
    my $modules = \%{$self->{MODULES}};
246
    my $module_files = \%{$self->{MODULE_FILES}};
247
    my $module_external_calling_convention = \%{$self->{MODULE_EXTERNAL_CALLING_CONVENTION}};
248 249

    my $file = shift;
250
    $file =~ s%^\./%%;
251 252

    my %ordinals;
253
    my $module;
254
    my $wine_extension = 0;
255

256
    $output->lazy_progress("$file");
257

258 259 260
    $module = $file;
    $module =~ s/^.*?([^\/]*)\.spec$/$1/;

261 262 263 264 265 266 267
    open(IN, "< $file") || die "$file: $!\n";
    $/ = "\n";
    my $header = 1;
    my $lookahead = 0;
    while($lookahead || defined($_ = <IN>)) {
	$lookahead = 0;
	s/^\s*(.*?)\s*$/$1/;
268 269
	if(s/^(.*?)\s*\#\s*(.*)\s*$/$1/) {
	    my $comment = $2;
Patrik Stridvall's avatar
Patrik Stridvall committed
270
	    if ($comment =~ /^Wine/i) { # FIXME: Kludge
271 272 273
		$wine_extension = 1;
	    }
	}
274 275 276
	/^$/ && next;

	if($header)  {
Patrik Stridvall's avatar
Patrik Stridvall committed
277
	    if(/^\d+|@/) { $header = 0; $lookahead = 1; }
278
	    next;
279
	}
280

281
	my $ordinal;
282
	my $ARCHES="arm|arm64|i386|powerpc|win32|win64|x86_64";
283
	if(/^(\d+|@)\s+
284
	   (cdecl|pascal|stdcall|varargs|thiscall)\s+
285
	   ((?:(?:-arch=(?:$ARCHES)(?:,(?:$ARCHES))*|-noname|-norelay|-ordinal|-i386|-ret16|-ret64|-register|-interrupt|-private)\s+)*)(\S+)\s*\(\s*(.*?)\s*\)\s*(\S*)$/x)
286
	{
287
	    my $calling_convention = $2;
Patrik Stridvall's avatar
Patrik Stridvall committed
288 289 290 291 292
	    my $flags = $3;
	    my $external_name = $4;
	    my $arguments = $5;
	    my $internal_name = $6;

293
	    $ordinal = $1;
294

Patrik Stridvall's avatar
Patrik Stridvall committed
295 296
	    $flags =~ s/\s+/ /g;

297 298 299 300
            if (!$internal_name)
            {
                $internal_name = ($flags =~ /-register/ ? "__regs_" : "") . $external_name;
            }
Patrik Stridvall's avatar
Patrik Stridvall committed
301

Patrik Stridvall's avatar
Patrik Stridvall committed
302 303 304 305
	    if($flags =~ /-noname/) {
		# $external_name = "@";
	    }

Patrik Stridvall's avatar
Patrik Stridvall committed
306 307 308
	    if($flags =~ /(?:-register|-interrupt)/) {
		if($arguments) { $arguments .= " "; }
		$arguments .= "ptr";
Patrik Stridvall's avatar
Patrik Stridvall committed
309
		$calling_convention .= " -register";
Patrik Stridvall's avatar
Patrik Stridvall committed
310
	    }
311 312 313
	    if($flags =~ /(?:-i386)/) {
		$calling_convention .= " -i386";
	    }
Patrik Stridvall's avatar
Patrik Stridvall committed
314

Patrik Stridvall's avatar
Patrik Stridvall committed
315
	    if ($internal_name =~ /^(.*?)\.(.*?)$/) {
Patrik Stridvall's avatar
Patrik Stridvall committed
316
		my $forward_module = lc($1);
Patrik Stridvall's avatar
Patrik Stridvall committed
317
		my $forward_name = $2;
318
		$calling_convention = "forward";
Patrik Stridvall's avatar
Patrik Stridvall committed
319 320 321
		$$function_forward{$module}{$external_name} = [$forward_module, $forward_name];
	    }

322 323 324 325 326
            if($external_name ne "@") {
                $$module_external_calling_convention{$module}{$external_name} = $calling_convention;
            } else {
                $$module_external_calling_convention{$module}{"\@$ordinal"} = $calling_convention;
            }
327 328 329 330 331 332 333 334 335 336
	    if(!$$function_internal_name{$external_name}) {
		$$function_internal_name{$external_name} = $internal_name;
	    } else {
		$$function_internal_name{$external_name} .= " & $internal_name";
	    }
	    if(!$$function_external_name{$internal_name}) {
		$$function_external_name{$internal_name} = $external_name;
	    } else {
		$$function_external_name{$internal_name} .= " & $external_name";
	    }
337 338
	    $$function_internal_arguments{$internal_name} = $arguments;
	    $$function_external_arguments{$external_name} = $arguments;
339 340 341 342 343 344 345 346 347 348
	    if(!$$function_internal_ordinal{$internal_name}) {
		$$function_internal_ordinal{$internal_name} = $ordinal;
	    } else {
		$$function_internal_ordinal{$internal_name} .= " & $ordinal";
	    }
	    if(!$$function_external_ordinal{$external_name}) {
		$$function_external_ordinal{$external_name} = $ordinal;
	    } else {
		$$function_external_ordinal{$external_name} .= " & $ordinal";
	    }
349 350 351 352
	    $$function_internal_calling_convention{$internal_name} = $calling_convention;
	    $$function_external_calling_convention{$external_name} = $calling_convention;
	    if(!$$function_internal_module{$internal_name}) {
		$$function_internal_module{$internal_name} = "$module";
353
	    } else {
354 355 356 357
		$$function_internal_module{$internal_name} .= " & $module";
	    }
	    if(!$$function_external_module{$external_name}) {
		$$function_external_module{$external_name} = "$module";
358
	    } else {
359
		$$function_external_module{$external_name} .= " & $module";
360
	    }
361
	    $$function_wine_extension{$module}{$external_name} = $wine_extension;
362

363
	    if(0 && $options->spec_mismatch) {
364 365
		if($external_name eq "@") {
		    if($internal_name !~ /^\U$module\E_$ordinal$/) {
366
			$output->write("$file: $external_name: the internal name ($internal_name) mismatch\n");
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
		    }
		} else {
		    my $name = $external_name;

		    my $name1 = $name;
		    $name1 =~ s/^Zw/Nt/;

		    my $name2 = $name;
		    $name2 =~ s/^(?:_|Rtl|k32|K32)//;

		    my $name3 = $name;
		    $name3 =~ s/^INT_Int[0-9a-f]{2}Handler$/BUILTIN_DefaultIntHandler/;

		    my $name4 = $name;
		    $name4 =~ s/^(VxDCall)\d$/$1/;

383
		    # FIXME: This special case is because of a very ugly kludge that should be fixed IMHO
384 385 386 387 388 389
		    my $name5 = $name;
		    $name5 =~ s/^(.*?16)_(.*?)$/$1_fn$2/;

		    if(uc($internal_name) ne uc($external_name) &&
		       $internal_name !~ /(\Q$name\E|\Q$name1\E|\Q$name2\E|\Q$name3\E|\Q$name4\E|\Q$name5\E)/)
		    {
390
			$output->write("$file: $external_name: internal name ($internal_name) mismatch\n");
391 392 393
		    }
		}
	    }
394
	} elsif(/^(\d+|@)\s+stub(?:\s+(-arch=(?:$ARCHES)(?:,(?:$ARCHES))*|-noname|-norelay|-ordinal|-i386|-ret16|-ret64|-private))*\s+(\S+?)\s*(\(\s*(.*?)\s*\))?$/) {
395 396
	    $ordinal = $1;

Patrik Stridvall's avatar
Patrik Stridvall committed
397 398 399 400 401 402 403 404 405
	    my $flags = $2;
	    my $external_name = $3;

	    $flags = "" if !defined($flags);

	    if($flags =~ /-noname/) {
		# $external_name = "@";
	    }

406
	    my $internal_name = $external_name;
407

408 409 410 411 412
            if ($external_name ne "@") {
                $$module_external_calling_convention{$module}{$external_name} = "stub";
            } else {
                $$module_external_calling_convention{$module}{"\@$ordinal"} = "stub";
            }
413 414 415 416 417 418 419 420 421 422
	    if(!$$function_internal_name{$external_name}) {
		$$function_internal_name{$external_name} = $internal_name;
	    } else {
		$$function_internal_name{$external_name} .= " & $internal_name";
	    }
	    if(!$$function_external_name{$internal_name}) {
		$$function_external_name{$internal_name} = $external_name;
	    } else {
		$$function_external_name{$internal_name} .= " & $external_name";
	    }
423 424 425 426 427 428 429 430 431 432
	    if(!$$function_internal_ordinal{$internal_name}) {
		$$function_internal_ordinal{$internal_name} = $ordinal;
	    } else {
		$$function_internal_ordinal{$internal_name} .= " & $ordinal";
	    }
	    if(!$$function_external_ordinal{$external_name}) {
		$$function_external_ordinal{$external_name} = $ordinal;
	    } else {
		$$function_external_ordinal{$external_name} .= " & $ordinal";
	    }
433 434
	    if(!$$function_internal_module{$internal_name}) {
		$$function_internal_module{$internal_name} = "$module";
435
	    } else { # if($$function_internal_module{$internal_name} !~ /$module/) {
436 437 438 439
		$$function_internal_module{$internal_name} .= " & $module";
	    }
	    if(!$$function_external_module{$external_name}) {
		$$function_external_module{$external_name} = "$module";
440
	    } else { # if($$function_external_module{$external_name} !~ /$module/) {
441
		$$function_external_module{$external_name} .= " & $module";
442
	    }
443
	} elsif(/^(\d+|@)\s+extern(?:\s+(?:-arch=(?:$ARCHES)(?:,(?:$ARCHES))*|-noname|-norelay|-ordinal|-i386|-ret16|-ret64))*\s+(\S+)\s*(\S*)$/) {
444 445 446 447 448
	    $ordinal = $1;

	    my $external_name = $2;
	    my $internal_name = $3;

Patrik Stridvall's avatar
Patrik Stridvall committed
449 450
	    $internal_name = $external_name if !$internal_name;

451 452 453 454 455
            if ($external_name ne "@") {
                $$module_external_calling_convention{$module}{$external_name} = "extern";
            } else {
                $$module_external_calling_convention{$module}{"\@$ordinal"} = "extern";
            }
456
	} elsif(/^(?:\d+|@)\s+(?:equate|variable)/) {
457 458 459
	    # ignore
	} else {
	    my $next_line = <IN>;
Patrik Stridvall's avatar
Patrik Stridvall committed
460
	    if(!defined($next_line) || $next_line =~ /^\s*\d|@/) {
461 462 463 464 465 466
		die "$file: $.: syntax error: '$_'\n";
	    } else {
		$_ .= $next_line;
		$lookahead = 1;
	    }
	}
467

468
	if(defined($ordinal)) {
469
	    if($ordinal ne "@" && $ordinals{$ordinal}) {
470
		$output->write("$file: ordinal redefined: $_\n");
471 472 473
	    }
	    $ordinals{$ordinal}++;
	}
474 475
    }
    close(IN);
Patrik Stridvall's avatar
Patrik Stridvall committed
476 477

    $$modules{$module}++;
478 479

    $$module_files{$module} = $file;
480 481
}

482
sub name($) {
483
    my $self = shift;
484 485 486
    my $name = \${$self->{NAME}};

    return $$name;
487 488
}

489
sub is_allowed_kind($$) {
490 491 492 493 494 495 496 497 498
    my $self = shift;
    my $allowed_kind = \%{$self->{ALLOWED_KIND}};

    my $kind = shift;
    if(defined($kind)) {
	return $$allowed_kind{$kind};
    } else {
	return 0;
    }
Patrik Stridvall's avatar
Patrik Stridvall committed
499 500 501

}

502
sub allow_kind($$) {
Patrik Stridvall's avatar
Patrik Stridvall committed
503 504 505 506 507 508
    my $self = shift;
    my $allowed_kind = \%{$self->{ALLOWED_KIND}};

    my $kind = shift;

    $$allowed_kind{$kind}++;
509 510
}

511
sub is_limited_type($$) {
512 513 514 515 516 517 518 519
    my $self = shift;
    my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};

    my $type = shift;

    return $$allowed_modules_limited{$type};
}

520
sub is_allowed_type_in_module($$) {
521 522 523 524 525
    my $self = shift;
    my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
    my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};

    my $type = shift;
Patrik Stridvall's avatar
Patrik Stridvall committed
526 527 528
    my @modules = split(/ \& /, shift);

    if(!$$allowed_modules_limited{$type}) { return 1; }
529

Patrik Stridvall's avatar
Patrik Stridvall committed
530 531 532 533 534
    foreach my $module (@modules) {
	if($$allowed_modules{$type}{$module}) { return 1; }
    }

    return 0;
535 536
}

537
sub allow_type_in_module($$) {
Patrik Stridvall's avatar
Patrik Stridvall committed
538 539 540 541 542 543 544 545 546 547 548
    my $self = shift;
    my $allowed_modules = \%{$self->{ALLOWED_MODULES}};

    my $type = shift;
    my @modules = split(/ \& /, shift);

    foreach my $module (@modules) {
	$$allowed_modules{$type}{$module}++;
    }
}

549
sub type_used_in_module($$) {
550 551 552 553
    my $self = shift;
    my $used_modules = \%{$self->{USED_MODULES}};

    my $type = shift;
Patrik Stridvall's avatar
Patrik Stridvall committed
554 555 556 557 558
    my @modules = split(/ \& /, shift);

    foreach my $module (@modules) {
	$$used_modules{$type}{$module} = 1;
    }
559 560 561 562

    return ();
}

563
sub types_not_used($) {
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    my $self = shift;
    my $used_modules = \%{$self->{USED_MODULES}};
    my $allowed_modules = \%{$self->{ALLOWED_MODULES}};

    my $not_used;
    foreach my $type (sort(keys(%$allowed_modules))) {
	foreach my $module (sort(keys(%{$$allowed_modules{$type}}))) {
	    if(!$$used_modules{$type}{$module}) {
		$$not_used{$module}{$type} = 1;
	    }
	}
    }
    return $not_used;
}

579
sub types_unlimited_used_in_modules($) {
Patrik Stridvall's avatar
Patrik Stridvall committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593
    my $self = shift;

    my $used_modules = \%{$self->{USED_MODULES}};
    my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
    my $allowed_modules_unlimited = \%{$self->{ALLOWED_MODULES_UNLIMITED}};

    my $used_types;
    foreach my $type (sort(keys(%$allowed_modules_unlimited))) {
	my $count = 0;
	my @modules = ();
	foreach my $module (sort(keys(%{$$used_modules{$type}}))) {
	    $count++;
	    push @modules, $module;
	}
594
        if($count) {
Patrik Stridvall's avatar
Patrik Stridvall committed
595 596 597 598 599 600 601 602
	    foreach my $module (@modules) {
	      $$used_types{$type}{$module} = 1;
	    }
	}
    }
    return $used_types;
}

603
sub translate_argument($$) {
604 605 606
    my $self = shift;
    my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};

Patrik Stridvall's avatar
Patrik Stridvall committed
607 608 609 610 611
    my $type = shift;

    return $$translate_argument{$type};
}

612
sub declare_argument($$$) {
Patrik Stridvall's avatar
Patrik Stridvall committed
613 614 615 616 617
    my $self = shift;
    my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};

    my $type = shift;
    my $kind = shift;
618

Patrik Stridvall's avatar
Patrik Stridvall committed
619
    $$translate_argument{$type} = $kind;
620 621
}

622
sub all_declared_types($) {
623 624 625 626 627 628
    my $self = shift;
    my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};

    return sort(keys(%$translate_argument));
}

629
sub is_allowed_type_format($$$$) {
630 631 632 633 634 635 636 637 638 639 640 641
    my $self = shift;
    my $type_format = \%{$self->{TYPE_FORMAT}};

    my $module = shift;
    my $type = shift;
    my $format = shift;

    my $formats;

    if(defined($module) && defined($type)) {
	local $_;
	foreach (split(/ & /, $module)) {
642 643
	    if(defined($formats)) {
		$formats .= "|";
644 645
	    } else {
		$formats = "";
646
	    }
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
	    if(defined($$type_format{$_}{$type})) {
		$formats .= $$type_format{$_}{$type};
	    }
	}
    }

    if(defined($formats)) {
	local $_;
	foreach (split(/\|/, $formats)) {
	    if($_ eq $format) {
		return 1;
	    }
	}
    }

    return 0;
}

665
sub all_modules($) {
Patrik Stridvall's avatar
Patrik Stridvall committed
666 667 668 669 670 671
    my $self = shift;
    my $modules = \%{$self->{MODULES}};

    return sort(keys(%$modules));
}

672
sub is_module($$) {
Patrik Stridvall's avatar
Patrik Stridvall committed
673 674 675 676 677 678 679 680
    my $self = shift;
    my $modules = \%{$self->{MODULES}};

    my $name = shift;

    return $$modules{$name};
}

681
sub module_file($$) {
682 683 684 685 686 687 688 689 690
    my $self = shift;

    my $module = shift;

    my $module_files = \%{$self->{MODULE_FILES}};

    return $$module_files{$module};
}

691
sub all_internal_functions($) {
692
    my $self = shift;
693
    my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
694

695
    return sort(keys(%$function_internal_calling_convention));
696 697
}

698
sub all_internal_functions_in_module($$) {
Patrik Stridvall's avatar
Patrik Stridvall committed
699
    my $self = shift;
700 701
    my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
    my $function_internal_module = \%{$self->{FUNCTION_INTERNAL_MODULE}};
Patrik Stridvall's avatar
Patrik Stridvall committed
702 703 704 705

    my $module = shift;

    my @names;
706 707
    foreach my $name (keys(%$function_internal_calling_convention)) {
	if($$function_internal_module{$name} eq $module) {
Patrik Stridvall's avatar
Patrik Stridvall committed
708 709 710 711 712 713 714
	    push @names, $name;
	}
    }

    return sort(@names);
}

715
sub all_external_functions($) {
716
    my $self = shift;
717
    my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
718

719
    return sort(keys(%$function_external_name));
720 721
}

722
sub all_external_functions_in_module($$) {
723
    my $self = shift;
724
    my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
725 726 727 728 729
    my $function_external_module = \%{$self->{FUNCTION_EXTERNAL_MODULE}};

    my $module = shift;

    my @names;
730
    foreach my $name (keys(%$function_external_name)) {
731 732 733 734 735 736 737 738
	if($$function_external_module{$name} eq $module) {
	    push @names, $name;
	}
    }

    return sort(@names);
}

739
sub all_functions_in_module($$) {
740
    my $self = shift;
741
    my $module_external_calling_convention = \%{$self->{MODULE_EXTERNAL_CALLING_CONVENTION}};
742

743 744 745
    my $module = shift;

    return sort(keys(%{$$module_external_calling_convention{$module}}));
746 747
}

748
sub all_broken_forwards($) {
749
    my $self = shift;
750
    my $function_forward = \%{$self->{FUNCTION_FORWARD}};
751

752 753 754 755
    my @broken_forwards = ();
    foreach my $module (sort(keys(%$function_forward))) {
	foreach my $external_name (sort(keys(%{$$function_forward{$module}}))) {
	    (my $forward_module, my $forward_external_name) = @{$$function_forward{$module}{$external_name}};
756

757 758 759 760 761 762 763 764 765
	    my $forward_external_calling_convention =
		$self->function_external_calling_convention_in_module($forward_module, $forward_external_name);

	    if(!defined($forward_external_calling_convention)) {
		push @broken_forwards, [$module, $external_name, $forward_module, $forward_external_name];
	    }
	}
    }
    return @broken_forwards;
766 767
}

768

769
sub function_internal_ordinal($$) {
770 771 772 773 774 775 776 777
    my $self = shift;
    my $function_internal_ordinal = \%{$self->{FUNCTION_INTERNAL_ORDINAL}};

    my $name = shift;

    return $$function_internal_ordinal{$name};
}

778
sub function_external_ordinal($$) {
779 780 781 782 783 784 785 786
    my $self = shift;
    my $function_external_ordinal = \%{$self->{FUNCTION_EXTERNAL_ORDINAL}};

    my $name = shift;

    return $$function_external_ordinal{$name};
}

787
sub function_internal_calling_convention($$) {
788
    my $self = shift;
789
    my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
790 791 792

    my $name = shift;

793
    return $$function_internal_calling_convention{$name};
794 795
}

796
sub function_external_calling_convention($$) {
797
    my $self = shift;
798
    my $function_external_calling_convention = \%{$self->{FUNCTION_EXTERNAL_CALLING_CONVENTION}};
799 800 801

    my $name = shift;

802
    return $$function_external_calling_convention{$name};
803 804
}

805
sub function_external_calling_convention_in_module($$$) {
806 807 808 809 810 811 812 813 814
    my $self = shift;
    my $module_external_calling_convention = \%{$self->{MODULE_EXTERNAL_CALLING_CONVENTION}};

    my $module = shift;
    my $name = shift;

    return $$module_external_calling_convention{$module}{$name};
}

815
sub function_internal_name($$) {
816 817 818 819 820 821 822 823
    my $self = shift;
    my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};

    my $name = shift;

    return $$function_internal_name{$name};
}

824
sub function_external_name($$) {
Patrik Stridvall's avatar
Patrik Stridvall committed
825 826 827 828 829 830 831 832
    my $self = shift;
    my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};

    my $name = shift;

    return $$function_external_name{$name};
}

833
sub function_forward_final_destination($$$) {
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
    my $self = shift;

    my $function_forward = \%{$self->{FUNCTION_FORWARD}};

    my $module = shift;
    my $name = shift;

    my $forward_module = $module;
    my $forward_name = $name;
    while(defined(my $forward = $$function_forward{$forward_module}{$forward_name})) {
	($forward_module, $forward_name) = @$forward;
    }

    return ($forward_module, $forward_name);
}

850
sub is_function($$) {
851
    my $self = shift;
852
    my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
853 854 855

    my $name = shift;

856
    return $$function_internal_calling_convention{$name};
857 858
}

859
sub all_shared_internal_functions($$) {
860 861 862 863 864 865
    my $self = shift;
    my $function_shared = \%{$self->{FUNCTION_SHARED}};

    return sort(keys(%$function_shared));
}

866
sub is_shared_internal_function($$) {
867 868 869 870 871 872 873 874
    my $self = shift;
    my $function_shared = \%{$self->{FUNCTION_SHARED}};

    my $name = shift;

    return $$function_shared{$name};
}

875
sub found_shared_internal_function($$) {
876 877 878 879 880 881 882 883
    my $self = shift;
    my $function_shared = \%{$self->{FUNCTION_SHARED}};

    my $name = shift;

    $$function_shared{$name} = 1;
}

884
sub function_internal_arguments($$) {
885 886 887 888 889 890 891 892
    my $self = shift;
    my $function_internal_arguments = \%{$self->{FUNCTION_INTERNAL_ARGUMENTS}};

    my $name = shift;

    return $$function_internal_arguments{$name};
}

893
sub function_external_arguments($$) {
894 895 896 897 898 899 900 901
    my $self = shift;
    my $function_external_arguments = \%{$self->{FUNCTION_EXTERNAL_ARGUMENTS}};

    my $name = shift;

    return $$function_external_arguments{$name};
}

902
sub function_internal_module($$) {
903
    my $self = shift;
904
    my $function_internal_module = \%{$self->{FUNCTION_INTERNAL_MODULE}};
905 906 907

    my $name = shift;

908
    return $$function_internal_module{$name};
909 910
}

911
sub function_external_module($$) {
912
    my $self = shift;
913
    my $function_external_module = \%{$self->{FUNCTION_EXTERNAL_MODULE}};
914 915 916

    my $name = shift;

917
    return $$function_external_module{$name};
918 919
}

920
sub function_wine_extension($$$) {
921 922 923 924 925 926 927 928 929
    my $self = shift;
    my $function_wine_extension = \%{$self->{FUNCTION_WINE_EXTENSION}};

    my $module = shift;
    my $name = shift;

    return $$function_wine_extension{$module}{$name};
}

930
sub is_function_stub($$$) {
931
    my $self = shift;
932
    my $module_external_calling_convention = \%{$self->{MODULE_EXTERNAL_CALLING_CONVENTION}};
933 934 935 936

    my $module = shift;
    my $name = shift;

Patrik Stridvall's avatar
Patrik Stridvall committed
937 938
    if($$module_external_calling_convention{$module}{$name} eq "stub") {
	return 1;
939 940 941 942
    }
    return 0;
}

943
sub is_function_stub_in_module($$$) {
944
    my $self = shift;
945
    my $module_external_calling_convention = \%{$self->{MODULE_EXTERNAL_CALLING_CONVENTION}};
946

947
    my $module = shift;
948 949
    my $name = shift;

950 951 952 953
    if(!defined($$module_external_calling_convention{$module}{$name})) {
	return 0;
    }
    return $$module_external_calling_convention{$module}{$name} eq "stub";
954 955
}

956 957 958 959
########################################################################
# class methods
#

960
sub _get_all_module_internal_ordinal($$) {
961
    my $winapi = shift;
962 963 964
    my $internal_name = shift;

    my @entries = ();
965 966 967 968 969

    my @name = (); {
	my $name = $winapi->function_external_name($internal_name);
	if(defined($name)) {
	    @name = split(/ & /, $name);
970
	}
971 972 973 974 975 976
    }

    my @module = (); {
	my $module = $winapi->function_internal_module($internal_name);
	if(defined($module)) {
	    @module = split(/ & /, $module);
977
	}
978
    }
979

980 981 982 983
    my @ordinal = (); {
	my $ordinal = $winapi->function_internal_ordinal($internal_name);
	if(defined($ordinal)) {
	    @ordinal = split(/ & /, $ordinal);
984 985 986
	}
    }

987 988 989 990 991
    my $name;
    my $module;
    my $ordinal;
    while(defined($name = shift @name) &&
	  defined($module = shift @module) &&
992
	  defined($ordinal = shift @ordinal))
993 994 995 996
    {
	push @entries, [$name, $module, $ordinal];
    }

997 998 999
    return @entries;
}

1000 1001
sub get_all_module_internal_ordinal16($) {
    return _get_all_module_internal_ordinal($win16api, $_[0]);
1002
}
1003

1004 1005
sub get_all_module_internal_ordinal32($) {
    return _get_all_module_internal_ordinal($win32api, $_[0]);
1006 1007
}

1008
sub get_all_module_internal_ordinal($) {
1009 1010
    my @entries = ();
    foreach my $winapi (@winapis) {
1011
	push @entries, _get_all_module_internal_ordinal($winapi, $_[0]);
1012 1013 1014 1015 1016
    }

    return @entries;
}

1017
sub _get_all_module_external_ordinal($$) {
1018 1019 1020 1021 1022 1023 1024 1025 1026
    my $winapi = shift;
    my $external_name = shift;

    my @entries = ();

    my @name = (); {
	my $name = $winapi->function_internal_name($external_name);
	if(defined($name)) {
	    @name = split(/ & /, $name);
1027
	}
1028 1029 1030 1031 1032 1033
    }

    my @module = (); {
	my $module = $winapi->function_external_module($external_name);
	if(defined($module)) {
	    @module = split(/ & /, $module);
1034
	}
1035
    }
1036

1037 1038 1039 1040
    my @ordinal = (); {
	my $ordinal = $winapi->function_external_ordinal($external_name);
	if(defined($ordinal)) {
	    @ordinal = split(/ & /, $ordinal);
1041 1042
	}
    }
1043

1044 1045 1046 1047 1048
    my $name;
    my $module;
    my $ordinal;
    while(defined($name = shift @name) &&
	  defined($module = shift @module) &&
1049
	  defined($ordinal = shift @ordinal))
1050 1051 1052
    {
	push @entries, [$name, $module, $ordinal];
    }
1053

1054 1055 1056
    return @entries;
}

1057 1058
sub get_all_module_external_ordinal16($) {
    return _get_all_module_external_ordinal($win16api, $_[0]);
1059 1060
}

1061 1062
sub get_all_module_external_ordinal32($) {
    return _get_all_module_external_ordinal($win32api, $_[0]);
1063 1064
}

1065
sub get_all_module_external_ordinal($) {
1066 1067
    my @entries = ();
    foreach my $winapi (@winapis) {
1068
	push @entries, _get_all_module_external_ordinal($winapi, $_[0]);
1069
    }
1070 1071 1072 1073

    return @entries;
}

1074
1;