widl: Rename some variables.
[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
44 #define END_OF_LIST(list)       \
45   do {                          \
46     if (list) {                 \
47       while (NEXT_LINK(list))   \
48         list = NEXT_LINK(list); \
49     }                           \
50   } while(0)
51
52 static FILE* server;
53 static int indent = 0;
54
55
56 static int print_server(const char *format, ...)
57 {
58     va_list va;
59     int i, r;
60
61     va_start(va, format);
62     for (i = 0; i < indent; i++)
63         fprintf(server, "    ");
64     r = vfprintf(server, format, va);
65     va_end(va);
66     return r;
67 }
68
69
70 static void write_procformatstring(type_t *iface)
71 {
72     func_t *func = iface->funcs;
73
74     print_server("static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
75     print_server("{\n");
76     indent++;
77     print_server("0,\n");
78     print_server("{\n");
79     indent++;
80
81     while (NEXT_LINK(func)) func = NEXT_LINK(func);
82     while (func)
83     {
84         var_t *def = func->def;
85
86         if (is_void(def->type, NULL))
87         {
88             print_server("0x5b,    /* FC_END */\n");
89             print_server("0x5c,    /* FC_PAD */\n");
90         }
91         else
92         {
93             print_server("0x53,    /* FC_RETURN_PARAM_BASETYPE */\n");
94             print_server("0x%02x,    /* <type> */\n", def->type->type);
95         }
96
97         func = PREV_LINK(func);
98     }
99
100     print_server("0x0\n");
101     indent--;
102     print_server("}\n");
103     indent--;
104     print_server("};\n");
105     print_server("\n");
106 }
107
108
109 static void write_typeformatstring(void)
110 {
111     print_server("static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
112     print_server("{\n");
113     indent++;
114     print_server("0,\n");
115     print_server("{\n");
116     indent++;
117     print_server("NdrFcShort(0x0),\n");
118     print_server("0x0\n");
119     indent--;
120     print_server("}\n");
121     indent--;
122     print_server("};\n");
123     print_server("\n");
124 }
125
126
127 static unsigned int get_required_stack_size(type_t *type)
128 {
129     switch(type->type)
130     {
131         case RPC_FC_BYTE:
132         case RPC_FC_CHAR:
133         case RPC_FC_WCHAR:
134         case RPC_FC_USHORT:
135         case RPC_FC_SHORT:
136         case RPC_FC_ULONG:
137         case RPC_FC_LONG:
138             return 4;
139
140       case RPC_FC_HYPER:
141             return 8;
142
143         default:
144             error("Unknown/unsupported type: %s\n", type->name);
145             return 0;
146     }
147 }
148
149
150 static void write_function_stubs(type_t *iface)
151 {
152     func_t *func = iface->funcs;
153     while (NEXT_LINK(func)) func = NEXT_LINK(func);
154     while (func)
155     {
156         var_t *def = func->def;
157
158         write_type(server, def->type, def, def->tname);
159         fprintf(server, " __RPC_STUB\n");
160         fprintf(server, "%s_", iface->name);
161         write_name(server, def);
162         fprintf(server, "(\n");
163         indent++;
164         print_server("PRPC_MESSAGE _pRpcMessage)\n");
165         indent--;
166
167         /* write the functions body */
168         fprintf(server, "{\n");
169         indent++;
170
171         /* declare return value '_RetVal' */
172         if (!is_void(def->type, NULL))
173         {
174             print_server("");
175             write_type(server, def->type, def, def->tname);
176             fprintf(server, " _RetVal;\n");
177         }
178
179         print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
180         print_server("RPC_STATUS _Status;\n");
181         fprintf(server, "\n");
182         print_server("((void)(_Status));\n");
183         print_server("NdrServerInitializeNew(\n");
184         indent++;
185         print_server("_pRpcMessage,\n");
186         print_server("&_StubMsg,\n");
187         print_server("&%s_StubDesc);\n", iface->name);
188         indent--;
189         fprintf(server, "\n");
190
191         print_server("RpcTryFinally\n");
192         print_server("{\n");
193         indent++;
194         print_server("RpcTryExcept\n");
195         print_server("{\n");
196         indent++;
197         print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
198         print_server("{\n");
199         indent++;
200         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
201         indent--;
202         print_server("}\n");
203         indent--;
204         print_server("}\n");
205         print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
206         print_server("{\n");
207         indent++;
208         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
209         indent--;
210         print_server("}\n");
211         print_server("RpcEndExcept\n");
212         fprintf(server, "\n");
213
214
215         /* Call the real server function */
216         if (!is_void(def->type, NULL))
217             print_server("_RetVal = ");
218         else
219             print_server("");
220         write_name(server, def);
221
222         /* FIXME: handle argument list */
223         fprintf(server, "();\n");
224
225         /* FIXME: Marshall the return value */
226         if (!is_void(def->type, NULL))
227         {
228             fprintf(server, "\n");
229             print_server("_StubMsg.BufferLength = %uU;\n", get_required_stack_size(def->type));
230             print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
231             fprintf(server, "\n");
232             print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
233             print_server("if (_Status)\n");
234             indent++;
235             print_server("RpcRaiseException(_Status);\n");
236             indent--;
237             fprintf(server, "\n");
238             print_server("_StubMsg.Buffer = (unsigned char __RPC_FAR *)_pRpcMessage->Buffer;\n");
239             fprintf(server, "\n");
240
241             print_server("*((");
242             write_type(server, def->type, def, def->tname);
243             fprintf(server, " __RPC_FAR *)_StubMsg.Buffer)++ = _RetVal;\n");
244         }
245
246         indent--;
247         print_server("}\n");
248         print_server("RpcFinally\n");
249         print_server("{\n");
250         print_server("}\n");
251         print_server("RpcEndFinally\n");
252
253         /* calculate buffer length */
254         fprintf(server, "\n");
255         print_server("_pRpcMessage->BufferLength =\n");
256         indent++;
257         print_server("(unsigned int)((long)_StubMsg.Buffer - (long)_pRpcMessage->Buffer);\n");
258         indent--;
259         indent--;
260         fprintf(server, "}\n");
261         fprintf(server, "\n");
262
263         func = PREV_LINK(func);
264     }
265 }
266
267
268 static void write_dispatchtable(type_t *iface)
269 {
270     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
271     unsigned long method_count = 0;
272     func_t *func = iface->funcs;
273
274     print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
275     print_server("{\n");
276     indent++;
277     while (NEXT_LINK(func)) func = NEXT_LINK(func);
278     while (func)
279     {
280         var_t *def = func->def;
281
282         print_server("%s_", iface->name);
283         write_name(server, def);
284         fprintf(server, ",\n");
285
286         method_count++;
287         func = PREV_LINK(func);
288     }
289     print_server("0\n");
290     indent--;
291     print_server("};\n");
292     print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
293     print_server("{\n");
294     indent++;
295     print_server("%u,\n", method_count);
296     print_server("%s_table\n", iface->name);
297     indent--;
298     print_server("};\n");
299     fprintf(server, "\n");
300 }
301
302
303 static void write_stubdescdecl(type_t *iface)
304 {
305     print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
306     fprintf(server, "\n");
307 }
308
309
310 static void write_stubdescriptor(type_t *iface)
311 {
312     print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
313     print_server("{\n");
314     indent++;
315     print_server("(void __RPC_FAR *)& %s___RpcServerInterface,\n", iface->name);
316     print_server("MIDL_user_allocate,\n");
317     print_server("MIDL_user_free,\n");
318     print_server("0,\n");
319     print_server("0,\n");
320     print_server("0,\n");
321     print_server("0,\n");
322     print_server("0,\n");
323     print_server("__MIDL_TypeFormatString.Format,\n");
324     print_server("1, /* -error bounds_check flag */\n");
325     print_server("0x10001, /* Ndr library version */\n");
326     print_server("0,\n");
327     print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
328     print_server("0,\n");
329     print_server("0,\n");
330     print_server("0,  /* notify & notify_flag routine table */\n");
331     print_server("1,  /* Flags */\n");
332     print_server("0,  /* Reserved3 */\n");
333     print_server("0,  /* Reserved4 */\n");
334     print_server("0   /* Reserved5 */\n");
335     indent--;
336     print_server("};\n");
337     fprintf(server, "\n");
338 }
339
340
341 static void write_serverinterfacedecl(type_t *iface)
342 {
343     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
344     UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
345
346     print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
347     fprintf(server, "\n");
348     print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
349     print_server("{\n");
350     indent++;
351     print_server("sizeof(RPC_SERVER_INTERFACE),\n");
352     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",
353                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
354                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
355                  uuid->Data4[7], LOWORD(ver), HIWORD(ver));
356     print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
357     print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
358     print_server("0,\n");
359     print_server("0,\n");
360     print_server("0,\n");
361     print_server("0,\n");
362     print_server("0,\n");
363     indent--;
364     print_server("};\n");
365     print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
366                  iface->name, LOWORD(ver), HIWORD(ver), iface->name);
367     fprintf(server, "\n");
368 }
369
370 static void write_formatdesc( const char *str )
371 {
372     print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
373     print_server("{\n");
374     indent++;
375     print_server("short Pad;\n");
376     print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
377     indent--;
378     print_server("} MIDL_%s_FORMAT_STRING;\n", str);
379     print_server("\n");
380 }
381
382
383 static void write_formatstringsdecl(type_t *iface)
384 {
385     func_t *func;
386     int byte_count = 1;
387
388     print_server("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
389
390     /* determine the proc format string size */
391     func = iface->funcs;
392     while (NEXT_LINK(func)) func = NEXT_LINK(func);
393     while (func)
394     {
395         byte_count += 2; /* FIXME: determine real size */
396         func = PREV_LINK(func);
397     }
398     print_server("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
399
400     fprintf(server, "\n");
401     write_formatdesc("TYPE");
402     write_formatdesc("PROC");
403     fprintf(server, "\n");
404     print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
405     print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
406     print_server("\n");
407 }
408
409
410 static void init_server(void)
411 {
412     if (server)
413         return;
414     if (!(server = fopen(server_name, "w")))
415         error("Could not open %s for output\n", server_name);
416
417     print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
418     print_server("#include <string.h>\n");
419     fprintf(server, "\n");
420     print_server("#include \"%s\"\n", header_name);
421     fprintf(server, "\n");
422 }
423
424
425 void write_server(ifref_t *ifaces)
426 {
427     ifref_t *lcur = ifaces;
428
429     if (!do_server)
430         return;
431     if (!lcur)
432         return;
433     END_OF_LIST(lcur);
434
435     init_server();
436     if (!server)
437         return;
438
439     write_formatstringsdecl(lcur->iface);
440     write_serverinterfacedecl(lcur->iface);
441     write_stubdescdecl(lcur->iface);
442
443     write_function_stubs(lcur->iface);
444
445     write_stubdescriptor(lcur->iface);
446     write_dispatchtable(lcur->iface);
447
448     print_server("#if !defined(__RPC_WIN32__)\n");
449     print_server("#error  Invalid build platform for this stub.\n");
450     print_server("#endif\n");
451     fprintf(server, "\n");
452
453     write_procformatstring(lcur->iface);
454     write_typeformatstring();
455
456     fprintf(server, "\n");
457
458     fclose(server);
459 }