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 #include "typelib_struct.h"
46 static int indent
= 0;
49 static int print_server(const char *format
, ...)
55 if (format
[0] != '\n')
56 for (i
= 0; i
< indent
; i
++)
58 r
= vfprintf(server
, format
, va
);
64 static void write_parameters_init(const func_t
*func
)
71 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
72 if (var
->type
->type
!= RPC_FC_BIND_PRIMITIVE
)
73 print_server("%s = 0;\n", var
->name
);
75 fprintf(server
, "\n");
79 static void write_function_stubs(type_t
*iface
, unsigned int *proc_offset
)
81 char *implicit_handle
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
82 int explicit_handle
= is_attr(iface
->attrs
, ATTR_EXPLICIT_HANDLE
);
85 const var_t
* explicit_handle_var
;
87 if (!iface
->funcs
) return;
88 LIST_FOR_EACH_ENTRY( func
, iface
->funcs
, const func_t
, entry
)
90 const var_t
*def
= func
->def
;
92 /* check for a defined binding handle */
93 explicit_handle_var
= get_explicit_handle_var(func
);
96 if (!explicit_handle_var
)
98 error("%s() does not define an explicit binding handle!\n", def
->name
);
102 else if (implicit_handle
)
104 if (explicit_handle_var
)
106 error("%s() must not define a binding handle!\n", def
->name
);
111 fprintf(server
, "void __RPC_STUB\n");
112 fprintf(server
, "%s_", iface
->name
);
113 write_name(server
, def
);
114 fprintf(server
, "(\n");
116 print_server("PRPC_MESSAGE _pRpcMessage)\n");
119 /* write the functions body */
120 fprintf(server
, "{\n");
123 /* Declare arguments */
124 declare_stub_args(server
, indent
, func
);
126 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
127 print_server("RPC_STATUS _Status;\n");
128 fprintf(server
, "\n");
131 print_server("((void)(_Status));\n");
132 print_server("NdrServerInitializeNew(\n");
134 print_server("_pRpcMessage,\n");
135 print_server("&_StubMsg,\n");
136 print_server("&%s_StubDesc);\n", iface
->name
);
138 fprintf(server
, "\n");
140 write_parameters_init(func
);
142 if (explicit_handle_var
)
144 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var
->name
);
145 fprintf(server
, "\n");
148 print_server("RpcTryFinally\n");
151 print_server("RpcTryExcept\n");
157 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
159 print_server("NdrConvert(\n");
161 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
162 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset
);
164 fprintf(server
, "\n");
166 /* unmarshall arguments */
167 write_remoting_arguments(server
, indent
, func
, PASS_IN
, PHASE_UNMARSHAL
);
170 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
173 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
178 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
181 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
184 print_server("RpcEndExcept\n");
185 fprintf(server
, "\n");
187 /* Assign 'out' arguments */
188 assign_stub_out_args(server
, indent
, func
);
190 /* Call the real server function */
191 if (!is_void(def
->type
))
192 print_server("_RetVal = ");
195 write_prefix_name(server
, prefix_server
, def
);
201 fprintf(server
, "(\n");
203 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
208 fprintf(server
, ",\n");
211 fprintf(server
, "*");
212 write_name(server
, var
);
214 fprintf(server
, ");\n");
219 fprintf(server
, "();\n");
222 if (has_out_arg_or_return(func
))
224 write_remoting_arguments(server
, indent
, func
, PASS_OUT
, PHASE_BUFFERSIZE
);
226 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
227 fprintf(server
, "\n");
228 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
229 print_server("if (_Status)\n");
231 print_server("RpcRaiseException(_Status);\n");
233 fprintf(server
, "\n");
234 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
235 fprintf(server
, "\n");
238 /* marshall arguments */
239 write_remoting_arguments(server
, indent
, func
, PASS_OUT
, PHASE_MARSHAL
);
241 /* marshall the return value */
242 if (!is_void(def
->type
))
243 print_phase_basetype(server
, indent
, PHASE_MARSHAL
, PASS_RETURN
, def
, "_RetVal");
247 print_server("RpcFinally\n");
251 write_remoting_arguments(server
, indent
, func
, PASS_OUT
, PHASE_FREE
);
255 print_server("RpcEndFinally\n");
257 /* calculate buffer length */
258 fprintf(server
, "\n");
259 print_server("_pRpcMessage->BufferLength =\n");
261 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
264 fprintf(server
, "}\n");
265 fprintf(server
, "\n");
267 /* update proc_offset */
268 *proc_offset
+= get_size_procformatstring_func( func
);
273 static void write_dispatchtable(type_t
*iface
)
275 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
276 unsigned long method_count
= 0;
279 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface
->name
);
283 if (iface
->funcs
) LIST_FOR_EACH_ENTRY( func
, iface
->funcs
, const func_t
, entry
)
285 var_t
*def
= func
->def
;
287 print_server("%s_", iface
->name
);
288 write_name(server
, def
);
289 fprintf(server
, ",\n");
295 print_server("};\n");
296 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
299 print_server("%u,\n", method_count
);
300 print_server("%s_table\n", iface
->name
);
302 print_server("};\n");
303 fprintf(server
, "\n");
307 static void write_stubdescdecl(type_t
*iface
)
309 print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface
->name
);
310 fprintf(server
, "\n");
314 static void write_stubdescriptor(type_t
*iface
, int expr_eval_routines
)
316 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface
->name
);
319 print_server("(void *)& %s___RpcServerInterface,\n", iface
->name
);
320 print_server("MIDL_user_allocate,\n");
321 print_server("MIDL_user_free,\n");
324 print_server("0,\n");
326 print_server("},\n");
327 print_server("0,\n");
328 print_server("0,\n");
329 if (expr_eval_routines
)
330 print_server("ExprEvalRoutines,\n");
332 print_server("0,\n");
333 print_server("0,\n");
334 print_server("__MIDL_TypeFormatString.Format,\n");
335 print_server("1, /* -error bounds_check flag */\n");
336 print_server("0x10001, /* Ndr library version */\n");
337 print_server("0,\n");
338 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
339 print_server("0,\n");
340 print_server("0,\n");
341 print_server("0, /* notify & notify_flag routine table */\n");
342 print_server("1, /* Flags */\n");
343 print_server("0, /* Reserved3 */\n");
344 print_server("0, /* Reserved4 */\n");
345 print_server("0 /* Reserved5 */\n");
347 print_server("};\n");
348 fprintf(server
, "\n");
352 static void write_serverinterfacedecl(type_t
*iface
)
354 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
355 UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
356 const str_list_t
*endpoints
= get_attrp(iface
->attrs
, ATTR_ENDPOINT
);
358 if (endpoints
) write_endpoints( server
, iface
->name
, endpoints
);
360 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
361 fprintf(server
, "\n");
362 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface
->name
);
365 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
366 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",
367 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
368 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6],
369 uuid
->Data4
[7], LOWORD(ver
), HIWORD(ver
));
370 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
371 print_server("&%s_v%d_%d_DispatchTable,\n", iface
->name
, LOWORD(ver
), HIWORD(ver
));
374 print_server("%u,\n", list_count(endpoints
));
375 print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface
->name
);
379 print_server("0,\n");
380 print_server("0,\n");
382 print_server("0,\n");
383 print_server("0,\n");
384 print_server("0,\n");
386 print_server("};\n");
388 print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
389 iface
->name
, iface
->name
);
391 print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
392 prefix_server
, iface
->name
, LOWORD(ver
), HIWORD(ver
), iface
->name
);
393 fprintf(server
, "\n");
397 static void init_server(void)
401 if (!(server
= fopen(server_name
, "w")))
402 error("Could not open %s for output\n", server_name
);
404 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION
, input_name
);
405 print_server("#include <string.h>\n");
406 fprintf(server
, "\n");
407 print_server("#include \"%s\"\n", header_name
);
408 fprintf(server
, "\n");
412 void write_server(ifref_list_t
*ifaces
)
414 unsigned int proc_offset
= 0;
419 if (do_everything
&& !ifaces
)
426 write_formatstringsdecl(server
, indent
, ifaces
, 0);
428 if (ifaces
) LIST_FOR_EACH_ENTRY( iface
, ifaces
, ifref_t
, entry
)
430 if (is_object(iface
->iface
->attrs
) || is_local(iface
->iface
->attrs
))
433 fprintf(server
, "/*****************************************************************************\n");
434 fprintf(server
, " * %s interface\n", iface
->iface
->name
);
435 fprintf(server
, " */\n");
436 fprintf(server
, "\n");
438 if (iface
->iface
->funcs
)
440 int expr_eval_routines
;
442 write_serverinterfacedecl(iface
->iface
);
443 write_stubdescdecl(iface
->iface
);
445 write_function_stubs(iface
->iface
, &proc_offset
);
447 print_server("#if !defined(__RPC_WIN32__)\n");
448 print_server("#error Invalid build platform for this stub.\n");
449 print_server("#endif\n");
451 fprintf(server
, "\n");
453 expr_eval_routines
= write_expr_eval_routines(server
, iface
->iface
->name
);
454 if (expr_eval_routines
)
455 write_expr_eval_routine_list(server
, iface
->iface
->name
);
457 write_stubdescriptor(iface
->iface
, expr_eval_routines
);
458 write_dispatchtable(iface
->iface
);
462 fprintf(server
, "\n");
464 write_procformatstring(server
, ifaces
, 0);
465 write_typeformatstring(server
, ifaces
, 0);