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
22 #include "wine/port.h"
39 #include "widltypes.h"
41 #include "typelib_struct.h"
44 #define END_OF_LIST(list) \
47 while (NEXT_LINK(list)) \
48 list = NEXT_LINK(list); \
53 static int indent
= 0;
55 static int print_client( const char *format
, ... )
61 for (i
= 0; i
< indent
; i
++)
63 r
= vfprintf(client
, format
, va
);
69 static void print_message_buffer_size(func_t
*func
)
71 unsigned int total_size
= 0;
75 var_t
*var
= func
->args
;
76 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
79 unsigned int alignment
= 8;
81 switch (var
->type
->type
)
94 total_size
+= 2 + alignment
% 2;
101 case RPC_FC_ERROR_STATUS_T
:
102 total_size
+= 4 + alignment
% 4;
108 total_size
+= 8 + alignment
% 8;
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
);
128 int method_count
= 0;
129 unsigned int proc_offset
= 0;
131 while (NEXT_LINK(func
)) func
= NEXT_LINK(func
);
134 var_t
*def
= func
->def
;
136 write_type(client
, def
->type
, def
, def
->tname
);
137 fprintf(client
, " ");
138 write_name(client
, def
);
139 fprintf(client
, "(\n");
142 write_args(client
, func
->args
, iface
->name
, 0, TRUE
);
144 print_client("void");
145 fprintf(client
, ")\n");
148 /* write the functions body */
149 fprintf(client
, "{\n");
152 /* declare return value '_RetVal' */
153 if (!is_void(def
->type
, NULL
))
156 write_type(client
, def
->type
, def
, def
->tname
);
157 fprintf(client
, " _RetVal;\n");
161 print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
163 print_client("RPC_MESSAGE _RpcMessage;\n");
164 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
165 fprintf(client
, "\n");
166 print_client("RpcTryFinally\n");
170 print_client("NdrClientInitializeNew(\n");
172 print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
173 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
174 print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface
->name
);
175 print_client("%d);\n", method_count
);
177 fprintf(client
, "\n");
181 print_client("_Handle = %s;\n", implicit_handle
);
182 fprintf(client
, "\n");
185 /* emit the message buffer size */
186 print_client("_StubMsg.BufferLength =");
187 print_message_buffer_size(func
);
188 fprintf(client
, ";\n");
190 print_client("NdrGetBuffer(\n");
192 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
193 print_client("_StubMsg.BufferLength,\n");
195 print_client("_Handle);\n");
197 print_client("%s__MIDL_AutoBindHandle);\n", iface
->name
);
199 fprintf(client
, "\n");
202 /* marshal arguments */
203 marshall_arguments(client
, indent
, func
);
205 /* send/receive message */
206 /* print_client("NdrNsSendReceive(\n"); */
207 /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
208 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
209 print_client("NdrSendReceive(\n");
211 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
212 print_client("(unsigned char *)_StubMsg.Buffer);\n");
215 print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
216 print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
218 /* unmarshal return value */
219 if (!is_void(def
->type
, NULL
))
221 fprintf(client
, "\n");
223 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
225 print_client("NdrConvert(\n");
227 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
228 print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset
);
230 fprintf(client
, "\n");
232 print_client("_RetVal = *((");
233 write_type(client
, def
->type
, def
, def
->tname
);
234 fprintf(client
, " *)_StubMsg.Buffer)++;\n");
237 /* update proc_offset */
241 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
244 proc_offset
+= 2; /* FIXME */
245 var
= PREV_LINK(var
);
248 proc_offset
+= 2; /* FIXME */
252 print_client("RpcFinally\n");
257 /* FIXME: emit client finally code */
259 print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
263 print_client("RpcEndFinally\n");
266 /* emit return code */
267 if (!is_void(def
->type
, NULL
))
269 fprintf(client
, "\n");
270 print_client("return _RetVal;\n");
274 fprintf(client
, "}\n");
275 fprintf(client
, "\n");
278 func
= PREV_LINK(func
);
283 static void write_bindinghandledecl(type_t
*iface
)
285 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n", iface
->name
);
286 fprintf(client
, "\n");
290 static void write_stubdescdecl(type_t
*iface
)
292 print_client("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface
->name
);
293 fprintf(client
, "\n");
297 static void write_stubdescriptor(type_t
*iface
)
299 char *implicit_handle
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
301 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface
->name
);
304 print_client("(void *)& %s___RpcClientInterface,\n", iface
->name
);
305 print_client("MIDL_user_allocate,\n");
306 print_client("MIDL_user_free,\n");
308 print_client("&%s,\n", implicit_handle
);
310 print_client("&%s__MIDL_AutoBindHandle,\n", iface
->name
);
311 print_client("0,\n");
312 print_client("0,\n");
313 print_client("0,\n");
314 print_client("0,\n");
315 print_client("__MIDL_TypeFormatString.Format,\n");
316 print_client("1, /* -error bounds_check flag */\n");
317 print_client("0x10001, /* Ndr library version */\n");
318 print_client("0,\n");
319 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
320 print_client("0,\n");
321 print_client("0,\n");
322 print_client("0, /* notify & notify_flag routine table */\n");
323 print_client("1, /* Flags */\n");
324 print_client("0, /* Reserved3 */\n");
325 print_client("0, /* Reserved4 */\n");
326 print_client("0 /* Reserved5 */\n");
328 print_client("};\n");
329 fprintf(client
, "\n");
333 static void write_clientinterfacedecl(type_t
*iface
)
335 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
336 UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
338 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface
->name
);
341 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
342 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",
343 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
344 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6],
345 uuid
->Data4
[7], LOWORD(ver
), HIWORD(ver
));
346 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
347 print_client("0,\n");
348 print_client("0,\n");
349 print_client("0,\n");
350 print_client("0,\n");
351 print_client("0,\n");
352 print_client("0,\n");
354 print_client("};\n");
355 print_client("RPC_IF_HANDLE %s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
356 iface
->name
, LOWORD(ver
), HIWORD(ver
), iface
->name
);
357 fprintf(client
, "\n");
361 static void write_formatdesc( const char *str
)
363 print_client("typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
366 print_client("short Pad;\n");
367 print_client("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
369 print_client("} MIDL_%s_FORMAT_STRING;\n", str
);
374 static void write_formatstringsdecl(type_t
*iface
)
380 print_client("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
382 /* determine the proc format string size */
384 while (NEXT_LINK(func
)) func
= NEXT_LINK(func
);
387 /* argument list size */
391 while (NEXT_LINK(var
)) var
= NEXT_LINK(var
);
394 byte_count
+= 2; /* FIXME: determine real size */
395 var
= PREV_LINK(var
);
399 /* return value size */
400 byte_count
+= 2; /* FIXME: determine real size */
401 func
= PREV_LINK(func
);
403 print_client("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count
);
405 fprintf(client
, "\n");
406 write_formatdesc("TYPE");
407 write_formatdesc("PROC");
408 fprintf(client
, "\n");
409 print_client("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
410 print_client("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
415 static void write_implicithandledecl(type_t
*iface
)
417 char *implicit_handle
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
421 fprintf(client
, "handle_t %s;\n", implicit_handle
);
422 fprintf(client
, "\n");
427 static void init_client(void)
430 if (!(client
= fopen(client_name
, "w")))
431 error("Could not open %s for output\n", client_name
);
433 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION
, input_name
);
434 print_client("#include <string.h>\n");
435 print_client("#ifdef _ALPHA_\n");
436 print_client("#include <stdarg.h>\n");
437 print_client("#endif\n");
438 fprintf(client
, "\n");
439 print_client("#include \"%s\"\n", header_name
);
440 fprintf(client
, "\n");
444 void write_client(ifref_t
*ifaces
)
446 ifref_t
*iface
= ifaces
;
460 fprintf(client
, "/*****************************************************************************\n");
461 fprintf(client
, " * %s interface\n", iface
->iface
->name
);
462 fprintf(client
, " */\n");
463 fprintf(client
, "\n");
465 write_formatstringsdecl(iface
->iface
);
466 write_implicithandledecl(iface
->iface
);
468 write_clientinterfacedecl(iface
->iface
);
469 write_stubdescdecl(iface
->iface
);
470 write_bindinghandledecl(iface
->iface
);
472 write_function_stubs(iface
->iface
);
473 write_stubdescriptor(iface
->iface
);
475 print_client("#if !defined(__RPC_WIN32__)\n");
476 print_client("#error Invalid build platform for this stub.\n");
477 print_client("#endif\n");
478 fprintf(client
, "\n");
480 write_procformatstring(client
, iface
->iface
);
481 write_typeformatstring(client
);
483 fprintf(client
, "\n");
485 iface
= PREV_LINK(iface
);