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, #???
77 'wrepl_nbt_name' => 4,
79 'ipv6address' => 4, #16?
84 sub GetElementLevelTable($$$)
86 my ($e, $pointer_default, $ms_union) = @_;
90 my @bracket_array = ();
95 if (has_property($e, "size_is")) {
96 @size_is = split /,/, has_property($e, "size_is");
99 if (has_property($e, "length_is")) {
100 @length_is = split /,/, has_property($e, "length_is");
103 if (defined($e->{ARRAY_LEN})) {
104 @bracket_array = @{$e->{ARRAY_LEN}};
107 if (has_property($e, "out")) {
110 if (has_property($e, "string") and not has_property($e, "in")) { $needptrs++; }
111 if ($#bracket_array >= 0) { $needptrs = 0; }
113 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
116 # Parse the [][][][] style array stuff
117 for my $i (0 .. $#bracket_array) {
118 my $d = $bracket_array[$#bracket_array - $i];
121 my $is_surrounding = 0;
123 my $is_conformant = 0;
130 if ($size = shift @size_is) {
131 if ($e->{POINTERS} < 1 and has_property($e, "string")) {
133 delete($e->{PROPERTIES}->{string});
135 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
137 delete($e->{PROPERTIES}->{string});
139 fatal($e, "Must specify size_is() for conformant array!")
142 if (($length = shift @length_is) or $is_string) {
148 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
149 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
154 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
155 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
157 if ($i == 0 and $is_fixed and has_property($e, "string")) {
161 delete($e->{PROPERTIES}->{string});
167 LENGTH_IS => $length,
168 IS_DEFERRED => $is_deferred,
169 IS_SURROUNDING => $is_surrounding,
170 IS_ZERO_TERMINATED => $is_string,
171 IS_VARYING => $is_varying,
172 IS_CONFORMANT => $is_conformant,
173 IS_FIXED => $is_fixed,
174 IS_INLINE => $is_inline
178 # Next, all the pointers
179 foreach my $i (1..$e->{POINTERS}) {
180 my $level = "EMBEDDED";
181 # Top level "ref" pointers do not have a referrent identifier
182 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
186 # Only the first level gets the pointer type from the
187 # pointer property, the others get them from
188 # the pointer_default() interface property
190 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
191 # (Here they talk about the rightmost pointer, but testing shows
192 # they mean the leftmost pointer.)
196 $pt = pointer_type($e);
198 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
199 $pt = $pointer_default;
205 POINTER_INDEX => $pointer_idx,
206 IS_DEFERRED => "$is_deferred",
210 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
211 if ($i == 1 and $pt ne "ref" and
212 $e->{PARENT}->{TYPE} eq "FUNCTION" and
213 not has_property($e, "in"));
217 # everything that follows will be deferred
218 $is_deferred = 1 if ($level ne "TOP");
220 my $array_size = shift @size_is;
227 if ($array_length = shift @length_is) {
230 $array_length = $array_size;
235 if (scalar(@size_is) == 0 and has_property($e, "string") and
236 $i == $e->{POINTERS}) {
238 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
239 delete($e->{PROPERTIES}->{string});
242 if ($array_size or $is_string) {
245 SIZE_IS => $array_size,
246 LENGTH_IS => $array_length,
247 IS_DEFERRED => $is_deferred,
249 IS_ZERO_TERMINATED => $is_string,
250 IS_VARYING => $is_varying,
251 IS_CONFORMANT => $is_conformant,
260 if (defined(has_property($e, "subcontext"))) {
261 my $hdr_size = has_property($e, "subcontext");
262 my $subsize = has_property($e, "subcontext_size");
263 if (not defined($subsize)) {
268 TYPE => "SUBCONTEXT",
269 HEADER_SIZE => $hdr_size,
270 SUBCONTEXT_SIZE => $subsize,
271 IS_DEFERRED => $is_deferred,
272 COMPRESSION => has_property($e, "compression"),
276 if (my $switch = has_property($e, "switch_is")) {
279 SWITCH_IS => $switch,
280 IS_DEFERRED => $is_deferred
284 if (scalar(@size_is) > 0) {
285 fatal($e, "size_is() on non-array element");
288 if (scalar(@length_is) > 0) {
289 fatal($e, "length_is() on non-array element");
292 if (has_property($e, "string")) {
293 fatal($e, "string() attribute on non-array element");
298 DATA_TYPE => $e->{TYPE},
299 IS_DEFERRED => $is_deferred,
300 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
301 IS_SURROUNDING => 0 #FIXME
305 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
310 sub GetTypedefLevelTable($$$$)
312 my ($e, $data, $pointer_default, $ms_union) = @_;
321 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
326 #####################################################################
327 # see if a type contains any deferred data
328 sub can_contain_deferred($)
330 sub can_contain_deferred($);
333 return 1 unless (hasType($type)); # assume the worst
335 $type = getType($type);
337 return 0 if (Parse::Pidl::Typelist::is_scalar($type));
339 return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
341 return 0 unless defined($type->{ELEMENTS});
343 foreach (@{$type->{ELEMENTS}}) {
344 return 1 if ($_->{POINTERS});
345 return 1 if (can_contain_deferred ($_->{TYPE}));
355 return undef unless $e->{POINTERS};
357 return "ref" if (has_property($e, "ref"));
358 return "full" if (has_property($e, "ptr"));
359 return "sptr" if (has_property($e, "sptr"));
360 return "unique" if (has_property($e, "unique"));
361 return "relative" if (has_property($e, "relative"));
362 return "relative_short" if (has_property($e, "relative_short"));
363 return "ignore" if (has_property($e, "ignore"));
368 #####################################################################
369 # work out the correct alignment for a structure or union
370 sub find_largest_alignment($)
375 for my $e (@{$s->{ELEMENTS}}) {
378 if ($e->{POINTERS}) {
379 # this is a hack for NDR64
380 # the NDR layer translates this into
381 # an alignment of 4 for NDR and 8 for NDR64
383 } elsif (has_property($e, "subcontext")) {
385 } elsif (has_property($e, "transmit_as")) {
386 $a = align_type($e->{PROPERTIES}->{transmit_as});
388 $a = align_type($e->{TYPE});
391 $align = $a if ($align < $a);
397 #####################################################################
404 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
405 return $scalar_alignment->{$e->{NAME}};
408 return 0 if ($e eq "EMPTY");
410 unless (hasType($e)) {
411 # it must be an external type - all we can do is guess
412 # warning($e, "assuming alignment of unknown type '$e' is 4");
416 my $dt = getType($e);
418 if ($dt->{TYPE} eq "TYPEDEF") {
419 return align_type($dt->{DATA});
420 } elsif ($dt->{TYPE} eq "CONFORMANCE") {
421 return $dt->{DATA}->{ALIGN};
422 } elsif ($dt->{TYPE} eq "ENUM") {
423 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
424 } elsif ($dt->{TYPE} eq "BITMAP") {
425 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
426 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
427 # Struct/union without body: assume 4
428 return 4 unless (defined($dt->{ELEMENTS}));
429 return find_largest_alignment($dt);
432 die("Unknown data type type $dt->{TYPE}");
435 sub ParseElement($$$)
437 my ($e, $pointer_default, $ms_union) = @_;
439 $e->{TYPE} = expandAlias($e->{TYPE});
441 if (ref($e->{TYPE}) eq "HASH") {
442 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default, $ms_union);
448 PROPERTIES => $e->{PROPERTIES},
449 LEVELS => GetElementLevelTable($e, $pointer_default, $ms_union),
450 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
451 ALIGN => align_type($e->{TYPE}),
458 my ($struct, $pointer_default, $ms_union) = @_;
460 my $surrounding = undef;
464 NAME => $struct->{NAME},
465 SURROUNDING_ELEMENT => undef,
467 PROPERTIES => $struct->{PROPERTIES},
470 } unless defined($struct->{ELEMENTS});
472 CheckPointerTypes($struct, $pointer_default);
474 foreach my $x (@{$struct->{ELEMENTS}})
476 my $e = ParseElement($x, $pointer_default, $ms_union);
477 if ($x != $struct->{ELEMENTS}[-1] and
478 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
479 fatal($x, "conformant member not at end of struct");
484 my $e = $elements[-1];
485 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
486 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
490 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
491 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
492 $surrounding = $struct->{ELEMENTS}[-1];
496 if ($struct->{NAME}) {
497 $align = align_type($struct->{NAME});
502 NAME => $struct->{NAME},
503 SURROUNDING_ELEMENT => $surrounding,
504 ELEMENTS => \@elements,
505 PROPERTIES => $struct->{PROPERTIES},
513 my ($e, $pointer_default, $ms_union) = @_;
515 my $is_ms_union = $ms_union;
516 $is_ms_union = 1 if has_property($e, "ms_union");
518 my $switch_type = has_property($e, "switch_type");
519 unless (defined($switch_type)) { $switch_type = "uint32"; }
520 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
525 SWITCH_TYPE => $switch_type,
527 PROPERTIES => $e->{PROPERTIES},
528 HAS_DEFAULT => $hasdefault,
529 IS_MS_UNION => $is_ms_union,
532 } unless defined($e->{ELEMENTS});
534 CheckPointerTypes($e, $pointer_default);
536 foreach my $x (@{$e->{ELEMENTS}})
539 if ($x->{TYPE} eq "EMPTY") {
540 $t = { TYPE => "EMPTY" };
542 $t = ParseElement($x, $pointer_default, $ms_union);
544 if (has_property($x, "default")) {
545 $t->{CASE} = "default";
547 } elsif (defined($x->{PROPERTIES}->{case})) {
548 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
550 die("Union element $x->{NAME} has neither default nor case property");
557 $align = align_type($e->{NAME});
563 SWITCH_TYPE => $switch_type,
564 ELEMENTS => \@elements,
565 PROPERTIES => $e->{PROPERTIES},
566 HAS_DEFAULT => $hasdefault,
567 IS_MS_UNION => $is_ms_union,
575 my ($e, $pointer_default, $ms_union) = @_;
580 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
581 ELEMENTS => $e->{ELEMENTS},
582 PROPERTIES => $e->{PROPERTIES},
589 my ($e, $pointer_default, $ms_union) = @_;
594 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
595 ELEMENTS => $e->{ELEMENTS},
596 PROPERTIES => $e->{PROPERTIES},
603 my ($d, $pointer_default, $ms_union) = @_;
606 STRUCT => \&ParseStruct,
607 UNION => \&ParseUnion,
609 BITMAP => \&ParseBitmap,
610 TYPEDEF => \&ParseTypedef,
611 }->{$d->{TYPE}}->($d, $pointer_default, $ms_union);
618 my ($d, $pointer_default, $ms_union) = @_;
622 if (ref($d->{DATA}) eq "HASH") {
623 if (defined($d->{DATA}->{PROPERTIES})
624 and not defined($d->{PROPERTIES})) {
625 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
628 $data = ParseType($d->{DATA}, $pointer_default, $ms_union);
629 $data->{ALIGN} = align_type($d->{NAME});
631 $data = getType($d->{DATA});
637 PROPERTIES => $d->{PROPERTIES},
638 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default, $ms_union),
651 sub ParseFunction($$$$)
653 my ($ndr,$d,$opnum,$ms_union) = @_;
656 my $thisopnum = undef;
658 CheckPointerTypes($d, "ref");
660 if (not defined($d->{PROPERTIES}{noopnum})) {
661 $thisopnum = ${$opnum};
665 foreach my $x (@{$d->{ELEMENTS}}) {
666 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default}, $ms_union);
667 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
668 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
670 push (@elements, $e);
673 if ($d->{RETURN_TYPE} ne "void") {
674 $rettype = expandAlias($d->{RETURN_TYPE});
681 RETURN_TYPE => $rettype,
682 PROPERTIES => $d->{PROPERTIES},
683 ELEMENTS => \@elements,
688 sub CheckPointerTypes($$)
690 my ($s,$default) = @_;
692 return unless defined($s->{ELEMENTS});
694 foreach my $e (@{$s->{ELEMENTS}}) {
695 if ($e->{POINTERS} and not defined(pointer_type($e))) {
696 $e->{PROPERTIES}->{$default} = '1';
701 sub FindNestedTypes($$)
703 sub FindNestedTypes($$);
706 return unless defined($t->{ELEMENTS});
707 return if ($t->{TYPE} eq "ENUM");
708 return if ($t->{TYPE} eq "BITMAP");
710 foreach (@{$t->{ELEMENTS}}) {
711 if (ref($_->{TYPE}) eq "HASH") {
712 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
713 FindNestedTypes($l, $_->{TYPE});
718 sub ParseInterface($)
728 $ms_union = 1 if has_property($idl, "ms_union");
730 if (not has_property($idl, "pointer_default")) {
731 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
732 # and "unique" in Microsoft Extensions mode (default)
733 $idl->{PROPERTIES}->{pointer_default} = "unique";
736 foreach my $d (@{$idl->{DATA}}) {
737 if ($d->{TYPE} eq "FUNCTION") {
738 push (@functions, ParseFunction($idl, $d, \$opnum, $ms_union));
739 } elsif ($d->{TYPE} eq "CONST") {
740 push (@consts, ParseConst($idl, $d));
742 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}, $ms_union));
743 FindNestedTypes(\@types, $d);
749 if(defined $idl->{PROPERTIES}->{version}) {
750 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
751 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
752 $version = $idl->{PROPERTIES}->{version};
754 $version = $if_version[1] << 16 | $if_version[0];
758 # If no endpoint is set, default to the interface name as a named pipe
759 if (!defined $idl->{PROPERTIES}->{endpoint}) {
760 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
762 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
766 NAME => $idl->{NAME},
767 UUID => lc(has_property($idl, "uuid")),
770 PROPERTIES => $idl->{PROPERTIES},
771 FUNCTIONS => \@functions,
774 ENDPOINTS => \@endpoints
778 # Convert a IDL tree to a NDR tree
779 # Gives a result tree describing all that's necessary for easily generating
780 # NDR parsers / generators
785 return undef unless (defined($idl));
787 Parse::Pidl::NDR::Validate($idl);
792 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
793 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
794 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
807 foreach my $l (@{$e->{LEVELS}}) {
808 return $l if ($seen);
809 ($seen = 1) if ($l == $fl);
820 foreach my $l (@{$e->{LEVELS}}) {
821 (return $prev) if ($l == $fl);
828 sub ContainsString($)
832 foreach my $l (@{$e->{LEVELS}}) {
833 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
839 sub ContainsDeferred($$)
843 return 1 if ($l->{CONTAINS_DEFERRED});
845 while ($l = GetNextLevel($e,$l))
847 return 1 if ($l->{IS_DEFERRED});
848 return 1 if ($l->{CONTAINS_DEFERRED});
857 my $name = "<ANONYMOUS>";
859 $name = $e->{NAME} if defined($e->{NAME});
861 if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
862 return "$e->{PARENT}->{NAME}.$name";
865 if (defined($e->{PARENT}) and
866 defined($e->{PARENT}->{PARENT}) and
867 defined($e->{PARENT}->{PARENT}->{NAME})) {
868 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
874 ###################################
875 # find a sibling var in a structure
879 my($fn) = $e->{PARENT};
881 if ($name =~ /\*(.*)/) {
885 for my $e2 (@{$fn->{ELEMENTS}}) {
886 return $e2 if ($e2->{NAME} eq $name);
892 my %property_list = (
894 "helpstring" => ["INTERFACE", "FUNCTION"],
895 "version" => ["INTERFACE"],
896 "uuid" => ["INTERFACE"],
897 "endpoint" => ["INTERFACE"],
898 "pointer_default" => ["INTERFACE"],
899 "helper" => ["INTERFACE"],
900 "pyhelper" => ["INTERFACE"],
901 "authservice" => ["INTERFACE"],
902 "restricted" => ["INTERFACE"],
903 "no_srv_register" => ["INTERFACE"],
906 "object" => ["INTERFACE"],
907 "local" => ["INTERFACE", "FUNCTION"],
908 "iid_is" => ["ELEMENT"],
909 "call_as" => ["FUNCTION"],
910 "idempotent" => ["FUNCTION"],
913 "noopnum" => ["FUNCTION"],
915 "out" => ["ELEMENT"],
918 "ref" => ["ELEMENT", "TYPEDEF"],
919 "ptr" => ["ELEMENT", "TYPEDEF"],
920 "unique" => ["ELEMENT", "TYPEDEF"],
921 "ignore" => ["ELEMENT"],
922 "relative" => ["ELEMENT", "TYPEDEF"],
923 "relative_short" => ["ELEMENT", "TYPEDEF"],
924 "null_is_ffffffff" => ["ELEMENT"],
925 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
927 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
928 "value" => ["ELEMENT"],
929 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
932 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
933 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
934 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
935 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
936 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
937 "nopython" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
938 "todo" => ["FUNCTION"],
941 "switch_is" => ["ELEMENT"],
942 "switch_type" => ["ELEMENT", "UNION"],
943 "nodiscriminant" => ["UNION"],
944 "ms_union" => ["INTERFACE", "UNION"],
945 "case" => ["ELEMENT"],
946 "default" => ["ELEMENT"],
948 "represent_as" => ["ELEMENT"],
949 "transmit_as" => ["ELEMENT"],
952 "subcontext" => ["ELEMENT"],
953 "subcontext_size" => ["ELEMENT"],
954 "compression" => ["ELEMENT"],
957 "enum8bit" => ["ENUM"],
958 "enum16bit" => ["ENUM"],
959 "v1_enum" => ["ENUM"],
962 "bitmap8bit" => ["BITMAP"],
963 "bitmap16bit" => ["BITMAP"],
964 "bitmap32bit" => ["BITMAP"],
965 "bitmap64bit" => ["BITMAP"],
968 "range" => ["ELEMENT", "PIPE"],
969 "size_is" => ["ELEMENT"],
970 "string" => ["ELEMENT"],
971 "noheader" => ["ELEMENT"],
972 "charset" => ["ELEMENT"],
973 "length_is" => ["ELEMENT"],
976 #####################################################################
977 # check for unknown properties
978 sub ValidProperties($$)
982 return unless defined $e->{PROPERTIES};
984 foreach my $key (keys %{$e->{PROPERTIES}}) {
985 warning($e, el_name($e) . ": unknown property '$key'")
986 unless defined($property_list{$key});
988 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
989 unless grep(/^$t$/, @{$property_list{$key}});
997 return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
998 my $ti = getType($t);
1000 if (not defined ($ti)) {
1002 } elsif ($ti->{TYPE} eq "TYPEDEF") {
1003 return mapToScalar($ti->{DATA});
1004 } elsif ($ti->{TYPE} eq "ENUM") {
1005 return Parse::Pidl::Typelist::enum_type_fn($ti);
1006 } elsif ($ti->{TYPE} eq "BITMAP") {
1007 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
1013 #####################################################################
1014 # validate an element
1019 ValidProperties($e,"ELEMENT");
1021 # Check whether switches are used correctly.
1022 if (my $switch = has_property($e, "switch_is")) {
1023 my $e2 = find_sibling($e, $switch);
1024 my $type = getType($e->{TYPE});
1026 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
1027 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1030 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
1031 my $discriminator_type = has_property($type->{DATA}, "switch_type");
1032 $discriminator_type = "uint32" unless defined ($discriminator_type);
1034 my $t1 = mapScalarType(mapToScalar($discriminator_type));
1036 if (not defined($t1)) {
1037 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1040 my $t2 = mapScalarType(mapToScalar($e2->{TYPE}));
1041 if (not defined($t2)) {
1042 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1046 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1051 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1052 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1055 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1056 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1059 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1060 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1063 if (has_property($e, "represent_as") and has_property($e, "value")) {
1064 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1067 if (has_property($e, "subcontext")) {
1068 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1071 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1072 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1075 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1076 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1079 if (!$e->{POINTERS} && (
1080 has_property($e, "ptr") or
1081 has_property($e, "unique") or
1082 has_property($e, "relative") or
1083 has_property($e, "relative_short") or
1084 has_property($e, "ref"))) {
1085 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
1089 #####################################################################
1095 ValidProperties($enum, "ENUM");
1098 #####################################################################
1104 ValidProperties($bitmap, "BITMAP");
1107 #####################################################################
1111 my($struct) = shift;
1113 ValidProperties($struct, "STRUCT");
1115 return unless defined($struct->{ELEMENTS});
1117 foreach my $e (@{$struct->{ELEMENTS}}) {
1118 $e->{PARENT} = $struct;
1123 #####################################################################
1129 ValidProperties($union,"UNION");
1131 if (has_property($union->{PARENT}, "nodiscriminant") and
1132 has_property($union->{PARENT}, "switch_type")) {
1133 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1136 return unless defined($union->{ELEMENTS});
1138 foreach my $e (@{$union->{ELEMENTS}}) {
1139 $e->{PARENT} = $union;
1141 if (defined($e->{PROPERTIES}->{default}) and
1142 defined($e->{PROPERTIES}->{case})) {
1143 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1146 unless (defined ($e->{PROPERTIES}->{default}) or
1147 defined ($e->{PROPERTIES}->{case})) {
1148 fatal($e, "Union member $e->{NAME} must have default or case property");
1151 if (has_property($e, "ref")) {
1152 fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1160 #####################################################################
1165 my $data = $pipe->{DATA};
1167 ValidProperties($pipe, "PIPE");
1169 fatal($pipe, $pipe->{NAME} . ": 'pipe' is not yet supported by pidl");
1172 #####################################################################
1176 my($typedef) = shift;
1177 my $data = $typedef->{DATA};
1179 ValidProperties($typedef, "TYPEDEF");
1181 return unless (ref($data) eq "HASH");
1183 $data->{PARENT} = $typedef;
1185 $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1186 $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1191 #####################################################################
1192 # validate a function
1193 sub ValidFunction($)
1197 ValidProperties($fn,"FUNCTION");
1199 foreach my $e (@{$fn->{ELEMENTS}}) {
1201 if (has_property($e, "ref") && !$e->{POINTERS}) {
1202 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1208 #####################################################################
1215 TYPEDEF => \&ValidTypedef,
1216 STRUCT => \&ValidStruct,
1217 UNION => \&ValidUnion,
1218 ENUM => \&ValidEnum,
1219 BITMAP => \&ValidBitmap,
1221 }->{$t->{TYPE}}->($t);
1224 #####################################################################
1225 # parse the interface definitions
1226 sub ValidInterface($)
1228 my($interface) = shift;
1229 my($data) = $interface->{DATA};
1231 if (has_property($interface, "helper")) {
1232 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1235 ValidProperties($interface,"INTERFACE");
1237 if (has_property($interface, "pointer_default")) {
1238 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1239 ("ref", "unique", "ptr"))) {
1240 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1244 if (has_property($interface, "object")) {
1245 if (has_property($interface, "version") &&
1246 $interface->{PROPERTIES}->{version} != 0) {
1247 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1250 if (!defined($interface->{BASE}) &&
1251 not ($interface->{NAME} eq "IUnknown")) {
1252 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1256 foreach my $d (@{$data}) {
1257 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1258 ($d->{TYPE} eq "TYPEDEF" or
1259 $d->{TYPE} eq "STRUCT" or
1260 $d->{TYPE} eq "UNION" or
1261 $d->{TYPE} eq "ENUM" or
1262 $d->{TYPE} eq "BITMAP" or
1263 $d->{TYPE} eq "PIPE") && ValidType($d);
1268 #####################################################################
1269 # Validate an IDL structure
1274 foreach my $x (@{$idl}) {
1275 ($x->{TYPE} eq "INTERFACE") &&
1277 ($x->{TYPE} eq "IMPORTLIB") &&
1278 fatal($x, "importlib() not supported");
1282 sub is_charset_array($$)
1286 return 0 if ($l->{TYPE} ne "ARRAY");
1288 my $nl = GetNextLevel($e,$l);
1290 return 0 unless ($nl->{TYPE} eq "DATA");
1292 return has_property($e, "charset");