shell32: Fix and simplify the FO_COPY operation, with tests.
[wine.git] / tools / widl / server.c
blob2efb712b2570b8d51cb8cab18c5a16c7c5d2d8b9
1 /*
2 * IDL Compiler
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <ctype.h>
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
37 #include "typegen.h"
39 static FILE* server;
40 static int indent = 0;
43 static void print_server(const char *format, ...)
45 va_list va;
46 va_start(va, format);
47 print(server, indent, format, va);
48 va_end(va);
51 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
53 char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
54 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
55 const func_t *func;
56 const var_t *var;
57 const var_t* explicit_handle_var;
59 if (!iface->funcs) return;
60 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
62 const var_t *def = func->def;
63 int has_full_pointer = is_full_pointer_function(func);
65 /* check for a defined binding handle */
66 explicit_handle_var = get_explicit_handle_var(func);
67 if (explicit_handle)
69 if (!explicit_handle_var)
71 error("%s() does not define an explicit binding handle!\n", def->name);
72 return;
75 else if (implicit_handle)
77 if (explicit_handle_var)
79 error("%s() must not define a binding handle!\n", def->name);
80 return;
84 fprintf(server, "void __RPC_STUB\n");
85 fprintf(server, "%s_", iface->name);
86 write_name(server, def);
87 fprintf(server, "(\n");
88 indent++;
89 print_server("PRPC_MESSAGE _pRpcMessage)\n");
90 indent--;
92 /* write the functions body */
93 fprintf(server, "{\n");
94 indent++;
96 /* Declare arguments */
97 declare_stub_args(server, indent, func);
99 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
100 print_server("RPC_STATUS _Status;\n");
101 fprintf(server, "\n");
104 print_server("((void)(_Status));\n");
105 print_server("NdrServerInitializeNew(\n");
106 indent++;
107 print_server("_pRpcMessage,\n");
108 print_server("&_StubMsg,\n");
109 print_server("&%s_StubDesc);\n", iface->name);
110 indent--;
111 fprintf(server, "\n");
113 write_parameters_init(server, indent, func);
115 if (explicit_handle_var)
117 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
118 fprintf(server, "\n");
121 print_server("RpcTryFinally\n");
122 print_server("{\n");
123 indent++;
124 print_server("RpcTryExcept\n");
125 print_server("{\n");
126 indent++;
128 if (has_full_pointer)
129 write_full_pointer_init(server, indent, func, TRUE);
131 if (func->args)
133 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
134 indent++;
135 print_server("NdrConvert(\n");
136 indent++;
137 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
138 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
139 indent -= 2;
140 fprintf(server, "\n");
142 /* unmarshall arguments */
143 write_remoting_arguments(server, indent, func, PASS_IN, PHASE_UNMARSHAL);
146 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
147 print_server("{\n");
148 indent++;
149 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
150 indent--;
151 print_server("}\n");
152 indent--;
153 print_server("}\n");
154 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
155 print_server("{\n");
156 indent++;
157 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
158 indent--;
159 print_server("}\n");
160 print_server("RpcEndExcept\n");
161 fprintf(server, "\n");
163 /* Assign 'out' arguments */
164 assign_stub_out_args(server, indent, func);
166 /* Call the real server function */
167 if (!is_void(get_func_return_type(func)))
168 print_server("_RetVal = ");
169 else
170 print_server("");
171 write_prefix_name(server, prefix_server, def);
173 if (func->args)
175 int first_arg = 1;
177 fprintf(server, "(\n");
178 indent++;
179 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
181 if (first_arg)
182 first_arg = 0;
183 else
184 fprintf(server, ",\n");
185 if (is_context_handle(var->type))
187 /* if the context_handle attribute appears in the chain of types
188 * without pointers being followed, then the context handle must
189 * be direct, otherwise it is a pointer */
190 int is_ch_ptr = is_aliaschain_attr(var->type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
191 print_server("(");
192 write_type_decl_left(server, var->type);
193 fprintf(server, ")%sNDRSContextValue(%s)", is_ch_ptr ? "" : "*", var->name);
195 else
197 print_server("");
198 if (var->type->declarray)
199 fprintf(server, "*");
200 write_name(server, var);
203 fprintf(server, ");\n");
204 indent--;
206 else
208 fprintf(server, "();\n");
211 if (has_out_arg_or_return(func))
213 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_BUFFERSIZE);
215 if (!is_void(get_func_return_type(func)))
216 write_remoting_arguments(server, indent, func, PASS_RETURN, PHASE_BUFFERSIZE);
218 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
219 fprintf(server, "\n");
220 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
221 print_server("if (_Status)\n");
222 indent++;
223 print_server("RpcRaiseException(_Status);\n");
224 indent--;
225 fprintf(server, "\n");
226 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
227 fprintf(server, "\n");
230 /* marshall arguments */
231 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_MARSHAL);
233 /* marshall the return value */
234 if (!is_void(get_func_return_type(func)))
235 write_remoting_arguments(server, indent, func, PASS_RETURN, PHASE_MARSHAL);
237 indent--;
238 print_server("}\n");
239 print_server("RpcFinally\n");
240 print_server("{\n");
241 indent++;
243 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_FREE);
245 if (has_full_pointer)
246 write_full_pointer_free(server, indent, func);
248 indent--;
249 print_server("}\n");
250 print_server("RpcEndFinally\n");
252 /* calculate buffer length */
253 fprintf(server, "\n");
254 print_server("_pRpcMessage->BufferLength =\n");
255 indent++;
256 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
257 indent--;
258 indent--;
259 fprintf(server, "}\n");
260 fprintf(server, "\n");
262 /* update proc_offset */
263 *proc_offset += get_size_procformatstring_func( 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 const func_t *func;
274 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
275 print_server("{\n");
276 indent++;
278 if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
280 var_t *def = func->def;
282 print_server("%s_", iface->name);
283 write_name(server, def);
284 fprintf(server, ",\n");
286 method_count++;
288 print_server("0\n");
289 indent--;
290 print_server("};\n");
291 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
292 print_server("{\n");
293 indent++;
294 print_server("%u,\n", method_count);
295 print_server("%s_table\n", iface->name);
296 indent--;
297 print_server("};\n");
298 fprintf(server, "\n");
302 static void write_stubdescdecl(type_t *iface)
304 print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
305 fprintf(server, "\n");
309 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
311 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
312 print_server("{\n");
313 indent++;
314 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
315 print_server("MIDL_user_allocate,\n");
316 print_server("MIDL_user_free,\n");
317 print_server("{\n");
318 indent++;
319 print_server("0,\n");
320 indent--;
321 print_server("},\n");
322 print_server("0,\n");
323 print_server("0,\n");
324 if (expr_eval_routines)
325 print_server("ExprEvalRoutines,\n");
326 else
327 print_server("0,\n");
328 print_server("0,\n");
329 print_server("__MIDL_TypeFormatString.Format,\n");
330 print_server("1, /* -error bounds_check flag */\n");
331 print_server("0x10001, /* Ndr library version */\n");
332 print_server("0,\n");
333 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
334 print_server("0,\n");
335 print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
336 print_server("0, /* notify & notify_flag routine table */\n");
337 print_server("1, /* Flags */\n");
338 print_server("0, /* Reserved3 */\n");
339 print_server("0, /* Reserved4 */\n");
340 print_server("0 /* Reserved5 */\n");
341 indent--;
342 print_server("};\n");
343 fprintf(server, "\n");
347 static void write_serverinterfacedecl(type_t *iface)
349 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
350 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
351 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
353 if (endpoints) write_endpoints( server, iface->name, endpoints );
355 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
356 fprintf(server, "\n");
357 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
358 print_server("{\n");
359 indent++;
360 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
361 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",
362 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
363 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
364 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
365 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
366 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
367 if (endpoints)
369 print_server("%u,\n", list_count(endpoints));
370 print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
372 else
374 print_server("0,\n");
375 print_server("0,\n");
377 print_server("0,\n");
378 print_server("0,\n");
379 print_server("0,\n");
380 indent--;
381 print_server("};\n");
382 if (old_names)
383 print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
384 iface->name, iface->name);
385 else
386 print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
387 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
388 fprintf(server, "\n");
392 static void init_server(void)
394 if (server)
395 return;
396 if (!(server = fopen(server_name, "w")))
397 error("Could not open %s for output\n", server_name);
399 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
400 print_server("#include <string.h>\n");
401 fprintf(server, "\n");
402 print_server("#include \"%s\"\n", header_name);
403 fprintf(server, "\n");
407 void write_server(ifref_list_t *ifaces)
409 unsigned int proc_offset = 0;
410 int expr_eval_routines;
411 ifref_t *iface;
413 if (!do_server)
414 return;
415 if (do_everything && !need_stub_files(ifaces))
416 return;
418 init_server();
419 if (!server)
420 return;
422 write_formatstringsdecl(server, indent, ifaces, need_stub);
423 expr_eval_routines = write_expr_eval_routines(server, server_token);
424 if (expr_eval_routines)
425 write_expr_eval_routine_list(server, server_token);
426 write_user_quad_list(server);
428 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, ifref_t, entry )
430 if (!need_stub(iface->iface))
431 continue;
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 write_serverinterfacedecl(iface->iface);
441 write_stubdescdecl(iface->iface);
443 write_function_stubs(iface->iface, &proc_offset);
445 print_server("#if !defined(__RPC_WIN32__)\n");
446 print_server("#error Invalid build platform for this stub.\n");
447 print_server("#endif\n");
449 fprintf(server, "\n");
450 write_stubdescriptor(iface->iface, expr_eval_routines);
451 write_dispatchtable(iface->iface);
455 fprintf(server, "\n");
457 write_procformatstring(server, ifaces, need_stub);
458 write_typeformatstring(server, ifaces, need_stub);
460 fclose(server);