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