Initial source import
[drsuapi_dissector.git] / pidl / lib / Parse / Pidl / NDR.pm
blobd326f6704097944daacdefe300b74df6292f90d6
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
8 =pod
10 =head1 NAME
12 Parse::Pidl::NDR - NDR parsing information generator
14 =head1 DESCRIPTION
16 Return a table describing the order in which the parts of an element
17 should be parsed
18 Possible level types:
19 - POINTER
20 - ARRAY
21 - SUBCONTEXT
22 - SWITCH
23 - DATA
25 =head1 AUTHOR
27 Jelmer Vernooij <jelmer@samba.org>
29 =cut
31 package Parse::Pidl::NDR;
33 require Exporter;
34 use 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);
40 use strict;
41 use Parse::Pidl qw(warning fatal);
42 use Parse::Pidl::Typelist qw(hasType getType expandAlias mapScalarType);
43 use Parse::Pidl::Util qw(has_property property_matches);
45 # Alignment of the built-in scalar types
46 my $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 'int3264' => 5,
58 'uint3264' => 5,
59 'hyper' => 8,
60 'double' => 8,
61 'pointer' => 8,
62 'dlong' => 4,
63 'udlong' => 4,
64 'udlongr' => 4,
65 'DATA_BLOB' => 4,
66 'string' => 4,
67 'string_array' => 4, #???
68 'time_t' => 4,
69 'NTTIME' => 4,
70 'NTTIME_1sec' => 4,
71 'NTTIME_hyper' => 8,
72 'WERROR' => 4,
73 'NTSTATUS' => 4,
74 'COMRESULT' => 4,
75 'nbt_string' => 4,
76 'wrepl_nbt_name' => 4,
77 'ipv4address' => 4
80 sub GetElementLevelTable($$)
82 my ($e, $pointer_default) = @_;
84 my $order = [];
85 my $is_deferred = 0;
86 my @bracket_array = ();
87 my @length_is = ();
88 my @size_is = ();
89 my $pointer_idx = 0;
91 if (has_property($e, "size_is")) {
92 @size_is = split /,/, has_property($e, "size_is");
95 if (has_property($e, "length_is")) {
96 @length_is = split /,/, has_property($e, "length_is");
99 if (defined($e->{ARRAY_LEN})) {
100 @bracket_array = @{$e->{ARRAY_LEN}};
103 if (has_property($e, "out")) {
104 my $needptrs = 1;
106 if (has_property($e, "string")) { $needptrs++; }
107 if ($#bracket_array >= 0) { $needptrs = 0; }
109 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
112 # Parse the [][][][] style array stuff
113 for my $i (0 .. $#bracket_array) {
114 my $d = $bracket_array[$#bracket_array - $i];
115 my $size = $d;
116 my $length = $d;
117 my $is_surrounding = 0;
118 my $is_varying = 0;
119 my $is_conformant = 0;
120 my $is_string = 0;
121 my $is_fixed = 0;
122 my $is_inline = 0;
124 if ($d eq "*") {
125 $is_conformant = 1;
126 if ($size = shift @size_is) {
127 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
128 $is_string = 1;
129 delete($e->{PROPERTIES}->{string});
130 } else {
131 fatal($e, "Must specify size_is() for conformant array!")
134 if (($length = shift @length_is) or $is_string) {
135 $is_varying = 1;
136 } else {
137 $length = $size;
140 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
141 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
142 $is_surrounding = 1;
146 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
147 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
149 if ($i == 0 and $is_fixed and has_property($e, "string")) {
150 $is_fixed = 0;
151 $is_varying = 1;
152 $is_string = 1;
153 delete($e->{PROPERTIES}->{string});
156 push (@$order, {
157 TYPE => "ARRAY",
158 SIZE_IS => $size,
159 LENGTH_IS => $length,
160 IS_DEFERRED => $is_deferred,
161 IS_SURROUNDING => $is_surrounding,
162 IS_ZERO_TERMINATED => $is_string,
163 IS_VARYING => $is_varying,
164 IS_CONFORMANT => $is_conformant,
165 IS_FIXED => $is_fixed,
166 IS_INLINE => $is_inline
170 # Next, all the pointers
171 foreach my $i (1..$e->{POINTERS}) {
172 my $level = "EMBEDDED";
173 # Top level "ref" pointers do not have a referrent identifier
174 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
176 my $pt;
178 # Only the first level gets the pointer type from the
179 # pointer property, the others get them from
180 # the pointer_default() interface property
182 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
183 # (Here they talk about the rightmost pointer, but testing shows
184 # they mean the leftmost pointer.)
186 # --metze
188 $pt = pointer_type($e);
189 if ($i > 1) {
190 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
191 $pt = $pointer_default;
194 push (@$order, {
195 TYPE => "POINTER",
196 POINTER_TYPE => $pt,
197 POINTER_INDEX => $pointer_idx,
198 IS_DEFERRED => "$is_deferred",
199 LEVEL => $level
202 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
203 if ($i == 1 and $pt ne "ref" and
204 $e->{PARENT}->{TYPE} eq "FUNCTION" and
205 not has_property($e, "in"));
207 $pointer_idx++;
209 # everything that follows will be deferred
210 $is_deferred = 1 if ($level ne "TOP");
212 my $array_size = shift @size_is;
213 my $array_length;
214 my $is_varying;
215 my $is_conformant;
216 my $is_string = 0;
217 if ($array_size) {
218 $is_conformant = 1;
219 if ($array_length = shift @length_is) {
220 $is_varying = 1;
221 } else {
222 $array_length = $array_size;
223 $is_varying =0;
227 if (scalar(@size_is) == 0 and has_property($e, "string") and
228 $i == $e->{POINTERS}) {
229 $is_string = 1;
230 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
231 delete($e->{PROPERTIES}->{string});
234 if ($array_size or $is_string) {
235 push (@$order, {
236 TYPE => "ARRAY",
237 SIZE_IS => $array_size,
238 LENGTH_IS => $array_length,
239 IS_DEFERRED => $is_deferred,
240 IS_SURROUNDING => 0,
241 IS_ZERO_TERMINATED => $is_string,
242 IS_VARYING => $is_varying,
243 IS_CONFORMANT => $is_conformant,
244 IS_FIXED => 0,
245 IS_INLINE => 0
248 $is_deferred = 0;
252 if (defined(has_property($e, "subcontext"))) {
253 my $hdr_size = has_property($e, "subcontext");
254 my $subsize = has_property($e, "subcontext_size");
255 if (not defined($subsize)) {
256 $subsize = -1;
259 push (@$order, {
260 TYPE => "SUBCONTEXT",
261 HEADER_SIZE => $hdr_size,
262 SUBCONTEXT_SIZE => $subsize,
263 IS_DEFERRED => $is_deferred,
264 COMPRESSION => has_property($e, "compression"),
268 if (my $switch = has_property($e, "switch_is")) {
269 push (@$order, {
270 TYPE => "SWITCH",
271 SWITCH_IS => $switch,
272 IS_DEFERRED => $is_deferred
276 if (scalar(@size_is) > 0) {
277 fatal($e, "size_is() on non-array element");
280 if (scalar(@length_is) > 0) {
281 fatal($e, "length_is() on non-array element");
284 if (has_property($e, "string")) {
285 fatal($e, "string() attribute on non-array element");
288 push (@$order, {
289 TYPE => "DATA",
290 DATA_TYPE => $e->{TYPE},
291 IS_DEFERRED => $is_deferred,
292 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
293 IS_SURROUNDING => 0 #FIXME
296 my $i = 0;
297 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
299 return $order;
302 sub GetTypedefLevelTable($$$)
304 my ($e, $data, $pointer_default) = @_;
306 my $order = [];
308 push (@$order, {
309 TYPE => "TYPEDEF"
312 my $i = 0;
313 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
315 return $order;
318 #####################################################################
319 # see if a type contains any deferred data
320 sub can_contain_deferred($)
322 sub can_contain_deferred($);
323 my ($type) = @_;
325 return 1 unless (hasType($type)); # assume the worst
327 $type = getType($type);
329 return 0 if (Parse::Pidl::Typelist::is_scalar($type));
331 return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
333 return 0 unless defined($type->{ELEMENTS});
335 foreach (@{$type->{ELEMENTS}}) {
336 return 1 if ($_->{POINTERS});
337 return 1 if (can_contain_deferred ($_->{TYPE}));
340 return 0;
343 sub pointer_type($)
345 my $e = shift;
347 return undef unless $e->{POINTERS};
349 return "ref" if (has_property($e, "ref"));
350 return "full" if (has_property($e, "ptr"));
351 return "sptr" if (has_property($e, "sptr"));
352 return "unique" if (has_property($e, "unique"));
353 return "relative" if (has_property($e, "relative"));
354 return "relative_short" if (has_property($e, "relative_short"));
355 return "ignore" if (has_property($e, "ignore"));
357 return undef;
360 #####################################################################
361 # work out the correct alignment for a structure or union
362 sub find_largest_alignment($)
364 my $s = shift;
366 my $align = 1;
367 for my $e (@{$s->{ELEMENTS}}) {
368 my $a = 1;
370 if ($e->{POINTERS}) {
371 # this is a hack for NDR64
372 # the NDR layer translates this into
373 # an alignment of 4 for NDR and 8 for NDR64
374 $a = 5;
375 } elsif (has_property($e, "subcontext")) {
376 $a = 1;
377 } elsif (has_property($e, "transmit_as")) {
378 $a = align_type($e->{PROPERTIES}->{transmit_as});
379 } else {
380 $a = align_type($e->{TYPE});
383 $align = $a if ($align < $a);
386 return $align;
389 #####################################################################
390 # align a type
391 sub align_type($)
393 sub align_type($);
394 my ($e) = @_;
396 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
397 return $scalar_alignment->{$e->{NAME}};
400 return 0 if ($e eq "EMPTY");
402 unless (hasType($e)) {
403 # it must be an external type - all we can do is guess
404 # warning($e, "assuming alignment of unknown type '$e' is 4");
405 return 4;
408 my $dt = getType($e);
410 if ($dt->{TYPE} eq "TYPEDEF") {
411 return align_type($dt->{DATA});
412 } elsif ($dt->{TYPE} eq "CONFORMANCE") {
413 return $dt->{DATA}->{ALIGN};
414 } elsif ($dt->{TYPE} eq "ENUM") {
415 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
416 } elsif ($dt->{TYPE} eq "BITMAP") {
417 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
418 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
419 # Struct/union without body: assume 4
420 return 4 unless (defined($dt->{ELEMENTS}));
421 return find_largest_alignment($dt);
424 die("Unknown data type type $dt->{TYPE}");
427 sub ParseElement($$)
429 my ($e, $pointer_default) = @_;
431 $e->{TYPE} = expandAlias($e->{TYPE});
433 if (ref($e->{TYPE}) eq "HASH") {
434 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default);
437 return {
438 NAME => $e->{NAME},
439 TYPE => $e->{TYPE},
440 PROPERTIES => $e->{PROPERTIES},
441 LEVELS => GetElementLevelTable($e, $pointer_default),
442 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
443 ALIGN => align_type($e->{TYPE}),
444 ORIGINAL => $e
448 sub ParseStruct($$)
450 my ($struct, $pointer_default) = @_;
451 my @elements = ();
452 my $surrounding = undef;
454 return {
455 TYPE => "STRUCT",
456 NAME => $struct->{NAME},
457 SURROUNDING_ELEMENT => undef,
458 ELEMENTS => undef,
459 PROPERTIES => $struct->{PROPERTIES},
460 ORIGINAL => $struct,
461 ALIGN => undef
462 } unless defined($struct->{ELEMENTS});
464 CheckPointerTypes($struct, $pointer_default);
466 foreach my $x (@{$struct->{ELEMENTS}})
468 my $e = ParseElement($x, $pointer_default);
469 if ($x != $struct->{ELEMENTS}[-1] and
470 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
471 fatal($x, "conformant member not at end of struct");
473 push @elements, $e;
476 my $e = $elements[-1];
477 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
478 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
479 $surrounding = $e;
482 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
483 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
484 $surrounding = $struct->{ELEMENTS}[-1];
487 my $align = undef;
488 if ($struct->{NAME}) {
489 $align = align_type($struct->{NAME});
492 return {
493 TYPE => "STRUCT",
494 NAME => $struct->{NAME},
495 SURROUNDING_ELEMENT => $surrounding,
496 ELEMENTS => \@elements,
497 PROPERTIES => $struct->{PROPERTIES},
498 ORIGINAL => $struct,
499 ALIGN => $align
503 sub ParseUnion($$)
505 my ($e, $pointer_default) = @_;
506 my @elements = ();
507 my $hasdefault = 0;
508 my $switch_type = has_property($e, "switch_type");
509 unless (defined($switch_type)) { $switch_type = "uint32"; }
510 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
512 return {
513 TYPE => "UNION",
514 NAME => $e->{NAME},
515 SWITCH_TYPE => $switch_type,
516 ELEMENTS => undef,
517 PROPERTIES => $e->{PROPERTIES},
518 HAS_DEFAULT => $hasdefault,
519 ORIGINAL => $e,
520 ALIGN => undef
521 } unless defined($e->{ELEMENTS});
523 CheckPointerTypes($e, $pointer_default);
525 foreach my $x (@{$e->{ELEMENTS}})
527 my $t;
528 if ($x->{TYPE} eq "EMPTY") {
529 $t = { TYPE => "EMPTY" };
530 } else {
531 $t = ParseElement($x, $pointer_default);
533 if (has_property($x, "default")) {
534 $t->{CASE} = "default";
535 $hasdefault = 1;
536 } elsif (defined($x->{PROPERTIES}->{case})) {
537 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
538 } else {
539 die("Union element $x->{NAME} has neither default nor case property");
541 push @elements, $t;
544 my $align = undef;
545 if ($e->{NAME}) {
546 $align = align_type($e->{NAME});
549 return {
550 TYPE => "UNION",
551 NAME => $e->{NAME},
552 SWITCH_TYPE => $switch_type,
553 ELEMENTS => \@elements,
554 PROPERTIES => $e->{PROPERTIES},
555 HAS_DEFAULT => $hasdefault,
556 ORIGINAL => $e,
557 ALIGN => $align
561 sub ParseEnum($$)
563 my ($e, $pointer_default) = @_;
565 return {
566 TYPE => "ENUM",
567 NAME => $e->{NAME},
568 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
569 ELEMENTS => $e->{ELEMENTS},
570 PROPERTIES => $e->{PROPERTIES},
571 ORIGINAL => $e
575 sub ParseBitmap($$)
577 my ($e, $pointer_default) = @_;
579 return {
580 TYPE => "BITMAP",
581 NAME => $e->{NAME},
582 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
583 ELEMENTS => $e->{ELEMENTS},
584 PROPERTIES => $e->{PROPERTIES},
585 ORIGINAL => $e
589 sub ParseType($$)
591 my ($d, $pointer_default) = @_;
593 my $data = {
594 STRUCT => \&ParseStruct,
595 UNION => \&ParseUnion,
596 ENUM => \&ParseEnum,
597 BITMAP => \&ParseBitmap,
598 TYPEDEF => \&ParseTypedef,
599 }->{$d->{TYPE}}->($d, $pointer_default);
601 return $data;
604 sub ParseTypedef($$)
606 my ($d, $pointer_default) = @_;
608 if (defined($d->{DATA}->{PROPERTIES}) && !defined($d->{PROPERTIES})) {
609 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
612 my $data = ParseType($d->{DATA}, $pointer_default);
613 $data->{ALIGN} = align_type($d->{NAME});
615 return {
616 NAME => $d->{NAME},
617 TYPE => $d->{TYPE},
618 PROPERTIES => $d->{PROPERTIES},
619 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default),
620 DATA => $data,
621 ORIGINAL => $d
625 sub ParseConst($$)
627 my ($ndr,$d) = @_;
629 return $d;
632 sub ParseFunction($$$)
634 my ($ndr,$d,$opnum) = @_;
635 my @elements = ();
636 my $rettype = undef;
637 my $thisopnum = undef;
639 CheckPointerTypes($d, "ref");
641 if (not defined($d->{PROPERTIES}{noopnum})) {
642 $thisopnum = ${$opnum};
643 ${$opnum}++;
646 foreach my $x (@{$d->{ELEMENTS}}) {
647 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default});
648 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
649 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
651 push (@elements, $e);
654 if ($d->{RETURN_TYPE} ne "void") {
655 $rettype = expandAlias($d->{RETURN_TYPE});
658 my $async = 0;
659 if (has_property($d, "async")) { $async = 1; }
661 return {
662 NAME => $d->{NAME},
663 TYPE => "FUNCTION",
664 OPNUM => $thisopnum,
665 ASYNC => $async,
666 RETURN_TYPE => $rettype,
667 PROPERTIES => $d->{PROPERTIES},
668 ELEMENTS => \@elements,
669 ORIGINAL => $d
673 sub CheckPointerTypes($$)
675 my ($s,$default) = @_;
677 return unless defined($s->{ELEMENTS});
679 foreach my $e (@{$s->{ELEMENTS}}) {
680 if ($e->{POINTERS} and not defined(pointer_type($e))) {
681 $e->{PROPERTIES}->{$default} = '1';
686 sub FindNestedTypes($$)
688 sub FindNestedTypes($$);
689 my ($l, $t) = @_;
691 return unless defined($t->{ELEMENTS});
692 return if ($t->{TYPE} eq "ENUM");
693 return if ($t->{TYPE} eq "BITMAP");
695 foreach (@{$t->{ELEMENTS}}) {
696 if (ref($_->{TYPE}) eq "HASH") {
697 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
698 FindNestedTypes($l, $_->{TYPE});
703 sub ParseInterface($)
705 my $idl = shift;
706 my @types = ();
707 my @consts = ();
708 my @functions = ();
709 my @endpoints;
710 my $opnum = 0;
711 my $version;
713 if (not has_property($idl, "pointer_default")) {
714 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
715 # and "unique" in Microsoft Extensions mode (default)
716 $idl->{PROPERTIES}->{pointer_default} = "unique";
719 foreach my $d (@{$idl->{DATA}}) {
720 if ($d->{TYPE} eq "FUNCTION") {
721 push (@functions, ParseFunction($idl, $d, \$opnum));
722 } elsif ($d->{TYPE} eq "CONST") {
723 push (@consts, ParseConst($idl, $d));
724 } else {
725 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}));
726 FindNestedTypes(\@types, $d);
730 $version = "0.0";
732 if(defined $idl->{PROPERTIES}->{version}) {
733 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
734 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
735 $version = $idl->{PROPERTIES}->{version};
736 } else {
737 $version = $if_version[1] << 16 | $if_version[0];
741 # If no endpoint is set, default to the interface name as a named pipe
742 if (!defined $idl->{PROPERTIES}->{endpoint}) {
743 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
744 } else {
745 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
748 return {
749 NAME => $idl->{NAME},
750 UUID => lc(has_property($idl, "uuid")),
751 VERSION => $version,
752 TYPE => "INTERFACE",
753 PROPERTIES => $idl->{PROPERTIES},
754 FUNCTIONS => \@functions,
755 CONSTS => \@consts,
756 TYPES => \@types,
757 ENDPOINTS => \@endpoints
761 # Convert a IDL tree to a NDR tree
762 # Gives a result tree describing all that's necessary for easily generating
763 # NDR parsers / generators
764 sub Parse($)
766 my $idl = shift;
768 return undef unless (defined($idl));
770 Parse::Pidl::NDR::Validate($idl);
772 my @ndr = ();
774 foreach (@{$idl}) {
775 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
776 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
777 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
780 return \@ndr;
783 sub GetNextLevel($$)
785 my $e = shift;
786 my $fl = shift;
788 my $seen = 0;
790 foreach my $l (@{$e->{LEVELS}}) {
791 return $l if ($seen);
792 ($seen = 1) if ($l == $fl);
795 return undef;
798 sub GetPrevLevel($$)
800 my ($e,$fl) = @_;
801 my $prev = undef;
803 foreach my $l (@{$e->{LEVELS}}) {
804 (return $prev) if ($l == $fl);
805 $prev = $l;
808 return undef;
811 sub ContainsString($)
813 my ($e) = @_;
815 foreach my $l (@{$e->{LEVELS}}) {
816 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
819 return 0;
822 sub ContainsDeferred($$)
824 my ($e,$l) = @_;
826 return 1 if ($l->{CONTAINS_DEFERRED});
828 while ($l = GetNextLevel($e,$l))
830 return 1 if ($l->{IS_DEFERRED});
831 return 1 if ($l->{CONTAINS_DEFERRED});
834 return 0;
837 sub el_name($)
839 my $e = shift;
840 my $name = "<ANONYMOUS>";
842 $name = $e->{NAME} if defined($e->{NAME});
844 if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
845 return "$e->{PARENT}->{NAME}.$name";
848 if (defined($e->{PARENT}) and
849 defined($e->{PARENT}->{PARENT}) and
850 defined($e->{PARENT}->{PARENT}->{NAME})) {
851 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
854 return $name;
857 ###################################
858 # find a sibling var in a structure
859 sub find_sibling($$)
861 my($e,$name) = @_;
862 my($fn) = $e->{PARENT};
864 if ($name =~ /\*(.*)/) {
865 $name = $1;
868 for my $e2 (@{$fn->{ELEMENTS}}) {
869 return $e2 if ($e2->{NAME} eq $name);
872 return undef;
875 my %property_list = (
876 # interface
877 "helpstring" => ["INTERFACE", "FUNCTION"],
878 "version" => ["INTERFACE"],
879 "uuid" => ["INTERFACE"],
880 "endpoint" => ["INTERFACE"],
881 "pointer_default" => ["INTERFACE"],
882 "helper" => ["INTERFACE"],
883 "pyhelper" => ["INTERFACE"],
884 "authservice" => ["INTERFACE"],
885 "restricted" => ["INTERFACE"],
887 # dcom
888 "object" => ["INTERFACE"],
889 "local" => ["INTERFACE", "FUNCTION"],
890 "iid_is" => ["ELEMENT"],
891 "call_as" => ["FUNCTION"],
892 "idempotent" => ["FUNCTION"],
894 # function
895 "noopnum" => ["FUNCTION"],
896 "in" => ["ELEMENT"],
897 "out" => ["ELEMENT"],
898 "async" => ["FUNCTION"],
900 # pointer
901 "ref" => ["ELEMENT"],
902 "ptr" => ["ELEMENT"],
903 "unique" => ["ELEMENT"],
904 "ignore" => ["ELEMENT"],
905 "relative" => ["ELEMENT"],
906 "relative_short" => ["ELEMENT"],
907 "null_is_ffffffff" => ["ELEMENT"],
908 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
910 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
911 "value" => ["ELEMENT"],
912 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
914 # generic
915 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
916 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
917 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
918 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
919 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
920 "todo" => ["FUNCTION"],
922 # union
923 "switch_is" => ["ELEMENT"],
924 "switch_type" => ["ELEMENT", "UNION"],
925 "nodiscriminant" => ["UNION"],
926 "case" => ["ELEMENT"],
927 "default" => ["ELEMENT"],
929 "represent_as" => ["ELEMENT"],
930 "transmit_as" => ["ELEMENT"],
932 # subcontext
933 "subcontext" => ["ELEMENT"],
934 "subcontext_size" => ["ELEMENT"],
935 "compression" => ["ELEMENT"],
937 # enum
938 "enum8bit" => ["ENUM"],
939 "enum16bit" => ["ENUM"],
940 "v1_enum" => ["ENUM"],
942 # bitmap
943 "bitmap8bit" => ["BITMAP"],
944 "bitmap16bit" => ["BITMAP"],
945 "bitmap32bit" => ["BITMAP"],
946 "bitmap64bit" => ["BITMAP"],
948 # array
949 "range" => ["ELEMENT", "PIPE"],
950 "size_is" => ["ELEMENT"],
951 "string" => ["ELEMENT"],
952 "noheader" => ["ELEMENT"],
953 "charset" => ["ELEMENT"],
954 "length_is" => ["ELEMENT"],
957 #####################################################################
958 # check for unknown properties
959 sub ValidProperties($$)
961 my ($e,$t) = @_;
963 return unless defined $e->{PROPERTIES};
965 foreach my $key (keys %{$e->{PROPERTIES}}) {
966 warning($e, el_name($e) . ": unknown property '$key'")
967 unless defined($property_list{$key});
969 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
970 unless grep(/^$t$/, @{$property_list{$key}});
974 sub mapToScalar($)
976 sub mapToScalar($);
977 my $t = shift;
978 return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
979 my $ti = getType($t);
981 if (not defined ($ti)) {
982 return undef;
983 } elsif ($ti->{TYPE} eq "TYPEDEF") {
984 return mapToScalar($ti->{DATA});
985 } elsif ($ti->{TYPE} eq "ENUM") {
986 return Parse::Pidl::Typelist::enum_type_fn($ti);
987 } elsif ($ti->{TYPE} eq "BITMAP") {
988 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
991 return undef;
994 #####################################################################
995 # validate an element
996 sub ValidElement($)
998 my $e = shift;
1000 ValidProperties($e,"ELEMENT");
1002 # Check whether switches are used correctly.
1003 if (my $switch = has_property($e, "switch_is")) {
1004 my $e2 = find_sibling($e, $switch);
1005 my $type = getType($e->{TYPE});
1007 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
1008 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1011 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
1012 my $discriminator_type = has_property($type->{DATA}, "switch_type");
1013 $discriminator_type = "uint32" unless defined ($discriminator_type);
1015 my $t1 = mapScalarType(mapToScalar($discriminator_type));
1017 if (not defined($t1)) {
1018 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1021 my $t2 = mapScalarType(mapToScalar($e2->{TYPE}));
1022 if (not defined($t2)) {
1023 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1026 if ($t1 ne $t2) {
1027 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1032 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1033 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1036 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1037 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1040 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1041 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1044 if (has_property($e, "represent_as") and has_property($e, "value")) {
1045 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1048 if (has_property($e, "subcontext")) {
1049 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1052 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1053 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1056 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1057 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1060 if (!$e->{POINTERS} && (
1061 has_property($e, "ptr") or
1062 has_property($e, "unique") or
1063 has_property($e, "relative") or
1064 has_property($e, "relative_short") or
1065 has_property($e, "ref"))) {
1066 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
1070 #####################################################################
1071 # validate an enum
1072 sub ValidEnum($)
1074 my ($enum) = @_;
1076 ValidProperties($enum, "ENUM");
1079 #####################################################################
1080 # validate a bitmap
1081 sub ValidBitmap($)
1083 my ($bitmap) = @_;
1085 ValidProperties($bitmap, "BITMAP");
1088 #####################################################################
1089 # validate a struct
1090 sub ValidStruct($)
1092 my($struct) = shift;
1094 ValidProperties($struct, "STRUCT");
1096 return unless defined($struct->{ELEMENTS});
1098 foreach my $e (@{$struct->{ELEMENTS}}) {
1099 $e->{PARENT} = $struct;
1100 ValidElement($e);
1104 #####################################################################
1105 # parse a union
1106 sub ValidUnion($)
1108 my($union) = shift;
1110 ValidProperties($union,"UNION");
1112 if (has_property($union->{PARENT}, "nodiscriminant") and
1113 has_property($union->{PARENT}, "switch_type")) {
1114 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1117 return unless defined($union->{ELEMENTS});
1119 foreach my $e (@{$union->{ELEMENTS}}) {
1120 $e->{PARENT} = $union;
1122 if (defined($e->{PROPERTIES}->{default}) and
1123 defined($e->{PROPERTIES}->{case})) {
1124 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1127 unless (defined ($e->{PROPERTIES}->{default}) or
1128 defined ($e->{PROPERTIES}->{case})) {
1129 fatal($e, "Union member $e->{NAME} must have default or case property");
1132 if (has_property($e, "ref")) {
1133 fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1137 ValidElement($e);
1141 #####################################################################
1142 # validate a pipe
1143 sub ValidPipe($)
1145 my ($pipe) = @_;
1146 my $data = $pipe->{DATA};
1148 ValidProperties($pipe, "PIPE");
1150 fatal($pipe, $pipe->{NAME} . ": 'pipe' is not yet supported by pidl");
1153 #####################################################################
1154 # parse a typedef
1155 sub ValidTypedef($)
1157 my($typedef) = shift;
1158 my $data = $typedef->{DATA};
1160 ValidProperties($typedef, "TYPEDEF");
1162 $data->{PARENT} = $typedef;
1164 $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1165 $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1167 ValidType($data) if (ref($data) eq "HASH");
1170 #####################################################################
1171 # validate a function
1172 sub ValidFunction($)
1174 my($fn) = shift;
1176 ValidProperties($fn,"FUNCTION");
1178 foreach my $e (@{$fn->{ELEMENTS}}) {
1179 $e->{PARENT} = $fn;
1180 if (has_property($e, "ref") && !$e->{POINTERS}) {
1181 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1183 ValidElement($e);
1187 #####################################################################
1188 # validate a type
1189 sub ValidType($)
1191 my ($t) = @_;
1194 TYPEDEF => \&ValidTypedef,
1195 STRUCT => \&ValidStruct,
1196 UNION => \&ValidUnion,
1197 ENUM => \&ValidEnum,
1198 BITMAP => \&ValidBitmap,
1199 PIPE => \&ValidPipe
1200 }->{$t->{TYPE}}->($t);
1203 #####################################################################
1204 # parse the interface definitions
1205 sub ValidInterface($)
1207 my($interface) = shift;
1208 my($data) = $interface->{DATA};
1210 if (has_property($interface, "helper")) {
1211 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1214 ValidProperties($interface,"INTERFACE");
1216 if (has_property($interface, "pointer_default")) {
1217 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1218 ("ref", "unique", "ptr"))) {
1219 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1223 if (has_property($interface, "object")) {
1224 if (has_property($interface, "version") &&
1225 $interface->{PROPERTIES}->{version} != 0) {
1226 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1229 if (!defined($interface->{BASE}) &&
1230 not ($interface->{NAME} eq "IUnknown")) {
1231 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1235 foreach my $d (@{$data}) {
1236 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1237 ($d->{TYPE} eq "TYPEDEF" or
1238 $d->{TYPE} eq "STRUCT" or
1239 $d->{TYPE} eq "UNION" or
1240 $d->{TYPE} eq "ENUM" or
1241 $d->{TYPE} eq "BITMAP" or
1242 $d->{TYPE} eq "PIPE") && ValidType($d);
1247 #####################################################################
1248 # Validate an IDL structure
1249 sub Validate($)
1251 my($idl) = shift;
1253 foreach my $x (@{$idl}) {
1254 ($x->{TYPE} eq "INTERFACE") &&
1255 ValidInterface($x);
1256 ($x->{TYPE} eq "IMPORTLIB") &&
1257 fatal($x, "importlib() not supported");
1261 sub is_charset_array($$)
1263 my ($e,$l) = @_;
1265 return 0 if ($l->{TYPE} ne "ARRAY");
1267 my $nl = GetNextLevel($e,$l);
1269 return 0 unless ($nl->{TYPE} eq "DATA");
1271 return has_property($e, "charset");