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 (defined(util::has_property($e, "subcontext"))) {
136 my $hdr_size = util::has_property($e, "subcontext");
137 my $subsize = util::has_property($e, "subcontext_size");
138 if (not defined($subsize)) {
143 TYPE => "SUBCONTEXT",
144 HEADER_SIZE => $hdr_size,
145 SUBCONTEXT_SIZE => $subsize,
146 IS_DEFERRED => $is_deferred,
147 COMPRESSION => util::has_property($e, "compression"),
148 OBFUSCATION => util::has_property($e, "obfuscation")
152 if (my $switch = util::has_property($e, "switch_is")) {
155 SWITCH_IS => $switch,
156 IS_DEFERRED => $is_deferred
162 DATA_TYPE => $e->{TYPE},
163 IS_DEFERRED => $is_deferred,
164 CONTAINS_DEFERRED => can_contain_deferred($e),
165 IS_SURROUNDING => is_surrounding_string($e)
169 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
174 #####################################################################
175 # see if a type contains any deferred data
176 sub can_contain_deferred
180 return 1 if ($e->{POINTERS});
181 return 0 if (typelist::is_scalar($e->{TYPE}));
182 return 0 if (defined(util::has_property($e, "subcontext")));
183 return 1 unless (typelist::hasType($e->{TYPE})); # assume the worst
185 my $type = typelist::getType($e->{TYPE});
187 foreach my $x (@{$type->{DATA}->{ELEMENTS}}) {
188 return 1 if (can_contain_deferred ($x));
198 return undef unless $e->{POINTERS};
200 return "ref" if (util::has_property($e, "ref"));
201 return "ptr" if (util::has_property($e, "ptr"));
202 return "unique" if (util::has_property($e, "unique"));
203 return "relative" if (util::has_property($e, "relative"));
204 return "ignore" if (util::has_property($e, "ignore"));
209 sub is_surrounding_string($)
215 return ($e->{TYPE} eq "string") and ($e->{POINTERS} == 0)
216 and util::property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")
217 and $e->{PARENT}->{TYPE} ne "FUNCTION";
221 #####################################################################
222 # work out the correct alignment for a structure or union
223 sub find_largest_alignment($)
228 for my $e (@{$s->{ELEMENTS}}) {
231 if (Ndr::need_wire_pointer($e)) {
234 $a = align_type($e->{TYPE});
237 $align = $a if ($align < $a);
243 #####################################################################
249 unless (typelist::hasType($e)) {
250 # it must be an external type - all we can do is guess
251 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
255 my $dt = typelist::getType($e)->{DATA};
257 if ($dt->{TYPE} eq "ENUM") {
258 return align_type(typelist::enum_type_fn($dt));
259 } elsif ($dt->{TYPE} eq "BITMAP") {
260 return align_type(typelist::bitmap_type_fn($dt));
261 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
262 return find_largest_alignment($dt);
263 } elsif ($dt->{TYPE} eq "SCALAR") {
264 return typelist::getScalarAlignment($dt->{NAME});
267 die("Unknown data type type $dt->{TYPE}");
270 # determine if an element needs a reference pointer on the wire
271 # in its NDR representation
272 sub need_wire_pointer($)
276 my $n = $e->{POINTERS};
277 my $pt = pointer_type($e);
279 # Top level "ref" pointers do not have a referrent identifier
282 and $e->{PARENT}->{TYPE} eq "FUNCTION")
297 PROPERTIES => $e->{PROPERTIES},
298 LEVELS => GetElementLevelTable($e)
306 my $surrounding = undef;
308 foreach my $x (@{$struct->{ELEMENTS}})
310 push @elements, ParseElement($x);
313 my $e = $elements[-1];
314 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
315 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
319 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
320 && util::property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
321 $surrounding = $struct->{ELEMENTS}[-1];
326 SURROUNDING_ELEMENT => $surrounding,
327 ELEMENTS => \@elements,
328 PROPERTIES => $struct->{PROPERTIES}
336 my $switch_type = util::has_property($e, "switch_type");
337 unless (defined($switch_type)) { $switch_type = "uint32"; }
339 if (util::has_property($e, "nodiscriminant")) { $switch_type = undef; }
341 foreach my $x (@{$e->{ELEMENTS}})
344 if ($x->{TYPE} eq "EMPTY") {
345 $t = { TYPE => "EMPTY" };
347 $t = ParseElement($x);
349 if (util::has_property($x, "default")) {
350 $t->{CASE} = "default";
351 } elsif (defined($x->{PROPERTIES}->{case})) {
352 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
354 die("Union element $x->{NAME} has neither default nor case property");
361 SWITCH_TYPE => $switch_type,
362 ELEMENTS => \@elements,
363 PROPERTIES => $e->{PROPERTIES}
373 BASE_TYPE => typelist::enum_type_fn($e),
374 ELEMENTS => $e->{ELEMENTS},
375 PROPERTIES => $e->{PROPERTIES}
385 BASE_TYPE => typelist::bitmap_type_fn($e),
386 ELEMENTS => $e->{ELEMENTS},
387 PROPERTIES => $e->{PROPERTIES}
404 if ($d->{DATA}->{TYPE} eq "STRUCT" or $d->{DATA}->{TYPE} eq "UNION") {
405 CheckPointerTypes($d->{DATA}, $ndr->{PROPERTIES}->{pointer_default});
408 if (defined($d->{PROPERTIES}) && !defined($d->{DATA}->{PROPERTIES})) {
409 $d->{DATA}->{PROPERTIES} = $d->{PROPERTIES};
412 if ($d->{DATA}->{TYPE} eq "STRUCT") {
413 $data = ParseStruct($d->{DATA});
414 } elsif ($d->{DATA}->{TYPE} eq "UNION") {
415 $data = ParseUnion($d->{DATA});
416 } elsif ($d->{DATA}->{TYPE} eq "ENUM") {
417 $data = ParseEnum($d->{DATA});
418 } elsif ($d->{DATA}->{TYPE} eq "BITMAP") {
419 $data = ParseBitmap($d->{DATA});
421 die("Unknown data type '$d->{DATA}->{TYPE}'");
424 $data->{ALIGN} = align_type($d->{NAME});
429 PROPERTIES => $d->{PROPERTIES},
442 sub ParseFunction($$$)
449 my $thisopnum = undef;
451 CheckPointerTypes($d,
452 $ndr->{PROPERTIES}->{pointer_default_top}
455 if (not defined($d->{PROPERTIES}{noopnum})) {
456 $thisopnum = ${$opnum};
460 foreach my $x (@{$d->{ELEMENTS}}) {
461 my $e = ParseElement($x);
462 if (util::has_property($x, "in")) {
463 push (@{$e->{DIRECTION}}, "in");
466 if (util::has_property($x, "out")) {
467 push (@{$e->{DIRECTION}}, "out");
470 push (@elements, $e);
473 if ($d->{RETURN_TYPE} ne "void") {
474 $rettype = $d->{RETURN_TYPE};
481 RETURN_TYPE => $rettype,
482 PROPERTIES => $d->{PROPERTIES},
483 ELEMENTS => \@elements
487 sub CheckPointerTypes($$)
492 foreach my $e (@{$s->{ELEMENTS}}) {
493 if ($e->{POINTERS}) {
494 if (not defined(Ndr::pointer_type($e))) {
495 $e->{PROPERTIES}->{$default} = 1;
498 if (Ndr::pointer_type($e) eq "ptr") {
499 print "Warning: ptr is not supported by pidl yet\n";
505 sub ParseInterface($)
516 if (not util::has_property($idl, "pointer_default")) {
517 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
518 # and "unique" in Microsoft Extensions mode (default)
519 $idl->{PROPERTIES}->{pointer_default} = "unique";
522 if (not util::has_property($idl, "pointer_default_top")) {
523 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
526 foreach my $d (@{$idl->{DATA}}) {
527 if ($d->{TYPE} eq "TYPEDEF") {
528 push (@typedefs, ParseTypedef($idl, $d));
531 if ($d->{TYPE} eq "DECLARE") {
532 push (@declares, ParseDeclare($idl, $d));
535 if ($d->{TYPE} eq "FUNCTION") {
536 push (@functions, ParseFunction($idl, $d, \$opnum));
539 if ($d->{TYPE} eq "CONST") {
540 push (@consts, ParseConst($idl, $d));
546 if(defined $idl->{PROPERTIES}->{version}) {
547 $version = $idl->{PROPERTIES}->{version};
550 # If no endpoint is set, default to the interface name as a named pipe
551 if (!defined $idl->{PROPERTIES}->{endpoint}) {
552 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
554 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
558 NAME => $idl->{NAME},
559 UUID => util::has_property($idl, "uuid"),
562 PROPERTIES => $idl->{PROPERTIES},
563 FUNCTIONS => \@functions,
565 TYPEDEFS => \@typedefs,
566 DECLARES => \@declares,
567 ENDPOINTS => \@endpoints
571 # Convert a IDL tree to a NDR tree
572 # Gives a result tree describing all that's necessary for easily generating
574 # - list of interfaces
575 # - list with functions
576 # - list with in elements
577 # - list with out elements
579 # - list with structs
580 # - alignment of structure
581 # - list with elements
583 # - alignment of union
584 # - list with elements
587 # - list with bitmaps
592 # properties are saved
593 # pointer types explicitly specified
599 foreach my $x (@{$idl}) {
600 push @ndr, ParseInterface($x);
613 foreach my $l (@{$e->{LEVELS}}) {
614 return $l if ($seen);
615 ($seen = 1) if ($l == $fl);
627 foreach my $l (@{$e->{LEVELS}}) {
628 (return $prev) if ($l == $fl);
635 sub ContainsDeferred($$)
641 return 1 if ($l->{IS_DEFERRED});
642 return 1 if ($l->{CONTAINS_DEFERRED});
643 } while ($l = Ndr::GetNextLevel($e,$l));