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