server.c 14 KB
Newer Older
1 2 3
/*
 * IDL Compiler
 *
4
 * Copyright 2005-2006 Eric Kohl
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 */

#include "config.h"
#include "wine/port.h"

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <signal.h>

#include "widl.h"
#include "utils.h"
#include "parser.h"
#include "header.h"
#include "windef.h"

#include "widl.h"
#include "typelib.h"
#include "typelib_struct.h"
43
#include "typegen.h"
44 45 46 47 48

static FILE* server;
static int indent = 0;


49
static void print_server(const char *format, ...)
50 51 52
{
    va_list va;
    va_start(va, format);
53
    print(server, indent, format, va);
54 55 56
    va_end(va);
}

57
static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
58
{
59 60
    char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
    int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
61
    const func_t *func;
62 63
    const var_t *var;
    const var_t* explicit_handle_var;
64

65 66
    if (!iface->funcs) return;
    LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
67
    {
68
        const var_t *def = func->def;
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        /* check for a defined binding handle */
        explicit_handle_var = get_explicit_handle_var(func);
        if (explicit_handle)
        {
            if (!explicit_handle_var)
            {
                error("%s() does not define an explicit binding handle!\n", def->name);
                return;
            }
        }
        else if (implicit_handle)
        {
            if (explicit_handle_var)
            {
                error("%s() must not define a binding handle!\n", def->name);
                return;
            }
        }

89
        fprintf(server, "void __RPC_STUB\n");
90 91 92 93 94 95 96 97 98 99 100
        fprintf(server, "%s_", iface->name);
        write_name(server, def);
        fprintf(server, "(\n");
        indent++;
        print_server("PRPC_MESSAGE _pRpcMessage)\n");
        indent--;

        /* write the functions body */
        fprintf(server, "{\n");
        indent++;

101
        /* Declare arguments */
102
        declare_stub_args(server, indent, func);
103

104 105 106
        print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
        print_server("RPC_STATUS _Status;\n");
        fprintf(server, "\n");
107 108


109 110 111 112 113 114 115 116 117
        print_server("((void)(_Status));\n");
        print_server("NdrServerInitializeNew(\n");
        indent++;
        print_server("_pRpcMessage,\n");
        print_server("&_StubMsg,\n");
        print_server("&%s_StubDesc);\n", iface->name);
        indent--;
        fprintf(server, "\n");

118
        write_parameters_init(server, indent, func);
119

120 121 122 123 124 125
        if (explicit_handle_var)
        {
            print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
            fprintf(server, "\n");
        }

126 127 128 129 130 131
        print_server("RpcTryFinally\n");
        print_server("{\n");
        indent++;
        print_server("RpcTryExcept\n");
        print_server("{\n");
        indent++;
132 133 134 135 136 137 138 139

        if (func->args)
        {
            print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
            indent++;
            print_server("NdrConvert(\n");
            indent++;
            print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
140
            print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
141 142 143
            indent -= 2;
            fprintf(server, "\n");

144
            /* unmarshall arguments */
145
            write_remoting_arguments(server, indent, func, PASS_IN, PHASE_UNMARSHAL);
146 147
        }

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
        print_server("{\n");
        indent++;
        print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
        indent--;
        print_server("}\n");
        indent--;
        print_server("}\n");
        print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
        print_server("{\n");
        indent++;
        print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
        indent--;
        print_server("}\n");
        print_server("RpcEndExcept\n");
        fprintf(server, "\n");

165
        /* Assign 'out' arguments */
166
        assign_stub_out_args(server, indent, func);
167 168

        /* Call the real server function */
169
        if (!is_void(def->type))
170 171 172
            print_server("_RetVal = ");
        else
            print_server("");
173
        write_prefix_name(server, prefix_server, def);
174

175 176 177 178 179 180
        if (func->args)
        {
            int first_arg = 1;

            fprintf(server, "(\n");
            indent++;
181
            LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
182 183 184 185 186
            {
                if (first_arg)
                    first_arg = 0;
                else
                    fprintf(server, ",\n");
187 188 189
                if (is_context_handle(var->type))
                {
                    print_server("(");
190
                    write_type_decl_left(server, var->type);
191 192 193 194 195 196 197 198 199
                    fprintf(server, ")%sNDRSContextValue(%s)", is_ptr(var->type) ? "" : "*", var->name);
                }
                else
                {
                    print_server("");
                    if (var->type->declarray)
                        fprintf(server, "*");
                    write_name(server, var);
                }
200 201 202 203 204 205 206 207
            }
            fprintf(server, ");\n");
            indent--;
        }
        else
        {
            fprintf(server, "();\n");
        }
208

209
        if (has_out_arg_or_return(func))
210
        {
211
            write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_BUFFERSIZE);
212

213 214 215 216 217 218 219 220
            print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
            fprintf(server, "\n");
            print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
            print_server("if (_Status)\n");
            indent++;
            print_server("RpcRaiseException(_Status);\n");
            indent--;
            fprintf(server, "\n");
221
            print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
222
            fprintf(server, "\n");
223
        }
224

225
        /* marshall arguments */
226
        write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_MARSHAL);
Robert Shearman's avatar
Robert Shearman committed
227

228
        /* marshall the return value */
229
        if (!is_void(def->type))
230
            print_phase_basetype(server, indent, PHASE_MARSHAL, PASS_RETURN, def, "_RetVal");
231 232 233 234 235

        indent--;
        print_server("}\n");
        print_server("RpcFinally\n");
        print_server("{\n");
236 237
        indent++;

238
        write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_FREE);
239 240

        indent--;
241 242 243 244 245 246 247
        print_server("}\n");
        print_server("RpcEndFinally\n");

        /* calculate buffer length */
        fprintf(server, "\n");
        print_server("_pRpcMessage->BufferLength =\n");
        indent++;
248
        print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
249 250 251 252 253
        indent--;
        indent--;
        fprintf(server, "}\n");
        fprintf(server, "\n");

254
        /* update proc_offset */
255
        *proc_offset += get_size_procformatstring_func( func );
256 257 258 259 260 261 262 263
    }
}


static void write_dispatchtable(type_t *iface)
{
    unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
    unsigned long method_count = 0;
264
    const func_t *func;
265 266 267 268

    print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
    print_server("{\n");
    indent++;
269 270

    if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
271
    {
Eric Kohl's avatar
Eric Kohl committed
272
        var_t *def = func->def;
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

        print_server("%s_", iface->name);
        write_name(server, def);
        fprintf(server, ",\n");

        method_count++;
    }
    print_server("0\n");
    indent--;
    print_server("};\n");
    print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, LOWORD(ver), HIWORD(ver));
    print_server("{\n");
    indent++;
    print_server("%u,\n", method_count);
    print_server("%s_table\n", iface->name);
    indent--;
    print_server("};\n");
    fprintf(server, "\n");
}


static void write_stubdescdecl(type_t *iface)
{
296
    print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
297 298 299 300
    fprintf(server, "\n");
}


301
static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
302 303 304 305
{
    print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
    print_server("{\n");
    indent++;
306
    print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
307 308
    print_server("MIDL_user_allocate,\n");
    print_server("MIDL_user_free,\n");
309 310
    print_server("{\n");
    indent++;
311
    print_server("0,\n");
312 313
    indent--;
    print_server("},\n");
314 315
    print_server("0,\n");
    print_server("0,\n");
316 317 318 319
    if (expr_eval_routines)
        print_server("ExprEvalRoutines,\n");
    else
        print_server("0,\n");
320 321 322 323 324 325 326
    print_server("0,\n");
    print_server("__MIDL_TypeFormatString.Format,\n");
    print_server("1, /* -error bounds_check flag */\n");
    print_server("0x10001, /* Ndr library version */\n");
    print_server("0,\n");
    print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
    print_server("0,\n");
327
    print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    print_server("0,  /* notify & notify_flag routine table */\n");
    print_server("1,  /* Flags */\n");
    print_server("0,  /* Reserved3 */\n");
    print_server("0,  /* Reserved4 */\n");
    print_server("0   /* Reserved5 */\n");
    indent--;
    print_server("};\n");
    fprintf(server, "\n");
}


static void write_serverinterfacedecl(type_t *iface)
{
    unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
    UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
343 344 345
    const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);

    if (endpoints) write_endpoints( server, iface->name, endpoints );
346 347 348 349 350 351 352 353 354 355 356 357 358

    print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, LOWORD(ver), HIWORD(ver));
    fprintf(server, "\n");
    print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
    print_server("{\n");
    indent++;
    print_server("sizeof(RPC_SERVER_INTERFACE),\n");
    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",
                 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
                 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
                 uuid->Data4[7], LOWORD(ver), HIWORD(ver));
    print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
    print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, LOWORD(ver), HIWORD(ver));
359 360 361 362 363 364 365 366 367 368
    if (endpoints)
    {
        print_server("%u,\n", list_count(endpoints));
        print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
    }
    else
    {
        print_server("0,\n");
        print_server("0,\n");
    }
369 370 371 372 373
    print_server("0,\n");
    print_server("0,\n");
    print_server("0,\n");
    indent--;
    print_server("};\n");
374 375 376 377
    if (old_names)
        print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
                     iface->name, iface->name);
    else
378 379
        print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
                     prefix_server, iface->name, LOWORD(ver), HIWORD(ver), iface->name);
380 381 382 383 384 385 386 387 388 389 390
    fprintf(server, "\n");
}


static void init_server(void)
{
    if (server)
        return;
    if (!(server = fopen(server_name, "w")))
        error("Could not open %s for output\n", server_name);

391
    print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
392
    print_server("#include <string.h>\n");
393
    fprintf(server, "\n");
394
    print_server("#include \"%s\"\n", header_name);
395 396 397 398
    fprintf(server, "\n");
}


399
void write_server(ifref_list_t *ifaces)
400
{
401
    unsigned int proc_offset = 0;
402
    int expr_eval_routines;
403
    ifref_t *iface;
404 405 406

    if (!do_server)
        return;
407
    if (do_everything && !need_stub_files(ifaces))
408 409 410 411 412 413
        return;

    init_server();
    if (!server)
        return;

414
    write_formatstringsdecl(server, indent, ifaces, need_stub);
415 416 417 418
    expr_eval_routines = write_expr_eval_routines(server, server_token);
    if (expr_eval_routines)
        write_expr_eval_routine_list(server, server_token);
    write_user_quad_list(server);
419

420
    if (ifaces) LIST_FOR_EACH_ENTRY( iface, ifaces, ifref_t, entry )
421
    {
422
        if (!need_stub(iface->iface))
423 424
            continue;

425 426 427 428
        fprintf(server, "/*****************************************************************************\n");
        fprintf(server, " * %s interface\n", iface->iface->name);
        fprintf(server, " */\n");
        fprintf(server, "\n");
429

430 431 432 433 434
        if (iface->iface->funcs)
        {
            write_serverinterfacedecl(iface->iface);
            write_stubdescdecl(iface->iface);
    
435
            write_function_stubs(iface->iface, &proc_offset);
436
    
437 438 439
            print_server("#if !defined(__RPC_WIN32__)\n");
            print_server("#error  Invalid build platform for this stub.\n");
            print_server("#endif\n");
440

441 442 443 444
            fprintf(server, "\n");
            write_stubdescriptor(iface->iface, expr_eval_routines);
            write_dispatchtable(iface->iface);
        }
445
    }
446

447 448
    fprintf(server, "\n");

449 450
    write_procformatstring(server, ifaces, need_stub);
    write_typeformatstring(server, ifaces, need_stub);
451

452 453
    fclose(server);
}