widl: Replace uses of get_func_return_type with type_function_get_rettype.
[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 statement_t *stmt;
54     const var_t *var;
55     const var_t* explicit_handle_var;
56
57     STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
58     {
59         const var_t *func = stmt->u.var;
60         int has_full_pointer = is_full_pointer_function(func);
61
62         /* check for a defined binding handle */
63         explicit_handle_var = get_explicit_handle_var(func);
64
65         print_server("struct __frame_%s_%s\n{\n", iface->name, get_name(func));
66         indent++;
67         print_server("__DECL_EXCEPTION_FRAME\n");
68         print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
69
70         /* Declare arguments */
71         declare_stub_args(server, indent, func);
72
73         indent--;
74         print_server("};\n\n");
75
76         print_server("static void __finally_%s_%s(", iface->name, get_name(func));
77         fprintf(server," struct __frame_%s_%s *__frame )\n{\n", iface->name, get_name(func));
78
79         indent++;
80         write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
81
82         if (has_full_pointer)
83             write_full_pointer_free(server, indent, func);
84
85         indent--;
86         print_server("}\n\n");
87
88         print_server("void __RPC_STUB %s_%s( PRPC_MESSAGE _pRpcMessage )\n", iface->name, get_name(func));
89
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");
96
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));
105
106         write_parameters_init(server, indent, func, "__frame->");
107
108         if (explicit_handle_var)
109         {
110             print_server("__frame->%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
111             fprintf(server, "\n");
112         }
113
114         print_server("RpcTryFinally\n");
115         print_server("{\n");
116         indent++;
117         print_server("RpcTryExcept\n");
118         print_server("{\n");
119         indent++;
120
121         if (has_full_pointer)
122             write_full_pointer_init(server, indent, func, TRUE);
123
124         if (type_get_function_args(func->type))
125         {
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");
132
133             /* unmarshall arguments */
134             write_remoting_arguments(server, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
135         }
136
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");
153
154         /* Assign 'out' arguments */
155         assign_stub_out_args(server, indent, func, "__frame->");
156
157         /* Call the real server function */
158         if (!is_void(type_function_get_rettype(func->type)))
159             print_server("__frame->_RetVal = ");
160         else
161             print_server("");
162         fprintf(server, "%s%s", 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", var->type->declarray ? "*" : "", 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         *proc_offset += get_size_procformatstring_func( func );
245     }
246 }
247
248
249 static void write_dispatchtable(type_t *iface)
250 {
251     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
252     unsigned long method_count = 0;
253     const statement_t *stmt;
254
255     print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
256     print_server("{\n");
257     indent++;
258
259     STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
260     {
261         var_t *func = stmt->u.var;
262         print_server("%s_%s,\n", iface->name, get_name(func));
263         method_count++;
264     }
265     print_server("0\n");
266     indent--;
267     print_server("};\n");
268     print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
269     print_server("{\n");
270     indent++;
271     print_server("%u,\n", method_count);
272     print_server("%s_table\n", iface->name);
273     indent--;
274     print_server("};\n");
275     fprintf(server, "\n");
276 }
277
278
279 static void write_stubdescdecl(type_t *iface)
280 {
281     print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
282     fprintf(server, "\n");
283 }
284
285
286 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
287 {
288     print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
289     print_server("{\n");
290     indent++;
291     print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
292     print_server("MIDL_user_allocate,\n");
293     print_server("MIDL_user_free,\n");
294     print_server("{\n");
295     indent++;
296     print_server("0,\n");
297     indent--;
298     print_server("},\n");
299     print_server("0,\n");
300     print_server("0,\n");
301     if (expr_eval_routines)
302         print_server("ExprEvalRoutines,\n");
303     else
304         print_server("0,\n");
305     print_server("0,\n");
306     print_server("__MIDL_TypeFormatString.Format,\n");
307     print_server("1, /* -error bounds_check flag */\n");
308     print_server("0x10001, /* Ndr library version */\n");
309     print_server("0,\n");
310     print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
311     print_server("0,\n");
312     print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
313     print_server("0,  /* notify & notify_flag routine table */\n");
314     print_server("1,  /* Flags */\n");
315     print_server("0,  /* Reserved3 */\n");
316     print_server("0,  /* Reserved4 */\n");
317     print_server("0   /* Reserved5 */\n");
318     indent--;
319     print_server("};\n");
320     fprintf(server, "\n");
321 }
322
323
324 static void write_serverinterfacedecl(type_t *iface)
325 {
326     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
327     UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
328     const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
329
330     if (endpoints) write_endpoints( server, iface->name, endpoints );
331
332     print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
333     fprintf(server, "\n");
334     print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
335     print_server("{\n");
336     indent++;
337     print_server("sizeof(RPC_SERVER_INTERFACE),\n");
338     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",
339                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
340                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
341                  uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
342     print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
343     print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
344     if (endpoints)
345     {
346         print_server("%u,\n", list_count(endpoints));
347         print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
348     }
349     else
350     {
351         print_server("0,\n");
352         print_server("0,\n");
353     }
354     print_server("0,\n");
355     print_server("0,\n");
356     print_server("0,\n");
357     indent--;
358     print_server("};\n");
359     if (old_names)
360         print_server("RPC_IF_HANDLE %s_ServerIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
361                      iface->name, iface->name);
362     else
363         print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
364                      prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
365     fprintf(server, "\n");
366 }
367
368
369 static void init_server(void)
370 {
371     if (server)
372         return;
373     if (!(server = fopen(server_name, "w")))
374         error("Could not open %s for output\n", server_name);
375
376     print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
377     print_server("#include <string.h>\n");
378     fprintf(server, "\n");
379     print_server("#include \"%s\"\n", header_name);
380     print_server("\n");
381     print_server( "#ifndef DECLSPEC_HIDDEN\n");
382     print_server( "#define DECLSPEC_HIDDEN\n");
383     print_server( "#endif\n");
384     print_server( "\n");
385     write_exceptions( server );
386     print_server("\n");
387     print_server("struct __server_frame\n");
388     print_server("{\n");
389     print_server("    __DECL_EXCEPTION_FRAME\n");
390     print_server("    MIDL_STUB_MESSAGE _StubMsg;\n");
391     print_server("};\n");
392     print_server("\n");
393     print_server("static int __server_filter( struct __server_frame *__frame )\n");
394     print_server( "{\n");
395     print_server( "    return RPC_BAD_STUB_DATA_EXCEPTION_FILTER;\n");
396     print_server( "}\n");
397     print_server( "\n");
398 }
399
400
401 static void write_server_stmts(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
402 {
403     const statement_t *stmt;
404     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
405     {
406         if (stmt->type == STMT_LIBRARY)
407             write_server_stmts(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
408         else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
409         {
410             type_t *iface = stmt->u.type;
411             if (!need_stub(iface))
412                 continue;
413
414             fprintf(server, "/*****************************************************************************\n");
415             fprintf(server, " * %s interface\n", iface->name);
416             fprintf(server, " */\n");
417             fprintf(server, "\n");
418
419             if (statements_has_func(type_iface_get_stmts(iface)))
420             {
421                 write_serverinterfacedecl(iface);
422                 write_stubdescdecl(iface);
423
424                 write_function_stubs(iface, proc_offset);
425
426                 print_server("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
427                 print_server("#error  Invalid build platform for this stub.\n");
428                 print_server("#endif\n");
429
430                 fprintf(server, "\n");
431                 write_stubdescriptor(iface, expr_eval_routines);
432                 write_dispatchtable(iface);
433             }
434         }
435     }
436 }
437
438 static void write_server_routines(const statement_list_t *stmts)
439 {
440     unsigned int proc_offset = 0;
441     int expr_eval_routines;
442
443     write_formatstringsdecl(server, indent, stmts, need_stub);
444     expr_eval_routines = write_expr_eval_routines(server, server_token);
445     if (expr_eval_routines)
446         write_expr_eval_routine_list(server, server_token);
447     write_user_quad_list(server);
448
449     write_server_stmts(stmts, expr_eval_routines, &proc_offset);
450
451     fprintf(server, "\n");
452
453     write_procformatstring(server, stmts, need_stub);
454     write_typeformatstring(server, stmts, need_stub);
455 }
456
457 void write_server(const statement_list_t *stmts)
458 {
459     if (!do_server)
460         return;
461     if (do_everything && !need_stub_files(stmts))
462         return;
463
464     init_server();
465     if (!server)
466         return;
467
468     if (do_win32 && do_win64)
469     {
470         fprintf(server, "\n#ifndef _WIN64\n\n");
471         pointer_size = 4;
472         write_server_routines( stmts );
473         fprintf(server, "\n#else /* _WIN64 */\n\n");
474         pointer_size = 8;
475         write_server_routines( stmts );
476         fprintf(server, "\n#endif /* _WIN64 */\n");
477     }
478     else if (do_win32)
479     {
480         pointer_size = 4;
481         write_server_routines( stmts );
482     }
483     else if (do_win64)
484     {
485         pointer_size = 8;
486         write_server_routines( stmts );
487     }
488
489     fclose(server);
490 }