widl: Some cosmetic fixes in generated code.
[wine] / tools / widl / client.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 #include <unistd.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #include <signal.h>
31
32 #include "windef.h"
33
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
38
39 #include "widltypes.h"
40 #include "typelib.h"
41 #include "typelib_struct.h"
42 #include "typegen.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* client;
53 static int indent = 0;
54
55 static int print_client( const char *format, ... )
56 {
57     va_list va;
58     int i, r;
59
60     va_start(va, format);
61     for (i = 0; i < indent; i++)
62         fprintf(client, "    ");
63     r = vfprintf(client, format, va);
64     va_end(va);
65     return r;
66 }
67
68
69 static void print_message_buffer_size(func_t *func)
70 {
71     unsigned int total_size = 0;
72
73     if (func->args)
74     {
75         var_t *var = func->args;
76         while (NEXT_LINK(var)) var = NEXT_LINK(var);
77         while (var)
78         {
79             unsigned int alignment;
80
81             total_size += get_required_buffer_size(var, &alignment);
82             total_size += alignment;
83
84             var = PREV_LINK(var);
85         }
86     }
87     fprintf(client, " %u", total_size);
88 }
89
90
91 static void write_function_stubs(type_t *iface)
92 {
93     func_t *func = iface->funcs;
94     char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
95     int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
96     var_t *var;
97     int method_count = 0;
98     unsigned int proc_offset = 0;
99     unsigned int type_offset = 2;
100
101     while (NEXT_LINK(func)) func = NEXT_LINK(func);
102     while (func)
103     {
104         var_t *def = func->def;
105         var_t* explicit_handle_var;
106         unsigned int type_offset_func;
107
108         /* check for a defined binding handle */
109         explicit_handle_var = get_explicit_handle_var(func);
110         if (explicit_handle)
111         {
112             if (!explicit_handle_var)
113             {
114                 error("%s() does not define an explicit binding handle!\n", def->name);
115                 return;
116             }
117         }
118         else if (implicit_handle)
119         {
120             if (explicit_handle_var)
121             {
122                 error("%s() must not define a binding handle!\n", def->name);
123                 return;
124             }
125         }
126
127         write_type(client, def->type, def, def->tname);
128         fprintf(client, " ");
129         write_name(client, def);
130         fprintf(client, "(\n");
131         indent++;
132         if (func->args)
133             write_args(client, func->args, iface->name, 0, TRUE);
134         else
135             print_client("void");
136         fprintf(client, ")\n");
137         indent--;
138
139         /* write the functions body */
140         fprintf(client, "{\n");
141         indent++;
142
143         /* declare return value '_RetVal' */
144         if (!is_void(def->type, NULL))
145         {
146             print_client("");
147             write_type(client, def->type, def, def->tname);
148             fprintf(client, " _RetVal;\n");
149         }
150
151         if (implicit_handle || explicit_handle_var)
152             print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
153
154         print_client("RPC_MESSAGE _RpcMessage;\n");
155         print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
156         fprintf(client, "\n");
157         print_client("RpcTryFinally\n");
158         print_client("{\n");
159         indent++;
160
161         print_client("NdrClientInitializeNew(\n");
162         indent++;
163         print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
164         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
165         print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
166         print_client("%d);\n", method_count);
167         indent--;
168         fprintf(client, "\n");
169
170         if (implicit_handle)
171         {
172             print_client("_Handle = %s;\n", implicit_handle);
173             fprintf(client, "\n");
174         }
175         else if (explicit_handle_var)
176         {
177             print_client("_Handle = %s;\n", explicit_handle_var->name);
178             fprintf(client, "\n");
179         }
180
181         /* emit the message buffer size */
182         print_client("_StubMsg.BufferLength =");
183         print_message_buffer_size(func);
184         fprintf(client, ";\n");
185
186         print_client("NdrGetBuffer(\n");
187         indent++;
188         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
189         print_client("_StubMsg.BufferLength,\n");
190         if (implicit_handle || explicit_handle_var)
191             print_client("_Handle);\n");
192         else
193             print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
194         indent--;
195         fprintf(client, "\n");
196
197
198         /* make a copy so we don't increment the type offset twice */
199         type_offset_func = type_offset;
200
201         /* marshal arguments */
202         marshall_arguments(client, indent, func, &type_offset_func, PASS_IN);
203
204         /* send/receive message */
205         /* print_client("NdrNsSendReceive(\n"); */
206         /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
207         /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
208         print_client("NdrSendReceive(\n");
209         indent++;
210         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
211         print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
212         indent--;
213
214         print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
215         print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n");
216
217         unmarshall_arguments(client, indent, func, &type_offset, PASS_OUT);
218
219         /* unmarshal return value */
220         if (!is_void(def->type, NULL))
221         {
222             fprintf(client, "\n");
223
224             print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
225             indent++;
226             print_client("NdrConvert(\n");
227             indent++;
228             print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
229             print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
230             indent -= 2;
231             fprintf(client, "\n");
232
233             print_client("_RetVal = *(");
234             write_type(client, def->type, def, def->tname);
235             fprintf(client, " *)_StubMsg.Buffer;\n");
236             print_client("_StubMsg.Buffer += sizeof(");
237             write_type(client, def->type, def, def->tname);
238             fprintf(client, ");\n");
239         }
240
241         /* update proc_offset */
242         if (func->args)
243         {
244             var = func->args;
245             while (NEXT_LINK(var)) var = NEXT_LINK(var);
246             while (var)
247             {
248                 proc_offset += get_size_procformatstring_var(var);
249                 var = PREV_LINK(var);
250             }
251         }
252         if (!is_void(def->type, NULL))
253             proc_offset += get_size_procformatstring_var(def);
254
255         indent--;
256         print_client("}\n");
257         print_client("RpcFinally\n");
258         print_client("{\n");
259         indent++;
260
261
262         /* FIXME: emit client finally code */
263
264         print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
265
266         indent--;
267         print_client("}\n");
268         print_client("RpcEndFinally\n");
269
270
271         /* emit return code */
272         if (!is_void(def->type, NULL))
273         {
274             fprintf(client, "\n");
275             print_client("return _RetVal;\n");
276         }
277
278         indent--;
279         fprintf(client, "}\n");
280         fprintf(client, "\n");
281
282         method_count++;
283         func = PREV_LINK(func);
284     }
285 }
286
287
288 static void write_bindinghandledecl(type_t *iface)
289 {
290     print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n", iface->name);
291     fprintf(client, "\n");
292 }
293
294
295 static void write_stubdescdecl(type_t *iface)
296 {
297     print_client("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
298     fprintf(client, "\n");
299 }
300
301
302 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
303 {
304     char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
305
306     print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
307     print_client("{\n");
308     indent++;
309     print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
310     print_client("MIDL_user_allocate,\n");
311     print_client("MIDL_user_free,\n");
312     if (implicit_handle)
313         print_client("&%s,\n", implicit_handle);
314     else
315         print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
316     print_client("0,\n");
317     print_client("0,\n");
318     if (expr_eval_routines)
319         print_client("ExprEvalRoutines,\n");
320     else
321         print_client("0,\n");
322     print_client("0,\n");
323     print_client("__MIDL_TypeFormatString.Format,\n");
324     print_client("1, /* -error bounds_check flag */\n");
325     print_client("0x10001, /* Ndr library version */\n");
326     print_client("0,\n");
327     print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
328     print_client("0,\n");
329     print_client("0,\n");
330     print_client("0,  /* notify & notify_flag routine table */\n");
331     print_client("1,  /* Flags */\n");
332     print_client("0,  /* Reserved3 */\n");
333     print_client("0,  /* Reserved4 */\n");
334     print_client("0   /* Reserved5 */\n");
335     indent--;
336     print_client("};\n");
337     fprintf(client, "\n");
338 }
339
340
341 static void write_clientinterfacedecl(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_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
347     print_client("{\n");
348     indent++;
349     print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
350     print_client("{{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",
351                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
352                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
353                  uuid->Data4[7], LOWORD(ver), HIWORD(ver));
354     print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
355     print_client("0,\n");
356     print_client("0,\n");
357     print_client("0,\n");
358     print_client("0,\n");
359     print_client("0,\n");
360     print_client("0,\n");
361     indent--;
362     print_client("};\n");
363     print_client("RPC_IF_HANDLE %s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
364                  iface->name, LOWORD(ver), HIWORD(ver), iface->name);
365     fprintf(client, "\n");
366 }
367
368
369 static void write_formatdesc( const char *str )
370 {
371     print_client("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
372     print_client("{\n");
373     indent++;
374     print_client("short Pad;\n");
375     print_client("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
376     indent--;
377     print_client("} MIDL_%s_FORMAT_STRING;\n", str);
378     print_client("\n");
379 }
380
381
382 static void write_formatstringsdecl(type_t *iface)
383 {
384     int byte_count = 1;
385
386     print_client("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
387
388     /* determine the proc format string size */
389     if (iface->funcs)
390     {
391         func_t *func = iface->funcs;
392         while (NEXT_LINK(func)) func = NEXT_LINK(func);
393         while (func)
394         {
395             /* argument list size */
396             if (func->args)
397             {
398                 var_t *var = func->args;
399                 while (NEXT_LINK(var)) var = NEXT_LINK(var);
400                 while (var)
401                 {
402                     byte_count += 2; /* FIXME: determine real size */
403                     var = PREV_LINK(var);
404                 }
405             }
406     
407             /* return value size */
408             byte_count += 2; /* FIXME: determine real size */
409             func = PREV_LINK(func);
410         }
411     }
412     print_client("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
413
414     fprintf(client, "\n");
415     write_formatdesc("TYPE");
416     write_formatdesc("PROC");
417     fprintf(client, "\n");
418     print_client("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
419     print_client("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
420     print_client("\n");
421 }
422
423
424 static void write_implicithandledecl(type_t *iface)
425 {
426     char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
427
428     if (implicit_handle)
429     {
430         fprintf(client, "handle_t %s;\n", implicit_handle);
431         fprintf(client, "\n");
432     }
433 }
434
435
436 static void init_client(void)
437 {
438     if (client) return;
439     if (!(client = fopen(client_name, "w")))
440         error("Could not open %s for output\n", client_name);
441
442     print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
443     print_client("#include <string.h>\n");
444     print_client("#ifdef _ALPHA_\n");
445     print_client("#include <stdarg.h>\n");
446     print_client("#endif\n");
447     fprintf(client, "\n");
448     print_client("#include \"%s\"\n", header_name);
449     fprintf(client, "\n");
450 }
451
452
453 void write_client(ifref_t *ifaces)
454 {
455     ifref_t *iface = ifaces;
456
457     if (!do_client)
458         return;
459     if (!iface)
460         return;
461     END_OF_LIST(iface);
462
463     init_client();
464     if (!client)
465         return;
466
467     while (iface)
468     {
469         fprintf(client, "/*****************************************************************************\n");
470         fprintf(client, " * %s interface\n", iface->iface->name);
471         fprintf(client, " */\n");
472         fprintf(client, "\n");
473
474         if (iface->iface->funcs)
475         {
476             int expr_eval_routines;
477
478             write_formatstringsdecl(iface->iface);
479             write_implicithandledecl(iface->iface);
480     
481             write_clientinterfacedecl(iface->iface);
482             write_stubdescdecl(iface->iface);
483             write_bindinghandledecl(iface->iface);
484     
485             write_function_stubs(iface->iface);
486
487             print_client("#if !defined(__RPC_WIN32__)\n");
488             print_client("#error  Invalid build platform for this stub.\n");
489             print_client("#endif\n");
490             fprintf(client, "\n");
491
492             write_procformatstring(client, iface->iface);
493             write_typeformatstring(client, iface->iface);
494
495             fprintf(client, "\n");
496
497             expr_eval_routines = write_expr_eval_routines(client, iface->iface->name);
498             if (expr_eval_routines)
499                 write_expr_eval_routine_list(client, iface->iface->name);
500             write_stubdescriptor(iface->iface, expr_eval_routines);
501         }
502
503         iface = PREV_LINK(iface);
504     }
505
506     fclose(client);
507 }