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