wined3d: Fix D3DSIO_M#X# instruction for both ARB and GLSL shaders.
[wine.git] / tools / widl / client.c
blob6f8773e39579e667bc5e005c9d16d4819f5c1047
1 /*
2 * IDL Compiler
4 * Copyright 2005-2006 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 static FILE* client;
45 static int indent = 0;
47 static int print_client( const char *format, ... )
49 va_list va;
50 int i, r;
52 va_start(va, format);
53 for (i = 0; i < indent; i++)
54 fprintf(client, " ");
55 r = vfprintf(client, format, va);
56 va_end(va);
57 return r;
61 static void print_message_buffer_size(const func_t *func)
63 unsigned int total_size = 0;
65 if (func->args)
67 const var_t *var = func->args;
68 while (NEXT_LINK(var)) var = NEXT_LINK(var);
69 while (var)
71 unsigned int alignment;
73 total_size += get_required_buffer_size(var, &alignment, PASS_IN);
74 total_size += alignment;
76 var = PREV_LINK(var);
79 fprintf(client, " %u", total_size);
82 static void check_pointers(const func_t *func)
84 var_t *var;
85 int pointer_type;
87 if (!func->args)
88 return;
90 var = func->args;
91 while (NEXT_LINK(var)) var = NEXT_LINK(var);
92 while (var)
94 pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
95 if (!pointer_type)
96 pointer_type = RPC_FC_RP;
98 if (pointer_type == RPC_FC_RP)
100 if (var->ptr_level >= 1)
102 print_client("if (!%s)\n", var->name);
103 print_client("{\n");
104 indent++;
105 print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
106 indent--;
107 print_client("}\n\n");
111 var = PREV_LINK(var);
115 static void write_function_stubs(type_t *iface, unsigned int *proc_offset, unsigned int *type_offset)
117 const func_t *func = iface->funcs;
118 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
119 int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
120 var_t *var;
121 int method_count = 0;
123 while (NEXT_LINK(func)) func = NEXT_LINK(func);
124 while (func)
126 const var_t *def = func->def;
127 const var_t* explicit_handle_var;
128 unsigned int type_offset_func;
130 /* check for a defined binding handle */
131 explicit_handle_var = get_explicit_handle_var(func);
132 if (explicit_handle)
134 if (!explicit_handle_var)
136 error("%s() does not define an explicit binding handle!\n", def->name);
137 return;
140 else if (implicit_handle)
142 if (explicit_handle_var)
144 error("%s() must not define a binding handle!\n", def->name);
145 return;
149 write_type(client, def->type, def, def->tname);
150 fprintf(client, " ");
151 write_name(client, def);
152 fprintf(client, "(\n");
153 indent++;
154 if (func->args)
155 write_args(client, func->args, iface->name, 0, TRUE);
156 else
157 print_client("void");
158 fprintf(client, ")\n");
159 indent--;
161 /* write the functions body */
162 fprintf(client, "{\n");
163 indent++;
165 /* declare return value '_RetVal' */
166 if (!is_void(def->type, NULL))
168 print_client("");
169 write_type(client, def->type, def, def->tname);
170 fprintf(client, " _RetVal;\n");
173 if (implicit_handle || explicit_handle_var)
174 print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
176 print_client("RPC_MESSAGE _RpcMessage;\n");
177 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
178 fprintf(client, "\n");
180 /* check pointers */
181 check_pointers(func);
183 print_client("RpcTryFinally\n");
184 print_client("{\n");
185 indent++;
187 print_client("NdrClientInitializeNew(\n");
188 indent++;
189 print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
190 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
191 print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
192 print_client("%d);\n", method_count);
193 indent--;
194 fprintf(client, "\n");
196 if (implicit_handle)
198 print_client("_Handle = %s;\n", implicit_handle);
199 fprintf(client, "\n");
201 else if (explicit_handle_var)
203 print_client("_Handle = %s;\n", explicit_handle_var->name);
204 fprintf(client, "\n");
207 /* emit the message buffer size */
208 print_client("_StubMsg.BufferLength =");
209 print_message_buffer_size(func);
210 fprintf(client, ";\n");
212 type_offset_func = *type_offset;
213 write_remoting_arguments(client, indent, func, &type_offset_func, PASS_IN, PHASE_BUFFERSIZE);
215 print_client("NdrGetBuffer(\n");
216 indent++;
217 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
218 print_client("_StubMsg.BufferLength,\n");
219 if (implicit_handle || explicit_handle_var)
220 print_client("_Handle);\n");
221 else
222 print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
223 indent--;
224 fprintf(client, "\n");
227 /* make a copy so we don't increment the type offset twice */
228 type_offset_func = *type_offset;
230 /* marshal arguments */
231 write_remoting_arguments(client, indent, func, &type_offset_func, PASS_IN, PHASE_MARSHAL);
233 /* send/receive message */
234 /* print_client("NdrNsSendReceive(\n"); */
235 /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
236 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
237 print_client("NdrSendReceive(\n");
238 indent++;
239 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
240 print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
241 indent--;
243 print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
244 print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
246 if (has_out_arg_or_return(func))
248 fprintf(client, "\n");
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;
259 /* unmarshall arguments */
260 fprintf(client, "\n");
261 write_remoting_arguments(client, indent, func, type_offset, PASS_OUT, PHASE_UNMARSHAL);
263 /* unmarshal return value */
264 if (!is_void(def->type, NULL))
265 print_phase_basetype(client, indent, PHASE_UNMARSHAL, PASS_RETURN, def, "_RetVal");
267 /* update proc_offset */
268 if (func->args)
270 var = func->args;
271 while (NEXT_LINK(var)) var = NEXT_LINK(var);
272 while (var)
274 *proc_offset += get_size_procformatstring_var(var);
275 var = PREV_LINK(var);
278 if (!is_void(def->type, NULL))
279 *proc_offset += get_size_procformatstring_var(def);
280 else
281 *proc_offset += 2; /* FC_END and FC_PAD */
283 indent--;
284 print_client("}\n");
285 print_client("RpcFinally\n");
286 print_client("{\n");
287 indent++;
290 /* FIXME: emit client finally code */
292 print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
294 indent--;
295 print_client("}\n");
296 print_client("RpcEndFinally\n");
299 /* emit return code */
300 if (!is_void(def->type, NULL))
302 fprintf(client, "\n");
303 print_client("return _RetVal;\n");
306 indent--;
307 fprintf(client, "}\n");
308 fprintf(client, "\n");
310 method_count++;
311 func = PREV_LINK(func);
316 static void write_bindinghandledecl(type_t *iface)
318 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n", iface->name);
319 fprintf(client, "\n");
323 static void write_stubdescdecl(type_t *iface)
325 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
326 fprintf(client, "\n");
330 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
332 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
334 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
335 print_client("{\n");
336 indent++;
337 print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
338 print_client("MIDL_user_allocate,\n");
339 print_client("MIDL_user_free,\n");
340 print_client("{\n");
341 indent++;
342 if (implicit_handle)
343 print_client("&%s,\n", implicit_handle);
344 else
345 print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
346 indent--;
347 print_client("},\n");
348 print_client("0,\n");
349 print_client("0,\n");
350 if (expr_eval_routines)
351 print_client("ExprEvalRoutines,\n");
352 else
353 print_client("0,\n");
354 print_client("0,\n");
355 print_client("__MIDL_TypeFormatString.Format,\n");
356 print_client("1, /* -error bounds_check flag */\n");
357 print_client("0x10001, /* Ndr library version */\n");
358 print_client("0,\n");
359 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
360 print_client("0,\n");
361 print_client("0,\n");
362 print_client("0, /* notify & notify_flag routine table */\n");
363 print_client("1, /* Flags */\n");
364 print_client("0, /* Reserved3 */\n");
365 print_client("0, /* Reserved4 */\n");
366 print_client("0 /* Reserved5 */\n");
367 indent--;
368 print_client("};\n");
369 fprintf(client, "\n");
373 static void write_clientinterfacedecl(type_t *iface)
375 unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
376 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
378 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
379 print_client("{\n");
380 indent++;
381 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
382 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",
383 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
384 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
385 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
386 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
387 print_client("0,\n");
388 print_client("0,\n");
389 print_client("0,\n");
390 print_client("0,\n");
391 print_client("0,\n");
392 print_client("0,\n");
393 indent--;
394 print_client("};\n");
395 if (old_names)
396 print_client("RPC_IF_HANDLE %s_ClientIfHandle = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
397 iface->name, iface->name);
398 else
399 print_client("RPC_IF_HANDLE %s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
400 iface->name, LOWORD(ver), HIWORD(ver), iface->name);
401 fprintf(client, "\n");
405 static void write_formatdesc( const char *str )
407 print_client("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
408 print_client("{\n");
409 indent++;
410 print_client("short Pad;\n");
411 print_client("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
412 indent--;
413 print_client("} MIDL_%s_FORMAT_STRING;\n", str);
414 print_client("\n");
418 static void write_formatstringsdecl(ifref_t *ifaces)
420 print_client("#define TYPE_FORMAT_STRING_SIZE %d\n",
421 get_size_typeformatstring(ifaces));
423 print_client("#define PROC_FORMAT_STRING_SIZE %d\n",
424 get_size_procformatstring(ifaces));
426 fprintf(client, "\n");
427 write_formatdesc("TYPE");
428 write_formatdesc("PROC");
429 fprintf(client, "\n");
430 print_client("static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
431 print_client("static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
432 print_client("\n");
436 static void write_implicithandledecl(type_t *iface)
438 const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
440 if (implicit_handle)
442 fprintf(client, "handle_t %s;\n", implicit_handle);
443 fprintf(client, "\n");
448 static void init_client(void)
450 if (client) return;
451 if (!(client = fopen(client_name, "w")))
452 error("Could not open %s for output\n", client_name);
454 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
455 print_client("#include <string.h>\n");
456 print_client("#ifdef _ALPHA_\n");
457 print_client("#include <stdarg.h>\n");
458 print_client("#endif\n");
459 fprintf(client, "\n");
460 print_client("#include \"%s\"\n", header_name);
461 fprintf(client, "\n");
465 void write_client(ifref_t *ifaces)
467 unsigned int proc_offset = 0;
468 unsigned int type_offset = 2;
469 ifref_t *iface = ifaces;
471 if (!do_client)
472 return;
473 if (!iface)
474 return;
475 END_OF_LIST(iface);
477 init_client();
478 if (!client)
479 return;
481 write_formatstringsdecl(ifaces);
483 for (; iface; iface = PREV_LINK(iface))
485 if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
486 continue;
488 fprintf(client, "/*****************************************************************************\n");
489 fprintf(client, " * %s interface\n", iface->iface->name);
490 fprintf(client, " */\n");
491 fprintf(client, "\n");
493 if (iface->iface->funcs)
495 int expr_eval_routines;
497 write_implicithandledecl(iface->iface);
499 write_clientinterfacedecl(iface->iface);
500 write_stubdescdecl(iface->iface);
501 write_bindinghandledecl(iface->iface);
503 write_function_stubs(iface->iface, &proc_offset, &type_offset);
505 print_client("#if !defined(__RPC_WIN32__)\n");
506 print_client("#error Invalid build platform for this stub.\n");
507 print_client("#endif\n");
509 fprintf(client, "\n");
511 expr_eval_routines = write_expr_eval_routines(client, iface->iface->name);
512 if (expr_eval_routines)
513 write_expr_eval_routine_list(client, iface->iface->name);
514 write_stubdescriptor(iface->iface, expr_eval_routines);
518 fprintf(client, "\n");
520 write_procformatstring(client, ifaces);
521 write_typeformatstring(client, ifaces);
523 fclose(client);