widl: Add support for finally function in proxy methods.
[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 #include "expr.h"
40
41 static FILE* client;
42 static int indent = 0;
43
44 static void print_client( const char *format, ... )
45 {
46     va_list va;
47     va_start(va, format);
48     print(client, indent, format, va);
49     va_end(va);
50 }
51
52
53 static void check_pointers(const func_t *func)
54 {
55     const var_t *var;
56
57     if (!func->args)
58         return;
59
60     LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
61     {
62         if (is_var_ptr(var) && cant_be_null(var))
63         {
64             print_client("if (!%s)\n", var->name);
65             print_client("{\n");
66             indent++;
67             print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
68             indent--;
69             print_client("}\n\n");
70         }
71     }
72 }
73
74 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
75 {
76     const func_t *func;
77     const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
78     const var_t *var;
79     int method_count = 0;
80
81     if (!implicit_handle)
82         print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n\n", iface->name);
83
84     if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
85     {
86         const var_t *def = func->def;
87         const var_t* explicit_handle_var;
88         const var_t* explicit_generic_handle_var = NULL;
89         const var_t* context_handle_var = NULL;
90         int has_full_pointer = is_full_pointer_function(func);
91         const char *callconv = get_attrp(def->type->attrs, ATTR_CALLCONV);
92
93         /* check for a defined binding handle */
94         explicit_handle_var = get_explicit_handle_var(func);
95         if (!explicit_handle_var)
96         {
97             explicit_generic_handle_var = get_explicit_generic_handle_var(func);
98             if (!explicit_generic_handle_var)
99                 context_handle_var = get_context_handle_var(func);
100         }
101
102         write_type_decl_left(client, get_func_return_type(func));
103         if (needs_space_after(get_func_return_type(func)))
104           fprintf(client, " ");
105         if (callconv) fprintf(client, "%s ", callconv);
106         fprintf(client, "%s%s(\n", prefix_client, get_name(def));
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             if (is_attr(context_handle_var->attrs, ATTR_IN) &&
191                 !is_attr(context_handle_var->attrs, ATTR_OUT))
192             {
193                 print_client("else\n");
194                 indent++;
195                 print_client("RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);\n");
196                 indent--;
197             }
198             fprintf(client, "\n");
199         }
200         else if (implicit_handle)
201         {
202             print_client("_Handle = %s;\n", implicit_handle);
203             fprintf(client, "\n");
204         }
205
206         write_remoting_arguments(client, indent, 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 || explicit_generic_handle_var || context_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         /* marshal arguments */
220         write_remoting_arguments(client, indent, func, PASS_IN, PHASE_MARSHAL);
221
222         /* send/receive message */
223         /* print_client("NdrNsSendReceive(\n"); */
224         /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
225         /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
226         print_client("NdrSendReceive(\n");
227         indent++;
228         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
229         print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
230         indent--;
231
232         print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
233         print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
234
235         if (has_out_arg_or_return(func))
236         {
237             fprintf(client, "\n");
238
239             print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
240             indent++;
241             print_client("NdrConvert(\n");
242             indent++;
243             print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
244             print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
245             indent -= 2;
246         }
247
248         /* unmarshall arguments */
249         fprintf(client, "\n");
250         write_remoting_arguments(client, indent, func, PASS_OUT, PHASE_UNMARSHAL);
251
252         /* unmarshal return value */
253         if (!is_void(get_func_return_type(func)))
254         {
255             if (decl_indirect(get_func_return_type(func)))
256                 print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
257             else if (is_ptr(get_func_return_type(func)) || is_array(get_func_return_type(func)))
258                 print_client("%s = 0;\n", "_RetVal");
259             write_remoting_arguments(client, indent, func, PASS_RETURN, PHASE_UNMARSHAL);
260         }
261
262         /* update proc_offset */
263         if (func->args)
264         {
265             LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
266                 *proc_offset += get_size_procformatstring_type(var->name, var->type, var->attrs);
267         }
268         if (!is_void(get_func_return_type(func)))
269             *proc_offset += get_size_procformatstring_type("return value", get_func_return_type(func), NULL);
270         else
271             *proc_offset += 2; /* FC_END and FC_PAD */
272
273         indent--;
274         print_client("}\n");
275         print_client("RpcFinally\n");
276         print_client("{\n");
277         indent++;
278
279
280         /* FIXME: emit client finally code */
281
282         if (has_full_pointer)
283             write_full_pointer_free(client, indent, func);
284
285         print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
286
287         if (!implicit_handle && explicit_generic_handle_var)
288         {
289             fprintf(client, "\n");
290             print_client("if (_Handle)\n");
291             indent++;
292             print_client("%s_unbind(%s, _Handle);\n",
293                 get_explicit_generic_handle_type(explicit_generic_handle_var)->name,
294                 explicit_generic_handle_var->name);
295             indent--;
296         }
297
298         indent--;
299         print_client("}\n");
300         print_client("RpcEndFinally\n");
301
302
303         /* emit return code */
304         if (!is_void(get_func_return_type(func)))
305         {
306             fprintf(client, "\n");
307             print_client("return _RetVal;\n");
308         }
309
310         indent--;
311         fprintf(client, "}\n");
312         fprintf(client, "\n");
313
314         method_count++;
315     }
316 }
317
318
319 static void write_stubdescdecl(type_t *iface)
320 {
321     print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
322     fprintf(client, "\n");
323 }
324
325
326 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
327 {
328     const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
329
330     print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
331     print_client("{\n");
332     indent++;
333     print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
334     print_client("MIDL_user_allocate,\n");
335     print_client("MIDL_user_free,\n");
336     print_client("{\n");
337     indent++;
338     if (implicit_handle)
339         print_client("&%s,\n", implicit_handle);
340     else
341         print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
342     indent--;
343     print_client("},\n");
344     print_client("0,\n");
345     print_client("0,\n");
346     if (expr_eval_routines)
347         print_client("ExprEvalRoutines,\n");
348     else
349         print_client("0,\n");
350     print_client("0,\n");
351     print_client("__MIDL_TypeFormatString.Format,\n");
352     print_client("1, /* -error bounds_check flag */\n");
353     print_client("0x10001, /* Ndr library version */\n");
354     print_client("0,\n");
355     print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
356     print_client("0,\n");
357     print_client("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
358     print_client("0,  /* notify & notify_flag routine table */\n");
359     print_client("1,  /* Flags */\n");
360     print_client("0,  /* Reserved3 */\n");
361     print_client("0,  /* Reserved4 */\n");
362     print_client("0   /* Reserved5 */\n");
363     indent--;
364     print_client("};\n");
365     fprintf(client, "\n");
366 }
367
368
369 static void write_clientinterfacedecl(type_t *iface)
370 {
371     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
372     const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
373     const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
374
375     if (endpoints) write_endpoints( client, iface->name, endpoints );
376
377     print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
378     print_client("{\n");
379     indent++;
380     print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
381     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",
382                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
383                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
384                  uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
385     print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
386     print_client("0,\n");
387     if (endpoints)
388     {
389         print_client("%u,\n", list_count(endpoints));
390         print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
391     }
392     else
393     {
394         print_client("0,\n");
395         print_client("0,\n");
396     }
397     print_client("0,\n");
398     print_client("0,\n");
399     print_client("0,\n");
400     indent--;
401     print_client("};\n");
402     if (old_names)
403         print_client("RPC_IF_HANDLE %s_ClientIfHandle = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
404                      iface->name, iface->name);
405     else
406         print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
407                      prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
408     fprintf(client, "\n");
409 }
410
411
412 static void write_implicithandledecl(type_t *iface)
413 {
414     const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
415
416     if (implicit_handle)
417     {
418         fprintf(client, "handle_t %s;\n", implicit_handle);
419         fprintf(client, "\n");
420     }
421 }
422
423
424 static void init_client(void)
425 {
426     if (client) return;
427     if (!(client = fopen(client_name, "w")))
428         error("Could not open %s for output\n", client_name);
429
430     print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
431     print_client("#include <string.h>\n");
432     print_client("#ifdef _ALPHA_\n");
433     print_client("#include <stdarg.h>\n");
434     print_client("#endif\n");
435     fprintf(client, "\n");
436     print_client("#include \"%s\"\n", header_name);
437     fprintf(client, "\n");
438 }
439
440
441 static void write_client_ifaces(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
442 {
443     const statement_t *stmt;
444     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
445     {
446         if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
447         {
448             type_t *iface = stmt->u.type;
449             if (!need_stub(iface))
450                 return;
451
452             fprintf(client, "/*****************************************************************************\n");
453             fprintf(client, " * %s interface\n", iface->name);
454             fprintf(client, " */\n");
455             fprintf(client, "\n");
456
457             if (iface->funcs)
458             {
459                 write_implicithandledecl(iface);
460
461                 write_clientinterfacedecl(iface);
462                 write_stubdescdecl(iface);
463                 write_function_stubs(iface, proc_offset);
464
465                 print_client("#if !defined(__RPC_WIN32__)\n");
466                 print_client("#error  Invalid build platform for this stub.\n");
467                 print_client("#endif\n");
468
469                 fprintf(client, "\n");
470                 write_stubdescriptor(iface, expr_eval_routines);
471             }
472         }
473         else if (stmt->type == STMT_LIBRARY)
474             write_client_ifaces(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
475     }
476 }
477
478 void write_client(const statement_list_t *stmts)
479 {
480     unsigned int proc_offset = 0;
481     int expr_eval_routines;
482
483     if (!do_client)
484         return;
485     if (do_everything && !need_stub_files(stmts))
486         return;
487
488     init_client();
489     if (!client)
490         return;
491
492     write_formatstringsdecl(client, indent, stmts, need_stub);
493     expr_eval_routines = write_expr_eval_routines(client, client_token);
494     if (expr_eval_routines)
495         write_expr_eval_routine_list(client, client_token);
496     write_user_quad_list(client);
497
498     write_client_ifaces(stmts, expr_eval_routines, &proc_offset);
499
500     fprintf(client, "\n");
501
502     write_procformatstring(client, stmts, need_stub);
503     write_typeformatstring(client, stmts, need_stub);
504
505     fclose(client);
506 }