widl: Output endpoint information in client and server files.
[wine/multimedia.git] / tools / widl / client.c
blob3322a07e21d970350638f67e8aa6df2194babd17
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 <assert.h>
29 #include <ctype.h>
30 #include <signal.h>
32 #include "windef.h"
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
39 #include "widltypes.h"
40 #include "typelib.h"
41 #include "typelib_struct.h"
42 #include "typegen.h"
44 static FILE* client;
45 static int indent = 0;
47 static int print_client( const char *format, ... )
49 va_list va;
50 int i, r;
52 va_start(va, format);
53 if (format[0] != '\n')
54 for (i = 0; i < indent; i++)
55 fprintf(client, " ");
56 r = vfprintf(client, format, va);
57 va_end(va);
58 return r;
62 static void check_pointers(const func_t *func)
64 const var_t *var;
66 if (!func->args)
67 return;
69 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
71 if (is_var_ptr(var) && cant_be_null(var))
73 print_client("if (!%s)\n", var->name);
74 print_client("{\n");
75 indent++;
76 print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
77 indent--;
78 print_client("}\n\n");
83 static void write_function_stubs(type_t *iface, unsigned int *proc_offset, unsigned int *type_offset)
85 const func_t *func;
86 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
87 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
88 const var_t *var;
89 int method_count = 0;
91 if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
93 const var_t *def = func->def;
94 const var_t* explicit_handle_var;
95 unsigned int type_offset_func;
97 /* check for a defined binding handle */
98 explicit_handle_var = get_explicit_handle_var(func);
99 if (explicit_handle)
101 if (!explicit_handle_var)
103 error("%s() does not define an explicit binding handle!\n", def->name);
104 return;
107 else if (implicit_handle)
109 if (explicit_handle_var)
111 error("%s() must not define a binding handle!\n", def->name);
112 return;
116 write_type(client, def->type, def, def->tname);
117 fprintf(client, " ");
118 write_name(client, def);
119 fprintf(client, "(\n");
120 indent++;
121 if (func->args)
122 write_args(client, func->args, iface->name, 0, TRUE);
123 else
124 print_client("void");
125 fprintf(client, ")\n");
126 indent--;
128 /* write the functions body */
129 fprintf(client, "{\n");
130 indent++;
132 /* declare return value '_RetVal' */
133 if (!is_void(def->type, NULL))
135 print_client("");
136 write_type(client, def->type, def, def->tname);
137 fprintf(client, " _RetVal;\n");
140 if (implicit_handle || explicit_handle_var)
141 print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
143 print_client("RPC_MESSAGE _RpcMessage;\n");
144 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
145 fprintf(client, "\n");
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 type_offset_func = *type_offset;
175 write_remoting_arguments(client, indent, func, &type_offset_func, PASS_IN, PHASE_BUFFERSIZE);
177 print_client("NdrGetBuffer(\n");
178 indent++;
179 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
180 print_client("_StubMsg.BufferLength,\n");
181 if (implicit_handle || explicit_handle_var)
182 print_client("_Handle);\n");
183 else
184 print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
185 indent--;
186 fprintf(client, "\n");
189 /* make a copy so we don't increment the type offset twice */
190 type_offset_func = *type_offset;
192 /* marshal arguments */
193 write_remoting_arguments(client, indent, func, &type_offset_func, PASS_IN, PHASE_MARSHAL);
195 /* send/receive message */
196 /* print_client("NdrNsSendReceive(\n"); */
197 /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
198 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
199 print_client("NdrSendReceive(\n");
200 indent++;
201 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
202 print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
203 indent--;
205 print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
206 print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
208 if (has_out_arg_or_return(func))
210 fprintf(client, "\n");
212 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
213 indent++;
214 print_client("NdrConvert(\n");
215 indent++;
216 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
217 print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
218 indent -= 2;
221 /* unmarshall arguments */
222 fprintf(client, "\n");
223 write_remoting_arguments(client, indent, func, type_offset, PASS_OUT, PHASE_UNMARSHAL);
225 /* unmarshal return value */
226 if (!is_void(def->type, NULL))
227 print_phase_basetype(client, indent, PHASE_UNMARSHAL, PASS_RETURN, def, "_RetVal");
229 /* update proc_offset */
230 if (func->args)
232 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
233 *proc_offset += get_size_procformatstring_var(var);
235 if (!is_void(def->type, NULL))
236 *proc_offset += get_size_procformatstring_var(def);
237 else
238 *proc_offset += 2; /* FC_END and FC_PAD */
240 indent--;
241 print_client("}\n");
242 print_client("RpcFinally\n");
243 print_client("{\n");
244 indent++;
247 /* FIXME: emit client finally code */
249 print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
251 indent--;
252 print_client("}\n");
253 print_client("RpcEndFinally\n");
256 /* emit return code */
257 if (!is_void(def->type, NULL))
259 fprintf(client, "\n");
260 print_client("return _RetVal;\n");
263 indent--;
264 fprintf(client, "}\n");
265 fprintf(client, "\n");
267 method_count++;
272 static void write_bindinghandledecl(type_t *iface)
274 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n", iface->name);
275 fprintf(client, "\n");
279 static void write_stubdescdecl(type_t *iface)
281 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
282 fprintf(client, "\n");
286 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
288 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
290 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
291 print_client("{\n");
292 indent++;
293 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
294 print_client("MIDL_user_allocate,\n");
295 print_client("MIDL_user_free,\n");
296 print_client("{\n");
297 indent++;
298 if (implicit_handle)
299 print_client("&%s,\n", implicit_handle);
300 else
301 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
302 indent--;
303 print_client("},\n");
304 print_client("0,\n");
305 print_client("0,\n");
306 if (expr_eval_routines)
307 print_client("ExprEvalRoutines,\n");
308 else
309 print_client("0,\n");
310 print_client("0,\n");
311 print_client("__MIDL_TypeFormatString.Format,\n");
312 print_client("1, /* -error bounds_check flag */\n");
313 print_client("0x10001, /* Ndr library version */\n");
314 print_client("0,\n");
315 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
316 print_client("0,\n");
317 print_client("0,\n");
318 print_client("0, /* notify & notify_flag routine table */\n");
319 print_client("1, /* Flags */\n");
320 print_client("0, /* Reserved3 */\n");
321 print_client("0, /* Reserved4 */\n");
322 print_client("0 /* Reserved5 */\n");
323 indent--;
324 print_client("};\n");
325 fprintf(client, "\n");
329 static void write_clientinterfacedecl(type_t *iface)
331 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
332 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
333 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
335 if (endpoints) write_endpoints( client, iface->name, endpoints );
337 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
338 print_client("{\n");
339 indent++;
340 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
341 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",
342 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
343 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
344 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
345 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
346 print_client("0,\n");
347 if (endpoints)
349 print_client("%u,\n", list_count(endpoints));
350 print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
352 else
354 print_client("0,\n");
355 print_client("0,\n");
357 print_client("0,\n");
358 print_client("0,\n");
359 print_client("0,\n");
360 indent--;
361 print_client("};\n");
362 if (old_names)
363 print_client("RPC_IF_HANDLE %s_ClientIfHandle = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
364 iface->name, iface->name);
365 else
366 print_client("RPC_IF_HANDLE %s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
367 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
368 fprintf(client, "\n");
372 static void write_implicithandledecl(type_t *iface)
374 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
376 if (implicit_handle)
378 fprintf(client, "handle_t %s;\n", implicit_handle);
379 fprintf(client, "\n");
384 static void init_client(void)
386 if (client) return;
387 if (!(client = fopen(client_name, "w")))
388 error("Could not open %s for output\n", client_name);
390 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
391 print_client("#include <string.h>\n");
392 print_client("#ifdef _ALPHA_\n");
393 print_client("#include <stdarg.h>\n");
394 print_client("#endif\n");
395 fprintf(client, "\n");
396 print_client("#include \"%s\"\n", header_name);
397 fprintf(client, "\n");
401 void write_client(ifref_list_t *ifaces)
403 unsigned int proc_offset = 0;
404 unsigned int type_offset = 2;
405 ifref_t *iface;
407 if (!do_client)
408 return;
409 if (do_everything && !ifaces)
410 return;
412 init_client();
413 if (!client)
414 return;
416 write_formatstringsdecl(client, indent, ifaces, 0);
418 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, ifref_t, entry )
420 if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
421 continue;
423 fprintf(client, "/*****************************************************************************\n");
424 fprintf(client, " * %s interface\n", iface->iface->name);
425 fprintf(client, " */\n");
426 fprintf(client, "\n");
428 if (iface->iface->funcs)
430 int expr_eval_routines;
432 write_implicithandledecl(iface->iface);
434 write_clientinterfacedecl(iface->iface);
435 write_stubdescdecl(iface->iface);
436 write_bindinghandledecl(iface->iface);
438 write_function_stubs(iface->iface, &proc_offset, &type_offset);
440 print_client("#if !defined(__RPC_WIN32__)\n");
441 print_client("#error Invalid build platform for this stub.\n");
442 print_client("#endif\n");
444 fprintf(client, "\n");
446 expr_eval_routines = write_expr_eval_routines(client, iface->iface->name);
447 if (expr_eval_routines)
448 write_expr_eval_routine_list(client, iface->iface->name);
449 write_stubdescriptor(iface->iface, expr_eval_routines);
453 fprintf(client, "\n");
455 write_procformatstring(client, ifaces, 0);
456 write_typeformatstring(client, ifaces, 0);
458 fclose(client);