pidl/NDR: give anonymous structs and unions a name
[metze/samba/wip.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 ContainsPipe ContainsString);
38 @EXPORT_OK = qw(GetElementLevelTable ParseElement ReturnTypeElement 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 typeIs expandAlias mapScalarType is_fixed_size_scalar);
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         'int1632' => 3,
54         'uint1632' => 3,
55         'int32' => 4,
56         'uint32' => 4,
57         'int3264' => 5,
58         'uint3264' => 5,
59         'hyper' => 8,
60         'double' => 8,
61         'pointer' => 8,
62         'dlong' => 4,
63         'udlong' => 4,
64         'udlongr' => 4,
65         'DATA_BLOB' => 4,
66         'string' => 4,
67         'string_array' => 4, #???
68         'time_t' => 4,
69         'uid_t' => 8,
70         'gid_t' => 8,
71         'NTTIME' => 4,
72         'NTTIME_1sec' => 4,
73         'NTTIME_hyper' => 8,
74         'WERROR' => 4,
75         'NTSTATUS' => 4,
76         'COMRESULT' => 4,
77         'dns_string' => 4,
78         'nbt_string' => 4,
79         'wrepl_nbt_name' => 4,
80         'ipv4address' => 4,
81         'ipv6address' => 4, #16?
82         'dnsp_name' => 1,
83         'dnsp_string' => 1
84 };
85
86 sub GetElementLevelTable($$$)
87 {
88         my ($e, $pointer_default, $ms_union) = @_;
89
90         my $order = [];
91         my $is_deferred = 0;
92         my @bracket_array = ();
93         my @length_is = ();
94         my @size_is = ();
95         my $pointer_idx = 0;
96
97         if (has_property($e, "size_is")) {
98                 @size_is = split /,/, has_property($e, "size_is");
99         }
100
101         if (has_property($e, "length_is")) {
102                 @length_is = split /,/, has_property($e, "length_is");
103         }
104
105         if (defined($e->{ARRAY_LEN})) {
106                 @bracket_array = @{$e->{ARRAY_LEN}};
107         }
108
109         if (has_property($e, "out")) {
110                 my $needptrs = 1;
111
112                 if (has_property($e, "string") and not has_property($e, "in")) { $needptrs++; }
113                 if ($#bracket_array >= 0) { $needptrs = 0; }
114
115                 warning($e, "[out] argument `$e->{NAME}' not a pointer") if ($needptrs > $e->{POINTERS});
116         }
117
118         my $allow_pipe = ($e->{PARENT}->{TYPE} eq "FUNCTION");
119         my $is_pipe = typeIs($e->{TYPE}, "PIPE");
120
121         if ($is_pipe) {
122                 if (not $allow_pipe) {
123                         fatal($e, "argument `$e->{NAME}' is a pipe and not allowed on $e->{PARENT}->{TYPE}");
124                 }
125
126                 if ($e->{POINTERS} > 1) {
127                         fatal($e, "$e->{POINTERS} are not allowed on pipe element $e->{NAME}");
128                 }
129
130                 if ($e->{POINTERS} < 0) {
131                         fatal($e, "pipe element $e->{NAME} needs pointer");
132                 }
133
134                 if ($e->{POINTERS} == 1 and pointer_type($e) ne "ref") {
135                         fatal($e, "pointer should be 'ref' on pipe element $e->{NAME}");
136                 }
137
138                 if (scalar(@size_is) > 0) {
139                         fatal($e, "size_is() on pipe element");
140                 }
141
142                 if (scalar(@length_is) > 0) {
143                         fatal($e, "length_is() on pipe element");
144                 }
145
146                 if (scalar(@bracket_array) > 0) {
147                         fatal($e, "brackets on pipe element");
148                 }
149
150                 if (defined(has_property($e, "subcontext"))) {
151                         fatal($e, "subcontext on pipe element");
152                 }
153
154                 if (has_property($e, "switch_is")) {
155                         fatal($e, "switch_is on pipe element");
156                 }
157
158                 if (can_contain_deferred($e->{TYPE})) {
159                         fatal($e, "$e->{TYPE} can_contain_deferred - not allowed on pipe element");
160                 }
161         }
162
163         # Parse the [][][][] style array stuff
164         for my $i (0 .. $#bracket_array) {
165                 my $d = $bracket_array[$#bracket_array - $i];
166                 my $size = $d;
167                 my $length = $d;
168                 my $is_surrounding = 0;
169                 my $is_varying = 0;
170                 my $is_conformant = 0;
171                 my $is_string = 0;
172                 my $is_fixed = 0;
173                 my $is_inline = 0;
174                 my $is_to_null = 0;
175
176                 if ($d eq "*") {
177                         $is_conformant = 1;
178                         if ($size = shift @size_is) {
179                                 if ($e->{POINTERS} < 1 and has_property($e, "string")) {
180                                         $is_string = 1;
181                                         delete($e->{PROPERTIES}->{string});
182                                 }
183                         } elsif ((scalar(@size_is) == 0) and has_property($e, "string")) {
184                                 $is_string = 1;
185                                 delete($e->{PROPERTIES}->{string});
186                         } else {
187                                 fatal($e, "Must specify size_is() for conformant array!")
188                         }
189
190                         if (($length = shift @length_is) or $is_string) {
191                                 $is_varying = 1;
192                         } else {
193                                 $length = $size;
194                         }
195
196                         if ($e == $e->{PARENT}->{ELEMENTS}[-1] 
197                                 and $e->{PARENT}->{TYPE} ne "FUNCTION") {
198                                 $is_surrounding = 1;
199                         }
200                 }
201
202                 $is_fixed = 1 if (not $is_conformant and Parse::Pidl::Util::is_constant($size));
203                 $is_inline = 1 if (not $is_conformant and not Parse::Pidl::Util::is_constant($size));
204
205                 if ($i == 0 and $is_fixed and has_property($e, "string")) {
206                         $is_fixed = 0;
207                         $is_varying = 1;
208                         $is_string = 1;
209                         delete($e->{PROPERTIES}->{string});
210                 }
211
212                 if (has_property($e, "to_null")) {
213                         $is_to_null = 1;
214                 }
215
216                 push (@$order, {
217                         TYPE => "ARRAY",
218                         SIZE_IS => $size,
219                         LENGTH_IS => $length,
220                         IS_DEFERRED => $is_deferred,
221                         IS_SURROUNDING => $is_surrounding,
222                         IS_ZERO_TERMINATED => $is_string,
223                         IS_VARYING => $is_varying,
224                         IS_CONFORMANT => $is_conformant,
225                         IS_FIXED => $is_fixed,
226                         IS_INLINE => $is_inline,
227                         IS_TO_NULL => $is_to_null
228                 });
229         }
230
231         # Next, all the pointers
232         foreach my $i (1..$e->{POINTERS}) {
233                 my $level = "EMBEDDED";
234                 # Top level "ref" pointers do not have a referrent identifier
235                 $level = "TOP" if ($i == 1 and $e->{PARENT}->{TYPE} eq "FUNCTION");
236
237                 my $pt;
238                 #
239                 # Only the first level gets the pointer type from the
240                 # pointer property, the others get them from
241                 # the pointer_default() interface property
242                 #
243                 # see http://msdn2.microsoft.com/en-us/library/aa378984(VS.85).aspx
244                 # (Here they talk about the rightmost pointer, but testing shows
245                 #  they mean the leftmost pointer.)
246                 #
247                 # --metze
248                 #
249                 $pt = pointer_type($e);
250                 if ($i > 1) {
251                         $is_deferred = 1 if ($pt ne "ref" and $e->{PARENT}->{TYPE} eq "FUNCTION");
252                         $pt = $pointer_default;
253                 }
254
255                 push (@$order, { 
256                         TYPE => "POINTER",
257                         POINTER_TYPE => $pt,
258                         POINTER_INDEX => $pointer_idx,
259                         IS_DEFERRED => "$is_deferred",
260                         LEVEL => $level
261                 });
262
263                 warning($e, "top-level \[out\] pointer `$e->{NAME}' is not a \[ref\] pointer") 
264                         if ($i == 1 and $pt ne "ref" and
265                                 $e->{PARENT}->{TYPE} eq "FUNCTION" and 
266                                 not has_property($e, "in"));
267
268                 $pointer_idx++;
269                 
270                 # everything that follows will be deferred
271                 $is_deferred = 1 if ($level ne "TOP");
272
273                 my $array_size = shift @size_is;
274                 my $array_length;
275                 my $is_varying;
276                 my $is_conformant;
277                 my $is_string = 0;
278                 if ($array_size) {
279                         $is_conformant = 1;
280                         if ($array_length = shift @length_is) {
281                                 $is_varying = 1;
282                         } else {
283                                 $array_length = $array_size;
284                                 $is_varying =0;
285                         }
286                 } 
287                 
288                 if (scalar(@size_is) == 0 and has_property($e, "string") and 
289                     $i == $e->{POINTERS}) {
290                         $is_string = 1;
291                         $is_varying = $is_conformant = has_property($e, "noheader")?0:1;
292                         delete($e->{PROPERTIES}->{string});
293                 }
294
295                 if ($array_size or $is_string) {
296                         push (@$order, {
297                                 TYPE => "ARRAY",
298                                 SIZE_IS => $array_size,
299                                 LENGTH_IS => $array_length,
300                                 IS_DEFERRED => $is_deferred,
301                                 IS_SURROUNDING => 0,
302                                 IS_ZERO_TERMINATED => $is_string,
303                                 IS_VARYING => $is_varying,
304                                 IS_CONFORMANT => $is_conformant,
305                                 IS_FIXED => 0,
306                                 IS_INLINE => 0
307                         });
308
309                         $is_deferred = 0;
310                 } 
311         }
312
313         if ($is_pipe) {
314                 push (@$order, {
315                         TYPE => "PIPE",
316                         IS_DEFERRED => 0,
317                         CONTAINS_DEFERRED => 0,
318                 });
319
320                 my $i = 0;
321                 foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
322
323                 return $order;
324         }
325
326         if (defined(has_property($e, "subcontext"))) {
327                 my $hdr_size = has_property($e, "subcontext");
328                 my $subsize = has_property($e, "subcontext_size");
329                 if (not defined($subsize)) { 
330                         $subsize = -1; 
331                 }
332                 
333                 push (@$order, {
334                         TYPE => "SUBCONTEXT",
335                         HEADER_SIZE => $hdr_size,
336                         SUBCONTEXT_SIZE => $subsize,
337                         IS_DEFERRED => $is_deferred,
338                         COMPRESSION => has_property($e, "compression"),
339                 });
340         }
341
342         if (my $switch = has_property($e, "switch_is")) {
343                 push (@$order, {
344                         TYPE => "SWITCH", 
345                         SWITCH_IS => $switch,
346                         IS_DEFERRED => $is_deferred
347                 });
348         }
349
350         if (scalar(@size_is) > 0) {
351                 fatal($e, "size_is() on non-array element");
352         }
353
354         if (scalar(@length_is) > 0) {
355                 fatal($e, "length_is() on non-array element");
356         }
357
358         if (has_property($e, "string")) {
359                 fatal($e, "string() attribute on non-array element");
360         }
361
362         push (@$order, {
363                 TYPE => "DATA",
364                 DATA_TYPE => $e->{TYPE},
365                 IS_DEFERRED => $is_deferred,
366                 CONTAINS_DEFERRED => can_contain_deferred($e->{TYPE}),
367                 IS_SURROUNDING => 0 #FIXME
368         });
369
370         my $i = 0;
371         foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
372
373         return $order;
374 }
375
376 sub GetTypedefLevelTable($$$$)
377 {
378         my ($e, $data, $pointer_default, $ms_union) = @_;
379
380         my $order = [];
381
382         push (@$order, {
383                 TYPE => "TYPEDEF"
384         });
385
386         my $i = 0;
387         foreach (@$order) { $_->{LEVEL_INDEX} = $i; $i+=1; }
388
389         return $order;
390 }
391
392 #####################################################################
393 # see if a type contains any deferred data 
394 sub can_contain_deferred($)
395 {
396         sub can_contain_deferred($);
397         my ($type) = @_;
398
399         return 1 unless (hasType($type)); # assume the worst
400
401         $type = getType($type);
402
403         return 0 if (Parse::Pidl::Typelist::is_scalar($type));
404
405         return can_contain_deferred($type->{DATA}) if ($type->{TYPE} eq "TYPEDEF");
406
407         return 0 unless defined($type->{ELEMENTS});
408
409         foreach (@{$type->{ELEMENTS}}) {
410                 return 1 if ($_->{POINTERS});
411                 return 1 if (can_contain_deferred ($_->{TYPE}));
412         }
413         
414         return 0;
415 }
416
417 sub pointer_type($)
418 {
419         my $e = shift;
420
421         return undef unless $e->{POINTERS};
422         
423         return "ref" if (has_property($e, "ref"));
424         return "full" if (has_property($e, "ptr"));
425         return "sptr" if (has_property($e, "sptr"));
426         return "unique" if (has_property($e, "unique"));
427         return "relative" if (has_property($e, "relative"));
428         return "relative_short" if (has_property($e, "relative_short"));
429         return "ignore" if (has_property($e, "ignore"));
430
431         return undef;
432 }
433
434 #####################################################################
435 # work out the correct alignment for a structure or union
436 sub find_largest_alignment($)
437 {
438         my $s = shift;
439
440         my $align = 1;
441         for my $e (@{$s->{ELEMENTS}}) {
442                 my $a = 1;
443
444                 if ($e->{POINTERS}) {
445                         # this is a hack for NDR64
446                         # the NDR layer translates this into
447                         # an alignment of 4 for NDR and 8 for NDR64
448                         $a = 5;
449                 } elsif (has_property($e, "subcontext")) { 
450                         $a = 1;
451                 } elsif (has_property($e, "transmit_as")) {
452                         $a = align_type($e->{PROPERTIES}->{transmit_as});
453                 } else {
454                         $a = align_type($e->{TYPE}); 
455                 }
456
457                 $align = $a if ($align < $a);
458         }
459
460         return $align;
461 }
462
463 #####################################################################
464 # align a type
465 sub align_type($)
466 {
467         sub align_type($);
468         my ($e) = @_;
469
470         if (ref($e) eq "HASH" and $e->{TYPE} eq "SCALAR") {
471                 return $scalar_alignment->{$e->{NAME}};
472         }
473
474         return 0 if ($e eq "EMPTY");
475
476         unless (hasType($e)) {
477             # it must be an external type - all we can do is guess 
478                 # warning($e, "assuming alignment of unknown type '$e' is 4");
479             return 4;
480         }
481
482         my $dt = getType($e);
483
484         if ($dt->{TYPE} eq "TYPEDEF") {
485                 return align_type($dt->{DATA});
486         } elsif ($dt->{TYPE} eq "CONFORMANCE") {
487                 return $dt->{DATA}->{ALIGN};
488         } elsif ($dt->{TYPE} eq "ENUM") {
489                 return align_type(Parse::Pidl::Typelist::enum_type_fn($dt));
490         } elsif ($dt->{TYPE} eq "BITMAP") {
491                 return align_type(Parse::Pidl::Typelist::bitmap_type_fn($dt));
492         } elsif (($dt->{TYPE} eq "STRUCT") or ($dt->{TYPE} eq "UNION")) {
493                 # Struct/union without body: assume 4
494                 return 4 unless (defined($dt->{ELEMENTS}));
495                 return find_largest_alignment($dt);
496         } elsif (($dt->{TYPE} eq "PIPE")) {
497                 return 5;
498         }
499
500         die("Unknown data type type $dt->{TYPE}");
501 }
502
503 sub ParseElement($$$)
504 {
505         my ($e, $pointer_default, $ms_union) = @_;
506
507         $e->{TYPE} = expandAlias($e->{TYPE});
508
509         if (ref($e->{TYPE}) eq "HASH") {
510                 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default, $ms_union);
511         }
512
513         return {
514                 NAME => $e->{NAME},
515                 TYPE => $e->{TYPE},
516                 PROPERTIES => $e->{PROPERTIES},
517                 LEVELS => GetElementLevelTable($e, $pointer_default, $ms_union),
518                 REPRESENTATION_TYPE => ($e->{PROPERTIES}->{represent_as} or $e->{TYPE}),
519                 ALIGN => align_type($e->{TYPE}),
520                 ORIGINAL => $e
521         };
522 }
523
524 sub ParseName($)
525 {
526         my ($e) = @_;
527         my $name = $e->{NAME};
528
529         return $name if defined($name);
530
531         my $parent = $e->{PARENT};
532         my $parent_name = undef;
533
534         while (not defined($parent_name)) {
535                 last unless defined($parent);
536
537                 $parent_name = $parent->{NAME};
538                 $parent = $parent->{PARENT};
539         }
540
541         $parent_name = "global" unless defined($parent_name);
542         $name = "__pidl_".$e->{TYPE}."_".$parent_name."_".$e->{LINE};
543
544         return $name;
545 }
546
547 sub ParseStruct($$$)
548 {
549         my ($struct, $pointer_default, $ms_union) = @_;
550         my @elements = ();
551         my $surrounding = undef;
552         my $name = ParseName($struct);
553
554         return {
555                 TYPE => "STRUCT",
556                 NAME => $name,
557                 SURROUNDING_ELEMENT => undef,
558                 ELEMENTS => undef,
559                 PROPERTIES => $struct->{PROPERTIES},
560                 ORIGINAL => $struct,
561                 ALIGN => undef
562         } unless defined($struct->{ELEMENTS});
563
564         CheckPointerTypes($struct, $pointer_default);
565
566         foreach my $x (@{$struct->{ELEMENTS}}) 
567         {
568                 my $e = ParseElement($x, $pointer_default, $ms_union);
569                 if ($x != $struct->{ELEMENTS}[-1] and 
570                         $e->{LEVELS}[0]->{IS_SURROUNDING}) {
571                         fatal($x, "conformant member not at end of struct");
572                 }
573                 push @elements, $e;
574         }
575
576         my $e = $elements[-1];
577         if (defined($e) and defined($e->{LEVELS}[0]->{IS_SURROUNDING}) and
578                 $e->{LEVELS}[0]->{IS_SURROUNDING}) {
579                 $surrounding = $e;
580         }
581
582         if (defined $e->{TYPE} && $e->{TYPE} eq "string"
583             &&  property_matches($e, "flag", ".*LIBNDR_FLAG_STR_CONFORMANT.*")) {
584                 $surrounding = $struct->{ELEMENTS}[-1];
585         }
586
587         my $align = align_type($name);
588
589         return {
590                 TYPE => "STRUCT",
591                 NAME => $name,
592                 SURROUNDING_ELEMENT => $surrounding,
593                 ELEMENTS => \@elements,
594                 PROPERTIES => $struct->{PROPERTIES},
595                 ORIGINAL => $struct,
596                 ALIGN => $align
597         };
598 }
599
600 sub ParseUnion($$)
601 {
602         my ($e, $pointer_default, $ms_union) = @_;
603         my @elements = ();
604         my $is_ms_union = $ms_union;
605         $is_ms_union = 1 if has_property($e, "ms_union");
606         my $hasdefault = 0;
607         my $switch_type = has_property($e, "switch_type");
608         unless (defined($switch_type)) { $switch_type = "uint32"; }
609         if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
610         my $name = ParseName($e);
611
612         return {
613                 TYPE => "UNION",
614                 NAME => $name,
615                 SWITCH_TYPE => $switch_type,
616                 ELEMENTS => undef,
617                 PROPERTIES => $e->{PROPERTIES},
618                 HAS_DEFAULT => $hasdefault,
619                 IS_MS_UNION => $is_ms_union,
620                 ORIGINAL => $e,
621                 ALIGN => undef
622         } unless defined($e->{ELEMENTS});
623
624         CheckPointerTypes($e, $pointer_default);
625
626         foreach my $x (@{$e->{ELEMENTS}}) 
627         {
628                 my $t;
629                 if ($x->{TYPE} eq "EMPTY") {
630                         $t = { TYPE => "EMPTY" };
631                 } else {
632                         $t = ParseElement($x, $pointer_default, $ms_union);
633                 }
634                 if (has_property($x, "default")) {
635                         $t->{CASE} = "default";
636                         $hasdefault = 1;
637                 } elsif (defined($x->{PROPERTIES}->{case})) {
638                         $t->{CASE} = "case $x->{PROPERTIES}->{case}";
639                 } else {
640                         die("Union element $x->{NAME} has neither default nor case property");
641                 }
642                 push @elements, $t;
643         }
644
645         my $align = align_type($name);
646
647         return {
648                 TYPE => "UNION",
649                 NAME => $name,
650                 SWITCH_TYPE => $switch_type,
651                 ELEMENTS => \@elements,
652                 PROPERTIES => $e->{PROPERTIES},
653                 HAS_DEFAULT => $hasdefault,
654                 IS_MS_UNION => $is_ms_union,
655                 ORIGINAL => $e,
656                 ALIGN => $align
657         };
658 }
659
660 sub ParseEnum($$)
661 {
662         my ($e, $pointer_default, $ms_union) = @_;
663         my $name = ParseName($e);
664
665         return {
666                 TYPE => "ENUM",
667                 NAME => $name,
668                 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
669                 ELEMENTS => $e->{ELEMENTS},
670                 PROPERTIES => $e->{PROPERTIES},
671                 ORIGINAL => $e
672         };
673 }
674
675 sub ParseBitmap($$$)
676 {
677         my ($e, $pointer_default, $ms_union) = @_;
678         my $name = ParseName($e);
679
680         return {
681                 TYPE => "BITMAP",
682                 NAME => $name,
683                 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
684                 ELEMENTS => $e->{ELEMENTS},
685                 PROPERTIES => $e->{PROPERTIES},
686                 ORIGINAL => $e
687         };
688 }
689
690 sub ParsePipe($$$)
691 {
692         my ($pipe, $pointer_default, $ms_union) = @_;
693
694         my $pname = $pipe->{NAME};
695         $pname = $pipe->{PARENT}->{NAME} unless defined $pname;
696
697         if (not defined($pipe->{PROPERTIES})
698             and defined($pipe->{PARENT}->{PROPERTIES})) {
699                 $pipe->{PROPERTIES} = $pipe->{PARENT}->{PROPERTIES};
700         }
701
702         if (ref($pipe->{DATA}) eq "HASH") {
703                 if (not defined($pipe->{DATA}->{PROPERTIES})
704                     and defined($pipe->{PROPERTIES})) {
705                         $pipe->{DATA}->{PROPERTIES} = $pipe->{PROPERTIES};
706                 }
707         }
708
709         my $struct = ParseStruct($pipe->{DATA}, $pointer_default, $ms_union);
710         $struct->{ALIGN} = 5;
711         $struct->{NAME} = "$pname\_chunk";
712
713         # 'count' is element [0] and 'array' [1]
714         my $e = $struct->{ELEMENTS}[1];
715         # level [0] is of type "ARRAY"
716         my $l = $e->{LEVELS}[1];
717
718         # here we check that pipe elements have a fixed size type
719         while (defined($l)) {
720                 my $cl = $l;
721                 $l = GetNextLevel($e, $cl);
722                 if ($cl->{TYPE} ne "DATA") {
723                         fatal($pipe, el_name($pipe) . ": pipe contains non DATA level");
724                 }
725
726                 # for now we only support scalars
727                 next if is_fixed_size_scalar($cl->{DATA_TYPE});
728
729                 fatal($pipe, el_name($pipe) . ": pipe contains non fixed size type[$cl->{DATA_TYPE}]");
730         }
731
732         return {
733                 TYPE => "PIPE",
734                 NAME => $pipe->{NAME},
735                 DATA => $struct,
736                 PROPERTIES => $pipe->{PROPERTIES},
737                 ORIGINAL => $pipe,
738         };
739 }
740
741 sub ParseType($$$)
742 {
743         my ($d, $pointer_default, $ms_union) = @_;
744
745         my $data = {
746                 STRUCT => \&ParseStruct,
747                 UNION => \&ParseUnion,
748                 ENUM => \&ParseEnum,
749                 BITMAP => \&ParseBitmap,
750                 TYPEDEF => \&ParseTypedef,
751                 PIPE => \&ParsePipe,
752         }->{$d->{TYPE}}->($d, $pointer_default, $ms_union);
753
754         return $data;
755 }
756
757 sub ParseTypedef($$)
758 {
759         my ($d, $pointer_default, $ms_union) = @_;
760
761         my $data;
762
763         if (ref($d->{DATA}) eq "HASH") {
764                 if (defined($d->{DATA}->{PROPERTIES})
765                     and not defined($d->{PROPERTIES})) {
766                         $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
767                 }
768
769                 $data = ParseType($d->{DATA}, $pointer_default, $ms_union);
770                 $data->{ALIGN} = align_type($d->{NAME});
771         } else {
772                 $data = getType($d->{DATA});
773         }
774
775         return {
776                 NAME => $d->{NAME},
777                 TYPE => $d->{TYPE},
778                 PROPERTIES => $d->{PROPERTIES},
779                 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default, $ms_union),
780                 DATA => $data,
781                 ORIGINAL => $d
782         };
783 }
784
785 sub ParseConst($$)
786 {
787         my ($ndr,$d) = @_;
788
789         return $d;
790 }
791
792 sub ParseFunction($$$$)
793 {
794         my ($ndr,$d,$opnum,$ms_union) = @_;
795         my @elements = ();
796         my $rettype = undef;
797         my $thisopnum = undef;
798
799         CheckPointerTypes($d, "ref");
800
801         if (not defined($d->{PROPERTIES}{noopnum})) {
802                 $thisopnum = ${$opnum};
803                 ${$opnum}++;
804         }
805
806         foreach my $x (@{$d->{ELEMENTS}}) {
807                 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default}, $ms_union);
808                 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
809                 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
810
811                 push (@elements, $e);
812         }
813
814         if ($d->{RETURN_TYPE} ne "void") {
815                 $rettype = expandAlias($d->{RETURN_TYPE});
816         }
817         
818         return {
819                         NAME => $d->{NAME},
820                         TYPE => "FUNCTION",
821                         OPNUM => $thisopnum,
822                         RETURN_TYPE => $rettype,
823                         PROPERTIES => $d->{PROPERTIES},
824                         ELEMENTS => \@elements,
825                         ORIGINAL => $d
826                 };
827 }
828
829 sub ReturnTypeElement($)
830 {
831         my ($fn) = @_;
832
833         return undef unless defined($fn->{RETURN_TYPE});
834
835         my $e = {
836                 "NAME" => "result",
837                 "TYPE" => $fn->{RETURN_TYPE},
838                 "PROPERTIES" => undef,
839                 "POINTERS" => 0,
840                 "ARRAY_LEN" => [],
841                 "FILE" => $fn->{FILE},
842                 "LINE" => $fn->{LINE},
843         };
844
845         return ParseElement($e, 0, 0);
846 }
847
848 sub CheckPointerTypes($$)
849 {
850         my ($s,$default) = @_;
851
852         return unless defined($s->{ELEMENTS});
853
854         foreach my $e (@{$s->{ELEMENTS}}) {
855                 if ($e->{POINTERS} and not defined(pointer_type($e))) {
856                         $e->{PROPERTIES}->{$default} = '1';
857                 }
858         }
859 }
860
861 sub FindNestedTypes($$)
862 {
863         sub FindNestedTypes($$);
864         my ($l, $t) = @_;
865
866         return unless defined($t->{ELEMENTS});
867         return if ($t->{TYPE} eq "ENUM");
868         return if ($t->{TYPE} eq "BITMAP");
869
870         foreach (@{$t->{ELEMENTS}}) {
871                 if (ref($_->{TYPE}) eq "HASH") {
872                         push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
873                         FindNestedTypes($l, $_->{TYPE});
874                 }
875         }
876 }
877
878 sub ParseInterface($)
879 {
880         my $idl = shift;
881         my @types = ();
882         my @consts = ();
883         my @functions = ();
884         my @endpoints;
885         my $opnum = 0;
886         my $version;
887         my $ms_union = 0;
888         $ms_union = 1 if has_property($idl, "ms_union");
889
890         if (not has_property($idl, "pointer_default")) {
891                 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
892                 # and "unique" in Microsoft Extensions mode (default)
893                 $idl->{PROPERTIES}->{pointer_default} = "unique";
894         }
895
896         foreach my $d (@{$idl->{DATA}}) {
897                 if ($d->{TYPE} eq "FUNCTION") {
898                         push (@functions, ParseFunction($idl, $d, \$opnum, $ms_union));
899                 } elsif ($d->{TYPE} eq "CONST") {
900                         push (@consts, ParseConst($idl, $d));
901                 } else {
902                         push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}, $ms_union));
903                         FindNestedTypes(\@types, $d);
904                 }
905         }
906
907         $version = "0.0";
908
909         if(defined $idl->{PROPERTIES}->{version}) { 
910                 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
911                 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
912                                 $version = $idl->{PROPERTIES}->{version};
913                 } else {
914                                 $version = $if_version[1] << 16 | $if_version[0];
915                 }
916         }
917
918         # If no endpoint is set, default to the interface name as a named pipe
919         if (!defined $idl->{PROPERTIES}->{endpoint}) {
920                 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
921         } else {
922                 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
923         }
924
925         return { 
926                 NAME => $idl->{NAME},
927                 UUID => lc(has_property($idl, "uuid")),
928                 VERSION => $version,
929                 TYPE => "INTERFACE",
930                 PROPERTIES => $idl->{PROPERTIES},
931                 FUNCTIONS => \@functions,
932                 CONSTS => \@consts,
933                 TYPES => \@types,
934                 ENDPOINTS => \@endpoints,
935                 ORIGINAL => $idl
936         };
937 }
938
939 # Convert a IDL tree to a NDR tree
940 # Gives a result tree describing all that's necessary for easily generating
941 # NDR parsers / generators
942 sub Parse($)
943 {
944         my $idl = shift;
945
946         return undef unless (defined($idl));
947
948         Parse::Pidl::NDR::Validate($idl);
949         
950         my @ndr = ();
951
952         foreach (@{$idl}) {
953                 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
954                 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
955                 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
956         }
957
958         return \@ndr;
959 }
960
961 sub GetNextLevel($$)
962 {
963         my $e = shift;
964         my $fl = shift;
965
966         my $seen = 0;
967
968         foreach my $l (@{$e->{LEVELS}}) {
969                 return $l if ($seen);
970                 ($seen = 1) if ($l == $fl);
971         }
972
973         return undef;
974 }
975
976 sub GetPrevLevel($$)
977 {
978         my ($e,$fl) = @_;
979         my $prev = undef;
980
981         foreach my $l (@{$e->{LEVELS}}) {
982                 (return $prev) if ($l == $fl);
983                 $prev = $l;
984         }
985
986         return undef;
987 }
988
989 sub ContainsString($)
990 {
991         my ($e) = @_;
992
993         if (property_matches($e, "flag", ".*STR_NULLTERM.*")) {
994                 return 1;
995         }
996         if (exists($e->{LEVELS}) and $e->{LEVELS}->[0]->{TYPE} eq "ARRAY" and
997                 ($e->{LEVELS}->[0]->{IS_FIXED} or $e->{LEVELS}->[0]->{IS_INLINE}) and
998                 has_property($e, "charset"))
999         {
1000                 return 1;
1001         }
1002
1003         foreach my $l (@{$e->{LEVELS}}) {
1004                 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
1005         }
1006         if (property_matches($e, "charset", ".*DOS.*")) {
1007                 return 1;
1008         }
1009
1010         return 0;
1011 }
1012
1013 sub ContainsDeferred($$)
1014 {
1015         my ($e,$l) = @_;
1016
1017         return 1 if ($l->{CONTAINS_DEFERRED});
1018
1019         while ($l = GetNextLevel($e,$l))
1020         {
1021                 return 1 if ($l->{IS_DEFERRED}); 
1022                 return 1 if ($l->{CONTAINS_DEFERRED});
1023         } 
1024         
1025         return 0;
1026 }
1027
1028 sub ContainsPipe($$)
1029 {
1030         my ($e,$l) = @_;
1031
1032         return 1 if ($l->{TYPE} eq "PIPE");
1033
1034         while ($l = GetNextLevel($e,$l))
1035         {
1036                 return 1 if ($l->{TYPE} eq "PIPE");
1037         }
1038
1039         return 0;
1040 }
1041
1042 sub el_name($)
1043 {
1044         my $e = shift;
1045         my $name = "<ANONYMOUS>";
1046
1047         $name = $e->{NAME} if defined($e->{NAME});
1048
1049         if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
1050                 return "$e->{PARENT}->{NAME}.$name";
1051         }
1052
1053         if (defined($e->{PARENT}) and
1054             defined($e->{PARENT}->{PARENT}) and
1055             defined($e->{PARENT}->{PARENT}->{NAME})) {
1056                 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
1057         }
1058
1059         return $name;
1060 }
1061
1062 ###################################
1063 # find a sibling var in a structure
1064 sub find_sibling($$)
1065 {
1066         my($e,$name) = @_;
1067         my($fn) = $e->{PARENT};
1068
1069         if ($name =~ /\*(.*)/) {
1070                 $name = $1;
1071         }
1072
1073         for my $e2 (@{$fn->{ELEMENTS}}) {
1074                 return $e2 if ($e2->{NAME} eq $name);
1075         }
1076
1077         return undef;
1078 }
1079
1080 my %property_list = (
1081         # interface
1082         "helpstring"            => ["INTERFACE", "FUNCTION"],
1083         "version"               => ["INTERFACE"],
1084         "uuid"                  => ["INTERFACE"],
1085         "endpoint"              => ["INTERFACE"],
1086         "pointer_default"       => ["INTERFACE"],
1087         "helper"                => ["INTERFACE"],
1088         "pyhelper"              => ["INTERFACE"],
1089         "authservice"           => ["INTERFACE"],
1090         "restricted"            => ["INTERFACE"],
1091         "no_srv_register"       => ["INTERFACE"],
1092
1093         # dcom
1094         "object"                => ["INTERFACE"],
1095         "local"                 => ["INTERFACE", "FUNCTION"],
1096         "iid_is"                => ["ELEMENT"],
1097         "call_as"               => ["FUNCTION"],
1098         "idempotent"            => ["FUNCTION"],
1099
1100         # function
1101         "noopnum"               => ["FUNCTION"],
1102         "in"                    => ["ELEMENT"],
1103         "out"                   => ["ELEMENT"],
1104
1105         # pointer
1106         "ref"                   => ["ELEMENT", "TYPEDEF"],
1107         "ptr"                   => ["ELEMENT", "TYPEDEF"],
1108         "unique"                => ["ELEMENT", "TYPEDEF"],
1109         "ignore"                => ["ELEMENT"],
1110         "relative"              => ["ELEMENT", "TYPEDEF"],
1111         "relative_short"        => ["ELEMENT", "TYPEDEF"],
1112         "null_is_ffffffff"      => ["ELEMENT"],
1113         "relative_base"         => ["TYPEDEF", "STRUCT", "UNION"],
1114
1115         "gensize"               => ["TYPEDEF", "STRUCT", "UNION"],
1116         "value"                 => ["ELEMENT"],
1117         "flag"                  => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1118
1119         # generic
1120         "public"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1121         "nopush"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1122         "nopull"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "PIPE"],
1123         "nosize"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
1124         "noprint"               => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT", "PIPE"],
1125         "nopython"              => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
1126         "todo"                  => ["FUNCTION"],
1127         "skip"                  => ["ELEMENT"],
1128         "skip_noinit"           => ["ELEMENT"],
1129
1130         # union
1131         "switch_is"             => ["ELEMENT"],
1132         "switch_type"           => ["ELEMENT", "UNION"],
1133         "nodiscriminant"        => ["UNION"],
1134         "ms_union"              => ["INTERFACE", "UNION"],
1135         "case"                  => ["ELEMENT"],
1136         "default"               => ["ELEMENT"],
1137
1138         "represent_as"          => ["ELEMENT"],
1139         "transmit_as"           => ["ELEMENT"],
1140
1141         # subcontext
1142         "subcontext"            => ["ELEMENT"],
1143         "subcontext_size"       => ["ELEMENT"],
1144         "compression"           => ["ELEMENT"],
1145
1146         # enum
1147         "enum8bit"              => ["ENUM"],
1148         "enum16bit"             => ["ENUM"],
1149         "v1_enum"               => ["ENUM"],
1150
1151         # bitmap
1152         "bitmap8bit"            => ["BITMAP"],
1153         "bitmap16bit"           => ["BITMAP"],
1154         "bitmap32bit"           => ["BITMAP"],
1155         "bitmap64bit"           => ["BITMAP"],
1156
1157         # array
1158         "range"                 => ["ELEMENT", "PIPE"],
1159         "size_is"               => ["ELEMENT"],
1160         "string"                => ["ELEMENT"],
1161         "noheader"              => ["ELEMENT"],
1162         "charset"               => ["ELEMENT"],
1163         "length_is"             => ["ELEMENT"],
1164         "to_null"               => ["ELEMENT"],
1165 );
1166
1167 #####################################################################
1168 # check for unknown properties
1169 sub ValidProperties($$)
1170 {
1171         my ($e,$t) = @_;
1172
1173         return unless defined $e->{PROPERTIES};
1174
1175         foreach my $key (keys %{$e->{PROPERTIES}}) {
1176                 warning($e, el_name($e) . ": unknown property '$key'")
1177                         unless defined($property_list{$key});
1178
1179                 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
1180                         unless grep(/^$t$/, @{$property_list{$key}});
1181         }
1182 }
1183
1184 sub mapToScalar($)
1185 {
1186         sub mapToScalar($);
1187         my $t = shift;
1188         return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
1189         my $ti = getType($t);
1190
1191         if (not defined ($ti)) {
1192                 return undef;
1193         } elsif ($ti->{TYPE} eq "TYPEDEF") {
1194                 return mapToScalar($ti->{DATA});
1195         } elsif ($ti->{TYPE} eq "ENUM") {
1196                 return Parse::Pidl::Typelist::enum_type_fn($ti);
1197         } elsif ($ti->{TYPE} eq "BITMAP") {
1198                 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
1199         }
1200
1201         return undef;
1202 }
1203
1204 #####################################################################
1205 # validate an element
1206 sub ValidElement($)
1207 {
1208         my $e = shift;
1209
1210         ValidProperties($e,"ELEMENT");
1211
1212         # Check whether switches are used correctly.
1213         if (my $switch = has_property($e, "switch_is")) {
1214                 my $e2 = find_sibling($e, $switch);
1215                 my $type = getType($e->{TYPE});
1216
1217                 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
1218                         fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1219                 }
1220
1221                 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
1222                         my $discriminator_type = has_property($type->{DATA}, "switch_type");
1223                         $discriminator_type = "uint32" unless defined ($discriminator_type);
1224
1225                         my $t1 = mapScalarType(mapToScalar($discriminator_type));
1226
1227                         if (not defined($t1)) {
1228                                 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1229                         }
1230
1231                         my $t2 = mapScalarType(mapToScalar($e2->{TYPE}));
1232                         if (not defined($t2)) {
1233                                 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1234                         }
1235
1236                         if ($t1 ne $t2) {
1237                                 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1238                         }
1239                 }
1240         }
1241
1242         if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1243                 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1244         }
1245
1246         if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1247                 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1248         }
1249
1250         if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1251                 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1252         }
1253
1254         if (has_property($e, "represent_as") and has_property($e, "value")) {
1255                 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1256         }
1257
1258         if (has_property($e, "subcontext")) {
1259                 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1260         }
1261
1262         if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1263                 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1264         }
1265
1266         if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1267                 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1268         }
1269
1270         if (!$e->{POINTERS} && (
1271                 has_property($e, "ptr") or
1272                 has_property($e, "unique") or
1273                 has_property($e, "relative") or
1274                 has_property($e, "relative_short") or
1275                 has_property($e, "ref"))) {
1276                 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");      
1277         }
1278 }
1279
1280 #####################################################################
1281 # validate an enum
1282 sub ValidEnum($)
1283 {
1284         my ($enum) = @_;
1285
1286         ValidProperties($enum, "ENUM");
1287 }
1288
1289 #####################################################################
1290 # validate a bitmap
1291 sub ValidBitmap($)
1292 {
1293         my ($bitmap) = @_;
1294
1295         ValidProperties($bitmap, "BITMAP");
1296 }
1297
1298 #####################################################################
1299 # validate a struct
1300 sub ValidStruct($)
1301 {
1302         my($struct) = shift;
1303
1304         ValidProperties($struct, "STRUCT");
1305
1306         return unless defined($struct->{ELEMENTS});
1307
1308         foreach my $e (@{$struct->{ELEMENTS}}) {
1309                 $e->{PARENT} = $struct;
1310                 ValidElement($e);
1311         }
1312 }
1313
1314 #####################################################################
1315 # parse a union
1316 sub ValidUnion($)
1317 {
1318         my($union) = shift;
1319
1320         ValidProperties($union,"UNION");
1321
1322         if (has_property($union->{PARENT}, "nodiscriminant") and 
1323                 has_property($union->{PARENT}, "switch_type")) {
1324                 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1325         }
1326
1327         return unless defined($union->{ELEMENTS});
1328
1329         foreach my $e (@{$union->{ELEMENTS}}) {
1330                 $e->{PARENT} = $union;
1331
1332                 if (defined($e->{PROPERTIES}->{default}) and 
1333                         defined($e->{PROPERTIES}->{case})) {
1334                         fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1335                 }
1336                 
1337                 unless (defined ($e->{PROPERTIES}->{default}) or 
1338                                 defined ($e->{PROPERTIES}->{case})) {
1339                         fatal($e, "Union member $e->{NAME} must have default or case property");
1340                 }
1341
1342                 if (has_property($e, "ref")) {
1343                         fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1344                 }
1345
1346
1347                 ValidElement($e);
1348         }
1349 }
1350
1351 #####################################################################
1352 # validate a pipe
1353 sub ValidPipe($)
1354 {
1355         my ($pipe) = @_;
1356         my $struct = $pipe->{DATA};
1357
1358         ValidProperties($pipe, "PIPE");
1359
1360         $struct->{PARENT} = $pipe;
1361
1362         $struct->{FILE} = $pipe->{FILE} unless defined($struct->{FILE});
1363         $struct->{LINE} = $pipe->{LINE} unless defined($struct->{LINE});
1364
1365         ValidType($struct);
1366 }
1367
1368 #####################################################################
1369 # parse a typedef
1370 sub ValidTypedef($)
1371 {
1372         my($typedef) = shift;
1373         my $data = $typedef->{DATA};
1374
1375         ValidProperties($typedef, "TYPEDEF");
1376
1377         return unless (ref($data) eq "HASH");
1378
1379         $data->{PARENT} = $typedef;
1380
1381         $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1382         $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1383
1384         ValidType($data);
1385 }
1386
1387 #####################################################################
1388 # validate a function
1389 sub ValidFunction($)
1390 {
1391         my($fn) = shift;
1392
1393         ValidProperties($fn,"FUNCTION");
1394
1395         foreach my $e (@{$fn->{ELEMENTS}}) {
1396                 $e->{PARENT} = $fn;
1397                 if (has_property($e, "ref") && !$e->{POINTERS}) {
1398                         fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1399                 }
1400                 ValidElement($e);
1401         }
1402 }
1403
1404 #####################################################################
1405 # validate a type
1406 sub ValidType($)
1407 {
1408         my ($t) = @_;
1409
1410         { 
1411                 TYPEDEF => \&ValidTypedef,
1412                 STRUCT => \&ValidStruct,
1413                 UNION => \&ValidUnion,
1414                 ENUM => \&ValidEnum,
1415                 BITMAP => \&ValidBitmap,
1416                 PIPE => \&ValidPipe
1417         }->{$t->{TYPE}}->($t);
1418 }
1419
1420 #####################################################################
1421 # parse the interface definitions
1422 sub ValidInterface($)
1423 {
1424         my($interface) = shift;
1425         my($data) = $interface->{DATA};
1426
1427         if (has_property($interface, "helper")) {
1428                 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1429         }
1430
1431         ValidProperties($interface,"INTERFACE");
1432
1433         if (has_property($interface, "pointer_default")) {
1434                 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/, 
1435                                         ("ref", "unique", "ptr"))) {
1436                         fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1437                 }
1438         }
1439
1440         if (has_property($interface, "object")) {
1441                 if (has_property($interface, "version") && 
1442                         $interface->{PROPERTIES}->{version} != 0) {
1443                         fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1444                 }
1445
1446                 if (!defined($interface->{BASE}) && 
1447                         not ($interface->{NAME} eq "IUnknown")) {
1448                         fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1449                 }
1450         }
1451                 
1452         foreach my $d (@{$data}) {
1453                 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1454                 ($d->{TYPE} eq "TYPEDEF" or 
1455                  $d->{TYPE} eq "STRUCT" or
1456                  $d->{TYPE} eq "UNION" or 
1457                  $d->{TYPE} eq "ENUM" or
1458                  $d->{TYPE} eq "BITMAP" or
1459                  $d->{TYPE} eq "PIPE") && ValidType($d);
1460         }
1461
1462 }
1463
1464 #####################################################################
1465 # Validate an IDL structure
1466 sub Validate($)
1467 {
1468         my($idl) = shift;
1469
1470         foreach my $x (@{$idl}) {
1471                 ($x->{TYPE} eq "INTERFACE") && 
1472                     ValidInterface($x);
1473                 ($x->{TYPE} eq "IMPORTLIB") &&
1474                         fatal($x, "importlib() not supported");
1475         }
1476 }
1477
1478 sub is_charset_array($$)
1479 {
1480         my ($e,$l) = @_;
1481
1482         return 0 if ($l->{TYPE} ne "ARRAY");
1483
1484         my $nl = GetNextLevel($e,$l);
1485
1486         return 0 unless ($nl->{TYPE} eq "DATA");
1487
1488         return has_property($e, "charset");
1489 }
1490
1491
1492
1493 1;