push bf834a7eef2241618d351018da1587a7ae2466d1
[wine/hacks.git] / tools / widl / client.c
bloba23e298491cc6c024990a2772e2d3090d3f350a3
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, ... )
46 va_list va;
47 va_start(va, format);
48 print(client, indent, format, va);
49 va_end(va);
53 static void check_pointers(const var_t *func)
55 const var_t *var;
57 if (!type_get_function_args(func->type))
58 return;
60 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
62 if (is_var_ptr(var) && cant_be_null(var))
64 print_client("if (!%s)\n", var->name);
65 print_client("{\n");
66 indent++;
67 print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
68 indent--;
69 print_client("}\n\n");
74 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
76 const statement_t *stmt;
77 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
78 const var_t *var;
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 /* FIXME: emit client finally code */
130 if (has_full_pointer)
131 write_full_pointer_free(client, indent, func);
133 print_client("NdrFreeBuffer(&__frame->_StubMsg);\n");
135 if (!implicit_handle && explicit_generic_handle_var)
137 fprintf(client, "\n");
138 print_client("if (__frame->_Handle)\n");
139 indent++;
140 print_client("%s_unbind(__frame->%s, __frame->_Handle);\n",
141 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
142 explicit_generic_handle_var->name);
143 indent--;
145 indent--;
146 print_client( "}\n\n" );
148 write_type_decl_left(client, type_function_get_rettype(func->type));
149 if (needs_space_after(type_function_get_rettype(func->type)))
150 fprintf(client, " ");
151 if (callconv) fprintf(client, "%s ", callconv);
152 fprintf(client, "%s%s(\n", prefix_client, get_name(func));
153 indent++;
154 if (args)
155 write_args(client, args, iface->name, 0, TRUE);
156 else
157 print_client("void");
158 fprintf(client, ")\n");
159 indent--;
161 /* write the functions body */
162 fprintf(client, "{\n");
163 indent++;
164 print_client( "struct __frame_%s%s __f, * const __frame = &__f;\n", prefix_client, get_name(func) );
166 /* declare return value '_RetVal' */
167 if (!is_void(type_function_get_rettype(func->type)))
169 print_client("");
170 write_type_decl_left(client, type_function_get_rettype(func->type));
171 fprintf(client, " _RetVal;\n");
173 print_client("RPC_MESSAGE _RpcMessage;\n");
175 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
177 print_client( "__frame->_Handle = 0;\n" );
178 if (!implicit_handle && explicit_generic_handle_var)
179 print_client("__frame->%s = %s;\n",
180 explicit_generic_handle_var->name, explicit_generic_handle_var->name );
182 if (!is_void(type_function_get_rettype(func->type)) &&
183 decl_indirect(type_function_get_rettype(func->type)))
185 print_client("__frame->_p_%s = &%s;\n",
186 "_RetVal", "_RetVal");
188 fprintf(client, "\n");
190 print_client( "RpcExceptionInit( 0, __finally_%s%s );\n", prefix_client, get_name(func) );
192 if (has_full_pointer)
193 write_full_pointer_init(client, indent, func, FALSE);
195 /* check pointers */
196 check_pointers(func);
198 print_client("RpcTryFinally\n");
199 print_client("{\n");
200 indent++;
202 print_client("NdrClientInitializeNew(&_RpcMessage, &__frame->_StubMsg, &%s_StubDesc, %d);\n",
203 iface->name, method_count);
205 if (is_attr(func->attrs, ATTR_IDEMPOTENT) || is_attr(func->attrs, ATTR_BROADCAST))
207 print_client("_RpcMessage.RpcFlags = ( RPC_NCA_FLAGS_DEFAULT ");
208 if (is_attr(func->attrs, ATTR_IDEMPOTENT))
209 fprintf(client, "| RPC_NCA_FLAGS_IDEMPOTENT ");
210 if (is_attr(func->attrs, ATTR_BROADCAST))
211 fprintf(client, "| RPC_NCA_FLAGS_BROADCAST ");
212 fprintf(client, ");\n\n");
215 if (explicit_handle_var)
217 print_client("__frame->_Handle = %s;\n", explicit_handle_var->name);
218 fprintf(client, "\n");
220 else if (explicit_generic_handle_var)
222 print_client("__frame->_Handle = %s_bind(%s);\n",
223 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
224 explicit_generic_handle_var->name);
225 fprintf(client, "\n");
227 else if (context_handle_var)
229 /* if the context_handle attribute appears in the chain of types
230 * without pointers being followed, then the context handle must
231 * be direct, otherwise it is a pointer */
232 int is_ch_ptr = is_aliaschain_attr(context_handle_var->type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
233 print_client("if (%s%s != 0)\n", is_ch_ptr ? "*" : "", context_handle_var->name);
234 indent++;
235 print_client("__frame->_Handle = NDRCContextBinding(%s%s);\n",
236 is_ch_ptr ? "*" : "", context_handle_var->name);
237 indent--;
238 if (is_attr(context_handle_var->attrs, ATTR_IN) &&
239 !is_attr(context_handle_var->attrs, ATTR_OUT))
241 print_client("else\n");
242 indent++;
243 print_client("RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);\n");
244 indent--;
246 fprintf(client, "\n");
248 else if (implicit_handle)
250 print_client("__frame->_Handle = %s;\n", implicit_handle);
251 fprintf(client, "\n");
254 write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
256 print_client("NdrGetBuffer(&__frame->_StubMsg, __frame->_StubMsg.BufferLength, ");
257 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
258 fprintf(client, "__frame->_Handle);\n\n");
259 else
260 fprintf(client,"%s__MIDL_AutoBindHandle);\n\n", iface->name);
262 /* marshal arguments */
263 write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_MARSHAL);
265 /* send/receive message */
266 /* print_client("NdrNsSendReceive(\n"); */
267 /* print_client("(unsigned char *)__frame->_StubMsg.Buffer,\n"); */
268 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
269 print_client("NdrSendReceive(&__frame->_StubMsg, __frame->_StubMsg.Buffer);\n\n");
271 print_client("__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n");
272 print_client("__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
274 if (has_out_arg_or_return(func))
276 fprintf(client, "\n");
278 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
279 indent++;
280 print_client("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
281 *proc_offset);
282 indent--;
285 /* unmarshall arguments */
286 fprintf(client, "\n");
287 write_remoting_arguments(client, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
289 /* unmarshal return value */
290 if (!is_void(type_function_get_rettype(func->type)))
292 if (decl_indirect(type_function_get_rettype(func->type)))
293 print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
294 else if (is_ptr(type_function_get_rettype(func->type)) ||
295 is_array(type_function_get_rettype(func->type)))
296 print_client("%s = 0;\n", "_RetVal");
297 write_remoting_arguments(client, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
300 /* update proc_offset */
301 if (args)
303 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
304 *proc_offset += get_size_procformatstring_type(var->name, var->type, var->attrs);
306 if (!is_void(type_function_get_rettype(func->type)))
307 *proc_offset += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
308 else
309 *proc_offset += 2; /* FC_END and FC_PAD */
311 indent--;
312 print_client("}\n");
313 print_client("RpcFinally\n");
314 print_client("{\n");
315 indent++;
316 print_client( "__finally_%s%s( __frame );\n", prefix_client, get_name(func) );
317 indent--;
318 print_client("}\n");
319 print_client("RpcEndFinally\n");
322 /* emit return code */
323 if (!is_void(type_function_get_rettype(func->type)))
325 fprintf(client, "\n");
326 print_client("return _RetVal;\n");
329 indent--;
330 fprintf(client, "}\n");
331 fprintf(client, "\n");
333 method_count++;
338 static void write_stubdescdecl(type_t *iface)
340 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
341 fprintf(client, "\n");
345 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
347 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
349 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
350 print_client("{\n");
351 indent++;
352 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
353 print_client("MIDL_user_allocate,\n");
354 print_client("MIDL_user_free,\n");
355 print_client("{\n");
356 indent++;
357 if (implicit_handle)
358 print_client("&%s,\n", implicit_handle);
359 else
360 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
361 indent--;
362 print_client("},\n");
363 print_client("0,\n");
364 print_client("0,\n");
365 if (expr_eval_routines)
366 print_client("ExprEvalRoutines,\n");
367 else
368 print_client("0,\n");
369 print_client("0,\n");
370 print_client("__MIDL_TypeFormatString.Format,\n");
371 print_client("1, /* -error bounds_check flag */\n");
372 print_client("0x10001, /* Ndr library version */\n");
373 print_client("0,\n");
374 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
375 print_client("0,\n");
376 print_client("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
377 print_client("0, /* notify & notify_flag routine table */\n");
378 print_client("1, /* Flags */\n");
379 print_client("0, /* Reserved3 */\n");
380 print_client("0, /* Reserved4 */\n");
381 print_client("0 /* Reserved5 */\n");
382 indent--;
383 print_client("};\n");
384 fprintf(client, "\n");
388 static void write_clientinterfacedecl(type_t *iface)
390 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
391 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
392 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
394 if (endpoints) write_endpoints( client, iface->name, endpoints );
396 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
397 print_client("{\n");
398 indent++;
399 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
400 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",
401 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
402 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
403 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
404 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
405 print_client("0,\n");
406 if (endpoints)
408 print_client("%u,\n", list_count(endpoints));
409 print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
411 else
413 print_client("0,\n");
414 print_client("0,\n");
416 print_client("0,\n");
417 print_client("0,\n");
418 print_client("0,\n");
419 indent--;
420 print_client("};\n");
421 if (old_names)
422 print_client("RPC_IF_HANDLE %s_ClientIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
423 iface->name, iface->name);
424 else
425 print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
426 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
427 fprintf(client, "\n");
431 static void write_implicithandledecl(type_t *iface)
433 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
435 if (implicit_handle)
437 fprintf(client, "handle_t %s;\n", implicit_handle);
438 fprintf(client, "\n");
443 static void init_client(void)
445 if (client) return;
446 if (!(client = fopen(client_name, "w")))
447 error("Could not open %s for output\n", client_name);
449 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
450 print_client("#include <string.h>\n");
451 print_client("#ifdef _ALPHA_\n");
452 print_client("#include <stdarg.h>\n");
453 print_client("#endif\n");
454 fprintf(client, "\n");
455 print_client("#include \"%s\"\n", header_name);
456 print_client( "\n");
457 print_client( "#ifndef DECLSPEC_HIDDEN\n");
458 print_client( "#define DECLSPEC_HIDDEN\n");
459 print_client( "#endif\n");
460 print_client( "\n");
461 write_exceptions( client );
462 print_client( "\n");
466 static void write_client_ifaces(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
468 const statement_t *stmt;
469 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
471 if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
473 int has_func = 0;
474 const statement_t *stmt2;
475 type_t *iface = stmt->u.type;
476 if (!need_stub(iface))
477 return;
479 fprintf(client, "/*****************************************************************************\n");
480 fprintf(client, " * %s interface\n", iface->name);
481 fprintf(client, " */\n");
482 fprintf(client, "\n");
484 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
486 has_func = 1;
487 break;
490 if (has_func)
492 write_implicithandledecl(iface);
494 write_clientinterfacedecl(iface);
495 write_stubdescdecl(iface);
496 write_function_stubs(iface, proc_offset);
498 print_client("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
499 print_client("#error Invalid build platform for this stub.\n");
500 print_client("#endif\n");
502 fprintf(client, "\n");
503 write_stubdescriptor(iface, expr_eval_routines);
506 else if (stmt->type == STMT_LIBRARY)
507 write_client_ifaces(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
511 static void write_client_routines(const statement_list_t *stmts)
513 unsigned int proc_offset = 0;
514 int expr_eval_routines;
516 write_formatstringsdecl(client, indent, stmts, need_stub);
517 expr_eval_routines = write_expr_eval_routines(client, client_token);
518 if (expr_eval_routines)
519 write_expr_eval_routine_list(client, client_token);
520 write_user_quad_list(client);
522 write_client_ifaces(stmts, expr_eval_routines, &proc_offset);
524 fprintf(client, "\n");
526 write_procformatstring(client, stmts, need_stub);
527 write_typeformatstring(client, stmts, need_stub);
530 void write_client(const statement_list_t *stmts)
532 if (!do_client)
533 return;
534 if (do_everything && !need_stub_files(stmts))
535 return;
537 init_client();
538 if (!client)
539 return;
541 if (do_win32 && do_win64)
543 fprintf(client, "\n#ifndef _WIN64\n\n");
544 pointer_size = 4;
545 write_client_routines( stmts );
546 fprintf(client, "\n#else /* _WIN64 */\n\n");
547 pointer_size = 8;
548 write_client_routines( stmts );
549 fprintf(client, "\n#endif /* _WIN64 */\n");
551 else if (do_win32)
553 pointer_size = 4;
554 write_client_routines( stmts );
556 else if (do_win64)
558 pointer_size = 8;
559 write_client_routines( stmts );
562 fclose(client);