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