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(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 func_t *func = iface->funcs;
96 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 var_t *def = func->def;
104 unsigned long buffer_size = 0;
106 /* check for a defined binding handle */
107 explicit_handle_var = get_explicit_handle_var(func);
110 if (!explicit_handle_var)
112 error("%s() does not define an explicit binding handle!\n", def->name);
116 else if (implicit_handle)
118 if (explicit_handle_var)
120 error("%s() must not define a binding handle!\n", def->name);
125 fprintf(server, "void __RPC_STUB\n");
126 fprintf(server, "%s_", iface->name);
127 write_name(server, def);
128 fprintf(server, "(\n");
130 print_server("PRPC_MESSAGE _pRpcMessage)\n");
133 /* write the functions body */
134 fprintf(server, "{\n");
137 /* declare return value '_RetVal' */
138 if (!is_void(def->type, NULL))
141 write_type(server, def->type, def, def->tname);
142 fprintf(server, " _RetVal;\n");
145 /* declare arguments */
149 while (NEXT_LINK(var)) var = NEXT_LINK(var);
153 write_type(server, var->type, var, var->tname);
154 fprintf(server, " ");
155 write_name(server, var);
156 write_array(server, var->array, 0);
157 fprintf(server, ";\n");
159 var = PREV_LINK(var);
163 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
164 print_server("RPC_STATUS _Status;\n");
165 fprintf(server, "\n");
168 print_server("((void)(_Status));\n");
169 print_server("NdrServerInitializeNew(\n");
171 print_server("_pRpcMessage,\n");
172 print_server("&_StubMsg,\n");
173 print_server("&%s_StubDesc);\n", iface->name);
175 fprintf(server, "\n");
177 write_parameters_init(func);
179 if (explicit_handle_var)
181 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
182 fprintf(server, "\n");
185 print_server("RpcTryFinally\n");
188 print_server("RpcTryExcept\n");
194 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
196 print_server("NdrConvert(\n");
198 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
199 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
201 fprintf(server, "\n");
203 unmarshall_arguments(server, indent, func, &type_offset, PASS_IN);
206 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
209 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
214 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
217 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
220 print_server("RpcEndExcept\n");
221 fprintf(server, "\n");
224 /* Call the real server function */
225 if (!is_void(def->type, NULL))
226 print_server("_RetVal = ");
229 write_name(server, def);
235 fprintf(server, "(\n");
238 while (NEXT_LINK(var)) var = NEXT_LINK(var);
244 fprintf(server, ",\n");
246 write_name(server, var);
247 var = PREV_LINK(var);
249 fprintf(server, ");\n");
254 fprintf(server, "();\n");
259 var_t *var = func->args;
260 while (NEXT_LINK(var)) var = NEXT_LINK(var);
263 if (is_attr(var->attrs, ATTR_OUT))
265 unsigned int alignment;
266 buffer_size += get_required_buffer_size(var, &alignment);
267 buffer_size += alignment;
270 var = PREV_LINK(var);
274 if (!is_void(def->type, NULL))
276 unsigned int alignment;
277 buffer_size += get_required_buffer_size(def, &alignment);
278 buffer_size += alignment;
283 fprintf(server, "\n");
284 print_server("_StubMsg.BufferLength = %uU;\n", buffer_size);
285 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
286 fprintf(server, "\n");
287 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
288 print_server("if (_Status)\n");
290 print_server("RpcRaiseException(_Status);\n");
292 fprintf(server, "\n");
293 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
294 fprintf(server, "\n");
297 marshall_arguments(server, indent, func, &type_offset, PASS_OUT);
299 /* marshall the return value */
300 if (!is_void(def->type, NULL))
303 write_type(server, def->type, def, def->tname);
304 fprintf(server, " *)_StubMsg.Buffer = _RetVal;\n");
305 fprintf(server, "_StubMsg.Buffer += sizeof(");
306 write_type(server, def->type, def, def->tname);
307 fprintf(server, ");\n");
312 print_server("RpcFinally\n");
315 print_server("RpcEndFinally\n");
317 /* calculate buffer length */
318 fprintf(server, "\n");
319 print_server("_pRpcMessage->BufferLength =\n");
321 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
324 fprintf(server, "}\n");
325 fprintf(server, "\n");
327 /* update proc_offset */
331 while (NEXT_LINK(var)) var = NEXT_LINK(var);
334 proc_offset += get_size_procformatstring_var(var);
335 var = PREV_LINK(var);
338 if (!is_void(def->type, NULL))
339 proc_offset += get_size_procformatstring_var(def);
341 func = PREV_LINK(func);
346 static void write_dispatchtable(type_t *iface)
348 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
349 unsigned long method_count = 0;
350 func_t *func = iface->funcs;
352 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
355 while (NEXT_LINK(func)) func = NEXT_LINK(func);
358 var_t *def = func->def;
360 print_server("%s_", iface->name);
361 write_name(server, def);
362 fprintf(server, ",\n");
365 func = PREV_LINK(func);
369 print_server("};\n");
370 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
373 print_server("%u,\n", method_count);
374 print_server("%s_table\n", iface->name);
376 print_server("};\n");
377 fprintf(server, "\n");
381 static void write_stubdescdecl(type_t *iface)
383 print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
384 fprintf(server, "\n");
388 static void write_stubdescriptor(type_t *iface)
390 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
393 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
394 print_server("MIDL_user_allocate,\n");
395 print_server("MIDL_user_free,\n");
396 print_server("0,\n");
397 print_server("0,\n");
398 print_server("0,\n");
399 print_server("0,\n");
400 print_server("0,\n");
401 print_server("__MIDL_TypeFormatString.Format,\n");
402 print_server("1, /* -error bounds_check flag */\n");
403 print_server("0x10001, /* Ndr library version */\n");
404 print_server("0,\n");
405 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
406 print_server("0,\n");
407 print_server("0,\n");
408 print_server("0, /* notify & notify_flag routine table */\n");
409 print_server("1, /* Flags */\n");
410 print_server("0, /* Reserved3 */\n");
411 print_server("0, /* Reserved4 */\n");
412 print_server("0 /* Reserved5 */\n");
414 print_server("};\n");
415 fprintf(server, "\n");
419 static void write_serverinterfacedecl(type_t *iface)
421 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
422 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
424 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
425 fprintf(server, "\n");
426 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
429 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
430 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",
431 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
432 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
433 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
434 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
435 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
436 print_server("0,\n");
437 print_server("0,\n");
438 print_server("0,\n");
439 print_server("0,\n");
440 print_server("0,\n");
442 print_server("};\n");
443 print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
444 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
445 fprintf(server, "\n");
448 static void write_formatdesc( const char *str )
450 print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
453 print_server("short Pad;\n");
454 print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
456 print_server("} MIDL_%s_FORMAT_STRING;\n", str);
461 static void write_formatstringsdecl(type_t *iface)
465 print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
467 /* determine the proc format string size */
470 func_t *func = iface->funcs;
471 while (NEXT_LINK(func)) func = NEXT_LINK(func);
474 /* argument list size */
477 var_t *var = func->args;
478 while (NEXT_LINK(var)) var = NEXT_LINK(var);
481 byte_count += 2; /* FIXME: determine real size */
482 var = PREV_LINK(var);
486 /* return value size */
487 byte_count += 2; /* FIXME: determine real size */
488 func = PREV_LINK(func);
491 print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
493 fprintf(server, "\n");
494 write_formatdesc("TYPE");
495 write_formatdesc("PROC");
496 fprintf(server, "\n");
497 print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
498 print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
503 static void init_server(void)
507 if (!(server = fopen(server_name, "w")))
508 error("Could not open %s for output\n", server_name);
510 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
511 print_server("#include <string.h>\n");
512 fprintf(server, "\n");
513 print_server("#include \"%s\"\n", header_name);
514 fprintf(server, "\n");
518 void write_server(ifref_t *ifaces)
520 ifref_t *iface = ifaces;
534 fprintf(server, "/*****************************************************************************\n");
535 fprintf(server, " * %s interface\n", iface->iface->name);
536 fprintf(server, " */\n");
537 fprintf(server, "\n");
539 if (iface->iface->funcs)
541 write_formatstringsdecl(iface->iface);
542 write_serverinterfacedecl(iface->iface);
543 write_stubdescdecl(iface->iface);
545 write_function_stubs(iface->iface);
547 write_stubdescriptor(iface->iface);
548 write_dispatchtable(iface->iface);
551 print_server("#if !defined(__RPC_WIN32__)\n");
552 print_server("#error Invalid build platform for this stub.\n");
553 print_server("#endif\n");
554 fprintf(server, "\n");
556 write_procformatstring(server, iface->iface);
557 write_typeformatstring(server, iface->iface);
559 fprintf(server, "\n");
561 iface = PREV_LINK(iface);