3edb9b732f26454f922aeed3cde915051153b330
[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, $ms_union) = @_;
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, $ms_union) = @_;
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, $ms_union) = @_;
438
439         $e->{TYPE} = expandAlias($e->{TYPE});
440
441         if (ref($e->{TYPE}) eq "HASH") {
442                 $e->{TYPE} = ParseType($e->{TYPE}, $pointer_default, $ms_union);
443         }
444
445         return {
446                 NAME => $e->{NAME},
447                 TYPE => $e->{TYPE},
448                 PROPERTIES => $e->{PROPERTIES},
449                 LEVELS => GetElementLevelTable($e, $pointer_default, $ms_union),
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, $ms_union) = @_;
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, $ms_union);
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, $ms_union) = @_;
514         my @elements = ();
515         my $is_ms_union = $ms_union;
516         $is_ms_union = 1 if has_property($e, "ms_union");
517         my $hasdefault = 0;
518         my $switch_type = has_property($e, "switch_type");
519         unless (defined($switch_type)) { $switch_type = "uint32"; }
520         if (has_property($e, "nodiscriminant")) { $switch_type = undef; }
521
522         return {
523                 TYPE => "UNION",
524                 NAME => $e->{NAME},
525                 SWITCH_TYPE => $switch_type,
526                 ELEMENTS => undef,
527                 PROPERTIES => $e->{PROPERTIES},
528                 HAS_DEFAULT => $hasdefault,
529                 IS_MS_UNION => $is_ms_union,
530                 ORIGINAL => $e,
531                 ALIGN => undef
532         } unless defined($e->{ELEMENTS});
533
534         CheckPointerTypes($e, $pointer_default);
535
536         foreach my $x (@{$e->{ELEMENTS}}) 
537         {
538                 my $t;
539                 if ($x->{TYPE} eq "EMPTY") {
540                         $t = { TYPE => "EMPTY" };
541                 } else {
542                         $t = ParseElement($x, $pointer_default, $ms_union);
543                 }
544                 if (has_property($x, "default")) {
545                         $t->{CASE} = "default";
546                         $hasdefault = 1;
547                 } elsif (defined($x->{PROPERTIES}->{case})) {
548                         $t->{CASE} = "case $x->{PROPERTIES}->{case}";
549                 } else {
550                         die("Union element $x->{NAME} has neither default nor case property");
551                 }
552                 push @elements, $t;
553         }
554
555         my $align = undef;
556         if ($e->{NAME}) {
557                 $align = align_type($e->{NAME});
558         }
559
560         return {
561                 TYPE => "UNION",
562                 NAME => $e->{NAME},
563                 SWITCH_TYPE => $switch_type,
564                 ELEMENTS => \@elements,
565                 PROPERTIES => $e->{PROPERTIES},
566                 HAS_DEFAULT => $hasdefault,
567                 IS_MS_UNION => $is_ms_union,
568                 ORIGINAL => $e,
569                 ALIGN => $align
570         };
571 }
572
573 sub ParseEnum($$)
574 {
575         my ($e, $pointer_default, $ms_union) = @_;
576
577         return {
578                 TYPE => "ENUM",
579                 NAME => $e->{NAME},
580                 BASE_TYPE => Parse::Pidl::Typelist::enum_type_fn($e),
581                 ELEMENTS => $e->{ELEMENTS},
582                 PROPERTIES => $e->{PROPERTIES},
583                 ORIGINAL => $e
584         };
585 }
586
587 sub ParseBitmap($$$)
588 {
589         my ($e, $pointer_default, $ms_union) = @_;
590
591         return {
592                 TYPE => "BITMAP",
593                 NAME => $e->{NAME},
594                 BASE_TYPE => Parse::Pidl::Typelist::bitmap_type_fn($e),
595                 ELEMENTS => $e->{ELEMENTS},
596                 PROPERTIES => $e->{PROPERTIES},
597                 ORIGINAL => $e
598         };
599 }
600
601 sub ParseType($$$)
602 {
603         my ($d, $pointer_default, $ms_union) = @_;
604
605         my $data = {
606                 STRUCT => \&ParseStruct,
607                 UNION => \&ParseUnion,
608                 ENUM => \&ParseEnum,
609                 BITMAP => \&ParseBitmap,
610                 TYPEDEF => \&ParseTypedef,
611         }->{$d->{TYPE}}->($d, $pointer_default, $ms_union);
612
613         return $data;
614 }
615
616 sub ParseTypedef($$)
617 {
618         my ($d, $pointer_default, $ms_union) = @_;
619
620         my $data;
621
622         if (ref($d->{DATA}) eq "HASH") {
623                 if (defined($d->{DATA}->{PROPERTIES})
624                     and not defined($d->{PROPERTIES})) {
625                         $d->{PROPERTIES} = $d->{DATA}->{PROPERTIES};
626                 }
627
628                 $data = ParseType($d->{DATA}, $pointer_default, $ms_union);
629                 $data->{ALIGN} = align_type($d->{NAME});
630         } else {
631                 $data = getType($d->{DATA});
632         }
633
634         return {
635                 NAME => $d->{NAME},
636                 TYPE => $d->{TYPE},
637                 PROPERTIES => $d->{PROPERTIES},
638                 LEVELS => GetTypedefLevelTable($d, $data, $pointer_default, $ms_union),
639                 DATA => $data,
640                 ORIGINAL => $d
641         };
642 }
643
644 sub ParseConst($$)
645 {
646         my ($ndr,$d) = @_;
647
648         return $d;
649 }
650
651 sub ParseFunction($$$$)
652 {
653         my ($ndr,$d,$opnum,$ms_union) = @_;
654         my @elements = ();
655         my $rettype = undef;
656         my $thisopnum = undef;
657
658         CheckPointerTypes($d, "ref");
659
660         if (not defined($d->{PROPERTIES}{noopnum})) {
661                 $thisopnum = ${$opnum};
662                 ${$opnum}++;
663         }
664
665         foreach my $x (@{$d->{ELEMENTS}}) {
666                 my $e = ParseElement($x, $ndr->{PROPERTIES}->{pointer_default}, $ms_union);
667                 push (@{$e->{DIRECTION}}, "in") if (has_property($x, "in"));
668                 push (@{$e->{DIRECTION}}, "out") if (has_property($x, "out"));
669
670                 push (@elements, $e);
671         }
672
673         if ($d->{RETURN_TYPE} ne "void") {
674                 $rettype = expandAlias($d->{RETURN_TYPE});
675         }
676         
677         return {
678                         NAME => $d->{NAME},
679                         TYPE => "FUNCTION",
680                         OPNUM => $thisopnum,
681                         RETURN_TYPE => $rettype,
682                         PROPERTIES => $d->{PROPERTIES},
683                         ELEMENTS => \@elements,
684                         ORIGINAL => $d
685                 };
686 }
687
688 sub CheckPointerTypes($$)
689 {
690         my ($s,$default) = @_;
691
692         return unless defined($s->{ELEMENTS});
693
694         foreach my $e (@{$s->{ELEMENTS}}) {
695                 if ($e->{POINTERS} and not defined(pointer_type($e))) {
696                         $e->{PROPERTIES}->{$default} = '1';
697                 }
698         }
699 }
700
701 sub FindNestedTypes($$)
702 {
703         sub FindNestedTypes($$);
704         my ($l, $t) = @_;
705
706         return unless defined($t->{ELEMENTS});
707         return if ($t->{TYPE} eq "ENUM");
708         return if ($t->{TYPE} eq "BITMAP");
709
710         foreach (@{$t->{ELEMENTS}}) {
711                 if (ref($_->{TYPE}) eq "HASH") {
712                         push (@$l, $_->{TYPE}) if (defined($_->{TYPE}->{NAME}));
713                         FindNestedTypes($l, $_->{TYPE});
714                 }
715         }
716 }
717
718 sub ParseInterface($)
719 {
720         my $idl = shift;
721         my @types = ();
722         my @consts = ();
723         my @functions = ();
724         my @endpoints;
725         my $opnum = 0;
726         my $version;
727         my $ms_union = 0;
728         $ms_union = 1 if has_property($idl, "ms_union");
729
730         if (not has_property($idl, "pointer_default")) {
731                 # MIDL defaults to "ptr" in DCE compatible mode (/osf)
732                 # and "unique" in Microsoft Extensions mode (default)
733                 $idl->{PROPERTIES}->{pointer_default} = "unique";
734         }
735
736         foreach my $d (@{$idl->{DATA}}) {
737                 if ($d->{TYPE} eq "FUNCTION") {
738                         push (@functions, ParseFunction($idl, $d, \$opnum, $ms_union));
739                 } elsif ($d->{TYPE} eq "CONST") {
740                         push (@consts, ParseConst($idl, $d));
741                 } else {
742                         push (@types, ParseType($d, $idl->{PROPERTIES}->{pointer_default}, $ms_union));
743                         FindNestedTypes(\@types, $d);
744                 }
745         }
746
747         $version = "0.0";
748
749         if(defined $idl->{PROPERTIES}->{version}) { 
750                 my @if_version = split(/\./, $idl->{PROPERTIES}->{version});
751                 if ($if_version[0] == $idl->{PROPERTIES}->{version}) {
752                                 $version = $idl->{PROPERTIES}->{version};
753                 } else {
754                                 $version = $if_version[1] << 16 | $if_version[0];
755                 }
756         }
757
758         # If no endpoint is set, default to the interface name as a named pipe
759         if (!defined $idl->{PROPERTIES}->{endpoint}) {
760                 push @endpoints, "\"ncacn_np:[\\\\pipe\\\\" . $idl->{NAME} . "]\"";
761         } else {
762                 @endpoints = split /,/, $idl->{PROPERTIES}->{endpoint};
763         }
764
765         return { 
766                 NAME => $idl->{NAME},
767                 UUID => lc(has_property($idl, "uuid")),
768                 VERSION => $version,
769                 TYPE => "INTERFACE",
770                 PROPERTIES => $idl->{PROPERTIES},
771                 FUNCTIONS => \@functions,
772                 CONSTS => \@consts,
773                 TYPES => \@types,
774                 ENDPOINTS => \@endpoints
775         };
776 }
777
778 # Convert a IDL tree to a NDR tree
779 # Gives a result tree describing all that's necessary for easily generating
780 # NDR parsers / generators
781 sub Parse($)
782 {
783         my $idl = shift;
784
785         return undef unless (defined($idl));
786
787         Parse::Pidl::NDR::Validate($idl);
788         
789         my @ndr = ();
790
791         foreach (@{$idl}) {
792                 ($_->{TYPE} eq "CPP_QUOTE") && push(@ndr, $_);
793                 ($_->{TYPE} eq "INTERFACE") && push(@ndr, ParseInterface($_));
794                 ($_->{TYPE} eq "IMPORT") && push(@ndr, $_);
795         }
796
797         return \@ndr;
798 }
799
800 sub GetNextLevel($$)
801 {
802         my $e = shift;
803         my $fl = shift;
804
805         my $seen = 0;
806
807         foreach my $l (@{$e->{LEVELS}}) {
808                 return $l if ($seen);
809                 ($seen = 1) if ($l == $fl);
810         }
811
812         return undef;
813 }
814
815 sub GetPrevLevel($$)
816 {
817         my ($e,$fl) = @_;
818         my $prev = undef;
819
820         foreach my $l (@{$e->{LEVELS}}) {
821                 (return $prev) if ($l == $fl);
822                 $prev = $l;
823         }
824
825         return undef;
826 }
827
828 sub ContainsString($)
829 {
830         my ($e) = @_;
831
832         foreach my $l (@{$e->{LEVELS}}) {
833                 return 1 if ($l->{TYPE} eq "ARRAY" and $l->{IS_ZERO_TERMINATED});
834         }
835
836         return 0;
837 }
838
839 sub ContainsDeferred($$)
840 {
841         my ($e,$l) = @_;
842
843         return 1 if ($l->{CONTAINS_DEFERRED});
844
845         while ($l = GetNextLevel($e,$l))
846         {
847                 return 1 if ($l->{IS_DEFERRED}); 
848                 return 1 if ($l->{CONTAINS_DEFERRED});
849         } 
850         
851         return 0;
852 }
853
854 sub el_name($)
855 {
856         my $e = shift;
857         my $name = "<ANONYMOUS>";
858
859         $name = $e->{NAME} if defined($e->{NAME});
860
861         if (defined($e->{PARENT}) and defined($e->{PARENT}->{NAME})) {
862                 return "$e->{PARENT}->{NAME}.$name";
863         }
864
865         if (defined($e->{PARENT}) and
866             defined($e->{PARENT}->{PARENT}) and
867             defined($e->{PARENT}->{PARENT}->{NAME})) {
868                 return "$e->{PARENT}->{PARENT}->{NAME}.$name";
869         }
870
871         return $name;
872 }
873
874 ###################################
875 # find a sibling var in a structure
876 sub find_sibling($$)
877 {
878         my($e,$name) = @_;
879         my($fn) = $e->{PARENT};
880
881         if ($name =~ /\*(.*)/) {
882                 $name = $1;
883         }
884
885         for my $e2 (@{$fn->{ELEMENTS}}) {
886                 return $e2 if ($e2->{NAME} eq $name);
887         }
888
889         return undef;
890 }
891
892 my %property_list = (
893         # interface
894         "helpstring"            => ["INTERFACE", "FUNCTION"],
895         "version"               => ["INTERFACE"],
896         "uuid"                  => ["INTERFACE"],
897         "endpoint"              => ["INTERFACE"],
898         "pointer_default"       => ["INTERFACE"],
899         "helper"                => ["INTERFACE"],
900         "pyhelper"              => ["INTERFACE"],
901         "authservice"           => ["INTERFACE"],
902         "restricted"            => ["INTERFACE"],
903         "no_srv_register"       => ["INTERFACE"],
904
905         # dcom
906         "object"                => ["INTERFACE"],
907         "local"                 => ["INTERFACE", "FUNCTION"],
908         "iid_is"                => ["ELEMENT"],
909         "call_as"               => ["FUNCTION"],
910         "idempotent"            => ["FUNCTION"],
911
912         # function
913         "noopnum"               => ["FUNCTION"],
914         "in"                    => ["ELEMENT"],
915         "out"                   => ["ELEMENT"],
916
917         # pointer
918         "ref"                   => ["ELEMENT", "TYPEDEF"],
919         "ptr"                   => ["ELEMENT", "TYPEDEF"],
920         "unique"                => ["ELEMENT", "TYPEDEF"],
921         "ignore"                => ["ELEMENT"],
922         "relative"              => ["ELEMENT", "TYPEDEF"],
923         "relative_short"        => ["ELEMENT", "TYPEDEF"],
924         "null_is_ffffffff"      => ["ELEMENT"],
925         "relative_base"         => ["TYPEDEF", "STRUCT", "UNION"],
926
927         "gensize"               => ["TYPEDEF", "STRUCT", "UNION"],
928         "value"                 => ["ELEMENT"],
929         "flag"                  => ["ELEMENT", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
930
931         # generic
932         "public"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
933         "nopush"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
934         "nopull"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
935         "nosize"                => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
936         "noprint"               => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP", "ELEMENT"],
937         "nopython"              => ["FUNCTION", "TYPEDEF", "STRUCT", "UNION", "ENUM", "BITMAP"],
938         "todo"                  => ["FUNCTION"],
939
940         # union
941         "switch_is"             => ["ELEMENT"],
942         "switch_type"           => ["ELEMENT", "UNION"],
943         "nodiscriminant"        => ["UNION"],
944         "ms_union"              => ["INTERFACE", "UNION"],
945         "case"                  => ["ELEMENT"],
946         "default"               => ["ELEMENT"],
947
948         "represent_as"          => ["ELEMENT"],
949         "transmit_as"           => ["ELEMENT"],
950
951         # subcontext
952         "subcontext"            => ["ELEMENT"],
953         "subcontext_size"       => ["ELEMENT"],
954         "compression"           => ["ELEMENT"],
955
956         # enum
957         "enum8bit"              => ["ENUM"],
958         "enum16bit"             => ["ENUM"],
959         "v1_enum"               => ["ENUM"],
960
961         # bitmap
962         "bitmap8bit"            => ["BITMAP"],
963         "bitmap16bit"           => ["BITMAP"],
964         "bitmap32bit"           => ["BITMAP"],
965         "bitmap64bit"           => ["BITMAP"],
966
967         # array
968         "range"                 => ["ELEMENT", "PIPE"],
969         "size_is"               => ["ELEMENT"],
970         "string"                => ["ELEMENT"],
971         "noheader"              => ["ELEMENT"],
972         "charset"               => ["ELEMENT"],
973         "length_is"             => ["ELEMENT"],
974 );
975
976 #####################################################################
977 # check for unknown properties
978 sub ValidProperties($$)
979 {
980         my ($e,$t) = @_;
981
982         return unless defined $e->{PROPERTIES};
983
984         foreach my $key (keys %{$e->{PROPERTIES}}) {
985                 warning($e, el_name($e) . ": unknown property '$key'")
986                         unless defined($property_list{$key});
987
988                 fatal($e, el_name($e) . ": property '$key' not allowed on '$t'")
989                         unless grep(/^$t$/, @{$property_list{$key}});
990         }
991 }
992
993 sub mapToScalar($)
994 {
995         sub mapToScalar($);
996         my $t = shift;
997         return $t->{NAME} if (ref($t) eq "HASH" and $t->{TYPE} eq "SCALAR");
998         my $ti = getType($t);
999
1000         if (not defined ($ti)) {
1001                 return undef;
1002         } elsif ($ti->{TYPE} eq "TYPEDEF") {
1003                 return mapToScalar($ti->{DATA});
1004         } elsif ($ti->{TYPE} eq "ENUM") {
1005                 return Parse::Pidl::Typelist::enum_type_fn($ti);
1006         } elsif ($ti->{TYPE} eq "BITMAP") {
1007                 return Parse::Pidl::Typelist::bitmap_type_fn($ti);
1008         }
1009
1010         return undef;
1011 }
1012
1013 #####################################################################
1014 # validate an element
1015 sub ValidElement($)
1016 {
1017         my $e = shift;
1018
1019         ValidProperties($e,"ELEMENT");
1020
1021         # Check whether switches are used correctly.
1022         if (my $switch = has_property($e, "switch_is")) {
1023                 my $e2 = find_sibling($e, $switch);
1024                 my $type = getType($e->{TYPE});
1025
1026                 if (defined($type) and $type->{DATA}->{TYPE} ne "UNION") {
1027                         fatal($e, el_name($e) . ": switch_is() used on non-union type $e->{TYPE} which is a $type->{DATA}->{TYPE}");
1028                 }
1029
1030                 if (not has_property($type->{DATA}, "nodiscriminant") and defined($e2)) {
1031                         my $discriminator_type = has_property($type->{DATA}, "switch_type");
1032                         $discriminator_type = "uint32" unless defined ($discriminator_type);
1033
1034                         my $t1 = mapScalarType(mapToScalar($discriminator_type));
1035
1036                         if (not defined($t1)) {
1037                                 fatal($e, el_name($e) . ": unable to map discriminator type '$discriminator_type' to scalar");
1038                         }
1039
1040                         my $t2 = mapScalarType(mapToScalar($e2->{TYPE}));
1041                         if (not defined($t2)) {
1042                                 fatal($e, el_name($e) . ": unable to map variable used for switch_is() to scalar");
1043                         }
1044
1045                         if ($t1 ne $t2) {
1046                                 warning($e, el_name($e) . ": switch_is() is of type $e2->{TYPE} ($t2), while discriminator type for union $type->{NAME} is $discriminator_type ($t1)");
1047                         }
1048                 }
1049         }
1050
1051         if (has_property($e, "subcontext") and has_property($e, "represent_as")) {
1052                 fatal($e, el_name($e) . " : subcontext() and represent_as() can not be used on the same element");
1053         }
1054
1055         if (has_property($e, "subcontext") and has_property($e, "transmit_as")) {
1056                 fatal($e, el_name($e) . " : subcontext() and transmit_as() can not be used on the same element");
1057         }
1058
1059         if (has_property($e, "represent_as") and has_property($e, "transmit_as")) {
1060                 fatal($e, el_name($e) . " : represent_as() and transmit_as() can not be used on the same element");
1061         }
1062
1063         if (has_property($e, "represent_as") and has_property($e, "value")) {
1064                 fatal($e, el_name($e) . " : represent_as() and value() can not be used on the same element");
1065         }
1066
1067         if (has_property($e, "subcontext")) {
1068                 warning($e, "subcontext() is deprecated. Use represent_as() or transmit_as() instead");
1069         }
1070
1071         if (defined (has_property($e, "subcontext_size")) and not defined(has_property($e, "subcontext"))) {
1072                 fatal($e, el_name($e) . " : subcontext_size() on non-subcontext element");
1073         }
1074
1075         if (defined (has_property($e, "compression")) and not defined(has_property($e, "subcontext"))) {
1076                 fatal($e, el_name($e) . " : compression() on non-subcontext element");
1077         }
1078
1079         if (!$e->{POINTERS} && (
1080                 has_property($e, "ptr") or
1081                 has_property($e, "unique") or
1082                 has_property($e, "relative") or
1083                 has_property($e, "relative_short") or
1084                 has_property($e, "ref"))) {
1085                 fatal($e, el_name($e) . " : pointer properties on non-pointer element\n");      
1086         }
1087 }
1088
1089 #####################################################################
1090 # validate an enum
1091 sub ValidEnum($)
1092 {
1093         my ($enum) = @_;
1094
1095         ValidProperties($enum, "ENUM");
1096 }
1097
1098 #####################################################################
1099 # validate a bitmap
1100 sub ValidBitmap($)
1101 {
1102         my ($bitmap) = @_;
1103
1104         ValidProperties($bitmap, "BITMAP");
1105 }
1106
1107 #####################################################################
1108 # validate a struct
1109 sub ValidStruct($)
1110 {
1111         my($struct) = shift;
1112
1113         ValidProperties($struct, "STRUCT");
1114
1115         return unless defined($struct->{ELEMENTS});
1116
1117         foreach my $e (@{$struct->{ELEMENTS}}) {
1118                 $e->{PARENT} = $struct;
1119                 ValidElement($e);
1120         }
1121 }
1122
1123 #####################################################################
1124 # parse a union
1125 sub ValidUnion($)
1126 {
1127         my($union) = shift;
1128
1129         ValidProperties($union,"UNION");
1130
1131         if (has_property($union->{PARENT}, "nodiscriminant") and 
1132                 has_property($union->{PARENT}, "switch_type")) {
1133                 fatal($union->{PARENT}, $union->{PARENT}->{NAME} . ": switch_type(" . $union->{PARENT}->{PROPERTIES}->{switch_type} . ") on union without discriminant");
1134         }
1135
1136         return unless defined($union->{ELEMENTS});
1137
1138         foreach my $e (@{$union->{ELEMENTS}}) {
1139                 $e->{PARENT} = $union;
1140
1141                 if (defined($e->{PROPERTIES}->{default}) and 
1142                         defined($e->{PROPERTIES}->{case})) {
1143                         fatal($e, "Union member $e->{NAME} can not have both default and case properties!");
1144                 }
1145                 
1146                 unless (defined ($e->{PROPERTIES}->{default}) or 
1147                                 defined ($e->{PROPERTIES}->{case})) {
1148                         fatal($e, "Union member $e->{NAME} must have default or case property");
1149                 }
1150
1151                 if (has_property($e, "ref")) {
1152                         fatal($e, el_name($e) . ": embedded ref pointers are not supported yet\n");
1153                 }
1154
1155
1156                 ValidElement($e);
1157         }
1158 }
1159
1160 #####################################################################
1161 # validate a pipe
1162 sub ValidPipe($)
1163 {
1164         my ($pipe) = @_;
1165         my $data = $pipe->{DATA};
1166
1167         ValidProperties($pipe, "PIPE");
1168
1169         fatal($pipe, $pipe->{NAME} . ": 'pipe' is not yet supported by pidl");
1170 }
1171
1172 #####################################################################
1173 # parse a typedef
1174 sub ValidTypedef($)
1175 {
1176         my($typedef) = shift;
1177         my $data = $typedef->{DATA};
1178
1179         ValidProperties($typedef, "TYPEDEF");
1180
1181         return unless (ref($data) eq "HASH");
1182
1183         $data->{PARENT} = $typedef;
1184
1185         $data->{FILE} = $typedef->{FILE} unless defined($data->{FILE});
1186         $data->{LINE} = $typedef->{LINE} unless defined($data->{LINE});
1187
1188         ValidType($data);
1189 }
1190
1191 #####################################################################
1192 # validate a function
1193 sub ValidFunction($)
1194 {
1195         my($fn) = shift;
1196
1197         ValidProperties($fn,"FUNCTION");
1198
1199         foreach my $e (@{$fn->{ELEMENTS}}) {
1200                 $e->{PARENT} = $fn;
1201                 if (has_property($e, "ref") && !$e->{POINTERS}) {
1202                         fatal($e, "[ref] variables must be pointers ($fn->{NAME}/$e->{NAME})");
1203                 }
1204                 ValidElement($e);
1205         }
1206 }
1207
1208 #####################################################################
1209 # validate a type
1210 sub ValidType($)
1211 {
1212         my ($t) = @_;
1213
1214         { 
1215                 TYPEDEF => \&ValidTypedef,
1216                 STRUCT => \&ValidStruct,
1217                 UNION => \&ValidUnion,
1218                 ENUM => \&ValidEnum,
1219                 BITMAP => \&ValidBitmap,
1220                 PIPE => \&ValidPipe
1221         }->{$t->{TYPE}}->($t);
1222 }
1223
1224 #####################################################################
1225 # parse the interface definitions
1226 sub ValidInterface($)
1227 {
1228         my($interface) = shift;
1229         my($data) = $interface->{DATA};
1230
1231         if (has_property($interface, "helper")) {
1232                 warning($interface, "helper() is pidl-specific and deprecated. Use `include' instead");
1233         }
1234
1235         ValidProperties($interface,"INTERFACE");
1236
1237         if (has_property($interface, "pointer_default")) {
1238                 if (not grep (/$interface->{PROPERTIES}->{pointer_default}/, 
1239                                         ("ref", "unique", "ptr"))) {
1240                         fatal($interface, "Unknown default pointer type `$interface->{PROPERTIES}->{pointer_default}'");
1241                 }
1242         }
1243
1244         if (has_property($interface, "object")) {
1245                 if (has_property($interface, "version") && 
1246                         $interface->{PROPERTIES}->{version} != 0) {
1247                         fatal($interface, "Object interfaces must have version 0.0 ($interface->{NAME})");
1248                 }
1249
1250                 if (!defined($interface->{BASE}) && 
1251                         not ($interface->{NAME} eq "IUnknown")) {
1252                         fatal($interface, "Object interfaces must all derive from IUnknown ($interface->{NAME})");
1253                 }
1254         }
1255                 
1256         foreach my $d (@{$data}) {
1257                 ($d->{TYPE} eq "FUNCTION") && ValidFunction($d);
1258                 ($d->{TYPE} eq "TYPEDEF" or 
1259                  $d->{TYPE} eq "STRUCT" or
1260                  $d->{TYPE} eq "UNION" or 
1261                  $d->{TYPE} eq "ENUM" or
1262                  $d->{TYPE} eq "BITMAP" or
1263                  $d->{TYPE} eq "PIPE") && ValidType($d);
1264         }
1265
1266 }
1267
1268 #####################################################################
1269 # Validate an IDL structure
1270 sub Validate($)
1271 {
1272         my($idl) = shift;
1273
1274         foreach my $x (@{$idl}) {
1275                 ($x->{TYPE} eq "INTERFACE") && 
1276                     ValidInterface($x);
1277                 ($x->{TYPE} eq "IMPORTLIB") &&
1278                         fatal($x, "importlib() not supported");
1279         }
1280 }
1281
1282 sub is_charset_array($$)
1283 {
1284         my ($e,$l) = @_;
1285
1286         return 0 if ($l->{TYPE} ne "ARRAY");
1287
1288         my $nl = GetNextLevel($e,$l);
1289
1290         return 0 unless ($nl->{TYPE} eq "DATA");
1291
1292         return has_property($e, "charset");
1293 }
1294
1295
1296
1297 1;