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