comctl32: A couple fixes for tab icon offsets.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 #define END_OF_LIST(list)       \
46   do {                          \
47     if (list) {                 \
48       while (NEXT_LINK(list))   \
49         list = NEXT_LINK(list); \
50     }                           \
51   } while(0)
52
53 static FILE* server;
54 static int indent = 0;
55
56
57 static int print_server(const char *format, ...)
58 {
59     va_list va;
60     int i, r;
61
62     va_start(va, format);
63     for (i = 0; i < indent; i++)
64         fprintf(server, "    ");
65     r = vfprintf(server, format, va);
66     va_end(va);
67     return r;
68 }
69
70
71 static void write_parameters_init(const func_t *func)
72 {
73     const var_t *var;
74
75     if (!func->args)
76         return;
77
78     var = func->args;
79     while (NEXT_LINK(var)) var = NEXT_LINK(var);
80     while (var)
81     {
82         if (var->type->type != RPC_FC_BIND_PRIMITIVE)
83             print_server("%s = 0;\n", var->name);
84
85         var = PREV_LINK(var);
86     }
87     fprintf(server, "\n");
88 }
89
90
91 static void write_function_stubs(type_t *iface)
92 {
93     char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
94     int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
95     const func_t *func = iface->funcs;
96     const var_t *var;
97     const var_t* explicit_handle_var;
98     unsigned int proc_offset = 0;
99     unsigned int type_offset = 2;
100
101     while (NEXT_LINK(func)) func = NEXT_LINK(func);
102     while (func)
103     {
104         const var_t *def = func->def;
105         unsigned long buffer_size = 0;
106         unsigned int type_offset_func;
107
108         /* check for a defined binding handle */
109         explicit_handle_var = get_explicit_handle_var(func);
110         if (explicit_handle)
111         {
112             if (!explicit_handle_var)
113             {
114                 error("%s() does not define an explicit binding handle!\n", def->name);
115                 return;
116             }
117         }
118         else if (implicit_handle)
119         {
120             if (explicit_handle_var)
121             {
122                 error("%s() must not define a binding handle!\n", def->name);
123                 return;
124             }
125         }
126
127         fprintf(server, "void __RPC_STUB\n");
128         fprintf(server, "%s_", iface->name);
129         write_name(server, def);
130         fprintf(server, "(\n");
131         indent++;
132         print_server("PRPC_MESSAGE _pRpcMessage)\n");
133         indent--;
134
135         /* write the functions body */
136         fprintf(server, "{\n");
137         indent++;
138
139         /* declare return value '_RetVal' */
140         if (!is_void(def->type, NULL))
141         {
142             print_server("");
143             write_type(server, def->type, def, def->tname);
144             fprintf(server, " _RetVal;\n");
145         }
146
147         /* declare arguments */
148         if (func->args)
149         {
150             var = func->args;
151             while (NEXT_LINK(var)) var = NEXT_LINK(var);
152             while (var)
153             {
154                 print_server("");
155                 write_type(server, var->type, var, var->tname);
156                 fprintf(server, " ");
157                 write_name(server, var);
158                 write_array(server, var->array, 0);
159                 fprintf(server, ";\n");
160
161                 var = PREV_LINK(var);
162             }
163         }
164
165         print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
166         print_server("RPC_STATUS _Status;\n");
167         fprintf(server, "\n");
168
169
170         print_server("((void)(_Status));\n");
171         print_server("NdrServerInitializeNew(\n");
172         indent++;
173         print_server("_pRpcMessage,\n");
174         print_server("&_StubMsg,\n");
175         print_server("&%s_StubDesc);\n", iface->name);
176         indent--;
177         fprintf(server, "\n");
178
179         write_parameters_init(func);
180
181         if (explicit_handle_var)
182         {
183             print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
184             fprintf(server, "\n");
185         }
186
187         print_server("RpcTryFinally\n");
188         print_server("{\n");
189         indent++;
190         print_server("RpcTryExcept\n");
191         print_server("{\n");
192         indent++;
193
194         if (func->args)
195         {
196             print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
197             indent++;
198             print_server("NdrConvert(\n");
199             indent++;
200             print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
201             print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
202             indent -= 2;
203             fprintf(server, "\n");
204
205             /* make a copy so we don't increment the type offset twice */
206             type_offset_func = type_offset;
207
208             /* unmarshall arguments */
209             write_remoting_arguments(server, indent, func, &type_offset_func, PASS_IN, PHASE_UNMARSHAL);
210         }
211
212         print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
213         print_server("{\n");
214         indent++;
215         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
216         indent--;
217         print_server("}\n");
218         indent--;
219         print_server("}\n");
220         print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
221         print_server("{\n");
222         indent++;
223         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
224         indent--;
225         print_server("}\n");
226         print_server("RpcEndExcept\n");
227         fprintf(server, "\n");
228
229
230         /* Call the real server function */
231         if (!is_void(def->type, NULL))
232             print_server("_RetVal = ");
233         else
234             print_server("");
235         write_name(server, def);
236
237         if (func->args)
238         {
239             int first_arg = 1;
240
241             fprintf(server, "(\n");
242             indent++;
243             var = func->args;
244             while (NEXT_LINK(var)) var = NEXT_LINK(var);
245             while (var)
246             {
247                 if (first_arg)
248                     first_arg = 0;
249                 else
250                     fprintf(server, ",\n");
251                 print_server("");
252                 write_name(server, var);
253                 var = PREV_LINK(var);
254             }
255             fprintf(server, ");\n");
256             indent--;
257         }
258         else
259         {
260             fprintf(server, "();\n");
261         }
262
263         if (func->args)
264         {
265             const var_t *var = func->args;
266             while (NEXT_LINK(var)) var = NEXT_LINK(var);
267             while (var)
268             {
269                 if (is_attr(var->attrs, ATTR_OUT))
270                 {
271                     unsigned int alignment;
272                     buffer_size += get_required_buffer_size(var, &alignment);
273                     buffer_size += alignment;
274                 }
275         
276                 var = PREV_LINK(var);
277             }
278         }
279
280         if (!is_void(def->type, NULL))
281         {
282             unsigned int alignment;
283             buffer_size += get_required_buffer_size(def, &alignment);
284             buffer_size += alignment;
285         }
286
287         if (buffer_size)
288         {
289             fprintf(server, "\n");
290             print_server("_StubMsg.BufferLength = %uU;\n", buffer_size);
291             print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
292             fprintf(server, "\n");
293             print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
294             print_server("if (_Status)\n");
295             indent++;
296             print_server("RpcRaiseException(_Status);\n");
297             indent--;
298             fprintf(server, "\n");
299             print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
300             fprintf(server, "\n");
301         }
302
303         /* marshall arguments */
304         write_remoting_arguments(server, indent, func, &type_offset, PASS_OUT, PHASE_MARSHAL);
305
306         /* marshall the return value */
307         if (!is_void(def->type, NULL))
308         {
309             unsigned int alignment;
310             unsigned int size = get_required_buffer_size(def, &alignment);
311             print_server("_StubMsg.Buffer += (unsigned char *)(((long)_StubMsg.Buffer + %u) & ~0x%x);\n",
312                          size - 1, size - 1);
313             print_server("*(");
314             write_type(server, def->type, def, def->tname);
315             fprintf(server, " *)_StubMsg.Buffer = _RetVal;\n");
316             print_server("_StubMsg.Buffer += sizeof(");
317             write_type(server, def->type, def, def->tname);
318             fprintf(server, ");\n");
319         }
320
321         indent--;
322         print_server("}\n");
323         print_server("RpcFinally\n");
324         print_server("{\n");
325         print_server("}\n");
326         print_server("RpcEndFinally\n");
327
328         /* calculate buffer length */
329         fprintf(server, "\n");
330         print_server("_pRpcMessage->BufferLength =\n");
331         indent++;
332         print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
333         indent--;
334         indent--;
335         fprintf(server, "}\n");
336         fprintf(server, "\n");
337
338         /* update proc_offset */
339         if (func->args)
340         {
341             var = func->args;
342             while (NEXT_LINK(var)) var = NEXT_LINK(var);
343             while (var)
344             {
345                 proc_offset += get_size_procformatstring_var(var);
346                 var = PREV_LINK(var);
347             }
348         }
349         if (!is_void(def->type, NULL))
350             proc_offset += get_size_procformatstring_var(def);
351         else
352             proc_offset += 2; /* FC_END and FC_PAD */
353
354         func = PREV_LINK(func);
355     }
356 }
357
358
359 static void write_dispatchtable(type_t *iface)
360 {
361     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
362     unsigned long method_count = 0;
363     func_t *func = iface->funcs;
364
365     print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
366     print_server("{\n");
367     indent++;
368     while (NEXT_LINK(func)) func = NEXT_LINK(func);
369     while (func)
370     {
371         var_t *def = func->def;
372
373         print_server("%s_", iface->name);
374         write_name(server, def);
375         fprintf(server, ",\n");
376
377         method_count++;
378         func = PREV_LINK(func);
379     }
380     print_server("0\n");
381     indent--;
382     print_server("};\n");
383     print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
384     print_server("{\n");
385     indent++;
386     print_server("%u,\n", method_count);
387     print_server("%s_table\n", iface->name);
388     indent--;
389     print_server("};\n");
390     fprintf(server, "\n");
391 }
392
393
394 static void write_stubdescdecl(type_t *iface)
395 {
396     print_server("extern const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
397     fprintf(server, "\n");
398 }
399
400
401 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
402 {
403     print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
404     print_server("{\n");
405     indent++;
406     print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
407     print_server("MIDL_user_allocate,\n");
408     print_server("MIDL_user_free,\n");
409     print_server("0,\n");
410     print_server("0,\n");
411     print_server("0,\n");
412     if (expr_eval_routines)
413         print_server("ExprEvalRoutines,\n");
414     else
415         print_server("0,\n");
416     print_server("0,\n");
417     print_server("__MIDL_TypeFormatString.Format,\n");
418     print_server("1, /* -error bounds_check flag */\n");
419     print_server("0x10001, /* Ndr library version */\n");
420     print_server("0,\n");
421     print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
422     print_server("0,\n");
423     print_server("0,\n");
424     print_server("0,  /* notify & notify_flag routine table */\n");
425     print_server("1,  /* Flags */\n");
426     print_server("0,  /* Reserved3 */\n");
427     print_server("0,  /* Reserved4 */\n");
428     print_server("0   /* Reserved5 */\n");
429     indent--;
430     print_server("};\n");
431     fprintf(server, "\n");
432 }
433
434
435 static void write_serverinterfacedecl(type_t *iface)
436 {
437     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
438     UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
439
440     print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
441     fprintf(server, "\n");
442     print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
443     print_server("{\n");
444     indent++;
445     print_server("sizeof(RPC_SERVER_INTERFACE),\n");
446     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",
447                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
448                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
449                  uuid->Data4[7], LOWORD(ver), HIWORD(ver));
450     print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
451     print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
452     print_server("0,\n");
453     print_server("0,\n");
454     print_server("0,\n");
455     print_server("0,\n");
456     print_server("0,\n");
457     indent--;
458     print_server("};\n");
459     print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
460                  iface->name, LOWORD(ver), HIWORD(ver), iface->name);
461     fprintf(server, "\n");
462 }
463
464 static void write_formatdesc( const char *str )
465 {
466     print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
467     print_server("{\n");
468     indent++;
469     print_server("short Pad;\n");
470     print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
471     indent--;
472     print_server("} MIDL_%s_FORMAT_STRING;\n", str);
473     print_server("\n");
474 }
475
476
477 static void write_formatstringsdecl(type_t *iface)
478 {
479     print_server("#define TYPE_FORMAT_STRING_SIZE %d\n",
480                  get_size_typeformatstring(iface));
481
482     print_server("#define PROC_FORMAT_STRING_SIZE %d\n",
483                  get_size_procformatstring(iface));
484
485     fprintf(server, "\n");
486     write_formatdesc("TYPE");
487     write_formatdesc("PROC");
488     fprintf(server, "\n");
489     print_server("extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
490     print_server("extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
491     print_server("\n");
492 }
493
494
495 static void init_server(void)
496 {
497     if (server)
498         return;
499     if (!(server = fopen(server_name, "w")))
500         error("Could not open %s for output\n", server_name);
501
502     print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
503     print_server("#include <string.h>\n");
504     fprintf(server, "\n");
505     print_server("#include \"%s\"\n", header_name);
506     fprintf(server, "\n");
507 }
508
509
510 void write_server(ifref_t *ifaces)
511 {
512     ifref_t *iface = ifaces;
513
514     if (!do_server)
515         return;
516     if (!iface)
517         return;
518     END_OF_LIST(iface);
519
520     init_server();
521     if (!server)
522         return;
523
524     while (iface)
525     {
526         fprintf(server, "/*****************************************************************************\n");
527         fprintf(server, " * %s interface\n", iface->iface->name);
528         fprintf(server, " */\n");
529         fprintf(server, "\n");
530
531         if (iface->iface->funcs)
532         {
533             int expr_eval_routines;
534
535             write_formatstringsdecl(iface->iface);
536             write_serverinterfacedecl(iface->iface);
537             write_stubdescdecl(iface->iface);
538     
539             write_function_stubs(iface->iface);
540     
541             print_server("#if !defined(__RPC_WIN32__)\n");
542             print_server("#error  Invalid build platform for this stub.\n");
543             print_server("#endif\n");
544             fprintf(server, "\n");
545
546             write_procformatstring(server, iface->iface);
547             write_typeformatstring(server, iface->iface);
548
549             fprintf(server, "\n");
550
551             expr_eval_routines = write_expr_eval_routines(server, iface->iface->name);
552             if (expr_eval_routines)
553                 write_expr_eval_routine_list(server, iface->iface->name);
554
555             write_stubdescriptor(iface->iface, expr_eval_routines);
556             write_dispatchtable(iface->iface);
557         }
558
559         iface = PREV_LINK(iface);
560     }
561
562     fclose(server);
563 }