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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
39 #include "widltypes.h"
41 #include "typelib_struct.h"
44 #define END_OF_LIST(list) \
47 while (NEXT_LINK(list)) \
48 list = NEXT_LINK(list); \
53 static int indent = 0;
55 static int print_client( const char *format, ... )
61 for (i = 0; i < indent; i++)
63 r = vfprintf(client, format, va);
69 static void print_message_buffer_size(const func_t *func)
71 unsigned int total_size = 0;
75 const var_t *var = func->args;
76 while (NEXT_LINK(var)) var = NEXT_LINK(var);
79 unsigned int alignment;
81 total_size += get_required_buffer_size(var, &alignment);
82 total_size += alignment;
87 fprintf(client, " %u", total_size);
91 static void write_function_stubs(type_t *iface)
93 const func_t *func = iface->funcs;
94 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
95 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
98 unsigned int proc_offset = 0;
99 unsigned int type_offset = 2;
101 while (NEXT_LINK(func)) func = NEXT_LINK(func);
104 const var_t *def = func->def;
105 const var_t* explicit_handle_var;
106 unsigned int type_offset_func;
108 /* check for a defined binding handle */
109 explicit_handle_var = get_explicit_handle_var(func);
112 if (!explicit_handle_var)
114 error("%s() does not define an explicit binding handle!\n", def->name);
118 else if (implicit_handle)
120 if (explicit_handle_var)
122 error("%s() must not define a binding handle!\n", def->name);
127 write_type(client, def->type, def, def->tname);
128 fprintf(client, " ");
129 write_name(client, def);
130 fprintf(client, "(\n");
133 write_args(client, func->args, iface->name, 0, TRUE);
135 print_client("void");
136 fprintf(client, ")\n");
139 /* write the functions body */
140 fprintf(client, "{\n");
143 /* declare return value '_RetVal' */
144 if (!is_void(def->type, NULL))
147 write_type(client, def->type, def, def->tname);
148 fprintf(client, " _RetVal;\n");
151 if (implicit_handle || explicit_handle_var)
152 print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
154 print_client("RPC_MESSAGE _RpcMessage;\n");
155 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
156 fprintf(client, "\n");
157 print_client("RpcTryFinally\n");
161 print_client("NdrClientInitializeNew(\n");
163 print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
164 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
165 print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
166 print_client("%d);\n", method_count);
168 fprintf(client, "\n");
172 print_client("_Handle = %s;\n", implicit_handle);
173 fprintf(client, "\n");
175 else if (explicit_handle_var)
177 print_client("_Handle = %s;\n", explicit_handle_var->name);
178 fprintf(client, "\n");
181 /* emit the message buffer size */
182 print_client("_StubMsg.BufferLength =");
183 print_message_buffer_size(func);
184 fprintf(client, ";\n");
186 print_client("NdrGetBuffer(\n");
188 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
189 print_client("_StubMsg.BufferLength,\n");
190 if (implicit_handle || explicit_handle_var)
191 print_client("_Handle);\n");
193 print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
195 fprintf(client, "\n");
198 /* make a copy so we don't increment the type offset twice */
199 type_offset_func = type_offset;
201 /* marshal arguments */
202 write_remoting_arguments(client, indent, func, &type_offset_func, PASS_IN, PHASE_MARSHAL);
204 /* send/receive message */
205 /* print_client("NdrNsSendReceive(\n"); */
206 /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
207 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
208 print_client("NdrSendReceive(\n");
210 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
211 print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
214 print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
215 print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n");
217 /* unmarshall arguments */
218 write_remoting_arguments(client, indent, func, &type_offset, PASS_OUT, PHASE_UNMARSHAL);
220 /* unmarshal return value */
221 if (!is_void(def->type, NULL))
223 fprintf(client, "\n");
225 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
227 print_client("NdrConvert(\n");
229 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
230 print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
232 fprintf(client, "\n");
234 print_client("_RetVal = *(");
235 write_type(client, def->type, def, def->tname);
236 fprintf(client, " *)_StubMsg.Buffer;\n");
237 print_client("_StubMsg.Buffer += sizeof(");
238 write_type(client, def->type, def, def->tname);
239 fprintf(client, ");\n");
242 /* update proc_offset */
246 while (NEXT_LINK(var)) var = NEXT_LINK(var);
249 proc_offset += get_size_procformatstring_var(var);
250 var = PREV_LINK(var);
253 if (!is_void(def->type, NULL))
254 proc_offset += get_size_procformatstring_var(def);
256 proc_offset += 2; /* FC_END and FC_PAD */
260 print_client("RpcFinally\n");
265 /* FIXME: emit client finally code */
267 print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
271 print_client("RpcEndFinally\n");
274 /* emit return code */
275 if (!is_void(def->type, NULL))
277 fprintf(client, "\n");
278 print_client("return _RetVal;\n");
282 fprintf(client, "}\n");
283 fprintf(client, "\n");
286 func = PREV_LINK(func);
291 static void write_bindinghandledecl(type_t *iface)
293 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n", iface->name);
294 fprintf(client, "\n");
298 static void write_stubdescdecl(type_t *iface)
300 print_client("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
301 fprintf(client, "\n");
305 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
307 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
309 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
312 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
313 print_client("MIDL_user_allocate,\n");
314 print_client("MIDL_user_free,\n");
316 print_client("&%s,\n", implicit_handle);
318 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
319 print_client("0,\n");
320 print_client("0,\n");
321 if (expr_eval_routines)
322 print_client("ExprEvalRoutines,\n");
324 print_client("0,\n");
325 print_client("0,\n");
326 print_client("__MIDL_TypeFormatString.Format,\n");
327 print_client("1, /* -error bounds_check flag */\n");
328 print_client("0x10001, /* Ndr library version */\n");
329 print_client("0,\n");
330 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
331 print_client("0,\n");
332 print_client("0,\n");
333 print_client("0, /* notify & notify_flag routine table */\n");
334 print_client("1, /* Flags */\n");
335 print_client("0, /* Reserved3 */\n");
336 print_client("0, /* Reserved4 */\n");
337 print_client("0 /* Reserved5 */\n");
339 print_client("};\n");
340 fprintf(client, "\n");
344 static void write_clientinterfacedecl(type_t *iface)
346 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
347 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
349 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
352 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
353 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",
354 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
355 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
356 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
357 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
358 print_client("0,\n");
359 print_client("0,\n");
360 print_client("0,\n");
361 print_client("0,\n");
362 print_client("0,\n");
363 print_client("0,\n");
365 print_client("};\n");
366 print_client("RPC_IF_HANDLE %s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
367 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
368 fprintf(client, "\n");
372 static void write_formatdesc( const char *str )
374 print_client("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
377 print_client("short Pad;\n");
378 print_client("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
380 print_client("} MIDL_%s_FORMAT_STRING;\n", str);
385 static void write_formatstringsdecl(type_t *iface)
387 print_client("#define TYPE_FORMAT_STRING_SIZE %d\n",
388 get_size_typeformatstring(iface));
390 print_client("#define PROC_FORMAT_STRING_SIZE %d\n",
391 get_size_procformatstring(iface));
393 fprintf(client, "\n");
394 write_formatdesc("TYPE");
395 write_formatdesc("PROC");
396 fprintf(client, "\n");
397 print_client("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
398 print_client("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
403 static void write_implicithandledecl(type_t *iface)
405 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
409 fprintf(client, "handle_t %s;\n", implicit_handle);
410 fprintf(client, "\n");
415 static void init_client(void)
418 if (!(client = fopen(client_name, "w")))
419 error("Could not open %s for output\n", client_name);
421 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
422 print_client("#include <string.h>\n");
423 print_client("#ifdef _ALPHA_\n");
424 print_client("#include <stdarg.h>\n");
425 print_client("#endif\n");
426 fprintf(client, "\n");
427 print_client("#include \"%s\"\n", header_name);
428 fprintf(client, "\n");
432 void write_client(ifref_t *ifaces)
434 ifref_t *iface = ifaces;
448 fprintf(client, "/*****************************************************************************\n");
449 fprintf(client, " * %s interface\n", iface->iface->name);
450 fprintf(client, " */\n");
451 fprintf(client, "\n");
453 if (iface->iface->funcs)
455 int expr_eval_routines;
457 write_formatstringsdecl(iface->iface);
458 write_implicithandledecl(iface->iface);
460 write_clientinterfacedecl(iface->iface);
461 write_stubdescdecl(iface->iface);
462 write_bindinghandledecl(iface->iface);
464 write_function_stubs(iface->iface);
466 print_client("#if !defined(__RPC_WIN32__)\n");
467 print_client("#error Invalid build platform for this stub.\n");
468 print_client("#endif\n");
469 fprintf(client, "\n");
471 write_procformatstring(client, iface->iface);
472 write_typeformatstring(client, iface->iface);
474 fprintf(client, "\n");
476 expr_eval_routines = write_expr_eval_routines(client, iface->iface->name);
477 if (expr_eval_routines)
478 write_expr_eval_routine_list(client, iface->iface->name);
479 write_stubdescriptor(iface->iface, expr_eval_routines);
482 iface = PREV_LINK(iface);