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