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
22 #include "wine/port.h"
37 #include "widltypes.h"
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, ... )
49 print(client, indent, format, va);
54 static void check_pointers(const var_t *func)
58 if (!type_get_function_args(func->type))
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);
68 print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
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);
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) );
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" );
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) );
129 if (has_full_pointer)
130 write_full_pointer_free(client, indent, func);
132 print_client("NdrFreeBuffer(&__frame->_StubMsg);\n");
134 if (!implicit_handle && explicit_generic_handle_var)
136 fprintf(client, "\n");
137 print_client("if (__frame->_Handle)\n");
139 print_client("%s_unbind(__frame->%s, __frame->_Handle);\n",
140 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
141 explicit_generic_handle_var->name);
145 print_client( "}\n\n" );
147 write_type_decl_left(client, type_function_get_rettype(func->type));
148 if (needs_space_after(type_function_get_rettype(func->type)))
149 fprintf(client, " ");
150 if (callconv) fprintf(client, "%s ", callconv);
151 fprintf(client, "%s%s(\n", prefix_client, get_name(func));
154 write_args(client, args, iface->name, 0, TRUE);
156 print_client("void");
157 fprintf(client, ")\n");
160 /* write the functions body */
161 fprintf(client, "{\n");
163 print_client( "struct __frame_%s%s __f, * const __frame = &__f;\n", prefix_client, get_name(func) );
165 /* declare return value '_RetVal' */
166 if (!is_void(type_function_get_rettype(func->type)))
168 print_client("%s", "");
169 write_type_decl_left(client, type_function_get_rettype(func->type));
170 fprintf(client, " _RetVal;\n");
172 print_client("RPC_MESSAGE _RpcMessage;\n");
174 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
176 print_client( "__frame->_Handle = 0;\n" );
177 if (!implicit_handle && explicit_generic_handle_var)
178 print_client("__frame->%s = %s;\n",
179 explicit_generic_handle_var->name, explicit_generic_handle_var->name );
181 if (!is_void(type_function_get_rettype(func->type)) &&
182 decl_indirect(type_function_get_rettype(func->type)))
184 print_client("__frame->_p_%s = &%s;\n",
185 "_RetVal", "_RetVal");
187 fprintf(client, "\n");
189 print_client( "RpcExceptionInit( 0, __finally_%s%s );\n", prefix_client, get_name(func) );
191 if (has_full_pointer)
192 write_full_pointer_init(client, indent, func, FALSE);
195 check_pointers(func);
197 print_client("RpcTryFinally\n");
201 print_client("NdrClientInitializeNew(&_RpcMessage, &__frame->_StubMsg, &%s_StubDesc, %d);\n",
202 iface->name, method_count);
204 if (is_attr(func->attrs, ATTR_IDEMPOTENT) || is_attr(func->attrs, ATTR_BROADCAST))
206 print_client("_RpcMessage.RpcFlags = ( RPC_NCA_FLAGS_DEFAULT ");
207 if (is_attr(func->attrs, ATTR_IDEMPOTENT))
208 fprintf(client, "| RPC_NCA_FLAGS_IDEMPOTENT ");
209 if (is_attr(func->attrs, ATTR_BROADCAST))
210 fprintf(client, "| RPC_NCA_FLAGS_BROADCAST ");
211 fprintf(client, ");\n\n");
214 if (explicit_handle_var)
216 print_client("__frame->_Handle = %s;\n", explicit_handle_var->name);
217 fprintf(client, "\n");
219 else if (explicit_generic_handle_var)
221 print_client("__frame->_Handle = %s_bind(%s);\n",
222 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
223 explicit_generic_handle_var->name);
224 fprintf(client, "\n");
226 else if (context_handle_var)
228 /* if the context_handle attribute appears in the chain of types
229 * without pointers being followed, then the context handle must
230 * be direct, otherwise it is a pointer */
231 int is_ch_ptr = is_aliaschain_attr(context_handle_var->type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
232 print_client("if (%s%s != 0)\n", is_ch_ptr ? "*" : "", context_handle_var->name);
234 print_client("__frame->_Handle = NDRCContextBinding(%s%s);\n",
235 is_ch_ptr ? "*" : "", context_handle_var->name);
237 if (is_attr(context_handle_var->attrs, ATTR_IN) &&
238 !is_attr(context_handle_var->attrs, ATTR_OUT))
240 print_client("else\n");
242 print_client("RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);\n");
245 fprintf(client, "\n");
247 else if (implicit_handle)
249 print_client("__frame->_Handle = %s;\n", implicit_handle);
250 fprintf(client, "\n");
253 write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
255 print_client("NdrGetBuffer(&__frame->_StubMsg, __frame->_StubMsg.BufferLength, ");
256 if (implicit_handle || explicit_handle_var || explicit_generic_handle_var || context_handle_var)
257 fprintf(client, "__frame->_Handle);\n\n");
259 fprintf(client,"%s__MIDL_AutoBindHandle);\n\n", iface->name);
261 /* marshal arguments */
262 write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_MARSHAL);
264 /* send/receive message */
265 /* print_client("NdrNsSendReceive(\n"); */
266 /* print_client("(unsigned char *)__frame->_StubMsg.Buffer,\n"); */
267 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
268 print_client("NdrSendReceive(&__frame->_StubMsg, __frame->_StubMsg.Buffer);\n\n");
270 print_client("__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n");
271 print_client("__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
273 if (has_out_arg_or_return(func))
275 fprintf(client, "\n");
277 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
279 print_client("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
284 /* unmarshall arguments */
285 fprintf(client, "\n");
286 write_remoting_arguments(client, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
288 /* unmarshal return value */
289 if (!is_void(type_function_get_rettype(func->type)))
291 if (decl_indirect(type_function_get_rettype(func->type)))
292 print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
293 else if (is_ptr(type_function_get_rettype(func->type)) ||
294 is_array(type_function_get_rettype(func->type)))
295 print_client("%s = 0;\n", "_RetVal");
296 write_remoting_arguments(client, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
299 /* update proc_offset */
302 LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
303 *proc_offset += get_size_procformatstring_type(var->name, var->type, var->attrs);
305 if (!is_void(type_function_get_rettype(func->type)))
306 *proc_offset += get_size_procformatstring_type("return value", type_function_get_rettype(func->type), NULL);
308 *proc_offset += 2; /* FC_END and FC_PAD */
312 print_client("RpcFinally\n");
315 print_client( "__finally_%s%s( __frame );\n", prefix_client, get_name(func) );
318 print_client("RpcEndFinally\n");
321 /* emit return code */
322 if (!is_void(type_function_get_rettype(func->type)))
324 fprintf(client, "\n");
325 print_client("return _RetVal;\n");
329 fprintf(client, "}\n");
330 fprintf(client, "\n");
337 static void write_stubdescdecl(type_t *iface)
339 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
340 fprintf(client, "\n");
344 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
346 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
348 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
351 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
352 print_client("MIDL_user_allocate,\n");
353 print_client("MIDL_user_free,\n");
357 print_client("&%s,\n", implicit_handle);
359 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
361 print_client("},\n");
362 print_client("0,\n");
363 print_client("0,\n");
364 if (expr_eval_routines)
365 print_client("ExprEvalRoutines,\n");
367 print_client("0,\n");
368 print_client("0,\n");
369 print_client("__MIDL_TypeFormatString.Format,\n");
370 print_client("1, /* -error bounds_check flag */\n");
371 print_client("0x10001, /* Ndr library version */\n");
372 print_client("0,\n");
373 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
374 print_client("0,\n");
375 print_client("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
376 print_client("0, /* notify & notify_flag routine table */\n");
377 print_client("1, /* Flags */\n");
378 print_client("0, /* Reserved3 */\n");
379 print_client("0, /* Reserved4 */\n");
380 print_client("0 /* Reserved5 */\n");
382 print_client("};\n");
383 fprintf(client, "\n");
387 static void write_clientinterfacedecl(type_t *iface)
389 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
390 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
391 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
393 if (endpoints) write_endpoints( client, iface->name, endpoints );
395 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
398 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
399 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",
400 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
401 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
402 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
403 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
404 print_client("0,\n");
407 print_client("%u,\n", list_count(endpoints));
408 print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
412 print_client("0,\n");
413 print_client("0,\n");
415 print_client("0,\n");
416 print_client("0,\n");
417 print_client("0,\n");
419 print_client("};\n");
421 print_client("RPC_IF_HANDLE %s_ClientIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
422 iface->name, iface->name);
424 print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
425 prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
426 fprintf(client, "\n");
430 static void write_implicithandledecl(type_t *iface)
432 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
436 fprintf(client, "handle_t %s;\n", implicit_handle);
437 fprintf(client, "\n");
442 static void init_client(void)
445 if (!(client = fopen(client_name, "w")))
446 error("Could not open %s for output\n", client_name);
448 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
449 print_client("#include <string.h>\n");
450 print_client("#ifdef _ALPHA_\n");
451 print_client("#include <stdarg.h>\n");
452 print_client("#endif\n");
453 fprintf(client, "\n");
454 print_client("#include \"%s\"\n", header_name);
456 print_client( "#ifndef DECLSPEC_HIDDEN\n");
457 print_client( "#define DECLSPEC_HIDDEN\n");
458 print_client( "#endif\n");
460 write_exceptions( client );
465 static void write_client_ifaces(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
467 const statement_t *stmt;
468 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
470 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
473 const statement_t *stmt2;
474 type_t *iface = stmt->u.type;
475 if (!need_stub(iface))
478 fprintf(client, "/*****************************************************************************\n");
479 fprintf(client, " * %s interface\n", iface->name);
480 fprintf(client, " */\n");
481 fprintf(client, "\n");
483 STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
491 write_implicithandledecl(iface);
493 write_clientinterfacedecl(iface);
494 write_stubdescdecl(iface);
495 write_function_stubs(iface, proc_offset);
497 print_client("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
498 print_client("#error Invalid build platform for this stub.\n");
499 print_client("#endif\n");
501 fprintf(client, "\n");
502 write_stubdescriptor(iface, expr_eval_routines);
505 else if (stmt->type == STMT_LIBRARY)
506 write_client_ifaces(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
510 static void write_client_routines(const statement_list_t *stmts)
512 unsigned int proc_offset = 0;
513 int expr_eval_routines;
515 write_formatstringsdecl(client, indent, stmts, need_stub);
516 expr_eval_routines = write_expr_eval_routines(client, client_token);
517 if (expr_eval_routines)
518 write_expr_eval_routine_list(client, client_token);
519 write_user_quad_list(client);
521 write_client_ifaces(stmts, expr_eval_routines, &proc_offset);
523 fprintf(client, "\n");
525 write_procformatstring(client, stmts, need_stub);
526 write_typeformatstring(client, stmts, need_stub);
529 void write_client(const statement_list_t *stmts)
533 if (do_everything && !need_stub_files(stmts))
540 if (do_win32 && do_win64)
542 fprintf(client, "\n#ifndef _WIN64\n\n");
544 write_client_routines( stmts );
545 fprintf(client, "\n#else /* _WIN64 */\n\n");
547 write_client_routines( stmts );
548 fprintf(client, "\n#endif /* _WIN64 */\n");
553 write_client_routines( stmts );
558 write_client_routines( stmts );