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
8 package Parse::Pidl::NDR;
12 @EXPORT = qw(GetPrevLevel GetNextLevel ContainsDeferred);
15 use Parse::Pidl::Typelist qw(hasType getType);
16 use Parse::Pidl::Util qw(has_property property_matches);
21 warn ("$e->{FILE}:$e->{LINE}: Warning: $s\n");
24 #####################################################################
25 # return a table describing the order in which the parts of an element
27 # Possible level types:
33 sub GetElementLevelTable($)
39 my @bracket_array = ();
43 if (has_property($e, "size_is")) {
44 @size_is = split /,/, has_property($e, "size_is");
47 if (has_property($e, "length_is")) {
48 @length_is = split /,/, has_property($e, "length_is");
51 if (defined($e->{ARRAY_LEN})) {
52 @bracket_array = @{$e->{ARRAY_LEN}};
55 # Parse the [][][][] style array stuff
56 foreach my $d (@bracket_array) {
59 my $is_surrounding = 0;
61 my $is_conformant = 0;
66 if ($size = shift @size_is) {
67 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
69 delete($e->{PROPERTIES}->{string});
71 print "$e->{FILE}:$e->{LINE}: Must specify size_is() for conformant array!\n";
75 if (($length = shift @length_is) or $is_string) {
81 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
82 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
91 IS_DEFERRED => "$is_deferred",
92 IS_SURROUNDING => "$is_surrounding",
93 IS_ZERO_TERMINATED => "$is_string",
94 IS_VARYING => "$is_varying",
95 IS_CONFORMANT => "$is_conformant",
96 IS_FIXED => (not $is_conformant and Parse::Pidl::Util::is_constant($size)),
97 IS_INLINE => (not $is_conformant and not Parse::Pidl::Util::is_constant($size))
101 # Next, all the pointers
102 foreach my $i (1..$e->{POINTERS}) {
103 my $pt = pointer_type($e);
105 my $level = "EMBEDDED";
106 # Top level "ref" pointers do not have a referrent identifier
107 $level = "TOP" if ( defined($pt)
109 and $e->{PARENT}->{TYPE} eq "FUNCTION");
113 # for now, there can only be one pointer type per element
114 POINTER_TYPE => pointer_type($e),
115 IS_DEFERRED => "$is_deferred",
119 # everything that follows will be deferred
120 $is_deferred = 1 if ($e->{PARENT}->{TYPE} ne "FUNCTION");
122 my $array_size = shift @size_is;
129 if ($array_length = shift @length_is) {
132 $array_length = $array_size;
137 if (scalar(@size_is) == 0 and has_property($e, "string")) {
139 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
140 delete($e->{PROPERTIES}->{string});
143 if ($array_size or $is_string) {
146 IS_ZERO_TERMINATED => "$is_string",
147 SIZE_IS => $array_size,
148 LENGTH_IS => $array_length,
149 IS_DEFERRED => "$is_deferred",
151 IS_VARYING => "$is_varying",
152 IS_CONFORMANT => "$is_conformant",
161 if (defined(has_property($e, "subcontext"))) {
162 my $hdr_size = has_property($e, "subcontext");
163 my $subsize = has_property($e, "subcontext_size");
164 if (not defined($subsize)) {
169 TYPE => "SUBCONTEXT",
170 HEADER_SIZE => $hdr_size,
171 SUBCONTEXT_SIZE => $subsize,
172 IS_DEFERRED => $is_deferred,
173 COMPRESSION => has_property($e, "compression"),
174 OBFUSCATION => has_property($e, "obfuscation")
178 if (my $switch = has_property($e, "switch_is")) {
181 SWITCH_IS => $switch,
182 IS_DEFERRED => $is_deferred
186 if (scalar(@size_is) > 0) {
187 nonfatal($e, "size_is() on non-array element");
190 if (scalar(@length_is) > 0) {
191 nonfatal($e, "length_is() on non-array element");
194 if (has_property($e, "string")) {
195 nonfatal($e, "string() attribute on non-array element");
200 DATA_TYPE => $e->{TYPE},
201 IS_DEFERRED => $is_deferred,
202 CONTAINS_DEFERRED => can_contain_deferred($e),
203 IS_SURROUNDING => 0 #FIXME
207 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
212 #####################################################################
213 # see if a type contains any deferred data
214 sub can_contain_deferred
218 return 0 if (Parse::Pidl::Typelist::is_scalar($e->{TYPE}));
219 return 1 unless (hasType($e->{TYPE})); # assume the worst
221 my $type = getType($e->{TYPE});
223 foreach my $x (@{$type->{DATA}->{ELEMENTS}}) {
224 return 1 if ($x->{POINTERS});
225 return 1 if (can_contain_deferred ($x));
235 return undef unless $e->{POINTERS};
237 return "ref" if (has_property($e, "ref"));
238 return "ptr" if (has_property($e, "ptr"));
239 return "sptr" if (has_property($e, "sptr"));
240 return "unique" if (has_property($e, "unique"));
241 return "relative" if (has_property($e, "relative"));
242 return "ignore" if (has_property($e, "ignore"));
247 #####################################################################
248 # work out the correct alignment for a structure or union
249 sub find_largest_alignment($)
254 for my $e (@{$s->{ELEMENTS}}) {
257 if ($e->{POINTERS}) {
259 } elsif (has_property($e, "subcontext")){
262 $a = align_type($e->{TYPE});
265 $align = $a if ($align < $a);
271 #####################################################################
277 unless (hasType($e)) {
278 # it must be an external type - all we can do is guess
279 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
283 my $dt = getType($e)->{DATA};
285 if ($dt->{TYPE} eq "ENUM") {
286 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
287 } elsif ($dt->{TYPE} eq "BITMAP") {
288 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
289 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
290 return find_largest_alignment($dt);
291 } elsif ($dt->{TYPE} eq "SCALAR") {
292 return Parse::Pidl::Typelist::getScalarAlignment($dt->{NAME});
295 die("Unknown data type type $dt->{TYPE}");
305 PROPERTIES => $e->{PROPERTIES},
306 LEVELS => GetElementLevelTable($e),
307 ALIGN => align_type($e->{TYPE})
315 my $surrounding = undef;
317 foreach my $x (@{$struct->{ELEMENTS}})
319 push @elements, ParseElement($x);
322 my $e = $elements[-1];
323 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
324 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
328 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
329 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
330 $surrounding = $struct->{ELEMENTS}[-1];
335 SURROUNDING_ELEMENT => $surrounding,
336 ELEMENTS => \@elements,
337 PROPERTIES => $struct->{PROPERTIES}
345 my $switch_type = has_property($e, "switch_type");
346 unless (defined($switch_type)) { $switch_type = "uint32"; }
348 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
350 foreach my $x (@{$e->{ELEMENTS}})
353 if ($x->{TYPE} eq "EMPTY") {
354 $t = { TYPE => "EMPTY" };
356 $t = ParseElement($x);
358 if (has_property($x, "default")) {
359 $t->{CASE} = "default";
360 } elsif (defined($x->{PROPERTIES}->{case})) {
361 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
363 die("Union element $x->{NAME} has neither default nor case property");
370 SWITCH_TYPE => $switch_type,
371 ELEMENTS => \@elements,
372 PROPERTIES => $e->{PROPERTIES}
382 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
383 ELEMENTS => $e->{ELEMENTS},
384 PROPERTIES => $e->{PROPERTIES}
394 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
395 ELEMENTS => $e->{ELEMENTS},
396 PROPERTIES => $e->{PROPERTIES}
405 if ($d->{DATA}->{TYPE} eq "STRUCT" or $d->{DATA}->{TYPE} eq "UNION") {
406 CheckPointerTypes($d->{DATA}, $ndr->{PROPERTIES}->{pointer_default});
409 if (defined($d->{PROPERTIES}) && !defined($d->{DATA}->{PROPERTIES})) {
410 $d->{DATA}->{PROPERTIES} = $d->{PROPERTIES};
414 STRUCT => \&ParseStruct,
415 UNION => \&ParseUnion,
417 BITMAP => \&ParseBitmap
418 }->{$d->{DATA}->{TYPE}}->($d->{DATA});
420 $data->{ALIGN} = align_type($d->{NAME});
425 PROPERTIES => $d->{PROPERTIES},
437 sub ParseFunction($$$)
439 my ($ndr,$d,$opnum) = @_;
442 my $thisopnum = undef;
444 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default_top});
446 if (not defined($d->{PROPERTIES}{noopnum})) {
447 $thisopnum = ${$opnum};
451 foreach my $x (@{$d->{ELEMENTS}}) {
452 my $e = ParseElement($x);
453 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
454 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
455 push (@elements, $e);
458 if ($d->{RETURN_TYPE} ne "void") {
459 $rettype = $d->{RETURN_TYPE};
466 RETURN_TYPE => $rettype,
467 PROPERTIES => $d->{PROPERTIES},
468 ELEMENTS => \@elements
472 sub CheckPointerTypes($$)
477 foreach my $e (@{$s->{ELEMENTS}}) {
478 if ($e->{POINTERS} and not defined(pointer_type($e))) {
479 $e->{PROPERTIES}->{$default} = 1;
484 sub ParseInterface($)
495 if (not has_property($idl, "pointer_default")) {
496 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
497 # and "unique" in Microsoft Extensions mode (default)
498 $idl->{PROPERTIES}->{pointer_default} = "unique";
501 if (not has_property($idl, "pointer_default_top")) {
502 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
505 foreach my $d (@{$idl->{DATA}}) {
506 if ($d->{TYPE} eq "TYPEDEF") {
507 push (@typedefs, ParseTypedef($idl, $d));
510 if ($d->{TYPE} eq "DECLARE") {
511 push (@declares, $d);
514 if ($d->{TYPE} eq "FUNCTION") {
515 push (@functions, ParseFunction($idl, $d, \$opnum));
518 if ($d->{TYPE} eq "CONST") {
519 push (@consts, ParseConst($idl, $d));
525 if(defined $idl->{PROPERTIES}->{version}) {
526 $version = $idl->{PROPERTIES}->{version};
529 # If no endpoint is set, default to the interface name as a named pipe
530 if (!defined $idl->{PROPERTIES}->{endpoint}) {
531 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
533 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
537 NAME => $idl->{NAME},
538 UUID => has_property($idl, "uuid"),
541 PROPERTIES => $idl->{PROPERTIES},
542 FUNCTIONS => \@functions,
544 TYPEDEFS => \@typedefs,
545 DECLARES => \@declares,
546 ENDPOINTS => \@endpoints
550 # Convert a IDL tree to a NDR tree
551 # Gives a result tree describing all that's necessary for easily generating
552 # NDR parsers / generators
558 push(@ndr, ParseInterface($_)) foreach (@{$idl});
570 foreach my $l (@{$e->{LEVELS}}) {
571 return $l if ($seen);
572 ($seen = 1) if ($l == $fl);
583 foreach my $l (@{$e->{LEVELS}}) {
584 (return $prev) if ($l == $fl);
591 sub ContainsDeferred($$)
596 return 1 if ($l->{IS_DEFERRED});
597 return 1 if ($l->{CONTAINS_DEFERRED});
598 } while ($l = GetNextLevel($e,$l));