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
22 #include "wine/port.h"
37 #include "widltypes.h"
42 static int indent
= 0;
44 static void print_client( const char *format
, ... )
48 print(client
, indent
, format
, va
);
53 static void check_pointers(const func_t
*func
)
60 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
62 if (is_var_ptr(var
) && cant_be_null(var
))
64 print_client("if (!%s)\n", var
->name
);
67 print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
69 print_client("}\n\n");
74 static void write_function_stubs(type_t
*iface
, unsigned int *proc_offset
)
77 const char *implicit_handle
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
82 print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n\n", iface
->name
);
84 if (iface
->funcs
) LIST_FOR_EACH_ENTRY( func
, iface
->funcs
, const func_t
, entry
)
86 const var_t
*def
= func
->def
;
87 const var_t
* explicit_handle_var
;
88 const var_t
* explicit_generic_handle_var
= NULL
;
89 const var_t
* context_handle_var
= NULL
;
90 int has_full_pointer
= is_full_pointer_function(func
);
91 const char *callconv
= get_attrp(def
->type
->attrs
, ATTR_CALLCONV
);
93 /* check for a defined binding handle */
94 explicit_handle_var
= get_explicit_handle_var(func
);
95 if (!explicit_handle_var
)
97 explicit_generic_handle_var
= get_explicit_generic_handle_var(func
);
98 if (!explicit_generic_handle_var
)
99 context_handle_var
= get_context_handle_var(func
);
102 write_type_decl_left(client
, get_func_return_type(func
));
103 if (needs_space_after(get_func_return_type(func
)))
104 fprintf(client
, " ");
105 if (callconv
) fprintf(client
, "%s ", callconv
);
106 write_prefix_name(client
, prefix_client
, def
);
107 fprintf(client
, "(\n");
110 write_args(client
, func
->args
, iface
->name
, 0, TRUE
);
112 print_client("void");
113 fprintf(client
, ")\n");
116 /* write the functions body */
117 fprintf(client
, "{\n");
120 /* declare return value '_RetVal' */
121 if (!is_void(get_func_return_type(func
)))
124 write_type_decl_left(client
, get_func_return_type(func
));
125 fprintf(client
, " _RetVal;\n");
128 if (implicit_handle
|| explicit_handle_var
|| explicit_generic_handle_var
|| context_handle_var
)
129 print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
131 print_client("RPC_MESSAGE _RpcMessage;\n");
132 print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
133 if (!is_void(get_func_return_type(func
)) && decl_indirect(get_func_return_type(func
)))
135 print_client("void *_p_%s = &%s;\n",
136 "_RetVal", "_RetVal");
138 fprintf(client
, "\n");
140 if (has_full_pointer
)
141 write_full_pointer_init(client
, indent
, func
, FALSE
);
144 check_pointers(func
);
146 print_client("RpcTryFinally\n");
150 print_client("NdrClientInitializeNew(\n");
152 print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
153 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
154 print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface
->name
);
155 print_client("%d);\n", method_count
);
157 fprintf(client
, "\n");
159 if (is_attr(def
->attrs
, ATTR_IDEMPOTENT
) || is_attr(def
->attrs
, ATTR_BROADCAST
))
161 print_client("_RpcMessage.RpcFlags = ( RPC_NCA_FLAGS_DEFAULT ");
162 if (is_attr(def
->attrs
, ATTR_IDEMPOTENT
))
163 fprintf(client
, "| RPC_NCA_FLAGS_IDEMPOTENT ");
164 if (is_attr(def
->attrs
, ATTR_BROADCAST
))
165 fprintf(client
, "| RPC_NCA_FLAGS_BROADCAST ");
166 fprintf(client
, ");\n\n");
169 if (explicit_handle_var
)
171 print_client("_Handle = %s;\n", explicit_handle_var
->name
);
172 fprintf(client
, "\n");
174 else if (explicit_generic_handle_var
)
176 print_client("_Handle = %s_bind(%s);\n",
177 get_explicit_generic_handle_type(explicit_generic_handle_var
)->name
,
178 explicit_generic_handle_var
->name
);
179 fprintf(client
, "\n");
181 else if (context_handle_var
)
183 /* if the context_handle attribute appears in the chain of types
184 * without pointers being followed, then the context handle must
185 * be direct, otherwise it is a pointer */
186 int is_ch_ptr
= is_aliaschain_attr(context_handle_var
->type
, ATTR_CONTEXTHANDLE
) ? FALSE
: TRUE
;
187 print_client("if (%s%s != 0)\n", is_ch_ptr
? "*" : "", context_handle_var
->name
);
189 print_client("_Handle = NDRCContextBinding(%s%s);\n", is_ch_ptr
? "*" : "", context_handle_var
->name
);
191 fprintf(client
, "\n");
193 else if (implicit_handle
)
195 print_client("_Handle = %s;\n", implicit_handle
);
196 fprintf(client
, "\n");
199 write_remoting_arguments(client
, indent
, func
, PASS_IN
, PHASE_BUFFERSIZE
);
201 print_client("NdrGetBuffer(\n");
203 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
204 print_client("_StubMsg.BufferLength,\n");
205 if (implicit_handle
|| explicit_handle_var
|| explicit_generic_handle_var
|| context_handle_var
)
206 print_client("_Handle);\n");
208 print_client("%s__MIDL_AutoBindHandle);\n", iface
->name
);
210 fprintf(client
, "\n");
212 /* marshal arguments */
213 write_remoting_arguments(client
, indent
, func
, PASS_IN
, PHASE_MARSHAL
);
215 /* send/receive message */
216 /* print_client("NdrNsSendReceive(\n"); */
217 /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
218 /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
219 print_client("NdrSendReceive(\n");
221 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
222 print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
225 print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
226 print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
228 if (has_out_arg_or_return(func
))
230 fprintf(client
, "\n");
232 print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
234 print_client("NdrConvert(\n");
236 print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
237 print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset
);
241 /* unmarshall arguments */
242 fprintf(client
, "\n");
243 write_remoting_arguments(client
, indent
, func
, PASS_OUT
, PHASE_UNMARSHAL
);
245 /* unmarshal return value */
246 if (!is_void(get_func_return_type(func
)))
248 if (decl_indirect(get_func_return_type(func
)))
249 print_client("MIDL_memset(&%s, 0, sizeof(%s));\n", "_RetVal", "_RetVal");
250 else if (is_ptr(get_func_return_type(func
)) || is_array(get_func_return_type(func
)))
251 print_client("%s = 0;\n", "_RetVal");
252 write_remoting_arguments(client
, indent
, func
, PASS_RETURN
, PHASE_UNMARSHAL
);
255 /* update proc_offset */
258 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
259 *proc_offset
+= get_size_procformatstring_type(var
->name
, var
->type
, var
->attrs
);
261 if (!is_void(get_func_return_type(func
)))
262 *proc_offset
+= get_size_procformatstring_type("return value", get_func_return_type(func
), NULL
);
264 *proc_offset
+= 2; /* FC_END and FC_PAD */
268 print_client("RpcFinally\n");
273 /* FIXME: emit client finally code */
275 if (has_full_pointer
)
276 write_full_pointer_free(client
, indent
, func
);
278 print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
280 if (!implicit_handle
&& explicit_generic_handle_var
)
282 fprintf(client
, "\n");
283 print_client("if (_Handle)\n");
285 print_client("%s_unbind(%s, _Handle);\n",
286 get_explicit_generic_handle_type(explicit_generic_handle_var
)->name
,
287 explicit_generic_handle_var
->name
);
293 print_client("RpcEndFinally\n");
296 /* emit return code */
297 if (!is_void(get_func_return_type(func
)))
299 fprintf(client
, "\n");
300 print_client("return _RetVal;\n");
304 fprintf(client
, "}\n");
305 fprintf(client
, "\n");
312 static void write_stubdescdecl(type_t
*iface
)
314 print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface
->name
);
315 fprintf(client
, "\n");
319 static void write_stubdescriptor(type_t
*iface
, int expr_eval_routines
)
321 const char *implicit_handle
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
323 print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface
->name
);
326 print_client("(void *)& %s___RpcClientInterface,\n", iface
->name
);
327 print_client("MIDL_user_allocate,\n");
328 print_client("MIDL_user_free,\n");
332 print_client("&%s,\n", implicit_handle
);
334 print_client("&%s__MIDL_AutoBindHandle,\n", iface
->name
);
336 print_client("},\n");
337 print_client("0,\n");
338 print_client("0,\n");
339 if (expr_eval_routines
)
340 print_client("ExprEvalRoutines,\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("%s,\n", list_empty(&user_type_list
) ? "0" : "UserMarshalRoutines");
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");
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 const UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
366 const str_list_t
*endpoints
= get_attrp(iface
->attrs
, ATTR_ENDPOINT
);
368 if (endpoints
) write_endpoints( client
, iface
->name
, endpoints
);
370 print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface
->name
);
373 print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
374 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",
375 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
376 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6],
377 uuid
->Data4
[7], MAJORVERSION(ver
), MINORVERSION(ver
));
378 print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
379 print_client("0,\n");
382 print_client("%u,\n", list_count(endpoints
));
383 print_client("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface
->name
);
387 print_client("0,\n");
388 print_client("0,\n");
390 print_client("0,\n");
391 print_client("0,\n");
392 print_client("0,\n");
394 print_client("};\n");
396 print_client("RPC_IF_HANDLE %s_ClientIfHandle = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
397 iface
->name
, iface
->name
);
399 print_client("RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
400 prefix_client
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
), iface
->name
);
401 fprintf(client
, "\n");
405 static void write_implicithandledecl(type_t
*iface
)
407 const char *implicit_handle
= get_attrp(iface
->attrs
, ATTR_IMPLICIT_HANDLE
);
411 fprintf(client
, "handle_t %s;\n", implicit_handle
);
412 fprintf(client
, "\n");
417 static void init_client(void)
420 if (!(client
= fopen(client_name
, "w")))
421 error("Could not open %s for output\n", client_name
);
423 print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION
, input_name
);
424 print_client("#include <string.h>\n");
425 print_client("#ifdef _ALPHA_\n");
426 print_client("#include <stdarg.h>\n");
427 print_client("#endif\n");
428 fprintf(client
, "\n");
429 print_client("#include \"%s\"\n", header_name
);
430 fprintf(client
, "\n");
434 static void write_client_ifaces(const statement_list_t
*stmts
, int expr_eval_routines
, unsigned int *proc_offset
)
436 const statement_t
*stmt
;
437 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
439 if (stmt
->type
== STMT_TYPE
&& stmt
->u
.type
->type
== RPC_FC_IP
)
441 type_t
*iface
= stmt
->u
.type
;
442 if (!need_stub(iface
))
445 fprintf(client
, "/*****************************************************************************\n");
446 fprintf(client
, " * %s interface\n", iface
->name
);
447 fprintf(client
, " */\n");
448 fprintf(client
, "\n");
452 write_implicithandledecl(iface
);
454 write_clientinterfacedecl(iface
);
455 write_stubdescdecl(iface
);
456 write_function_stubs(iface
, proc_offset
);
458 print_client("#if !defined(__RPC_WIN32__)\n");
459 print_client("#error Invalid build platform for this stub.\n");
460 print_client("#endif\n");
462 fprintf(client
, "\n");
463 write_stubdescriptor(iface
, expr_eval_routines
);
466 else if (stmt
->type
== STMT_LIBRARY
)
467 write_client_ifaces(stmt
->u
.lib
->stmts
, expr_eval_routines
, proc_offset
);
471 void write_client(const statement_list_t
*stmts
)
473 unsigned int proc_offset
= 0;
474 int expr_eval_routines
;
478 if (do_everything
&& !need_stub_files(stmts
))
485 write_formatstringsdecl(client
, indent
, stmts
, need_stub
);
486 expr_eval_routines
= write_expr_eval_routines(client
, client_token
);
487 if (expr_eval_routines
)
488 write_expr_eval_routine_list(client
, client_token
);
489 write_user_quad_list(client
);
491 write_client_ifaces(stmts
, expr_eval_routines
, &proc_offset
);
493 fprintf(client
, "\n");
495 write_procformatstring(client
, stmts
, need_stub
);
496 write_typeformatstring(client
, stmts
, need_stub
);