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