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 #####################################################################
17 # return a table describing the order in which the parts of an element
19 # Possible level types:
28 package Parse::Pidl::NDR;
31 use vars qw($VERSION);
34 @EXPORT = qw(GetPrevLevel GetNextLevel ContainsDeferred ContainsString);
37 use Parse::Pidl::Typelist qw(hasType getType);
38 use Parse::Pidl::Util qw(has_property property_matches);
40 # Alignment of the built-in scalar types
41 my $scalar_alignment = {
56 'string_array' => 4, #???
65 'wrepl_nbt_name' => 4,
74 warn ("$e->{FILE}:$e->{LINE}: Warning: $s\n");
77 #####################################################################
78 # signal a fatal validation error
82 die("$pos->{FILE}:$pos->{LINE}:$s\n");
85 sub GetElementLevelTable($)
91 my @bracket_array = ();
96 if (has_property($e, "size_is")) {
97 @size_is = split /,/, has_property($e, "size_is");
100 if (has_property($e, "length_is")) {
101 @length_is = split /,/, has_property($e, "length_is");
104 if (defined($e->{ARRAY_LEN})) {
105 @bracket_array = @{$e->{ARRAY_LEN}};
108 # Parse the [][][][] style array stuff
109 foreach my $d (@bracket_array) {
112 my $is_surrounding = 0;
114 my $is_conformant = 0;
119 if ($size = shift @size_is) {
120 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
122 delete($e->{PROPERTIES}->{string});
124 print "$e->{FILE}:$e->{LINE}: Must specify size_is() for conformant array!\n";
128 if (($length = shift @length_is) or $is_string) {
134 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
135 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
143 LENGTH_IS => $length,
144 IS_DEFERRED => "$is_deferred",
145 IS_SURROUNDING => "$is_surrounding",
146 IS_ZERO_TERMINATED => "$is_string",
147 IS_VARYING => "$is_varying",
148 IS_CONFORMANT => "$is_conformant",
149 IS_FIXED => (not $is_conformant and Parse::Pidl::Util::is_constant($size)),
150 IS_INLINE => (not $is_conformant and not Parse::Pidl::Util::is_constant($size))
154 # Next, all the pointers
155 foreach my $i (1..$e->{POINTERS}) {
156 my $pt = pointer_type($e);
158 my $level = "EMBEDDED";
159 # Top level "ref" pointers do not have a referrent identifier
160 $level = "TOP" if ( defined($pt)
162 and $e->{PARENT}->{TYPE} eq "FUNCTION");
166 # for now, there can only be one pointer type per element
167 POINTER_TYPE => pointer_type($e),
168 POINTER_INDEX => $pointer_idx,
169 IS_DEFERRED => "$is_deferred",
175 # everything that follows will be deferred
176 $is_deferred = 1 if ($e->{PARENT}->{TYPE} ne "FUNCTION");
178 my $array_size = shift @size_is;
185 if ($array_length = shift @length_is) {
188 $array_length = $array_size;
193 if (scalar(@size_is) == 0 and has_property($e, "string")) {
195 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
196 delete($e->{PROPERTIES}->{string});
199 if ($array_size or $is_string) {
202 IS_ZERO_TERMINATED => "$is_string",
203 SIZE_IS => $array_size,
204 LENGTH_IS => $array_length,
205 IS_DEFERRED => "$is_deferred",
207 IS_VARYING => "$is_varying",
208 IS_CONFORMANT => "$is_conformant",
217 if (defined(has_property($e, "subcontext"))) {
218 my $hdr_size = has_property($e, "subcontext");
219 my $subsize = has_property($e, "subcontext_size");
220 if (not defined($subsize)) {
225 TYPE => "SUBCONTEXT",
226 HEADER_SIZE => $hdr_size,
227 SUBCONTEXT_SIZE => $subsize,
228 IS_DEFERRED => $is_deferred,
229 COMPRESSION => has_property($e, "compression"),
230 OBFUSCATION => has_property($e, "obfuscation")
234 if (my $switch = has_property($e, "switch_is")) {
237 SWITCH_IS => $switch,
238 IS_DEFERRED => $is_deferred
242 if (scalar(@size_is) > 0) {
243 nonfatal($e, "size_is() on non-array element");
246 if (scalar(@length_is) > 0) {
247 nonfatal($e, "length_is() on non-array element");
250 if (has_property($e, "string")) {
251 nonfatal($e, "string() attribute on non-array element");
256 DATA_TYPE => $e->{TYPE},
257 IS_DEFERRED => $is_deferred,
258 CONTAINS_DEFERRED => can_contain_deferred($e),
259 IS_SURROUNDING => 0 #FIXME
263 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
268 #####################################################################
269 # see if a type contains any deferred data
270 sub can_contain_deferred
274 return 0 if (Parse::Pidl::Typelist::is_scalar($e->{TYPE}));
275 return 1 unless (hasType($e->{TYPE})); # assume the worst
277 my $type = getType($e->{TYPE});
279 foreach my $x (@{$type->{DATA}->{ELEMENTS}}) {
280 return 1 if ($x->{POINTERS});
281 return 1 if (can_contain_deferred ($x));
291 return undef unless $e->{POINTERS};
293 return "ref" if (has_property($e, "ref"));
294 return "ptr" if (has_property($e, "ptr"));
295 return "sptr" if (has_property($e, "sptr"));
296 return "unique" if (has_property($e, "unique"));
297 return "relative" if (has_property($e, "relative"));
298 return "ignore" if (has_property($e, "ignore"));
303 #####################################################################
304 # work out the correct alignment for a structure or union
305 sub find_largest_alignment($)
310 for my $e (@{$s->{ELEMENTS}}) {
313 if ($e->{POINTERS}) {
315 } elsif (has_property($e, "subcontext")){
318 $a = align_type($e->{TYPE});
321 $align = $a if ($align < $a);
327 #####################################################################
333 unless (hasType($e)) {
334 # it must be an external type - all we can do is guess
335 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
339 my $dt = getType($e)->{DATA};
341 if ($dt->{TYPE} eq "ENUM") {
342 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
343 } elsif ($dt->{TYPE} eq "BITMAP") {
344 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
345 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
346 return find_largest_alignment($dt);
347 } elsif ($dt->{TYPE} eq "SCALAR") {
348 return $scalar_alignment->{$dt->{NAME}};
351 die("Unknown data type type $dt->{TYPE}");
361 PROPERTIES => $e->{PROPERTIES},
362 LEVELS => GetElementLevelTable($e),
363 ALIGN => align_type($e->{TYPE}),
372 my $surrounding = undef;
374 foreach my $x (@{$struct->{ELEMENTS}})
376 my $e = ParseElement($x);
377 if ($x != $struct->{ELEMENTS}[-1] and
378 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
379 print "$x->{FILE}:$x->{LINE}: error: conformant member not at end of struct\n";
384 my $e = $elements[-1];
385 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
386 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
390 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
391 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
392 $surrounding = $struct->{ELEMENTS}[-1];
397 SURROUNDING_ELEMENT => $surrounding,
398 ELEMENTS => \@elements,
399 PROPERTIES => $struct->{PROPERTIES},
408 my $switch_type = has_property($e, "switch_type");
409 unless (defined($switch_type)) { $switch_type = "uint32"; }
411 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
414 foreach my $x (@{$e->{ELEMENTS}})
417 if ($x->{TYPE} eq "EMPTY") {
418 $t = { TYPE => "EMPTY" };
420 $t = ParseElement($x);
422 if (has_property($x, "default")) {
423 $t->{CASE} = "default";
425 } elsif (defined($x->{PROPERTIES}->{case})) {
426 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
428 die("Union element $x->{NAME} has neither default nor case property");
435 SWITCH_TYPE => $switch_type,
436 ELEMENTS => \@elements,
437 PROPERTIES => $e->{PROPERTIES},
438 HAS_DEFAULT => $hasdefault,
449 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
450 ELEMENTS => $e->{ELEMENTS},
451 PROPERTIES => $e->{PROPERTIES},
462 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
463 ELEMENTS => $e->{ELEMENTS},
464 PROPERTIES => $e->{PROPERTIES},
474 if ($d->{DATA}->{TYPE} eq "STRUCT" or $d->{DATA}->{TYPE} eq "UNION") {
475 CheckPointerTypes($d->{DATA}, $ndr->{PROPERTIES}->{pointer_default});
478 if (defined($d->{PROPERTIES}) && !defined($d->{DATA}->{PROPERTIES})) {
479 $d->{DATA}->{PROPERTIES} = $d->{PROPERTIES};
483 STRUCT => \&ParseStruct,
484 UNION => \&ParseUnion,
486 BITMAP => \&ParseBitmap
487 }->{$d->{DATA}->{TYPE}}->($d->{DATA});
489 $data->{ALIGN} = align_type($d->{NAME});
494 PROPERTIES => $d->{PROPERTIES},
507 sub ParseFunction($$$)
509 my ($ndr,$d,$opnum) = @_;
512 my $thisopnum = undef;
514 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default_top});
516 if (not defined($d->{PROPERTIES}{noopnum})) {
517 $thisopnum = ${$opnum};
521 foreach my $x (@{$d->{ELEMENTS}}) {
522 my $e = ParseElement($x);
523 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
524 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
525 push (@elements, $e);
528 if ($d->{RETURN_TYPE} ne "void") {
529 $rettype = $d->{RETURN_TYPE};
533 if (has_property($d, "async")) { $async = 1; }
540 RETURN_TYPE => $rettype,
541 PROPERTIES => $d->{PROPERTIES},
542 ELEMENTS => \@elements,
547 sub CheckPointerTypes($$)
552 foreach my $e (@{$s->{ELEMENTS}}) {
553 if ($e->{POINTERS} and not defined(pointer_type($e))) {
554 $e->{PROPERTIES}->{$default} = 1;
559 sub ParseInterface($)
570 if (not has_property($idl, "pointer_default")) {
571 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
572 # and "unique" in Microsoft Extensions mode (default)
573 $idl->{PROPERTIES}->{pointer_default} = "unique";
576 if (not has_property($idl, "pointer_default_top")) {
577 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
580 foreach my $d (@{$idl->{DATA}}) {
581 if ($d->{TYPE} eq "TYPEDEF") {
582 push (@typedefs, ParseTypedef($idl, $d));
585 if ($d->{TYPE} eq "DECLARE") {
586 push (@declares, $d);
589 if ($d->{TYPE} eq "FUNCTION") {
590 push (@functions, ParseFunction($idl, $d, \$opnum));
593 if ($d->{TYPE} eq "CONST") {
594 push (@consts, ParseConst($idl, $d));
600 if(defined $idl->{PROPERTIES}->{version}) {
601 $version = $idl->{PROPERTIES}->{version};
604 # If no endpoint is set, default to the interface name as a named pipe
605 if (!defined $idl->{PROPERTIES}->{endpoint}) {
606 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
608 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
612 NAME => $idl->{NAME},
613 UUID => lc(has_property($idl, "uuid")),
616 PROPERTIES => $idl->{PROPERTIES},
617 FUNCTIONS => \@functions,
619 TYPEDEFS => \@typedefs,
620 DECLARES => \@declares,
621 ENDPOINTS => \@endpoints
625 # Convert a IDL tree to a NDR tree
626 # Gives a result tree describing all that's necessary for easily generating
627 # NDR parsers / generators
633 push(@ndr, ParseInterface($_)) foreach (@{$idl});
645 foreach my $l (@{$e->{LEVELS}}) {
646 return $l if ($seen);
647 ($seen = 1) if ($l == $fl);
658 foreach my $l (@{$e->{LEVELS}}) {
659 (return $prev) if ($l == $fl);
666 sub ContainsString($)
670 foreach my $l (@{$e->{LEVELS}}) {
671 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
677 sub ContainsDeferred($$)
681 return 1 if ($l->{CONTAINS_DEFERRED});
683 while ($l = GetNextLevel($e,$l))
685 return 1 if ($l->{IS_DEFERRED});
686 return 1 if ($l->{CONTAINS_DEFERRED});
696 if ($e->{PARENT} && $e->{PARENT}->{NAME}) {
697 return "$e->{PARENT}->{NAME}.$e->{NAME}";
700 if ($e->{PARENT} && $e->{PARENT}->{PARENT}->{NAME}) {
701 return "$e->{PARENT}->{PARENT}->{NAME}.$e->{NAME}";
705 return "$e->{PARENT}->{NAME}.$e->{NAME}";
711 ###################################
712 # find a sibling var in a structure
716 my($fn) = $e->{PARENT};
718 if ($name =~ /\*(.*)/) {
722 for my $e2 (@{$fn->{ELEMENTS}}) {
723 return $e2 if ($e2->{NAME} eq $name);
729 my %property_list = (
731 "helpstring" => ["INTERFACE", "FUNCTION"],
732 "version" => ["INTERFACE"],
733 "uuid" => ["INTERFACE"],
734 "endpoint" => ["INTERFACE"],
735 "pointer_default" => ["INTERFACE"],
736 "pointer_default_top" => ["INTERFACE"],
737 "depends" => ["INTERFACE"],
738 "authservice" => ["INTERFACE"],
741 "object" => ["INTERFACE"],
742 "local" => ["INTERFACE", "FUNCTION"],
743 "iid_is" => ["ELEMENT"],
744 "call_as" => ["FUNCTION"],
745 "idempotent" => ["FUNCTION"],
748 "noopnum" => ["FUNCTION"],
750 "out" => ["ELEMENT"],
751 "async" => ["FUNCTION"],
754 "ref" => ["ELEMENT"],
755 "ptr" => ["ELEMENT"],
756 "sptr" => ["ELEMENT"],
757 "unique" => ["ELEMENT"],
758 "ignore" => ["ELEMENT"],
759 "relative" => ["ELEMENT"],
760 "relative_base" => ["TYPEDEF"],
762 "gensize" => ["TYPEDEF"],
763 "value" => ["ELEMENT"],
764 "flag" => ["ELEMENT", "TYPEDEF"],
767 "public" => ["FUNCTION", "TYPEDEF"],
768 "nopush" => ["FUNCTION", "TYPEDEF"],
769 "nopull" => ["FUNCTION", "TYPEDEF"],
770 "noprint" => ["FUNCTION", "TYPEDEF"],
771 "noejs" => ["FUNCTION", "TYPEDEF"],
774 "switch_is" => ["ELEMENT"],
775 "switch_type" => ["ELEMENT", "TYPEDEF"],
776 "nodiscriminant" => ["TYPEDEF"],
777 "case" => ["ELEMENT"],
778 "default" => ["ELEMENT"],
781 "subcontext" => ["ELEMENT"],
782 "subcontext_size" => ["ELEMENT"],
783 "compression" => ["ELEMENT"],
784 "obfuscation" => ["ELEMENT"],
787 "enum8bit" => ["TYPEDEF"],
788 "enum16bit" => ["TYPEDEF"],
789 "v1_enum" => ["TYPEDEF"],
792 "bitmap8bit" => ["TYPEDEF"],
793 "bitmap16bit" => ["TYPEDEF"],
794 "bitmap32bit" => ["TYPEDEF"],
795 "bitmap64bit" => ["TYPEDEF"],
798 "range" => ["ELEMENT"],
799 "size_is" => ["ELEMENT"],
800 "string" => ["ELEMENT"],
801 "noheader" => ["ELEMENT"],
802 "charset" => ["ELEMENT"],
803 "length_is" => ["ELEMENT"],
806 #####################################################################
807 # check for unknown properties
808 sub ValidProperties($$)
812 return unless defined $e->{PROPERTIES};
814 foreach my $key (keys %{$e->{PROPERTIES}}) {
815 fatal($e, el_name($e) . ": unknown property '$key'\n")
816 unless defined($property_list{$key});
818 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'\n")
819 unless grep($t, @{$property_list{$key}});
826 my $ti = getType($t);
828 if (not defined ($ti)) {
830 } elsif ($ti->{DATA}->{TYPE} eq "ENUM") {
831 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
832 } elsif ($ti->{DATA}->{TYPE} eq "BITMAP") {
833 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
834 } elsif ($ti->{DATA}->{TYPE} eq "SCALAR") {
841 #####################################################################
847 ValidProperties($e,"ELEMENT");
849 if (has_property($e, "ptr")) {
850 fatal($e, el_name($e) . " : pidl does not support full NDR pointers yet\n");
853 # Check whether switches are used correctly.
854 if (my $switch = has_property($e, "switch_is")) {
855 my $e2 = find_sibling($e, $switch);
856 my $type = getType($e->{TYPE});
858 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
859 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
862 if (!has_property($type, "nodiscriminant") and defined($e2)) {
863 my $discriminator_type = has_property($type, "switch_type");
864 $discriminator_type = "uint32" unless defined ($discriminator_type);
866 my $t1 = mapToScalar($discriminator_type);
868 if (not defined($t1)) {
869 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
872 my $t2 = mapToScalar($e2->{TYPE});
873 if (not defined($t2)) {
874 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
878 nonfatal($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
883 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
884 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
887 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
888 fatal($e, el_name($e) . " : compression() on non-subcontext element");
891 if (defined (has_property($e, "obfuscation")) and not defined(has_property($e, "subcontext"))) {
892 fatal($e, el_name($e) . " : obfuscation() on non-subcontext element");
895 if (!$e->{POINTERS} && (
896 has_property($e, "ptr") or
897 has_property($e, "sptr") or
898 has_property($e, "unique") or
899 has_property($e, "relative") or
900 has_property($e, "ref"))) {
901 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
905 #####################################################################
911 ValidProperties($struct,"STRUCT");
913 foreach my $e (@{$struct->{ELEMENTS}}) {
914 $e->{PARENT} = $struct;
919 #####################################################################
925 ValidProperties($union,"UNION");
927 if (has_property($union->{PARENT}, "nodiscriminant") and has_property($union->{PARENT}, "switch_type")) {
928 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type() on union without discriminant");
931 foreach my $e (@{$union->{ELEMENTS}}) {
932 $e->{PARENT} = $union;
934 if (defined($e->{PROPERTIES}->{default}) and
935 defined($e->{PROPERTIES}->{case})) {
936 fatal $e, "Union member $e->{NAME} can not have both default and case properties!\n";
939 unless (defined ($e->{PROPERTIES}->{default}) or
940 defined ($e->{PROPERTIES}->{case})) {
941 fatal $e, "Union member $e->{NAME} must have default or case property\n";
944 if (has_property($e, "ref")) {
945 fatal($e, el_name($e) . " : embedded ref pointers are not supported yet\n");
953 #####################################################################
957 my($typedef) = shift;
958 my $data = $typedef->{DATA};
960 ValidProperties($typedef,"TYPEDEF");
962 $data->{PARENT} = $typedef;
964 if (ref($data) eq "HASH") {
965 if ($data->{TYPE} eq "STRUCT") {
969 if ($data->{TYPE} eq "UNION") {
975 #####################################################################
981 ValidProperties($fn,"FUNCTION");
983 foreach my $e (@{$fn->{ELEMENTS}}) {
985 if (has_property($e, "ref") && !$e->{POINTERS}) {
986 fatal $e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})\n";
992 #####################################################################
993 # parse the interface definitions
994 sub ValidInterface($)
996 my($interface) = shift;
997 my($data) = $interface->{DATA};
999 ValidProperties($interface,"INTERFACE");
1001 if (has_property($interface, "pointer_default") &&
1002 $interface->{PROPERTIES}->{pointer_default} eq "ptr") {
1003 fatal $interface, "Full pointers are not supported yet\n";
1006 if (has_property($interface, "object")) {
1007 if (has_property($interface, "version") &&
1008 $interface->{PROPERTIES}->{version} != 0) {
1009 fatal $interface, "Object interfaces must have version 0.0 ($interface->{NAME})\n";
1012 if (!defined($interface->{BASE}) &&
1013 not ($interface->{NAME} eq "IUnknown")) {
1014 fatal $interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})\n";
1018 foreach my $d (@{$data}) {
1019 ($d->{TYPE} eq "TYPEDEF") &&
1021 ($d->{TYPE} eq "FUNCTION") &&
1027 #####################################################################
1028 # Validate an IDL structure
1033 foreach my $x (@{$idl}) {
1034 ($x->{TYPE} eq "INTERFACE") &&