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