widl: Add support for marshalling and unmarshalling conformant strings.
[wine.git] / tools / widl / server.c
blob005d85bded0e45682f95e8e6a500e872aa7ada23
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;
98 unsigned int type_offset = 2;
100 while (NEXT_LINK(func)) func = NEXT_LINK(func);
101 while (func)
103 var_t *def = func->def;
105 /* check for a defined binding handle */
106 explicit_handle_var = get_explicit_handle_var(func);
107 if (explicit_handle)
109 if (!explicit_handle_var)
111 error("%s() does not define an explicit binding handle!\n", def->name);
112 return;
115 else if (implicit_handle)
117 if (explicit_handle_var)
119 error("%s() must not define a binding handle!\n", def->name);
120 return;
124 fprintf(server, "void __RPC_STUB\n");
125 fprintf(server, "%s_", iface->name);
126 write_name(server, def);
127 fprintf(server, "(\n");
128 indent++;
129 print_server("PRPC_MESSAGE _pRpcMessage)\n");
130 indent--;
132 /* write the functions body */
133 fprintf(server, "{\n");
134 indent++;
136 /* declare return value '_RetVal' */
137 if (!is_void(def->type, NULL))
139 print_server("");
140 write_type(server, def->type, def, def->tname);
141 fprintf(server, " _RetVal;\n");
144 /* declare arguments */
145 if (func->args)
147 var = func->args;
148 while (NEXT_LINK(var)) var = NEXT_LINK(var);
149 while (var)
151 print_server("");
152 write_type(server, var->type, var, var->tname);
153 fprintf(server, " ");
154 write_name(server, var);
155 fprintf(server, ";\n");
157 var = PREV_LINK(var);
161 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
162 print_server("RPC_STATUS _Status;\n");
163 fprintf(server, "\n");
166 print_server("((void)(_Status));\n");
167 print_server("NdrServerInitializeNew(\n");
168 indent++;
169 print_server("_pRpcMessage,\n");
170 print_server("&_StubMsg,\n");
171 print_server("&%s_StubDesc);\n", iface->name);
172 indent--;
173 fprintf(server, "\n");
175 write_parameters_init(func);
177 if (explicit_handle_var)
179 print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
180 fprintf(server, "\n");
183 print_server("RpcTryFinally\n");
184 print_server("{\n");
185 indent++;
186 print_server("RpcTryExcept\n");
187 print_server("{\n");
188 indent++;
190 if (func->args)
192 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
193 indent++;
194 print_server("NdrConvert(\n");
195 indent++;
196 print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
197 print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
198 indent -= 2;
199 fprintf(server, "\n");
201 unmarshall_arguments(server, indent, func, &type_offset);
204 print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
205 print_server("{\n");
206 indent++;
207 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
208 indent--;
209 print_server("}\n");
210 indent--;
211 print_server("}\n");
212 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
213 print_server("{\n");
214 indent++;
215 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
216 indent--;
217 print_server("}\n");
218 print_server("RpcEndExcept\n");
219 fprintf(server, "\n");
222 /* Call the real server function */
223 if (!is_void(def->type, NULL))
224 print_server("_RetVal = ");
225 else
226 print_server("");
227 write_name(server, def);
229 if (func->args)
231 int first_arg = 1;
233 fprintf(server, "(\n");
234 indent++;
235 var = func->args;
236 while (NEXT_LINK(var)) var = NEXT_LINK(var);
237 while (var)
239 if (first_arg)
240 first_arg = 0;
241 else
242 fprintf(server, ",\n");
243 print_server("");
244 write_name(server, var);
245 var = PREV_LINK(var);
247 fprintf(server, ");\n");
248 indent--;
250 else
252 fprintf(server, "();\n");
255 /* marshall the return value */
256 if (!is_void(def->type, NULL))
258 fprintf(server, "\n");
259 print_server("_StubMsg.BufferLength = %uU;\n", get_required_buffer_size(def->type));
260 print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
261 fprintf(server, "\n");
262 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
263 print_server("if (_Status)\n");
264 indent++;
265 print_server("RpcRaiseException(_Status);\n");
266 indent--;
267 fprintf(server, "\n");
268 print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
269 fprintf(server, "\n");
271 print_server("*(");
272 write_type(server, def->type, def, def->tname);
273 fprintf(server, " *)_StubMsg.Buffer = _RetVal;\n");
274 fprintf(server, "_StubMsg.Buffer += sizeof(");
275 write_type(server, def->type, def, def->tname);
276 fprintf(server, ");\n");
279 indent--;
280 print_server("}\n");
281 print_server("RpcFinally\n");
282 print_server("{\n");
283 print_server("}\n");
284 print_server("RpcEndFinally\n");
286 /* calculate buffer length */
287 fprintf(server, "\n");
288 print_server("_pRpcMessage->BufferLength =\n");
289 indent++;
290 print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
291 indent--;
292 indent--;
293 fprintf(server, "}\n");
294 fprintf(server, "\n");
296 /* update proc_offset */
297 if (func->args)
299 var = func->args;
300 while (NEXT_LINK(var)) var = NEXT_LINK(var);
301 while (var)
303 proc_offset += get_size_procformatstring_var(var);
304 var = PREV_LINK(var);
307 if (!is_void(def->type, NULL))
308 proc_offset += get_size_procformatstring_var(def);
310 func = PREV_LINK(func);
315 static void write_dispatchtable(type_t *iface)
317 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
318 unsigned long method_count = 0;
319 func_t *func = iface->funcs;
321 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
322 print_server("{\n");
323 indent++;
324 while (NEXT_LINK(func)) func = NEXT_LINK(func);
325 while (func)
327 var_t *def = func->def;
329 print_server("%s_", iface->name);
330 write_name(server, def);
331 fprintf(server, ",\n");
333 method_count++;
334 func = PREV_LINK(func);
336 print_server("0\n");
337 indent--;
338 print_server("};\n");
339 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
340 print_server("{\n");
341 indent++;
342 print_server("%u,\n", method_count);
343 print_server("%s_table\n", iface->name);
344 indent--;
345 print_server("};\n");
346 fprintf(server, "\n");
350 static void write_stubdescdecl(type_t *iface)
352 print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
353 fprintf(server, "\n");
357 static void write_stubdescriptor(type_t *iface)
359 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
360 print_server("{\n");
361 indent++;
362 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
363 print_server("MIDL_user_allocate,\n");
364 print_server("MIDL_user_free,\n");
365 print_server("0,\n");
366 print_server("0,\n");
367 print_server("0,\n");
368 print_server("0,\n");
369 print_server("0,\n");
370 print_server("__MIDL_TypeFormatString.Format,\n");
371 print_server("1, /* -error bounds_check flag */\n");
372 print_server("0x10001, /* Ndr library version */\n");
373 print_server("0,\n");
374 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
375 print_server("0,\n");
376 print_server("0,\n");
377 print_server("0, /* notify & notify_flag routine table */\n");
378 print_server("1, /* Flags */\n");
379 print_server("0, /* Reserved3 */\n");
380 print_server("0, /* Reserved4 */\n");
381 print_server("0 /* Reserved5 */\n");
382 indent--;
383 print_server("};\n");
384 fprintf(server, "\n");
388 static void write_serverinterfacedecl(type_t *iface)
390 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
391 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
393 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
394 fprintf(server, "\n");
395 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
396 print_server("{\n");
397 indent++;
398 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
399 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",
400 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
401 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
402 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
403 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
404 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
405 print_server("0,\n");
406 print_server("0,\n");
407 print_server("0,\n");
408 print_server("0,\n");
409 print_server("0,\n");
410 indent--;
411 print_server("};\n");
412 print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
413 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
414 fprintf(server, "\n");
417 static void write_formatdesc( const char *str )
419 print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
420 print_server("{\n");
421 indent++;
422 print_server("short Pad;\n");
423 print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
424 indent--;
425 print_server("} MIDL_%s_FORMAT_STRING;\n", str);
426 print_server("\n");
430 static void write_formatstringsdecl(type_t *iface)
432 int byte_count = 1;
434 print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
436 /* determine the proc format string size */
437 if (iface->funcs)
439 func_t *func = iface->funcs;
440 while (NEXT_LINK(func)) func = NEXT_LINK(func);
441 while (func)
443 /* argument list size */
444 if (func->args)
446 var_t *var = func->args;
447 while (NEXT_LINK(var)) var = NEXT_LINK(var);
448 while (var)
450 byte_count += 2; /* FIXME: determine real size */
451 var = PREV_LINK(var);
455 /* return value size */
456 byte_count += 2; /* FIXME: determine real size */
457 func = PREV_LINK(func);
460 print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
462 fprintf(server, "\n");
463 write_formatdesc("TYPE");
464 write_formatdesc("PROC");
465 fprintf(server, "\n");
466 print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
467 print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
468 print_server("\n");
472 static void init_server(void)
474 if (server)
475 return;
476 if (!(server = fopen(server_name, "w")))
477 error("Could not open %s for output\n", server_name);
479 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
480 print_server("#include <string.h>\n");
481 fprintf(server, "\n");
482 print_server("#include \"%s\"\n", header_name);
483 fprintf(server, "\n");
487 void write_server(ifref_t *ifaces)
489 ifref_t *iface = ifaces;
491 if (!do_server)
492 return;
493 if (!iface)
494 return;
495 END_OF_LIST(iface);
497 init_server();
498 if (!server)
499 return;
501 while (iface)
503 fprintf(server, "/*****************************************************************************\n");
504 fprintf(server, " * %s interface\n", iface->iface->name);
505 fprintf(server, " */\n");
506 fprintf(server, "\n");
508 if (iface->iface->funcs)
510 write_formatstringsdecl(iface->iface);
511 write_serverinterfacedecl(iface->iface);
512 write_stubdescdecl(iface->iface);
514 write_function_stubs(iface->iface);
516 write_stubdescriptor(iface->iface);
517 write_dispatchtable(iface->iface);
520 print_server("#if !defined(__RPC_WIN32__)\n");
521 print_server("#error Invalid build platform for this stub.\n");
522 print_server("#endif\n");
523 fprintf(server, "\n");
525 write_procformatstring(server, iface->iface);
526 write_typeformatstring(server, iface->iface);
528 fprintf(server, "\n");
530 iface = PREV_LINK(iface);
533 fclose(server);