mshtml: Added nsIURI::Equals implementation on URIs without necko interface associated.
[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
40 static FILE* client;
41 static int indent = 0;
42
43 static void print_client( const char *format, ... )
44 {
45     va_list va;
46     va_start(va, format);
47     print(client, indent, format, va);
48     va_end(va);
49 }
50
51
52 static void check_pointers(const func_t *func)
53 {
54     const var_t *var;
55
56     if (!func->args)
57         return;
58
59     LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
60     {
61         if (is_var_ptr(var) && cant_be_null(var))
62         {
63             print_client("if (!%s)\n", var->name);
64             print_client("{\n");
65             indent++;
66             print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
67             indent--;
68             print_client("}\n\n");
69         }
70     }
71 }
72
73 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
74 {
75     const func_t *func;
76     const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
77     int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_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         int has_full_pointer = is_full_pointer_function(func);
89
90         /* check for a defined binding handle */
91         explicit_handle_var = get_explicit_handle_var(func);
92         if (explicit_handle)
93         {
94             if (!explicit_handle_var)
95             {
96                 error("%s() does not define an explicit binding handle!\n", def->name);
97                 return;
98             }
99         }
100         else if (implicit_handle)
101         {
102             if (explicit_handle_var)
103             {
104                 error("%s() must not define a binding handle!\n", def->name);
105                 return;
106             }
107         }
108
109         write_type_decl_left(client, def->type);
110         if (needs_space_after(def->type))
111           fprintf(client, " ");
112         write_prefix_name(client, prefix_client, def);
113         fprintf(client, "(\n");
114         indent++;
115         if (func->args)
116             write_args(client, func->args, iface->name, 0, TRUE);
117         else
118             print_client("void");
119         fprintf(client, ")\n");
120         indent--;
121
122         /* write the functions body */
123         fprintf(client, "{\n");
124         indent++;
125
126         /* declare return value '_RetVal' */
127         if (!is_void(def->type))
128         {
129             print_client("");
130             write_type_decl_left(client, def->type);
131             fprintf(client, " _RetVal;\n");
132         }
133
134         if (implicit_handle || explicit_handle_var)
135             print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
136
137         print_client("RPC_MESSAGE _RpcMessage;\n");
138         print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
139         if (!is_void(def->type) && decl_indirect(def->type))
140         {
141             print_client("void *_p_%s = &%s;\n",
142                          "_RetVal", "_RetVal");
143         }
144         fprintf(client, "\n");
145
146         if (has_full_pointer)
147             write_full_pointer_init(client, indent, func, FALSE);
148
149         /* check pointers */
150         check_pointers(func);
151
152         print_client("RpcTryFinally\n");
153         print_client("{\n");
154         indent++;
155
156         print_client("NdrClientInitializeNew(\n");
157         indent++;
158         print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
159         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
160         print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
161         print_client("%d);\n", method_count);
162         indent--;
163         fprintf(client, "\n");
164
165         if (implicit_handle)
166         {
167             print_client("_Handle = %s;\n", implicit_handle);
168             fprintf(client, "\n");
169         }
170         else if (explicit_handle_var)
171         {
172             print_client("_Handle = %s;\n", explicit_handle_var->name);
173             fprintf(client, "\n");
174         }
175
176         write_remoting_arguments(client, indent, func, PASS_IN, PHASE_BUFFERSIZE);
177
178         print_client("NdrGetBuffer(\n");
179         indent++;
180         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
181         print_client("_StubMsg.BufferLength,\n");
182         if (implicit_handle || explicit_handle_var)
183             print_client("_Handle);\n");
184         else
185             print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
186         indent--;
187         fprintf(client, "\n");
188
189         /* marshal arguments */
190         write_remoting_arguments(client, indent, func, PASS_IN, PHASE_MARSHAL);
191
192         /* send/receive message */
193         /* print_client("NdrNsSendReceive(\n"); */
194         /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
195         /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
196         print_client("NdrSendReceive(\n");
197         indent++;
198         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
199         print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
200         indent--;
201
202         print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
203         print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
204
205         if (has_out_arg_or_return(func))
206         {
207             fprintf(client, "\n");
208
209             print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
210             indent++;
211             print_client("NdrConvert(\n");
212             indent++;
213             print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
214             print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
215             indent -= 2;
216         }
217
218         /* unmarshall arguments */
219         fprintf(client, "\n");
220         write_remoting_arguments(client, indent, func, PASS_OUT, PHASE_UNMARSHAL);
221
222         /* unmarshal return value */
223         if (!is_void(def->type))
224         {
225             if (decl_indirect(def->type))
226                 print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
227             else if (is_ptr(def->type) || is_array(def->type))
228                 print_client("%s = 0;\n", "_RetVal");
229             write_remoting_arguments(client, indent, func, PASS_RETURN, PHASE_UNMARSHAL);
230         }
231
232         /* update proc_offset */
233         if (func->args)
234         {
235             LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
236                 *proc_offset += get_size_procformatstring_var(var);
237         }
238         if (!is_void(def->type))
239             *proc_offset += get_size_procformatstring_var(def);
240         else
241             *proc_offset += 2; /* FC_END and FC_PAD */
242
243         indent--;
244         print_client("}\n");
245         print_client("RpcFinally\n");
246         print_client("{\n");
247         indent++;
248
249
250         /* FIXME: emit client finally code */
251
252         if (has_full_pointer)
253             write_full_pointer_free(client, indent, func);
254
255         print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
256
257         indent--;
258         print_client("}\n");
259         print_client("RpcEndFinally\n");
260
261
262         /* emit return code */
263         if (!is_void(def->type))
264         {
265             fprintf(client, "\n");
266             print_client("return _RetVal;\n");
267         }
268
269         indent--;
270         fprintf(client, "}\n");
271         fprintf(client, "\n");
272
273         method_count++;
274     }
275 }
276
277
278 static void write_stubdescdecl(type_t *iface)
279 {
280     print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
281     fprintf(client, "\n");
282 }
283
284
285 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
286 {
287     const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
288
289     print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
290     print_client("{\n");
291     indent++;
292     print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
293     print_client("MIDL_user_allocate,\n");
294     print_client("MIDL_user_free,\n");
295     print_client("{\n");
296     indent++;
297     if (implicit_handle)
298         print_client("&%s,\n", implicit_handle);
299     else
300         print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
301     indent--;
302     print_client("},\n");
303     print_client("0,\n");
304     print_client("0,\n");
305     if (expr_eval_routines)
306         print_client("ExprEvalRoutines,\n");
307     else
308         print_client("0,\n");
309     print_client("0,\n");
310     print_client("__MIDL_TypeFormatString.Format,\n");
311     print_client("1, /* -error bounds_check flag */\n");
312     print_client("0x10001, /* Ndr library version */\n");
313     print_client("0,\n");
314     print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
315     print_client("0,\n");
316     print_client("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
317     print_client("0,  /* notify & notify_flag routine table */\n");
318     print_client("1,  /* Flags */\n");
319     print_client("0,  /* Reserved3 */\n");
320     print_client("0,  /* Reserved4 */\n");
321     print_client("0   /* Reserved5 */\n");
322     indent--;
323     print_client("};\n");
324     fprintf(client, "\n");
325 }
326
327
328 static void write_clientinterfacedecl(type_t *iface)
329 {
330     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
331     const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
332     const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
333
334     if (endpoints) write_endpoints( client, iface->name, endpoints );
335
336     print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
337     print_client("{\n");
338     indent++;
339     print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
340     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",
341                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
342                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
343                  uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
344     print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
345     print_client("0,\n");
346     if (endpoints)
347     {
348         print_client("%u,\n", list_count(endpoints));
349         print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
350     }
351     else
352     {
353         print_client("0,\n");
354         print_client("0,\n");
355     }
356     print_client("0,\n");
357     print_client("0,\n");
358     print_client("0,\n");
359     indent--;
360     print_client("};\n");
361     if (old_names)
362         print_client("RPC_IF_HANDLE %s_ClientIfHandle = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
363                      iface->name, iface->name);
364     else
365         print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
366                      prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
367     fprintf(client, "\n");
368 }
369
370
371 static void write_implicithandledecl(type_t *iface)
372 {
373     const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
374
375     if (implicit_handle)
376     {
377         fprintf(client, "handle_t %s;\n", implicit_handle);
378         fprintf(client, "\n");
379     }
380 }
381
382
383 static void init_client(void)
384 {
385     if (client) return;
386     if (!(client = fopen(client_name, "w")))
387         error("Could not open %s for output\n", client_name);
388
389     print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
390     print_client("#include <string.h>\n");
391     print_client("#ifdef _ALPHA_\n");
392     print_client("#include <stdarg.h>\n");
393     print_client("#endif\n");
394     fprintf(client, "\n");
395     print_client("#include \"%s\"\n", header_name);
396     fprintf(client, "\n");
397 }
398
399
400 void write_client(ifref_list_t *ifaces)
401 {
402     unsigned int proc_offset = 0;
403     int expr_eval_routines;
404     ifref_t *iface;
405
406     if (!do_client)
407         return;
408     if (do_everything && !need_stub_files(ifaces))
409         return;
410
411     init_client();
412     if (!client)
413         return;
414
415     write_formatstringsdecl(client, indent, ifaces, need_stub);
416     expr_eval_routines = write_expr_eval_routines(client, client_token);
417     if (expr_eval_routines)
418         write_expr_eval_routine_list(client, client_token);
419     write_user_quad_list(client);
420
421     if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, ifref_t, entry )
422     {
423         if (!need_stub(iface->iface))
424             continue;
425
426         fprintf(client, "/*****************************************************************************\n");
427         fprintf(client, " * %s interface\n", iface->iface->name);
428         fprintf(client, " */\n");
429         fprintf(client, "\n");
430
431         if (iface->iface->funcs)
432         {
433             write_implicithandledecl(iface->iface);
434     
435             write_clientinterfacedecl(iface->iface);
436             write_stubdescdecl(iface->iface);
437             write_function_stubs(iface->iface, &proc_offset);
438
439             print_client("#if !defined(__RPC_WIN32__)\n");
440             print_client("#error  Invalid build platform for this stub.\n");
441             print_client("#endif\n");
442
443             fprintf(client, "\n");
444             write_stubdescriptor(iface->iface, expr_eval_routines);
445         }
446     }
447
448     fprintf(client, "\n");
449
450     write_procformatstring(client, ifaces, need_stub);
451     write_typeformatstring(client, ifaces, need_stub);
452
453     fclose(client);
454 }