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 declare_args(const func_t *func)
93 int in_attr, out_attr;
101 while (NEXT_LINK(var)) var = NEXT_LINK(var);
104 in_attr = is_attr(var->attrs, ATTR_IN);
105 out_attr = is_attr(var->attrs, ATTR_OUT);
106 if (!out_attr && !in_attr)
109 if (!in_attr && !is_attr(var->attrs, ATTR_STRING))
112 write_type(server, var->type, NULL, var->tname);
113 fprintf(server, " _W%u;\n", i++);
117 write_type(server, var->type, var, var->tname);
118 fprintf(server, " ");
119 write_name(server, var);
120 write_array(server, var->array, 0);
121 fprintf(server, ";\n");
123 var = PREV_LINK(var);
128 static void assign_out_args(const func_t *func)
130 int in_attr, out_attr;
133 const expr_t *size_is;
140 while (NEXT_LINK(var)) var = NEXT_LINK(var);
143 size_is = get_attrp(var->attrs, ATTR_SIZEIS);
144 has_size = size_is && (size_is->type != EXPR_VOID);
145 in_attr = is_attr(var->attrs, ATTR_IN);
146 out_attr = is_attr(var->attrs, ATTR_OUT);
147 if (!out_attr && !in_attr)
153 write_name(server, var);
157 type_t *type = var->type;
158 while (type->type == 0 && type->ref)
161 fprintf(server, " = NdrAllocate(&_StubMsg, ");
162 write_expr(server, size_is, 1);
163 fprintf(server, " * %u);\n", get_type_memsize(type));
167 fprintf(server, " = &_W%u;\n", i++);
173 var = PREV_LINK(var);
177 fprintf(server, "\n");
181 static void write_function_stubs(type_t *iface)
183 char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
184 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
185 const func_t *func = iface->funcs;
187 const var_t* explicit_handle_var;
188 unsigned int proc_offset = 0;
189 unsigned int type_offset = 2;
191 while (NEXT_LINK(func)) func = NEXT_LINK(func);
194 const var_t *def = func->def;
195 unsigned long buffer_size = 0;
196 unsigned int type_offset_func;
198 /* check for a defined binding handle */
199 explicit_handle_var = get_explicit_handle_var(func);
202 if (!explicit_handle_var)
204 error("%s() does not define an explicit binding handle!\n", def->name);
208 else if (implicit_handle)
210 if (explicit_handle_var)
212 error("%s() must not define a binding handle!\n", def->name);
217 fprintf(server, "void __RPC_STUB\n");
218 fprintf(server, "%s_", iface->name);
219 write_name(server, def);
220 fprintf(server, "(\n");
222 print_server("PRPC_MESSAGE _pRpcMessage)\n");
225 /* write the functions body */
226 fprintf(server, "{\n");
229 /* declare return value '_RetVal' */
230 if (!is_void(def->type, NULL))
233 write_type(server, def->type, def, def->tname);
234 fprintf(server, " _RetVal;\n");
237 /* Declare arguments */
240 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
241 print_server("RPC_STATUS _Status;\n");
242 fprintf(server, "\n");
245 print_server("((void)(_Status));\n");
246 print_server("NdrServerInitializeNew(\n");
248 print_server("_pRpcMessage,\n");
249 print_server("&_StubMsg,\n");
250 print_server("&%s_StubDesc);\n", iface->name);
252 fprintf(server, "\n");
254 write_parameters_init(func);
256 if (explicit_handle_var)
258 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
259 fprintf(server, "\n");
262 print_server("RpcTryFinally\n");
265 print_server("RpcTryExcept\n");
271 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
273 print_server("NdrConvert(\n");
275 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
276 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
278 fprintf(server, "\n");
280 /* make a copy so we don't increment the type offset twice */
281 type_offset_func = type_offset;
283 /* unmarshall arguments */
284 write_remoting_arguments(server, indent, func, &type_offset_func, PASS_IN, PHASE_UNMARSHAL);
287 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
290 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
295 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
298 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
301 print_server("RpcEndExcept\n");
302 fprintf(server, "\n");
304 /* Assign 'out' arguments */
305 assign_out_args(func);
307 /* Call the real server function */
308 if (!is_void(def->type, NULL))
309 print_server("_RetVal = ");
312 write_name(server, def);
318 fprintf(server, "(\n");
321 while (NEXT_LINK(var)) var = NEXT_LINK(var);
327 fprintf(server, ",\n");
329 write_name(server, var);
330 var = PREV_LINK(var);
332 fprintf(server, ");\n");
337 fprintf(server, "();\n");
342 const var_t *var = func->args;
343 while (NEXT_LINK(var)) var = NEXT_LINK(var);
346 if (is_attr(var->attrs, ATTR_OUT))
348 unsigned int alignment;
349 buffer_size += get_required_buffer_size(var, &alignment);
350 buffer_size += alignment;
353 var = PREV_LINK(var);
357 if (!is_void(def->type, NULL))
359 unsigned int alignment;
360 buffer_size += get_required_buffer_size(def, &alignment);
361 buffer_size += alignment;
364 if (has_out_arg_or_return(func))
366 fprintf(server, "\n");
367 print_server("_StubMsg.BufferLength = %u;\n", buffer_size);
369 type_offset_func = type_offset;
370 write_remoting_arguments(server, indent, func, &type_offset_func, PASS_OUT, PHASE_BUFFERSIZE);
372 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
373 fprintf(server, "\n");
374 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
375 print_server("if (_Status)\n");
377 print_server("RpcRaiseException(_Status);\n");
379 fprintf(server, "\n");
380 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
381 fprintf(server, "\n");
384 type_offset_func = type_offset;
386 /* marshall arguments */
387 write_remoting_arguments(server, indent, func, &type_offset, PASS_OUT, PHASE_MARSHAL);
389 /* marshall the return value */
390 if (!is_void(def->type, NULL))
392 unsigned int alignment;
393 unsigned int size = get_required_buffer_size(def, &alignment);
394 print_server("_StubMsg.Buffer += (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
397 write_type(server, def->type, def, def->tname);
398 fprintf(server, " *)_StubMsg.Buffer = _RetVal;\n");
399 print_server("_StubMsg.Buffer += sizeof(");
400 write_type(server, def->type, def, def->tname);
401 fprintf(server, ");\n");
406 print_server("RpcFinally\n");
410 write_remoting_arguments(server, indent, func, &type_offset_func, PASS_OUT, PHASE_FREE);
414 print_server("RpcEndFinally\n");
416 /* calculate buffer length */
417 fprintf(server, "\n");
418 print_server("_pRpcMessage->BufferLength =\n");
420 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
423 fprintf(server, "}\n");
424 fprintf(server, "\n");
426 /* update proc_offset */
430 while (NEXT_LINK(var)) var = NEXT_LINK(var);
433 proc_offset += get_size_procformatstring_var(var);
434 var = PREV_LINK(var);
437 if (!is_void(def->type, NULL))
438 proc_offset += get_size_procformatstring_var(def);
440 proc_offset += 2; /* FC_END and FC_PAD */
442 func = PREV_LINK(func);
447 static void write_dispatchtable(type_t *iface)
449 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
450 unsigned long method_count = 0;
451 func_t *func = iface->funcs;
453 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
456 while (NEXT_LINK(func)) func = NEXT_LINK(func);
459 var_t *def = func->def;
461 print_server("%s_", iface->name);
462 write_name(server, def);
463 fprintf(server, ",\n");
466 func = PREV_LINK(func);
470 print_server("};\n");
471 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
474 print_server("%u,\n", method_count);
475 print_server("%s_table\n", iface->name);
477 print_server("};\n");
478 fprintf(server, "\n");
482 static void write_stubdescdecl(type_t *iface)
484 print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
485 fprintf(server, "\n");
489 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
491 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
494 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
495 print_server("MIDL_user_allocate,\n");
496 print_server("MIDL_user_free,\n");
499 print_server("0,\n");
501 print_server("},\n");
502 print_server("0,\n");
503 print_server("0,\n");
504 if (expr_eval_routines)
505 print_server("ExprEvalRoutines,\n");
507 print_server("0,\n");
508 print_server("0,\n");
509 print_server("__MIDL_TypeFormatString.Format,\n");
510 print_server("1, /* -error bounds_check flag */\n");
511 print_server("0x10001, /* Ndr library version */\n");
512 print_server("0,\n");
513 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
514 print_server("0,\n");
515 print_server("0,\n");
516 print_server("0, /* notify & notify_flag routine table */\n");
517 print_server("1, /* Flags */\n");
518 print_server("0, /* Reserved3 */\n");
519 print_server("0, /* Reserved4 */\n");
520 print_server("0 /* Reserved5 */\n");
522 print_server("};\n");
523 fprintf(server, "\n");
527 static void write_serverinterfacedecl(type_t *iface)
529 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
530 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
532 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
533 fprintf(server, "\n");
534 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
537 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
538 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",
539 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
540 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
541 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
542 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
543 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
544 print_server("0,\n");
545 print_server("0,\n");
546 print_server("0,\n");
547 print_server("0,\n");
548 print_server("0,\n");
550 print_server("};\n");
551 print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
552 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
553 fprintf(server, "\n");
556 static void write_formatdesc( const char *str )
558 print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
561 print_server("short Pad;\n");
562 print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
564 print_server("} MIDL_%s_FORMAT_STRING;\n", str);
569 static void write_formatstringsdecl(type_t *iface)
571 print_server("#define TYPE_FORMAT_STRING_SIZE %d\n",
572 get_size_typeformatstring(iface));
574 print_server("#define PROC_FORMAT_STRING_SIZE %d\n",
575 get_size_procformatstring(iface));
577 fprintf(server, "\n");
578 write_formatdesc("TYPE");
579 write_formatdesc("PROC");
580 fprintf(server, "\n");
581 print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
582 print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
587 static void init_server(void)
591 if (!(server = fopen(server_name, "w")))
592 error("Could not open %s for output\n", server_name);
594 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
595 print_server("#include <string.h>\n");
596 fprintf(server, "\n");
597 print_server("#include \"%s\"\n", header_name);
598 fprintf(server, "\n");
602 void write_server(ifref_t *ifaces)
604 ifref_t *iface = ifaces;
616 for (; iface; iface = PREV_LINK(iface))
618 if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
621 fprintf(server, "/*****************************************************************************\n");
622 fprintf(server, " * %s interface\n", iface->iface->name);
623 fprintf(server, " */\n");
624 fprintf(server, "\n");
626 if (iface->iface->funcs)
628 int expr_eval_routines;
630 write_formatstringsdecl(iface->iface);
631 write_serverinterfacedecl(iface->iface);
632 write_stubdescdecl(iface->iface);
634 write_function_stubs(iface->iface);
636 print_server("#if !defined(__RPC_WIN32__)\n");
637 print_server("#error Invalid build platform for this stub.\n");
638 print_server("#endif\n");
639 fprintf(server, "\n");
641 write_procformatstring(server, iface->iface);
642 write_typeformatstring(server, iface->iface);
644 fprintf(server, "\n");
646 expr_eval_routines = write_expr_eval_routines(server, iface->iface->name);
647 if (expr_eval_routines)
648 write_expr_eval_routine_list(server, iface->iface->name);
650 write_stubdescriptor(iface->iface, expr_eval_routines);
651 write_dispatchtable(iface->iface);