pidl: Add skip option to elements.
[Samba.git] / pidl / lib / Parse / Pidl / Dump.pm
blobbf5811c116f8f5c240ce01a3ae5cbf26a1f44620
1 ###################################################
2 # dump function for IDL structures
3 # Copyright tridge@samba.org 2000
4 # Copyright jelmer@samba.org 2005
5 # released under the GNU GPL
7 =pod
9 =head1 NAME
11 Parse::Pidl::Dump - Dump support
13 =head1 DESCRIPTION
15 This module provides functions that can generate IDL code from
16 internal pidl data structures.
18 =cut
20 package Parse::Pidl::Dump;
22 use Exporter;
24 use vars qw($VERSION);
25 $VERSION = '0.01';
26 @ISA = qw(Exporter);
27 @EXPORT_OK = qw(DumpType DumpTypedef DumpStruct DumpEnum DumpBitmap DumpUnion DumpFunction);
29 use strict;
30 use Parse::Pidl::Util qw(has_property);
32 my($res);
34 #####################################################################
35 # dump a properties list
36 sub DumpProperties($)
38 my($props) = shift;
39 my $res = "";
41 foreach my $d ($props) {
42 foreach my $k (keys %{$d}) {
43 if ($k eq "in") {
44 $res .= "[in] ";
45 next;
47 if ($k eq "out") {
48 $res .= "[out] ";
49 next;
51 if ($k eq "ref") {
52 $res .= "[ref] ";
53 next;
55 $res .= "[$k($d->{$k})] ";
58 return $res;
61 #####################################################################
62 # dump a structure element
63 sub DumpElement($)
65 my($element) = shift;
66 my $res = "";
68 (defined $element->{PROPERTIES}) &&
69 ($res .= DumpProperties($element->{PROPERTIES}));
70 $res .= DumpType($element->{TYPE});
71 $res .= " ";
72 for my $i (1..$element->{POINTERS}) {
73 $res .= "*";
75 $res .= "$element->{NAME}";
76 foreach (@{$element->{ARRAY_LEN}}) {
77 $res .= "[$_]";
80 return $res;
83 #####################################################################
84 # dump a struct
85 sub DumpStruct($)
87 my($struct) = shift;
88 my($res);
90 $res .= "struct ";
91 if ($struct->{NAME}) {
92 $res.="$struct->{NAME} ";
95 $res.="{\n";
96 if (defined $struct->{ELEMENTS}) {
97 foreach (@{$struct->{ELEMENTS}}) {
98 $res .= "\t" . DumpElement($_) . ";\n";
101 $res .= "}";
103 return $res;
107 #####################################################################
108 # dump a struct
109 sub DumpEnum($)
111 my($enum) = shift;
112 my($res);
114 $res .= "enum {\n";
116 foreach (@{$enum->{ELEMENTS}}) {
117 if (/^([A-Za-z0-9_]+)[ \t]*\((.*)\)$/) {
118 $res .= "\t$1 = $2,\n";
119 } else {
120 $res .= "\t$_,\n";
124 $res.= "}";
126 return $res;
129 #####################################################################
130 # dump a struct
131 sub DumpBitmap($)
133 my($bitmap) = shift;
134 my($res);
136 $res .= "bitmap {\n";
138 foreach (@{$bitmap->{ELEMENTS}}) {
139 if (/^([A-Za-z0-9_]+)[ \t]*\((.*)\)$/) {
140 $res .= "\t$1 = $2,\n";
141 } else {
142 die ("Bitmap $bitmap->{NAME} has field $_ without proper value");
146 $res.= "}";
148 return $res;
152 #####################################################################
153 # dump a union element
154 sub DumpUnionElement($)
156 my($element) = shift;
157 my($res);
159 if (has_property($element, "default")) {
160 $res .= "[default] ;\n";
161 } else {
162 $res .= "[case($element->{PROPERTIES}->{case})] ";
163 $res .= DumpElement($element), if defined($element);
164 $res .= ";\n";
167 return $res;
170 #####################################################################
171 # dump a union
172 sub DumpUnion($)
174 my($union) = shift;
175 my($res);
177 (defined $union->{PROPERTIES}) &&
178 ($res .= DumpProperties($union->{PROPERTIES}));
179 $res .= "union {\n";
180 foreach my $e (@{$union->{ELEMENTS}}) {
181 $res .= DumpUnionElement($e);
183 $res .= "}";
185 return $res;
188 #####################################################################
189 # dump a type
190 sub DumpType($)
192 my($data) = shift;
194 if (ref($data) eq "HASH") {
195 return DumpStruct($data) if ($data->{TYPE} eq "STRUCT");
196 return DumpUnion($data) if ($data->{TYPE} eq "UNION");
197 return DumpEnum($data) if ($data->{TYPE} eq "ENUM");
198 return DumpBitmap($data) if ($data->{TYPE} eq "BITMAP");
199 } else {
200 return $data;
204 #####################################################################
205 # dump a typedef
206 sub DumpTypedef($)
208 my($typedef) = shift;
209 my($res);
211 $res .= "typedef ";
212 $res .= DumpType($typedef->{DATA});
213 $res .= " $typedef->{NAME};\n\n";
215 return $res;
218 #####################################################################
219 # dump a typedef
220 sub DumpFunction($)
222 my($function) = shift;
223 my($first) = 1;
224 my($res);
226 $res .= DumpType($function->{RETURN_TYPE});
227 $res .= " $function->{NAME}(\n";
228 for my $d (@{$function->{ELEMENTS}}) {
229 unless ($first) { $res .= ",\n"; } $first = 0;
230 $res .= DumpElement($d);
232 $res .= "\n);\n\n";
234 return $res;
237 #####################################################################
238 # dump a module header
239 sub DumpInterfaceProperties($)
241 my($header) = shift;
242 my($data) = $header->{DATA};
243 my($first) = 1;
244 my($res);
246 $res .= "[\n";
247 foreach my $k (keys %{$data}) {
248 $first || ($res .= ",\n"); $first = 0;
249 $res .= "$k($data->{$k})";
251 $res .= "\n]\n";
253 return $res;
256 #####################################################################
257 # dump the interface definitions
258 sub DumpInterface($)
260 my($interface) = shift;
261 my($data) = $interface->{DATA};
262 my($res);
264 $res .= DumpInterfaceProperties($interface->{PROPERTIES});
266 $res .= "interface $interface->{NAME}\n{\n";
267 foreach my $d (@{$data}) {
268 ($d->{TYPE} eq "TYPEDEF") &&
269 ($res .= DumpTypedef($d));
270 ($d->{TYPE} eq "FUNCTION") &&
271 ($res .= DumpFunction($d));
273 $res .= "}\n";
275 return $res;
279 #####################################################################
280 # dump a parsed IDL structure back into an IDL file
281 sub Dump($)
283 my($idl) = shift;
284 my($res);
286 $res = "/* Dumped by pidl */\n\n";
287 foreach my $x (@{$idl}) {
288 ($x->{TYPE} eq "INTERFACE") &&
289 ($res .= DumpInterface($x));
291 return $res;