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, #???
76 'wrepl_nbt_name' => 4,
78 'ipv6address' => 4, #16?
82 sub GetElementLevelTable($$)
84 my ($e, $pointer_default) = @_;
88 my @bracket_array = ();
93 if (has_property($e, "size_is")) {
94 @size_is = split /,/, has_property($e, "size_is");
97 if (has_property($e, "length_is")) {
98 @length_is = split /,/, has_property($e, "length_is");
101 if (defined($e->{ARRAY_LEN})) {
102 @bracket_array = @{$e->{ARRAY_LEN}};
105 if (has_property($e, "out")) {
108 if (has_property($e, "string") and not has_property($e, "in")) { $needptrs++; }
109 if ($#bracket_array >= 0) { $needptrs = 0; }
111 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
114 # Parse the [][][][] style array stuff
115 for my $i (0 .. $#bracket_array) {
116 my $d = $bracket_array[$#bracket_array - $i];
119 my $is_surrounding = 0;
121 my $is_conformant = 0;
128 if ($size = shift @size_is) {
129 if ($e->{POINTERS} < 1 and has_property($e, "string")) {
131 delete($e->{PROPERTIES}->{string});
133 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
135 delete($e->{PROPERTIES}->{string});
137 fatal($e, "Must specify size_is() for conformant array!")
140 if (($length = shift @length_is) or $is_string) {
146 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
147 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
152 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
153 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
155 if ($i == 0 and $is_fixed and has_property($e, "string")) {
159 delete($e->{PROPERTIES}->{string});
165 LENGTH_IS => $length,
166 IS_DEFERRED => $is_deferred,
167 IS_SURROUNDING => $is_surrounding,
168 IS_ZERO_TERMINATED => $is_string,
169 IS_VARYING => $is_varying,
170 IS_CONFORMANT => $is_conformant,
171 IS_FIXED => $is_fixed,
172 IS_INLINE => $is_inline
176 # Next, all the pointers
177 foreach my $i (1..$e->{POINTERS}) {
178 my $level = "EMBEDDED";
179 # Top level "ref" pointers do not have a referrent identifier
180 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
184 # Only the first level gets the pointer type from the
185 # pointer property, the others get them from
186 # the pointer_default() interface property
188 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
189 # (Here they talk about the rightmost pointer, but testing shows
190 # they mean the leftmost pointer.)
194 $pt = pointer_type($e);
196 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
197 $pt = $pointer_default;
203 POINTER_INDEX => $pointer_idx,
204 IS_DEFERRED => "$is_deferred",
208 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
209 if ($i == 1 and $pt ne "ref" and
210 $e->{PARENT}->{TYPE} eq "FUNCTION" and
211 not has_property($e, "in"));
215 # everything that follows will be deferred
216 $is_deferred = 1 if ($level ne "TOP");
218 my $array_size = shift @size_is;
225 if ($array_length = shift @length_is) {
228 $array_length = $array_size;
233 if (scalar(@size_is) == 0 and has_property($e, "string") and
234 $i == $e->{POINTERS}) {
236 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
237 delete($e->{PROPERTIES}->{string});
240 if ($array_size or $is_string) {
243 SIZE_IS => $array_size,
244 LENGTH_IS => $array_length,
245 IS_DEFERRED => $is_deferred,
247 IS_ZERO_TERMINATED => $is_string,
248 IS_VARYING => $is_varying,
249 IS_CONFORMANT => $is_conformant,
258 if (defined(has_property($e, "subcontext"))) {
259 my $hdr_size = has_property($e, "subcontext");
260 my $subsize = has_property($e, "subcontext_size");
261 if (not defined($subsize)) {
266 TYPE => "SUBCONTEXT",
267 HEADER_SIZE => $hdr_size,
268 SUBCONTEXT_SIZE => $subsize,
269 IS_DEFERRED => $is_deferred,
270 COMPRESSION => has_property($e, "compression"),
274 if (my $switch = has_property($e, "switch_is")) {
277 SWITCH_IS => $switch,
278 IS_DEFERRED => $is_deferred
282 if (scalar(@size_is) > 0) {
283 fatal($e, "size_is() on non-array element");
286 if (scalar(@length_is) > 0) {
287 fatal($e, "length_is() on non-array element");
290 if (has_property($e, "string")) {
291 fatal($e, "string() attribute on non-array element");
296 DATA_TYPE => $e->{TYPE},
297 IS_DEFERRED => $is_deferred,
298 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
299 IS_SURROUNDING => 0 #FIXME
303 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
308 sub GetTypedefLevelTable($$$)
310 my ($e, $data, $pointer_default) = @_;
319 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
324 #####################################################################
325 # see if a type contains any deferred data
326 sub can_contain_deferred($)
328 sub can_contain_deferred($);
331 return 1 unless (hasType($type)); # assume the worst
333 $type = getType($type);
335 return 0 if (Parse::Pidl::Typelist::is_scalar($type));
337 return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
339 return 0 unless defined($type->{ELEMENTS});
341 foreach (@{$type->{ELEMENTS}}) {
342 return 1 if ($_->{POINTERS});
343 return 1 if (can_contain_deferred ($_->{TYPE}));
353 return undef unless $e->{POINTERS};
355 return "ref" if (has_property($e, "ref"));
356 return "full" if (has_property($e, "ptr"));
357 return "sptr" if (has_property($e, "sptr"));
358 return "unique" if (has_property($e, "unique"));
359 return "relative" if (has_property($e, "relative"));
360 return "relative_short" if (has_property($e, "relative_short"));
361 return "ignore" if (has_property($e, "ignore"));
366 #####################################################################
367 # work out the correct alignment for a structure or union
368 sub find_largest_alignment($)
373 for my $e (@{$s->{ELEMENTS}}) {
376 if ($e->{POINTERS}) {
377 # this is a hack for NDR64
378 # the NDR layer translates this into
379 # an alignment of 4 for NDR and 8 for NDR64
381 } elsif (has_property($e, "subcontext")) {
383 } elsif (has_property($e, "transmit_as")) {
384 $a = align_type($e->{PROPERTIES}->{transmit_as});
386 $a = align_type($e->{TYPE});
389 $align = $a if ($align < $a);
395 #####################################################################
402 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
403 return $scalar_alignment->{$e->{NAME}};
406 return 0 if ($e eq "EMPTY");
408 unless (hasType($e)) {
409 # it must be an external type - all we can do is guess
410 # warning($e, "assuming alignment of unknown type '$e' is 4");
414 my $dt = getType($e);
416 if ($dt->{TYPE} eq "TYPEDEF") {
417 return align_type($dt->{DATA});
418 } elsif ($dt->{TYPE} eq "CONFORMANCE") {
419 return $dt->{DATA}->{ALIGN};
420 } elsif ($dt->{TYPE} eq "ENUM") {
421 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
422 } elsif ($dt->{TYPE} eq "BITMAP") {
423 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
424 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
425 # Struct/union without body: assume 4
426 return 4 unless (defined($dt->{ELEMENTS}));
427 return find_largest_alignment($dt);
430 die("Unknown data type type $dt->{TYPE}");
435 my ($e, $pointer_default) = @_;
437 $e->{TYPE} = expandAlias($e->{TYPE});
439 if (ref($e->{TYPE}) eq "HASH") {
440 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default);
446 PROPERTIES => $e->{PROPERTIES},
447 LEVELS => GetElementLevelTable($e, $pointer_default),
448 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
449 ALIGN => align_type($e->{TYPE}),
456 my ($struct, $pointer_default) = @_;
458 my $surrounding = undef;
462 NAME => $struct->{NAME},
463 SURROUNDING_ELEMENT => undef,
465 PROPERTIES => $struct->{PROPERTIES},
468 } unless defined($struct->{ELEMENTS});
470 CheckPointerTypes($struct, $pointer_default);
472 foreach my $x (@{$struct->{ELEMENTS}})
474 my $e = ParseElement($x, $pointer_default);
475 if ($x != $struct->{ELEMENTS}[-1] and
476 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
477 fatal($x, "conformant member not at end of struct");
482 my $e = $elements[-1];
483 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
484 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
488 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
489 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
490 $surrounding = $struct->{ELEMENTS}[-1];
494 if ($struct->{NAME}) {
495 $align = align_type($struct->{NAME});
500 NAME => $struct->{NAME},
501 SURROUNDING_ELEMENT => $surrounding,
502 ELEMENTS => \@elements,
503 PROPERTIES => $struct->{PROPERTIES},
511 my ($e, $pointer_default) = @_;
514 my $switch_type = has_property($e, "switch_type");
515 unless (defined($switch_type)) { $switch_type = "uint32"; }
516 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
521 SWITCH_TYPE => $switch_type,
523 PROPERTIES => $e->{PROPERTIES},
524 HAS_DEFAULT => $hasdefault,
527 } unless defined($e->{ELEMENTS});
529 CheckPointerTypes($e, $pointer_default);
531 foreach my $x (@{$e->{ELEMENTS}})
534 if ($x->{TYPE} eq "EMPTY") {
535 $t = { TYPE => "EMPTY" };
537 $t = ParseElement($x, $pointer_default);
539 if (has_property($x, "default")) {
540 $t->{CASE} = "default";
542 } elsif (defined($x->{PROPERTIES}->{case})) {
543 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
545 die("Union element $x->{NAME} has neither default nor case property");
552 $align = align_type($e->{NAME});
558 SWITCH_TYPE => $switch_type,
559 ELEMENTS => \@elements,
560 PROPERTIES => $e->{PROPERTIES},
561 HAS_DEFAULT => $hasdefault,
569 my ($e, $pointer_default) = @_;
574 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
575 ELEMENTS => $e->{ELEMENTS},
576 PROPERTIES => $e->{PROPERTIES},
583 my ($e, $pointer_default) = @_;
588 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
589 ELEMENTS => $e->{ELEMENTS},
590 PROPERTIES => $e->{PROPERTIES},
597 my ($d, $pointer_default) = @_;
600 STRUCT => \&ParseStruct,
601 UNION => \&ParseUnion,
603 BITMAP => \&ParseBitmap,
604 TYPEDEF => \&ParseTypedef,
605 }->{$d->{TYPE}}->($d, $pointer_default);
612 my ($d, $pointer_default) = @_;
616 if (ref($d->{DATA}) eq "HASH") {
617 if (defined($d->{DATA}->{PROPERTIES})
618 and not defined($d->{PROPERTIES})) {
619 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
622 $data = ParseType($d->{DATA}, $pointer_default);
623 $data->{ALIGN} = align_type($d->{NAME});
625 $data = getType($d->{DATA});
631 PROPERTIES => $d->{PROPERTIES},
632 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default),
645 sub ParseFunction($$$)
647 my ($ndr,$d,$opnum) = @_;
650 my $thisopnum = undef;
652 CheckPointerTypes($d, "ref");
654 if (not defined($d->{PROPERTIES}{noopnum})) {
655 $thisopnum = ${$opnum};
659 foreach my $x (@{$d->{ELEMENTS}}) {
660 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default});
661 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
662 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
664 push (@elements, $e);
667 if ($d->{RETURN_TYPE} ne "void") {
668 $rettype = expandAlias($d->{RETURN_TYPE});
675 RETURN_TYPE => $rettype,
676 PROPERTIES => $d->{PROPERTIES},
677 ELEMENTS => \@elements,
682 sub CheckPointerTypes($$)
684 my ($s,$default) = @_;
686 return unless defined($s->{ELEMENTS});
688 foreach my $e (@{$s->{ELEMENTS}}) {
689 if ($e->{POINTERS} and not defined(pointer_type($e))) {
690 $e->{PROPERTIES}->{$default} = '1';
695 sub FindNestedTypes($$)
697 sub FindNestedTypes($$);
700 return unless defined($t->{ELEMENTS});
701 return if ($t->{TYPE} eq "ENUM");
702 return if ($t->{TYPE} eq "BITMAP");
704 foreach (@{$t->{ELEMENTS}}) {
705 if (ref($_->{TYPE}) eq "HASH") {
706 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
707 FindNestedTypes($l, $_->{TYPE});
712 sub ParseInterface($)
722 if (not has_property($idl, "pointer_default")) {
723 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
724 # and "unique" in Microsoft Extensions mode (default)
725 $idl->{PROPERTIES}->{pointer_default} = "unique";
728 foreach my $d (@{$idl->{DATA}}) {
729 if ($d->{TYPE} eq "FUNCTION") {
730 push (@functions, ParseFunction($idl, $d, \$opnum));
731 } elsif ($d->{TYPE} eq "CONST") {
732 push (@consts, ParseConst($idl, $d));
734 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}));
735 FindNestedTypes(\@types, $d);
741 if(defined $idl->{PROPERTIES}->{version}) {
742 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
743 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
744 $version = $idl->{PROPERTIES}->{version};
746 $version = $if_version[1] << 16 | $if_version[0];
750 # If no endpoint is set, default to the interface name as a named pipe
751 if (!defined $idl->{PROPERTIES}->{endpoint}) {
752 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
754 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
758 NAME => $idl->{NAME},
759 UUID => lc(has_property($idl, "uuid")),
762 PROPERTIES => $idl->{PROPERTIES},
763 FUNCTIONS => \@functions,
766 ENDPOINTS => \@endpoints
770 # Convert a IDL tree to a NDR tree
771 # Gives a result tree describing all that's necessary for easily generating
772 # NDR parsers / generators
777 return undef unless (defined($idl));
779 Parse::Pidl::NDR::Validate($idl);
784 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
785 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
786 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
799 foreach my $l (@{$e->{LEVELS}}) {
800 return $l if ($seen);
801 ($seen = 1) if ($l == $fl);
812 foreach my $l (@{$e->{LEVELS}}) {
813 (return $prev) if ($l == $fl);
820 sub ContainsString($)
824 foreach my $l (@{$e->{LEVELS}}) {
825 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
831 sub ContainsDeferred($$)
835 return 1 if ($l->{CONTAINS_DEFERRED});
837 while ($l = GetNextLevel($e,$l))
839 return 1 if ($l->{IS_DEFERRED});
840 return 1 if ($l->{CONTAINS_DEFERRED});
849 my $name = "<ANONYMOUS>";
851 $name = $e->{NAME} if defined($e->{NAME});
853 if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
854 return "$e->{PARENT}->{NAME}.$name";
857 if (defined($e->{PARENT}) and
858 defined($e->{PARENT}->{PARENT}) and
859 defined($e->{PARENT}->{PARENT}->{NAME})) {
860 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
866 ###################################
867 # find a sibling var in a structure
871 my($fn) = $e->{PARENT};
873 if ($name =~ /\*(.*)/) {
877 for my $e2 (@{$fn->{ELEMENTS}}) {
878 return $e2 if ($e2->{NAME} eq $name);
884 my %property_list = (
886 "helpstring" => ["INTERFACE", "FUNCTION"],
887 "version" => ["INTERFACE"],
888 "uuid" => ["INTERFACE"],
889 "endpoint" => ["INTERFACE"],
890 "pointer_default" => ["INTERFACE"],
891 "helper" => ["INTERFACE"],
892 "pyhelper" => ["INTERFACE"],
893 "authservice" => ["INTERFACE"],
894 "restricted" => ["INTERFACE"],
895 "no_srv_register" => ["INTERFACE"],
898 "object" => ["INTERFACE"],
899 "local" => ["INTERFACE", "FUNCTION"],
900 "iid_is" => ["ELEMENT"],
901 "call_as" => ["FUNCTION"],
902 "idempotent" => ["FUNCTION"],
905 "noopnum" => ["FUNCTION"],
907 "out" => ["ELEMENT"],
910 "ref" => ["ELEMENT", "TYPEDEF"],
911 "ptr" => ["ELEMENT", "TYPEDEF"],
912 "unique" => ["ELEMENT", "TYPEDEF"],
913 "ignore" => ["ELEMENT"],
914 "relative" => ["ELEMENT", "TYPEDEF"],
915 "relative_short" => ["ELEMENT", "TYPEDEF"],
916 "null_is_ffffffff" => ["ELEMENT"],
917 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
919 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
920 "value" => ["ELEMENT"],
921 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
924 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
925 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
926 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
927 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
928 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
929 "nopython" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
930 "todo" => ["FUNCTION"],
933 "switch_is" => ["ELEMENT"],
934 "switch_type" => ["ELEMENT", "UNION"],
935 "nodiscriminant" => ["UNION"],
936 "case" => ["ELEMENT"],
937 "default" => ["ELEMENT"],
939 "represent_as" => ["ELEMENT"],
940 "transmit_as" => ["ELEMENT"],
943 "subcontext" => ["ELEMENT"],
944 "subcontext_size" => ["ELEMENT"],
945 "compression" => ["ELEMENT"],
948 "enum8bit" => ["ENUM"],
949 "enum16bit" => ["ENUM"],
950 "v1_enum" => ["ENUM"],
953 "bitmap8bit" => ["BITMAP"],
954 "bitmap16bit" => ["BITMAP"],
955 "bitmap32bit" => ["BITMAP"],
956 "bitmap64bit" => ["BITMAP"],
959 "range" => ["ELEMENT", "PIPE"],
960 "size_is" => ["ELEMENT"],
961 "string" => ["ELEMENT"],
962 "noheader" => ["ELEMENT"],
963 "charset" => ["ELEMENT"],
964 "length_is" => ["ELEMENT"],
967 #####################################################################
968 # check for unknown properties
969 sub ValidProperties($$)
973 return unless defined $e->{PROPERTIES};
975 foreach my $key (keys %{$e->{PROPERTIES}}) {
976 warning($e, el_name($e) . ": unknown property '$key'")
977 unless defined($property_list{$key});
979 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
980 unless grep(/^$t$/, @{$property_list{$key}});
988 return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
989 my $ti = getType($t);
991 if (not defined ($ti)) {
993 } elsif ($ti->{TYPE} eq "TYPEDEF") {
994 return mapToScalar($ti->{DATA});
995 } elsif ($ti->{TYPE} eq "ENUM") {
996 return Parse::Pidl::Typelist::enum_type_fn($ti);
997 } elsif ($ti->{TYPE} eq "BITMAP") {
998 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
1004 #####################################################################
1005 # validate an element
1010 ValidProperties($e,"ELEMENT");
1012 # Check whether switches are used correctly.
1013 if (my $switch = has_property($e, "switch_is")) {
1014 my $e2 = find_sibling($e, $switch);
1015 my $type = getType($e->{TYPE});
1017 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
1018 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1021 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
1022 my $discriminator_type = has_property($type->{DATA}, "switch_type");
1023 $discriminator_type = "uint32" unless defined ($discriminator_type);
1025 my $t1 = mapScalarType(mapToScalar($discriminator_type));
1027 if (not defined($t1)) {
1028 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1031 my $t2 = mapScalarType(mapToScalar($e2->{TYPE}));
1032 if (not defined($t2)) {
1033 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1037 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1042 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1043 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1046 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1047 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1050 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1051 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1054 if (has_property($e, "represent_as") and has_property($e, "value")) {
1055 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1058 if (has_property($e, "subcontext")) {
1059 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1062 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1063 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1066 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1067 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1070 if (!$e->{POINTERS} && (
1071 has_property($e, "ptr") or
1072 has_property($e, "unique") or
1073 has_property($e, "relative") or
1074 has_property($e, "relative_short") or
1075 has_property($e, "ref"))) {
1076 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
1080 #####################################################################
1086 ValidProperties($enum, "ENUM");
1089 #####################################################################
1095 ValidProperties($bitmap, "BITMAP");
1098 #####################################################################
1102 my($struct) = shift;
1104 ValidProperties($struct, "STRUCT");
1106 return unless defined($struct->{ELEMENTS});
1108 foreach my $e (@{$struct->{ELEMENTS}}) {
1109 $e->{PARENT} = $struct;
1114 #####################################################################
1120 ValidProperties($union,"UNION");
1122 if (has_property($union->{PARENT}, "nodiscriminant") and
1123 has_property($union->{PARENT}, "switch_type")) {
1124 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1127 return unless defined($union->{ELEMENTS});
1129 foreach my $e (@{$union->{ELEMENTS}}) {
1130 $e->{PARENT} = $union;
1132 if (defined($e->{PROPERTIES}->{default}) and
1133 defined($e->{PROPERTIES}->{case})) {
1134 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1137 unless (defined ($e->{PROPERTIES}->{default}) or
1138 defined ($e->{PROPERTIES}->{case})) {
1139 fatal($e, "Union member $e->{NAME} must have default or case property");
1142 if (has_property($e, "ref")) {
1143 fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1151 #####################################################################
1156 my $data = $pipe->{DATA};
1158 ValidProperties($pipe, "PIPE");
1160 fatal($pipe, $pipe->{NAME} . ": 'pipe' is not yet supported by pidl");
1163 #####################################################################
1167 my($typedef) = shift;
1168 my $data = $typedef->{DATA};
1170 ValidProperties($typedef, "TYPEDEF");
1172 return unless (ref($data) eq "HASH");
1174 $data->{PARENT} = $typedef;
1176 $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1177 $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1182 #####################################################################
1183 # validate a function
1184 sub ValidFunction($)
1188 ValidProperties($fn,"FUNCTION");
1190 foreach my $e (@{$fn->{ELEMENTS}}) {
1192 if (has_property($e, "ref") && !$e->{POINTERS}) {
1193 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1199 #####################################################################
1206 TYPEDEF => \&ValidTypedef,
1207 STRUCT => \&ValidStruct,
1208 UNION => \&ValidUnion,
1209 ENUM => \&ValidEnum,
1210 BITMAP => \&ValidBitmap,
1212 }->{$t->{TYPE}}->($t);
1215 #####################################################################
1216 # parse the interface definitions
1217 sub ValidInterface($)
1219 my($interface) = shift;
1220 my($data) = $interface->{DATA};
1222 if (has_property($interface, "helper")) {
1223 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1226 ValidProperties($interface,"INTERFACE");
1228 if (has_property($interface, "pointer_default")) {
1229 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1230 ("ref", "unique", "ptr"))) {
1231 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1235 if (has_property($interface, "object")) {
1236 if (has_property($interface, "version") &&
1237 $interface->{PROPERTIES}->{version} != 0) {
1238 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1241 if (!defined($interface->{BASE}) &&
1242 not ($interface->{NAME} eq "IUnknown")) {
1243 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1247 foreach my $d (@{$data}) {
1248 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1249 ($d->{TYPE} eq "TYPEDEF" or
1250 $d->{TYPE} eq "STRUCT" or
1251 $d->{TYPE} eq "UNION" or
1252 $d->{TYPE} eq "ENUM" or
1253 $d->{TYPE} eq "BITMAP" or
1254 $d->{TYPE} eq "PIPE") && ValidType($d);
1259 #####################################################################
1260 # Validate an IDL structure
1265 foreach my $x (@{$idl}) {
1266 ($x->{TYPE} eq "INTERFACE") &&
1268 ($x->{TYPE} eq "IMPORTLIB") &&
1269 fatal($x, "importlib() not supported");
1273 sub is_charset_array($$)
1277 return 0 if ($l->{TYPE} ne "ARRAY");
1279 my $nl = GetNextLevel($e,$l);
1281 return 0 unless ($nl->{TYPE} eq "DATA");
1283 return has_property($e, "charset");