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