r21572: More work towards supporting tagged types.
[Samba/ekacnet.git] / source / pidl / lib / Parse / Pidl / NDR.pm
blob3e45bca1adc793ff8b7f3224c79d4cc066fe35f5
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);
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) = @_;
340 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
341 return $scalar_alignment->{$e->{NAME}};
344 unless (hasType($e)) {
345 # it must be an external type - all we can do is guess
346 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
347 return 4;
350 my $dt = getType($e);
352 if ($dt->{TYPE} eq "TYPEDEF") {
353 return align_type($dt->{DATA});
354 } elsif ($dt->{TYPE} eq "ENUM") {
355 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
356 } elsif ($dt->{TYPE} eq "BITMAP") {
357 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
358 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
359 return find_largest_alignment($dt);
362 die("Unknown data type type $dt->{TYPE}");
365 sub ParseElement($$)
367 my ($e, $pointer_default) = @_;
369 $e->{TYPE} = expandAlias($e->{TYPE});
371 if (ref($e->{TYPE}) eq "HASH") {
372 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default);
375 return {
376 NAME => $e->{NAME},
377 TYPE => $e->{TYPE},
378 PROPERTIES => $e->{PROPERTIES},
379 LEVELS => GetElementLevelTable($e),
380 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
381 ALIGN => align_type($e->{TYPE}),
382 ORIGINAL => $e
386 sub ParseStruct($$)
388 my ($struct, $pointer_default) = @_;
389 my @elements = ();
390 my $surrounding = undef;
392 foreach my $x (@{$struct->{ELEMENTS}})
394 my $e = ParseElement($x, $pointer_default);
395 if ($x != $struct->{ELEMENTS}[-1] and
396 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
397 fatal($x, "conformant member not at end of struct");
399 push @elements, $e;
402 my $e = $elements[-1];
403 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
404 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
405 $surrounding = $e;
408 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
409 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
410 $surrounding = $struct->{ELEMENTS}[-1];
413 my $align = undef;
414 if ($struct->{NAME}) {
415 $align = align_type($struct->{NAME});
418 return {
419 TYPE => "STRUCT",
420 NAME => $struct->{NAME},
421 SURROUNDING_ELEMENT => $surrounding,
422 ELEMENTS => \@elements,
423 PROPERTIES => $struct->{PROPERTIES},
424 ORIGINAL => $struct,
425 ALIGN => $align
429 sub ParseUnion($$)
431 my ($e, $pointer_default) = @_;
432 my @elements = ();
433 my $switch_type = has_property($e, "switch_type");
434 unless (defined($switch_type)) { $switch_type = "uint32"; }
436 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
438 my $hasdefault = 0;
439 foreach my $x (@{$e->{ELEMENTS}})
441 my $t;
442 if ($x->{TYPE} eq "EMPTY") {
443 $t = { TYPE => "EMPTY" };
444 } else {
445 $t = ParseElement($x, $pointer_default);
447 if (has_property($x, "default")) {
448 $t->{CASE} = "default";
449 $hasdefault = 1;
450 } elsif (defined($x->{PROPERTIES}->{case})) {
451 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
452 } else {
453 die("Union element $x->{NAME} has neither default nor case property");
455 push @elements, $t;
458 return {
459 TYPE => "UNION",
460 NAME => $e->{NAME},
461 SWITCH_TYPE => $switch_type,
462 ELEMENTS => \@elements,
463 PROPERTIES => $e->{PROPERTIES},
464 HAS_DEFAULT => $hasdefault,
465 ORIGINAL => $e
469 sub ParseEnum($$)
471 my ($e, $pointer_default) = @_;
473 return {
474 TYPE => "ENUM",
475 NAME => $e->{NAME},
476 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
477 ELEMENTS => $e->{ELEMENTS},
478 PROPERTIES => $e->{PROPERTIES},
479 ORIGINAL => $e
483 sub ParseBitmap($$)
485 my ($e, $pointer_default) = @_;
487 return {
488 TYPE => "BITMAP",
489 NAME => $e->{NAME},
490 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
491 ELEMENTS => $e->{ELEMENTS},
492 PROPERTIES => $e->{PROPERTIES},
493 ORIGINAL => $e
497 sub ParseType($$)
499 my ($d, $pointer_default) = @_;
501 if ($d->{TYPE} eq "STRUCT" or $d->{TYPE} eq "UNION") {
502 CheckPointerTypes($d, $pointer_default);
505 my $data = {
506 STRUCT => \&ParseStruct,
507 UNION => \&ParseUnion,
508 ENUM => \&ParseEnum,
509 BITMAP => \&ParseBitmap,
510 TYPEDEF => \&ParseTypedef,
511 }->{$d->{TYPE}}->($d, $pointer_default);
513 return $data;
516 sub ParseTypedef($$)
518 my ($d, $pointer_default) = @_;
520 if (defined($d->{DATA}->{PROPERTIES}) && !defined($d->{PROPERTIES})) {
521 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
524 my $data = ParseType($d->{DATA}, $pointer_default);
525 $data->{ALIGN} = align_type($d->{NAME});
527 return {
528 NAME => $d->{NAME},
529 TYPE => $d->{TYPE},
530 PROPERTIES => $d->{PROPERTIES},
531 DATA => $data,
532 ORIGINAL => $d
536 sub ParseConst($$)
538 my ($ndr,$d) = @_;
540 return $d;
543 sub ParseFunction($$$)
545 my ($ndr,$d,$opnum) = @_;
546 my @elements = ();
547 my $rettype = undef;
548 my $thisopnum = undef;
550 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default_top});
552 if (not defined($d->{PROPERTIES}{noopnum})) {
553 $thisopnum = ${$opnum};
554 ${$opnum}++;
557 foreach my $x (@{$d->{ELEMENTS}}) {
558 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default});
559 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
560 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
562 push (@elements, $e);
565 if ($d->{RETURN_TYPE} ne "void") {
566 $rettype = expandAlias($d->{RETURN_TYPE});
569 my $async = 0;
570 if (has_property($d, "async")) { $async = 1; }
572 return {
573 NAME => $d->{NAME},
574 TYPE => "FUNCTION",
575 OPNUM => $thisopnum,
576 ASYNC => $async,
577 RETURN_TYPE => $rettype,
578 PROPERTIES => $d->{PROPERTIES},
579 ELEMENTS => \@elements,
580 ORIGINAL => $d
584 sub CheckPointerTypes($$)
586 my ($s,$default) = @_;
588 foreach my $e (@{$s->{ELEMENTS}}) {
589 if ($e->{POINTERS} and not defined(pointer_type($e))) {
590 $e->{PROPERTIES}->{$default} = 1;
595 sub FindNestedTypes($$)
597 sub FindNestedTypes($$);
598 my ($l, $t) = @_;
600 return if not defined($t->{ELEMENTS});
602 foreach (@{$t->{ELEMENTS}}) {
603 if (ref($_->{TYPE}) eq "HASH") {
604 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
605 FindNestedTypes($l, $_->{TYPE});
610 sub ParseInterface($)
612 my $idl = shift;
613 my @types = ();
614 my @consts = ();
615 my @functions = ();
616 my @endpoints;
617 my @declares = ();
618 my $opnum = 0;
619 my $version;
621 if (not has_property($idl, "pointer_default")) {
622 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
623 # and "unique" in Microsoft Extensions mode (default)
624 $idl->{PROPERTIES}->{pointer_default} = "unique";
627 if (not has_property($idl, "pointer_default_top")) {
628 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
629 } else {
630 warning($idl, "pointer_default_top() is a pidl extension and should not be used");
633 foreach my $d (@{$idl->{DATA}}) {
634 if ($d->{TYPE} eq "DECLARE") {
635 push (@declares, $d);
636 } elsif ($d->{TYPE} eq "FUNCTION") {
637 push (@functions, ParseFunction($idl, $d, \$opnum));
638 } elsif ($d->{TYPE} eq "CONST") {
639 push (@consts, ParseConst($idl, $d));
640 } else {
641 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}));
642 FindNestedTypes(\@types, $d);
646 $version = "0.0";
648 if(defined $idl->{PROPERTIES}->{version}) {
649 $version = $idl->{PROPERTIES}->{version};
652 # If no endpoint is set, default to the interface name as a named pipe
653 if (!defined $idl->{PROPERTIES}->{endpoint}) {
654 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
655 } else {
656 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
659 return {
660 NAME => $idl->{NAME},
661 UUID => lc(has_property($idl, "uuid")),
662 VERSION => $version,
663 TYPE => "INTERFACE",
664 PROPERTIES => $idl->{PROPERTIES},
665 FUNCTIONS => \@functions,
666 CONSTS => \@consts,
667 TYPES => \@types,
668 DECLARES => \@declares,
669 ENDPOINTS => \@endpoints
673 # Convert a IDL tree to a NDR tree
674 # Gives a result tree describing all that's necessary for easily generating
675 # NDR parsers / generators
676 sub Parse($)
678 my $idl = shift;
680 return undef unless (defined($idl));
682 Parse::Pidl::NDR::Validate($idl);
684 my @ndr = ();
686 foreach (@{$idl}) {
687 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
688 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
691 return \@ndr;
694 sub GetNextLevel($$)
696 my $e = shift;
697 my $fl = shift;
699 my $seen = 0;
701 foreach my $l (@{$e->{LEVELS}}) {
702 return $l if ($seen);
703 ($seen = 1) if ($l == $fl);
706 return undef;
709 sub GetPrevLevel($$)
711 my ($e,$fl) = @_;
712 my $prev = undef;
714 foreach my $l (@{$e->{LEVELS}}) {
715 (return $prev) if ($l == $fl);
716 $prev = $l;
719 return undef;
722 sub ContainsString($)
724 my ($e) = @_;
726 foreach my $l (@{$e->{LEVELS}}) {
727 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
730 return 0;
733 sub ContainsDeferred($$)
735 my ($e,$l) = @_;
737 return 1 if ($l->{CONTAINS_DEFERRED});
739 while ($l = GetNextLevel($e,$l))
741 return 1 if ($l->{IS_DEFERRED});
742 return 1 if ($l->{CONTAINS_DEFERRED});
745 return 0;
748 sub el_name($)
750 my $e = shift;
752 if ($e->{PARENT} && $e->{PARENT}->{NAME}) {
753 return "$e->{PARENT}->{NAME}.$e->{NAME}";
756 if ($e->{PARENT} && $e->{PARENT}->{PARENT}->{NAME}) {
757 return "$e->{PARENT}->{PARENT}->{NAME}.$e->{NAME}";
760 if ($e->{PARENT}) {
761 return "$e->{PARENT}->{NAME}.$e->{NAME}";
764 return $e->{NAME};
767 ###################################
768 # find a sibling var in a structure
769 sub find_sibling($$)
771 my($e,$name) = @_;
772 my($fn) = $e->{PARENT};
774 if ($name =~ /\*(.*)/) {
775 $name = $1;
778 for my $e2 (@{$fn->{ELEMENTS}}) {
779 return $e2 if ($e2->{NAME} eq $name);
782 return undef;
785 my %property_list = (
786 # interface
787 "helpstring" => ["INTERFACE", "FUNCTION"],
788 "version" => ["INTERFACE"],
789 "uuid" => ["INTERFACE"],
790 "endpoint" => ["INTERFACE"],
791 "pointer_default" => ["INTERFACE"],
792 "pointer_default_top" => ["INTERFACE"],
793 "helper" => ["INTERFACE"],
794 "authservice" => ["INTERFACE"],
796 # dcom
797 "object" => ["INTERFACE"],
798 "local" => ["INTERFACE", "FUNCTION"],
799 "iid_is" => ["ELEMENT"],
800 "call_as" => ["FUNCTION"],
801 "idempotent" => ["FUNCTION"],
803 # function
804 "noopnum" => ["FUNCTION"],
805 "in" => ["ELEMENT"],
806 "out" => ["ELEMENT"],
807 "async" => ["FUNCTION"],
809 # pointer
810 "ref" => ["ELEMENT"],
811 "ptr" => ["ELEMENT"],
812 "unique" => ["ELEMENT"],
813 "ignore" => ["ELEMENT"],
814 "relative" => ["ELEMENT"],
815 "relative_base" => ["TYPEDEF"],
817 "gensize" => ["TYPEDEF"],
818 "value" => ["ELEMENT"],
819 "flag" => ["ELEMENT", "TYPEDEF"],
821 # generic
822 "public" => ["FUNCTION", "TYPEDEF"],
823 "nopush" => ["FUNCTION", "TYPEDEF"],
824 "nopull" => ["FUNCTION", "TYPEDEF"],
825 "nosize" => ["FUNCTION", "TYPEDEF"],
826 "noprint" => ["FUNCTION", "TYPEDEF"],
827 "noejs" => ["FUNCTION", "TYPEDEF"],
829 # union
830 "switch_is" => ["ELEMENT"],
831 "switch_type" => ["ELEMENT", "TYPEDEF"],
832 "nodiscriminant" => ["TYPEDEF"],
833 "case" => ["ELEMENT"],
834 "default" => ["ELEMENT"],
836 "represent_as" => ["ELEMENT"],
837 "transmit_as" => ["ELEMENT"],
839 # subcontext
840 "subcontext" => ["ELEMENT"],
841 "subcontext_size" => ["ELEMENT"],
842 "compression" => ["ELEMENT"],
844 # enum
845 "enum8bit" => ["TYPEDEF"],
846 "enum16bit" => ["TYPEDEF"],
847 "v1_enum" => ["TYPEDEF"],
849 # bitmap
850 "bitmap8bit" => ["TYPEDEF"],
851 "bitmap16bit" => ["TYPEDEF"],
852 "bitmap32bit" => ["TYPEDEF"],
853 "bitmap64bit" => ["TYPEDEF"],
855 # array
856 "range" => ["ELEMENT"],
857 "size_is" => ["ELEMENT"],
858 "string" => ["ELEMENT"],
859 "noheader" => ["ELEMENT"],
860 "charset" => ["ELEMENT"],
861 "length_is" => ["ELEMENT"],
864 #####################################################################
865 # check for unknown properties
866 sub ValidProperties($$)
868 my ($e,$t) = @_;
870 return unless defined $e->{PROPERTIES};
872 foreach my $key (keys %{$e->{PROPERTIES}}) {
873 warning($e, el_name($e) . ": unknown property '$key'")
874 unless defined($property_list{$key});
876 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
877 unless grep($t, @{$property_list{$key}});
881 sub mapToScalar($)
883 my $t = shift;
884 my $ti = getType($t);
886 if (not defined ($ti)) {
887 return undef;
888 } elsif ($ti->{DATA}->{TYPE} eq "ENUM") {
889 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
890 } elsif ($ti->{DATA}->{TYPE} eq "BITMAP") {
891 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
892 } elsif ($ti->{DATA}->{TYPE} eq "SCALAR") {
893 return $t;
896 return undef;
899 #####################################################################
900 # validate an element
901 sub ValidElement($)
903 my $e = shift;
905 ValidProperties($e,"ELEMENT");
907 # Check whether switches are used correctly.
908 if (my $switch = has_property($e, "switch_is")) {
909 my $e2 = find_sibling($e, $switch);
910 my $type = getType($e->{TYPE});
912 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
913 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
916 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
917 my $discriminator_type = has_property($type->{DATA}, "switch_type");
918 $discriminator_type = "uint32" unless defined ($discriminator_type);
920 my $t1 = mapToScalar($discriminator_type);
922 if (not defined($t1)) {
923 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
926 my $t2 = mapToScalar($e2->{TYPE});
927 if (not defined($t2)) {
928 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
931 if ($t1 ne $t2) {
932 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
937 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
938 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
941 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
942 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
945 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
946 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
949 if (has_property($e, "represent_as") and has_property($e, "value")) {
950 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
953 if (has_property($e, "subcontext")) {
954 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
957 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
958 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
961 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
962 fatal($e, el_name($e) . " : compression() on non-subcontext element");
965 if (!$e->{POINTERS} && (
966 has_property($e, "ptr") or
967 has_property($e, "unique") or
968 has_property($e, "relative") or
969 has_property($e, "ref"))) {
970 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
974 #####################################################################
975 # validate an enum
976 sub ValidEnum($)
978 my ($enum) = @_;
980 ValidProperties($enum, "ENUM");
983 #####################################################################
984 # validate a bitmap
985 sub ValidBitmap($)
987 my ($bitmap) = @_;
989 ValidProperties($bitmap, "BITMAP");
992 #####################################################################
993 # validate a struct
994 sub ValidStruct($)
996 my($struct) = shift;
998 ValidProperties($struct, "STRUCT");
1000 foreach my $e (@{$struct->{ELEMENTS}}) {
1001 $e->{PARENT} = $struct;
1002 ValidElement($e);
1006 #####################################################################
1007 # parse a union
1008 sub ValidUnion($)
1010 my($union) = shift;
1012 ValidProperties($union,"UNION");
1014 if (has_property($union->{PARENT}, "nodiscriminant") and has_property($union->{PARENT}, "switch_type")) {
1015 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type() on union without discriminant");
1018 foreach my $e (@{$union->{ELEMENTS}}) {
1019 $e->{PARENT} = $union;
1021 if (defined($e->{PROPERTIES}->{default}) and
1022 defined($e->{PROPERTIES}->{case})) {
1023 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1026 unless (defined ($e->{PROPERTIES}->{default}) or
1027 defined ($e->{PROPERTIES}->{case})) {
1028 fatal($e, "Union member $e->{NAME} must have default or case property");
1031 if (has_property($e, "ref")) {
1032 fatal($e, el_name($e) . " : embedded ref pointers are not supported yet\n");
1036 ValidElement($e);
1040 #####################################################################
1041 # parse a typedef
1042 sub ValidTypedef($)
1044 my($typedef) = shift;
1045 my $data = $typedef->{DATA};
1047 ValidProperties($typedef, "TYPEDEF");
1049 $data->{PARENT} = $typedef;
1051 ValidType($data) if (ref($data) eq "HASH");
1054 #####################################################################
1055 # validate a function
1056 sub ValidFunction($)
1058 my($fn) = shift;
1060 ValidProperties($fn,"FUNCTION");
1062 foreach my $e (@{$fn->{ELEMENTS}}) {
1063 $e->{PARENT} = $fn;
1064 if (has_property($e, "ref") && !$e->{POINTERS}) {
1065 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1067 ValidElement($e);
1071 #####################################################################
1072 # validate a type
1073 sub ValidType($)
1075 my ($t) = @_;
1078 TYPEDEF => \&ValidTypedef,
1079 STRUCT => \&ValidStruct,
1080 UNION => \&ValidUnion,
1081 ENUM => \&ValidEnum,
1082 BITMAP => \&ValidBitmap
1083 }->{$t->{TYPE}}->($t);
1086 #####################################################################
1087 # parse the interface definitions
1088 sub ValidInterface($)
1090 my($interface) = shift;
1091 my($data) = $interface->{DATA};
1093 if (has_property($interface, "helper")) {
1094 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1097 ValidProperties($interface,"INTERFACE");
1099 if (has_property($interface, "pointer_default")) {
1100 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1101 ("ref", "unique", "ptr"))) {
1102 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1106 if (has_property($interface, "object")) {
1107 if (has_property($interface, "version") &&
1108 $interface->{PROPERTIES}->{version} != 0) {
1109 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1112 if (!defined($interface->{BASE}) &&
1113 not ($interface->{NAME} eq "IUnknown")) {
1114 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1118 foreach my $d (@{$data}) {
1119 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1120 ($d->{TYPE} eq "TYPEDEF" or
1121 $d->{TYPE} eq "STRUCT" or
1122 $d->{TYPE} eq "UNION" or
1123 $d->{TYPE} eq "ENUM" or
1124 $d->{TYPE} eq "BITMAP") && ValidType($d);
1129 #####################################################################
1130 # Validate an IDL structure
1131 sub Validate($)
1133 my($idl) = shift;
1135 foreach my $x (@{$idl}) {
1136 ($x->{TYPE} eq "INTERFACE") &&
1137 ValidInterface($x);
1138 ($x->{TYPE} eq "IMPORTLIB") &&
1139 warning($x, "importlib() not supported");