dnsp: Parse TXT records
[Samba/gebeck_regimport.git] / pidl / lib / Parse / Pidl / NDR.pm
blobb5dd2aaf9cdc969129f032d1285ccdb942d4ac6f
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 'nbt_string' => 4,
76 'wrepl_nbt_name' => 4,
77 'ipv4address' => 4,
78 'ipv6address' => 4, #16?
79 'dnsp_name' => 1,
80 'dnsp_string' => 1
83 sub GetElementLevelTable($$)
85 my ($e, $pointer_default) = @_;
87 my $order = [];
88 my $is_deferred = 0;
89 my @bracket_array = ();
90 my @length_is = ();
91 my @size_is = ();
92 my $pointer_idx = 0;
94 if (has_property($e, "size_is")) {
95 @size_is = split /,/, has_property($e, "size_is");
98 if (has_property($e, "length_is")) {
99 @length_is = split /,/, has_property($e, "length_is");
102 if (defined($e->{ARRAY_LEN})) {
103 @bracket_array = @{$e->{ARRAY_LEN}};
106 if (has_property($e, "out")) {
107 my $needptrs = 1;
109 if (has_property($e, "string") and not has_property($e, "in")) { $needptrs++; }
110 if ($#bracket_array >= 0) { $needptrs = 0; }
112 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
115 # Parse the [][][][] style array stuff
116 for my $i (0 .. $#bracket_array) {
117 my $d = $bracket_array[$#bracket_array - $i];
118 my $size = $d;
119 my $length = $d;
120 my $is_surrounding = 0;
121 my $is_varying = 0;
122 my $is_conformant = 0;
123 my $is_string = 0;
124 my $is_fixed = 0;
125 my $is_inline = 0;
127 if ($d eq "*") {
128 $is_conformant = 1;
129 if ($size = shift @size_is) {
130 if ($e->{POINTERS} < 1 and has_property($e, "string")) {
131 $is_string = 1;
132 delete($e->{PROPERTIES}->{string});
134 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
135 $is_string = 1;
136 delete($e->{PROPERTIES}->{string});
137 } else {
138 fatal($e, "Must specify size_is() for conformant array!")
141 if (($length = shift @length_is) or $is_string) {
142 $is_varying = 1;
143 } else {
144 $length = $size;
147 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
148 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
149 $is_surrounding = 1;
153 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
154 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
156 if ($i == 0 and $is_fixed and has_property($e, "string")) {
157 $is_fixed = 0;
158 $is_varying = 1;
159 $is_string = 1;
160 delete($e->{PROPERTIES}->{string});
163 push (@$order, {
164 TYPE => "ARRAY",
165 SIZE_IS => $size,
166 LENGTH_IS => $length,
167 IS_DEFERRED => $is_deferred,
168 IS_SURROUNDING => $is_surrounding,
169 IS_ZERO_TERMINATED => $is_string,
170 IS_VARYING => $is_varying,
171 IS_CONFORMANT => $is_conformant,
172 IS_FIXED => $is_fixed,
173 IS_INLINE => $is_inline
177 # Next, all the pointers
178 foreach my $i (1..$e->{POINTERS}) {
179 my $level = "EMBEDDED";
180 # Top level "ref" pointers do not have a referrent identifier
181 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
183 my $pt;
185 # Only the first level gets the pointer type from the
186 # pointer property, the others get them from
187 # the pointer_default() interface property
189 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
190 # (Here they talk about the rightmost pointer, but testing shows
191 # they mean the leftmost pointer.)
193 # --metze
195 $pt = pointer_type($e);
196 if ($i > 1) {
197 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
198 $pt = $pointer_default;
201 push (@$order, {
202 TYPE => "POINTER",
203 POINTER_TYPE => $pt,
204 POINTER_INDEX => $pointer_idx,
205 IS_DEFERRED => "$is_deferred",
206 LEVEL => $level
209 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
210 if ($i == 1 and $pt ne "ref" and
211 $e->{PARENT}->{TYPE} eq "FUNCTION" and
212 not has_property($e, "in"));
214 $pointer_idx++;
216 # everything that follows will be deferred
217 $is_deferred = 1 if ($level ne "TOP");
219 my $array_size = shift @size_is;
220 my $array_length;
221 my $is_varying;
222 my $is_conformant;
223 my $is_string = 0;
224 if ($array_size) {
225 $is_conformant = 1;
226 if ($array_length = shift @length_is) {
227 $is_varying = 1;
228 } else {
229 $array_length = $array_size;
230 $is_varying =0;
234 if (scalar(@size_is) == 0 and has_property($e, "string") and
235 $i == $e->{POINTERS}) {
236 $is_string = 1;
237 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
238 delete($e->{PROPERTIES}->{string});
241 if ($array_size or $is_string) {
242 push (@$order, {
243 TYPE => "ARRAY",
244 SIZE_IS => $array_size,
245 LENGTH_IS => $array_length,
246 IS_DEFERRED => $is_deferred,
247 IS_SURROUNDING => 0,
248 IS_ZERO_TERMINATED => $is_string,
249 IS_VARYING => $is_varying,
250 IS_CONFORMANT => $is_conformant,
251 IS_FIXED => 0,
252 IS_INLINE => 0
255 $is_deferred = 0;
259 if (defined(has_property($e, "subcontext"))) {
260 my $hdr_size = has_property($e, "subcontext");
261 my $subsize = has_property($e, "subcontext_size");
262 if (not defined($subsize)) {
263 $subsize = -1;
266 push (@$order, {
267 TYPE => "SUBCONTEXT",
268 HEADER_SIZE => $hdr_size,
269 SUBCONTEXT_SIZE => $subsize,
270 IS_DEFERRED => $is_deferred,
271 COMPRESSION => has_property($e, "compression"),
275 if (my $switch = has_property($e, "switch_is")) {
276 push (@$order, {
277 TYPE => "SWITCH",
278 SWITCH_IS => $switch,
279 IS_DEFERRED => $is_deferred
283 if (scalar(@size_is) > 0) {
284 fatal($e, "size_is() on non-array element");
287 if (scalar(@length_is) > 0) {
288 fatal($e, "length_is() on non-array element");
291 if (has_property($e, "string")) {
292 fatal($e, "string() attribute on non-array element");
295 push (@$order, {
296 TYPE => "DATA",
297 DATA_TYPE => $e->{TYPE},
298 IS_DEFERRED => $is_deferred,
299 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
300 IS_SURROUNDING => 0 #FIXME
303 my $i = 0;
304 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
306 return $order;
309 sub GetTypedefLevelTable($$$)
311 my ($e, $data, $pointer_default) = @_;
313 my $order = [];
315 push (@$order, {
316 TYPE => "TYPEDEF"
319 my $i = 0;
320 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
322 return $order;
325 #####################################################################
326 # see if a type contains any deferred data
327 sub can_contain_deferred($)
329 sub can_contain_deferred($);
330 my ($type) = @_;
332 return 1 unless (hasType($type)); # assume the worst
334 $type = getType($type);
336 return 0 if (Parse::Pidl::Typelist::is_scalar($type));
338 return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
340 return 0 unless defined($type->{ELEMENTS});
342 foreach (@{$type->{ELEMENTS}}) {
343 return 1 if ($_->{POINTERS});
344 return 1 if (can_contain_deferred ($_->{TYPE}));
347 return 0;
350 sub pointer_type($)
352 my $e = shift;
354 return undef unless $e->{POINTERS};
356 return "ref" if (has_property($e, "ref"));
357 return "full" if (has_property($e, "ptr"));
358 return "sptr" if (has_property($e, "sptr"));
359 return "unique" if (has_property($e, "unique"));
360 return "relative" if (has_property($e, "relative"));
361 return "relative_short" if (has_property($e, "relative_short"));
362 return "ignore" if (has_property($e, "ignore"));
364 return undef;
367 #####################################################################
368 # work out the correct alignment for a structure or union
369 sub find_largest_alignment($)
371 my $s = shift;
373 my $align = 1;
374 for my $e (@{$s->{ELEMENTS}}) {
375 my $a = 1;
377 if ($e->{POINTERS}) {
378 # this is a hack for NDR64
379 # the NDR layer translates this into
380 # an alignment of 4 for NDR and 8 for NDR64
381 $a = 5;
382 } elsif (has_property($e, "subcontext")) {
383 $a = 1;
384 } elsif (has_property($e, "transmit_as")) {
385 $a = align_type($e->{PROPERTIES}->{transmit_as});
386 } else {
387 $a = align_type($e->{TYPE});
390 $align = $a if ($align < $a);
393 return $align;
396 #####################################################################
397 # align a type
398 sub align_type($)
400 sub align_type($);
401 my ($e) = @_;
403 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
404 return $scalar_alignment->{$e->{NAME}};
407 return 0 if ($e eq "EMPTY");
409 unless (hasType($e)) {
410 # it must be an external type - all we can do is guess
411 # warning($e, "assuming alignment of unknown type '$e' is 4");
412 return 4;
415 my $dt = getType($e);
417 if ($dt->{TYPE} eq "TYPEDEF") {
418 return align_type($dt->{DATA});
419 } elsif ($dt->{TYPE} eq "CONFORMANCE") {
420 return $dt->{DATA}->{ALIGN};
421 } elsif ($dt->{TYPE} eq "ENUM") {
422 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
423 } elsif ($dt->{TYPE} eq "BITMAP") {
424 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
425 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
426 # Struct/union without body: assume 4
427 return 4 unless (defined($dt->{ELEMENTS}));
428 return find_largest_alignment($dt);
431 die("Unknown data type type $dt->{TYPE}");
434 sub ParseElement($$)
436 my ($e, $pointer_default) = @_;
438 $e->{TYPE} = expandAlias($e->{TYPE});
440 if (ref($e->{TYPE}) eq "HASH") {
441 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default);
444 return {
445 NAME => $e->{NAME},
446 TYPE => $e->{TYPE},
447 PROPERTIES => $e->{PROPERTIES},
448 LEVELS => GetElementLevelTable($e, $pointer_default),
449 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
450 ALIGN => align_type($e->{TYPE}),
451 ORIGINAL => $e
455 sub ParseStruct($$)
457 my ($struct, $pointer_default) = @_;
458 my @elements = ();
459 my $surrounding = undef;
461 return {
462 TYPE => "STRUCT",
463 NAME => $struct->{NAME},
464 SURROUNDING_ELEMENT => undef,
465 ELEMENTS => undef,
466 PROPERTIES => $struct->{PROPERTIES},
467 ORIGINAL => $struct,
468 ALIGN => undef
469 } unless defined($struct->{ELEMENTS});
471 CheckPointerTypes($struct, $pointer_default);
473 foreach my $x (@{$struct->{ELEMENTS}})
475 my $e = ParseElement($x, $pointer_default);
476 if ($x != $struct->{ELEMENTS}[-1] and
477 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
478 fatal($x, "conformant member not at end of struct");
480 push @elements, $e;
483 my $e = $elements[-1];
484 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
485 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
486 $surrounding = $e;
489 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
490 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
491 $surrounding = $struct->{ELEMENTS}[-1];
494 my $align = undef;
495 if ($struct->{NAME}) {
496 $align = align_type($struct->{NAME});
499 return {
500 TYPE => "STRUCT",
501 NAME => $struct->{NAME},
502 SURROUNDING_ELEMENT => $surrounding,
503 ELEMENTS => \@elements,
504 PROPERTIES => $struct->{PROPERTIES},
505 ORIGINAL => $struct,
506 ALIGN => $align
510 sub ParseUnion($$)
512 my ($e, $pointer_default) = @_;
513 my @elements = ();
514 my $hasdefault = 0;
515 my $switch_type = has_property($e, "switch_type");
516 unless (defined($switch_type)) { $switch_type = "uint32"; }
517 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
519 return {
520 TYPE => "UNION",
521 NAME => $e->{NAME},
522 SWITCH_TYPE => $switch_type,
523 ELEMENTS => undef,
524 PROPERTIES => $e->{PROPERTIES},
525 HAS_DEFAULT => $hasdefault,
526 ORIGINAL => $e,
527 ALIGN => undef
528 } unless defined($e->{ELEMENTS});
530 CheckPointerTypes($e, $pointer_default);
532 foreach my $x (@{$e->{ELEMENTS}})
534 my $t;
535 if ($x->{TYPE} eq "EMPTY") {
536 $t = { TYPE => "EMPTY" };
537 } else {
538 $t = ParseElement($x, $pointer_default);
540 if (has_property($x, "default")) {
541 $t->{CASE} = "default";
542 $hasdefault = 1;
543 } elsif (defined($x->{PROPERTIES}->{case})) {
544 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
545 } else {
546 die("Union element $x->{NAME} has neither default nor case property");
548 push @elements, $t;
551 my $align = undef;
552 if ($e->{NAME}) {
553 $align = align_type($e->{NAME});
556 return {
557 TYPE => "UNION",
558 NAME => $e->{NAME},
559 SWITCH_TYPE => $switch_type,
560 ELEMENTS => \@elements,
561 PROPERTIES => $e->{PROPERTIES},
562 HAS_DEFAULT => $hasdefault,
563 ORIGINAL => $e,
564 ALIGN => $align
568 sub ParseEnum($$)
570 my ($e, $pointer_default) = @_;
572 return {
573 TYPE => "ENUM",
574 NAME => $e->{NAME},
575 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
576 ELEMENTS => $e->{ELEMENTS},
577 PROPERTIES => $e->{PROPERTIES},
578 ORIGINAL => $e
582 sub ParseBitmap($$)
584 my ($e, $pointer_default) = @_;
586 return {
587 TYPE => "BITMAP",
588 NAME => $e->{NAME},
589 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
590 ELEMENTS => $e->{ELEMENTS},
591 PROPERTIES => $e->{PROPERTIES},
592 ORIGINAL => $e
596 sub ParseType($$)
598 my ($d, $pointer_default) = @_;
600 my $data = {
601 STRUCT => \&ParseStruct,
602 UNION => \&ParseUnion,
603 ENUM => \&ParseEnum,
604 BITMAP => \&ParseBitmap,
605 TYPEDEF => \&ParseTypedef,
606 }->{$d->{TYPE}}->($d, $pointer_default);
608 return $data;
611 sub ParseTypedef($$)
613 my ($d, $pointer_default) = @_;
615 my $data;
617 if (ref($d->{DATA}) eq "HASH") {
618 if (defined($d->{DATA}->{PROPERTIES})
619 and not defined($d->{PROPERTIES})) {
620 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
623 $data = ParseType($d->{DATA}, $pointer_default);
624 $data->{ALIGN} = align_type($d->{NAME});
625 } else {
626 $data = getType($d->{DATA});
629 return {
630 NAME => $d->{NAME},
631 TYPE => $d->{TYPE},
632 PROPERTIES => $d->{PROPERTIES},
633 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default),
634 DATA => $data,
635 ORIGINAL => $d
639 sub ParseConst($$)
641 my ($ndr,$d) = @_;
643 return $d;
646 sub ParseFunction($$$)
648 my ($ndr,$d,$opnum) = @_;
649 my @elements = ();
650 my $rettype = undef;
651 my $thisopnum = undef;
653 CheckPointerTypes($d, "ref");
655 if (not defined($d->{PROPERTIES}{noopnum})) {
656 $thisopnum = ${$opnum};
657 ${$opnum}++;
660 foreach my $x (@{$d->{ELEMENTS}}) {
661 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default});
662 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
663 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
665 push (@elements, $e);
668 if ($d->{RETURN_TYPE} ne "void") {
669 $rettype = expandAlias($d->{RETURN_TYPE});
672 return {
673 NAME => $d->{NAME},
674 TYPE => "FUNCTION",
675 OPNUM => $thisopnum,
676 RETURN_TYPE => $rettype,
677 PROPERTIES => $d->{PROPERTIES},
678 ELEMENTS => \@elements,
679 ORIGINAL => $d
683 sub CheckPointerTypes($$)
685 my ($s,$default) = @_;
687 return unless defined($s->{ELEMENTS});
689 foreach my $e (@{$s->{ELEMENTS}}) {
690 if ($e->{POINTERS} and not defined(pointer_type($e))) {
691 $e->{PROPERTIES}->{$default} = '1';
696 sub FindNestedTypes($$)
698 sub FindNestedTypes($$);
699 my ($l, $t) = @_;
701 return unless defined($t->{ELEMENTS});
702 return if ($t->{TYPE} eq "ENUM");
703 return if ($t->{TYPE} eq "BITMAP");
705 foreach (@{$t->{ELEMENTS}}) {
706 if (ref($_->{TYPE}) eq "HASH") {
707 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
708 FindNestedTypes($l, $_->{TYPE});
713 sub ParseInterface($)
715 my $idl = shift;
716 my @types = ();
717 my @consts = ();
718 my @functions = ();
719 my @endpoints;
720 my $opnum = 0;
721 my $version;
723 if (not has_property($idl, "pointer_default")) {
724 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
725 # and "unique" in Microsoft Extensions mode (default)
726 $idl->{PROPERTIES}->{pointer_default} = "unique";
729 foreach my $d (@{$idl->{DATA}}) {
730 if ($d->{TYPE} eq "FUNCTION") {
731 push (@functions, ParseFunction($idl, $d, \$opnum));
732 } elsif ($d->{TYPE} eq "CONST") {
733 push (@consts, ParseConst($idl, $d));
734 } else {
735 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}));
736 FindNestedTypes(\@types, $d);
740 $version = "0.0";
742 if(defined $idl->{PROPERTIES}->{version}) {
743 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
744 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
745 $version = $idl->{PROPERTIES}->{version};
746 } else {
747 $version = $if_version[1] << 16 | $if_version[0];
751 # If no endpoint is set, default to the interface name as a named pipe
752 if (!defined $idl->{PROPERTIES}->{endpoint}) {
753 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
754 } else {
755 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
758 return {
759 NAME => $idl->{NAME},
760 UUID => lc(has_property($idl, "uuid")),
761 VERSION => $version,
762 TYPE => "INTERFACE",
763 PROPERTIES => $idl->{PROPERTIES},
764 FUNCTIONS => \@functions,
765 CONSTS => \@consts,
766 TYPES => \@types,
767 ENDPOINTS => \@endpoints
771 # Convert a IDL tree to a NDR tree
772 # Gives a result tree describing all that's necessary for easily generating
773 # NDR parsers / generators
774 sub Parse($)
776 my $idl = shift;
778 return undef unless (defined($idl));
780 Parse::Pidl::NDR::Validate($idl);
782 my @ndr = ();
784 foreach (@{$idl}) {
785 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
786 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
787 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
790 return \@ndr;
793 sub GetNextLevel($$)
795 my $e = shift;
796 my $fl = shift;
798 my $seen = 0;
800 foreach my $l (@{$e->{LEVELS}}) {
801 return $l if ($seen);
802 ($seen = 1) if ($l == $fl);
805 return undef;
808 sub GetPrevLevel($$)
810 my ($e,$fl) = @_;
811 my $prev = undef;
813 foreach my $l (@{$e->{LEVELS}}) {
814 (return $prev) if ($l == $fl);
815 $prev = $l;
818 return undef;
821 sub ContainsString($)
823 my ($e) = @_;
825 foreach my $l (@{$e->{LEVELS}}) {
826 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
829 return 0;
832 sub ContainsDeferred($$)
834 my ($e,$l) = @_;
836 return 1 if ($l->{CONTAINS_DEFERRED});
838 while ($l = GetNextLevel($e,$l))
840 return 1 if ($l->{IS_DEFERRED});
841 return 1 if ($l->{CONTAINS_DEFERRED});
844 return 0;
847 sub el_name($)
849 my $e = shift;
850 my $name = "<ANONYMOUS>";
852 $name = $e->{NAME} if defined($e->{NAME});
854 if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
855 return "$e->{PARENT}->{NAME}.$name";
858 if (defined($e->{PARENT}) and
859 defined($e->{PARENT}->{PARENT}) and
860 defined($e->{PARENT}->{PARENT}->{NAME})) {
861 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
864 return $name;
867 ###################################
868 # find a sibling var in a structure
869 sub find_sibling($$)
871 my($e,$name) = @_;
872 my($fn) = $e->{PARENT};
874 if ($name =~ /\*(.*)/) {
875 $name = $1;
878 for my $e2 (@{$fn->{ELEMENTS}}) {
879 return $e2 if ($e2->{NAME} eq $name);
882 return undef;
885 my %property_list = (
886 # interface
887 "helpstring" => ["INTERFACE", "FUNCTION"],
888 "version" => ["INTERFACE"],
889 "uuid" => ["INTERFACE"],
890 "endpoint" => ["INTERFACE"],
891 "pointer_default" => ["INTERFACE"],
892 "helper" => ["INTERFACE"],
893 "pyhelper" => ["INTERFACE"],
894 "authservice" => ["INTERFACE"],
895 "restricted" => ["INTERFACE"],
896 "no_srv_register" => ["INTERFACE"],
898 # dcom
899 "object" => ["INTERFACE"],
900 "local" => ["INTERFACE", "FUNCTION"],
901 "iid_is" => ["ELEMENT"],
902 "call_as" => ["FUNCTION"],
903 "idempotent" => ["FUNCTION"],
905 # function
906 "noopnum" => ["FUNCTION"],
907 "in" => ["ELEMENT"],
908 "out" => ["ELEMENT"],
910 # pointer
911 "ref" => ["ELEMENT", "TYPEDEF"],
912 "ptr" => ["ELEMENT", "TYPEDEF"],
913 "unique" => ["ELEMENT", "TYPEDEF"],
914 "ignore" => ["ELEMENT"],
915 "relative" => ["ELEMENT", "TYPEDEF"],
916 "relative_short" => ["ELEMENT", "TYPEDEF"],
917 "null_is_ffffffff" => ["ELEMENT"],
918 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
920 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
921 "value" => ["ELEMENT"],
922 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
924 # generic
925 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
926 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
927 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
928 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
929 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
930 "nopython" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
931 "todo" => ["FUNCTION"],
933 # union
934 "switch_is" => ["ELEMENT"],
935 "switch_type" => ["ELEMENT", "UNION"],
936 "nodiscriminant" => ["UNION"],
937 "case" => ["ELEMENT"],
938 "default" => ["ELEMENT"],
940 "represent_as" => ["ELEMENT"],
941 "transmit_as" => ["ELEMENT"],
943 # subcontext
944 "subcontext" => ["ELEMENT"],
945 "subcontext_size" => ["ELEMENT"],
946 "compression" => ["ELEMENT"],
948 # enum
949 "enum8bit" => ["ENUM"],
950 "enum16bit" => ["ENUM"],
951 "v1_enum" => ["ENUM"],
953 # bitmap
954 "bitmap8bit" => ["BITMAP"],
955 "bitmap16bit" => ["BITMAP"],
956 "bitmap32bit" => ["BITMAP"],
957 "bitmap64bit" => ["BITMAP"],
959 # array
960 "range" => ["ELEMENT", "PIPE"],
961 "size_is" => ["ELEMENT"],
962 "string" => ["ELEMENT"],
963 "noheader" => ["ELEMENT"],
964 "charset" => ["ELEMENT"],
965 "length_is" => ["ELEMENT"],
968 #####################################################################
969 # check for unknown properties
970 sub ValidProperties($$)
972 my ($e,$t) = @_;
974 return unless defined $e->{PROPERTIES};
976 foreach my $key (keys %{$e->{PROPERTIES}}) {
977 warning($e, el_name($e) . ": unknown property '$key'")
978 unless defined($property_list{$key});
980 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
981 unless grep(/^$t$/, @{$property_list{$key}});
985 sub mapToScalar($)
987 sub mapToScalar($);
988 my $t = shift;
989 return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
990 my $ti = getType($t);
992 if (not defined ($ti)) {
993 return undef;
994 } elsif ($ti->{TYPE} eq "TYPEDEF") {
995 return mapToScalar($ti->{DATA});
996 } elsif ($ti->{TYPE} eq "ENUM") {
997 return Parse::Pidl::Typelist::enum_type_fn($ti);
998 } elsif ($ti->{TYPE} eq "BITMAP") {
999 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
1002 return undef;
1005 #####################################################################
1006 # validate an element
1007 sub ValidElement($)
1009 my $e = shift;
1011 ValidProperties($e,"ELEMENT");
1013 # Check whether switches are used correctly.
1014 if (my $switch = has_property($e, "switch_is")) {
1015 my $e2 = find_sibling($e, $switch);
1016 my $type = getType($e->{TYPE});
1018 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
1019 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1022 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
1023 my $discriminator_type = has_property($type->{DATA}, "switch_type");
1024 $discriminator_type = "uint32" unless defined ($discriminator_type);
1026 my $t1 = mapScalarType(mapToScalar($discriminator_type));
1028 if (not defined($t1)) {
1029 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1032 my $t2 = mapScalarType(mapToScalar($e2->{TYPE}));
1033 if (not defined($t2)) {
1034 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1037 if ($t1 ne $t2) {
1038 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1043 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1044 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1047 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1048 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1051 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1052 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1055 if (has_property($e, "represent_as") and has_property($e, "value")) {
1056 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1059 if (has_property($e, "subcontext")) {
1060 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1063 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1064 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1067 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1068 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1071 if (!$e->{POINTERS} && (
1072 has_property($e, "ptr") or
1073 has_property($e, "unique") or
1074 has_property($e, "relative") or
1075 has_property($e, "relative_short") or
1076 has_property($e, "ref"))) {
1077 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
1081 #####################################################################
1082 # validate an enum
1083 sub ValidEnum($)
1085 my ($enum) = @_;
1087 ValidProperties($enum, "ENUM");
1090 #####################################################################
1091 # validate a bitmap
1092 sub ValidBitmap($)
1094 my ($bitmap) = @_;
1096 ValidProperties($bitmap, "BITMAP");
1099 #####################################################################
1100 # validate a struct
1101 sub ValidStruct($)
1103 my($struct) = shift;
1105 ValidProperties($struct, "STRUCT");
1107 return unless defined($struct->{ELEMENTS});
1109 foreach my $e (@{$struct->{ELEMENTS}}) {
1110 $e->{PARENT} = $struct;
1111 ValidElement($e);
1115 #####################################################################
1116 # parse a union
1117 sub ValidUnion($)
1119 my($union) = shift;
1121 ValidProperties($union,"UNION");
1123 if (has_property($union->{PARENT}, "nodiscriminant") and
1124 has_property($union->{PARENT}, "switch_type")) {
1125 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1128 return unless defined($union->{ELEMENTS});
1130 foreach my $e (@{$union->{ELEMENTS}}) {
1131 $e->{PARENT} = $union;
1133 if (defined($e->{PROPERTIES}->{default}) and
1134 defined($e->{PROPERTIES}->{case})) {
1135 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1138 unless (defined ($e->{PROPERTIES}->{default}) or
1139 defined ($e->{PROPERTIES}->{case})) {
1140 fatal($e, "Union member $e->{NAME} must have default or case property");
1143 if (has_property($e, "ref")) {
1144 fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1148 ValidElement($e);
1152 #####################################################################
1153 # validate a pipe
1154 sub ValidPipe($)
1156 my ($pipe) = @_;
1157 my $data = $pipe->{DATA};
1159 ValidProperties($pipe, "PIPE");
1161 fatal($pipe, $pipe->{NAME} . ": 'pipe' is not yet supported by pidl");
1164 #####################################################################
1165 # parse a typedef
1166 sub ValidTypedef($)
1168 my($typedef) = shift;
1169 my $data = $typedef->{DATA};
1171 ValidProperties($typedef, "TYPEDEF");
1173 return unless (ref($data) eq "HASH");
1175 $data->{PARENT} = $typedef;
1177 $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1178 $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1180 ValidType($data);
1183 #####################################################################
1184 # validate a function
1185 sub ValidFunction($)
1187 my($fn) = shift;
1189 ValidProperties($fn,"FUNCTION");
1191 foreach my $e (@{$fn->{ELEMENTS}}) {
1192 $e->{PARENT} = $fn;
1193 if (has_property($e, "ref") && !$e->{POINTERS}) {
1194 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1196 ValidElement($e);
1200 #####################################################################
1201 # validate a type
1202 sub ValidType($)
1204 my ($t) = @_;
1207 TYPEDEF => \&ValidTypedef,
1208 STRUCT => \&ValidStruct,
1209 UNION => \&ValidUnion,
1210 ENUM => \&ValidEnum,
1211 BITMAP => \&ValidBitmap,
1212 PIPE => \&ValidPipe
1213 }->{$t->{TYPE}}->($t);
1216 #####################################################################
1217 # parse the interface definitions
1218 sub ValidInterface($)
1220 my($interface) = shift;
1221 my($data) = $interface->{DATA};
1223 if (has_property($interface, "helper")) {
1224 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1227 ValidProperties($interface,"INTERFACE");
1229 if (has_property($interface, "pointer_default")) {
1230 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1231 ("ref", "unique", "ptr"))) {
1232 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1236 if (has_property($interface, "object")) {
1237 if (has_property($interface, "version") &&
1238 $interface->{PROPERTIES}->{version} != 0) {
1239 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1242 if (!defined($interface->{BASE}) &&
1243 not ($interface->{NAME} eq "IUnknown")) {
1244 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1248 foreach my $d (@{$data}) {
1249 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1250 ($d->{TYPE} eq "TYPEDEF" or
1251 $d->{TYPE} eq "STRUCT" or
1252 $d->{TYPE} eq "UNION" or
1253 $d->{TYPE} eq "ENUM" or
1254 $d->{TYPE} eq "BITMAP" or
1255 $d->{TYPE} eq "PIPE") && ValidType($d);
1260 #####################################################################
1261 # Validate an IDL structure
1262 sub Validate($)
1264 my($idl) = shift;
1266 foreach my $x (@{$idl}) {
1267 ($x->{TYPE} eq "INTERFACE") &&
1268 ValidInterface($x);
1269 ($x->{TYPE} eq "IMPORTLIB") &&
1270 fatal($x, "importlib() not supported");
1274 sub is_charset_array($$)
1276 my ($e,$l) = @_;
1278 return 0 if ($l->{TYPE} ne "ARRAY");
1280 my $nl = GetNextLevel($e,$l);
1282 return 0 unless ($nl->{TYPE} eq "DATA");
1284 return has_property($e, "charset");