shell32: Add support of KF_REDIRECT_COPY_CONTENTS flag to redirection.
[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, ... ) __attribute__((format (printf, 1, 2)));
45 static void print_client( const char *format, ... )
46 {
47     va_list va;
48     va_start(va, format);
49     print(client, indent, format, va);
50     va_end(va);
51 }
52
53 static void write_client_func_decl( const type_t *iface, const var_t *func )
54 {
55     const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
56     const var_list_t *args = type_get_function_args(func->type);
57     type_t *rettype = type_function_get_rettype(func->type);
58
59     if (!callconv) callconv = "__cdecl";
60     write_type_decl_left(client, rettype);
61     fprintf(client, " %s ", callconv);
62     fprintf(client, "%s%s(\n", prefix_client, get_name(func));
63     indent++;
64     if (args)
65         write_args(client, args, iface->name, 0, TRUE);
66     else
67         print_client("void");
68     fprintf(client, ")\n");
69     indent--;
70 }
71
72 static void write_function_stub( const type_t *iface, const var_t *func,
73                                  int method_count, unsigned int proc_offset )
74 {
75     unsigned char explicit_fc, implicit_fc;
76     int has_full_pointer = is_full_pointer_function(func);
77     type_t *rettype = type_function_get_rettype(func->type);
78     const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
79     int has_ret = !is_void(rettype);
80
81     if (is_interpreted_func( iface, func ))
82     {
83         write_client_func_decl( iface, func );
84         write_client_call_routine( client, iface, func, iface->name, proc_offset );
85         return;
86     }
87
88     print_client( "struct __frame_%s%s\n{\n", prefix_client, get_name(func) );
89     indent++;
90     print_client( "__DECL_EXCEPTION_FRAME\n" );
91     print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
92     if (handle_var)
93     {
94         if (explicit_fc == RPC_FC_BIND_GENERIC)
95             print_client("%s %s;\n",
96                          get_explicit_generic_handle_type(handle_var)->name, handle_var->name );
97         print_client("RPC_BINDING_HANDLE _Handle;\n");
98     }
99
100     if (has_ret && decl_indirect(rettype))
101     {
102         print_client("void *_p_%s;\n", "_RetVal" );
103     }
104     indent--;
105     print_client( "};\n\n" );
106
107     print_client( "static void __finally_%s%s(", prefix_client, get_name(func) );
108     print_client( " struct __frame_%s%s *__frame )\n{\n", prefix_client, get_name(func) );
109     indent++;
110
111     if (has_full_pointer)
112         write_full_pointer_free(client, indent, func);
113
114     print_client("NdrFreeBuffer(&__frame->_StubMsg);\n");
115
116     if (explicit_fc == RPC_FC_BIND_GENERIC)
117     {
118         fprintf(client, "\n");
119         print_client("if (__frame->_Handle)\n");
120         indent++;
121         print_client("%s_unbind(__frame->%s, __frame->_Handle);\n",
122                      get_explicit_generic_handle_type(handle_var)->name, handle_var->name);
123         indent--;
124     }
125     indent--;
126     print_client( "}\n\n" );
127
128     write_client_func_decl( iface, func );
129
130     /* write the functions body */
131     fprintf(client, "{\n");
132     indent++;
133     print_client( "struct __frame_%s%s __f, * const __frame = &__f;\n", prefix_client, get_name(func) );
134
135     /* declare return value '_RetVal' */
136     if (has_ret)
137     {
138         print_client("%s", "");
139         write_type_decl_left(client, rettype);
140         fprintf(client, " _RetVal;\n");
141     }
142     print_client("RPC_MESSAGE _RpcMessage;\n");
143
144     if (handle_var)
145     {
146         print_client( "__frame->_Handle = 0;\n" );
147         if (explicit_fc == RPC_FC_BIND_GENERIC)
148             print_client("__frame->%s = %s;\n", handle_var->name, handle_var->name );
149     }
150     if (has_ret && decl_indirect(rettype))
151     {
152         print_client("__frame->_p_%s = &%s;\n",
153                      "_RetVal", "_RetVal");
154     }
155     fprintf(client, "\n");
156
157     print_client( "RpcExceptionInit( 0, __finally_%s%s );\n", prefix_client, get_name(func) );
158
159     if (has_full_pointer)
160         write_full_pointer_init(client, indent, func, FALSE);
161
162     /* check pointers */
163     write_pointer_checks( client, indent, func );
164
165     print_client("RpcTryFinally\n");
166     print_client("{\n");
167     indent++;
168
169     print_client("NdrClientInitializeNew(&_RpcMessage, &__frame->_StubMsg, &%s_StubDesc, %d);\n",
170                  iface->name, method_count);
171
172     if (is_attr(func->attrs, ATTR_IDEMPOTENT) || is_attr(func->attrs, ATTR_BROADCAST))
173     {
174         print_client("_RpcMessage.RpcFlags = ( RPC_NCA_FLAGS_DEFAULT ");
175         if (is_attr(func->attrs, ATTR_IDEMPOTENT))
176             fprintf(client, "| RPC_NCA_FLAGS_IDEMPOTENT ");
177         if (is_attr(func->attrs, ATTR_BROADCAST))
178             fprintf(client, "| RPC_NCA_FLAGS_BROADCAST ");
179         fprintf(client, ");\n\n");
180     }
181
182     switch (explicit_fc)
183     {
184     case RPC_FC_BIND_PRIMITIVE:
185         print_client("__frame->_Handle = %s;\n", handle_var->name);
186         fprintf(client, "\n");
187         break;
188     case RPC_FC_BIND_GENERIC:
189         print_client("__frame->_Handle = %s_bind(%s);\n",
190                      get_explicit_generic_handle_type(handle_var)->name, handle_var->name);
191         fprintf(client, "\n");
192         break;
193     case RPC_FC_BIND_CONTEXT:
194     {
195         /* if the context_handle attribute appears in the chain of types
196          * without pointers being followed, then the context handle must
197          * be direct, otherwise it is a pointer */
198         int is_ch_ptr = !is_aliaschain_attr(handle_var->type, ATTR_CONTEXTHANDLE);
199         print_client("if (%s%s != 0)\n", is_ch_ptr ? "*" : "", handle_var->name);
200         indent++;
201         print_client("__frame->_Handle = NDRCContextBinding(%s%s);\n",
202                      is_ch_ptr ? "*" : "", handle_var->name);
203         indent--;
204         if (is_attr(handle_var->attrs, ATTR_IN) && !is_attr(handle_var->attrs, ATTR_OUT))
205         {
206             print_client("else\n");
207             indent++;
208             print_client("RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);\n");
209             indent--;
210         }
211         fprintf(client, "\n");
212         break;
213     }
214     case 0:  /* implicit handle */
215         if (handle_var)
216         {
217             print_client("__frame->_Handle = %s;\n", handle_var->name);
218             fprintf(client, "\n");
219         }
220         break;
221     }
222
223     write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
224
225     print_client("NdrGetBuffer(&__frame->_StubMsg, __frame->_StubMsg.BufferLength, ");
226     if (handle_var)
227         fprintf(client, "__frame->_Handle);\n\n");
228     else
229         fprintf(client,"%s__MIDL_AutoBindHandle);\n\n", iface->name);
230
231     /* marshal arguments */
232     write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_MARSHAL);
233
234     /* send/receive message */
235     /* print_client("NdrNsSendReceive(\n"); */
236     /* print_client("(unsigned char *)__frame->_StubMsg.Buffer,\n"); */
237     /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
238     print_client("NdrSendReceive(&__frame->_StubMsg, __frame->_StubMsg.Buffer);\n\n");
239
240     print_client("__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n");
241     print_client("__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
242
243     if (has_out_arg_or_return(func))
244     {
245         fprintf(client, "\n");
246
247         print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
248         indent++;
249         print_client("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
250                      proc_offset);
251         indent--;
252     }
253
254     /* unmarshall arguments */
255     fprintf(client, "\n");
256     write_remoting_arguments(client, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
257
258     /* unmarshal return value */
259     if (has_ret)
260     {
261         if (decl_indirect(rettype))
262             print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
263         else if (is_ptr(rettype) || is_array(rettype))
264             print_client("%s = 0;\n", "_RetVal");
265         write_remoting_arguments(client, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
266     }
267
268     indent--;
269     print_client("}\n");
270     print_client("RpcFinally\n");
271     print_client("{\n");
272     indent++;
273     print_client( "__finally_%s%s( __frame );\n", prefix_client, get_name(func) );
274     indent--;
275     print_client("}\n");
276     print_client("RpcEndFinally\n");
277
278
279     /* emit return code */
280     if (has_ret)
281     {
282         fprintf(client, "\n");
283         print_client("return _RetVal;\n");
284     }
285
286     indent--;
287     fprintf(client, "}\n");
288     fprintf(client, "\n");
289 }
290
291 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
292 {
293     const statement_t *stmt;
294     const var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
295     int method_count = 0;
296
297     if (!implicit_handle)
298         print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n\n", iface->name);
299
300     STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
301     {
302         const var_t *func = stmt->u.var;
303         write_function_stub( iface, func, method_count++, *proc_offset );
304         *proc_offset += get_size_procformatstring_func( iface, func );
305     }
306 }
307
308
309 static void write_stubdescdecl(type_t *iface)
310 {
311     print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
312     fprintf(client, "\n");
313 }
314
315
316 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
317 {
318     const var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
319
320     print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
321     print_client("{\n");
322     indent++;
323     print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
324     print_client("MIDL_user_allocate,\n");
325     print_client("MIDL_user_free,\n");
326     print_client("{\n");
327     indent++;
328     if (implicit_handle)
329         print_client("&%s,\n", implicit_handle->name);
330     else
331         print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
332     indent--;
333     print_client("},\n");
334     print_client("0,\n");
335     if (!list_empty( &generic_handle_list ))
336         print_client("BindingRoutines,\n");
337     else
338         print_client("0,\n");
339     if (expr_eval_routines)
340         print_client("ExprEvalRoutines,\n");
341     else
342         print_client("0,\n");
343     print_client("0,\n");
344     print_client("__MIDL_TypeFormatString.Format,\n");
345     print_client("1, /* -error bounds_check flag */\n");
346     print_client("0x%x, /* Ndr library version */\n", get_stub_mode() == MODE_Oif ? 0x50002 : 0x10001);
347     print_client("0,\n");
348     print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
349     print_client("0,\n");
350     print_client("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
351     print_client("0,  /* notify & notify_flag routine table */\n");
352     print_client("1,  /* Flags */\n");
353     print_client("0,  /* Reserved3 */\n");
354     print_client("0,  /* Reserved4 */\n");
355     print_client("0   /* Reserved5 */\n");
356     indent--;
357     print_client("};\n");
358     fprintf(client, "\n");
359 }
360
361
362 static void write_clientinterfacedecl(type_t *iface)
363 {
364     unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
365     const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
366     const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
367
368     if (endpoints) write_endpoints( client, iface->name, endpoints );
369
370     print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
371     print_client("{\n");
372     indent++;
373     print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
374     print_client("{{0x%08x,0x%04x,0x%04x,{0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x}},{%d,%d}},\n",
375                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
376                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
377                  uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
378     print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
379     print_client("0,\n");
380     if (endpoints)
381     {
382         print_client("%u,\n", list_count(endpoints));
383         print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
384     }
385     else
386     {
387         print_client("0,\n");
388         print_client("0,\n");
389     }
390     print_client("0,\n");
391     print_client("0,\n");
392     print_client("0,\n");
393     indent--;
394     print_client("};\n");
395     if (old_names)
396         print_client("RPC_IF_HANDLE %s_ClientIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
397                      iface->name, iface->name);
398     else
399         print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
400                      prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
401     fprintf(client, "\n");
402 }
403
404
405 static void write_implicithandledecl(type_t *iface)
406 {
407     const var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
408
409     if (implicit_handle)
410     {
411         write_type_decl( client, implicit_handle->type, implicit_handle->name );
412         fprintf(client, ";\n\n");
413     }
414 }
415
416
417 static void init_client(void)
418 {
419     if (client) return;
420     if (!(client = fopen(client_name, "w")))
421         error("Could not open %s for output\n", client_name);
422
423     print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
424     print_client("#include <string.h>\n");
425     print_client( "\n");
426     print_client("#include \"%s\"\n", header_name);
427     print_client( "\n");
428     print_client( "#ifndef DECLSPEC_HIDDEN\n");
429     print_client( "#define DECLSPEC_HIDDEN\n");
430     print_client( "#endif\n");
431     print_client( "\n");
432 }
433
434
435 static void write_client_ifaces(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
436 {
437     const statement_t *stmt;
438     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
439     {
440         if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
441         {
442             int has_func = 0;
443             const statement_t *stmt2;
444             type_t *iface = stmt->u.type;
445             if (!need_stub(iface))
446                 return;
447
448             fprintf(client, "/*****************************************************************************\n");
449             fprintf(client, " * %s interface\n", iface->name);
450             fprintf(client, " */\n");
451             fprintf(client, "\n");
452
453             STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
454             {
455                 has_func = 1;
456                 break;
457             }
458
459             if (has_func)
460             {
461                 write_implicithandledecl(iface);
462
463                 write_clientinterfacedecl(iface);
464                 write_stubdescdecl(iface);
465                 write_function_stubs(iface, proc_offset);
466
467                 print_client("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
468                 print_client("#error  Invalid build platform for this stub.\n");
469                 print_client("#endif\n");
470
471                 fprintf(client, "\n");
472                 write_stubdescriptor(iface, expr_eval_routines);
473             }
474         }
475         else if (stmt->type == STMT_LIBRARY)
476             write_client_ifaces(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
477     }
478 }
479
480 static void write_generic_handle_routine_list(void)
481 {
482     generic_handle_t *gh;
483
484     if (list_empty( &generic_handle_list )) return;
485     print_client( "static const GENERIC_BINDING_ROUTINE_PAIR BindingRoutines[] =\n" );
486     print_client( "{\n" );
487     indent++;
488     LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
489     {
490         print_client( "{ (GENERIC_BINDING_ROUTINE)%s_bind, (GENERIC_UNBIND_ROUTINE)%s_unbind },\n",
491                       gh->name, gh->name );
492     }
493     indent--;
494     print_client( "};\n\n" );
495 }
496
497 static void write_client_routines(const statement_list_t *stmts)
498 {
499     unsigned int proc_offset = 0;
500     int expr_eval_routines;
501
502     if (need_inline_stubs_file( stmts ))
503     {
504         write_exceptions( client );
505         print_client( "\n");
506     }
507
508     write_formatstringsdecl(client, indent, stmts, need_stub);
509     expr_eval_routines = write_expr_eval_routines(client, client_token);
510     if (expr_eval_routines)
511         write_expr_eval_routine_list(client, client_token);
512     write_generic_handle_routine_list();
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
523 void write_client(const statement_list_t *stmts)
524 {
525     if (!do_client)
526         return;
527     if (do_everything && !need_stub_files(stmts))
528         return;
529
530     init_client();
531     if (!client)
532         return;
533
534     if (do_win32 && do_win64)
535     {
536         fprintf(client, "#ifndef _WIN64\n\n");
537         pointer_size = 4;
538         write_client_routines( stmts );
539         fprintf(client, "\n#else /* _WIN64 */\n\n");
540         pointer_size = 8;
541         write_client_routines( stmts );
542         fprintf(client, "\n#endif /* _WIN64 */\n");
543     }
544     else if (do_win32)
545     {
546         pointer_size = 4;
547         write_client_routines( stmts );
548     }
549     else if (do_win64)
550     {
551         pointer_size = 8;
552         write_client_routines( stmts );
553     }
554
555     fclose(client);
556 }