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