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