pidl: Add skip option to elements.
[Samba.git] / pidl / lib / Parse / Pidl / NDR.pm
blobc4cef64a75d2094906c16c542c0d897d3368058d
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
8 =pod
10 =head1 NAME
12 Parse::Pidl::NDR - NDR parsing information generator
14 =head1 DESCRIPTION
16 Return a table describing the order in which the parts of an element
17 should be parsed
18 Possible level types:
19 - POINTER
20 - ARRAY
21 - SUBCONTEXT
22 - SWITCH
23 - DATA
25 =head1 AUTHOR
27 Jelmer Vernooij <jelmer@samba.org>
29 =cut
31 package Parse::Pidl::NDR;
33 require Exporter;
34 use vars qw($VERSION);
35 $VERSION = '0.01';
36 @ISA = qw(Exporter);
37 @EXPORT = qw(GetPrevLevel GetNextLevel ContainsDeferred ContainsPipe ContainsString);
38 @EXPORT_OK = qw(GetElementLevelTable ParseElement ValidElement align_type mapToScalar ParseType can_contain_deferred is_charset_array);
40 use strict;
41 use Parse::Pidl qw(warning fatal);
42 use Parse::Pidl::Typelist qw(hasType getType typeIs expandAlias mapScalarType is_fixed_size_scalar);
43 use Parse::Pidl::Util qw(has_property property_matches);
45 # Alignment of the built-in scalar types
46 my $scalar_alignment = {
47 'void' => 0,
48 'char' => 1,
49 'int8' => 1,
50 'uint8' => 1,
51 'int16' => 2,
52 'uint16' => 2,
53 'int1632' => 3,
54 'uint1632' => 3,
55 'int32' => 4,
56 'uint32' => 4,
57 'int3264' => 5,
58 'uint3264' => 5,
59 'hyper' => 8,
60 'double' => 8,
61 'pointer' => 8,
62 'dlong' => 4,
63 'udlong' => 4,
64 'udlongr' => 4,
65 'DATA_BLOB' => 4,
66 'string' => 4,
67 'string_array' => 4, #???
68 'time_t' => 4,
69 'uid_t' => 8,
70 'gid_t' => 8,
71 'NTTIME' => 4,
72 'NTTIME_1sec' => 4,
73 'NTTIME_hyper' => 8,
74 'WERROR' => 4,
75 'NTSTATUS' => 4,
76 'COMRESULT' => 4,
77 'dns_string' => 4,
78 'nbt_string' => 4,
79 'wrepl_nbt_name' => 4,
80 'ipv4address' => 4,
81 'ipv6address' => 4, #16?
82 'dnsp_name' => 1,
83 'dnsp_string' => 1
86 sub GetElementLevelTable($$$)
88 my ($e, $pointer_default, $ms_union) = @_;
90 my $order = [];
91 my $is_deferred = 0;
92 my @bracket_array = ();
93 my @length_is = ();
94 my @size_is = ();
95 my $pointer_idx = 0;
97 if (has_property($e, "size_is")) {
98 @size_is = split /,/, has_property($e, "size_is");
101 if (has_property($e, "length_is")) {
102 @length_is = split /,/, has_property($e, "length_is");
105 if (defined($e->{ARRAY_LEN})) {
106 @bracket_array = @{$e->{ARRAY_LEN}};
109 if (has_property($e, "out")) {
110 my $needptrs = 1;
112 if (has_property($e, "string") and not has_property($e, "in")) { $needptrs++; }
113 if ($#bracket_array >= 0) { $needptrs = 0; }
115 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
118 my $allow_pipe = ($e->{PARENT}->{TYPE} eq "FUNCTION");
119 my $is_pipe = typeIs($e->{TYPE}, "PIPE");
121 if ($is_pipe) {
122 if (not $allow_pipe) {
123 fatal($e, "argument `$e->{NAME}' is a pipe and not allowed on $e->{PARENT}->{TYPE}");
126 if ($e->{POINTERS} > 1) {
127 fatal($e, "$e->{POINTERS} are not allowed on pipe element $e->{NAME}");
130 if ($e->{POINTERS} < 0) {
131 fatal($e, "pipe element $e->{NAME} needs pointer");
134 if ($e->{POINTERS} == 1 and pointer_type($e) ne "ref") {
135 fatal($e, "pointer should be 'ref' on pipe element $e->{NAME}");
138 if (scalar(@size_is) > 0) {
139 fatal($e, "size_is() on pipe element");
142 if (scalar(@length_is) > 0) {
143 fatal($e, "length_is() on pipe element");
146 if (scalar(@bracket_array) > 0) {
147 fatal($e, "brackets on pipe element");
150 if (defined(has_property($e, "subcontext"))) {
151 fatal($e, "subcontext on pipe element");
154 if (has_property($e, "switch_is")) {
155 fatal($e, "switch_is on pipe element");
158 if (can_contain_deferred($e->{TYPE})) {
159 fatal($e, "$e->{TYPE} can_contain_deferred - not allowed on pipe element");
163 # Parse the [][][][] style array stuff
164 for my $i (0 .. $#bracket_array) {
165 my $d = $bracket_array[$#bracket_array - $i];
166 my $size = $d;
167 my $length = $d;
168 my $is_surrounding = 0;
169 my $is_varying = 0;
170 my $is_conformant = 0;
171 my $is_string = 0;
172 my $is_fixed = 0;
173 my $is_inline = 0;
174 my $is_to_null = 0;
176 if ($d eq "*") {
177 $is_conformant = 1;
178 if ($size = shift @size_is) {
179 if ($e->{POINTERS} < 1 and has_property($e, "string")) {
180 $is_string = 1;
181 delete($e->{PROPERTIES}->{string});
183 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
184 $is_string = 1;
185 delete($e->{PROPERTIES}->{string});
186 } else {
187 fatal($e, "Must specify size_is() for conformant array!")
190 if (($length = shift @length_is) or $is_string) {
191 $is_varying = 1;
192 } else {
193 $length = $size;
196 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
197 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
198 $is_surrounding = 1;
202 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
203 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
205 if ($i == 0 and $is_fixed and has_property($e, "string")) {
206 $is_fixed = 0;
207 $is_varying = 1;
208 $is_string = 1;
209 delete($e->{PROPERTIES}->{string});
212 if (has_property($e, "to_null")) {
213 $is_to_null = 1;
216 push (@$order, {
217 TYPE => "ARRAY",
218 SIZE_IS => $size,
219 LENGTH_IS => $length,
220 IS_DEFERRED => $is_deferred,
221 IS_SURROUNDING => $is_surrounding,
222 IS_ZERO_TERMINATED => $is_string,
223 IS_VARYING => $is_varying,
224 IS_CONFORMANT => $is_conformant,
225 IS_FIXED => $is_fixed,
226 IS_INLINE => $is_inline,
227 IS_TO_NULL => $is_to_null
231 # Next, all the pointers
232 foreach my $i (1..$e->{POINTERS}) {
233 my $level = "EMBEDDED";
234 # Top level "ref" pointers do not have a referrent identifier
235 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
237 my $pt;
239 # Only the first level gets the pointer type from the
240 # pointer property, the others get them from
241 # the pointer_default() interface property
243 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
244 # (Here they talk about the rightmost pointer, but testing shows
245 # they mean the leftmost pointer.)
247 # --metze
249 $pt = pointer_type($e);
250 if ($i > 1) {
251 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
252 $pt = $pointer_default;
255 push (@$order, {
256 TYPE => "POINTER",
257 POINTER_TYPE => $pt,
258 POINTER_INDEX => $pointer_idx,
259 IS_DEFERRED => "$is_deferred",
260 LEVEL => $level
263 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
264 if ($i == 1 and $pt ne "ref" and
265 $e->{PARENT}->{TYPE} eq "FUNCTION" and
266 not has_property($e, "in"));
268 $pointer_idx++;
270 # everything that follows will be deferred
271 $is_deferred = 1 if ($level ne "TOP");
273 my $array_size = shift @size_is;
274 my $array_length;
275 my $is_varying;
276 my $is_conformant;
277 my $is_string = 0;
278 if ($array_size) {
279 $is_conformant = 1;
280 if ($array_length = shift @length_is) {
281 $is_varying = 1;
282 } else {
283 $array_length = $array_size;
284 $is_varying =0;
288 if (scalar(@size_is) == 0 and has_property($e, "string") and
289 $i == $e->{POINTERS}) {
290 $is_string = 1;
291 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
292 delete($e->{PROPERTIES}->{string});
295 if ($array_size or $is_string) {
296 push (@$order, {
297 TYPE => "ARRAY",
298 SIZE_IS => $array_size,
299 LENGTH_IS => $array_length,
300 IS_DEFERRED => $is_deferred,
301 IS_SURROUNDING => 0,
302 IS_ZERO_TERMINATED => $is_string,
303 IS_VARYING => $is_varying,
304 IS_CONFORMANT => $is_conformant,
305 IS_FIXED => 0,
306 IS_INLINE => 0
309 $is_deferred = 0;
313 if ($is_pipe) {
314 push (@$order, {
315 TYPE => "PIPE",
316 IS_DEFERRED => 0,
317 CONTAINS_DEFERRED => 0,
320 my $i = 0;
321 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
323 return $order;
326 if (defined(has_property($e, "subcontext"))) {
327 my $hdr_size = has_property($e, "subcontext");
328 my $subsize = has_property($e, "subcontext_size");
329 if (not defined($subsize)) {
330 $subsize = -1;
333 push (@$order, {
334 TYPE => "SUBCONTEXT",
335 HEADER_SIZE => $hdr_size,
336 SUBCONTEXT_SIZE => $subsize,
337 IS_DEFERRED => $is_deferred,
338 COMPRESSION => has_property($e, "compression"),
342 if (my $switch = has_property($e, "switch_is")) {
343 push (@$order, {
344 TYPE => "SWITCH",
345 SWITCH_IS => $switch,
346 IS_DEFERRED => $is_deferred
350 if (scalar(@size_is) > 0) {
351 fatal($e, "size_is() on non-array element");
354 if (scalar(@length_is) > 0) {
355 fatal($e, "length_is() on non-array element");
358 if (has_property($e, "string")) {
359 fatal($e, "string() attribute on non-array element");
362 push (@$order, {
363 TYPE => "DATA",
364 DATA_TYPE => $e->{TYPE},
365 IS_DEFERRED => $is_deferred,
366 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
367 IS_SURROUNDING => 0 #FIXME
370 my $i = 0;
371 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
373 return $order;
376 sub GetTypedefLevelTable($$$$)
378 my ($e, $data, $pointer_default, $ms_union) = @_;
380 my $order = [];
382 push (@$order, {
383 TYPE => "TYPEDEF"
386 my $i = 0;
387 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
389 return $order;
392 #####################################################################
393 # see if a type contains any deferred data
394 sub can_contain_deferred($)
396 sub can_contain_deferred($);
397 my ($type) = @_;
399 return 1 unless (hasType($type)); # assume the worst
401 $type = getType($type);
403 return 0 if (Parse::Pidl::Typelist::is_scalar($type));
405 return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
407 return 0 unless defined($type->{ELEMENTS});
409 foreach (@{$type->{ELEMENTS}}) {
410 return 1 if ($_->{POINTERS});
411 return 1 if (can_contain_deferred ($_->{TYPE}));
414 return 0;
417 sub pointer_type($)
419 my $e = shift;
421 return undef unless $e->{POINTERS};
423 return "ref" if (has_property($e, "ref"));
424 return "full" if (has_property($e, "ptr"));
425 return "sptr" if (has_property($e, "sptr"));
426 return "unique" if (has_property($e, "unique"));
427 return "relative" if (has_property($e, "relative"));
428 return "relative_short" if (has_property($e, "relative_short"));
429 return "ignore" if (has_property($e, "ignore"));
431 return undef;
434 #####################################################################
435 # work out the correct alignment for a structure or union
436 sub find_largest_alignment($)
438 my $s = shift;
440 my $align = 1;
441 for my $e (@{$s->{ELEMENTS}}) {
442 my $a = 1;
444 if ($e->{POINTERS}) {
445 # this is a hack for NDR64
446 # the NDR layer translates this into
447 # an alignment of 4 for NDR and 8 for NDR64
448 $a = 5;
449 } elsif (has_property($e, "subcontext")) {
450 $a = 1;
451 } elsif (has_property($e, "transmit_as")) {
452 $a = align_type($e->{PROPERTIES}->{transmit_as});
453 } else {
454 $a = align_type($e->{TYPE});
457 $align = $a if ($align < $a);
460 return $align;
463 #####################################################################
464 # align a type
465 sub align_type($)
467 sub align_type($);
468 my ($e) = @_;
470 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
471 return $scalar_alignment->{$e->{NAME}};
474 return 0 if ($e eq "EMPTY");
476 unless (hasType($e)) {
477 # it must be an external type - all we can do is guess
478 # warning($e, "assuming alignment of unknown type '$e' is 4");
479 return 4;
482 my $dt = getType($e);
484 if ($dt->{TYPE} eq "TYPEDEF") {
485 return align_type($dt->{DATA});
486 } elsif ($dt->{TYPE} eq "CONFORMANCE") {
487 return $dt->{DATA}->{ALIGN};
488 } elsif ($dt->{TYPE} eq "ENUM") {
489 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
490 } elsif ($dt->{TYPE} eq "BITMAP") {
491 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
492 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
493 # Struct/union without body: assume 4
494 return 4 unless (defined($dt->{ELEMENTS}));
495 return find_largest_alignment($dt);
496 } elsif (($dt->{TYPE} eq "PIPE")) {
497 return 5;
500 die("Unknown data type type $dt->{TYPE}");
503 sub ParseElement($$$)
505 my ($e, $pointer_default, $ms_union) = @_;
507 $e->{TYPE} = expandAlias($e->{TYPE});
509 if (ref($e->{TYPE}) eq "HASH") {
510 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default, $ms_union);
513 return {
514 NAME => $e->{NAME},
515 TYPE => $e->{TYPE},
516 PROPERTIES => $e->{PROPERTIES},
517 LEVELS => GetElementLevelTable($e, $pointer_default, $ms_union),
518 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
519 ALIGN => align_type($e->{TYPE}),
520 ORIGINAL => $e
524 sub ParseStruct($$$)
526 my ($struct, $pointer_default, $ms_union) = @_;
527 my @elements = ();
528 my $surrounding = undef;
530 return {
531 TYPE => "STRUCT",
532 NAME => $struct->{NAME},
533 SURROUNDING_ELEMENT => undef,
534 ELEMENTS => undef,
535 PROPERTIES => $struct->{PROPERTIES},
536 ORIGINAL => $struct,
537 ALIGN => undef
538 } unless defined($struct->{ELEMENTS});
540 CheckPointerTypes($struct, $pointer_default);
542 foreach my $x (@{$struct->{ELEMENTS}})
544 my $e = ParseElement($x, $pointer_default, $ms_union);
545 if ($x != $struct->{ELEMENTS}[-1] and
546 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
547 fatal($x, "conformant member not at end of struct");
549 push @elements, $e;
552 my $e = $elements[-1];
553 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
554 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
555 $surrounding = $e;
558 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
559 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
560 $surrounding = $struct->{ELEMENTS}[-1];
563 my $align = undef;
564 if ($struct->{NAME}) {
565 $align = align_type($struct->{NAME});
568 return {
569 TYPE => "STRUCT",
570 NAME => $struct->{NAME},
571 SURROUNDING_ELEMENT => $surrounding,
572 ELEMENTS => \@elements,
573 PROPERTIES => $struct->{PROPERTIES},
574 ORIGINAL => $struct,
575 ALIGN => $align
579 sub ParseUnion($$)
581 my ($e, $pointer_default, $ms_union) = @_;
582 my @elements = ();
583 my $is_ms_union = $ms_union;
584 $is_ms_union = 1 if has_property($e, "ms_union");
585 my $hasdefault = 0;
586 my $switch_type = has_property($e, "switch_type");
587 unless (defined($switch_type)) { $switch_type = "uint32"; }
588 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
590 return {
591 TYPE => "UNION",
592 NAME => $e->{NAME},
593 SWITCH_TYPE => $switch_type,
594 ELEMENTS => undef,
595 PROPERTIES => $e->{PROPERTIES},
596 HAS_DEFAULT => $hasdefault,
597 IS_MS_UNION => $is_ms_union,
598 ORIGINAL => $e,
599 ALIGN => undef
600 } unless defined($e->{ELEMENTS});
602 CheckPointerTypes($e, $pointer_default);
604 foreach my $x (@{$e->{ELEMENTS}})
606 my $t;
607 if ($x->{TYPE} eq "EMPTY") {
608 $t = { TYPE => "EMPTY" };
609 } else {
610 $t = ParseElement($x, $pointer_default, $ms_union);
612 if (has_property($x, "default")) {
613 $t->{CASE} = "default";
614 $hasdefault = 1;
615 } elsif (defined($x->{PROPERTIES}->{case})) {
616 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
617 } else {
618 die("Union element $x->{NAME} has neither default nor case property");
620 push @elements, $t;
623 my $align = undef;
624 if ($e->{NAME}) {
625 $align = align_type($e->{NAME});
628 return {
629 TYPE => "UNION",
630 NAME => $e->{NAME},
631 SWITCH_TYPE => $switch_type,
632 ELEMENTS => \@elements,
633 PROPERTIES => $e->{PROPERTIES},
634 HAS_DEFAULT => $hasdefault,
635 IS_MS_UNION => $is_ms_union,
636 ORIGINAL => $e,
637 ALIGN => $align
641 sub ParseEnum($$)
643 my ($e, $pointer_default, $ms_union) = @_;
645 return {
646 TYPE => "ENUM",
647 NAME => $e->{NAME},
648 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
649 ELEMENTS => $e->{ELEMENTS},
650 PROPERTIES => $e->{PROPERTIES},
651 ORIGINAL => $e
655 sub ParseBitmap($$$)
657 my ($e, $pointer_default, $ms_union) = @_;
659 return {
660 TYPE => "BITMAP",
661 NAME => $e->{NAME},
662 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
663 ELEMENTS => $e->{ELEMENTS},
664 PROPERTIES => $e->{PROPERTIES},
665 ORIGINAL => $e
669 sub ParsePipe($$$)
671 my ($pipe, $pointer_default, $ms_union) = @_;
673 my $pname = $pipe->{NAME};
674 $pname = $pipe->{PARENT}->{NAME} unless defined $pname;
676 if (not defined($pipe->{PROPERTIES})
677 and defined($pipe->{PARENT}->{PROPERTIES})) {
678 $pipe->{PROPERTIES} = $pipe->{PARENT}->{PROPERTIES};
681 if (ref($pipe->{DATA}) eq "HASH") {
682 if (not defined($pipe->{DATA}->{PROPERTIES})
683 and defined($pipe->{PROPERTIES})) {
684 $pipe->{DATA}->{PROPERTIES} = $pipe->{PROPERTIES};
688 my $struct = ParseStruct($pipe->{DATA}, $pointer_default, $ms_union);
689 $struct->{ALIGN} = 5;
690 $struct->{NAME} = "$pname\_chunk";
692 # 'count' is element [0] and 'array' [1]
693 my $e = $struct->{ELEMENTS}[1];
694 # level [0] is of type "ARRAY"
695 my $l = $e->{LEVELS}[1];
697 # here we check that pipe elements have a fixed size type
698 while (defined($l)) {
699 my $cl = $l;
700 $l = GetNextLevel($e, $cl);
701 if ($cl->{TYPE} ne "DATA") {
702 fatal($pipe, el_name($pipe) . ": pipe contains non DATA level");
705 # for now we only support scalars
706 next if is_fixed_size_scalar($cl->{DATA_TYPE});
708 fatal($pipe, el_name($pipe) . ": pipe contains non fixed size type[$cl->{DATA_TYPE}]");
711 return {
712 TYPE => "PIPE",
713 NAME => $pipe->{NAME},
714 DATA => $struct,
715 PROPERTIES => $pipe->{PROPERTIES},
716 ORIGINAL => $pipe,
720 sub ParseType($$$)
722 my ($d, $pointer_default, $ms_union) = @_;
724 my $data = {
725 STRUCT => \&ParseStruct,
726 UNION => \&ParseUnion,
727 ENUM => \&ParseEnum,
728 BITMAP => \&ParseBitmap,
729 TYPEDEF => \&ParseTypedef,
730 PIPE => \&ParsePipe,
731 }->{$d->{TYPE}}->($d, $pointer_default, $ms_union);
733 return $data;
736 sub ParseTypedef($$)
738 my ($d, $pointer_default, $ms_union) = @_;
740 my $data;
742 if (ref($d->{DATA}) eq "HASH") {
743 if (defined($d->{DATA}->{PROPERTIES})
744 and not defined($d->{PROPERTIES})) {
745 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
748 $data = ParseType($d->{DATA}, $pointer_default, $ms_union);
749 $data->{ALIGN} = align_type($d->{NAME});
750 } else {
751 $data = getType($d->{DATA});
754 return {
755 NAME => $d->{NAME},
756 TYPE => $d->{TYPE},
757 PROPERTIES => $d->{PROPERTIES},
758 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default, $ms_union),
759 DATA => $data,
760 ORIGINAL => $d
764 sub ParseConst($$)
766 my ($ndr,$d) = @_;
768 return $d;
771 sub ParseFunction($$$$)
773 my ($ndr,$d,$opnum,$ms_union) = @_;
774 my @elements = ();
775 my $rettype = undef;
776 my $thisopnum = undef;
778 CheckPointerTypes($d, "ref");
780 if (not defined($d->{PROPERTIES}{noopnum})) {
781 $thisopnum = ${$opnum};
782 ${$opnum}++;
785 foreach my $x (@{$d->{ELEMENTS}}) {
786 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default}, $ms_union);
787 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
788 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
790 push (@elements, $e);
793 if ($d->{RETURN_TYPE} ne "void") {
794 $rettype = expandAlias($d->{RETURN_TYPE});
797 return {
798 NAME => $d->{NAME},
799 TYPE => "FUNCTION",
800 OPNUM => $thisopnum,
801 RETURN_TYPE => $rettype,
802 PROPERTIES => $d->{PROPERTIES},
803 ELEMENTS => \@elements,
804 ORIGINAL => $d
808 sub CheckPointerTypes($$)
810 my ($s,$default) = @_;
812 return unless defined($s->{ELEMENTS});
814 foreach my $e (@{$s->{ELEMENTS}}) {
815 if ($e->{POINTERS} and not defined(pointer_type($e))) {
816 $e->{PROPERTIES}->{$default} = '1';
821 sub FindNestedTypes($$)
823 sub FindNestedTypes($$);
824 my ($l, $t) = @_;
826 return unless defined($t->{ELEMENTS});
827 return if ($t->{TYPE} eq "ENUM");
828 return if ($t->{TYPE} eq "BITMAP");
830 foreach (@{$t->{ELEMENTS}}) {
831 if (ref($_->{TYPE}) eq "HASH") {
832 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
833 FindNestedTypes($l, $_->{TYPE});
838 sub ParseInterface($)
840 my $idl = shift;
841 my @types = ();
842 my @consts = ();
843 my @functions = ();
844 my @endpoints;
845 my $opnum = 0;
846 my $version;
847 my $ms_union = 0;
848 $ms_union = 1 if has_property($idl, "ms_union");
850 if (not has_property($idl, "pointer_default")) {
851 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
852 # and "unique" in Microsoft Extensions mode (default)
853 $idl->{PROPERTIES}->{pointer_default} = "unique";
856 foreach my $d (@{$idl->{DATA}}) {
857 if ($d->{TYPE} eq "FUNCTION") {
858 push (@functions, ParseFunction($idl, $d, \$opnum, $ms_union));
859 } elsif ($d->{TYPE} eq "CONST") {
860 push (@consts, ParseConst($idl, $d));
861 } else {
862 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}, $ms_union));
863 FindNestedTypes(\@types, $d);
867 $version = "0.0";
869 if(defined $idl->{PROPERTIES}->{version}) {
870 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
871 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
872 $version = $idl->{PROPERTIES}->{version};
873 } else {
874 $version = $if_version[1] << 16 | $if_version[0];
878 # If no endpoint is set, default to the interface name as a named pipe
879 if (!defined $idl->{PROPERTIES}->{endpoint}) {
880 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
881 } else {
882 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
885 return {
886 NAME => $idl->{NAME},
887 UUID => lc(has_property($idl, "uuid")),
888 VERSION => $version,
889 TYPE => "INTERFACE",
890 PROPERTIES => $idl->{PROPERTIES},
891 FUNCTIONS => \@functions,
892 CONSTS => \@consts,
893 TYPES => \@types,
894 ENDPOINTS => \@endpoints
898 # Convert a IDL tree to a NDR tree
899 # Gives a result tree describing all that's necessary for easily generating
900 # NDR parsers / generators
901 sub Parse($)
903 my $idl = shift;
905 return undef unless (defined($idl));
907 Parse::Pidl::NDR::Validate($idl);
909 my @ndr = ();
911 foreach (@{$idl}) {
912 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
913 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
914 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
917 return \@ndr;
920 sub GetNextLevel($$)
922 my $e = shift;
923 my $fl = shift;
925 my $seen = 0;
927 foreach my $l (@{$e->{LEVELS}}) {
928 return $l if ($seen);
929 ($seen = 1) if ($l == $fl);
932 return undef;
935 sub GetPrevLevel($$)
937 my ($e,$fl) = @_;
938 my $prev = undef;
940 foreach my $l (@{$e->{LEVELS}}) {
941 (return $prev) if ($l == $fl);
942 $prev = $l;
945 return undef;
948 sub ContainsString($)
950 my ($e) = @_;
952 foreach my $l (@{$e->{LEVELS}}) {
953 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
956 return 0;
959 sub ContainsDeferred($$)
961 my ($e,$l) = @_;
963 return 1 if ($l->{CONTAINS_DEFERRED});
965 while ($l = GetNextLevel($e,$l))
967 return 1 if ($l->{IS_DEFERRED});
968 return 1 if ($l->{CONTAINS_DEFERRED});
971 return 0;
974 sub ContainsPipe($$)
976 my ($e,$l) = @_;
978 return 1 if ($l->{TYPE} eq "PIPE");
980 while ($l = GetNextLevel($e,$l))
982 return 1 if ($l->{TYPE} eq "PIPE");
985 return 0;
988 sub el_name($)
990 my $e = shift;
991 my $name = "<ANONYMOUS>";
993 $name = $e->{NAME} if defined($e->{NAME});
995 if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
996 return "$e->{PARENT}->{NAME}.$name";
999 if (defined($e->{PARENT}) and
1000 defined($e->{PARENT}->{PARENT}) and
1001 defined($e->{PARENT}->{PARENT}->{NAME})) {
1002 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
1005 return $name;
1008 ###################################
1009 # find a sibling var in a structure
1010 sub find_sibling($$)
1012 my($e,$name) = @_;
1013 my($fn) = $e->{PARENT};
1015 if ($name =~ /\*(.*)/) {
1016 $name = $1;
1019 for my $e2 (@{$fn->{ELEMENTS}}) {
1020 return $e2 if ($e2->{NAME} eq $name);
1023 return undef;
1026 my %property_list = (
1027 # interface
1028 "helpstring" => ["INTERFACE", "FUNCTION"],
1029 "version" => ["INTERFACE"],
1030 "uuid" => ["INTERFACE"],
1031 "endpoint" => ["INTERFACE"],
1032 "pointer_default" => ["INTERFACE"],
1033 "helper" => ["INTERFACE"],
1034 "pyhelper" => ["INTERFACE"],
1035 "authservice" => ["INTERFACE"],
1036 "restricted" => ["INTERFACE"],
1037 "no_srv_register" => ["INTERFACE"],
1039 # dcom
1040 "object" => ["INTERFACE"],
1041 "local" => ["INTERFACE", "FUNCTION"],
1042 "iid_is" => ["ELEMENT"],
1043 "call_as" => ["FUNCTION"],
1044 "idempotent" => ["FUNCTION"],
1046 # function
1047 "noopnum" => ["FUNCTION"],
1048 "in" => ["ELEMENT"],
1049 "out" => ["ELEMENT"],
1051 # pointer
1052 "ref" => ["ELEMENT", "TYPEDEF"],
1053 "ptr" => ["ELEMENT", "TYPEDEF"],
1054 "unique" => ["ELEMENT", "TYPEDEF"],
1055 "ignore" => ["ELEMENT"],
1056 "relative" => ["ELEMENT", "TYPEDEF"],
1057 "relative_short" => ["ELEMENT", "TYPEDEF"],
1058 "null_is_ffffffff" => ["ELEMENT"],
1059 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
1061 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
1062 "value" => ["ELEMENT"],
1063 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1065 # generic
1066 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1067 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1068 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1069 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
1070 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT", "PIPE"],
1071 "nopython" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
1072 "todo" => ["FUNCTION"],
1073 "skip" => ["ELEMENT"],
1075 # union
1076 "switch_is" => ["ELEMENT"],
1077 "switch_type" => ["ELEMENT", "UNION"],
1078 "nodiscriminant" => ["UNION"],
1079 "ms_union" => ["INTERFACE", "UNION"],
1080 "case" => ["ELEMENT"],
1081 "default" => ["ELEMENT"],
1083 "represent_as" => ["ELEMENT"],
1084 "transmit_as" => ["ELEMENT"],
1086 # subcontext
1087 "subcontext" => ["ELEMENT"],
1088 "subcontext_size" => ["ELEMENT"],
1089 "compression" => ["ELEMENT"],
1091 # enum
1092 "enum8bit" => ["ENUM"],
1093 "enum16bit" => ["ENUM"],
1094 "v1_enum" => ["ENUM"],
1096 # bitmap
1097 "bitmap8bit" => ["BITMAP"],
1098 "bitmap16bit" => ["BITMAP"],
1099 "bitmap32bit" => ["BITMAP"],
1100 "bitmap64bit" => ["BITMAP"],
1102 # array
1103 "range" => ["ELEMENT", "PIPE"],
1104 "size_is" => ["ELEMENT"],
1105 "string" => ["ELEMENT"],
1106 "noheader" => ["ELEMENT"],
1107 "charset" => ["ELEMENT"],
1108 "length_is" => ["ELEMENT"],
1109 "to_null" => ["ELEMENT"],
1112 #####################################################################
1113 # check for unknown properties
1114 sub ValidProperties($$)
1116 my ($e,$t) = @_;
1118 return unless defined $e->{PROPERTIES};
1120 foreach my $key (keys %{$e->{PROPERTIES}}) {
1121 warning($e, el_name($e) . ": unknown property '$key'")
1122 unless defined($property_list{$key});
1124 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
1125 unless grep(/^$t$/, @{$property_list{$key}});
1129 sub mapToScalar($)
1131 sub mapToScalar($);
1132 my $t = shift;
1133 return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
1134 my $ti = getType($t);
1136 if (not defined ($ti)) {
1137 return undef;
1138 } elsif ($ti->{TYPE} eq "TYPEDEF") {
1139 return mapToScalar($ti->{DATA});
1140 } elsif ($ti->{TYPE} eq "ENUM") {
1141 return Parse::Pidl::Typelist::enum_type_fn($ti);
1142 } elsif ($ti->{TYPE} eq "BITMAP") {
1143 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
1146 return undef;
1149 #####################################################################
1150 # validate an element
1151 sub ValidElement($)
1153 my $e = shift;
1155 ValidProperties($e,"ELEMENT");
1157 # Check whether switches are used correctly.
1158 if (my $switch = has_property($e, "switch_is")) {
1159 my $e2 = find_sibling($e, $switch);
1160 my $type = getType($e->{TYPE});
1162 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
1163 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1166 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
1167 my $discriminator_type = has_property($type->{DATA}, "switch_type");
1168 $discriminator_type = "uint32" unless defined ($discriminator_type);
1170 my $t1 = mapScalarType(mapToScalar($discriminator_type));
1172 if (not defined($t1)) {
1173 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1176 my $t2 = mapScalarType(mapToScalar($e2->{TYPE}));
1177 if (not defined($t2)) {
1178 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1181 if ($t1 ne $t2) {
1182 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1187 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1188 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1191 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1192 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1195 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1196 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1199 if (has_property($e, "represent_as") and has_property($e, "value")) {
1200 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1203 if (has_property($e, "subcontext")) {
1204 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1207 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1208 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1211 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1212 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1215 if (!$e->{POINTERS} && (
1216 has_property($e, "ptr") or
1217 has_property($e, "unique") or
1218 has_property($e, "relative") or
1219 has_property($e, "relative_short") or
1220 has_property($e, "ref"))) {
1221 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
1225 #####################################################################
1226 # validate an enum
1227 sub ValidEnum($)
1229 my ($enum) = @_;
1231 ValidProperties($enum, "ENUM");
1234 #####################################################################
1235 # validate a bitmap
1236 sub ValidBitmap($)
1238 my ($bitmap) = @_;
1240 ValidProperties($bitmap, "BITMAP");
1243 #####################################################################
1244 # validate a struct
1245 sub ValidStruct($)
1247 my($struct) = shift;
1249 ValidProperties($struct, "STRUCT");
1251 return unless defined($struct->{ELEMENTS});
1253 foreach my $e (@{$struct->{ELEMENTS}}) {
1254 $e->{PARENT} = $struct;
1255 ValidElement($e);
1259 #####################################################################
1260 # parse a union
1261 sub ValidUnion($)
1263 my($union) = shift;
1265 ValidProperties($union,"UNION");
1267 if (has_property($union->{PARENT}, "nodiscriminant") and
1268 has_property($union->{PARENT}, "switch_type")) {
1269 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1272 return unless defined($union->{ELEMENTS});
1274 foreach my $e (@{$union->{ELEMENTS}}) {
1275 $e->{PARENT} = $union;
1277 if (defined($e->{PROPERTIES}->{default}) and
1278 defined($e->{PROPERTIES}->{case})) {
1279 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1282 unless (defined ($e->{PROPERTIES}->{default}) or
1283 defined ($e->{PROPERTIES}->{case})) {
1284 fatal($e, "Union member $e->{NAME} must have default or case property");
1287 if (has_property($e, "ref")) {
1288 fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1292 ValidElement($e);
1296 #####################################################################
1297 # validate a pipe
1298 sub ValidPipe($)
1300 my ($pipe) = @_;
1301 my $struct = $pipe->{DATA};
1303 ValidProperties($pipe, "PIPE");
1305 $struct->{PARENT} = $pipe;
1307 $struct->{FILE} = $pipe->{FILE} unless defined($struct->{FILE});
1308 $struct->{LINE} = $pipe->{LINE} unless defined($struct->{LINE});
1310 ValidType($struct);
1313 #####################################################################
1314 # parse a typedef
1315 sub ValidTypedef($)
1317 my($typedef) = shift;
1318 my $data = $typedef->{DATA};
1320 ValidProperties($typedef, "TYPEDEF");
1322 return unless (ref($data) eq "HASH");
1324 $data->{PARENT} = $typedef;
1326 $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1327 $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1329 ValidType($data);
1332 #####################################################################
1333 # validate a function
1334 sub ValidFunction($)
1336 my($fn) = shift;
1338 ValidProperties($fn,"FUNCTION");
1340 foreach my $e (@{$fn->{ELEMENTS}}) {
1341 $e->{PARENT} = $fn;
1342 if (has_property($e, "ref") && !$e->{POINTERS}) {
1343 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1345 ValidElement($e);
1349 #####################################################################
1350 # validate a type
1351 sub ValidType($)
1353 my ($t) = @_;
1356 TYPEDEF => \&ValidTypedef,
1357 STRUCT => \&ValidStruct,
1358 UNION => \&ValidUnion,
1359 ENUM => \&ValidEnum,
1360 BITMAP => \&ValidBitmap,
1361 PIPE => \&ValidPipe
1362 }->{$t->{TYPE}}->($t);
1365 #####################################################################
1366 # parse the interface definitions
1367 sub ValidInterface($)
1369 my($interface) = shift;
1370 my($data) = $interface->{DATA};
1372 if (has_property($interface, "helper")) {
1373 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1376 ValidProperties($interface,"INTERFACE");
1378 if (has_property($interface, "pointer_default")) {
1379 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1380 ("ref", "unique", "ptr"))) {
1381 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1385 if (has_property($interface, "object")) {
1386 if (has_property($interface, "version") &&
1387 $interface->{PROPERTIES}->{version} != 0) {
1388 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1391 if (!defined($interface->{BASE}) &&
1392 not ($interface->{NAME} eq "IUnknown")) {
1393 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1397 foreach my $d (@{$data}) {
1398 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1399 ($d->{TYPE} eq "TYPEDEF" or
1400 $d->{TYPE} eq "STRUCT" or
1401 $d->{TYPE} eq "UNION" or
1402 $d->{TYPE} eq "ENUM" or
1403 $d->{TYPE} eq "BITMAP" or
1404 $d->{TYPE} eq "PIPE") && ValidType($d);
1409 #####################################################################
1410 # Validate an IDL structure
1411 sub Validate($)
1413 my($idl) = shift;
1415 foreach my $x (@{$idl}) {
1416 ($x->{TYPE} eq "INTERFACE") &&
1417 ValidInterface($x);
1418 ($x->{TYPE} eq "IMPORTLIB") &&
1419 fatal($x, "importlib() not supported");
1423 sub is_charset_array($$)
1425 my ($e,$l) = @_;
1427 return 0 if ($l->{TYPE} ne "ARRAY");
1429 my $nl = GetNextLevel($e,$l);
1431 return 0 unless ($nl->{TYPE} eq "DATA");
1433 return has_property($e, "charset");