setupapi: Make sure machine name is non-empty before failing.
[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_client_func_decl( const type_t *iface, const var_t *func )
76 {
77     const char *callconv = get_attrp(func->type->attrs, ATTR_CALLCONV);
78     const var_list_t *args = type_get_function_args(func->type);
79     type_t *rettype = type_function_get_rettype(func->type);
80
81     write_type_decl_left(client, rettype);
82     if (needs_space_after(rettype)) fprintf(client, " ");
83     if (callconv) fprintf(client, "%s ", callconv);
84     fprintf(client, "%s%s(\n", prefix_client, get_name(func));
85     indent++;
86     if (args)
87         write_args(client, args, iface->name, 0, TRUE);
88     else
89         print_client("void");
90     fprintf(client, ")\n");
91     indent--;
92 }
93
94 static void write_function_stub( const type_t *iface, const var_t *func,
95                                  int method_count, unsigned int proc_offset )
96 {
97     unsigned char explicit_fc, implicit_fc;
98     int has_full_pointer = is_full_pointer_function(func);
99     type_t *rettype = type_function_get_rettype(func->type);
100     const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
101     int has_ret = !is_void(rettype);
102
103     if (is_interpreted_func( iface, func ))
104     {
105         write_client_func_decl( iface, func );
106         write_client_call_routine( client, iface, func, iface->name, proc_offset );
107         return;
108     }
109
110     print_client( "struct __frame_%s%s\n{\n", prefix_client, get_name(func) );
111     indent++;
112     print_client( "__DECL_EXCEPTION_FRAME\n" );
113     print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
114     if (handle_var)
115     {
116         if (explicit_fc == RPC_FC_BIND_GENERIC)
117             print_client("%s %s;\n",
118                          get_explicit_generic_handle_type(handle_var)->name, handle_var->name );
119         print_client("RPC_BINDING_HANDLE _Handle;\n");
120     }
121
122     if (has_ret && decl_indirect(rettype))
123     {
124         print_client("void *_p_%s;\n", "_RetVal" );
125     }
126     indent--;
127     print_client( "};\n\n" );
128
129     print_client( "static void __finally_%s%s(", prefix_client, get_name(func) );
130     print_client( " struct __frame_%s%s *__frame )\n{\n", prefix_client, get_name(func) );
131     indent++;
132
133     if (has_full_pointer)
134         write_full_pointer_free(client, indent, func);
135
136     print_client("NdrFreeBuffer(&__frame->_StubMsg);\n");
137
138     if (explicit_fc == RPC_FC_BIND_GENERIC)
139     {
140         fprintf(client, "\n");
141         print_client("if (__frame->_Handle)\n");
142         indent++;
143         print_client("%s_unbind(__frame->%s, __frame->_Handle);\n",
144                      get_explicit_generic_handle_type(handle_var)->name, handle_var->name);
145         indent--;
146     }
147     indent--;
148     print_client( "}\n\n" );
149
150     write_client_func_decl( iface, func );
151
152     /* write the functions body */
153     fprintf(client, "{\n");
154     indent++;
155     print_client( "struct __frame_%s%s __f, * const __frame = &__f;\n", prefix_client, get_name(func) );
156
157     /* declare return value '_RetVal' */
158     if (has_ret)
159     {
160         print_client("%s", "");
161         write_type_decl_left(client, rettype);
162         fprintf(client, " _RetVal;\n");
163     }
164     print_client("RPC_MESSAGE _RpcMessage;\n");
165
166     if (handle_var)
167     {
168         print_client( "__frame->_Handle = 0;\n" );
169         if (explicit_fc == RPC_FC_BIND_GENERIC)
170             print_client("__frame->%s = %s;\n", handle_var->name, handle_var->name );
171     }
172     if (has_ret && decl_indirect(rettype))
173     {
174         print_client("__frame->_p_%s = &%s;\n",
175                      "_RetVal", "_RetVal");
176     }
177     fprintf(client, "\n");
178
179     print_client( "RpcExceptionInit( 0, __finally_%s%s );\n", prefix_client, get_name(func) );
180
181     if (has_full_pointer)
182         write_full_pointer_init(client, indent, func, FALSE);
183
184     /* check pointers */
185     check_pointers(func);
186
187     print_client("RpcTryFinally\n");
188     print_client("{\n");
189     indent++;
190
191     print_client("NdrClientInitializeNew(&_RpcMessage, &__frame->_StubMsg, &%s_StubDesc, %d);\n",
192                  iface->name, method_count);
193
194     if (is_attr(func->attrs, ATTR_IDEMPOTENT) || is_attr(func->attrs, ATTR_BROADCAST))
195     {
196         print_client("_RpcMessage.RpcFlags = ( RPC_NCA_FLAGS_DEFAULT ");
197         if (is_attr(func->attrs, ATTR_IDEMPOTENT))
198             fprintf(client, "| RPC_NCA_FLAGS_IDEMPOTENT ");
199         if (is_attr(func->attrs, ATTR_BROADCAST))
200             fprintf(client, "| RPC_NCA_FLAGS_BROADCAST ");
201         fprintf(client, ");\n\n");
202     }
203
204     switch (explicit_fc)
205     {
206     case RPC_FC_BIND_PRIMITIVE:
207         print_client("__frame->_Handle = %s;\n", handle_var->name);
208         fprintf(client, "\n");
209         break;
210     case RPC_FC_BIND_GENERIC:
211         print_client("__frame->_Handle = %s_bind(%s);\n",
212                      get_explicit_generic_handle_type(handle_var)->name, handle_var->name);
213         fprintf(client, "\n");
214         break;
215     case RPC_FC_BIND_CONTEXT:
216     {
217         /* if the context_handle attribute appears in the chain of types
218          * without pointers being followed, then the context handle must
219          * be direct, otherwise it is a pointer */
220         int is_ch_ptr = !is_aliaschain_attr(handle_var->type, ATTR_CONTEXTHANDLE);
221         print_client("if (%s%s != 0)\n", is_ch_ptr ? "*" : "", handle_var->name);
222         indent++;
223         print_client("__frame->_Handle = NDRCContextBinding(%s%s);\n",
224                      is_ch_ptr ? "*" : "", handle_var->name);
225         indent--;
226         if (is_attr(handle_var->attrs, ATTR_IN) && !is_attr(handle_var->attrs, ATTR_OUT))
227         {
228             print_client("else\n");
229             indent++;
230             print_client("RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);\n");
231             indent--;
232         }
233         fprintf(client, "\n");
234         break;
235     }
236     case 0:  /* implicit handle */
237         if (handle_var)
238         {
239             print_client("__frame->_Handle = %s;\n", handle_var->name);
240             fprintf(client, "\n");
241         }
242         break;
243     }
244
245     write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
246
247     print_client("NdrGetBuffer(&__frame->_StubMsg, __frame->_StubMsg.BufferLength, ");
248     if (handle_var)
249         fprintf(client, "__frame->_Handle);\n\n");
250     else
251         fprintf(client,"%s__MIDL_AutoBindHandle);\n\n", iface->name);
252
253     /* marshal arguments */
254     write_remoting_arguments(client, indent, func, "", PASS_IN, PHASE_MARSHAL);
255
256     /* send/receive message */
257     /* print_client("NdrNsSendReceive(\n"); */
258     /* print_client("(unsigned char *)__frame->_StubMsg.Buffer,\n"); */
259     /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
260     print_client("NdrSendReceive(&__frame->_StubMsg, __frame->_StubMsg.Buffer);\n\n");
261
262     print_client("__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n");
263     print_client("__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
264
265     if (has_out_arg_or_return(func))
266     {
267         fprintf(client, "\n");
268
269         print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
270         indent++;
271         print_client("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
272                      proc_offset);
273         indent--;
274     }
275
276     /* unmarshall arguments */
277     fprintf(client, "\n");
278     write_remoting_arguments(client, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
279
280     /* unmarshal return value */
281     if (has_ret)
282     {
283         if (decl_indirect(rettype))
284             print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
285         else if (is_ptr(rettype) || is_array(rettype))
286             print_client("%s = 0;\n", "_RetVal");
287         write_remoting_arguments(client, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
288     }
289
290     indent--;
291     print_client("}\n");
292     print_client("RpcFinally\n");
293     print_client("{\n");
294     indent++;
295     print_client( "__finally_%s%s( __frame );\n", prefix_client, get_name(func) );
296     indent--;
297     print_client("}\n");
298     print_client("RpcEndFinally\n");
299
300
301     /* emit return code */
302     if (has_ret)
303     {
304         fprintf(client, "\n");
305         print_client("return _RetVal;\n");
306     }
307
308     indent--;
309     fprintf(client, "}\n");
310     fprintf(client, "\n");
311 }
312
313 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
314 {
315     const statement_t *stmt;
316     const var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
317     int method_count = 0;
318
319     if (!implicit_handle)
320         print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n\n", iface->name);
321
322     STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
323     {
324         const var_t *func = stmt->u.var;
325         write_function_stub( iface, func, method_count++, *proc_offset );
326         *proc_offset += get_size_procformatstring_func( iface, func );
327     }
328 }
329
330
331 static void write_stubdescdecl(type_t *iface)
332 {
333     print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
334     fprintf(client, "\n");
335 }
336
337
338 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
339 {
340     const var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
341
342     print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
343     print_client("{\n");
344     indent++;
345     print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
346     print_client("MIDL_user_allocate,\n");
347     print_client("MIDL_user_free,\n");
348     print_client("{\n");
349     indent++;
350     if (implicit_handle)
351         print_client("&%s,\n", implicit_handle->name);
352     else
353         print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
354     indent--;
355     print_client("},\n");
356     print_client("0,\n");
357     if (!list_empty( &generic_handle_list ))
358         print_client("BindingRoutines,\n");
359     else
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("0x%x, /* Ndr library version */\n", get_stub_mode() == MODE_Oif ? 0x50002 : 0x10001);
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 int 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%08x,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 DECLSPEC_HIDDEN = (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 DECLSPEC_HIDDEN = (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 var_t *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
430
431     if (implicit_handle)
432     {
433         write_type_decl( client, implicit_handle->type, implicit_handle->name );
434         fprintf(client, ";\n\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( "\n");
448     print_client("#include \"%s\"\n", header_name);
449     print_client( "\n");
450     print_client( "#ifndef DECLSPEC_HIDDEN\n");
451     print_client( "#define DECLSPEC_HIDDEN\n");
452     print_client( "#endif\n");
453     print_client( "\n");
454 }
455
456
457 static void write_client_ifaces(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
458 {
459     const statement_t *stmt;
460     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
461     {
462         if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
463         {
464             int has_func = 0;
465             const statement_t *stmt2;
466             type_t *iface = stmt->u.type;
467             if (!need_stub(iface))
468                 return;
469
470             fprintf(client, "/*****************************************************************************\n");
471             fprintf(client, " * %s interface\n", iface->name);
472             fprintf(client, " */\n");
473             fprintf(client, "\n");
474
475             STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
476             {
477                 has_func = 1;
478                 break;
479             }
480
481             if (has_func)
482             {
483                 write_implicithandledecl(iface);
484
485                 write_clientinterfacedecl(iface);
486                 write_stubdescdecl(iface);
487                 write_function_stubs(iface, proc_offset);
488
489                 print_client("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
490                 print_client("#error  Invalid build platform for this stub.\n");
491                 print_client("#endif\n");
492
493                 fprintf(client, "\n");
494                 write_stubdescriptor(iface, expr_eval_routines);
495             }
496         }
497         else if (stmt->type == STMT_LIBRARY)
498             write_client_ifaces(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
499     }
500 }
501
502 static void write_generic_handle_routine_list(void)
503 {
504     generic_handle_t *gh;
505
506     if (list_empty( &generic_handle_list )) return;
507     print_client( "static const GENERIC_BINDING_ROUTINE_PAIR BindingRoutines[] =\n" );
508     print_client( "{\n" );
509     indent++;
510     LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
511     {
512         print_client( "{ (GENERIC_BINDING_ROUTINE)%s_bind, (GENERIC_UNBIND_ROUTINE)%s_unbind },\n",
513                       gh->name, gh->name );
514     }
515     indent--;
516     print_client( "};\n\n" );
517 }
518
519 static void write_client_routines(const statement_list_t *stmts)
520 {
521     unsigned int proc_offset = 0;
522     int expr_eval_routines;
523
524     if (need_inline_stubs_file( stmts ))
525     {
526         write_exceptions( client );
527         print_client( "\n");
528     }
529
530     write_formatstringsdecl(client, indent, stmts, need_stub);
531     expr_eval_routines = write_expr_eval_routines(client, client_token);
532     if (expr_eval_routines)
533         write_expr_eval_routine_list(client, client_token);
534     write_generic_handle_routine_list();
535     write_user_quad_list(client);
536
537     write_client_ifaces(stmts, expr_eval_routines, &proc_offset);
538
539     fprintf(client, "\n");
540
541     write_procformatstring(client, stmts, need_stub);
542     write_typeformatstring(client, stmts, need_stub);
543 }
544
545 void write_client(const statement_list_t *stmts)
546 {
547     if (!do_client)
548         return;
549     if (do_everything && !need_stub_files(stmts))
550         return;
551
552     init_client();
553     if (!client)
554         return;
555
556     if (do_win32 && do_win64)
557     {
558         fprintf(client, "#ifndef _WIN64\n\n");
559         pointer_size = 4;
560         write_client_routines( stmts );
561         fprintf(client, "\n#else /* _WIN64 */\n\n");
562         pointer_size = 8;
563         write_client_routines( stmts );
564         fprintf(client, "\n#endif /* _WIN64 */\n");
565     }
566     else if (do_win32)
567     {
568         pointer_size = 4;
569         write_client_routines( stmts );
570     }
571     else if (do_win64)
572     {
573         pointer_size = 8;
574         write_client_routines( stmts );
575     }
576
577     fclose(client);
578 }