Release 20021031.
[wine/multimedia.git] / tools / winapi_check / winapi_local.pm
blob6e6e1f5a1d6d01e8ca213716b3f2b6f5a76c6f1e
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
19 package winapi_local;
21 use strict;
23 use nativeapi qw($nativeapi);
24 use options qw($options);
25 use output qw($output);
26 use winapi qw($win16api $win32api @winapis);
28 sub check_function {
29 my $function = shift;
31 my $return_type = $function->return_type;
32 my $calling_convention = $function->calling_convention;
33 my $calling_convention16 = $function->calling_convention16;
34 my $calling_convention32 = $function->calling_convention32;
35 my $internal_name = $function->internal_name;
36 my $external_name16 = $function->external_name16;
37 my $external_name32 = $function->external_name32;
38 my $module16 = $function->module16;
39 my $module32 = $function->module32;
40 my $refargument_types = $function->argument_types;
42 if(!defined($refargument_types)) {
43 return;
46 if($options->win16 && $options->report_module($module16)) {
47 _check_function($return_type,
48 $calling_convention, $external_name16,
49 $internal_name, $refargument_types,
50 $win16api);
53 if($options->win32 && $options->report_module($module32)) {
54 _check_function($return_type,
55 $calling_convention, $external_name32,
56 $internal_name, $refargument_types,
57 $win32api);
61 sub _check_function {
62 my $return_type = shift;
63 my $calling_convention = shift;
64 my $external_name = shift;
65 my $internal_name = shift;
66 my $refargument_types = shift;
67 my @argument_types = @$refargument_types;
68 my $winapi = shift;
70 my $module = $winapi->function_internal_module($internal_name);
72 if($winapi->name eq "win16") {
73 if($winapi->is_function_stub_in_module($module, $internal_name)) {
74 if($options->implemented) {
75 $output->write("function implemented but declared as stub in .spec file\n");
77 return;
78 } elsif($winapi->is_function_stub_in_module($module, $internal_name)) {
79 if($options->implemented_win32) {
80 $output->write("32-bit variant of function implemented but declared as stub in .spec file\n");
82 return;
84 } elsif($winapi->is_function_stub_in_module($module, $internal_name)) {
85 if($options->implemented) {
86 $output->write("function implemented but declared as stub in .spec file\n");
88 return;
91 my $forbidden_return_type = 0;
92 my $implemented_return_kind;
93 $winapi->type_used_in_module($return_type,$module);
94 if(!defined($implemented_return_kind = $winapi->translate_argument($return_type))) {
95 $winapi->declare_argument($return_type, "unknown");
96 if($return_type ne "") {
97 $output->write("no translation defined: " . $return_type . "\n");
99 } elsif(!$winapi->is_allowed_kind($implemented_return_kind) ||
100 !$winapi->is_allowed_type_in_module($return_type, $module))
102 $forbidden_return_type = 1;
103 $winapi->allow_kind($implemented_return_kind);
104 $winapi->allow_type_in_module($return_type, $module);
105 if($options->report_argument_forbidden($return_type)) {
106 $output->write("return type is forbidden: $return_type ($implemented_return_kind)\n");
110 my $segmented = 0;
111 if(defined($implemented_return_kind) && $implemented_return_kind =~ /^segptr|segstr$/) {
112 $segmented = 1;
115 my $implemented_calling_convention;
116 if($winapi->name eq "win16") {
117 if($calling_convention =~ /^__cdecl$/) {
118 $implemented_calling_convention = "cdecl";
119 } elsif($calling_convention =~ /^VFWAPIV|WINAPIV$/) {
120 $implemented_calling_convention = "varargs";
121 } elsif($calling_convention =~ /^__stdcall|VFWAPI|WINAPI|CALLBACK$/) {
122 if($implemented_return_kind =~ /^s_word|word|void$/) {
123 $implemented_calling_convention = "pascal16";
124 } else {
125 $implemented_calling_convention = "pascal";
127 } elsif($calling_convention =~ /^__asm$/) {
128 $implemented_calling_convention = "asm";
129 } else {
130 $implemented_calling_convention = "cdecl";
132 } elsif($winapi->name eq "win32") {
133 if($calling_convention =~ /^__cdecl$/) {
134 $implemented_calling_convention = "cdecl";
135 } elsif($calling_convention =~ /^VFWAPIV|WINAPIV$/) {
136 $implemented_calling_convention = "varargs";
137 } elsif($calling_convention =~ /^__stdcall|VFWAPI|WINAPI|CALLBACK$/) {
138 if(defined($implemented_return_kind) && $implemented_return_kind =~ /^longlong$/) {
139 $implemented_calling_convention = "stdcall"; # FIXME: Check entry flags
140 } else {
141 $implemented_calling_convention = "stdcall";
143 } elsif($calling_convention =~ /^__asm$/) {
144 $implemented_calling_convention = "asm";
145 } else {
146 $implemented_calling_convention = "cdecl";
150 my $declared_calling_convention = $winapi->function_internal_calling_convention($internal_name);
151 my @declared_argument_kinds = split(/\s+/, $winapi->function_internal_arguments($internal_name));
153 if($implemented_calling_convention ne $declared_calling_convention &&
154 $implemented_calling_convention ne "asm" &&
155 !($declared_calling_convention =~ /^pascal/ && $forbidden_return_type) &&
156 !($implemented_calling_convention =~ /^cdecl|varargs$/ && $declared_calling_convention =~ /^cdecl|varargs$/))
158 if($options->calling_convention && (
159 ($options->calling_convention_win16 && $winapi->name eq "win16") ||
160 ($options->calling_convention_win32 && $winapi->name eq "win32")) &&
161 !$nativeapi->is_function($internal_name))
163 $output->write("calling convention mismatch: $implemented_calling_convention != $declared_calling_convention\n");
167 if($declared_calling_convention eq "varargs") {
168 if($#argument_types != -1 && $argument_types[$#argument_types] eq "...") {
169 pop @argument_types;
170 } else {
171 $output->write("function not implemented as vararg\n");
173 } elsif($#argument_types != -1 && $argument_types[$#argument_types] eq "...") {
174 if($#argument_types == 0 || $winapi->name eq "win16") {
175 pop @argument_types;
176 } else {
177 $output->write("function not declared as vararg\n");
181 if($#argument_types != -1 && $argument_types[$#argument_types] eq "CONTEXT *" &&
182 $internal_name !~ /^(Get|Set)ThreadContext$/) # FIXME: Kludge
184 $#argument_types--;
187 if($internal_name =~ /^NTDLL__ftol|NTDLL__CIpow$/) { # FIXME: Kludge
188 # ignore
189 } else {
190 my $n = 0;
191 my @argument_kinds = map {
192 my $type = $_;
193 my $kind = "unknown";
194 $winapi->type_used_in_module($type,$module);
195 if($type eq "CONTEXT86 *") {
196 $kind = "context86";
197 } elsif(!defined($kind = $winapi->translate_argument($type))) {
198 $winapi->declare_argument($type, "unknown");
199 $output->write("no translation defined: " . $type . "\n");
200 } elsif(!$winapi->is_allowed_kind($kind) ||
201 !$winapi->is_allowed_type_in_module($type, $module))
203 $winapi->allow_kind($kind);
204 $winapi->allow_type_in_module($type, $module);
205 if($options->report_argument_forbidden($type)) {
206 $output->write("argument " . ($n + 1) . " type is forbidden: " . $type . " (" . $kind . ")\n");
210 # FIXME: Kludge
211 if(defined($kind) && $kind eq "struct16") {
212 $n+=4;
213 ("long", "long", "long", "long");
214 } elsif(defined($kind) && $kind eq "longlong") {
215 $n+=2;
216 ("long", "long");
217 } else {
218 $n++;
219 $kind;
221 } @argument_types;
223 for my $n (0..$#argument_kinds) {
224 if(!defined($argument_kinds[$n]) || !defined($declared_argument_kinds[$n])) { next; }
226 if($argument_kinds[$n] =~ /^segptr|segstr$/ ||
227 $declared_argument_kinds[$n] =~ /^segptr|segstr$/)
229 $segmented = 1;
232 # FIXME: Kludge
233 if(!defined($argument_types[$n])) {
234 $argument_types[$n] = "";
237 if($argument_kinds[$n] eq "context86") {
238 # Nothing
239 } elsif(!$winapi->is_allowed_kind($argument_kinds[$n]) ||
240 !$winapi->is_allowed_type_in_module($argument_types[$n], $module))
242 $winapi->allow_kind($argument_kinds[$n]);
243 $winapi->allow_type_in_module($argument_types[$n],, $module);
244 if($options->report_argument_forbidden($argument_types[$n])) {
245 $output->write("argument " . ($n + 1) . " type is forbidden: " .
246 "$argument_types[$n] ($argument_kinds[$n])\n");
248 } elsif($argument_kinds[$n] ne $declared_argument_kinds[$n]) {
249 if($options->report_argument_kind($argument_kinds[$n]) ||
250 $options->report_argument_kind($declared_argument_kinds[$n]))
252 $output->write("argument " . ($n + 1) . " type mismatch: " .
253 $argument_types[$n] . " ($argument_kinds[$n]) != " .
254 $declared_argument_kinds[$n] . "\n");
259 if($#argument_kinds != $#declared_argument_kinds &&
260 $implemented_calling_convention ne "asm")
262 if($options->argument_count) {
263 $output->write("argument count differs: " .
264 ($#argument_types + 1) . " != " .
265 ($#declared_argument_kinds + 1) . "\n");
271 if($segmented && $options->shared_segmented && $winapi->is_shared_internal_function($internal_name)) {
272 $output->write("function using segmented pointers shared between Win16 och Win32\n");
276 sub check_statements {
277 my $functions = shift;
278 my $function = shift;
280 my $module16 = $function->module16;
281 my $module32 = $function->module32;
283 if($options->win16 && $options->report_module($module16)) {
284 _check_statements($win16api, $functions, $function);
287 if($options->win32 && $options->report_module($module32)) {
288 _check_statements($win16api, $functions, $function);
292 sub _check_statements {
293 my $winapi = shift;
294 my $functions = shift;
295 my $function = shift;
297 my $module = $function->module;
298 my $internal_name = $function->internal_name;
300 my $first_debug_message = 1;
301 local $_ = $function->statements;
302 while(defined($_)) {
303 if(s/(\w+)\s*(?:\(\s*(\w+)\s*\))?\s*\(\s*((?:\"[^\"]*\"|\([^\)]*\)|[^\)])*?)\s*\)//) {
304 my $called_name = $1;
305 my $channel = $2;
306 my $called_arguments = $3;
307 if($called_name =~ /^if|for|while|switch|sizeof$/) {
308 # Nothing
309 } elsif($called_name =~ /^ERR|FIXME|MSG|TRACE|WARN$/) {
310 if($first_debug_message && $called_name =~ /^FIXME|TRACE$/) {
311 $first_debug_message = 0;
312 if($called_arguments =~ /^\"\((.*?)\)(.*?)\"\s*,\s*(.*?)$/) {
313 my $formating = $1;
314 my $extra = $2;
315 my $arguments = $3;
317 my $format;
318 my $argument;
319 my $n = 0;
320 while($formating && ($formating =~ s/^([^,]*),?//, $format = $1, $format =~ s/^\s*(.*?)\s*$/$1/) &&
321 $arguments && ($arguments =~ s/^([^,]*),?//, $argument = $1, $argument =~ s/^\s*(.*?)\s*$/$1/))
323 my $type = @{$function->argument_types}[$n];
324 my $name = @{$function->argument_names}[$n];
326 $n++;
328 if(!defined($type)) { last; }
330 $format =~ s/^\w+\s*[:=]?\s*//;
331 $format =~ s/\s*\{[^\{\}]*\}$//;
332 $format =~ s/\s*\[[^\[\]]*\]$//;
333 $format =~ s/^\'(.*?)\'$/$1/;
334 $format =~ s/^\\\"(.*?)\\\"$/$1/;
336 if($options->debug_messages) {
337 if($argument !~ /$name/) {
338 $output->write("$called_name: argument $n is wrong ($name != '$argument')\n");
339 } elsif(!$winapi->is_allowed_type_format($module, $type, $format)) {
340 $output->write("$called_name: argument $n ($type $name) has illegal format ($format)\n");
345 if($options->debug_messages) {
346 my $count = $#{$function->argument_types} + 1;
347 if($n != $count) {
348 $output->write("$called_name: argument count mismatch ($n != $count)\n");
353 } elsif($options->cross_call) {
354 $$functions{$internal_name}->function_called($called_name);
355 if(!defined($$functions{$called_name})) {
356 $$functions{$called_name} = 'winapi_function'->new;
358 $$functions{$called_name}->function_called_by($internal_name);
360 } else {
361 undef $_;
366 sub check_file {
367 my $file = shift;
368 my $functions = shift;
370 if($options->cross_call) {
371 my @names = sort(keys(%$functions));
372 for my $name (@names) {
373 my @called_names = $$functions{$name}->called_function_names;
374 my @called_by_names = $$functions{$name}->called_by_function_names;
375 my $module = $$functions{$name}->module;
377 if($options->cross_call_win32_win16) {
378 my $module16 = $$functions{$name}->module16;
379 my $module32 = $$functions{$name}->module32;
381 if($#called_names >= 0 && (defined($module16) || defined($module32)) ) {
382 for my $called_name (@called_names) {
383 my $called_module16 = $$functions{$called_name}->module16;
384 my $called_module32 = $$functions{$called_name}->module32;
385 if(defined($module32) &&
386 defined($called_module16) && !defined($called_module32) &&
387 $name ne $called_name)
389 $output->write("$file: $module: $name: illegal call to $called_name (Win32 -> Win16)\n");
395 if($options->cross_call_unicode_ascii) {
396 if($name =~ /W$/) {
397 for my $called_name (@called_names) {
398 if($called_name =~ /A$/) {
399 $output->write("$file: $module: $name: illegal call to $called_name (Unicode -> ASCII)\n");