r21457: Cope with anonymous nested types in the NDR layer. This doesn't handled
[Samba/ekacnet.git] / source / pidl / lib / Parse / Pidl / NDR.pm
bloba921e5cbe5018112593a2ccbdafbd9b22aa8b351
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);
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 = shift;
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;
117 if ($d eq "*") {
118 $is_conformant = 1;
119 if ($size = shift @size_is) {
120 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
121 $is_string = 1;
122 delete($e->{PROPERTIES}->{string});
123 } else {
124 fatal($e, "Must specify size_is() for conformant array!")
127 if (($length = shift @length_is) or $is_string) {
128 $is_varying = 1;
129 } else {
130 $length = $size;
133 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
134 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
135 $is_surrounding = 1;
139 push (@$order, {
140 TYPE => "ARRAY",
141 SIZE_IS => $size,
142 LENGTH_IS => $length,
143 IS_DEFERRED => "$is_deferred",
144 IS_SURROUNDING => "$is_surrounding",
145 IS_ZERO_TERMINATED => "$is_string",
146 IS_VARYING => "$is_varying",
147 IS_CONFORMANT => "$is_conformant",
148 IS_FIXED => (not $is_conformant and Parse::Pidl::Util::is_constant($size)),
149 IS_INLINE => (not $is_conformant and not Parse::Pidl::Util::is_constant($size))
153 # Next, all the pointers
154 foreach my $i (1..$e->{POINTERS}) {
155 my $pt = pointer_type($e);
157 my $level = "EMBEDDED";
158 # Top level "ref" pointers do not have a referrent identifier
159 $level = "TOP" if ( defined($pt)
160 and $i == 1
161 and $e->{PARENT}->{TYPE} eq "FUNCTION");
163 push (@$order, {
164 TYPE => "POINTER",
165 # for now, there can only be one pointer type per element
166 POINTER_TYPE => pointer_type($e),
167 POINTER_INDEX => $pointer_idx,
168 IS_DEFERRED => "$is_deferred",
169 LEVEL => $level
172 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
173 if ($i == 1 and pointer_type($e) ne "ref" and
174 $e->{PARENT}->{TYPE} eq "FUNCTION" and
175 not has_property($e, "in"));
177 $pointer_idx++;
179 # everything that follows will be deferred
180 $is_deferred = 1 if ($e->{PARENT}->{TYPE} ne "FUNCTION");
182 my $array_size = shift @size_is;
183 my $array_length;
184 my $is_varying;
185 my $is_conformant;
186 my $is_string = 0;
187 if ($array_size) {
188 $is_conformant = 1;
189 if ($array_length = shift @length_is) {
190 $is_varying = 1;
191 } else {
192 $array_length = $array_size;
193 $is_varying =0;
197 if (scalar(@size_is) == 0 and has_property($e, "string") and
198 $i == $e->{POINTERS}) {
199 $is_string = 1;
200 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
201 delete($e->{PROPERTIES}->{string});
204 if ($array_size or $is_string) {
205 push (@$order, {
206 TYPE => "ARRAY",
207 IS_ZERO_TERMINATED => "$is_string",
208 SIZE_IS => $array_size,
209 LENGTH_IS => $array_length,
210 IS_DEFERRED => "$is_deferred",
211 IS_SURROUNDING => 0,
212 IS_VARYING => "$is_varying",
213 IS_CONFORMANT => "$is_conformant",
214 IS_FIXED => 0,
215 IS_INLINE => 0,
218 $is_deferred = 0;
222 if (defined(has_property($e, "subcontext"))) {
223 my $hdr_size = has_property($e, "subcontext");
224 my $subsize = has_property($e, "subcontext_size");
225 if (not defined($subsize)) {
226 $subsize = -1;
229 push (@$order, {
230 TYPE => "SUBCONTEXT",
231 HEADER_SIZE => $hdr_size,
232 SUBCONTEXT_SIZE => $subsize,
233 IS_DEFERRED => $is_deferred,
234 COMPRESSION => has_property($e, "compression"),
238 if (my $switch = has_property($e, "switch_is")) {
239 push (@$order, {
240 TYPE => "SWITCH",
241 SWITCH_IS => $switch,
242 IS_DEFERRED => $is_deferred
246 if (scalar(@size_is) > 0) {
247 warning($e, "size_is() on non-array element");
250 if (scalar(@length_is) > 0) {
251 warning($e, "length_is() on non-array element");
254 if (has_property($e, "string")) {
255 warning($e, "string() attribute on non-array element");
258 push (@$order, {
259 TYPE => "DATA",
260 DATA_TYPE => $e->{TYPE},
261 IS_DEFERRED => $is_deferred,
262 CONTAINS_DEFERRED => can_contain_deferred($e),
263 IS_SURROUNDING => 0 #FIXME
266 my $i = 0;
267 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
269 return $order;
272 #####################################################################
273 # see if a type contains any deferred data
274 sub can_contain_deferred
276 my $e = shift;
278 return 0 if (Parse::Pidl::Typelist::is_scalar($e->{TYPE}));
279 return 1 unless (hasType($e->{TYPE})); # assume the worst
281 my $type = getType($e->{TYPE});
283 foreach my $x (@{$type->{DATA}->{ELEMENTS}}) {
284 return 1 if ($x->{POINTERS});
285 return 1 if (can_contain_deferred ($x));
288 return 0;
291 sub pointer_type($)
293 my $e = shift;
295 return undef unless $e->{POINTERS};
297 return "ref" if (has_property($e, "ref"));
298 return "full" if (has_property($e, "ptr"));
299 return "sptr" if (has_property($e, "sptr"));
300 return "unique" if (has_property($e, "unique"));
301 return "relative" if (has_property($e, "relative"));
302 return "ignore" if (has_property($e, "ignore"));
304 return undef;
307 #####################################################################
308 # work out the correct alignment for a structure or union
309 sub find_largest_alignment($)
311 my $s = shift;
313 my $align = 1;
314 for my $e (@{$s->{ELEMENTS}}) {
315 my $a = 1;
317 if ($e->{POINTERS}) {
318 $a = 4;
319 } elsif (has_property($e, "subcontext")) {
320 $a = 1;
321 } elsif (has_property($e, "transmit_as")) {
322 $a = align_type($e->{PROPERTIES}->{transmit_as});
323 } else {
324 $a = align_type($e->{TYPE});
327 $align = $a if ($align < $a);
330 return $align;
333 #####################################################################
334 # align a type
335 sub align_type($)
337 sub align_type($);
338 my $e = shift;
340 unless (hasType($e)) {
341 # it must be an external type - all we can do is guess
342 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
343 return 4;
346 my $dt = getType($e)->{DATA};
348 if ($dt->{TYPE} eq "ENUM") {
349 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
350 } elsif ($dt->{TYPE} eq "BITMAP") {
351 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
352 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
353 return find_largest_alignment($dt);
354 } elsif ($dt->{TYPE} eq "SCALAR") {
355 return $scalar_alignment->{$dt->{NAME}};
358 die("Unknown data type type $dt->{TYPE}");
361 sub ParseElement($$)
363 my ($e, $pointer_default) = @_;
365 $e->{TYPE} = expandAlias($e->{TYPE});
367 if (ref($e->{TYPE}) eq "HASH") {
368 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default);
371 return {
372 NAME => $e->{NAME},
373 TYPE => $e->{TYPE},
374 PROPERTIES => $e->{PROPERTIES},
375 LEVELS => GetElementLevelTable($e),
376 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
377 ALIGN => align_type($e->{TYPE}),
378 ORIGINAL => $e
382 sub ParseStruct($$)
384 my ($struct, $pointer_default) = @_;
385 my @elements = ();
386 my $surrounding = undef;
388 foreach my $x (@{$struct->{ELEMENTS}})
390 my $e = ParseElement($x, $pointer_default);
391 if ($x != $struct->{ELEMENTS}[-1] and
392 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
393 fatal($x, "conformant member not at end of struct");
395 push @elements, $e;
398 my $e = $elements[-1];
399 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
400 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
401 $surrounding = $e;
404 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
405 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
406 $surrounding = $struct->{ELEMENTS}[-1];
409 my $align = undef;
410 if ($struct->{NAME}) {
411 $align = align_type($struct->{NAME});
414 return {
415 TYPE => "STRUCT",
416 NAME => $struct->{NAME},
417 SURROUNDING_ELEMENT => $surrounding,
418 ELEMENTS => \@elements,
419 PROPERTIES => $struct->{PROPERTIES},
420 ORIGINAL => $struct,
421 ALIGN => $align
425 sub ParseUnion($$)
427 my ($e, $pointer_default) = @_;
428 my @elements = ();
429 my $switch_type = has_property($e, "switch_type");
430 unless (defined($switch_type)) { $switch_type = "uint32"; }
432 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
434 my $hasdefault = 0;
435 foreach my $x (@{$e->{ELEMENTS}})
437 my $t;
438 if ($x->{TYPE} eq "EMPTY") {
439 $t = { TYPE => "EMPTY" };
440 } else {
441 $t = ParseElement($x, $pointer_default);
443 if (has_property($x, "default")) {
444 $t->{CASE} = "default";
445 $hasdefault = 1;
446 } elsif (defined($x->{PROPERTIES}->{case})) {
447 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
448 } else {
449 die("Union element $x->{NAME} has neither default nor case property");
451 push @elements, $t;
454 return {
455 TYPE => "UNION",
456 NAME => $e->{NAME},
457 SWITCH_TYPE => $switch_type,
458 ELEMENTS => \@elements,
459 PROPERTIES => $e->{PROPERTIES},
460 HAS_DEFAULT => $hasdefault,
461 ORIGINAL => $e
465 sub ParseEnum($$)
467 my ($e, $pointer_default) = @_;
469 return {
470 TYPE => "ENUM",
471 NAME => $e->{NAME},
472 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
473 ELEMENTS => $e->{ELEMENTS},
474 PROPERTIES => $e->{PROPERTIES},
475 ORIGINAL => $e
479 sub ParseBitmap($$)
481 my ($e, $pointer_default) = @_;
483 return {
484 TYPE => "BITMAP",
485 NAME => $e->{NAME},
486 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
487 ELEMENTS => $e->{ELEMENTS},
488 PROPERTIES => $e->{PROPERTIES},
489 ORIGINAL => $e
493 sub ParseType($$)
495 my ($d, $pointer_default) = @_;
497 if ($d->{TYPE} eq "STRUCT" or $d->{TYPE} eq "UNION") {
498 CheckPointerTypes($d, $pointer_default);
501 my $data = {
502 STRUCT => \&ParseStruct,
503 UNION => \&ParseUnion,
504 ENUM => \&ParseEnum,
505 BITMAP => \&ParseBitmap,
506 TYPEDEF => \&ParseTypedef,
507 }->{$d->{TYPE}}->($d, $pointer_default);
509 return $data;
512 sub ParseTypedef($$)
514 my ($d, $pointer_default) = @_;
516 if (defined($d->{DATA}->{PROPERTIES}) && !defined($d->{PROPERTIES})) {
517 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
520 my $data = ParseType($d->{DATA}, $pointer_default);
521 $data->{ALIGN} = align_type($d->{NAME});
523 return {
524 NAME => $d->{NAME},
525 TYPE => $d->{TYPE},
526 PROPERTIES => $d->{PROPERTIES},
527 DATA => $data,
528 ORIGINAL => $d
532 sub ParseConst($$)
534 my ($ndr,$d) = @_;
536 return $d;
539 sub ParseFunction($$$)
541 my ($ndr,$d,$opnum) = @_;
542 my @elements = ();
543 my $rettype = undef;
544 my $thisopnum = undef;
546 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default_top});
548 if (not defined($d->{PROPERTIES}{noopnum})) {
549 $thisopnum = ${$opnum};
550 ${$opnum}++;
553 foreach my $x (@{$d->{ELEMENTS}}) {
554 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default});
555 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
556 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
558 push (@elements, $e);
561 if ($d->{RETURN_TYPE} ne "void") {
562 $rettype = expandAlias($d->{RETURN_TYPE});
565 my $async = 0;
566 if (has_property($d, "async")) { $async = 1; }
568 return {
569 NAME => $d->{NAME},
570 TYPE => "FUNCTION",
571 OPNUM => $thisopnum,
572 ASYNC => $async,
573 RETURN_TYPE => $rettype,
574 PROPERTIES => $d->{PROPERTIES},
575 ELEMENTS => \@elements,
576 ORIGINAL => $d
580 sub CheckPointerTypes($$)
582 my ($s,$default) = @_;
584 foreach my $e (@{$s->{ELEMENTS}}) {
585 if ($e->{POINTERS} and not defined(pointer_type($e))) {
586 $e->{PROPERTIES}->{$default} = 1;
591 sub ParseInterface($)
593 my $idl = shift;
594 my @types = ();
595 my @consts = ();
596 my @functions = ();
597 my @endpoints;
598 my @declares = ();
599 my $opnum = 0;
600 my $version;
602 if (not has_property($idl, "pointer_default")) {
603 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
604 # and "unique" in Microsoft Extensions mode (default)
605 $idl->{PROPERTIES}->{pointer_default} = "unique";
608 if (not has_property($idl, "pointer_default_top")) {
609 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
610 } else {
611 warning($idl, "pointer_default_top() is a pidl extension and should not be used");
614 foreach my $d (@{$idl->{DATA}}) {
615 if ($d->{TYPE} eq "DECLARE") {
616 push (@declares, $d);
617 } elsif ($d->{TYPE} eq "FUNCTION") {
618 push (@functions, ParseFunction($idl, $d, \$opnum));
619 } elsif ($d->{TYPE} eq "CONST") {
620 push (@consts, ParseConst($idl, $d));
621 } else {
622 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}));
626 $version = "0.0";
628 if(defined $idl->{PROPERTIES}->{version}) {
629 $version = $idl->{PROPERTIES}->{version};
632 # If no endpoint is set, default to the interface name as a named pipe
633 if (!defined $idl->{PROPERTIES}->{endpoint}) {
634 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
635 } else {
636 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
639 return {
640 NAME => $idl->{NAME},
641 UUID => lc(has_property($idl, "uuid")),
642 VERSION => $version,
643 TYPE => "INTERFACE",
644 PROPERTIES => $idl->{PROPERTIES},
645 FUNCTIONS => \@functions,
646 CONSTS => \@consts,
647 TYPES => \@types,
648 DECLARES => \@declares,
649 ENDPOINTS => \@endpoints
653 # Convert a IDL tree to a NDR tree
654 # Gives a result tree describing all that's necessary for easily generating
655 # NDR parsers / generators
656 sub Parse($)
658 my $idl = shift;
660 return undef unless (defined($idl));
662 Parse::Pidl::NDR::Validate($idl);
664 my @ndr = ();
666 foreach (@{$idl}) {
667 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
668 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
671 return \@ndr;
674 sub GetNextLevel($$)
676 my $e = shift;
677 my $fl = shift;
679 my $seen = 0;
681 foreach my $l (@{$e->{LEVELS}}) {
682 return $l if ($seen);
683 ($seen = 1) if ($l == $fl);
686 return undef;
689 sub GetPrevLevel($$)
691 my ($e,$fl) = @_;
692 my $prev = undef;
694 foreach my $l (@{$e->{LEVELS}}) {
695 (return $prev) if ($l == $fl);
696 $prev = $l;
699 return undef;
702 sub ContainsString($)
704 my ($e) = @_;
706 foreach my $l (@{$e->{LEVELS}}) {
707 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
710 return 0;
713 sub ContainsDeferred($$)
715 my ($e,$l) = @_;
717 return 1 if ($l->{CONTAINS_DEFERRED});
719 while ($l = GetNextLevel($e,$l))
721 return 1 if ($l->{IS_DEFERRED});
722 return 1 if ($l->{CONTAINS_DEFERRED});
725 return 0;
728 sub el_name($)
730 my $e = shift;
732 if ($e->{PARENT} && $e->{PARENT}->{NAME}) {
733 return "$e->{PARENT}->{NAME}.$e->{NAME}";
736 if ($e->{PARENT} && $e->{PARENT}->{PARENT}->{NAME}) {
737 return "$e->{PARENT}->{PARENT}->{NAME}.$e->{NAME}";
740 if ($e->{PARENT}) {
741 return "$e->{PARENT}->{NAME}.$e->{NAME}";
744 return $e->{NAME};
747 ###################################
748 # find a sibling var in a structure
749 sub find_sibling($$)
751 my($e,$name) = @_;
752 my($fn) = $e->{PARENT};
754 if ($name =~ /\*(.*)/) {
755 $name = $1;
758 for my $e2 (@{$fn->{ELEMENTS}}) {
759 return $e2 if ($e2->{NAME} eq $name);
762 return undef;
765 my %property_list = (
766 # interface
767 "helpstring" => ["INTERFACE", "FUNCTION"],
768 "version" => ["INTERFACE"],
769 "uuid" => ["INTERFACE"],
770 "endpoint" => ["INTERFACE"],
771 "pointer_default" => ["INTERFACE"],
772 "pointer_default_top" => ["INTERFACE"],
773 "helper" => ["INTERFACE"],
774 "authservice" => ["INTERFACE"],
776 # dcom
777 "object" => ["INTERFACE"],
778 "local" => ["INTERFACE", "FUNCTION"],
779 "iid_is" => ["ELEMENT"],
780 "call_as" => ["FUNCTION"],
781 "idempotent" => ["FUNCTION"],
783 # function
784 "noopnum" => ["FUNCTION"],
785 "in" => ["ELEMENT"],
786 "out" => ["ELEMENT"],
787 "async" => ["FUNCTION"],
789 # pointer
790 "ref" => ["ELEMENT"],
791 "ptr" => ["ELEMENT"],
792 "unique" => ["ELEMENT"],
793 "ignore" => ["ELEMENT"],
794 "relative" => ["ELEMENT"],
795 "relative_base" => ["TYPEDEF"],
797 "gensize" => ["TYPEDEF"],
798 "value" => ["ELEMENT"],
799 "flag" => ["ELEMENT", "TYPEDEF"],
801 # generic
802 "public" => ["FUNCTION", "TYPEDEF"],
803 "nopush" => ["FUNCTION", "TYPEDEF"],
804 "nopull" => ["FUNCTION", "TYPEDEF"],
805 "nosize" => ["FUNCTION", "TYPEDEF"],
806 "noprint" => ["FUNCTION", "TYPEDEF"],
807 "noejs" => ["FUNCTION", "TYPEDEF"],
809 # union
810 "switch_is" => ["ELEMENT"],
811 "switch_type" => ["ELEMENT", "TYPEDEF"],
812 "nodiscriminant" => ["TYPEDEF"],
813 "case" => ["ELEMENT"],
814 "default" => ["ELEMENT"],
816 "represent_as" => ["ELEMENT"],
817 "transmit_as" => ["ELEMENT"],
819 # subcontext
820 "subcontext" => ["ELEMENT"],
821 "subcontext_size" => ["ELEMENT"],
822 "compression" => ["ELEMENT"],
824 # enum
825 "enum8bit" => ["TYPEDEF"],
826 "enum16bit" => ["TYPEDEF"],
827 "v1_enum" => ["TYPEDEF"],
829 # bitmap
830 "bitmap8bit" => ["TYPEDEF"],
831 "bitmap16bit" => ["TYPEDEF"],
832 "bitmap32bit" => ["TYPEDEF"],
833 "bitmap64bit" => ["TYPEDEF"],
835 # array
836 "range" => ["ELEMENT"],
837 "size_is" => ["ELEMENT"],
838 "string" => ["ELEMENT"],
839 "noheader" => ["ELEMENT"],
840 "charset" => ["ELEMENT"],
841 "length_is" => ["ELEMENT"],
844 #####################################################################
845 # check for unknown properties
846 sub ValidProperties($$)
848 my ($e,$t) = @_;
850 return unless defined $e->{PROPERTIES};
852 foreach my $key (keys %{$e->{PROPERTIES}}) {
853 warning($e, el_name($e) . ": unknown property '$key'")
854 unless defined($property_list{$key});
856 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
857 unless grep($t, @{$property_list{$key}});
861 sub mapToScalar($)
863 my $t = shift;
864 my $ti = getType($t);
866 if (not defined ($ti)) {
867 return undef;
868 } elsif ($ti->{DATA}->{TYPE} eq "ENUM") {
869 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
870 } elsif ($ti->{DATA}->{TYPE} eq "BITMAP") {
871 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
872 } elsif ($ti->{DATA}->{TYPE} eq "SCALAR") {
873 return $t;
876 return undef;
879 #####################################################################
880 # validate an element
881 sub ValidElement($)
883 my $e = shift;
885 ValidProperties($e,"ELEMENT");
887 # Check whether switches are used correctly.
888 if (my $switch = has_property($e, "switch_is")) {
889 my $e2 = find_sibling($e, $switch);
890 my $type = getType($e->{TYPE});
892 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
893 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
896 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
897 my $discriminator_type = has_property($type->{DATA}, "switch_type");
898 $discriminator_type = "uint32" unless defined ($discriminator_type);
900 my $t1 = mapToScalar($discriminator_type);
902 if (not defined($t1)) {
903 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
906 my $t2 = mapToScalar($e2->{TYPE});
907 if (not defined($t2)) {
908 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
911 if ($t1 ne $t2) {
912 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
917 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
918 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
921 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
922 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
925 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
926 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
929 if (has_property($e, "represent_as") and has_property($e, "value")) {
930 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
933 if (has_property($e, "subcontext")) {
934 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
937 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
938 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
941 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
942 fatal($e, el_name($e) . " : compression() on non-subcontext element");
945 if (!$e->{POINTERS} && (
946 has_property($e, "ptr") or
947 has_property($e, "unique") or
948 has_property($e, "relative") or
949 has_property($e, "ref"))) {
950 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
954 #####################################################################
955 # validate an enum
956 sub ValidEnum($)
958 my ($enum) = @_;
960 ValidProperties($enum, "ENUM");
963 #####################################################################
964 # validate a bitmap
965 sub ValidBitmap($)
967 my ($bitmap) = @_;
969 ValidProperties($bitmap, "BITMAP");
972 #####################################################################
973 # validate a struct
974 sub ValidStruct($)
976 my($struct) = shift;
978 ValidProperties($struct, "STRUCT");
980 foreach my $e (@{$struct->{ELEMENTS}}) {
981 $e->{PARENT} = $struct;
982 ValidElement($e);
986 #####################################################################
987 # parse a union
988 sub ValidUnion($)
990 my($union) = shift;
992 ValidProperties($union,"UNION");
994 if (has_property($union->{PARENT}, "nodiscriminant") and has_property($union->{PARENT}, "switch_type")) {
995 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type() on union without discriminant");
998 foreach my $e (@{$union->{ELEMENTS}}) {
999 $e->{PARENT} = $union;
1001 if (defined($e->{PROPERTIES}->{default}) and
1002 defined($e->{PROPERTIES}->{case})) {
1003 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1006 unless (defined ($e->{PROPERTIES}->{default}) or
1007 defined ($e->{PROPERTIES}->{case})) {
1008 fatal($e, "Union member $e->{NAME} must have default or case property");
1011 if (has_property($e, "ref")) {
1012 fatal($e, el_name($e) . " : embedded ref pointers are not supported yet\n");
1016 ValidElement($e);
1020 #####################################################################
1021 # parse a typedef
1022 sub ValidTypedef($)
1024 my($typedef) = shift;
1025 my $data = $typedef->{DATA};
1027 ValidProperties($typedef, "TYPEDEF");
1029 $data->{PARENT} = $typedef;
1031 ValidType($data) if (ref($data) eq "HASH");
1034 #####################################################################
1035 # validate a function
1036 sub ValidFunction($)
1038 my($fn) = shift;
1040 ValidProperties($fn,"FUNCTION");
1042 foreach my $e (@{$fn->{ELEMENTS}}) {
1043 $e->{PARENT} = $fn;
1044 if (has_property($e, "ref") && !$e->{POINTERS}) {
1045 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1047 ValidElement($e);
1051 #####################################################################
1052 # validate a type
1053 sub ValidType($)
1055 my ($t) = @_;
1058 TYPEDEF => \&ValidTypedef,
1059 STRUCT => \&ValidStruct,
1060 UNION => \&ValidUnion,
1061 ENUM => \&ValidEnum,
1062 BITMAP => \&ValidBitmap
1063 }->{$t->{TYPE}}->($t);
1066 #####################################################################
1067 # parse the interface definitions
1068 sub ValidInterface($)
1070 my($interface) = shift;
1071 my($data) = $interface->{DATA};
1073 if (has_property($interface, "helper")) {
1074 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1077 ValidProperties($interface,"INTERFACE");
1079 if (has_property($interface, "pointer_default")) {
1080 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1081 ("ref", "unique", "ptr"))) {
1082 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1086 if (has_property($interface, "object")) {
1087 if (has_property($interface, "version") &&
1088 $interface->{PROPERTIES}->{version} != 0) {
1089 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1092 if (!defined($interface->{BASE}) &&
1093 not ($interface->{NAME} eq "IUnknown")) {
1094 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1098 foreach my $d (@{$data}) {
1099 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1100 ($d->{TYPE} eq "TYPEDEF" or
1101 $d->{TYPE} eq "STRUCT" or
1102 $d->{TYPE} eq "UNION" or
1103 $d->{TYPE} eq "ENUM" or
1104 $d->{TYPE} eq "BITMAP") && ValidType($d);
1109 #####################################################################
1110 # Validate an IDL structure
1111 sub Validate($)
1113 my($idl) = shift;
1115 foreach my $x (@{$idl}) {
1116 ($x->{TYPE} eq "INTERFACE") &&
1117 ValidInterface($x);
1118 ($x->{TYPE} eq "IMPORTLIB") &&
1119 warning($x, "importlib() not supported");