wined3d: Some improvements for SFLAG_CONVERTED checking.
[wine/winequartzdrv.git] / tools / widl / server.c
blob3bcd2319ea2c676a2438bdb073d7a24c5e7fe8a2
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 <assert.h>
31 #include <ctype.h>
32 #include <signal.h>
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
39 #include "typegen.h"
41 static FILE* server;
42 static int indent = 0;
45 static void print_server(const char *format, ...)
47 va_list va;
48 va_start(va, format);
49 print(server, indent, format, va);
50 va_end(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);
57 const func_t *func;
58 const var_t *var;
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;
66 /* check for a defined binding handle */
67 explicit_handle_var = get_explicit_handle_var(func);
68 if (explicit_handle)
70 if (!explicit_handle_var)
72 error("%s() does not define an explicit binding handle!\n", def->name);
73 return;
76 else if (implicit_handle)
78 if (explicit_handle_var)
80 error("%s() must not define a binding handle!\n", def->name);
81 return;
85 fprintf(server, "void __RPC_STUB\n");
86 fprintf(server, "%s_", iface->name);
87 write_name(server, def);
88 fprintf(server, "(\n");
89 indent++;
90 print_server("PRPC_MESSAGE _pRpcMessage)\n");
91 indent--;
93 /* write the functions body */
94 fprintf(server, "{\n");
95 indent++;
97 /* Declare arguments */
98 declare_stub_args(server, indent, func);
100 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
101 print_server("RPC_STATUS _Status;\n");
102 fprintf(server, "\n");
105 print_server("((void)(_Status));\n");
106 print_server("NdrServerInitializeNew(\n");
107 indent++;
108 print_server("_pRpcMessage,\n");
109 print_server("&_StubMsg,\n");
110 print_server("&%s_StubDesc);\n", iface->name);
111 indent--;
112 fprintf(server, "\n");
114 write_parameters_init(server, indent, func);
116 if (explicit_handle_var)
118 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
119 fprintf(server, "\n");
122 print_server("RpcTryFinally\n");
123 print_server("{\n");
124 indent++;
125 print_server("RpcTryExcept\n");
126 print_server("{\n");
127 indent++;
129 if (func->args)
131 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
132 indent++;
133 print_server("NdrConvert(\n");
134 indent++;
135 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
136 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
137 indent -= 2;
138 fprintf(server, "\n");
140 /* unmarshall arguments */
141 write_remoting_arguments(server, indent, func, PASS_IN, PHASE_UNMARSHAL);
144 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
145 print_server("{\n");
146 indent++;
147 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
148 indent--;
149 print_server("}\n");
150 indent--;
151 print_server("}\n");
152 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
153 print_server("{\n");
154 indent++;
155 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
156 indent--;
157 print_server("}\n");
158 print_server("RpcEndExcept\n");
159 fprintf(server, "\n");
161 /* Assign 'out' arguments */
162 assign_stub_out_args(server, indent, func);
164 /* Call the real server function */
165 if (!is_void(def->type))
166 print_server("_RetVal = ");
167 else
168 print_server("");
169 write_prefix_name(server, prefix_server, def);
171 if (func->args)
173 int first_arg = 1;
175 fprintf(server, "(\n");
176 indent++;
177 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
179 if (first_arg)
180 first_arg = 0;
181 else
182 fprintf(server, ",\n");
183 if (is_context_handle(var->type))
185 print_server("(");
186 write_type_decl_left(server, var->type);
187 fprintf(server, ")%sNDRSContextValue(%s)", is_ptr(var->type) ? "" : "*", var->name);
189 else
191 print_server("");
192 if (var->type->declarray)
193 fprintf(server, "*");
194 write_name(server, var);
197 fprintf(server, ");\n");
198 indent--;
200 else
202 fprintf(server, "();\n");
205 if (has_out_arg_or_return(func))
207 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_BUFFERSIZE);
209 if (!is_void(def->type))
210 write_remoting_arguments(server, indent, func, PASS_RETURN, PHASE_BUFFERSIZE);
212 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
213 fprintf(server, "\n");
214 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
215 print_server("if (_Status)\n");
216 indent++;
217 print_server("RpcRaiseException(_Status);\n");
218 indent--;
219 fprintf(server, "\n");
220 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
221 fprintf(server, "\n");
224 /* marshall arguments */
225 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_MARSHAL);
227 /* marshall the return value */
228 if (!is_void(def->type))
229 write_remoting_arguments(server, indent, func, PASS_RETURN, PHASE_MARSHAL);
231 indent--;
232 print_server("}\n");
233 print_server("RpcFinally\n");
234 print_server("{\n");
235 indent++;
237 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_FREE);
239 indent--;
240 print_server("}\n");
241 print_server("RpcEndFinally\n");
243 /* calculate buffer length */
244 fprintf(server, "\n");
245 print_server("_pRpcMessage->BufferLength =\n");
246 indent++;
247 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
248 indent--;
249 indent--;
250 fprintf(server, "}\n");
251 fprintf(server, "\n");
253 /* update proc_offset */
254 *proc_offset += get_size_procformatstring_func( func );
259 static void write_dispatchtable(type_t *iface)
261 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
262 unsigned long method_count = 0;
263 const func_t *func;
265 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
266 print_server("{\n");
267 indent++;
269 if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
271 var_t *def = func->def;
273 print_server("%s_", iface->name);
274 write_name(server, def);
275 fprintf(server, ",\n");
277 method_count++;
279 print_server("0\n");
280 indent--;
281 print_server("};\n");
282 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
283 print_server("{\n");
284 indent++;
285 print_server("%u,\n", method_count);
286 print_server("%s_table\n", iface->name);
287 indent--;
288 print_server("};\n");
289 fprintf(server, "\n");
293 static void write_stubdescdecl(type_t *iface)
295 print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
296 fprintf(server, "\n");
300 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
302 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
303 print_server("{\n");
304 indent++;
305 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
306 print_server("MIDL_user_allocate,\n");
307 print_server("MIDL_user_free,\n");
308 print_server("{\n");
309 indent++;
310 print_server("0,\n");
311 indent--;
312 print_server("},\n");
313 print_server("0,\n");
314 print_server("0,\n");
315 if (expr_eval_routines)
316 print_server("ExprEvalRoutines,\n");
317 else
318 print_server("0,\n");
319 print_server("0,\n");
320 print_server("__MIDL_TypeFormatString.Format,\n");
321 print_server("1, /* -error bounds_check flag */\n");
322 print_server("0x10001, /* Ndr library version */\n");
323 print_server("0,\n");
324 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
325 print_server("0,\n");
326 print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
327 print_server("0, /* notify & notify_flag routine table */\n");
328 print_server("1, /* Flags */\n");
329 print_server("0, /* Reserved3 */\n");
330 print_server("0, /* Reserved4 */\n");
331 print_server("0 /* Reserved5 */\n");
332 indent--;
333 print_server("};\n");
334 fprintf(server, "\n");
338 static void write_serverinterfacedecl(type_t *iface)
340 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
341 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
342 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
344 if (endpoints) write_endpoints( server, iface->name, endpoints );
346 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
347 fprintf(server, "\n");
348 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
349 print_server("{\n");
350 indent++;
351 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
352 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",
353 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
354 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
355 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
356 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
357 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
358 if (endpoints)
360 print_server("%u,\n", list_count(endpoints));
361 print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
363 else
365 print_server("0,\n");
366 print_server("0,\n");
368 print_server("0,\n");
369 print_server("0,\n");
370 print_server("0,\n");
371 indent--;
372 print_server("};\n");
373 if (old_names)
374 print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
375 iface->name, iface->name);
376 else
377 print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
378 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
379 fprintf(server, "\n");
383 static void init_server(void)
385 if (server)
386 return;
387 if (!(server = fopen(server_name, "w")))
388 error("Could not open %s for output\n", server_name);
390 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
391 print_server("#include <string.h>\n");
392 fprintf(server, "\n");
393 print_server("#include \"%s\"\n", header_name);
394 fprintf(server, "\n");
398 void write_server(ifref_list_t *ifaces)
400 unsigned int proc_offset = 0;
401 int expr_eval_routines;
402 ifref_t *iface;
404 if (!do_server)
405 return;
406 if (do_everything && !need_stub_files(ifaces))
407 return;
409 init_server();
410 if (!server)
411 return;
413 write_formatstringsdecl(server, indent, ifaces, need_stub);
414 expr_eval_routines = write_expr_eval_routines(server, server_token);
415 if (expr_eval_routines)
416 write_expr_eval_routine_list(server, server_token);
417 write_user_quad_list(server);
419 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, ifref_t, entry )
421 if (!need_stub(iface->iface))
422 continue;
424 fprintf(server, "/*****************************************************************************\n");
425 fprintf(server, " * %s interface\n", iface->iface->name);
426 fprintf(server, " */\n");
427 fprintf(server, "\n");
429 if (iface->iface->funcs)
431 write_serverinterfacedecl(iface->iface);
432 write_stubdescdecl(iface->iface);
434 write_function_stubs(iface->iface, &proc_offset);
436 print_server("#if !defined(__RPC_WIN32__)\n");
437 print_server("#error Invalid build platform for this stub.\n");
438 print_server("#endif\n");
440 fprintf(server, "\n");
441 write_stubdescriptor(iface->iface, expr_eval_routines);
442 write_dispatchtable(iface->iface);
446 fprintf(server, "\n");
448 write_procformatstring(server, ifaces, need_stub);
449 write_typeformatstring(server, ifaces, need_stub);
451 fclose(server);