pidl/lib: Add recursion detection logic to prevent looping.
[Samba.git] / pidl / lib / Parse / Pidl / NDR.pm
blob6cd7162232863b7fe648d6fbc00e5c8cc835ecc3
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 ReturnTypeElement ValidElement align_type mapToScalar ParseType can_contain_deferred is_charset_array);
40 use strict;
41 use warnings;
42 use Parse::Pidl qw(warning fatal);
43 use Parse::Pidl::Typelist qw(hasType getType typeIs expandAlias mapScalarType is_fixed_size_scalar);
44 use Parse::Pidl::Util qw(has_property property_matches);
46 # Alignment of the built-in scalar types
47 my $scalar_alignment = {
48 'void' => 0,
49 'char' => 1,
50 'int8' => 1,
51 'uint8' => 1,
52 'int16' => 2,
53 'uint16' => 2,
54 'int1632' => 3,
55 'uint1632' => 3,
56 'int32' => 4,
57 'uint32' => 4,
58 'int3264' => 5,
59 'uint3264' => 5,
60 'hyper' => 8,
61 'int64' => 8,
62 'double' => 8,
63 'pointer' => 8,
64 'dlong' => 4,
65 'udlong' => 4,
66 'udlongr' => 4,
67 'DATA_BLOB' => 4,
68 'string' => 4,
69 'string_array' => 4, #???
70 'time_t' => 4,
71 'uid_t' => 8,
72 'gid_t' => 8,
73 'NTTIME' => 4,
74 'NTTIME_1sec' => 4,
75 'NTTIME_hyper' => 8,
76 'WERROR' => 4,
77 'NTSTATUS' => 4,
78 'COMRESULT' => 4,
79 'dns_string' => 4,
80 'nbt_string' => 4,
81 'wrepl_nbt_name' => 4,
82 'ipv4address' => 4,
83 'ipv6address' => 4, #16?
84 'dnsp_name' => 1,
85 'dnsp_string' => 1,
86 'HRESULT' => 4,
89 sub GetElementLevelTable($$$)
91 my ($e, $pointer_default, $ms_union) = @_;
93 my $order = [];
94 my $is_deferred = 0;
95 my @bracket_array = ();
96 my @length_is = ();
97 my @size_is = ();
98 my $pointer_idx = 0;
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 if (has_property($e, "out")) {
113 my $needptrs = 1;
115 if (has_property($e, "string") and not has_property($e, "in")) { $needptrs++; }
116 if ($#bracket_array >= 0) { $needptrs = 0; }
118 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
121 my $allow_pipe = (($e->{PARENT}->{TYPE} // '') eq "FUNCTION");
122 my $is_pipe = typeIs($e->{TYPE}, "PIPE");
124 if ($is_pipe) {
125 if (not $allow_pipe) {
126 fatal($e, "argument `$e->{NAME}' is a pipe and not allowed on $e->{PARENT}->{TYPE}");
129 if ($e->{POINTERS} > 1) {
130 fatal($e, "$e->{POINTERS} are not allowed on pipe element $e->{NAME}");
133 if ($e->{POINTERS} < 0) {
134 fatal($e, "pipe element $e->{NAME} needs pointer");
137 if ($e->{POINTERS} == 1 and pointer_type($e) ne "ref") {
138 fatal($e, "pointer should be 'ref' on pipe element $e->{NAME}");
141 if (scalar(@size_is) > 0) {
142 fatal($e, "size_is() on pipe element");
145 if (scalar(@length_is) > 0) {
146 fatal($e, "length_is() on pipe element");
149 if (scalar(@bracket_array) > 0) {
150 fatal($e, "brackets on pipe element");
153 if (defined(has_property($e, "subcontext"))) {
154 fatal($e, "subcontext on pipe element");
157 if (has_property($e, "switch_is")) {
158 fatal($e, "switch_is on pipe element");
161 if (can_contain_deferred($e->{TYPE})) {
162 fatal($e, "$e->{TYPE} can_contain_deferred - not allowed on pipe element");
166 # Parse the [][][][] style array stuff
167 for my $i (0 .. $#bracket_array) {
168 my $d = $bracket_array[$#bracket_array - $i];
169 my $size = $d;
170 my $length = $d;
171 my $is_surrounding = 0;
172 my $is_varying = 0;
173 my $is_conformant = 0;
174 my $is_string = 0;
175 my $is_fixed = 0;
176 my $is_inline = 0;
177 my $is_to_null = 0;
179 if ($d eq "*") {
180 $is_conformant = 1;
181 if ($size = shift @size_is) {
182 if ($e->{POINTERS} < 1 and has_property($e, "string")) {
183 $is_string = 1;
184 delete($e->{PROPERTIES}->{string});
186 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
187 $is_string = 1;
188 delete($e->{PROPERTIES}->{string});
189 } else {
190 fatal($e, "Must specify size_is() for conformant array!")
193 if (($length = shift @length_is) or $is_string) {
194 $is_varying = 1;
195 } else {
196 $length = $size;
199 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
200 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
201 $is_surrounding = 1;
205 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
206 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
208 if ($i == 0 and $is_fixed and has_property($e, "string")) {
209 $is_fixed = 0;
210 $is_varying = 1;
211 $is_string = 1;
212 delete($e->{PROPERTIES}->{string});
215 if (has_property($e, "to_null")) {
216 $is_to_null = 1;
219 push (@$order, {
220 TYPE => "ARRAY",
221 SIZE_IS => $size,
222 LENGTH_IS => $length,
223 IS_DEFERRED => $is_deferred,
224 IS_SURROUNDING => $is_surrounding,
225 IS_ZERO_TERMINATED => $is_string,
226 IS_VARYING => $is_varying,
227 IS_CONFORMANT => $is_conformant,
228 IS_FIXED => $is_fixed,
229 IS_INLINE => $is_inline,
230 IS_TO_NULL => $is_to_null
234 # Next, all the pointers
235 foreach my $i (1..$e->{POINTERS}) {
236 my $level = "EMBEDDED";
237 # Top level "ref" pointers do not have a referrent identifier
238 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
240 my $pt;
242 # Only the first level gets the pointer type from the
243 # pointer property, the others get them from
244 # the pointer_default() interface property
246 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
247 # (Here they talk about the rightmost pointer, but testing shows
248 # they mean the leftmost pointer.)
250 # --metze
252 $pt = pointer_type($e);
253 if ($i > 1) {
254 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
255 $pt = $pointer_default;
258 push (@$order, {
259 TYPE => "POINTER",
260 POINTER_TYPE => $pt,
261 POINTER_INDEX => $pointer_idx,
262 IS_DEFERRED => "$is_deferred",
263 LEVEL => $level
266 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
267 if ($i == 1 and $pt ne "ref" and
268 $e->{PARENT}->{TYPE} eq "FUNCTION" and
269 not has_property($e, "in"));
271 $pointer_idx++;
273 # everything that follows will be deferred
274 $is_deferred = 1 if ($level ne "TOP");
276 my $array_size = shift @size_is;
277 my $array_length;
278 my $is_varying;
279 my $is_conformant;
280 my $is_string = 0;
281 if ($array_size) {
282 $is_conformant = 1;
283 if ($array_length = shift @length_is) {
284 $is_varying = 1;
285 } else {
286 $array_length = $array_size;
287 $is_varying =0;
291 if (scalar(@size_is) == 0 and has_property($e, "string") and
292 $i == $e->{POINTERS}) {
293 $is_string = 1;
294 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
295 delete($e->{PROPERTIES}->{string});
298 if ($array_size or $is_string) {
299 push (@$order, {
300 TYPE => "ARRAY",
301 SIZE_IS => $array_size,
302 LENGTH_IS => $array_length,
303 IS_DEFERRED => $is_deferred,
304 IS_SURROUNDING => 0,
305 IS_ZERO_TERMINATED => $is_string,
306 IS_VARYING => $is_varying,
307 IS_CONFORMANT => $is_conformant,
308 IS_FIXED => 0,
309 IS_INLINE => 0
312 $is_deferred = 0;
316 if ($is_pipe) {
317 push (@$order, {
318 TYPE => "PIPE",
319 IS_DEFERRED => 0,
320 CONTAINS_DEFERRED => 0,
323 my $i = 0;
324 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
326 return $order;
329 if (defined(has_property($e, "subcontext"))) {
330 my $hdr_size = has_property($e, "subcontext");
331 my $subsize = has_property($e, "subcontext_size");
332 if (not defined($subsize)) {
333 $subsize = -1;
336 push (@$order, {
337 TYPE => "SUBCONTEXT",
338 HEADER_SIZE => $hdr_size,
339 SUBCONTEXT_SIZE => $subsize,
340 IS_DEFERRED => $is_deferred,
341 COMPRESSION => has_property($e, "compression"),
345 if (my $switch = has_property($e, "switch_is")) {
346 push (@$order, {
347 TYPE => "SWITCH",
348 SWITCH_IS => $switch,
349 IS_DEFERRED => $is_deferred
353 if (scalar(@size_is) > 0) {
354 fatal($e, "size_is() on non-array element");
357 if (scalar(@length_is) > 0) {
358 fatal($e, "length_is() on non-array element");
361 if (has_property($e, "string")) {
362 fatal($e, "string() attribute on non-array element");
365 push (@$order, {
366 TYPE => "DATA",
367 DATA_TYPE => $e->{TYPE},
368 IS_DEFERRED => $is_deferred,
369 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
370 IS_SURROUNDING => 0 #FIXME
373 my $i = 0;
374 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
376 return $order;
379 sub GetTypedefLevelTable($$$$)
381 my ($e, $data, $pointer_default, $ms_union) = @_;
383 my $order = [];
385 push (@$order, {
386 TYPE => "TYPEDEF"
389 my $i = 0;
390 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
392 return $order;
395 #####################################################################
396 # see if a type contains any deferred data
397 sub can_contain_deferred
399 sub can_contain_deferred;
400 my ($type, @types_visited) = @_;
402 return 1 unless (hasType($type)); # assume the worst
404 $type = getType($type);
406 return 0 if (Parse::Pidl::Typelist::is_scalar($type));
408 foreach (@types_visited) {
409 if ($_ == $type) {
410 # we have already encountered this
411 # type, avoid recursion loop here
412 # and return
413 return 0;
417 return can_contain_deferred($type->{DATA},
418 @types_visited) if ($type->{TYPE} eq "TYPEDEF");
420 return 0 unless defined($type->{ELEMENTS});
422 foreach (@{$type->{ELEMENTS}}) {
423 return 1 if ($_->{POINTERS});
424 push(@types_visited,$type);
425 if (can_contain_deferred ($_->{TYPE},@types_visited)) {
426 pop(@types_visited);
427 return 1;
429 pop(@types_visited);
431 return 0;
434 sub pointer_type($)
436 my $e = shift;
438 return undef unless $e->{POINTERS};
440 return "ref" if (has_property($e, "ref"));
441 return "full" if (has_property($e, "ptr"));
442 return "sptr" if (has_property($e, "sptr"));
443 return "unique" if (has_property($e, "unique"));
444 return "relative" if (has_property($e, "relative"));
445 return "relative_short" if (has_property($e, "relative_short"));
446 return "ignore" if (has_property($e, "ignore"));
448 return undef;
451 #####################################################################
452 # work out the correct alignment for a structure or union
453 sub find_largest_alignment
455 my ($s, @types_visited) = @_;
457 my $align = 1;
458 for my $e (@{$s->{ELEMENTS}}) {
459 my $a = 1;
460 if ($e->{POINTERS}) {
461 # this is a hack for NDR64
462 # the NDR layer translates this into
463 # an alignment of 4 for NDR and 8 for NDR64
464 $a = 5;
465 } elsif (has_property($e, "subcontext")) {
466 $a = 1;
467 } elsif (has_property($e, "transmit_as")) {
468 $a = align_type($e->{PROPERTIES}->{transmit_as},
469 @types_visited);
470 } else {
471 $a = align_type($e->{TYPE}, @types_visited);
474 $align = $a if ($align < $a);
477 return $align;
480 #####################################################################
481 # align a type
482 sub align_type
484 sub align_type;
485 my ($e, @types_visited) = @_;
486 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
487 my $ret = $scalar_alignment->{$e->{NAME}};
488 if (not defined $ret) {
489 warning($e, "no scalar alignment for $e->{NAME}!");
490 return 0;
492 return $ret;
495 return 0 if ($e eq "EMPTY");
497 unless (hasType($e)) {
498 # it must be an external type - all we can do is guess
499 # warning($e, "assuming alignment of unknown type '$e' is 4");
500 return 4;
502 my $dt = getType($e);
504 foreach (@types_visited) {
505 if ($_ == $dt) {
506 # Chapt 14 of the DCE 1.1: Remote Procedure Call
507 # specification (available from pubs.opengroup.org)
508 # states:
509 # "The alignment of a structure in the octet stream is
510 # the largest of the alignments of the fields it
511 # contains. These fields may also be constructed types.
512 # The same alignment rules apply recursively to
513 # nested constructed types. "
515 # in the worst case scenario
516 # struct c1 {
517 # membertypea mema;
518 # membertypeb memb;
519 # struct c1 memc;
521 # the nested struct c1 memc when encountered
522 # returns 0 ensuring the alignment will be calculated
523 # based on the other fields
524 return 0;
529 if ($dt->{TYPE} eq "TYPEDEF") {
530 return align_type($dt->{DATA}, @types_visited);
531 } elsif ($dt->{TYPE} eq "CONFORMANCE") {
532 return $dt->{DATA}->{ALIGN};
533 } elsif ($dt->{TYPE} eq "ENUM") {
534 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt),
535 @types_visited);
536 } elsif ($dt->{TYPE} eq "BITMAP") {
537 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt),
538 @types_visited);
539 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
540 # Struct/union without body: assume 4
541 return 4 unless (defined($dt->{ELEMENTS}));
542 my $res;
543 push(@types_visited, $dt);
544 $res = find_largest_alignment($dt, @types_visited);
545 pop(@types_visited);
546 return $res
547 } elsif (($dt->{TYPE} eq "PIPE")) {
548 return 5;
551 die("Unknown data type type $dt->{TYPE}");
554 sub ParseElement($$$)
556 my ($e, $pointer_default, $ms_union) = @_;
558 $e->{TYPE} = expandAlias($e->{TYPE});
560 if (ref($e->{TYPE}) eq "HASH") {
561 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default, $ms_union);
564 return {
565 NAME => $e->{NAME},
566 TYPE => $e->{TYPE},
567 PROPERTIES => $e->{PROPERTIES},
568 LEVELS => GetElementLevelTable($e, $pointer_default, $ms_union),
569 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
570 ALIGN => align_type($e->{TYPE}),
571 ORIGINAL => $e
575 sub ParseStruct($$$)
577 my ($struct, $pointer_default, $ms_union) = @_;
578 my @elements = ();
579 my $surrounding = undef;
581 return {
582 TYPE => "STRUCT",
583 NAME => $struct->{NAME},
584 SURROUNDING_ELEMENT => undef,
585 ELEMENTS => undef,
586 PROPERTIES => $struct->{PROPERTIES},
587 ORIGINAL => $struct,
588 ALIGN => undef
589 } unless defined($struct->{ELEMENTS});
591 CheckPointerTypes($struct, $pointer_default);
593 foreach my $x (@{$struct->{ELEMENTS}})
595 my $e = ParseElement($x, $pointer_default, $ms_union);
596 if ($x != $struct->{ELEMENTS}[-1] and
597 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
598 fatal($x, "conformant member not at end of struct");
600 push @elements, $e;
603 my $e = $elements[-1];
604 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
605 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
606 $surrounding = $e;
609 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
610 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
611 $surrounding = $struct->{ELEMENTS}[-1];
614 my $align = undef;
615 if ($struct->{NAME}) {
616 $align = align_type($struct->{NAME});
619 return {
620 TYPE => "STRUCT",
621 NAME => $struct->{NAME},
622 SURROUNDING_ELEMENT => $surrounding,
623 ELEMENTS => \@elements,
624 PROPERTIES => $struct->{PROPERTIES},
625 ORIGINAL => $struct,
626 ALIGN => $align
630 sub ParseUnion($$)
632 my ($e, $pointer_default, $ms_union) = @_;
633 my @elements = ();
634 my $is_ms_union = $ms_union;
635 $is_ms_union = 1 if has_property($e, "ms_union");
636 my $hasdefault = 0;
637 my $switch_type = has_property($e, "switch_type");
638 unless (defined($switch_type)) { $switch_type = "uint32"; }
639 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
641 return {
642 TYPE => "UNION",
643 NAME => $e->{NAME},
644 SWITCH_TYPE => $switch_type,
645 ELEMENTS => undef,
646 PROPERTIES => $e->{PROPERTIES},
647 HAS_DEFAULT => $hasdefault,
648 IS_MS_UNION => $is_ms_union,
649 ORIGINAL => $e,
650 ALIGN => undef
651 } unless defined($e->{ELEMENTS});
653 CheckPointerTypes($e, $pointer_default);
655 foreach my $x (@{$e->{ELEMENTS}})
657 my $t;
658 if ($x->{TYPE} eq "EMPTY") {
659 $t = { TYPE => "EMPTY" };
660 } else {
661 $t = ParseElement($x, $pointer_default, $ms_union);
663 if (has_property($x, "default")) {
664 $t->{CASE} = "default";
665 $hasdefault = 1;
666 } elsif (defined($x->{PROPERTIES}->{case})) {
667 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
668 } else {
669 die("Union element $x->{NAME} has neither default nor case property");
671 push @elements, $t;
674 my $align = undef;
675 if ($e->{NAME}) {
676 $align = align_type($e->{NAME});
679 return {
680 TYPE => "UNION",
681 NAME => $e->{NAME},
682 SWITCH_TYPE => $switch_type,
683 ELEMENTS => \@elements,
684 PROPERTIES => $e->{PROPERTIES},
685 HAS_DEFAULT => $hasdefault,
686 IS_MS_UNION => $is_ms_union,
687 ORIGINAL => $e,
688 ALIGN => $align
692 sub ParseEnum($$)
694 my ($e, $pointer_default, $ms_union) = @_;
696 return {
697 TYPE => "ENUM",
698 NAME => $e->{NAME},
699 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
700 ELEMENTS => $e->{ELEMENTS},
701 PROPERTIES => $e->{PROPERTIES},
702 ORIGINAL => $e
706 sub ParseBitmap($$$)
708 my ($e, $pointer_default, $ms_union) = @_;
710 return {
711 TYPE => "BITMAP",
712 NAME => $e->{NAME},
713 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
714 ELEMENTS => $e->{ELEMENTS},
715 PROPERTIES => $e->{PROPERTIES},
716 ORIGINAL => $e
720 sub ParsePipe($$$)
722 my ($pipe, $pointer_default, $ms_union) = @_;
724 my $pname = $pipe->{NAME};
725 $pname = $pipe->{PARENT}->{NAME} unless defined $pname;
727 if (not defined($pipe->{PROPERTIES})
728 and defined($pipe->{PARENT}->{PROPERTIES})) {
729 $pipe->{PROPERTIES} = $pipe->{PARENT}->{PROPERTIES};
732 if (ref($pipe->{DATA}) eq "HASH") {
733 if (not defined($pipe->{DATA}->{PROPERTIES})
734 and defined($pipe->{PROPERTIES})) {
735 $pipe->{DATA}->{PROPERTIES} = $pipe->{PROPERTIES};
739 my $struct = ParseStruct($pipe->{DATA}, $pointer_default, $ms_union);
740 $struct->{ALIGN} = 5;
741 $struct->{NAME} = "$pname\_chunk";
743 # 'count' is element [0] and 'array' [1]
744 my $e = $struct->{ELEMENTS}[1];
745 # level [0] is of type "ARRAY"
746 my $l = $e->{LEVELS}[1];
748 # here we check that pipe elements have a fixed size type
749 while (defined($l)) {
750 my $cl = $l;
751 $l = GetNextLevel($e, $cl);
752 if ($cl->{TYPE} ne "DATA") {
753 fatal($pipe, el_name($pipe) . ": pipe contains non DATA level");
756 # for now we only support scalars
757 next if is_fixed_size_scalar($cl->{DATA_TYPE});
759 fatal($pipe, el_name($pipe) . ": pipe contains non fixed size type[$cl->{DATA_TYPE}]");
762 return {
763 TYPE => "PIPE",
764 NAME => $pipe->{NAME},
765 DATA => $struct,
766 PROPERTIES => $pipe->{PROPERTIES},
767 ORIGINAL => $pipe,
771 sub ParseType($$$)
773 my ($d, $pointer_default, $ms_union) = @_;
775 my $data = {
776 STRUCT => \&ParseStruct,
777 UNION => \&ParseUnion,
778 ENUM => \&ParseEnum,
779 BITMAP => \&ParseBitmap,
780 TYPEDEF => \&ParseTypedef,
781 PIPE => \&ParsePipe,
782 }->{$d->{TYPE}}->($d, $pointer_default, $ms_union);
784 return $data;
787 sub ParseTypedef($$)
789 my ($d, $pointer_default, $ms_union) = @_;
791 my $data;
793 if (ref($d->{DATA}) eq "HASH") {
794 if (defined($d->{DATA}->{PROPERTIES})
795 and not defined($d->{PROPERTIES})) {
796 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
799 $data = ParseType($d->{DATA}, $pointer_default, $ms_union);
800 $data->{ALIGN} = align_type($d->{NAME});
801 } else {
802 $data = getType($d->{DATA});
805 return {
806 NAME => $d->{NAME},
807 TYPE => $d->{TYPE},
808 PROPERTIES => $d->{PROPERTIES},
809 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default, $ms_union),
810 DATA => $data,
811 ORIGINAL => $d
815 sub ParseConst($$)
817 my ($ndr,$d) = @_;
819 return $d;
822 sub ParseFunction($$$$)
824 my ($ndr,$d,$opnum,$ms_union) = @_;
825 my @elements = ();
826 my $rettype = undef;
827 my $thisopnum = undef;
829 CheckPointerTypes($d, "ref");
831 if (not defined($d->{PROPERTIES}{noopnum})) {
832 $thisopnum = ${$opnum};
833 ${$opnum}++;
836 foreach my $x (@{$d->{ELEMENTS}}) {
837 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default}, $ms_union);
838 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
839 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
841 push (@elements, $e);
844 if ($d->{RETURN_TYPE} ne "void") {
845 $rettype = expandAlias($d->{RETURN_TYPE});
848 return {
849 NAME => $d->{NAME},
850 TYPE => "FUNCTION",
851 OPNUM => $thisopnum,
852 RETURN_TYPE => $rettype,
853 PROPERTIES => $d->{PROPERTIES},
854 ELEMENTS => \@elements,
855 ORIGINAL => $d
859 sub ReturnTypeElement($)
861 my ($fn) = @_;
863 return undef unless defined($fn->{RETURN_TYPE});
865 my $e = {
866 "NAME" => "result",
867 "TYPE" => $fn->{RETURN_TYPE},
868 "PROPERTIES" => undef,
869 "POINTERS" => 0,
870 "ARRAY_LEN" => [],
871 "FILE" => $fn->{FILE},
872 "LINE" => $fn->{LINE},
875 return ParseElement($e, 0, 0);
878 sub CheckPointerTypes($$)
880 my ($s,$default) = @_;
882 return unless defined($s->{ELEMENTS});
884 foreach my $e (@{$s->{ELEMENTS}}) {
885 if ($e->{POINTERS} and not defined(pointer_type($e))) {
886 $e->{PROPERTIES}->{$default} = '1';
891 sub FindNestedTypes($$)
893 sub FindNestedTypes($$);
894 my ($l, $t) = @_;
896 return unless defined($t->{ELEMENTS});
897 return if ($t->{TYPE} eq "ENUM");
898 return if ($t->{TYPE} eq "BITMAP");
900 foreach (@{$t->{ELEMENTS}}) {
901 if (ref($_->{TYPE}) eq "HASH") {
902 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
903 FindNestedTypes($l, $_->{TYPE});
908 sub ParseInterface($)
910 my $idl = shift;
911 my @types = ();
912 my @consts = ();
913 my @functions = ();
914 my @endpoints;
915 my $opnum = 0;
916 my $version;
917 my $ms_union = 0;
918 $ms_union = 1 if has_property($idl, "ms_union");
920 if (not has_property($idl, "pointer_default")) {
921 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
922 # and "unique" in Microsoft Extensions mode (default)
923 $idl->{PROPERTIES}->{pointer_default} = "unique";
926 foreach my $d (@{$idl->{DATA}}) {
927 if ($d->{TYPE} eq "FUNCTION") {
928 push (@functions, ParseFunction($idl, $d, \$opnum, $ms_union));
929 } elsif ($d->{TYPE} eq "CONST") {
930 push (@consts, ParseConst($idl, $d));
931 } else {
932 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}, $ms_union));
933 FindNestedTypes(\@types, $d);
937 $version = "0.0";
939 if(defined $idl->{PROPERTIES}->{version}) {
940 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
941 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
942 $version = $idl->{PROPERTIES}->{version};
943 } else {
944 $version = $if_version[1] << 16 | $if_version[0];
948 # If no endpoint is set, default to the interface name as a named pipe
949 if (!defined $idl->{PROPERTIES}->{endpoint}) {
950 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
951 } else {
952 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
955 return {
956 NAME => $idl->{NAME},
957 UUID => lc(has_property($idl, "uuid") // ''),
958 VERSION => $version,
959 TYPE => "INTERFACE",
960 PROPERTIES => $idl->{PROPERTIES},
961 FUNCTIONS => \@functions,
962 CONSTS => \@consts,
963 TYPES => \@types,
964 ENDPOINTS => \@endpoints,
965 ORIGINAL => $idl
969 # Convert a IDL tree to a NDR tree
970 # Gives a result tree describing all that's necessary for easily generating
971 # NDR parsers / generators
972 sub Parse($)
974 my $idl = shift;
976 return undef unless (defined($idl));
978 Parse::Pidl::NDR::Validate($idl);
980 my @ndr = ();
982 foreach (@{$idl}) {
983 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
984 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
985 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
988 return \@ndr;
991 sub GetNextLevel($$)
993 my $e = shift;
994 my $fl = shift;
996 my $seen = 0;
998 foreach my $l (@{$e->{LEVELS}}) {
999 return $l if ($seen);
1000 ($seen = 1) if ($l == $fl);
1003 return undef;
1006 sub GetPrevLevel($$)
1008 my ($e,$fl) = @_;
1009 my $prev = undef;
1011 foreach my $l (@{$e->{LEVELS}}) {
1012 (return $prev) if ($l == $fl);
1013 $prev = $l;
1016 return undef;
1019 sub ContainsString($)
1021 my ($e) = @_;
1023 if (property_matches($e, "flag", ".*STR_NULLTERM.*")) {
1024 return 1;
1026 if (exists($e->{LEVELS}) and $e->{LEVELS}->[0]->{TYPE} eq "ARRAY" and
1027 ($e->{LEVELS}->[0]->{IS_FIXED} or $e->{LEVELS}->[0]->{IS_INLINE}) and
1028 has_property($e, "charset"))
1030 return 1;
1033 foreach my $l (@{$e->{LEVELS}}) {
1034 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
1036 if (property_matches($e, "charset", ".*DOS.*")) {
1037 return 1;
1040 return 0;
1043 sub ContainsDeferred($$)
1045 my ($e,$l) = @_;
1047 return 1 if ($l->{CONTAINS_DEFERRED});
1049 while ($l = GetNextLevel($e,$l))
1051 return 1 if ($l->{IS_DEFERRED});
1052 return 1 if ($l->{CONTAINS_DEFERRED});
1055 return 0;
1058 sub ContainsPipe($$)
1060 my ($e,$l) = @_;
1062 return 1 if ($l->{TYPE} eq "PIPE");
1064 while ($l = GetNextLevel($e,$l))
1066 return 1 if ($l->{TYPE} eq "PIPE");
1069 return 0;
1072 sub el_name($)
1074 my $e = shift;
1075 my $name = "<ANONYMOUS>";
1077 $name = $e->{NAME} if defined($e->{NAME});
1079 if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
1080 return "$e->{PARENT}->{NAME}.$name";
1083 if (defined($e->{PARENT}) and
1084 defined($e->{PARENT}->{PARENT}) and
1085 defined($e->{PARENT}->{PARENT}->{NAME})) {
1086 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
1089 return $name;
1092 ###################################
1093 # find a sibling var in a structure
1094 sub find_sibling($$)
1096 my($e,$name) = @_;
1097 my($fn) = $e->{PARENT};
1099 if ($name =~ /\*(.*)/) {
1100 $name = $1;
1103 for my $e2 (@{$fn->{ELEMENTS}}) {
1104 return $e2 if ($e2->{NAME} eq $name);
1107 return undef;
1110 my %property_list = (
1111 # interface
1112 "helpstring" => ["INTERFACE", "FUNCTION"],
1113 "version" => ["INTERFACE"],
1114 "uuid" => ["INTERFACE"],
1115 "endpoint" => ["INTERFACE"],
1116 "pointer_default" => ["INTERFACE"],
1117 "helper" => ["INTERFACE"],
1118 "pyhelper" => ["INTERFACE"],
1119 "authservice" => ["INTERFACE"],
1120 "restricted" => ["INTERFACE"],
1121 "no_srv_register" => ["INTERFACE"],
1123 # dcom
1124 "object" => ["INTERFACE"],
1125 "local" => ["INTERFACE", "FUNCTION"],
1126 "iid_is" => ["ELEMENT"],
1127 "call_as" => ["FUNCTION"],
1128 "idempotent" => ["FUNCTION"],
1130 # function
1131 "noopnum" => ["FUNCTION"],
1132 "in" => ["ELEMENT"],
1133 "out" => ["ELEMENT"],
1135 # pointer
1136 "ref" => ["ELEMENT", "TYPEDEF"],
1137 "ptr" => ["ELEMENT", "TYPEDEF"],
1138 "unique" => ["ELEMENT", "TYPEDEF"],
1139 "ignore" => ["ELEMENT"],
1140 "relative" => ["ELEMENT", "TYPEDEF"],
1141 "relative_short" => ["ELEMENT", "TYPEDEF"],
1142 "null_is_ffffffff" => ["ELEMENT"],
1143 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
1145 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
1146 "value" => ["ELEMENT"],
1147 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1148 "max_recursion" => ["ELEMENT"],
1150 # generic
1151 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1152 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1153 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1154 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
1155 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT", "PIPE"],
1156 "nopython" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
1157 "todo" => ["FUNCTION"],
1158 "skip" => ["ELEMENT"],
1159 "skip_noinit" => ["ELEMENT"],
1161 # union
1162 "switch_is" => ["ELEMENT"],
1163 "switch_type" => ["ELEMENT", "UNION"],
1164 "nodiscriminant" => ["UNION"],
1165 "ms_union" => ["INTERFACE", "UNION"],
1166 "case" => ["ELEMENT"],
1167 "default" => ["ELEMENT"],
1169 "represent_as" => ["ELEMENT"],
1170 "transmit_as" => ["ELEMENT"],
1172 # subcontext
1173 "subcontext" => ["ELEMENT"],
1174 "subcontext_size" => ["ELEMENT"],
1175 "compression" => ["ELEMENT"],
1177 # enum
1178 "enum8bit" => ["ENUM"],
1179 "enum16bit" => ["ENUM"],
1180 "v1_enum" => ["ENUM"],
1182 # bitmap
1183 "bitmap8bit" => ["BITMAP"],
1184 "bitmap16bit" => ["BITMAP"],
1185 "bitmap32bit" => ["BITMAP"],
1186 "bitmap64bit" => ["BITMAP"],
1188 # array
1189 "range" => ["ELEMENT", "PIPE"],
1190 "size_is" => ["ELEMENT"],
1191 "string" => ["ELEMENT"],
1192 "noheader" => ["ELEMENT"],
1193 "charset" => ["ELEMENT"],
1194 "length_is" => ["ELEMENT"],
1195 "to_null" => ["ELEMENT"],
1198 #####################################################################
1199 # check for unknown properties
1200 sub ValidProperties($$)
1202 my ($e,$t) = @_;
1204 return unless defined $e->{PROPERTIES};
1206 foreach my $key (keys %{$e->{PROPERTIES}}) {
1207 warning($e, el_name($e) . ": unknown property '$key'")
1208 unless defined($property_list{$key});
1210 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
1211 unless grep(/^$t$/, @{$property_list{$key}});
1215 sub mapToScalar($)
1217 sub mapToScalar($);
1218 my $t = shift;
1219 return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
1220 my $ti = getType($t);
1222 if (not defined ($ti)) {
1223 return undef;
1224 } elsif ($ti->{TYPE} eq "TYPEDEF") {
1225 return mapToScalar($ti->{DATA});
1226 } elsif ($ti->{TYPE} eq "ENUM") {
1227 return Parse::Pidl::Typelist::enum_type_fn($ti);
1228 } elsif ($ti->{TYPE} eq "BITMAP") {
1229 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
1232 return undef;
1235 #####################################################################
1236 # validate an element
1237 sub ValidElement($)
1239 my $e = shift;
1241 ValidProperties($e,"ELEMENT");
1243 # Check whether switches are used correctly.
1244 if (my $switch = has_property($e, "switch_is")) {
1245 my $e2 = find_sibling($e, $switch);
1246 my $type = getType($e->{TYPE});
1248 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
1249 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1252 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
1253 my $discriminator_type = has_property($type->{DATA}, "switch_type");
1254 $discriminator_type = "uint32" unless defined ($discriminator_type);
1256 my $t1 = mapScalarType(mapToScalar($discriminator_type));
1258 if (not defined($t1)) {
1259 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1262 my $t2 = mapScalarType(mapToScalar($e2->{TYPE}));
1263 if (not defined($t2)) {
1264 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1267 if ($t1 ne $t2) {
1268 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1273 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1274 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1277 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1278 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1281 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1282 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1285 if (has_property($e, "represent_as") and has_property($e, "value")) {
1286 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1289 if (has_property($e, "subcontext")) {
1290 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1293 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1294 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1297 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1298 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1301 if (!$e->{POINTERS} && (
1302 has_property($e, "ptr") or
1303 has_property($e, "unique") or
1304 has_property($e, "relative") or
1305 has_property($e, "relative_short") or
1306 has_property($e, "ref"))) {
1307 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
1311 #####################################################################
1312 # validate an enum
1313 sub ValidEnum($)
1315 my ($enum) = @_;
1317 ValidProperties($enum, "ENUM");
1320 #####################################################################
1321 # validate a bitmap
1322 sub ValidBitmap($)
1324 my ($bitmap) = @_;
1326 ValidProperties($bitmap, "BITMAP");
1329 #####################################################################
1330 # validate a struct
1331 sub ValidStruct($)
1333 my($struct) = shift;
1335 ValidProperties($struct, "STRUCT");
1337 return unless defined($struct->{ELEMENTS});
1339 foreach my $e (@{$struct->{ELEMENTS}}) {
1340 $e->{PARENT} = $struct;
1341 ValidElement($e);
1345 #####################################################################
1346 # parse a union
1347 sub ValidUnion($)
1349 my($union) = shift;
1351 ValidProperties($union,"UNION");
1353 if (has_property($union->{PARENT}, "nodiscriminant") and
1354 has_property($union->{PARENT}, "switch_type")) {
1355 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1358 return unless defined($union->{ELEMENTS});
1360 foreach my $e (@{$union->{ELEMENTS}}) {
1361 $e->{PARENT} = $union;
1363 if (defined($e->{PROPERTIES}->{default}) and
1364 defined($e->{PROPERTIES}->{case})) {
1365 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1368 unless (defined ($e->{PROPERTIES}->{default}) or
1369 defined ($e->{PROPERTIES}->{case})) {
1370 fatal($e, "Union member $e->{NAME} must have default or case property");
1373 if (has_property($e, "ref")) {
1374 fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1378 ValidElement($e);
1382 #####################################################################
1383 # validate a pipe
1384 sub ValidPipe($)
1386 my ($pipe) = @_;
1387 my $struct = $pipe->{DATA};
1389 ValidProperties($pipe, "PIPE");
1391 $struct->{PARENT} = $pipe;
1393 $struct->{FILE} = $pipe->{FILE} unless defined($struct->{FILE});
1394 $struct->{LINE} = $pipe->{LINE} unless defined($struct->{LINE});
1396 ValidType($struct);
1399 #####################################################################
1400 # parse a typedef
1401 sub ValidTypedef($)
1403 my($typedef) = shift;
1404 my $data = $typedef->{DATA};
1406 ValidProperties($typedef, "TYPEDEF");
1408 return unless (ref($data) eq "HASH");
1410 $data->{PARENT} = $typedef;
1412 $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1413 $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1415 ValidType($data);
1418 #####################################################################
1419 # validate a function
1420 sub ValidFunction($)
1422 my($fn) = shift;
1424 ValidProperties($fn,"FUNCTION");
1426 foreach my $e (@{$fn->{ELEMENTS}}) {
1427 $e->{PARENT} = $fn;
1428 if (has_property($e, "ref") && !$e->{POINTERS}) {
1429 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1431 ValidElement($e);
1435 #####################################################################
1436 # validate a type
1437 sub ValidType($)
1439 my ($t) = @_;
1442 TYPEDEF => \&ValidTypedef,
1443 STRUCT => \&ValidStruct,
1444 UNION => \&ValidUnion,
1445 ENUM => \&ValidEnum,
1446 BITMAP => \&ValidBitmap,
1447 PIPE => \&ValidPipe
1448 }->{$t->{TYPE}}->($t);
1451 #####################################################################
1452 # parse the interface definitions
1453 sub ValidInterface($)
1455 my($interface) = shift;
1456 my($data) = $interface->{DATA};
1458 if (has_property($interface, "helper")) {
1459 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1462 ValidProperties($interface,"INTERFACE");
1464 if (has_property($interface, "pointer_default")) {
1465 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1466 ("ref", "unique", "ptr"))) {
1467 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1471 if (has_property($interface, "object")) {
1472 if (has_property($interface, "version") &&
1473 $interface->{PROPERTIES}->{version} != 0) {
1474 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1477 if (!defined($interface->{BASE}) &&
1478 not ($interface->{NAME} eq "IUnknown")) {
1479 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1483 foreach my $d (@{$data}) {
1484 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1485 ($d->{TYPE} eq "TYPEDEF" or
1486 $d->{TYPE} eq "STRUCT" or
1487 $d->{TYPE} eq "UNION" or
1488 $d->{TYPE} eq "ENUM" or
1489 $d->{TYPE} eq "BITMAP" or
1490 $d->{TYPE} eq "PIPE") && ValidType($d);
1495 #####################################################################
1496 # Validate an IDL structure
1497 sub Validate($)
1499 my($idl) = shift;
1501 foreach my $x (@{$idl}) {
1502 ($x->{TYPE} eq "INTERFACE") &&
1503 ValidInterface($x);
1504 ($x->{TYPE} eq "IMPORTLIB") &&
1505 fatal($x, "importlib() not supported");
1509 sub is_charset_array($$)
1511 my ($e,$l) = @_;
1513 return 0 if ($l->{TYPE} ne "ARRAY");
1515 my $nl = GetNextLevel($e,$l);
1517 return 0 unless ($nl->{TYPE} eq "DATA");
1519 return has_property($e, "charset");