widl: Add support for marshalling and unmarshalling conformant strings.
[wine] / tools / widl / client.c
1 /*
2  * IDL Compiler
3  *
4  * Copyright 2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 #define END_OF_LIST(list)       \
45   do {                          \
46     if (list) {                 \
47       while (NEXT_LINK(list))   \
48         list = NEXT_LINK(list); \
49     }                           \
50   } while(0)
51
52 static FILE* client;
53 static int indent = 0;
54
55 static int print_client( const char *format, ... )
56 {
57     va_list va;
58     int i, r;
59
60     va_start(va, format);
61     for (i = 0; i < indent; i++)
62         fprintf(client, "    ");
63     r = vfprintf(client, format, va);
64     va_end(va);
65     return r;
66 }
67
68
69 static void print_message_buffer_size(func_t *func)
70 {
71     unsigned int total_size = 0;
72
73     if (func->args)
74     {
75         var_t *var = func->args;
76         while (NEXT_LINK(var)) var = NEXT_LINK(var);
77         while (var)
78         {
79             unsigned int alignment = 8;
80     
81             switch (var->type->type)
82             {
83             case RPC_FC_BYTE:
84             case RPC_FC_CHAR:
85             case RPC_FC_USMALL:
86             case RPC_FC_SMALL:
87                 total_size += 1;
88                 alignment = 1;
89                 break;
90     
91             case RPC_FC_WCHAR:
92             case RPC_FC_USHORT:
93             case RPC_FC_SHORT:
94                 total_size += 2 + alignment % 2;
95                 alignment = 2;
96                 break;
97     
98             case RPC_FC_ULONG:
99             case RPC_FC_LONG:
100             case RPC_FC_FLOAT:
101             case RPC_FC_ERROR_STATUS_T:
102                 total_size += 4 + alignment % 4;
103                 alignment = 4;
104                 break;
105     
106             case RPC_FC_HYPER:
107             case RPC_FC_DOUBLE:
108                 total_size += 8 + alignment % 8;
109                 alignment = 8;
110                 break;
111     
112             default:
113                 alignment = 1;
114             }
115     
116             var = PREV_LINK(var);
117         }
118     }
119     fprintf(client, " %u", total_size);
120 }
121
122
123 static void write_function_stubs(type_t *iface)
124 {
125     func_t *func = iface->funcs;
126     char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
127     int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
128     var_t *var;
129     int method_count = 0;
130     unsigned int proc_offset = 0;
131     unsigned int type_offset = 2;
132
133     while (NEXT_LINK(func)) func = NEXT_LINK(func);
134     while (func)
135     {
136         var_t *def = func->def;
137         var_t* explicit_handle_var;
138
139         /* check for a defined binding handle */
140         explicit_handle_var = get_explicit_handle_var(func);
141         if (explicit_handle)
142         {
143             if (!explicit_handle_var)
144             {
145                 error("%s() does not define an explicit binding handle!\n", def->name);
146                 return;
147             }
148         }
149         else if (implicit_handle)
150         {
151             if (explicit_handle_var)
152             {
153                 error("%s() must not define a binding handle!\n", def->name);
154                 return;
155             }
156         }
157
158         write_type(client, def->type, def, def->tname);
159         fprintf(client, " ");
160         write_name(client, def);
161         fprintf(client, "(\n");
162         indent++;
163         if (func->args)
164             write_args(client, func->args, iface->name, 0, TRUE);
165         else
166             print_client("void");
167         fprintf(client, ")\n");
168         indent--;
169
170         /* write the functions body */
171         fprintf(client, "{\n");
172         indent++;
173
174         /* declare return value '_RetVal' */
175         if (!is_void(def->type, NULL))
176         {
177             print_client("");
178             write_type(client, def->type, def, def->tname);
179             fprintf(client, " _RetVal;\n");
180         }
181
182         if (implicit_handle || explicit_handle_var)
183             print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
184
185         print_client("RPC_MESSAGE _RpcMessage;\n");
186         print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
187         fprintf(client, "\n");
188         print_client("RpcTryFinally\n");
189         print_client("{\n");
190         indent++;
191
192         print_client("NdrClientInitializeNew(\n");
193         indent++;
194         print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
195         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
196         print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
197         print_client("%d);\n", method_count);
198         indent--;
199         fprintf(client, "\n");
200
201         if (implicit_handle)
202         {
203             print_client("_Handle = %s;\n", implicit_handle);
204             fprintf(client, "\n");
205         }
206         else if (explicit_handle_var)
207         {
208             print_client("_Handle = %s;\n", explicit_handle_var->name);
209             fprintf(client, "\n");
210         }
211
212         /* emit the message buffer size */
213         print_client("_StubMsg.BufferLength =");
214         print_message_buffer_size(func);
215         fprintf(client, ";\n");
216
217         print_client("NdrGetBuffer(\n");
218         indent++;
219         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
220         print_client("_StubMsg.BufferLength,\n");
221         if (implicit_handle || explicit_handle_var)
222             print_client("_Handle);\n");
223         else
224             print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
225         indent--;
226         fprintf(client, "\n");
227
228
229         /* marshal arguments */
230         marshall_arguments(client, indent, func, &type_offset);
231
232         /* send/receive message */
233         /* print_client("NdrNsSendReceive(\n"); */
234         /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
235         /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
236         print_client("NdrSendReceive(\n");
237         indent++;
238         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
239         print_client("(unsigned char *)_StubMsg.Buffer);\n");
240         indent--;
241
242         print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
243         print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
244
245         /* unmarshal return value */
246         if (!is_void(def->type, NULL))
247         {
248             fprintf(client, "\n");
249
250             print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
251             indent++;
252             print_client("NdrConvert(\n");
253             indent++;
254             print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
255             print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
256             indent -= 2;
257             fprintf(client, "\n");
258
259             print_client("_RetVal = *(");
260             write_type(client, def->type, def, def->tname);
261             fprintf(client, " *)_StubMsg.Buffer;\n");
262             fprintf(client, "_StubMsg.Buffer += sizeof(");
263             write_type(client, def->type, def, def->tname);
264             fprintf(client, ");\n");
265         }
266
267         /* update proc_offset */
268         if (func->args)
269         {
270             var = func->args;
271             while (NEXT_LINK(var)) var = NEXT_LINK(var);
272             while (var)
273             {
274                 proc_offset += get_size_procformatstring_var(var);
275                 var = PREV_LINK(var);
276             }
277         }
278         if (!is_void(def->type, NULL))
279             proc_offset += get_size_procformatstring_var(def);
280
281         indent--;
282         print_client("}\n");
283         print_client("RpcFinally\n");
284         print_client("{\n");
285         indent++;
286
287
288         /* FIXME: emit client finally code */
289
290         print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
291
292         indent--;
293         print_client("}\n");
294         print_client("RpcEndFinally\n");
295
296
297         /* emit return code */
298         if (!is_void(def->type, NULL))
299         {
300             fprintf(client, "\n");
301             print_client("return _RetVal;\n");
302         }
303
304         indent--;
305         fprintf(client, "}\n");
306         fprintf(client, "\n");
307
308         method_count++;
309         func = PREV_LINK(func);
310     }
311 }
312
313
314 static void write_bindinghandledecl(type_t *iface)
315 {
316     print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n", iface->name);
317     fprintf(client, "\n");
318 }
319
320
321 static void write_stubdescdecl(type_t *iface)
322 {
323     print_client("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
324     fprintf(client, "\n");
325 }
326
327
328 static void write_stubdescriptor(type_t *iface)
329 {
330     char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
331
332     print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
333     print_client("{\n");
334     indent++;
335     print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
336     print_client("MIDL_user_allocate,\n");
337     print_client("MIDL_user_free,\n");
338     if (implicit_handle)
339         print_client("&%s,\n", implicit_handle);
340     else
341         print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
342     print_client("0,\n");
343     print_client("0,\n");
344     print_client("0,\n");
345     print_client("0,\n");
346     print_client("__MIDL_TypeFormatString.Format,\n");
347     print_client("1, /* -error bounds_check flag */\n");
348     print_client("0x10001, /* Ndr library version */\n");
349     print_client("0,\n");
350     print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
351     print_client("0,\n");
352     print_client("0,\n");
353     print_client("0,  /* notify & notify_flag routine table */\n");
354     print_client("1,  /* Flags */\n");
355     print_client("0,  /* Reserved3 */\n");
356     print_client("0,  /* Reserved4 */\n");
357     print_client("0   /* Reserved5 */\n");
358     indent--;
359     print_client("};\n");
360     fprintf(client, "\n");
361 }
362
363
364 static void write_clientinterfacedecl(type_t *iface)
365 {
366     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
367     UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
368
369     print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
370     print_client("{\n");
371     indent++;
372     print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
373     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",
374                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
375                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
376                  uuid->Data4[7], LOWORD(ver), HIWORD(ver));
377     print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
378     print_client("0,\n");
379     print_client("0,\n");
380     print_client("0,\n");
381     print_client("0,\n");
382     print_client("0,\n");
383     print_client("0,\n");
384     indent--;
385     print_client("};\n");
386     print_client("RPC_IF_HANDLE %s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
387                  iface->name, LOWORD(ver), HIWORD(ver), iface->name);
388     fprintf(client, "\n");
389 }
390
391
392 static void write_formatdesc( const char *str )
393 {
394     print_client("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
395     print_client("{\n");
396     indent++;
397     print_client("short Pad;\n");
398     print_client("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
399     indent--;
400     print_client("} MIDL_%s_FORMAT_STRING;\n", str);
401     print_client("\n");
402 }
403
404
405 static void write_formatstringsdecl(type_t *iface)
406 {
407     int byte_count = 1;
408
409     print_client("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
410
411     /* determine the proc format string size */
412     if (iface->funcs)
413     {
414         func_t *func = iface->funcs;
415         while (NEXT_LINK(func)) func = NEXT_LINK(func);
416         while (func)
417         {
418             /* argument list size */
419             if (func->args)
420             {
421                 var_t *var = func->args;
422                 while (NEXT_LINK(var)) var = NEXT_LINK(var);
423                 while (var)
424                 {
425                     byte_count += 2; /* FIXME: determine real size */
426                     var = PREV_LINK(var);
427                 }
428             }
429     
430             /* return value size */
431             byte_count += 2; /* FIXME: determine real size */
432             func = PREV_LINK(func);
433         }
434     }
435     print_client("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
436
437     fprintf(client, "\n");
438     write_formatdesc("TYPE");
439     write_formatdesc("PROC");
440     fprintf(client, "\n");
441     print_client("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
442     print_client("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
443     print_client("\n");
444 }
445
446
447 static void write_implicithandledecl(type_t *iface)
448 {
449     char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
450
451     if (implicit_handle)
452     {
453         fprintf(client, "handle_t %s;\n", implicit_handle);
454         fprintf(client, "\n");
455     }
456 }
457
458
459 static void init_client(void)
460 {
461     if (client) return;
462     if (!(client = fopen(client_name, "w")))
463         error("Could not open %s for output\n", client_name);
464
465     print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
466     print_client("#include <string.h>\n");
467     print_client("#ifdef _ALPHA_\n");
468     print_client("#include <stdarg.h>\n");
469     print_client("#endif\n");
470     fprintf(client, "\n");
471     print_client("#include \"%s\"\n", header_name);
472     fprintf(client, "\n");
473 }
474
475
476 void write_client(ifref_t *ifaces)
477 {
478     ifref_t *iface = ifaces;
479
480     if (!do_client)
481         return;
482     if (!iface)
483         return;
484     END_OF_LIST(iface);
485
486     init_client();
487     if (!client)
488         return;
489
490     while (iface)
491     {
492         fprintf(client, "/*****************************************************************************\n");
493         fprintf(client, " * %s interface\n", iface->iface->name);
494         fprintf(client, " */\n");
495         fprintf(client, "\n");
496
497         if (iface->iface->funcs)
498         {
499             write_formatstringsdecl(iface->iface);
500             write_implicithandledecl(iface->iface);
501     
502             write_clientinterfacedecl(iface->iface);
503             write_stubdescdecl(iface->iface);
504             write_bindinghandledecl(iface->iface);
505     
506             write_function_stubs(iface->iface);
507             write_stubdescriptor(iface->iface);
508         }
509
510         print_client("#if !defined(__RPC_WIN32__)\n");
511         print_client("#error  Invalid build platform for this stub.\n");
512         print_client("#endif\n");
513         fprintf(client, "\n");
514
515         write_procformatstring(client, iface->iface);
516         write_typeformatstring(client, iface->iface);
517
518         fprintf(client, "\n");
519
520         iface = PREV_LINK(iface);
521     }
522
523     fclose(client);
524 }