r21430: Support tagged types without typedef. This means:
[Samba/ekacnet.git] / source / pidl / lib / Parse / Pidl / NDR.pm
blobd55df7a452f09e492d334b0f775197294d8c3b12
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 CONVERT_TO => has_property($e, ""),
261 CONVERT_FROM => has_property($e, ""),
262 DATA_TYPE => $e->{TYPE},
263 IS_DEFERRED => $is_deferred,
264 CONTAINS_DEFERRED => can_contain_deferred($e),
265 IS_SURROUNDING => 0 #FIXME
268 my $i = 0;
269 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
271 return $order;
274 #####################################################################
275 # see if a type contains any deferred data
276 sub can_contain_deferred
278 my $e = shift;
280 return 0 if (Parse::Pidl::Typelist::is_scalar($e->{TYPE}));
281 return 1 unless (hasType($e->{TYPE})); # assume the worst
283 my $type = getType($e->{TYPE});
285 foreach my $x (@{$type->{DATA}->{ELEMENTS}}) {
286 return 1 if ($x->{POINTERS});
287 return 1 if (can_contain_deferred ($x));
290 return 0;
293 sub pointer_type($)
295 my $e = shift;
297 return undef unless $e->{POINTERS};
299 return "ref" if (has_property($e, "ref"));
300 return "full" if (has_property($e, "ptr"));
301 return "sptr" if (has_property($e, "sptr"));
302 return "unique" if (has_property($e, "unique"));
303 return "relative" if (has_property($e, "relative"));
304 return "ignore" if (has_property($e, "ignore"));
306 return undef;
309 #####################################################################
310 # work out the correct alignment for a structure or union
311 sub find_largest_alignment($)
313 my $s = shift;
315 my $align = 1;
316 for my $e (@{$s->{ELEMENTS}}) {
317 my $a = 1;
319 if ($e->{POINTERS}) {
320 $a = 4;
321 } elsif (has_property($e, "subcontext")) {
322 $a = 1;
323 } elsif (has_property($e, "transmit_as")) {
324 $a = align_type($e->{PROPERTIES}->{transmit_as});
325 } else {
326 $a = align_type($e->{TYPE});
329 $align = $a if ($align < $a);
332 return $align;
335 #####################################################################
336 # align a type
337 sub align_type($)
339 sub align_type($);
340 my $e = shift;
342 unless (hasType($e)) {
343 # it must be an external type - all we can do is guess
344 # print "Warning: assuming alignment of unknown type '$e' is 4\n";
345 return 4;
348 my $dt = getType($e)->{DATA};
350 if ($dt->{TYPE} eq "ENUM") {
351 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
352 } elsif ($dt->{TYPE} eq "BITMAP") {
353 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
354 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
355 return find_largest_alignment($dt);
356 } elsif ($dt->{TYPE} eq "SCALAR") {
357 return $scalar_alignment->{$dt->{NAME}};
360 die("Unknown data type type $dt->{TYPE}");
363 sub ParseElement($)
365 my $e = shift;
367 $e->{TYPE} = expandAlias($e->{TYPE});
369 return {
370 NAME => $e->{NAME},
371 TYPE => $e->{TYPE},
372 PROPERTIES => $e->{PROPERTIES},
373 LEVELS => GetElementLevelTable($e),
374 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
375 ALIGN => align_type($e->{TYPE}),
376 ORIGINAL => $e
380 sub ParseStruct($$)
382 my ($ndr,$struct) = @_;
383 my @elements = ();
384 my $surrounding = undef;
387 foreach my $x (@{$struct->{ELEMENTS}})
389 my $e = ParseElement($x);
390 if ($x != $struct->{ELEMENTS}[-1] and
391 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
392 fatal($x, "conformant member not at end of struct");
394 push @elements, $e;
397 my $e = $elements[-1];
398 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
399 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
400 $surrounding = $e;
403 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
404 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
405 $surrounding = $struct->{ELEMENTS}[-1];
408 my $align = undef;
409 if ($struct->{NAME}) {
410 $align = align_type($struct->{NAME});
413 return {
414 TYPE => "STRUCT",
415 NAME => $struct->{NAME},
416 SURROUNDING_ELEMENT => $surrounding,
417 ELEMENTS => \@elements,
418 PROPERTIES => $struct->{PROPERTIES},
419 ORIGINAL => $struct,
420 ALIGN => $align
424 sub ParseUnion($$)
426 my ($ndr,$e) = @_;
427 my @elements = ();
428 my $switch_type = has_property($e, "switch_type");
429 unless (defined($switch_type)) { $switch_type = "uint32"; }
431 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
433 my $hasdefault = 0;
434 foreach my $x (@{$e->{ELEMENTS}})
436 my $t;
437 if ($x->{TYPE} eq "EMPTY") {
438 $t = { TYPE => "EMPTY" };
439 } else {
440 $t = ParseElement($x);
442 if (has_property($x, "default")) {
443 $t->{CASE} = "default";
444 $hasdefault = 1;
445 } elsif (defined($x->{PROPERTIES}->{case})) {
446 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
447 } else {
448 die("Union element $x->{NAME} has neither default nor case property");
450 push @elements, $t;
453 return {
454 TYPE => "UNION",
455 NAME => $e->{NAME},
456 SWITCH_TYPE => $switch_type,
457 ELEMENTS => \@elements,
458 PROPERTIES => $e->{PROPERTIES},
459 HAS_DEFAULT => $hasdefault,
460 ORIGINAL => $e
464 sub ParseEnum($$)
466 my ($ndr,$e) = @_;
468 return {
469 TYPE => "ENUM",
470 NAME => $e->{NAME},
471 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
472 ELEMENTS => $e->{ELEMENTS},
473 PROPERTIES => $e->{PROPERTIES},
474 ORIGINAL => $e
478 sub ParseBitmap($$)
480 my ($ndr,$e) = @_;
482 return {
483 TYPE => "BITMAP",
484 NAME => $e->{NAME},
485 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
486 ELEMENTS => $e->{ELEMENTS},
487 PROPERTIES => $e->{PROPERTIES},
488 ORIGINAL => $e
492 sub ParseType($$)
494 my ($ndr, $d) = @_;
496 if ($d->{TYPE} eq "STRUCT" or $d->{TYPE} eq "UNION") {
497 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default});
500 my $data = {
501 STRUCT => \&ParseStruct,
502 UNION => \&ParseUnion,
503 ENUM => \&ParseEnum,
504 BITMAP => \&ParseBitmap,
505 TYPEDEF => \&ParseTypedef,
506 }->{$d->{TYPE}}->($ndr, $d);
508 return $data;
511 sub ParseTypedef($$)
513 my ($ndr,$d) = @_;
515 if (defined($d->{DATA}->{PROPERTIES}) && !defined($d->{PROPERTIES})) {
516 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
519 my $data = ParseType($ndr, $d->{DATA});
520 $data->{ALIGN} = align_type($d->{NAME});
522 return {
523 NAME => $d->{NAME},
524 TYPE => $d->{TYPE},
525 PROPERTIES => $d->{PROPERTIES},
526 DATA => $data,
527 ORIGINAL => $d
531 sub ParseConst($$)
533 my ($ndr,$d) = @_;
535 return $d;
538 sub ParseFunction($$$)
540 my ($ndr,$d,$opnum) = @_;
541 my @elements = ();
542 my $rettype = undef;
543 my $thisopnum = undef;
545 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default_top});
547 if (not defined($d->{PROPERTIES}{noopnum})) {
548 $thisopnum = ${$opnum};
549 ${$opnum}++;
552 foreach my $x (@{$d->{ELEMENTS}}) {
553 my $e = ParseElement($x);
554 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
555 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
557 push (@elements, $e);
560 if ($d->{RETURN_TYPE} ne "void") {
561 $rettype = expandAlias($d->{RETURN_TYPE});
564 my $async = 0;
565 if (has_property($d, "async")) { $async = 1; }
567 return {
568 NAME => $d->{NAME},
569 TYPE => "FUNCTION",
570 OPNUM => $thisopnum,
571 ASYNC => $async,
572 RETURN_TYPE => $rettype,
573 PROPERTIES => $d->{PROPERTIES},
574 ELEMENTS => \@elements,
575 ORIGINAL => $d
579 sub CheckPointerTypes($$)
581 my ($s,$default) = @_;
583 foreach my $e (@{$s->{ELEMENTS}}) {
584 if ($e->{POINTERS} and not defined(pointer_type($e))) {
585 $e->{PROPERTIES}->{$default} = 1;
590 sub ParseInterface($)
592 my $idl = shift;
593 my @types = ();
594 my @consts = ();
595 my @functions = ();
596 my @endpoints;
597 my @declares = ();
598 my $opnum = 0;
599 my $version;
601 if (not has_property($idl, "pointer_default")) {
602 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
603 # and "unique" in Microsoft Extensions mode (default)
604 $idl->{PROPERTIES}->{pointer_default} = "unique";
607 if (not has_property($idl, "pointer_default_top")) {
608 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
609 } else {
610 warning($idl, "pointer_default_top() is a pidl extension and should not be used");
613 foreach my $d (@{$idl->{DATA}}) {
614 if ($d->{TYPE} eq "DECLARE") {
615 push (@declares, $d);
616 } elsif ($d->{TYPE} eq "FUNCTION") {
617 push (@functions, ParseFunction($idl, $d, \$opnum));
618 } elsif ($d->{TYPE} eq "CONST") {
619 push (@consts, ParseConst($idl, $d));
620 } else {
621 push (@types, ParseType($idl, $d));
625 $version = "0.0";
627 if(defined $idl->{PROPERTIES}->{version}) {
628 $version = $idl->{PROPERTIES}->{version};
631 # If no endpoint is set, default to the interface name as a named pipe
632 if (!defined $idl->{PROPERTIES}->{endpoint}) {
633 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
634 } else {
635 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
638 return {
639 NAME => $idl->{NAME},
640 UUID => lc(has_property($idl, "uuid")),
641 VERSION => $version,
642 TYPE => "INTERFACE",
643 PROPERTIES => $idl->{PROPERTIES},
644 FUNCTIONS => \@functions,
645 CONSTS => \@consts,
646 TYPES => \@types,
647 DECLARES => \@declares,
648 ENDPOINTS => \@endpoints
652 # Convert a IDL tree to a NDR tree
653 # Gives a result tree describing all that's necessary for easily generating
654 # NDR parsers / generators
655 sub Parse($)
657 my $idl = shift;
659 return undef unless (defined($idl));
661 Parse::Pidl::NDR::Validate($idl);
663 my @ndr = ();
665 foreach (@{$idl}) {
666 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
667 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
670 return \@ndr;
673 sub GetNextLevel($$)
675 my $e = shift;
676 my $fl = shift;
678 my $seen = 0;
680 foreach my $l (@{$e->{LEVELS}}) {
681 return $l if ($seen);
682 ($seen = 1) if ($l == $fl);
685 return undef;
688 sub GetPrevLevel($$)
690 my ($e,$fl) = @_;
691 my $prev = undef;
693 foreach my $l (@{$e->{LEVELS}}) {
694 (return $prev) if ($l == $fl);
695 $prev = $l;
698 return undef;
701 sub ContainsString($)
703 my ($e) = @_;
705 foreach my $l (@{$e->{LEVELS}}) {
706 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
709 return 0;
712 sub ContainsDeferred($$)
714 my ($e,$l) = @_;
716 return 1 if ($l->{CONTAINS_DEFERRED});
718 while ($l = GetNextLevel($e,$l))
720 return 1 if ($l->{IS_DEFERRED});
721 return 1 if ($l->{CONTAINS_DEFERRED});
724 return 0;
727 sub el_name($)
729 my $e = shift;
731 if ($e->{PARENT} && $e->{PARENT}->{NAME}) {
732 return "$e->{PARENT}->{NAME}.$e->{NAME}";
735 if ($e->{PARENT} && $e->{PARENT}->{PARENT}->{NAME}) {
736 return "$e->{PARENT}->{PARENT}->{NAME}.$e->{NAME}";
739 if ($e->{PARENT}) {
740 return "$e->{PARENT}->{NAME}.$e->{NAME}";
743 return $e->{NAME};
746 ###################################
747 # find a sibling var in a structure
748 sub find_sibling($$)
750 my($e,$name) = @_;
751 my($fn) = $e->{PARENT};
753 if ($name =~ /\*(.*)/) {
754 $name = $1;
757 for my $e2 (@{$fn->{ELEMENTS}}) {
758 return $e2 if ($e2->{NAME} eq $name);
761 return undef;
764 my %property_list = (
765 # interface
766 "helpstring" => ["INTERFACE", "FUNCTION"],
767 "version" => ["INTERFACE"],
768 "uuid" => ["INTERFACE"],
769 "endpoint" => ["INTERFACE"],
770 "pointer_default" => ["INTERFACE"],
771 "pointer_default_top" => ["INTERFACE"],
772 "helper" => ["INTERFACE"],
773 "authservice" => ["INTERFACE"],
775 # dcom
776 "object" => ["INTERFACE"],
777 "local" => ["INTERFACE", "FUNCTION"],
778 "iid_is" => ["ELEMENT"],
779 "call_as" => ["FUNCTION"],
780 "idempotent" => ["FUNCTION"],
782 # function
783 "noopnum" => ["FUNCTION"],
784 "in" => ["ELEMENT"],
785 "out" => ["ELEMENT"],
786 "async" => ["FUNCTION"],
788 # pointer
789 "ref" => ["ELEMENT"],
790 "ptr" => ["ELEMENT"],
791 "unique" => ["ELEMENT"],
792 "ignore" => ["ELEMENT"],
793 "relative" => ["ELEMENT"],
794 "relative_base" => ["TYPEDEF"],
796 "gensize" => ["TYPEDEF"],
797 "value" => ["ELEMENT"],
798 "flag" => ["ELEMENT", "TYPEDEF"],
800 # generic
801 "public" => ["FUNCTION", "TYPEDEF"],
802 "nopush" => ["FUNCTION", "TYPEDEF"],
803 "nopull" => ["FUNCTION", "TYPEDEF"],
804 "nosize" => ["FUNCTION", "TYPEDEF"],
805 "noprint" => ["FUNCTION", "TYPEDEF"],
806 "noejs" => ["FUNCTION", "TYPEDEF"],
808 # union
809 "switch_is" => ["ELEMENT"],
810 "switch_type" => ["ELEMENT", "TYPEDEF"],
811 "nodiscriminant" => ["TYPEDEF"],
812 "case" => ["ELEMENT"],
813 "default" => ["ELEMENT"],
815 "represent_as" => ["ELEMENT"],
816 "transmit_as" => ["ELEMENT"],
818 # subcontext
819 "subcontext" => ["ELEMENT"],
820 "subcontext_size" => ["ELEMENT"],
821 "compression" => ["ELEMENT"],
823 # enum
824 "enum8bit" => ["TYPEDEF"],
825 "enum16bit" => ["TYPEDEF"],
826 "v1_enum" => ["TYPEDEF"],
828 # bitmap
829 "bitmap8bit" => ["TYPEDEF"],
830 "bitmap16bit" => ["TYPEDEF"],
831 "bitmap32bit" => ["TYPEDEF"],
832 "bitmap64bit" => ["TYPEDEF"],
834 # array
835 "range" => ["ELEMENT"],
836 "size_is" => ["ELEMENT"],
837 "string" => ["ELEMENT"],
838 "noheader" => ["ELEMENT"],
839 "charset" => ["ELEMENT"],
840 "length_is" => ["ELEMENT"],
843 #####################################################################
844 # check for unknown properties
845 sub ValidProperties($$)
847 my ($e,$t) = @_;
849 return unless defined $e->{PROPERTIES};
851 foreach my $key (keys %{$e->{PROPERTIES}}) {
852 warning($e, el_name($e) . ": unknown property '$key'")
853 unless defined($property_list{$key});
855 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
856 unless grep($t, @{$property_list{$key}});
860 sub mapToScalar($)
862 my $t = shift;
863 my $ti = getType($t);
865 if (not defined ($ti)) {
866 return undef;
867 } elsif ($ti->{DATA}->{TYPE} eq "ENUM") {
868 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
869 } elsif ($ti->{DATA}->{TYPE} eq "BITMAP") {
870 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
871 } elsif ($ti->{DATA}->{TYPE} eq "SCALAR") {
872 return $t;
875 return undef;
878 #####################################################################
879 # validate an element
880 sub ValidElement($)
882 my $e = shift;
884 ValidProperties($e,"ELEMENT");
886 # Check whether switches are used correctly.
887 if (my $switch = has_property($e, "switch_is")) {
888 my $e2 = find_sibling($e, $switch);
889 my $type = getType($e->{TYPE});
891 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
892 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
895 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
896 my $discriminator_type = has_property($type->{DATA}, "switch_type");
897 $discriminator_type = "uint32" unless defined ($discriminator_type);
899 my $t1 = mapToScalar($discriminator_type);
901 if (not defined($t1)) {
902 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
905 my $t2 = mapToScalar($e2->{TYPE});
906 if (not defined($t2)) {
907 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
910 if ($t1 ne $t2) {
911 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
916 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
917 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
920 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
921 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
924 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
925 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
928 if (has_property($e, "represent_as") and has_property($e, "value")) {
929 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
932 if (has_property($e, "subcontext")) {
933 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
936 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
937 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
940 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
941 fatal($e, el_name($e) . " : compression() on non-subcontext element");
944 if (!$e->{POINTERS} && (
945 has_property($e, "ptr") or
946 has_property($e, "unique") or
947 has_property($e, "relative") or
948 has_property($e, "ref"))) {
949 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
953 #####################################################################
954 # validate an enum
955 sub ValidEnum($)
957 my ($enum) = @_;
959 ValidProperties($enum, "ENUM");
962 #####################################################################
963 # validate a bitmap
964 sub ValidBitmap($)
966 my ($bitmap) = @_;
968 ValidProperties($bitmap, "BITMAP");
971 #####################################################################
972 # validate a struct
973 sub ValidStruct($)
975 my($struct) = shift;
977 ValidProperties($struct, "STRUCT");
979 foreach my $e (@{$struct->{ELEMENTS}}) {
980 $e->{PARENT} = $struct;
981 ValidElement($e);
985 #####################################################################
986 # parse a union
987 sub ValidUnion($)
989 my($union) = shift;
991 ValidProperties($union,"UNION");
993 if (has_property($union->{PARENT}, "nodiscriminant") and has_property($union->{PARENT}, "switch_type")) {
994 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type() on union without discriminant");
997 foreach my $e (@{$union->{ELEMENTS}}) {
998 $e->{PARENT} = $union;
1000 if (defined($e->{PROPERTIES}->{default}) and
1001 defined($e->{PROPERTIES}->{case})) {
1002 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1005 unless (defined ($e->{PROPERTIES}->{default}) or
1006 defined ($e->{PROPERTIES}->{case})) {
1007 fatal($e, "Union member $e->{NAME} must have default or case property");
1010 if (has_property($e, "ref")) {
1011 fatal($e, el_name($e) . " : embedded ref pointers are not supported yet\n");
1015 ValidElement($e);
1019 #####################################################################
1020 # parse a typedef
1021 sub ValidTypedef($)
1023 my($typedef) = shift;
1024 my $data = $typedef->{DATA};
1026 ValidProperties($typedef, "TYPEDEF");
1028 $data->{PARENT} = $typedef;
1030 ValidType($data) if (ref($data) eq "HASH");
1033 #####################################################################
1034 # validate a function
1035 sub ValidFunction($)
1037 my($fn) = shift;
1039 ValidProperties($fn,"FUNCTION");
1041 foreach my $e (@{$fn->{ELEMENTS}}) {
1042 $e->{PARENT} = $fn;
1043 if (has_property($e, "ref") && !$e->{POINTERS}) {
1044 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1046 ValidElement($e);
1050 #####################################################################
1051 # validate a type
1052 sub ValidType($)
1054 my ($t) = @_;
1057 TYPEDEF => \&ValidTypedef,
1058 STRUCT => \&ValidStruct,
1059 UNION => \&ValidUnion,
1060 ENUM => \&ValidEnum,
1061 BITMAP => \&ValidBitmap
1062 }->{$t->{TYPE}}->($t);
1065 #####################################################################
1066 # parse the interface definitions
1067 sub ValidInterface($)
1069 my($interface) = shift;
1070 my($data) = $interface->{DATA};
1072 if (has_property($interface, "helper")) {
1073 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1076 ValidProperties($interface,"INTERFACE");
1078 if (has_property($interface, "pointer_default")) {
1079 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1080 ("ref", "unique", "ptr"))) {
1081 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1085 if (has_property($interface, "object")) {
1086 if (has_property($interface, "version") &&
1087 $interface->{PROPERTIES}->{version} != 0) {
1088 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1091 if (!defined($interface->{BASE}) &&
1092 not ($interface->{NAME} eq "IUnknown")) {
1093 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1097 foreach my $d (@{$data}) {
1098 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1099 ($d->{TYPE} eq "TYPEDEF" or
1100 $d->{TYPE} eq "STRUCT" or
1101 $d->{TYPE} eq "UNION" or
1102 $d->{TYPE} eq "ENUM" or
1103 $d->{TYPE} eq "BITMAP") && ValidType($d);
1108 #####################################################################
1109 # Validate an IDL structure
1110 sub Validate($)
1112 my($idl) = shift;
1114 foreach my $x (@{$idl}) {
1115 ($x->{TYPE} eq "INTERFACE") &&
1116 ValidInterface($x);
1117 ($x->{TYPE} eq "IMPORTLIB") &&
1118 warning($x, "importlib() not supported");