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"
43 #define END_OF_LIST(list) \
46 while (NEXT_LINK(list)) \
47 list = NEXT_LINK(list); \
52 static int indent
= 0;
54 static int print_client( const char *format
, ... )
60 for (i
= 0; i
< indent
; i
++)
62 r
= vfprintf(client
, format
, va
);
68 static void write_procformatstring(type_t
*iface
)
70 func_t
*cur
= iface
->funcs
;
72 print_client("static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
79 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
82 var_t
*def
= cur
->def
;
84 if (is_void(def
->type
, NULL
))
86 print_client("0x5b, /* FC_END */\n");
87 print_client("0x5c, /* FC_PAD */\n");
91 print_client("0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
92 print_client("0x%02x, /* <type> */\n", def
->type
->type
);
98 print_client("0x0\n");
102 print_client("};\n");
107 static void write_typeformatstring(void)
109 print_client("static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
112 print_client("0,\n");
115 print_client("NdrFcShort(0x0),\n");
116 print_client("0x0\n");
120 print_client("};\n");
125 static void write_function_stubs(type_t
*iface
)
127 func_t
*cur
= iface
->funcs
;
128 char *handle_name
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
129 int method_count
= 0;
130 unsigned int proc_offset
= 0;
132 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
135 var_t
*def
= cur
->def
;
137 write_type(client
, def
->type
, def
, def
->tname
);
138 fprintf(client
, " ");
139 write_name(client
, def
);
140 fprintf(client
, "(\n");
143 write_args(client
, cur
->args
, iface
->name
, 0, TRUE
);
145 print_client("void");
146 fprintf(client
, ")\n");
149 /* write the functions body */
150 fprintf(client
, "{\n");
153 /* declare return value '_RetVal' */
154 if (!is_void(def
->type
, NULL
))
157 write_type(client
, def
->type
, def
, def
->tname
);
158 fprintf(client
, " _RetVal;\n");
162 print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
164 print_client("RPC_MESSAGE _RpcMessage;\n");
165 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
166 fprintf(client
, "\n");
167 print_client("RpcTryFinally\n");
171 print_client("NdrClientInitializeNew(\n");
173 print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
174 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
175 print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface
->name
);
176 print_client("%d);\n", method_count
);
178 fprintf(client
, "\n");
181 print_client("_Handle = %s;\n", handle_name
);
183 /* FIXME: marshal arguments */
184 print_client("_StubMsg.BufferLength = 0UL;\n");
185 /* print_client("NdrNsGetBuffer(\n"); */
186 print_client("NdrGetBuffer(\n");
188 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
189 print_client("_StubMsg.BufferLength,\n");
191 print_client("%_Handle);\n");
193 print_client("%s__MIDL_AutoBindHandle);\n", iface
->name
);
195 fprintf(client
, "\n");
198 /* send/receive message */
199 /* print_client("NdrNsSendReceive(\n"); */
200 print_client("NdrSendReceive(\n");
202 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
203 print_client("(unsigned char __RPC_FAR *)_StubMsg.Buffer);\n");
204 /* print_client("(unsigned char __RPC_FAR *)_StubMsg.Buffer,\n"); */
205 /* print_client("(RPC_BINDING_HANDLE __RPC_FAR *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
208 /* unmarshal return value */
209 if (is_void(def
->type
, NULL
))
215 fprintf(client
, "\n");
217 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
219 print_client("NdrConvert(\n");
221 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
222 print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString[%u]);\n", proc_offset
);
224 fprintf(client
, "\n");
226 print_client("_RetVal = *((");
227 write_type(client
, def
->type
, def
, def
->tname
);
228 fprintf(client
, " __RPC_FAR *)_StubMsg.Buffer)++;\n");
230 /* FIXME: update proc_offset */
236 print_client("RpcFinally\n");
241 /* FIXME: emit client finally code */
243 print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
247 print_client("RpcEndFinally\n");
250 /* emit return code */
251 if (!is_void(def
->type
, NULL
))
253 fprintf(client
, "\n");
254 print_client("return _RetVal;\n");
258 fprintf(client
, "}\n");
259 fprintf(client
, "\n");
262 cur
= PREV_LINK(cur
);
267 static void write_bindinghandledecl(type_t
*iface
)
269 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n", iface
->name
);
270 fprintf(client
, "\n");
274 static void write_stubdescdecl(type_t
*iface
)
276 print_client("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface
->name
);
277 fprintf(client
, "\n");
281 static void write_stubdescriptor(type_t
*iface
)
283 char *handle_name
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
285 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface
->name
);
288 print_client("(void __RPC_FAR *)& %s___RpcClientInterface,\n", iface
->name
);
289 print_client("MIDL_user_allocate,\n");
290 print_client("MIDL_user_free,\n");
292 print_client("&%s,\n", handle_name
);
294 print_client("&%s__MIDL_AutoBindHandle,\n", iface
->name
);
295 print_client("0,\n");
296 print_client("0,\n");
297 print_client("0,\n");
298 print_client("0,\n");
299 print_client("__MIDL_TypeFormatString.Format,\n");
300 print_client("1, /* -error bounds_check flag */\n");
301 print_client("0x10001, /* Ndr library version */\n");
302 print_client("0,\n");
303 print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
304 print_client("0,\n");
305 print_client("0,\n");
306 print_client("0, /* notify & notify_flag routine table */\n");
307 print_client("1, /* Flags */\n");
308 print_client("0, /* Reserved3 */\n");
309 print_client("0, /* Reserved4 */\n");
310 print_client("0 /* Reserved5 */\n");
312 print_client("};\n");
313 fprintf(client
, "\n");
317 static void write_clientinterfacedecl(type_t
*iface
)
319 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
320 UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
322 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface
->name
);
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 print_client("0,\n");
333 print_client("0,\n");
334 print_client("0,\n");
335 print_client("0,\n");
336 print_client("0,\n");
338 print_client("};\n");
339 print_client("RPC_IF_HANDLE %s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
340 iface
->name
, LOWORD(ver
), HIWORD(ver
), iface
->name
);
341 fprintf(client
, "\n");
345 static void write_formatdesc( const char *str
)
347 print_client("typedef struct _MIDL_%s_FORMAT_STRING\n", str
);
350 print_client("short Pad;\n");
351 print_client("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str
);
353 print_client("} MIDL_%s_FORMAT_STRING;\n", str
);
358 static void write_formatstringsdecl(type_t
*iface
)
363 print_client("#define TYPE_FORMAT_STRING_SIZE %d\n", 3); /* FIXME */
365 /* determine the proc format string size */
367 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
370 byte_count
+= 2; /* FIXME: determine real size */
371 cur
= PREV_LINK(cur
);
373 print_client("#define PROC_FORMAT_STRING_SIZE %d\n", byte_count
);
375 fprintf(client
, "\n");
376 write_formatdesc("TYPE");
377 write_formatdesc("PROC");
378 fprintf(client
, "\n");
379 print_client("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
380 print_client("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
385 static void write_implicithandledecl(type_t
*iface
)
387 char *var
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
391 fprintf(client
, "handle_t %s;\n", var
);
392 fprintf(client
, "\n");
397 static void init_client(void)
400 if (!(client
= fopen(client_name
, "w")))
401 error("Could not open %s for output\n", client_name
);
403 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION
, input_name
);
404 print_client("#include <string.h>\n");
405 print_client("#ifdef _ALPHA_\n");
406 print_client("#include <stdarg.h>\n");
407 print_client("#endif\n");
408 fprintf(client
, "\n");
409 print_client("#include \"%s\"\n", header_name
);
410 fprintf(client
, "\n");
414 void write_client(ifref_t
*ifaces
)
416 ifref_t
*lcur
= ifaces
;
428 write_formatstringsdecl(lcur
->iface
);
429 write_implicithandledecl(lcur
->iface
);
431 write_clientinterfacedecl(lcur
->iface
);
432 write_stubdescdecl(lcur
->iface
);
433 write_bindinghandledecl(lcur
->iface
);
435 write_function_stubs(lcur
->iface
);
436 write_stubdescriptor(lcur
->iface
);
438 print_client("#if !defined(__RPC_WIN32__)\n");
439 print_client("#error Invalid build platform for this stub.\n");
440 print_client("#endif\n");
441 fprintf(client
, "\n");
443 write_procformatstring(lcur
->iface
);
444 write_typeformatstring();
446 fprintf(client
, "\n");