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, PASS_IN);
82 total_size += alignment;
87 fprintf(client, " %u", total_size);
90 static void check_pointers(const func_t *func)
99 while (NEXT_LINK(var)) var = NEXT_LINK(var);
102 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
104 pointer_type = RPC_FC_RP;
106 if (pointer_type == RPC_FC_RP)
108 if (var->ptr_level >= 1)
110 print_client("if (!%s)\n", var->name);
113 print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
115 print_client("}\n\n");
119 var = PREV_LINK(var);
123 static void write_function_stubs(type_t *iface)
125 const func_t *func = iface->funcs;
126 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
127 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
129 int method_count = 0;
130 unsigned int proc_offset = 0;
131 unsigned int type_offset = 2;
133 while (NEXT_LINK(func)) func = NEXT_LINK(func);
136 const var_t *def = func->def;
137 const var_t* explicit_handle_var;
138 unsigned int type_offset_func;
140 /* check for a defined binding handle */
141 explicit_handle_var = get_explicit_handle_var(func);
144 if (!explicit_handle_var)
146 error("%s() does not define an explicit binding handle!\n", def->name);
150 else if (implicit_handle)
152 if (explicit_handle_var)
154 error("%s() must not define a binding handle!\n", def->name);
159 write_type(client, def->type, def, def->tname);
160 fprintf(client, " ");
161 write_name(client, def);
162 fprintf(client, "(\n");
165 write_args(client, func->args, iface->name, 0, TRUE);
167 print_client("void");
168 fprintf(client, ")\n");
171 /* write the functions body */
172 fprintf(client, "{\n");
175 /* declare return value '_RetVal' */
176 if (!is_void(def->type, NULL))
179 write_type(client, def->type, def, def->tname);
180 fprintf(client, " _RetVal;\n");
183 if (implicit_handle || explicit_handle_var)
184 print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
186 print_client("RPC_MESSAGE _RpcMessage;\n");
187 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
188 fprintf(client, "\n");
191 check_pointers(func);
193 print_client("RpcTryFinally\n");
197 print_client("NdrClientInitializeNew(\n");
199 print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
200 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
201 print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
202 print_client("%d);\n", method_count);
204 fprintf(client, "\n");
208 print_client("_Handle = %s;\n", implicit_handle);
209 fprintf(client, "\n");
211 else if (explicit_handle_var)
213 print_client("_Handle = %s;\n", explicit_handle_var->name);
214 fprintf(client, "\n");
217 /* emit the message buffer size */
218 print_client("_StubMsg.BufferLength =");
219 print_message_buffer_size(func);
220 fprintf(client, ";\n");
222 type_offset_func = type_offset;
223 write_remoting_arguments(client, indent, func, &type_offset_func, PASS_IN, PHASE_BUFFERSIZE);
225 print_client("NdrGetBuffer(\n");
227 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
228 print_client("_StubMsg.BufferLength,\n");
229 if (implicit_handle || explicit_handle_var)
230 print_client("_Handle);\n");
232 print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
234 fprintf(client, "\n");
237 /* make a copy so we don't increment the type offset twice */
238 type_offset_func = type_offset;
240 /* marshal arguments */
241 write_remoting_arguments(client, indent, func, &type_offset_func, PASS_IN, PHASE_MARSHAL);
243 /* send/receive message */
244 /* print_client("NdrNsSendReceive(\n"); */
245 /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
246 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
247 print_client("NdrSendReceive(\n");
249 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
250 print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
253 print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
254 print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
256 if (has_out_arg_or_return(func))
258 fprintf(client, "\n");
260 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
262 print_client("NdrConvert(\n");
264 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
265 print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
269 /* unmarshall arguments */
270 fprintf(client, "\n");
271 write_remoting_arguments(client, indent, func, &type_offset, PASS_OUT, PHASE_UNMARSHAL);
273 /* unmarshal return value */
274 if (!is_void(def->type, NULL))
275 print_phase_basetype(client, indent, PHASE_UNMARSHAL, PASS_RETURN, def, "_RetVal");
277 /* update proc_offset */
281 while (NEXT_LINK(var)) var = NEXT_LINK(var);
284 proc_offset += get_size_procformatstring_var(var);
285 var = PREV_LINK(var);
288 if (!is_void(def->type, NULL))
289 proc_offset += get_size_procformatstring_var(def);
291 proc_offset += 2; /* FC_END and FC_PAD */
295 print_client("RpcFinally\n");
300 /* FIXME: emit client finally code */
302 print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
306 print_client("RpcEndFinally\n");
309 /* emit return code */
310 if (!is_void(def->type, NULL))
312 fprintf(client, "\n");
313 print_client("return _RetVal;\n");
317 fprintf(client, "}\n");
318 fprintf(client, "\n");
321 func = PREV_LINK(func);
326 static void write_bindinghandledecl(type_t *iface)
328 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n", iface->name);
329 fprintf(client, "\n");
333 static void write_stubdescdecl(type_t *iface)
335 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
336 fprintf(client, "\n");
340 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
342 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
344 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
347 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
348 print_client("MIDL_user_allocate,\n");
349 print_client("MIDL_user_free,\n");
353 print_client("&%s,\n", implicit_handle);
355 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
357 print_client("},\n");
358 print_client("0,\n");
359 print_client("0,\n");
360 if (expr_eval_routines)
361 print_client("ExprEvalRoutines,\n");
363 print_client("0,\n");
364 print_client("0,\n");
365 print_client("__MIDL_TypeFormatString.Format,\n");
366 print_client("1, /* -error bounds_check flag */\n");
367 print_client("0x10001, /* Ndr library version */\n");
368 print_client("0,\n");
369 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
370 print_client("0,\n");
371 print_client("0,\n");
372 print_client("0, /* notify & notify_flag routine table */\n");
373 print_client("1, /* Flags */\n");
374 print_client("0, /* Reserved3 */\n");
375 print_client("0, /* Reserved4 */\n");
376 print_client("0 /* Reserved5 */\n");
378 print_client("};\n");
379 fprintf(client, "\n");
383 static void write_clientinterfacedecl(type_t *iface)
385 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
386 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
388 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
391 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
392 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",
393 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
394 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
395 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
396 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
397 print_client("0,\n");
398 print_client("0,\n");
399 print_client("0,\n");
400 print_client("0,\n");
401 print_client("0,\n");
402 print_client("0,\n");
404 print_client("};\n");
406 print_client("RPC_IF_HANDLE %s_ClientIfHandle = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
407 iface->name, iface->name);
409 print_client("RPC_IF_HANDLE %s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
410 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
411 fprintf(client, "\n");
415 static void write_formatdesc( const char *str )
417 print_client("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
420 print_client("short Pad;\n");
421 print_client("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
423 print_client("} MIDL_%s_FORMAT_STRING;\n", str);
428 static void write_formatstringsdecl(type_t *iface)
430 print_client("#define TYPE_FORMAT_STRING_SIZE %d\n",
431 get_size_typeformatstring(iface));
433 print_client("#define PROC_FORMAT_STRING_SIZE %d\n",
434 get_size_procformatstring(iface));
436 fprintf(client, "\n");
437 write_formatdesc("TYPE");
438 write_formatdesc("PROC");
439 fprintf(client, "\n");
440 print_client("static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
441 print_client("static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
446 static void write_implicithandledecl(type_t *iface)
448 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
452 fprintf(client, "handle_t %s;\n", implicit_handle);
453 fprintf(client, "\n");
458 static void init_client(void)
461 if (!(client = fopen(client_name, "w")))
462 error("Could not open %s for output\n", client_name);
464 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
465 print_client("#include <string.h>\n");
466 print_client("#ifdef _ALPHA_\n");
467 print_client("#include <stdarg.h>\n");
468 print_client("#endif\n");
469 fprintf(client, "\n");
470 print_client("#include \"%s\"\n", header_name);
471 fprintf(client, "\n");
475 void write_client(ifref_t *ifaces)
477 ifref_t *iface = ifaces;
489 for (; iface; iface = PREV_LINK(iface))
491 if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
494 fprintf(client, "/*****************************************************************************\n");
495 fprintf(client, " * %s interface\n", iface->iface->name);
496 fprintf(client, " */\n");
497 fprintf(client, "\n");
499 if (iface->iface->funcs)
501 int expr_eval_routines;
503 write_formatstringsdecl(iface->iface);
504 write_implicithandledecl(iface->iface);
506 write_clientinterfacedecl(iface->iface);
507 write_stubdescdecl(iface->iface);
508 write_bindinghandledecl(iface->iface);
510 write_function_stubs(iface->iface);
512 print_client("#if !defined(__RPC_WIN32__)\n");
513 print_client("#error Invalid build platform for this stub.\n");
514 print_client("#endif\n");
515 fprintf(client, "\n");
517 write_procformatstring(client, iface->iface);
518 write_typeformatstring(client, iface->iface);
520 fprintf(client, "\n");
522 expr_eval_routines = write_expr_eval_routines(client, iface->iface->name);
523 if (expr_eval_routines)
524 write_expr_eval_routine_list(client, iface->iface->name);
525 write_stubdescriptor(iface->iface, expr_eval_routines);