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 #####################################################################
338 unless (hasType($e)) {
339 # it must be an external type - all we can do is guess
340 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
344 my $dt = getType($e)->{DATA};
346 if ($dt->{TYPE} eq "ENUM") {
347 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
348 } elsif ($dt->{TYPE} eq "BITMAP") {
349 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
350 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
351 return find_largest_alignment($dt);
352 } elsif ($dt->{TYPE} eq "SCALAR") {
353 return $scalar_alignment->{$dt->{NAME}};
356 die("Unknown data type type $dt->{TYPE}");
366 PROPERTIES => $e->{PROPERTIES},
367 LEVELS => GetElementLevelTable($e),
368 ALIGN => align_type($e->{TYPE}),
375 my ($ndr,$struct) = @_;
377 my $surrounding = undef;
379 foreach my $x (@{$struct->{ELEMENTS}})
381 my $e = ParseElement($x);
382 if ($x != $struct->{ELEMENTS}[-1] and
383 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
384 print "$x->{FILE}:$x->{LINE}: error: conformant member not at end of struct\n";
389 my $e = $elements[-1];
390 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
391 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
395 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
396 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
397 $surrounding = $struct->{ELEMENTS}[-1];
402 SURROUNDING_ELEMENT => $surrounding,
403 ELEMENTS => \@elements,
404 PROPERTIES => $struct->{PROPERTIES},
413 my $switch_type = has_property($e, "switch_type");
414 unless (defined($switch_type)) { $switch_type = "uint32"; }
416 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
419 foreach my $x (@{$e->{ELEMENTS}})
422 if ($x->{TYPE} eq "EMPTY") {
423 $t = { TYPE => "EMPTY" };
425 $t = ParseElement($x);
427 if (has_property($x, "default")) {
428 $t->{CASE} = "default";
430 } elsif (defined($x->{PROPERTIES}->{case})) {
431 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
433 die("Union element $x->{NAME} has neither default nor case property");
440 SWITCH_TYPE => $switch_type,
441 ELEMENTS => \@elements,
442 PROPERTIES => $e->{PROPERTIES},
443 HAS_DEFAULT => $hasdefault,
454 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
455 ELEMENTS => $e->{ELEMENTS},
456 PROPERTIES => $e->{PROPERTIES},
467 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
468 ELEMENTS => $e->{ELEMENTS},
469 PROPERTIES => $e->{PROPERTIES},
478 if ($d->{TYPE} eq "STRUCT" or $d->{TYPE} eq "UNION") {
479 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default});
483 STRUCT => \&ParseStruct,
484 UNION => \&ParseUnion,
486 BITMAP => \&ParseBitmap,
487 TYPEDEF => \&ParseTypedef,
488 }->{$d->{TYPE}}->($ndr, $d);
497 if (defined($d->{PROPERTIES}) && !defined($d->{DATA}->{PROPERTIES})) {
498 $d->{DATA}->{PROPERTIES} = $d->{PROPERTIES};
501 my $data = ParseType($ndr, $d->{DATA});
502 $data->{ALIGN} = align_type($d->{NAME});
507 PROPERTIES => $d->{PROPERTIES},
520 sub ParseFunction($$$)
522 my ($ndr,$d,$opnum) = @_;
525 my $thisopnum = undef;
527 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default_top});
529 if (not defined($d->{PROPERTIES}{noopnum})) {
530 $thisopnum = ${$opnum};
534 foreach my $x (@{$d->{ELEMENTS}}) {
535 my $e = ParseElement($x);
536 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
537 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
538 push (@elements, $e);
541 if ($d->{RETURN_TYPE} ne "void") {
542 $rettype = $d->{RETURN_TYPE};
546 if (has_property($d, "async")) { $async = 1; }
553 RETURN_TYPE => $rettype,
554 PROPERTIES => $d->{PROPERTIES},
555 ELEMENTS => \@elements,
560 sub CheckPointerTypes($$)
565 foreach my $e (@{$s->{ELEMENTS}}) {
566 if ($e->{POINTERS} and not defined(pointer_type($e))) {
567 $e->{PROPERTIES}->{$default} = 1;
572 sub ParseInterface($)
583 if (not has_property($idl, "pointer_default")) {
584 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
585 # and "unique" in Microsoft Extensions mode (default)
586 $idl->{PROPERTIES}->{pointer_default} = "unique";
589 if (not has_property($idl, "pointer_default_top")) {
590 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
593 foreach my $d (@{$idl->{DATA}}) {
594 if ($d->{TYPE} eq "DECLARE") {
595 push (@declares, $d);
596 } elsif ($d->{TYPE} eq "FUNCTION") {
597 push (@functions, ParseFunction($idl, $d, \$opnum));
598 } elsif ($d->{TYPE} eq "CONST") {
599 push (@consts, ParseConst($idl, $d));
601 push (@types, ParseType($idl, $d));
607 if(defined $idl->{PROPERTIES}->{version}) {
608 $version = $idl->{PROPERTIES}->{version};
611 # If no endpoint is set, default to the interface name as a named pipe
612 if (!defined $idl->{PROPERTIES}->{endpoint}) {
613 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
615 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
619 NAME => $idl->{NAME},
620 UUID => lc(has_property($idl, "uuid")),
623 PROPERTIES => $idl->{PROPERTIES},
624 FUNCTIONS => \@functions,
627 DECLARES => \@declares,
628 ENDPOINTS => \@endpoints
632 # Convert a IDL tree to a NDR tree
633 # Gives a result tree describing all that's necessary for easily generating
634 # NDR parsers / generators
639 return undef unless (defined($idl));
641 Parse::Pidl::NDR::Validate($idl);
646 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
659 foreach my $l (@{$e->{LEVELS}}) {
660 return $l if ($seen);
661 ($seen = 1) if ($l == $fl);
672 foreach my $l (@{$e->{LEVELS}}) {
673 (return $prev) if ($l == $fl);
680 sub ContainsString($)
684 foreach my $l (@{$e->{LEVELS}}) {
685 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
691 sub ContainsDeferred($$)
695 return 1 if ($l->{CONTAINS_DEFERRED});
697 while ($l = GetNextLevel($e,$l))
699 return 1 if ($l->{IS_DEFERRED});
700 return 1 if ($l->{CONTAINS_DEFERRED});
710 if ($e->{PARENT} && $e->{PARENT}->{NAME}) {
711 return "$e->{PARENT}->{NAME}.$e->{NAME}";
714 if ($e->{PARENT} && $e->{PARENT}->{PARENT}->{NAME}) {
715 return "$e->{PARENT}->{PARENT}->{NAME}.$e->{NAME}";
719 return "$e->{PARENT}->{NAME}.$e->{NAME}";
725 ###################################
726 # find a sibling var in a structure
730 my($fn) = $e->{PARENT};
732 if ($name =~ /\*(.*)/) {
736 for my $e2 (@{$fn->{ELEMENTS}}) {
737 return $e2 if ($e2->{NAME} eq $name);
743 my %property_list = (
745 "helpstring" => ["INTERFACE", "FUNCTION"],
746 "version" => ["INTERFACE"],
747 "uuid" => ["INTERFACE"],
748 "endpoint" => ["INTERFACE"],
749 "pointer_default" => ["INTERFACE"],
750 "pointer_default_top" => ["INTERFACE"],
751 "depends" => ["INTERFACE"],
752 "authservice" => ["INTERFACE"],
755 "object" => ["INTERFACE"],
756 "local" => ["INTERFACE", "FUNCTION"],
757 "iid_is" => ["ELEMENT"],
758 "call_as" => ["FUNCTION"],
759 "idempotent" => ["FUNCTION"],
762 "noopnum" => ["FUNCTION"],
764 "out" => ["ELEMENT"],
765 "async" => ["FUNCTION"],
768 "ref" => ["ELEMENT"],
769 "ptr" => ["ELEMENT"],
770 "sptr" => ["ELEMENT"],
771 "unique" => ["ELEMENT"],
772 "ignore" => ["ELEMENT"],
773 "relative" => ["ELEMENT"],
774 "relative_base" => ["TYPEDEF"],
776 "gensize" => ["TYPEDEF"],
777 "value" => ["ELEMENT"],
778 "flag" => ["ELEMENT", "TYPEDEF"],
781 "public" => ["FUNCTION", "TYPEDEF"],
782 "nopush" => ["FUNCTION", "TYPEDEF"],
783 "nopull" => ["FUNCTION", "TYPEDEF"],
784 "noprint" => ["FUNCTION", "TYPEDEF"],
785 "noejs" => ["FUNCTION", "TYPEDEF"],
788 "switch_is" => ["ELEMENT"],
789 "switch_type" => ["ELEMENT", "TYPEDEF"],
790 "nodiscriminant" => ["TYPEDEF"],
791 "case" => ["ELEMENT"],
792 "default" => ["ELEMENT"],
795 "subcontext" => ["ELEMENT"],
796 "subcontext_size" => ["ELEMENT"],
797 "compression" => ["ELEMENT"],
798 "obfuscation" => ["ELEMENT"],
801 "enum8bit" => ["TYPEDEF"],
802 "enum16bit" => ["TYPEDEF"],
803 "v1_enum" => ["TYPEDEF"],
806 "bitmap8bit" => ["TYPEDEF"],
807 "bitmap16bit" => ["TYPEDEF"],
808 "bitmap32bit" => ["TYPEDEF"],
809 "bitmap64bit" => ["TYPEDEF"],
812 "range" => ["ELEMENT"],
813 "size_is" => ["ELEMENT"],
814 "string" => ["ELEMENT"],
815 "noheader" => ["ELEMENT"],
816 "charset" => ["ELEMENT"],
817 "length_is" => ["ELEMENT"],
820 #####################################################################
821 # check for unknown properties
822 sub ValidProperties($$)
826 return unless defined $e->{PROPERTIES};
828 foreach my $key (keys %{$e->{PROPERTIES}}) {
829 fatal($e, el_name($e) . ": unknown property '$key'\n")
830 unless defined($property_list{$key});
832 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'\n")
833 unless grep($t, @{$property_list{$key}});
840 my $ti = getType($t);
842 if (not defined ($ti)) {
844 } elsif ($ti->{DATA}->{TYPE} eq "ENUM") {
845 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
846 } elsif ($ti->{DATA}->{TYPE} eq "BITMAP") {
847 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
848 } elsif ($ti->{DATA}->{TYPE} eq "SCALAR") {
855 #####################################################################
861 ValidProperties($e,"ELEMENT");
863 if (has_property($e, "ptr")) {
864 fatal($e, el_name($e) . " : pidl does not support full NDR pointers yet\n");
867 # Check whether switches are used correctly.
868 if (my $switch = has_property($e, "switch_is")) {
869 my $e2 = find_sibling($e, $switch);
870 my $type = getType($e->{TYPE});
872 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
873 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
876 if (!has_property($type, "nodiscriminant") and defined($e2)) {
877 my $discriminator_type = has_property($type, "switch_type");
878 $discriminator_type = "uint32" unless defined ($discriminator_type);
880 my $t1 = mapToScalar($discriminator_type);
882 if (not defined($t1)) {
883 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
886 my $t2 = mapToScalar($e2->{TYPE});
887 if (not defined($t2)) {
888 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
892 nonfatal($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
897 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
898 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
901 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
902 fatal($e, el_name($e) . " : compression() on non-subcontext element");
905 if (defined (has_property($e, "obfuscation")) and not defined(has_property($e, "subcontext"))) {
906 fatal($e, el_name($e) . " : obfuscation() on non-subcontext element");
909 if (!$e->{POINTERS} && (
910 has_property($e, "ptr") or
911 has_property($e, "sptr") or
912 has_property($e, "unique") or
913 has_property($e, "relative") or
914 has_property($e, "ref"))) {
915 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
919 #####################################################################
925 ValidProperties($struct,"STRUCT");
927 foreach my $e (@{$struct->{ELEMENTS}}) {
928 $e->{PARENT} = $struct;
933 #####################################################################
939 ValidProperties($union,"UNION");
941 if (has_property($union->{PARENT}, "nodiscriminant") and has_property($union->{PARENT}, "switch_type")) {
942 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type() on union without discriminant");
945 foreach my $e (@{$union->{ELEMENTS}}) {
946 $e->{PARENT} = $union;
948 if (defined($e->{PROPERTIES}->{default}) and
949 defined($e->{PROPERTIES}->{case})) {
950 fatal $e, "Union member $e->{NAME} can not have both default and case properties!\n";
953 unless (defined ($e->{PROPERTIES}->{default}) or
954 defined ($e->{PROPERTIES}->{case})) {
955 fatal $e, "Union member $e->{NAME} must have default or case property\n";
958 if (has_property($e, "ref")) {
959 fatal($e, el_name($e) . " : embedded ref pointers are not supported yet\n");
967 #####################################################################
971 my($typedef) = shift;
972 my $data = $typedef->{DATA};
974 ValidProperties($typedef,"TYPEDEF");
976 $data->{PARENT} = $typedef;
978 if (ref($data) eq "HASH") {
979 if ($data->{TYPE} eq "STRUCT") {
983 if ($data->{TYPE} eq "UNION") {
989 #####################################################################
995 ValidProperties($fn,"FUNCTION");
997 foreach my $e (@{$fn->{ELEMENTS}}) {
999 if (has_property($e, "ref") && !$e->{POINTERS}) {
1000 fatal $e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})\n";
1006 #####################################################################
1007 # parse the interface definitions
1008 sub ValidInterface($)
1010 my($interface) = shift;
1011 my($data) = $interface->{DATA};
1013 ValidProperties($interface,"INTERFACE");
1015 if (has_property($interface, "pointer_default") &&
1016 $interface->{PROPERTIES}->{pointer_default} eq "ptr") {
1017 fatal $interface, "Full pointers are not supported yet\n";
1020 if (has_property($interface, "object")) {
1021 if (has_property($interface, "version") &&
1022 $interface->{PROPERTIES}->{version} != 0) {
1023 fatal $interface, "Object interfaces must have version 0.0 ($interface->{NAME})\n";
1026 if (!defined($interface->{BASE}) &&
1027 not ($interface->{NAME} eq "IUnknown")) {
1028 fatal $interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})\n";
1032 foreach my $d (@{$data}) {
1033 ($d->{TYPE} eq "TYPEDEF") &&
1035 ($d->{TYPE} eq "FUNCTION") &&
1041 #####################################################################
1042 # Validate an IDL structure
1047 foreach my $x (@{$idl}) {
1048 ($x->{TYPE} eq "INTERFACE") &&