widl: Remove unused headers.
[wine/gsoc_dplay.git] / tools / widl / client.c
blob2b102c87827dff3ab36154b6af76b70be2f79b49
1 /*
2 * IDL Compiler
4 * Copyright 2005-2006 Eric Kohl
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <ctype.h>
30 #include "widl.h"
31 #include "utils.h"
32 #include "parser.h"
33 #include "header.h"
35 #include "widltypes.h"
36 #include "typegen.h"
38 static FILE* client;
39 static int indent = 0;
41 static void print_client( const char *format, ... )
43 va_list va;
44 va_start(va, format);
45 print(client, indent, format, va);
46 va_end(va);
50 static void check_pointers(const func_t *func)
52 const var_t *var;
54 if (!func->args)
55 return;
57 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
59 if (is_var_ptr(var) && cant_be_null(var))
61 print_client("if (!%s)\n", var->name);
62 print_client("{\n");
63 indent++;
64 print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
65 indent--;
66 print_client("}\n\n");
71 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
73 const func_t *func;
74 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
75 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
76 const var_t *var;
77 int method_count = 0;
79 if (!implicit_handle)
80 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n\n", iface->name);
82 if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
84 const var_t *def = func->def;
85 const var_t* explicit_handle_var;
86 int has_full_pointer = is_full_pointer_function(func);
88 /* check for a defined binding handle */
89 explicit_handle_var = get_explicit_handle_var(func);
90 if (explicit_handle)
92 if (!explicit_handle_var)
94 error("%s() does not define an explicit binding handle!\n", def->name);
95 return;
98 else if (implicit_handle)
100 if (explicit_handle_var)
102 error("%s() must not define a binding handle!\n", def->name);
103 return;
107 write_type_decl_left(client, def->type);
108 if (needs_space_after(def->type))
109 fprintf(client, " ");
110 write_prefix_name(client, prefix_client, def);
111 fprintf(client, "(\n");
112 indent++;
113 if (func->args)
114 write_args(client, func->args, iface->name, 0, TRUE);
115 else
116 print_client("void");
117 fprintf(client, ")\n");
118 indent--;
120 /* write the functions body */
121 fprintf(client, "{\n");
122 indent++;
124 /* declare return value '_RetVal' */
125 if (!is_void(def->type))
127 print_client("");
128 write_type_decl_left(client, def->type);
129 fprintf(client, " _RetVal;\n");
132 if (implicit_handle || explicit_handle_var)
133 print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
135 print_client("RPC_MESSAGE _RpcMessage;\n");
136 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
137 if (!is_void(def->type) && decl_indirect(def->type))
139 print_client("void *_p_%s = &%s;\n",
140 "_RetVal", "_RetVal");
142 fprintf(client, "\n");
144 if (has_full_pointer)
145 write_full_pointer_init(client, indent, func, FALSE);
147 /* check pointers */
148 check_pointers(func);
150 print_client("RpcTryFinally\n");
151 print_client("{\n");
152 indent++;
154 print_client("NdrClientInitializeNew(\n");
155 indent++;
156 print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
157 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
158 print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
159 print_client("%d);\n", method_count);
160 indent--;
161 fprintf(client, "\n");
163 if (implicit_handle)
165 print_client("_Handle = %s;\n", implicit_handle);
166 fprintf(client, "\n");
168 else if (explicit_handle_var)
170 print_client("_Handle = %s;\n", explicit_handle_var->name);
171 fprintf(client, "\n");
174 write_remoting_arguments(client, indent, func, PASS_IN, PHASE_BUFFERSIZE);
176 print_client("NdrGetBuffer(\n");
177 indent++;
178 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
179 print_client("_StubMsg.BufferLength,\n");
180 if (implicit_handle || explicit_handle_var)
181 print_client("_Handle);\n");
182 else
183 print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
184 indent--;
185 fprintf(client, "\n");
187 /* marshal arguments */
188 write_remoting_arguments(client, indent, func, PASS_IN, PHASE_MARSHAL);
190 /* send/receive message */
191 /* print_client("NdrNsSendReceive(\n"); */
192 /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
193 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
194 print_client("NdrSendReceive(\n");
195 indent++;
196 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
197 print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
198 indent--;
200 print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
201 print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
203 if (has_out_arg_or_return(func))
205 fprintf(client, "\n");
207 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
208 indent++;
209 print_client("NdrConvert(\n");
210 indent++;
211 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
212 print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
213 indent -= 2;
216 /* unmarshall arguments */
217 fprintf(client, "\n");
218 write_remoting_arguments(client, indent, func, PASS_OUT, PHASE_UNMARSHAL);
220 /* unmarshal return value */
221 if (!is_void(def->type))
223 if (decl_indirect(def->type))
224 print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
225 else if (is_ptr(def->type) || is_array(def->type))
226 print_client("%s = 0;\n", "_RetVal");
227 write_remoting_arguments(client, indent, func, PASS_RETURN, PHASE_UNMARSHAL);
230 /* update proc_offset */
231 if (func->args)
233 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
234 *proc_offset += get_size_procformatstring_var(var);
236 if (!is_void(def->type))
237 *proc_offset += get_size_procformatstring_var(def);
238 else
239 *proc_offset += 2; /* FC_END and FC_PAD */
241 indent--;
242 print_client("}\n");
243 print_client("RpcFinally\n");
244 print_client("{\n");
245 indent++;
248 /* FIXME: emit client finally code */
250 if (has_full_pointer)
251 write_full_pointer_free(client, indent, func);
253 print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
255 indent--;
256 print_client("}\n");
257 print_client("RpcEndFinally\n");
260 /* emit return code */
261 if (!is_void(def->type))
263 fprintf(client, "\n");
264 print_client("return _RetVal;\n");
267 indent--;
268 fprintf(client, "}\n");
269 fprintf(client, "\n");
271 method_count++;
276 static void write_stubdescdecl(type_t *iface)
278 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
279 fprintf(client, "\n");
283 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
285 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
287 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
288 print_client("{\n");
289 indent++;
290 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
291 print_client("MIDL_user_allocate,\n");
292 print_client("MIDL_user_free,\n");
293 print_client("{\n");
294 indent++;
295 if (implicit_handle)
296 print_client("&%s,\n", implicit_handle);
297 else
298 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
299 indent--;
300 print_client("},\n");
301 print_client("0,\n");
302 print_client("0,\n");
303 if (expr_eval_routines)
304 print_client("ExprEvalRoutines,\n");
305 else
306 print_client("0,\n");
307 print_client("0,\n");
308 print_client("__MIDL_TypeFormatString.Format,\n");
309 print_client("1, /* -error bounds_check flag */\n");
310 print_client("0x10001, /* Ndr library version */\n");
311 print_client("0,\n");
312 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
313 print_client("0,\n");
314 print_client("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
315 print_client("0, /* notify & notify_flag routine table */\n");
316 print_client("1, /* Flags */\n");
317 print_client("0, /* Reserved3 */\n");
318 print_client("0, /* Reserved4 */\n");
319 print_client("0 /* Reserved5 */\n");
320 indent--;
321 print_client("};\n");
322 fprintf(client, "\n");
326 static void write_clientinterfacedecl(type_t *iface)
328 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
329 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
330 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
332 if (endpoints) write_endpoints( client, iface->name, endpoints );
334 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
335 print_client("{\n");
336 indent++;
337 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
338 print_client("{{0x%08lx,0x%04x,0x%04x,{0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x}},{%d,%d}},\n",
339 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
340 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
341 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
342 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
343 print_client("0,\n");
344 if (endpoints)
346 print_client("%u,\n", list_count(endpoints));
347 print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
349 else
351 print_client("0,\n");
352 print_client("0,\n");
354 print_client("0,\n");
355 print_client("0,\n");
356 print_client("0,\n");
357 indent--;
358 print_client("};\n");
359 if (old_names)
360 print_client("RPC_IF_HANDLE %s_ClientIfHandle = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
361 iface->name, iface->name);
362 else
363 print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
364 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
365 fprintf(client, "\n");
369 static void write_implicithandledecl(type_t *iface)
371 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
373 if (implicit_handle)
375 fprintf(client, "handle_t %s;\n", implicit_handle);
376 fprintf(client, "\n");
381 static void init_client(void)
383 if (client) return;
384 if (!(client = fopen(client_name, "w")))
385 error("Could not open %s for output\n", client_name);
387 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
388 print_client("#include <string.h>\n");
389 print_client("#ifdef _ALPHA_\n");
390 print_client("#include <stdarg.h>\n");
391 print_client("#endif\n");
392 fprintf(client, "\n");
393 print_client("#include \"%s\"\n", header_name);
394 fprintf(client, "\n");
398 void write_client(ifref_list_t *ifaces)
400 unsigned int proc_offset = 0;
401 int expr_eval_routines;
402 ifref_t *iface;
404 if (!do_client)
405 return;
406 if (do_everything && !need_stub_files(ifaces))
407 return;
409 init_client();
410 if (!client)
411 return;
413 write_formatstringsdecl(client, indent, ifaces, need_stub);
414 expr_eval_routines = write_expr_eval_routines(client, client_token);
415 if (expr_eval_routines)
416 write_expr_eval_routine_list(client, client_token);
417 write_user_quad_list(client);
419 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, ifref_t, entry )
421 if (!need_stub(iface->iface))
422 continue;
424 fprintf(client, "/*****************************************************************************\n");
425 fprintf(client, " * %s interface\n", iface->iface->name);
426 fprintf(client, " */\n");
427 fprintf(client, "\n");
429 if (iface->iface->funcs)
431 write_implicithandledecl(iface->iface);
433 write_clientinterfacedecl(iface->iface);
434 write_stubdescdecl(iface->iface);
435 write_function_stubs(iface->iface, &proc_offset);
437 print_client("#if !defined(__RPC_WIN32__)\n");
438 print_client("#error Invalid build platform for this stub.\n");
439 print_client("#endif\n");
441 fprintf(client, "\n");
442 write_stubdescriptor(iface->iface, expr_eval_routines);
446 fprintf(client, "\n");
448 write_procformatstring(client, ifaces, need_stub);
449 write_typeformatstring(client, ifaces, need_stub);
451 fclose(client);