- Updated API files
[wine.git] / tools / winapi_check / winapi.pm
blobcc96b709b146267fb5cbe250d85f4b1c1aed6f76
1 package winapi;
3 use strict;
5 sub new {
6 my $proto = shift;
7 my $class = ref($proto) || $proto;
8 my $self = {};
9 bless ($self, $class);
11 my $options = \${$self->{OPTIONS}};
12 my $output = \${$self->{OUTPUT}};
13 my $name = \${$self->{NAME}};
15 $$options = shift;
16 $$output = shift;
17 $$name = shift;
18 my $path = shift;
20 my @files = map {
21 s/^.\/(.*)$/$1/;
22 $_;
23 } split(/\n/, `find $path -name \\*.api`);
25 foreach my $file (@files) {
26 my $module = $file;
27 $module =~ s/.*?\/([^\/]*?)\.api$/$1/;
28 $self->parse_api_file($file,$module);
31 return $self;
34 sub parse_api_file {
35 my $self = shift;
37 my $options = \${$self->{OPTIONS}};
38 my $output = \${$self->{OUTPUT}};
39 my $allowed_kind = \%{$self->{ALLOWED_KIND}};
40 my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
41 my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
42 my $allowed_modules_unlimited = \%{$self->{ALLOWED_MODULES_UNLIMITED}};
43 my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
44 my $type_format = \%{$self->{TYPE_FORMAT}};
46 my $file = shift;
47 my $module = shift;
49 my $kind;
50 my $format;
51 my $extension = 0;
52 my $forbidden = 0;
54 if($$options->progress) {
55 $$output->progress("$file");
58 open(IN, "< $file") || die "$file: $!\n";
59 $/ = "\n";
60 while(<IN>) {
61 s/^\s*?(.*?)\s*$/$1/; # remove whitespace at begin and end of line
62 s/^(.*?)\s*#.*$/$1/; # remove comments
63 /^$/ && next; # skip empty lines
65 if(s/^%(\S+)\s*//) {
66 $kind = $1;
67 $format = undef;
68 $forbidden = 0;
69 $extension = 0;
71 $$allowed_kind{$kind} = 1;
72 if(/^--forbidden/) {
73 $forbidden = 1;
74 } elsif(/^--extension/) {
75 $extension = 1;
76 } elsif(/^--format=(\".*?\"|\S*)/) {
77 $format = $1;
78 $format =~ s/^\"(.*?)\"$/$1/;
81 if(!defined($format)) {
82 if($kind eq "long") {
83 $format = "%d|%u|%x|%X|";
84 $format .= "%hd|%hu|%hx|%hX|";
85 $format .= "%ld|%lu|%lx|%lX|";
86 $format .= "%04x|%04X|0x%04x|0x%04X|";
87 $format .= "%08x|%08X|0x%08x|0x%08X|";
88 $format .= "%08lx|%08lX|0x%08lx|0x%08lX";
89 } elsif($kind eq "longlong") {
90 $format = "%lld";
91 } elsif($kind eq "ptr") {
92 $format = "%p";
93 } elsif($kind eq "segptr") {
94 $format = "%p";
95 } elsif($kind eq "str") {
96 $format = "%p|%s";
97 } elsif($kind eq "wstr") {
98 $format = "%p|%s";
99 } elsif($kind eq "word") {
100 $format = "%d|%u|%x|%X|";
101 $format .= "%hd|%hu|%hx|%hX|";
102 $format .= "%04x|%04X|0x%04x|0x%04X";
103 } else {
104 $format = "<unknown>";
107 } elsif(defined($kind)) {
108 my $type = $_;
109 if(!$forbidden) {
110 if(defined($module)) {
111 if($$allowed_modules_unlimited{$type}) {
112 $$output->write("$file: type ($type) already specificed as an unlimited type\n");
113 } elsif(!$$allowed_modules{$type}{$module}) {
114 $$allowed_modules{$type}{$module} = 1;
115 $$allowed_modules_limited{$type} = 1;
116 } else {
117 $$output->write("$file: type ($type) already specificed\n");
119 } else {
120 $$allowed_modules_unlimited{$type} = 1;
122 } else {
123 $$allowed_modules_limited{$type} = 1;
125 if(defined($$translate_argument{$type}) && $$translate_argument{$type} ne $kind) {
126 $$output->write("$file: type ($type) respecified as different kind ($kind != $$translate_argument{$type})\n");
127 } else {
128 $$translate_argument{$type} = $kind;
131 $$type_format{$module}{$type} = $format;
132 } else {
133 $$output->write("$file: file must begin with %<type> statement\n");
134 exit 1;
137 close(IN);
140 sub get_spec_file_type {
141 my $proto = shift;
142 my $class = ref($proto) || $proto;
144 my $file = shift;
146 my $module;
147 my $type;
149 open(IN, "< $file") || die "$file: $!\n";
150 local $/ = "\n";
151 while(<IN>) {
152 s/^\s*(.*?)\s*$/$1/;
153 s/^(.*?)\s*#.*$/$1/;
154 /^$/ && next;
156 if(/^name\s*(\S*)/) { $module = $1; }
157 if(/^type\s*(\w+)/) { $type = $1; }
159 if(defined($module) && defined($type)) { last; }
161 close(IN);
163 return ($type, $module);
166 sub read_spec_files {
167 my $proto = shift;
168 my $class = ref($proto) || $proto;
170 my $modules = shift;
171 my $wine_dir = shift;
172 my $current_dir = shift;
173 my $files = shift;
174 my $win16api = shift;
175 my $win32api = shift;
177 foreach my $file (@$files) {
178 (my $type, my $module) = 'winapi'->get_spec_file_type("$wine_dir/$file");
179 $modules->spec_file_module($file, $module);
180 if($type eq "win16") {
181 $win16api->parse_spec_file("$wine_dir/$file");
182 } elsif($type eq "win32") {
183 $win32api->parse_spec_file("$wine_dir/$file");
188 sub read_all_spec_files {
189 my $proto = shift;
190 my $class = ref($proto) || $proto;
192 my $modules = shift;
193 my $wine_dir = shift;
194 my $current_dir = shift;
195 my $file_type = shift;
196 my $win16api = shift;
197 my $win32api = shift;
199 my @files = map {
200 s/^$wine_dir\/(.*)$/$1/;
201 if(&$file_type($_) eq "library") {
203 } else {
206 } split(/\n/, `find $wine_dir -name \\*.spec`);
208 'winapi'->read_spec_files($modules, $wine_dir, $current_dir, \@files, $win16api, $win32api);
211 sub parse_spec_file {
212 my $self = shift;
214 my $options = \${$self->{OPTIONS}};
215 my $output = \${$self->{OUTPUT}};
216 my $function_arguments = \%{$self->{FUNCTION_ARGUMENTS}};
217 my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
218 my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
219 my $function_stub = \%{$self->{FUNCTION_STUB}};
220 my $function_module = \%{$self->{FUNCTION_MODULE}};
221 my $modules = \%{$self->{MODULES}};
223 my $file = shift;
225 my %ordinals;
226 my $type;
227 my $module;
229 if($$options->progress) {
230 $$output->progress("$file");
233 open(IN, "< $file") || die "$file: $!\n";
234 $/ = "\n";
235 my $header = 1;
236 my $lookahead = 0;
237 while($lookahead || defined($_ = <IN>)) {
238 $lookahead = 0;
239 s/^\s*(.*?)\s*$/$1/;
240 s/^(.*?)\s*#.*$/$1/;
241 /^$/ && next;
243 if($header) {
244 if(/^name\s*(\S*)/) { $module = $1; }
245 if(/^type\s*(\w+)/) { $type = $1; }
246 if(/^\d+|@/) { $header = 0; $lookahead = 1; }
247 next;
250 my $ordinal;
251 if(/^(\d+|@)\s+(pascal|pascal16|stdcall|cdecl|register|interrupt|varargs)\s+(\S+)\s*\(\s*(.*?)\s*\)\s*(\S+)$/) {
252 my $calling_convention = $2;
253 my $external_name = $3;
254 my $arguments = $4;
255 my $internal_name = $5;
257 $ordinal = $1;
259 # FIXME: Internal name existing more than once not handled properly
260 $$function_external_name{$internal_name} = $external_name;
261 $$function_arguments{$internal_name} = $arguments;
262 $$function_calling_convention{$internal_name} = $calling_convention;
263 if(!$$function_module{$internal_name}) {
264 $$function_module{$internal_name} = "$module";
265 } elsif($$function_module{$internal_name} !~ /$module/) {
266 $$function_module{$internal_name} .= " & $module";
269 if(0 && $$options->spec_mismatch) {
270 if($external_name eq "@") {
271 if($internal_name !~ /^\U$module\E_$ordinal$/) {
272 $$output->write("$file: $external_name: the internal name ($internal_name) mismatch\n");
274 } else {
275 my $name = $external_name;
277 my $name1 = $name;
278 $name1 =~ s/^Zw/Nt/;
280 my $name2 = $name;
281 $name2 =~ s/^(?:_|Rtl|k32|K32)//;
283 my $name3 = $name;
284 $name3 =~ s/^INT_Int[0-9a-f]{2}Handler$/BUILTIN_DefaultIntHandler/;
286 my $name4 = $name;
287 $name4 =~ s/^(VxDCall)\d$/$1/;
289 # FIXME: This special case is becuase of a very ugly kludge that should be fixed IMHO
290 my $name5 = $name;
291 $name5 =~ s/^(.*?16)_(.*?)$/$1_fn$2/;
293 if(uc($internal_name) ne uc($external_name) &&
294 $internal_name !~ /(\Q$name\E|\Q$name1\E|\Q$name2\E|\Q$name3\E|\Q$name4\E|\Q$name5\E)/)
296 $$output->write("$file: $external_name: internal name ($internal_name) mismatch\n");
300 } elsif(/^(\d+|@)\s+stub\s+(\S+)$/) {
301 my $external_name = $2;
303 $ordinal = $1;
305 my $internal_name;
306 if($type eq "win16") {
307 $internal_name = $external_name . "16";
308 } else {
309 $internal_name = $external_name;
312 # FIXME: Internal name existing more than once not handled properly
313 $$function_stub{$internal_name} = 1;
314 if(!$$function_module{$internal_name}) {
315 $$function_module{$internal_name} = "$module";
316 } elsif($$function_module{$internal_name} !~ /$module/) {
317 $$function_module{$internal_name} .= " & $module";
319 } elsif(/^(\d+|@)\s+(equate|long|word|extern|forward)/) {
320 # ignore
321 } else {
322 my $next_line = <IN>;
323 if(!defined($next_line) || $next_line =~ /^\s*\d|@/) {
324 die "$file: $.: syntax error: '$_'\n";
325 } else {
326 $_ .= $next_line;
327 $lookahead = 1;
331 if(defined($ordinal)) {
332 if($ordinal ne "@" && $ordinals{$ordinal}) {
333 $$output->write("$file: ordinal redefined: $_\n");
335 $ordinals{$ordinal}++;
338 close(IN);
340 $$modules{$module}++;
343 sub name {
344 my $self = shift;
345 my $name = \${$self->{NAME}};
347 return $$name;
350 sub is_allowed_kind {
351 my $self = shift;
352 my $allowed_kind = \%{$self->{ALLOWED_KIND}};
354 my $kind = shift;
355 if(defined($kind)) {
356 return $$allowed_kind{$kind};
357 } else {
358 return 0;
362 sub is_limited_type {
363 my $self = shift;
364 my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
366 my $type = shift;
368 return $$allowed_modules_limited{$type};
371 sub allowed_type_in_module {
372 my $self = shift;
373 my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
374 my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
376 my $type = shift;
377 my @modules = split(/ \& /, shift);
379 if(!$$allowed_modules_limited{$type}) { return 1; }
381 foreach my $module (@modules) {
382 if($$allowed_modules{$type}{$module}) { return 1; }
385 return 0;
388 sub type_used_in_module {
389 my $self = shift;
390 my $used_modules = \%{$self->{USED_MODULES}};
392 my $type = shift;
393 my @modules = split(/ \& /, shift);
395 foreach my $module (@modules) {
396 $$used_modules{$type}{$module} = 1;
399 return ();
402 sub types_not_used {
403 my $self = shift;
404 my $used_modules = \%{$self->{USED_MODULES}};
405 my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
407 my $not_used;
408 foreach my $type (sort(keys(%$allowed_modules))) {
409 foreach my $module (sort(keys(%{$$allowed_modules{$type}}))) {
410 if(!$$used_modules{$type}{$module}) {
411 $$not_used{$module}{$type} = 1;
415 return $not_used;
418 sub types_unlimited_used_in_modules {
419 my $self = shift;
421 my $output = \${$self->{OUTPUT}};
422 my $used_modules = \%{$self->{USED_MODULES}};
423 my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
424 my $allowed_modules_unlimited = \%{$self->{ALLOWED_MODULES_UNLIMITED}};
426 my $used_types;
427 foreach my $type (sort(keys(%$allowed_modules_unlimited))) {
428 my $count = 0;
429 my @modules = ();
430 foreach my $module (sort(keys(%{$$used_modules{$type}}))) {
431 $count++;
432 push @modules, $module;
434 if($count) {
435 foreach my $module (@modules) {
436 $$used_types{$type}{$module} = 1;
440 return $used_types;
443 sub translate_argument {
444 my $self = shift;
445 my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
447 my $argument = shift;
449 return $$translate_argument{$argument};
452 sub all_declared_types {
453 my $self = shift;
454 my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
456 return sort(keys(%$translate_argument));
459 sub found_type {
460 my $self = shift;
461 my $type_found = \%{$self->{TYPE_FOUND}};
463 my $name = shift;
465 $$type_found{$name}++;
468 sub type_found {
469 my $self = shift;
470 my $type_found= \%{$self->{TYPE_FOUND}};
472 my $name = shift;
474 return $$type_found{$name};
477 sub is_allowed_type_format {
478 my $self = shift;
479 my $type_format = \%{$self->{TYPE_FORMAT}};
481 my $module = shift;
482 my $type = shift;
483 my $format = shift;
485 my $formats;
487 if(defined($module) && defined($type)) {
488 local $_;
489 foreach (split(/ & /, $module)) {
490 if(defined($formats)) {
491 $formats .= "|";
492 } else {
493 $formats = "";
495 if(defined($$type_format{$_}{$type})) {
496 $formats .= $$type_format{$_}{$type};
501 if(defined($formats)) {
502 local $_;
503 foreach (split(/\|/, $formats)) {
504 if($_ eq $format) {
505 return 1;
510 return 0;
513 sub all_modules {
514 my $self = shift;
515 my $modules = \%{$self->{MODULES}};
517 return sort(keys(%$modules));
520 sub is_module {
521 my $self = shift;
522 my $modules = \%{$self->{MODULES}};
524 my $name = shift;
526 return $$modules{$name};
529 sub all_functions {
530 my $self = shift;
531 my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
533 return sort(keys(%$function_calling_convention));
536 sub all_functions_in_module {
537 my $self = shift;
538 my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
539 my $function_module = \%{$self->{FUNCTION_MODULE}};
541 my $module = shift;
543 my @names;
544 foreach my $name (keys(%$function_calling_convention)) {
545 if($$function_module{$name} eq $module) {
546 push @names, $name;
550 return sort(@names);
553 sub all_functions_stub {
554 my $self = shift;
555 my $function_stub = \%{$self->{FUNCTION_STUB}};
557 return sort(keys(%$function_stub));
560 sub all_functions_found {
561 my $self = shift;
562 my $function_found = \%{$self->{FUNCTION_FOUND}};
564 return sort(keys(%$function_found));
567 sub function_calling_convention {
568 my $self = shift;
569 my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
571 my $name = shift;
573 return $$function_calling_convention{$name};
576 sub function_external_name {
577 my $self = shift;
578 my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
580 my $name = shift;
582 return $$function_external_name{$name};
585 sub is_function {
586 my $self = shift;
587 my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
589 my $name = shift;
591 return $$function_calling_convention{$name};
594 sub is_shared_function {
595 my $self = shift;
596 my $function_shared = \%{$self->{FUNCTION_SHARED}};
598 my $name = shift;
600 return $$function_shared{$name};
603 sub found_shared_function {
604 my $self = shift;
605 my $function_shared = \%{$self->{FUNCTION_SHARED}};
607 my $name = shift;
609 $$function_shared{$name} = 1;
612 sub function_arguments {
613 my $self = shift;
614 my $function_arguments = \%{$self->{FUNCTION_ARGUMENTS}};
616 my $name = shift;
618 return $$function_arguments{$name};
621 sub function_module {
622 my $self = shift;
623 my $function_module = \%{$self->{FUNCTION_MODULE}};
625 my $name = shift;
627 return $$function_module{$name};
630 sub function_stub {
631 my $self = shift;
632 my $function_stub = \%{$self->{FUNCTION_STUB}};
634 my $name = shift;
636 return $$function_stub{$name};
639 sub found_function {
640 my $self = shift;
641 my $function_found = \%{$self->{FUNCTION_FOUND}};
643 my $name = shift;
645 $$function_found{$name}++;
648 sub function_found {
649 my $self = shift;
650 my $function_found = \%{$self->{FUNCTION_FOUND}};
652 my $name = shift;
654 return $$function_found{$name};