Add support for double type in pidl.
[Samba/fernandojvsilva.git] / pidl / lib / Parse / Pidl / NDR.pm
blob95cd4b9dc3deeddd033cc72dd76411ecaa5e5175
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 'double' => 8,
57 'pointer' => 8,
58 'dlong' => 4,
59 'udlong' => 4,
60 'udlongr' => 4,
61 'DATA_BLOB' => 4,
62 'string' => 4,
63 'string_array' => 4, #???
64 'time_t' => 4,
65 'NTTIME' => 4,
66 'NTTIME_1sec' => 4,
67 'NTTIME_hyper' => 8,
68 'WERROR' => 4,
69 'NTSTATUS' => 4,
70 'COMRESULT' => 4,
71 'nbt_string' => 4,
72 'wrepl_nbt_name' => 4,
73 'ipv4address' => 4
76 sub GetElementLevelTable($$)
78 my ($e, $pointer_default) = @_;
80 my $order = [];
81 my $is_deferred = 0;
82 my @bracket_array = ();
83 my @length_is = ();
84 my @size_is = ();
85 my $pointer_idx = 0;
87 if (has_property($e, "size_is")) {
88 @size_is = split /,/, has_property($e, "size_is");
91 if (has_property($e, "length_is")) {
92 @length_is = split /,/, has_property($e, "length_is");
95 if (defined($e->{ARRAY_LEN})) {
96 @bracket_array = @{$e->{ARRAY_LEN}};
99 if (has_property($e, "out")) {
100 my $needptrs = 1;
102 if (has_property($e, "string")) { $needptrs++; }
103 if ($#bracket_array >= 0) { $needptrs = 0; }
105 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
108 # Parse the [][][][] style array stuff
109 for my $i (0 .. $#bracket_array) {
110 my $d = $bracket_array[$#bracket_array - $i];
111 my $size = $d;
112 my $length = $d;
113 my $is_surrounding = 0;
114 my $is_varying = 0;
115 my $is_conformant = 0;
116 my $is_string = 0;
117 my $is_fixed = 0;
118 my $is_inline = 0;
120 if ($d eq "*") {
121 $is_conformant = 1;
122 if ($size = shift @size_is) {
123 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
124 $is_string = 1;
125 delete($e->{PROPERTIES}->{string});
126 } else {
127 fatal($e, "Must specify size_is() for conformant array!")
130 if (($length = shift @length_is) or $is_string) {
131 $is_varying = 1;
132 } else {
133 $length = $size;
136 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
137 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
138 $is_surrounding = 1;
142 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
143 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
145 push (@$order, {
146 TYPE => "ARRAY",
147 SIZE_IS => $size,
148 LENGTH_IS => $length,
149 IS_DEFERRED => $is_deferred,
150 IS_SURROUNDING => $is_surrounding,
151 IS_ZERO_TERMINATED => $is_string,
152 IS_VARYING => $is_varying,
153 IS_CONFORMANT => $is_conformant,
154 IS_FIXED => $is_fixed,
155 IS_INLINE => $is_inline
159 # Next, all the pointers
160 foreach my $i (1..$e->{POINTERS}) {
161 my $level = "EMBEDDED";
162 # Top level "ref" pointers do not have a referrent identifier
163 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
165 my $pt;
167 # Only the first level gets the pointer type from the
168 # pointer property, the others get them from
169 # the pointer_default() interface property
171 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
172 # (Here they talk about the rightmost pointer, but testing shows
173 # they mean the leftmost pointer.)
175 # --metze
177 $pt = pointer_type($e);
178 if ($i > 1) {
179 $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
180 $pt = $pointer_default;
183 push (@$order, {
184 TYPE => "POINTER",
185 POINTER_TYPE => $pt,
186 POINTER_INDEX => $pointer_idx,
187 IS_DEFERRED => "$is_deferred",
188 LEVEL => $level
191 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer")
192 if ($i == 1 and $pt ne "ref" and
193 $e->{PARENT}->{TYPE} eq "FUNCTION" and
194 not has_property($e, "in"));
196 $pointer_idx++;
198 # everything that follows will be deferred
199 $is_deferred = 1 if ($level ne "TOP");
201 my $array_size = shift @size_is;
202 my $array_length;
203 my $is_varying;
204 my $is_conformant;
205 my $is_string = 0;
206 if ($array_size) {
207 $is_conformant = 1;
208 if ($array_length = shift @length_is) {
209 $is_varying = 1;
210 } else {
211 $array_length = $array_size;
212 $is_varying =0;
216 if (scalar(@size_is) == 0 and has_property($e, "string") and
217 $i == $e->{POINTERS}) {
218 $is_string = 1;
219 $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
220 delete($e->{PROPERTIES}->{string});
223 if ($array_size or $is_string) {
224 push (@$order, {
225 TYPE => "ARRAY",
226 SIZE_IS => $array_size,
227 LENGTH_IS => $array_length,
228 IS_DEFERRED => $is_deferred,
229 IS_SURROUNDING => 0,
230 IS_ZERO_TERMINATED => $is_string,
231 IS_VARYING => $is_varying,
232 IS_CONFORMANT => $is_conformant,
233 IS_FIXED => 0,
234 IS_INLINE => 0
237 $is_deferred = 0;
241 if (defined(has_property($e, "subcontext"))) {
242 my $hdr_size = has_property($e, "subcontext");
243 my $subsize = has_property($e, "subcontext_size");
244 if (not defined($subsize)) {
245 $subsize = -1;
248 push (@$order, {
249 TYPE => "SUBCONTEXT",
250 HEADER_SIZE => $hdr_size,
251 SUBCONTEXT_SIZE => $subsize,
252 IS_DEFERRED => $is_deferred,
253 COMPRESSION => has_property($e, "compression"),
257 if (my $switch = has_property($e, "switch_is")) {
258 push (@$order, {
259 TYPE => "SWITCH",
260 SWITCH_IS => $switch,
261 IS_DEFERRED => $is_deferred
265 if (scalar(@size_is) > 0) {
266 fatal($e, "size_is() on non-array element");
269 if (scalar(@length_is) > 0) {
270 fatal($e, "length_is() on non-array element");
273 if (has_property($e, "string")) {
274 fatal($e, "string() attribute on non-array element");
277 push (@$order, {
278 TYPE => "DATA",
279 DATA_TYPE => $e->{TYPE},
280 IS_DEFERRED => $is_deferred,
281 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
282 IS_SURROUNDING => 0 #FIXME
285 my $i = 0;
286 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
288 return $order;
291 sub GetTypedefLevelTable($$$)
293 my ($e, $data, $pointer_default) = @_;
295 my $order = [];
297 push (@$order, {
298 TYPE => "TYPEDEF"
301 my $i = 0;
302 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
304 return $order;
307 #####################################################################
308 # see if a type contains any deferred data
309 sub can_contain_deferred($)
311 sub can_contain_deferred($);
312 my ($type) = @_;
314 return 1 unless (hasType($type)); # assume the worst
316 $type = getType($type);
318 return 0 if (Parse::Pidl::Typelist::is_scalar($type));
320 return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
322 return 0 unless defined($type->{ELEMENTS});
324 foreach (@{$type->{ELEMENTS}}) {
325 return 1 if ($_->{POINTERS});
326 return 1 if (can_contain_deferred ($_->{TYPE}));
329 return 0;
332 sub pointer_type($)
334 my $e = shift;
336 return undef unless $e->{POINTERS};
338 return "ref" if (has_property($e, "ref"));
339 return "full" if (has_property($e, "ptr"));
340 return "sptr" if (has_property($e, "sptr"));
341 return "unique" if (has_property($e, "unique"));
342 return "relative" if (has_property($e, "relative"));
343 return "ignore" if (has_property($e, "ignore"));
345 return undef;
348 #####################################################################
349 # work out the correct alignment for a structure or union
350 sub find_largest_alignment($)
352 my $s = shift;
354 my $align = 1;
355 for my $e (@{$s->{ELEMENTS}}) {
356 my $a = 1;
358 if ($e->{POINTERS}) {
359 $a = 4;
360 } elsif (has_property($e, "subcontext")) {
361 $a = 1;
362 } elsif (has_property($e, "transmit_as")) {
363 $a = align_type($e->{PROPERTIES}->{transmit_as});
364 } else {
365 $a = align_type($e->{TYPE});
368 $align = $a if ($align < $a);
371 return $align;
374 #####################################################################
375 # align a type
376 sub align_type($)
378 sub align_type($);
379 my ($e) = @_;
381 if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
382 return $scalar_alignment->{$e->{NAME}};
385 return 0 if ($e eq "EMPTY");
387 unless (hasType($e)) {
388 # it must be an external type - all we can do is guess
389 # warning($e, "assuming alignment of unknown type '$e' is 4");
390 return 4;
393 my $dt = getType($e);
395 if ($dt->{TYPE} eq "TYPEDEF") {
396 return align_type($dt->{DATA});
397 } elsif ($dt->{TYPE} eq "ENUM") {
398 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
399 } elsif ($dt->{TYPE} eq "BITMAP") {
400 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
401 } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
402 # Struct/union without body: assume 4
403 return 4 unless (defined($dt->{ELEMENTS}));
404 return find_largest_alignment($dt);
407 die("Unknown data type type $dt->{TYPE}");
410 sub ParseElement($$)
412 my ($e, $pointer_default) = @_;
414 $e->{TYPE} = expandAlias($e->{TYPE});
416 if (ref($e->{TYPE}) eq "HASH") {
417 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default);
420 return {
421 NAME => $e->{NAME},
422 TYPE => $e->{TYPE},
423 PROPERTIES => $e->{PROPERTIES},
424 LEVELS => GetElementLevelTable($e, $pointer_default),
425 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
426 ALIGN => align_type($e->{TYPE}),
427 ORIGINAL => $e
431 sub ParseStruct($$)
433 my ($struct, $pointer_default) = @_;
434 my @elements = ();
435 my $surrounding = undef;
437 return {
438 TYPE => "STRUCT",
439 NAME => $struct->{NAME},
440 SURROUNDING_ELEMENT => undef,
441 ELEMENTS => undef,
442 PROPERTIES => $struct->{PROPERTIES},
443 ORIGINAL => $struct,
444 ALIGN => undef
445 } unless defined($struct->{ELEMENTS});
447 CheckPointerTypes($struct, $pointer_default);
449 foreach my $x (@{$struct->{ELEMENTS}})
451 my $e = ParseElement($x, $pointer_default);
452 if ($x != $struct->{ELEMENTS}[-1] and
453 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
454 fatal($x, "conformant member not at end of struct");
456 push @elements, $e;
459 my $e = $elements[-1];
460 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
461 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
462 $surrounding = $e;
465 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
466 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
467 $surrounding = $struct->{ELEMENTS}[-1];
470 my $align = undef;
471 if ($struct->{NAME}) {
472 $align = align_type($struct->{NAME});
475 return {
476 TYPE => "STRUCT",
477 NAME => $struct->{NAME},
478 SURROUNDING_ELEMENT => $surrounding,
479 ELEMENTS => \@elements,
480 PROPERTIES => $struct->{PROPERTIES},
481 ORIGINAL => $struct,
482 ALIGN => $align
486 sub ParseUnion($$)
488 my ($e, $pointer_default) = @_;
489 my @elements = ();
490 my $hasdefault = 0;
491 my $switch_type = has_property($e, "switch_type");
492 unless (defined($switch_type)) { $switch_type = "uint32"; }
493 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
495 return {
496 TYPE => "UNION",
497 NAME => $e->{NAME},
498 SWITCH_TYPE => $switch_type,
499 ELEMENTS => undef,
500 PROPERTIES => $e->{PROPERTIES},
501 HAS_DEFAULT => $hasdefault,
502 ORIGINAL => $e
503 } unless defined($e->{ELEMENTS});
505 CheckPointerTypes($e, $pointer_default);
507 foreach my $x (@{$e->{ELEMENTS}})
509 my $t;
510 if ($x->{TYPE} eq "EMPTY") {
511 $t = { TYPE => "EMPTY" };
512 } else {
513 $t = ParseElement($x, $pointer_default);
515 if (has_property($x, "default")) {
516 $t->{CASE} = "default";
517 $hasdefault = 1;
518 } elsif (defined($x->{PROPERTIES}->{case})) {
519 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
520 } else {
521 die("Union element $x->{NAME} has neither default nor case property");
523 push @elements, $t;
526 return {
527 TYPE => "UNION",
528 NAME => $e->{NAME},
529 SWITCH_TYPE => $switch_type,
530 ELEMENTS => \@elements,
531 PROPERTIES => $e->{PROPERTIES},
532 HAS_DEFAULT => $hasdefault,
533 ORIGINAL => $e
537 sub ParseEnum($$)
539 my ($e, $pointer_default) = @_;
541 return {
542 TYPE => "ENUM",
543 NAME => $e->{NAME},
544 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
545 ELEMENTS => $e->{ELEMENTS},
546 PROPERTIES => $e->{PROPERTIES},
547 ORIGINAL => $e
551 sub ParseBitmap($$)
553 my ($e, $pointer_default) = @_;
555 return {
556 TYPE => "BITMAP",
557 NAME => $e->{NAME},
558 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
559 ELEMENTS => $e->{ELEMENTS},
560 PROPERTIES => $e->{PROPERTIES},
561 ORIGINAL => $e
565 sub ParseType($$)
567 my ($d, $pointer_default) = @_;
569 my $data = {
570 STRUCT => \&ParseStruct,
571 UNION => \&ParseUnion,
572 ENUM => \&ParseEnum,
573 BITMAP => \&ParseBitmap,
574 TYPEDEF => \&ParseTypedef,
575 }->{$d->{TYPE}}->($d, $pointer_default);
577 return $data;
580 sub ParseTypedef($$)
582 my ($d, $pointer_default) = @_;
584 if (defined($d->{DATA}->{PROPERTIES}) && !defined($d->{PROPERTIES})) {
585 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
588 my $data = ParseType($d->{DATA}, $pointer_default);
589 $data->{ALIGN} = align_type($d->{NAME});
591 return {
592 NAME => $d->{NAME},
593 TYPE => $d->{TYPE},
594 PROPERTIES => $d->{PROPERTIES},
595 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default),
596 DATA => $data,
597 ORIGINAL => $d
601 sub ParseConst($$)
603 my ($ndr,$d) = @_;
605 return $d;
608 sub ParseFunction($$$)
610 my ($ndr,$d,$opnum) = @_;
611 my @elements = ();
612 my $rettype = undef;
613 my $thisopnum = undef;
615 CheckPointerTypes($d, "ref");
617 if (not defined($d->{PROPERTIES}{noopnum})) {
618 $thisopnum = ${$opnum};
619 ${$opnum}++;
622 foreach my $x (@{$d->{ELEMENTS}}) {
623 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default});
624 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
625 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
627 push (@elements, $e);
630 if ($d->{RETURN_TYPE} ne "void") {
631 $rettype = expandAlias($d->{RETURN_TYPE});
634 my $async = 0;
635 if (has_property($d, "async")) { $async = 1; }
637 return {
638 NAME => $d->{NAME},
639 TYPE => "FUNCTION",
640 OPNUM => $thisopnum,
641 ASYNC => $async,
642 RETURN_TYPE => $rettype,
643 PROPERTIES => $d->{PROPERTIES},
644 ELEMENTS => \@elements,
645 ORIGINAL => $d
649 sub CheckPointerTypes($$)
651 my ($s,$default) = @_;
653 return unless defined($s->{ELEMENTS});
655 foreach my $e (@{$s->{ELEMENTS}}) {
656 if ($e->{POINTERS} and not defined(pointer_type($e))) {
657 $e->{PROPERTIES}->{$default} = '1';
662 sub FindNestedTypes($$)
664 sub FindNestedTypes($$);
665 my ($l, $t) = @_;
667 return unless defined($t->{ELEMENTS});
668 return if ($t->{TYPE} eq "ENUM");
669 return if ($t->{TYPE} eq "BITMAP");
671 foreach (@{$t->{ELEMENTS}}) {
672 if (ref($_->{TYPE}) eq "HASH") {
673 push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
674 FindNestedTypes($l, $_->{TYPE});
679 sub ParseInterface($)
681 my $idl = shift;
682 my @types = ();
683 my @consts = ();
684 my @functions = ();
685 my @endpoints;
686 my $opnum = 0;
687 my $version;
689 if (not has_property($idl, "pointer_default")) {
690 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
691 # and "unique" in Microsoft Extensions mode (default)
692 $idl->{PROPERTIES}->{pointer_default} = "unique";
695 foreach my $d (@{$idl->{DATA}}) {
696 if ($d->{TYPE} eq "FUNCTION") {
697 push (@functions, ParseFunction($idl, $d, \$opnum));
698 } elsif ($d->{TYPE} eq "CONST") {
699 push (@consts, ParseConst($idl, $d));
700 } else {
701 push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}));
702 FindNestedTypes(\@types, $d);
706 $version = "0.0";
708 if(defined $idl->{PROPERTIES}->{version}) {
709 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
710 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
711 $version = $idl->{PROPERTIES}->{version};
712 } else {
713 $version = $if_version[1] << 16 | $if_version[0];
717 # If no endpoint is set, default to the interface name as a named pipe
718 if (!defined $idl->{PROPERTIES}->{endpoint}) {
719 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
720 } else {
721 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
724 return {
725 NAME => $idl->{NAME},
726 UUID => lc(has_property($idl, "uuid")),
727 VERSION => $version,
728 TYPE => "INTERFACE",
729 PROPERTIES => $idl->{PROPERTIES},
730 FUNCTIONS => \@functions,
731 CONSTS => \@consts,
732 TYPES => \@types,
733 ENDPOINTS => \@endpoints
737 # Convert a IDL tree to a NDR tree
738 # Gives a result tree describing all that's necessary for easily generating
739 # NDR parsers / generators
740 sub Parse($)
742 my $idl = shift;
744 return undef unless (defined($idl));
746 Parse::Pidl::NDR::Validate($idl);
748 my @ndr = ();
750 foreach (@{$idl}) {
751 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
752 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
753 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
756 return \@ndr;
759 sub GetNextLevel($$)
761 my $e = shift;
762 my $fl = shift;
764 my $seen = 0;
766 foreach my $l (@{$e->{LEVELS}}) {
767 return $l if ($seen);
768 ($seen = 1) if ($l == $fl);
771 return undef;
774 sub GetPrevLevel($$)
776 my ($e,$fl) = @_;
777 my $prev = undef;
779 foreach my $l (@{$e->{LEVELS}}) {
780 (return $prev) if ($l == $fl);
781 $prev = $l;
784 return undef;
787 sub ContainsString($)
789 my ($e) = @_;
791 foreach my $l (@{$e->{LEVELS}}) {
792 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
795 return 0;
798 sub ContainsDeferred($$)
800 my ($e,$l) = @_;
802 return 1 if ($l->{CONTAINS_DEFERRED});
804 while ($l = GetNextLevel($e,$l))
806 return 1 if ($l->{IS_DEFERRED});
807 return 1 if ($l->{CONTAINS_DEFERRED});
810 return 0;
813 sub el_name($)
815 my $e = shift;
816 my $name = "<ANONYMOUS>";
818 $name = $e->{NAME} if defined($e->{NAME});
820 if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
821 return "$e->{PARENT}->{NAME}.$name";
824 if (defined($e->{PARENT}) and
825 defined($e->{PARENT}->{PARENT}) and
826 defined($e->{PARENT}->{PARENT}->{NAME})) {
827 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
830 return $name;
833 ###################################
834 # find a sibling var in a structure
835 sub find_sibling($$)
837 my($e,$name) = @_;
838 my($fn) = $e->{PARENT};
840 if ($name =~ /\*(.*)/) {
841 $name = $1;
844 for my $e2 (@{$fn->{ELEMENTS}}) {
845 return $e2 if ($e2->{NAME} eq $name);
848 return undef;
851 my %property_list = (
852 # interface
853 "helpstring" => ["INTERFACE", "FUNCTION"],
854 "version" => ["INTERFACE"],
855 "uuid" => ["INTERFACE"],
856 "endpoint" => ["INTERFACE"],
857 "pointer_default" => ["INTERFACE"],
858 "helper" => ["INTERFACE"],
859 "pyhelper" => ["INTERFACE"],
860 "authservice" => ["INTERFACE"],
861 "restricted" => ["INTERFACE"],
863 # dcom
864 "object" => ["INTERFACE"],
865 "local" => ["INTERFACE", "FUNCTION"],
866 "iid_is" => ["ELEMENT"],
867 "call_as" => ["FUNCTION"],
868 "idempotent" => ["FUNCTION"],
870 # function
871 "noopnum" => ["FUNCTION"],
872 "in" => ["ELEMENT"],
873 "out" => ["ELEMENT"],
874 "async" => ["FUNCTION"],
876 # pointer
877 "ref" => ["ELEMENT"],
878 "ptr" => ["ELEMENT"],
879 "unique" => ["ELEMENT"],
880 "ignore" => ["ELEMENT"],
881 "relative" => ["ELEMENT"],
882 "null_is_ffffffff" => ["ELEMENT"],
883 "relative_base" => ["TYPEDEF", "STRUCT", "UNION"],
885 "gensize" => ["TYPEDEF", "STRUCT", "UNION"],
886 "value" => ["ELEMENT"],
887 "flag" => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
889 # generic
890 "public" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
891 "nopush" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
892 "nopull" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
893 "nosize" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
894 "noprint" => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
895 "todo" => ["FUNCTION"],
897 # union
898 "switch_is" => ["ELEMENT"],
899 "switch_type" => ["ELEMENT", "UNION"],
900 "nodiscriminant" => ["UNION"],
901 "case" => ["ELEMENT"],
902 "default" => ["ELEMENT"],
904 "represent_as" => ["ELEMENT"],
905 "transmit_as" => ["ELEMENT"],
907 # subcontext
908 "subcontext" => ["ELEMENT"],
909 "subcontext_size" => ["ELEMENT"],
910 "compression" => ["ELEMENT"],
912 # enum
913 "enum8bit" => ["ENUM"],
914 "enum16bit" => ["ENUM"],
915 "v1_enum" => ["ENUM"],
917 # bitmap
918 "bitmap8bit" => ["BITMAP"],
919 "bitmap16bit" => ["BITMAP"],
920 "bitmap32bit" => ["BITMAP"],
921 "bitmap64bit" => ["BITMAP"],
923 # array
924 "range" => ["ELEMENT"],
925 "size_is" => ["ELEMENT"],
926 "string" => ["ELEMENT"],
927 "noheader" => ["ELEMENT"],
928 "charset" => ["ELEMENT"],
929 "length_is" => ["ELEMENT"],
932 #####################################################################
933 # check for unknown properties
934 sub ValidProperties($$)
936 my ($e,$t) = @_;
938 return unless defined $e->{PROPERTIES};
940 foreach my $key (keys %{$e->{PROPERTIES}}) {
941 warning($e, el_name($e) . ": unknown property '$key'")
942 unless defined($property_list{$key});
944 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
945 unless grep(/^$t$/, @{$property_list{$key}});
949 sub mapToScalar($)
951 sub mapToScalar($);
952 my $t = shift;
953 return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
954 my $ti = getType($t);
956 if (not defined ($ti)) {
957 return undef;
958 } elsif ($ti->{TYPE} eq "TYPEDEF") {
959 return mapToScalar($ti->{DATA});
960 } elsif ($ti->{TYPE} eq "ENUM") {
961 return Parse::Pidl::Typelist::enum_type_fn($ti);
962 } elsif ($ti->{TYPE} eq "BITMAP") {
963 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
966 return undef;
969 #####################################################################
970 # validate an element
971 sub ValidElement($)
973 my $e = shift;
975 ValidProperties($e,"ELEMENT");
977 # Check whether switches are used correctly.
978 if (my $switch = has_property($e, "switch_is")) {
979 my $e2 = find_sibling($e, $switch);
980 my $type = getType($e->{TYPE});
982 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
983 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
986 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
987 my $discriminator_type = has_property($type->{DATA}, "switch_type");
988 $discriminator_type = "uint32" unless defined ($discriminator_type);
990 my $t1 = mapToScalar($discriminator_type);
992 if (not defined($t1)) {
993 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
996 my $t2 = mapToScalar($e2->{TYPE});
997 if (not defined($t2)) {
998 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1001 if ($t1 ne $t2) {
1002 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1007 if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1008 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1011 if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1012 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1015 if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1016 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1019 if (has_property($e, "represent_as") and has_property($e, "value")) {
1020 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1023 if (has_property($e, "subcontext")) {
1024 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1027 if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1028 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1031 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1032 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1035 if (!$e->{POINTERS} && (
1036 has_property($e, "ptr") or
1037 has_property($e, "unique") or
1038 has_property($e, "relative") or
1039 has_property($e, "ref"))) {
1040 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
1044 #####################################################################
1045 # validate an enum
1046 sub ValidEnum($)
1048 my ($enum) = @_;
1050 ValidProperties($enum, "ENUM");
1053 #####################################################################
1054 # validate a bitmap
1055 sub ValidBitmap($)
1057 my ($bitmap) = @_;
1059 ValidProperties($bitmap, "BITMAP");
1062 #####################################################################
1063 # validate a struct
1064 sub ValidStruct($)
1066 my($struct) = shift;
1068 ValidProperties($struct, "STRUCT");
1070 return unless defined($struct->{ELEMENTS});
1072 foreach my $e (@{$struct->{ELEMENTS}}) {
1073 $e->{PARENT} = $struct;
1074 ValidElement($e);
1078 #####################################################################
1079 # parse a union
1080 sub ValidUnion($)
1082 my($union) = shift;
1084 ValidProperties($union,"UNION");
1086 if (has_property($union->{PARENT}, "nodiscriminant") and
1087 has_property($union->{PARENT}, "switch_type")) {
1088 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1091 return unless defined($union->{ELEMENTS});
1093 foreach my $e (@{$union->{ELEMENTS}}) {
1094 $e->{PARENT} = $union;
1096 if (defined($e->{PROPERTIES}->{default}) and
1097 defined($e->{PROPERTIES}->{case})) {
1098 fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1101 unless (defined ($e->{PROPERTIES}->{default}) or
1102 defined ($e->{PROPERTIES}->{case})) {
1103 fatal($e, "Union member $e->{NAME} must have default or case property");
1106 if (has_property($e, "ref")) {
1107 fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1111 ValidElement($e);
1115 #####################################################################
1116 # parse a typedef
1117 sub ValidTypedef($)
1119 my($typedef) = shift;
1120 my $data = $typedef->{DATA};
1122 ValidProperties($typedef, "TYPEDEF");
1124 $data->{PARENT} = $typedef;
1126 $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1127 $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1129 ValidType($data) if (ref($data) eq "HASH");
1132 #####################################################################
1133 # validate a function
1134 sub ValidFunction($)
1136 my($fn) = shift;
1138 ValidProperties($fn,"FUNCTION");
1140 foreach my $e (@{$fn->{ELEMENTS}}) {
1141 $e->{PARENT} = $fn;
1142 if (has_property($e, "ref") && !$e->{POINTERS}) {
1143 fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1145 ValidElement($e);
1149 #####################################################################
1150 # validate a type
1151 sub ValidType($)
1153 my ($t) = @_;
1156 TYPEDEF => \&ValidTypedef,
1157 STRUCT => \&ValidStruct,
1158 UNION => \&ValidUnion,
1159 ENUM => \&ValidEnum,
1160 BITMAP => \&ValidBitmap
1161 }->{$t->{TYPE}}->($t);
1164 #####################################################################
1165 # parse the interface definitions
1166 sub ValidInterface($)
1168 my($interface) = shift;
1169 my($data) = $interface->{DATA};
1171 if (has_property($interface, "helper")) {
1172 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1175 ValidProperties($interface,"INTERFACE");
1177 if (has_property($interface, "pointer_default")) {
1178 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/,
1179 ("ref", "unique", "ptr"))) {
1180 fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1184 if (has_property($interface, "object")) {
1185 if (has_property($interface, "version") &&
1186 $interface->{PROPERTIES}->{version} != 0) {
1187 fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1190 if (!defined($interface->{BASE}) &&
1191 not ($interface->{NAME} eq "IUnknown")) {
1192 fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1196 foreach my $d (@{$data}) {
1197 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1198 ($d->{TYPE} eq "TYPEDEF" or
1199 $d->{TYPE} eq "STRUCT" or
1200 $d->{TYPE} eq "UNION" or
1201 $d->{TYPE} eq "ENUM" or
1202 $d->{TYPE} eq "BITMAP") && ValidType($d);
1207 #####################################################################
1208 # Validate an IDL structure
1209 sub Validate($)
1211 my($idl) = shift;
1213 foreach my $x (@{$idl}) {
1214 ($x->{TYPE} eq "INTERFACE") &&
1215 ValidInterface($x);
1216 ($x->{TYPE} eq "IMPORTLIB") &&
1217 fatal($x, "importlib() not supported");
1221 sub is_charset_array($$)
1223 my ($e,$l) = @_;
1225 return 0 if ($l->{TYPE} ne "ARRAY");
1227 my $nl = GetNextLevel($e,$l);
1229 return 0 unless ($nl->{TYPE} eq "DATA");
1231 return has_property($e, "charset");