winepulse: Move AudioClient's GetService into mmdevapi.
[wine.git] / tools / widl / server.c
blob1d8ab41185d35510613e40f0cd95b2ad0ceb5047
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"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
28 #include "widl.h"
29 #include "utils.h"
30 #include "parser.h"
31 #include "header.h"
33 #include "typegen.h"
35 static FILE* server;
36 static int indent = 0;
39 static void print_server(const char *format, ...) __attribute__((format (printf, 1, 2)));
40 static void print_server(const char *format, ...)
42 va_list va;
43 va_start(va, format);
44 print(server, indent, format, va);
45 va_end(va);
48 static void write_function_stub(const type_t *iface, const var_t *func, unsigned int proc_offset)
50 const var_t *var;
51 unsigned char explicit_fc, implicit_fc;
52 int has_full_pointer = is_full_pointer_function(func);
53 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
54 type_t *ret_type = type_function_get_rettype(func->declspec.type);
56 if (is_interpreted_func( iface, func )) return;
58 print_server("struct __frame_%s_%s\n{\n", iface->name, get_name(func));
59 indent++;
60 print_server("__DECL_EXCEPTION_FRAME\n");
61 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
63 /* Declare arguments */
64 declare_stub_args(server, indent, func);
66 indent--;
67 print_server("};\n\n");
69 print_server("static void __finally_%s_%s(", iface->name, get_name(func));
70 fprintf(server," struct __frame_%s_%s *__frame )\n{\n", iface->name, get_name(func));
72 indent++;
73 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
75 if (!is_void(ret_type))
76 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_FREE);
78 if (has_full_pointer)
79 write_full_pointer_free(server, indent, func);
81 indent--;
82 print_server("}\n\n");
84 print_server("void __RPC_STUB %s_%s( PRPC_MESSAGE _pRpcMessage )\n", iface->name, get_name(func));
86 /* write the functions body */
87 fprintf(server, "{\n");
88 indent++;
89 print_server("struct __frame_%s_%s __f, * const __frame = &__f;\n", iface->name, get_name(func));
90 if (has_out_arg_or_return(func)) print_server("RPC_STATUS _Status;\n");
91 fprintf(server, "\n");
93 print_server("NdrServerInitializeNew(\n");
94 indent++;
95 print_server("_pRpcMessage,\n");
96 print_server("&__frame->_StubMsg,\n");
97 print_server("&%s_StubDesc);\n", iface->name);
98 indent--;
99 fprintf(server, "\n");
100 print_server( "RpcExceptionInit( __server_filter, __finally_%s_%s );\n", iface->name, get_name(func));
102 write_parameters_init(server, indent, func, "__frame->");
104 if (explicit_fc == FC_BIND_PRIMITIVE)
106 print_server("__frame->%s = _pRpcMessage->Handle;\n", handle_var->name);
107 fprintf(server, "\n");
110 print_server("RpcTryFinally\n");
111 print_server("{\n");
112 indent++;
113 print_server("RpcTryExcept\n");
114 print_server("{\n");
115 indent++;
117 if (has_full_pointer)
118 write_full_pointer_init(server, indent, func, TRUE);
120 if (type_function_get_args(func->declspec.type))
122 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
123 indent++;
124 print_server("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
125 proc_offset);
126 indent--;
127 fprintf(server, "\n");
129 /* unmarshall arguments */
130 write_remoting_arguments(server, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
133 print_server("if (__frame->_StubMsg.Buffer > __frame->_StubMsg.BufferEnd)\n");
134 print_server("{\n");
135 indent++;
136 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
137 indent--;
138 print_server("}\n");
139 indent--;
140 print_server("}\n");
141 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
142 print_server("{\n");
143 indent++;
144 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
145 indent--;
146 print_server("}\n");
147 print_server("RpcEndExcept\n");
148 fprintf(server, "\n");
150 /* Assign 'out' arguments */
151 assign_stub_out_args(server, indent, func, "__frame->");
153 /* Call the real server function */
154 if (is_context_handle(ret_type))
156 print_server("__frame->_RetVal = NDRSContextUnmarshall((char*)0, _pRpcMessage->DataRepresentation);\n");
157 print_server("*((");
158 write_type_decl(server, type_function_get_ret(func->declspec.type), NULL);
159 fprintf(server, "*)NDRSContextValue(__frame->_RetVal)) = ");
161 else
162 print_server("%s", is_void(ret_type) ? "" : "__frame->_RetVal = ");
163 fprintf(server, "%s%s", prefix_server, get_name(func));
165 if (type_function_get_args(func->declspec.type))
167 int first_arg = 1;
169 fprintf(server, "(\n");
170 indent++;
171 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
173 if (first_arg)
174 first_arg = 0;
175 else
176 fprintf(server, ",\n");
177 if (is_context_handle(var->declspec.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 const char *ch_ptr = is_aliaschain_attr(var->declspec.type, ATTR_CONTEXTHANDLE) ? "*" : "";
183 print_server("(");
184 write_type_decl_left(server, &var->declspec);
185 fprintf(server, ")%sNDRSContextValue(__frame->%s)", ch_ptr, var->name);
187 else
189 print_server("%s__frame->%s", is_array(var->declspec.type) && !type_array_is_decl_as_ptr(var->declspec.type) ? "*" : "", var->name);
192 fprintf(server, ");\n");
193 indent--;
195 else
197 fprintf(server, "();\n");
200 if (has_out_arg_or_return(func))
202 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
204 if (!is_void(ret_type))
205 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
207 print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.BufferLength;\n");
208 fprintf(server, "\n");
209 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
210 print_server("if (_Status)\n");
211 indent++;
212 print_server("RpcRaiseException(_Status);\n");
213 indent--;
214 fprintf(server, "\n");
215 print_server("__frame->_StubMsg.Buffer = _pRpcMessage->Buffer;\n");
216 fprintf(server, "\n");
219 /* marshall arguments */
220 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
222 /* marshall the return value */
223 if (!is_void(ret_type))
224 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
226 indent--;
227 print_server("}\n");
228 print_server("RpcFinally\n");
229 print_server("{\n");
230 indent++;
231 print_server("__finally_%s_%s( __frame );\n", iface->name, get_name(func));
232 indent--;
233 print_server("}\n");
234 print_server("RpcEndFinally\n");
236 /* calculate buffer length */
237 fprintf(server, "\n");
238 print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
239 indent--;
240 fprintf(server, "}\n");
241 fprintf(server, "\n");
245 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
247 const statement_t *stmt;
249 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
251 var_t *func = stmt->u.var;
253 write_function_stub( iface, func, *proc_offset );
255 /* update proc_offset */
256 func->procstring_offset = *proc_offset;
257 *proc_offset += get_size_procformatstring_func( iface, func );
262 static void write_dispatchtable(type_t *iface)
264 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
265 unsigned int method_count = 0;
266 const statement_t *stmt;
268 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
269 print_server("{\n");
270 indent++;
272 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
274 var_t *func = stmt->u.var;
275 if (is_interpreted_func( iface, func ))
276 print_server("%s,\n", get_stub_mode() == MODE_Oif ? "NdrServerCall2" : "NdrServerCall");
277 else
278 print_server("%s_%s,\n", iface->name, get_name(func));
279 method_count++;
281 print_server("0\n");
282 indent--;
283 print_server("};\n");
284 print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
285 print_server("{\n");
286 indent++;
287 print_server("%u,\n", method_count);
288 print_server("%s_table\n", iface->name);
289 indent--;
290 print_server("};\n");
291 fprintf(server, "\n");
295 static void write_routinetable(type_t *iface)
297 const statement_t *stmt;
299 print_server( "static const SERVER_ROUTINE %s_ServerRoutineTable[] =\n", iface->name );
300 print_server( "{\n" );
301 indent++;
302 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
304 var_t *func = stmt->u.var;
305 if (is_local( func->attrs )) continue;
306 print_server( "(void *)%s%s,\n", prefix_server, get_name(func));
308 indent--;
309 print_server( "};\n\n" );
313 static void write_rundown_routines(void)
315 context_handle_t *ch;
316 int count = list_count( &context_handle_list );
318 if (!count) return;
319 print_server( "static const NDR_RUNDOWN RundownRoutines[] =\n" );
320 print_server( "{\n" );
321 indent++;
322 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
324 print_server( "%s_rundown", ch->name );
325 if (--count) fputc( ',', server );
326 fputc( '\n', server );
328 indent--;
329 print_server( "};\n\n" );
333 static void write_serverinfo(type_t *iface)
335 print_server( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
336 print_server( "{\n" );
337 indent++;
338 print_server( "&%s_StubDesc,\n", iface->name );
339 print_server( "%s_ServerRoutineTable,\n", iface->name );
340 print_server( "__MIDL_ProcFormatString.Format,\n" );
341 print_server( "%s_FormatStringOffsetTable,\n", iface->name );
342 print_server( "0,\n" );
343 print_server( "0,\n" );
344 print_server( "0,\n" );
345 print_server( "0\n" );
346 indent--;
347 print_server( "};\n\n" );
351 static void write_stubdescdecl(type_t *iface)
353 print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
354 fprintf(server, "\n");
358 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
360 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
361 print_server("{\n");
362 indent++;
363 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
364 print_server("MIDL_user_allocate,\n");
365 print_server("MIDL_user_free,\n");
366 print_server("{\n");
367 indent++;
368 print_server("0,\n");
369 indent--;
370 print_server("},\n");
371 if (!list_empty( &context_handle_list ))
372 print_server("RundownRoutines,\n");
373 else
374 print_server("0,\n");
375 print_server("0,\n");
376 if (expr_eval_routines)
377 print_server("ExprEvalRoutines,\n");
378 else
379 print_server("0,\n");
380 print_server("0,\n");
381 print_server("__MIDL_TypeFormatString.Format,\n");
382 print_server("1, /* -error bounds_check flag */\n");
383 print_server("0x%x, /* Ndr library version */\n", get_stub_mode() == MODE_Oif ? 0x50002 : 0x10001);
384 print_server("0,\n");
385 print_server("0x50200ca, /* MIDL Version 5.2.202 */\n");
386 print_server("0,\n");
387 print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
388 print_server("0, /* notify & notify_flag routine table */\n");
389 print_server("1, /* Flags */\n");
390 print_server("0, /* Reserved3 */\n");
391 print_server("0, /* Reserved4 */\n");
392 print_server("0 /* Reserved5 */\n");
393 indent--;
394 print_server("};\n");
395 fprintf(server, "\n");
399 static void write_serverinterfacedecl(type_t *iface)
401 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
402 struct uuid *uuid = get_attrp(iface->attrs, ATTR_UUID);
403 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
405 if (endpoints) write_endpoints( server, iface->name, endpoints );
407 print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
408 print_server( "static const MIDL_SERVER_INFO %s_ServerInfo;\n", iface->name );
409 fprintf(server, "\n");
410 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
411 print_server("{\n");
412 indent++;
413 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
414 print_server("{{0x%08x,0x%04x,0x%04x,{0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x}},{%d,%d}},\n",
415 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
416 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
417 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
418 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
419 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
420 if (endpoints)
422 print_server("%u,\n", list_count(endpoints));
423 print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
425 else
427 print_server("0,\n");
428 print_server("0,\n");
430 print_server("0,\n");
431 print_server("&%s_ServerInfo,\n", iface->name);
432 print_server("0,\n");
433 indent--;
434 print_server("};\n");
435 if (old_names)
436 print_server("RPC_IF_HANDLE %s_ServerIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
437 iface->name, iface->name);
438 else
439 print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
440 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
441 fprintf(server, "\n");
445 static void init_server(void)
447 if (server)
448 return;
449 if (!(server = fopen(server_name, "w")))
450 error("Could not open %s for output\n", server_name);
452 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
453 print_server("#include <string.h>\n");
454 fprintf(server, "\n");
455 print_server("#include \"%s\"\n", header_name);
456 print_server("\n");
457 print_server( "#ifndef DECLSPEC_HIDDEN\n");
458 print_server( "#define DECLSPEC_HIDDEN\n");
459 print_server( "#endif\n");
460 print_server( "\n");
464 static void write_server_stmts(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
466 const statement_t *stmt;
467 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
469 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
471 type_t *iface = stmt->u.type;
472 if (!need_stub(iface))
473 continue;
475 fprintf(server, "/*****************************************************************************\n");
476 fprintf(server, " * %s interface\n", iface->name);
477 fprintf(server, " */\n");
478 fprintf(server, "\n");
480 if (statements_has_func(type_iface_get_stmts(iface)))
482 write_serverinterfacedecl(iface);
483 write_stubdescdecl(iface);
485 write_function_stubs(iface, proc_offset);
487 print_server("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
488 print_server("#error Invalid build platform for this stub.\n");
489 print_server("#endif\n");
491 fprintf(server, "\n");
492 write_procformatstring_offsets( server, iface );
493 write_stubdescriptor(iface, expr_eval_routines);
494 write_dispatchtable(iface);
495 write_routinetable(iface);
496 write_serverinfo(iface);
502 static void write_server_routines(const statement_list_t *stmts)
504 unsigned int proc_offset = 0;
505 int expr_eval_routines;
507 if (need_inline_stubs_file( stmts ))
509 write_exceptions( server );
510 print_server("\n");
511 print_server("struct __server_frame\n");
512 print_server("{\n");
513 print_server(" __DECL_EXCEPTION_FRAME\n");
514 print_server(" MIDL_STUB_MESSAGE _StubMsg;\n");
515 print_server("};\n");
516 print_server("\n");
517 print_server("static int __server_filter( struct __server_frame *__frame )\n");
518 print_server( "{\n");
519 print_server( " return (__frame->code == STATUS_ACCESS_VIOLATION) ||\n");
520 print_server( " (__frame->code == STATUS_DATATYPE_MISALIGNMENT) ||\n");
521 print_server( " (__frame->code == RPC_X_BAD_STUB_DATA) ||\n");
522 print_server( " (__frame->code == RPC_S_INVALID_BOUND);\n");
523 print_server( "}\n");
524 print_server( "\n");
527 write_formatstringsdecl(server, indent, stmts, need_stub);
528 expr_eval_routines = write_expr_eval_routines(server, server_token);
529 if (expr_eval_routines)
530 write_expr_eval_routine_list(server, server_token);
531 write_user_quad_list(server);
532 write_rundown_routines();
534 write_server_stmts(stmts, expr_eval_routines, &proc_offset);
536 write_procformatstring(server, stmts, need_stub);
537 write_typeformatstring(server, stmts, need_stub);
540 void write_server(const statement_list_t *stmts)
542 if (!do_server)
543 return;
544 if (do_everything && !need_stub_files(stmts))
545 return;
547 init_server();
548 if (!server)
549 return;
551 write_server_routines( stmts );
552 fclose(server);