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