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-2005
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);
40 use Parse::Pidl::Typelist qw(hasType getType);
41 use Parse::Pidl::Util qw(has_property property_matches);
43 # Alignment of the built-in scalar types
44 my $scalar_alignment = {
59 'string_array' => 4, #???
68 'wrepl_nbt_name' => 4,
77 warn ("$e->{FILE}:$e->{LINE}: Warning: $s\n");
80 #####################################################################
81 # signal a fatal validation error
85 die("$pos->{FILE}:$pos->{LINE}:$s\n");
88 sub GetElementLevelTable($)
94 my @bracket_array = ();
99 if (has_property($e, "size_is")) {
100 @size_is = split /,/, has_property($e, "size_is");
103 if (has_property($e, "length_is")) {
104 @length_is = split /,/, has_property($e, "length_is");
107 if (defined($e->{ARRAY_LEN})) {
108 @bracket_array = @{$e->{ARRAY_LEN}};
111 # Parse the [][][][] style array stuff
112 for my $i (0 .. $#bracket_array) {
113 my $d = $bracket_array[$#bracket_array - $i];
116 my $is_surrounding = 0;
118 my $is_conformant = 0;
123 if ($size = shift @size_is) {
124 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
126 delete($e->{PROPERTIES}->{string});
128 print "$e->{FILE}:$e->{LINE}: Must specify size_is() for conformant array!\n";
132 if (($length = shift @length_is) or $is_string) {
138 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
139 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
147 LENGTH_IS => $length,
148 IS_DEFERRED => "$is_deferred",
149 IS_SURROUNDING => "$is_surrounding",
150 IS_ZERO_TERMINATED => "$is_string",
151 IS_VARYING => "$is_varying",
152 IS_CONFORMANT => "$is_conformant",
153 IS_FIXED => (not $is_conformant and Parse::Pidl::Util::is_constant($size)),
154 IS_INLINE => (not $is_conformant and not Parse::Pidl::Util::is_constant($size))
158 # Next, all the pointers
159 foreach my $i (1..$e->{POINTERS}) {
160 my $pt = pointer_type($e);
162 my $level = "EMBEDDED";
163 # Top level "ref" pointers do not have a referrent identifier
164 $level = "TOP" if ( defined($pt)
166 and $e->{PARENT}->{TYPE} eq "FUNCTION");
170 # for now, there can only be one pointer type per element
171 POINTER_TYPE => pointer_type($e),
172 POINTER_INDEX => $pointer_idx,
173 IS_DEFERRED => "$is_deferred",
179 # everything that follows will be deferred
180 $is_deferred = 1 if ($e->{PARENT}->{TYPE} ne "FUNCTION");
182 my $array_size = shift @size_is;
189 if ($array_length = shift @length_is) {
192 $array_length = $array_size;
197 if (scalar(@size_is) == 0 and has_property($e, "string")) {
199 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
200 delete($e->{PROPERTIES}->{string});
203 if ($array_size or $is_string) {
206 IS_ZERO_TERMINATED => "$is_string",
207 SIZE_IS => $array_size,
208 LENGTH_IS => $array_length,
209 IS_DEFERRED => "$is_deferred",
211 IS_VARYING => "$is_varying",
212 IS_CONFORMANT => "$is_conformant",
221 if (defined(has_property($e, "subcontext"))) {
222 my $hdr_size = has_property($e, "subcontext");
223 my $subsize = has_property($e, "subcontext_size");
224 if (not defined($subsize)) {
229 TYPE => "SUBCONTEXT",
230 HEADER_SIZE => $hdr_size,
231 SUBCONTEXT_SIZE => $subsize,
232 IS_DEFERRED => $is_deferred,
233 COMPRESSION => has_property($e, "compression"),
234 OBFUSCATION => has_property($e, "obfuscation")
238 if (my $switch = has_property($e, "switch_is")) {
241 SWITCH_IS => $switch,
242 IS_DEFERRED => $is_deferred
246 if (scalar(@size_is) > 0) {
247 nonfatal($e, "size_is() on non-array element");
250 if (scalar(@length_is) > 0) {
251 nonfatal($e, "length_is() on non-array element");
254 if (has_property($e, "string")) {
255 nonfatal($e, "string() attribute on non-array element");
260 DATA_TYPE => $e->{TYPE},
261 IS_DEFERRED => $is_deferred,
262 CONTAINS_DEFERRED => can_contain_deferred($e),
263 IS_SURROUNDING => 0 #FIXME
267 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
272 #####################################################################
273 # see if a type contains any deferred data
274 sub can_contain_deferred
278 return 0 if (Parse::Pidl::Typelist::is_scalar($e->{TYPE}));
279 return 1 unless (hasType($e->{TYPE})); # assume the worst
281 my $type = getType($e->{TYPE});
283 foreach my $x (@{$type->{DATA}->{ELEMENTS}}) {
284 return 1 if ($x->{POINTERS});
285 return 1 if (can_contain_deferred ($x));
295 return undef unless $e->{POINTERS};
297 return "ref" if (has_property($e, "ref"));
298 return "ptr" if (has_property($e, "ptr"));
299 return "sptr" if (has_property($e, "sptr"));
300 return "unique" if (has_property($e, "unique"));
301 return "relative" if (has_property($e, "relative"));
302 return "ignore" if (has_property($e, "ignore"));
307 #####################################################################
308 # work out the correct alignment for a structure or union
309 sub find_largest_alignment($)
314 for my $e (@{$s->{ELEMENTS}}) {
317 if ($e->{POINTERS}) {
319 } elsif (has_property($e, "subcontext")){
322 $a = align_type($e->{TYPE});
325 $align = $a if ($align < $a);
331 #####################################################################
337 unless (hasType($e)) {
338 # it must be an external type - all we can do is guess
339 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
343 my $dt = getType($e)->{DATA};
345 if ($dt->{TYPE} eq "ENUM") {
346 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
347 } elsif ($dt->{TYPE} eq "BITMAP") {
348 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
349 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
350 return find_largest_alignment($dt);
351 } elsif ($dt->{TYPE} eq "SCALAR") {
352 return $scalar_alignment->{$dt->{NAME}};
355 die("Unknown data type type $dt->{TYPE}");
365 PROPERTIES => $e->{PROPERTIES},
366 LEVELS => GetElementLevelTable($e),
367 ALIGN => align_type($e->{TYPE}),
376 my $surrounding = undef;
378 foreach my $x (@{$struct->{ELEMENTS}})
380 my $e = ParseElement($x);
381 if ($x != $struct->{ELEMENTS}[-1] and
382 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
383 print "$x->{FILE}:$x->{LINE}: error: conformant member not at end of struct\n";
388 my $e = $elements[-1];
389 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
390 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
394 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
395 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
396 $surrounding = $struct->{ELEMENTS}[-1];
401 SURROUNDING_ELEMENT => $surrounding,
402 ELEMENTS => \@elements,
403 PROPERTIES => $struct->{PROPERTIES},
412 my $switch_type = has_property($e, "switch_type");
413 unless (defined($switch_type)) { $switch_type = "uint32"; }
415 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
418 foreach my $x (@{$e->{ELEMENTS}})
421 if ($x->{TYPE} eq "EMPTY") {
422 $t = { TYPE => "EMPTY" };
424 $t = ParseElement($x);
426 if (has_property($x, "default")) {
427 $t->{CASE} = "default";
429 } elsif (defined($x->{PROPERTIES}->{case})) {
430 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
432 die("Union element $x->{NAME} has neither default nor case property");
439 SWITCH_TYPE => $switch_type,
440 ELEMENTS => \@elements,
441 PROPERTIES => $e->{PROPERTIES},
442 HAS_DEFAULT => $hasdefault,
453 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
454 ELEMENTS => $e->{ELEMENTS},
455 PROPERTIES => $e->{PROPERTIES},
466 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
467 ELEMENTS => $e->{ELEMENTS},
468 PROPERTIES => $e->{PROPERTIES},
478 if ($d->{DATA}->{TYPE} eq "STRUCT" or $d->{DATA}->{TYPE} eq "UNION") {
479 CheckPointerTypes($d->{DATA}, $ndr->{PROPERTIES}->{pointer_default});
482 if (defined($d->{PROPERTIES}) && !defined($d->{DATA}->{PROPERTIES})) {
483 $d->{DATA}->{PROPERTIES} = $d->{PROPERTIES};
487 STRUCT => \&ParseStruct,
488 UNION => \&ParseUnion,
490 BITMAP => \&ParseBitmap
491 }->{$d->{DATA}->{TYPE}}->($d->{DATA});
493 $data->{ALIGN} = align_type($d->{NAME});
498 PROPERTIES => $d->{PROPERTIES},
511 sub ParseFunction($$$)
513 my ($ndr,$d,$opnum) = @_;
516 my $thisopnum = undef;
518 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default_top});
520 if (not defined($d->{PROPERTIES}{noopnum})) {
521 $thisopnum = ${$opnum};
525 foreach my $x (@{$d->{ELEMENTS}}) {
526 my $e = ParseElement($x);
527 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
528 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
529 push (@elements, $e);
532 if ($d->{RETURN_TYPE} ne "void") {
533 $rettype = $d->{RETURN_TYPE};
537 if (has_property($d, "async")) { $async = 1; }
544 RETURN_TYPE => $rettype,
545 PROPERTIES => $d->{PROPERTIES},
546 ELEMENTS => \@elements,
551 sub CheckPointerTypes($$)
556 foreach my $e (@{$s->{ELEMENTS}}) {
557 if ($e->{POINTERS} and not defined(pointer_type($e))) {
558 $e->{PROPERTIES}->{$default} = 1;
563 sub ParseInterface($)
574 if (not has_property($idl, "pointer_default")) {
575 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
576 # and "unique" in Microsoft Extensions mode (default)
577 $idl->{PROPERTIES}->{pointer_default} = "unique";
580 if (not has_property($idl, "pointer_default_top")) {
581 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
584 foreach my $d (@{$idl->{DATA}}) {
585 if ($d->{TYPE} eq "TYPEDEF") {
586 push (@typedefs, ParseTypedef($idl, $d));
589 if ($d->{TYPE} eq "DECLARE") {
590 push (@declares, $d);
593 if ($d->{TYPE} eq "FUNCTION") {
594 push (@functions, ParseFunction($idl, $d, \$opnum));
597 if ($d->{TYPE} eq "CONST") {
598 push (@consts, ParseConst($idl, $d));
604 if(defined $idl->{PROPERTIES}->{version}) {
605 $version = $idl->{PROPERTIES}->{version};
608 # If no endpoint is set, default to the interface name as a named pipe
609 if (!defined $idl->{PROPERTIES}->{endpoint}) {
610 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
612 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
616 NAME => $idl->{NAME},
617 UUID => lc(has_property($idl, "uuid")),
620 PROPERTIES => $idl->{PROPERTIES},
621 FUNCTIONS => \@functions,
623 TYPEDEFS => \@typedefs,
624 DECLARES => \@declares,
625 ENDPOINTS => \@endpoints
629 # Convert a IDL tree to a NDR tree
630 # Gives a result tree describing all that's necessary for easily generating
631 # NDR parsers / generators
636 return undef unless (defined($idl));
638 Parse::Pidl::NDR::Validate($idl);
642 push(@ndr, ParseInterface($_)) foreach (@{$idl});
654 foreach my $l (@{$e->{LEVELS}}) {
655 return $l if ($seen);
656 ($seen = 1) if ($l == $fl);
667 foreach my $l (@{$e->{LEVELS}}) {
668 (return $prev) if ($l == $fl);
675 sub ContainsString($)
679 foreach my $l (@{$e->{LEVELS}}) {
680 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
686 sub ContainsDeferred($$)
690 return 1 if ($l->{CONTAINS_DEFERRED});
692 while ($l = GetNextLevel($e,$l))
694 return 1 if ($l->{IS_DEFERRED});
695 return 1 if ($l->{CONTAINS_DEFERRED});
705 if ($e->{PARENT} && $e->{PARENT}->{NAME}) {
706 return "$e->{PARENT}->{NAME}.$e->{NAME}";
709 if ($e->{PARENT} && $e->{PARENT}->{PARENT}->{NAME}) {
710 return "$e->{PARENT}->{PARENT}->{NAME}.$e->{NAME}";
714 return "$e->{PARENT}->{NAME}.$e->{NAME}";
720 ###################################
721 # find a sibling var in a structure
725 my($fn) = $e->{PARENT};
727 if ($name =~ /\*(.*)/) {
731 for my $e2 (@{$fn->{ELEMENTS}}) {
732 return $e2 if ($e2->{NAME} eq $name);
738 my %property_list = (
740 "helpstring" => ["INTERFACE", "FUNCTION"],
741 "version" => ["INTERFACE"],
742 "uuid" => ["INTERFACE"],
743 "endpoint" => ["INTERFACE"],
744 "pointer_default" => ["INTERFACE"],
745 "pointer_default_top" => ["INTERFACE"],
746 "depends" => ["INTERFACE"],
747 "authservice" => ["INTERFACE"],
750 "object" => ["INTERFACE"],
751 "local" => ["INTERFACE", "FUNCTION"],
752 "iid_is" => ["ELEMENT"],
753 "call_as" => ["FUNCTION"],
754 "idempotent" => ["FUNCTION"],
757 "noopnum" => ["FUNCTION"],
759 "out" => ["ELEMENT"],
760 "async" => ["FUNCTION"],
763 "ref" => ["ELEMENT"],
764 "ptr" => ["ELEMENT"],
765 "sptr" => ["ELEMENT"],
766 "unique" => ["ELEMENT"],
767 "ignore" => ["ELEMENT"],
768 "relative" => ["ELEMENT"],
769 "relative_base" => ["TYPEDEF"],
771 "gensize" => ["TYPEDEF"],
772 "value" => ["ELEMENT"],
773 "flag" => ["ELEMENT", "TYPEDEF"],
776 "public" => ["FUNCTION", "TYPEDEF"],
777 "nopush" => ["FUNCTION", "TYPEDEF"],
778 "nopull" => ["FUNCTION", "TYPEDEF"],
779 "noprint" => ["FUNCTION", "TYPEDEF"],
780 "noejs" => ["FUNCTION", "TYPEDEF"],
783 "switch_is" => ["ELEMENT"],
784 "switch_type" => ["ELEMENT", "TYPEDEF"],
785 "nodiscriminant" => ["TYPEDEF"],
786 "case" => ["ELEMENT"],
787 "default" => ["ELEMENT"],
790 "subcontext" => ["ELEMENT"],
791 "subcontext_size" => ["ELEMENT"],
792 "compression" => ["ELEMENT"],
793 "obfuscation" => ["ELEMENT"],
796 "enum8bit" => ["TYPEDEF"],
797 "enum16bit" => ["TYPEDEF"],
798 "v1_enum" => ["TYPEDEF"],
801 "bitmap8bit" => ["TYPEDEF"],
802 "bitmap16bit" => ["TYPEDEF"],
803 "bitmap32bit" => ["TYPEDEF"],
804 "bitmap64bit" => ["TYPEDEF"],
807 "range" => ["ELEMENT"],
808 "size_is" => ["ELEMENT"],
809 "string" => ["ELEMENT"],
810 "noheader" => ["ELEMENT"],
811 "charset" => ["ELEMENT"],
812 "length_is" => ["ELEMENT"],
815 #####################################################################
816 # check for unknown properties
817 sub ValidProperties($$)
821 return unless defined $e->{PROPERTIES};
823 foreach my $key (keys %{$e->{PROPERTIES}}) {
824 fatal($e, el_name($e) . ": unknown property '$key'\n")
825 unless defined($property_list{$key});
827 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'\n")
828 unless grep($t, @{$property_list{$key}});
835 my $ti = getType($t);
837 if (not defined ($ti)) {
839 } elsif ($ti->{DATA}->{TYPE} eq "ENUM") {
840 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
841 } elsif ($ti->{DATA}->{TYPE} eq "BITMAP") {
842 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
843 } elsif ($ti->{DATA}->{TYPE} eq "SCALAR") {
850 #####################################################################
856 ValidProperties($e,"ELEMENT");
858 if (has_property($e, "ptr")) {
859 fatal($e, el_name($e) . " : pidl does not support full NDR pointers yet\n");
862 # Check whether switches are used correctly.
863 if (my $switch = has_property($e, "switch_is")) {
864 my $e2 = find_sibling($e, $switch);
865 my $type = getType($e->{TYPE});
867 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
868 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
871 if (!has_property($type, "nodiscriminant") and defined($e2)) {
872 my $discriminator_type = has_property($type, "switch_type");
873 $discriminator_type = "uint32" unless defined ($discriminator_type);
875 my $t1 = mapToScalar($discriminator_type);
877 if (not defined($t1)) {
878 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
881 my $t2 = mapToScalar($e2->{TYPE});
882 if (not defined($t2)) {
883 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
887 nonfatal($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
892 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
893 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
896 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
897 fatal($e, el_name($e) . " : compression() on non-subcontext element");
900 if (defined (has_property($e, "obfuscation")) and not defined(has_property($e, "subcontext"))) {
901 fatal($e, el_name($e) . " : obfuscation() on non-subcontext element");
904 if (!$e->{POINTERS} && (
905 has_property($e, "ptr") or
906 has_property($e, "sptr") or
907 has_property($e, "unique") or
908 has_property($e, "relative") or
909 has_property($e, "ref"))) {
910 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
914 #####################################################################
920 ValidProperties($struct,"STRUCT");
922 foreach my $e (@{$struct->{ELEMENTS}}) {
923 $e->{PARENT} = $struct;
928 #####################################################################
934 ValidProperties($union,"UNION");
936 if (has_property($union->{PARENT}, "nodiscriminant") and has_property($union->{PARENT}, "switch_type")) {
937 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type() on union without discriminant");
940 foreach my $e (@{$union->{ELEMENTS}}) {
941 $e->{PARENT} = $union;
943 if (defined($e->{PROPERTIES}->{default}) and
944 defined($e->{PROPERTIES}->{case})) {
945 fatal $e, "Union member $e->{NAME} can not have both default and case properties!\n";
948 unless (defined ($e->{PROPERTIES}->{default}) or
949 defined ($e->{PROPERTIES}->{case})) {
950 fatal $e, "Union member $e->{NAME} must have default or case property\n";
953 if (has_property($e, "ref")) {
954 fatal($e, el_name($e) . " : embedded ref pointers are not supported yet\n");
962 #####################################################################
966 my($typedef) = shift;
967 my $data = $typedef->{DATA};
969 ValidProperties($typedef,"TYPEDEF");
971 $data->{PARENT} = $typedef;
973 if (ref($data) eq "HASH") {
974 if ($data->{TYPE} eq "STRUCT") {
978 if ($data->{TYPE} eq "UNION") {
984 #####################################################################
990 ValidProperties($fn,"FUNCTION");
992 foreach my $e (@{$fn->{ELEMENTS}}) {
994 if (has_property($e, "ref") && !$e->{POINTERS}) {
995 fatal $e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})\n";
1001 #####################################################################
1002 # parse the interface definitions
1003 sub ValidInterface($)
1005 my($interface) = shift;
1006 my($data) = $interface->{DATA};
1008 ValidProperties($interface,"INTERFACE");
1010 if (has_property($interface, "pointer_default") &&
1011 $interface->{PROPERTIES}->{pointer_default} eq "ptr") {
1012 fatal $interface, "Full pointers are not supported yet\n";
1015 if (has_property($interface, "object")) {
1016 if (has_property($interface, "version") &&
1017 $interface->{PROPERTIES}->{version} != 0) {
1018 fatal $interface, "Object interfaces must have version 0.0 ($interface->{NAME})\n";
1021 if (!defined($interface->{BASE}) &&
1022 not ($interface->{NAME} eq "IUnknown")) {
1023 fatal $interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})\n";
1027 foreach my $d (@{$data}) {
1028 ($d->{TYPE} eq "TYPEDEF") &&
1030 ($d->{TYPE} eq "FUNCTION") &&
1036 #####################################################################
1037 # Validate an IDL structure
1042 foreach my $x (@{$idl}) {
1043 ($x->{TYPE} eq "INTERFACE") &&