widl: Out parameters.
[wine] / tools / widl / server.c
1 /*
2  * IDL Compiler
3  *
4  * Copyright 2005 Eric Kohl
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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>
33
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
38 #include "windef.h"
39
40 #include "widl.h"
41 #include "typelib.h"
42 #include "typelib_struct.h"
43 #include "typegen.h"
44
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)
52
53 static FILE* server;
54 static int indent = 0;
55
56
57 static int print_server(const char *format, ...)
58 {
59     va_list va;
60     int i, r;
61
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;
68 }
69
70
71 static void write_parameters_init(func_t *func)
72 {
73     var_t *var;
74
75     if (!func->args)
76         return;
77
78     var = func->args;
79     while (NEXT_LINK(var)) var = NEXT_LINK(var);
80     while (var)
81     {
82         print_server("%s = 0;\n", var->name);
83
84         var = PREV_LINK(var);
85     }
86     fprintf(server, "\n");
87 }
88
89
90 static void write_function_stubs(type_t *iface)
91 {
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;
99
100     while (NEXT_LINK(func)) func = NEXT_LINK(func);
101     while (func)
102     {
103         var_t *def = func->def;
104         unsigned long buffer_size = 0;
105
106         /* check for a defined binding handle */
107         explicit_handle_var = get_explicit_handle_var(func);
108         if (explicit_handle)
109         {
110             if (!explicit_handle_var)
111             {
112                 error("%s() does not define an explicit binding handle!\n", def->name);
113                 return;
114             }
115         }
116         else if (implicit_handle)
117         {
118             if (explicit_handle_var)
119             {
120                 error("%s() must not define a binding handle!\n", def->name);
121                 return;
122             }
123         }
124
125         fprintf(server, "void __RPC_STUB\n");
126         fprintf(server, "%s_", iface->name);
127         write_name(server, def);
128         fprintf(server, "(\n");
129         indent++;
130         print_server("PRPC_MESSAGE _pRpcMessage)\n");
131         indent--;
132
133         /* write the functions body */
134         fprintf(server, "{\n");
135         indent++;
136
137         /* declare return value '_RetVal' */
138         if (!is_void(def->type, NULL))
139         {
140             print_server("");
141             write_type(server, def->type, def, def->tname);
142             fprintf(server, " _RetVal;\n");
143         }
144
145         /* declare arguments */
146         if (func->args)
147         {
148             var = func->args;
149             while (NEXT_LINK(var)) var = NEXT_LINK(var);
150             while (var)
151             {
152                 print_server("");
153                 write_type(server, var->type, var, var->tname);
154                 fprintf(server, " ");
155                 write_name(server, var);
156                 write_array(server, var->array, 0);
157                 fprintf(server, ";\n");
158
159                 var = PREV_LINK(var);
160             }
161         }
162
163         print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
164         print_server("RPC_STATUS _Status;\n");
165         fprintf(server, "\n");
166
167
168         print_server("((void)(_Status));\n");
169         print_server("NdrServerInitializeNew(\n");
170         indent++;
171         print_server("_pRpcMessage,\n");
172         print_server("&_StubMsg,\n");
173         print_server("&%s_StubDesc);\n", iface->name);
174         indent--;
175         fprintf(server, "\n");
176
177         write_parameters_init(func);
178
179         if (explicit_handle_var)
180         {
181             print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
182             fprintf(server, "\n");
183         }
184
185         print_server("RpcTryFinally\n");
186         print_server("{\n");
187         indent++;
188         print_server("RpcTryExcept\n");
189         print_server("{\n");
190         indent++;
191
192         if (func->args)
193         {
194             print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
195             indent++;
196             print_server("NdrConvert(\n");
197             indent++;
198             print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
199             print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
200             indent -= 2;
201             fprintf(server, "\n");
202
203             unmarshall_arguments(server, indent, func, &type_offset, PASS_IN);
204         }
205
206         print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
207         print_server("{\n");
208         indent++;
209         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
210         indent--;
211         print_server("}\n");
212         indent--;
213         print_server("}\n");
214         print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
215         print_server("{\n");
216         indent++;
217         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
218         indent--;
219         print_server("}\n");
220         print_server("RpcEndExcept\n");
221         fprintf(server, "\n");
222
223
224         /* Call the real server function */
225         if (!is_void(def->type, NULL))
226             print_server("_RetVal = ");
227         else
228             print_server("");
229         write_name(server, def);
230
231         if (func->args)
232         {
233             int first_arg = 1;
234
235             fprintf(server, "(\n");
236             indent++;
237             var = func->args;
238             while (NEXT_LINK(var)) var = NEXT_LINK(var);
239             while (var)
240             {
241                 if (first_arg)
242                     first_arg = 0;
243                 else
244                     fprintf(server, ",\n");
245                 print_server("");
246                 write_name(server, var);
247                 var = PREV_LINK(var);
248             }
249             fprintf(server, ");\n");
250             indent--;
251         }
252         else
253         {
254             fprintf(server, "();\n");
255         }
256
257         if (func->args)
258         {
259             var_t *var = func->args;
260             while (NEXT_LINK(var)) var = NEXT_LINK(var);
261             while (var)
262             {
263                 if (is_attr(var->attrs, ATTR_OUT))
264                 {
265                     unsigned int alignment;
266                     buffer_size += get_required_buffer_size(var, &alignment);
267                     buffer_size += alignment;
268                 }
269         
270                 var = PREV_LINK(var);
271             }
272         }
273
274         if (!is_void(def->type, NULL))
275         {
276             unsigned int alignment;
277             buffer_size += get_required_buffer_size(def, &alignment);
278             buffer_size += alignment;
279         }
280
281         if (buffer_size)
282         {
283             fprintf(server, "\n");
284             print_server("_StubMsg.BufferLength = %uU;\n", buffer_size);
285             print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
286             fprintf(server, "\n");
287             print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
288             print_server("if (_Status)\n");
289             indent++;
290             print_server("RpcRaiseException(_Status);\n");
291             indent--;
292             fprintf(server, "\n");
293             print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
294             fprintf(server, "\n");
295         }
296
297         marshall_arguments(server, indent, func, &type_offset, PASS_OUT);
298
299         /* marshall the return value */
300         if (!is_void(def->type, NULL))
301         {
302             print_server("*(");
303             write_type(server, def->type, def, def->tname);
304             fprintf(server, " *)_StubMsg.Buffer = _RetVal;\n");
305             fprintf(server, "_StubMsg.Buffer += sizeof(");
306             write_type(server, def->type, def, def->tname);
307             fprintf(server, ");\n");
308         }
309
310         indent--;
311         print_server("}\n");
312         print_server("RpcFinally\n");
313         print_server("{\n");
314         print_server("}\n");
315         print_server("RpcEndFinally\n");
316
317         /* calculate buffer length */
318         fprintf(server, "\n");
319         print_server("_pRpcMessage->BufferLength =\n");
320         indent++;
321         print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
322         indent--;
323         indent--;
324         fprintf(server, "}\n");
325         fprintf(server, "\n");
326
327         /* update proc_offset */
328         if (func->args)
329         {
330             var = func->args;
331             while (NEXT_LINK(var)) var = NEXT_LINK(var);
332             while (var)
333             {
334                 proc_offset += get_size_procformatstring_var(var);
335                 var = PREV_LINK(var);
336             }
337         }
338         if (!is_void(def->type, NULL))
339             proc_offset += get_size_procformatstring_var(def);
340
341         func = PREV_LINK(func);
342     }
343 }
344
345
346 static void write_dispatchtable(type_t *iface)
347 {
348     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
349     unsigned long method_count = 0;
350     func_t *func = iface->funcs;
351
352     print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
353     print_server("{\n");
354     indent++;
355     while (NEXT_LINK(func)) func = NEXT_LINK(func);
356     while (func)
357     {
358         var_t *def = func->def;
359
360         print_server("%s_", iface->name);
361         write_name(server, def);
362         fprintf(server, ",\n");
363
364         method_count++;
365         func = PREV_LINK(func);
366     }
367     print_server("0\n");
368     indent--;
369     print_server("};\n");
370     print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
371     print_server("{\n");
372     indent++;
373     print_server("%u,\n", method_count);
374     print_server("%s_table\n", iface->name);
375     indent--;
376     print_server("};\n");
377     fprintf(server, "\n");
378 }
379
380
381 static void write_stubdescdecl(type_t *iface)
382 {
383     print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
384     fprintf(server, "\n");
385 }
386
387
388 static void write_stubdescriptor(type_t *iface)
389 {
390     print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
391     print_server("{\n");
392     indent++;
393     print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
394     print_server("MIDL_user_allocate,\n");
395     print_server("MIDL_user_free,\n");
396     print_server("0,\n");
397     print_server("0,\n");
398     print_server("0,\n");
399     print_server("0,\n");
400     print_server("0,\n");
401     print_server("__MIDL_TypeFormatString.Format,\n");
402     print_server("1, /* -error bounds_check flag */\n");
403     print_server("0x10001, /* Ndr library version */\n");
404     print_server("0,\n");
405     print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
406     print_server("0,\n");
407     print_server("0,\n");
408     print_server("0,  /* notify & notify_flag routine table */\n");
409     print_server("1,  /* Flags */\n");
410     print_server("0,  /* Reserved3 */\n");
411     print_server("0,  /* Reserved4 */\n");
412     print_server("0   /* Reserved5 */\n");
413     indent--;
414     print_server("};\n");
415     fprintf(server, "\n");
416 }
417
418
419 static void write_serverinterfacedecl(type_t *iface)
420 {
421     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
422     UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
423
424     print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
425     fprintf(server, "\n");
426     print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
427     print_server("{\n");
428     indent++;
429     print_server("sizeof(RPC_SERVER_INTERFACE),\n");
430     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",
431                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
432                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
433                  uuid->Data4[7], LOWORD(ver), HIWORD(ver));
434     print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
435     print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
436     print_server("0,\n");
437     print_server("0,\n");
438     print_server("0,\n");
439     print_server("0,\n");
440     print_server("0,\n");
441     indent--;
442     print_server("};\n");
443     print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
444                  iface->name, LOWORD(ver), HIWORD(ver), iface->name);
445     fprintf(server, "\n");
446 }
447
448 static void write_formatdesc( const char *str )
449 {
450     print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
451     print_server("{\n");
452     indent++;
453     print_server("short Pad;\n");
454     print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
455     indent--;
456     print_server("} MIDL_%s_FORMAT_STRING;\n", str);
457     print_server("\n");
458 }
459
460
461 static void write_formatstringsdecl(type_t *iface)
462 {
463     int byte_count = 1;
464
465     print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
466
467     /* determine the proc format string size */
468     if (iface->funcs)
469     {
470         func_t *func = iface->funcs;
471         while (NEXT_LINK(func)) func = NEXT_LINK(func);
472         while (func)
473         {
474             /* argument list size */
475             if (func->args)
476             {
477                 var_t *var = func->args;
478                 while (NEXT_LINK(var)) var = NEXT_LINK(var);
479                 while (var)
480                 {
481                     byte_count += 2; /* FIXME: determine real size */
482                     var = PREV_LINK(var);
483                 }
484             }
485     
486             /* return value size */
487             byte_count += 2; /* FIXME: determine real size */
488             func = PREV_LINK(func);
489         }
490     }
491     print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
492
493     fprintf(server, "\n");
494     write_formatdesc("TYPE");
495     write_formatdesc("PROC");
496     fprintf(server, "\n");
497     print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
498     print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
499     print_server("\n");
500 }
501
502
503 static void init_server(void)
504 {
505     if (server)
506         return;
507     if (!(server = fopen(server_name, "w")))
508         error("Could not open %s for output\n", server_name);
509
510     print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
511     print_server("#include <string.h>\n");
512     fprintf(server, "\n");
513     print_server("#include \"%s\"\n", header_name);
514     fprintf(server, "\n");
515 }
516
517
518 void write_server(ifref_t *ifaces)
519 {
520     ifref_t *iface = ifaces;
521
522     if (!do_server)
523         return;
524     if (!iface)
525         return;
526     END_OF_LIST(iface);
527
528     init_server();
529     if (!server)
530         return;
531
532     while (iface)
533     {
534         fprintf(server, "/*****************************************************************************\n");
535         fprintf(server, " * %s interface\n", iface->iface->name);
536         fprintf(server, " */\n");
537         fprintf(server, "\n");
538
539         if (iface->iface->funcs)
540         {
541             write_formatstringsdecl(iface->iface);
542             write_serverinterfacedecl(iface->iface);
543             write_stubdescdecl(iface->iface);
544     
545             write_function_stubs(iface->iface);
546     
547             write_stubdescriptor(iface->iface);
548             write_dispatchtable(iface->iface);
549         }
550
551         print_server("#if !defined(__RPC_WIN32__)\n");
552         print_server("#error  Invalid build platform for this stub.\n");
553         print_server("#endif\n");
554         fprintf(server, "\n");
555
556         write_procformatstring(server, iface->iface);
557         write_typeformatstring(server, iface->iface);
558
559         fprintf(server, "\n");
560
561         iface = PREV_LINK(iface);
562     }
563
564     fclose(server);
565 }