aaabe3328e991357d3be8b73b876186f56b4fe3b
[amitay/samba.git] / pidl / lib / Parse / Pidl / Samba4 / TDR.pm
1 ###################################################
2 # Trivial Parser Generator
3 # Copyright jelmer@samba.org 2005-2007
4 # released under the GNU GPL
5
6 package Parse::Pidl::Samba4::TDR;
7 use Parse::Pidl qw(fatal);
8 use Parse::Pidl::Util qw(has_property ParseExpr is_constant);
9 use Parse::Pidl::Samba4 qw(is_intree choose_header);
10 use Parse::Pidl::Typelist qw(mapTypeName);
11
12 use base Parse::Pidl::Base;
13
14 use Exporter;
15 push @ISA, qw(Exporter);
16 @EXPORT_OK = qw(ParserType $res $res_hdr);
17
18 use vars qw($VERSION);
19 $VERSION = '0.01';
20
21 use strict;
22
23 sub new($) {
24         my ($class) = shift;
25         my $self = { res => "", res_hdr => "", tabs => "" };
26         bless($self, $class);
27 }
28
29 sub typearg($) { 
30         my $t = shift; 
31         return(", const char *name") if ($t eq "print");
32         return(", TALLOC_CTX *mem_ctx") if ($t eq "pull");
33         return("");
34 }
35
36 sub fn_declare($$$)
37 {
38         my ($self, $p, $d) = @_;
39         if ($p) { 
40                 $self->pidl($d); $self->pidl_hdr("$d;"); 
41         } else { 
42                 $self->pidl("static $d"); 
43         }
44 }
45
46 sub ContainsArray($)
47 {
48         my $e = shift;
49         foreach (@{$e->{ELEMENTS}}) {
50                 next if (has_property($_, "charset") and
51                         scalar(@{$_->{ARRAY_LEN}}) == 1);
52                 return 1 if (defined($_->{ARRAY_LEN}) and 
53                                 scalar(@{$_->{ARRAY_LEN}}) > 0);
54         }
55         return 0;
56 }
57
58 sub ParserElement($$$$)
59 {
60         my ($self, $e,$t,$env) = @_;
61         my $switch = "";
62         my $array = "";
63         my $name = "";
64         my $mem_ctx = "mem_ctx";
65
66         fatal($e,"Pointers not supported in TDR") if ($e->{POINTERS} > 0);
67         fatal($e,"size_is() not supported in TDR") if (has_property($e, "size_is"));
68         fatal($e,"length_is() not supported in TDR") if (has_property($e, "length_is"));
69
70         if ($t eq "print") {
71                 $name = ", \"$e->{NAME}\"$array";
72         }
73
74         if (has_property($e, "flag")) {
75                 $self->pidl("{");
76                 $self->indent;
77                 $self->pidl("uint32_t saved_flags = tdr->flags;");
78                 $self->pidl("tdr->flags |= $e->{PROPERTIES}->{flag};");
79         }
80
81         if (has_property($e, "charset")) {
82                 fatal($e,"charset() on non-array element") unless (defined($e->{ARRAY_LEN}) and scalar(@{$e->{ARRAY_LEN}}) > 0);
83                 
84                 my $len = ParseExpr(@{$e->{ARRAY_LEN}}[0], $env, $e);
85                 if ($len eq "*") { $len = "-1"; }
86                 $name = ", mem_ctx" if ($t eq "pull");
87                 $self->pidl("TDR_CHECK(tdr_$t\_charset(tdr$name, &v->$e->{NAME}, $len, sizeof($e->{TYPE}_t), CH_$e->{PROPERTIES}->{charset}));");
88                 return;
89         }
90
91         if (has_property($e, "switch_is")) {
92                 $switch = ", " . ParseExpr($e->{PROPERTIES}->{switch_is}, $env, $e);
93         }
94
95         if (defined($e->{ARRAY_LEN}) and scalar(@{$e->{ARRAY_LEN}}) > 0) {
96                 my $len = ParseExpr($e->{ARRAY_LEN}[0], $env, $e);
97
98                 if ($t eq "pull" and not is_constant($len)) {
99                         $self->pidl("TDR_ALLOC(mem_ctx, v->$e->{NAME}, $len);");
100                         $mem_ctx = "v->$e->{NAME}";
101                 }
102
103                 $self->pidl("for (i = 0; i < $len; i++) {");
104                 $self->indent;
105                 $array = "[i]";
106         }
107
108         if ($t eq "pull") {
109                 $name = ", $mem_ctx";
110         }
111
112         if (has_property($e, "value") && $t eq "push") {
113                 $self->pidl("v->$e->{NAME} = ".ParseExpr($e->{PROPERTIES}->{value}, $env, $e).";");
114         }
115
116         $self->pidl("TDR_CHECK(tdr_$t\_$e->{TYPE}(tdr$name$switch, &v->$e->{NAME}$array));");
117
118         if ($array) { $self->deindent; $self->pidl("}"); }
119
120         if (has_property($e, "flag")) {
121                 $self->pidl("tdr->flags = saved_flags;");
122                 $self->deindent;
123                 $self->pidl("}");
124         }
125 }
126
127 sub ParserStruct($$$$$)
128 {
129         my ($self, $e,$t,$p) = @_;
130
131         $self->fn_declare($p,"NTSTATUS tdr_$t\_$e->{NAME} (struct tdr_$t *tdr".typearg($t).", struct $e->{NAME} *v)");
132         $self->pidl("{"); $self->indent;
133         $self->pidl("int i;") if (ContainsArray($e));
134
135         if ($t eq "print") {
136                 $self->pidl("tdr->print(tdr, \"\%-25s: struct $e->{NAME}\", name);");
137                 $self->pidl("tdr->level++;");
138         }
139
140         my %env = map { $_->{NAME} => "v->$_->{NAME}" } @{$e->{ELEMENTS}};
141         $env{"this"} = "v";
142         $self->ParserElement($_, $t, \%env) foreach (@{$e->{ELEMENTS}});
143         
144         if ($t eq "print") {
145                 $self->pidl("tdr->level--;");
146         }
147
148         $self->pidl("return NT_STATUS_OK;");
149
150         $self->deindent; $self->pidl("}");
151 }
152
153 sub ParserUnion($$$$)
154 {
155         my ($self, $e,$t,$p) = @_;
156
157         $self->fn_declare($p,"NTSTATUS tdr_$t\_$e->{NAME}(struct tdr_$t *tdr".typearg($t).", int level, union $e->{NAME} *v)");
158         $self->pidl("{"); $self->indent;
159         $self->pidl("int i;") if (ContainsArray($e));
160
161         if ($t eq "print") {
162                 $self->pidl("tdr->print(tdr, \"\%-25s: union $e->{NAME}\", name);");
163                 $self->pidl("tdr->level++;");
164         }
165         
166         $self->pidl("switch (level) {"); $self->indent;
167         foreach (@{$e->{ELEMENTS}}) {
168                 if (has_property($_, "case")) {
169                         $self->pidl("case " . $_->{PROPERTIES}->{case} . ":");
170                 } elsif (has_property($_, "default")) {
171                         $self->pidl("default:");
172                 }
173                 $self->indent; $self->ParserElement($_, $t, {}); $self->deindent;
174                 $self->pidl("break;");
175         }
176         $self->deindent; $self->pidl("}");
177
178         if ($t eq "print") {
179                 $self->pidl("tdr->level--;");
180         }
181         
182         $self->pidl("return NT_STATUS_OK;\n");
183         $self->deindent; $self->pidl("}");
184 }
185
186 sub ParserBitmap($$$$)
187 {
188         my ($self,$e,$t,$p) = @_;
189         return if ($p);
190         $self->pidl("#define tdr_$t\_$e->{NAME} tdr_$t\_" . Parse::Pidl::Typelist::bitmap_type_fn($e));
191 }
192
193 sub ParserEnum($$$$)
194 {
195         my ($self,$e,$t,$p) = @_;
196         my $bt = Parse::Pidl::Typelist::enum_type_fn($e);
197         my $mt = mapTypeName($bt);
198
199         $self->fn_declare($p, "NTSTATUS tdr_$t\_$e->{NAME} (struct tdr_$t *tdr".typearg($t).", enum $e->{NAME} *v)");
200         $self->pidl("{");
201         if ($t eq "pull") {
202                 $self->pidl("\t$mt r;");
203                 $self->pidl("\tTDR_CHECK(tdr_$t\_$bt(tdr, mem_ctx, \&r));");
204                 $self->pidl("\t*v = r;");
205         } elsif ($t eq "push") {
206                 $self->pidl("\tTDR_CHECK(tdr_$t\_$bt(tdr, ($mt *)v));");
207         } elsif ($t eq "print") {
208                 $self->pidl("\t/* FIXME */");
209         }
210         $self->pidl("\treturn NT_STATUS_OK;");
211         $self->pidl("}");
212 }
213
214 sub ParserTypedef($$$$)
215 {
216         my ($self, $e,$t,$p) = @_;
217
218         $self->ParserType($e->{DATA},$t);
219 }
220
221 sub ParserType($$$)
222 {
223         my ($self, $e,$t) = @_;
224
225         return if (has_property($e, "no$t"));
226
227         my $handlers = { 
228                 STRUCT => \&ParserStruct, UNION => \&ParserUnion, 
229                 ENUM => \&ParserEnum, BITMAP => \&ParserBitmap,
230                 TYPEDEF => \&ParserTypedef
231         };
232         
233         $handlers->{$e->{TYPE}}->($self, $e, $t, has_property($e, "public")) 
234                 if (defined($handlers->{$e->{TYPE}}));
235
236         $self->pidl("");
237 }
238
239 sub ParserInterface($$)
240 {
241         my ($self,$x) = @_;
242         
243         $self->pidl_hdr("#ifndef __TDR_$x->{NAME}_HEADER__");
244         $self->pidl_hdr("#define __TDR_$x->{NAME}_HEADER__");
245
246         foreach (@{$x->{DATA}}) {
247                 $self->ParserType($_, "pull");
248                 $self->ParserType($_, "push");
249                 $self->ParserType($_, "print");
250         }
251
252         $self->pidl_hdr("#endif /* __TDR_$x->{NAME}_HEADER__ */");
253 }
254
255 sub Parser($$$$)
256 {
257         my ($self,$idl,$hdrname,$baseheader) = @_;
258         $self->pidl("/* autogenerated by pidl */");
259         if (is_intree()) {
260                 $self->pidl("#include \"includes.h\"");
261         } else {
262                 $self->pidl("#include <stdio.h>");
263                 $self->pidl("#include <stdbool.h>");
264                 $self->pidl("#include <stdlib.h>");
265                 $self->pidl("#include <stdint.h>");
266                 $self->pidl("#include <stdarg.h>");
267                 $self->pidl("#include <string.h>");
268                 $self->pidl("#include <core/ntstatus.h>");
269         }
270         $self->pidl("#include \"$hdrname\"");
271         $self->pidl("");
272         $self->pidl_hdr("/* autogenerated by pidl */");
273         $self->pidl_hdr("#include \"$baseheader\"");
274         $self->pidl_hdr(choose_header("lib/tdr/tdr.h", "tdr.h"));
275         $self->pidl_hdr("");
276
277         foreach (@$idl) { $self->ParserInterface($_) if ($_->{TYPE} eq "INTERFACE"); }  
278         return ($self->{res_hdr}, $self->{res});
279 }
280
281 1;