widl: Replace erroneously removed current_func assignment.
[wine/winequartzdrv.git] / tools / widl / client.c
blob92208fb04475d559a1e49ce3a18a43b10a83ec10
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)
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 (!implicit_handle)
92 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n\n", iface->name);
94 if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
96 const var_t *def = func->def;
97 const var_t* explicit_handle_var;
99 /* check for a defined binding handle */
100 explicit_handle_var = get_explicit_handle_var(func);
101 if (explicit_handle)
103 if (!explicit_handle_var)
105 error("%s() does not define an explicit binding handle!\n", def->name);
106 return;
109 else if (implicit_handle)
111 if (explicit_handle_var)
113 error("%s() must not define a binding handle!\n", def->name);
114 return;
118 write_type(client, def->type);
119 fprintf(client, " ");
120 write_prefix_name(client, prefix_client, def);
121 fprintf(client, "(\n");
122 indent++;
123 if (func->args)
124 write_args(client, func->args, iface->name, 0, TRUE);
125 else
126 print_client("void");
127 fprintf(client, ")\n");
128 indent--;
130 /* write the functions body */
131 fprintf(client, "{\n");
132 indent++;
134 /* declare return value '_RetVal' */
135 if (!is_void(def->type))
137 print_client("");
138 write_type(client, def->type);
139 fprintf(client, " _RetVal;\n");
142 if (implicit_handle || explicit_handle_var)
143 print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
145 print_client("RPC_MESSAGE _RpcMessage;\n");
146 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
147 fprintf(client, "\n");
149 /* check pointers */
150 check_pointers(func);
152 print_client("RpcTryFinally\n");
153 print_client("{\n");
154 indent++;
156 print_client("NdrClientInitializeNew(\n");
157 indent++;
158 print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
159 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
160 print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
161 print_client("%d);\n", method_count);
162 indent--;
163 fprintf(client, "\n");
165 if (implicit_handle)
167 print_client("_Handle = %s;\n", implicit_handle);
168 fprintf(client, "\n");
170 else if (explicit_handle_var)
172 print_client("_Handle = %s;\n", explicit_handle_var->name);
173 fprintf(client, "\n");
176 write_remoting_arguments(client, indent, func, PASS_IN, PHASE_BUFFERSIZE);
178 print_client("NdrGetBuffer(\n");
179 indent++;
180 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
181 print_client("_StubMsg.BufferLength,\n");
182 if (implicit_handle || explicit_handle_var)
183 print_client("_Handle);\n");
184 else
185 print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
186 indent--;
187 fprintf(client, "\n");
189 /* marshal arguments */
190 write_remoting_arguments(client, indent, func, PASS_IN, PHASE_MARSHAL);
192 /* send/receive message */
193 /* print_client("NdrNsSendReceive(\n"); */
194 /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
195 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
196 print_client("NdrSendReceive(\n");
197 indent++;
198 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
199 print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
200 indent--;
202 print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
203 print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
205 if (has_out_arg_or_return(func))
207 fprintf(client, "\n");
209 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
210 indent++;
211 print_client("NdrConvert(\n");
212 indent++;
213 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
214 print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
215 indent -= 2;
218 /* unmarshall arguments */
219 fprintf(client, "\n");
220 write_remoting_arguments(client, indent, func, PASS_OUT, PHASE_UNMARSHAL);
222 /* unmarshal return value */
223 if (!is_void(def->type))
224 print_phase_basetype(client, indent, PHASE_UNMARSHAL, PASS_RETURN, def, "_RetVal");
226 /* update proc_offset */
227 if (func->args)
229 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
230 *proc_offset += get_size_procformatstring_var(var);
232 if (!is_void(def->type))
233 *proc_offset += get_size_procformatstring_var(def);
234 else
235 *proc_offset += 2; /* FC_END and FC_PAD */
237 indent--;
238 print_client("}\n");
239 print_client("RpcFinally\n");
240 print_client("{\n");
241 indent++;
244 /* FIXME: emit client finally code */
246 print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
248 indent--;
249 print_client("}\n");
250 print_client("RpcEndFinally\n");
253 /* emit return code */
254 if (!is_void(def->type))
256 fprintf(client, "\n");
257 print_client("return _RetVal;\n");
260 indent--;
261 fprintf(client, "}\n");
262 fprintf(client, "\n");
264 method_count++;
269 static void write_stubdescdecl(type_t *iface)
271 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
272 fprintf(client, "\n");
276 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
278 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
280 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
281 print_client("{\n");
282 indent++;
283 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
284 print_client("MIDL_user_allocate,\n");
285 print_client("MIDL_user_free,\n");
286 print_client("{\n");
287 indent++;
288 if (implicit_handle)
289 print_client("&%s,\n", implicit_handle);
290 else
291 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
292 indent--;
293 print_client("},\n");
294 print_client("0,\n");
295 print_client("0,\n");
296 if (expr_eval_routines)
297 print_client("ExprEvalRoutines,\n");
298 else
299 print_client("0,\n");
300 print_client("0,\n");
301 print_client("__MIDL_TypeFormatString.Format,\n");
302 print_client("1, /* -error bounds_check flag */\n");
303 print_client("0x10001, /* Ndr library version */\n");
304 print_client("0,\n");
305 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
306 print_client("0,\n");
307 print_client("0,\n");
308 print_client("0, /* notify & notify_flag routine table */\n");
309 print_client("1, /* Flags */\n");
310 print_client("0, /* Reserved3 */\n");
311 print_client("0, /* Reserved4 */\n");
312 print_client("0 /* Reserved5 */\n");
313 indent--;
314 print_client("};\n");
315 fprintf(client, "\n");
319 static void write_clientinterfacedecl(type_t *iface)
321 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
322 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
323 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
325 if (endpoints) write_endpoints( client, iface->name, endpoints );
327 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
328 print_client("{\n");
329 indent++;
330 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
331 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",
332 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
333 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
334 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
335 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
336 print_client("0,\n");
337 if (endpoints)
339 print_client("%u,\n", list_count(endpoints));
340 print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
342 else
344 print_client("0,\n");
345 print_client("0,\n");
347 print_client("0,\n");
348 print_client("0,\n");
349 print_client("0,\n");
350 indent--;
351 print_client("};\n");
352 if (old_names)
353 print_client("RPC_IF_HANDLE %s_ClientIfHandle = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
354 iface->name, iface->name);
355 else
356 print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
357 prefix_client, iface->name, LOWORD(ver), HIWORD(ver), iface->name);
358 fprintf(client, "\n");
362 static void write_implicithandledecl(type_t *iface)
364 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
366 if (implicit_handle)
368 fprintf(client, "handle_t %s;\n", implicit_handle);
369 fprintf(client, "\n");
374 static void init_client(void)
376 if (client) return;
377 if (!(client = fopen(client_name, "w")))
378 error("Could not open %s for output\n", client_name);
380 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
381 print_client("#include <string.h>\n");
382 print_client("#ifdef _ALPHA_\n");
383 print_client("#include <stdarg.h>\n");
384 print_client("#endif\n");
385 fprintf(client, "\n");
386 print_client("#include \"%s\"\n", header_name);
387 fprintf(client, "\n");
391 void write_client(ifref_list_t *ifaces)
393 unsigned int proc_offset = 0;
394 ifref_t *iface;
396 if (!do_client)
397 return;
398 if (do_everything && !ifaces)
399 return;
401 init_client();
402 if (!client)
403 return;
405 write_formatstringsdecl(client, indent, ifaces, 0);
407 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, ifref_t, entry )
409 if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
410 continue;
412 fprintf(client, "/*****************************************************************************\n");
413 fprintf(client, " * %s interface\n", iface->iface->name);
414 fprintf(client, " */\n");
415 fprintf(client, "\n");
417 if (iface->iface->funcs)
419 int expr_eval_routines;
421 write_implicithandledecl(iface->iface);
423 write_clientinterfacedecl(iface->iface);
424 write_stubdescdecl(iface->iface);
425 write_function_stubs(iface->iface, &proc_offset);
427 print_client("#if !defined(__RPC_WIN32__)\n");
428 print_client("#error Invalid build platform for this stub.\n");
429 print_client("#endif\n");
431 fprintf(client, "\n");
433 expr_eval_routines = write_expr_eval_routines(client, iface->iface->name);
434 if (expr_eval_routines)
435 write_expr_eval_routine_list(client, iface->iface->name);
436 write_stubdescriptor(iface->iface, expr_eval_routines);
440 fprintf(client, "\n");
442 write_procformatstring(client, ifaces, 0);
443 write_typeformatstring(client, ifaces, 0);
445 fclose(client);