widl: Add helper functions that return the size of procformat and
[wine/wine64.git] / tools / widl / server.c
blobb007a8f98228088dd149a2c35bd0f03bbdd4faa8
1 /*
2 * IDL Compiler
4 * Copyright 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 #define END_OF_LIST(list) \
46 do { \
47 if (list) { \
48 while (NEXT_LINK(list)) \
49 list = NEXT_LINK(list); \
50 } \
51 } while(0)
53 static FILE* server;
54 static int indent = 0;
57 static int print_server(const char *format, ...)
59 va_list va;
60 int i, r;
62 va_start(va, format);
63 for (i = 0; i < indent; i++)
64 fprintf(server, " ");
65 r = vfprintf(server, format, va);
66 va_end(va);
67 return r;
71 static void write_parameters_init(func_t *func)
73 var_t *var;
75 if (!func->args)
76 return;
78 var = func->args;
79 while (NEXT_LINK(var)) var = NEXT_LINK(var);
80 while (var)
82 print_server("%s = 0;\n", var->name);
84 var = PREV_LINK(var);
86 fprintf(server, "\n");
90 static void write_function_stubs(type_t *iface)
92 char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
93 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
94 func_t *func = iface->funcs;
95 var_t *var;
96 var_t* explicit_handle_var;
97 unsigned int proc_offset = 0;
99 while (NEXT_LINK(func)) func = NEXT_LINK(func);
100 while (func)
102 var_t *def = func->def;
104 /* check for a defined binding handle */
105 explicit_handle_var = get_explicit_handle_var(func);
106 if (explicit_handle)
108 if (!explicit_handle_var)
110 error("%s() does not define an explicit binding handle!\n", def->name);
111 return;
114 else if (implicit_handle)
116 if (explicit_handle_var)
118 error("%s() must not define a binding handle!\n", def->name);
119 return;
123 fprintf(server, "void __RPC_STUB\n");
124 fprintf(server, "%s_", iface->name);
125 write_name(server, def);
126 fprintf(server, "(\n");
127 indent++;
128 print_server("PRPC_MESSAGE _pRpcMessage)\n");
129 indent--;
131 /* write the functions body */
132 fprintf(server, "{\n");
133 indent++;
135 /* declare return value '_RetVal' */
136 if (!is_void(def->type, NULL))
138 print_server("");
139 write_type(server, def->type, def, def->tname);
140 fprintf(server, " _RetVal;\n");
143 /* declare arguments */
144 if (func->args)
146 var = func->args;
147 while (NEXT_LINK(var)) var = NEXT_LINK(var);
148 while (var)
150 print_server("");
151 write_type(server, var->type, var, var->tname);
152 fprintf(server, " ");
153 write_name(server, var);
154 fprintf(server, ";\n");
156 var = PREV_LINK(var);
160 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
161 print_server("RPC_STATUS _Status;\n");
162 fprintf(server, "\n");
165 print_server("((void)(_Status));\n");
166 print_server("NdrServerInitializeNew(\n");
167 indent++;
168 print_server("_pRpcMessage,\n");
169 print_server("&_StubMsg,\n");
170 print_server("&%s_StubDesc);\n", iface->name);
171 indent--;
172 fprintf(server, "\n");
174 write_parameters_init(func);
176 if (explicit_handle_var)
178 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
179 fprintf(server, "\n");
182 print_server("RpcTryFinally\n");
183 print_server("{\n");
184 indent++;
185 print_server("RpcTryExcept\n");
186 print_server("{\n");
187 indent++;
189 if (func->args)
191 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
192 indent++;
193 print_server("NdrConvert(\n");
194 indent++;
195 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
196 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
197 indent -= 2;
198 fprintf(server, "\n");
200 unmarshall_arguments(server, indent, func);
203 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
204 print_server("{\n");
205 indent++;
206 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
207 indent--;
208 print_server("}\n");
209 indent--;
210 print_server("}\n");
211 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
212 print_server("{\n");
213 indent++;
214 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
215 indent--;
216 print_server("}\n");
217 print_server("RpcEndExcept\n");
218 fprintf(server, "\n");
221 /* Call the real server function */
222 if (!is_void(def->type, NULL))
223 print_server("_RetVal = ");
224 else
225 print_server("");
226 write_name(server, def);
228 if (func->args)
230 int first_arg = 1;
232 fprintf(server, "(\n");
233 indent++;
234 var = func->args;
235 while (NEXT_LINK(var)) var = NEXT_LINK(var);
236 while (var)
238 if (first_arg)
239 first_arg = 0;
240 else
241 fprintf(server, ",\n");
242 print_server("");
243 write_name(server, var);
244 var = PREV_LINK(var);
246 fprintf(server, ");\n");
247 indent--;
249 else
251 fprintf(server, "();\n");
254 /* marshall the return value */
255 if (!is_void(def->type, NULL))
257 fprintf(server, "\n");
258 print_server("_StubMsg.BufferLength = %uU;\n", get_required_buffer_size(def->type));
259 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
260 fprintf(server, "\n");
261 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
262 print_server("if (_Status)\n");
263 indent++;
264 print_server("RpcRaiseException(_Status);\n");
265 indent--;
266 fprintf(server, "\n");
267 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
268 fprintf(server, "\n");
270 print_server("*(");
271 write_type(server, def->type, def, def->tname);
272 fprintf(server, " *)_StubMsg.Buffer = _RetVal;\n");
273 fprintf(server, "_StubMsg.Buffer += sizeof(");
274 write_type(server, def->type, def, def->tname);
275 fprintf(server, ");\n");
278 indent--;
279 print_server("}\n");
280 print_server("RpcFinally\n");
281 print_server("{\n");
282 print_server("}\n");
283 print_server("RpcEndFinally\n");
285 /* calculate buffer length */
286 fprintf(server, "\n");
287 print_server("_pRpcMessage->BufferLength =\n");
288 indent++;
289 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
290 indent--;
291 indent--;
292 fprintf(server, "}\n");
293 fprintf(server, "\n");
295 /* update proc_offset */
296 if (func->args)
298 var = func->args;
299 while (NEXT_LINK(var)) var = NEXT_LINK(var);
300 while (var)
302 proc_offset += get_size_procformatstring_var(var);
303 var = PREV_LINK(var);
306 if (!is_void(def->type, NULL))
307 proc_offset += get_size_procformatstring_var(def);
309 func = PREV_LINK(func);
314 static void write_dispatchtable(type_t *iface)
316 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
317 unsigned long method_count = 0;
318 func_t *func = iface->funcs;
320 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
321 print_server("{\n");
322 indent++;
323 while (NEXT_LINK(func)) func = NEXT_LINK(func);
324 while (func)
326 var_t *def = func->def;
328 print_server("%s_", iface->name);
329 write_name(server, def);
330 fprintf(server, ",\n");
332 method_count++;
333 func = PREV_LINK(func);
335 print_server("0\n");
336 indent--;
337 print_server("};\n");
338 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
339 print_server("{\n");
340 indent++;
341 print_server("%u,\n", method_count);
342 print_server("%s_table\n", iface->name);
343 indent--;
344 print_server("};\n");
345 fprintf(server, "\n");
349 static void write_stubdescdecl(type_t *iface)
351 print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
352 fprintf(server, "\n");
356 static void write_stubdescriptor(type_t *iface)
358 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
359 print_server("{\n");
360 indent++;
361 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
362 print_server("MIDL_user_allocate,\n");
363 print_server("MIDL_user_free,\n");
364 print_server("0,\n");
365 print_server("0,\n");
366 print_server("0,\n");
367 print_server("0,\n");
368 print_server("0,\n");
369 print_server("__MIDL_TypeFormatString.Format,\n");
370 print_server("1, /* -error bounds_check flag */\n");
371 print_server("0x10001, /* Ndr library version */\n");
372 print_server("0,\n");
373 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
374 print_server("0,\n");
375 print_server("0,\n");
376 print_server("0, /* notify & notify_flag routine table */\n");
377 print_server("1, /* Flags */\n");
378 print_server("0, /* Reserved3 */\n");
379 print_server("0, /* Reserved4 */\n");
380 print_server("0 /* Reserved5 */\n");
381 indent--;
382 print_server("};\n");
383 fprintf(server, "\n");
387 static void write_serverinterfacedecl(type_t *iface)
389 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
390 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
392 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
393 fprintf(server, "\n");
394 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
395 print_server("{\n");
396 indent++;
397 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
398 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",
399 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
400 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
401 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
402 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
403 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
404 print_server("0,\n");
405 print_server("0,\n");
406 print_server("0,\n");
407 print_server("0,\n");
408 print_server("0,\n");
409 indent--;
410 print_server("};\n");
411 print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
412 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
413 fprintf(server, "\n");
416 static void write_formatdesc( const char *str )
418 print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
419 print_server("{\n");
420 indent++;
421 print_server("short Pad;\n");
422 print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
423 indent--;
424 print_server("} MIDL_%s_FORMAT_STRING;\n", str);
425 print_server("\n");
429 static void write_formatstringsdecl(type_t *iface)
431 func_t *func;
432 var_t *var;
433 int byte_count = 1;
435 print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
437 /* determine the proc format string size */
438 func = iface->funcs;
439 while (NEXT_LINK(func)) func = NEXT_LINK(func);
440 while (func)
442 /* argument list size */
443 if (func->args)
445 var = func->args;
446 while (NEXT_LINK(var)) var = NEXT_LINK(var);
447 while (var)
449 byte_count += 2; /* FIXME: determine real size */
450 var = PREV_LINK(var);
454 /* return value size */
455 byte_count += 2; /* FIXME: determine real size */
456 func = PREV_LINK(func);
458 print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
460 fprintf(server, "\n");
461 write_formatdesc("TYPE");
462 write_formatdesc("PROC");
463 fprintf(server, "\n");
464 print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
465 print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
466 print_server("\n");
470 static void init_server(void)
472 if (server)
473 return;
474 if (!(server = fopen(server_name, "w")))
475 error("Could not open %s for output\n", server_name);
477 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
478 print_server("#include <string.h>\n");
479 fprintf(server, "\n");
480 print_server("#include \"%s\"\n", header_name);
481 fprintf(server, "\n");
485 void write_server(ifref_t *ifaces)
487 ifref_t *iface = ifaces;
489 if (!do_server)
490 return;
491 if (!iface)
492 return;
493 END_OF_LIST(iface);
495 init_server();
496 if (!server)
497 return;
499 while (iface)
501 fprintf(server, "/*****************************************************************************\n");
502 fprintf(server, " * %s interface\n", iface->iface->name);
503 fprintf(server, " */\n");
504 fprintf(server, "\n");
506 write_formatstringsdecl(iface->iface);
507 write_serverinterfacedecl(iface->iface);
508 write_stubdescdecl(iface->iface);
510 write_function_stubs(iface->iface);
512 write_stubdescriptor(iface->iface);
513 write_dispatchtable(iface->iface);
515 print_server("#if !defined(__RPC_WIN32__)\n");
516 print_server("#error Invalid build platform for this stub.\n");
517 print_server("#endif\n");
518 fprintf(server, "\n");
520 write_procformatstring(server, iface->iface);
521 write_typeformatstring(server, iface->iface);
523 fprintf(server, "\n");
525 iface = PREV_LINK(iface);
528 fclose(server);