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