wer: Add new stubbed wer.dll.
[wine/hramrach.git] / tools / widl / server.c
blobfd6489939135bd34cff110baccc4d1f471eb579c
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_stubs(type_t *iface, unsigned int *proc_offset)
54 const statement_t *stmt;
55 const var_t *var;
56 const var_t* explicit_handle_var;
58 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
60 const var_t *func = stmt->u.var;
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(func));
67 indent++;
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);
74 indent--;
75 print_server("};\n\n");
77 print_server("static void __finally_%s_%s(", iface->name, get_name(func));
78 fprintf(server," struct __frame_%s_%s *__frame )\n{\n", iface->name, get_name(func));
80 indent++;
81 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
83 if (!is_void(type_function_get_rettype(func->type)))
84 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_FREE);
86 if (has_full_pointer)
87 write_full_pointer_free(server, indent, func);
89 indent--;
90 print_server("}\n\n");
92 print_server("void __RPC_STUB %s_%s( PRPC_MESSAGE _pRpcMessage )\n", iface->name, get_name(func));
94 /* write the functions body */
95 fprintf(server, "{\n");
96 indent++;
97 print_server("struct __frame_%s_%s __f, * const __frame = &__f;\n", iface->name, get_name(func));
98 if (has_out_arg_or_return(func)) print_server("RPC_STATUS _Status;\n");
99 fprintf(server, "\n");
101 print_server("NdrServerInitializeNew(\n");
102 indent++;
103 print_server("_pRpcMessage,\n");
104 print_server("&__frame->_StubMsg,\n");
105 print_server("&%s_StubDesc);\n", iface->name);
106 indent--;
107 fprintf(server, "\n");
108 print_server( "RpcExceptionInit( __server_filter, __finally_%s_%s );\n", iface->name, get_name(func));
110 write_parameters_init(server, indent, func, "__frame->");
112 if (explicit_handle_var)
114 print_server("__frame->%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
115 fprintf(server, "\n");
118 print_server("RpcTryFinally\n");
119 print_server("{\n");
120 indent++;
121 print_server("RpcTryExcept\n");
122 print_server("{\n");
123 indent++;
125 if (has_full_pointer)
126 write_full_pointer_init(server, indent, func, TRUE);
128 if (type_get_function_args(func->type))
130 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
131 indent++;
132 print_server("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
133 *proc_offset);
134 indent--;
135 fprintf(server, "\n");
137 /* unmarshall arguments */
138 write_remoting_arguments(server, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
141 print_server("if (__frame->_StubMsg.Buffer > __frame->_StubMsg.BufferEnd)\n");
142 print_server("{\n");
143 indent++;
144 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
145 indent--;
146 print_server("}\n");
147 indent--;
148 print_server("}\n");
149 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
150 print_server("{\n");
151 indent++;
152 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
153 indent--;
154 print_server("}\n");
155 print_server("RpcEndExcept\n");
156 fprintf(server, "\n");
158 /* Assign 'out' arguments */
159 assign_stub_out_args(server, indent, func, "__frame->");
161 /* Call the real server function */
162 print_server("%s%s%s",
163 is_void(type_function_get_rettype(func->type)) ? "" : "__frame->_RetVal = ",
164 prefix_server, get_name(func));
166 if (type_get_function_args(func->type))
168 int first_arg = 1;
170 fprintf(server, "(\n");
171 indent++;
172 LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
174 if (first_arg)
175 first_arg = 0;
176 else
177 fprintf(server, ",\n");
178 if (is_context_handle(var->type))
180 /* if the context_handle attribute appears in the chain of types
181 * without pointers being followed, then the context handle must
182 * be direct, otherwise it is a pointer */
183 int is_ch_ptr = is_aliaschain_attr(var->type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
184 print_server("(");
185 write_type_decl_left(server, var->type);
186 fprintf(server, ")%sNDRSContextValue(__frame->%s)",
187 is_ch_ptr ? "" : "*", var->name);
189 else
191 print_server("%s__frame->%s", is_array(var->type) && !type_array_is_decl_as_ptr(var->type) ? "*" : "", var->name);
194 fprintf(server, ");\n");
195 indent--;
197 else
199 fprintf(server, "();\n");
202 if (has_out_arg_or_return(func))
204 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
206 if (!is_void(type_function_get_rettype(func->type)))
207 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
209 print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.BufferLength;\n");
210 fprintf(server, "\n");
211 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
212 print_server("if (_Status)\n");
213 indent++;
214 print_server("RpcRaiseException(_Status);\n");
215 indent--;
216 fprintf(server, "\n");
217 print_server("__frame->_StubMsg.Buffer = _pRpcMessage->Buffer;\n");
218 fprintf(server, "\n");
221 /* marshall arguments */
222 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
224 /* marshall the return value */
225 if (!is_void(type_function_get_rettype(func->type)))
226 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
228 indent--;
229 print_server("}\n");
230 print_server("RpcFinally\n");
231 print_server("{\n");
232 indent++;
233 print_server("__finally_%s_%s( __frame );\n", iface->name, get_name(func));
234 indent--;
235 print_server("}\n");
236 print_server("RpcEndFinally\n");
238 /* calculate buffer length */
239 fprintf(server, "\n");
240 print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
241 indent--;
242 fprintf(server, "}\n");
243 fprintf(server, "\n");
245 /* update proc_offset */
246 *proc_offset += get_size_procformatstring_func( func );
251 static void write_dispatchtable(type_t *iface)
253 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
254 unsigned int method_count = 0;
255 const statement_t *stmt;
257 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
258 print_server("{\n");
259 indent++;
261 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
263 var_t *func = stmt->u.var;
264 print_server("%s_%s,\n", iface->name, get_name(func));
265 method_count++;
267 print_server("0\n");
268 indent--;
269 print_server("};\n");
270 print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
271 print_server("{\n");
272 indent++;
273 print_server("%u,\n", method_count);
274 print_server("%s_table\n", iface->name);
275 indent--;
276 print_server("};\n");
277 fprintf(server, "\n");
281 static void write_stubdescdecl(type_t *iface)
283 print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
284 fprintf(server, "\n");
288 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
290 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
291 print_server("{\n");
292 indent++;
293 print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
294 print_server("MIDL_user_allocate,\n");
295 print_server("MIDL_user_free,\n");
296 print_server("{\n");
297 indent++;
298 print_server("0,\n");
299 indent--;
300 print_server("},\n");
301 print_server("0,\n");
302 print_server("0,\n");
303 if (expr_eval_routines)
304 print_server("ExprEvalRoutines,\n");
305 else
306 print_server("0,\n");
307 print_server("0,\n");
308 print_server("__MIDL_TypeFormatString.Format,\n");
309 print_server("1, /* -error bounds_check flag */\n");
310 print_server("0x10001, /* Ndr library version */\n");
311 print_server("0,\n");
312 print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
313 print_server("0,\n");
314 print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
315 print_server("0, /* notify & notify_flag routine table */\n");
316 print_server("1, /* Flags */\n");
317 print_server("0, /* Reserved3 */\n");
318 print_server("0, /* Reserved4 */\n");
319 print_server("0 /* Reserved5 */\n");
320 indent--;
321 print_server("};\n");
322 fprintf(server, "\n");
326 static void write_serverinterfacedecl(type_t *iface)
328 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
329 UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
330 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
332 if (endpoints) write_endpoints( server, iface->name, endpoints );
334 print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
335 fprintf(server, "\n");
336 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
337 print_server("{\n");
338 indent++;
339 print_server("sizeof(RPC_SERVER_INTERFACE),\n");
340 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",
341 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
342 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
343 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
344 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
345 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
346 if (endpoints)
348 print_server("%u,\n", list_count(endpoints));
349 print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
351 else
353 print_server("0,\n");
354 print_server("0,\n");
356 print_server("0,\n");
357 print_server("0,\n");
358 print_server("0,\n");
359 indent--;
360 print_server("};\n");
361 if (old_names)
362 print_server("RPC_IF_HANDLE %s_ServerIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
363 iface->name, iface->name);
364 else
365 print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
366 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
367 fprintf(server, "\n");
371 static void init_server(void)
373 if (server)
374 return;
375 if (!(server = fopen(server_name, "w")))
376 error("Could not open %s for output\n", server_name);
378 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
379 print_server("#include <string.h>\n");
380 fprintf(server, "\n");
381 print_server("#include \"%s\"\n", header_name);
382 print_server("\n");
383 print_server( "#ifndef DECLSPEC_HIDDEN\n");
384 print_server( "#define DECLSPEC_HIDDEN\n");
385 print_server( "#endif\n");
386 print_server( "\n");
387 write_exceptions( server );
388 print_server("\n");
389 print_server("struct __server_frame\n");
390 print_server("{\n");
391 print_server(" __DECL_EXCEPTION_FRAME\n");
392 print_server(" MIDL_STUB_MESSAGE _StubMsg;\n");
393 print_server("};\n");
394 print_server("\n");
395 print_server("static int __server_filter( struct __server_frame *__frame )\n");
396 print_server( "{\n");
397 print_server( " return (__frame->code == STATUS_ACCESS_VIOLATION) ||\n");
398 print_server( " (__frame->code == STATUS_DATATYPE_MISALIGNMENT) ||\n");
399 print_server( " (__frame->code == RPC_X_BAD_STUB_DATA) ||\n");
400 print_server( " (__frame->code == RPC_S_INVALID_BOUND);\n");
401 print_server( "}\n");
402 print_server( "\n");
406 static void write_server_stmts(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
408 const statement_t *stmt;
409 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
411 if (stmt->type == STMT_LIBRARY)
412 write_server_stmts(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
413 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
415 type_t *iface = stmt->u.type;
416 if (!need_stub(iface))
417 continue;
419 fprintf(server, "/*****************************************************************************\n");
420 fprintf(server, " * %s interface\n", iface->name);
421 fprintf(server, " */\n");
422 fprintf(server, "\n");
424 if (statements_has_func(type_iface_get_stmts(iface)))
426 write_serverinterfacedecl(iface);
427 write_stubdescdecl(iface);
429 write_function_stubs(iface, proc_offset);
431 print_server("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
432 print_server("#error Invalid build platform for this stub.\n");
433 print_server("#endif\n");
435 fprintf(server, "\n");
436 write_stubdescriptor(iface, expr_eval_routines);
437 write_dispatchtable(iface);
443 static void write_server_routines(const statement_list_t *stmts)
445 unsigned int proc_offset = 0;
446 int expr_eval_routines;
448 write_formatstringsdecl(server, indent, stmts, need_stub);
449 expr_eval_routines = write_expr_eval_routines(server, server_token);
450 if (expr_eval_routines)
451 write_expr_eval_routine_list(server, server_token);
452 write_user_quad_list(server);
454 write_server_stmts(stmts, expr_eval_routines, &proc_offset);
456 fprintf(server, "\n");
458 write_procformatstring(server, stmts, need_stub);
459 write_typeformatstring(server, stmts, need_stub);
462 void write_server(const statement_list_t *stmts)
464 if (!do_server)
465 return;
466 if (do_everything && !need_stub_files(stmts))
467 return;
469 init_server();
470 if (!server)
471 return;
473 if (do_win32 && do_win64)
475 fprintf(server, "\n#ifndef _WIN64\n\n");
476 pointer_size = 4;
477 write_server_routines( stmts );
478 fprintf(server, "\n#else /* _WIN64 */\n\n");
479 pointer_size = 8;
480 write_server_routines( stmts );
481 fprintf(server, "\n#endif /* _WIN64 */\n");
483 else if (do_win32)
485 pointer_size = 4;
486 write_server_routines( stmts );
488 else if (do_win64)
490 pointer_size = 8;
491 write_server_routines( stmts );
494 fclose(server);