winemac: Send IME key input from ImeProcessKey.
[wine.git] / tools / widl / client.c
blob704be91cb78b7d91b0beee6eab5a035e053516f5
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"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
28 #include "widl.h"
29 #include "utils.h"
30 #include "parser.h"
31 #include "header.h"
33 #include "widltypes.h"
34 #include "typegen.h"
35 #include "expr.h"
37 static FILE* client;
38 static int indent = 0;
40 static void print_client( const char *format, ... ) __attribute__((format (printf, 1, 2)));
41 static void print_client( const char *format, ... )
43 va_list va;
44 va_start(va, format);
45 print(client, indent, format, va);
46 va_end(va);
49 static void write_client_func_decl( const type_t *iface, const var_t *func )
51 const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
52 const var_list_t *args = type_function_get_args(func->declspec.type);
53 const decl_spec_t *rettype = type_function_get_ret(func->declspec.type);
55 if (!callconv) callconv = "__cdecl";
56 write_type_decl_left(client, rettype);
57 fprintf(client, " %s ", callconv);
58 fprintf(client, "%s%s(\n", prefix_client, get_name(func));
59 indent++;
60 if (args) write_args(client, args, iface->name, 0, TRUE, NAME_DEFAULT);
61 else
62 print_client("void");
63 fprintf(client, ")\n");
64 indent--;
67 static void write_function_stub( const type_t *iface, const var_t *func,
68 int method_count, unsigned int proc_offset )
70 unsigned char explicit_fc, implicit_fc;
71 int has_full_pointer = is_full_pointer_function(func);
72 var_t *retval = type_function_get_retval(func->declspec.type);
73 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
74 int has_ret = !is_void(retval->declspec.type);
76 if (is_interpreted_func( iface, func ))
78 write_client_func_decl( iface, func );
79 write_client_call_routine( client, iface, func, iface->name, proc_offset );
80 return;
83 print_client( "struct __frame_%s%s\n{\n", prefix_client, get_name(func) );
84 indent++;
85 print_client( "__DECL_EXCEPTION_FRAME\n" );
86 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
87 if (handle_var)
89 if (explicit_fc == FC_BIND_GENERIC)
90 print_client("%s %s;\n",
91 get_explicit_generic_handle_type(handle_var)->name, handle_var->name );
92 print_client("RPC_BINDING_HANDLE _Handle;\n");
95 if (has_ret && decl_indirect(retval->declspec.type))
97 print_client("void *_p_%s;\n", retval->name);
99 indent--;
100 print_client( "};\n\n" );
102 print_client( "static void __finally_%s%s(", prefix_client, get_name(func) );
103 print_client( " struct __frame_%s%s *__frame )\n{\n", prefix_client, get_name(func) );
104 indent++;
106 if (has_full_pointer)
107 write_full_pointer_free(client, indent, func);
109 print_client("NdrFreeBuffer(&__frame->_StubMsg);\n");
111 if (explicit_fc == FC_BIND_GENERIC)
113 fprintf(client, "\n");
114 print_client("if (__frame->_Handle)\n");
115 indent++;
116 print_client("%s_unbind(__frame->%s, __frame->_Handle);\n",
117 get_explicit_generic_handle_type(handle_var)->name, handle_var->name);
118 indent--;
120 indent--;
121 print_client( "}\n\n" );
123 write_client_func_decl( iface, func );
125 /* write the functions body */
126 fprintf(client, "{\n");
127 indent++;
128 print_client( "struct __frame_%s%s __f, * const __frame = &__f;\n", prefix_client, get_name(func) );
130 /* declare return value */
131 if (has_ret)
133 print_client("%s", "");
134 write_type_decl(client, &retval->declspec, retval->name);
135 fprintf(client, ";\n");
137 print_client("RPC_MESSAGE _RpcMessage;\n");
139 if (handle_var)
141 print_client( "__frame->_Handle = 0;\n" );
142 if (explicit_fc == FC_BIND_GENERIC)
143 print_client("__frame->%s = %s;\n", handle_var->name, handle_var->name );
145 if (has_ret && decl_indirect(retval->declspec.type))
147 print_client("__frame->_p_%s = &%s;\n", retval->name, retval->name);
149 fprintf(client, "\n");
151 print_client( "RpcExceptionInit( 0, __finally_%s%s );\n", prefix_client, get_name(func) );
153 if (has_full_pointer)
154 write_full_pointer_init(client, indent, func, FALSE);
156 /* check pointers */
157 write_pointer_checks( client, indent, func );
159 print_client("RpcTryFinally\n");
160 print_client("{\n");
161 indent++;
163 print_client("NdrClientInitializeNew(&_RpcMessage, &__frame->_StubMsg, &%s_StubDesc, %d);\n",
164 iface->name, method_count);
166 if (is_attr(func->attrs, ATTR_IDEMPOTENT) || is_attr(func->attrs, ATTR_BROADCAST))
168 print_client("_RpcMessage.RpcFlags = ( RPC_NCA_FLAGS_DEFAULT ");
169 if (is_attr(func->attrs, ATTR_IDEMPOTENT))
170 fprintf(client, "| RPC_NCA_FLAGS_IDEMPOTENT ");
171 if (is_attr(func->attrs, ATTR_BROADCAST))
172 fprintf(client, "| RPC_NCA_FLAGS_BROADCAST ");
173 fprintf(client, ");\n\n");
176 switch (explicit_fc)
178 case FC_BIND_PRIMITIVE:
179 print_client("__frame->_Handle = %s;\n", handle_var->name);
180 fprintf(client, "\n");
181 break;
182 case FC_BIND_GENERIC:
183 print_client("__frame->_Handle = %s_bind(%s);\n",
184 get_explicit_generic_handle_type(handle_var)->name, handle_var->name);
185 fprintf(client, "\n");
186 break;
187 case FC_BIND_CONTEXT:
189 /* if the context_handle attribute appears in the chain of types
190 * without pointers being followed, then the context handle must
191 * be direct, otherwise it is a pointer */
192 int is_ch_ptr = !is_aliaschain_attr(handle_var->declspec.type, ATTR_CONTEXTHANDLE);
193 print_client("if (%s%s != 0)\n", is_ch_ptr ? "*" : "", handle_var->name);
194 indent++;
195 print_client("__frame->_Handle = NDRCContextBinding(%s%s);\n",
196 is_ch_ptr ? "*" : "", handle_var->name);
197 indent--;
198 if (is_attr(handle_var->attrs, ATTR_IN) && !is_attr(handle_var->attrs, ATTR_OUT))
200 print_client("else\n");
201 indent++;
202 print_client("RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);\n");
203 indent--;
205 fprintf(client, "\n");
206 break;
208 case 0: /* implicit handle */
209 if (handle_var)
211 print_client("__frame->_Handle = %s;\n", handle_var->name);
212 fprintf(client, "\n");
214 break;
217 write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
219 print_client("NdrGetBuffer(&__frame->_StubMsg, __frame->_StubMsg.BufferLength, ");
220 if (handle_var)
221 fprintf(client, "__frame->_Handle);\n\n");
222 else
223 fprintf(client,"%s__MIDL_AutoBindHandle);\n\n", iface->name);
225 /* marshal arguments */
226 write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_MARSHAL);
228 /* send/receive message */
229 /* print_client("NdrNsSendReceive(\n"); */
230 /* print_client("(unsigned char *)__frame->_StubMsg.Buffer,\n"); */
231 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
232 print_client("NdrSendReceive(&__frame->_StubMsg, __frame->_StubMsg.Buffer);\n\n");
234 print_client("__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n");
235 print_client("__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
237 if (has_out_arg_or_return(func))
239 fprintf(client, "\n");
241 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
242 indent++;
243 print_client("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
244 proc_offset);
245 indent--;
248 /* unmarshall arguments */
249 fprintf(client, "\n");
250 write_remoting_arguments(client, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
252 /* unmarshal return value */
253 if (has_ret)
255 if (decl_indirect(retval->declspec.type))
256 print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", retval->name, retval->name);
257 else if (is_ptr(retval->declspec.type) || is_array(retval->declspec.type))
258 print_client("%s = 0;\n", retval->name);
259 write_remoting_arguments(client, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
262 indent--;
263 print_client("}\n");
264 print_client("RpcFinally\n");
265 print_client("{\n");
266 indent++;
267 print_client( "__finally_%s%s( __frame );\n", prefix_client, get_name(func) );
268 indent--;
269 print_client("}\n");
270 print_client("RpcEndFinally\n");
273 /* emit return code */
274 if (has_ret)
276 fprintf(client, "\n");
277 print_client("return %s;\n", retval->name);
280 indent--;
281 fprintf(client, "}\n");
282 fprintf(client, "\n");
285 static void write_serialize_function(FILE *file, const type_t *type, const type_t *iface,
286 const char *func_name, const char *ret_type)
288 enum stub_mode mode = get_stub_mode();
289 static int emitted_pickling_info;
291 if (iface && !type->typestring_offset)
293 /* FIXME: Those are mostly basic types. They should be implemented
294 * using NdrMesSimpleType* functions */
295 if (ret_type) warning("Serialization of type %s is not supported\n", type->name);
296 return;
299 if (!emitted_pickling_info && iface && mode != MODE_Os)
301 fprintf(file, "static const MIDL_TYPE_PICKLING_INFO __MIDL_TypePicklingInfo =\n");
302 fprintf(file, "{\n");
303 fprintf(file, " 0x33205054,\n");
304 fprintf(file, " 0x3,\n");
305 fprintf(file, " 0,\n");
306 fprintf(file, " 0,\n");
307 fprintf(file, " 0\n");
308 fprintf(file, "};\n");
309 fprintf(file, "\n");
310 emitted_pickling_info = 1;
313 /* FIXME: Assuming explicit handle */
315 fprintf(file, "%s __cdecl %s_%s(handle_t IDL_handle, %s *IDL_type)%s\n",
316 ret_type ? ret_type : "void", type->name, func_name, type->name, iface ? "" : ";");
317 if (!iface) return; /* declaration only */
319 fprintf(file, "{\n");
320 fprintf(file, " %sNdrMesType%s%s(\n", ret_type ? "return " : "", func_name,
321 mode != MODE_Os ? "2" : "");
322 fprintf(file, " IDL_handle,\n");
323 if (mode != MODE_Os)
324 fprintf(file, " (MIDL_TYPE_PICKLING_INFO*)&__MIDL_TypePicklingInfo,\n");
325 fprintf(file, " &%s_StubDesc,\n", iface->name);
326 fprintf(file, " (PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%u],\n",
327 type->typestring_offset);
328 fprintf(file, " IDL_type);\n");
329 fprintf(file, "}\n");
330 fprintf(file, "\n");
333 void write_serialize_functions(FILE *file, const type_t *type, const type_t *iface)
335 if (is_attr(type->attrs, ATTR_ENCODE))
337 write_serialize_function(file, type, iface, "AlignSize", "SIZE_T");
338 write_serialize_function(file, type, iface, "Encode", NULL);
340 if (is_attr(type->attrs, ATTR_DECODE))
342 write_serialize_function(file, type, iface, "Decode", NULL);
343 write_serialize_function(file, type, iface, "Free", NULL);
347 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
349 const statement_t *stmt;
350 const var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
351 int method_count = 0;
353 if (!implicit_handle)
354 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n\n", iface->name);
356 LIST_FOR_EACH_ENTRY( stmt, type_iface_get_stmts(iface), const statement_t, entry )
358 switch (stmt->type)
360 case STMT_DECLARATION:
362 const var_t *func = stmt->u.var;
363 if (stmt->u.var->declspec.stgclass != STG_NONE
364 || type_get_type_detect_alias(stmt->u.var->declspec.type) != TYPE_FUNCTION)
365 continue;
366 write_function_stub( iface, func, method_count++, *proc_offset );
367 *proc_offset += get_size_procformatstring_func( iface, func );
368 break;
370 case STMT_TYPEDEF:
372 typeref_t *ref;
373 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry)
374 write_serialize_functions(client, ref->type, iface);
375 break;
377 default:
378 break;
384 static void write_stubdescdecl(type_t *iface)
386 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
387 fprintf(client, "\n");
391 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
393 const var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
395 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
396 print_client("{\n");
397 indent++;
398 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
399 print_client("MIDL_user_allocate,\n");
400 print_client("MIDL_user_free,\n");
401 print_client("{\n");
402 indent++;
403 if (implicit_handle)
404 print_client("&%s,\n", implicit_handle->name);
405 else
406 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
407 indent--;
408 print_client("},\n");
409 print_client("0,\n");
410 if (!list_empty( &generic_handle_list ))
411 print_client("BindingRoutines,\n");
412 else
413 print_client("0,\n");
414 if (expr_eval_routines)
415 print_client("ExprEvalRoutines,\n");
416 else
417 print_client("0,\n");
418 print_client("0,\n");
419 print_client("__MIDL_TypeFormatString.Format,\n");
420 print_client("1, /* -error bounds_check flag */\n");
421 print_client("0x%x, /* Ndr library version */\n", get_stub_mode() == MODE_Oif ? 0x50002 : 0x10001);
422 print_client("0,\n");
423 print_client("0x50200ca, /* MIDL Version 5.2.202 */\n");
424 print_client("0,\n");
425 print_client("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
426 print_client("0, /* notify & notify_flag routine table */\n");
427 print_client("1, /* Flags */\n");
428 print_client("0, /* Reserved3 */\n");
429 print_client("0, /* Reserved4 */\n");
430 print_client("0 /* Reserved5 */\n");
431 indent--;
432 print_client("};\n");
433 fprintf(client, "\n");
437 static void write_clientinterfacedecl(type_t *iface)
439 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
440 const struct uuid *uuid = get_attrp(iface->attrs, ATTR_UUID);
441 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
443 if (endpoints) write_endpoints( client, iface->name, endpoints );
445 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
446 print_client("{\n");
447 indent++;
448 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
449 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",
450 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
451 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
452 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
453 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
454 print_client("0,\n");
455 if (endpoints)
457 print_client("%u,\n", list_count(endpoints));
458 print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
460 else
462 print_client("0,\n");
463 print_client("0,\n");
465 print_client("0,\n");
466 print_client("0,\n");
467 print_client("0,\n");
468 indent--;
469 print_client("};\n");
470 if (old_names)
471 print_client("RPC_IF_HANDLE %s_ClientIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
472 iface->name, iface->name);
473 else
474 print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
475 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
476 fprintf(client, "\n");
480 static void write_implicithandledecl(type_t *iface)
482 const var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
484 if (implicit_handle)
486 write_type_decl(client, &implicit_handle->declspec, implicit_handle->name);
487 fprintf(client, ";\n\n");
492 static void init_client(void)
494 if (client) return;
495 if (!(client = fopen(client_name, "w")))
496 error("Could not open %s for output\n", client_name);
498 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
499 print_client("#include <string.h>\n");
500 print_client( "\n");
501 print_client("#include \"%s\"\n", header_name);
502 print_client( "\n");
503 print_client( "#ifndef DECLSPEC_HIDDEN\n");
504 print_client( "#define DECLSPEC_HIDDEN\n");
505 print_client( "#endif\n");
506 print_client( "\n");
510 static void write_client_ifaces(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
512 const statement_t *stmt;
513 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
515 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
517 int needs_stub = 0;
518 const statement_t *stmt2;
519 type_t *iface = stmt->u.type;
520 if (!need_stub(iface))
521 return;
523 fprintf(client, "/*****************************************************************************\n");
524 fprintf(client, " * %s interface\n", iface->name);
525 fprintf(client, " */\n");
526 fprintf(client, "\n");
528 LIST_FOR_EACH_ENTRY(stmt2, type_iface_get_stmts(iface), const statement_t, entry)
530 if (stmt2->type == STMT_DECLARATION && stmt2->u.var->declspec.stgclass == STG_NONE &&
531 type_get_type_detect_alias(stmt2->u.var->declspec.type) == TYPE_FUNCTION)
533 needs_stub = 1;
534 break;
536 if (stmt2->type == STMT_TYPEDEF)
538 typeref_t *ref;
539 if (stmt2->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt2->u.type_list, typeref_t, entry)
541 if (is_attr(ref->type->attrs, ATTR_ENCODE)
542 || is_attr(ref->type->attrs, ATTR_DECODE))
544 needs_stub = 1;
545 break;
548 if (needs_stub)
549 break;
552 if (needs_stub)
554 write_implicithandledecl(iface);
556 write_clientinterfacedecl(iface);
557 write_stubdescdecl(iface);
558 write_function_stubs(iface, proc_offset);
560 print_client("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
561 print_client("#error Invalid build platform for this stub.\n");
562 print_client("#endif\n");
564 fprintf(client, "\n");
565 write_stubdescriptor(iface, expr_eval_routines);
571 static void write_generic_handle_routine_list(void)
573 generic_handle_t *gh;
575 if (list_empty( &generic_handle_list )) return;
576 print_client( "static const GENERIC_BINDING_ROUTINE_PAIR BindingRoutines[] =\n" );
577 print_client( "{\n" );
578 indent++;
579 LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
581 print_client( "{ (GENERIC_BINDING_ROUTINE)%s_bind, (GENERIC_UNBIND_ROUTINE)%s_unbind },\n",
582 gh->name, gh->name );
584 indent--;
585 print_client( "};\n\n" );
588 static void write_client_routines(const statement_list_t *stmts)
590 unsigned int proc_offset = 0;
591 int expr_eval_routines;
593 if (need_inline_stubs_file( stmts ))
595 write_exceptions( client );
596 print_client( "\n");
599 write_formatstringsdecl(client, indent, stmts, need_stub);
600 expr_eval_routines = write_expr_eval_routines(client, client_token);
601 if (expr_eval_routines)
602 write_expr_eval_routine_list(client, client_token);
603 write_generic_handle_routine_list();
604 write_user_quad_list(client);
606 write_client_ifaces(stmts, expr_eval_routines, &proc_offset);
608 fprintf(client, "\n");
610 write_procformatstring(client, stmts, need_stub);
611 write_typeformatstring(client, stmts, need_stub);
614 void write_client(const statement_list_t *stmts)
616 if (!do_client)
617 return;
618 if (do_everything && !need_stub_files(stmts))
619 return;
621 init_client();
622 if (!client)
623 return;
625 write_client_routines( stmts );
626 fclose(client);