source: branches/samba-3.5.x/pidl/lib/Parse/Pidl/NDR.pm

Last change on this file was 414, checked in by Herwig Bauernfeind, 15 years ago

Samba 3.5.0: Initial import

File size: 29.0 KB
Line 
1###################################################
2# Samba4 NDR info tree generator
3# Copyright tridge@samba.org 2000-2003
4# Copyright tpot@samba.org 2001
5# Copyright jelmer@samba.org 2004-2006
6# released under the GNU GPL
7
8=pod
9
10=head1 NAME
11
12Parse::Pidl::NDR - NDR parsing information generator
13
14=head1 DESCRIPTION
15
16Return a table describing the order in which the parts of an element
17should be parsed
18Possible level types:
19 - POINTER
20 - ARRAY
21 - SUBCONTEXT
22 - SWITCH
23 - DATA
24
25=head1 AUTHOR
26
27Jelmer Vernooij <jelmer@samba.org>
28
29=cut
30
31package Parse::Pidl::NDR;
32
33require Exporter;
34use vars qw($VERSION);
35$VERSION = '0.01';
36@ISA = qw(Exporter);
37@EXPORT = qw(GetPrevLevel GetNextLevel ContainsDeferred ContainsString);
38@EXPORT_OK = qw(GetElementLevelTable ParseElement ValidElement align_type mapToScalar ParseType can_contain_deferred is_charset_array);
39
40use strict;
41use Parse::Pidl qw(warning fatal);
42use Parse::Pidl::Typelist qw(hasType getType expandAlias);
43use Parse::Pidl::Util qw(has_property property_matches);
44
45# Alignment of the built-in scalar types
46my $scalar_alignment = {
47 'void' => 0,
48 'char' => 1,
49 'int8' => 1,
50 'uint8' => 1,
51 'int16' => 2,
52 'uint16' => 2,
53 'int1632' => 3,
54 'uint1632' => 3,
55 'int32' => 4,
56 'uint32' => 4,
57 'hyper' => 8,
58 'double' => 8,
59 'pointer' => 8,
60 'dlong' => 4,
61 'udlong' => 4,
62 'udlongr' => 4,
63 'DATA_BLOB' => 4,
64 'string' => 4,
65 'string_array' => 4, #???
66 'time_t' => 4,
67 'NTTIME' => 4,
68 'NTTIME_1sec' => 4,
69 'NTTIME_hyper' => 8,
70 'WERROR' => 4,
71 'NTSTATUS' => 4,
72 'COMRESULT' => 4,
73 'nbt_string' => 4,
74 'wrepl_nbt_name' => 4,
75 'ipv4address' => 4
76};
77
78sub GetElementLevelTable($$)
79{
80 my ($e, $pointer_default) = @_;
81
82 my $order = [];
83 my $is_deferred = 0;
84 my @bracket_array = ();
85 my @length_is = ();
86 my @size_is = ();
87 my $pointer_idx = 0;
88
89 if (has_property($e, "size_is")) {
90 @size_is = split /,/, has_property($e, "size_is");
91 }
92
93 if (has_property($e, "length_is")) {
94 @length_is = split /,/, has_property($e, "length_is");
95 }
96
97 if (defined($e->{ARRAY_LEN})) {
98 @bracket_array = @{$e->{ARRAY_LEN}};
99 }
100
101 if (has_property($e, "out")) {
102 my $needptrs = 1;
103
104 if (has_property($e, "string")) { $needptrs++; }
105 if ($#bracket_array >= 0) { $needptrs = 0; }
106
107 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
108 }
109
110 # Parse the [][][][] style array stuff
111 for my $i (0 .. $#bracket_array) {
112 my $d = $bracket_array[$#bracket_array - $i];
113 my $size = $d;
114 my $length = $d;
115 my $is_surrounding = 0;
116 my $is_varying = 0;
117 my $is_conformant = 0;
118 my $is_string = 0;
119 my $is_fixed = 0;
120 my $is_inline = 0;
121
122 if ($d eq "*") {
123 $is_conformant = 1;
124 if ($size = shift @size_is) {
125 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
126 $is_string = 1;
127 delete($e->{PROPERTIES}->{string});
128 } else {
129 fatal($e, "Must specify size_is() for conformant array!")
130 }
131
132 if (($length = shift @length_is) or $is_string) {
133 $is_varying = 1;
134 } else {
135 $length = $size;
136 }
137
138 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
139 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
140 $is_surrounding = 1;
141 }
142 }
143
144 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
145 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
146
147 if ($i == 0 and $is_fixed and has_property($e, "string")) {
148 $is_fixed = 0;
149 $is_varying = 1;
150 $is_string = 1;
151 delete($e->{PROPERTIES}->{string});
152 }
153
154 push (@$order, {
155 TYPE => "ARRAY",
156 SIZE_IS => $size,
157 LENGTH_IS => $length,
158 IS_DEFERRED => $is_deferred,
159 IS_SURROUNDING => $is_surrounding,
160 IS_ZERO_TERMINATED => $is_string,
161 IS_VARYING => $is_varying,
162 IS_CONFORMANT => $is_conformant,
163 IS_FIXED => $is_fixed,
164 IS_INLINE => $is_inline
165 });
166 }
167
168 # Next, all the pointers
169 foreach my $i (1..$e->{POINTERS}) {
170 my $level = "EMBEDDED";
171 # Top level "ref" pointers do not have a referrent identifier
172 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
173
174 my $pt;
175 #
176 # Only the first level gets the pointer type from the
177 # pointer property, the others get them from
178 # the pointer_default() interface property
179 #
180 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
181 # (Here they talk about the rightmost pointer, but testing shows
182 # they mean the leftmost pointer.)
183 #
184 # --metze
185 #
186 $pt = pointer_type($e);
187 if ($i > 1) {
188 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
189 $pt = $pointer_default;
190 }
191
192 push (@$order, {
193 TYPE => "POINTER",
194 POINTER_TYPE => $pt,
195 POINTER_INDEX => $pointer_idx,
196 IS_DEFERRED => "$is_deferred",
197 LEVEL => $level
198 });
199
200 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
201 if ($i == 1 and $pt ne "ref" and
202 $e->{PARENT}->{TYPE} eq "FUNCTION" and
203 not has_property($e, "in"));
204
205 $pointer_idx++;
206
207 # everything that follows will be deferred
208 $is_deferred = 1 if ($level ne "TOP");
209
210 my $array_size = shift @size_is;
211 my $array_length;
212 my $is_varying;
213 my $is_conformant;
214 my $is_string = 0;
215 if ($array_size) {
216 $is_conformant = 1;
217 if ($array_length = shift @length_is) {
218 $is_varying = 1;
219 } else {
220 $array_length = $array_size;
221 $is_varying =0;
222 }
223 }
224
225 if (scalar(@size_is) == 0 and has_property($e, "string") and
226 $i == $e->{POINTERS}) {
227 $is_string = 1;
228 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
229 delete($e->{PROPERTIES}->{string});
230 }
231
232 if ($array_size or $is_string) {
233 push (@$order, {
234 TYPE => "ARRAY",
235 SIZE_IS => $array_size,
236 LENGTH_IS => $array_length,
237 IS_DEFERRED => $is_deferred,
238 IS_SURROUNDING => 0,
239 IS_ZERO_TERMINATED => $is_string,
240 IS_VARYING => $is_varying,
241 IS_CONFORMANT => $is_conformant,
242 IS_FIXED => 0,
243 IS_INLINE => 0
244 });
245
246 $is_deferred = 0;
247 }
248 }
249
250 if (defined(has_property($e, "subcontext"))) {
251 my $hdr_size = has_property($e, "subcontext");
252 my $subsize = has_property($e, "subcontext_size");
253 if (not defined($subsize)) {
254 $subsize = -1;
255 }
256
257 push (@$order, {
258 TYPE => "SUBCONTEXT",
259 HEADER_SIZE => $hdr_size,
260 SUBCONTEXT_SIZE => $subsize,
261 IS_DEFERRED => $is_deferred,
262 COMPRESSION => has_property($e, "compression"),
263 });
264 }
265
266 if (my $switch = has_property($e, "switch_is")) {
267 push (@$order, {
268 TYPE => "SWITCH",
269 SWITCH_IS => $switch,
270 IS_DEFERRED => $is_deferred
271 });
272 }
273
274 if (scalar(@size_is) > 0) {
275 fatal($e, "size_is() on non-array element");
276 }
277
278 if (scalar(@length_is) > 0) {
279 fatal($e, "length_is() on non-array element");
280 }
281
282 if (has_property($e, "string")) {
283 fatal($e, "string() attribute on non-array element");
284 }
285
286 push (@$order, {
287 TYPE => "DATA",
288 DATA_TYPE => $e->{TYPE},
289 IS_DEFERRED => $is_deferred,
290 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
291 IS_SURROUNDING => 0 #FIXME
292 });
293
294 my $i = 0;
295 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
296
297 return $order;
298}
299
300sub GetTypedefLevelTable($$$)
301{
302 my ($e, $data, $pointer_default) = @_;
303
304 my $order = [];
305
306 push (@$order, {
307 TYPE => "TYPEDEF"
308 });
309
310 my $i = 0;
311 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
312
313 return $order;
314}
315
316#####################################################################
317# see if a type contains any deferred data
318sub can_contain_deferred($)
319{
320 sub can_contain_deferred($);
321 my ($type) = @_;
322
323 return 1 unless (hasType($type)); # assume the worst
324
325 $type = getType($type);
326
327 return 0 if (Parse::Pidl::Typelist::is_scalar($type));
328
329 return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
330
331 return 0 unless defined($type->{ELEMENTS});
332
333 foreach (@{$type->{ELEMENTS}}) {
334 return 1 if ($_->{POINTERS});
335 return 1 if (can_contain_deferred ($_->{TYPE}));
336 }
337
338 return 0;
339}
340
341sub pointer_type($)
342{
343 my $e = shift;
344
345 return undef unless $e->{POINTERS};
346
347 return "ref" if (has_property($e, "ref"));
348 return "full" if (has_property($e, "ptr"));
349 return "sptr" if (has_property($e, "sptr"));
350 return "unique" if (has_property($e, "unique"));
351 return "relative" if (has_property($e, "relative"));
352 return "ignore" if (has_property($e, "ignore"));
353
354 return undef;
355}
356
357#####################################################################
358# work out the correct alignment for a structure or union
359sub find_largest_alignment($)
360{
361 my $s = shift;
362
363 my $align = 1;
364 for my $e (@{$s->{ELEMENTS}}) {
365 my $a = 1;
366
367 if ($e->{POINTERS}) {
368 # this is a hack for NDR64
369 # the NDR layer translates this into
370 # an alignment of 4 for NDR and 8 for NDR64
371 $a = 5;
372 } elsif (has_property($e, "subcontext")) {
373 $a = 1;
374 } elsif (has_property($e, "transmit_as")) {
375 $a = align_type($e->{PROPERTIES}->{transmit_as});
376 } else {
377 $a = align_type($e->{TYPE});
378 }
379
380 $align = $a if ($align < $a);
381 }
382
383 return $align;
384}
385
386#####################################################################
387# align a type
388sub align_type($)
389{
390 sub align_type($);
391 my ($e) = @_;
392
393 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
394 return $scalar_alignment->{$e->{NAME}};
395 }
396
397 return 0 if ($e eq "EMPTY");
398
399 unless (hasType($e)) {
400 # it must be an external type - all we can do is guess
401 # warning($e, "assuming alignment of unknown type '$e' is 4");
402 return 4;
403 }
404
405 my $dt = getType($e);
406
407 if ($dt->{TYPE} eq "TYPEDEF") {
408 return align_type($dt->{DATA});
409 } elsif ($dt->{TYPE} eq "ENUM") {
410 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
411 } elsif ($dt->{TYPE} eq "BITMAP") {
412 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
413 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
414 # Struct/union without body: assume 4
415 return 4 unless (defined($dt->{ELEMENTS}));
416 return find_largest_alignment($dt);
417 }
418
419 die("Unknown data type type $dt->{TYPE}");
420}
421
422sub ParseElement($$)
423{
424 my ($e, $pointer_default) = @_;
425
426 $e->{TYPE} = expandAlias($e->{TYPE});
427
428 if (ref($e->{TYPE}) eq "HASH") {
429 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default);
430 }
431
432 return {
433 NAME => $e->{NAME},
434 TYPE => $e->{TYPE},
435 PROPERTIES => $e->{PROPERTIES},
436 LEVELS => GetElementLevelTable($e, $pointer_default),
437 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
438 ALIGN => align_type($e->{TYPE}),
439 ORIGINAL => $e
440 };
441}
442
443sub ParseStruct($$)
444{
445 my ($struct, $pointer_default) = @_;
446 my @elements = ();
447 my $surrounding = undef;
448
449 return {
450 TYPE => "STRUCT",
451 NAME => $struct->{NAME},
452 SURROUNDING_ELEMENT => undef,
453 ELEMENTS => undef,
454 PROPERTIES => $struct->{PROPERTIES},
455 ORIGINAL => $struct,
456 ALIGN => undef
457 } unless defined($struct->{ELEMENTS});
458
459 CheckPointerTypes($struct, $pointer_default);
460
461 foreach my $x (@{$struct->{ELEMENTS}})
462 {
463 my $e = ParseElement($x, $pointer_default);
464 if ($x != $struct->{ELEMENTS}[-1] and
465 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
466 fatal($x, "conformant member not at end of struct");
467 }
468 push @elements, $e;
469 }
470
471 my $e = $elements[-1];
472 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
473 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
474 $surrounding = $e;
475 }
476
477 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
478 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
479 $surrounding = $struct->{ELEMENTS}[-1];
480 }
481
482 my $align = undef;
483 if ($struct->{NAME}) {
484 $align = align_type($struct->{NAME});
485 }
486
487 return {
488 TYPE => "STRUCT",
489 NAME => $struct->{NAME},
490 SURROUNDING_ELEMENT => $surrounding,
491 ELEMENTS => \@elements,
492 PROPERTIES => $struct->{PROPERTIES},
493 ORIGINAL => $struct,
494 ALIGN => $align
495 };
496}
497
498sub ParseUnion($$)
499{
500 my ($e, $pointer_default) = @_;
501 my @elements = ();
502 my $hasdefault = 0;
503 my $switch_type = has_property($e, "switch_type");
504 unless (defined($switch_type)) { $switch_type = "uint32"; }
505 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
506
507 return {
508 TYPE => "UNION",
509 NAME => $e->{NAME},
510 SWITCH_TYPE => $switch_type,
511 ELEMENTS => undef,
512 PROPERTIES => $e->{PROPERTIES},
513 HAS_DEFAULT => $hasdefault,
514 ORIGINAL => $e,
515 ALIGN => undef
516 } unless defined($e->{ELEMENTS});
517
518 CheckPointerTypes($e, $pointer_default);
519
520 foreach my $x (@{$e->{ELEMENTS}})
521 {
522 my $t;
523 if ($x->{TYPE} eq "EMPTY") {
524 $t = { TYPE => "EMPTY" };
525 } else {
526 $t = ParseElement($x, $pointer_default);
527 }
528 if (has_property($x, "default")) {
529 $t->{CASE} = "default";
530 $hasdefault = 1;
531 } elsif (defined($x->{PROPERTIES}->{case})) {
532 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
533 } else {
534 die("Union element $x->{NAME} has neither default nor case property");
535 }
536 push @elements, $t;
537 }
538
539 my $align = undef;
540 if ($e->{NAME}) {
541 $align = align_type($e->{NAME});
542 }
543
544 return {
545 TYPE => "UNION",
546 NAME => $e->{NAME},
547 SWITCH_TYPE => $switch_type,
548 ELEMENTS => \@elements,
549 PROPERTIES => $e->{PROPERTIES},
550 HAS_DEFAULT => $hasdefault,
551 ORIGINAL => $e,
552 ALIGN => $align
553 };
554}
555
556sub ParseEnum($$)
557{
558 my ($e, $pointer_default) = @_;
559
560 return {
561 TYPE => "ENUM",
562 NAME => $e->{NAME},
563 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
564 ELEMENTS => $e->{ELEMENTS},
565 PROPERTIES => $e->{PROPERTIES},
566 ORIGINAL => $e
567 };
568}
569
570sub ParseBitmap($$)
571{
572 my ($e, $pointer_default) = @_;
573
574 return {
575 TYPE => "BITMAP",
576 NAME => $e->{NAME},
577 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
578 ELEMENTS => $e->{ELEMENTS},
579 PROPERTIES => $e->{PROPERTIES},
580 ORIGINAL => $e
581 };
582}
583
584sub ParseType($$)
585{
586 my ($d, $pointer_default) = @_;
587
588 my $data = {
589 STRUCT => \&ParseStruct,
590 UNION => \&ParseUnion,
591 ENUM => \&ParseEnum,
592 BITMAP => \&ParseBitmap,
593 TYPEDEF => \&ParseTypedef,
594 }->{$d->{TYPE}}->($d, $pointer_default);
595
596 return $data;
597}
598
599sub ParseTypedef($$)
600{
601 my ($d, $pointer_default) = @_;
602
603 if (defined($d->{DATA}->{PROPERTIES}) && !defined($d->{PROPERTIES})) {
604 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
605 }
606
607 my $data = ParseType($d->{DATA}, $pointer_default);
608 $data->{ALIGN} = align_type($d->{NAME});
609
610 return {
611 NAME => $d->{NAME},
612 TYPE => $d->{TYPE},
613 PROPERTIES => $d->{PROPERTIES},
614 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default),
615 DATA => $data,
616 ORIGINAL => $d
617 };
618}
619
620sub ParseConst($$)
621{
622 my ($ndr,$d) = @_;
623
624 return $d;
625}
626
627sub ParseFunction($$$)
628{
629 my ($ndr,$d,$opnum) = @_;
630 my @elements = ();
631 my $rettype = undef;
632 my $thisopnum = undef;
633
634 CheckPointerTypes($d, "ref");
635
636 if (not defined($d->{PROPERTIES}{noopnum})) {
637 $thisopnum = ${$opnum};
638 ${$opnum}++;
639 }
640
641 foreach my $x (@{$d->{ELEMENTS}}) {
642 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default});
643 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
644 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
645
646 push (@elements, $e);
647 }
648
649 if ($d->{RETURN_TYPE} ne "void") {
650 $rettype = expandAlias($d->{RETURN_TYPE});
651 }
652
653 my $async = 0;
654 if (has_property($d, "async")) { $async = 1; }
655
656 return {
657 NAME => $d->{NAME},
658 TYPE => "FUNCTION",
659 OPNUM => $thisopnum,
660 ASYNC => $async,
661 RETURN_TYPE => $rettype,
662 PROPERTIES => $d->{PROPERTIES},
663 ELEMENTS => \@elements,
664 ORIGINAL => $d
665 };
666}
667
668sub CheckPointerTypes($$)
669{
670 my ($s,$default) = @_;
671
672 return unless defined($s->{ELEMENTS});
673
674 foreach my $e (@{$s->{ELEMENTS}}) {
675 if ($e->{POINTERS} and not defined(pointer_type($e))) {
676 $e->{PROPERTIES}->{$default} = '1';
677 }
678 }
679}
680
681sub FindNestedTypes($$)
682{
683 sub FindNestedTypes($$);
684 my ($l, $t) = @_;
685
686 return unless defined($t->{ELEMENTS});
687 return if ($t->{TYPE} eq "ENUM");
688 return if ($t->{TYPE} eq "BITMAP");
689
690 foreach (@{$t->{ELEMENTS}}) {
691 if (ref($_->{TYPE}) eq "HASH") {
692 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
693 FindNestedTypes($l, $_->{TYPE});
694 }
695 }
696}
697
698sub ParseInterface($)
699{
700 my $idl = shift;
701 my @types = ();
702 my @consts = ();
703 my @functions = ();
704 my @endpoints;
705 my $opnum = 0;
706 my $version;
707
708 if (not has_property($idl, "pointer_default")) {
709 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
710 # and "unique" in Microsoft Extensions mode (default)
711 $idl->{PROPERTIES}->{pointer_default} = "unique";
712 }
713
714 foreach my $d (@{$idl->{DATA}}) {
715 if ($d->{TYPE} eq "FUNCTION") {
716 push (@functions, ParseFunction($idl, $d, \$opnum));
717 } elsif ($d->{TYPE} eq "CONST") {
718 push (@consts, ParseConst($idl, $d));
719 } else {
720 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}));
721 FindNestedTypes(\@types, $d);
722 }
723 }
724
725 $version = "0.0";
726
727 if(defined $idl->{PROPERTIES}->{version}) {
728 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
729 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
730 $version = $idl->{PROPERTIES}->{version};
731 } else {
732 $version = $if_version[1] << 16 | $if_version[0];
733 }
734 }
735
736 # If no endpoint is set, default to the interface name as a named pipe
737 if (!defined $idl->{PROPERTIES}->{endpoint}) {
738 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
739 } else {
740 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
741 }
742
743 return {
744 NAME => $idl->{NAME},
745 UUID => lc(has_property($idl, "uuid")),
746 VERSION => $version,
747 TYPE => "INTERFACE",
748 PROPERTIES => $idl->{PROPERTIES},
749 FUNCTIONS => \@functions,
750 CONSTS => \@consts,
751 TYPES => \@types,
752 ENDPOINTS => \@endpoints
753 };
754}
755
756# Convert a IDL tree to a NDR tree
757# Gives a result tree describing all that's necessary for easily generating
758# NDR parsers / generators
759sub Parse($)
760{
761 my $idl = shift;
762
763 return undef unless (defined($idl));
764
765 Parse::Pidl::NDR::Validate($idl);
766
767 my @ndr = ();
768
769 foreach (@{$idl}) {
770 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
771 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
772 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
773 }
774
775 return \@ndr;
776}
777
778sub GetNextLevel($$)
779{
780 my $e = shift;
781 my $fl = shift;
782
783 my $seen = 0;
784
785 foreach my $l (@{$e->{LEVELS}}) {
786 return $l if ($seen);
787 ($seen = 1) if ($l == $fl);
788 }
789
790 return undef;
791}
792
793sub GetPrevLevel($$)
794{
795 my ($e,$fl) = @_;
796 my $prev = undef;
797
798 foreach my $l (@{$e->{LEVELS}}) {
799 (return $prev) if ($l == $fl);
800 $prev = $l;
801 }
802
803 return undef;
804}
805
806sub ContainsString($)
807{
808 my ($e) = @_;
809
810 foreach my $l (@{$e->{LEVELS}}) {
811 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
812 }
813
814 return 0;
815}
816
817sub ContainsDeferred($$)
818{
819 my ($e,$l) = @_;
820
821 return 1 if ($l->{CONTAINS_DEFERRED});
822
823 while ($l = GetNextLevel($e,$l))
824 {
825 return 1 if ($l->{IS_DEFERRED});
826 return 1 if ($l->{CONTAINS_DEFERRED});
827 }
828
829 return 0;
830}
831
832sub el_name($)
833{
834 my $e = shift;
835 my $name = "<ANONYMOUS>";
836
837 $name = $e->{NAME} if defined($e->{NAME});
838
839 if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
840 return "$e->{PARENT}->{NAME}.$name";
841 }
842
843 if (defined($e->{PARENT}) and
844 defined($e->{PARENT}->{PARENT}) and
845 defined($e->{PARENT}->{PARENT}->{NAME})) {
846 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
847 }
848
849 return $name;
850}
851
852###################################
853# find a sibling var in a structure
854sub find_sibling($$)
855{
856 my($e,$name) = @_;
857 my($fn) = $e->{PARENT};
858
859 if ($name =~ /\*(.*)/) {
860 $name = $1;
861 }
862
863 for my $e2 (@{$fn->{ELEMENTS}}) {
864 return $e2 if ($e2->{NAME} eq $name);
865 }
866
867 return undef;
868}
869
870my %property_list = (
871 # interface
872 "helpstring" => ["INTERFACE", "FUNCTION"],
873 "version" => ["INTERFACE"],
874 "uuid" => ["INTERFACE"],
875 "endpoint" => ["INTERFACE"],
876 "pointer_default" => ["INTERFACE"],
877 "helper" => ["INTERFACE"],
878 "pyhelper" => ["INTERFACE"],
879 "authservice" => ["INTERFACE"],
880 "restricted" => ["INTERFACE"],
881
882 # dcom
883 "object" => ["INTERFACE"],
884 "local" => ["INTERFACE", "FUNCTION"],
885 "iid_is" => ["ELEMENT"],
886 "call_as" => ["FUNCTION"],
887 "idempotent" => ["FUNCTION"],
888
889 # function
890 "noopnum" => ["FUNCTION"],
891 "in" => ["ELEMENT"],
892 "out" => ["ELEMENT"],
893 "async" => ["FUNCTION"],
894
895 # pointer
896 "ref" => ["ELEMENT"],
897 "ptr" => ["ELEMENT"],
898 "unique" => ["ELEMENT"],
899 "ignore" => ["ELEMENT"],
900 "relative" => ["ELEMENT"],
901 "null_is_ffffffff" => ["ELEMENT"],
902 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
903
904 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
905 "value" => ["ELEMENT"],
906 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
907
908 # generic
909 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
910 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
911 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
912 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
913 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
914 "todo" => ["FUNCTION"],
915
916 # union
917 "switch_is" => ["ELEMENT"],
918 "switch_type" => ["ELEMENT", "UNION"],
919 "nodiscriminant" => ["UNION"],
920 "case" => ["ELEMENT"],
921 "default" => ["ELEMENT"],
922
923 "represent_as" => ["ELEMENT"],
924 "transmit_as" => ["ELEMENT"],
925
926 # subcontext
927 "subcontext" => ["ELEMENT"],
928 "subcontext_size" => ["ELEMENT"],
929 "compression" => ["ELEMENT"],
930
931 # enum
932 "enum8bit" => ["ENUM"],
933 "enum16bit" => ["ENUM"],
934 "v1_enum" => ["ENUM"],
935
936 # bitmap
937 "bitmap8bit" => ["BITMAP"],
938 "bitmap16bit" => ["BITMAP"],
939 "bitmap32bit" => ["BITMAP"],
940 "bitmap64bit" => ["BITMAP"],
941
942 # array
943 "range" => ["ELEMENT", "PIPE"],
944 "size_is" => ["ELEMENT"],
945 "string" => ["ELEMENT"],
946 "noheader" => ["ELEMENT"],
947 "charset" => ["ELEMENT"],
948 "length_is" => ["ELEMENT"],
949);
950
951#####################################################################
952# check for unknown properties
953sub ValidProperties($$)
954{
955 my ($e,$t) = @_;
956
957 return unless defined $e->{PROPERTIES};
958
959 foreach my $key (keys %{$e->{PROPERTIES}}) {
960 warning($e, el_name($e) . ": unknown property '$key'")
961 unless defined($property_list{$key});
962
963 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
964 unless grep(/^$t$/, @{$property_list{$key}});
965 }
966}
967
968sub mapToScalar($)
969{
970 sub mapToScalar($);
971 my $t = shift;
972 return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
973 my $ti = getType($t);
974
975 if (not defined ($ti)) {
976 return undef;
977 } elsif ($ti->{TYPE} eq "TYPEDEF") {
978 return mapToScalar($ti->{DATA});
979 } elsif ($ti->{TYPE} eq "ENUM") {
980 return Parse::Pidl::Typelist::enum_type_fn($ti);
981 } elsif ($ti->{TYPE} eq "BITMAP") {
982 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
983 }
984
985 return undef;
986}
987
988#####################################################################
989# validate an element
990sub ValidElement($)
991{
992 my $e = shift;
993
994 ValidProperties($e,"ELEMENT");
995
996 # Check whether switches are used correctly.
997 if (my $switch = has_property($e, "switch_is")) {
998 my $e2 = find_sibling($e, $switch);
999 my $type = getType($e->{TYPE});
1000
1001 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
1002 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1003 }
1004
1005 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
1006 my $discriminator_type = has_property($type->{DATA}, "switch_type");
1007 $discriminator_type = "uint32" unless defined ($discriminator_type);
1008
1009 my $t1 = mapToScalar($discriminator_type);
1010
1011 if (not defined($t1)) {
1012 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1013 }
1014
1015 my $t2 = mapToScalar($e2->{TYPE});
1016 if (not defined($t2)) {
1017 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1018 }
1019
1020 if ($t1 ne $t2) {
1021 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1022 }
1023 }
1024 }
1025
1026 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1027 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1028 }
1029
1030 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1031 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1032 }
1033
1034 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1035 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1036 }
1037
1038 if (has_property($e, "represent_as") and has_property($e, "value")) {
1039 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1040 }
1041
1042 if (has_property($e, "subcontext")) {
1043 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1044 }
1045
1046 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1047 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1048 }
1049
1050 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1051 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1052 }
1053
1054 if (!$e->{POINTERS} && (
1055 has_property($e, "ptr") or
1056 has_property($e, "unique") or
1057 has_property($e, "relative") or
1058 has_property($e, "ref"))) {
1059 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
1060 }
1061}
1062
1063#####################################################################
1064# validate an enum
1065sub ValidEnum($)
1066{
1067 my ($enum) = @_;
1068
1069 ValidProperties($enum, "ENUM");
1070}
1071
1072#####################################################################
1073# validate a bitmap
1074sub ValidBitmap($)
1075{
1076 my ($bitmap) = @_;
1077
1078 ValidProperties($bitmap, "BITMAP");
1079}
1080
1081#####################################################################
1082# validate a struct
1083sub ValidStruct($)
1084{
1085 my($struct) = shift;
1086
1087 ValidProperties($struct, "STRUCT");
1088
1089 return unless defined($struct->{ELEMENTS});
1090
1091 foreach my $e (@{$struct->{ELEMENTS}}) {
1092 $e->{PARENT} = $struct;
1093 ValidElement($e);
1094 }
1095}
1096
1097#####################################################################
1098# parse a union
1099sub ValidUnion($)
1100{
1101 my($union) = shift;
1102
1103 ValidProperties($union,"UNION");
1104
1105 if (has_property($union->{PARENT}, "nodiscriminant") and
1106 has_property($union->{PARENT}, "switch_type")) {
1107 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1108 }
1109
1110 return unless defined($union->{ELEMENTS});
1111
1112 foreach my $e (@{$union->{ELEMENTS}}) {
1113 $e->{PARENT} = $union;
1114
1115 if (defined($e->{PROPERTIES}->{default}) and
1116 defined($e->{PROPERTIES}->{case})) {
1117 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1118 }
1119
1120 unless (defined ($e->{PROPERTIES}->{default}) or
1121 defined ($e->{PROPERTIES}->{case})) {
1122 fatal($e, "Union member $e->{NAME} must have default or case property");
1123 }
1124
1125 if (has_property($e, "ref")) {
1126 fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1127 }
1128
1129
1130 ValidElement($e);
1131 }
1132}
1133
1134#####################################################################
1135# validate a pipe
1136sub ValidPipe($)
1137{
1138 my ($pipe) = @_;
1139 my $data = $pipe->{DATA};
1140
1141 ValidProperties($pipe, "PIPE");
1142
1143 fatal($pipe, $pipe->{NAME} . ": 'pipe' is not yet supported by pidl");
1144}
1145
1146#####################################################################
1147# parse a typedef
1148sub ValidTypedef($)
1149{
1150 my($typedef) = shift;
1151 my $data = $typedef->{DATA};
1152
1153 ValidProperties($typedef, "TYPEDEF");
1154
1155 $data->{PARENT} = $typedef;
1156
1157 $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1158 $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1159
1160 ValidType($data) if (ref($data) eq "HASH");
1161}
1162
1163#####################################################################
1164# validate a function
1165sub ValidFunction($)
1166{
1167 my($fn) = shift;
1168
1169 ValidProperties($fn,"FUNCTION");
1170
1171 foreach my $e (@{$fn->{ELEMENTS}}) {
1172 $e->{PARENT} = $fn;
1173 if (has_property($e, "ref") && !$e->{POINTERS}) {
1174 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1175 }
1176 ValidElement($e);
1177 }
1178}
1179
1180#####################################################################
1181# validate a type
1182sub ValidType($)
1183{
1184 my ($t) = @_;
1185
1186 {
1187 TYPEDEF => \&ValidTypedef,
1188 STRUCT => \&ValidStruct,
1189 UNION => \&ValidUnion,
1190 ENUM => \&ValidEnum,
1191 BITMAP => \&ValidBitmap,
1192 PIPE => \&ValidPipe
1193 }->{$t->{TYPE}}->($t);
1194}
1195
1196#####################################################################
1197# parse the interface definitions
1198sub ValidInterface($)
1199{
1200 my($interface) = shift;
1201 my($data) = $interface->{DATA};
1202
1203 if (has_property($interface, "helper")) {
1204 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1205 }
1206
1207 ValidProperties($interface,"INTERFACE");
1208
1209 if (has_property($interface, "pointer_default")) {
1210 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1211 ("ref", "unique", "ptr"))) {
1212 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1213 }
1214 }
1215
1216 if (has_property($interface, "object")) {
1217 if (has_property($interface, "version") &&
1218 $interface->{PROPERTIES}->{version} != 0) {
1219 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1220 }
1221
1222 if (!defined($interface->{BASE}) &&
1223 not ($interface->{NAME} eq "IUnknown")) {
1224 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1225 }
1226 }
1227
1228 foreach my $d (@{$data}) {
1229 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1230 ($d->{TYPE} eq "TYPEDEF" or
1231 $d->{TYPE} eq "STRUCT" or
1232 $d->{TYPE} eq "UNION" or
1233 $d->{TYPE} eq "ENUM" or
1234 $d->{TYPE} eq "BITMAP" or
1235 $d->{TYPE} eq "PIPE") && ValidType($d);
1236 }
1237
1238}
1239
1240#####################################################################
1241# Validate an IDL structure
1242sub Validate($)
1243{
1244 my($idl) = shift;
1245
1246 foreach my $x (@{$idl}) {
1247 ($x->{TYPE} eq "INTERFACE") &&
1248 ValidInterface($x);
1249 ($x->{TYPE} eq "IMPORTLIB") &&
1250 fatal($x, "importlib() not supported");
1251 }
1252}
1253
1254sub is_charset_array($$)
1255{
1256 my ($e,$l) = @_;
1257
1258 return 0 if ($l->{TYPE} ne "ARRAY");
1259
1260 my $nl = GetNextLevel($e,$l);
1261
1262 return 0 unless ($nl->{TYPE} eq "DATA");
1263
1264 return has_property($e, "charset");
1265}
1266
1267
1268
12691;
Note: See TracBrowser for help on using the repository browser.