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