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"
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 if (var->type->type != RPC_FC_BIND_PRIMITIVE)
83 print_server("%s = 0;\n", var->name);
87 fprintf(server, "\n");
91 static void write_function_stubs(type_t *iface)
93 char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
94 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
95 const func_t *func = iface->funcs;
97 const var_t* explicit_handle_var;
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 unsigned long buffer_size = 0;
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 fprintf(server, "void __RPC_STUB\n");
128 fprintf(server, "%s_", iface->name);
129 write_name(server, def);
130 fprintf(server, "(\n");
132 print_server("PRPC_MESSAGE _pRpcMessage)\n");
135 /* write the functions body */
136 fprintf(server, "{\n");
139 /* declare return value '_RetVal' */
140 if (!is_void(def->type, NULL))
143 write_type(server, def->type, def, def->tname);
144 fprintf(server, " _RetVal;\n");
147 /* declare arguments */
151 while (NEXT_LINK(var)) var = NEXT_LINK(var);
155 write_type(server, var->type, var, var->tname);
156 fprintf(server, " ");
157 write_name(server, var);
158 write_array(server, var->array, 0);
159 fprintf(server, ";\n");
161 var = PREV_LINK(var);
165 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
166 print_server("RPC_STATUS _Status;\n");
167 fprintf(server, "\n");
170 print_server("((void)(_Status));\n");
171 print_server("NdrServerInitializeNew(\n");
173 print_server("_pRpcMessage,\n");
174 print_server("&_StubMsg,\n");
175 print_server("&%s_StubDesc);\n", iface->name);
177 fprintf(server, "\n");
179 write_parameters_init(func);
181 if (explicit_handle_var)
183 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
184 fprintf(server, "\n");
187 print_server("RpcTryFinally\n");
190 print_server("RpcTryExcept\n");
196 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
198 print_server("NdrConvert(\n");
200 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
201 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
203 fprintf(server, "\n");
205 /* make a copy so we don't increment the type offset twice */
206 type_offset_func = type_offset;
208 /* unmarshall arguments */
209 write_remoting_arguments(server, indent, func, &type_offset_func, PASS_IN, PHASE_UNMARSHAL);
212 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
215 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
220 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
223 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
226 print_server("RpcEndExcept\n");
227 fprintf(server, "\n");
230 /* Call the real server function */
231 if (!is_void(def->type, NULL))
232 print_server("_RetVal = ");
235 write_name(server, def);
241 fprintf(server, "(\n");
244 while (NEXT_LINK(var)) var = NEXT_LINK(var);
250 fprintf(server, ",\n");
252 write_name(server, var);
253 var = PREV_LINK(var);
255 fprintf(server, ");\n");
260 fprintf(server, "();\n");
265 const var_t *var = func->args;
266 while (NEXT_LINK(var)) var = NEXT_LINK(var);
269 if (is_attr(var->attrs, ATTR_OUT))
271 unsigned int alignment;
272 buffer_size += get_required_buffer_size(var, &alignment);
273 buffer_size += alignment;
276 var = PREV_LINK(var);
280 if (!is_void(def->type, NULL))
282 unsigned int alignment;
283 buffer_size += get_required_buffer_size(def, &alignment);
284 buffer_size += alignment;
289 fprintf(server, "\n");
290 print_server("_StubMsg.BufferLength = %uU;\n", buffer_size);
291 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
292 fprintf(server, "\n");
293 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
294 print_server("if (_Status)\n");
296 print_server("RpcRaiseException(_Status);\n");
298 fprintf(server, "\n");
299 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
300 fprintf(server, "\n");
303 /* marshall arguments */
304 write_remoting_arguments(server, indent, func, &type_offset, PASS_OUT, PHASE_MARSHAL);
306 /* marshall the return value */
307 if (!is_void(def->type, NULL))
309 unsigned int alignment;
310 unsigned int size = get_required_buffer_size(def, &alignment);
311 print_server("_StubMsg.Buffer += (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
314 write_type(server, def->type, def, def->tname);
315 fprintf(server, " *)_StubMsg.Buffer = _RetVal;\n");
316 print_server("_StubMsg.Buffer += sizeof(");
317 write_type(server, def->type, def, def->tname);
318 fprintf(server, ");\n");
323 print_server("RpcFinally\n");
326 print_server("RpcEndFinally\n");
328 /* calculate buffer length */
329 fprintf(server, "\n");
330 print_server("_pRpcMessage->BufferLength =\n");
332 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
335 fprintf(server, "}\n");
336 fprintf(server, "\n");
338 /* update proc_offset */
342 while (NEXT_LINK(var)) var = NEXT_LINK(var);
345 proc_offset += get_size_procformatstring_var(var);
346 var = PREV_LINK(var);
349 if (!is_void(def->type, NULL))
350 proc_offset += get_size_procformatstring_var(def);
352 proc_offset += 2; /* FC_END and FC_PAD */
354 func = PREV_LINK(func);
359 static void write_dispatchtable(type_t *iface)
361 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
362 unsigned long method_count = 0;
363 func_t *func = iface->funcs;
365 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
368 while (NEXT_LINK(func)) func = NEXT_LINK(func);
371 var_t *def = func->def;
373 print_server("%s_", iface->name);
374 write_name(server, def);
375 fprintf(server, ",\n");
378 func = PREV_LINK(func);
382 print_server("};\n");
383 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
386 print_server("%u,\n", method_count);
387 print_server("%s_table\n", iface->name);
389 print_server("};\n");
390 fprintf(server, "\n");
394 static void write_stubdescdecl(type_t *iface)
396 print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
397 fprintf(server, "\n");
401 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
403 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
406 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
407 print_server("MIDL_user_allocate,\n");
408 print_server("MIDL_user_free,\n");
409 print_server("0,\n");
410 print_server("0,\n");
411 print_server("0,\n");
412 if (expr_eval_routines)
413 print_server("ExprEvalRoutines,\n");
415 print_server("0,\n");
416 print_server("0,\n");
417 print_server("__MIDL_TypeFormatString.Format,\n");
418 print_server("1, /* -error bounds_check flag */\n");
419 print_server("0x10001, /* Ndr library version */\n");
420 print_server("0,\n");
421 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
422 print_server("0,\n");
423 print_server("0,\n");
424 print_server("0, /* notify & notify_flag routine table */\n");
425 print_server("1, /* Flags */\n");
426 print_server("0, /* Reserved3 */\n");
427 print_server("0, /* Reserved4 */\n");
428 print_server("0 /* Reserved5 */\n");
430 print_server("};\n");
431 fprintf(server, "\n");
435 static void write_serverinterfacedecl(type_t *iface)
437 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
438 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
440 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
441 fprintf(server, "\n");
442 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
445 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
446 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",
447 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
448 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
449 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
450 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
451 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
452 print_server("0,\n");
453 print_server("0,\n");
454 print_server("0,\n");
455 print_server("0,\n");
456 print_server("0,\n");
458 print_server("};\n");
459 print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
460 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
461 fprintf(server, "\n");
464 static void write_formatdesc( const char *str )
466 print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
469 print_server("short Pad;\n");
470 print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
472 print_server("} MIDL_%s_FORMAT_STRING;\n", str);
477 static void write_formatstringsdecl(type_t *iface)
479 print_server("#define TYPE_FORMAT_STRING_SIZE %d\n",
480 get_size_typeformatstring(iface));
482 print_server("#define PROC_FORMAT_STRING_SIZE %d\n",
483 get_size_procformatstring(iface));
485 fprintf(server, "\n");
486 write_formatdesc("TYPE");
487 write_formatdesc("PROC");
488 fprintf(server, "\n");
489 print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
490 print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
495 static void init_server(void)
499 if (!(server = fopen(server_name, "w")))
500 error("Could not open %s for output\n", server_name);
502 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
503 print_server("#include <string.h>\n");
504 fprintf(server, "\n");
505 print_server("#include \"%s\"\n", header_name);
506 fprintf(server, "\n");
510 void write_server(ifref_t *ifaces)
512 ifref_t *iface = ifaces;
526 fprintf(server, "/*****************************************************************************\n");
527 fprintf(server, " * %s interface\n", iface->iface->name);
528 fprintf(server, " */\n");
529 fprintf(server, "\n");
531 if (iface->iface->funcs)
533 int expr_eval_routines;
535 write_formatstringsdecl(iface->iface);
536 write_serverinterfacedecl(iface->iface);
537 write_stubdescdecl(iface->iface);
539 write_function_stubs(iface->iface);
541 print_server("#if !defined(__RPC_WIN32__)\n");
542 print_server("#error Invalid build platform for this stub.\n");
543 print_server("#endif\n");
544 fprintf(server, "\n");
546 write_procformatstring(server, iface->iface);
547 write_typeformatstring(server, iface->iface);
549 fprintf(server, "\n");
551 expr_eval_routines = write_expr_eval_routines(server, iface->iface->name);
552 if (expr_eval_routines)
553 write_expr_eval_routine_list(server, iface->iface->name);
555 write_stubdescriptor(iface->iface, expr_eval_routines);
556 write_dispatchtable(iface->iface);
559 iface = PREV_LINK(iface);