pidl/NDR: correctly check for valid properties
[Samba/ekacnet.git] / source4 / pidl / lib / Parse / Pidl / NDR.pm
blob25743f512fcd282d370067c29859103100079dda
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);
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 'int32' => 4,
54 'uint32' => 4,
55 'hyper' => 8,
56 'pointer' => 8,
57 'dlong' => 4,
58 'udlong' => 4,
59 'udlongr' => 4,
60 'DATA_BLOB' => 4,
61 'string' => 4,
62 'string_array' => 4, #???
63 'time_t' => 4,
64 'NTTIME' => 4,
65 'NTTIME_1sec' => 4,
66 'NTTIME_hyper' => 8,
67 'WERROR' => 4,
68 'NTSTATUS' => 4,
69 'COMRESULT' => 4,
70 'nbt_string' => 4,
71 'wrepl_nbt_name' => 4,
72 'ipv4address' => 4
75 sub GetElementLevelTable($$)
77 my ($e, $pointer_default) = @_;
79 my $order = [];
80 my $is_deferred = 0;
81 my @bracket_array = ();
82 my @length_is = ();
83 my @size_is = ();
84 my $pointer_idx = 0;
86 if (has_property($e, "size_is")) {
87 @size_is = split /,/, has_property($e, "size_is");
90 if (has_property($e, "length_is")) {
91 @length_is = split /,/, has_property($e, "length_is");
94 if (defined($e->{ARRAY_LEN})) {
95 @bracket_array = @{$e->{ARRAY_LEN}};
98 if (has_property($e, "out")) {
99 my $needptrs = 1;
101 if (has_property($e, "string")) { $needptrs++; }
102 if ($#bracket_array >= 0) { $needptrs = 0; }
104 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
107 # Parse the [][][][] style array stuff
108 for my $i (0 .. $#bracket_array) {
109 my $d = $bracket_array[$#bracket_array - $i];
110 my $size = $d;
111 my $length = $d;
112 my $is_surrounding = 0;
113 my $is_varying = 0;
114 my $is_conformant = 0;
115 my $is_string = 0;
116 my $is_fixed = 0;
117 my $is_inline = 0;
119 if ($d eq "*") {
120 $is_conformant = 1;
121 if ($size = shift @size_is) {
122 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
123 $is_string = 1;
124 delete($e->{PROPERTIES}->{string});
125 } else {
126 fatal($e, "Must specify size_is() for conformant array!")
129 if (($length = shift @length_is) or $is_string) {
130 $is_varying = 1;
131 } else {
132 $length = $size;
135 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
136 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
137 $is_surrounding = 1;
141 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
142 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
144 push (@$order, {
145 TYPE => "ARRAY",
146 SIZE_IS => $size,
147 LENGTH_IS => $length,
148 IS_DEFERRED => $is_deferred,
149 IS_SURROUNDING => $is_surrounding,
150 IS_ZERO_TERMINATED => $is_string,
151 IS_VARYING => $is_varying,
152 IS_CONFORMANT => $is_conformant,
153 IS_FIXED => $is_fixed,
154 IS_INLINE => $is_inline
158 # Next, all the pointers
159 foreach my $i (1..$e->{POINTERS}) {
160 my $level = "EMBEDDED";
161 # Top level "ref" pointers do not have a referrent identifier
162 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
164 my $pt;
166 # Only the first level gets the pointer type from the
167 # pointer property, the others get them from
168 # the pointer_default() interface property
170 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
171 # (Here they talk about the rightmost pointer, but testing shows
172 # they mean the leftmost pointer.)
174 # --metze
176 $pt = pointer_type($e);
177 if ($i > 1) {
178 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
179 $pt = $pointer_default;
182 push (@$order, {
183 TYPE => "POINTER",
184 POINTER_TYPE => $pt,
185 POINTER_INDEX => $pointer_idx,
186 IS_DEFERRED => "$is_deferred",
187 LEVEL => $level
190 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
191 if ($i == 1 and $pt ne "ref" and
192 $e->{PARENT}->{TYPE} eq "FUNCTION" and
193 not has_property($e, "in"));
195 $pointer_idx++;
197 # everything that follows will be deferred
198 $is_deferred = 1 if ($level ne "TOP");
200 my $array_size = shift @size_is;
201 my $array_length;
202 my $is_varying;
203 my $is_conformant;
204 my $is_string = 0;
205 if ($array_size) {
206 $is_conformant = 1;
207 if ($array_length = shift @length_is) {
208 $is_varying = 1;
209 } else {
210 $array_length = $array_size;
211 $is_varying =0;
215 if (scalar(@size_is) == 0 and has_property($e, "string") and
216 $i == $e->{POINTERS}) {
217 $is_string = 1;
218 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
219 delete($e->{PROPERTIES}->{string});
222 if ($array_size or $is_string) {
223 push (@$order, {
224 TYPE => "ARRAY",
225 SIZE_IS => $array_size,
226 LENGTH_IS => $array_length,
227 IS_DEFERRED => $is_deferred,
228 IS_SURROUNDING => 0,
229 IS_ZERO_TERMINATED => $is_string,
230 IS_VARYING => $is_varying,
231 IS_CONFORMANT => $is_conformant,
232 IS_FIXED => 0,
233 IS_INLINE => 0
236 $is_deferred = 0;
240 if (defined(has_property($e, "subcontext"))) {
241 my $hdr_size = has_property($e, "subcontext");
242 my $subsize = has_property($e, "subcontext_size");
243 if (not defined($subsize)) {
244 $subsize = -1;
247 push (@$order, {
248 TYPE => "SUBCONTEXT",
249 HEADER_SIZE => $hdr_size,
250 SUBCONTEXT_SIZE => $subsize,
251 IS_DEFERRED => $is_deferred,
252 COMPRESSION => has_property($e, "compression"),
256 if (my $switch = has_property($e, "switch_is")) {
257 push (@$order, {
258 TYPE => "SWITCH",
259 SWITCH_IS => $switch,
260 IS_DEFERRED => $is_deferred
264 if (scalar(@size_is) > 0) {
265 fatal($e, "size_is() on non-array element");
268 if (scalar(@length_is) > 0) {
269 fatal($e, "length_is() on non-array element");
272 if (has_property($e, "string")) {
273 fatal($e, "string() attribute on non-array element");
276 push (@$order, {
277 TYPE => "DATA",
278 DATA_TYPE => $e->{TYPE},
279 IS_DEFERRED => $is_deferred,
280 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
281 IS_SURROUNDING => 0 #FIXME
284 my $i = 0;
285 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
287 return $order;
290 #####################################################################
291 # see if a type contains any deferred data
292 sub can_contain_deferred($)
294 sub can_contain_deferred($);
295 my ($type) = @_;
297 return 1 unless (hasType($type)); # assume the worst
299 $type = getType($type);
301 return 0 if (Parse::Pidl::Typelist::is_scalar($type));
303 return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
305 return 0 unless defined($type->{ELEMENTS});
307 foreach (@{$type->{ELEMENTS}}) {
308 return 1 if ($_->{POINTERS});
309 return 1 if (can_contain_deferred ($_->{TYPE}));
312 return 0;
315 sub pointer_type($)
317 my $e = shift;
319 return undef unless $e->{POINTERS};
321 return "ref" if (has_property($e, "ref"));
322 return "full" if (has_property($e, "ptr"));
323 return "sptr" if (has_property($e, "sptr"));
324 return "unique" if (has_property($e, "unique"));
325 return "relative" if (has_property($e, "relative"));
326 return "ignore" if (has_property($e, "ignore"));
328 return undef;
331 #####################################################################
332 # work out the correct alignment for a structure or union
333 sub find_largest_alignment($)
335 my $s = shift;
337 my $align = 1;
338 for my $e (@{$s->{ELEMENTS}}) {
339 my $a = 1;
341 if ($e->{POINTERS}) {
342 $a = 4;
343 } elsif (has_property($e, "subcontext")) {
344 $a = 1;
345 } elsif (has_property($e, "transmit_as")) {
346 $a = align_type($e->{PROPERTIES}->{transmit_as});
347 } else {
348 $a = align_type($e->{TYPE});
351 $align = $a if ($align < $a);
354 return $align;
357 #####################################################################
358 # align a type
359 sub align_type($)
361 sub align_type($);
362 my ($e) = @_;
364 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
365 return $scalar_alignment->{$e->{NAME}};
368 return 0 if ($e eq "EMPTY");
370 unless (hasType($e)) {
371 # it must be an external type - all we can do is guess
372 # warning($e, "assuming alignment of unknown type '$e' is 4");
373 return 4;
376 my $dt = getType($e);
378 if ($dt->{TYPE} eq "TYPEDEF") {
379 return align_type($dt->{DATA});
380 } elsif ($dt->{TYPE} eq "ENUM") {
381 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
382 } elsif ($dt->{TYPE} eq "BITMAP") {
383 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
384 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
385 # Struct/union without body: assume 4
386 return 4 unless (defined($dt->{ELEMENTS}));
387 return find_largest_alignment($dt);
390 die("Unknown data type type $dt->{TYPE}");
393 sub ParseElement($$)
395 my ($e, $pointer_default) = @_;
397 $e->{TYPE} = expandAlias($e->{TYPE});
399 if (ref($e->{TYPE}) eq "HASH") {
400 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default);
403 return {
404 NAME => $e->{NAME},
405 TYPE => $e->{TYPE},
406 PROPERTIES => $e->{PROPERTIES},
407 LEVELS => GetElementLevelTable($e, $pointer_default),
408 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
409 ALIGN => align_type($e->{TYPE}),
410 ORIGINAL => $e
414 sub ParseStruct($$)
416 my ($struct, $pointer_default) = @_;
417 my @elements = ();
418 my $surrounding = undef;
420 return {
421 TYPE => "STRUCT",
422 NAME => $struct->{NAME},
423 SURROUNDING_ELEMENT => undef,
424 ELEMENTS => undef,
425 PROPERTIES => $struct->{PROPERTIES},
426 ORIGINAL => $struct,
427 ALIGN => undef
428 } unless defined($struct->{ELEMENTS});
430 CheckPointerTypes($struct, $pointer_default);
432 foreach my $x (@{$struct->{ELEMENTS}})
434 my $e = ParseElement($x, $pointer_default);
435 if ($x != $struct->{ELEMENTS}[-1] and
436 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
437 fatal($x, "conformant member not at end of struct");
439 push @elements, $e;
442 my $e = $elements[-1];
443 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
444 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
445 $surrounding = $e;
448 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
449 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
450 $surrounding = $struct->{ELEMENTS}[-1];
453 my $align = undef;
454 if ($struct->{NAME}) {
455 $align = align_type($struct->{NAME});
458 return {
459 TYPE => "STRUCT",
460 NAME => $struct->{NAME},
461 SURROUNDING_ELEMENT => $surrounding,
462 ELEMENTS => \@elements,
463 PROPERTIES => $struct->{PROPERTIES},
464 ORIGINAL => $struct,
465 ALIGN => $align
469 sub ParseUnion($$)
471 my ($e, $pointer_default) = @_;
472 my @elements = ();
473 my $hasdefault = 0;
474 my $switch_type = has_property($e, "switch_type");
475 unless (defined($switch_type)) { $switch_type = "uint32"; }
476 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
478 return {
479 TYPE => "UNION",
480 NAME => $e->{NAME},
481 SWITCH_TYPE => $switch_type,
482 ELEMENTS => undef,
483 PROPERTIES => $e->{PROPERTIES},
484 HAS_DEFAULT => $hasdefault,
485 ORIGINAL => $e
486 } unless defined($e->{ELEMENTS});
488 CheckPointerTypes($e, $pointer_default);
490 foreach my $x (@{$e->{ELEMENTS}})
492 my $t;
493 if ($x->{TYPE} eq "EMPTY") {
494 $t = { TYPE => "EMPTY" };
495 } else {
496 $t = ParseElement($x, $pointer_default);
498 if (has_property($x, "default")) {
499 $t->{CASE} = "default";
500 $hasdefault = 1;
501 } elsif (defined($x->{PROPERTIES}->{case})) {
502 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
503 } else {
504 die("Union element $x->{NAME} has neither default nor case property");
506 push @elements, $t;
509 return {
510 TYPE => "UNION",
511 NAME => $e->{NAME},
512 SWITCH_TYPE => $switch_type,
513 ELEMENTS => \@elements,
514 PROPERTIES => $e->{PROPERTIES},
515 HAS_DEFAULT => $hasdefault,
516 ORIGINAL => $e
520 sub ParseEnum($$)
522 my ($e, $pointer_default) = @_;
524 return {
525 TYPE => "ENUM",
526 NAME => $e->{NAME},
527 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
528 ELEMENTS => $e->{ELEMENTS},
529 PROPERTIES => $e->{PROPERTIES},
530 ORIGINAL => $e
534 sub ParseBitmap($$)
536 my ($e, $pointer_default) = @_;
538 return {
539 TYPE => "BITMAP",
540 NAME => $e->{NAME},
541 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
542 ELEMENTS => $e->{ELEMENTS},
543 PROPERTIES => $e->{PROPERTIES},
544 ORIGINAL => $e
548 sub ParseType($$)
550 my ($d, $pointer_default) = @_;
552 my $data = {
553 STRUCT => \&ParseStruct,
554 UNION => \&ParseUnion,
555 ENUM => \&ParseEnum,
556 BITMAP => \&ParseBitmap,
557 TYPEDEF => \&ParseTypedef,
558 }->{$d->{TYPE}}->($d, $pointer_default);
560 return $data;
563 sub ParseTypedef($$)
565 my ($d, $pointer_default) = @_;
567 if (defined($d->{DATA}->{PROPERTIES}) && !defined($d->{PROPERTIES})) {
568 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
571 my $data = ParseType($d->{DATA}, $pointer_default);
572 $data->{ALIGN} = align_type($d->{NAME});
574 return {
575 NAME => $d->{NAME},
576 TYPE => $d->{TYPE},
577 PROPERTIES => $d->{PROPERTIES},
578 DATA => $data,
579 ORIGINAL => $d
583 sub ParseConst($$)
585 my ($ndr,$d) = @_;
587 return $d;
590 sub ParseFunction($$$)
592 my ($ndr,$d,$opnum) = @_;
593 my @elements = ();
594 my $rettype = undef;
595 my $thisopnum = undef;
597 CheckPointerTypes($d, "ref");
599 if (not defined($d->{PROPERTIES}{noopnum})) {
600 $thisopnum = ${$opnum};
601 ${$opnum}++;
604 foreach my $x (@{$d->{ELEMENTS}}) {
605 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default});
606 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
607 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
609 push (@elements, $e);
612 if ($d->{RETURN_TYPE} ne "void") {
613 $rettype = expandAlias($d->{RETURN_TYPE});
616 my $async = 0;
617 if (has_property($d, "async")) { $async = 1; }
619 return {
620 NAME => $d->{NAME},
621 TYPE => "FUNCTION",
622 OPNUM => $thisopnum,
623 ASYNC => $async,
624 RETURN_TYPE => $rettype,
625 PROPERTIES => $d->{PROPERTIES},
626 ELEMENTS => \@elements,
627 ORIGINAL => $d
631 sub CheckPointerTypes($$)
633 my ($s,$default) = @_;
635 return unless defined($s->{ELEMENTS});
637 foreach my $e (@{$s->{ELEMENTS}}) {
638 if ($e->{POINTERS} and not defined(pointer_type($e))) {
639 $e->{PROPERTIES}->{$default} = '1';
644 sub FindNestedTypes($$)
646 sub FindNestedTypes($$);
647 my ($l, $t) = @_;
649 return unless defined($t->{ELEMENTS});
650 return if ($t->{TYPE} eq "ENUM");
651 return if ($t->{TYPE} eq "BITMAP");
653 foreach (@{$t->{ELEMENTS}}) {
654 if (ref($_->{TYPE}) eq "HASH") {
655 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
656 FindNestedTypes($l, $_->{TYPE});
661 sub ParseInterface($)
663 my $idl = shift;
664 my @types = ();
665 my @consts = ();
666 my @functions = ();
667 my @endpoints;
668 my $opnum = 0;
669 my $version;
671 if (not has_property($idl, "pointer_default")) {
672 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
673 # and "unique" in Microsoft Extensions mode (default)
674 $idl->{PROPERTIES}->{pointer_default} = "unique";
677 foreach my $d (@{$idl->{DATA}}) {
678 if ($d->{TYPE} eq "FUNCTION") {
679 push (@functions, ParseFunction($idl, $d, \$opnum));
680 } elsif ($d->{TYPE} eq "CONST") {
681 push (@consts, ParseConst($idl, $d));
682 } else {
683 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}));
684 FindNestedTypes(\@types, $d);
688 $version = "0.0";
690 if(defined $idl->{PROPERTIES}->{version}) {
691 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
692 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
693 $version = $idl->{PROPERTIES}->{version};
694 } else {
695 $version = $if_version[1] << 16 | $if_version[0];
699 # If no endpoint is set, default to the interface name as a named pipe
700 if (!defined $idl->{PROPERTIES}->{endpoint}) {
701 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
702 } else {
703 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
706 return {
707 NAME => $idl->{NAME},
708 UUID => lc(has_property($idl, "uuid")),
709 VERSION => $version,
710 TYPE => "INTERFACE",
711 PROPERTIES => $idl->{PROPERTIES},
712 FUNCTIONS => \@functions,
713 CONSTS => \@consts,
714 TYPES => \@types,
715 ENDPOINTS => \@endpoints
719 # Convert a IDL tree to a NDR tree
720 # Gives a result tree describing all that's necessary for easily generating
721 # NDR parsers / generators
722 sub Parse($)
724 my $idl = shift;
726 return undef unless (defined($idl));
728 Parse::Pidl::NDR::Validate($idl);
730 my @ndr = ();
732 foreach (@{$idl}) {
733 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
734 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
735 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
738 return \@ndr;
741 sub GetNextLevel($$)
743 my $e = shift;
744 my $fl = shift;
746 my $seen = 0;
748 foreach my $l (@{$e->{LEVELS}}) {
749 return $l if ($seen);
750 ($seen = 1) if ($l == $fl);
753 return undef;
756 sub GetPrevLevel($$)
758 my ($e,$fl) = @_;
759 my $prev = undef;
761 foreach my $l (@{$e->{LEVELS}}) {
762 (return $prev) if ($l == $fl);
763 $prev = $l;
766 return undef;
769 sub ContainsString($)
771 my ($e) = @_;
773 foreach my $l (@{$e->{LEVELS}}) {
774 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
777 return 0;
780 sub ContainsDeferred($$)
782 my ($e,$l) = @_;
784 return 1 if ($l->{CONTAINS_DEFERRED});
786 while ($l = GetNextLevel($e,$l))
788 return 1 if ($l->{IS_DEFERRED});
789 return 1 if ($l->{CONTAINS_DEFERRED});
792 return 0;
795 sub el_name($)
797 my $e = shift;
798 my $name = "<ANONYMOUS>";
800 $name = $e->{NAME} if defined($e->{NAME});
802 if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
803 return "$e->{PARENT}->{NAME}.$name";
806 if (defined($e->{PARENT}) and
807 defined($e->{PARENT}->{PARENT}) and
808 defined($e->{PARENT}->{PARENT}->{NAME})) {
809 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
812 return $name;
815 ###################################
816 # find a sibling var in a structure
817 sub find_sibling($$)
819 my($e,$name) = @_;
820 my($fn) = $e->{PARENT};
822 if ($name =~ /\*(.*)/) {
823 $name = $1;
826 for my $e2 (@{$fn->{ELEMENTS}}) {
827 return $e2 if ($e2->{NAME} eq $name);
830 return undef;
833 my %property_list = (
834 # interface
835 "helpstring" => ["INTERFACE", "FUNCTION"],
836 "version" => ["INTERFACE"],
837 "uuid" => ["INTERFACE"],
838 "endpoint" => ["INTERFACE"],
839 "pointer_default" => ["INTERFACE"],
840 "helper" => ["INTERFACE"],
841 "authservice" => ["INTERFACE"],
843 # dcom
844 "object" => ["INTERFACE"],
845 "local" => ["INTERFACE", "FUNCTION"],
846 "iid_is" => ["ELEMENT"],
847 "call_as" => ["FUNCTION"],
848 "idempotent" => ["FUNCTION"],
850 # function
851 "noopnum" => ["FUNCTION"],
852 "in" => ["ELEMENT"],
853 "out" => ["ELEMENT"],
854 "async" => ["FUNCTION"],
856 # pointer
857 "ref" => ["ELEMENT"],
858 "ptr" => ["ELEMENT"],
859 "unique" => ["ELEMENT"],
860 "ignore" => ["ELEMENT"],
861 "relative" => ["ELEMENT"],
862 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
864 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
865 "value" => ["ELEMENT"],
866 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
868 # generic
869 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
870 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
871 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
872 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
873 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
874 "noejs" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
875 "todo" => ["FUNCTION"],
877 # union
878 "switch_is" => ["ELEMENT"],
879 "switch_type" => ["ELEMENT", "UNION"],
880 "nodiscriminant" => ["UNION"],
881 "case" => ["ELEMENT"],
882 "default" => ["ELEMENT"],
884 "represent_as" => ["ELEMENT"],
885 "transmit_as" => ["ELEMENT"],
887 # subcontext
888 "subcontext" => ["ELEMENT"],
889 "subcontext_size" => ["ELEMENT"],
890 "compression" => ["ELEMENT"],
892 # enum
893 "enum8bit" => ["ENUM"],
894 "enum16bit" => ["ENUM"],
895 "v1_enum" => ["ENUM"],
897 # bitmap
898 "bitmap8bit" => ["BITMAP"],
899 "bitmap16bit" => ["BITMAP"],
900 "bitmap32bit" => ["BITMAP"],
901 "bitmap64bit" => ["BITMAP"],
903 # array
904 "range" => ["ELEMENT"],
905 "size_is" => ["ELEMENT"],
906 "string" => ["ELEMENT"],
907 "noheader" => ["ELEMENT"],
908 "charset" => ["ELEMENT"],
909 "length_is" => ["ELEMENT"],
912 #####################################################################
913 # check for unknown properties
914 sub ValidProperties($$)
916 my ($e,$t) = @_;
918 return unless defined $e->{PROPERTIES};
920 foreach my $key (keys %{$e->{PROPERTIES}}) {
921 warning($e, el_name($e) . ": unknown property '$key'")
922 unless defined($property_list{$key});
924 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
925 unless grep(/^$t$/, @{$property_list{$key}});
929 sub mapToScalar($)
931 sub mapToScalar($);
932 my $t = shift;
933 return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
934 my $ti = getType($t);
936 if (not defined ($ti)) {
937 return undef;
938 } elsif ($ti->{TYPE} eq "TYPEDEF") {
939 return mapToScalar($ti->{DATA});
940 } elsif ($ti->{TYPE} eq "ENUM") {
941 return Parse::Pidl::Typelist::enum_type_fn($ti);
942 } elsif ($ti->{TYPE} eq "BITMAP") {
943 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
946 return undef;
949 #####################################################################
950 # validate an element
951 sub ValidElement($)
953 my $e = shift;
955 ValidProperties($e,"ELEMENT");
957 # Check whether switches are used correctly.
958 if (my $switch = has_property($e, "switch_is")) {
959 my $e2 = find_sibling($e, $switch);
960 my $type = getType($e->{TYPE});
962 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
963 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
966 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
967 my $discriminator_type = has_property($type->{DATA}, "switch_type");
968 $discriminator_type = "uint32" unless defined ($discriminator_type);
970 my $t1 = mapToScalar($discriminator_type);
972 if (not defined($t1)) {
973 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
976 my $t2 = mapToScalar($e2->{TYPE});
977 if (not defined($t2)) {
978 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
981 if ($t1 ne $t2) {
982 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
987 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
988 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
991 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
992 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
995 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
996 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
999 if (has_property($e, "represent_as") and has_property($e, "value")) {
1000 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1003 if (has_property($e, "subcontext")) {
1004 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1007 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1008 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1011 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1012 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1015 if (!$e->{POINTERS} && (
1016 has_property($e, "ptr") or
1017 has_property($e, "unique") or
1018 has_property($e, "relative") or
1019 has_property($e, "ref"))) {
1020 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
1024 #####################################################################
1025 # validate an enum
1026 sub ValidEnum($)
1028 my ($enum) = @_;
1030 ValidProperties($enum, "ENUM");
1033 #####################################################################
1034 # validate a bitmap
1035 sub ValidBitmap($)
1037 my ($bitmap) = @_;
1039 ValidProperties($bitmap, "BITMAP");
1042 #####################################################################
1043 # validate a struct
1044 sub ValidStruct($)
1046 my($struct) = shift;
1048 ValidProperties($struct, "STRUCT");
1050 return unless defined($struct->{ELEMENTS});
1052 foreach my $e (@{$struct->{ELEMENTS}}) {
1053 $e->{PARENT} = $struct;
1054 ValidElement($e);
1058 #####################################################################
1059 # parse a union
1060 sub ValidUnion($)
1062 my($union) = shift;
1064 ValidProperties($union,"UNION");
1066 if (has_property($union->{PARENT}, "nodiscriminant") and has_property($union->{PARENT}, "switch_type")) {
1067 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type() on union without discriminant");
1070 return unless defined($union->{ELEMENTS});
1072 foreach my $e (@{$union->{ELEMENTS}}) {
1073 $e->{PARENT} = $union;
1075 if (defined($e->{PROPERTIES}->{default}) and
1076 defined($e->{PROPERTIES}->{case})) {
1077 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1080 unless (defined ($e->{PROPERTIES}->{default}) or
1081 defined ($e->{PROPERTIES}->{case})) {
1082 fatal($e, "Union member $e->{NAME} must have default or case property");
1085 if (has_property($e, "ref")) {
1086 fatal($e, el_name($e) . " : embedded ref pointers are not supported yet\n");
1090 ValidElement($e);
1094 #####################################################################
1095 # parse a typedef
1096 sub ValidTypedef($)
1098 my($typedef) = shift;
1099 my $data = $typedef->{DATA};
1101 ValidProperties($typedef, "TYPEDEF");
1103 $data->{PARENT} = $typedef;
1105 $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1106 $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1108 ValidType($data) if (ref($data) eq "HASH");
1111 #####################################################################
1112 # validate a function
1113 sub ValidFunction($)
1115 my($fn) = shift;
1117 ValidProperties($fn,"FUNCTION");
1119 foreach my $e (@{$fn->{ELEMENTS}}) {
1120 $e->{PARENT} = $fn;
1121 if (has_property($e, "ref") && !$e->{POINTERS}) {
1122 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1124 ValidElement($e);
1128 #####################################################################
1129 # validate a type
1130 sub ValidType($)
1132 my ($t) = @_;
1135 TYPEDEF => \&ValidTypedef,
1136 STRUCT => \&ValidStruct,
1137 UNION => \&ValidUnion,
1138 ENUM => \&ValidEnum,
1139 BITMAP => \&ValidBitmap
1140 }->{$t->{TYPE}}->($t);
1143 #####################################################################
1144 # parse the interface definitions
1145 sub ValidInterface($)
1147 my($interface) = shift;
1148 my($data) = $interface->{DATA};
1150 if (has_property($interface, "helper")) {
1151 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1154 ValidProperties($interface,"INTERFACE");
1156 if (has_property($interface, "pointer_default")) {
1157 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1158 ("ref", "unique", "ptr"))) {
1159 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1163 if (has_property($interface, "object")) {
1164 if (has_property($interface, "version") &&
1165 $interface->{PROPERTIES}->{version} != 0) {
1166 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1169 if (!defined($interface->{BASE}) &&
1170 not ($interface->{NAME} eq "IUnknown")) {
1171 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1175 foreach my $d (@{$data}) {
1176 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1177 ($d->{TYPE} eq "TYPEDEF" or
1178 $d->{TYPE} eq "STRUCT" or
1179 $d->{TYPE} eq "UNION" or
1180 $d->{TYPE} eq "ENUM" or
1181 $d->{TYPE} eq "BITMAP") && ValidType($d);
1186 #####################################################################
1187 # Validate an IDL structure
1188 sub Validate($)
1190 my($idl) = shift;
1192 foreach my $x (@{$idl}) {
1193 ($x->{TYPE} eq "INTERFACE") &&
1194 ValidInterface($x);
1195 ($x->{TYPE} eq "IMPORTLIB") &&
1196 fatal($x, "importlib() not supported");
1200 sub is_charset_array($$)
1202 my ($e,$l) = @_;
1204 return 0 if ($l->{TYPE} ne "ARRAY");
1206 my $nl = GetNextLevel($e,$l);
1208 return 0 unless ($nl->{TYPE} eq "DATA");
1210 return has_property($e, "charset");