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