hidclass.sys: Create link collection caps during parsing.
[wine.git] / tools / widl / server.c
blobcce880bbf6600495e339483dcdb6112f2a45ef88
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"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <ctype.h>
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
37 #include "typegen.h"
39 static FILE* server;
40 static int indent = 0;
43 static void print_server(const char *format, ...) __attribute__((format (printf, 1, 2)));
44 static void print_server(const char *format, ...)
46 va_list va;
47 va_start(va, format);
48 print(server, indent, format, va);
49 va_end(va);
52 static void write_function_stub(const type_t *iface, const var_t *func, unsigned int proc_offset)
54 const var_t *var;
55 unsigned char explicit_fc, implicit_fc;
56 int has_full_pointer = is_full_pointer_function(func);
57 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
58 type_t *ret_type = type_function_get_rettype(func->declspec.type);
60 if (is_interpreted_func( iface, func )) return;
62 print_server("struct __frame_%s_%s\n{\n", iface->name, get_name(func));
63 indent++;
64 print_server("__DECL_EXCEPTION_FRAME\n");
65 print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
67 /* Declare arguments */
68 declare_stub_args(server, indent, func);
70 indent--;
71 print_server("};\n\n");
73 print_server("static void __finally_%s_%s(", iface->name, get_name(func));
74 fprintf(server," struct __frame_%s_%s *__frame )\n{\n", iface->name, get_name(func));
76 indent++;
77 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
79 if (!is_void(ret_type))
80 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_FREE);
82 if (has_full_pointer)
83 write_full_pointer_free(server, indent, func);
85 indent--;
86 print_server("}\n\n");
88 print_server("void __RPC_STUB %s_%s( PRPC_MESSAGE _pRpcMessage )\n", iface->name, get_name(func));
90 /* write the functions body */
91 fprintf(server, "{\n");
92 indent++;
93 print_server("struct __frame_%s_%s __f, * const __frame = &__f;\n", iface->name, get_name(func));
94 if (has_out_arg_or_return(func)) print_server("RPC_STATUS _Status;\n");
95 fprintf(server, "\n");
97 print_server("NdrServerInitializeNew(\n");
98 indent++;
99 print_server("_pRpcMessage,\n");
100 print_server("&__frame->_StubMsg,\n");
101 print_server("&%s_StubDesc);\n", iface->name);
102 indent--;
103 fprintf(server, "\n");
104 print_server( "RpcExceptionInit( __server_filter, __finally_%s_%s );\n", iface->name, get_name(func));
106 write_parameters_init(server, indent, func, "__frame->");
108 if (explicit_fc == FC_BIND_PRIMITIVE)
110 print_server("__frame->%s = _pRpcMessage->Handle;\n", handle_var->name);
111 fprintf(server, "\n");
114 print_server("RpcTryFinally\n");
115 print_server("{\n");
116 indent++;
117 print_server("RpcTryExcept\n");
118 print_server("{\n");
119 indent++;
121 if (has_full_pointer)
122 write_full_pointer_init(server, indent, func, TRUE);
124 if (type_function_get_args(func->declspec.type))
126 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
127 indent++;
128 print_server("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
129 proc_offset);
130 indent--;
131 fprintf(server, "\n");
133 /* unmarshall arguments */
134 write_remoting_arguments(server, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
137 print_server("if (__frame->_StubMsg.Buffer > __frame->_StubMsg.BufferEnd)\n");
138 print_server("{\n");
139 indent++;
140 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
141 indent--;
142 print_server("}\n");
143 indent--;
144 print_server("}\n");
145 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
146 print_server("{\n");
147 indent++;
148 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
149 indent--;
150 print_server("}\n");
151 print_server("RpcEndExcept\n");
152 fprintf(server, "\n");
154 /* Assign 'out' arguments */
155 assign_stub_out_args(server, indent, func, "__frame->");
157 /* Call the real server function */
158 if (is_context_handle(ret_type))
160 print_server("__frame->_RetVal = NDRSContextUnmarshall((char*)0, _pRpcMessage->DataRepresentation);\n");
161 print_server("*((");
162 write_type_decl(server, type_function_get_ret(func->declspec.type), NULL);
163 fprintf(server, "*)NDRSContextValue(__frame->_RetVal)) = ");
165 else
166 print_server("%s", is_void(ret_type) ? "" : "__frame->_RetVal = ");
167 fprintf(server, "%s%s", prefix_server, get_name(func));
169 if (type_function_get_args(func->declspec.type))
171 int first_arg = 1;
173 fprintf(server, "(\n");
174 indent++;
175 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
177 if (first_arg)
178 first_arg = 0;
179 else
180 fprintf(server, ",\n");
181 if (is_context_handle(var->declspec.type))
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 const char *ch_ptr = is_aliaschain_attr(var->declspec.type, ATTR_CONTEXTHANDLE) ? "*" : "";
187 print_server("(");
188 write_type_decl_left(server, &var->declspec);
189 fprintf(server, ")%sNDRSContextValue(__frame->%s)", ch_ptr, var->name);
191 else
193 print_server("%s__frame->%s", is_array(var->declspec.type) && !type_array_is_decl_as_ptr(var->declspec.type) ? "*" : "", var->name);
196 fprintf(server, ");\n");
197 indent--;
199 else
201 fprintf(server, "();\n");
204 if (has_out_arg_or_return(func))
206 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
208 if (!is_void(ret_type))
209 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
211 print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.BufferLength;\n");
212 fprintf(server, "\n");
213 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
214 print_server("if (_Status)\n");
215 indent++;
216 print_server("RpcRaiseException(_Status);\n");
217 indent--;
218 fprintf(server, "\n");
219 print_server("__frame->_StubMsg.Buffer = _pRpcMessage->Buffer;\n");
220 fprintf(server, "\n");
223 /* marshall arguments */
224 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
226 /* marshall the return value */
227 if (!is_void(ret_type))
228 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
230 indent--;
231 print_server("}\n");
232 print_server("RpcFinally\n");
233 print_server("{\n");
234 indent++;
235 print_server("__finally_%s_%s( __frame );\n", iface->name, get_name(func));
236 indent--;
237 print_server("}\n");
238 print_server("RpcEndFinally\n");
240 /* calculate buffer length */
241 fprintf(server, "\n");
242 print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
243 indent--;
244 fprintf(server, "}\n");
245 fprintf(server, "\n");
249 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
251 const statement_t *stmt;
253 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
255 var_t *func = stmt->u.var;
257 write_function_stub( iface, func, *proc_offset );
259 /* update proc_offset */
260 func->procstring_offset = *proc_offset;
261 *proc_offset += get_size_procformatstring_func( iface, func );
266 static void write_dispatchtable(type_t *iface)
268 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
269 unsigned int method_count = 0;
270 const statement_t *stmt;
272 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
273 print_server("{\n");
274 indent++;
276 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
278 var_t *func = stmt->u.var;
279 if (is_interpreted_func( iface, func ))
280 print_server("%s,\n", get_stub_mode() == MODE_Oif ? "NdrServerCall2" : "NdrServerCall");
281 else
282 print_server("%s_%s,\n", iface->name, get_name(func));
283 method_count++;
285 print_server("0\n");
286 indent--;
287 print_server("};\n");
288 print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
289 print_server("{\n");
290 indent++;
291 print_server("%u,\n", method_count);
292 print_server("%s_table\n", iface->name);
293 indent--;
294 print_server("};\n");
295 fprintf(server, "\n");
299 static void write_routinetable(type_t *iface)
301 const statement_t *stmt;
303 print_server( "static const SERVER_ROUTINE %s_ServerRoutineTable[] =\n", iface->name );
304 print_server( "{\n" );
305 indent++;
306 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
308 var_t *func = stmt->u.var;
309 if (is_local( func->attrs )) continue;
310 print_server( "(void *)%s%s,\n", prefix_server, get_name(func));
312 indent--;
313 print_server( "};\n\n" );
317 static void write_rundown_routines(void)
319 context_handle_t *ch;
320 int count = list_count( &context_handle_list );
322 if (!count) return;
323 print_server( "static const NDR_RUNDOWN RundownRoutines[] =\n" );
324 print_server( "{\n" );
325 indent++;
326 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
328 print_server( "%s_rundown", ch->name );
329 if (--count) fputc( ',', server );
330 fputc( '\n', server );
332 indent--;
333 print_server( "};\n\n" );
337 static void write_serverinfo(type_t *iface)
339 print_server( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
340 print_server( "{\n" );
341 indent++;
342 print_server( "&%s_StubDesc,\n", iface->name );
343 print_server( "%s_ServerRoutineTable,\n", iface->name );
344 print_server( "__MIDL_ProcFormatString.Format,\n" );
345 print_server( "%s_FormatStringOffsetTable,\n", iface->name );
346 print_server( "0,\n" );
347 print_server( "0,\n" );
348 print_server( "0,\n" );
349 print_server( "0\n" );
350 indent--;
351 print_server( "};\n\n" );
355 static void write_stubdescdecl(type_t *iface)
357 print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
358 fprintf(server, "\n");
362 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
364 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
365 print_server("{\n");
366 indent++;
367 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
368 print_server("MIDL_user_allocate,\n");
369 print_server("MIDL_user_free,\n");
370 print_server("{\n");
371 indent++;
372 print_server("0,\n");
373 indent--;
374 print_server("},\n");
375 if (!list_empty( &context_handle_list ))
376 print_server("RundownRoutines,\n");
377 else
378 print_server("0,\n");
379 print_server("0,\n");
380 if (expr_eval_routines)
381 print_server("ExprEvalRoutines,\n");
382 else
383 print_server("0,\n");
384 print_server("0,\n");
385 print_server("__MIDL_TypeFormatString.Format,\n");
386 print_server("1, /* -error bounds_check flag */\n");
387 print_server("0x%x, /* Ndr library version */\n", get_stub_mode() == MODE_Oif ? 0x50002 : 0x10001);
388 print_server("0,\n");
389 print_server("0x50200ca, /* MIDL Version 5.2.202 */\n");
390 print_server("0,\n");
391 print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
392 print_server("0, /* notify & notify_flag routine table */\n");
393 print_server("1, /* Flags */\n");
394 print_server("0, /* Reserved3 */\n");
395 print_server("0, /* Reserved4 */\n");
396 print_server("0 /* Reserved5 */\n");
397 indent--;
398 print_server("};\n");
399 fprintf(server, "\n");
403 static void write_serverinterfacedecl(type_t *iface)
405 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
406 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
407 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
409 if (endpoints) write_endpoints( server, iface->name, endpoints );
411 print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
412 print_server( "static const MIDL_SERVER_INFO %s_ServerInfo;\n", iface->name );
413 fprintf(server, "\n");
414 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
415 print_server("{\n");
416 indent++;
417 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
418 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",
419 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
420 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
421 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
422 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
423 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
424 if (endpoints)
426 print_server("%u,\n", list_count(endpoints));
427 print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
429 else
431 print_server("0,\n");
432 print_server("0,\n");
434 print_server("0,\n");
435 print_server("&%s_ServerInfo,\n", iface->name);
436 print_server("0,\n");
437 indent--;
438 print_server("};\n");
439 if (old_names)
440 print_server("RPC_IF_HANDLE %s_ServerIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
441 iface->name, iface->name);
442 else
443 print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
444 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
445 fprintf(server, "\n");
449 static void init_server(void)
451 if (server)
452 return;
453 if (!(server = fopen(server_name, "w")))
454 error("Could not open %s for output\n", server_name);
456 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
457 print_server("#include <string.h>\n");
458 fprintf(server, "\n");
459 print_server("#include \"%s\"\n", header_name);
460 print_server("\n");
461 print_server( "#ifndef DECLSPEC_HIDDEN\n");
462 print_server( "#define DECLSPEC_HIDDEN\n");
463 print_server( "#endif\n");
464 print_server( "\n");
468 static void write_server_stmts(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
470 const statement_t *stmt;
471 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
473 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
475 type_t *iface = stmt->u.type;
476 if (!need_stub(iface))
477 continue;
479 fprintf(server, "/*****************************************************************************\n");
480 fprintf(server, " * %s interface\n", iface->name);
481 fprintf(server, " */\n");
482 fprintf(server, "\n");
484 if (statements_has_func(type_iface_get_stmts(iface)))
486 write_serverinterfacedecl(iface);
487 write_stubdescdecl(iface);
489 write_function_stubs(iface, proc_offset);
491 print_server("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
492 print_server("#error Invalid build platform for this stub.\n");
493 print_server("#endif\n");
495 fprintf(server, "\n");
496 write_procformatstring_offsets( server, iface );
497 write_stubdescriptor(iface, expr_eval_routines);
498 write_dispatchtable(iface);
499 write_routinetable(iface);
500 write_serverinfo(iface);
506 static void write_server_routines(const statement_list_t *stmts)
508 unsigned int proc_offset = 0;
509 int expr_eval_routines;
511 if (need_inline_stubs_file( stmts ))
513 write_exceptions( server );
514 print_server("\n");
515 print_server("struct __server_frame\n");
516 print_server("{\n");
517 print_server(" __DECL_EXCEPTION_FRAME\n");
518 print_server(" MIDL_STUB_MESSAGE _StubMsg;\n");
519 print_server("};\n");
520 print_server("\n");
521 print_server("static int __server_filter( struct __server_frame *__frame )\n");
522 print_server( "{\n");
523 print_server( " return (__frame->code == STATUS_ACCESS_VIOLATION) ||\n");
524 print_server( " (__frame->code == STATUS_DATATYPE_MISALIGNMENT) ||\n");
525 print_server( " (__frame->code == RPC_X_BAD_STUB_DATA) ||\n");
526 print_server( " (__frame->code == RPC_S_INVALID_BOUND);\n");
527 print_server( "}\n");
528 print_server( "\n");
531 write_formatstringsdecl(server, indent, stmts, need_stub);
532 expr_eval_routines = write_expr_eval_routines(server, server_token);
533 if (expr_eval_routines)
534 write_expr_eval_routine_list(server, server_token);
535 write_user_quad_list(server);
536 write_rundown_routines();
538 write_server_stmts(stmts, expr_eval_routines, &proc_offset);
540 write_procformatstring(server, stmts, need_stub);
541 write_typeformatstring(server, stmts, need_stub);
544 void write_server(const statement_list_t *stmts)
546 if (!do_server)
547 return;
548 if (do_everything && !need_stub_files(stmts))
549 return;
551 init_server();
552 if (!server)
553 return;
555 write_server_routines( stmts );
556 fclose(server);