winapi.pm 27.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# 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
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

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
sub import {
    $Exporter::ExportLevel++;
    &Exporter::import(@_);
    $Exporter::ExportLevel--;
Patrik Stridvall's avatar
Patrik Stridvall committed
42

Patrik Stridvall's avatar
Patrik Stridvall committed
43 44 45 46
    if (defined($modules) && defined($win16api) && defined($win32api)) {
	return;
    }

47 48
    require modules;
    import modules qw($modules);
49

50 51
    my @spec_files16 = $modules->allowed_spec_files16;
    $win16api = 'winapi'->new("win16", \@spec_files16);
52

53 54
    my @spec_files32 = $modules->allowed_spec_files32;
    $win32api = 'winapi'->new("win32", \@spec_files32);
55

56 57 58 59 60 61 62 63 64 65 66 67
    @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
68
    }
69 70
}

71

72 73 74 75 76 77
sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self  = {};
    bless ($self, $class);

78
    my $name = \${$self->{NAME}};
Patrik Stridvall's avatar
Patrik Stridvall committed
79 80 81
    my $function_forward = \%{$self->{FUNCTION_FORWARD}};
    my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
    my $function_module = \%{$self->{FUNCTION_MODULE}};
82 83

    $$name = shift;
Patrik Stridvall's avatar
Patrik Stridvall committed
84 85 86 87 88
    my $refspec_files = shift;

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

90
    $self->parse_api_file("$$name.api");
91

92 93 94 95 96 97 98
    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
99 100
	}
    }
101 102 103 104

    return $self;
}

105 106 107 108 109 110 111 112
sub win16api {
    return $win16api;
}

sub win32api {
    return $win32api;
}

113 114
sub parse_api_file {
    my $self = shift;
Patrik Stridvall's avatar
Patrik Stridvall committed
115

116 117 118
    my $allowed_kind = \%{$self->{ALLOWED_KIND}};
    my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
    my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
119
    my $allowed_modules_unlimited = \%{$self->{ALLOWED_MODULES_UNLIMITED}};
120
    my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
121
    my $type_format = \%{$self->{TYPE_FORMAT}};
122 123 124

    my $file = shift;

125
    my $module;
126
    my $kind;
127
    my $format;
128
    my $extension = 0;
129 130
    my $forbidden = 0;

131
    $output->lazy_progress("$file");
132

133
    open(IN, "< $winapi_dir/$file") || die "$winapi_dir/$file: $!\n";
134 135 136 137 138 139
    $/ = "\n";
    while(<IN>) {
	s/^\s*?(.*?)\s*$/$1/; # remove whitespace at begin and end of line
	s/^(.*?)\s*#.*$/$1/;  # remove comments
	/^$/ && next;         # skip empty lines

140 141 142 143 144 145
	if(/^%%(\S+)$/) {
	    $module = $1;
	    $module =~ s/\.dll$//; # FIXME: Kludge
	} elsif(!$modules->is_allowed_module($module)) {
	    # Nothing
	} elsif(s/^%(\S+)\s*//) {
146
	    $kind = $1;
147
	    $format = undef;
148
	    $forbidden = 0;
149
	    $extension = 0;
150 151

	    $$allowed_kind{$kind} = 1;
152
	    if(/^--forbidden/) {
153
		$forbidden = 1;
154 155
	    } elsif(/^--extension/) {
		$extension = 1;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	    } 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>";
		}
186 187 188 189
	    }
	} elsif(defined($kind)) {
	    my $type = $_;
	    if(!$forbidden) {
190 191
		if(defined($module)) {
		    if($$allowed_modules_unlimited{$type}) {
192
			$output->write("$file: type ($type) already specificed as an unlimited type\n");
193 194 195 196
		    } elsif(!$$allowed_modules{$type}{$module}) {
			$$allowed_modules{$type}{$module} = 1;
			$$allowed_modules_limited{$type} = 1;
		    } else {
197
			$output->write("$file: type ($type) already specificed\n");
198 199 200
		    }
		} else {
		    $$allowed_modules_unlimited{$type} = 1;
201 202 203 204
		}
	    } else {
		$$allowed_modules_limited{$type} = 1;
	    }
205
	    if(defined($$translate_argument{$type}) && $$translate_argument{$type} ne $kind) {
206
		$output->write("$file: type ($type) respecified as different kind ($kind != $$translate_argument{$type})\n");
207 208 209
	    } else {
		$$translate_argument{$type} = $kind;
	    }
210

211
	    $$type_format{$module}{$type} = $format;
212
	} else {
213
	    $output->write("$file: file must begin with %<type> statement\n");
214 215 216 217 218 219 220 221
	    exit 1;
	}
    }
    close(IN);
}

sub parse_spec_file {
    my $self = shift;
222

223 224 225 226 227 228
    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}};
229
    my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
Patrik Stridvall's avatar
Patrik Stridvall committed
230
    my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
231
    my $function_forward = \%{$self->{FUNCTION_FORWARD}};
232 233
    my $function_internal_module = \%{$self->{FUNCTION_INTERNAL_MODULE}};
    my $function_external_module = \%{$self->{FUNCTION_EXTERNAL_MODULE}};
234
    my $function_wine_extension = \%{$self->{FUNCTION_WINE_EXTENSION}};
Patrik Stridvall's avatar
Patrik Stridvall committed
235
    my $modules = \%{$self->{MODULES}};
236
    my $module_files = \%{$self->{MODULE_FILES}};
237
    my $module_external_calling_convention = \%{$self->{MODULE_EXTERNAL_CALLING_CONVENTION}};
238 239

    my $file = shift;
240
    $file =~ s%^\./%%;
241 242

    my %ordinals;
243
    my $module;
244
    my $wine_extension = 0;
245

246
    $output->lazy_progress("$file");
247

248 249 250
    $module = $file;
    $module =~ s/^.*?([^\/]*)\.spec$/$1/;

251 252 253 254 255 256 257
    open(IN, "< $file") || die "$file: $!\n";
    $/ = "\n";
    my $header = 1;
    my $lookahead = 0;
    while($lookahead || defined($_ = <IN>)) {
	$lookahead = 0;
	s/^\s*(.*?)\s*$/$1/;
258 259
	if(s/^(.*?)\s*\#\s*(.*)\s*$/$1/) {
	    my $comment = $2;
Patrik Stridvall's avatar
Patrik Stridvall committed
260
	    if ($comment =~ /^Wine/i) { # FIXME: Kludge
261 262 263
		$wine_extension = 1;
	    }
	}
264 265 266
	/^$/ && next;

	if($header)  {
Patrik Stridvall's avatar
Patrik Stridvall committed
267
	    if(/^\d+|@/) { $header = 0; $lookahead = 1; }
268
	    next;
269
	}
270

271
	my $ordinal;
272
	if(/^(\d+|@)\s+
273 274
	   (pascal|stdcall|cdecl|varargs)\s+
	   ((?:(?:-noname|-norelay|-i386|-ret16|-ret64|-register|-interrupt|-private)\s+)*)(\S+)\s*\(\s*(.*?)\s*\)\s*(\S*)$/x)
275
	{
276
	    my $calling_convention = $2;
Patrik Stridvall's avatar
Patrik Stridvall committed
277 278 279 280 281
	    my $flags = $3;
	    my $external_name = $4;
	    my $arguments = $5;
	    my $internal_name = $6;

282
	    $ordinal = $1;
283

Patrik Stridvall's avatar
Patrik Stridvall committed
284 285
	    $flags =~ s/\s+/ /g;

Patrik Stridvall's avatar
Patrik Stridvall committed
286 287
	    $internal_name = $external_name if !$internal_name;

Patrik Stridvall's avatar
Patrik Stridvall committed
288 289 290 291
	    if($flags =~ /-noname/) {
		# $external_name = "@";
	    }

Patrik Stridvall's avatar
Patrik Stridvall committed
292 293 294
	    if($flags =~ /(?:-register|-interrupt)/) {
		if($arguments) { $arguments .= " "; }
		$arguments .= "ptr";
Patrik Stridvall's avatar
Patrik Stridvall committed
295
		$calling_convention .= " -register";
Patrik Stridvall's avatar
Patrik Stridvall committed
296 297
	    }

Patrik Stridvall's avatar
Patrik Stridvall committed
298
	    if ($internal_name =~ /^(.*?)\.(.*?)$/) {
Patrik Stridvall's avatar
Patrik Stridvall committed
299
		my $forward_module = lc($1);
Patrik Stridvall's avatar
Patrik Stridvall committed
300 301 302 303 304 305 306 307 308 309 310
		my $forward_name = $2;

		if (0) {
		    $calling_convention .= " -forward";
		} else {
		    $calling_convention = "forward";
		}

		$$function_forward{$module}{$external_name} = [$forward_module, $forward_name];
	    }

311 312 313 314 315
            if($external_name ne "@") {
                $$module_external_calling_convention{$module}{$external_name} = $calling_convention;
            } else {
                $$module_external_calling_convention{$module}{"\@$ordinal"} = $calling_convention;
            }
316 317 318 319 320 321 322 323 324 325
	    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";
	    }
326 327
	    $$function_internal_arguments{$internal_name} = $arguments;
	    $$function_external_arguments{$external_name} = $arguments;
328 329 330 331 332 333 334 335 336 337
	    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";
	    }
338 339 340 341
	    $$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";
342
	    } else {
343 344 345 346
		$$function_internal_module{$internal_name} .= " & $module";
	    }
	    if(!$$function_external_module{$external_name}) {
		$$function_external_module{$external_name} = "$module";
347
	    } else {
348
		$$function_external_module{$external_name} .= " & $module";
349
	    }
350
	    $$function_wine_extension{$module}{$external_name} = $wine_extension;
351

352
	    if(0 && $options->spec_mismatch) {
353 354
		if($external_name eq "@") {
		    if($internal_name !~ /^\U$module\E_$ordinal$/) {
355
			$output->write("$file: $external_name: the internal name ($internal_name) mismatch\n");
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
		    }
		} 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/;

		    # FIXME: This special case is becuase of a very ugly kludge that should be fixed IMHO
		    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)/)
		    {
379
			$output->write("$file: $external_name: internal name ($internal_name) mismatch\n");
380 381 382
		    }
		}
	    }
383
	} elsif(/^(\d+|@)\s+stub(?:\s+(-noname|-norelay|-i386|-ret16|-ret64|-private))?\s+(\S+)$/) {
384 385
	    $ordinal = $1;

Patrik Stridvall's avatar
Patrik Stridvall committed
386 387 388 389 390 391 392 393 394
	    my $flags = $2;
	    my $external_name = $3;

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

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

395
	    my $internal_name = $external_name;
396

397 398 399 400 401
            if ($external_name ne "@") {
                $$module_external_calling_convention{$module}{$external_name} = "stub";
            } else {
                $$module_external_calling_convention{$module}{"\@$ordinal"} = "stub";
            }
402 403 404 405 406 407 408 409 410 411
	    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";
	    }
412 413 414 415 416 417 418 419 420 421
	    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";
	    }
422 423
	    if(!$$function_internal_module{$internal_name}) {
		$$function_internal_module{$internal_name} = "$module";
424
	    } else { # if($$function_internal_module{$internal_name} !~ /$module/) {
425 426 427 428
		$$function_internal_module{$internal_name} .= " & $module";
	    }
	    if(!$$function_external_module{$external_name}) {
		$$function_external_module{$external_name} = "$module";
429
	    } else { # if($$function_external_module{$external_name} !~ /$module/) {
430
		$$function_external_module{$external_name} .= " & $module";
431
	    }
432
	} elsif(/^(\d+|@)\s+extern(?:\s+(?:-norelay|-i386|-ret16|-ret64))?\s+(\S+)\s*(\S*)$/) {
433 434 435 436 437
	    $ordinal = $1;

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

Patrik Stridvall's avatar
Patrik Stridvall committed
438 439
	    $internal_name = $external_name if !$internal_name;

440 441 442 443 444 445
            if ($external_name ne "@") {
                $$module_external_calling_convention{$module}{$external_name} = "extern";
            } else {
                $$module_external_calling_convention{$module}{"\@$ordinal"} = "extern";
            }
	} elsif(/^(\d+|@)\s+(equate|variable)/) {
446 447 448
	    # ignore
	} else {
	    my $next_line = <IN>;
Patrik Stridvall's avatar
Patrik Stridvall committed
449
	    if(!defined($next_line) || $next_line =~ /^\s*\d|@/) {
450 451 452 453 454 455
		die "$file: $.: syntax error: '$_'\n";
	    } else {
		$_ .= $next_line;
		$lookahead = 1;
	    }
	}
456

457
	if(defined($ordinal)) {
458
	    if($ordinal ne "@" && $ordinals{$ordinal}) {
459
		$output->write("$file: ordinal redefined: $_\n");
460 461 462
	    }
	    $ordinals{$ordinal}++;
	}
463 464
    }
    close(IN);
Patrik Stridvall's avatar
Patrik Stridvall committed
465 466

    $$modules{$module}++;
467 468

    $$module_files{$module} = $file;
469 470 471 472
}

sub name {
    my $self = shift;
473 474 475
    my $name = \${$self->{NAME}};

    return $$name;
476 477 478 479 480 481 482 483 484 485 486 487
}

sub is_allowed_kind {
    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
488 489 490 491 492 493 494 495 496 497

}

sub allow_kind {
    my $self = shift;
    my $allowed_kind = \%{$self->{ALLOWED_KIND}};

    my $kind = shift;

    $$allowed_kind{$kind}++;
498 499
}

500 501 502 503 504 505 506 507 508
sub is_limited_type {
    my $self = shift;
    my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};

    my $type = shift;

    return $$allowed_modules_limited{$type};
}

Patrik Stridvall's avatar
Patrik Stridvall committed
509
sub is_allowed_type_in_module {
510 511 512 513 514
    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
515 516 517
    my @modules = split(/ \& /, shift);

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

Patrik Stridvall's avatar
Patrik Stridvall committed
519 520 521 522 523
    foreach my $module (@modules) {
	if($$allowed_modules{$type}{$module}) { return 1; }
    }

    return 0;
524 525
}

Patrik Stridvall's avatar
Patrik Stridvall committed
526 527 528 529 530 531 532 533 534 535 536 537
sub allow_type_in_module {
    my $self = shift;
    my $allowed_modules = \%{$self->{ALLOWED_MODULES}};

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

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

538 539 540 541 542
sub type_used_in_module {
    my $self = shift;
    my $used_modules = \%{$self->{USED_MODULES}};

    my $type = shift;
Patrik Stridvall's avatar
Patrik Stridvall committed
543 544 545 546 547
    my @modules = split(/ \& /, shift);

    foreach my $module (@modules) {
	$$used_modules{$type}{$module} = 1;
    }
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

    return ();
}

sub types_not_used {
    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;
}

Patrik Stridvall's avatar
Patrik Stridvall committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
sub types_unlimited_used_in_modules {
    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;
	}
583
        if($count) {
Patrik Stridvall's avatar
Patrik Stridvall committed
584 585 586 587 588 589 590 591
	    foreach my $module (@modules) {
	      $$used_types{$type}{$module} = 1;
	    }
	}
    }
    return $used_types;
}

592 593 594 595
sub translate_argument {
    my $self = shift;
    my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};

Patrik Stridvall's avatar
Patrik Stridvall committed
596 597 598 599 600 601 602 603 604 605 606
    my $type = shift;

    return $$translate_argument{$type};
}

sub declare_argument {
    my $self = shift;
    my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};

    my $type = shift;
    my $kind = shift;
607

Patrik Stridvall's avatar
Patrik Stridvall committed
608
    $$translate_argument{$type} = $kind;
609 610 611 612 613 614 615 616 617
}

sub all_declared_types {
    my $self = shift;
    my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};

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

618 619 620 621 622 623 624 625 626 627 628 629 630
sub is_allowed_type_format {
    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)) {
631 632
	    if(defined($formats)) {
		$formats .= "|";
633 634
	    } else {
		$formats = "";
635
	    }
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
	    if(defined($$type_format{$_}{$type})) {
		$formats .= $$type_format{$_}{$type};
	    }
	}
    }

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

    return 0;
}

Patrik Stridvall's avatar
Patrik Stridvall committed
654 655 656 657 658 659 660
sub all_modules {
    my $self = shift;
    my $modules = \%{$self->{MODULES}};

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

Patrik Stridvall's avatar
Patrik Stridvall committed
661 662 663 664 665 666 667 668 669
sub is_module {
    my $self = shift;
    my $modules = \%{$self->{MODULES}};

    my $name = shift;

    return $$modules{$name};
}

670 671 672 673 674 675 676 677 678 679
sub module_file {
    my $self = shift;

    my $module = shift;

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

    return $$module_files{$module};
}

680
sub all_internal_functions {
681
    my $self = shift;
682
    my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
683

684
    return sort(keys(%$function_internal_calling_convention));
685 686
}

687
sub all_internal_functions_in_module {
Patrik Stridvall's avatar
Patrik Stridvall committed
688
    my $self = shift;
689 690
    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
691 692 693 694

    my $module = shift;

    my @names;
695 696
    foreach my $name (keys(%$function_internal_calling_convention)) {
	if($$function_internal_module{$name} eq $module) {
Patrik Stridvall's avatar
Patrik Stridvall committed
697 698 699 700 701 702 703
	    push @names, $name;
	}
    }

    return sort(@names);
}

704 705
sub all_external_functions {
    my $self = shift;
706
    my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
707

708
    return sort(keys(%$function_external_name));
709 710 711 712
}

sub all_external_functions_in_module {
    my $self = shift;
713
    my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
714 715 716 717 718
    my $function_external_module = \%{$self->{FUNCTION_EXTERNAL_MODULE}};

    my $module = shift;

    my @names;
719
    foreach my $name (keys(%$function_external_name)) {
720 721 722 723 724 725 726 727
	if($$function_external_module{$name} eq $module) {
	    push @names, $name;
	}
    }

    return sort(@names);
}

728
sub all_functions_in_module {
729
    my $self = shift;
730
    my $module_external_calling_convention = \%{$self->{MODULE_EXTERNAL_CALLING_CONVENTION}};
731

732 733 734
    my $module = shift;

    return sort(keys(%{$$module_external_calling_convention{$module}}));
735 736
}

737
sub all_broken_forwards {
738
    my $self = shift;
739
    my $function_forward = \%{$self->{FUNCTION_FORWARD}};
740

741 742 743 744
    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}};
745

746 747 748 749 750 751 752 753 754
	    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;
755 756
}

757

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
sub function_internal_ordinal {
    my $self = shift;
    my $function_internal_ordinal = \%{$self->{FUNCTION_INTERNAL_ORDINAL}};

    my $name = shift;

    return $$function_internal_ordinal{$name};
}

sub function_external_ordinal {
    my $self = shift;
    my $function_external_ordinal = \%{$self->{FUNCTION_EXTERNAL_ORDINAL}};

    my $name = shift;

    return $$function_external_ordinal{$name};
}

sub function_internal_calling_convention {
777
    my $self = shift;
778
    my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
779 780 781

    my $name = shift;

782
    return $$function_internal_calling_convention{$name};
783 784
}

785
sub function_external_calling_convention {
786
    my $self = shift;
787
    my $function_external_calling_convention = \%{$self->{FUNCTION_EXTERNAL_CALLING_CONVENTION}};
788 789 790

    my $name = shift;

791
    return $$function_external_calling_convention{$name};
792 793
}

794 795 796 797 798 799 800 801 802 803
sub function_external_calling_convention_in_module {
    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};
}

804 805 806 807 808 809 810 811 812
sub function_internal_name {
    my $self = shift;
    my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};

    my $name = shift;

    return $$function_internal_name{$name};
}

Patrik Stridvall's avatar
Patrik Stridvall committed
813 814 815 816 817 818 819 820 821
sub function_external_name {
    my $self = shift;
    my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};

    my $name = shift;

    return $$function_external_name{$name};
}

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
sub function_forward_final_destination {
    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);
}

839 840
sub is_function {
    my $self = shift;
841
    my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
842 843 844

    my $name = shift;

845
    return $$function_internal_calling_convention{$name};
846 847
}

848 849 850 851 852 853 854 855
sub all_shared_internal_functions {
    my $self = shift;
    my $function_shared = \%{$self->{FUNCTION_SHARED}};

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

sub is_shared_internal_function {
856 857 858 859 860 861 862 863
    my $self = shift;
    my $function_shared = \%{$self->{FUNCTION_SHARED}};

    my $name = shift;

    return $$function_shared{$name};
}

864
sub found_shared_internal_function {
865 866 867 868 869 870 871 872
    my $self = shift;
    my $function_shared = \%{$self->{FUNCTION_SHARED}};

    my $name = shift;

    $$function_shared{$name} = 1;
}

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
sub function_internal_arguments {
    my $self = shift;
    my $function_internal_arguments = \%{$self->{FUNCTION_INTERNAL_ARGUMENTS}};

    my $name = shift;

    return $$function_internal_arguments{$name};
}

sub function_external_arguments {
    my $self = shift;
    my $function_external_arguments = \%{$self->{FUNCTION_EXTERNAL_ARGUMENTS}};

    my $name = shift;

    return $$function_external_arguments{$name};
}

sub function_internal_module {
892
    my $self = shift;
893
    my $function_internal_module = \%{$self->{FUNCTION_INTERNAL_MODULE}};
894 895 896

    my $name = shift;

897
    return $$function_internal_module{$name};
898 899
}

900
sub function_external_module {
901
    my $self = shift;
902
    my $function_external_module = \%{$self->{FUNCTION_EXTERNAL_MODULE}};
903 904 905

    my $name = shift;

906
    return $$function_external_module{$name};
907 908
}

909 910 911 912 913 914 915 916 917 918
sub function_wine_extension {
    my $self = shift;
    my $function_wine_extension = \%{$self->{FUNCTION_WINE_EXTENSION}};

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

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

919 920
sub is_function_stub {
    my $self = shift;
921
    my $module_external_calling_convention = \%{$self->{MODULE_EXTERNAL_CALLING_CONVENTION}};
922 923 924 925

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

Patrik Stridvall's avatar
Patrik Stridvall committed
926 927
    if($$module_external_calling_convention{$module}{$name} eq "stub") {
	return 1;
928 929 930 931 932
    }
    return 0;
}

sub is_function_stub_in_module {
933
    my $self = shift;
934
    my $module_external_calling_convention = \%{$self->{MODULE_EXTERNAL_CALLING_CONVENTION}};
935

936
    my $module = shift;
937 938
    my $name = shift;

939 940 941 942
    if(!defined($$module_external_calling_convention{$module}{$name})) {
	return 0;
    }
    return $$module_external_calling_convention{$module}{$name} eq "stub";
943 944
}

945 946 947 948
########################################################################
# class methods
#

949 950
sub _get_all_module_internal_ordinal {
    my $winapi = shift;
951 952 953
    my $internal_name = shift;

    my @entries = ();
954 955 956 957 958

    my @name = (); {
	my $name = $winapi->function_external_name($internal_name);
	if(defined($name)) {
	    @name = split(/ & /, $name);
959
	}
960 961 962 963 964 965
    }

    my @module = (); {
	my $module = $winapi->function_internal_module($internal_name);
	if(defined($module)) {
	    @module = split(/ & /, $module);
966
	}
967
    }
968

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

976 977 978 979 980
    my $name;
    my $module;
    my $ordinal;
    while(defined($name = shift @name) &&
	  defined($module = shift @module) &&
981
	  defined($ordinal = shift @ordinal))
982 983 984 985
    {
	push @entries, [$name, $module, $ordinal];
    }

986 987 988
    return @entries;
}

989 990 991
sub get_all_module_internal_ordinal16 {
    return _get_all_module_internal_ordinal($win16api, @_);
}
992

993 994 995 996 997
sub get_all_module_internal_ordinal32 {
    return _get_all_module_internal_ordinal($win32api, @_);
}

sub get_all_module_internal_ordinal {
998 999
    my @entries = ();
    foreach my $winapi (@winapis) {
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	push @entries, _get_all_module_internal_ordinal($winapi, @_);
    }

    return @entries;
}

sub _get_all_module_external_ordinal {
    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);
1016
	}
1017 1018 1019 1020 1021 1022
    }

    my @module = (); {
	my $module = $winapi->function_external_module($external_name);
	if(defined($module)) {
	    @module = split(/ & /, $module);
1023
	}
1024
    }
1025

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

1033 1034 1035 1036 1037
    my $name;
    my $module;
    my $ordinal;
    while(defined($name = shift @name) &&
	  defined($module = shift @module) &&
1038
	  defined($ordinal = shift @ordinal))
1039 1040 1041
    {
	push @entries, [$name, $module, $ordinal];
    }
1042

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
    return @entries;
}

sub get_all_module_external_ordinal16 {
    return _get_all_module_external_ordinal($win16api, @_);
}

sub get_all_module_external_ordinal32 {
    return _get_all_module_external_ordinal($win32api, @_);
}

sub get_all_module_external_ordinal {
    my @entries = ();
    foreach my $winapi (@winapis) {
	push @entries, _get_all_module_external_ordinal($winapi, @_);
    }
1059 1060 1061 1062

    return @entries;
}

1063
1;