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);
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"),
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 CONVERT_TO => has_property($e, ""),
261 CONVERT_FROM => has_property($e, ""),
262 DATA_TYPE => $e->{TYPE},
263 IS_DEFERRED => $is_deferred,
264 CONTAINS_DEFERRED => can_contain_deferred($e),
265 IS_SURROUNDING => 0 #FIXME
269 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
274 #####################################################################
275 # see if a type contains any deferred data
276 sub can_contain_deferred
280 return 0 if (Parse::Pidl::Typelist::is_scalar($e->{TYPE}));
281 return 1 unless (hasType($e->{TYPE})); # assume the worst
283 my $type = getType($e->{TYPE});
285 foreach my $x (@{$type->{DATA}->{ELEMENTS}}) {
286 return 1 if ($x->{POINTERS});
287 return 1 if (can_contain_deferred ($x));
297 return undef unless $e->{POINTERS};
299 return "ref" if (has_property($e, "ref"));
300 return "ptr" if (has_property($e, "ptr"));
301 return "sptr" if (has_property($e, "sptr"));
302 return "unique" if (has_property($e, "unique"));
303 return "relative" if (has_property($e, "relative"));
304 return "ignore" if (has_property($e, "ignore"));
309 #####################################################################
310 # work out the correct alignment for a structure or union
311 sub find_largest_alignment($)
316 for my $e (@{$s->{ELEMENTS}}) {
319 if ($e->{POINTERS}) {
321 } elsif (has_property($e, "subcontext")) {
323 } elsif (has_property($e, "transmit_as")) {
324 $a = align_type($e->{PROPERTIES}->{transmit_as});
326 $a = align_type($e->{TYPE});
329 $align = $a if ($align < $a);
335 #####################################################################
342 unless (hasType($e)) {
343 # it must be an external type - all we can do is guess
344 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
348 my $dt = getType($e)->{DATA};
350 if ($dt->{TYPE} eq "ENUM") {
351 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
352 } elsif ($dt->{TYPE} eq "BITMAP") {
353 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
354 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
355 return find_largest_alignment($dt);
356 } elsif ($dt->{TYPE} eq "SCALAR") {
357 return $scalar_alignment->{$dt->{NAME}};
360 die("Unknown data type type $dt->{TYPE}");
370 PROPERTIES => $e->{PROPERTIES},
371 LEVELS => GetElementLevelTable($e),
372 REPRESENTATION_TYPE => $e->{PROPERTIES}->{represent_as},
373 ALIGN => align_type($e->{TYPE}),
380 my ($ndr,$struct) = @_;
382 my $surrounding = undef;
384 foreach my $x (@{$struct->{ELEMENTS}})
386 my $e = ParseElement($x);
387 if ($x != $struct->{ELEMENTS}[-1] and
388 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
389 print "$x->{FILE}:$x->{LINE}: error: conformant member not at end of struct\n";
394 my $e = $elements[-1];
395 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
396 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
400 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
401 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
402 $surrounding = $struct->{ELEMENTS}[-1];
407 SURROUNDING_ELEMENT => $surrounding,
408 ELEMENTS => \@elements,
409 PROPERTIES => $struct->{PROPERTIES},
418 my $switch_type = has_property($e, "switch_type");
419 unless (defined($switch_type)) { $switch_type = "uint32"; }
421 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
424 foreach my $x (@{$e->{ELEMENTS}})
427 if ($x->{TYPE} eq "EMPTY") {
428 $t = { TYPE => "EMPTY" };
430 $t = ParseElement($x);
432 if (has_property($x, "default")) {
433 $t->{CASE} = "default";
435 } elsif (defined($x->{PROPERTIES}->{case})) {
436 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
438 die("Union element $x->{NAME} has neither default nor case property");
445 SWITCH_TYPE => $switch_type,
446 ELEMENTS => \@elements,
447 PROPERTIES => $e->{PROPERTIES},
448 HAS_DEFAULT => $hasdefault,
459 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
460 ELEMENTS => $e->{ELEMENTS},
461 PROPERTIES => $e->{PROPERTIES},
472 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
473 ELEMENTS => $e->{ELEMENTS},
474 PROPERTIES => $e->{PROPERTIES},
483 if ($d->{TYPE} eq "STRUCT" or $d->{TYPE} eq "UNION") {
484 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default});
488 STRUCT => \&ParseStruct,
489 UNION => \&ParseUnion,
491 BITMAP => \&ParseBitmap,
492 TYPEDEF => \&ParseTypedef,
493 }->{$d->{TYPE}}->($ndr, $d);
502 if (defined($d->{PROPERTIES}) && !defined($d->{DATA}->{PROPERTIES})) {
503 $d->{DATA}->{PROPERTIES} = $d->{PROPERTIES};
506 my $data = ParseType($ndr, $d->{DATA});
507 $data->{ALIGN} = align_type($d->{NAME});
512 PROPERTIES => $d->{PROPERTIES},
525 sub ParseFunction($$$)
527 my ($ndr,$d,$opnum) = @_;
530 my $thisopnum = undef;
532 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default_top});
534 if (not defined($d->{PROPERTIES}{noopnum})) {
535 $thisopnum = ${$opnum};
539 foreach my $x (@{$d->{ELEMENTS}}) {
540 my $e = ParseElement($x);
541 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
542 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
543 push (@elements, $e);
546 if ($d->{RETURN_TYPE} ne "void") {
547 $rettype = $d->{RETURN_TYPE};
551 if (has_property($d, "async")) { $async = 1; }
558 RETURN_TYPE => $rettype,
559 PROPERTIES => $d->{PROPERTIES},
560 ELEMENTS => \@elements,
565 sub CheckPointerTypes($$)
570 foreach my $e (@{$s->{ELEMENTS}}) {
571 if ($e->{POINTERS} and not defined(pointer_type($e))) {
572 $e->{PROPERTIES}->{$default} = 1;
577 sub ParseInterface($)
588 if (not has_property($idl, "pointer_default")) {
589 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
590 # and "unique" in Microsoft Extensions mode (default)
591 $idl->{PROPERTIES}->{pointer_default} = "unique";
594 if (not has_property($idl, "pointer_default_top")) {
595 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
598 foreach my $d (@{$idl->{DATA}}) {
599 if ($d->{TYPE} eq "DECLARE") {
600 push (@declares, $d);
601 } elsif ($d->{TYPE} eq "FUNCTION") {
602 push (@functions, ParseFunction($idl, $d, \$opnum));
603 } elsif ($d->{TYPE} eq "CONST") {
604 push (@consts, ParseConst($idl, $d));
606 push (@types, ParseType($idl, $d));
612 if(defined $idl->{PROPERTIES}->{version}) {
613 $version = $idl->{PROPERTIES}->{version};
616 # If no endpoint is set, default to the interface name as a named pipe
617 if (!defined $idl->{PROPERTIES}->{endpoint}) {
618 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
620 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
624 NAME => $idl->{NAME},
625 UUID => lc(has_property($idl, "uuid")),
628 PROPERTIES => $idl->{PROPERTIES},
629 FUNCTIONS => \@functions,
632 DECLARES => \@declares,
633 ENDPOINTS => \@endpoints
637 # Convert a IDL tree to a NDR tree
638 # Gives a result tree describing all that's necessary for easily generating
639 # NDR parsers / generators
644 return undef unless (defined($idl));
646 Parse::Pidl::NDR::Validate($idl);
651 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
664 foreach my $l (@{$e->{LEVELS}}) {
665 return $l if ($seen);
666 ($seen = 1) if ($l == $fl);
677 foreach my $l (@{$e->{LEVELS}}) {
678 (return $prev) if ($l == $fl);
685 sub ContainsString($)
689 foreach my $l (@{$e->{LEVELS}}) {
690 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
696 sub ContainsDeferred($$)
700 return 1 if ($l->{CONTAINS_DEFERRED});
702 while ($l = GetNextLevel($e,$l))
704 return 1 if ($l->{IS_DEFERRED});
705 return 1 if ($l->{CONTAINS_DEFERRED});
715 if ($e->{PARENT} && $e->{PARENT}->{NAME}) {
716 return "$e->{PARENT}->{NAME}.$e->{NAME}";
719 if ($e->{PARENT} && $e->{PARENT}->{PARENT}->{NAME}) {
720 return "$e->{PARENT}->{PARENT}->{NAME}.$e->{NAME}";
724 return "$e->{PARENT}->{NAME}.$e->{NAME}";
730 ###################################
731 # find a sibling var in a structure
735 my($fn) = $e->{PARENT};
737 if ($name =~ /\*(.*)/) {
741 for my $e2 (@{$fn->{ELEMENTS}}) {
742 return $e2 if ($e2->{NAME} eq $name);
748 my %property_list = (
750 "helpstring" => ["INTERFACE", "FUNCTION"],
751 "version" => ["INTERFACE"],
752 "uuid" => ["INTERFACE"],
753 "endpoint" => ["INTERFACE"],
754 "pointer_default" => ["INTERFACE"],
755 "pointer_default_top" => ["INTERFACE"],
756 "depends" => ["INTERFACE"],
757 "helper" => ["INTERFACE"],
758 "authservice" => ["INTERFACE"],
761 "object" => ["INTERFACE"],
762 "local" => ["INTERFACE", "FUNCTION"],
763 "iid_is" => ["ELEMENT"],
764 "call_as" => ["FUNCTION"],
765 "idempotent" => ["FUNCTION"],
768 "noopnum" => ["FUNCTION"],
770 "out" => ["ELEMENT"],
771 "async" => ["FUNCTION"],
774 "ref" => ["ELEMENT"],
775 "ptr" => ["ELEMENT"],
776 "sptr" => ["ELEMENT"],
777 "unique" => ["ELEMENT"],
778 "ignore" => ["ELEMENT"],
779 "relative" => ["ELEMENT"],
780 "relative_base" => ["TYPEDEF"],
782 "gensize" => ["TYPEDEF"],
783 "value" => ["ELEMENT"],
784 "flag" => ["ELEMENT", "TYPEDEF"],
787 "public" => ["FUNCTION", "TYPEDEF"],
788 "nopush" => ["FUNCTION", "TYPEDEF"],
789 "nopull" => ["FUNCTION", "TYPEDEF"],
790 "noprint" => ["FUNCTION", "TYPEDEF"],
791 "noejs" => ["FUNCTION", "TYPEDEF"],
794 "switch_is" => ["ELEMENT"],
795 "switch_type" => ["ELEMENT", "TYPEDEF"],
796 "nodiscriminant" => ["TYPEDEF"],
797 "case" => ["ELEMENT"],
798 "default" => ["ELEMENT"],
800 "represent_as" => ["ELEMENT"],
801 "transmit_as" => ["ELEMENT"],
804 "subcontext" => ["ELEMENT"],
805 "subcontext_size" => ["ELEMENT"],
806 "compression" => ["ELEMENT"],
809 "enum8bit" => ["TYPEDEF"],
810 "enum16bit" => ["TYPEDEF"],
811 "v1_enum" => ["TYPEDEF"],
814 "bitmap8bit" => ["TYPEDEF"],
815 "bitmap16bit" => ["TYPEDEF"],
816 "bitmap32bit" => ["TYPEDEF"],
817 "bitmap64bit" => ["TYPEDEF"],
820 "range" => ["ELEMENT"],
821 "size_is" => ["ELEMENT"],
822 "string" => ["ELEMENT"],
823 "noheader" => ["ELEMENT"],
824 "charset" => ["ELEMENT"],
825 "length_is" => ["ELEMENT"],
828 #####################################################################
829 # check for unknown properties
830 sub ValidProperties($$)
834 return unless defined $e->{PROPERTIES};
836 foreach my $key (keys %{$e->{PROPERTIES}}) {
837 fatal($e, el_name($e) . ": unknown property '$key'\n")
838 unless defined($property_list{$key});
840 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'\n")
841 unless grep($t, @{$property_list{$key}});
848 my $ti = getType($t);
850 if (not defined ($ti)) {
852 } elsif ($ti->{DATA}->{TYPE} eq "ENUM") {
853 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
854 } elsif ($ti->{DATA}->{TYPE} eq "BITMAP") {
855 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
856 } elsif ($ti->{DATA}->{TYPE} eq "SCALAR") {
863 #####################################################################
869 ValidProperties($e,"ELEMENT");
871 if (has_property($e, "ptr")) {
872 fatal($e, el_name($e) . " : pidl does not support full NDR pointers yet\n");
875 # Check whether switches are used correctly.
876 if (my $switch = has_property($e, "switch_is")) {
877 my $e2 = find_sibling($e, $switch);
878 my $type = getType($e->{TYPE});
880 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
881 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
884 if (!has_property($type, "nodiscriminant") and defined($e2)) {
885 my $discriminator_type = has_property($type, "switch_type");
886 $discriminator_type = "uint32" unless defined ($discriminator_type);
888 my $t1 = mapToScalar($discriminator_type);
890 if (not defined($t1)) {
891 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
894 my $t2 = mapToScalar($e2->{TYPE});
895 if (not defined($t2)) {
896 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
900 nonfatal($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
906 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
907 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
910 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
911 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
914 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
915 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
918 if (has_property($e, "represent_as") and has_property($e, "value")) {
919 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
922 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
923 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
926 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
927 fatal($e, el_name($e) . " : compression() on non-subcontext element");
930 if (!$e->{POINTERS} && (
931 has_property($e, "ptr") or
932 has_property($e, "sptr") or
933 has_property($e, "unique") or
934 has_property($e, "relative") or
935 has_property($e, "ref"))) {
936 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
940 #####################################################################
946 ValidProperties($struct,"STRUCT");
948 foreach my $e (@{$struct->{ELEMENTS}}) {
949 $e->{PARENT} = $struct;
954 #####################################################################
960 ValidProperties($union,"UNION");
962 if (has_property($union->{PARENT}, "nodiscriminant") and has_property($union->{PARENT}, "switch_type")) {
963 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type() on union without discriminant");
966 foreach my $e (@{$union->{ELEMENTS}}) {
967 $e->{PARENT} = $union;
969 if (defined($e->{PROPERTIES}->{default}) and
970 defined($e->{PROPERTIES}->{case})) {
971 fatal $e, "Union member $e->{NAME} can not have both default and case properties!\n";
974 unless (defined ($e->{PROPERTIES}->{default}) or
975 defined ($e->{PROPERTIES}->{case})) {
976 fatal $e, "Union member $e->{NAME} must have default or case property\n";
979 if (has_property($e, "ref")) {
980 fatal($e, el_name($e) . " : embedded ref pointers are not supported yet\n");
988 #####################################################################
992 my($typedef) = shift;
993 my $data = $typedef->{DATA};
995 ValidProperties($typedef,"TYPEDEF");
997 $data->{PARENT} = $typedef;
999 if (ref($data) eq "HASH") {
1000 if ($data->{TYPE} eq "STRUCT") {
1004 if ($data->{TYPE} eq "UNION") {
1010 #####################################################################
1012 sub ValidFunction($)
1016 ValidProperties($fn,"FUNCTION");
1018 foreach my $e (@{$fn->{ELEMENTS}}) {
1020 if (has_property($e, "ref") && !$e->{POINTERS}) {
1021 fatal $e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})\n";
1027 #####################################################################
1028 # parse the interface definitions
1029 sub ValidInterface($)
1031 my($interface) = shift;
1032 my($data) = $interface->{DATA};
1034 ValidProperties($interface,"INTERFACE");
1036 if (has_property($interface, "pointer_default") &&
1037 $interface->{PROPERTIES}->{pointer_default} eq "ptr") {
1038 fatal $interface, "Full pointers are not supported yet\n";
1041 if (has_property($interface, "object")) {
1042 if (has_property($interface, "version") &&
1043 $interface->{PROPERTIES}->{version} != 0) {
1044 fatal $interface, "Object interfaces must have version 0.0 ($interface->{NAME})\n";
1047 if (!defined($interface->{BASE}) &&
1048 not ($interface->{NAME} eq "IUnknown")) {
1049 fatal $interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})\n";
1053 foreach my $d (@{$data}) {
1054 ($d->{TYPE} eq "TYPEDEF") &&
1056 ($d->{TYPE} eq "FUNCTION") &&
1062 #####################################################################
1063 # Validate an IDL structure
1068 foreach my $x (@{$idl}) {
1069 ($x->{TYPE} eq "INTERFACE") &&