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