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"
44 #define END_OF_LIST(list) \
47 while (NEXT_LINK(list)) \
48 list = NEXT_LINK(list); \
53 static int indent = 0;
56 static int print_server(const char *format, ...)
62 for (i = 0; i < indent; i++)
64 r = vfprintf(server, format, va);
70 static void write_procformatstring(type_t *iface)
72 func_t *func = iface->funcs;
74 print_server("static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
81 while (NEXT_LINK(func)) func = NEXT_LINK(func);
84 var_t *def = func->def;
86 if (is_void(def->type, NULL))
88 print_server("0x5b, /* FC_END */\n");
89 print_server("0x5c, /* FC_PAD */\n");
93 print_server("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
94 print_server("0x%02x, /* <type> */\n", def->type->type);
97 func = PREV_LINK(func);
100 print_server("0x0\n");
104 print_server("};\n");
109 static void write_typeformatstring(void)
111 print_server("static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
114 print_server("0,\n");
117 print_server("NdrFcShort(0x0),\n");
118 print_server("0x0\n");
122 print_server("};\n");
127 static unsigned int get_required_stack_size(type_t *type)
144 error("Unknown/unsupported type: %s\n", type->name);
150 static void write_function_stubs(type_t *iface)
152 func_t *func = iface->funcs;
153 while (NEXT_LINK(func)) func = NEXT_LINK(func);
156 var_t *def = func->def;
158 write_type(server, def->type, def, def->tname);
159 fprintf(server, " __RPC_STUB\n");
160 fprintf(server, "%s_", iface->name);
161 write_name(server, def);
162 fprintf(server, "(\n");
164 print_server("PRPC_MESSAGE _pRpcMessage)\n");
167 /* write the functions body */
168 fprintf(server, "{\n");
171 /* declare return value '_RetVal' */
172 if (!is_void(def->type, NULL))
175 write_type(server, def->type, def, def->tname);
176 fprintf(server, " _RetVal;\n");
179 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
180 print_server("RPC_STATUS _Status;\n");
181 fprintf(server, "\n");
182 print_server("((void)(_Status));\n");
183 print_server("NdrServerInitializeNew(\n");
185 print_server("_pRpcMessage,\n");
186 print_server("&_StubMsg,\n");
187 print_server("&%s_StubDesc);\n", iface->name);
189 fprintf(server, "\n");
191 print_server("RpcTryFinally\n");
194 print_server("RpcTryExcept\n");
197 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
200 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
205 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
208 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
211 print_server("RpcEndExcept\n");
212 fprintf(server, "\n");
215 /* Call the real server function */
216 if (!is_void(def->type, NULL))
217 print_server("_RetVal = ");
220 write_name(server, def);
222 /* FIXME: handle argument list */
223 fprintf(server, "();\n");
225 /* FIXME: Marshall the return value */
226 if (!is_void(def->type, NULL))
228 fprintf(server, "\n");
229 print_server("_StubMsg.BufferLength = %uU;\n", get_required_stack_size(def->type));
230 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
231 fprintf(server, "\n");
232 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
233 print_server("if (_Status)\n");
235 print_server("RpcRaiseException(_Status);\n");
237 fprintf(server, "\n");
238 print_server("_StubMsg.Buffer = (unsigned char __RPC_FAR *)_pRpcMessage->Buffer;\n");
239 fprintf(server, "\n");
242 write_type(server, def->type, def, def->tname);
243 fprintf(server, " __RPC_FAR *)_StubMsg.Buffer)++ = _RetVal;\n");
248 print_server("RpcFinally\n");
251 print_server("RpcEndFinally\n");
253 /* calculate buffer length */
254 fprintf(server, "\n");
255 print_server("_pRpcMessage->BufferLength =\n");
257 print_server("(unsigned int)((long)_StubMsg.Buffer - (long)_pRpcMessage->Buffer);\n");
260 fprintf(server, "}\n");
261 fprintf(server, "\n");
263 func = PREV_LINK(func);
268 static void write_dispatchtable(type_t *iface)
270 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
271 unsigned long method_count = 0;
272 func_t *func = iface->funcs;
274 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
277 while (NEXT_LINK(func)) func = NEXT_LINK(func);
280 var_t *def = func->def;
282 print_server("%s_", iface->name);
283 write_name(server, def);
284 fprintf(server, ",\n");
287 func = PREV_LINK(func);
291 print_server("};\n");
292 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
295 print_server("%u,\n", method_count);
296 print_server("%s_table\n", iface->name);
298 print_server("};\n");
299 fprintf(server, "\n");
303 static void write_stubdescdecl(type_t *iface)
305 print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
306 fprintf(server, "\n");
310 static void write_stubdescriptor(type_t *iface)
312 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
315 print_server("(void __RPC_FAR *)& %s___RpcServerInterface,\n", iface->name);
316 print_server("MIDL_user_allocate,\n");
317 print_server("MIDL_user_free,\n");
318 print_server("0,\n");
319 print_server("0,\n");
320 print_server("0,\n");
321 print_server("0,\n");
322 print_server("0,\n");
323 print_server("__MIDL_TypeFormatString.Format,\n");
324 print_server("1, /* -error bounds_check flag */\n");
325 print_server("0x10001, /* Ndr library version */\n");
326 print_server("0,\n");
327 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
328 print_server("0,\n");
329 print_server("0,\n");
330 print_server("0, /* notify & notify_flag routine table */\n");
331 print_server("1, /* Flags */\n");
332 print_server("0, /* Reserved3 */\n");
333 print_server("0, /* Reserved4 */\n");
334 print_server("0 /* Reserved5 */\n");
336 print_server("};\n");
337 fprintf(server, "\n");
341 static void write_serverinterfacedecl(type_t *iface)
343 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
344 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
346 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
347 fprintf(server, "\n");
348 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
351 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
352 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",
353 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
354 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
355 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
356 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
357 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
358 print_server("0,\n");
359 print_server("0,\n");
360 print_server("0,\n");
361 print_server("0,\n");
362 print_server("0,\n");
364 print_server("};\n");
365 print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
366 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
367 fprintf(server, "\n");
370 static void write_formatdesc( const char *str )
372 print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
375 print_server("short Pad;\n");
376 print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
378 print_server("} MIDL_%s_FORMAT_STRING;\n", str);
383 static void write_formatstringsdecl(type_t *iface)
388 print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
390 /* determine the proc format string size */
392 while (NEXT_LINK(func)) func = NEXT_LINK(func);
395 byte_count += 2; /* FIXME: determine real size */
396 func = PREV_LINK(func);
398 print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
400 fprintf(server, "\n");
401 write_formatdesc("TYPE");
402 write_formatdesc("PROC");
403 fprintf(server, "\n");
404 print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
405 print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
410 static void init_server(void)
414 if (!(server = fopen(server_name, "w")))
415 error("Could not open %s for output\n", server_name);
417 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
418 print_server("#include <string.h>\n");
419 fprintf(server, "\n");
420 print_server("#include \"%s\"\n", header_name);
421 fprintf(server, "\n");
425 void write_server(ifref_t *ifaces)
427 ifref_t *lcur = ifaces;
439 write_formatstringsdecl(lcur->iface);
440 write_serverinterfacedecl(lcur->iface);
441 write_stubdescdecl(lcur->iface);
443 write_function_stubs(lcur->iface);
445 write_stubdescriptor(lcur->iface);
446 write_dispatchtable(lcur->iface);
448 print_server("#if !defined(__RPC_WIN32__)\n");
449 print_server("#error Invalid build platform for this stub.\n");
450 print_server("#endif\n");
451 fprintf(server, "\n");
453 write_procformatstring(lcur->iface);
454 write_typeformatstring();
456 fprintf(server, "\n");