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 = {
60 'string_array' => 4, #???
69 'wrepl_nbt_name' => 4,
78 warn ("$e->{FILE}:$e->{LINE}: Warning: $s\n");
81 #####################################################################
82 # signal a fatal validation error
86 die("$pos->{FILE}:$pos->{LINE}:$s\n");
89 sub GetElementLevelTable($)
95 my @bracket_array = ();
100 if (has_property($e, "size_is")) {
101 @size_is = split /,/, has_property($e, "size_is");
104 if (has_property($e, "length_is")) {
105 @length_is = split /,/, has_property($e, "length_is");
108 if (defined($e->{ARRAY_LEN})) {
109 @bracket_array = @{$e->{ARRAY_LEN}};
112 # Parse the [][][][] style array stuff
113 for my $i (0 .. $#bracket_array) {
114 my $d = $bracket_array[$#bracket_array - $i];
117 my $is_surrounding = 0;
119 my $is_conformant = 0;
124 if ($size = shift @size_is) {
125 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
127 delete($e->{PROPERTIES}->{string});
129 print "$e->{FILE}:$e->{LINE}: Must specify size_is() for conformant array!\n";
133 if (($length = shift @length_is) or $is_string) {
139 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
140 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
148 LENGTH_IS => $length,
149 IS_DEFERRED => "$is_deferred",
150 IS_SURROUNDING => "$is_surrounding",
151 IS_ZERO_TERMINATED => "$is_string",
152 IS_VARYING => "$is_varying",
153 IS_CONFORMANT => "$is_conformant",
154 IS_FIXED => (not $is_conformant and Parse::Pidl::Util::is_constant($size)),
155 IS_INLINE => (not $is_conformant and not Parse::Pidl::Util::is_constant($size))
159 # Next, all the pointers
160 foreach my $i (1..$e->{POINTERS}) {
161 my $pt = pointer_type($e);
163 my $level = "EMBEDDED";
164 # Top level "ref" pointers do not have a referrent identifier
165 $level = "TOP" if ( defined($pt)
167 and $e->{PARENT}->{TYPE} eq "FUNCTION");
171 # for now, there can only be one pointer type per element
172 POINTER_TYPE => pointer_type($e),
173 POINTER_INDEX => $pointer_idx,
174 IS_DEFERRED => "$is_deferred",
180 # everything that follows will be deferred
181 $is_deferred = 1 if ($e->{PARENT}->{TYPE} ne "FUNCTION");
183 my $array_size = shift @size_is;
190 if ($array_length = shift @length_is) {
193 $array_length = $array_size;
198 if (scalar(@size_is) == 0 and has_property($e, "string")) {
200 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
201 delete($e->{PROPERTIES}->{string});
204 if ($array_size or $is_string) {
207 IS_ZERO_TERMINATED => "$is_string",
208 SIZE_IS => $array_size,
209 LENGTH_IS => $array_length,
210 IS_DEFERRED => "$is_deferred",
212 IS_VARYING => "$is_varying",
213 IS_CONFORMANT => "$is_conformant",
222 if (defined(has_property($e, "subcontext"))) {
223 my $hdr_size = has_property($e, "subcontext");
224 my $subsize = has_property($e, "subcontext_size");
225 if (not defined($subsize)) {
230 TYPE => "SUBCONTEXT",
231 HEADER_SIZE => $hdr_size,
232 SUBCONTEXT_SIZE => $subsize,
233 IS_DEFERRED => $is_deferred,
234 COMPRESSION => has_property($e, "compression"),
235 OBFUSCATION => has_property($e, "obfuscation")
239 if (my $switch = has_property($e, "switch_is")) {
242 SWITCH_IS => $switch,
243 IS_DEFERRED => $is_deferred
247 if (scalar(@size_is) > 0) {
248 nonfatal($e, "size_is() on non-array element");
251 if (scalar(@length_is) > 0) {
252 nonfatal($e, "length_is() on non-array element");
255 if (has_property($e, "string")) {
256 nonfatal($e, "string() attribute on non-array element");
261 DATA_TYPE => $e->{TYPE},
262 IS_DEFERRED => $is_deferred,
263 CONTAINS_DEFERRED => can_contain_deferred($e),
264 IS_SURROUNDING => 0 #FIXME
268 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
273 #####################################################################
274 # see if a type contains any deferred data
275 sub can_contain_deferred
279 return 0 if (Parse::Pidl::Typelist::is_scalar($e->{TYPE}));
280 return 1 unless (hasType($e->{TYPE})); # assume the worst
282 my $type = getType($e->{TYPE});
284 foreach my $x (@{$type->{DATA}->{ELEMENTS}}) {
285 return 1 if ($x->{POINTERS});
286 return 1 if (can_contain_deferred ($x));
296 return undef unless $e->{POINTERS};
298 return "ref" if (has_property($e, "ref"));
299 return "ptr" if (has_property($e, "ptr"));
300 return "sptr" if (has_property($e, "sptr"));
301 return "unique" if (has_property($e, "unique"));
302 return "relative" if (has_property($e, "relative"));
303 return "ignore" if (has_property($e, "ignore"));
308 #####################################################################
309 # work out the correct alignment for a structure or union
310 sub find_largest_alignment($)
315 for my $e (@{$s->{ELEMENTS}}) {
318 if ($e->{POINTERS}) {
320 } elsif (has_property($e, "subcontext")) {
322 } elsif (has_property($e, "represent_as")) {
323 $a = align_type($e->{PROPERTIES}->{represent_as});
325 $a = align_type($e->{TYPE});
328 $align = $a if ($align < $a);
334 #####################################################################
341 unless (hasType($e)) {
342 # it must be an external type - all we can do is guess
343 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
347 my $dt = getType($e)->{DATA};
349 if ($dt->{TYPE} eq "ENUM") {
350 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
351 } elsif ($dt->{TYPE} eq "BITMAP") {
352 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
353 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
354 return find_largest_alignment($dt);
355 } elsif ($dt->{TYPE} eq "SCALAR") {
356 return $scalar_alignment->{$dt->{NAME}};
359 die("Unknown data type type $dt->{TYPE}");
369 PROPERTIES => $e->{PROPERTIES},
370 LEVELS => GetElementLevelTable($e),
371 ALIGN => align_type($e->{TYPE}),
378 my ($ndr,$struct) = @_;
380 my $surrounding = undef;
382 foreach my $x (@{$struct->{ELEMENTS}})
384 my $e = ParseElement($x);
385 if ($x != $struct->{ELEMENTS}[-1] and
386 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
387 print "$x->{FILE}:$x->{LINE}: error: conformant member not at end of struct\n";
392 my $e = $elements[-1];
393 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
394 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
398 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
399 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
400 $surrounding = $struct->{ELEMENTS}[-1];
405 SURROUNDING_ELEMENT => $surrounding,
406 ELEMENTS => \@elements,
407 PROPERTIES => $struct->{PROPERTIES},
416 my $switch_type = has_property($e, "switch_type");
417 unless (defined($switch_type)) { $switch_type = "uint32"; }
419 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
422 foreach my $x (@{$e->{ELEMENTS}})
425 if ($x->{TYPE} eq "EMPTY") {
426 $t = { TYPE => "EMPTY" };
428 $t = ParseElement($x);
430 if (has_property($x, "default")) {
431 $t->{CASE} = "default";
433 } elsif (defined($x->{PROPERTIES}->{case})) {
434 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
436 die("Union element $x->{NAME} has neither default nor case property");
443 SWITCH_TYPE => $switch_type,
444 ELEMENTS => \@elements,
445 PROPERTIES => $e->{PROPERTIES},
446 HAS_DEFAULT => $hasdefault,
457 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
458 ELEMENTS => $e->{ELEMENTS},
459 PROPERTIES => $e->{PROPERTIES},
470 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
471 ELEMENTS => $e->{ELEMENTS},
472 PROPERTIES => $e->{PROPERTIES},
481 if ($d->{TYPE} eq "STRUCT" or $d->{TYPE} eq "UNION") {
482 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default});
486 STRUCT => \&ParseStruct,
487 UNION => \&ParseUnion,
489 BITMAP => \&ParseBitmap,
490 TYPEDEF => \&ParseTypedef,
491 }->{$d->{TYPE}}->($ndr, $d);
500 if (defined($d->{PROPERTIES}) && !defined($d->{DATA}->{PROPERTIES})) {
501 $d->{DATA}->{PROPERTIES} = $d->{PROPERTIES};
504 my $data = ParseType($ndr, $d->{DATA});
505 $data->{ALIGN} = align_type($d->{NAME});
510 PROPERTIES => $d->{PROPERTIES},
523 sub ParseFunction($$$)
525 my ($ndr,$d,$opnum) = @_;
528 my $thisopnum = undef;
530 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default_top});
532 if (not defined($d->{PROPERTIES}{noopnum})) {
533 $thisopnum = ${$opnum};
537 foreach my $x (@{$d->{ELEMENTS}}) {
538 my $e = ParseElement($x);
539 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
540 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
541 push (@elements, $e);
544 if ($d->{RETURN_TYPE} ne "void") {
545 $rettype = $d->{RETURN_TYPE};
549 if (has_property($d, "async")) { $async = 1; }
556 RETURN_TYPE => $rettype,
557 PROPERTIES => $d->{PROPERTIES},
558 ELEMENTS => \@elements,
563 sub CheckPointerTypes($$)
568 foreach my $e (@{$s->{ELEMENTS}}) {
569 if ($e->{POINTERS} and not defined(pointer_type($e))) {
570 $e->{PROPERTIES}->{$default} = 1;
575 sub ParseInterface($)
586 if (not has_property($idl, "pointer_default")) {
587 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
588 # and "unique" in Microsoft Extensions mode (default)
589 $idl->{PROPERTIES}->{pointer_default} = "unique";
592 if (not has_property($idl, "pointer_default_top")) {
593 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
596 foreach my $d (@{$idl->{DATA}}) {
597 if ($d->{TYPE} eq "DECLARE") {
598 push (@declares, $d);
599 } elsif ($d->{TYPE} eq "FUNCTION") {
600 push (@functions, ParseFunction($idl, $d, \$opnum));
601 } elsif ($d->{TYPE} eq "CONST") {
602 push (@consts, ParseConst($idl, $d));
604 push (@types, ParseType($idl, $d));
610 if(defined $idl->{PROPERTIES}->{version}) {
611 $version = $idl->{PROPERTIES}->{version};
614 # If no endpoint is set, default to the interface name as a named pipe
615 if (!defined $idl->{PROPERTIES}->{endpoint}) {
616 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
618 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
622 NAME => $idl->{NAME},
623 UUID => lc(has_property($idl, "uuid")),
626 PROPERTIES => $idl->{PROPERTIES},
627 FUNCTIONS => \@functions,
630 DECLARES => \@declares,
631 ENDPOINTS => \@endpoints
635 # Convert a IDL tree to a NDR tree
636 # Gives a result tree describing all that's necessary for easily generating
637 # NDR parsers / generators
642 return undef unless (defined($idl));
644 Parse::Pidl::NDR::Validate($idl);
649 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
662 foreach my $l (@{$e->{LEVELS}}) {
663 return $l if ($seen);
664 ($seen = 1) if ($l == $fl);
675 foreach my $l (@{$e->{LEVELS}}) {
676 (return $prev) if ($l == $fl);
683 sub ContainsString($)
687 foreach my $l (@{$e->{LEVELS}}) {
688 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
694 sub ContainsDeferred($$)
698 return 1 if ($l->{CONTAINS_DEFERRED});
700 while ($l = GetNextLevel($e,$l))
702 return 1 if ($l->{IS_DEFERRED});
703 return 1 if ($l->{CONTAINS_DEFERRED});
713 if ($e->{PARENT} && $e->{PARENT}->{NAME}) {
714 return "$e->{PARENT}->{NAME}.$e->{NAME}";
717 if ($e->{PARENT} && $e->{PARENT}->{PARENT}->{NAME}) {
718 return "$e->{PARENT}->{PARENT}->{NAME}.$e->{NAME}";
722 return "$e->{PARENT}->{NAME}.$e->{NAME}";
728 ###################################
729 # find a sibling var in a structure
733 my($fn) = $e->{PARENT};
735 if ($name =~ /\*(.*)/) {
739 for my $e2 (@{$fn->{ELEMENTS}}) {
740 return $e2 if ($e2->{NAME} eq $name);
746 my %property_list = (
748 "helpstring" => ["INTERFACE", "FUNCTION"],
749 "version" => ["INTERFACE"],
750 "uuid" => ["INTERFACE"],
751 "endpoint" => ["INTERFACE"],
752 "pointer_default" => ["INTERFACE"],
753 "pointer_default_top" => ["INTERFACE"],
754 "depends" => ["INTERFACE"],
755 "helper" => ["INTERFACE"],
756 "authservice" => ["INTERFACE"],
759 "object" => ["INTERFACE"],
760 "local" => ["INTERFACE", "FUNCTION"],
761 "iid_is" => ["ELEMENT"],
762 "call_as" => ["FUNCTION"],
763 "idempotent" => ["FUNCTION"],
766 "noopnum" => ["FUNCTION"],
768 "out" => ["ELEMENT"],
769 "async" => ["FUNCTION"],
772 "ref" => ["ELEMENT"],
773 "ptr" => ["ELEMENT"],
774 "sptr" => ["ELEMENT"],
775 "unique" => ["ELEMENT"],
776 "ignore" => ["ELEMENT"],
777 "relative" => ["ELEMENT"],
778 "relative_base" => ["TYPEDEF"],
780 "gensize" => ["TYPEDEF"],
781 "value" => ["ELEMENT"],
782 "flag" => ["ELEMENT", "TYPEDEF"],
785 "public" => ["FUNCTION", "TYPEDEF"],
786 "nopush" => ["FUNCTION", "TYPEDEF"],
787 "nopull" => ["FUNCTION", "TYPEDEF"],
788 "noprint" => ["FUNCTION", "TYPEDEF"],
789 "noejs" => ["FUNCTION", "TYPEDEF"],
792 "switch_is" => ["ELEMENT"],
793 "switch_type" => ["ELEMENT", "TYPEDEF"],
794 "nodiscriminant" => ["TYPEDEF"],
795 "case" => ["ELEMENT"],
796 "default" => ["ELEMENT"],
798 "represent_as" => ["ELEMENT"],
801 "subcontext" => ["ELEMENT"],
802 "subcontext_size" => ["ELEMENT"],
803 "compression" => ["ELEMENT"],
804 "obfuscation" => ["ELEMENT"],
807 "enum8bit" => ["TYPEDEF"],
808 "enum16bit" => ["TYPEDEF"],
809 "v1_enum" => ["TYPEDEF"],
812 "bitmap8bit" => ["TYPEDEF"],
813 "bitmap16bit" => ["TYPEDEF"],
814 "bitmap32bit" => ["TYPEDEF"],
815 "bitmap64bit" => ["TYPEDEF"],
818 "range" => ["ELEMENT"],
819 "size_is" => ["ELEMENT"],
820 "string" => ["ELEMENT"],
821 "noheader" => ["ELEMENT"],
822 "charset" => ["ELEMENT"],
823 "length_is" => ["ELEMENT"],
826 #####################################################################
827 # check for unknown properties
828 sub ValidProperties($$)
832 return unless defined $e->{PROPERTIES};
834 foreach my $key (keys %{$e->{PROPERTIES}}) {
835 fatal($e, el_name($e) . ": unknown property '$key'\n")
836 unless defined($property_list{$key});
838 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'\n")
839 unless grep($t, @{$property_list{$key}});
846 my $ti = getType($t);
848 if (not defined ($ti)) {
850 } elsif ($ti->{DATA}->{TYPE} eq "ENUM") {
851 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
852 } elsif ($ti->{DATA}->{TYPE} eq "BITMAP") {
853 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
854 } elsif ($ti->{DATA}->{TYPE} eq "SCALAR") {
861 #####################################################################
867 ValidProperties($e,"ELEMENT");
869 if (has_property($e, "ptr")) {
870 fatal($e, el_name($e) . " : pidl does not support full NDR pointers yet\n");
873 # Check whether switches are used correctly.
874 if (my $switch = has_property($e, "switch_is")) {
875 my $e2 = find_sibling($e, $switch);
876 my $type = getType($e->{TYPE});
878 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
879 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
882 if (!has_property($type, "nodiscriminant") and defined($e2)) {
883 my $discriminator_type = has_property($type, "switch_type");
884 $discriminator_type = "uint32" unless defined ($discriminator_type);
886 my $t1 = mapToScalar($discriminator_type);
888 if (not defined($t1)) {
889 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
892 my $t2 = mapToScalar($e2->{TYPE});
893 if (not defined($t2)) {
894 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
898 nonfatal($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
903 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
904 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
907 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
908 fatal($e, el_name($e) . " : compression() on non-subcontext element");
911 if (defined (has_property($e, "obfuscation")) and not defined(has_property($e, "subcontext"))) {
912 fatal($e, el_name($e) . " : obfuscation() on non-subcontext element");
915 if (!$e->{POINTERS} && (
916 has_property($e, "ptr") or
917 has_property($e, "sptr") or
918 has_property($e, "unique") or
919 has_property($e, "relative") or
920 has_property($e, "ref"))) {
921 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
925 #####################################################################
931 ValidProperties($struct,"STRUCT");
933 foreach my $e (@{$struct->{ELEMENTS}}) {
934 $e->{PARENT} = $struct;
939 #####################################################################
945 ValidProperties($union,"UNION");
947 if (has_property($union->{PARENT}, "nodiscriminant") and has_property($union->{PARENT}, "switch_type")) {
948 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type() on union without discriminant");
951 foreach my $e (@{$union->{ELEMENTS}}) {
952 $e->{PARENT} = $union;
954 if (defined($e->{PROPERTIES}->{default}) and
955 defined($e->{PROPERTIES}->{case})) {
956 fatal $e, "Union member $e->{NAME} can not have both default and case properties!\n";
959 unless (defined ($e->{PROPERTIES}->{default}) or
960 defined ($e->{PROPERTIES}->{case})) {
961 fatal $e, "Union member $e->{NAME} must have default or case property\n";
964 if (has_property($e, "ref")) {
965 fatal($e, el_name($e) . " : embedded ref pointers are not supported yet\n");
973 #####################################################################
977 my($typedef) = shift;
978 my $data = $typedef->{DATA};
980 ValidProperties($typedef,"TYPEDEF");
982 $data->{PARENT} = $typedef;
984 if (ref($data) eq "HASH") {
985 if ($data->{TYPE} eq "STRUCT") {
989 if ($data->{TYPE} eq "UNION") {
995 #####################################################################
1001 ValidProperties($fn,"FUNCTION");
1003 foreach my $e (@{$fn->{ELEMENTS}}) {
1005 if (has_property($e, "ref") && !$e->{POINTERS}) {
1006 fatal $e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})\n";
1012 #####################################################################
1013 # parse the interface definitions
1014 sub ValidInterface($)
1016 my($interface) = shift;
1017 my($data) = $interface->{DATA};
1019 ValidProperties($interface,"INTERFACE");
1021 if (has_property($interface, "pointer_default") &&
1022 $interface->{PROPERTIES}->{pointer_default} eq "ptr") {
1023 fatal $interface, "Full pointers are not supported yet\n";
1026 if (has_property($interface, "object")) {
1027 if (has_property($interface, "version") &&
1028 $interface->{PROPERTIES}->{version} != 0) {
1029 fatal $interface, "Object interfaces must have version 0.0 ($interface->{NAME})\n";
1032 if (!defined($interface->{BASE}) &&
1033 not ($interface->{NAME} eq "IUnknown")) {
1034 fatal $interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})\n";
1038 foreach my $d (@{$data}) {
1039 ($d->{TYPE} eq "TYPEDEF") &&
1041 ($d->{TYPE} eq "FUNCTION") &&
1047 #####################################################################
1048 # Validate an IDL structure
1053 foreach my $x (@{$idl}) {
1054 ($x->{TYPE} eq "INTERFACE") &&