4 * Copyright 2005 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"
42 #include "typelib_struct.h"
45 #define END_OF_LIST(list) \
48 while (NEXT_LINK(list)) \
49 list = NEXT_LINK(list); \
54 static int indent = 0;
57 static int print_server(const char *format, ...)
63 for (i = 0; i < indent; i++)
65 r = vfprintf(server, format, va);
71 static void write_parameters_init(const func_t *func)
79 while (NEXT_LINK(var)) var = NEXT_LINK(var);
82 print_server("%s = 0;\n", var->name);
86 fprintf(server, "\n");
90 static void write_function_stubs(type_t *iface)
92 char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
93 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
94 const func_t *func = iface->funcs;
96 const var_t* explicit_handle_var;
97 unsigned int proc_offset = 0;
98 unsigned int type_offset = 2;
100 while (NEXT_LINK(func)) func = NEXT_LINK(func);
103 const var_t *def = func->def;
104 unsigned long buffer_size = 0;
105 unsigned int type_offset_func;
107 /* check for a defined binding handle */
108 explicit_handle_var = get_explicit_handle_var(func);
111 if (!explicit_handle_var)
113 error("%s() does not define an explicit binding handle!\n", def->name);
117 else if (implicit_handle)
119 if (explicit_handle_var)
121 error("%s() must not define a binding handle!\n", def->name);
126 fprintf(server, "void __RPC_STUB\n");
127 fprintf(server, "%s_", iface->name);
128 write_name(server, def);
129 fprintf(server, "(\n");
131 print_server("PRPC_MESSAGE _pRpcMessage)\n");
134 /* write the functions body */
135 fprintf(server, "{\n");
138 /* declare return value '_RetVal' */
139 if (!is_void(def->type, NULL))
142 write_type(server, def->type, def, def->tname);
143 fprintf(server, " _RetVal;\n");
146 /* declare arguments */
150 while (NEXT_LINK(var)) var = NEXT_LINK(var);
154 write_type(server, var->type, var, var->tname);
155 fprintf(server, " ");
156 write_name(server, var);
157 write_array(server, var->array, 0);
158 fprintf(server, ";\n");
160 var = PREV_LINK(var);
164 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
165 print_server("RPC_STATUS _Status;\n");
166 fprintf(server, "\n");
169 print_server("((void)(_Status));\n");
170 print_server("NdrServerInitializeNew(\n");
172 print_server("_pRpcMessage,\n");
173 print_server("&_StubMsg,\n");
174 print_server("&%s_StubDesc);\n", iface->name);
176 fprintf(server, "\n");
178 write_parameters_init(func);
180 if (explicit_handle_var)
182 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
183 fprintf(server, "\n");
186 print_server("RpcTryFinally\n");
189 print_server("RpcTryExcept\n");
195 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
197 print_server("NdrConvert(\n");
199 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
200 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
202 fprintf(server, "\n");
204 /* make a copy so we don't increment the type offset twice */
205 type_offset_func = type_offset;
207 /* unmarshall arguments */
208 write_remoting_arguments(server, indent, func, &type_offset_func, PASS_IN, PHASE_UNMARSHAL);
211 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
214 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
219 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
222 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
225 print_server("RpcEndExcept\n");
226 fprintf(server, "\n");
229 /* Call the real server function */
230 if (!is_void(def->type, NULL))
231 print_server("_RetVal = ");
234 write_name(server, def);
240 fprintf(server, "(\n");
243 while (NEXT_LINK(var)) var = NEXT_LINK(var);
249 fprintf(server, ",\n");
251 write_name(server, var);
252 var = PREV_LINK(var);
254 fprintf(server, ");\n");
259 fprintf(server, "();\n");
264 const var_t *var = func->args;
265 while (NEXT_LINK(var)) var = NEXT_LINK(var);
268 if (is_attr(var->attrs, ATTR_OUT))
270 unsigned int alignment;
271 buffer_size += get_required_buffer_size(var, &alignment);
272 buffer_size += alignment;
275 var = PREV_LINK(var);
279 if (!is_void(def->type, NULL))
281 unsigned int alignment;
282 buffer_size += get_required_buffer_size(def, &alignment);
283 buffer_size += alignment;
288 fprintf(server, "\n");
289 print_server("_StubMsg.BufferLength = %uU;\n", buffer_size);
290 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
291 fprintf(server, "\n");
292 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
293 print_server("if (_Status)\n");
295 print_server("RpcRaiseException(_Status);\n");
297 fprintf(server, "\n");
298 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
299 fprintf(server, "\n");
302 /* marshall arguments */
303 write_remoting_arguments(server, indent, func, &type_offset, PASS_OUT, PHASE_MARSHAL);
305 /* marshall the return value */
306 if (!is_void(def->type, NULL))
309 write_type(server, def->type, def, def->tname);
310 fprintf(server, " *)_StubMsg.Buffer = _RetVal;\n");
311 print_server("_StubMsg.Buffer += sizeof(");
312 write_type(server, def->type, def, def->tname);
313 fprintf(server, ");\n");
318 print_server("RpcFinally\n");
321 print_server("RpcEndFinally\n");
323 /* calculate buffer length */
324 fprintf(server, "\n");
325 print_server("_pRpcMessage->BufferLength =\n");
327 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
330 fprintf(server, "}\n");
331 fprintf(server, "\n");
333 /* update proc_offset */
337 while (NEXT_LINK(var)) var = NEXT_LINK(var);
340 proc_offset += get_size_procformatstring_var(var);
341 var = PREV_LINK(var);
344 if (!is_void(def->type, NULL))
345 proc_offset += get_size_procformatstring_var(def);
347 proc_offset += 2; /* FC_END and FC_PAD */
349 func = PREV_LINK(func);
354 static void write_dispatchtable(type_t *iface)
356 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
357 unsigned long method_count = 0;
358 func_t *func = iface->funcs;
360 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
363 while (NEXT_LINK(func)) func = NEXT_LINK(func);
366 var_t *def = func->def;
368 print_server("%s_", iface->name);
369 write_name(server, def);
370 fprintf(server, ",\n");
373 func = PREV_LINK(func);
377 print_server("};\n");
378 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
381 print_server("%u,\n", method_count);
382 print_server("%s_table\n", iface->name);
384 print_server("};\n");
385 fprintf(server, "\n");
389 static void write_stubdescdecl(type_t *iface)
391 print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
392 fprintf(server, "\n");
396 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
398 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
401 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
402 print_server("MIDL_user_allocate,\n");
403 print_server("MIDL_user_free,\n");
404 print_server("0,\n");
405 print_server("0,\n");
406 print_server("0,\n");
407 if (expr_eval_routines)
408 print_server("ExprEvalRoutines,\n");
410 print_server("0,\n");
411 print_server("0,\n");
412 print_server("__MIDL_TypeFormatString.Format,\n");
413 print_server("1, /* -error bounds_check flag */\n");
414 print_server("0x10001, /* Ndr library version */\n");
415 print_server("0,\n");
416 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
417 print_server("0,\n");
418 print_server("0,\n");
419 print_server("0, /* notify & notify_flag routine table */\n");
420 print_server("1, /* Flags */\n");
421 print_server("0, /* Reserved3 */\n");
422 print_server("0, /* Reserved4 */\n");
423 print_server("0 /* Reserved5 */\n");
425 print_server("};\n");
426 fprintf(server, "\n");
430 static void write_serverinterfacedecl(type_t *iface)
432 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
433 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
435 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
436 fprintf(server, "\n");
437 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
440 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
441 print_server("{{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",
442 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
443 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
444 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
445 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
446 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
447 print_server("0,\n");
448 print_server("0,\n");
449 print_server("0,\n");
450 print_server("0,\n");
451 print_server("0,\n");
453 print_server("};\n");
454 print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
455 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
456 fprintf(server, "\n");
459 static void write_formatdesc( const char *str )
461 print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
464 print_server("short Pad;\n");
465 print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
467 print_server("} MIDL_%s_FORMAT_STRING;\n", str);
472 static void write_formatstringsdecl(type_t *iface)
476 print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
478 /* determine the proc format string size */
481 func_t *func = iface->funcs;
482 while (NEXT_LINK(func)) func = NEXT_LINK(func);
485 /* argument list size */
488 var_t *var = func->args;
489 while (NEXT_LINK(var)) var = NEXT_LINK(var);
492 byte_count += 2; /* FIXME: determine real size */
493 var = PREV_LINK(var);
497 /* return value size */
498 byte_count += 2; /* FIXME: determine real size */
499 func = PREV_LINK(func);
502 print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
504 fprintf(server, "\n");
505 write_formatdesc("TYPE");
506 write_formatdesc("PROC");
507 fprintf(server, "\n");
508 print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
509 print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
514 static void init_server(void)
518 if (!(server = fopen(server_name, "w")))
519 error("Could not open %s for output\n", server_name);
521 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
522 print_server("#include <string.h>\n");
523 fprintf(server, "\n");
524 print_server("#include \"%s\"\n", header_name);
525 fprintf(server, "\n");
529 void write_server(ifref_t *ifaces)
531 ifref_t *iface = ifaces;
545 fprintf(server, "/*****************************************************************************\n");
546 fprintf(server, " * %s interface\n", iface->iface->name);
547 fprintf(server, " */\n");
548 fprintf(server, "\n");
550 if (iface->iface->funcs)
552 int expr_eval_routines;
554 write_formatstringsdecl(iface->iface);
555 write_serverinterfacedecl(iface->iface);
556 write_stubdescdecl(iface->iface);
558 write_function_stubs(iface->iface);
560 print_server("#if !defined(__RPC_WIN32__)\n");
561 print_server("#error Invalid build platform for this stub.\n");
562 print_server("#endif\n");
563 fprintf(server, "\n");
565 write_procformatstring(server, iface->iface);
566 write_typeformatstring(server, iface->iface);
568 fprintf(server, "\n");
570 expr_eval_routines = write_expr_eval_routines(server, iface->iface->name);
571 if (expr_eval_routines)
572 write_expr_eval_routine_list(server, iface->iface->name);
574 write_stubdescriptor(iface->iface, expr_eval_routines);
575 write_dispatchtable(iface->iface);
578 iface = PREV_LINK(iface);