1 ###################################################
2 # create C header files for an IDL structure
3 # Copyright tridge@samba.org 2000
4 # Copyright jelmer@samba.org 2005
5 # released under the GNU GPL
7 package Parse::Pidl::Samba4::Header;
12 @EXPORT_OK = qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv);
15 use Parse::Pidl qw(fatal);
16 use Parse::Pidl::Typelist qw(mapTypeName scalar_is_reference);
17 use Parse::Pidl::Util qw(has_property is_constant unmake_str ParseExpr);
18 use Parse::Pidl::Samba4 qw(is_intree ElementStars ArrayBrackets choose_header);
20 use vars qw($VERSION);
26 sub pidl($) { $res .= shift; }
31 $res .="\t" foreach (1..$tab_depth);
35 #####################################################################
36 # parse a properties list
37 sub HeaderProperties($$)
39 my($props,$ignores) = @_;
42 foreach my $d (keys %{$props}) {
43 next if (grep(/^$d$/, @$ignores));
44 if($props->{$d} ne "1") {
45 $ret.= "$d($props->{$d}),";
52 pidl "/* [" . substr($ret, 0, -1) . "] */";
56 #####################################################################
57 # parse a structure element
63 if (has_property($element, "represent_as")) {
64 pidl mapTypeName($element->{PROPERTIES}->{represent_as})." ";
66 if (ref($element->{TYPE}) eq "HASH") {
67 HeaderType($element, $element->{TYPE}, $element->{TYPE}->{NAME});
69 HeaderType($element, $element->{TYPE}, "");
71 pidl " ".ElementStars($element);
73 pidl $element->{NAME};
74 pidl ArrayBrackets($element);
77 if (defined $element->{PROPERTIES}) {
78 HeaderProperties($element->{PROPERTIES}, ["in", "out"]);
83 #####################################################################
85 sub HeaderStruct($$;$)
87 my($struct,$name,$tail) = @_;
89 return if (not defined($struct->{ELEMENTS}));
93 foreach (@{$struct->{ELEMENTS}}) {
98 # some compilers can't handle empty structures
99 pidl tabs()."char _empty_;\n";
103 if (defined $struct->{PROPERTIES}) {
104 HeaderProperties($struct->{PROPERTIES}, []);
106 pidl $tail if defined($tail);
109 #####################################################################
113 my($enum,$name,$tail) = @_;
117 if (defined($enum->{ELEMENTS})) {
118 pidl "\n#ifndef USE_UINT_ENUMS\n";
121 foreach my $e (@{$enum->{ELEMENTS}}) {
122 unless ($first) { pidl ",\n"; }
135 pidl " { __donnot_use_enum_$name=0x7FFFFFFF}\n";
136 foreach my $e (@{$enum->{ELEMENTS}}) {
140 if ($t =~ /(.*)=(.*)/) {
144 fatal($e->{ORIGINAL}, "you can't mix enum member with values and without values!")
145 unless ($without_val == 0);
150 fatal($e->{ORIGINAL}, "you can't mix enum member with values and without values!")
151 unless ($with_val == 0);
153 pidl "#define $name ( $value )\n";
157 pidl $tail if defined($tail);
160 #####################################################################
164 my($bitmap,$name) = @_;
166 return unless defined($bitmap->{ELEMENTS});
168 pidl "/* bitmap $name */\n";
169 pidl "#define $_\n" foreach (@{$bitmap->{ELEMENTS}});
173 #####################################################################
175 sub HeaderUnion($$;$)
177 my($union,$name,$tail) = @_;
181 return if (not defined($union->{ELEMENTS}));
184 foreach my $e (@{$union->{ELEMENTS}}) {
185 if ($e->{TYPE} ne "EMPTY") {
186 if (! defined $done{$e->{NAME}}) {
189 $done{$e->{NAME}} = 1;
195 if (defined $union->{PROPERTIES}) {
196 HeaderProperties($union->{PROPERTIES}, []);
198 pidl $tail if defined($tail);
201 #####################################################################
203 sub HeaderType($$$;$)
205 my($e,$data,$name,$tail) = @_;
206 if (ref($data) eq "HASH") {
207 ($data->{TYPE} eq "ENUM") && HeaderEnum($data, $name, $tail);
208 ($data->{TYPE} eq "BITMAP") && HeaderBitmap($data, $name);
209 ($data->{TYPE} eq "STRUCT") && HeaderStruct($data, $name, $tail);
210 ($data->{TYPE} eq "UNION") && HeaderUnion($data, $name, $tail);
214 if (has_property($e, "charset")) {
217 pidl mapTypeName($e->{TYPE});
219 pidl $tail if defined($tail);
222 #####################################################################
224 sub HeaderTypedef($;$)
226 my($typedef,$tail) = @_;
227 HeaderType($typedef, $typedef->{DATA}, $typedef->{NAME}, $tail) if defined ($typedef->{DATA});
230 #####################################################################
235 if (!defined($const->{ARRAY_LEN}[0])) {
236 pidl "#define $const->{NAME}\t( $const->{VALUE} )\n";
238 pidl "#define $const->{NAME}\t $const->{VALUE}\n";
242 sub ElementDirection($)
246 return "inout" if (has_property($e, "in") and has_property($e, "out"));
247 return "in" if (has_property($e, "in"));
248 return "out" if (has_property($e, "out"));
252 #####################################################################
254 sub HeaderFunctionInOut($$)
258 return unless defined($fn->{ELEMENTS});
260 foreach my $e (@{$fn->{ELEMENTS}}) {
261 HeaderElement($e) if (ElementDirection($e) eq $prop);
265 #####################################################################
266 # determine if we need an "in" or "out" section
267 sub HeaderFunctionInOut_needed($$)
271 return 1 if ($prop eq "out" && defined($fn->{RETURN_TYPE}));
273 return undef unless defined($fn->{ELEMENTS});
275 foreach my $e (@{$fn->{ELEMENTS}}) {
276 return 1 if (ElementDirection($e) eq $prop);
284 #####################################################################
286 sub HeaderFunction($)
290 return if ($headerstructs{$fn->{NAME}});
292 $headerstructs{$fn->{NAME}} = 1;
294 pidl "\nstruct $fn->{NAME} {\n";
298 if (HeaderFunctionInOut_needed($fn, "in") or
299 HeaderFunctionInOut_needed($fn, "inout")) {
300 pidl tabs()."struct {\n";
302 HeaderFunctionInOut($fn, "in");
303 HeaderFunctionInOut($fn, "inout");
305 pidl tabs()."} in;\n\n";
309 if (HeaderFunctionInOut_needed($fn, "out") or
310 HeaderFunctionInOut_needed($fn, "inout")) {
311 pidl tabs()."struct {\n";
313 HeaderFunctionInOut($fn, "out");
314 HeaderFunctionInOut($fn, "inout");
315 if (defined($fn->{RETURN_TYPE})) {
316 pidl tabs().mapTypeName($fn->{RETURN_TYPE}) . " result;\n";
319 pidl tabs()."} out;\n\n";
324 # sigh - some compilers don't like empty structures
325 pidl tabs()."int _dummy_element;\n";
338 pidl choose_header("librpc/gen_ndr/$_\.h", "gen_ndr/$_.h") . "\n";
345 foreach (@includes) {
346 pidl "#include $_\n";
350 #####################################################################
351 # parse the interface definitions
352 sub HeaderInterface($)
354 my($interface) = shift;
356 pidl "#ifndef _HEADER_$interface->{NAME}\n";
357 pidl "#define _HEADER_$interface->{NAME}\n\n";
359 foreach my $c (@{$interface->{CONSTS}}) {
363 foreach my $t (@{$interface->{TYPES}}) {
364 HeaderTypedef($t, ";\n\n") if ($t->{TYPE} eq "TYPEDEF");
365 HeaderStruct($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "STRUCT");
366 HeaderUnion($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "UNION");
367 HeaderEnum($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "ENUM");
368 HeaderBitmap($t, $t->{NAME}) if ($t->{TYPE} eq "BITMAP");
371 foreach my $fn (@{$interface->{FUNCTIONS}}) {
375 pidl "#endif /* _HEADER_$interface->{NAME} */\n";
382 pidl unmake_str($quote->{DATA}) . "\n";
385 #####################################################################
386 # parse a parsed IDL into a C header
394 pidl "/* header auto-generated by pidl */\n\n";
396 pidl "#include <util/data_blob.h>\n";
398 pidl "#include <stdint.h>\n";
402 ($_->{TYPE} eq "CPP_QUOTE") && HeaderQuote($_);
403 ($_->{TYPE} eq "INTERFACE") && HeaderInterface($_);
404 ($_->{TYPE} eq "IMPORT") && HeaderImport(@{$_->{PATHS}});
405 ($_->{TYPE} eq "INCLUDE") && HeaderInclude(@{$_->{PATHS}});
411 sub GenerateStructEnv($$)
416 foreach my $e (@{$x->{ELEMENTS}}) {
417 $env{$e->{NAME}} = "$v->$e->{NAME}";
425 sub EnvSubstituteValue($$)
429 # Substitute the value() values in the env
430 foreach my $e (@{$s->{ELEMENTS}}) {
431 next unless (defined(my $v = has_property($e, "value")));
433 $env->{$e->{NAME}} = ParseExpr($v, $env, $e);
439 sub GenerateFunctionInEnv($;$)
441 my ($fn, $base) = @_;
444 $base = "r->" unless defined($base);
446 foreach my $e (@{$fn->{ELEMENTS}}) {
447 if (grep (/in/, @{$e->{DIRECTION}})) {
448 $env{$e->{NAME}} = $base."in.$e->{NAME}";
455 sub GenerateFunctionOutEnv($;$)
457 my ($fn, $base) = @_;
460 $base = "r->" unless defined($base);
462 foreach my $e (@{$fn->{ELEMENTS}}) {
463 if (grep (/out/, @{$e->{DIRECTION}})) {
464 $env{$e->{NAME}} = $base."out.$e->{NAME}";
465 } elsif (grep (/in/, @{$e->{DIRECTION}})) {
466 $env{$e->{NAME}} = $base."in.$e->{NAME}";