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"
40 static int indent
= 0;
43 static void print_server(const char *format
, ...)
47 print(server
, indent
, format
, va
);
51 static void write_function_stubs(type_t
*iface
, unsigned int *proc_offset
)
55 const var_t
* explicit_handle_var
;
57 if (!iface
->funcs
) return;
58 LIST_FOR_EACH_ENTRY( func
, iface
->funcs
, const func_t
, entry
)
60 const var_t
*def
= func
->def
;
61 int has_full_pointer
= is_full_pointer_function(func
);
63 /* check for a defined binding handle */
64 explicit_handle_var
= get_explicit_handle_var(func
);
66 print_server("struct __frame_%s_%s\n{\n", iface
->name
, get_name(def
));
68 print_server("__DECL_EXCEPTION_FRAME\n");
69 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
71 /* Declare arguments */
72 declare_stub_args(server
, indent
, func
);
75 print_server("};\n\n");
77 print_server("static void __finally_%s_%s(", iface
->name
, get_name(def
));
78 fprintf(server
," struct __frame_%s_%s *__frame )\n{\n", iface
->name
, get_name(def
));
81 write_remoting_arguments(server
, indent
, func
, "__frame->", PASS_OUT
, PHASE_FREE
);
84 write_full_pointer_free(server
, indent
, func
);
87 print_server("}\n\n");
89 print_server("void __RPC_STUB %s_%s( PRPC_MESSAGE _pRpcMessage )\n", iface
->name
, get_name(def
));
91 /* write the functions body */
92 fprintf(server
, "{\n");
94 print_server("struct __frame_%s_%s __f, * const __frame = &__f;\n", iface
->name
, get_name(def
));
95 if (has_out_arg_or_return(func
)) print_server("RPC_STATUS _Status;\n");
96 fprintf(server
, "\n");
98 print_server("NdrServerInitializeNew(\n");
100 print_server("_pRpcMessage,\n");
101 print_server("&__frame->_StubMsg,\n");
102 print_server("&%s_StubDesc);\n", iface
->name
);
104 fprintf(server
, "\n");
105 print_server( "RpcExceptionInit( __server_filter, __finally_%s_%s );\n", iface
->name
, get_name(def
));
107 write_parameters_init(server
, indent
, func
, "__frame->");
109 if (explicit_handle_var
)
111 print_server("__frame->%s = _pRpcMessage->Handle;\n", explicit_handle_var
->name
);
112 fprintf(server
, "\n");
115 print_server("RpcTryFinally\n");
118 print_server("RpcTryExcept\n");
122 if (has_full_pointer
)
123 write_full_pointer_init(server
, indent
, func
, TRUE
);
127 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
129 print_server("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
132 fprintf(server
, "\n");
134 /* unmarshall arguments */
135 write_remoting_arguments(server
, indent
, func
, "__frame->", PASS_IN
, PHASE_UNMARSHAL
);
138 print_server("if (__frame->_StubMsg.Buffer > __frame->_StubMsg.BufferEnd)\n");
141 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
146 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
149 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
152 print_server("RpcEndExcept\n");
153 fprintf(server
, "\n");
155 /* Assign 'out' arguments */
156 assign_stub_out_args(server
, indent
, func
, "__frame->");
158 /* Call the real server function */
159 if (!is_void(get_func_return_type(func
)))
160 print_server("__frame->_RetVal = ");
163 fprintf(server
, "%s%s", prefix_server
, get_name(def
));
169 fprintf(server
, "(\n");
171 LIST_FOR_EACH_ENTRY( var
, func
->args
, const var_t
, entry
)
176 fprintf(server
, ",\n");
177 if (is_context_handle(var
->type
))
179 /* if the context_handle attribute appears in the chain of types
180 * without pointers being followed, then the context handle must
181 * be direct, otherwise it is a pointer */
182 int is_ch_ptr
= is_aliaschain_attr(var
->type
, ATTR_CONTEXTHANDLE
) ? FALSE
: TRUE
;
184 write_type_decl_left(server
, var
->type
);
185 fprintf(server
, ")%sNDRSContextValue(__frame->%s)",
186 is_ch_ptr
? "" : "*", var
->name
);
190 print_server("%s__frame->%s", var
->type
->declarray
? "*" : "", var
->name
);
193 fprintf(server
, ");\n");
198 fprintf(server
, "();\n");
201 if (has_out_arg_or_return(func
))
203 write_remoting_arguments(server
, indent
, func
, "__frame->", PASS_OUT
, PHASE_BUFFERSIZE
);
205 if (!is_void(get_func_return_type(func
)))
206 write_remoting_arguments(server
, indent
, func
, "__frame->", PASS_RETURN
, PHASE_BUFFERSIZE
);
208 print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.BufferLength;\n");
209 fprintf(server
, "\n");
210 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
211 print_server("if (_Status)\n");
213 print_server("RpcRaiseException(_Status);\n");
215 fprintf(server
, "\n");
216 print_server("__frame->_StubMsg.Buffer = _pRpcMessage->Buffer;\n");
217 fprintf(server
, "\n");
220 /* marshall arguments */
221 write_remoting_arguments(server
, indent
, func
, "__frame->", PASS_OUT
, PHASE_MARSHAL
);
223 /* marshall the return value */
224 if (!is_void(get_func_return_type(func
)))
225 write_remoting_arguments(server
, indent
, func
, "__frame->", PASS_RETURN
, PHASE_MARSHAL
);
229 print_server("RpcFinally\n");
232 print_server("__finally_%s_%s( __frame );\n", iface
->name
, get_name(def
));
235 print_server("RpcEndFinally\n");
237 /* calculate buffer length */
238 fprintf(server
, "\n");
239 print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
241 fprintf(server
, "}\n");
242 fprintf(server
, "\n");
244 /* update proc_offset */
245 *proc_offset
+= get_size_procformatstring_func( func
);
250 static void write_dispatchtable(type_t
*iface
)
252 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
253 unsigned long method_count
= 0;
256 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface
->name
);
260 if (iface
->funcs
) LIST_FOR_EACH_ENTRY( func
, iface
->funcs
, const func_t
, entry
)
262 var_t
*def
= func
->def
;
263 print_server("%s_%s,\n", iface
->name
, get_name(def
));
268 print_server("};\n");
269 print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
272 print_server("%u,\n", method_count
);
273 print_server("%s_table\n", iface
->name
);
275 print_server("};\n");
276 fprintf(server
, "\n");
280 static void write_stubdescdecl(type_t
*iface
)
282 print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface
->name
);
283 fprintf(server
, "\n");
287 static void write_stubdescriptor(type_t
*iface
, int expr_eval_routines
)
289 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface
->name
);
292 print_server("(void *)& %s___RpcServerInterface,\n", iface
->name
);
293 print_server("MIDL_user_allocate,\n");
294 print_server("MIDL_user_free,\n");
297 print_server("0,\n");
299 print_server("},\n");
300 print_server("0,\n");
301 print_server("0,\n");
302 if (expr_eval_routines
)
303 print_server("ExprEvalRoutines,\n");
305 print_server("0,\n");
306 print_server("0,\n");
307 print_server("__MIDL_TypeFormatString.Format,\n");
308 print_server("1, /* -error bounds_check flag */\n");
309 print_server("0x10001, /* Ndr library version */\n");
310 print_server("0,\n");
311 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
312 print_server("0,\n");
313 print_server("%s,\n", list_empty(&user_type_list
) ? "0" : "UserMarshalRoutines");
314 print_server("0, /* notify & notify_flag routine table */\n");
315 print_server("1, /* Flags */\n");
316 print_server("0, /* Reserved3 */\n");
317 print_server("0, /* Reserved4 */\n");
318 print_server("0 /* Reserved5 */\n");
320 print_server("};\n");
321 fprintf(server
, "\n");
325 static void write_serverinterfacedecl(type_t
*iface
)
327 unsigned long ver
= get_attrv(iface
->attrs
, ATTR_VERSION
);
328 UUID
*uuid
= get_attrp(iface
->attrs
, ATTR_UUID
);
329 const str_list_t
*endpoints
= get_attrp(iface
->attrs
, ATTR_ENDPOINT
);
331 if (endpoints
) write_endpoints( server
, iface
->name
, endpoints
);
333 print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
334 fprintf(server
, "\n");
335 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface
->name
);
338 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
339 print_server("{{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",
340 uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
341 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6],
342 uuid
->Data4
[7], MAJORVERSION(ver
), MINORVERSION(ver
));
343 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
344 print_server("&%s_v%d_%d_DispatchTable,\n", iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
));
347 print_server("%u,\n", list_count(endpoints
));
348 print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface
->name
);
352 print_server("0,\n");
353 print_server("0,\n");
355 print_server("0,\n");
356 print_server("0,\n");
357 print_server("0,\n");
359 print_server("};\n");
361 print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
362 iface
->name
, iface
->name
);
364 print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
365 prefix_server
, iface
->name
, MAJORVERSION(ver
), MINORVERSION(ver
), iface
->name
);
366 fprintf(server
, "\n");
370 static void init_server(void)
374 if (!(server
= fopen(server_name
, "w")))
375 error("Could not open %s for output\n", server_name
);
377 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION
, input_name
);
378 print_server("#include <string.h>\n");
379 fprintf(server
, "\n");
380 print_server("#include \"%s\"\n", header_name
);
382 write_exceptions( server
);
384 print_server("struct __server_frame\n");
386 print_server(" __DECL_EXCEPTION_FRAME;\n");
387 print_server(" MIDL_STUB_MESSAGE _StubMsg;\n");
388 print_server("};\n");
390 print_server("static int __server_filter( struct __server_frame *__frame )\n");
391 print_server( "{\n");
392 print_server( " return RPC_BAD_STUB_DATA_EXCEPTION_FILTER;\n");
393 print_server( "}\n");
398 static void write_server_stmts(const statement_list_t
*stmts
, int expr_eval_routines
, unsigned int *proc_offset
)
400 const statement_t
*stmt
;
401 if (stmts
) LIST_FOR_EACH_ENTRY( stmt
, stmts
, const statement_t
, entry
)
403 if (stmt
->type
== STMT_LIBRARY
)
404 write_server_stmts(stmt
->u
.lib
->stmts
, expr_eval_routines
, proc_offset
);
405 else if (stmt
->type
== STMT_TYPE
&& stmt
->u
.type
->type
== RPC_FC_IP
)
407 type_t
*iface
= stmt
->u
.type
;
408 if (!need_stub(iface
))
411 fprintf(server
, "/*****************************************************************************\n");
412 fprintf(server
, " * %s interface\n", iface
->name
);
413 fprintf(server
, " */\n");
414 fprintf(server
, "\n");
418 write_serverinterfacedecl(iface
);
419 write_stubdescdecl(iface
);
421 write_function_stubs(iface
, proc_offset
);
423 print_server("#if !defined(__RPC_WIN32__)\n");
424 print_server("#error Invalid build platform for this stub.\n");
425 print_server("#endif\n");
427 fprintf(server
, "\n");
428 write_stubdescriptor(iface
, expr_eval_routines
);
429 write_dispatchtable(iface
);
435 void write_server(const statement_list_t
*stmts
)
437 unsigned int proc_offset
= 0;
438 int expr_eval_routines
;
442 if (do_everything
&& !need_stub_files(stmts
))
449 write_formatstringsdecl(server
, indent
, stmts
, need_stub
);
450 expr_eval_routines
= write_expr_eval_routines(server
, server_token
);
451 if (expr_eval_routines
)
452 write_expr_eval_routine_list(server
, server_token
);
453 write_user_quad_list(server
);
455 write_server_stmts(stmts
, expr_eval_routines
, &proc_offset
);
457 fprintf(server
, "\n");
459 write_procformatstring(server
, stmts
, need_stub
);
460 write_typeformatstring(server
, stmts
, need_stub
);