r18523: Allow [out] on arrays as well as pointers, use in unixinfo.
[Samba.git] / source / pidl / lib / Parse / Pidl / NDR.pm
blobf274836fe7aadf5be9e2f2b62c5bc36efbc84015
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);
39 use strict;
40 use Parse::Pidl::Typelist qw(hasType getType expandAlias);
41 use Parse::Pidl::Util qw(has_property property_matches);
43 # Alignment of the built-in scalar types
44 my $scalar_alignment = {
45 'void' => 0,
46 'char' => 1,
47 'int8' => 1,
48 'uint8' => 1,
49 'int16' => 2,
50 'uint16' => 2,
51 'int32' => 4,
52 'uint32' => 4,
53 'hyper' => 8,
54 'pointer' => 8,
55 'dlong' => 4,
56 'udlong' => 4,
57 'udlongr' => 4,
58 'DATA_BLOB' => 4,
59 'string' => 4,
60 'string_array' => 4, #???
61 'time_t' => 4,
62 'NTTIME' => 4,
63 'NTTIME_1sec' => 4,
64 'NTTIME_hyper' => 8,
65 'WERROR' => 4,
66 'NTSTATUS' => 4,
67 'COMRESULT' => 4,
68 'nbt_string' => 4,
69 'wrepl_nbt_name' => 4,
70 'ipv4address' => 4
75 sub nonfatal($$)
77 my ($e,$s) = @_;
78 warn ("$e->{FILE}:$e->{LINE}: Warning: $s\n");
81 #####################################################################
82 # signal a fatal validation error
83 sub fatal($$)
85 my ($pos,$s) = @_;
86 die("$pos->{FILE}:$pos->{LINE}:$s\n");
89 sub GetElementLevelTable($)
91 my $e = shift;
93 my $order = [];
94 my $is_deferred = 0;
95 my @bracket_array = ();
96 my @length_is = ();
97 my @size_is = ();
98 my $pointer_idx = 0;
100 if (has_property($e, "size_is")) {
101 @size_is = split /,/, has_property($e, "size_is");
104 if (has_property($e, "length_is")) {
105 @length_is = split /,/, has_property($e, "length_is");
108 if (defined($e->{ARRAY_LEN})) {
109 @bracket_array = @{$e->{ARRAY_LEN}};
112 # Parse the [][][][] style array stuff
113 for my $i (0 .. $#bracket_array) {
114 my $d = $bracket_array[$#bracket_array - $i];
115 my $size = $d;
116 my $length = $d;
117 my $is_surrounding = 0;
118 my $is_varying = 0;
119 my $is_conformant = 0;
120 my $is_string = 0;
122 if ($d eq "*") {
123 $is_conformant = 1;
124 if ($size = shift @size_is) {
125 } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
126 $is_string = 1;
127 delete($e->{PROPERTIES}->{string});
128 } else {
129 print "$e->{FILE}:$e->{LINE}: Must specify size_is() for conformant array!\n";
130 exit 1;
133 if (($length = shift @length_is) or $is_string) {
134 $is_varying = 1;
135 } else {
136 $length = $size;
139 if ($e == $e->{PARENT}->{ELEMENTS}[-1]
140 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
141 $is_surrounding = 1;
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 => (not $is_conformant and Parse::Pidl::Util::is_constant($size)),
155 IS_INLINE => (not $is_conformant and not Parse::Pidl::Util::is_constant($size))
159 # Next, all the pointers
160 foreach my $i (1..$e->{POINTERS}) {
161 my $pt = pointer_type($e);
163 my $level = "EMBEDDED";
164 # Top level "ref" pointers do not have a referrent identifier
165 $level = "TOP" if ( defined($pt)
166 and $i == 1
167 and $e->{PARENT}->{TYPE} eq "FUNCTION");
169 push (@$order, {
170 TYPE => "POINTER",
171 # for now, there can only be one pointer type per element
172 POINTER_TYPE => pointer_type($e),
173 POINTER_INDEX => $pointer_idx,
174 IS_DEFERRED => "$is_deferred",
175 LEVEL => $level
178 $pointer_idx++;
180 # everything that follows will be deferred
181 $is_deferred = 1 if ($e->{PARENT}->{TYPE} ne "FUNCTION");
183 my $array_size = shift @size_is;
184 my $array_length;
185 my $is_varying;
186 my $is_conformant;
187 my $is_string = 0;
188 if ($array_size) {
189 $is_conformant = 1;
190 if ($array_length = shift @length_is) {
191 $is_varying = 1;
192 } else {
193 $array_length = $array_size;
194 $is_varying =0;
198 if (scalar(@size_is) == 0 and has_property($e, "string")) {
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 nonfatal($e, "size_is() on non-array element");
250 if (scalar(@length_is) > 0) {
251 nonfatal($e, "length_is() on non-array element");
254 if (has_property($e, "string")) {
255 nonfatal($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 "ptr" 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},
375 ALIGN => align_type($e->{TYPE}),
376 ORIGINAL => $e
380 sub ParseStruct($$)
382 my ($ndr,$struct) = @_;
383 my @elements = ();
384 my $surrounding = undef;
386 foreach my $x (@{$struct->{ELEMENTS}})
388 my $e = ParseElement($x);
389 if ($x != $struct->{ELEMENTS}[-1] and
390 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
391 print "$x->{FILE}:$x->{LINE}: error: conformant member not at end of struct\n";
393 push @elements, $e;
396 my $e = $elements[-1];
397 if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
398 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
399 $surrounding = $e;
402 if (defined $e->{TYPE} && $e->{TYPE} eq "string"
403 && property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
404 $surrounding = $struct->{ELEMENTS}[-1];
407 return {
408 TYPE => "STRUCT",
409 SURROUNDING_ELEMENT => $surrounding,
410 ELEMENTS => \@elements,
411 PROPERTIES => $struct->{PROPERTIES},
412 ORIGINAL => $struct
416 sub ParseUnion($$)
418 my ($ndr,$e) = @_;
419 my @elements = ();
420 my $switch_type = has_property($e, "switch_type");
421 unless (defined($switch_type)) { $switch_type = "uint32"; }
423 if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
425 my $hasdefault = 0;
426 foreach my $x (@{$e->{ELEMENTS}})
428 my $t;
429 if ($x->{TYPE} eq "EMPTY") {
430 $t = { TYPE => "EMPTY" };
431 } else {
432 $t = ParseElement($x);
434 if (has_property($x, "default")) {
435 $t->{CASE} = "default";
436 $hasdefault = 1;
437 } elsif (defined($x->{PROPERTIES}->{case})) {
438 $t->{CASE} = "case $x->{PROPERTIES}->{case}";
439 } else {
440 die("Union element $x->{NAME} has neither default nor case property");
442 push @elements, $t;
445 return {
446 TYPE => "UNION",
447 SWITCH_TYPE => $switch_type,
448 ELEMENTS => \@elements,
449 PROPERTIES => $e->{PROPERTIES},
450 HAS_DEFAULT => $hasdefault,
451 ORIGINAL => $e
455 sub ParseEnum($$)
457 my ($ndr,$e) = @_;
459 return {
460 TYPE => "ENUM",
461 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
462 ELEMENTS => $e->{ELEMENTS},
463 PROPERTIES => $e->{PROPERTIES},
464 ORIGINAL => $e
468 sub ParseBitmap($$)
470 my ($ndr,$e) = @_;
472 return {
473 TYPE => "BITMAP",
474 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
475 ELEMENTS => $e->{ELEMENTS},
476 PROPERTIES => $e->{PROPERTIES},
477 ORIGINAL => $e
481 sub ParseType($$)
483 my ($ndr, $d) = @_;
485 if ($d->{TYPE} eq "STRUCT" or $d->{TYPE} eq "UNION") {
486 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default});
489 my $data = {
490 STRUCT => \&ParseStruct,
491 UNION => \&ParseUnion,
492 ENUM => \&ParseEnum,
493 BITMAP => \&ParseBitmap,
494 TYPEDEF => \&ParseTypedef,
495 }->{$d->{TYPE}}->($ndr, $d);
497 return $data;
500 sub ParseTypedef($$)
502 my ($ndr,$d) = @_;
504 if (defined($d->{PROPERTIES}) && !defined($d->{DATA}->{PROPERTIES})) {
505 $d->{DATA}->{PROPERTIES} = $d->{PROPERTIES};
508 my $data = ParseType($ndr, $d->{DATA});
509 $data->{ALIGN} = align_type($d->{NAME});
511 return {
512 NAME => $d->{NAME},
513 TYPE => $d->{TYPE},
514 PROPERTIES => $d->{PROPERTIES},
515 DATA => $data,
516 ORIGINAL => $d
520 sub ParseConst($$)
522 my ($ndr,$d) = @_;
524 return $d;
527 sub ParseFunction($$$)
529 my ($ndr,$d,$opnum) = @_;
530 my @elements = ();
531 my $rettype = undef;
532 my $thisopnum = undef;
534 CheckPointerTypes($d, $ndr->{PROPERTIES}->{pointer_default_top});
536 if (not defined($d->{PROPERTIES}{noopnum})) {
537 $thisopnum = ${$opnum};
538 ${$opnum}++;
541 foreach my $x (@{$d->{ELEMENTS}}) {
542 my $e = ParseElement($x);
543 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
544 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
546 nonfatal($x, "`$e->{NAME}' is [out] argument but not a pointer or array")
547 if ($e->{LEVELS}[0]->{TYPE} ne "POINTER" and
548 $e->{LEVELS}[0]->{TYPE} ne "ARRAY" and
549 grep(/out/, @{$e->{DIRECTION}}));
551 push (@elements, $e);
554 if ($d->{RETURN_TYPE} ne "void") {
555 $rettype = expandAlias($d->{RETURN_TYPE});
558 my $async = 0;
559 if (has_property($d, "async")) { $async = 1; }
561 return {
562 NAME => $d->{NAME},
563 TYPE => "FUNCTION",
564 OPNUM => $thisopnum,
565 ASYNC => $async,
566 RETURN_TYPE => $rettype,
567 PROPERTIES => $d->{PROPERTIES},
568 ELEMENTS => \@elements,
569 ORIGINAL => $d
573 sub CheckPointerTypes($$)
575 my ($s,$default) = @_;
577 foreach my $e (@{$s->{ELEMENTS}}) {
578 if ($e->{POINTERS} and not defined(pointer_type($e))) {
579 $e->{PROPERTIES}->{$default} = 1;
584 #FIXME: Remove when ref handling in Samba4 is fixed
585 sub AddKeepRef($)
587 my $d = shift;
589 if ($d->{TYPE} eq "FUNCTION") {
590 foreach (@{$d->{ELEMENTS}}) {
591 $_->{PROPERTIES}->{keepref} = 1;
593 } elsif ($d->{TYPE} eq "TYPEDEF" and ($d->{DATA}->{TYPE} eq "STRUCT"
594 or $d->{DATA}->{TYPE} eq "UNION")) {
595 foreach (@{$d->{DATA}->{ELEMENTS}}) {
596 $_->{PROPERTIES}->{keepref} = 1;
601 sub ParseInterface($)
603 my $idl = shift;
604 my @types = ();
605 my @consts = ();
606 my @functions = ();
607 my @endpoints;
608 my @declares = ();
609 my $opnum = 0;
610 my $version;
612 if (not has_property($idl, "pointer_default")) {
613 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
614 # and "unique" in Microsoft Extensions mode (default)
615 $idl->{PROPERTIES}->{pointer_default} = "unique";
618 if (not has_property($idl, "pointer_default_top")) {
619 $idl->{PROPERTIES}->{pointer_default_top} = "ref";
622 foreach my $d (@{$idl->{DATA}}) {
623 if ($d->{TYPE} eq "DECLARE") {
624 push (@declares, $d);
625 } elsif ($d->{TYPE} eq "FUNCTION") {
626 AddKeepRef($d) if (has_property($idl, "keepref"));
627 push (@functions, ParseFunction($idl, $d, \$opnum));
628 } elsif ($d->{TYPE} eq "CONST") {
629 push (@consts, ParseConst($idl, $d));
630 } else {
631 AddKeepRef($d) if (has_property($idl, "keepref"));
632 push (@types, ParseType($idl, $d));
636 $version = "0.0";
638 if(defined $idl->{PROPERTIES}->{version}) {
639 $version = $idl->{PROPERTIES}->{version};
642 # If no endpoint is set, default to the interface name as a named pipe
643 if (!defined $idl->{PROPERTIES}->{endpoint}) {
644 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
645 } else {
646 @endpoints = split / /, $idl->{PROPERTIES}->{endpoint};
649 return {
650 NAME => $idl->{NAME},
651 UUID => lc(has_property($idl, "uuid")),
652 VERSION => $version,
653 TYPE => "INTERFACE",
654 PROPERTIES => $idl->{PROPERTIES},
655 FUNCTIONS => \@functions,
656 CONSTS => \@consts,
657 TYPES => \@types,
658 DECLARES => \@declares,
659 ENDPOINTS => \@endpoints
663 # Convert a IDL tree to a NDR tree
664 # Gives a result tree describing all that's necessary for easily generating
665 # NDR parsers / generators
666 sub Parse($)
668 my $idl = shift;
670 return undef unless (defined($idl));
672 Parse::Pidl::NDR::Validate($idl);
674 my @ndr = ();
676 foreach (@{$idl}) {
677 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
680 return \@ndr;
683 sub GetNextLevel($$)
685 my $e = shift;
686 my $fl = shift;
688 my $seen = 0;
690 foreach my $l (@{$e->{LEVELS}}) {
691 return $l if ($seen);
692 ($seen = 1) if ($l == $fl);
695 return undef;
698 sub GetPrevLevel($$)
700 my ($e,$fl) = @_;
701 my $prev = undef;
703 foreach my $l (@{$e->{LEVELS}}) {
704 (return $prev) if ($l == $fl);
705 $prev = $l;
708 return undef;
711 sub ContainsString($)
713 my ($e) = @_;
715 foreach my $l (@{$e->{LEVELS}}) {
716 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
719 return 0;
722 sub ContainsDeferred($$)
724 my ($e,$l) = @_;
726 return 1 if ($l->{CONTAINS_DEFERRED});
728 while ($l = GetNextLevel($e,$l))
730 return 1 if ($l->{IS_DEFERRED});
731 return 1 if ($l->{CONTAINS_DEFERRED});
734 return 0;
737 sub el_name($)
739 my $e = shift;
741 if ($e->{PARENT} && $e->{PARENT}->{NAME}) {
742 return "$e->{PARENT}->{NAME}.$e->{NAME}";
745 if ($e->{PARENT} && $e->{PARENT}->{PARENT}->{NAME}) {
746 return "$e->{PARENT}->{PARENT}->{NAME}.$e->{NAME}";
749 if ($e->{PARENT}) {
750 return "$e->{PARENT}->{NAME}.$e->{NAME}";
753 return $e->{NAME};
756 ###################################
757 # find a sibling var in a structure
758 sub find_sibling($$)
760 my($e,$name) = @_;
761 my($fn) = $e->{PARENT};
763 if ($name =~ /\*(.*)/) {
764 $name = $1;
767 for my $e2 (@{$fn->{ELEMENTS}}) {
768 return $e2 if ($e2->{NAME} eq $name);
771 return undef;
774 my %property_list = (
775 # interface
776 "helpstring" => ["INTERFACE", "FUNCTION"],
777 "version" => ["INTERFACE"],
778 "uuid" => ["INTERFACE"],
779 "endpoint" => ["INTERFACE"],
780 "pointer_default" => ["INTERFACE"],
781 "pointer_default_top" => ["INTERFACE"],
782 "depends" => ["INTERFACE"],
783 "helper" => ["INTERFACE"],
784 "authservice" => ["INTERFACE"],
786 # dcom
787 "object" => ["INTERFACE"],
788 "local" => ["INTERFACE", "FUNCTION"],
789 "iid_is" => ["ELEMENT"],
790 "call_as" => ["FUNCTION"],
791 "idempotent" => ["FUNCTION"],
793 # function
794 "noopnum" => ["FUNCTION"],
795 "in" => ["ELEMENT"],
796 "out" => ["ELEMENT"],
797 "async" => ["FUNCTION"],
799 # pointer
800 "ref" => ["ELEMENT"],
801 "ptr" => ["ELEMENT"],
802 "sptr" => ["ELEMENT"],
803 "unique" => ["ELEMENT"],
804 "ignore" => ["ELEMENT"],
805 "relative" => ["ELEMENT"],
806 "relative_base" => ["TYPEDEF"],
808 "gensize" => ["TYPEDEF"],
809 "value" => ["ELEMENT"],
810 "flag" => ["ELEMENT", "TYPEDEF"],
812 # generic
813 "public" => ["FUNCTION", "TYPEDEF"],
814 "nopush" => ["FUNCTION", "TYPEDEF"],
815 "nopull" => ["FUNCTION", "TYPEDEF"],
816 "nosize" => ["FUNCTION", "TYPEDEF"],
817 "noprint" => ["FUNCTION", "TYPEDEF"],
818 "noejs" => ["FUNCTION", "TYPEDEF"],
820 # union
821 "switch_is" => ["ELEMENT"],
822 "switch_type" => ["ELEMENT", "TYPEDEF"],
823 "nodiscriminant" => ["TYPEDEF"],
824 "case" => ["ELEMENT"],
825 "default" => ["ELEMENT"],
827 "represent_as" => ["ELEMENT"],
828 "transmit_as" => ["ELEMENT"],
830 # subcontext
831 "subcontext" => ["ELEMENT"],
832 "subcontext_size" => ["ELEMENT"],
833 "compression" => ["ELEMENT"],
835 # enum
836 "enum8bit" => ["TYPEDEF"],
837 "enum16bit" => ["TYPEDEF"],
838 "v1_enum" => ["TYPEDEF"],
840 # bitmap
841 "bitmap8bit" => ["TYPEDEF"],
842 "bitmap16bit" => ["TYPEDEF"],
843 "bitmap32bit" => ["TYPEDEF"],
844 "bitmap64bit" => ["TYPEDEF"],
846 # array
847 "range" => ["ELEMENT"],
848 "size_is" => ["ELEMENT"],
849 "string" => ["ELEMENT"],
850 "noheader" => ["ELEMENT"],
851 "charset" => ["ELEMENT"],
852 "length_is" => ["ELEMENT"],
854 # temporary (should be removed once we've migrated away from
855 # relying on ref pointers being there in Samba4's code)
856 "keepref" => ["ELEMENT","INTERFACE"],
859 #####################################################################
860 # check for unknown properties
861 sub ValidProperties($$)
863 my ($e,$t) = @_;
865 return unless defined $e->{PROPERTIES};
867 foreach my $key (keys %{$e->{PROPERTIES}}) {
868 fatal($e, el_name($e) . ": unknown property '$key'\n")
869 unless defined($property_list{$key});
871 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'\n")
872 unless grep($t, @{$property_list{$key}});
876 sub mapToScalar($)
878 my $t = shift;
879 my $ti = getType($t);
881 if (not defined ($ti)) {
882 return undef;
883 } elsif ($ti->{DATA}->{TYPE} eq "ENUM") {
884 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
885 } elsif ($ti->{DATA}->{TYPE} eq "BITMAP") {
886 return Parse::Pidl::Typelist::enum_type_fn($ti->{DATA});
887 } elsif ($ti->{DATA}->{TYPE} eq "SCALAR") {
888 return $t;
891 return undef;
894 #####################################################################
895 # parse a struct
896 sub ValidElement($)
898 my $e = shift;
900 ValidProperties($e,"ELEMENT");
902 if (has_property($e, "ptr")) {
903 fatal($e, el_name($e) . " : pidl does not support full NDR pointers yet\n");
906 # Check whether switches are used correctly.
907 if (my $switch = has_property($e, "switch_is")) {
908 my $e2 = find_sibling($e, $switch);
909 my $type = getType($e->{TYPE});
911 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
912 fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
915 if (!has_property($type, "nodiscriminant") and defined($e2)) {
916 my $discriminator_type = has_property($type, "switch_type");
917 $discriminator_type = "uint32" unless defined ($discriminator_type);
919 my $t1 = mapToScalar($discriminator_type);
921 if (not defined($t1)) {
922 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
925 my $t2 = mapToScalar($e2->{TYPE});
926 if (not defined($t2)) {
927 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
930 if ($t1 ne $t2) {
931 nonfatal($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 (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
954 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
957 if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
958 fatal($e, el_name($e) . " : compression() on non-subcontext element");
961 if (!$e->{POINTERS} && (
962 has_property($e, "ptr") or
963 has_property($e, "sptr") or
964 has_property($e, "unique") or
965 has_property($e, "relative") or
966 has_property($e, "ref"))) {
967 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");
971 #####################################################################
972 # parse 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!\n";
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\n";
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 if (ref($data) eq "HASH") {
1031 if ($data->{TYPE} eq "STRUCT") {
1032 ValidStruct($data);
1035 if ($data->{TYPE} eq "UNION") {
1036 ValidUnion($data);
1041 #####################################################################
1042 # parse a function
1043 sub ValidFunction($)
1045 my($fn) = shift;
1047 ValidProperties($fn,"FUNCTION");
1049 foreach my $e (@{$fn->{ELEMENTS}}) {
1050 $e->{PARENT} = $fn;
1051 if (has_property($e, "ref") && !$e->{POINTERS}) {
1052 fatal $e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})\n";
1054 ValidElement($e);
1058 #####################################################################
1059 # parse the interface definitions
1060 sub ValidInterface($)
1062 my($interface) = shift;
1063 my($data) = $interface->{DATA};
1065 ValidProperties($interface,"INTERFACE");
1067 if (has_property($interface, "pointer_default") &&
1068 $interface->{PROPERTIES}->{pointer_default} eq "ptr") {
1069 fatal $interface, "Full pointers are not supported yet\n";
1072 if (has_property($interface, "object")) {
1073 if (has_property($interface, "version") &&
1074 $interface->{PROPERTIES}->{version} != 0) {
1075 fatal $interface, "Object interfaces must have version 0.0 ($interface->{NAME})\n";
1078 if (!defined($interface->{BASE}) &&
1079 not ($interface->{NAME} eq "IUnknown")) {
1080 fatal $interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})\n";
1084 foreach my $d (@{$data}) {
1085 ($d->{TYPE} eq "TYPEDEF") &&
1086 ValidTypedef($d);
1087 ($d->{TYPE} eq "FUNCTION") &&
1088 ValidFunction($d);
1093 #####################################################################
1094 # Validate an IDL structure
1095 sub Validate($)
1097 my($idl) = shift;
1099 foreach my $x (@{$idl}) {
1100 ($x->{TYPE} eq "INTERFACE") &&
1101 ValidInterface($x);