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
12 Parse::Pidl::NDR - NDR parsing information generator
16 Return a table describing the order in which the parts of an element
27 Jelmer Vernooij <jelmer@samba.org>
31 package Parse::Pidl::NDR;
34 use vars qw($VERSION);
37 @EXPORT = qw(GetPrevLevel GetNextLevel ContainsDeferred ContainsString);
38 @EXPORT_OK = qw(GetElementLevelTable ParseElement ValidElement align_type mapToScalar ParseType can_contain_deferred is_charset_array);
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 = {
67 'string_array' => 4, #???
79 'wrepl_nbt_name' => 4,
81 'ipv6address' => 4, #16?
86 sub GetElementLevelTable($$$)
88 my ($e, $pointer_default, $ms_union) = @_;
92 my @bracket_array = ();
97 if (has_property($e, "size_is")) {
98 @size_is = split /,/, has_property($e, "size_is");
101 if (has_property($e, "length_is")) {
102 @length_is = split /,/, has_property($e, "length_is");
105 if (defined($e->{ARRAY_LEN})) {
106 @bracket_array = @{$e->{ARRAY_LEN}};
109 if (has_property($e, "out")) {
112 if (has_property($e, "string") and not has_property($e, "in")) { $needptrs++; }
113 if ($#bracket_array >= 0) { $needptrs = 0; }
115 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
118 # Parse the [][][][] style array stuff
119 for my $i (0 .. $#bracket_array) {
120 my $d = $bracket_array[$#bracket_array - $i];
123 my $is_surrounding = 0;
125 my $is_conformant = 0;
132 if ($size = shift @size_is) {
133 if ($e->{POINTERS} < 1 and has_property($e, "string")) {
135 delete($e->{PROPERTIES}->{string});
137 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
139 delete($e->{PROPERTIES}->{string});
141 fatal($e, "Must specify size_is() for conformant array!")
144 if (($length = shift @length_is) or $is_string) {
150 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
151 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
156 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
157 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
159 if ($i == 0 and $is_fixed and has_property($e, "string")) {
163 delete($e->{PROPERTIES}->{string});
169 LENGTH_IS => $length,
170 IS_DEFERRED => $is_deferred,
171 IS_SURROUNDING => $is_surrounding,
172 IS_ZERO_TERMINATED => $is_string,
173 IS_VARYING => $is_varying,
174 IS_CONFORMANT => $is_conformant,
175 IS_FIXED => $is_fixed,
176 IS_INLINE => $is_inline
180 # Next, all the pointers
181 foreach my $i (1..$e->{POINTERS}) {
182 my $level = "EMBEDDED";
183 # Top level "ref" pointers do not have a referrent identifier
184 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
188 # Only the first level gets the pointer type from the
189 # pointer property, the others get them from
190 # the pointer_default() interface property
192 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
193 # (Here they talk about the rightmost pointer, but testing shows
194 # they mean the leftmost pointer.)
198 $pt = pointer_type($e);
200 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
201 $pt = $pointer_default;
207 POINTER_INDEX => $pointer_idx,
208 IS_DEFERRED => "$is_deferred",
212 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
213 if ($i == 1 and $pt ne "ref" and
214 $e->{PARENT}->{TYPE} eq "FUNCTION" and
215 not has_property($e, "in"));
219 # everything that follows will be deferred
220 $is_deferred = 1 if ($level ne "TOP");
222 my $array_size = shift @size_is;
229 if ($array_length = shift @length_is) {
232 $array_length = $array_size;
237 if (scalar(@size_is) == 0 and has_property($e, "string") and
238 $i == $e->{POINTERS}) {
240 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
241 delete($e->{PROPERTIES}->{string});
244 if ($array_size or $is_string) {
247 SIZE_IS => $array_size,
248 LENGTH_IS => $array_length,
249 IS_DEFERRED => $is_deferred,
251 IS_ZERO_TERMINATED => $is_string,
252 IS_VARYING => $is_varying,
253 IS_CONFORMANT => $is_conformant,
262 if (defined(has_property($e, "subcontext"))) {
263 my $hdr_size = has_property($e, "subcontext");
264 my $subsize = has_property($e, "subcontext_size");
265 if (not defined($subsize)) {
270 TYPE => "SUBCONTEXT",
271 HEADER_SIZE => $hdr_size,
272 SUBCONTEXT_SIZE => $subsize,
273 IS_DEFERRED => $is_deferred,
274 COMPRESSION => has_property($e, "compression"),
278 if (my $switch = has_property($e, "switch_is")) {
281 SWITCH_IS => $switch,
282 IS_DEFERRED => $is_deferred
286 if (scalar(@size_is) > 0) {
287 fatal($e, "size_is() on non-array element");
290 if (scalar(@length_is) > 0) {
291 fatal($e, "length_is() on non-array element");
294 if (has_property($e, "string")) {
295 fatal($e, "string() attribute on non-array element");
300 DATA_TYPE => $e->{TYPE},
301 IS_DEFERRED => $is_deferred,
302 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
303 IS_SURROUNDING => 0 #FIXME
307 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
312 sub GetTypedefLevelTable($$$$)
314 my ($e, $data, $pointer_default, $ms_union) = @_;
323 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
328 #####################################################################
329 # see if a type contains any deferred data
330 sub can_contain_deferred($)
332 sub can_contain_deferred($);
335 return 1 unless (hasType($type)); # assume the worst
337 $type = getType($type);
339 return 0 if (Parse::Pidl::Typelist::is_scalar($type));
341 return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
343 return 0 unless defined($type->{ELEMENTS});
345 foreach (@{$type->{ELEMENTS}}) {
346 return 1 if ($_->{POINTERS});
347 return 1 if (can_contain_deferred ($_->{TYPE}));
357 return undef unless $e->{POINTERS};
359 return "ref" if (has_property($e, "ref"));
360 return "full" if (has_property($e, "ptr"));
361 return "sptr" if (has_property($e, "sptr"));
362 return "unique" if (has_property($e, "unique"));
363 return "relative" if (has_property($e, "relative"));
364 return "relative_short" if (has_property($e, "relative_short"));
365 return "ignore" if (has_property($e, "ignore"));
370 #####################################################################
371 # work out the correct alignment for a structure or union
372 sub find_largest_alignment($)
377 for my $e (@{$s->{ELEMENTS}}) {
380 if ($e->{POINTERS}) {
381 # this is a hack for NDR64
382 # the NDR layer translates this into
383 # an alignment of 4 for NDR and 8 for NDR64
385 } elsif (has_property($e, "subcontext")) {
387 } elsif (has_property($e, "transmit_as")) {
388 $a = align_type($e->{PROPERTIES}->{transmit_as});
390 $a = align_type($e->{TYPE});
393 $align = $a if ($align < $a);
399 #####################################################################
406 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
407 return $scalar_alignment->{$e->{NAME}};
410 return 0 if ($e eq "EMPTY");
412 unless (hasType($e)) {
413 # it must be an external type - all we can do is guess
414 # warning($e, "assuming alignment of unknown type '$e' is 4");
418 my $dt = getType($e);
420 if ($dt->{TYPE} eq "TYPEDEF") {
421 return align_type($dt->{DATA});
422 } elsif ($dt->{TYPE} eq "CONFORMANCE") {
423 return $dt->{DATA}->{ALIGN};
424 } elsif ($dt->{TYPE} eq "ENUM") {
425 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
426 } elsif ($dt->{TYPE} eq "BITMAP") {
427 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
428 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
429 # Struct/union without body: assume 4
430 return 4 unless (defined($dt->{ELEMENTS}));
431 return find_largest_alignment($dt);
434 die("Unknown data type type $dt->{TYPE}");
437 sub ParseElement($$$)
439 my ($e, $pointer_default, $ms_union) = @_;
441 $e->{TYPE} = expandAlias($e->{TYPE});
443 if (ref($e->{TYPE}) eq "HASH") {
444 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default, $ms_union);
450 PROPERTIES => $e->{PROPERTIES},
451 LEVELS => GetElementLevelTable($e, $pointer_default, $ms_union),
452 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
453 ALIGN => align_type($e->{TYPE}),
460 my ($struct, $pointer_default, $ms_union) = @_;
462 my $surrounding = undef;
466 NAME => $struct->{NAME},
467 SURROUNDING_ELEMENT => undef,
469 PROPERTIES => $struct->{PROPERTIES},
472 } unless defined($struct->{ELEMENTS});
474 CheckPointerTypes($struct, $pointer_default);
476 foreach my $x (@{$struct->{ELEMENTS}})
478 my $e = ParseElement($x, $pointer_default, $ms_union);
479 if ($x != $struct->{ELEMENTS}[-1] and
480 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
481 fatal($x, "conformant member not at end of struct");
486 my $e = $elements[-1];
487 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
488 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
492 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
493 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
494 $surrounding = $struct->{ELEMENTS}[-1];
498 if ($struct->{NAME}) {
499 $align = align_type($struct->{NAME});
504 NAME => $struct->{NAME},
505 SURROUNDING_ELEMENT => $surrounding,
506 ELEMENTS => \@elements,
507 PROPERTIES => $struct->{PROPERTIES},
515 my ($e, $pointer_default, $ms_union) = @_;
517 my $is_ms_union = $ms_union;
518 $is_ms_union = 1 if has_property($e, "ms_union");
520 my $switch_type = has_property($e, "switch_type");
521 unless (defined($switch_type)) { $switch_type = "uint32"; }
522 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
527 SWITCH_TYPE => $switch_type,
529 PROPERTIES => $e->{PROPERTIES},
530 HAS_DEFAULT => $hasdefault,
531 IS_MS_UNION => $is_ms_union,
534 } unless defined($e->{ELEMENTS});
536 CheckPointerTypes($e, $pointer_default);
538 foreach my $x (@{$e->{ELEMENTS}})
541 if ($x->{TYPE} eq "EMPTY") {
542 $t = { TYPE => "EMPTY" };
544 $t = ParseElement($x, $pointer_default, $ms_union);
546 if (has_property($x, "default")) {
547 $t->{CASE} = "default";
549 } elsif (defined($x->{PROPERTIES}->{case})) {
550 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
552 die("Union element $x->{NAME} has neither default nor case property");
559 $align = align_type($e->{NAME});
565 SWITCH_TYPE => $switch_type,
566 ELEMENTS => \@elements,
567 PROPERTIES => $e->{PROPERTIES},
568 HAS_DEFAULT => $hasdefault,
569 IS_MS_UNION => $is_ms_union,
577 my ($e, $pointer_default, $ms_union) = @_;
582 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
583 ELEMENTS => $e->{ELEMENTS},
584 PROPERTIES => $e->{PROPERTIES},
591 my ($e, $pointer_default, $ms_union) = @_;
596 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
597 ELEMENTS => $e->{ELEMENTS},
598 PROPERTIES => $e->{PROPERTIES},
605 my ($d, $pointer_default, $ms_union) = @_;
608 STRUCT => \&ParseStruct,
609 UNION => \&ParseUnion,
611 BITMAP => \&ParseBitmap,
612 TYPEDEF => \&ParseTypedef,
613 }->{$d->{TYPE}}->($d, $pointer_default, $ms_union);
620 my ($d, $pointer_default, $ms_union) = @_;
624 if (ref($d->{DATA}) eq "HASH") {
625 if (defined($d->{DATA}->{PROPERTIES})
626 and not defined($d->{PROPERTIES})) {
627 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
630 $data = ParseType($d->{DATA}, $pointer_default, $ms_union);
631 $data->{ALIGN} = align_type($d->{NAME});
633 $data = getType($d->{DATA});
639 PROPERTIES => $d->{PROPERTIES},
640 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default, $ms_union),
653 sub ParseFunction($$$$)
655 my ($ndr,$d,$opnum,$ms_union) = @_;
658 my $thisopnum = undef;
660 CheckPointerTypes($d, "ref");
662 if (not defined($d->{PROPERTIES}{noopnum})) {
663 $thisopnum = ${$opnum};
667 foreach my $x (@{$d->{ELEMENTS}}) {
668 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default}, $ms_union);
669 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
670 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
672 push (@elements, $e);
675 if ($d->{RETURN_TYPE} ne "void") {
676 $rettype = expandAlias($d->{RETURN_TYPE});
683 RETURN_TYPE => $rettype,
684 PROPERTIES => $d->{PROPERTIES},
685 ELEMENTS => \@elements,
690 sub CheckPointerTypes($$)
692 my ($s,$default) = @_;
694 return unless defined($s->{ELEMENTS});
696 foreach my $e (@{$s->{ELEMENTS}}) {
697 if ($e->{POINTERS} and not defined(pointer_type($e))) {
698 $e->{PROPERTIES}->{$default} = '1';
703 sub FindNestedTypes($$)
705 sub FindNestedTypes($$);
708 return unless defined($t->{ELEMENTS});
709 return if ($t->{TYPE} eq "ENUM");
710 return if ($t->{TYPE} eq "BITMAP");
712 foreach (@{$t->{ELEMENTS}}) {
713 if (ref($_->{TYPE}) eq "HASH") {
714 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
715 FindNestedTypes($l, $_->{TYPE});
720 sub ParseInterface($)
730 $ms_union = 1 if has_property($idl, "ms_union");
732 if (not has_property($idl, "pointer_default")) {
733 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
734 # and "unique" in Microsoft Extensions mode (default)
735 $idl->{PROPERTIES}->{pointer_default} = "unique";
738 foreach my $d (@{$idl->{DATA}}) {
739 if ($d->{TYPE} eq "FUNCTION") {
740 push (@functions, ParseFunction($idl, $d, \$opnum, $ms_union));
741 } elsif ($d->{TYPE} eq "CONST") {
742 push (@consts, ParseConst($idl, $d));
744 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}, $ms_union));
745 FindNestedTypes(\@types, $d);
751 if(defined $idl->{PROPERTIES}->{version}) {
752 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
753 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
754 $version = $idl->{PROPERTIES}->{version};
756 $version = $if_version[1] << 16 | $if_version[0];
760 # If no endpoint is set, default to the interface name as a named pipe
761 if (!defined $idl->{PROPERTIES}->{endpoint}) {
762 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
764 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
768 NAME => $idl->{NAME},
769 UUID => lc(has_property($idl, "uuid")),
772 PROPERTIES => $idl->{PROPERTIES},
773 FUNCTIONS => \@functions,
776 ENDPOINTS => \@endpoints
780 # Convert a IDL tree to a NDR tree
781 # Gives a result tree describing all that's necessary for easily generating
782 # NDR parsers / generators
787 return undef unless (defined($idl));
789 Parse::Pidl::NDR::Validate($idl);
794 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
795 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
796 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
809 foreach my $l (@{$e->{LEVELS}}) {
810 return $l if ($seen);
811 ($seen = 1) if ($l == $fl);
822 foreach my $l (@{$e->{LEVELS}}) {
823 (return $prev) if ($l == $fl);
830 sub ContainsString($)
834 foreach my $l (@{$e->{LEVELS}}) {
835 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
841 sub ContainsDeferred($$)
845 return 1 if ($l->{CONTAINS_DEFERRED});
847 while ($l = GetNextLevel($e,$l))
849 return 1 if ($l->{IS_DEFERRED});
850 return 1 if ($l->{CONTAINS_DEFERRED});
859 my $name = "<ANONYMOUS>";
861 $name = $e->{NAME} if defined($e->{NAME});
863 if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
864 return "$e->{PARENT}->{NAME}.$name";
867 if (defined($e->{PARENT}) and
868 defined($e->{PARENT}->{PARENT}) and
869 defined($e->{PARENT}->{PARENT}->{NAME})) {
870 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
876 ###################################
877 # find a sibling var in a structure
881 my($fn) = $e->{PARENT};
883 if ($name =~ /\*(.*)/) {
887 for my $e2 (@{$fn->{ELEMENTS}}) {
888 return $e2 if ($e2->{NAME} eq $name);
894 my %property_list = (
896 "helpstring" => ["INTERFACE", "FUNCTION"],
897 "version" => ["INTERFACE"],
898 "uuid" => ["INTERFACE"],
899 "endpoint" => ["INTERFACE"],
900 "pointer_default" => ["INTERFACE"],
901 "helper" => ["INTERFACE"],
902 "pyhelper" => ["INTERFACE"],
903 "authservice" => ["INTERFACE"],
904 "restricted" => ["INTERFACE"],
905 "no_srv_register" => ["INTERFACE"],
908 "object" => ["INTERFACE"],
909 "local" => ["INTERFACE", "FUNCTION"],
910 "iid_is" => ["ELEMENT"],
911 "call_as" => ["FUNCTION"],
912 "idempotent" => ["FUNCTION"],
915 "noopnum" => ["FUNCTION"],
917 "out" => ["ELEMENT"],
920 "ref" => ["ELEMENT", "TYPEDEF"],
921 "ptr" => ["ELEMENT", "TYPEDEF"],
922 "unique" => ["ELEMENT", "TYPEDEF"],
923 "ignore" => ["ELEMENT"],
924 "relative" => ["ELEMENT", "TYPEDEF"],
925 "relative_short" => ["ELEMENT", "TYPEDEF"],
926 "null_is_ffffffff" => ["ELEMENT"],
927 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
929 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
930 "value" => ["ELEMENT"],
931 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
934 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
935 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
936 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
937 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
938 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
939 "nopython" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
940 "todo" => ["FUNCTION"],
943 "switch_is" => ["ELEMENT"],
944 "switch_type" => ["ELEMENT", "UNION"],
945 "nodiscriminant" => ["UNION"],
946 "ms_union" => ["INTERFACE", "UNION"],
947 "case" => ["ELEMENT"],
948 "default" => ["ELEMENT"],
950 "represent_as" => ["ELEMENT"],
951 "transmit_as" => ["ELEMENT"],
954 "subcontext" => ["ELEMENT"],
955 "subcontext_size" => ["ELEMENT"],
956 "compression" => ["ELEMENT"],
959 "enum8bit" => ["ENUM"],
960 "enum16bit" => ["ENUM"],
961 "v1_enum" => ["ENUM"],
964 "bitmap8bit" => ["BITMAP"],
965 "bitmap16bit" => ["BITMAP"],
966 "bitmap32bit" => ["BITMAP"],
967 "bitmap64bit" => ["BITMAP"],
970 "range" => ["ELEMENT", "PIPE"],
971 "size_is" => ["ELEMENT"],
972 "string" => ["ELEMENT"],
973 "noheader" => ["ELEMENT"],
974 "charset" => ["ELEMENT"],
975 "length_is" => ["ELEMENT"],
978 #####################################################################
979 # check for unknown properties
980 sub ValidProperties($$)
984 return unless defined $e->{PROPERTIES};
986 foreach my $key (keys %{$e->{PROPERTIES}}) {
987 warning($e, el_name($e) . ": unknown property '$key'")
988 unless defined($property_list{$key});
990 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
991 unless grep(/^$t$/, @{$property_list{$key}});
999 return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
1000 my $ti = getType($t);
1002 if (not defined ($ti)) {
1004 } elsif ($ti->{TYPE} eq "TYPEDEF") {
1005 return mapToScalar($ti->{DATA});
1006 } elsif ($ti->{TYPE} eq "ENUM") {
1007 return Parse::Pidl::Typelist::enum_type_fn($ti);
1008 } elsif ($ti->{TYPE} eq "BITMAP") {
1009 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
1015 #####################################################################
1016 # validate an element
1021 ValidProperties($e,"ELEMENT");
1023 # Check whether switches are used correctly.
1024 if (my $switch = has_property($e, "switch_is")) {
1025 my $e2 = find_sibling($e, $switch);
1026 my $type = getType($e->{TYPE});
1028 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
1029 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1032 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
1033 my $discriminator_type = has_property($type->{DATA}, "switch_type");
1034 $discriminator_type = "uint32" unless defined ($discriminator_type);
1036 my $t1 = mapScalarType(mapToScalar($discriminator_type));
1038 if (not defined($t1)) {
1039 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1042 my $t2 = mapScalarType(mapToScalar($e2->{TYPE}));
1043 if (not defined($t2)) {
1044 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1048 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1053 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1054 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1057 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1058 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1061 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1062 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1065 if (has_property($e, "represent_as") and has_property($e, "value")) {
1066 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1069 if (has_property($e, "subcontext")) {
1070 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1073 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1074 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1077 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1078 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1081 if (!$e->{POINTERS} && (
1082 has_property($e, "ptr") or
1083 has_property($e, "unique") or
1084 has_property($e, "relative") or
1085 has_property($e, "relative_short") or
1086 has_property($e, "ref"))) {
1087 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
1091 #####################################################################
1097 ValidProperties($enum, "ENUM");
1100 #####################################################################
1106 ValidProperties($bitmap, "BITMAP");
1109 #####################################################################
1113 my($struct) = shift;
1115 ValidProperties($struct, "STRUCT");
1117 return unless defined($struct->{ELEMENTS});
1119 foreach my $e (@{$struct->{ELEMENTS}}) {
1120 $e->{PARENT} = $struct;
1125 #####################################################################
1131 ValidProperties($union,"UNION");
1133 if (has_property($union->{PARENT}, "nodiscriminant") and
1134 has_property($union->{PARENT}, "switch_type")) {
1135 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1138 return unless defined($union->{ELEMENTS});
1140 foreach my $e (@{$union->{ELEMENTS}}) {
1141 $e->{PARENT} = $union;
1143 if (defined($e->{PROPERTIES}->{default}) and
1144 defined($e->{PROPERTIES}->{case})) {
1145 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1148 unless (defined ($e->{PROPERTIES}->{default}) or
1149 defined ($e->{PROPERTIES}->{case})) {
1150 fatal($e, "Union member $e->{NAME} must have default or case property");
1153 if (has_property($e, "ref")) {
1154 fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1162 #####################################################################
1167 my $data = $pipe->{DATA};
1169 ValidProperties($pipe, "PIPE");
1171 fatal($pipe, $pipe->{NAME} . ": 'pipe' is not yet supported by pidl");
1174 #####################################################################
1178 my($typedef) = shift;
1179 my $data = $typedef->{DATA};
1181 ValidProperties($typedef, "TYPEDEF");
1183 return unless (ref($data) eq "HASH");
1185 $data->{PARENT} = $typedef;
1187 $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1188 $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1193 #####################################################################
1194 # validate a function
1195 sub ValidFunction($)
1199 ValidProperties($fn,"FUNCTION");
1201 foreach my $e (@{$fn->{ELEMENTS}}) {
1203 if (has_property($e, "ref") && !$e->{POINTERS}) {
1204 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1210 #####################################################################
1217 TYPEDEF => \&ValidTypedef,
1218 STRUCT => \&ValidStruct,
1219 UNION => \&ValidUnion,
1220 ENUM => \&ValidEnum,
1221 BITMAP => \&ValidBitmap,
1223 }->{$t->{TYPE}}->($t);
1226 #####################################################################
1227 # parse the interface definitions
1228 sub ValidInterface($)
1230 my($interface) = shift;
1231 my($data) = $interface->{DATA};
1233 if (has_property($interface, "helper")) {
1234 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1237 ValidProperties($interface,"INTERFACE");
1239 if (has_property($interface, "pointer_default")) {
1240 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1241 ("ref", "unique", "ptr"))) {
1242 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1246 if (has_property($interface, "object")) {
1247 if (has_property($interface, "version") &&
1248 $interface->{PROPERTIES}->{version} != 0) {
1249 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1252 if (!defined($interface->{BASE}) &&
1253 not ($interface->{NAME} eq "IUnknown")) {
1254 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1258 foreach my $d (@{$data}) {
1259 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1260 ($d->{TYPE} eq "TYPEDEF" or
1261 $d->{TYPE} eq "STRUCT" or
1262 $d->{TYPE} eq "UNION" or
1263 $d->{TYPE} eq "ENUM" or
1264 $d->{TYPE} eq "BITMAP" or
1265 $d->{TYPE} eq "PIPE") && ValidType($d);
1270 #####################################################################
1271 # Validate an IDL structure
1276 foreach my $x (@{$idl}) {
1277 ($x->{TYPE} eq "INTERFACE") &&
1279 ($x->{TYPE} eq "IMPORTLIB") &&
1280 fatal($x, "importlib() not supported");
1284 sub is_charset_array($$)
1288 return 0 if ($l->{TYPE} ne "ARRAY");
1290 my $nl = GetNextLevel($e,$l);
1292 return 0 unless ($nl->{TYPE} eq "DATA");
1294 return has_property($e, "charset");