user32: Added tests for CharUpper and CharLower.
[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 declare_args(const func_t *func)
92 {
93     int in_attr, out_attr;
94     int i = 0;
95     var_t *var;
96
97     if (!func->args)
98         return;
99
100     var = func->args;
101     while (NEXT_LINK(var)) var = NEXT_LINK(var);
102     while (var)
103     {
104         const expr_t *size_is = get_attrp(var->attrs, ATTR_SIZEIS);
105         int has_size = size_is && (size_is->type != EXPR_VOID);
106         int is_string = is_attr(var->attrs, ATTR_STRING);
107
108         in_attr = is_attr(var->attrs, ATTR_IN);
109         out_attr = is_attr(var->attrs, ATTR_OUT);
110         if (!out_attr && !in_attr)
111             in_attr = 1;
112
113         if (!in_attr && !has_size && !is_string)
114         {
115             int indirection;
116             print_server("");
117             write_type(server, var->type, NULL, var->tname);
118             for (indirection = 0; indirection < var->ptr_level - 1; indirection++)
119                 fprintf(server, "*");
120             fprintf(server, " _W%u;\n", i++);
121         }
122
123         print_server("");
124         write_type(server, var->type, var, var->tname);
125         fprintf(server, " ");
126         write_name(server, var);
127         write_array(server, var->array, 0);
128         fprintf(server, ";\n");
129
130         var = PREV_LINK(var);
131     }
132 }
133
134
135 static void assign_out_args(const func_t *func)
136 {
137     int in_attr, out_attr;
138     int i = 0, sep = 0;
139     var_t *var;
140     const expr_t *size_is;
141     int has_size;
142
143     if (!func->args)
144         return;
145
146     var = func->args;
147     while (NEXT_LINK(var)) var = NEXT_LINK(var);
148     while (var)
149     {
150         int is_string = is_attr(var->attrs, ATTR_STRING);
151         size_is = get_attrp(var->attrs, ATTR_SIZEIS);
152         has_size = size_is && (size_is->type != EXPR_VOID);
153         in_attr = is_attr(var->attrs, ATTR_IN);
154         out_attr = is_attr(var->attrs, ATTR_OUT);
155         if (!out_attr && !in_attr)
156             in_attr = 1;
157
158         if (!in_attr)
159         {
160             print_server("");
161             write_name(server, var);
162
163             if (has_size)
164             {
165                 type_t *type = var->type;
166                 while (type->type == 0 && type->ref)
167                     type = type->ref;
168
169                 fprintf(server, " = NdrAllocate(&_StubMsg, ");
170                 write_expr(server, size_is, 1);
171                 fprintf(server, " * %u);\n", get_type_memsize(type));
172             }
173             else if (!is_string)
174             {
175                 fprintf(server, " = &_W%u;\n", i);
176                 if (var->ptr_level > 1)
177                     print_server("_W%u = 0;\n", i);
178                 i++;
179             }
180
181             sep = 1;
182         }
183
184         var = PREV_LINK(var);
185     }
186
187     if (sep)
188         fprintf(server, "\n");
189 }
190
191
192 static void write_function_stubs(type_t *iface)
193 {
194     char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
195     int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
196     const func_t *func = iface->funcs;
197     const var_t *var;
198     const var_t* explicit_handle_var;
199     unsigned int proc_offset = 0;
200     unsigned int type_offset = 2;
201
202     while (NEXT_LINK(func)) func = NEXT_LINK(func);
203     while (func)
204     {
205         const var_t *def = func->def;
206         unsigned long buffer_size = 0;
207         unsigned int type_offset_func;
208
209         /* check for a defined binding handle */
210         explicit_handle_var = get_explicit_handle_var(func);
211         if (explicit_handle)
212         {
213             if (!explicit_handle_var)
214             {
215                 error("%s() does not define an explicit binding handle!\n", def->name);
216                 return;
217             }
218         }
219         else if (implicit_handle)
220         {
221             if (explicit_handle_var)
222             {
223                 error("%s() must not define a binding handle!\n", def->name);
224                 return;
225             }
226         }
227
228         fprintf(server, "void __RPC_STUB\n");
229         fprintf(server, "%s_", iface->name);
230         write_name(server, def);
231         fprintf(server, "(\n");
232         indent++;
233         print_server("PRPC_MESSAGE _pRpcMessage)\n");
234         indent--;
235
236         /* write the functions body */
237         fprintf(server, "{\n");
238         indent++;
239
240         /* declare return value '_RetVal' */
241         if (!is_void(def->type, NULL))
242         {
243             print_server("");
244             write_type(server, def->type, def, def->tname);
245             fprintf(server, " _RetVal;\n");
246         }
247
248         /* Declare arguments */
249         declare_args(func);
250
251         print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
252         print_server("RPC_STATUS _Status;\n");
253         fprintf(server, "\n");
254
255
256         print_server("((void)(_Status));\n");
257         print_server("NdrServerInitializeNew(\n");
258         indent++;
259         print_server("_pRpcMessage,\n");
260         print_server("&_StubMsg,\n");
261         print_server("&%s_StubDesc);\n", iface->name);
262         indent--;
263         fprintf(server, "\n");
264
265         write_parameters_init(func);
266
267         if (explicit_handle_var)
268         {
269             print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
270             fprintf(server, "\n");
271         }
272
273         print_server("RpcTryFinally\n");
274         print_server("{\n");
275         indent++;
276         print_server("RpcTryExcept\n");
277         print_server("{\n");
278         indent++;
279
280         if (func->args)
281         {
282             print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
283             indent++;
284             print_server("NdrConvert(\n");
285             indent++;
286             print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
287             print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
288             indent -= 2;
289             fprintf(server, "\n");
290
291             /* make a copy so we don't increment the type offset twice */
292             type_offset_func = type_offset;
293
294             /* unmarshall arguments */
295             write_remoting_arguments(server, indent, func, &type_offset_func, PASS_IN, PHASE_UNMARSHAL);
296         }
297
298         print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
299         print_server("{\n");
300         indent++;
301         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
302         indent--;
303         print_server("}\n");
304         indent--;
305         print_server("}\n");
306         print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
307         print_server("{\n");
308         indent++;
309         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
310         indent--;
311         print_server("}\n");
312         print_server("RpcEndExcept\n");
313         fprintf(server, "\n");
314
315         /* Assign 'out' arguments */
316         assign_out_args(func);
317
318         /* Call the real server function */
319         if (!is_void(def->type, NULL))
320             print_server("_RetVal = ");
321         else
322             print_server("");
323         write_name(server, def);
324
325         if (func->args)
326         {
327             int first_arg = 1;
328
329             fprintf(server, "(\n");
330             indent++;
331             var = func->args;
332             while (NEXT_LINK(var)) var = NEXT_LINK(var);
333             while (var)
334             {
335                 if (first_arg)
336                     first_arg = 0;
337                 else
338                     fprintf(server, ",\n");
339                 print_server("");
340                 write_name(server, var);
341                 var = PREV_LINK(var);
342             }
343             fprintf(server, ");\n");
344             indent--;
345         }
346         else
347         {
348             fprintf(server, "();\n");
349         }
350
351         if (func->args)
352         {
353             const var_t *var = func->args;
354             while (NEXT_LINK(var)) var = NEXT_LINK(var);
355             while (var)
356             {
357                 if (is_attr(var->attrs, ATTR_OUT))
358                 {
359                     unsigned int alignment;
360                     buffer_size += get_required_buffer_size(var, &alignment, PASS_OUT);
361                     buffer_size += alignment;
362                 }
363
364                 var = PREV_LINK(var);
365             }
366         }
367
368         if (!is_void(def->type, NULL))
369         {
370             unsigned int alignment;
371             buffer_size += get_required_buffer_size(def, &alignment, PASS_RETURN);
372             buffer_size += alignment;
373         }
374
375         if (has_out_arg_or_return(func))
376         {
377             fprintf(server, "\n");
378             print_server("_StubMsg.BufferLength = %u;\n", buffer_size);
379
380             type_offset_func = type_offset;
381             write_remoting_arguments(server, indent, func, &type_offset_func, PASS_OUT, PHASE_BUFFERSIZE);
382
383             print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
384             fprintf(server, "\n");
385             print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
386             print_server("if (_Status)\n");
387             indent++;
388             print_server("RpcRaiseException(_Status);\n");
389             indent--;
390             fprintf(server, "\n");
391             print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
392             fprintf(server, "\n");
393         }
394
395         type_offset_func = type_offset;
396
397         /* marshall arguments */
398         write_remoting_arguments(server, indent, func, &type_offset, PASS_OUT, PHASE_MARSHAL);
399
400         /* marshall the return value */
401         if (!is_void(def->type, NULL))
402             print_phase_basetype(server, indent, PHASE_MARSHAL, PASS_RETURN, def, "_RetVal");
403
404         indent--;
405         print_server("}\n");
406         print_server("RpcFinally\n");
407         print_server("{\n");
408         indent++;
409
410         write_remoting_arguments(server, indent, func, &type_offset_func, PASS_OUT, PHASE_FREE);
411
412         indent--;
413         print_server("}\n");
414         print_server("RpcEndFinally\n");
415
416         /* calculate buffer length */
417         fprintf(server, "\n");
418         print_server("_pRpcMessage->BufferLength =\n");
419         indent++;
420         print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
421         indent--;
422         indent--;
423         fprintf(server, "}\n");
424         fprintf(server, "\n");
425
426         /* update proc_offset */
427         if (func->args)
428         {
429             var = func->args;
430             while (NEXT_LINK(var)) var = NEXT_LINK(var);
431             while (var)
432             {
433                 proc_offset += get_size_procformatstring_var(var);
434                 var = PREV_LINK(var);
435             }
436         }
437         if (!is_void(def->type, NULL))
438             proc_offset += get_size_procformatstring_var(def);
439         else
440             proc_offset += 2; /* FC_END and FC_PAD */
441
442         func = PREV_LINK(func);
443     }
444 }
445
446
447 static void write_dispatchtable(type_t *iface)
448 {
449     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
450     unsigned long method_count = 0;
451     func_t *func = iface->funcs;
452
453     print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
454     print_server("{\n");
455     indent++;
456     while (NEXT_LINK(func)) func = NEXT_LINK(func);
457     while (func)
458     {
459         var_t *def = func->def;
460
461         print_server("%s_", iface->name);
462         write_name(server, def);
463         fprintf(server, ",\n");
464
465         method_count++;
466         func = PREV_LINK(func);
467     }
468     print_server("0\n");
469     indent--;
470     print_server("};\n");
471     print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
472     print_server("{\n");
473     indent++;
474     print_server("%u,\n", method_count);
475     print_server("%s_table\n", iface->name);
476     indent--;
477     print_server("};\n");
478     fprintf(server, "\n");
479 }
480
481
482 static void write_stubdescdecl(type_t *iface)
483 {
484     print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
485     fprintf(server, "\n");
486 }
487
488
489 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
490 {
491     print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
492     print_server("{\n");
493     indent++;
494     print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
495     print_server("MIDL_user_allocate,\n");
496     print_server("MIDL_user_free,\n");
497     print_server("{\n");
498     indent++;
499     print_server("0,\n");
500     indent--;
501     print_server("},\n");
502     print_server("0,\n");
503     print_server("0,\n");
504     if (expr_eval_routines)
505         print_server("ExprEvalRoutines,\n");
506     else
507         print_server("0,\n");
508     print_server("0,\n");
509     print_server("__MIDL_TypeFormatString.Format,\n");
510     print_server("1, /* -error bounds_check flag */\n");
511     print_server("0x10001, /* Ndr library version */\n");
512     print_server("0,\n");
513     print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
514     print_server("0,\n");
515     print_server("0,\n");
516     print_server("0,  /* notify & notify_flag routine table */\n");
517     print_server("1,  /* Flags */\n");
518     print_server("0,  /* Reserved3 */\n");
519     print_server("0,  /* Reserved4 */\n");
520     print_server("0   /* Reserved5 */\n");
521     indent--;
522     print_server("};\n");
523     fprintf(server, "\n");
524 }
525
526
527 static void write_serverinterfacedecl(type_t *iface)
528 {
529     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
530     UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
531
532     print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
533     fprintf(server, "\n");
534     print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
535     print_server("{\n");
536     indent++;
537     print_server("sizeof(RPC_SERVER_INTERFACE),\n");
538     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",
539                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
540                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
541                  uuid->Data4[7], LOWORD(ver), HIWORD(ver));
542     print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
543     print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
544     print_server("0,\n");
545     print_server("0,\n");
546     print_server("0,\n");
547     print_server("0,\n");
548     print_server("0,\n");
549     indent--;
550     print_server("};\n");
551     if (old_names)
552         print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
553                      iface->name, iface->name);
554     else
555         print_server("RPC_IF_HANDLE %s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
556                      iface->name, LOWORD(ver), HIWORD(ver), iface->name);
557     fprintf(server, "\n");
558 }
559
560 static void write_formatdesc( const char *str )
561 {
562     print_server("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
563     print_server("{\n");
564     indent++;
565     print_server("short Pad;\n");
566     print_server("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
567     indent--;
568     print_server("} MIDL_%s_FORMAT_STRING;\n", str);
569     print_server("\n");
570 }
571
572
573 static void write_formatstringsdecl(type_t *iface)
574 {
575     print_server("#define TYPE_FORMAT_STRING_SIZE %d\n",
576                  get_size_typeformatstring(iface));
577
578     print_server("#define PROC_FORMAT_STRING_SIZE %d\n",
579                  get_size_procformatstring(iface));
580
581     fprintf(server, "\n");
582     write_formatdesc("TYPE");
583     write_formatdesc("PROC");
584     fprintf(server, "\n");
585     print_server("static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
586     print_server("static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
587     print_server("\n");
588 }
589
590
591 static void init_server(void)
592 {
593     if (server)
594         return;
595     if (!(server = fopen(server_name, "w")))
596         error("Could not open %s for output\n", server_name);
597
598     print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
599     print_server("#include <string.h>\n");
600     fprintf(server, "\n");
601     print_server("#include \"%s\"\n", header_name);
602     fprintf(server, "\n");
603 }
604
605
606 void write_server(ifref_t *ifaces)
607 {
608     ifref_t *iface = ifaces;
609
610     if (!do_server)
611         return;
612     if (!ifaces)
613         return;
614     END_OF_LIST(iface);
615
616     init_server();
617     if (!server)
618         return;
619
620     for (; iface; iface = PREV_LINK(iface))
621     {
622         if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
623             continue;
624
625         fprintf(server, "/*****************************************************************************\n");
626         fprintf(server, " * %s interface\n", iface->iface->name);
627         fprintf(server, " */\n");
628         fprintf(server, "\n");
629
630         if (iface->iface->funcs)
631         {
632             int expr_eval_routines;
633
634             write_formatstringsdecl(iface->iface);
635             write_serverinterfacedecl(iface->iface);
636             write_stubdescdecl(iface->iface);
637     
638             write_function_stubs(iface->iface);
639     
640             print_server("#if !defined(__RPC_WIN32__)\n");
641             print_server("#error  Invalid build platform for this stub.\n");
642             print_server("#endif\n");
643             fprintf(server, "\n");
644
645             write_procformatstring(server, iface->iface);
646             write_typeformatstring(server, iface->iface);
647
648             fprintf(server, "\n");
649
650             expr_eval_routines = write_expr_eval_routines(server, iface->iface->name);
651             if (expr_eval_routines)
652                 write_expr_eval_routine_list(server, iface->iface->name);
653
654             write_stubdescriptor(iface->iface, expr_eval_routines);
655             write_dispatchtable(iface->iface);
656         }
657     }
658
659     fclose(server);
660 }