widl: Avoid lvalue casts in generated code.
[wine/winequartzdrv.git] / tools / widl / client.c
blob3fd5f5c36473e7299a6241671feb475cd7fb8baa
1 /*
2 * IDL Compiler
4 * Copyright 2005 Eric Kohl
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
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>
32 #include "windef.h"
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
39 #include "widltypes.h"
40 #include "typelib.h"
41 #include "typelib_struct.h"
42 #include "typegen.h"
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)
52 static FILE* client;
53 static int indent = 0;
55 static int print_client( const char *format, ... )
57 va_list va;
58 int i, r;
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;
69 static void print_message_buffer_size(func_t *func)
71 unsigned int total_size = 0;
73 if (func->args)
75 var_t *var = func->args;
76 while (NEXT_LINK(var)) var = NEXT_LINK(var);
77 while (var)
79 unsigned int alignment = 8;
81 switch (var->type->type)
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;
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;
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;
106 case RPC_FC_HYPER:
107 case RPC_FC_DOUBLE:
108 total_size += 8 + alignment % 8;
109 alignment = 8;
110 break;
112 default:
113 alignment = 1;
116 var = PREV_LINK(var);
119 fprintf(client, " %u", total_size);
123 static void write_function_stubs(type_t *iface)
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;
132 while (NEXT_LINK(func)) func = NEXT_LINK(func);
133 while (func)
135 var_t *def = func->def;
136 var_t* explicit_handle_var;
138 /* check for a defined binding handle */
139 explicit_handle_var = get_explicit_handle_var(func);
140 if (explicit_handle)
142 if (!explicit_handle_var)
144 error("%s() does not define an explicit binding handle!\n", def->name);
145 return;
148 else if (implicit_handle)
150 if (explicit_handle_var)
152 error("%s() must not define a binding handle!\n", def->name);
153 return;
157 write_type(client, def->type, def, def->tname);
158 fprintf(client, " ");
159 write_name(client, def);
160 fprintf(client, "(\n");
161 indent++;
162 if (func->args)
163 write_args(client, func->args, iface->name, 0, TRUE);
164 else
165 print_client("void");
166 fprintf(client, ")\n");
167 indent--;
169 /* write the functions body */
170 fprintf(client, "{\n");
171 indent++;
173 /* declare return value '_RetVal' */
174 if (!is_void(def->type, NULL))
176 print_client("");
177 write_type(client, def->type, def, def->tname);
178 fprintf(client, " _RetVal;\n");
181 if (implicit_handle || explicit_handle_var)
182 print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
184 print_client("RPC_MESSAGE _RpcMessage;\n");
185 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
186 fprintf(client, "\n");
187 print_client("RpcTryFinally\n");
188 print_client("{\n");
189 indent++;
191 print_client("NdrClientInitializeNew(\n");
192 indent++;
193 print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
194 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
195 print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
196 print_client("%d);\n", method_count);
197 indent--;
198 fprintf(client, "\n");
200 if (implicit_handle)
202 print_client("_Handle = %s;\n", implicit_handle);
203 fprintf(client, "\n");
205 else if (explicit_handle_var)
207 print_client("_Handle = %s;\n", explicit_handle_var->name);
208 fprintf(client, "\n");
211 /* emit the message buffer size */
212 print_client("_StubMsg.BufferLength =");
213 print_message_buffer_size(func);
214 fprintf(client, ";\n");
216 print_client("NdrGetBuffer(\n");
217 indent++;
218 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
219 print_client("_StubMsg.BufferLength,\n");
220 if (implicit_handle || explicit_handle_var)
221 print_client("_Handle);\n");
222 else
223 print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
224 indent--;
225 fprintf(client, "\n");
228 /* marshal arguments */
229 marshall_arguments(client, indent, func);
231 /* send/receive message */
232 /* print_client("NdrNsSendReceive(\n"); */
233 /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
234 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
235 print_client("NdrSendReceive(\n");
236 indent++;
237 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
238 print_client("(unsigned char *)_StubMsg.Buffer);\n");
239 indent--;
241 print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
242 print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
244 /* unmarshal return value */
245 if (!is_void(def->type, NULL))
247 fprintf(client, "\n");
249 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
250 indent++;
251 print_client("NdrConvert(\n");
252 indent++;
253 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
254 print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
255 indent -= 2;
256 fprintf(client, "\n");
258 print_client("_RetVal = *(");
259 write_type(client, def->type, def, def->tname);
260 fprintf(client, " *)_StubMsg.Buffer;\n");
261 fprintf(client, "_StubMsg.Buffer += sizeof(");
262 write_type(client, def->type, def, def->tname);
263 fprintf(client, ");\n");
266 /* update proc_offset */
267 if (func->args)
269 var = func->args;
270 while (NEXT_LINK(var)) var = NEXT_LINK(var);
271 while (var)
273 proc_offset += 2; /* FIXME */
274 var = PREV_LINK(var);
277 proc_offset += 2; /* FIXME */
279 indent--;
280 print_client("}\n");
281 print_client("RpcFinally\n");
282 print_client("{\n");
283 indent++;
286 /* FIXME: emit client finally code */
288 print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
290 indent--;
291 print_client("}\n");
292 print_client("RpcEndFinally\n");
295 /* emit return code */
296 if (!is_void(def->type, NULL))
298 fprintf(client, "\n");
299 print_client("return _RetVal;\n");
302 indent--;
303 fprintf(client, "}\n");
304 fprintf(client, "\n");
306 method_count++;
307 func = PREV_LINK(func);
312 static void write_bindinghandledecl(type_t *iface)
314 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n", iface->name);
315 fprintf(client, "\n");
319 static void write_stubdescdecl(type_t *iface)
321 print_client("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
322 fprintf(client, "\n");
326 static void write_stubdescriptor(type_t *iface)
328 char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
330 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
331 print_client("{\n");
332 indent++;
333 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
334 print_client("MIDL_user_allocate,\n");
335 print_client("MIDL_user_free,\n");
336 if (implicit_handle)
337 print_client("&%s,\n", implicit_handle);
338 else
339 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
340 print_client("0,\n");
341 print_client("0,\n");
342 print_client("0,\n");
343 print_client("0,\n");
344 print_client("__MIDL_TypeFormatString.Format,\n");
345 print_client("1, /* -error bounds_check flag */\n");
346 print_client("0x10001, /* Ndr library version */\n");
347 print_client("0,\n");
348 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
349 print_client("0,\n");
350 print_client("0,\n");
351 print_client("0, /* notify & notify_flag routine table */\n");
352 print_client("1, /* Flags */\n");
353 print_client("0, /* Reserved3 */\n");
354 print_client("0, /* Reserved4 */\n");
355 print_client("0 /* Reserved5 */\n");
356 indent--;
357 print_client("};\n");
358 fprintf(client, "\n");
362 static void write_clientinterfacedecl(type_t *iface)
364 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
365 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
367 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
368 print_client("{\n");
369 indent++;
370 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
371 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",
372 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
373 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
374 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
375 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
376 print_client("0,\n");
377 print_client("0,\n");
378 print_client("0,\n");
379 print_client("0,\n");
380 print_client("0,\n");
381 print_client("0,\n");
382 indent--;
383 print_client("};\n");
384 print_client("RPC_IF_HANDLE %s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
385 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
386 fprintf(client, "\n");
390 static void write_formatdesc( const char *str )
392 print_client("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
393 print_client("{\n");
394 indent++;
395 print_client("short Pad;\n");
396 print_client("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
397 indent--;
398 print_client("} MIDL_%s_FORMAT_STRING;\n", str);
399 print_client("\n");
403 static void write_formatstringsdecl(type_t *iface)
405 func_t *func;
406 var_t *var;
407 int byte_count = 1;
409 print_client("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
411 /* determine the proc format string size */
412 func = iface->funcs;
413 while (NEXT_LINK(func)) func = NEXT_LINK(func);
414 while (func)
416 /* argument list size */
417 if (func->args)
419 var = func->args;
420 while (NEXT_LINK(var)) var = NEXT_LINK(var);
421 while (var)
423 byte_count += 2; /* FIXME: determine real size */
424 var = PREV_LINK(var);
428 /* return value size */
429 byte_count += 2; /* FIXME: determine real size */
430 func = PREV_LINK(func);
432 print_client("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count);
434 fprintf(client, "\n");
435 write_formatdesc("TYPE");
436 write_formatdesc("PROC");
437 fprintf(client, "\n");
438 print_client("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
439 print_client("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
440 print_client("\n");
444 static void write_implicithandledecl(type_t *iface)
446 char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
448 if (implicit_handle)
450 fprintf(client, "handle_t %s;\n", implicit_handle);
451 fprintf(client, "\n");
456 static void init_client(void)
458 if (client) return;
459 if (!(client = fopen(client_name, "w")))
460 error("Could not open %s for output\n", client_name);
462 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
463 print_client("#include <string.h>\n");
464 print_client("#ifdef _ALPHA_\n");
465 print_client("#include <stdarg.h>\n");
466 print_client("#endif\n");
467 fprintf(client, "\n");
468 print_client("#include \"%s\"\n", header_name);
469 fprintf(client, "\n");
473 void write_client(ifref_t *ifaces)
475 ifref_t *iface = ifaces;
477 if (!do_client)
478 return;
479 if (!iface)
480 return;
481 END_OF_LIST(iface);
483 init_client();
484 if (!client)
485 return;
487 while (iface)
489 fprintf(client, "/*****************************************************************************\n");
490 fprintf(client, " * %s interface\n", iface->iface->name);
491 fprintf(client, " */\n");
492 fprintf(client, "\n");
494 write_formatstringsdecl(iface->iface);
495 write_implicithandledecl(iface->iface);
497 write_clientinterfacedecl(iface->iface);
498 write_stubdescdecl(iface->iface);
499 write_bindinghandledecl(iface->iface);
501 write_function_stubs(iface->iface);
502 write_stubdescriptor(iface->iface);
504 print_client("#if !defined(__RPC_WIN32__)\n");
505 print_client("#error Invalid build platform for this stub.\n");
506 print_client("#endif\n");
507 fprintf(client, "\n");
509 write_procformatstring(client, iface->iface);
510 write_typeformatstring(client, iface->iface);
512 fprintf(client, "\n");
514 iface = PREV_LINK(iface);
517 fclose(client);