widl: Add support for finally function in proxy methods.
[wine] / tools / widl / server.c
1 /*
2  * IDL Compiler
3  *
4  * Copyright 2005-2006 Eric Kohl
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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>
31
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
36
37 #include "typegen.h"
38
39 static FILE* server;
40 static int indent = 0;
41
42
43 static void print_server(const char *format, ...)
44 {
45     va_list va;
46     va_start(va, format);
47     print(server, indent, format, va);
48     va_end(va);
49 }
50
51 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
52 {
53     const func_t *func;
54     const var_t *var;
55     const var_t* explicit_handle_var;
56
57     if (!iface->funcs) return;
58     LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
59     {
60         const var_t *def = func->def;
61         int has_full_pointer = is_full_pointer_function(func);
62
63         /* check for a defined binding handle */
64         explicit_handle_var = get_explicit_handle_var(func);
65
66         print_server("void __RPC_STUB %s_%s( PRPC_MESSAGE _pRpcMessage )\n", iface->name, get_name(def));
67
68         /* write the functions body */
69         fprintf(server, "{\n");
70         indent++;
71
72         /* Declare arguments */
73         declare_stub_args(server, indent, func);
74
75         print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
76         print_server("RPC_STATUS _Status;\n");
77         fprintf(server, "\n");
78
79
80         print_server("((void)(_Status));\n");
81         print_server("NdrServerInitializeNew(\n");
82         indent++;
83         print_server("_pRpcMessage,\n");
84         print_server("&_StubMsg,\n");
85         print_server("&%s_StubDesc);\n", iface->name);
86         indent--;
87         fprintf(server, "\n");
88
89         write_parameters_init(server, indent, func);
90
91         if (explicit_handle_var)
92         {
93             print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
94             fprintf(server, "\n");
95         }
96
97         print_server("RpcTryFinally\n");
98         print_server("{\n");
99         indent++;
100         print_server("RpcTryExcept\n");
101         print_server("{\n");
102         indent++;
103
104         if (has_full_pointer)
105             write_full_pointer_init(server, indent, func, TRUE);
106
107         if (func->args)
108         {
109             print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
110             indent++;
111             print_server("NdrConvert(\n");
112             indent++;
113             print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
114             print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
115             indent -= 2;
116             fprintf(server, "\n");
117
118             /* unmarshall arguments */
119             write_remoting_arguments(server, indent, func, PASS_IN, PHASE_UNMARSHAL);
120         }
121
122         print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
123         print_server("{\n");
124         indent++;
125         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
126         indent--;
127         print_server("}\n");
128         indent--;
129         print_server("}\n");
130         print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
131         print_server("{\n");
132         indent++;
133         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
134         indent--;
135         print_server("}\n");
136         print_server("RpcEndExcept\n");
137         fprintf(server, "\n");
138
139         /* Assign 'out' arguments */
140         assign_stub_out_args(server, indent, func);
141
142         /* Call the real server function */
143         if (!is_void(get_func_return_type(func)))
144             print_server("_RetVal = ");
145         else
146             print_server("");
147         fprintf(server, "%s%s", prefix_server, get_name(def));
148
149         if (func->args)
150         {
151             int first_arg = 1;
152
153             fprintf(server, "(\n");
154             indent++;
155             LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
156             {
157                 if (first_arg)
158                     first_arg = 0;
159                 else
160                     fprintf(server, ",\n");
161                 if (is_context_handle(var->type))
162                 {
163                     /* if the context_handle attribute appears in the chain of types
164                      * without pointers being followed, then the context handle must
165                      * be direct, otherwise it is a pointer */
166                     int is_ch_ptr = is_aliaschain_attr(var->type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
167                     print_server("(");
168                     write_type_decl_left(server, var->type);
169                     fprintf(server, ")%sNDRSContextValue(%s)", is_ch_ptr ? "" : "*", var->name);
170                 }
171                 else
172                 {
173                     print_server("%s%s", var->type->declarray ? "*" : "", get_name(var));
174                 }
175             }
176             fprintf(server, ");\n");
177             indent--;
178         }
179         else
180         {
181             fprintf(server, "();\n");
182         }
183
184         if (has_out_arg_or_return(func))
185         {
186             write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_BUFFERSIZE);
187
188             if (!is_void(get_func_return_type(func)))
189                 write_remoting_arguments(server, indent, func, PASS_RETURN, PHASE_BUFFERSIZE);
190
191             print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
192             fprintf(server, "\n");
193             print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
194             print_server("if (_Status)\n");
195             indent++;
196             print_server("RpcRaiseException(_Status);\n");
197             indent--;
198             fprintf(server, "\n");
199             print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
200             fprintf(server, "\n");
201         }
202
203         /* marshall arguments */
204         write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_MARSHAL);
205
206         /* marshall the return value */
207         if (!is_void(get_func_return_type(func)))
208             write_remoting_arguments(server, indent, func, PASS_RETURN, PHASE_MARSHAL);
209
210         indent--;
211         print_server("}\n");
212         print_server("RpcFinally\n");
213         print_server("{\n");
214         indent++;
215
216         write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_FREE);
217
218         if (has_full_pointer)
219             write_full_pointer_free(server, indent, func);
220
221         indent--;
222         print_server("}\n");
223         print_server("RpcEndFinally\n");
224
225         /* calculate buffer length */
226         fprintf(server, "\n");
227         print_server("_pRpcMessage->BufferLength =\n");
228         indent++;
229         print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
230         indent--;
231         indent--;
232         fprintf(server, "}\n");
233         fprintf(server, "\n");
234
235         /* update proc_offset */
236         *proc_offset += get_size_procformatstring_func( func );
237     }
238 }
239
240
241 static void write_dispatchtable(type_t *iface)
242 {
243     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
244     unsigned long method_count = 0;
245     const func_t *func;
246
247     print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
248     print_server("{\n");
249     indent++;
250
251     if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
252     {
253         var_t *def = func->def;
254         print_server("%s_%s,\n", iface->name, get_name(def));
255         method_count++;
256     }
257     print_server("0\n");
258     indent--;
259     print_server("};\n");
260     print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
261     print_server("{\n");
262     indent++;
263     print_server("%u,\n", method_count);
264     print_server("%s_table\n", iface->name);
265     indent--;
266     print_server("};\n");
267     fprintf(server, "\n");
268 }
269
270
271 static void write_stubdescdecl(type_t *iface)
272 {
273     print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
274     fprintf(server, "\n");
275 }
276
277
278 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
279 {
280     print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
281     print_server("{\n");
282     indent++;
283     print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
284     print_server("MIDL_user_allocate,\n");
285     print_server("MIDL_user_free,\n");
286     print_server("{\n");
287     indent++;
288     print_server("0,\n");
289     indent--;
290     print_server("},\n");
291     print_server("0,\n");
292     print_server("0,\n");
293     if (expr_eval_routines)
294         print_server("ExprEvalRoutines,\n");
295     else
296         print_server("0,\n");
297     print_server("0,\n");
298     print_server("__MIDL_TypeFormatString.Format,\n");
299     print_server("1, /* -error bounds_check flag */\n");
300     print_server("0x10001, /* Ndr library version */\n");
301     print_server("0,\n");
302     print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
303     print_server("0,\n");
304     print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
305     print_server("0,  /* notify & notify_flag routine table */\n");
306     print_server("1,  /* Flags */\n");
307     print_server("0,  /* Reserved3 */\n");
308     print_server("0,  /* Reserved4 */\n");
309     print_server("0   /* Reserved5 */\n");
310     indent--;
311     print_server("};\n");
312     fprintf(server, "\n");
313 }
314
315
316 static void write_serverinterfacedecl(type_t *iface)
317 {
318     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
319     UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
320     const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
321
322     if (endpoints) write_endpoints( server, iface->name, endpoints );
323
324     print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
325     fprintf(server, "\n");
326     print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
327     print_server("{\n");
328     indent++;
329     print_server("sizeof(RPC_SERVER_INTERFACE),\n");
330     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",
331                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
332                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
333                  uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
334     print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
335     print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
336     if (endpoints)
337     {
338         print_server("%u,\n", list_count(endpoints));
339         print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
340     }
341     else
342     {
343         print_server("0,\n");
344         print_server("0,\n");
345     }
346     print_server("0,\n");
347     print_server("0,\n");
348     print_server("0,\n");
349     indent--;
350     print_server("};\n");
351     if (old_names)
352         print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
353                      iface->name, iface->name);
354     else
355         print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
356                      prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
357     fprintf(server, "\n");
358 }
359
360
361 static void init_server(void)
362 {
363     if (server)
364         return;
365     if (!(server = fopen(server_name, "w")))
366         error("Could not open %s for output\n", server_name);
367
368     print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
369     print_server("#include <string.h>\n");
370     fprintf(server, "\n");
371     print_server("#include \"%s\"\n", header_name);
372     print_server("\n");
373     print_server("#ifndef USE_COMPILER_EXCEPTIONS\n");
374     print_server("\n");
375     print_server("#include \"wine/exception.h\"\n");
376     print_server("#undef RpcTryExcept\n");
377     print_server("#undef RpcExcept\n");
378     print_server("#undef RpcEndExcept\n");
379     print_server("#undef RpcExceptionCode\n");
380     print_server("\n");
381     print_server( "struct __server_frame\n");
382     print_server( "{\n");
383     print_server( "    EXCEPTION_REGISTRATION_RECORD frame;\n");
384     print_server( "    sigjmp_buf                    jmp;\n");
385     print_server( "};\n");
386     print_server( "\n");
387     print_server("static DWORD __server_exception_handler( EXCEPTION_RECORD *record,\n");
388     print_server("                                         EXCEPTION_REGISTRATION_RECORD *frame,\n");
389     print_server("                                         CONTEXT *context,\n");
390     print_server("                                         EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
391     print_server("{\n");
392     print_server("    struct __server_frame *server_frame = (struct __server_frame *)frame;\n");
393     print_server("\n");
394     print_server("    if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
395     print_server("        return ExceptionContinueSearch;\n");
396     print_server("\n");
397     print_server("    if (record->ExceptionCode != STATUS_ACCESS_VIOLATION &&\n");
398     print_server("        record->ExceptionCode != STATUS_DATATYPE_MISALIGNMENT &&\n");
399     print_server("        record->ExceptionCode != RPC_X_BAD_STUB_DATA &&\n");
400     print_server("        record->ExceptionCode != RPC_S_INVALID_BOUND)\n");
401     print_server("        return ExceptionContinueSearch;\n");
402     print_server("\n");
403     print_server("    __wine_rtl_unwind( frame, record );\n");
404     print_server("    __wine_pop_frame( frame );\n");
405     print_server("    siglongjmp( server_frame->jmp, 1 );\n");
406     print_server("}\n");
407     print_server("#define RpcTryExcept \\\n");
408     print_server("    do { \\\n");
409     print_server("        struct __server_frame __server_frame; \\\n");
410     print_server("        __server_frame.frame.Handler = __server_exception_handler; \\\n");
411     print_server("        if (!sigsetjmp( __server_frame.jmp, 0 )) \\\n");
412     print_server("        { \\\n");
413     print_server("            __wine_push_frame( &__server_frame.frame ); \\\n");
414     print_server("            {\n");
415     print_server("\n");
416     print_server("#define RpcExcept(expr) \\\n");
417     print_server("            } \\\n");
418     print_server("            __wine_pop_frame( &__server_frame.frame ); \\\n");
419     print_server("        } \\\n");
420     print_server("        else \\\n");
421     print_server("        {\n");
422     print_server("\n");
423     print_server("#define RpcEndExcept \\\n");
424     print_server("        } \\\n");
425     print_server("    } while(0);\n");
426     print_server("\n");
427     print_server("#endif /* USE_COMPILER_EXCEPTIONS */\n");
428     print_server("\n");
429 }
430
431
432 static void write_server_stmts(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
433 {
434     const statement_t *stmt;
435     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
436     {
437         if (stmt->type == STMT_LIBRARY)
438             write_server_stmts(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
439         else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
440         {
441             type_t *iface = stmt->u.type;
442             if (!need_stub(iface))
443                 continue;
444
445             fprintf(server, "/*****************************************************************************\n");
446             fprintf(server, " * %s interface\n", iface->name);
447             fprintf(server, " */\n");
448             fprintf(server, "\n");
449
450             if (iface->funcs)
451             {
452                 write_serverinterfacedecl(iface);
453                 write_stubdescdecl(iface);
454
455                 write_function_stubs(iface, proc_offset);
456
457                 print_server("#if !defined(__RPC_WIN32__)\n");
458                 print_server("#error  Invalid build platform for this stub.\n");
459                 print_server("#endif\n");
460
461                 fprintf(server, "\n");
462                 write_stubdescriptor(iface, expr_eval_routines);
463                 write_dispatchtable(iface);
464             }
465         }
466     }
467 }
468
469 void write_server(const statement_list_t *stmts)
470 {
471     unsigned int proc_offset = 0;
472     int expr_eval_routines;
473
474     if (!do_server)
475         return;
476     if (do_everything && !need_stub_files(stmts))
477         return;
478
479     init_server();
480     if (!server)
481         return;
482
483     write_formatstringsdecl(server, indent, stmts, need_stub);
484     expr_eval_routines = write_expr_eval_routines(server, server_token);
485     if (expr_eval_routines)
486         write_expr_eval_routine_list(server, server_token);
487     write_user_quad_list(server);
488
489     write_server_stmts(stmts, expr_eval_routines, &proc_offset);
490
491     fprintf(server, "\n");
492
493     write_procformatstring(server, stmts, need_stub);
494     write_typeformatstring(server, stmts, need_stub);
495
496     fclose(server);
497 }