widl: Add a more generic way of determining the type of handle for a function.
[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, ...) __attribute__((format (printf, 1, 2)));
44 static void print_server(const char *format, ...)
45 {
46     va_list va;
47     va_start(va, format);
48     print(server, indent, format, va);
49     va_end(va);
50 }
51
52 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
53 {
54     const statement_t *stmt;
55     const var_t *var;
56
57     STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
58     {
59         unsigned char explicit_fc, implicit_fc;
60         var_t *func = stmt->u.var;
61         int has_full_pointer = is_full_pointer_function(func);
62         const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
63
64         print_server("struct __frame_%s_%s\n{\n", iface->name, get_name(func));
65         indent++;
66         print_server("__DECL_EXCEPTION_FRAME\n");
67         print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
68
69         /* Declare arguments */
70         declare_stub_args(server, indent, func);
71
72         indent--;
73         print_server("};\n\n");
74
75         print_server("static void __finally_%s_%s(", iface->name, get_name(func));
76         fprintf(server," struct __frame_%s_%s *__frame )\n{\n", iface->name, get_name(func));
77
78         indent++;
79         write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
80
81         if (!is_void(type_function_get_rettype(func->type)))
82             write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_FREE);
83
84         if (has_full_pointer)
85             write_full_pointer_free(server, indent, func);
86
87         indent--;
88         print_server("}\n\n");
89
90         print_server("void __RPC_STUB %s_%s( PRPC_MESSAGE _pRpcMessage )\n", iface->name, get_name(func));
91
92         /* write the functions body */
93         fprintf(server, "{\n");
94         indent++;
95         print_server("struct __frame_%s_%s __f, * const __frame = &__f;\n", iface->name, get_name(func));
96         if (has_out_arg_or_return(func)) print_server("RPC_STATUS _Status;\n");
97         fprintf(server, "\n");
98
99         print_server("NdrServerInitializeNew(\n");
100         indent++;
101         print_server("_pRpcMessage,\n");
102         print_server("&__frame->_StubMsg,\n");
103         print_server("&%s_StubDesc);\n", iface->name);
104         indent--;
105         fprintf(server, "\n");
106         print_server( "RpcExceptionInit( __server_filter, __finally_%s_%s );\n", iface->name, get_name(func));
107
108         write_parameters_init(server, indent, func, "__frame->");
109
110         if (explicit_fc == RPC_FC_BIND_PRIMITIVE)
111         {
112             print_server("__frame->%s = _pRpcMessage->Handle;\n", handle_var->name);
113             fprintf(server, "\n");
114         }
115
116         print_server("RpcTryFinally\n");
117         print_server("{\n");
118         indent++;
119         print_server("RpcTryExcept\n");
120         print_server("{\n");
121         indent++;
122
123         if (has_full_pointer)
124             write_full_pointer_init(server, indent, func, TRUE);
125
126         if (type_get_function_args(func->type))
127         {
128             print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
129             indent++;
130             print_server("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
131                          *proc_offset);
132             indent--;
133             fprintf(server, "\n");
134
135             /* unmarshall arguments */
136             write_remoting_arguments(server, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
137         }
138
139         print_server("if (__frame->_StubMsg.Buffer > __frame->_StubMsg.BufferEnd)\n");
140         print_server("{\n");
141         indent++;
142         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
143         indent--;
144         print_server("}\n");
145         indent--;
146         print_server("}\n");
147         print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
148         print_server("{\n");
149         indent++;
150         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
151         indent--;
152         print_server("}\n");
153         print_server("RpcEndExcept\n");
154         fprintf(server, "\n");
155
156         /* Assign 'out' arguments */
157         assign_stub_out_args(server, indent, func, "__frame->");
158
159         /* Call the real server function */
160         print_server("%s%s%s",
161                      is_void(type_function_get_rettype(func->type)) ? "" : "__frame->_RetVal = ",
162                      prefix_server, get_name(func));
163
164         if (type_get_function_args(func->type))
165         {
166             int first_arg = 1;
167
168             fprintf(server, "(\n");
169             indent++;
170             LIST_FOR_EACH_ENTRY( var, type_get_function_args(func->type), const var_t, entry )
171             {
172                 if (first_arg)
173                     first_arg = 0;
174                 else
175                     fprintf(server, ",\n");
176                 if (is_context_handle(var->type))
177                 {
178                     /* if the context_handle attribute appears in the chain of types
179                      * without pointers being followed, then the context handle must
180                      * be direct, otherwise it is a pointer */
181                     int is_ch_ptr = is_aliaschain_attr(var->type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
182                     print_server("(");
183                     write_type_decl_left(server, var->type);
184                     fprintf(server, ")%sNDRSContextValue(__frame->%s)",
185                             is_ch_ptr ? "" : "*", var->name);
186                 }
187                 else
188                 {
189                     print_server("%s__frame->%s", is_array(var->type) && !type_array_is_decl_as_ptr(var->type) ? "*" : "", var->name);
190                 }
191             }
192             fprintf(server, ");\n");
193             indent--;
194         }
195         else
196         {
197             fprintf(server, "();\n");
198         }
199
200         if (has_out_arg_or_return(func))
201         {
202             write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
203
204             if (!is_void(type_function_get_rettype(func->type)))
205                 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
206
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");
217         }
218
219         /* marshall arguments */
220         write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
221
222         /* marshall the return value */
223         if (!is_void(type_function_get_rettype(func->type)))
224             write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
225
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");
235
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");
242
243         /* update proc_offset */
244         func->procstring_offset = *proc_offset;
245         *proc_offset += get_size_procformatstring_func( func );
246     }
247 }
248
249
250 static void write_dispatchtable(type_t *iface)
251 {
252     unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
253     unsigned int method_count = 0;
254     const statement_t *stmt;
255
256     print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
257     print_server("{\n");
258     indent++;
259
260     STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
261     {
262         var_t *func = stmt->u.var;
263         print_server("%s_%s,\n", iface->name, get_name(func));
264         method_count++;
265     }
266     print_server("0\n");
267     indent--;
268     print_server("};\n");
269     print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
270     print_server("{\n");
271     indent++;
272     print_server("%u,\n", method_count);
273     print_server("%s_table\n", iface->name);
274     indent--;
275     print_server("};\n");
276     fprintf(server, "\n");
277 }
278
279
280 static void write_routinetable(type_t *iface)
281 {
282     const statement_t *stmt;
283
284     print_server( "static const SERVER_ROUTINE %s_ServerRoutineTable[] =\n", iface->name );
285     print_server( "{\n" );
286     indent++;
287     STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
288     {
289         var_t *func = stmt->u.var;
290         if (is_local( func->attrs )) continue;
291         print_server( "(SERVER_ROUTINE)%s,\n", func->name );
292     }
293     indent--;
294     print_server( "};\n\n" );
295 }
296
297
298 static void write_serverinfo(type_t *iface)
299 {
300     print_server( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
301     print_server( "{\n" );
302     indent++;
303     print_server( "&%s_StubDesc,\n", iface->name );
304     print_server( "%s_ServerRoutineTable,\n", iface->name );
305     print_server( "__MIDL_ProcFormatString.Format,\n" );
306     print_server( "%s_FormatStringOffsetTable,\n", iface->name );
307     print_server( "0,\n" );
308     print_server( "0,\n" );
309     print_server( "0,\n" );
310     print_server( "0\n" );
311     indent--;
312     print_server( "};\n\n" );
313 }
314
315
316 static void write_stubdescdecl(type_t *iface)
317 {
318     print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
319     fprintf(server, "\n");
320 }
321
322
323 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
324 {
325     print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
326     print_server("{\n");
327     indent++;
328     print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
329     print_server("MIDL_user_allocate,\n");
330     print_server("MIDL_user_free,\n");
331     print_server("{\n");
332     indent++;
333     print_server("0,\n");
334     indent--;
335     print_server("},\n");
336     print_server("0,\n");
337     print_server("0,\n");
338     if (expr_eval_routines)
339         print_server("ExprEvalRoutines,\n");
340     else
341         print_server("0,\n");
342     print_server("0,\n");
343     print_server("__MIDL_TypeFormatString.Format,\n");
344     print_server("1, /* -error bounds_check flag */\n");
345     print_server("0x10001, /* Ndr library version */\n");
346     print_server("0,\n");
347     print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
348     print_server("0,\n");
349     print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
350     print_server("0,  /* notify & notify_flag routine table */\n");
351     print_server("1,  /* Flags */\n");
352     print_server("0,  /* Reserved3 */\n");
353     print_server("0,  /* Reserved4 */\n");
354     print_server("0   /* Reserved5 */\n");
355     indent--;
356     print_server("};\n");
357     fprintf(server, "\n");
358 }
359
360
361 static void write_serverinterfacedecl(type_t *iface)
362 {
363     unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
364     UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
365     const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
366
367     if (endpoints) write_endpoints( server, iface->name, endpoints );
368
369     print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
370     print_server( "static const MIDL_SERVER_INFO %s_ServerInfo;\n", iface->name );
371     fprintf(server, "\n");
372     print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
373     print_server("{\n");
374     indent++;
375     print_server("sizeof(RPC_SERVER_INTERFACE),\n");
376     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",
377                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
378                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
379                  uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
380     print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
381     print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
382     if (endpoints)
383     {
384         print_server("%u,\n", list_count(endpoints));
385         print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
386     }
387     else
388     {
389         print_server("0,\n");
390         print_server("0,\n");
391     }
392     print_server("0,\n");
393     print_server("&%s_ServerInfo,\n", iface->name);
394     print_server("0,\n");
395     indent--;
396     print_server("};\n");
397     if (old_names)
398         print_server("RPC_IF_HANDLE %s_ServerIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
399                      iface->name, iface->name);
400     else
401         print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
402                      prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
403     fprintf(server, "\n");
404 }
405
406
407 static void init_server(void)
408 {
409     if (server)
410         return;
411     if (!(server = fopen(server_name, "w")))
412         error("Could not open %s for output\n", server_name);
413
414     print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
415     print_server("#include <string.h>\n");
416     fprintf(server, "\n");
417     print_server("#include \"%s\"\n", header_name);
418     print_server("\n");
419     print_server( "#ifndef DECLSPEC_HIDDEN\n");
420     print_server( "#define DECLSPEC_HIDDEN\n");
421     print_server( "#endif\n");
422     print_server( "\n");
423 }
424
425
426 static void write_server_stmts(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
427 {
428     const statement_t *stmt;
429     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
430     {
431         if (stmt->type == STMT_LIBRARY)
432             write_server_stmts(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
433         else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
434         {
435             type_t *iface = stmt->u.type;
436             if (!need_stub(iface))
437                 continue;
438
439             fprintf(server, "/*****************************************************************************\n");
440             fprintf(server, " * %s interface\n", iface->name);
441             fprintf(server, " */\n");
442             fprintf(server, "\n");
443
444             if (statements_has_func(type_iface_get_stmts(iface)))
445             {
446                 write_serverinterfacedecl(iface);
447                 write_stubdescdecl(iface);
448
449                 write_function_stubs(iface, proc_offset);
450
451                 print_server("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
452                 print_server("#error  Invalid build platform for this stub.\n");
453                 print_server("#endif\n");
454
455                 fprintf(server, "\n");
456                 write_procformatstring_offsets( server, iface );
457                 write_stubdescriptor(iface, expr_eval_routines);
458                 write_dispatchtable(iface);
459                 write_routinetable(iface);
460                 write_serverinfo(iface);
461             }
462         }
463     }
464 }
465
466 static void write_server_routines(const statement_list_t *stmts)
467 {
468     unsigned int proc_offset = 0;
469     int expr_eval_routines;
470
471     write_exceptions( server );
472     print_server("\n");
473     print_server("struct __server_frame\n");
474     print_server("{\n");
475     print_server("    __DECL_EXCEPTION_FRAME\n");
476     print_server("    MIDL_STUB_MESSAGE _StubMsg;\n");
477     print_server("};\n");
478     print_server("\n");
479     print_server("static int __server_filter( struct __server_frame *__frame )\n");
480     print_server( "{\n");
481     print_server( "    return (__frame->code == STATUS_ACCESS_VIOLATION) ||\n");
482     print_server( "           (__frame->code == STATUS_DATATYPE_MISALIGNMENT) ||\n");
483     print_server( "           (__frame->code == RPC_X_BAD_STUB_DATA) ||\n");
484     print_server( "           (__frame->code == RPC_S_INVALID_BOUND);\n");
485     print_server( "}\n");
486     print_server( "\n");
487
488     write_formatstringsdecl(server, indent, stmts, need_stub);
489     expr_eval_routines = write_expr_eval_routines(server, server_token);
490     if (expr_eval_routines)
491         write_expr_eval_routine_list(server, server_token);
492     write_user_quad_list(server);
493
494     write_server_stmts(stmts, expr_eval_routines, &proc_offset);
495
496     write_procformatstring(server, stmts, need_stub);
497     write_typeformatstring(server, stmts, need_stub);
498 }
499
500 void write_server(const statement_list_t *stmts)
501 {
502     if (!do_server)
503         return;
504     if (do_everything && !need_stub_files(stmts))
505         return;
506
507     init_server();
508     if (!server)
509         return;
510
511     if (do_win32 && do_win64)
512     {
513         fprintf(server, "#ifndef _WIN64\n\n");
514         pointer_size = 4;
515         write_server_routines( stmts );
516         fprintf(server, "\n#else /* _WIN64 */\n\n");
517         pointer_size = 8;
518         write_server_routines( stmts );
519         fprintf(server, "\n#endif /* _WIN64 */\n");
520     }
521     else if (do_win32)
522     {
523         pointer_size = 4;
524         write_server_routines( stmts );
525     }
526     else if (do_win64)
527     {
528         pointer_size = 8;
529         write_server_routines( stmts );
530     }
531
532     fclose(server);
533 }