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