2 # Copyright 1999, 2000, 2001 Patrik Stridvall
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
28 @EXPORT_OK = qw($win16api $win32api @winapis);
30 use vars qw($win16api $win32api @winapis);
35 $current_dir $wine_dir
37 use modules qw($modules);
38 use options qw($options);
39 use output qw($output);
41 my @spec_files16 = $modules->allowed_spec_files16;
42 $win16api = 'winapi'->new("win16", \@spec_files16);
44 my @spec_files32 = $modules->allowed_spec_files32;
45 $win32api = 'winapi'->new("win32", \@spec_files32);
47 @winapis = ($win16api, $win32api);
49 for my $internal_name ($win32api->all_internal_functions) {
50 my $module16 = $win16api->function_internal_module($internal_name);
51 my $module32 = $win16api->function_internal_module($internal_name);
52 if(defined($module16) &&
53 !$win16api->is_function_stub_in_module($module16, $internal_name) &&
54 !$win32api->is_function_stub_in_module($module32, $internal_name))
56 $win16api->found_shared_internal_function($internal_name);
57 $win32api->found_shared_internal_function($internal_name);
63 my $class = ref($proto) || $proto;
65 bless ($self, $class);
67 my $name = \${$self->{NAME}};
68 my $function_forward = \%{$self->{FUNCTION_FORWARD}};
69 my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
70 my $function_module = \%{$self->{FUNCTION_MODULE}};
73 my $refspec_files = shift;
75 foreach my $file (@$refspec_files) {
76 $self->parse_spec_file("$wine_dir/$file");
79 foreach my $file (get_api_files($$name)) {
81 $module =~ s/.*?\/([^\/]*?)\.api$/$1/;
83 if($modules->is_allowed_module($module)) {
84 $self->parse_api_file($file,$module);
88 foreach my $forward_name (sort(keys(%$function_forward))) {
89 $$function_forward{$forward_name} =~ /^(\S*):(\S*)\.(\S*)$/;
90 (my $from_module, my $to_module, my $external_name) = ($1, $2, $3);
91 my $internal_name = $$function_internal_name{$external_name};
92 if(defined($internal_name)) {
93 $$function_module{$internal_name} .= " & $from_module";
111 my $allowed_kind = \%{$self->{ALLOWED_KIND}};
112 my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
113 my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
114 my $allowed_modules_unlimited = \%{$self->{ALLOWED_MODULES_UNLIMITED}};
115 my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
116 my $type_format = \%{$self->{TYPE_FORMAT}};
126 $output->lazy_progress("$file");
128 open(IN, "< $wine_dir/$file") || die "$wine_dir/$file: $!\n";
131 s/^\s*?(.*?)\s*$/$1/; # remove whitespace at begin and end of line
132 s/^(.*?)\s*#.*$/$1/; # remove comments
133 /^$/ && next; # skip empty lines
141 $$allowed_kind{$kind} = 1;
144 } elsif(/^--extension/) {
146 } elsif(/^--format=(\".*?\"|\S*)/) {
148 $format =~ s/^\"(.*?)\"$/$1/;
151 if(!defined($format)) {
152 if($kind eq "long") {
153 $format = "%d|%u|%x|%X|";
154 $format .= "%hd|%hu|%hx|%hX|";
155 $format .= "%ld|%lu|%lx|%lX|";
156 $format .= "%04x|%04X|0x%04x|0x%04X|";
157 $format .= "%08x|%08X|0x%08x|0x%08X|";
158 $format .= "%08lx|%08lX|0x%08lx|0x%08lX";
159 } elsif($kind eq "longlong") {
161 } elsif($kind eq "ptr") {
163 } elsif($kind eq "segptr") {
165 } elsif($kind eq "str") {
167 } elsif($kind eq "wstr") {
169 } elsif($kind eq "word") {
170 $format = "%d|%u|%x|%X|";
171 $format .= "%hd|%hu|%hx|%hX|";
172 $format .= "%04x|%04X|0x%04x|0x%04X";
174 $format = "<unknown>";
177 } elsif(defined($kind)) {
180 if(defined($module)) {
181 if($$allowed_modules_unlimited{$type}) {
182 $output->write("$file: type ($type) already specificed as an unlimited type\n");
183 } elsif(!$$allowed_modules{$type}{$module}) {
184 $$allowed_modules{$type}{$module} = 1;
185 $$allowed_modules_limited{$type} = 1;
187 $output->write("$file: type ($type) already specificed\n");
190 $$allowed_modules_unlimited{$type} = 1;
193 $$allowed_modules_limited{$type} = 1;
195 if(defined($$translate_argument{$type}) && $$translate_argument{$type} ne $kind) {
196 $output->write("$file: type ($type) respecified as different kind ($kind != $$translate_argument{$type})\n");
198 $$translate_argument{$type} = $kind;
201 $$type_format{$module}{$type} = $format;
203 $output->write("$file: file must begin with %<type> statement\n");
210 sub parse_spec_file {
213 my $function_internal_arguments = \%{$self->{FUNCTION_INTERNAL_ARGUMENTS}};
214 my $function_external_arguments = \%{$self->{FUNCTION_EXTERNAL_ARGUMENTS}};
215 my $function_internal_ordinal = \%{$self->{FUNCTION_INTERNAL_ORDINAL}};
216 my $function_external_ordinal = \%{$self->{FUNCTION_EXTERNAL_ORDINAL}};
217 my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
218 my $function_external_calling_convention = \%{$self->{FUNCTION_EXTERNAL_CALLING_CONVENTION}};
219 my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
220 my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
221 my $function_stub = \%{$self->{FUNCTION_STUB}};
222 my $function_forward = \%{$self->{FUNCTION_FORWARD}};
223 my $function_internal_module = \%{$self->{FUNCTION_INTERNAL_MODULE}};
224 my $function_external_module = \%{$self->{FUNCTION_EXTERNAL_MODULE}};
225 my $modules = \%{$self->{MODULES}};
226 my $module_files = \%{$self->{MODULE_FILES}};
236 $output->lazy_progress("$file");
238 open(IN, "< $file") || die "$file: $!\n";
242 while($lookahead || defined($_ = <IN>)) {
249 if(/^name\s*(\S*)/) { $module = $1; }
250 if(/^file\s*(\S*)/) { $module_file = $1; }
251 if(/^type\s*(\w+)/) { $type = $1; }
252 if(/^\d+|@/) { $header = 0; $lookahead = 1; }
258 (pascal|pascal16|stdcall|cdecl|varargs)\s+
259 ((?:(?:-noimport|-norelay|-i386|-ret64|-register|-interrupt)\s+)*)(\S+)\s*\(\s*(.*?)\s*\)\s*(\S+)$/x)
261 my $calling_convention = $2;
263 my $external_name = $4;
265 my $internal_name = $6;
271 if($flags =~ /(?:-register|-interrupt)/) {
272 if($arguments) { $arguments .= " "; }
276 if(!$$function_internal_name{$external_name}) {
277 $$function_internal_name{$external_name} = $internal_name;
279 $$function_internal_name{$external_name} .= " & $internal_name";
281 if(!$$function_external_name{$internal_name}) {
282 $$function_external_name{$internal_name} = $external_name;
284 $$function_external_name{$internal_name} .= " & $external_name";
286 $$function_internal_arguments{$internal_name} = $arguments;
287 $$function_external_arguments{$external_name} = $arguments;
288 if(!$$function_internal_ordinal{$internal_name}) {
289 $$function_internal_ordinal{$internal_name} = $ordinal;
291 $$function_internal_ordinal{$internal_name} .= " & $ordinal";
293 if(!$$function_external_ordinal{$external_name}) {
294 $$function_external_ordinal{$external_name} = $ordinal;
296 $$function_external_ordinal{$external_name} .= " & $ordinal";
298 $$function_internal_calling_convention{$internal_name} = $calling_convention;
299 $$function_external_calling_convention{$external_name} = $calling_convention;
300 if(!$$function_internal_module{$internal_name}) {
301 $$function_internal_module{$internal_name} = "$module";
303 $$function_internal_module{$internal_name} .= " & $module";
305 if(!$$function_external_module{$external_name}) {
306 $$function_external_module{$external_name} = "$module";
308 $$function_external_module{$external_name} .= " & $module";
311 if(0 && $options->spec_mismatch) {
312 if($external_name eq "@") {
313 if($internal_name !~ /^\U$module\E_$ordinal$/) {
314 $output->write("$file: $external_name: the internal name ($internal_name) mismatch\n");
317 my $name = $external_name;
323 $name2 =~ s/^(?:_|Rtl|k32|K32)//;
326 $name3 =~ s/^INT_Int[0-9a-f]{2}Handler$/BUILTIN_DefaultIntHandler/;
329 $name4 =~ s/^(VxDCall)\d$/$1/;
331 # FIXME: This special case is becuase of a very ugly kludge that should be fixed IMHO
333 $name5 =~ s/^(.*?16)_(.*?)$/$1_fn$2/;
335 if(uc($internal_name) ne uc($external_name) &&
336 $internal_name !~ /(\Q$name\E|\Q$name1\E|\Q$name2\E|\Q$name3\E|\Q$name4\E|\Q$name5\E)/)
338 $output->write("$file: $external_name: internal name ($internal_name) mismatch\n");
342 } elsif(/^(\d+|@)\s+stub(?:\s+(?:-noimport|-norelay|-i386|-ret64))?\s+(\S+)$/) {
343 my $external_name = $2;
348 if(0 && $type eq "win16") {
349 if($external_name =~ /\d$/) {
350 $internal_name = $external_name . "_16";
352 $internal_name = $external_name . "16";
355 $internal_name = $external_name;
358 $$function_stub{$module}{$external_name} = 1;
359 if(!$$function_internal_name{$external_name}) {
360 $$function_internal_name{$external_name} = $internal_name;
362 $$function_internal_name{$external_name} .= " & $internal_name";
364 if(!$$function_external_name{$internal_name}) {
365 $$function_external_name{$internal_name} = $external_name;
367 $$function_external_name{$internal_name} .= " & $external_name";
369 if(!$$function_internal_ordinal{$internal_name}) {
370 $$function_internal_ordinal{$internal_name} = $ordinal;
372 $$function_internal_ordinal{$internal_name} .= " & $ordinal";
374 if(!$$function_external_ordinal{$external_name}) {
375 $$function_external_ordinal{$external_name} = $ordinal;
377 $$function_external_ordinal{$external_name} .= " & $ordinal";
379 if(!$$function_internal_module{$internal_name}) {
380 $$function_internal_module{$internal_name} = "$module";
381 } else { # if($$function_internal_module{$internal_name} !~ /$module/) {
382 $$function_internal_module{$internal_name} .= " & $module";
384 if(!$$function_external_module{$external_name}) {
385 $$function_external_module{$external_name} = "$module";
386 } else { # if($$function_external_module{$external_name} !~ /$module/) {
387 $$function_external_module{$external_name} .= " & $module";
389 } elsif(/^(\d+|@)\s+forward(?:\s+(?:-noimport|-norelay|-i386|-ret64))?\s+(\S+)\s+(\S+)\.(\S+)$/) {
392 my $external_name = $2;
393 my $forward_module = lc($3);
394 my $forward_name = $4;
396 $$function_forward{$external_name} = "$module:$forward_module.$forward_name";
397 } elsif(/^(\d+|@)\s+(equate|extern|variable)/) {
400 my $next_line = <IN>;
401 if(!defined($next_line) || $next_line =~ /^\s*\d|@/) {
402 die "$file: $.: syntax error: '$_'\n";
409 if(defined($ordinal)) {
410 if($ordinal ne "@" && $ordinals{$ordinal}) {
411 $output->write("$file: ordinal redefined: $_\n");
413 $ordinals{$ordinal}++;
418 $$modules{$module}++;
420 $$module_files{$module} = $file;
425 my $name = \${$self->{NAME}};
430 sub is_allowed_kind {
432 my $allowed_kind = \%{$self->{ALLOWED_KIND}};
436 return $$allowed_kind{$kind};
445 my $allowed_kind = \%{$self->{ALLOWED_KIND}};
449 $$allowed_kind{$kind}++;
452 sub is_limited_type {
454 my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
458 return $$allowed_modules_limited{$type};
461 sub is_allowed_type_in_module {
463 my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
464 my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
467 my @modules = split(/ \& /, shift);
469 if(!$$allowed_modules_limited{$type}) { return 1; }
471 foreach my $module (@modules) {
472 if($$allowed_modules{$type}{$module}) { return 1; }
478 sub allow_type_in_module {
480 my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
483 my @modules = split(/ \& /, shift);
485 foreach my $module (@modules) {
486 $$allowed_modules{$type}{$module}++;
490 sub type_used_in_module {
492 my $used_modules = \%{$self->{USED_MODULES}};
495 my @modules = split(/ \& /, shift);
497 foreach my $module (@modules) {
498 $$used_modules{$type}{$module} = 1;
506 my $used_modules = \%{$self->{USED_MODULES}};
507 my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
510 foreach my $type (sort(keys(%$allowed_modules))) {
511 foreach my $module (sort(keys(%{$$allowed_modules{$type}}))) {
512 if(!$$used_modules{$type}{$module}) {
513 $$not_used{$module}{$type} = 1;
520 sub types_unlimited_used_in_modules {
523 my $used_modules = \%{$self->{USED_MODULES}};
524 my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
525 my $allowed_modules_unlimited = \%{$self->{ALLOWED_MODULES_UNLIMITED}};
528 foreach my $type (sort(keys(%$allowed_modules_unlimited))) {
531 foreach my $module (sort(keys(%{$$used_modules{$type}}))) {
533 push @modules, $module;
536 foreach my $module (@modules) {
537 $$used_types{$type}{$module} = 1;
544 sub translate_argument {
546 my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
550 return $$translate_argument{$type};
553 sub declare_argument {
555 my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
560 $$translate_argument{$type} = $kind;
563 sub all_declared_types {
565 my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
567 return sort(keys(%$translate_argument));
570 sub is_allowed_type_format {
572 my $type_format = \%{$self->{TYPE_FORMAT}};
580 if(defined($module) && defined($type)) {
582 foreach (split(/ & /, $module)) {
583 if(defined($formats)) {
588 if(defined($$type_format{$_}{$type})) {
589 $formats .= $$type_format{$_}{$type};
594 if(defined($formats)) {
596 foreach (split(/\|/, $formats)) {
608 my $modules = \%{$self->{MODULES}};
610 return sort(keys(%$modules));
615 my $modules = \%{$self->{MODULES}};
619 return $$modules{$name};
627 my $module_files = \%{$self->{MODULE_FILES}};
629 return $$module_files{$module};
632 sub all_internal_functions {
634 my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
636 return sort(keys(%$function_internal_calling_convention));
639 sub all_internal_functions_in_module {
641 my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
642 my $function_internal_module = \%{$self->{FUNCTION_INTERNAL_MODULE}};
647 foreach my $name (keys(%$function_internal_calling_convention)) {
648 if($$function_internal_module{$name} eq $module) {
656 sub all_external_functions {
658 my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
660 return sort(keys(%$function_internal_name));
663 sub all_external_functions_in_module {
665 my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
666 my $function_external_module = \%{$self->{FUNCTION_EXTERNAL_MODULE}};
671 foreach my $name (keys(%$function_internal_name)) {
672 if($$function_external_module{$name} eq $module) {
680 sub all_functions_stub {
682 my $function_stub = \%{$self->{FUNCTION_STUB}};
683 my $modules = \%{$self->{MODULES}};
686 foreach my $module (keys(%$modules)) {
687 push @stubs, keys(%{$$function_stub{$module}});
692 sub all_functions_stub_in_module {
694 my $function_stub = \%{$self->{FUNCTION_STUB}};
698 return sort(keys(%{$$function_stub{$module}}));
701 sub function_internal_ordinal {
703 my $function_internal_ordinal = \%{$self->{FUNCTION_INTERNAL_ORDINAL}};
707 return $$function_internal_ordinal{$name};
710 sub function_external_ordinal {
712 my $function_external_ordinal = \%{$self->{FUNCTION_EXTERNAL_ORDINAL}};
716 return $$function_external_ordinal{$name};
719 sub function_internal_calling_convention {
721 my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
725 return $$function_internal_calling_convention{$name};
728 sub function_external_calling_convention {
730 my $function_external_calling_convention = \%{$self->{FUNCTION_EXTERNAL_CALLING_CONVENTION}};
734 return $$function_external_calling_convention{$name};
737 sub function_internal_name {
739 my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
743 return $$function_internal_name{$name};
746 sub function_external_name {
748 my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
752 return $$function_external_name{$name};
757 my $function_internal_calling_convention = \%{$self->{FUNCTION_INTERNAL_CALLING_CONVENTION}};
761 return $$function_internal_calling_convention{$name};
764 sub all_shared_internal_functions {
766 my $function_shared = \%{$self->{FUNCTION_SHARED}};
768 return sort(keys(%$function_shared));
771 sub is_shared_internal_function {
773 my $function_shared = \%{$self->{FUNCTION_SHARED}};
777 return $$function_shared{$name};
780 sub found_shared_internal_function {
782 my $function_shared = \%{$self->{FUNCTION_SHARED}};
786 $$function_shared{$name} = 1;
789 sub function_internal_arguments {
791 my $function_internal_arguments = \%{$self->{FUNCTION_INTERNAL_ARGUMENTS}};
795 return $$function_internal_arguments{$name};
798 sub function_external_arguments {
800 my $function_external_arguments = \%{$self->{FUNCTION_EXTERNAL_ARGUMENTS}};
804 return $$function_external_arguments{$name};
807 sub function_internal_module {
809 my $function_internal_module = \%{$self->{FUNCTION_INTERNAL_MODULE}};
813 return $$function_internal_module{$name};
816 sub function_external_module {
818 my $function_external_module = \%{$self->{FUNCTION_EXTERNAL_MODULE}};
822 return $$function_external_module{$name};
825 sub is_function_stub {
827 my $function_stub = \%{$self->{FUNCTION_STUB}};
828 my $modules = \%{$self->{MODULES}};
833 foreach my $module (keys(%$modules)) {
834 if($$function_stub{$module}{$name}) {
842 sub is_function_stub_in_module {
844 my $function_stub = \%{$self->{FUNCTION_STUB}};
849 return $$function_stub{$module}{$name};
852 ########################################################################
856 sub _get_all_module_internal_ordinal {
858 my $internal_name = shift;
863 my $name = $winapi->function_external_name($internal_name);
865 @name = split(/ & /, $name);
870 my $module = $winapi->function_internal_module($internal_name);
871 if(defined($module)) {
872 @module = split(/ & /, $module);
877 my $ordinal = $winapi->function_internal_ordinal($internal_name);
878 if(defined($ordinal)) {
879 @ordinal = split(/ & /, $ordinal);
886 while(defined($name = shift @name) &&
887 defined($module = shift @module) &&
888 defined($ordinal = shift @ordinal))
890 push @entries, [$name, $module, $ordinal];
896 sub get_all_module_internal_ordinal16 {
897 return _get_all_module_internal_ordinal($win16api, @_);
900 sub get_all_module_internal_ordinal32 {
901 return _get_all_module_internal_ordinal($win32api, @_);
904 sub get_all_module_internal_ordinal {
906 foreach my $winapi (@winapis) {
907 push @entries, _get_all_module_internal_ordinal($winapi, @_);
913 sub _get_all_module_external_ordinal {
915 my $external_name = shift;
920 my $name = $winapi->function_internal_name($external_name);
922 @name = split(/ & /, $name);
927 my $module = $winapi->function_external_module($external_name);
928 if(defined($module)) {
929 @module = split(/ & /, $module);
934 my $ordinal = $winapi->function_external_ordinal($external_name);
935 if(defined($ordinal)) {
936 @ordinal = split(/ & /, $ordinal);
943 while(defined($name = shift @name) &&
944 defined($module = shift @module) &&
945 defined($ordinal = shift @ordinal))
947 push @entries, [$name, $module, $ordinal];
953 sub get_all_module_external_ordinal16 {
954 return _get_all_module_external_ordinal($win16api, @_);
957 sub get_all_module_external_ordinal32 {
958 return _get_all_module_external_ordinal($win32api, @_);
961 sub get_all_module_external_ordinal {
963 foreach my $winapi (@winapis) {
964 push @entries, _get_all_module_external_ordinal($winapi, @_);