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