Sync to wine-0.9.60:
[reactos.git] / reactos / tools / widl / client.c
1 /*
2 * IDL Compiler
3 *
4 * Copyright 2005-2006 Eric Kohl
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <ctype.h>
31
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
36
37 #include "widltypes.h"
38 #include "typegen.h"
39
40 static FILE* client;
41 static int indent = 0;
42
43 static void print_client( const char *format, ... )
44 {
45 va_list va;
46 va_start(va, format);
47 print(client, indent, format, va);
48 va_end(va);
49 }
50
51
52 static void check_pointers(const func_t *func)
53 {
54 const var_t *var;
55
56 if (!func->args)
57 return;
58
59 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
60 {
61 if (is_var_ptr(var) && cant_be_null(var))
62 {
63 print_client("if (!%s)\n", var->name);
64 print_client("{\n");
65 indent++;
66 print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
67 indent--;
68 print_client("}\n\n");
69 }
70 }
71 }
72
73 const var_t* get_context_handle_var(const func_t* func)
74 {
75 const var_t* var;
76
77 if (!func->args)
78 return NULL;
79
80 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
81 if (is_attr(var->attrs, ATTR_IN) && is_context_handle(var->type))
82 return var;
83
84 return NULL;
85 }
86
87 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
88 {
89 const func_t *func;
90 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
91 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
92 const var_t *var;
93 int method_count = 0;
94
95 if (!implicit_handle)
96 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n\n", iface->name);
97
98 if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
99 {
100 const var_t *def = func->def;
101 const var_t* explicit_handle_var;
102 const var_t* explicit_generic_handle_var = NULL;
103 const var_t* context_handle_var = NULL;
104 int has_full_pointer = is_full_pointer_function(func);
105 const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
106
107 /* check for a defined binding handle */
108 explicit_handle_var = get_explicit_handle_var(func);
109 if (!explicit_handle_var)
110 {
111 explicit_generic_handle_var = get_explicit_generic_handle_var(func);
112 if (!explicit_generic_handle_var)
113 context_handle_var = get_context_handle_var(func);
114 }
115 if (explicit_handle)
116 {
117 if (!explicit_handle_var && !explicit_generic_handle_var && !context_handle_var)
118 {
119 error("%s() does not define an explicit binding handle!\n", def->name);
120 return;
121 }
122 }
123 else if (implicit_handle)
124 {
125 if (explicit_handle_var)
126 {
127 error("%s() must not define a binding handle!\n", def->name);
128 return;
129 }
130 }
131
132 write_type_decl_left(client, get_func_return_type(func));
133 if (needs_space_after(get_func_return_type(func)))
134 fprintf(client, " ");
135 if (callconv) fprintf(client, "%s ", callconv);
136 write_prefix_name(client, prefix_client, def);
137 fprintf(client, "(\n");
138 indent++;
139 if (func->args)
140 write_args(client, func->args, iface->name, 0, TRUE);
141 else
142 print_client("void");
143 fprintf(client, ")\n");
144 indent--;
145
146 /* write the functions body */
147 fprintf(client, "{\n");
148 indent++;
149
150 /* declare return value '_RetVal' */
151 if (!is_void(get_func_return_type(func)))
152 {
153 print_client("");
154 write_type_decl_left(client, get_func_return_type(func));
155 fprintf(client, " _RetVal;\n");
156 }
157
158 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
159 print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
160
161 print_client("RPC_MESSAGE _RpcMessage;\n");
162 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
163 if (!is_void(get_func_return_type(func)) && decl_indirect(get_func_return_type(func)))
164 {
165 print_client("void *_p_%s = &%s;\n",
166 "_RetVal", "_RetVal");
167 }
168 fprintf(client, "\n");
169
170 if (has_full_pointer)
171 write_full_pointer_init(client, indent, func, FALSE);
172
173 /* check pointers */
174 check_pointers(func);
175
176 print_client("RpcTryFinally\n");
177 print_client("{\n");
178 indent++;
179
180 print_client("NdrClientInitializeNew(\n");
181 indent++;
182 print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
183 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
184 print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
185 print_client("%d);\n", method_count);
186 indent--;
187 fprintf(client, "\n");
188
189 if (is_attr(def->attrs, ATTR_IDEMPOTENT) || is_attr(def->attrs, ATTR_BROADCAST))
190 {
191 print_client("_RpcMessage.RpcFlags = ( RPC_NCA_FLAGS_DEFAULT ");
192 if (is_attr(def->attrs, ATTR_IDEMPOTENT))
193 fprintf(client, "| RPC_NCA_FLAGS_IDEMPOTENT ");
194 if (is_attr(def->attrs, ATTR_BROADCAST))
195 fprintf(client, "| RPC_NCA_FLAGS_BROADCAST ");
196 fprintf(client, ");\n\n");
197 }
198
199 if (implicit_handle)
200 {
201 print_client("_Handle = %s;\n", implicit_handle);
202 fprintf(client, "\n");
203 }
204 else if (explicit_handle_var)
205 {
206 print_client("_Handle = %s;\n", explicit_handle_var->name);
207 fprintf(client, "\n");
208 }
209 else if (explicit_generic_handle_var)
210 {
211 print_client("_Handle = %s_bind(%s);\n",
212 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
213 explicit_generic_handle_var->name);
214 fprintf(client, "\n");
215 }
216 else if (context_handle_var)
217 {
218 /* if the context_handle attribute appears in the chain of types
219 * without pointers being followed, then the context handle must
220 * be direct, otherwise it is a pointer */
221 int is_ch_ptr = is_aliaschain_attr(context_handle_var->type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
222 print_client("if (%s%s != 0)\n", is_ch_ptr ? "*" : "", context_handle_var->name);
223 indent++;
224 print_client("_Handle = NDRCContextBinding(%s%s);\n", is_ch_ptr ? "*" : "", context_handle_var->name);
225 indent--;
226 fprintf(client, "\n");
227 }
228
229 write_remoting_arguments(client, indent, func, PASS_IN, PHASE_BUFFERSIZE);
230
231 print_client("NdrGetBuffer(\n");
232 indent++;
233 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
234 print_client("_StubMsg.BufferLength,\n");
235 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
236 print_client("_Handle);\n");
237 else
238 print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
239 indent--;
240 fprintf(client, "\n");
241
242 /* marshal arguments */
243 write_remoting_arguments(client, indent, func, PASS_IN, PHASE_MARSHAL);
244
245 /* send/receive message */
246 /* print_client("NdrNsSendReceive(\n"); */
247 /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
248 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
249 print_client("NdrSendReceive(\n");
250 indent++;
251 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
252 print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
253 indent--;
254
255 print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
256 print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
257
258 if (has_out_arg_or_return(func))
259 {
260 fprintf(client, "\n");
261
262 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
263 indent++;
264 print_client("NdrConvert(\n");
265 indent++;
266 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
267 print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
268 indent -= 2;
269 }
270
271 /* unmarshall arguments */
272 fprintf(client, "\n");
273 write_remoting_arguments(client, indent, func, PASS_OUT, PHASE_UNMARSHAL);
274
275 /* unmarshal return value */
276 if (!is_void(get_func_return_type(func)))
277 {
278 if (decl_indirect(get_func_return_type(func)))
279 print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
280 else if (is_ptr(get_func_return_type(func)) || is_array(get_func_return_type(func)))
281 print_client("%s = 0;\n", "_RetVal");
282 write_remoting_arguments(client, indent, func, PASS_RETURN, PHASE_UNMARSHAL);
283 }
284
285 /* update proc_offset */
286 if (func->args)
287 {
288 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
289 *proc_offset += get_size_procformatstring_type(var->name, var->type, var->attrs);
290 }
291 if (!is_void(get_func_return_type(func)))
292 *proc_offset += get_size_procformatstring_type("return value", get_func_return_type(func), NULL);
293 else
294 *proc_offset += 2; /* FC_END and FC_PAD */
295
296 indent--;
297 print_client("}\n");
298 print_client("RpcFinally\n");
299 print_client("{\n");
300 indent++;
301
302
303 /* FIXME: emit client finally code */
304
305 if (has_full_pointer)
306 write_full_pointer_free(client, indent, func);
307
308 print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
309
310 if (!implicit_handle && explicit_generic_handle_var)
311 {
312 fprintf(client, "\n");
313 print_client("if (_Handle)\n");
314 indent++;
315 print_client("%s_unbind(%s, _Handle);\n",
316 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
317 explicit_generic_handle_var->name);
318 indent--;
319 }
320
321 indent--;
322 print_client("}\n");
323 print_client("RpcEndFinally\n");
324
325
326 /* emit return code */
327 if (!is_void(get_func_return_type(func)))
328 {
329 fprintf(client, "\n");
330 print_client("return _RetVal;\n");
331 }
332
333 indent--;
334 fprintf(client, "}\n");
335 fprintf(client, "\n");
336
337 method_count++;
338 }
339 }
340
341
342 static void write_stubdescdecl(type_t *iface)
343 {
344 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
345 fprintf(client, "\n");
346 }
347
348
349 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
350 {
351 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
352
353 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
354 print_client("{\n");
355 indent++;
356 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
357 print_client("MIDL_user_allocate,\n");
358 print_client("MIDL_user_free,\n");
359 print_client("{\n");
360 indent++;
361 if (implicit_handle)
362 print_client("&%s,\n", implicit_handle);
363 else
364 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
365 indent--;
366 print_client("},\n");
367 print_client("0,\n");
368 print_client("0,\n");
369 if (expr_eval_routines)
370 print_client("ExprEvalRoutines,\n");
371 else
372 print_client("0,\n");
373 print_client("0,\n");
374 print_client("__MIDL_TypeFormatString.Format,\n");
375 print_client("1, /* -error bounds_check flag */\n");
376 print_client("0x10001, /* Ndr library version */\n");
377 print_client("0,\n");
378 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
379 print_client("0,\n");
380 print_client("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
381 print_client("0, /* notify & notify_flag routine table */\n");
382 print_client("1, /* Flags */\n");
383 print_client("0, /* Reserved3 */\n");
384 print_client("0, /* Reserved4 */\n");
385 print_client("0 /* Reserved5 */\n");
386 indent--;
387 print_client("};\n");
388 fprintf(client, "\n");
389 }
390
391
392 static void write_clientinterfacedecl(type_t *iface)
393 {
394 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
395 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
396 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
397
398 if (endpoints) write_endpoints( client, iface->name, endpoints );
399
400 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
401 print_client("{\n");
402 indent++;
403 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
404 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",
405 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
406 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
407 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
408 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
409 print_client("0,\n");
410 if (endpoints)
411 {
412 print_client("%u,\n", list_count(endpoints));
413 print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
414 }
415 else
416 {
417 print_client("0,\n");
418 print_client("0,\n");
419 }
420 print_client("0,\n");
421 print_client("0,\n");
422 print_client("0,\n");
423 indent--;
424 print_client("};\n");
425 if (old_names)
426 print_client("RPC_IF_HANDLE %s_ClientIfHandle = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
427 iface->name, iface->name);
428 else
429 print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
430 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
431 fprintf(client, "\n");
432 }
433
434
435 static void write_implicithandledecl(type_t *iface)
436 {
437 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
438
439 if (implicit_handle)
440 {
441 fprintf(client, "handle_t %s;\n", implicit_handle);
442 fprintf(client, "\n");
443 }
444 }
445
446
447 static void init_client(void)
448 {
449 if (client) return;
450 if (!(client = fopen(client_name, "w")))
451 error("Could not open %s for output\n", client_name);
452
453 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
454 print_client("#include <string.h>\n");
455 print_client("#ifdef _ALPHA_\n");
456 print_client("#include <stdarg.h>\n");
457 print_client("#endif\n");
458 fprintf(client, "\n");
459 print_client("#include \"%s\"\n", header_name);
460 fprintf(client, "\n");
461 }
462
463
464 void write_client(ifref_list_t *ifaces)
465 {
466 unsigned int proc_offset = 0;
467 int expr_eval_routines;
468 ifref_t *iface;
469
470 if (!do_client)
471 return;
472 if (do_everything && !need_stub_files(ifaces))
473 return;
474
475 init_client();
476 if (!client)
477 return;
478
479 write_formatstringsdecl(client, indent, ifaces, need_stub);
480 expr_eval_routines = write_expr_eval_routines(client, client_token);
481 if (expr_eval_routines)
482 write_expr_eval_routine_list(client, client_token);
483 write_user_quad_list(client);
484
485 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, ifref_t, entry )
486 {
487 if (!need_stub(iface->iface))
488 continue;
489
490 fprintf(client, "/*****************************************************************************\n");
491 fprintf(client, " * %s interface\n", iface->iface->name);
492 fprintf(client, " */\n");
493 fprintf(client, "\n");
494
495 if (iface->iface->funcs)
496 {
497 write_implicithandledecl(iface->iface);
498
499 write_clientinterfacedecl(iface->iface);
500 write_stubdescdecl(iface->iface);
501 write_function_stubs(iface->iface, &proc_offset);
502
503 print_client("#if !defined(__RPC_WIN32__)\n");
504 print_client("#error Invalid build platform for this stub.\n");
505 print_client("#endif\n");
506
507 fprintf(client, "\n");
508 write_stubdescriptor(iface->iface, expr_eval_routines);
509 }
510 }
511
512 fprintf(client, "\n");
513
514 write_procformatstring(client, ifaces, need_stub);
515 write_typeformatstring(client, ifaces, need_stub);
516
517 fclose(client);
518 }