pidl: parse idl 'pipe' typedefs, but print out a not supported message for now
[ab/samba.git/.git] / pidl / lib / Parse / Pidl / NDR.pm
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
7
8 =pod
9
10 =head1 NAME
11
12 Parse::Pidl::NDR - NDR parsing information generator
13
14 =head1 DESCRIPTION
15
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
24
25 =head1 AUTHOR
26
27 Jelmer Vernooij <jelmer@samba.org>
28
29 =cut
30
31 package Parse::Pidl::NDR;
32
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);
39
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);
44
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
74 };
75
76 sub GetElementLevelTable($$)
77 {
78         my ($e, $pointer_default) = @_;
79
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;
86
87         if (has_property($e, "size_is")) {
88                 @size_is = split /,/, has_property($e, "size_is");
89         }
90
91         if (has_property($e, "length_is")) {
92                 @length_is = split /,/, has_property($e, "length_is");
93         }
94
95         if (defined($e->{ARRAY_LEN})) {
96                 @bracket_array = @{$e->{ARRAY_LEN}};
97         }
98
99         if (has_property($e, "out")) {
100                 my $needptrs = 1;
101
102                 if (has_property($e, "string")) { $needptrs++; }
103                 if ($#bracket_array >= 0) { $needptrs = 0; }
104
105                 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
106         }
107
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;
119
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!")
128                         }
129
130                         if (($length = shift @length_is) or $is_string) {
131                                 $is_varying = 1;
132                         } else {
133                                 $length = $size;
134                         }
135
136                         if ($e == $e->{PARENT}->{ELEMENTS}[-1] 
137                                 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
138                                 $is_surrounding = 1;
139                         }
140                 }
141
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));
144
145                 if ($i == 0 and $is_fixed and has_property($e, "string")) {
146                         $is_fixed = 0;
147                         $is_varying = 1;
148                         $is_string = 1;
149                         delete($e->{PROPERTIES}->{string});
150                 }
151
152                 push (@$order, {
153                         TYPE => "ARRAY",
154                         SIZE_IS => $size,
155                         LENGTH_IS => $length,
156                         IS_DEFERRED => $is_deferred,
157                         IS_SURROUNDING => $is_surrounding,
158                         IS_ZERO_TERMINATED => $is_string,
159                         IS_VARYING => $is_varying,
160                         IS_CONFORMANT => $is_conformant,
161                         IS_FIXED => $is_fixed,
162                         IS_INLINE => $is_inline
163                 });
164         }
165
166         # Next, all the pointers
167         foreach my $i (1..$e->{POINTERS}) {
168                 my $level = "EMBEDDED";
169                 # Top level "ref" pointers do not have a referrent identifier
170                 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
171
172                 my $pt;
173                 #
174                 # Only the first level gets the pointer type from the
175                 # pointer property, the others get them from
176                 # the pointer_default() interface property
177                 #
178                 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
179                 # (Here they talk about the rightmost pointer, but testing shows
180                 #  they mean the leftmost pointer.)
181                 #
182                 # --metze
183                 #
184                 $pt = pointer_type($e);
185                 if ($i > 1) {
186                         $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
187                         $pt = $pointer_default;
188                 }
189
190                 push (@$order, { 
191                         TYPE => "POINTER",
192                         POINTER_TYPE => $pt,
193                         POINTER_INDEX => $pointer_idx,
194                         IS_DEFERRED => "$is_deferred",
195                         LEVEL => $level
196                 });
197
198                 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer") 
199                         if ($i == 1 and $pt ne "ref" and
200                                 $e->{PARENT}->{TYPE} eq "FUNCTION" and 
201                                 not has_property($e, "in"));
202
203                 $pointer_idx++;
204                 
205                 # everything that follows will be deferred
206                 $is_deferred = 1 if ($level ne "TOP");
207
208                 my $array_size = shift @size_is;
209                 my $array_length;
210                 my $is_varying;
211                 my $is_conformant;
212                 my $is_string = 0;
213                 if ($array_size) {
214                         $is_conformant = 1;
215                         if ($array_length = shift @length_is) {
216                                 $is_varying = 1;
217                         } else {
218                                 $array_length = $array_size;
219                                 $is_varying =0;
220                         }
221                 } 
222                 
223                 if (scalar(@size_is) == 0 and has_property($e, "string") and 
224                     $i == $e->{POINTERS}) {
225                         $is_string = 1;
226                         $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
227                         delete($e->{PROPERTIES}->{string});
228                 }
229
230                 if ($array_size or $is_string) {
231                         push (@$order, {
232                                 TYPE => "ARRAY",
233                                 SIZE_IS => $array_size,
234                                 LENGTH_IS => $array_length,
235                                 IS_DEFERRED => $is_deferred,
236                                 IS_SURROUNDING => 0,
237                                 IS_ZERO_TERMINATED => $is_string,
238                                 IS_VARYING => $is_varying,
239                                 IS_CONFORMANT => $is_conformant,
240                                 IS_FIXED => 0,
241                                 IS_INLINE => 0
242                         });
243
244                         $is_deferred = 0;
245                 } 
246         }
247
248         if (defined(has_property($e, "subcontext"))) {
249                 my $hdr_size = has_property($e, "subcontext");
250                 my $subsize = has_property($e, "subcontext_size");
251                 if (not defined($subsize)) { 
252                         $subsize = -1; 
253                 }
254                 
255                 push (@$order, {
256                         TYPE => "SUBCONTEXT",
257                         HEADER_SIZE => $hdr_size,
258                         SUBCONTEXT_SIZE => $subsize,
259                         IS_DEFERRED => $is_deferred,
260                         COMPRESSION => has_property($e, "compression"),
261                 });
262         }
263
264         if (my $switch = has_property($e, "switch_is")) {
265                 push (@$order, {
266                         TYPE => "SWITCH", 
267                         SWITCH_IS => $switch,
268                         IS_DEFERRED => $is_deferred
269                 });
270         }
271
272         if (scalar(@size_is) > 0) {
273                 fatal($e, "size_is() on non-array element");
274         }
275
276         if (scalar(@length_is) > 0) {
277                 fatal($e, "length_is() on non-array element");
278         }
279
280         if (has_property($e, "string")) {
281                 fatal($e, "string() attribute on non-array element");
282         }
283
284         push (@$order, {
285                 TYPE => "DATA",
286                 DATA_TYPE => $e->{TYPE},
287                 IS_DEFERRED => $is_deferred,
288                 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
289                 IS_SURROUNDING => 0 #FIXME
290         });
291
292         my $i = 0;
293         foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
294
295         return $order;
296 }
297
298 sub GetTypedefLevelTable($$$)
299 {
300         my ($e, $data, $pointer_default) = @_;
301
302         my $order = [];
303
304         push (@$order, {
305                 TYPE => "TYPEDEF"
306         });
307
308         my $i = 0;
309         foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
310
311         return $order;
312 }
313
314 #####################################################################
315 # see if a type contains any deferred data 
316 sub can_contain_deferred($)
317 {
318         sub can_contain_deferred($);
319         my ($type) = @_;
320
321         return 1 unless (hasType($type)); # assume the worst
322
323         $type = getType($type);
324
325         return 0 if (Parse::Pidl::Typelist::is_scalar($type));
326
327         return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
328
329         return 0 unless defined($type->{ELEMENTS});
330
331         foreach (@{$type->{ELEMENTS}}) {
332                 return 1 if ($_->{POINTERS});
333                 return 1 if (can_contain_deferred ($_->{TYPE}));
334         }
335         
336         return 0;
337 }
338
339 sub pointer_type($)
340 {
341         my $e = shift;
342
343         return undef unless $e->{POINTERS};
344         
345         return "ref" if (has_property($e, "ref"));
346         return "full" if (has_property($e, "ptr"));
347         return "sptr" if (has_property($e, "sptr"));
348         return "unique" if (has_property($e, "unique"));
349         return "relative" if (has_property($e, "relative"));
350         return "ignore" if (has_property($e, "ignore"));
351
352         return undef;
353 }
354
355 #####################################################################
356 # work out the correct alignment for a structure or union
357 sub find_largest_alignment($)
358 {
359         my $s = shift;
360
361         my $align = 1;
362         for my $e (@{$s->{ELEMENTS}}) {
363                 my $a = 1;
364
365                 if ($e->{POINTERS}) {
366                         $a = 4; 
367                 } elsif (has_property($e, "subcontext")) { 
368                         $a = 1;
369                 } elsif (has_property($e, "transmit_as")) {
370                         $a = align_type($e->{PROPERTIES}->{transmit_as});
371                 } else {
372                         $a = align_type($e->{TYPE}); 
373                 }
374
375                 $align = $a if ($align < $a);
376         }
377
378         return $align;
379 }
380
381 #####################################################################
382 # align a type
383 sub align_type($)
384 {
385         sub align_type($);
386         my ($e) = @_;
387
388         if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
389                 return $scalar_alignment->{$e->{NAME}};
390         }
391
392         return 0 if ($e eq "EMPTY");
393
394         unless (hasType($e)) {
395             # it must be an external type - all we can do is guess 
396                 # warning($e, "assuming alignment of unknown type '$e' is 4");
397             return 4;
398         }
399
400         my $dt = getType($e);
401
402         if ($dt->{TYPE} eq "TYPEDEF") {
403                 return align_type($dt->{DATA});
404         } elsif ($dt->{TYPE} eq "ENUM") {
405                 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
406         } elsif ($dt->{TYPE} eq "BITMAP") {
407                 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
408         } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
409                 # Struct/union without body: assume 4
410                 return 4 unless (defined($dt->{ELEMENTS}));
411                 return find_largest_alignment($dt);
412         }
413
414         die("Unknown data type type $dt->{TYPE}");
415 }
416
417 sub ParseElement($$)
418 {
419         my ($e, $pointer_default) = @_;
420
421         $e->{TYPE} = expandAlias($e->{TYPE});
422
423         if (ref($e->{TYPE}) eq "HASH") {
424                 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default);
425         }
426
427         return {
428                 NAME => $e->{NAME},
429                 TYPE => $e->{TYPE},
430                 PROPERTIES => $e->{PROPERTIES},
431                 LEVELS => GetElementLevelTable($e, $pointer_default),
432                 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
433                 ALIGN => align_type($e->{TYPE}),
434                 ORIGINAL => $e
435         };
436 }
437
438 sub ParseStruct($$)
439 {
440         my ($struct, $pointer_default) = @_;
441         my @elements = ();
442         my $surrounding = undef;
443
444         return {
445                 TYPE => "STRUCT",
446                 NAME => $struct->{NAME},
447                 SURROUNDING_ELEMENT => undef,
448                 ELEMENTS => undef,
449                 PROPERTIES => $struct->{PROPERTIES},
450                 ORIGINAL => $struct,
451                 ALIGN => undef
452         } unless defined($struct->{ELEMENTS});
453
454         CheckPointerTypes($struct, $pointer_default);
455
456         foreach my $x (@{$struct->{ELEMENTS}}) 
457         {
458                 my $e = ParseElement($x, $pointer_default);
459                 if ($x != $struct->{ELEMENTS}[-1] and 
460                         $e->{LEVELS}[0]->{IS_SURROUNDING}) {
461                         fatal($x, "conformant member not at end of struct");
462                 }
463                 push @elements, $e;
464         }
465
466         my $e = $elements[-1];
467         if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
468                 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
469                 $surrounding = $e;
470         }
471
472         if (defined $e->{TYPE} && $e->{TYPE} eq "string"
473             &&  property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
474                 $surrounding = $struct->{ELEMENTS}[-1];
475         }
476
477         my $align = undef;
478         if ($struct->{NAME}) {
479                 $align = align_type($struct->{NAME});
480         }
481                 
482         return {
483                 TYPE => "STRUCT",
484                 NAME => $struct->{NAME},
485                 SURROUNDING_ELEMENT => $surrounding,
486                 ELEMENTS => \@elements,
487                 PROPERTIES => $struct->{PROPERTIES},
488                 ORIGINAL => $struct,
489                 ALIGN => $align
490         };
491 }
492
493 sub ParseUnion($$)
494 {
495         my ($e, $pointer_default) = @_;
496         my @elements = ();
497         my $hasdefault = 0;
498         my $switch_type = has_property($e, "switch_type");
499         unless (defined($switch_type)) { $switch_type = "uint32"; }
500         if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
501
502         return {
503                 TYPE => "UNION",
504                 NAME => $e->{NAME},
505                 SWITCH_TYPE => $switch_type,
506                 ELEMENTS => undef,
507                 PROPERTIES => $e->{PROPERTIES},
508                 HAS_DEFAULT => $hasdefault,
509                 ORIGINAL => $e
510         } unless defined($e->{ELEMENTS});
511
512         CheckPointerTypes($e, $pointer_default);
513
514         foreach my $x (@{$e->{ELEMENTS}}) 
515         {
516                 my $t;
517                 if ($x->{TYPE} eq "EMPTY") {
518                         $t = { TYPE => "EMPTY" };
519                 } else {
520                         $t = ParseElement($x, $pointer_default);
521                 }
522                 if (has_property($x, "default")) {
523                         $t->{CASE} = "default";
524                         $hasdefault = 1;
525                 } elsif (defined($x->{PROPERTIES}->{case})) {
526                         $t->{CASE} = "case $x->{PROPERTIES}->{case}";
527                 } else {
528                         die("Union element $x->{NAME} has neither default nor case property");
529                 }
530                 push @elements, $t;
531         }
532
533         return {
534                 TYPE => "UNION",
535                 NAME => $e->{NAME},
536                 SWITCH_TYPE => $switch_type,
537                 ELEMENTS => \@elements,
538                 PROPERTIES => $e->{PROPERTIES},
539                 HAS_DEFAULT => $hasdefault,
540                 ORIGINAL => $e
541         };
542 }
543
544 sub ParseEnum($$)
545 {
546         my ($e, $pointer_default) = @_;
547
548         return {
549                 TYPE => "ENUM",
550                 NAME => $e->{NAME},
551                 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
552                 ELEMENTS => $e->{ELEMENTS},
553                 PROPERTIES => $e->{PROPERTIES},
554                 ORIGINAL => $e
555         };
556 }
557
558 sub ParseBitmap($$)
559 {
560         my ($e, $pointer_default) = @_;
561
562         return {
563                 TYPE => "BITMAP",
564                 NAME => $e->{NAME},
565                 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
566                 ELEMENTS => $e->{ELEMENTS},
567                 PROPERTIES => $e->{PROPERTIES},
568                 ORIGINAL => $e
569         };
570 }
571
572 sub ParseType($$)
573 {
574         my ($d, $pointer_default) = @_;
575
576         my $data = {
577                 STRUCT => \&ParseStruct,
578                 UNION => \&ParseUnion,
579                 ENUM => \&ParseEnum,
580                 BITMAP => \&ParseBitmap,
581                 TYPEDEF => \&ParseTypedef,
582         }->{$d->{TYPE}}->($d, $pointer_default);
583
584         return $data;
585 }
586
587 sub ParseTypedef($$)
588 {
589         my ($d, $pointer_default) = @_;
590
591         if (defined($d->{DATA}->{PROPERTIES}) && !defined($d->{PROPERTIES})) {
592                 $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
593         }
594
595         my $data = ParseType($d->{DATA}, $pointer_default);
596         $data->{ALIGN} = align_type($d->{NAME});
597
598         return {
599                 NAME => $d->{NAME},
600                 TYPE => $d->{TYPE},
601                 PROPERTIES => $d->{PROPERTIES},
602                 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default),
603                 DATA => $data,
604                 ORIGINAL => $d
605         };
606 }
607
608 sub ParseConst($$)
609 {
610         my ($ndr,$d) = @_;
611
612         return $d;
613 }
614
615 sub ParseFunction($$$)
616 {
617         my ($ndr,$d,$opnum) = @_;
618         my @elements = ();
619         my $rettype = undef;
620         my $thisopnum = undef;
621
622         CheckPointerTypes($d, "ref");
623
624         if (not defined($d->{PROPERTIES}{noopnum})) {
625                 $thisopnum = ${$opnum};
626                 ${$opnum}++;
627         }
628
629         foreach my $x (@{$d->{ELEMENTS}}) {
630                 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default});
631                 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
632                 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
633
634                 push (@elements, $e);
635         }
636
637         if ($d->{RETURN_TYPE} ne "void") {
638                 $rettype = expandAlias($d->{RETURN_TYPE});
639         }
640         
641         my $async = 0;
642         if (has_property($d, "async")) { $async = 1; }
643         
644         return {
645                         NAME => $d->{NAME},
646                         TYPE => "FUNCTION",
647                         OPNUM => $thisopnum,
648                         ASYNC => $async,
649                         RETURN_TYPE => $rettype,
650                         PROPERTIES => $d->{PROPERTIES},
651                         ELEMENTS => \@elements,
652                         ORIGINAL => $d
653                 };
654 }
655
656 sub CheckPointerTypes($$)
657 {
658         my ($s,$default) = @_;
659
660         return unless defined($s->{ELEMENTS});
661
662         foreach my $e (@{$s->{ELEMENTS}}) {
663                 if ($e->{POINTERS} and not defined(pointer_type($e))) {
664                         $e->{PROPERTIES}->{$default} = '1';
665                 }
666         }
667 }
668
669 sub FindNestedTypes($$)
670 {
671         sub FindNestedTypes($$);
672         my ($l, $t) = @_;
673
674         return unless defined($t->{ELEMENTS});
675         return if ($t->{TYPE} eq "ENUM");
676         return if ($t->{TYPE} eq "BITMAP");
677
678         foreach (@{$t->{ELEMENTS}}) {
679                 if (ref($_->{TYPE}) eq "HASH") {
680                         push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
681                         FindNestedTypes($l, $_->{TYPE});
682                 }
683         }
684 }
685
686 sub ParseInterface($)
687 {
688         my $idl = shift;
689         my @types = ();
690         my @consts = ();
691         my @functions = ();
692         my @endpoints;
693         my $opnum = 0;
694         my $version;
695
696         if (not has_property($idl, "pointer_default")) {
697                 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
698                 # and "unique" in Microsoft Extensions mode (default)
699                 $idl->{PROPERTIES}->{pointer_default} = "unique";
700         }
701
702         foreach my $d (@{$idl->{DATA}}) {
703                 if ($d->{TYPE} eq "FUNCTION") {
704                         push (@functions, ParseFunction($idl, $d, \$opnum));
705                 } elsif ($d->{TYPE} eq "CONST") {
706                         push (@consts, ParseConst($idl, $d));
707                 } else {
708                         push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}));
709                         FindNestedTypes(\@types, $d);
710                 }
711         }
712
713         $version = "0.0";
714
715         if(defined $idl->{PROPERTIES}->{version}) { 
716                 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
717                 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
718                                 $version = $idl->{PROPERTIES}->{version};
719                 } else {
720                                 $version = $if_version[1] << 16 | $if_version[0];
721                 }
722         }
723
724         # If no endpoint is set, default to the interface name as a named pipe
725         if (!defined $idl->{PROPERTIES}->{endpoint}) {
726                 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
727         } else {
728                 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
729         }
730
731         return { 
732                 NAME => $idl->{NAME},
733                 UUID => lc(has_property($idl, "uuid")),
734                 VERSION => $version,
735                 TYPE => "INTERFACE",
736                 PROPERTIES => $idl->{PROPERTIES},
737                 FUNCTIONS => \@functions,
738                 CONSTS => \@consts,
739                 TYPES => \@types,
740                 ENDPOINTS => \@endpoints
741         };
742 }
743
744 # Convert a IDL tree to a NDR tree
745 # Gives a result tree describing all that's necessary for easily generating
746 # NDR parsers / generators
747 sub Parse($)
748 {
749         my $idl = shift;
750
751         return undef unless (defined($idl));
752
753         Parse::Pidl::NDR::Validate($idl);
754         
755         my @ndr = ();
756
757         foreach (@{$idl}) {
758                 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
759                 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
760                 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
761         }
762
763         return \@ndr;
764 }
765
766 sub GetNextLevel($$)
767 {
768         my $e = shift;
769         my $fl = shift;
770
771         my $seen = 0;
772
773         foreach my $l (@{$e->{LEVELS}}) {
774                 return $l if ($seen);
775                 ($seen = 1) if ($l == $fl);
776         }
777
778         return undef;
779 }
780
781 sub GetPrevLevel($$)
782 {
783         my ($e,$fl) = @_;
784         my $prev = undef;
785
786         foreach my $l (@{$e->{LEVELS}}) {
787                 (return $prev) if ($l == $fl);
788                 $prev = $l;
789         }
790
791         return undef;
792 }
793
794 sub ContainsString($)
795 {
796         my ($e) = @_;
797
798         foreach my $l (@{$e->{LEVELS}}) {
799                 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
800         }
801
802         return 0;
803 }
804
805 sub ContainsDeferred($$)
806 {
807         my ($e,$l) = @_;
808
809         return 1 if ($l->{CONTAINS_DEFERRED});
810
811         while ($l = GetNextLevel($e,$l))
812         {
813                 return 1 if ($l->{IS_DEFERRED}); 
814                 return 1 if ($l->{CONTAINS_DEFERRED});
815         } 
816         
817         return 0;
818 }
819
820 sub el_name($)
821 {
822         my $e = shift;
823         my $name = "<ANONYMOUS>";
824
825         $name = $e->{NAME} if defined($e->{NAME});
826
827         if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
828                 return "$e->{PARENT}->{NAME}.$name";
829         }
830
831         if (defined($e->{PARENT}) and
832             defined($e->{PARENT}->{PARENT}) and
833             defined($e->{PARENT}->{PARENT}->{NAME})) {
834                 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
835         }
836
837         return $name;
838 }
839
840 ###################################
841 # find a sibling var in a structure
842 sub find_sibling($$)
843 {
844         my($e,$name) = @_;
845         my($fn) = $e->{PARENT};
846
847         if ($name =~ /\*(.*)/) {
848                 $name = $1;
849         }
850
851         for my $e2 (@{$fn->{ELEMENTS}}) {
852                 return $e2 if ($e2->{NAME} eq $name);
853         }
854
855         return undef;
856 }
857
858 my %property_list = (
859         # interface
860         "helpstring"            => ["INTERFACE", "FUNCTION"],
861         "version"               => ["INTERFACE"],
862         "uuid"                  => ["INTERFACE"],
863         "endpoint"              => ["INTERFACE"],
864         "pointer_default"       => ["INTERFACE"],
865         "helper"                => ["INTERFACE"],
866         "pyhelper"              => ["INTERFACE"],
867         "authservice"           => ["INTERFACE"],
868         "restricted"    => ["INTERFACE"],
869
870         # dcom
871         "object"                => ["INTERFACE"],
872         "local"                 => ["INTERFACE", "FUNCTION"],
873         "iid_is"                => ["ELEMENT"],
874         "call_as"               => ["FUNCTION"],
875         "idempotent"            => ["FUNCTION"],
876
877         # function
878         "noopnum"               => ["FUNCTION"],
879         "in"                    => ["ELEMENT"],
880         "out"                   => ["ELEMENT"],
881         "async"                 => ["FUNCTION"],
882
883         # pointer
884         "ref"                   => ["ELEMENT"],
885         "ptr"                   => ["ELEMENT"],
886         "unique"                => ["ELEMENT"],
887         "ignore"                => ["ELEMENT"],
888         "relative"              => ["ELEMENT"],
889         "null_is_ffffffff" => ["ELEMENT"],
890         "relative_base"         => ["TYPEDEF", "STRUCT", "UNION"],
891
892         "gensize"               => ["TYPEDEF", "STRUCT", "UNION"],
893         "value"                 => ["ELEMENT"],
894         "flag"                  => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
895
896         # generic
897         "public"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
898         "nopush"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
899         "nopull"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
900         "nosize"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
901         "noprint"               => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
902         "todo"                  => ["FUNCTION"],
903
904         # union
905         "switch_is"             => ["ELEMENT"],
906         "switch_type"           => ["ELEMENT", "UNION"],
907         "nodiscriminant"        => ["UNION"],
908         "case"                  => ["ELEMENT"],
909         "default"               => ["ELEMENT"],
910
911         "represent_as"          => ["ELEMENT"],
912         "transmit_as"           => ["ELEMENT"],
913
914         # subcontext
915         "subcontext"            => ["ELEMENT"],
916         "subcontext_size"       => ["ELEMENT"],
917         "compression"           => ["ELEMENT"],
918
919         # enum
920         "enum8bit"              => ["ENUM"],
921         "enum16bit"             => ["ENUM"],
922         "v1_enum"               => ["ENUM"],
923
924         # bitmap
925         "bitmap8bit"            => ["BITMAP"],
926         "bitmap16bit"           => ["BITMAP"],
927         "bitmap32bit"           => ["BITMAP"],
928         "bitmap64bit"           => ["BITMAP"],
929
930         # array
931         "range"                 => ["ELEMENT", "PIPE"],
932         "size_is"               => ["ELEMENT"],
933         "string"                => ["ELEMENT"],
934         "noheader"              => ["ELEMENT"],
935         "charset"               => ["ELEMENT"],
936         "length_is"             => ["ELEMENT"],
937 );
938
939 #####################################################################
940 # check for unknown properties
941 sub ValidProperties($$)
942 {
943         my ($e,$t) = @_;
944
945         return unless defined $e->{PROPERTIES};
946
947         foreach my $key (keys %{$e->{PROPERTIES}}) {
948                 warning($e, el_name($e) . ": unknown property '$key'")
949                         unless defined($property_list{$key});
950
951                 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
952                         unless grep(/^$t$/, @{$property_list{$key}});
953         }
954 }
955
956 sub mapToScalar($)
957 {
958         sub mapToScalar($);
959         my $t = shift;
960         return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
961         my $ti = getType($t);
962
963         if (not defined ($ti)) {
964                 return undef;
965         } elsif ($ti->{TYPE} eq "TYPEDEF") {
966                 return mapToScalar($ti->{DATA});
967         } elsif ($ti->{TYPE} eq "ENUM") {
968                 return Parse::Pidl::Typelist::enum_type_fn($ti);
969         } elsif ($ti->{TYPE} eq "BITMAP") {
970                 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
971         }
972
973         return undef;
974 }
975
976 #####################################################################
977 # validate an element
978 sub ValidElement($)
979 {
980         my $e = shift;
981
982         ValidProperties($e,"ELEMENT");
983
984         # Check whether switches are used correctly.
985         if (my $switch = has_property($e, "switch_is")) {
986                 my $e2 = find_sibling($e, $switch);
987                 my $type = getType($e->{TYPE});
988
989                 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
990                         fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
991                 }
992
993                 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
994                         my $discriminator_type = has_property($type->{DATA}, "switch_type");
995                         $discriminator_type = "uint32" unless defined ($discriminator_type);
996
997                         my $t1 = mapToScalar($discriminator_type);
998
999                         if (not defined($t1)) {
1000                                 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1001                         }
1002
1003                         my $t2 = mapToScalar($e2->{TYPE});
1004                         if (not defined($t2)) {
1005                                 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1006                         }
1007
1008                         if ($t1 ne $t2) {
1009                                 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1010                         }
1011                 }
1012         }
1013
1014         if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1015                 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1016         }
1017
1018         if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1019                 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1020         }
1021
1022         if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1023                 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1024         }
1025
1026         if (has_property($e, "represent_as") and has_property($e, "value")) {
1027                 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1028         }
1029
1030         if (has_property($e, "subcontext")) {
1031                 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1032         }
1033
1034         if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1035                 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1036         }
1037
1038         if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1039                 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1040         }
1041
1042         if (!$e->{POINTERS} && (
1043                 has_property($e, "ptr") or
1044                 has_property($e, "unique") or
1045                 has_property($e, "relative") or
1046                 has_property($e, "ref"))) {
1047                 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");      
1048         }
1049 }
1050
1051 #####################################################################
1052 # validate an enum
1053 sub ValidEnum($)
1054 {
1055         my ($enum) = @_;
1056
1057         ValidProperties($enum, "ENUM");
1058 }
1059
1060 #####################################################################
1061 # validate a bitmap
1062 sub ValidBitmap($)
1063 {
1064         my ($bitmap) = @_;
1065
1066         ValidProperties($bitmap, "BITMAP");
1067 }
1068
1069 #####################################################################
1070 # validate a struct
1071 sub ValidStruct($)
1072 {
1073         my($struct) = shift;
1074
1075         ValidProperties($struct, "STRUCT");
1076
1077         return unless defined($struct->{ELEMENTS});
1078
1079         foreach my $e (@{$struct->{ELEMENTS}}) {
1080                 $e->{PARENT} = $struct;
1081                 ValidElement($e);
1082         }
1083 }
1084
1085 #####################################################################
1086 # parse a union
1087 sub ValidUnion($)
1088 {
1089         my($union) = shift;
1090
1091         ValidProperties($union,"UNION");
1092
1093         if (has_property($union->{PARENT}, "nodiscriminant") and 
1094                 has_property($union->{PARENT}, "switch_type")) {
1095                 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1096         }
1097
1098         return unless defined($union->{ELEMENTS});
1099
1100         foreach my $e (@{$union->{ELEMENTS}}) {
1101                 $e->{PARENT} = $union;
1102
1103                 if (defined($e->{PROPERTIES}->{default}) and 
1104                         defined($e->{PROPERTIES}->{case})) {
1105                         fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1106                 }
1107                 
1108                 unless (defined ($e->{PROPERTIES}->{default}) or 
1109                                 defined ($e->{PROPERTIES}->{case})) {
1110                         fatal($e, "Union member $e->{NAME} must have default or case property");
1111                 }
1112
1113                 if (has_property($e, "ref")) {
1114                         fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1115                 }
1116
1117
1118                 ValidElement($e);
1119         }
1120 }
1121
1122 #####################################################################
1123 # validate a pipe
1124 sub ValidPipe($)
1125 {
1126         my ($pipe) = @_;
1127         my $data = $pipe->{DATA};
1128
1129         ValidProperties($pipe, "PIPE");
1130
1131         fatal($pipe, $pipe->{NAME} . ": 'pipe' is not yet supported by pidl");
1132 }
1133
1134 #####################################################################
1135 # parse a typedef
1136 sub ValidTypedef($)
1137 {
1138         my($typedef) = shift;
1139         my $data = $typedef->{DATA};
1140
1141         ValidProperties($typedef, "TYPEDEF");
1142
1143         $data->{PARENT} = $typedef;
1144
1145         $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1146         $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1147
1148         ValidType($data) if (ref($data) eq "HASH");
1149 }
1150
1151 #####################################################################
1152 # validate a function
1153 sub ValidFunction($)
1154 {
1155         my($fn) = shift;
1156
1157         ValidProperties($fn,"FUNCTION");
1158
1159         foreach my $e (@{$fn->{ELEMENTS}}) {
1160                 $e->{PARENT} = $fn;
1161                 if (has_property($e, "ref") && !$e->{POINTERS}) {
1162                         fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1163                 }
1164                 ValidElement($e);
1165         }
1166 }
1167
1168 #####################################################################
1169 # validate a type
1170 sub ValidType($)
1171 {
1172         my ($t) = @_;
1173
1174         { 
1175                 TYPEDEF => \&ValidTypedef,
1176                 STRUCT => \&ValidStruct,
1177                 UNION => \&ValidUnion,
1178                 ENUM => \&ValidEnum,
1179                 BITMAP => \&ValidBitmap,
1180                 PIPE => \&ValidPipe
1181         }->{$t->{TYPE}}->($t);
1182 }
1183
1184 #####################################################################
1185 # parse the interface definitions
1186 sub ValidInterface($)
1187 {
1188         my($interface) = shift;
1189         my($data) = $interface->{DATA};
1190
1191         if (has_property($interface, "helper")) {
1192                 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1193         }
1194
1195         ValidProperties($interface,"INTERFACE");
1196
1197         if (has_property($interface, "pointer_default")) {
1198                 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/, 
1199                                         ("ref", "unique", "ptr"))) {
1200                         fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1201                 }
1202         }
1203
1204         if (has_property($interface, "object")) {
1205                 if (has_property($interface, "version") && 
1206                         $interface->{PROPERTIES}->{version} != 0) {
1207                         fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1208                 }
1209
1210                 if (!defined($interface->{BASE}) && 
1211                         not ($interface->{NAME} eq "IUnknown")) {
1212                         fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1213                 }
1214         }
1215                 
1216         foreach my $d (@{$data}) {
1217                 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1218                 ($d->{TYPE} eq "TYPEDEF" or 
1219                  $d->{TYPE} eq "STRUCT" or
1220                  $d->{TYPE} eq "UNION" or 
1221                  $d->{TYPE} eq "ENUM" or
1222                  $d->{TYPE} eq "BITMAP" or
1223                  $d->{TYPE} eq "PIPE") && ValidType($d);
1224         }
1225
1226 }
1227
1228 #####################################################################
1229 # Validate an IDL structure
1230 sub Validate($)
1231 {
1232         my($idl) = shift;
1233
1234         foreach my $x (@{$idl}) {
1235                 ($x->{TYPE} eq "INTERFACE") && 
1236                     ValidInterface($x);
1237                 ($x->{TYPE} eq "IMPORTLIB") &&
1238                         fatal($x, "importlib() not supported");
1239         }
1240 }
1241
1242 sub is_charset_array($$)
1243 {
1244         my ($e,$l) = @_;
1245
1246         return 0 if ($l->{TYPE} ne "ARRAY");
1247
1248         my $nl = GetNextLevel($e,$l);
1249
1250         return 0 unless ($nl->{TYPE} eq "DATA");
1251
1252         return has_property($e, "charset");
1253 }
1254
1255
1256
1257 1;