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;
99 while (NEXT_LINK(func
)) func
= NEXT_LINK(func
);
102 var_t
*def
= func
->def
;
104 /* check for a defined binding handle */
105 explicit_handle_var
= get_explicit_handle_var(func
);
108 if (!explicit_handle_var
)
110 error("%s() does not define an explicit binding handle!\n", def
->name
);
114 else if (implicit_handle
)
116 if (explicit_handle_var
)
118 error("%s() must not define a binding handle!\n", def
->name
);
123 fprintf(server
, "void __RPC_STUB\n");
124 fprintf(server
, "%s_", iface
->name
);
125 write_name(server
, def
);
126 fprintf(server
, "(\n");
128 print_server("PRPC_MESSAGE _pRpcMessage)\n");
131 /* write the functions body */
132 fprintf(server
, "{\n");
135 /* declare return value '_RetVal' */
136 if (!is_void(def
->type
, NULL
))
139 write_type(server
, def
->type
, def
, def
->tname
);
140 fprintf(server
, " _RetVal;\n");
143 /* declare arguments */
147 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
151 write_type(server
, var
->type
, var
, var
->tname
);
152 fprintf(server
, " ");
153 write_name(server
, var
);
154 fprintf(server
, ";\n");
156 var
= PREV_LINK(var
);
160 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
161 print_server("RPC_STATUS _Status;\n");
162 fprintf(server
, "\n");
165 print_server("((void)(_Status));\n");
166 print_server("NdrServerInitializeNew(\n");
168 print_server("_pRpcMessage,\n");
169 print_server("&_StubMsg,\n");
170 print_server("&%s_StubDesc);\n", iface
->name
);
172 fprintf(server
, "\n");
174 write_parameters_init(func
);
176 if (explicit_handle_var
)
178 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var
->name
);
179 fprintf(server
, "\n");
182 print_server("RpcTryFinally\n");
185 print_server("RpcTryExcept\n");
191 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
193 print_server("NdrConvert(\n");
195 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
196 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
198 fprintf(server
, "\n");
200 unmarshall_arguments(server
, indent
, func
);
203 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
206 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
211 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
214 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
217 print_server("RpcEndExcept\n");
218 fprintf(server
, "\n");
221 /* Call the real server function */
222 if (!is_void(def
->type
, NULL
))
223 print_server("_RetVal = ");
226 write_name(server
, def
);
232 fprintf(server
, "(\n");
235 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
241 fprintf(server
, ",\n");
243 write_name(server
, var
);
244 var
= PREV_LINK(var
);
246 fprintf(server
, ");\n");
251 fprintf(server
, "();\n");
254 /* marshall the return value */
255 if (!is_void(def
->type
, NULL
))
257 fprintf(server
, "\n");
258 print_server("_StubMsg.BufferLength = %uU;\n", get_required_buffer_size(def
->type
));
259 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
260 fprintf(server
, "\n");
261 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
262 print_server("if (_Status)\n");
264 print_server("RpcRaiseException(_Status);\n");
266 fprintf(server
, "\n");
267 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
268 fprintf(server
, "\n");
271 write_type(server
, def
->type
, def
, def
->tname
);
272 fprintf(server
, " *)_StubMsg.Buffer = _RetVal;\n");
273 fprintf(server
, "_StubMsg.Buffer += sizeof(");
274 write_type(server
, def
->type
, def
, def
->tname
);
275 fprintf(server
, ");\n");
280 print_server("RpcFinally\n");
283 print_server("RpcEndFinally\n");
285 /* calculate buffer length */
286 fprintf(server
, "\n");
287 print_server("_pRpcMessage->BufferLength =\n");
289 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
292 fprintf(server
, "}\n");
293 fprintf(server
, "\n");
295 /* update proc_offset */
299 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
302 proc_offset
+= get_size_procformatstring_var(var
);
303 var
= PREV_LINK(var
);
306 if (!is_void(def
->type
, NULL
))
307 proc_offset
+= get_size_procformatstring_var(def
);
309 func
= PREV_LINK(func
);
314 static void write_dispatchtable(type_t
*iface
)
316 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
317 unsigned long method_count
= 0;
318 func_t
*func
= iface
->funcs
;
320 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface
->name
);
323 while (NEXT_LINK(func
)) func
= NEXT_LINK(func
);
326 var_t
*def
= func
->def
;
328 print_server("%s_", iface
->name
);
329 write_name(server
, def
);
330 fprintf(server
, ",\n");
333 func
= PREV_LINK(func
);
337 print_server("};\n");
338 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
341 print_server("%u,\n", method_count
);
342 print_server("%s_table\n", iface
->name
);
344 print_server("};\n");
345 fprintf(server
, "\n");
349 static void write_stubdescdecl(type_t
*iface
)
351 print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface
->name
);
352 fprintf(server
, "\n");
356 static void write_stubdescriptor(type_t
*iface
)
358 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface
->name
);
361 print_server("(void *)& %s___RpcServerInterface,\n", iface
->name
);
362 print_server("MIDL_user_allocate,\n");
363 print_server("MIDL_user_free,\n");
364 print_server("0,\n");
365 print_server("0,\n");
366 print_server("0,\n");
367 print_server("0,\n");
368 print_server("0,\n");
369 print_server("__MIDL_TypeFormatString.Format,\n");
370 print_server("1, /* -error bounds_check flag */\n");
371 print_server("0x10001, /* Ndr library version */\n");
372 print_server("0,\n");
373 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
374 print_server("0,\n");
375 print_server("0,\n");
376 print_server("0, /* notify & notify_flag routine table */\n");
377 print_server("1, /* Flags */\n");
378 print_server("0, /* Reserved3 */\n");
379 print_server("0, /* Reserved4 */\n");
380 print_server("0 /* Reserved5 */\n");
382 print_server("};\n");
383 fprintf(server
, "\n");
387 static void write_serverinterfacedecl(type_t
*iface
)
389 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
390 UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
392 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
393 fprintf(server
, "\n");
394 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface
->name
);
397 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
398 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",
399 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
400 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6],
401 uuid
->Data4
[7], LOWORD(ver
), HIWORD(ver
));
402 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
403 print_server("&%s_v%d_%d_DispatchTable,\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
404 print_server("0,\n");
405 print_server("0,\n");
406 print_server("0,\n");
407 print_server("0,\n");
408 print_server("0,\n");
410 print_server("};\n");
411 print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
412 iface
->name
, LOWORD(ver
), HIWORD(ver
), iface
->name
);
413 fprintf(server
, "\n");
416 static void write_formatdesc( const char *str
)
418 print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
421 print_server("short Pad;\n");
422 print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
424 print_server("} MIDL_%s_FORMAT_STRING;\n", str
);
429 static void write_formatstringsdecl(type_t
*iface
)
435 print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
437 /* determine the proc format string size */
439 while (NEXT_LINK(func
)) func
= NEXT_LINK(func
);
442 /* argument list size */
446 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
449 byte_count
+= 2; /* FIXME: determine real size */
450 var
= PREV_LINK(var
);
454 /* return value size */
455 byte_count
+= 2; /* FIXME: determine real size */
456 func
= PREV_LINK(func
);
458 print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count
);
460 fprintf(server
, "\n");
461 write_formatdesc("TYPE");
462 write_formatdesc("PROC");
463 fprintf(server
, "\n");
464 print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
465 print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
470 static void init_server(void)
474 if (!(server
= fopen(server_name
, "w")))
475 error("Could not open %s for output\n", server_name
);
477 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION
, input_name
);
478 print_server("#include <string.h>\n");
479 fprintf(server
, "\n");
480 print_server("#include \"%s\"\n", header_name
);
481 fprintf(server
, "\n");
485 void write_server(ifref_t
*ifaces
)
487 ifref_t
*iface
= ifaces
;
501 fprintf(server
, "/*****************************************************************************\n");
502 fprintf(server
, " * %s interface\n", iface
->iface
->name
);
503 fprintf(server
, " */\n");
504 fprintf(server
, "\n");
506 write_formatstringsdecl(iface
->iface
);
507 write_serverinterfacedecl(iface
->iface
);
508 write_stubdescdecl(iface
->iface
);
510 write_function_stubs(iface
->iface
);
512 write_stubdescriptor(iface
->iface
);
513 write_dispatchtable(iface
->iface
);
515 print_server("#if !defined(__RPC_WIN32__)\n");
516 print_server("#error Invalid build platform for this stub.\n");
517 print_server("#endif\n");
518 fprintf(server
, "\n");
520 write_procformatstring(server
, iface
->iface
);
521 write_typeformatstring(server
, iface
->iface
);
523 fprintf(server
, "\n");
525 iface
= PREV_LINK(iface
);