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