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