winebuild: Add a -ordinal flag for entry points that must be imported by ordinal...
[wine/multimedia.git] / tools / widl / server.c
blob49ceb69603de5c54ffea9ab6ef9f1d6174a86fd6
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"
38 #include "windef.h"
40 #include "widl.h"
41 #include "typelib.h"
42 #include "typelib_struct.h"
43 #include "typegen.h"
45 static FILE* server;
46 static int indent = 0;
49 static void print_server(const char *format, ...)
51 va_list va;
52 va_start(va, format);
53 print(server, indent, format, va);
54 va_end(va);
57 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
59 char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
60 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
61 const func_t *func;
62 const var_t *var;
63 const var_t* explicit_handle_var;
65 if (!iface->funcs) return;
66 LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
68 const var_t *def = func->def;
70 /* check for a defined binding handle */
71 explicit_handle_var = get_explicit_handle_var(func);
72 if (explicit_handle)
74 if (!explicit_handle_var)
76 error("%s() does not define an explicit binding handle!\n", def->name);
77 return;
80 else if (implicit_handle)
82 if (explicit_handle_var)
84 error("%s() must not define a binding handle!\n", def->name);
85 return;
89 fprintf(server, "void __RPC_STUB\n");
90 fprintf(server, "%s_", iface->name);
91 write_name(server, def);
92 fprintf(server, "(\n");
93 indent++;
94 print_server("PRPC_MESSAGE _pRpcMessage)\n");
95 indent--;
97 /* write the functions body */
98 fprintf(server, "{\n");
99 indent++;
101 /* Declare arguments */
102 declare_stub_args(server, indent, func);
104 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
105 print_server("RPC_STATUS _Status;\n");
106 fprintf(server, "\n");
109 print_server("((void)(_Status));\n");
110 print_server("NdrServerInitializeNew(\n");
111 indent++;
112 print_server("_pRpcMessage,\n");
113 print_server("&_StubMsg,\n");
114 print_server("&%s_StubDesc);\n", iface->name);
115 indent--;
116 fprintf(server, "\n");
118 write_parameters_init(server, indent, func);
120 if (explicit_handle_var)
122 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
123 fprintf(server, "\n");
126 print_server("RpcTryFinally\n");
127 print_server("{\n");
128 indent++;
129 print_server("RpcTryExcept\n");
130 print_server("{\n");
131 indent++;
133 if (func->args)
135 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
136 indent++;
137 print_server("NdrConvert(\n");
138 indent++;
139 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
140 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
141 indent -= 2;
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");
149 print_server("{\n");
150 indent++;
151 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
152 indent--;
153 print_server("}\n");
154 indent--;
155 print_server("}\n");
156 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
157 print_server("{\n");
158 indent++;
159 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
160 indent--;
161 print_server("}\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 = ");
171 else
172 print_server("");
173 write_prefix_name(server, prefix_server, def);
175 if (func->args)
177 int first_arg = 1;
179 fprintf(server, "(\n");
180 indent++;
181 LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
183 if (first_arg)
184 first_arg = 0;
185 else
186 fprintf(server, ",\n");
187 if (is_context_handle(var->type))
189 print_server("(");
190 write_type_decl_left(server, var->type);
191 fprintf(server, ")%sNDRSContextValue(%s)", is_ptr(var->type) ? "" : "*", var->name);
193 else
195 print_server("");
196 if (var->type->declarray)
197 fprintf(server, "*");
198 write_name(server, var);
201 fprintf(server, ");\n");
202 indent--;
204 else
206 fprintf(server, "();\n");
209 if (has_out_arg_or_return(func))
211 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_BUFFERSIZE);
213 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
214 fprintf(server, "\n");
215 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
216 print_server("if (_Status)\n");
217 indent++;
218 print_server("RpcRaiseException(_Status);\n");
219 indent--;
220 fprintf(server, "\n");
221 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
222 fprintf(server, "\n");
225 /* marshall arguments */
226 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_MARSHAL);
228 /* marshall the return value */
229 if (!is_void(def->type))
230 print_phase_basetype(server, indent, PHASE_MARSHAL, PASS_RETURN, def, "_RetVal");
232 indent--;
233 print_server("}\n");
234 print_server("RpcFinally\n");
235 print_server("{\n");
236 indent++;
238 write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_FREE);
240 indent--;
241 print_server("}\n");
242 print_server("RpcEndFinally\n");
244 /* calculate buffer length */
245 fprintf(server, "\n");
246 print_server("_pRpcMessage->BufferLength =\n");
247 indent++;
248 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
249 indent--;
250 indent--;
251 fprintf(server, "}\n");
252 fprintf(server, "\n");
254 /* update proc_offset */
255 *proc_offset += get_size_procformatstring_func( func );
260 static void write_dispatchtable(type_t *iface)
262 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
263 unsigned long method_count = 0;
264 const func_t *func;
266 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
267 print_server("{\n");
268 indent++;
270 if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
272 var_t *def = func->def;
274 print_server("%s_", iface->name);
275 write_name(server, def);
276 fprintf(server, ",\n");
278 method_count++;
280 print_server("0\n");
281 indent--;
282 print_server("};\n");
283 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
284 print_server("{\n");
285 indent++;
286 print_server("%u,\n", method_count);
287 print_server("%s_table\n", iface->name);
288 indent--;
289 print_server("};\n");
290 fprintf(server, "\n");
294 static void write_stubdescdecl(type_t *iface)
296 print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
297 fprintf(server, "\n");
301 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
303 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
304 print_server("{\n");
305 indent++;
306 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
307 print_server("MIDL_user_allocate,\n");
308 print_server("MIDL_user_free,\n");
309 print_server("{\n");
310 indent++;
311 print_server("0,\n");
312 indent--;
313 print_server("},\n");
314 print_server("0,\n");
315 print_server("0,\n");
316 if (expr_eval_routines)
317 print_server("ExprEvalRoutines,\n");
318 else
319 print_server("0,\n");
320 print_server("0,\n");
321 print_server("__MIDL_TypeFormatString.Format,\n");
322 print_server("1, /* -error bounds_check flag */\n");
323 print_server("0x10001, /* Ndr library version */\n");
324 print_server("0,\n");
325 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
326 print_server("0,\n");
327 print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
328 print_server("0, /* notify & notify_flag routine table */\n");
329 print_server("1, /* Flags */\n");
330 print_server("0, /* Reserved3 */\n");
331 print_server("0, /* Reserved4 */\n");
332 print_server("0 /* Reserved5 */\n");
333 indent--;
334 print_server("};\n");
335 fprintf(server, "\n");
339 static void write_serverinterfacedecl(type_t *iface)
341 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
342 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
343 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
345 if (endpoints) write_endpoints( server, iface->name, endpoints );
347 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
348 fprintf(server, "\n");
349 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
350 print_server("{\n");
351 indent++;
352 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
353 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",
354 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
355 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
356 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
357 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
358 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
359 if (endpoints)
361 print_server("%u,\n", list_count(endpoints));
362 print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
364 else
366 print_server("0,\n");
367 print_server("0,\n");
369 print_server("0,\n");
370 print_server("0,\n");
371 print_server("0,\n");
372 indent--;
373 print_server("};\n");
374 if (old_names)
375 print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
376 iface->name, iface->name);
377 else
378 print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
379 prefix_server, iface->name, LOWORD(ver), HIWORD(ver), iface->name);
380 fprintf(server, "\n");
384 static void init_server(void)
386 if (server)
387 return;
388 if (!(server = fopen(server_name, "w")))
389 error("Could not open %s for output\n", server_name);
391 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
392 print_server("#include <string.h>\n");
393 fprintf(server, "\n");
394 print_server("#include \"%s\"\n", header_name);
395 fprintf(server, "\n");
399 void write_server(ifref_list_t *ifaces)
401 unsigned int proc_offset = 0;
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);
415 if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, ifref_t, entry )
417 if (!need_stub(iface->iface))
418 continue;
420 fprintf(server, "/*****************************************************************************\n");
421 fprintf(server, " * %s interface\n", iface->iface->name);
422 fprintf(server, " */\n");
423 fprintf(server, "\n");
425 if (iface->iface->funcs)
427 int expr_eval_routines;
429 write_serverinterfacedecl(iface->iface);
430 write_stubdescdecl(iface->iface);
432 write_function_stubs(iface->iface, &proc_offset);
434 print_server("#if !defined(__RPC_WIN32__)\n");
435 print_server("#error Invalid build platform for this stub.\n");
436 print_server("#endif\n");
438 fprintf(server, "\n");
440 expr_eval_routines = write_expr_eval_routines(server, iface->iface->name);
441 if (expr_eval_routines)
442 write_expr_eval_routine_list(server, iface->iface->name);
444 write_user_quad_list(server);
445 write_stubdescriptor(iface->iface, expr_eval_routines);
446 write_dispatchtable(iface->iface);
450 fprintf(server, "\n");
452 write_procformatstring(server, ifaces, need_stub);
453 write_typeformatstring(server, ifaces, need_stub);
455 fclose(server);