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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
42 static int indent = 0;
45 static void print_server(const char *format, ...)
49 print(server, indent, format, va);
53 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
55 char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
56 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
59 const var_t* explicit_handle_var;
61 if (!iface->funcs) return;
62 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
64 const var_t *def = func->def;
65 int has_full_pointer = is_full_pointer_function(func);
67 /* check for a defined binding handle */
68 explicit_handle_var = get_explicit_handle_var(func);
71 if (!explicit_handle_var)
73 error("%s() does not define an explicit binding handle!\n", def->name);
77 else if (implicit_handle)
79 if (explicit_handle_var)
81 error("%s() must not define a binding handle!\n", def->name);
86 fprintf(server, "void __RPC_STUB\n");
87 fprintf(server, "%s_", iface->name);
88 write_name(server, def);
89 fprintf(server, "(\n");
91 print_server("PRPC_MESSAGE _pRpcMessage)\n");
94 /* write the functions body */
95 fprintf(server, "{\n");
98 /* Declare arguments */
99 declare_stub_args(server, indent, func);
101 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
102 print_server("RPC_STATUS _Status;\n");
103 fprintf(server, "\n");
106 print_server("((void)(_Status));\n");
107 print_server("NdrServerInitializeNew(\n");
109 print_server("_pRpcMessage,\n");
110 print_server("&_StubMsg,\n");
111 print_server("&%s_StubDesc);\n", iface->name);
113 fprintf(server, "\n");
115 write_parameters_init(server, indent, func);
117 if (explicit_handle_var)
119 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
120 fprintf(server, "\n");
123 print_server("RpcTryFinally\n");
126 print_server("RpcTryExcept\n");
130 if (has_full_pointer)
131 write_full_pointer_init(server, indent, func, TRUE);
135 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
137 print_server("NdrConvert(\n");
139 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
140 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
142 fprintf(server, "\n");
144 /* unmarshall arguments */
145 write_remoting_arguments(server, indent, func, PASS_IN, PHASE_UNMARSHAL);
148 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
151 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
156 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
159 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
162 print_server("RpcEndExcept\n");
163 fprintf(server, "\n");
165 /* Assign 'out' arguments */
166 assign_stub_out_args(server, indent, func);
168 /* Call the real server function */
169 if (!is_void(def->type))
170 print_server("_RetVal = ");
173 write_prefix_name(server, prefix_server, def);
179 fprintf(server, "(\n");
181 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
186 fprintf(server, ",\n");
187 if (is_context_handle(var->type))
190 write_type_decl_left(server, var->type);
191 fprintf(server, ")%sNDRSContextValue(%s)", is_ptr(var->type) ? "" : "*", var->name);
196 if (var->type->declarray)
197 fprintf(server, "*");
198 write_name(server, var);
201 fprintf(server, ");\n");
206 fprintf(server, "();\n");
209 if (has_out_arg_or_return(func))
211 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_BUFFERSIZE);
213 if (!is_void(def->type))
214 write_remoting_arguments(server, indent, func, PASS_RETURN, PHASE_BUFFERSIZE);
216 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
217 fprintf(server, "\n");
218 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
219 print_server("if (_Status)\n");
221 print_server("RpcRaiseException(_Status);\n");
223 fprintf(server, "\n");
224 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
225 fprintf(server, "\n");
228 /* marshall arguments */
229 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_MARSHAL);
231 /* marshall the return value */
232 if (!is_void(def->type))
233 write_remoting_arguments(server, indent, func, PASS_RETURN, PHASE_MARSHAL);
237 print_server("RpcFinally\n");
241 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_FREE);
243 if (has_full_pointer)
244 write_full_pointer_free(server, indent, func);
248 print_server("RpcEndFinally\n");
250 /* calculate buffer length */
251 fprintf(server, "\n");
252 print_server("_pRpcMessage->BufferLength =\n");
254 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
257 fprintf(server, "}\n");
258 fprintf(server, "\n");
260 /* update proc_offset */
261 *proc_offset += get_size_procformatstring_func( func );
266 static void write_dispatchtable(type_t *iface)
268 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
269 unsigned long method_count = 0;
272 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
276 if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
278 var_t *def = func->def;
280 print_server("%s_", iface->name);
281 write_name(server, def);
282 fprintf(server, ",\n");
288 print_server("};\n");
289 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
292 print_server("%u,\n", method_count);
293 print_server("%s_table\n", iface->name);
295 print_server("};\n");
296 fprintf(server, "\n");
300 static void write_stubdescdecl(type_t *iface)
302 print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
303 fprintf(server, "\n");
307 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
309 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
312 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
313 print_server("MIDL_user_allocate,\n");
314 print_server("MIDL_user_free,\n");
317 print_server("0,\n");
319 print_server("},\n");
320 print_server("0,\n");
321 print_server("0,\n");
322 if (expr_eval_routines)
323 print_server("ExprEvalRoutines,\n");
325 print_server("0,\n");
326 print_server("0,\n");
327 print_server("__MIDL_TypeFormatString.Format,\n");
328 print_server("1, /* -error bounds_check flag */\n");
329 print_server("0x10001, /* Ndr library version */\n");
330 print_server("0,\n");
331 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
332 print_server("0,\n");
333 print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
334 print_server("0, /* notify & notify_flag routine table */\n");
335 print_server("1, /* Flags */\n");
336 print_server("0, /* Reserved3 */\n");
337 print_server("0, /* Reserved4 */\n");
338 print_server("0 /* Reserved5 */\n");
340 print_server("};\n");
341 fprintf(server, "\n");
345 static void write_serverinterfacedecl(type_t *iface)
347 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
348 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
349 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
351 if (endpoints) write_endpoints( server, iface->name, endpoints );
353 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
354 fprintf(server, "\n");
355 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
358 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
359 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",
360 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
361 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
362 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
363 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
364 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
367 print_server("%u,\n", list_count(endpoints));
368 print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
372 print_server("0,\n");
373 print_server("0,\n");
375 print_server("0,\n");
376 print_server("0,\n");
377 print_server("0,\n");
379 print_server("};\n");
381 print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
382 iface->name, iface->name);
384 print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
385 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
386 fprintf(server, "\n");
390 static void init_server(void)
394 if (!(server = fopen(server_name, "w")))
395 error("Could not open %s for output\n", server_name);
397 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
398 print_server("#include <string.h>\n");
399 fprintf(server, "\n");
400 print_server("#include \"%s\"\n", header_name);
401 fprintf(server, "\n");
405 void write_server(ifref_list_t *ifaces)
407 unsigned int proc_offset = 0;
408 int expr_eval_routines;
413 if (do_everything && !need_stub_files(ifaces))
420 write_formatstringsdecl(server, indent, ifaces, need_stub);
421 expr_eval_routines = write_expr_eval_routines(server, server_token);
422 if (expr_eval_routines)
423 write_expr_eval_routine_list(server, server_token);
424 write_user_quad_list(server);
426 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, ifref_t, entry )
428 if (!need_stub(iface->iface))
431 fprintf(server, "/*****************************************************************************\n");
432 fprintf(server, " * %s interface\n", iface->iface->name);
433 fprintf(server, " */\n");
434 fprintf(server, "\n");
436 if (iface->iface->funcs)
438 write_serverinterfacedecl(iface->iface);
439 write_stubdescdecl(iface->iface);
441 write_function_stubs(iface->iface, &proc_offset);
443 print_server("#if !defined(__RPC_WIN32__)\n");
444 print_server("#error Invalid build platform for this stub.\n");
445 print_server("#endif\n");
447 fprintf(server, "\n");
448 write_stubdescriptor(iface->iface, expr_eval_routines);
449 write_dispatchtable(iface->iface);
453 fprintf(server, "\n");
455 write_procformatstring(server, ifaces, need_stub);
456 write_typeformatstring(server, ifaces, need_stub);