client.c 14.4 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
 */

#include "config.h"
#include "wine/port.h"
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <signal.h>

#include "windef.h"

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

#include "widltypes.h"
#include "typelib.h"
#include "typelib_struct.h"
42
#include "typegen.h"
43 44 45 46 47 48 49 50 51 52

static FILE* client;
static int indent = 0;

static int print_client( const char *format, ... )
{
    va_list va;
    int i, r;

    va_start(va, format);
53 54 55
    if (format[0] != '\n')
        for (i = 0; i < indent; i++)
            fprintf(client, "    ");
56 57 58 59 60 61
    r = vfprintf(client, format, va);
    va_end(va);
    return r;
}


62
static void print_message_buffer_size(const func_t *func)
63 64 65 66 67
{
    unsigned int total_size = 0;

    if (func->args)
    {
68
        const var_t *var = func->args;
69 70 71
        while (NEXT_LINK(var)) var = NEXT_LINK(var);
        while (var)
        {
72 73
            unsigned int alignment;

74
            total_size += get_required_buffer_size(var, &alignment, PASS_IN);
75 76
            total_size += alignment;

77 78 79 80 81 82
            var = PREV_LINK(var);
        }
    }
    fprintf(client, " %u", total_size);
}

83
static void check_pointers(const func_t *func)
84 85 86 87
{
    var_t *var;

    if (!func->args)
88
        return;
89 90 91 92 93

    var = func->args;
    while (NEXT_LINK(var)) var = NEXT_LINK(var);
    while (var)
    {
94
        if (is_var_ptr(var) && cant_be_null(var))
95
        {
96 97 98 99 100 101
            print_client("if (!%s)\n", var->name);
            print_client("{\n");
            indent++;
            print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
            indent--;
            print_client("}\n\n");
102
        }
103 104 105 106 107

        var = PREV_LINK(var);
    }
}

108
static void write_function_stubs(type_t *iface, unsigned int *proc_offset, unsigned int *type_offset)
109
{
110 111
    const func_t *func = iface->funcs;
    const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
112
    int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
113
    var_t *var;
114 115
    int method_count = 0;

Eric Kohl's avatar
Eric Kohl committed
116 117
    while (NEXT_LINK(func)) func = NEXT_LINK(func);
    while (func)
118
    {
119 120
        const var_t *def = func->def;
        const var_t* explicit_handle_var;
121
        unsigned int type_offset_func;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

        /* 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;
            }
        }
141 142 143 144 145 146

        write_type(client, def->type, def, def->tname);
        fprintf(client, " ");
        write_name(client, def);
        fprintf(client, "(\n");
        indent++;
Eric Kohl's avatar
Eric Kohl committed
147 148
        if (func->args)
            write_args(client, func->args, iface->name, 0, TRUE);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        else
            print_client("void");
        fprintf(client, ")\n");
        indent--;

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

        /* declare return value '_RetVal' */
        if (!is_void(def->type, NULL))
        {
            print_client("");
            write_type(client, def->type, def, def->tname);
            fprintf(client, " _RetVal;\n");
        }

166
        if (implicit_handle || explicit_handle_var)
167 168 169 170 171
            print_client("RPC_BINDING_HANDLE _Handle = 0;\n");

        print_client("RPC_MESSAGE _RpcMessage;\n");
        print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
        fprintf(client, "\n");
172 173 174 175

        /* check pointers */
        check_pointers(func);

176 177 178 179 180 181 182 183 184 185 186 187 188
        print_client("RpcTryFinally\n");
        print_client("{\n");
        indent++;

        print_client("NdrClientInitializeNew(\n");
        indent++;
        print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
        print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
        print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
        print_client("%d);\n", method_count);
        indent--;
        fprintf(client, "\n");

Eric Kohl's avatar
Eric Kohl committed
189
        if (implicit_handle)
190
        {
Eric Kohl's avatar
Eric Kohl committed
191
            print_client("_Handle = %s;\n", implicit_handle);
192 193
            fprintf(client, "\n");
        }
194 195 196 197 198
        else if (explicit_handle_var)
        {
            print_client("_Handle = %s;\n", explicit_handle_var->name);
            fprintf(client, "\n");
        }
199 200 201

        /* emit the message buffer size */
        print_client("_StubMsg.BufferLength =");
202
        print_message_buffer_size(func);
203
        fprintf(client, ";\n");
204

205
        type_offset_func = *type_offset;
206 207
        write_remoting_arguments(client, indent, func, &type_offset_func, PASS_IN, PHASE_BUFFERSIZE);

208 209 210 211
        print_client("NdrGetBuffer(\n");
        indent++;
        print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
        print_client("_StubMsg.BufferLength,\n");
212
        if (implicit_handle || explicit_handle_var)
213
            print_client("_Handle);\n");
214 215 216 217 218 219
        else
            print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
        indent--;
        fprintf(client, "\n");


220
        /* make a copy so we don't increment the type offset twice */
221
        type_offset_func = *type_offset;
222

223
        /* marshal arguments */
224
        write_remoting_arguments(client, indent, func, &type_offset_func, PASS_IN, PHASE_MARSHAL);
225

226
        /* send/receive message */
227
        /* print_client("NdrNsSendReceive(\n"); */
228 229
        /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
        /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
230 231 232
        print_client("NdrSendReceive(\n");
        indent++;
        print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
233
        print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
234 235
        indent--;

236
        print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
237
        print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
Robert Shearman's avatar
Robert Shearman committed
238

239
        if (has_out_arg_or_return(func))
240 241 242 243 244 245 246 247
        {
            fprintf(client, "\n");

            print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
            indent++;
            print_client("NdrConvert(\n");
            indent++;
            print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
248
            print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
249
            indent -= 2;
250
        }
251

252 253
        /* unmarshall arguments */
        fprintf(client, "\n");
254
        write_remoting_arguments(client, indent, func, type_offset, PASS_OUT, PHASE_UNMARSHAL);
255 256 257

        /* unmarshal return value */
        if (!is_void(def->type, NULL))
258
            print_phase_basetype(client, indent, PHASE_UNMARSHAL, PASS_RETURN, def, "_RetVal");
259

260 261 262 263 264 265 266
        /* update proc_offset */
        if (func->args)
        {
            var = func->args;
            while (NEXT_LINK(var)) var = NEXT_LINK(var);
            while (var)
            {
267
                *proc_offset += get_size_procformatstring_var(var);
268 269
                var = PREV_LINK(var);
            }
270
        }
271
        if (!is_void(def->type, NULL))
272
            *proc_offset += get_size_procformatstring_var(def);
273
        else
274
            *proc_offset += 2; /* FC_END and FC_PAD */
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

        indent--;
        print_client("}\n");
        print_client("RpcFinally\n");
        print_client("{\n");
        indent++;


        /* FIXME: emit client finally code */

        print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");

        indent--;
        print_client("}\n");
        print_client("RpcEndFinally\n");


        /* emit return code */
        if (!is_void(def->type, NULL))
        {
            fprintf(client, "\n");
            print_client("return _RetVal;\n");
        }

        indent--;
        fprintf(client, "}\n");
        fprintf(client, "\n");

        method_count++;
Eric Kohl's avatar
Eric Kohl committed
304
        func = PREV_LINK(func);
305 306 307 308 309 310 311 312 313 314 315 316 317
    }
}


static void write_bindinghandledecl(type_t *iface)
{
    print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n", iface->name);
    fprintf(client, "\n");
}


static void write_stubdescdecl(type_t *iface)
{
318
    print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
319 320 321 322
    fprintf(client, "\n");
}


323
static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
324
{
325
    const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
326 327 328 329

    print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
    print_client("{\n");
    indent++;
330
    print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
331 332
    print_client("MIDL_user_allocate,\n");
    print_client("MIDL_user_free,\n");
333 334
    print_client("{\n");
    indent++;
Eric Kohl's avatar
Eric Kohl committed
335 336
    if (implicit_handle)
        print_client("&%s,\n", implicit_handle);
337 338
    else
        print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
339 340
    indent--;
    print_client("},\n");
341 342
    print_client("0,\n");
    print_client("0,\n");
343 344 345 346
    if (expr_eval_routines)
        print_client("ExprEvalRoutines,\n");
    else
        print_client("0,\n");
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    print_client("0,\n");
    print_client("__MIDL_TypeFormatString.Format,\n");
    print_client("1, /* -error bounds_check flag */\n");
    print_client("0x10001, /* Ndr library version */\n");
    print_client("0,\n");
    print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
    print_client("0,\n");
    print_client("0,\n");
    print_client("0,  /* notify & notify_flag routine table */\n");
    print_client("1,  /* Flags */\n");
    print_client("0,  /* Reserved3 */\n");
    print_client("0,  /* Reserved4 */\n");
    print_client("0   /* Reserved5 */\n");
    indent--;
    print_client("};\n");
    fprintf(client, "\n");
}


static void write_clientinterfacedecl(type_t *iface)
{
    unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
369
    const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

    print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
    print_client("{\n");
    indent++;
    print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
    print_client("{{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_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
    print_client("0,\n");
    print_client("0,\n");
    print_client("0,\n");
    print_client("0,\n");
    print_client("0,\n");
    print_client("0,\n");
    indent--;
    print_client("};\n");
388 389 390 391 392 393
    if (old_names)
        print_client("RPC_IF_HANDLE %s_ClientIfHandle = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
                     iface->name, iface->name);
    else
        print_client("RPC_IF_HANDLE %s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
                     iface->name, LOWORD(ver), HIWORD(ver), iface->name);
394 395 396 397 398 399
    fprintf(client, "\n");
}


static void write_implicithandledecl(type_t *iface)
{
400
    const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
401

402
    if (implicit_handle)
403
    {
404
        fprintf(client, "handle_t %s;\n", implicit_handle);
405 406 407 408 409 410 411 412 413 414 415
        fprintf(client, "\n");
    }
}


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

416
    print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
417
    print_client("#include <string.h>\n");
418
    print_client("#ifdef _ALPHA_\n");
419
    print_client("#include <stdarg.h>\n");
420 421
    print_client("#endif\n");
    fprintf(client, "\n");
422
    print_client("#include \"%s\"\n", header_name);
423 424 425 426 427 428
    fprintf(client, "\n");
}


void write_client(ifref_t *ifaces)
{
429 430
    unsigned int proc_offset = 0;
    unsigned int type_offset = 2;
431
    ifref_t *iface = ifaces;
432 433 434

    if (!do_client)
        return;
435
    if (!iface)
436
        return;
437
    END_OF_LIST(iface);
438 439 440 441 442

    init_client();
    if (!client)
        return;

443
    write_formatstringsdecl(client, indent, ifaces, 0);
444

445
    for (; iface; iface = PREV_LINK(iface))
446
    {
447 448 449
        if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
            continue;

450 451 452 453
        fprintf(client, "/*****************************************************************************\n");
        fprintf(client, " * %s interface\n", iface->iface->name);
        fprintf(client, " */\n");
        fprintf(client, "\n");
454

455 456
        if (iface->iface->funcs)
        {
457 458
            int expr_eval_routines;

459 460 461 462 463 464
            write_implicithandledecl(iface->iface);
    
            write_clientinterfacedecl(iface->iface);
            write_stubdescdecl(iface->iface);
            write_bindinghandledecl(iface->iface);
    
465
            write_function_stubs(iface->iface, &proc_offset, &type_offset);
466

467 468 469
            print_client("#if !defined(__RPC_WIN32__)\n");
            print_client("#error  Invalid build platform for this stub.\n");
            print_client("#endif\n");
470

471 472 473 474 475 476 477
            fprintf(client, "\n");

            expr_eval_routines = write_expr_eval_routines(client, iface->iface->name);
            if (expr_eval_routines)
                write_expr_eval_routine_list(client, iface->iface->name);
            write_stubdescriptor(iface->iface, expr_eval_routines);
        }
478
    }
479

480 481
    fprintf(client, "\n");

482 483
    write_procformatstring(client, ifaces, 0);
    write_typeformatstring(client, ifaces, 0);
484

485 486
    fclose(client);
}