push 8724397e7ede0f147b6e05994a72142d44d4fecc
[wine/hacks.git] / tools / widl / client.c
blobb683a14b315bd092af9349eae7f9d9c5c9a43c3c
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 const var_t *var;
80 int method_count = 0;
82 if (!implicit_handle)
83 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n\n", iface->name);
85 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
87 const var_t *func = stmt->u.var;
88 const var_t* explicit_handle_var;
89 const var_t* explicit_generic_handle_var = NULL;
90 const var_t* context_handle_var = NULL;
91 int has_full_pointer = is_full_pointer_function(func);
92 const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
93 const var_list_t *args = type_get_function_args(func->type);
95 /* check for a defined binding handle */
96 explicit_handle_var = get_explicit_handle_var(func);
97 if (!explicit_handle_var)
99 explicit_generic_handle_var = get_explicit_generic_handle_var(func);
100 if (!explicit_generic_handle_var)
101 context_handle_var = get_context_handle_var(func);
104 print_client( "struct __frame_%s%s\n{\n", prefix_client, get_name(func) );
105 indent++;
106 print_client( "__DECL_EXCEPTION_FRAME\n" );
107 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
108 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
110 if (!implicit_handle && explicit_generic_handle_var)
111 print_client("%s %s;\n",
112 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
113 explicit_generic_handle_var->name );
114 print_client("RPC_BINDING_HANDLE _Handle;\n");
117 if (!is_void(type_function_get_rettype(func->type)) &&
118 decl_indirect(type_function_get_rettype(func->type)))
120 print_client("void *_p_%s;\n", "_RetVal" );
122 indent--;
123 print_client( "};\n\n" );
125 print_client( "static void __finally_%s%s(", prefix_client, get_name(func) );
126 print_client( " struct __frame_%s%s *__frame )\n{\n", prefix_client, get_name(func) );
127 indent++;
129 /* FIXME: emit client finally code */
131 if (has_full_pointer)
132 write_full_pointer_free(client, indent, func);
134 print_client("NdrFreeBuffer(&__frame->_StubMsg);\n");
136 if (!implicit_handle && explicit_generic_handle_var)
138 fprintf(client, "\n");
139 print_client("if (__frame->_Handle)\n");
140 indent++;
141 print_client("%s_unbind(__frame->%s, __frame->_Handle);\n",
142 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
143 explicit_generic_handle_var->name);
144 indent--;
146 indent--;
147 print_client( "}\n\n" );
149 write_type_decl_left(client, type_function_get_rettype(func->type));
150 if (needs_space_after(type_function_get_rettype(func->type)))
151 fprintf(client, " ");
152 if (callconv) fprintf(client, "%s ", callconv);
153 fprintf(client, "%s%s(\n", prefix_client, get_name(func));
154 indent++;
155 if (args)
156 write_args(client, args, iface->name, 0, TRUE);
157 else
158 print_client("void");
159 fprintf(client, ")\n");
160 indent--;
162 /* write the functions body */
163 fprintf(client, "{\n");
164 indent++;
165 print_client( "struct __frame_%s%s __f, * const __frame = &__f;\n", prefix_client, get_name(func) );
167 /* declare return value '_RetVal' */
168 if (!is_void(type_function_get_rettype(func->type)))
170 print_client("%s", "");
171 write_type_decl_left(client, type_function_get_rettype(func->type));
172 fprintf(client, " _RetVal;\n");
174 print_client("RPC_MESSAGE _RpcMessage;\n");
176 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
178 print_client( "__frame->_Handle = 0;\n" );
179 if (!implicit_handle && explicit_generic_handle_var)
180 print_client("__frame->%s = %s;\n",
181 explicit_generic_handle_var->name, explicit_generic_handle_var->name );
183 if (!is_void(type_function_get_rettype(func->type)) &&
184 decl_indirect(type_function_get_rettype(func->type)))
186 print_client("__frame->_p_%s = &%s;\n",
187 "_RetVal", "_RetVal");
189 fprintf(client, "\n");
191 print_client( "RpcExceptionInit( 0, __finally_%s%s );\n", prefix_client, get_name(func) );
193 if (has_full_pointer)
194 write_full_pointer_init(client, indent, func, FALSE);
196 /* check pointers */
197 check_pointers(func);
199 print_client("RpcTryFinally\n");
200 print_client("{\n");
201 indent++;
203 print_client("NdrClientInitializeNew(&_RpcMessage, &__frame->_StubMsg, &%s_StubDesc, %d);\n",
204 iface->name, method_count);
206 if (is_attr(func->attrs, ATTR_IDEMPOTENT) || is_attr(func->attrs, ATTR_BROADCAST))
208 print_client("_RpcMessage.RpcFlags = ( RPC_NCA_FLAGS_DEFAULT ");
209 if (is_attr(func->attrs, ATTR_IDEMPOTENT))
210 fprintf(client, "| RPC_NCA_FLAGS_IDEMPOTENT ");
211 if (is_attr(func->attrs, ATTR_BROADCAST))
212 fprintf(client, "| RPC_NCA_FLAGS_BROADCAST ");
213 fprintf(client, ");\n\n");
216 if (explicit_handle_var)
218 print_client("__frame->_Handle = %s;\n", explicit_handle_var->name);
219 fprintf(client, "\n");
221 else if (explicit_generic_handle_var)
223 print_client("__frame->_Handle = %s_bind(%s);\n",
224 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
225 explicit_generic_handle_var->name);
226 fprintf(client, "\n");
228 else if (context_handle_var)
230 /* if the context_handle attribute appears in the chain of types
231 * without pointers being followed, then the context handle must
232 * be direct, otherwise it is a pointer */
233 int is_ch_ptr = is_aliaschain_attr(context_handle_var->type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
234 print_client("if (%s%s != 0)\n", is_ch_ptr ? "*" : "", context_handle_var->name);
235 indent++;
236 print_client("__frame->_Handle = NDRCContextBinding(%s%s);\n",
237 is_ch_ptr ? "*" : "", context_handle_var->name);
238 indent--;
239 if (is_attr(context_handle_var->attrs, ATTR_IN) &&
240 !is_attr(context_handle_var->attrs, ATTR_OUT))
242 print_client("else\n");
243 indent++;
244 print_client("RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);\n");
245 indent--;
247 fprintf(client, "\n");
249 else if (implicit_handle)
251 print_client("__frame->_Handle = %s;\n", implicit_handle);
252 fprintf(client, "\n");
255 write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
257 print_client("NdrGetBuffer(&__frame->_StubMsg, __frame->_StubMsg.BufferLength, ");
258 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
259 fprintf(client, "__frame->_Handle);\n\n");
260 else
261 fprintf(client,"%s__MIDL_AutoBindHandle);\n\n", iface->name);
263 /* marshal arguments */
264 write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_MARSHAL);
266 /* send/receive message */
267 /* print_client("NdrNsSendReceive(\n"); */
268 /* print_client("(unsigned char *)__frame->_StubMsg.Buffer,\n"); */
269 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
270 print_client("NdrSendReceive(&__frame->_StubMsg, __frame->_StubMsg.Buffer);\n\n");
272 print_client("__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n");
273 print_client("__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
275 if (has_out_arg_or_return(func))
277 fprintf(client, "\n");
279 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
280 indent++;
281 print_client("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
282 *proc_offset);
283 indent--;
286 /* unmarshall arguments */
287 fprintf(client, "\n");
288 write_remoting_arguments(client, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
290 /* unmarshal return value */
291 if (!is_void(type_function_get_rettype(func->type)))
293 if (decl_indirect(type_function_get_rettype(func->type)))
294 print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
295 else if (is_ptr(type_function_get_rettype(func->type)) ||
296 is_array(type_function_get_rettype(func->type)))
297 print_client("%s = 0;\n", "_RetVal");
298 write_remoting_arguments(client, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
301 /* update proc_offset */
302 if (args)
304 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
305 *proc_offset += get_size_procformatstring_type(var->name, var->type, var->attrs);
307 if (!is_void(type_function_get_rettype(func->type)))
308 *proc_offset += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
309 else
310 *proc_offset += 2; /* FC_END and FC_PAD */
312 indent--;
313 print_client("}\n");
314 print_client("RpcFinally\n");
315 print_client("{\n");
316 indent++;
317 print_client( "__finally_%s%s( __frame );\n", prefix_client, get_name(func) );
318 indent--;
319 print_client("}\n");
320 print_client("RpcEndFinally\n");
323 /* emit return code */
324 if (!is_void(type_function_get_rettype(func->type)))
326 fprintf(client, "\n");
327 print_client("return _RetVal;\n");
330 indent--;
331 fprintf(client, "}\n");
332 fprintf(client, "\n");
334 method_count++;
339 static void write_stubdescdecl(type_t *iface)
341 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
342 fprintf(client, "\n");
346 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
348 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
350 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
351 print_client("{\n");
352 indent++;
353 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
354 print_client("MIDL_user_allocate,\n");
355 print_client("MIDL_user_free,\n");
356 print_client("{\n");
357 indent++;
358 if (implicit_handle)
359 print_client("&%s,\n", implicit_handle);
360 else
361 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
362 indent--;
363 print_client("},\n");
364 print_client("0,\n");
365 print_client("0,\n");
366 if (expr_eval_routines)
367 print_client("ExprEvalRoutines,\n");
368 else
369 print_client("0,\n");
370 print_client("0,\n");
371 print_client("__MIDL_TypeFormatString.Format,\n");
372 print_client("1, /* -error bounds_check flag */\n");
373 print_client("0x10001, /* Ndr library version */\n");
374 print_client("0,\n");
375 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
376 print_client("0,\n");
377 print_client("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
378 print_client("0, /* notify & notify_flag routine table */\n");
379 print_client("1, /* Flags */\n");
380 print_client("0, /* Reserved3 */\n");
381 print_client("0, /* Reserved4 */\n");
382 print_client("0 /* Reserved5 */\n");
383 indent--;
384 print_client("};\n");
385 fprintf(client, "\n");
389 static void write_clientinterfacedecl(type_t *iface)
391 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
392 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
393 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
395 if (endpoints) write_endpoints( client, iface->name, endpoints );
397 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
398 print_client("{\n");
399 indent++;
400 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
401 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",
402 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
403 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
404 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
405 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
406 print_client("0,\n");
407 if (endpoints)
409 print_client("%u,\n", list_count(endpoints));
410 print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
412 else
414 print_client("0,\n");
415 print_client("0,\n");
417 print_client("0,\n");
418 print_client("0,\n");
419 print_client("0,\n");
420 indent--;
421 print_client("};\n");
422 if (old_names)
423 print_client("RPC_IF_HANDLE %s_ClientIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
424 iface->name, iface->name);
425 else
426 print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
427 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
428 fprintf(client, "\n");
432 static void write_implicithandledecl(type_t *iface)
434 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
436 if (implicit_handle)
438 fprintf(client, "handle_t %s;\n", implicit_handle);
439 fprintf(client, "\n");
444 static void init_client(void)
446 if (client) return;
447 if (!(client = fopen(client_name, "w")))
448 error("Could not open %s for output\n", client_name);
450 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
451 print_client("#include <string.h>\n");
452 print_client("#ifdef _ALPHA_\n");
453 print_client("#include <stdarg.h>\n");
454 print_client("#endif\n");
455 fprintf(client, "\n");
456 print_client("#include \"%s\"\n", header_name);
457 print_client( "\n");
458 print_client( "#ifndef DECLSPEC_HIDDEN\n");
459 print_client( "#define DECLSPEC_HIDDEN\n");
460 print_client( "#endif\n");
461 print_client( "\n");
462 write_exceptions( client );
463 print_client( "\n");
467 static void write_client_ifaces(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
469 const statement_t *stmt;
470 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
472 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
474 int has_func = 0;
475 const statement_t *stmt2;
476 type_t *iface = stmt->u.type;
477 if (!need_stub(iface))
478 return;
480 fprintf(client, "/*****************************************************************************\n");
481 fprintf(client, " * %s interface\n", iface->name);
482 fprintf(client, " */\n");
483 fprintf(client, "\n");
485 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
487 has_func = 1;
488 break;
491 if (has_func)
493 write_implicithandledecl(iface);
495 write_clientinterfacedecl(iface);
496 write_stubdescdecl(iface);
497 write_function_stubs(iface, proc_offset);
499 print_client("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
500 print_client("#error Invalid build platform for this stub.\n");
501 print_client("#endif\n");
503 fprintf(client, "\n");
504 write_stubdescriptor(iface, expr_eval_routines);
507 else if (stmt->type == STMT_LIBRARY)
508 write_client_ifaces(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
512 static void write_client_routines(const statement_list_t *stmts)
514 unsigned int proc_offset = 0;
515 int expr_eval_routines;
517 write_formatstringsdecl(client, indent, stmts, need_stub);
518 expr_eval_routines = write_expr_eval_routines(client, client_token);
519 if (expr_eval_routines)
520 write_expr_eval_routine_list(client, client_token);
521 write_user_quad_list(client);
523 write_client_ifaces(stmts, expr_eval_routines, &proc_offset);
525 fprintf(client, "\n");
527 write_procformatstring(client, stmts, need_stub);
528 write_typeformatstring(client, stmts, need_stub);
531 void write_client(const statement_list_t *stmts)
533 if (!do_client)
534 return;
535 if (do_everything && !need_stub_files(stmts))
536 return;
538 init_client();
539 if (!client)
540 return;
542 if (do_win32 && do_win64)
544 fprintf(client, "\n#ifndef _WIN64\n\n");
545 pointer_size = 4;
546 write_client_routines( stmts );
547 fprintf(client, "\n#else /* _WIN64 */\n\n");
548 pointer_size = 8;
549 write_client_routines( stmts );
550 fprintf(client, "\n#endif /* _WIN64 */\n");
552 else if (do_win32)
554 pointer_size = 4;
555 write_client_routines( stmts );
557 else if (do_win64)
559 pointer_size = 8;
560 write_client_routines( stmts );
563 fclose(client);