winegstreamer: Always update time when Run is called.
[wine.git] / tools / widl / client.c
blobb5ac0ae4675fa1896241b16d278a4b27187e3007
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 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <ctype.h>
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
37 #include "widltypes.h"
38 #include "typegen.h"
39 #include "expr.h"
41 static FILE* client;
42 static int indent = 0;
44 static void print_client( const char *format, ... ) __attribute__((format (printf, 1, 2)));
45 static void print_client( const char *format, ... )
47 va_list va;
48 va_start(va, format);
49 print(client, indent, format, va);
50 va_end(va);
54 static void check_pointers(const var_t *func)
56 const var_t *var;
58 if (!type_get_function_args(func->type))
59 return;
61 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
63 if (is_var_ptr(var) && cant_be_null(var))
65 print_client("if (!%s)\n", var->name);
66 print_client("{\n");
67 indent++;
68 print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
69 indent--;
70 print_client("}\n\n");
75 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
77 const statement_t *stmt;
78 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
79 int method_count = 0;
81 if (!implicit_handle)
82 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n\n", iface->name);
84 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
86 const var_t *func = stmt->u.var;
87 const var_t* explicit_handle_var;
88 const var_t* explicit_generic_handle_var = NULL;
89 const var_t* context_handle_var = NULL;
90 int has_full_pointer = is_full_pointer_function(func);
91 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
92 const var_list_t *args = type_get_function_args(func->type);
94 /* check for a defined binding handle */
95 explicit_handle_var = get_explicit_handle_var(func);
96 if (!explicit_handle_var)
98 explicit_generic_handle_var = get_explicit_generic_handle_var(func);
99 if (!explicit_generic_handle_var)
100 context_handle_var = get_context_handle_var(func);
103 print_client( "struct __frame_%s%s\n{\n", prefix_client, get_name(func) );
104 indent++;
105 print_client( "__DECL_EXCEPTION_FRAME\n" );
106 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
107 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
109 if (!implicit_handle && explicit_generic_handle_var)
110 print_client("%s %s;\n",
111 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
112 explicit_generic_handle_var->name );
113 print_client("RPC_BINDING_HANDLE _Handle;\n");
116 if (!is_void(type_function_get_rettype(func->type)) &&
117 decl_indirect(type_function_get_rettype(func->type)))
119 print_client("void *_p_%s;\n", "_RetVal" );
121 indent--;
122 print_client( "};\n\n" );
124 print_client( "static void __finally_%s%s(", prefix_client, get_name(func) );
125 print_client( " struct __frame_%s%s *__frame )\n{\n", prefix_client, get_name(func) );
126 indent++;
128 if (has_full_pointer)
129 write_full_pointer_free(client, indent, func);
131 print_client("NdrFreeBuffer(&__frame->_StubMsg);\n");
133 if (!implicit_handle && explicit_generic_handle_var)
135 fprintf(client, "\n");
136 print_client("if (__frame->_Handle)\n");
137 indent++;
138 print_client("%s_unbind(__frame->%s, __frame->_Handle);\n",
139 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
140 explicit_generic_handle_var->name);
141 indent--;
143 indent--;
144 print_client( "}\n\n" );
146 write_type_decl_left(client, type_function_get_rettype(func->type));
147 if (needs_space_after(type_function_get_rettype(func->type)))
148 fprintf(client, " ");
149 if (callconv) fprintf(client, "%s ", callconv);
150 fprintf(client, "%s%s(\n", prefix_client, get_name(func));
151 indent++;
152 if (args)
153 write_args(client, args, iface->name, 0, TRUE);
154 else
155 print_client("void");
156 fprintf(client, ")\n");
157 indent--;
159 /* write the functions body */
160 fprintf(client, "{\n");
161 indent++;
162 print_client( "struct __frame_%s%s __f, * const __frame = &__f;\n", prefix_client, get_name(func) );
164 /* declare return value '_RetVal' */
165 if (!is_void(type_function_get_rettype(func->type)))
167 print_client("%s", "");
168 write_type_decl_left(client, type_function_get_rettype(func->type));
169 fprintf(client, " _RetVal;\n");
171 print_client("RPC_MESSAGE _RpcMessage;\n");
173 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
175 print_client( "__frame->_Handle = 0;\n" );
176 if (!implicit_handle && explicit_generic_handle_var)
177 print_client("__frame->%s = %s;\n",
178 explicit_generic_handle_var->name, explicit_generic_handle_var->name );
180 if (!is_void(type_function_get_rettype(func->type)) &&
181 decl_indirect(type_function_get_rettype(func->type)))
183 print_client("__frame->_p_%s = &%s;\n",
184 "_RetVal", "_RetVal");
186 fprintf(client, "\n");
188 print_client( "RpcExceptionInit( 0, __finally_%s%s );\n", prefix_client, get_name(func) );
190 if (has_full_pointer)
191 write_full_pointer_init(client, indent, func, FALSE);
193 /* check pointers */
194 check_pointers(func);
196 print_client("RpcTryFinally\n");
197 print_client("{\n");
198 indent++;
200 print_client("NdrClientInitializeNew(&_RpcMessage, &__frame->_StubMsg, &%s_StubDesc, %d);\n",
201 iface->name, method_count);
203 if (is_attr(func->attrs, ATTR_IDEMPOTENT) || is_attr(func->attrs, ATTR_BROADCAST))
205 print_client("_RpcMessage.RpcFlags = ( RPC_NCA_FLAGS_DEFAULT ");
206 if (is_attr(func->attrs, ATTR_IDEMPOTENT))
207 fprintf(client, "| RPC_NCA_FLAGS_IDEMPOTENT ");
208 if (is_attr(func->attrs, ATTR_BROADCAST))
209 fprintf(client, "| RPC_NCA_FLAGS_BROADCAST ");
210 fprintf(client, ");\n\n");
213 if (explicit_handle_var)
215 print_client("__frame->_Handle = %s;\n", explicit_handle_var->name);
216 fprintf(client, "\n");
218 else if (explicit_generic_handle_var)
220 print_client("__frame->_Handle = %s_bind(%s);\n",
221 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
222 explicit_generic_handle_var->name);
223 fprintf(client, "\n");
225 else if (context_handle_var)
227 /* if the context_handle attribute appears in the chain of types
228 * without pointers being followed, then the context handle must
229 * be direct, otherwise it is a pointer */
230 int is_ch_ptr = is_aliaschain_attr(context_handle_var->type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
231 print_client("if (%s%s != 0)\n", is_ch_ptr ? "*" : "", context_handle_var->name);
232 indent++;
233 print_client("__frame->_Handle = NDRCContextBinding(%s%s);\n",
234 is_ch_ptr ? "*" : "", context_handle_var->name);
235 indent--;
236 if (is_attr(context_handle_var->attrs, ATTR_IN) &&
237 !is_attr(context_handle_var->attrs, ATTR_OUT))
239 print_client("else\n");
240 indent++;
241 print_client("RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);\n");
242 indent--;
244 fprintf(client, "\n");
246 else if (implicit_handle)
248 print_client("__frame->_Handle = %s;\n", implicit_handle);
249 fprintf(client, "\n");
252 write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
254 print_client("NdrGetBuffer(&__frame->_StubMsg, __frame->_StubMsg.BufferLength, ");
255 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
256 fprintf(client, "__frame->_Handle);\n\n");
257 else
258 fprintf(client,"%s__MIDL_AutoBindHandle);\n\n", iface->name);
260 /* marshal arguments */
261 write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_MARSHAL);
263 /* send/receive message */
264 /* print_client("NdrNsSendReceive(\n"); */
265 /* print_client("(unsigned char *)__frame->_StubMsg.Buffer,\n"); */
266 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
267 print_client("NdrSendReceive(&__frame->_StubMsg, __frame->_StubMsg.Buffer);\n\n");
269 print_client("__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n");
270 print_client("__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
272 if (has_out_arg_or_return(func))
274 fprintf(client, "\n");
276 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
277 indent++;
278 print_client("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
279 *proc_offset);
280 indent--;
283 /* unmarshall arguments */
284 fprintf(client, "\n");
285 write_remoting_arguments(client, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
287 /* unmarshal return value */
288 if (!is_void(type_function_get_rettype(func->type)))
290 if (decl_indirect(type_function_get_rettype(func->type)))
291 print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
292 else if (is_ptr(type_function_get_rettype(func->type)) ||
293 is_array(type_function_get_rettype(func->type)))
294 print_client("%s = 0;\n", "_RetVal");
295 write_remoting_arguments(client, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
298 /* update proc_offset */
299 *proc_offset += get_size_procformatstring_func( func );
301 indent--;
302 print_client("}\n");
303 print_client("RpcFinally\n");
304 print_client("{\n");
305 indent++;
306 print_client( "__finally_%s%s( __frame );\n", prefix_client, get_name(func) );
307 indent--;
308 print_client("}\n");
309 print_client("RpcEndFinally\n");
312 /* emit return code */
313 if (!is_void(type_function_get_rettype(func->type)))
315 fprintf(client, "\n");
316 print_client("return _RetVal;\n");
319 indent--;
320 fprintf(client, "}\n");
321 fprintf(client, "\n");
323 method_count++;
328 static void write_stubdescdecl(type_t *iface)
330 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
331 fprintf(client, "\n");
335 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
337 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
339 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
340 print_client("{\n");
341 indent++;
342 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
343 print_client("MIDL_user_allocate,\n");
344 print_client("MIDL_user_free,\n");
345 print_client("{\n");
346 indent++;
347 if (implicit_handle)
348 print_client("&%s,\n", implicit_handle);
349 else
350 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
351 indent--;
352 print_client("},\n");
353 print_client("0,\n");
354 print_client("0,\n");
355 if (expr_eval_routines)
356 print_client("ExprEvalRoutines,\n");
357 else
358 print_client("0,\n");
359 print_client("0,\n");
360 print_client("__MIDL_TypeFormatString.Format,\n");
361 print_client("1, /* -error bounds_check flag */\n");
362 print_client("0x10001, /* Ndr library version */\n");
363 print_client("0,\n");
364 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
365 print_client("0,\n");
366 print_client("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
367 print_client("0, /* notify & notify_flag routine table */\n");
368 print_client("1, /* Flags */\n");
369 print_client("0, /* Reserved3 */\n");
370 print_client("0, /* Reserved4 */\n");
371 print_client("0 /* Reserved5 */\n");
372 indent--;
373 print_client("};\n");
374 fprintf(client, "\n");
378 static void write_clientinterfacedecl(type_t *iface)
380 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
381 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
382 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
384 if (endpoints) write_endpoints( client, iface->name, endpoints );
386 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
387 print_client("{\n");
388 indent++;
389 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
390 print_client("{{0x%08x,0x%04x,0x%04x,{0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x}},{%d,%d}},\n",
391 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
392 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
393 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
394 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
395 print_client("0,\n");
396 if (endpoints)
398 print_client("%u,\n", list_count(endpoints));
399 print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
401 else
403 print_client("0,\n");
404 print_client("0,\n");
406 print_client("0,\n");
407 print_client("0,\n");
408 print_client("0,\n");
409 indent--;
410 print_client("};\n");
411 if (old_names)
412 print_client("RPC_IF_HANDLE %s_ClientIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
413 iface->name, iface->name);
414 else
415 print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
416 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
417 fprintf(client, "\n");
421 static void write_implicithandledecl(type_t *iface)
423 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
425 if (implicit_handle)
427 fprintf(client, "handle_t %s;\n", implicit_handle);
428 fprintf(client, "\n");
433 static void init_client(void)
435 if (client) return;
436 if (!(client = fopen(client_name, "w")))
437 error("Could not open %s for output\n", client_name);
439 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
440 print_client("#include <string.h>\n");
441 print_client("#ifdef _ALPHA_\n");
442 print_client("#include <stdarg.h>\n");
443 print_client("#endif\n");
444 fprintf(client, "\n");
445 print_client("#include \"%s\"\n", header_name);
446 print_client( "\n");
447 print_client( "#ifndef DECLSPEC_HIDDEN\n");
448 print_client( "#define DECLSPEC_HIDDEN\n");
449 print_client( "#endif\n");
450 print_client( "\n");
454 static void write_client_ifaces(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
456 const statement_t *stmt;
457 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
459 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
461 int has_func = 0;
462 const statement_t *stmt2;
463 type_t *iface = stmt->u.type;
464 if (!need_stub(iface))
465 return;
467 fprintf(client, "/*****************************************************************************\n");
468 fprintf(client, " * %s interface\n", iface->name);
469 fprintf(client, " */\n");
470 fprintf(client, "\n");
472 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
474 has_func = 1;
475 break;
478 if (has_func)
480 write_implicithandledecl(iface);
482 write_clientinterfacedecl(iface);
483 write_stubdescdecl(iface);
484 write_function_stubs(iface, proc_offset);
486 print_client("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
487 print_client("#error Invalid build platform for this stub.\n");
488 print_client("#endif\n");
490 fprintf(client, "\n");
491 write_stubdescriptor(iface, expr_eval_routines);
494 else if (stmt->type == STMT_LIBRARY)
495 write_client_ifaces(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
499 static void write_client_routines(const statement_list_t *stmts)
501 unsigned int proc_offset = 0;
502 int expr_eval_routines;
504 write_exceptions( client );
505 print_client( "\n");
507 write_formatstringsdecl(client, indent, stmts, need_stub);
508 expr_eval_routines = write_expr_eval_routines(client, client_token);
509 if (expr_eval_routines)
510 write_expr_eval_routine_list(client, client_token);
511 write_user_quad_list(client);
513 write_client_ifaces(stmts, expr_eval_routines, &proc_offset);
515 fprintf(client, "\n");
517 write_procformatstring(client, stmts, need_stub);
518 write_typeformatstring(client, stmts, need_stub);
521 void write_client(const statement_list_t *stmts)
523 if (!do_client)
524 return;
525 if (do_everything && !need_stub_files(stmts))
526 return;
528 init_client();
529 if (!client)
530 return;
532 if (do_win32 && do_win64)
534 fprintf(client, "#ifndef _WIN64\n\n");
535 pointer_size = 4;
536 write_client_routines( stmts );
537 fprintf(client, "\n#else /* _WIN64 */\n\n");
538 pointer_size = 8;
539 write_client_routines( stmts );
540 fprintf(client, "\n#endif /* _WIN64 */\n");
542 else if (do_win32)
544 pointer_size = 4;
545 write_client_routines( stmts );
547 else if (do_win64)
549 pointer_size = 8;
550 write_client_routines( stmts );
553 fclose(client);