widl: Declare fixed-size array args as pointers to arrays.
[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 <assert.h>
31 #include <ctype.h>
32 #include <signal.h>
33
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
38 #include "windef.h"
39
40 #include "widl.h"
41 #include "typelib.h"
42 #include "typelib_struct.h"
43 #include "typegen.h"
44
45 static FILE* server;
46 static int indent = 0;
47
48
49 static int print_server(const char *format, ...)
50 {
51     va_list va;
52     int i, r;
53
54     va_start(va, format);
55     if (format[0] != '\n')
56         for (i = 0; i < indent; i++)
57             fprintf(server, "    ");
58     r = vfprintf(server, format, va);
59     va_end(va);
60     return r;
61 }
62
63
64 static void write_parameters_init(const func_t *func)
65 {
66     const var_t *var;
67
68     if (!func->args)
69         return;
70
71     LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
72         if (var->type->type != RPC_FC_BIND_PRIMITIVE)
73             print_server("%s = 0;\n", var->name);
74
75     fprintf(server, "\n");
76 }
77
78
79 static void write_function_stubs(type_t *iface, unsigned int *proc_offset, unsigned int *type_offset)
80 {
81     char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
82     int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
83     const func_t *func;
84     const var_t *var;
85     const var_t* explicit_handle_var;
86
87     if (!iface->funcs) return;
88     LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
89     {
90         const var_t *def = func->def;
91         unsigned int type_offset_func;
92
93         /* check for a defined binding handle */
94         explicit_handle_var = get_explicit_handle_var(func);
95         if (explicit_handle)
96         {
97             if (!explicit_handle_var)
98             {
99                 error("%s() does not define an explicit binding handle!\n", def->name);
100                 return;
101             }
102         }
103         else if (implicit_handle)
104         {
105             if (explicit_handle_var)
106             {
107                 error("%s() must not define a binding handle!\n", def->name);
108                 return;
109             }
110         }
111
112         fprintf(server, "void __RPC_STUB\n");
113         fprintf(server, "%s_", iface->name);
114         write_name(server, def);
115         fprintf(server, "(\n");
116         indent++;
117         print_server("PRPC_MESSAGE _pRpcMessage)\n");
118         indent--;
119
120         /* write the functions body */
121         fprintf(server, "{\n");
122         indent++;
123
124         /* Declare arguments */
125         declare_stub_args(server, indent, func);
126
127         print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
128         print_server("RPC_STATUS _Status;\n");
129         fprintf(server, "\n");
130
131
132         print_server("((void)(_Status));\n");
133         print_server("NdrServerInitializeNew(\n");
134         indent++;
135         print_server("_pRpcMessage,\n");
136         print_server("&_StubMsg,\n");
137         print_server("&%s_StubDesc);\n", iface->name);
138         indent--;
139         fprintf(server, "\n");
140
141         write_parameters_init(func);
142
143         if (explicit_handle_var)
144         {
145             print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
146             fprintf(server, "\n");
147         }
148
149         print_server("RpcTryFinally\n");
150         print_server("{\n");
151         indent++;
152         print_server("RpcTryExcept\n");
153         print_server("{\n");
154         indent++;
155
156         if (func->args)
157         {
158             print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
159             indent++;
160             print_server("NdrConvert(\n");
161             indent++;
162             print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
163             print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
164             indent -= 2;
165             fprintf(server, "\n");
166
167             /* make a copy so we don't increment the type offset twice */
168             type_offset_func = *type_offset;
169
170             /* unmarshall arguments */
171             write_remoting_arguments(server, indent, func, &type_offset_func, PASS_IN, PHASE_UNMARSHAL);
172         }
173
174         print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
175         print_server("{\n");
176         indent++;
177         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
178         indent--;
179         print_server("}\n");
180         indent--;
181         print_server("}\n");
182         print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
183         print_server("{\n");
184         indent++;
185         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
186         indent--;
187         print_server("}\n");
188         print_server("RpcEndExcept\n");
189         fprintf(server, "\n");
190
191         /* Assign 'out' arguments */
192         assign_stub_out_args(server, indent, func);
193
194         /* Call the real server function */
195         if (!is_void(def->type, NULL))
196             print_server("_RetVal = ");
197         else
198             print_server("");
199         write_prefix_name(server, prefix_server, def);
200
201         if (func->args)
202         {
203             int first_arg = 1;
204
205             fprintf(server, "(\n");
206             indent++;
207             LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
208             {
209                 if (first_arg)
210                     first_arg = 0;
211                 else
212                     fprintf(server, ",\n");
213                 print_server("");
214                 if (var->array)
215                     fprintf(server, "*");
216                 write_name(server, var);
217             }
218             fprintf(server, ");\n");
219             indent--;
220         }
221         else
222         {
223             fprintf(server, "();\n");
224         }
225
226         if (has_out_arg_or_return(func))
227         {
228             type_offset_func = *type_offset;
229             write_remoting_arguments(server, indent, func, &type_offset_func, PASS_OUT, PHASE_BUFFERSIZE);
230
231             print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
232             fprintf(server, "\n");
233             print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
234             print_server("if (_Status)\n");
235             indent++;
236             print_server("RpcRaiseException(_Status);\n");
237             indent--;
238             fprintf(server, "\n");
239             print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
240             fprintf(server, "\n");
241         }
242
243         type_offset_func = *type_offset;
244
245         /* marshall arguments */
246         write_remoting_arguments(server, indent, func, type_offset, PASS_OUT, PHASE_MARSHAL);
247
248         /* marshall the return value */
249         if (!is_void(def->type, NULL))
250             print_phase_basetype(server, indent, PHASE_MARSHAL, PASS_RETURN, def, "_RetVal");
251
252         indent--;
253         print_server("}\n");
254         print_server("RpcFinally\n");
255         print_server("{\n");
256         indent++;
257
258         write_remoting_arguments(server, indent, func, &type_offset_func, PASS_OUT, PHASE_FREE);
259
260         indent--;
261         print_server("}\n");
262         print_server("RpcEndFinally\n");
263
264         /* calculate buffer length */
265         fprintf(server, "\n");
266         print_server("_pRpcMessage->BufferLength =\n");
267         indent++;
268         print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
269         indent--;
270         indent--;
271         fprintf(server, "}\n");
272         fprintf(server, "\n");
273
274         /* update proc_offset */
275         *proc_offset += get_size_procformatstring_func( func );
276     }
277 }
278
279
280 static void write_dispatchtable(type_t *iface)
281 {
282     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
283     unsigned long method_count = 0;
284     const func_t *func;
285
286     print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
287     print_server("{\n");
288     indent++;
289
290     if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
291     {
292         var_t *def = func->def;
293
294         print_server("%s_", iface->name);
295         write_name(server, def);
296         fprintf(server, ",\n");
297
298         method_count++;
299     }
300     print_server("0\n");
301     indent--;
302     print_server("};\n");
303     print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
304     print_server("{\n");
305     indent++;
306     print_server("%u,\n", method_count);
307     print_server("%s_table\n", iface->name);
308     indent--;
309     print_server("};\n");
310     fprintf(server, "\n");
311 }
312
313
314 static void write_stubdescdecl(type_t *iface)
315 {
316     print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
317     fprintf(server, "\n");
318 }
319
320
321 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
322 {
323     print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
324     print_server("{\n");
325     indent++;
326     print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
327     print_server("MIDL_user_allocate,\n");
328     print_server("MIDL_user_free,\n");
329     print_server("{\n");
330     indent++;
331     print_server("0,\n");
332     indent--;
333     print_server("},\n");
334     print_server("0,\n");
335     print_server("0,\n");
336     if (expr_eval_routines)
337         print_server("ExprEvalRoutines,\n");
338     else
339         print_server("0,\n");
340     print_server("0,\n");
341     print_server("__MIDL_TypeFormatString.Format,\n");
342     print_server("1, /* -error bounds_check flag */\n");
343     print_server("0x10001, /* Ndr library version */\n");
344     print_server("0,\n");
345     print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
346     print_server("0,\n");
347     print_server("0,\n");
348     print_server("0,  /* notify & notify_flag routine table */\n");
349     print_server("1,  /* Flags */\n");
350     print_server("0,  /* Reserved3 */\n");
351     print_server("0,  /* Reserved4 */\n");
352     print_server("0   /* Reserved5 */\n");
353     indent--;
354     print_server("};\n");
355     fprintf(server, "\n");
356 }
357
358
359 static void write_serverinterfacedecl(type_t *iface)
360 {
361     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
362     UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
363     const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
364
365     if (endpoints) write_endpoints( server, iface->name, endpoints );
366
367     print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
368     fprintf(server, "\n");
369     print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
370     print_server("{\n");
371     indent++;
372     print_server("sizeof(RPC_SERVER_INTERFACE),\n");
373     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",
374                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
375                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
376                  uuid->Data4[7], LOWORD(ver), HIWORD(ver));
377     print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
378     print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
379     if (endpoints)
380     {
381         print_server("%u,\n", list_count(endpoints));
382         print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
383     }
384     else
385     {
386         print_server("0,\n");
387         print_server("0,\n");
388     }
389     print_server("0,\n");
390     print_server("0,\n");
391     print_server("0,\n");
392     indent--;
393     print_server("};\n");
394     if (old_names)
395         print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
396                      iface->name, iface->name);
397     else
398         print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
399                      prefix_server, iface->name, LOWORD(ver), HIWORD(ver), iface->name);
400     fprintf(server, "\n");
401 }
402
403
404 static void init_server(void)
405 {
406     if (server)
407         return;
408     if (!(server = fopen(server_name, "w")))
409         error("Could not open %s for output\n", server_name);
410
411     print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
412     print_server("#include <string.h>\n");
413     fprintf(server, "\n");
414     print_server("#include \"%s\"\n", header_name);
415     fprintf(server, "\n");
416 }
417
418
419 void write_server(ifref_list_t *ifaces)
420 {
421     unsigned int proc_offset = 0;
422     unsigned int type_offset = 2;
423     ifref_t *iface;
424
425     if (!do_server)
426         return;
427     if (do_everything && !ifaces)
428         return;
429
430     init_server();
431     if (!server)
432         return;
433
434     write_formatstringsdecl(server, indent, ifaces, 0);
435
436     if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, ifref_t, entry )
437     {
438         if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
439             continue;
440
441         fprintf(server, "/*****************************************************************************\n");
442         fprintf(server, " * %s interface\n", iface->iface->name);
443         fprintf(server, " */\n");
444         fprintf(server, "\n");
445
446         if (iface->iface->funcs)
447         {
448             int expr_eval_routines;
449
450             write_serverinterfacedecl(iface->iface);
451             write_stubdescdecl(iface->iface);
452     
453             write_function_stubs(iface->iface, &proc_offset, &type_offset);
454     
455             print_server("#if !defined(__RPC_WIN32__)\n");
456             print_server("#error  Invalid build platform for this stub.\n");
457             print_server("#endif\n");
458
459             fprintf(server, "\n");
460
461             expr_eval_routines = write_expr_eval_routines(server, iface->iface->name);
462             if (expr_eval_routines)
463                 write_expr_eval_routine_list(server, iface->iface->name);
464
465             write_stubdescriptor(iface->iface, expr_eval_routines);
466             write_dispatchtable(iface->iface);
467         }
468     }
469
470     fprintf(server, "\n");
471
472     write_procformatstring(server, ifaces, 0);
473     write_typeformatstring(server, ifaces, 0);
474
475     fclose(server);
476 }