ndr dns: Add simple parser
[Samba/gebeck_regimport.git] / pidl / lib / Parse / Pidl / NDR.pm
blob21b75687a740fc81da6d6fd9e82f14a12bfff34a
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 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 expandAlias mapScalarType);
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 'NTTIME' => 4,
70 'NTTIME_1sec' => 4,
71 'NTTIME_hyper' => 8,
72 'WERROR' => 4,
73 'NTSTATUS' => 4,
74 'COMRESULT' => 4,
75 'dns_string' => 4,
76 'nbt_string' => 4,
77 'wrepl_nbt_name' => 4,
78 'ipv4address' => 4,
79 'ipv6address' => 4, #16?
80 'dnsp_name' => 1,
81 'dnsp_string' => 1
84 sub GetElementLevelTable($$)
86 my ($e, $pointer_default) = @_;
88 my $order = [];
89 my $is_deferred = 0;
90 my @bracket_array = ();
91 my @length_is = ();
92 my @size_is = ();
93 my $pointer_idx = 0;
95 if (has_property($e, "size_is")) {
96 @size_is = split /,/, has_property($e, "size_is");
99 if (has_property($e, "length_is")) {
100 @length_is = split /,/, has_property($e, "length_is");
103 if (defined($e->{ARRAY_LEN})) {
104 @bracket_array = @{$e->{ARRAY_LEN}};
107 if (has_property($e, "out")) {
108 my $needptrs = 1;
110 if (has_property($e, "string") and not has_property($e, "in")) { $needptrs++; }
111 if ($#bracket_array >= 0) { $needptrs = 0; }
113 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
116 # Parse the [][][][] style array stuff
117 for my $i (0 .. $#bracket_array) {
118 my $d = $bracket_array[$#bracket_array - $i];
119 my $size = $d;
120 my $length = $d;
121 my $is_surrounding = 0;
122 my $is_varying = 0;
123 my $is_conformant = 0;
124 my $is_string = 0;
125 my $is_fixed = 0;
126 my $is_inline = 0;
128 if ($d eq "*") {
129 $is_conformant = 1;
130 if ($size = shift @size_is) {
131 if ($e->{POINTERS} < 1 and has_property($e, "string")) {
132 $is_string = 1;
133 delete($e->{PROPERTIES}->{string});
135 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
136 $is_string = 1;
137 delete($e->{PROPERTIES}->{string});
138 } else {
139 fatal($e, "Must specify size_is() for conformant array!")
142 if (($length = shift @length_is) or $is_string) {
143 $is_varying = 1;
144 } else {
145 $length = $size;
148 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
149 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
150 $is_surrounding = 1;
154 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
155 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
157 if ($i == 0 and $is_fixed and has_property($e, "string")) {
158 $is_fixed = 0;
159 $is_varying = 1;
160 $is_string = 1;
161 delete($e->{PROPERTIES}->{string});
164 push (@$order, {
165 TYPE => "ARRAY",
166 SIZE_IS => $size,
167 LENGTH_IS => $length,
168 IS_DEFERRED => $is_deferred,
169 IS_SURROUNDING => $is_surrounding,
170 IS_ZERO_TERMINATED => $is_string,
171 IS_VARYING => $is_varying,
172 IS_CONFORMANT => $is_conformant,
173 IS_FIXED => $is_fixed,
174 IS_INLINE => $is_inline
178 # Next, all the pointers
179 foreach my $i (1..$e->{POINTERS}) {
180 my $level = "EMBEDDED";
181 # Top level "ref" pointers do not have a referrent identifier
182 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
184 my $pt;
186 # Only the first level gets the pointer type from the
187 # pointer property, the others get them from
188 # the pointer_default() interface property
190 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
191 # (Here they talk about the rightmost pointer, but testing shows
192 # they mean the leftmost pointer.)
194 # --metze
196 $pt = pointer_type($e);
197 if ($i > 1) {
198 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
199 $pt = $pointer_default;
202 push (@$order, {
203 TYPE => "POINTER",
204 POINTER_TYPE => $pt,
205 POINTER_INDEX => $pointer_idx,
206 IS_DEFERRED => "$is_deferred",
207 LEVEL => $level
210 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
211 if ($i == 1 and $pt ne "ref" and
212 $e->{PARENT}->{TYPE} eq "FUNCTION" and
213 not has_property($e, "in"));
215 $pointer_idx++;
217 # everything that follows will be deferred
218 $is_deferred = 1 if ($level ne "TOP");
220 my $array_size = shift @size_is;
221 my $array_length;
222 my $is_varying;
223 my $is_conformant;
224 my $is_string = 0;
225 if ($array_size) {
226 $is_conformant = 1;
227 if ($array_length = shift @length_is) {
228 $is_varying = 1;
229 } else {
230 $array_length = $array_size;
231 $is_varying =0;
235 if (scalar(@size_is) == 0 and has_property($e, "string") and
236 $i == $e->{POINTERS}) {
237 $is_string = 1;
238 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
239 delete($e->{PROPERTIES}->{string});
242 if ($array_size or $is_string) {
243 push (@$order, {
244 TYPE => "ARRAY",
245 SIZE_IS => $array_size,
246 LENGTH_IS => $array_length,
247 IS_DEFERRED => $is_deferred,
248 IS_SURROUNDING => 0,
249 IS_ZERO_TERMINATED => $is_string,
250 IS_VARYING => $is_varying,
251 IS_CONFORMANT => $is_conformant,
252 IS_FIXED => 0,
253 IS_INLINE => 0
256 $is_deferred = 0;
260 if (defined(has_property($e, "subcontext"))) {
261 my $hdr_size = has_property($e, "subcontext");
262 my $subsize = has_property($e, "subcontext_size");
263 if (not defined($subsize)) {
264 $subsize = -1;
267 push (@$order, {
268 TYPE => "SUBCONTEXT",
269 HEADER_SIZE => $hdr_size,
270 SUBCONTEXT_SIZE => $subsize,
271 IS_DEFERRED => $is_deferred,
272 COMPRESSION => has_property($e, "compression"),
276 if (my $switch = has_property($e, "switch_is")) {
277 push (@$order, {
278 TYPE => "SWITCH",
279 SWITCH_IS => $switch,
280 IS_DEFERRED => $is_deferred
284 if (scalar(@size_is) > 0) {
285 fatal($e, "size_is() on non-array element");
288 if (scalar(@length_is) > 0) {
289 fatal($e, "length_is() on non-array element");
292 if (has_property($e, "string")) {
293 fatal($e, "string() attribute on non-array element");
296 push (@$order, {
297 TYPE => "DATA",
298 DATA_TYPE => $e->{TYPE},
299 IS_DEFERRED => $is_deferred,
300 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
301 IS_SURROUNDING => 0 #FIXME
304 my $i = 0;
305 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
307 return $order;
310 sub GetTypedefLevelTable($$$)
312 my ($e, $data, $pointer_default) = @_;
314 my $order = [];
316 push (@$order, {
317 TYPE => "TYPEDEF"
320 my $i = 0;
321 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
323 return $order;
326 #####################################################################
327 # see if a type contains any deferred data
328 sub can_contain_deferred($)
330 sub can_contain_deferred($);
331 my ($type) = @_;
333 return 1 unless (hasType($type)); # assume the worst
335 $type = getType($type);
337 return 0 if (Parse::Pidl::Typelist::is_scalar($type));
339 return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
341 return 0 unless defined($type->{ELEMENTS});
343 foreach (@{$type->{ELEMENTS}}) {
344 return 1 if ($_->{POINTERS});
345 return 1 if (can_contain_deferred ($_->{TYPE}));
348 return 0;
351 sub pointer_type($)
353 my $e = shift;
355 return undef unless $e->{POINTERS};
357 return "ref" if (has_property($e, "ref"));
358 return "full" if (has_property($e, "ptr"));
359 return "sptr" if (has_property($e, "sptr"));
360 return "unique" if (has_property($e, "unique"));
361 return "relative" if (has_property($e, "relative"));
362 return "relative_short" if (has_property($e, "relative_short"));
363 return "ignore" if (has_property($e, "ignore"));
365 return undef;
368 #####################################################################
369 # work out the correct alignment for a structure or union
370 sub find_largest_alignment($)
372 my $s = shift;
374 my $align = 1;
375 for my $e (@{$s->{ELEMENTS}}) {
376 my $a = 1;
378 if ($e->{POINTERS}) {
379 # this is a hack for NDR64
380 # the NDR layer translates this into
381 # an alignment of 4 for NDR and 8 for NDR64
382 $a = 5;
383 } elsif (has_property($e, "subcontext")) {
384 $a = 1;
385 } elsif (has_property($e, "transmit_as")) {
386 $a = align_type($e->{PROPERTIES}->{transmit_as});
387 } else {
388 $a = align_type($e->{TYPE});
391 $align = $a if ($align < $a);
394 return $align;
397 #####################################################################
398 # align a type
399 sub align_type($)
401 sub align_type($);
402 my ($e) = @_;
404 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
405 return $scalar_alignment->{$e->{NAME}};
408 return 0 if ($e eq "EMPTY");
410 unless (hasType($e)) {
411 # it must be an external type - all we can do is guess
412 # warning($e, "assuming alignment of unknown type '$e' is 4");
413 return 4;
416 my $dt = getType($e);
418 if ($dt->{TYPE} eq "TYPEDEF") {
419 return align_type($dt->{DATA});
420 } elsif ($dt->{TYPE} eq "CONFORMANCE") {
421 return $dt->{DATA}->{ALIGN};
422 } elsif ($dt->{TYPE} eq "ENUM") {
423 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
424 } elsif ($dt->{TYPE} eq "BITMAP") {
425 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
426 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
427 # Struct/union without body: assume 4
428 return 4 unless (defined($dt->{ELEMENTS}));
429 return find_largest_alignment($dt);
432 die("Unknown data type type $dt->{TYPE}");
435 sub ParseElement($$)
437 my ($e, $pointer_default) = @_;
439 $e->{TYPE} = expandAlias($e->{TYPE});
441 if (ref($e->{TYPE}) eq "HASH") {
442 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default);
445 return {
446 NAME => $e->{NAME},
447 TYPE => $e->{TYPE},
448 PROPERTIES => $e->{PROPERTIES},
449 LEVELS => GetElementLevelTable($e, $pointer_default),
450 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
451 ALIGN => align_type($e->{TYPE}),
452 ORIGINAL => $e
456 sub ParseStruct($$)
458 my ($struct, $pointer_default) = @_;
459 my @elements = ();
460 my $surrounding = undef;
462 return {
463 TYPE => "STRUCT",
464 NAME => $struct->{NAME},
465 SURROUNDING_ELEMENT => undef,
466 ELEMENTS => undef,
467 PROPERTIES => $struct->{PROPERTIES},
468 ORIGINAL => $struct,
469 ALIGN => undef
470 } unless defined($struct->{ELEMENTS});
472 CheckPointerTypes($struct, $pointer_default);
474 foreach my $x (@{$struct->{ELEMENTS}})
476 my $e = ParseElement($x, $pointer_default);
477 if ($x != $struct->{ELEMENTS}[-1] and
478 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
479 fatal($x, "conformant member not at end of struct");
481 push @elements, $e;
484 my $e = $elements[-1];
485 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
486 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
487 $surrounding = $e;
490 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
491 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
492 $surrounding = $struct->{ELEMENTS}[-1];
495 my $align = undef;
496 if ($struct->{NAME}) {
497 $align = align_type($struct->{NAME});
500 return {
501 TYPE => "STRUCT",
502 NAME => $struct->{NAME},
503 SURROUNDING_ELEMENT => $surrounding,
504 ELEMENTS => \@elements,
505 PROPERTIES => $struct->{PROPERTIES},
506 ORIGINAL => $struct,
507 ALIGN => $align
511 sub ParseUnion($$)
513 my ($e, $pointer_default) = @_;
514 my @elements = ();
515 my $hasdefault = 0;
516 my $switch_type = has_property($e, "switch_type");
517 unless (defined($switch_type)) { $switch_type = "uint32"; }
518 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
520 return {
521 TYPE => "UNION",
522 NAME => $e->{NAME},
523 SWITCH_TYPE => $switch_type,
524 ELEMENTS => undef,
525 PROPERTIES => $e->{PROPERTIES},
526 HAS_DEFAULT => $hasdefault,
527 ORIGINAL => $e,
528 ALIGN => undef
529 } unless defined($e->{ELEMENTS});
531 CheckPointerTypes($e, $pointer_default);
533 foreach my $x (@{$e->{ELEMENTS}})
535 my $t;
536 if ($x->{TYPE} eq "EMPTY") {
537 $t = { TYPE => "EMPTY" };
538 } else {
539 $t = ParseElement($x, $pointer_default);
541 if (has_property($x, "default")) {
542 $t->{CASE} = "default";
543 $hasdefault = 1;
544 } elsif (defined($x->{PROPERTIES}->{case})) {
545 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
546 } else {
547 die("Union element $x->{NAME} has neither default nor case property");
549 push @elements, $t;
552 my $align = undef;
553 if ($e->{NAME}) {
554 $align = align_type($e->{NAME});
557 return {
558 TYPE => "UNION",
559 NAME => $e->{NAME},
560 SWITCH_TYPE => $switch_type,
561 ELEMENTS => \@elements,
562 PROPERTIES => $e->{PROPERTIES},
563 HAS_DEFAULT => $hasdefault,
564 ORIGINAL => $e,
565 ALIGN => $align
569 sub ParseEnum($$)
571 my ($e, $pointer_default) = @_;
573 return {
574 TYPE => "ENUM",
575 NAME => $e->{NAME},
576 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
577 ELEMENTS => $e->{ELEMENTS},
578 PROPERTIES => $e->{PROPERTIES},
579 ORIGINAL => $e
583 sub ParseBitmap($$)
585 my ($e, $pointer_default) = @_;
587 return {
588 TYPE => "BITMAP",
589 NAME => $e->{NAME},
590 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
591 ELEMENTS => $e->{ELEMENTS},
592 PROPERTIES => $e->{PROPERTIES},
593 ORIGINAL => $e
597 sub ParseType($$)
599 my ($d, $pointer_default) = @_;
601 my $data = {
602 STRUCT => \&ParseStruct,
603 UNION => \&ParseUnion,
604 ENUM => \&ParseEnum,
605 BITMAP => \&ParseBitmap,
606 TYPEDEF => \&ParseTypedef,
607 }->{$d->{TYPE}}->($d, $pointer_default);
609 return $data;
612 sub ParseTypedef($$)
614 my ($d, $pointer_default) = @_;
616 my $data;
618 if (ref($d->{DATA}) eq "HASH") {
619 if (defined($d->{DATA}->{PROPERTIES})
620 and not defined($d->{PROPERTIES})) {
621 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
624 $data = ParseType($d->{DATA}, $pointer_default);
625 $data->{ALIGN} = align_type($d->{NAME});
626 } else {
627 $data = getType($d->{DATA});
630 return {
631 NAME => $d->{NAME},
632 TYPE => $d->{TYPE},
633 PROPERTIES => $d->{PROPERTIES},
634 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default),
635 DATA => $data,
636 ORIGINAL => $d
640 sub ParseConst($$)
642 my ($ndr,$d) = @_;
644 return $d;
647 sub ParseFunction($$$)
649 my ($ndr,$d,$opnum) = @_;
650 my @elements = ();
651 my $rettype = undef;
652 my $thisopnum = undef;
654 CheckPointerTypes($d, "ref");
656 if (not defined($d->{PROPERTIES}{noopnum})) {
657 $thisopnum = ${$opnum};
658 ${$opnum}++;
661 foreach my $x (@{$d->{ELEMENTS}}) {
662 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default});
663 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
664 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
666 push (@elements, $e);
669 if ($d->{RETURN_TYPE} ne "void") {
670 $rettype = expandAlias($d->{RETURN_TYPE});
673 return {
674 NAME => $d->{NAME},
675 TYPE => "FUNCTION",
676 OPNUM => $thisopnum,
677 RETURN_TYPE => $rettype,
678 PROPERTIES => $d->{PROPERTIES},
679 ELEMENTS => \@elements,
680 ORIGINAL => $d
684 sub CheckPointerTypes($$)
686 my ($s,$default) = @_;
688 return unless defined($s->{ELEMENTS});
690 foreach my $e (@{$s->{ELEMENTS}}) {
691 if ($e->{POINTERS} and not defined(pointer_type($e))) {
692 $e->{PROPERTIES}->{$default} = '1';
697 sub FindNestedTypes($$)
699 sub FindNestedTypes($$);
700 my ($l, $t) = @_;
702 return unless defined($t->{ELEMENTS});
703 return if ($t->{TYPE} eq "ENUM");
704 return if ($t->{TYPE} eq "BITMAP");
706 foreach (@{$t->{ELEMENTS}}) {
707 if (ref($_->{TYPE}) eq "HASH") {
708 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
709 FindNestedTypes($l, $_->{TYPE});
714 sub ParseInterface($)
716 my $idl = shift;
717 my @types = ();
718 my @consts = ();
719 my @functions = ();
720 my @endpoints;
721 my $opnum = 0;
722 my $version;
724 if (not has_property($idl, "pointer_default")) {
725 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
726 # and "unique" in Microsoft Extensions mode (default)
727 $idl->{PROPERTIES}->{pointer_default} = "unique";
730 foreach my $d (@{$idl->{DATA}}) {
731 if ($d->{TYPE} eq "FUNCTION") {
732 push (@functions, ParseFunction($idl, $d, \$opnum));
733 } elsif ($d->{TYPE} eq "CONST") {
734 push (@consts, ParseConst($idl, $d));
735 } else {
736 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}));
737 FindNestedTypes(\@types, $d);
741 $version = "0.0";
743 if(defined $idl->{PROPERTIES}->{version}) {
744 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
745 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
746 $version = $idl->{PROPERTIES}->{version};
747 } else {
748 $version = $if_version[1] << 16 | $if_version[0];
752 # If no endpoint is set, default to the interface name as a named pipe
753 if (!defined $idl->{PROPERTIES}->{endpoint}) {
754 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
755 } else {
756 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
759 return {
760 NAME => $idl->{NAME},
761 UUID => lc(has_property($idl, "uuid")),
762 VERSION => $version,
763 TYPE => "INTERFACE",
764 PROPERTIES => $idl->{PROPERTIES},
765 FUNCTIONS => \@functions,
766 CONSTS => \@consts,
767 TYPES => \@types,
768 ENDPOINTS => \@endpoints
772 # Convert a IDL tree to a NDR tree
773 # Gives a result tree describing all that's necessary for easily generating
774 # NDR parsers / generators
775 sub Parse($)
777 my $idl = shift;
779 return undef unless (defined($idl));
781 Parse::Pidl::NDR::Validate($idl);
783 my @ndr = ();
785 foreach (@{$idl}) {
786 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
787 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
788 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
791 return \@ndr;
794 sub GetNextLevel($$)
796 my $e = shift;
797 my $fl = shift;
799 my $seen = 0;
801 foreach my $l (@{$e->{LEVELS}}) {
802 return $l if ($seen);
803 ($seen = 1) if ($l == $fl);
806 return undef;
809 sub GetPrevLevel($$)
811 my ($e,$fl) = @_;
812 my $prev = undef;
814 foreach my $l (@{$e->{LEVELS}}) {
815 (return $prev) if ($l == $fl);
816 $prev = $l;
819 return undef;
822 sub ContainsString($)
824 my ($e) = @_;
826 foreach my $l (@{$e->{LEVELS}}) {
827 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
830 return 0;
833 sub ContainsDeferred($$)
835 my ($e,$l) = @_;
837 return 1 if ($l->{CONTAINS_DEFERRED});
839 while ($l = GetNextLevel($e,$l))
841 return 1 if ($l->{IS_DEFERRED});
842 return 1 if ($l->{CONTAINS_DEFERRED});
845 return 0;
848 sub el_name($)
850 my $e = shift;
851 my $name = "<ANONYMOUS>";
853 $name = $e->{NAME} if defined($e->{NAME});
855 if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
856 return "$e->{PARENT}->{NAME}.$name";
859 if (defined($e->{PARENT}) and
860 defined($e->{PARENT}->{PARENT}) and
861 defined($e->{PARENT}->{PARENT}->{NAME})) {
862 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
865 return $name;
868 ###################################
869 # find a sibling var in a structure
870 sub find_sibling($$)
872 my($e,$name) = @_;
873 my($fn) = $e->{PARENT};
875 if ($name =~ /\*(.*)/) {
876 $name = $1;
879 for my $e2 (@{$fn->{ELEMENTS}}) {
880 return $e2 if ($e2->{NAME} eq $name);
883 return undef;
886 my %property_list = (
887 # interface
888 "helpstring" => ["INTERFACE", "FUNCTION"],
889 "version" => ["INTERFACE"],
890 "uuid" => ["INTERFACE"],
891 "endpoint" => ["INTERFACE"],
892 "pointer_default" => ["INTERFACE"],
893 "helper" => ["INTERFACE"],
894 "pyhelper" => ["INTERFACE"],
895 "authservice" => ["INTERFACE"],
896 "restricted" => ["INTERFACE"],
897 "no_srv_register" => ["INTERFACE"],
899 # dcom
900 "object" => ["INTERFACE"],
901 "local" => ["INTERFACE", "FUNCTION"],
902 "iid_is" => ["ELEMENT"],
903 "call_as" => ["FUNCTION"],
904 "idempotent" => ["FUNCTION"],
906 # function
907 "noopnum" => ["FUNCTION"],
908 "in" => ["ELEMENT"],
909 "out" => ["ELEMENT"],
911 # pointer
912 "ref" => ["ELEMENT", "TYPEDEF"],
913 "ptr" => ["ELEMENT", "TYPEDEF"],
914 "unique" => ["ELEMENT", "TYPEDEF"],
915 "ignore" => ["ELEMENT"],
916 "relative" => ["ELEMENT", "TYPEDEF"],
917 "relative_short" => ["ELEMENT", "TYPEDEF"],
918 "null_is_ffffffff" => ["ELEMENT"],
919 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
921 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
922 "value" => ["ELEMENT"],
923 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
925 # generic
926 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
927 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
928 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
929 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
930 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
931 "nopython" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
932 "todo" => ["FUNCTION"],
934 # union
935 "switch_is" => ["ELEMENT"],
936 "switch_type" => ["ELEMENT", "UNION"],
937 "nodiscriminant" => ["UNION"],
938 "case" => ["ELEMENT"],
939 "default" => ["ELEMENT"],
941 "represent_as" => ["ELEMENT"],
942 "transmit_as" => ["ELEMENT"],
944 # subcontext
945 "subcontext" => ["ELEMENT"],
946 "subcontext_size" => ["ELEMENT"],
947 "compression" => ["ELEMENT"],
949 # enum
950 "enum8bit" => ["ENUM"],
951 "enum16bit" => ["ENUM"],
952 "v1_enum" => ["ENUM"],
954 # bitmap
955 "bitmap8bit" => ["BITMAP"],
956 "bitmap16bit" => ["BITMAP"],
957 "bitmap32bit" => ["BITMAP"],
958 "bitmap64bit" => ["BITMAP"],
960 # array
961 "range" => ["ELEMENT", "PIPE"],
962 "size_is" => ["ELEMENT"],
963 "string" => ["ELEMENT"],
964 "noheader" => ["ELEMENT"],
965 "charset" => ["ELEMENT"],
966 "length_is" => ["ELEMENT"],
969 #####################################################################
970 # check for unknown properties
971 sub ValidProperties($$)
973 my ($e,$t) = @_;
975 return unless defined $e->{PROPERTIES};
977 foreach my $key (keys %{$e->{PROPERTIES}}) {
978 warning($e, el_name($e) . ": unknown property '$key'")
979 unless defined($property_list{$key});
981 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
982 unless grep(/^$t$/, @{$property_list{$key}});
986 sub mapToScalar($)
988 sub mapToScalar($);
989 my $t = shift;
990 return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
991 my $ti = getType($t);
993 if (not defined ($ti)) {
994 return undef;
995 } elsif ($ti->{TYPE} eq "TYPEDEF") {
996 return mapToScalar($ti->{DATA});
997 } elsif ($ti->{TYPE} eq "ENUM") {
998 return Parse::Pidl::Typelist::enum_type_fn($ti);
999 } elsif ($ti->{TYPE} eq "BITMAP") {
1000 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
1003 return undef;
1006 #####################################################################
1007 # validate an element
1008 sub ValidElement($)
1010 my $e = shift;
1012 ValidProperties($e,"ELEMENT");
1014 # Check whether switches are used correctly.
1015 if (my $switch = has_property($e, "switch_is")) {
1016 my $e2 = find_sibling($e, $switch);
1017 my $type = getType($e->{TYPE});
1019 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
1020 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1023 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
1024 my $discriminator_type = has_property($type->{DATA}, "switch_type");
1025 $discriminator_type = "uint32" unless defined ($discriminator_type);
1027 my $t1 = mapScalarType(mapToScalar($discriminator_type));
1029 if (not defined($t1)) {
1030 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1033 my $t2 = mapScalarType(mapToScalar($e2->{TYPE}));
1034 if (not defined($t2)) {
1035 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1038 if ($t1 ne $t2) {
1039 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1044 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1045 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1048 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1049 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1052 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1053 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1056 if (has_property($e, "represent_as") and has_property($e, "value")) {
1057 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1060 if (has_property($e, "subcontext")) {
1061 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1064 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1065 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1068 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1069 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1072 if (!$e->{POINTERS} && (
1073 has_property($e, "ptr") or
1074 has_property($e, "unique") or
1075 has_property($e, "relative") or
1076 has_property($e, "relative_short") or
1077 has_property($e, "ref"))) {
1078 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
1082 #####################################################################
1083 # validate an enum
1084 sub ValidEnum($)
1086 my ($enum) = @_;
1088 ValidProperties($enum, "ENUM");
1091 #####################################################################
1092 # validate a bitmap
1093 sub ValidBitmap($)
1095 my ($bitmap) = @_;
1097 ValidProperties($bitmap, "BITMAP");
1100 #####################################################################
1101 # validate a struct
1102 sub ValidStruct($)
1104 my($struct) = shift;
1106 ValidProperties($struct, "STRUCT");
1108 return unless defined($struct->{ELEMENTS});
1110 foreach my $e (@{$struct->{ELEMENTS}}) {
1111 $e->{PARENT} = $struct;
1112 ValidElement($e);
1116 #####################################################################
1117 # parse a union
1118 sub ValidUnion($)
1120 my($union) = shift;
1122 ValidProperties($union,"UNION");
1124 if (has_property($union->{PARENT}, "nodiscriminant") and
1125 has_property($union->{PARENT}, "switch_type")) {
1126 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1129 return unless defined($union->{ELEMENTS});
1131 foreach my $e (@{$union->{ELEMENTS}}) {
1132 $e->{PARENT} = $union;
1134 if (defined($e->{PROPERTIES}->{default}) and
1135 defined($e->{PROPERTIES}->{case})) {
1136 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1139 unless (defined ($e->{PROPERTIES}->{default}) or
1140 defined ($e->{PROPERTIES}->{case})) {
1141 fatal($e, "Union member $e->{NAME} must have default or case property");
1144 if (has_property($e, "ref")) {
1145 fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1149 ValidElement($e);
1153 #####################################################################
1154 # validate a pipe
1155 sub ValidPipe($)
1157 my ($pipe) = @_;
1158 my $data = $pipe->{DATA};
1160 ValidProperties($pipe, "PIPE");
1162 fatal($pipe, $pipe->{NAME} . ": 'pipe' is not yet supported by pidl");
1165 #####################################################################
1166 # parse a typedef
1167 sub ValidTypedef($)
1169 my($typedef) = shift;
1170 my $data = $typedef->{DATA};
1172 ValidProperties($typedef, "TYPEDEF");
1174 return unless (ref($data) eq "HASH");
1176 $data->{PARENT} = $typedef;
1178 $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1179 $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1181 ValidType($data);
1184 #####################################################################
1185 # validate a function
1186 sub ValidFunction($)
1188 my($fn) = shift;
1190 ValidProperties($fn,"FUNCTION");
1192 foreach my $e (@{$fn->{ELEMENTS}}) {
1193 $e->{PARENT} = $fn;
1194 if (has_property($e, "ref") && !$e->{POINTERS}) {
1195 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1197 ValidElement($e);
1201 #####################################################################
1202 # validate a type
1203 sub ValidType($)
1205 my ($t) = @_;
1208 TYPEDEF => \&ValidTypedef,
1209 STRUCT => \&ValidStruct,
1210 UNION => \&ValidUnion,
1211 ENUM => \&ValidEnum,
1212 BITMAP => \&ValidBitmap,
1213 PIPE => \&ValidPipe
1214 }->{$t->{TYPE}}->($t);
1217 #####################################################################
1218 # parse the interface definitions
1219 sub ValidInterface($)
1221 my($interface) = shift;
1222 my($data) = $interface->{DATA};
1224 if (has_property($interface, "helper")) {
1225 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1228 ValidProperties($interface,"INTERFACE");
1230 if (has_property($interface, "pointer_default")) {
1231 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1232 ("ref", "unique", "ptr"))) {
1233 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1237 if (has_property($interface, "object")) {
1238 if (has_property($interface, "version") &&
1239 $interface->{PROPERTIES}->{version} != 0) {
1240 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1243 if (!defined($interface->{BASE}) &&
1244 not ($interface->{NAME} eq "IUnknown")) {
1245 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1249 foreach my $d (@{$data}) {
1250 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1251 ($d->{TYPE} eq "TYPEDEF" or
1252 $d->{TYPE} eq "STRUCT" or
1253 $d->{TYPE} eq "UNION" or
1254 $d->{TYPE} eq "ENUM" or
1255 $d->{TYPE} eq "BITMAP" or
1256 $d->{TYPE} eq "PIPE") && ValidType($d);
1261 #####################################################################
1262 # Validate an IDL structure
1263 sub Validate($)
1265 my($idl) = shift;
1267 foreach my $x (@{$idl}) {
1268 ($x->{TYPE} eq "INTERFACE") &&
1269 ValidInterface($x);
1270 ($x->{TYPE} eq "IMPORTLIB") &&
1271 fatal($x, "importlib() not supported");
1275 sub is_charset_array($$)
1277 my ($e,$l) = @_;
1279 return 0 if ($l->{TYPE} ne "ARRAY");
1281 my $nl = GetNextLevel($e,$l);
1283 return 0 unless ($nl->{TYPE} eq "DATA");
1285 return has_property($e, "charset");