slightly more accurate structure alignment code - I need to do proper
[samba.git] / source / build / pidl / parser.pm
1 ###################################################
2 # Samba4 parser generator for IDL structures
3 # Copyright tridge@samba.org 2000-2003
4 # Copyright tpot@samba.org 2001
5 # released under the GNU GPL
6
7 package IdlParser;
8
9 use Data::Dumper;
10
11 my($res);
12
13 # the list of needed functions
14 my %needed;
15
16 #####################################################################
17 # parse a properties list
18 sub ParseProperties($)
19 {
20     my($props) = shift;
21     foreach my $d (@{$props}) {
22         if (ref($d) ne "HASH") {
23             $res .= "[$d] ";
24         } else {
25             foreach my $k (keys %{$d}) {
26                 $res .= "[$k($d->{$k})] ";
27             }
28         }
29     }
30 }
31
32 ####################################################################
33 # work out the name of a size_is() variable
34 sub find_size_var($$)
35 {
36         my($e) = shift;
37         my($size) = shift;
38         my($fn) = $e->{PARENT};
39
40         if (util::is_constant($size)) {
41                 return $size;
42         }
43
44         if ($size =~ /ndr->/) {
45                 return $size;
46         }
47         
48         if ($fn->{TYPE} ne "FUNCTION") {
49                 return "r->$size";
50         }
51
52         for my $e2 (@{$fn->{DATA}}) {
53                 if ($e2->{NAME} eq $size) {
54                         if (util::has_property($e2, "in")) {
55                                 return "r->in.$size";
56                         }
57                         if (util::has_property($e2, "out")) {
58                                 return "r->out.$size";
59                         }
60                 }
61         }
62         die "invalid variable in $size for element $e->{NAME} in $fn->{NAME}\n";
63 }
64
65
66 #####################################################################
67 # work out is a parse function should be declared static or not
68 sub fn_prefix($)
69 {
70         my $e = shift;
71         if (util::has_property($e, "public")) {
72                 return "static ";
73         }
74         return "";
75 }
76
77
78 #####################################################################
79 # work out the correct alignment for a structure
80 sub struct_alignment($)
81 {
82         my $s = shift;
83         my $align = 1;
84         for my $e (@{$s->{ELEMENTS}}) {
85                 if ($align < util::type_align($e)) {
86                         $align = util::type_align($e);
87                 }
88         }
89         return $align;
90 }
91
92 #####################################################################
93 # parse an array - push side
94 sub ParseArrayPush($$$)
95 {
96         my $e = shift;
97         my $var_prefix = shift;
98         my $ndr_flags = shift;
99
100         my $size = find_size_var($e, util::array_size($e));
101
102         if (defined $e->{CONFORMANT_SIZE}) {
103                 # the conformant size has already been pushed
104         } elsif (!util::is_inline_array($e)) {
105                 # we need to emit the array size
106                 $res .= "\t\tNDR_CHECK(ndr_push_uint32(ndr, $size));\n";
107         }
108
109         if (my $length = util::has_property($e, "length_is")) {
110                 $length = find_size_var($e, $length);
111                 $res .= "\t\tNDR_CHECK(ndr_push_uint32(ndr, 0));\n";
112                 $res .= "\t\tNDR_CHECK(ndr_push_uint32(ndr, $length));\n";
113                 $size = $length;
114         }
115
116         if (util::is_scalar_type($e->{TYPE})) {
117                 $res .= "\t\tNDR_CHECK(ndr_push_array_$e->{TYPE}(ndr, $ndr_flags, $var_prefix$e->{NAME}, $size));\n";
118         } else {
119                 $res .= "\t\tNDR_CHECK(ndr_push_array(ndr, $ndr_flags, $var_prefix$e->{NAME}, sizeof($var_prefix$e->{NAME}\[0]), $size, (ndr_push_flags_fn_t)ndr_push_$e->{TYPE}));\n";
120         }
121 }
122
123 #####################################################################
124 # print an array
125 sub ParseArrayPrint($$)
126 {
127         my $e = shift;
128         my $var_prefix = shift;
129         my $size = find_size_var($e, util::array_size($e));
130         my $length = util::has_property($e, "length_is");
131
132         if (defined $length) {
133                 $size = find_size_var($e, $length);
134         }
135
136         if (util::is_scalar_type($e->{TYPE})) {
137                 $res .= "\t\tndr_print_array_$e->{TYPE}(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME}, $size);\n";
138         } else {
139                 $res .= "\t\tndr_print_array(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME}, sizeof($var_prefix$e->{NAME}\[0]), $size, (ndr_print_fn_t)ndr_print_$e->{TYPE});\n";
140         }
141 }
142
143 #####################################################################
144 # parse an array - pull side
145 sub ParseArrayPull($$$)
146 {
147         my $e = shift;
148         my $var_prefix = shift;
149         my $ndr_flags = shift;
150
151         my $size = find_size_var($e, util::array_size($e));
152         my $alloc_size = $size;
153
154         # if this is a conformant array then we use that size to allocate, and make sure
155         # we allocate enough to pull the elements
156         if (defined $e->{CONFORMANT_SIZE}) {
157                 $alloc_size = $e->{CONFORMANT_SIZE};
158
159                 $res .= "\tif ($size > $alloc_size) {\n";
160                 $res .= "\t\treturn ndr_pull_error(ndr, NDR_ERR_CONFORMANT_SIZE, \"Bad conformant size %u should be %u\", $alloc_size, $size);\n";
161                 $res .= "\t}\n";
162         } elsif (!util::is_inline_array($e)) {
163                 # non fixed arrays encode the size just before the array
164                 $res .= "\t{\n";
165                 $res .= "\t\tuint32 _array_size;\n";
166                 $res .= "\t\tNDR_CHECK(ndr_pull_uint32(ndr, &_array_size));\n";
167                 $res .= "\t\tif ($size > _array_size) {\n";
168                 $res .= "\t\t\treturn ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, \"Bad array size %u should be %u\", _array_size, $size);\n";
169                 $res .= "\t\t}\n";
170                 $res .= "\t}\n";
171         }
172
173         if (util::need_alloc($e) && !util::is_fixed_array($e)) {
174                 if (!util::is_inline_array($e) || $ndr_flags eq "NDR_SCALARS") {
175                         $res .= "\t\tNDR_ALLOC_N_SIZE(ndr, $var_prefix$e->{NAME}, $alloc_size, sizeof($var_prefix$e->{NAME}\[0]));\n";
176                 }
177         }
178
179         if (my $length = util::has_property($e, "length_is")) {
180                 $length = find_size_var($e, $length);
181                 $res .= "\t\tuint32 _offset, _length;\n";
182                 $res .= "\t\tNDR_CHECK(ndr_pull_uint32(ndr, &_offset));\n";
183                 $res .= "\t\tNDR_CHECK(ndr_pull_uint32(ndr, &_length));\n";
184                 $res .= "\t\tif (_offset != 0) return ndr_pull_error(ndr, NDR_ERR_OFFSET, \"Bad array offset 0x%08x\", _offset);\n";
185                 $res .= "\t\tif (_length > $size || _length != $length) return ndr_pull_error(ndr, NDR_ERR_LENGTH, \"Bad array length 0x%08x > size 0x%08x\", _offset, $size);\n";
186                 $size = "_length";
187         }
188
189         if (util::is_scalar_type($e->{TYPE})) {
190                 $res .= "\t\tNDR_CHECK(ndr_pull_array_$e->{TYPE}(ndr, $ndr_flags, $var_prefix$e->{NAME}, $size));\n";
191         } else {
192                 $res .= "\t\tNDR_CHECK(ndr_pull_array(ndr, $ndr_flags, (void **)$var_prefix$e->{NAME}, sizeof($var_prefix$e->{NAME}\[0]), $size, (ndr_pull_flags_fn_t)ndr_pull_$e->{TYPE}));\n";
193         }
194 }
195
196
197 #####################################################################
198 # parse scalars in a structure element
199 sub ParseElementPushScalar($$$)
200 {
201         my($e) = shift;
202         my($var_prefix) = shift;
203         my($ndr_flags) = shift;
204         my $cprefix = util::c_push_prefix($e);
205
206         if (util::has_property($e, "relative")) {
207                 $res .= "\tNDR_CHECK(ndr_push_relative(ndr, NDR_SCALARS, $var_prefix$e->{NAME}, (ndr_push_const_fn_t) ndr_push_$e->{TYPE}));\n";
208         } elsif (util::is_inline_array($e)) {
209                 ParseArrayPush($e, "r->", "NDR_SCALARS");
210         } elsif (my $value = util::has_property($e, "value")) {
211                 $res .= "\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $value));\n";
212         } elsif (defined $e->{VALUE}) {
213                 $res .= "\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $e->{VALUE}));\n";
214         } elsif (util::need_wire_pointer($e)) {
215                 $res .= "\tNDR_CHECK(ndr_push_ptr(ndr, $var_prefix$e->{NAME}));\n";
216         } elsif (my $switch = util::has_property($e, "switch_is")) {
217                 ParseElementPushSwitch($e, $var_prefix, $ndr_flags, $switch);
218         } elsif (util::is_builtin_type($e->{TYPE})) {
219                 $res .= "\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $cprefix$var_prefix$e->{NAME}));\n";
220         } else {
221                 $res .= "\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
222         }
223 }
224
225 #####################################################################
226 # print scalars in a structure element
227 sub ParseElementPrintScalar($$)
228 {
229         my($e) = shift;
230         my($var_prefix) = shift;
231         my $cprefix = util::c_push_prefix($e);
232
233         if (defined $e->{VALUE}) {
234                 $res .= "\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $e->{VALUE});\n";
235         } elsif (util::has_direct_buffers($e)) {
236                 $res .= "\tndr_print_ptr(ndr, \"$e->{NAME}\", $var_prefix$e->{NAME});\n";
237                 $res .= "\tndr->depth++;\n";
238                 ParseElementPrintBuffer($e, $var_prefix);
239                 $res .= "\tndr->depth--;\n";
240         } elsif (my $switch = util::has_property($e, "switch_is")) {
241                 ParseElementPrintSwitch($e, $var_prefix, $switch);
242         } else {
243                 $res .= "\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});\n";
244         }
245 }
246
247 #####################################################################
248 # parse scalars in a structure element - pull size
249 sub ParseElementPullSwitch($$$$)
250 {
251         my($e) = shift;
252         my($var_prefix) = shift;
253         my($ndr_flags) = shift;
254         my $switch = shift;
255         my $switch_var = find_size_var($e, $switch);
256
257         my $cprefix = util::c_pull_prefix($e);
258
259         $res .= "\t{ uint16 _level = $switch_var;\n";
260
261         if (util::has_property($e, "subcontext")) {
262                 $res .= "\tNDR_CHECK(ndr_pull_subcontext_union_fn(ndr, &_level, $cprefix$var_prefix$e->{NAME}, (ndr_pull_union_fn_t) ndr_pull_$e->{TYPE}));\n";
263         } else {
264                 $res .= "\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, &_level, $cprefix$var_prefix$e->{NAME}));\n";
265         }
266
267
268         $res .= "\tif ((($ndr_flags) & NDR_SCALARS) && (_level != $switch_var)) return ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value %u in $e->{NAME}\");\n";
269         $res .= "\t}\n";
270 }
271
272 #####################################################################
273 # push switch element
274 sub ParseElementPushSwitch($$$$)
275 {
276         my($e) = shift;
277         my($var_prefix) = shift;
278         my($ndr_flags) = shift;
279         my $switch = shift;
280         my $switch_var = find_size_var($e, $switch);
281         my $cprefix = util::c_push_prefix($e);
282
283         if (util::has_property($e, "subcontext")) {
284                 $res .= "\tNDR_CHECK(ndr_push_subcontext_union_fn(ndr, $switch_var, $cprefix$var_prefix$e->{NAME}, (ndr_push_union_fn_t) ndr_pull_$e->{TYPE}));\n";
285         } else {
286                 $res .= "\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $switch_var, $cprefix$var_prefix$e->{NAME}));\n";
287         }
288 }
289
290 #####################################################################
291 # print scalars in a structure element 
292 sub ParseElementPrintSwitch($$$)
293 {
294         my($e) = shift;
295         my($var_prefix) = shift;
296         my $switch = shift;
297         my $switch_var = find_size_var($e, $switch);
298         my $cprefix = util::c_push_prefix($e);
299
300         $res .= "\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $switch_var, $cprefix$var_prefix$e->{NAME});\n";
301 }
302
303
304 #####################################################################
305 # parse scalars in a structure element - pull size
306 sub ParseElementPullScalar($$$)
307 {
308         my($e) = shift;
309         my($var_prefix) = shift;
310         my($ndr_flags) = shift;
311         my $cprefix = util::c_pull_prefix($e);
312
313         if (util::has_property($e, "relative")) {
314                 $res .= "\tNDR_CHECK(ndr_pull_relative(ndr, (const void **)&$var_prefix$e->{NAME}, sizeof(*$var_prefix$e->{NAME}), (ndr_pull_flags_fn_t)ndr_pull_$e->{TYPE}));\n";
315         } elsif (defined $e->{VALUE}) {
316                 $res .= "\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $e->{VALUE}));\n";
317         } elsif (util::is_inline_array($e)) {
318                 ParseArrayPull($e, "r->", "NDR_SCALARS");
319         } elsif (util::need_wire_pointer($e)) {
320                 $res .= "\tNDR_CHECK(ndr_pull_uint32(ndr, &_ptr_$e->{NAME}));\n";
321                 $res .= "\tif (_ptr_$e->{NAME}) {\n";
322                 $res .= "\t\tNDR_ALLOC(ndr, $var_prefix$e->{NAME});\n";
323                 $res .= "\t} else {\n";
324                 $res .= "\t\t$var_prefix$e->{NAME} = NULL;\n";
325                 $res .= "\t}\n";
326         } elsif (util::need_alloc($e)) {
327                 # no scalar component
328         } elsif (my $switch = util::has_property($e, "switch_is")) {
329                 ParseElementPullSwitch($e, $var_prefix, $ndr_flags, $switch);
330         } elsif (util::has_property($e, "subcontext")) {
331                 if (util::is_builtin_type($e->{TYPE})) {
332                         $res .= "\tNDR_CHECK(ndr_pull_subcontext_fn(ndr, $cprefix$var_prefix$e->{NAME}, (ndr_pull_fn_t) ndr_pull_$e->{TYPE}));\n";
333                 } else {
334                         $res .= "\tNDR_CHECK(ndr_pull_subcontext_flags_fn(ndr, $cprefix$var_prefix$e->{NAME}, (ndr_pull_flags_fn_t) ndr_pull_$e->{TYPE}));\n";
335                 }
336         } elsif (util::is_builtin_type($e->{TYPE})) {
337                 $res .= "\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $cprefix$var_prefix$e->{NAME}));\n";
338         } else {
339                 $res .= "\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
340         }
341 }
342
343 #####################################################################
344 # parse buffers in a structure element
345 sub ParseElementPushBuffer($$$)
346 {
347         my($e) = shift;
348         my($var_prefix) = shift;
349         my($ndr_flags) = shift;
350         my $cprefix = util::c_push_prefix($e);
351
352         if (util::is_pure_scalar($e)) {
353                 return;
354         }
355
356         if (util::need_wire_pointer($e)) {
357                 $res .= "\tif ($var_prefix$e->{NAME}) {\n";
358         }
359             
360         if (util::has_property($e, "relative")) {
361                 $res .= "\tNDR_CHECK(ndr_push_relative(ndr, NDR_BUFFERS, $cprefix$var_prefix$e->{NAME}, (ndr_push_const_fn_t) ndr_push_$e->{TYPE}));\n";
362         } elsif (util::is_inline_array($e)) {
363                 ParseArrayPush($e, "r->", "NDR_BUFFERS");
364         } elsif (util::array_size($e)) {
365                 ParseArrayPush($e, "r->", "NDR_SCALARS|NDR_BUFFERS");
366         } elsif (my $switch = util::has_property($e, "switch_is")) {
367                 if ($e->{POINTERS}) {
368                         ParseElementPushSwitch($e, $var_prefix, "NDR_BUFFERS|NDR_SCALARS", $switch);
369                 } else {
370                         ParseElementPushSwitch($e, $var_prefix, "NDR_BUFFERS", $switch);
371                 }
372         } elsif (util::is_builtin_type($e->{TYPE})) {
373                 $res .= "\t\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $cprefix$var_prefix$e->{NAME}));\n";
374         } elsif ($e->{POINTERS}) {
375                 $res .= "\t\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, NDR_SCALARS|NDR_BUFFERS, $cprefix$var_prefix$e->{NAME}));\n";
376         } else {
377                 $res .= "\t\tNDR_CHECK(ndr_push_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
378         }
379
380         if (util::need_wire_pointer($e)) {
381                 $res .= "\t}\n";
382         }       
383 }
384
385 #####################################################################
386 # print buffers in a structure element
387 sub ParseElementPrintBuffer($$)
388 {
389         my($e) = shift;
390         my($var_prefix) = shift;
391         my $cprefix = util::c_push_prefix($e);
392
393         if (util::need_wire_pointer($e)) {
394                 $res .= "\tif ($var_prefix$e->{NAME}) {\n";
395         }
396             
397         if (util::array_size($e)) {
398                 ParseArrayPrint($e, $var_prefix);
399         } elsif (my $switch = util::has_property($e, "switch_is")) {
400                 ParseElementPrintSwitch($e, $var_prefix, $switch);
401         } else {
402                 $res .= "\t\tndr_print_$e->{TYPE}(ndr, \"$e->{NAME}\", $cprefix$var_prefix$e->{NAME});\n";
403         }
404
405         if (util::need_wire_pointer($e)) {
406                 $res .= "\t}\n";
407         }       
408 }
409
410
411 #####################################################################
412 # parse buffers in a structure element - pull side
413 sub ParseElementPullBuffer($$$)
414 {
415         my($e) = shift;
416         my($var_prefix) = shift;
417         my($ndr_flags) = shift;
418         my $cprefix = util::c_pull_prefix($e);
419
420         if (util::is_pure_scalar($e)) {
421                 return;
422         }
423
424         if (util::has_property($e, "relative")) {
425                 return;
426         }
427
428         if (util::need_wire_pointer($e)) {
429                 $res .= "\tif ($var_prefix$e->{NAME}) {\n";
430         }
431             
432         if (util::is_inline_array($e)) {
433                 ParseArrayPull($e, "r->", "NDR_BUFFERS");
434         } elsif (util::array_size($e)) {
435                 ParseArrayPull($e, "r->", "NDR_SCALARS|NDR_BUFFERS");
436         } elsif (my $switch = util::has_property($e, "switch_is")) {
437                 if ($e->{POINTERS}) {
438                         ParseElementPullSwitch($e, $var_prefix, "NDR_SCALARS|NDR_BUFFERS", $switch);
439                 } else {
440                         ParseElementPullSwitch($e, $var_prefix, "NDR_BUFFERS", $switch);
441                 }
442         } elsif (util::has_property($e, "subcontext")) {
443                 if (util::is_builtin_type($e->{TYPE})) {
444                         $res .= "\tNDR_CHECK(ndr_pull_subcontext_fn(ndr, $cprefix$var_prefix$e->{NAME}, (ndr_pull_fn_t) ndr_pull_$e->{TYPE}));\n";
445                 } else {
446                         $res .= "\tNDR_CHECK(ndr_pull_subcontext_flags_fn(ndr, $cprefix$var_prefix$e->{NAME}, (ndr_pull_flags_fn_t) ndr_pull_$e->{TYPE}));\n";
447                 }
448         } elsif (util::is_builtin_type($e->{TYPE})) {
449                 $res .= "\t\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $cprefix$var_prefix$e->{NAME}));\n";
450         } elsif ($e->{POINTERS}) {
451                 $res .= "\t\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, NDR_SCALARS|NDR_BUFFERS, $cprefix$var_prefix$e->{NAME}));\n";
452         } else {
453                 $res .= "\t\tNDR_CHECK(ndr_pull_$e->{TYPE}(ndr, $ndr_flags, $cprefix$var_prefix$e->{NAME}));\n";
454         }
455
456         if (util::need_wire_pointer($e)) {
457                 $res .= "\t}\n";
458         }       
459 }
460
461 #####################################################################
462 # parse a struct
463 sub ParseStructPush($)
464 {
465         my($struct) = shift;
466         my $conform_e;
467
468         if (! defined $struct->{ELEMENTS}) {
469                 return;
470         }
471
472         # see if the structure contains a conformant array. If it
473         # does, then it must be the last element of the structure, and
474         # we need to push the conformant length early, as it fits on
475         # the wire before the structure (and even before the structure
476         # alignment)
477         my $e = $struct->{ELEMENTS}[-1];
478         if (defined $e->{ARRAY_LEN} && $e->{ARRAY_LEN} eq "*") {
479                 my $size = find_size_var($e, util::array_size($e));
480                 $e->{CONFORMANT_SIZE} = $size;
481                 $conform_e = $e;
482                 $res .= "\tNDR_CHECK(ndr_push_uint32(ndr, $size));\n";
483         }
484
485         $res .= "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
486
487         $res .= "\tNDR_CHECK(ndr_push_struct_start(ndr));\n";
488
489         my $align = struct_alignment($struct);
490         $res .= "\tNDR_CHECK(ndr_push_align(ndr, $align));\n";
491
492         foreach my $e (@{$struct->{ELEMENTS}}) {
493                 $e->{PARENT} = $struct;
494                 ParseElementPushScalar($e, "r->", "NDR_SCALARS");
495         }       
496
497         $res .= "\tndr_push_struct_end(ndr);\n";
498
499         $res .= "buffers:\n";
500         $res .= "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
501         foreach my $e (@{$struct->{ELEMENTS}}) {
502                 ParseElementPushBuffer($e, "r->", "NDR_BUFFERS");
503         }
504
505         $res .= "done:\n";
506 }
507
508 #####################################################################
509 # generate a struct print function
510 sub ParseStructPrint($)
511 {
512         my($struct) = shift;
513
514         if (! defined $struct->{ELEMENTS}) {
515                 return;
516         }
517
518         $res .= "\tndr->depth++;\n";
519         foreach my $e (@{$struct->{ELEMENTS}}) {
520                 ParseElementPrintScalar($e, "r->");
521         }
522         $res .= "\tndr->depth--;\n";
523 }
524
525 #####################################################################
526 # parse a struct - pull side
527 sub ParseStructPull($)
528 {
529         my($struct) = shift;
530         my $conform_e;
531
532         if (! defined $struct->{ELEMENTS}) {
533                 return;
534         }
535
536         # see if the structure contains a conformant array. If it
537         # does, then it must be the last element of the structure, and
538         # we need to pull the conformant length early, as it fits on
539         # the wire before the structure (and even before the structure
540         # alignment)
541         my $e = $struct->{ELEMENTS}[-1];
542         if (defined $e->{ARRAY_LEN} && $e->{ARRAY_LEN} eq "*") {
543                 $conform_e = $e;
544                 $res .= "\tuint32 _conformant_size;\n";
545                 $conform_e->{CONFORMANT_SIZE} = "_conformant_size";
546         }
547
548         # declare any internal pointers we need
549         foreach my $e (@{$struct->{ELEMENTS}}) {
550                 $e->{PARENT} = $struct;
551                 if (util::need_wire_pointer($e)) {
552                         $res .= "\tuint32 _ptr_$e->{NAME};\n";
553                 }
554         }
555
556         $res .= "\tNDR_CHECK(ndr_pull_struct_start(ndr));\n";
557
558         if (defined $conform_e) {
559                 $res .= "\tNDR_CHECK(ndr_pull_uint32(ndr, &$conform_e->{CONFORMANT_SIZE}));\n";
560         }
561
562         $res .= "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
563
564         my $align = struct_alignment($struct);
565         $res .= "\tNDR_CHECK(ndr_pull_align(ndr, $align));\n";
566
567         foreach my $e (@{$struct->{ELEMENTS}}) {
568                 ParseElementPullScalar($e, "r->", "NDR_SCALARS");
569         }       
570
571         $res .= "\tndr_pull_struct_end(ndr);\n";
572
573         $res .= "buffers:\n";
574         $res .= "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
575         foreach my $e (@{$struct->{ELEMENTS}}) {
576                 ParseElementPullBuffer($e, "r->", "NDR_BUFFERS");
577         }
578
579         $res .= "done:\n";
580 }
581
582
583 #####################################################################
584 # parse a union - push side
585 sub ParseUnionPush($)
586 {
587         my $e = shift;
588         my $have_default = 0;
589         $res .= "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
590
591         $res .= "\tNDR_CHECK(ndr_push_struct_start(ndr));\n";
592
593         if (!util::has_property($e, "nodiscriminant")) {
594                 $res .= "\tNDR_CHECK(ndr_push_uint16(ndr, level));\n";
595         }
596         $res .= "\tswitch (level) {\n";
597         foreach my $el (@{$e->{DATA}}) {
598                 if ($el->{CASE} eq "default") {
599                         $res .= "\tdefault:\n";
600                         $have_default = 1;
601                 } else {
602                         $res .= "\tcase $el->{CASE}:\n";
603                 }
604                 ParseElementPushScalar($el->{DATA}, "r->", "NDR_SCALARS");              
605                 $res .= "\tbreak;\n\n";
606         }
607         if (! $have_default) {
608                 $res .= "\tdefault:\n";
609                 $res .= "\t\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
610         }
611         $res .= "\t}\n";
612         $res .= "\tndr_push_struct_end(ndr);\n";
613         $res .= "buffers:\n";
614         $res .= "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
615         $res .= "\tswitch (level) {\n";
616         foreach my $el (@{$e->{DATA}}) {
617                 if ($el->{CASE} eq "default") {
618                         $res .= "\tdefault:\n";
619                 } else {
620                         $res .= "\tcase $el->{CASE}:\n";
621                 }
622                 ParseElementPushBuffer($el->{DATA}, "r->", "ndr_flags");
623                 $res .= "\tbreak;\n\n";
624         }
625         if (! $have_default) {
626                 $res .= "\tdefault:\n";
627                 $res .= "\t\treturn ndr_push_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", level);\n";
628         }
629         $res .= "\t}\n";
630         $res .= "done:\n";
631 }
632
633 #####################################################################
634 # print a union
635 sub ParseUnionPrint($)
636 {
637         my $e = shift;
638         my $have_default = 0;
639
640         $res .= "\tswitch (level) {\n";
641         foreach my $el (@{$e->{DATA}}) {
642                 if ($el->{CASE} eq "default") {
643                         $have_default = 1;
644                         $res .= "\tdefault:\n";
645                 } else {
646                         $res .= "\tcase $el->{CASE}:\n";
647                 }
648                 ParseElementPrintScalar($el->{DATA}, "r->");
649                 $res .= "\tbreak;\n\n";
650         }
651         if (! $have_default) {
652                 $res .= "\tdefault:\n\t\tndr_print_bad_level(ndr, name, level);\n";
653         }
654         $res .= "\t}\n";
655 }
656
657 #####################################################################
658 # parse a union - pull side
659 sub ParseUnionPull($)
660 {
661         my $e = shift;
662         my $have_default = 0;
663
664         $res .= "\tif (!(ndr_flags & NDR_SCALARS)) goto buffers;\n";
665
666         $res .= "\tNDR_CHECK(ndr_pull_struct_start(ndr));\n";
667
668         if (!util::has_property($e, "nodiscriminant")) {
669                 $res .= "\tNDR_CHECK(ndr_pull_uint16(ndr, level));\n";
670         }
671         $res .= "\tswitch (*level) {\n";
672         foreach my $el (@{$e->{DATA}}) {
673                 if ($el->{CASE} eq "default") {
674                         $res .= "\tdefault: {\n";
675                         $have_default = 1;
676                 } else {
677                         $res .= "\tcase $el->{CASE}: {\n";
678                 }
679                 my $e2 = $el->{DATA};
680                 if ($e2->{POINTERS}) {
681                         $res .= "\t\tuint32 _ptr_$e2->{NAME};\n";
682                 }
683                 ParseElementPullScalar($el->{DATA}, "r->", "NDR_SCALARS");              
684                 $res .= "\tbreak; }\n\n";
685         }
686         if (! $have_default) {
687                 $res .= "\tdefault:\n";
688                 $res .= "\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", *level);\n";
689         }
690         $res .= "\t}\n";
691         $res .= "\tndr_pull_struct_end(ndr);\n";
692         $res .= "buffers:\n";
693         $res .= "\tif (!(ndr_flags & NDR_BUFFERS)) goto done;\n";
694         $res .= "\tswitch (*level) {\n";
695         foreach my $el (@{$e->{DATA}}) {
696                 if ($el->{CASE} eq "default") {
697                         $res .= "\tdefault:\n";
698                 } else {
699                         $res .= "\tcase $el->{CASE}:\n";
700                 }
701                 ParseElementPullBuffer($el->{DATA}, "r->", "NDR_BUFFERS");
702                 $res .= "\tbreak;\n\n";
703         }
704         if (! $have_default) {
705                 $res .= "\tdefault:\n";
706                 $res .= "\t\treturn ndr_pull_error(ndr, NDR_ERR_BAD_SWITCH, \"Bad switch value \%u\", *level);\n";
707         }
708         $res .= "\t}\n";
709         $res .= "done:\n";
710 }
711
712 #####################################################################
713 # parse a type
714 sub ParseTypePush($)
715 {
716         my($data) = shift;
717
718         if (ref($data) eq "HASH") {
719                 ($data->{TYPE} eq "STRUCT") &&
720                     ParseStructPush($data);
721                 ($data->{TYPE} eq "UNION") &&
722                     ParseUnionPush($data);
723         }
724 }
725
726 #####################################################################
727 # generate a print function for a type
728 sub ParseTypePrint($)
729 {
730         my($data) = shift;
731
732         if (ref($data) eq "HASH") {
733                 ($data->{TYPE} eq "STRUCT") &&
734                     ParseStructPrint($data);
735                 ($data->{TYPE} eq "UNION") &&
736                     ParseUnionPrint($data);
737         }
738 }
739
740 #####################################################################
741 # parse a type
742 sub ParseTypePull($)
743 {
744         my($data) = shift;
745
746         if (ref($data) eq "HASH") {
747                 ($data->{TYPE} eq "STRUCT") &&
748                     ParseStructPull($data);
749                 ($data->{TYPE} eq "UNION") &&
750                     ParseUnionPull($data);
751         }
752 }
753
754 #####################################################################
755 # parse a typedef - push side
756 sub ParseTypedefPush($)
757 {
758         my($e) = shift;
759         my $static = fn_prefix($e);
760
761         if (! $needed{"push_$e->{NAME}"}) {
762 #               print "push_$e->{NAME} not needed\n";
763                 return;
764         }
765
766         if ($e->{DATA}->{TYPE} eq "STRUCT") {
767                 $res .= "$static" . "NTSTATUS ndr_push_$e->{NAME}(struct ndr_push *ndr, int ndr_flags, struct $e->{NAME} *r)";
768                 $res .= "\n{\n";
769                 ParseTypePush($e->{DATA});
770                 $res .= "\treturn NT_STATUS_OK;\n";
771                 $res .= "}\n\n";
772         }
773
774         if ($e->{DATA}->{TYPE} eq "UNION") {
775                 $res .= "$static" . "NTSTATUS ndr_push_$e->{NAME}(struct ndr_push *ndr, int ndr_flags, uint16 level, union $e->{NAME} *r)";
776                 $res .= "\n{\n";
777                 ParseTypePush($e->{DATA});
778                 $res .= "\treturn NT_STATUS_OK;\n";
779                 $res .= "}\n\n";
780         }
781 }
782
783
784 #####################################################################
785 # parse a typedef - pull side
786 sub ParseTypedefPull($)
787 {
788         my($e) = shift;
789         my $static = fn_prefix($e);
790
791         if (! $needed{"pull_$e->{NAME}"}) {
792 #               print "pull_$e->{NAME} not needed\n";
793                 return;
794         }
795
796         if ($e->{DATA}->{TYPE} eq "STRUCT") {
797                 $res .= "$static" . "NTSTATUS ndr_pull_$e->{NAME}(struct ndr_pull *ndr, int ndr_flags, struct $e->{NAME} *r)";
798                 $res .= "\n{\n";
799                 ParseTypePull($e->{DATA});
800                 $res .= "\treturn NT_STATUS_OK;\n";
801                 $res .= "}\n\n";
802         }
803
804         if ($e->{DATA}->{TYPE} eq "UNION") {
805                 $res .= "$static" . "NTSTATUS ndr_pull_$e->{NAME}(struct ndr_pull *ndr, int ndr_flags, uint16 *level, union $e->{NAME} *r)";
806                 $res .= "\n{\n";
807                 ParseTypePull($e->{DATA});
808                 $res .= "\treturn NT_STATUS_OK;\n";
809                 $res .= "}\n\n";
810         }
811 }
812
813
814 #####################################################################
815 # parse a typedef - print side
816 sub ParseTypedefPrint($)
817 {
818         my($e) = shift;
819
820         if ($e->{DATA}->{TYPE} eq "STRUCT") {
821                 $res .= "void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, struct $e->{NAME} *r)";
822                 $res .= "\n{\n";
823                 $res .= "\tndr_print_struct(ndr, name, \"$e->{NAME}\");\n";
824                 ParseTypePrint($e->{DATA});
825                 $res .= "}\n\n";
826         }
827
828         if ($e->{DATA}->{TYPE} eq "UNION") {
829                 $res .= "void ndr_print_$e->{NAME}(struct ndr_print *ndr, const char *name, uint16 level, union $e->{NAME} *r)";
830                 $res .= "\n{\n";
831                 $res .= "\tndr_print_union(ndr, name, level, \"$e->{NAME}\");\n";
832                 ParseTypePrint($e->{DATA});
833                 $res .= "}\n\n";
834         }
835 }
836
837 #####################################################################
838 # parse a function - print side
839 sub ParseFunctionPrint($)
840 {
841         my($fn) = shift;
842
843         $res .= "void ndr_print_$fn->{NAME}(struct ndr_print *ndr, const char *name, int flags, struct $fn->{NAME} *r)";
844         $res .= "\n{\n";
845         $res .= "\tndr_print_struct(ndr, name, \"$fn->{NAME}\");\n";
846         $res .= "\tndr->depth++;\n";
847         
848         $res .= "\tif (flags & NDR_IN) {\n";
849         $res .= "\t\tndr_print_struct(ndr, \"in\", \"$fn->{NAME}\");\n";
850         $res .= "\tndr->depth++;\n";
851         foreach my $e (@{$fn->{DATA}}) {
852                 if (util::has_property($e, "in")) {
853                         ParseElementPrintScalar($e, "r->in.");
854                 }
855         }
856         $res .= "\tndr->depth--;\n";
857         $res .= "\t}\n";
858         
859         $res .= "\tif (flags & NDR_OUT) {\n";
860         $res .= "\t\tndr_print_struct(ndr, \"out\", \"$fn->{NAME}\");\n";
861         $res .= "\tndr->depth++;\n";
862         foreach my $e (@{$fn->{DATA}}) {
863                 if (util::has_property($e, "out")) {
864                         ParseElementPrintScalar($e, "r->out.");
865                 }
866         }
867         if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
868                 $res .= "\tndr_print_$fn->{RETURN_TYPE}(ndr, \"result\", &r->out.result);\n";
869         }
870         $res .= "\tndr->depth--;\n";
871         $res .= "\t}\n";
872         
873         $res .= "\tndr->depth--;\n";
874         $res .= "}\n\n";
875 }
876
877
878 #####################################################################
879 # parse a function
880 sub ParseFunctionPush($)
881
882         my($function) = shift;
883
884         # Input function
885         $res .= "NTSTATUS ndr_push_$function->{NAME}(struct ndr_push *ndr, struct $function->{NAME} *r)\n{\n";
886
887         foreach my $e (@{$function->{DATA}}) {
888                 if (util::has_property($e, "in")) {
889                         $e->{PARENT} = $function;
890                         if (util::array_size($e)) {
891                                 if (util::need_wire_pointer($e)) {
892                                         $res .= "\tNDR_CHECK(ndr_push_ptr(ndr, r->in.$e->{NAME}));\n";
893                                 }
894                                 $res .= "\tif (r->in.$e->{NAME}) {\n";
895                                 ParseArrayPush($e, "r->in.", "NDR_SCALARS|NDR_BUFFERS");
896                                 $res .= "\t}\n";
897                         } else {
898                                 ParseElementPushScalar($e, "r->in.", "NDR_SCALARS|NDR_BUFFERS");
899                                 if ($e->{POINTERS}) {
900                                         ParseElementPushBuffer($e, "r->in.", "NDR_SCALARS|NDR_BUFFERS");
901                                 }
902                         }
903                 }
904         }
905     
906         $res .= "\n\treturn NT_STATUS_OK;\n}\n\n";
907 }
908
909 #####################################################################
910 # parse a function
911 sub ParseFunctionPull($)
912
913         my($fn) = shift;
914
915         # pull function args
916         $res .= "NTSTATUS ndr_pull_$fn->{NAME}(struct ndr_pull *ndr, struct $fn->{NAME} *r)\n{\n";
917
918         # declare any internal pointers we need
919         foreach my $e (@{$fn->{DATA}}) {
920                 if (util::has_property($e, "out")) {
921                         if (util::need_wire_pointer($e)) {
922                                 $res .= "\tuint32 _ptr_$e->{NAME};\n";
923                         }
924                 }
925         }
926
927         foreach my $e (@{$fn->{DATA}}) {
928                 if (util::has_property($e, "out")) {
929                         $e->{PARENT} = $fn;
930                         if (util::array_size($e)) {
931                                 if (util::need_wire_pointer($e)) {
932                                         $res .= "\tNDR_CHECK(ndr_pull_uint32(ndr, _ptr_$e->{NAME}));\n";
933                                         $res .= "\tif (_ptr_$e->{NAME}) {\n";
934                                 } else {
935                                         $res .= "\tif (r->out.$e->{NAME}) {\n";
936                                 }
937                                 ParseArrayPull($e, "r->out.", "NDR_SCALARS|NDR_BUFFERS");
938                                 $res .= "\t}\n";
939                         } else {
940                                 ParseElementPullScalar($e, "r->out.", "NDR_SCALARS|NDR_BUFFERS");
941                                 if ($e->{POINTERS}) {
942                                         ParseElementPullBuffer($e, "r->out.", "NDR_SCALARS|NDR_BUFFERS");
943                                 }
944                         }
945                 }
946         }
947
948         if ($fn->{RETURN_TYPE} && $fn->{RETURN_TYPE} ne "void") {
949                 $res .= "\tNDR_CHECK(ndr_pull_$fn->{RETURN_TYPE}(ndr, &r->out.result));\n";
950         }
951
952     
953         $res .= "\n\treturn NT_STATUS_OK;\n}\n\n";
954 }
955
956 #####################################################################
957 # parse the interface definitions
958 sub ParseInterface($)
959 {
960         my($interface) = shift;
961         my($data) = $interface->{DATA};
962         foreach my $d (@{$data}) {
963                 ($d->{TYPE} eq "TYPEDEF") &&
964                     ParseTypedefPush($d);
965                 ($d->{TYPE} eq "FUNCTION") && 
966                     ParseFunctionPush($d);
967         }
968         foreach my $d (@{$data}) {
969                 ($d->{TYPE} eq "TYPEDEF") &&
970                     ParseTypedefPull($d);
971                 ($d->{TYPE} eq "FUNCTION") && 
972                     ParseFunctionPull($d);
973         }
974         foreach my $d (@{$data}) {
975                 if ($d->{TYPE} eq "TYPEDEF" &&
976                     !util::has_property($d->{DATA}, "noprint")) {
977                         ParseTypedefPrint($d);
978                 }
979                 if ($d->{TYPE} eq "FUNCTION" &&
980                     !util::has_property($d, "noprint")) {
981                         ParseFunctionPrint($d);
982                 }
983         }
984 }
985
986 sub NeededFunction($)
987 {
988         my $fn = shift;
989         $needed{"pull_$fn->{NAME}"} = 1;
990         $needed{"push_$fn->{NAME}"} = 1;
991         foreach my $e (@{$fn->{DATA}}) {
992                 if (util::has_property($e, "out")) {
993                         $needed{"pull_$e->{TYPE}"} = 1;
994                 }
995                 if (util::has_property($e, "in")) {
996                         $needed{"push_$e->{TYPE}"} = 1;
997                 }
998         }
999 }
1000
1001 sub NeededTypedef($)
1002 {
1003         my $t = shift;
1004         if (util::has_property($t->{DATA}, "public")) {
1005                 $needed{"pull_$t->{NAME}"} = 1;
1006                 $needed{"push_$t->{NAME}"} = 1;         
1007         }
1008         if ($t->{DATA}->{TYPE} eq "STRUCT") {
1009                 for my $e (@{$t->{DATA}->{ELEMENTS}}) {
1010                                 if ($needed{"pull_$t->{NAME}"}) {
1011                                         $needed{"pull_$e->{TYPE}"} = 1;
1012                                 }
1013                                 if ($needed{"push_$t->{NAME}"}) {
1014                                         $needed{"push_$e->{TYPE}"} = 1;
1015                                 }
1016                         }
1017                 }
1018         if ($t->{DATA}->{TYPE} eq "UNION") {
1019                 for my $e (@{$t->{DATA}->{DATA}}) {
1020                         if ($needed{"pull_$t->{NAME}"}) {
1021                                 $needed{"pull_$e->{DATA}->{TYPE}"} = 1;
1022                         }
1023                         if ($needed{"push_$t->{NAME}"}) {
1024                                 $needed{"push_$e->{DATA}->{TYPE}"} = 1;
1025                         }
1026                 }
1027         }
1028 }
1029
1030 #####################################################################
1031 # work out what parse functions are needed
1032 sub BuildNeeded($)
1033 {
1034         my($interface) = shift;
1035         my($data) = $interface->{DATA};
1036         foreach my $d (@{$data}) {
1037                 ($d->{TYPE} eq "FUNCTION") && 
1038                     NeededFunction($d);
1039         }
1040         foreach my $d (reverse @{$data}) {
1041                 ($d->{TYPE} eq "TYPEDEF") &&
1042                     NeededTypedef($d);
1043         }
1044 }
1045
1046
1047 #####################################################################
1048 # parse a parsed IDL structure back into an IDL file
1049 sub Parse($)
1050 {
1051         my($idl) = shift;
1052         $res = "/* parser auto-generated by pidl */\n\n";
1053         $res .= "#include \"includes.h\"\n\n";
1054         foreach my $x (@{$idl}) {
1055                 if ($x->{TYPE} eq "INTERFACE") { 
1056                         BuildNeeded($x);
1057                         ParseInterface($x);
1058                 }
1059         }
1060         return $res;
1061 }
1062
1063 1;