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
13 #####################################################################
14 # return a table describing the order in which the parts of an element
16 # Possible level types:
22 sub GetElementLevelTable($)
28 my @bracket_array = ();
32 if (util::has_property($e, "size_is")) {
33 @size_is = split /,/, util::has_property($e, "size_is");
36 if (util::has_property($e, "length_is")) {
37 @length_is = split /,/, util::has_property($e, "length_is");
40 if (defined($e->{ARRAY_LEN})) {
41 @bracket_array = @{$e->{ARRAY_LEN}};
44 # Parse the [][][][] style array stuff
45 foreach my $d (@bracket_array) {
48 my $is_surrounding = 0;
50 my $is_conformant = 0;
54 unless ($size = shift @size_is) {
55 print "$e->{FILE}:$e->{LINE}: Must specify size_is() for conformant array!\n";
59 if ($length = shift @length_is) {
65 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
66 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
75 IS_DEFERRED => "$is_deferred",
76 # Inline arrays (which are a pidl extension) are never encoded
77 # as surrounding the struct they're part of
78 IS_SURROUNDING => "$is_surrounding",
79 IS_VARYING => "$is_varying",
80 IS_CONFORMANT => "$is_conformant",
81 IS_FIXED => (not $is_conformant and util::is_constant($size)),
82 NO_METADATA => (not $is_conformant),
83 IS_INLINE => (not $is_conformant and not util::is_constant($size))
87 # Next, all the pointers
88 foreach my $i (1..$e->{POINTERS}) {
89 my $pt = pointer_type($e);
91 my $level = "EMBEDDED";
92 # Top level "ref" pointers do not have a referrent identifier
93 $level = "TOP" if ( defined($pt)
95 and $e->{PARENT}->{TYPE} eq "FUNCTION");
99 # for now, there can only be one pointer type per element
100 POINTER_TYPE => pointer_type($e),
101 IS_DEFERRED => "$is_deferred",
105 # everything that follows will be deferred
106 $is_deferred = 1 if ($e->{PARENT}->{TYPE} ne "FUNCTION");
110 if ($array_size = shift @size_is) {
112 if ($array_length = shift @length_is) {
115 $array_length = $array_size;
120 SIZE_IS => $array_size,
121 LENGTH_IS => $array_length,
122 IS_DEFERRED => "$is_deferred",
124 IS_VARYING => "$is_varying",
135 if (my $hdr_size = util::has_property($e, "subcontext")) {
136 my $subsize = util::has_property($e, "subcontext_size");
137 if (not defined($subsize)) {
142 TYPE => "SUBCONTEXT",
143 HEADER_SIZE => $hdr_size,
144 SUBCONTEXT_SIZE => $subsize,
145 IS_DEFERRED => $is_deferred,
146 COMPRESSION => util::has_property($e, "compression"),
147 OBFUSCATION => util::has_property($e, "obfuscation")
151 if (my $switch = util::has_property($e, "switch_is")) {
154 SWITCH_IS => $switch,
155 IS_DEFERRED => $is_deferred
161 DATA_TYPE => $e->{TYPE},
162 IS_DEFERRED => $is_deferred,
163 CONTAINS_DEFERRED => can_contain_deferred($e),
164 IS_SURROUNDING => is_surrounding_string($e)
168 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
173 #####################################################################
174 # see if a type contains any deferred data
175 sub can_contain_deferred
179 return 1 if ($e->{POINTERS});
180 return 0 if (is_scalar_type($e->{TYPE}));
181 return 0 if (util::has_property($e, "subcontext"));
182 return 1 unless (typelist::hasType($e->{TYPE})); # assume the worst
184 my $type = typelist::getType($e->{TYPE});
186 foreach my $x (@{$type->{DATA}->{ELEMENTS}}) {
187 return 1 if (can_contain_deferred ($x));
193 sub is_scalar_type($)
197 return 0 unless(typelist::hasType($type));
199 if (my $dt = typelist::getType($type)->{DATA}->{TYPE}) {
200 return 1 if ($dt eq "SCALAR" or $dt eq "ENUM" or $dt eq "BITMAP");
210 return undef unless $e->{POINTERS};
212 return "ref" if (util::has_property($e, "ref"));
213 return "ptr" if (util::has_property($e, "ptr"));
214 return "unique" if (util::has_property($e, "unique"));
215 return "relative" if (util::has_property($e, "relative"));
216 return "ignore" if (util::has_property($e, "ignore"));
221 sub is_surrounding_string($)
227 return ($e->{TYPE} eq "string") and ($e->{POINTERS} == 0)
228 and util::property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")
229 and $e->{PARENT}->{TYPE} ne "FUNCTION";
233 #####################################################################
234 # work out the correct alignment for a structure or union
235 sub find_largest_alignment($)
240 for my $e (@{$s->{ELEMENTS}}) {
243 if (Ndr::need_wire_pointer($e)) {
246 $a = align_type($e->{TYPE});
249 $align = $a if ($align < $a);
255 my %scalar_alignments =
274 "error_status_t" => 4,
284 #####################################################################
290 unless (typelist::hasType($e)) {
291 # it must be an external type - all we can do is guess
292 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
296 my $dt = typelist::getType($e)->{DATA};
298 if ($dt->{TYPE} eq "ENUM") {
299 return align_type(typelist::enum_type_fn($dt));
300 } elsif ($dt->{TYPE} eq "BITMAP") {
301 return align_type(typelist::bitmap_type_fn($dt));
302 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
303 return find_largest_alignment($dt);
304 } elsif ($dt->{TYPE} eq "SCALAR") {
305 return $scalar_alignments{$dt->{NAME}};
307 die("Unknown data type type $dt->{TYPE}");
311 # determine if an element needs a reference pointer on the wire
312 # in its NDR representation
313 sub need_wire_pointer($)
317 my $n = $e->{POINTERS};
318 my $pt = pointer_type($e);
320 # Top level "ref" pointers do not have a referrent identifier
323 and $e->{PARENT}->{TYPE} eq "FUNCTION")
338 PROPERTIES => $e->{PROPERTIES},
339 LEVELS => GetElementLevelTable($e)
347 my $surrounding = undef;
349 foreach my $x (@{$struct->{ELEMENTS}})
351 push @elements, ParseElement($x);
354 my $e = $elements[-1];
355 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
356 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
360 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
361 && util::property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
362 $surrounding = $struct->{ELEMENTS}[-1];
367 SURROUNDING_ELEMENT => $surrounding,
368 ELEMENTS => \@elements,
369 PROPERTIES => $struct->{PROPERTIES}
377 my $switch_type = util::has_property($e, "switch_type");
378 unless (defined($switch_type)) { $switch_type = "uint32"; }
380 if (util::has_property($e, "nodiscriminant")) { $switch_type = undef; }
382 foreach my $x (@{$e->{ELEMENTS}})
385 if ($x->{TYPE} eq "EMPTY") {
386 $t = { TYPE => "EMPTY" };
388 $t = ParseElement($x);
390 if (util::has_property($x, "default")) {
391 $t->{CASE} = "default";
392 } elsif (defined($x->{PROPERTIES}->{case})) {
393 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
395 die("Union element $x->{NAME} has neither default nor case property");
402 SWITCH_TYPE => $switch_type,
403 ELEMENTS => \@elements,
404 PROPERTIES => $e->{PROPERTIES}
414 BASE_TYPE => typelist::enum_type_fn($e),
415 ELEMENTS => $e->{ELEMENTS},
416 PROPERTIES => $e->{PROPERTIES}
426 BASE_TYPE => typelist::bitmap_type_fn($e),
427 ELEMENTS => $e->{ELEMENTS},
428 PROPERTIES => $e->{PROPERTIES}
445 if ($d->{DATA}->{TYPE} eq "STRUCT" or $d->{DATA}->{TYPE} eq "UNION") {
446 CheckPointerTypes($d->{DATA}, $ndr->{PROPERTIES}->{pointer_default});
449 if (defined($d->{PROPERTIES}) && !defined($d->{DATA}->{PROPERTIES})) {
450 $d->{DATA}->{PROPERTIES} = $d->{PROPERTIES};
453 if ($d->{DATA}->{TYPE} eq "STRUCT") {
454 $data = ParseStruct($d->{DATA});
455 } elsif ($d->{DATA}->{TYPE} eq "UNION") {
456 $data = ParseUnion($d->{DATA});
457 } elsif ($d->{DATA}->{TYPE} eq "ENUM") {
458 $data = ParseEnum($d->{DATA});
459 } elsif ($d->{DATA}->{TYPE} eq "BITMAP") {
460 $data = ParseBitmap($d->{DATA});
462 die("Unknown data type '$d->{DATA}->{TYPE}'");
465 $data->{ALIGN} = align_type($d->{NAME});
470 PROPERTIES => $d->{PROPERTIES},
483 sub ParseFunction($$$)
491 CheckPointerTypes($d,
492 $ndr->{PROPERTIES}->{pointer_default_top}
495 foreach my $x (@{$d->{ELEMENTS}}) {
496 my $e = ParseElement($x);
497 if (util::has_property($x, "in")) {
498 push (@{$e->{DIRECTION}}, "in");
501 if (util::has_property($x, "out")) {
502 push (@{$e->{DIRECTION}}, "out");
505 push (@elements, $e);
508 if ($d->{RETURN_TYPE} ne "void") {
509 $rettype = $d->{RETURN_TYPE};
516 RETURN_TYPE => $rettype,
517 PROPERTIES => $d->{PROPERTIES},
518 ELEMENTS => \@elements
522 sub CheckPointerTypes($$)
527 foreach my $e (@{$s->{ELEMENTS}}) {
528 if ($e->{POINTERS}) {
529 if (not defined(Ndr::pointer_type($e))) {
530 $e->{PROPERTIES}->{$default} = 1;
533 if (Ndr::pointer_type($e) eq "ptr") {
534 print "Warning: ptr is not supported by pidl yet\n";
540 sub ParseInterface($)
551 if (not util::has_property($idl, "pointer_default")) {
552 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
553 # and "unique" in Microsoft Extensions mode (default)
554 $idl->{PROPERTIES}->{pointer_default} = "unique";
557 if (not util::has_property($idl, "pointer_default_top")) {
558 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
561 foreach my $d (@{$idl->{DATA}}) {
562 if ($d->{TYPE} eq "TYPEDEF") {
563 push (@typedefs, ParseTypedef($idl, $d));
566 if ($d->{TYPE} eq "DECLARE") {
567 push (@declares, ParseDeclare($idl, $d));
570 if ($d->{TYPE} eq "FUNCTION") {
571 push (@functions, ParseFunction($idl, $d, $opnum));
575 if ($d->{TYPE} eq "CONST") {
576 push (@consts, ParseConst($idl, $d));
582 if(defined $idl->{PROPERTIES}->{version}) {
583 $version = $idl->{PROPERTIES}->{version};
586 # If no endpoint is set, default to the interface name as a named pipe
587 if (!defined $idl->{PROPERTIES}->{endpoint}) {
588 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
590 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
594 NAME => $idl->{NAME},
595 UUID => util::has_property($idl, "uuid"),
598 PROPERTIES => $idl->{PROPERTIES},
599 FUNCTIONS => \@functions,
601 TYPEDEFS => \@typedefs,
602 DECLARES => \@declares,
603 ENDPOINTS => \@endpoints
607 # Convert a IDL tree to a NDR tree
608 # Gives a result tree describing all that's necessary for easily generating
610 # - list of interfaces
611 # - list with functions
612 # - list with in elements
613 # - list with out elements
615 # - list with structs
616 # - alignment of structure
617 # - list with elements
619 # - alignment of union
620 # - list with elements
623 # - list with bitmaps
628 # properties are saved
629 # pointer types explicitly specified
635 foreach my $x (@{$idl}) {
636 push @ndr, ParseInterface($x);
649 foreach my $l (@{$e->{LEVELS}}) {
650 return $l if ($seen);
651 ($seen = 1) if ($l == $fl);
663 foreach my $l (@{$e->{LEVELS}}) {
664 (return $prev) if ($l == $fl);
671 sub ContainsDeferred($$)
677 return 1 if ($l->{IS_DEFERRED});
678 return 1 if ($l->{CONTAINS_DEFERRED});
679 } while ($l = Ndr::GetNextLevel($e,$l));