typegen.c 117 KB
Newer Older
1 2 3
/*
 * Format String Generator for IDL Compiler
 *
4
 * Copyright 2005-2006 Eric Kohl
5
 * Copyright 2005-2006 Robert Shearman
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23 24 25 26 27 28 29 30 31 32
 */

#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>
Robert Shearman's avatar
Robert Shearman committed
33
#include <limits.h>
34 35 36 37 38

#include "widl.h"
#include "utils.h"
#include "parser.h"
#include "header.h"
39
#include "wine/list.h"
40

41
#include "typegen.h"
42
#include "expr.h"
43

44 45 46 47 48
/* round size up to multiple of alignment */
#define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
/* value to add on to round size up to a multiple of alignment */
#define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))

49
static const func_t *current_func;
50
static const type_t *current_structure;
51
static const type_t *current_iface;
52 53 54 55 56 57

static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
struct expr_eval_routine
{
    struct list entry;
    const type_t *structure;
58
    unsigned int baseoff;
59 60
    const expr_t *expr;
};
61

62
static size_t fields_memsize(const var_list_t *fields, unsigned int *align);
63
static size_t write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
64
static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
65
                                const char *name, int write_ptr, unsigned int *tfsoff);
66
static const var_t *find_array_or_string_in_struct(const type_t *type);
67 68 69
static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
                               type_t *type,
                               const char *name, unsigned int *typestring_offset);
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
const char *string_of_type(unsigned char type)
{
    switch (type)
    {
    case RPC_FC_BYTE: return "FC_BYTE";
    case RPC_FC_CHAR: return "FC_CHAR";
    case RPC_FC_SMALL: return "FC_SMALL";
    case RPC_FC_USMALL: return "FC_USMALL";
    case RPC_FC_WCHAR: return "FC_WCHAR";
    case RPC_FC_SHORT: return "FC_SHORT";
    case RPC_FC_USHORT: return "FC_USHORT";
    case RPC_FC_LONG: return "FC_LONG";
    case RPC_FC_ULONG: return "FC_ULONG";
    case RPC_FC_FLOAT: return "FC_FLOAT";
    case RPC_FC_HYPER: return "FC_HYPER";
    case RPC_FC_DOUBLE: return "FC_DOUBLE";
    case RPC_FC_ENUM16: return "FC_ENUM16";
    case RPC_FC_ENUM32: return "FC_ENUM32";
    case RPC_FC_IGNORE: return "FC_IGNORE";
    case RPC_FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
    case RPC_FC_RP: return "FC_RP";
    case RPC_FC_UP: return "FC_UP";
    case RPC_FC_OP: return "FC_OP";
    case RPC_FC_FP: return "FC_FP";
95 96 97 98 99 100 101 102
    case RPC_FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
    case RPC_FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
    case RPC_FC_STRUCT: return "FC_STRUCT";
    case RPC_FC_PSTRUCT: return "FC_PSTRUCT";
    case RPC_FC_CSTRUCT: return "FC_CSTRUCT";
    case RPC_FC_CPSTRUCT: return "FC_CPSTRUCT";
    case RPC_FC_CVSTRUCT: return "FC_CVSTRUCT";
    case RPC_FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
103 104 105 106 107 108 109
    case RPC_FC_SMFARRAY: return "FC_SMFARRAY";
    case RPC_FC_LGFARRAY: return "FC_LGFARRAY";
    case RPC_FC_SMVARRAY: return "FC_SMVARRAY";
    case RPC_FC_LGVARRAY: return "FC_LGVARRAY";
    case RPC_FC_CARRAY: return "FC_CARRAY";
    case RPC_FC_CVARRAY: return "FC_CVARRAY";
    case RPC_FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
110 111
    case RPC_FC_ALIGNM4: return "FC_ALIGNM4";
    case RPC_FC_ALIGNM8: return "FC_ALIGNM8";
112
    case RPC_FC_POINTER: return "FC_POINTER";
113 114 115 116
    case RPC_FC_C_CSTRING: return "FC_C_CSTRING";
    case RPC_FC_C_WSTRING: return "FC_C_WSTRING";
    case RPC_FC_CSTRING: return "FC_CSTRING";
    case RPC_FC_WSTRING: return "FC_WSTRING";
117 118 119 120 121 122
    default:
        error("string_of_type: unknown type 0x%02x\n", type);
        return NULL;
    }
}

123
int is_struct(unsigned char type)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
{
    switch (type)
    {
    case RPC_FC_STRUCT:
    case RPC_FC_PSTRUCT:
    case RPC_FC_CSTRUCT:
    case RPC_FC_CPSTRUCT:
    case RPC_FC_CVSTRUCT:
    case RPC_FC_BOGUS_STRUCT:
        return 1;
    default:
        return 0;
    }
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
static int is_non_complex_struct(const type_t *type)
{
    switch (type->type)
    {
    case RPC_FC_STRUCT:
    case RPC_FC_PSTRUCT:
    case RPC_FC_CSTRUCT:
    case RPC_FC_CPSTRUCT:
    case RPC_FC_CVSTRUCT:
        return 1;
    default:
        return 0;
    }
}

154
int is_union(unsigned char type)
155 156 157 158 159 160 161 162 163 164 165
{
    switch (type)
    {
    case RPC_FC_ENCAPSULATED_UNION:
    case RPC_FC_NON_ENCAPSULATED_UNION:
        return 1;
    default:
        return 0;
    }
}

166 167 168 169 170 171 172 173 174 175 176
static int type_has_pointers(const type_t *type)
{
    if (is_user_type(type))
        return FALSE;
    else if (is_ptr(type))
        return TRUE;
    else if (is_array(type))
        return type_has_pointers(type->ref);
    else if (is_struct(type->type))
    {
        const var_t *field;
177
        if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
178 179 180 181 182 183 184 185 186 187 188
        {
            if (type_has_pointers(field->type))
                return TRUE;
        }
    }
    else if (is_union(type->type))
    {
        var_list_t *fields;
        const var_t *field;
        if (type->type == RPC_FC_ENCAPSULATED_UNION)
        {
189 190
            const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
            fields = uv->type->fields_or_args;
191 192
        }
        else
193
            fields = type->fields_or_args;
194 195 196 197 198 199 200 201 202 203
        if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
        {
            if (field->type && type_has_pointers(field->type))
                return TRUE;
        }
    }

    return FALSE;
}

204 205 206 207 208 209 210 211 212 213 214 215 216
static int type_has_full_pointer(const type_t *type)
{
    if (is_user_type(type))
        return FALSE;
    else if (type->type == RPC_FC_FP)
        return TRUE;
    else if (is_ptr(type))
        return FALSE;
    else if (is_array(type))
        return type_has_full_pointer(type->ref);
    else if (is_struct(type->type))
    {
        const var_t *field;
217
        if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
218 219 220 221 222 223 224 225 226 227 228
        {
            if (type_has_full_pointer(field->type))
                return TRUE;
        }
    }
    else if (is_union(type->type))
    {
        var_list_t *fields;
        const var_t *field;
        if (type->type == RPC_FC_ENCAPSULATED_UNION)
        {
229 230
            const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
            fields = uv->type->fields_or_args;
231 232
        }
        else
233
            fields = type->fields_or_args;
234 235 236 237 238 239 240 241 242 243
        if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
        {
            if (field->type && type_has_full_pointer(field->type))
                return TRUE;
        }
    }

    return FALSE;
}

244 245 246 247 248 249 250 251 252 253 254 255 256 257
static unsigned short user_type_offset(const char *name)
{
    user_type_t *ut;
    unsigned short off = 0;
    LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
    {
        if (strcmp(name, ut->name) == 0)
            return off;
        ++off;
    }
    error("user_type_offset: couldn't find type (%s)\n", name);
    return 0;
}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
{
    type->typestring_offset = offset;
    if (file) type->tfswrite = FALSE;
}

static void guard_rec(type_t *type)
{
    /* types that contain references to themselves (like a linked list),
       need to be shielded from infinite recursion when writing embedded
       types  */
    if (type->typestring_offset)
        type->tfswrite = FALSE;
    else
        type->typestring_offset = 1;
}

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
static type_t *get_user_type(const type_t *t, const char **pname)
{
    for (;;)
    {
        type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
        if (ut)
        {
            if (pname)
                *pname = t->name;
            return ut;
        }

        if (t->kind == TKIND_ALIAS)
            t = t->orig;
        else
            return 0;
    }
}

294
int is_user_type(const type_t *t)
295 296 297 298
{
    return get_user_type(t, NULL) != NULL;
}

299 300
static int is_embedded_complex(const type_t *type)
{
301
    unsigned char tc = type->type;
302 303
    return is_struct(tc) || is_union(tc) || is_array(type) || is_user_type(type)
        || (is_ptr(type) && type->ref->type == RPC_FC_IP);
304 305
}

306 307 308 309 310 311 312 313 314 315
static const char *get_context_handle_type_name(const type_t *type)
{
    const type_t *t;
    for (t = type; is_ptr(t); t = t->ref)
        if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
            return t->name;
    assert(0);
    return NULL;
}

316 317 318 319 320 321 322 323
#define WRITE_FCTYPE(file, fctype, typestring_offset) \
    do { \
        if (file) \
            fprintf(file, "/* %2u */\n", typestring_offset); \
        print_file((file), 2, "0x%02x,    /* " #fctype " */\n", RPC_##fctype); \
    } \
    while (0)

324
static void print_file(FILE *file, int indent, const char *format, ...)
325 326 327
{
    va_list va;
    va_start(va, format);
328
    print(file, indent, format, va);
329
    va_end(va);
330 331 332 333 334 335 336 337 338 339 340
}

void print(FILE *file, int indent, const char *format, va_list va)
{
    if (file)
    {
        if (format[0] != '\n')
            while (0 < indent--)
                fprintf(file, "    ");
        vfprintf(file, format, va);
    }
341 342
}

343

344
static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
345 346
{
    if (decl_indirect(t))
347
    {
348 349
        print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
                   local_var_prefix, n, local_var_prefix, n);
350 351
        print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
    }
352
    else if (is_ptr(t) || is_array(t))
353
        print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
354 355
}

356
void write_parameters_init(FILE *file, int indent, const func_t *func, const char *local_var_prefix)
357 358 359
{
    const var_t *var;

360
    if (!is_void(get_func_return_type(func)))
361
        write_var_init(file, indent, get_func_return_type(func), "_RetVal", local_var_prefix);
362

363 364 365 366
    if (!func->args)
        return;

    LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
367
        write_var_init(file, indent, var->type, var->name, local_var_prefix);
368 369 370 371

    fprintf(file, "\n");
}

372 373 374 375 376 377 378 379 380 381
static void write_formatdesc(FILE *f, int indent, const char *str)
{
    print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
    print_file(f, indent, "{\n");
    print_file(f, indent + 1, "short Pad;\n");
    print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
    print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
    print_file(f, indent, "\n");
}

382
void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
383 384
{
    print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
385
               get_size_typeformatstring(stmts, pred));
386 387

    print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
388
               get_size_procformatstring(stmts, pred));
389 390 391 392 393 394 395 396 397 398

    fprintf(f, "\n");
    write_formatdesc(f, indent, "TYPE");
    write_formatdesc(f, indent, "PROC");
    fprintf(f, "\n");
    print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
    print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
    print_file(f, indent, "\n");
}

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
static inline int is_base_type(unsigned char type)
{
    switch (type)
    {
    case RPC_FC_BYTE:
    case RPC_FC_CHAR:
    case RPC_FC_USMALL:
    case RPC_FC_SMALL:
    case RPC_FC_WCHAR:
    case RPC_FC_USHORT:
    case RPC_FC_SHORT:
    case RPC_FC_ULONG:
    case RPC_FC_LONG:
    case RPC_FC_HYPER:
    case RPC_FC_IGNORE:
    case RPC_FC_FLOAT:
    case RPC_FC_DOUBLE:
    case RPC_FC_ENUM16:
    case RPC_FC_ENUM32:
    case RPC_FC_ERROR_STATUS_T:
419
    case RPC_FC_BIND_PRIMITIVE:
420 421 422 423 424 425 426
        return TRUE;

    default:
        return FALSE;
    }
}

427 428 429 430 431 432 433 434
int decl_indirect(const type_t *t)
{
    return is_user_type(t)
        || (!is_base_type(t->type)
            && !is_ptr(t)
            && !is_array(t));
}

435 436 437 438 439
static size_t write_procformatstring_type(FILE *file, int indent,
                                          const char *name,
                                          const type_t *type,
                                          const attr_list_t *attrs,
                                          int is_return)
440
{
441
    size_t size;
442

443 444
    int is_in = is_attr(attrs, ATTR_IN);
    int is_out = is_attr(attrs, ATTR_OUT);
445 446

    if (!is_in && !is_out) is_in = TRUE;
447

448
    if (!type->declarray && is_base_type(type->type))
449
    {
450 451 452 453 454
        if (is_return)
            print_file(file, indent, "0x53,    /* FC_RETURN_PARAM_BASETYPE */\n");
        else
            print_file(file, indent, "0x4e,    /* FC_IN_PARAM_BASETYPE */\n");

455
        if (type->type == RPC_FC_BIND_PRIMITIVE)
456
        {
457
            print_file(file, indent, "0x%02x,    /* FC_IGNORE */\n", RPC_FC_IGNORE);
458 459
            size = 2; /* includes param type prefix */
        }
460
        else if (is_base_type(type->type))
461
        {
462
            print_file(file, indent, "0x%02x,    /* %s */\n", type->type, string_of_type(type->type));
463
            size = 2; /* includes param type prefix */
464 465 466
        }
        else
        {
467
            error("Unknown/unsupported type: %s (0x%02x)\n", name, type->type);
468 469 470 471 472 473 474
            size = 0;
        }
    }
    else
    {
        if (is_return)
            print_file(file, indent, "0x52,    /* FC_RETURN_PARAM */\n");
475
        else if (is_in && is_out)
476
            print_file(file, indent, "0x50,    /* FC_IN_OUT_PARAM */\n");
477
        else if (is_out)
478
            print_file(file, indent, "0x51,    /* FC_OUT_PARAM */\n");
479 480
        else
            print_file(file, indent, "0x4d,    /* FC_IN_PARAM */\n");
481

482
        print_file(file, indent, "0x01,\n");
483
        print_file(file, indent, "NdrFcShort(0x%x),\n", type->typestring_offset);
484
        size = 4; /* includes param type prefix */
485
    }
486
    return size;
487 488
}

489
static void write_procformatstring_stmts(FILE *file, int indent, const statement_list_t *stmts, type_pred_t pred)
490
{
491 492
    const statement_t *stmt;
    if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
493
    {
494
        if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
495
        {
496
            const func_t *func;
497 498 499
            if (!pred(stmt->u.type))
                continue;
            if (stmt->u.type->funcs) LIST_FOR_EACH_ENTRY( func, stmt->u.type->funcs, const func_t, entry )
500
            {
501
                if (is_local(func->def->attrs)) continue;
502 503
                /* emit argument data */
                if (func->args)
504
                {
505
                    const var_t *var;
506
                    LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
507
                        write_procformatstring_type(file, indent, var->name, var->type, var->attrs, FALSE);
508
                }
509

510
                /* emit return value data */
511
                if (is_void(get_func_return_type(func)))
512 513 514 515 516
                {
                    print_file(file, indent, "0x5b,    /* FC_END */\n");
                    print_file(file, indent, "0x5c,    /* FC_PAD */\n");
                }
                else
517
                    write_procformatstring_type(file, indent, "return value", get_func_return_type(func), NULL, TRUE);
518
            }
519
        }
520 521
        else if (stmt->type == STMT_LIBRARY)
            write_procformatstring_stmts(file, indent, stmt->u.lib->stmts, pred);
522
    }
523 524 525 526 527 528 529 530 531 532 533 534 535 536
}

void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
{
    int indent = 0;

    print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
    print_file(file, indent, "{\n");
    indent++;
    print_file(file, indent, "0,\n");
    print_file(file, indent, "{\n");
    indent++;

    write_procformatstring_stmts(file, indent, stmts, pred);
537 538 539 540 541 542 543 544 545

    print_file(file, indent, "0x0\n");
    indent--;
    print_file(file, indent, "}\n");
    indent--;
    print_file(file, indent, "};\n");
    print_file(file, indent, "\n");
}

546 547
static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
{
548
    if (is_base_type(type->type))
549
    {
550 551 552
        print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
        *typestring_offset += 1;
        return 1;
553
    }
554

555 556 557
    return 0;
}

558
/* write conformance / variance descriptor */
559
static size_t write_conf_or_var_desc(FILE *file, const type_t *structure,
560 561
                                     unsigned int baseoff, const type_t *type,
                                     const expr_t *expr)
562
{
563
    unsigned char operator_type = 0;
564 565
    unsigned char conftype = RPC_FC_NORMAL_CONFORMANCE;
    const char *conftype_string = "";
566
    const char *operator_string = "no operators";
567
    const expr_t *subexpr;
568

569 570 571 572 573 574
    if (!expr)
    {
        print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
        return 4;
    }

575 576 577 578 579 580 581 582 583
    if (!structure)
    {
        /* Top-level conformance calculations are done inline.  */
        print_file (file, 2, "0x%x,\t/* Corr desc: parameter */\n",
                    RPC_FC_TOP_LEVEL_CONFORMANCE);
        print_file (file, 2, "0x0,\n");
        print_file (file, 2, "NdrFcShort(0x0),\n");
        return 4;
    }
584

585 586 587 588 589 590 591 592 593 594
    if (expr->is_const)
    {
        if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
            error("write_conf_or_var_desc: constant value %ld is greater than "
                  "the maximum constant size of %d\n", expr->cval,
                  UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);

        print_file(file, 2, "0x%x, /* Corr desc: constant, val = %ld */\n",
                   RPC_FC_CONSTANT_CONFORMANCE, expr->cval);
        print_file(file, 2, "0x%x,\n", expr->cval & ~USHRT_MAX);
595
        print_file(file, 2, "NdrFcShort(0x%x),\n", expr->cval & USHRT_MAX);
596 597 598 599

        return 4;
    }

600 601 602 603 604 605
    if (is_ptr(type) || (is_array(type) && !type->declarray))
    {
        conftype = RPC_FC_POINTER_CONFORMANCE;
        conftype_string = "field pointer, ";
    }

606
    subexpr = expr;
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
    switch (subexpr->type)
    {
    case EXPR_PPTR:
        subexpr = subexpr->ref;
        operator_type = RPC_FC_DEREFERENCE;
        operator_string = "FC_DEREFERENCE";
        break;
    case EXPR_DIV:
        if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
        {
            subexpr = subexpr->ref;
            operator_type = RPC_FC_DIV_2;
            operator_string = "FC_DIV_2";
        }
        break;
    case EXPR_MUL:
        if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
        {
            subexpr = subexpr->ref;
            operator_type = RPC_FC_MULT_2;
            operator_string = "FC_MULT_2";
        }
        break;
    case EXPR_SUB:
        if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
        {
            subexpr = subexpr->ref;
            operator_type = RPC_FC_SUB_1;
            operator_string = "FC_SUB_1";
        }
        break;
    case EXPR_ADD:
        if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
        {
            subexpr = subexpr->ref;
            operator_type = RPC_FC_ADD_1;
            operator_string = "FC_ADD_1";
        }
        break;
    default:
        break;
    }

    if (subexpr->type == EXPR_IDENTIFIER)
651
    {
652
        const type_t *correlation_variable = NULL;
653
        unsigned char correlation_variable_type;
654
        unsigned char param_type = 0;
655 656
        size_t offset = 0;
        const var_t *var;
657

658
        if (structure->fields_or_args) LIST_FOR_EACH_ENTRY( var, structure->fields_or_args, const var_t, entry )
659
        {
660 661 662
            unsigned int align = 0;
            /* FIXME: take alignment into account */
            if (var->name && !strcmp(var->name, subexpr->u.sval))
663
            {
664 665
                correlation_variable = var->type;
                break;
666
            }
667
            offset += type_memsize(var->type, &align);
668
        }
669 670 671
        if (!correlation_variable)
            error("write_conf_or_var_desc: couldn't find variable %s in structure\n",
                  subexpr->u.sval);
672

673 674
        correlation_variable = expr_resolve_type(NULL, structure, expr);

675
        offset -= baseoff;
676
        correlation_variable_type = correlation_variable->type;
677

678
        switch (correlation_variable_type)
679 680 681 682 683 684 685 686 687 688 689
        {
        case RPC_FC_CHAR:
        case RPC_FC_SMALL:
            param_type = RPC_FC_SMALL;
            break;
        case RPC_FC_BYTE:
        case RPC_FC_USMALL:
            param_type = RPC_FC_USMALL;
            break;
        case RPC_FC_WCHAR:
        case RPC_FC_SHORT:
690
        case RPC_FC_ENUM16:
691 692 693 694 695 696
            param_type = RPC_FC_SHORT;
            break;
        case RPC_FC_USHORT:
            param_type = RPC_FC_USHORT;
            break;
        case RPC_FC_LONG:
697
        case RPC_FC_ENUM32:
698 699 700 701 702 703 704
            param_type = RPC_FC_LONG;
            break;
        case RPC_FC_ULONG:
            param_type = RPC_FC_ULONG;
            break;
        default:
            error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
705
                correlation_variable_type);
706
        }
707

708 709
        print_file(file, 2, "0x%x, /* Corr desc: %s%s */\n",
                   conftype | param_type, conftype_string, string_of_type(param_type));
710
        print_file(file, 2, "0x%x, /* %s */\n", operator_type, operator_string);
711 712
        print_file(file, 2, "NdrFcShort(0x%x), /* offset = %d */\n",
                   offset, offset);
713 714
    }
    else
715 716
    {
        unsigned int callback_offset = 0;
717 718
        struct expr_eval_routine *eval;
        int found = 0;
719

720
        LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
721
        {
722 723
            if (!strcmp (eval->structure->name, structure->name)
                && !compare_expr (eval->expr, expr))
724
            {
725 726
                found = 1;
                break;
727
            }
728
            callback_offset++;
729
        }
730 731

        if (!found)
732
        {
733 734
            eval = xmalloc (sizeof(*eval));
            eval->structure = structure;
735
            eval->baseoff = baseoff;
736 737
            eval->expr = expr;
            list_add_tail (&expr_eval_routines, &eval->entry);
738 739
        }

740 741 742
        if (callback_offset > USHRT_MAX)
            error("Maximum number of callback routines reached\n");

743
        print_file(file, 2, "0x%x, /* Corr desc: %s */\n", conftype, conftype_string);
744
        print_file(file, 2, "0x%x, /* %s */\n", RPC_FC_CALLBACK, "FC_CALLBACK");
745
        print_file(file, 2, "NdrFcShort(0x%x), /* %u */\n", callback_offset, callback_offset);
746
    }
747 748 749
    return 4;
}

750
static size_t fields_memsize(const var_list_t *fields, unsigned int *align)
Robert Shearman's avatar
Robert Shearman committed
751
{
752
    int have_align = FALSE;
Robert Shearman's avatar
Robert Shearman committed
753
    size_t size = 0;
754 755 756 757
    const var_t *v;

    if (!fields) return 0;
    LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
758 759 760 761 762 763 764 765
    {
        unsigned int falign = 0;
        size_t fsize = type_memsize(v->type, &falign);
        if (!have_align)
        {
            *align = falign;
            have_align = TRUE;
        }
766
        size = ROUND_SIZE(size, falign);
767 768
        size += fsize;
    }
769

770
    size = ROUND_SIZE(size, *align);
Robert Shearman's avatar
Robert Shearman committed
771 772 773
    return size;
}

774 775 776 777 778 779 780 781 782 783 784
static size_t union_memsize(const var_list_t *fields, unsigned int *pmaxa)
{
    size_t size, maxs = 0;
    unsigned int align = *pmaxa;
    const var_t *v;

    if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
    {
        /* we could have an empty default field with NULL type */
        if (v->type)
        {
785
            size = type_memsize(v->type, &align);
786 787 788 789 790 791 792 793
            if (maxs < size) maxs = size;
            if (*pmaxa < align) *pmaxa = align;
        }
    }

    return maxs;
}

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
int get_padding(const var_list_t *fields)
{
    unsigned short offset = 0;
    int salign = -1;
    const var_t *f;

    if (!fields)
        return 0;

    LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
    {
        type_t *ft = f->type;
        unsigned int align = 0;
        size_t size = type_memsize(ft, &align);
        if (salign == -1)
            salign = align;
810
        offset = ROUND_SIZE(offset, align);
811 812 813
        offset += size;
    }

814
    return ROUNDING(offset, salign);
815 816
}

817
size_t type_memsize(const type_t *t, unsigned int *align)
Robert Shearman's avatar
Robert Shearman committed
818 819 820
{
    size_t size = 0;

821 822 823
    if (t->kind == TKIND_ALIAS)
        size = type_memsize(t->orig, align);
    else if (t->declarray && is_conformant_array(t))
824 825 826 827 828
    {
        type_memsize(t->ref, align);
        size = 0;
    }
    else if (is_ptr(t) || is_conformant_array(t))
829 830 831 832 833
    {
        size = sizeof(void *);
        if (size > *align) *align = size;
    }
    else switch (t->type)
Robert Shearman's avatar
Robert Shearman committed
834 835 836 837 838 839
    {
    case RPC_FC_BYTE:
    case RPC_FC_CHAR:
    case RPC_FC_USMALL:
    case RPC_FC_SMALL:
        size = 1;
840
        if (size > *align) *align = size;
Robert Shearman's avatar
Robert Shearman committed
841 842 843 844 845 846
        break;
    case RPC_FC_WCHAR:
    case RPC_FC_USHORT:
    case RPC_FC_SHORT:
    case RPC_FC_ENUM16:
        size = 2;
847
        if (size > *align) *align = size;
Robert Shearman's avatar
Robert Shearman committed
848 849 850 851 852 853 854
        break;
    case RPC_FC_ULONG:
    case RPC_FC_LONG:
    case RPC_FC_ERROR_STATUS_T:
    case RPC_FC_ENUM32:
    case RPC_FC_FLOAT:
        size = 4;
855
        if (size > *align) *align = size;
Robert Shearman's avatar
Robert Shearman committed
856 857 858 859
        break;
    case RPC_FC_HYPER:
    case RPC_FC_DOUBLE:
        size = 8;
860
        if (size > *align) *align = size;
Robert Shearman's avatar
Robert Shearman committed
861 862 863 864 865 866 867
        break;
    case RPC_FC_STRUCT:
    case RPC_FC_CVSTRUCT:
    case RPC_FC_CPSTRUCT:
    case RPC_FC_CSTRUCT:
    case RPC_FC_PSTRUCT:
    case RPC_FC_BOGUS_STRUCT:
868
        size = fields_memsize(t->fields_or_args, align);
869
        break;
Robert Shearman's avatar
Robert Shearman committed
870 871
    case RPC_FC_ENCAPSULATED_UNION:
    case RPC_FC_NON_ENCAPSULATED_UNION:
872
        size = union_memsize(t->fields_or_args, align);
Robert Shearman's avatar
Robert Shearman committed
873
        break;
874 875 876 877 878
    case RPC_FC_SMFARRAY:
    case RPC_FC_LGFARRAY:
    case RPC_FC_SMVARRAY:
    case RPC_FC_LGVARRAY:
    case RPC_FC_BOGUS_ARRAY:
879
        size = t->dim * type_memsize(t->ref, align);
880
        break;
Robert Shearman's avatar
Robert Shearman committed
881
    default:
882
        error("type_memsize: Unknown type %d\n", t->type);
Robert Shearman's avatar
Robert Shearman committed
883 884 885 886 887 888
        size = 0;
    }

    return size;
}

889 890 891
int is_full_pointer_function(const func_t *func)
{
    const var_t *var;
892
    if (type_has_full_pointer(get_func_return_type(func)))
893 894 895 896 897 898 899 900 901 902 903
        return TRUE;
    if (!func->args)
        return FALSE;
    LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
        if (type_has_full_pointer( var->type ))
            return TRUE;
    return FALSE;
}

void write_full_pointer_init(FILE *file, int indent, const func_t *func, int is_server)
{
904
    print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
905 906 907 908 909 910
                   is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
    fprintf(file, "\n");
}

void write_full_pointer_free(FILE *file, int indent, const func_t *func)
{
911
    print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
912 913 914
    fprintf(file, "\n");
}

915
static unsigned int write_nonsimple_pointer(FILE *file, const type_t *type, size_t offset)
916 917 918
{
    short absoff = type->ref->typestring_offset;
    short reloff = absoff - (offset + 2);
919 920 921 922
    int ptr_attr = is_ptr(type->ref) ? 0x10 : 0x0;

    print_file(file, 2, "0x%02x, 0x%x,\t/* %s */\n",
               type->type, ptr_attr, string_of_type(type->type));
923 924 925 926 927
    print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%hd) */\n",
               reloff, reloff, absoff);
    return 4;
}

928
static unsigned int write_simple_pointer(FILE *file, const type_t *type)
929
{
930 931 932 933 934
    unsigned char fc = type->ref->type;
    /* for historical reasons, write_simple_pointer also handled string types,
     * but no longer does. catch bad uses of the function with this check */
    if (is_string_type(type->attrs, type))
        error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
935 936
    print_file(file, 2, "0x%02x, 0x8,\t/* %s [simple_pointer] */\n",
               type->type, string_of_type(type->type));
937
    print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
938 939 940 941
    print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
    return 4;
}

942 943 944 945 946 947 948
static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
{
    print_file(file, 0, "/* %u (", tfsoff);
    write_type_decl(file, t, NULL);
    print_file(file, 0, ") */\n");
}

949
static size_t write_pointer_tfs(FILE *file, type_t *type, unsigned int *typestring_offset)
950
{
951
    unsigned int offset = *typestring_offset;
952

953
    print_start_tfs_comment(file, type, offset);
954
    update_tfsoff(type, offset, file);
955 956 957 958 959 960 961 962 963

    if (type->ref->typestring_offset)
        *typestring_offset += write_nonsimple_pointer(file, type, offset);
    else if (is_base_type(type->ref->type))
        *typestring_offset += write_simple_pointer(file, type);

    return offset;
}

964
static int processed(const type_t *type)
965
{
966
    return type->typestring_offset && !type->tfswrite;
967 968
}

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
static int user_type_has_variable_size(const type_t *t)
{
    if (is_ptr(t))
        return TRUE;
    else
        switch (t->type)
        {
        case RPC_FC_PSTRUCT:
        case RPC_FC_CSTRUCT:
        case RPC_FC_CPSTRUCT:
        case RPC_FC_CVSTRUCT:
            return TRUE;
        }
    /* Note: Since this only applies to user types, we can't have a conformant
       array here, and strings should get filed under pointer in this case.  */
    return FALSE;
}

987 988
static void write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
{
989
    unsigned int start, absoff, flags;
990
    unsigned int align = 0, ualign = 0;
991
    const char *name = NULL;
992
    type_t *utype = get_user_type(type, &name);
993
    size_t usize = user_type_has_variable_size(utype) ? 0 : type_memsize(utype, &ualign);
994 995 996 997 998 999 1000 1001 1002
    size_t size = type_memsize(type, &align);
    unsigned short funoff = user_type_offset(name);
    short reloff;

    guard_rec(type);

    if (is_base_type(utype->type))
    {
        absoff = *tfsoff;
1003
        print_start_tfs_comment(file, utype, absoff);
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
        print_file(file, 2, "0x%x,\t/* %s */\n", utype->type, string_of_type(utype->type));
        print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
        *tfsoff += 2;
    }
    else
    {
        if (!processed(utype))
            write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
        absoff = utype->typestring_offset;
    }

1015 1016 1017 1018 1019 1020 1021
    if (utype->type == RPC_FC_RP)
        flags = 0x40;
    else if (utype->type == RPC_FC_UP)
        flags = 0x80;
    else
        flags = 0;

1022 1023
    start = *tfsoff;
    update_tfsoff(type, start, file);
1024
    print_start_tfs_comment(file, type, start);
1025
    print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", RPC_FC_USER_MARSHAL);
1026 1027
    print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
               flags | (align - 1), align - 1, flags);
1028 1029
    print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
    print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", size, size);
1030
    print_file(file, 2, "NdrFcShort(0x%lx),\t/* %lu */\n", usize, usize);
1031 1032 1033 1034 1035 1036
    *tfsoff += 8;
    reloff = absoff - *tfsoff;
    print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n", reloff, reloff, absoff);
    *tfsoff += 2;
}

1037 1038
static void write_member_type(FILE *file, const type_t *cont,
                              const attr_list_t *attrs, const type_t *type,
1039
                              unsigned int *corroff, unsigned int *tfsoff)
1040
{
1041
    if (is_embedded_complex(type) && !is_conformant_array(type))
1042
    {
1043 1044 1045
        size_t absoff;
        short reloff;

1046
        if (is_union(type->type) && is_attr(attrs, ATTR_SWITCHIS))
1047 1048 1049 1050 1051 1052 1053 1054 1055
        {
            absoff = *corroff;
            *corroff += 8;
        }
        else
        {
            absoff = type->typestring_offset;
        }
        reloff = absoff - (*tfsoff + 2);
1056 1057 1058 1059 1060 1061 1062 1063

        print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
        /* FIXME: actually compute necessary padding */
        print_file(file, 2, "0x0,\t/* FIXME: padding */\n");
        print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
                   reloff, reloff, absoff);
        *tfsoff += 4;
    }
1064
    else if (is_ptr(type) || is_conformant_array(type))
1065
    {
1066 1067 1068 1069
        unsigned char fc = (cont->type == RPC_FC_BOGUS_STRUCT
                            ? RPC_FC_POINTER
                            : RPC_FC_LONG);
        print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1070 1071
        *tfsoff += 1;
    }
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    else if (!write_base_type(file, type, tfsoff))
        error("Unsupported member type 0x%x\n", type->type);
}

static void write_end(FILE *file, unsigned int *tfsoff)
{
    if (*tfsoff % 2 == 0)
    {
        print_file(file, 2, "0x%x,\t\t/* FC_PAD */\n", RPC_FC_PAD);
        *tfsoff += 1;
    }
    print_file(file, 2, "0x%x,\t\t/* FC_END */\n", RPC_FC_END);
    *tfsoff += 1;
}

1087 1088 1089
static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
{
    unsigned int offset = 0;
1090
    var_list_t *fs = type->fields_or_args;
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    var_t *f;

    if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
    {
        unsigned int align = 0;
        type_t *ft = f->type;
        if (is_union(ft->type) && is_attr(f->attrs, ATTR_SWITCHIS))
        {
            unsigned int absoff = ft->typestring_offset;
            short reloff = absoff - (*tfsoff + 6);
            print_file(file, 0, "/* %d */\n", *tfsoff);
            print_file(file, 2, "0x%x,\t/* %s */\n", ft->type, string_of_type(ft->type));
            print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", RPC_FC_LONG);
1104
            write_conf_or_var_desc(file, current_structure, offset, ft,
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
                                   get_attrp(f->attrs, ATTR_SWITCHIS));
            print_file(file, 2, "NdrFcShort(%hd),\t/* Offset= %hd (%u) */\n",
                       reloff, reloff, absoff);
            *tfsoff += 8;
        }

        /* FIXME: take alignment into account */
        offset += type_memsize(ft, &align);
    }
}

1116
static int write_no_repeat_pointer_descriptions(
1117
    FILE *file, type_t *type,
1118 1119
    size_t *offset_in_memory, size_t *offset_in_buffer,
    unsigned int *typestring_offset)
1120
{
1121 1122
    int written = 0;
    unsigned int align;
1123

1124
    if (is_ptr(type) || (!type->declarray && is_conformant_array(type)))
1125
    {
1126 1127
        size_t memsize;

1128 1129 1130 1131 1132 1133
        print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", RPC_FC_NO_REPEAT);
        print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);

        /* pointer instance */
        print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
        print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);
1134
        *typestring_offset += 6;
1135

1136
        if (is_ptr(type))
1137 1138 1139 1140 1141 1142
        {
            if (is_string_type(type->attrs, type))
                write_string_tfs(file, NULL, type, NULL, typestring_offset);
            else
                write_pointer_tfs(file, type, typestring_offset);
        }
1143
        else
1144 1145 1146 1147 1148 1149 1150 1151 1152
        {
            unsigned absoff = type->typestring_offset;
            short reloff = absoff - (*typestring_offset + 2);
            /* FIXME: get pointer attributes from field */
            print_file(file, 2, "0x%02x, 0x0,\t/* %s */\n", RPC_FC_UP, "FC_UP");
            print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
                       reloff, reloff, absoff);
            *typestring_offset += 4;
        }
1153 1154

        align = 0;
1155 1156
        memsize = type_memsize(type, &align);
        *offset_in_memory += memsize;
1157 1158
        /* increment these separately as in the case of conformant (varying)
         * structures these start at different values */
1159
        *offset_in_buffer += memsize;
1160 1161 1162 1163 1164 1165 1166

        return 1;
    }

    if (is_non_complex_struct(type))
    {
        const var_t *v;
1167
        LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
        {
            if (offset_in_memory && offset_in_buffer)
            {
                size_t padding;
                align = 0;
                type_memsize(v->type, &align);
                padding = ROUNDING(*offset_in_memory, align);
                *offset_in_memory += padding;
                *offset_in_buffer += padding;
            }
1178
            written += write_no_repeat_pointer_descriptions(
1179
                file, v->type,
1180
                offset_in_memory, offset_in_buffer, typestring_offset);
1181
        }
1182
    }
1183 1184
    else
    {
1185
        size_t memsize;
1186
        align = 0;
1187 1188
        memsize = type_memsize(type, &align);
        *offset_in_memory += memsize;
1189 1190
        /* increment these separately as in the case of conformant (varying)
         * structures these start at different values */
1191
        *offset_in_buffer += memsize;
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
    }

    return written;
}

static int write_pointer_description_offsets(
    FILE *file, const attr_list_t *attrs, type_t *type,
    size_t *offset_in_memory, size_t *offset_in_buffer,
    unsigned int *typestring_offset)
{
    int written = 0;
    unsigned int align;

1205
    if (is_ptr(type) && type->ref->type != RPC_FC_IP)
1206 1207 1208
    {
        if (offset_in_memory && offset_in_buffer)
        {
1209 1210
            size_t memsize;

1211 1212 1213 1214 1215 1216
            /* pointer instance */
            /* FIXME: sometimes from end of structure, sometimes from beginning */
            print_file(file, 2, "NdrFcShort(0x%x), /* Memory offset = %d */\n", *offset_in_memory, *offset_in_memory);
            print_file(file, 2, "NdrFcShort(0x%x), /* Buffer offset = %d */\n", *offset_in_buffer, *offset_in_buffer);

            align = 0;
1217 1218 1219 1220 1221
            memsize = type_memsize(type, &align);
            *offset_in_memory += memsize;
            /* increment these separately as in the case of conformant (varying)
             * structures these start at different values */
            *offset_in_buffer += memsize;
1222 1223 1224
        }
        *typestring_offset += 4;

1225 1226 1227
        if (is_string_type(attrs, type))
            write_string_tfs(file, NULL, type, NULL, typestring_offset);
        else if (processed(type->ref) || is_base_type(type->ref->type))
1228 1229 1230 1231 1232 1233 1234 1235
            write_pointer_tfs(file, type, typestring_offset);
        else
            error("write_pointer_description_offsets: type format string unknown\n");

        return 1;
    }

    if (is_array(type))
1236
    {
1237 1238
        return write_pointer_description_offsets(
            file, attrs, type->ref, offset_in_memory, offset_in_buffer,
1239
            typestring_offset);
1240 1241 1242 1243 1244
    }
    else if (is_non_complex_struct(type))
    {
        /* otherwise search for interesting fields to parse */
        const var_t *v;
1245
        LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1246
        {
1247 1248 1249 1250 1251 1252 1253 1254 1255
            if (offset_in_memory && offset_in_buffer)
            {
                size_t padding;
                align = 0;
                type_memsize(v->type, &align);
                padding = ROUNDING(*offset_in_memory, align);
                *offset_in_memory += padding;
                *offset_in_buffer += padding;
            }
1256 1257 1258
            written += write_pointer_description_offsets(
                file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
                typestring_offset);
1259
        }
1260
    }
1261 1262
    else
    {
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
        if (offset_in_memory && offset_in_buffer)
        {
            size_t memsize;
            align = 0;
            memsize = type_memsize(type, &align);
            *offset_in_memory += memsize;
            /* increment these separately as in the case of conformant (varying)
             * structures these start at different values */
            *offset_in_buffer += memsize;
        }
1273
    }
1274

1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
    return written;
}

/* Note: if file is NULL return value is number of pointers to write, else
 * it is the number of type format characters written */
static int write_fixed_array_pointer_descriptions(
    FILE *file, const attr_list_t *attrs, type_t *type,
    size_t *offset_in_memory, size_t *offset_in_buffer,
    unsigned int *typestring_offset)
{
    unsigned int align;
    int pointer_count = 0;

    if (type->type == RPC_FC_SMFARRAY || type->type == RPC_FC_LGFARRAY)
    {
        unsigned int temp = 0;
        /* unfortunately, this needs to be done in two passes to avoid
         * writing out redundant FC_FIXED_REPEAT descriptions */
        pointer_count = write_pointer_description_offsets(
            NULL, attrs, type->ref, NULL, NULL, &temp);
        if (pointer_count > 0)
        {
            unsigned int increment_size;
            size_t offset_of_array_pointer_mem = 0;
            size_t offset_of_array_pointer_buf = 0;

            align = 0;
            increment_size = type_memsize(type->ref, &align);

            print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", RPC_FC_FIXED_REPEAT);
            print_file(file, 2, "0x%02x, /* FC_PAD */\n", RPC_FC_PAD);
            print_file(file, 2, "NdrFcShort(0x%x), /* Iterations = %d */\n", type->dim, type->dim);
            print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
            print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
            print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
            *typestring_offset += 10;

            pointer_count = write_pointer_description_offsets(
                file, attrs, type, &offset_of_array_pointer_mem,
                &offset_of_array_pointer_buf, typestring_offset);
        }
    }
    else if (is_struct(type->type))
    {
        const var_t *v;
1320
        LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1321
        {
1322 1323 1324 1325 1326 1327 1328 1329 1330
            if (offset_in_memory && offset_in_buffer)
            {
                size_t padding;
                align = 0;
                type_memsize(v->type, &align);
                padding = ROUNDING(*offset_in_memory, align);
                *offset_in_memory += padding;
                *offset_in_buffer += padding;
            }
1331 1332 1333 1334 1335 1336 1337
            pointer_count += write_fixed_array_pointer_descriptions(
                file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
                typestring_offset);
        }
    }
    else
    {
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
        if (offset_in_memory && offset_in_buffer)
        {
            size_t memsize;
            align = 0;
            memsize = type_memsize(type, &align);
            *offset_in_memory += memsize;
            /* increment these separately as in the case of conformant (varying)
             * structures these start at different values */
            *offset_in_buffer += memsize;
        }
1348 1349 1350 1351 1352 1353 1354 1355 1356
    }

    return pointer_count;
}

/* Note: if file is NULL return value is number of pointers to write, else
 * it is the number of type format characters written */
static int write_conformant_array_pointer_descriptions(
    FILE *file, const attr_list_t *attrs, type_t *type,
1357
    size_t offset_in_memory, unsigned int *typestring_offset)
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
{
    unsigned int align;
    int pointer_count = 0;

    if (is_conformant_array(type) && !type->length_is)
    {
        unsigned int temp = 0;
        /* unfortunately, this needs to be done in two passes to avoid
         * writing out redundant FC_VARIABLE_REPEAT descriptions */
        pointer_count = write_pointer_description_offsets(
            NULL, attrs, type->ref, NULL, NULL, &temp);
        if (pointer_count > 0)
        {
            unsigned int increment_size;
1372 1373
            size_t offset_of_array_pointer_mem = offset_in_memory;
            size_t offset_of_array_pointer_buf = offset_in_memory;
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383

            align = 0;
            increment_size = type_memsize(type->ref, &align);

            if (increment_size > USHRT_MAX)
                error("array size of %u bytes is too large\n", increment_size);

            print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
            print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", RPC_FC_FIXED_OFFSET);
            print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
1384
            print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", offset_in_memory, offset_in_memory);
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
            print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
            *typestring_offset += 8;

            pointer_count = write_pointer_description_offsets(
                file, attrs, type->ref, &offset_of_array_pointer_mem,
                &offset_of_array_pointer_buf, typestring_offset);
        }
    }

    return pointer_count;
}

/* Note: if file is NULL return value is number of pointers to write, else
 * it is the number of type format characters written */
static int write_varying_array_pointer_descriptions(
    FILE *file, const attr_list_t *attrs, type_t *type,
    size_t *offset_in_memory, size_t *offset_in_buffer,
    unsigned int *typestring_offset)
{
    unsigned int align;
    int pointer_count = 0;

    if (is_array(type) && type->length_is)
    {
        unsigned int temp = 0;
        /* unfortunately, this needs to be done in two passes to avoid
         * writing out redundant FC_VARIABLE_REPEAT descriptions */
        pointer_count = write_pointer_description_offsets(
            NULL, attrs, type->ref, NULL, NULL, &temp);
        if (pointer_count > 0)
        {
            unsigned int increment_size;

            align = 0;
            increment_size = type_memsize(type->ref, &align);

            if (increment_size > USHRT_MAX)
                error("array size of %u bytes is too large\n", increment_size);

            print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", RPC_FC_VARIABLE_REPEAT);
            print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", RPC_FC_VARIABLE_OFFSET);
            print_file(file, 2, "NdrFcShort(0x%x), /* Increment = %d */\n", increment_size, increment_size);
            print_file(file, 2, "NdrFcShort(0x%x), /* Offset to array = %d */\n", *offset_in_memory, *offset_in_memory);
            print_file(file, 2, "NdrFcShort(0x%x), /* Number of pointers = %d */\n", pointer_count, pointer_count);
            *typestring_offset += 8;

            pointer_count = write_pointer_description_offsets(
1432 1433
                file, attrs, type, offset_in_memory,
                offset_in_buffer, typestring_offset);
1434 1435 1436 1437 1438
        }
    }
    else if (is_struct(type->type))
    {
        const var_t *v;
1439
        LIST_FOR_EACH_ENTRY( v, type->fields_or_args, const var_t, entry )
1440
        {
1441 1442 1443
            if (offset_in_memory && offset_in_buffer)
            {
                size_t padding;
1444 1445 1446 1447 1448 1449 1450 1451

                if (is_array(v->type) && v->type->length_is)
                {
                    *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
                    /* skip over variance and offset in buffer */
                    *offset_in_buffer += 8;
                }

1452 1453 1454 1455 1456 1457
                align = 0;
                type_memsize(v->type, &align);
                padding = ROUNDING(*offset_in_memory, align);
                *offset_in_memory += padding;
                *offset_in_buffer += padding;
            }
1458 1459 1460 1461 1462 1463 1464
            pointer_count += write_varying_array_pointer_descriptions(
                file, v->attrs, v->type, offset_in_memory, offset_in_buffer,
                typestring_offset);
        }
    }
    else
    {
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
        if (offset_in_memory && offset_in_buffer)
        {
            size_t memsize;
            align = 0;
            memsize = type_memsize(type, &align);
            *offset_in_memory += memsize;
            /* increment these separately as in the case of conformant (varying)
             * structures these start at different values */
            *offset_in_buffer += memsize;
        }
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
    }

    return pointer_count;
}

static void write_pointer_description(FILE *file, type_t *type,
                                      unsigned int *typestring_offset)
{
    size_t offset_in_buffer;
    size_t offset_in_memory;

    /* pass 1: search for single instance of a pointer (i.e. don't descend
     * into arrays) */
1488 1489 1490
    if (!is_array(type))
    {
        offset_in_memory = 0;
1491
        offset_in_buffer = 0;
1492 1493 1494 1495
        write_no_repeat_pointer_descriptions(
            file, type,
            &offset_in_memory, &offset_in_buffer, typestring_offset);
    }
1496 1497 1498

    /* pass 2: search for pointers in fixed arrays */
    offset_in_memory = 0;
1499
    offset_in_buffer = 0;
1500 1501 1502 1503 1504 1505
    write_fixed_array_pointer_descriptions(
        file, NULL, type,
        &offset_in_memory, &offset_in_buffer, typestring_offset);

    /* pass 3: search for pointers in conformant only arrays (but don't descend
     * into conformant varying or varying arrays) */
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
    if ((!type->declarray || !current_structure) && is_conformant_array(type))
        write_conformant_array_pointer_descriptions(
            file, NULL, type, 0, typestring_offset);
    else if (type->type == RPC_FC_CPSTRUCT)
    {
        unsigned int align = 0;
        type_t *carray = find_array_or_string_in_struct(type)->type;
        write_conformant_array_pointer_descriptions(
            file, NULL, carray,
            type_memsize(type, &align),
            typestring_offset);
    }
1518

1519
    /* pass 4: search for pointers in varying arrays */
1520
    offset_in_memory = 0;
1521
    offset_in_buffer = 0;
1522 1523 1524
    write_varying_array_pointer_descriptions(
            file, NULL, type,
            &offset_in_memory, &offset_in_buffer, typestring_offset);
1525 1526
}

1527
int is_declptr(const type_t *t)
1528 1529 1530 1531
{
  return is_ptr(t) || (is_conformant_array(t) && !t->declarray);
}

1532
static size_t write_string_tfs(FILE *file, const attr_list_t *attrs,
1533
                               type_t *type,
1534
                               const char *name, unsigned int *typestring_offset)
1535
{
1536
    size_t start_offset;
1537 1538
    unsigned char rtype;

1539
    if (is_declptr(type))
1540
    {
1541 1542 1543 1544
        unsigned char flag = is_conformant_array(type) ? 0 : RPC_FC_P_SIMPLEPOINTER;
        int pointer_type = is_ptr(type) ? type->type : get_attrv(attrs, ATTR_POINTERTYPE);
        if (!pointer_type)
            pointer_type = RPC_FC_RP;
1545
        print_start_tfs_comment(file, type, *typestring_offset);
1546 1547 1548 1549 1550 1551 1552 1553 1554
        print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
                   pointer_type, flag, string_of_type(pointer_type),
                   flag ? " [simple_pointer]" : "");
        *typestring_offset += 2;
        if (!flag)
        {
            print_file(file, 2, "NdrFcShort(0x2),\n");
            *typestring_offset += 2;
        }
1555
    }
1556

1557 1558 1559
    start_offset = *typestring_offset;
    update_tfsoff(type, start_offset, file);

1560
    rtype = type->ref->type;
1561

1562
    if ((rtype != RPC_FC_BYTE) && (rtype != RPC_FC_CHAR) && (rtype != RPC_FC_WCHAR))
Robert Shearman's avatar
Robert Shearman committed
1563
    {
1564
        error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
1565
        return start_offset;
Robert Shearman's avatar
Robert Shearman committed
1566 1567
    }

1568
    if (type->declarray && !is_conformant_array(type))
Robert Shearman's avatar
Robert Shearman committed
1569
    {
1570
        /* FIXME: multi-dimensional array */
1571 1572 1573
        if (0xffffuL < type->dim)
            error("array size for parameter %s exceeds %u bytes by %lu bytes\n",
                  name, 0xffffu, type->dim - 0xffffu);
Robert Shearman's avatar
Robert Shearman committed
1574

1575
        if (rtype == RPC_FC_CHAR)
1576
            WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
Robert Shearman's avatar
Robert Shearman committed
1577
        else
1578
            WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
Robert Shearman's avatar
Robert Shearman committed
1579
        print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1580
        *typestring_offset += 2;
Robert Shearman's avatar
Robert Shearman committed
1581

1582
        print_file(file, 2, "NdrFcShort(0x%x), /* %d */\n", type->dim, type->dim);
1583
        *typestring_offset += 2;
Robert Shearman's avatar
Robert Shearman committed
1584

1585
        return start_offset;
Robert Shearman's avatar
Robert Shearman committed
1586
    }
1587
    else if (type->size_is)
Robert Shearman's avatar
Robert Shearman committed
1588
    {
1589 1590
        unsigned int align = 0;

1591
        if (rtype == RPC_FC_CHAR)
1592
            WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
Robert Shearman's avatar
Robert Shearman committed
1593
        else
1594
            WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1595
        print_file(file, 2, "0x%x, /* FC_STRING_SIZED */\n", RPC_FC_STRING_SIZED);
1596
        *typestring_offset += 2;
Robert Shearman's avatar
Robert Shearman committed
1597

1598
        *typestring_offset += write_conf_or_var_desc(
1599
            file, current_structure,
1600 1601 1602
            (type->declarray && current_structure
             ? type_memsize(current_structure, &align)
             : 0),
1603
            type, type->size_is);
1604

1605
        return start_offset;
Robert Shearman's avatar
Robert Shearman committed
1606 1607 1608
    }
    else
    {
1609
        if (rtype == RPC_FC_WCHAR)
1610
            WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
1611 1612
        else
            WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
1613
        print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
1614
        *typestring_offset += 2;
Robert Shearman's avatar
Robert Shearman committed
1615

1616
        return start_offset;
Robert Shearman's avatar
Robert Shearman committed
1617
    }
1618 1619
}

1620
static size_t write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
1621
                              const char *name, unsigned int *typestring_offset)
1622
{
1623 1624 1625 1626
    const expr_t *length_is = type->length_is;
    const expr_t *size_is = type->size_is;
    unsigned int align = 0;
    size_t size;
1627
    size_t start_offset;
1628
    int has_pointer;
1629
    int pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
1630 1631 1632 1633 1634
    unsigned int baseoff
        = type->declarray && current_structure
        ? type_memsize(current_structure, &align)
        : 0;

1635 1636 1637
    if (!pointer_type)
        pointer_type = RPC_FC_RP;

1638
    if (write_embedded_types(file, attrs, type->ref, name, FALSE, typestring_offset))
1639
        has_pointer = TRUE;
1640 1641
    else
        has_pointer = type_has_pointers(type->ref);
Robert Shearman's avatar
Robert Shearman committed
1642

1643
    align = 0;
1644
    size = type_memsize((is_conformant_array(type) ? type->ref : type), &align);
1645

1646 1647
    start_offset = *typestring_offset;
    update_tfsoff(type, start_offset, file);
1648
    print_start_tfs_comment(file, type, start_offset);
1649 1650 1651
    print_file(file, 2, "0x%02x,\t/* %s */\n", type->type, string_of_type(type->type));
    print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
    *typestring_offset += 2;
1652

1653
    align = 0;
1654 1655 1656
    if (type->type != RPC_FC_BOGUS_ARRAY)
    {
        unsigned char tc = type->type;
1657

1658
        if (tc == RPC_FC_LGFARRAY || tc == RPC_FC_LGVARRAY)
Robert Shearman's avatar
Robert Shearman committed
1659
        {
1660 1661
            print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", size, size);
            *typestring_offset += 4;
Robert Shearman's avatar
Robert Shearman committed
1662
        }
1663
        else
Robert Shearman's avatar
Robert Shearman committed
1664
        {
1665
            print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", size, size);
1666
            *typestring_offset += 2;
1667
        }
Robert Shearman's avatar
Robert Shearman committed
1668

1669 1670
        if (is_conformant_array(type))
            *typestring_offset
1671
                += write_conf_or_var_desc(file, current_structure, baseoff,
1672
                                          type, size_is);
Robert Shearman's avatar
Robert Shearman committed
1673

1674
        if (type->type == RPC_FC_SMVARRAY || type->type == RPC_FC_LGVARRAY)
Robert Shearman's avatar
Robert Shearman committed
1675
        {
1676 1677
            unsigned int elalign = 0;
            size_t elsize = type_memsize(type->ref, &elalign);
1678

1679
            if (type->type == RPC_FC_LGVARRAY)
1680
            {
1681 1682
                print_file(file, 2, "NdrFcLong(0x%x),\t/* %lu */\n", type->dim, type->dim);
                *typestring_offset += 4;
1683
            }
1684
            else
1685
            {
1686 1687
                print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", type->dim, type->dim);
                *typestring_offset += 2;
1688
            }
Robert Shearman's avatar
Robert Shearman committed
1689

1690 1691
            print_file(file, 2, "NdrFcShort(0x%x),\t/* %lu */\n", elsize, elsize);
            *typestring_offset += 2;
Robert Shearman's avatar
Robert Shearman committed
1692 1693
        }

1694 1695
        if (length_is)
            *typestring_offset
1696
                += write_conf_or_var_desc(file, current_structure, baseoff,
1697
                                          type, length_is);
1698

1699
        if (has_pointer && (!type->declarray || !current_structure))
1700 1701 1702 1703
        {
            print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
            print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
            *typestring_offset += 2;
1704
            write_pointer_description(file, type, typestring_offset);
1705
            print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
1706
            *typestring_offset += 1;
Robert Shearman's avatar
Robert Shearman committed
1707
        }
1708

1709
        write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
1710
        write_end(file, typestring_offset);
Robert Shearman's avatar
Robert Shearman committed
1711
    }
1712
    else
1713 1714 1715 1716 1717 1718
    {
        unsigned int dim = size_is ? 0 : type->dim;
        print_file(file, 2, "NdrFcShort(0x%x),\t/* %u */\n", dim, dim);
        *typestring_offset += 2;
        *typestring_offset
            += write_conf_or_var_desc(file, current_structure, baseoff,
1719
                                      type, size_is);
1720 1721
        *typestring_offset
            += write_conf_or_var_desc(file, current_structure, baseoff,
1722
                                      type, length_is);
1723 1724 1725
        write_member_type(file, type, NULL, type->ref, NULL, typestring_offset);
        write_end(file, typestring_offset);
    }
1726 1727

    return start_offset;
1728
}
1729

1730
static const var_t *find_array_or_string_in_struct(const type_t *type)
1731
{
1732 1733 1734 1735 1736 1737 1738 1739
    const var_t *last_field;
    const type_t *ft;

    if (!type->fields_or_args || list_empty(type->fields_or_args))
        return NULL;

    last_field = LIST_ENTRY( list_tail(type->fields_or_args), const var_t, entry );
    ft = last_field->type;
1740

1741
    if (ft->declarray && is_conformant_array(ft))
1742 1743
        return last_field;

1744
    if (ft->type == RPC_FC_CSTRUCT || ft->type == RPC_FC_CPSTRUCT || ft->type == RPC_FC_CVSTRUCT)
1745
        return find_array_or_string_in_struct(ft);
1746 1747
    else
        return NULL;
1748 1749
}

1750 1751
static void write_struct_members(FILE *file, const type_t *type,
                                 unsigned int *corroff, unsigned int *typestring_offset)
1752
{
1753
    const var_t *field;
1754
    unsigned short offset = 0;
1755 1756
    int salign = -1;
    int padding;
1757

1758
    if (type->fields_or_args) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
1759 1760 1761
    {
        type_t *ft = field->type;
        if (!ft->declarray || !is_conformant_array(ft))
1762 1763 1764
        {
            unsigned int align = 0;
            size_t size = type_memsize(ft, &align);
1765 1766
            if (salign == -1)
                salign = align;
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
            if ((align - 1) & offset)
            {
                unsigned char fc = 0;
                switch (align)
                {
                case 4:
                    fc = RPC_FC_ALIGNM4;
                    break;
                case 8:
                    fc = RPC_FC_ALIGNM8;
                    break;
                default:
1779
                    error("write_struct_members: cannot align type %d\n", ft->type);
1780 1781
                }
                print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
1782
                offset = ROUND_SIZE(offset, align);
1783 1784
                *typestring_offset += 1;
            }
1785 1786
            write_member_type(file, type, field->attrs, field->type, corroff,
                              typestring_offset);
1787 1788
            offset += size;
        }
1789
    }
1790

1791
    padding = ROUNDING(offset, salign);
1792 1793 1794 1795 1796 1797 1798 1799
    if (padding)
    {
        print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
                   RPC_FC_STRUCTPAD1 + padding - 1,
                   padding);
        *typestring_offset += 1;
    }

1800
    write_end(file, typestring_offset);
1801 1802
}

1803
static size_t write_struct_tfs(FILE *file, type_t *type,
1804
                               const char *name, unsigned int *tfsoff)
1805
{
1806
    const type_t *save_current_structure = current_structure;
1807
    unsigned int total_size;
1808
    const var_t *array;
1809
    size_t start_offset;
1810
    size_t array_offset;
1811
    int has_pointers = 0;
1812
    unsigned int align = 0;
1813 1814
    unsigned int corroff;
    var_t *f;
1815

1816
    guard_rec(type);
1817
    current_structure = type;
1818

1819 1820 1821 1822
    total_size = type_memsize(type, &align);
    if (total_size > USHRT_MAX)
        error("structure size for %s exceeds %d bytes by %d bytes\n",
              name, USHRT_MAX, total_size - USHRT_MAX);
1823

1824
    if (type->fields_or_args) LIST_FOR_EACH_ENTRY(f, type->fields_or_args, var_t, entry)
1825 1826
        has_pointers |= write_embedded_types(file, f->attrs, f->type, f->name,
                                             FALSE, tfsoff);
1827
    if (!has_pointers) has_pointers = type_has_pointers(type);
1828

1829 1830 1831 1832
    array = find_array_or_string_in_struct(type);
    if (array && !processed(array->type))
        array_offset
            = is_attr(array->attrs, ATTR_STRING)
1833
            ? write_string_tfs(file, array->attrs, array->type, array->name, tfsoff)
1834
            : write_array_tfs(file, array->attrs, array->type, array->name, tfsoff);
1835 1836 1837

    corroff = *tfsoff;
    write_descriptors(file, type, tfsoff);
1838

1839 1840
    start_offset = *tfsoff;
    update_tfsoff(type, start_offset, file);
1841
    print_start_tfs_comment(file, type, start_offset);
1842 1843 1844 1845
    print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
    print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
    print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", total_size, total_size);
    *tfsoff += 4;
1846

1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
    if (array)
    {
        unsigned int absoff = array->type->typestring_offset;
        short reloff = absoff - *tfsoff;
        print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%lu) */\n",
                   reloff, reloff, absoff);
        *tfsoff += 2;
    }
    else if (type->type == RPC_FC_BOGUS_STRUCT)
    {
        print_file(file, 2, "NdrFcShort(0x0),\n");
        *tfsoff += 2;
    }
1860

1861 1862
    if (type->type == RPC_FC_BOGUS_STRUCT)
    {
1863 1864 1865
        /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
           nothing is written to file yet.  On the actual writing pass,
           this will have been updated.  */
1866 1867
        unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
        short reloff = absoff - *tfsoff;
1868
        print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
1869
                   reloff, reloff, absoff);
1870 1871
        *tfsoff += 2;
    }
1872 1873 1874
    else if ((type->type == RPC_FC_PSTRUCT) ||
             (type->type == RPC_FC_CPSTRUCT) ||
             (type->type == RPC_FC_CVSTRUCT && has_pointers))
1875 1876 1877 1878
    {
        print_file(file, 2, "0x%x, /* FC_PP */\n", RPC_FC_PP);
        print_file(file, 2, "0x%x, /* FC_PAD */\n", RPC_FC_PAD);
        *tfsoff += 2;
1879
        write_pointer_description(file, type, tfsoff);
1880 1881 1882
        print_file(file, 2, "0x%x, /* FC_END */\n", RPC_FC_END);
        *tfsoff += 1;
    }
1883

1884
    write_struct_members(file, type, &corroff, tfsoff);
1885

1886 1887
    if (type->type == RPC_FC_BOGUS_STRUCT)
    {
1888
        const var_list_t *fs = type->fields_or_args;
1889 1890 1891 1892 1893 1894 1895
        const var_t *f;

        type->ptrdesc = *tfsoff;
        if (fs) LIST_FOR_EACH_ENTRY(f, fs, const var_t, entry)
        {
            type_t *ft = f->type;
            if (is_ptr(ft))
1896 1897 1898 1899 1900 1901
            {
                if (is_string_type(f->attrs, ft))
                    write_string_tfs(file, f->attrs, ft, f->name, tfsoff);
                else
                    write_pointer_tfs(file, ft, tfsoff);
            }
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
            else if (!ft->declarray && is_conformant_array(ft))
            {
                unsigned int absoff = ft->typestring_offset;
                short reloff = absoff - (*tfsoff + 2);
                int ptr_type = get_attrv(f->attrs, ATTR_POINTERTYPE);
                /* FIXME: We need to store pointer attributes for arrays
                   so we don't lose pointer_default info.  */
                if (ptr_type == 0)
                    ptr_type = RPC_FC_UP;
                print_file(file, 0, "/* %d */\n", *tfsoff);
                print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
                           string_of_type(ptr_type));
                print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
                           reloff, reloff, absoff);
                *tfsoff += 4;
            }
1918
        }
1919 1920
        if (type->ptrdesc == *tfsoff)
            type->ptrdesc = 0;
1921 1922
    }

1923
    current_structure = save_current_structure;
1924
    return start_offset;
1925 1926
}

1927 1928 1929
static size_t write_pointer_only_tfs(FILE *file, const attr_list_t *attrs, int pointer_type,
                                     unsigned char flags, size_t offset,
                                     unsigned int *typeformat_offset)
1930
{
1931 1932
    size_t start_offset = *typeformat_offset;
    short reloff = offset - (*typeformat_offset + 2);
1933 1934 1935 1936
    int in_attr, out_attr;
    in_attr = is_attr(attrs, ATTR_IN);
    out_attr = is_attr(attrs, ATTR_OUT);
    if (!in_attr && !out_attr) in_attr = 1;
1937

1938 1939 1940
    if (out_attr && !in_attr && pointer_type == RPC_FC_RP)
        flags |= 0x04;

1941
    print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
1942
               pointer_type,
1943
               flags,
1944
               string_of_type(pointer_type));
1945 1946 1947 1948 1949 1950 1951 1952 1953
    if (file)
    {
        if (flags & 0x04)
            fprintf(file, " [allocated_on_stack]");
        if (flags & 0x10)
            fprintf(file, " [pointer_deref]");
        fprintf(file, " */\n");
    }

1954
    print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", reloff, offset);
1955
    *typeformat_offset += 4;
1956 1957

    return start_offset;
1958 1959
}

1960
static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
1961
{
1962 1963 1964 1965 1966
    if (t == NULL)
    {
        print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
    }
    else if (is_base_type(t->type))
1967 1968 1969 1970 1971 1972
    {
        print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
                   t->type, string_of_type(t->type));
    }
    else if (t->typestring_offset)
    {
1973
        short reloff = t->typestring_offset - *tfsoff;
1974 1975 1976 1977 1978 1979 1980 1981 1982
        print_file(file, 2, "NdrFcShort(0x%x),\t/* Offset= %d (%d) */\n",
                   reloff, reloff, t->typestring_offset);
    }
    else
        error("write_branch_type: type unimplemented (0x%x)\n", t->type);

    *tfsoff += 2;
}

1983
static size_t write_union_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
1984
{
1985 1986
    unsigned int align = 0;
    unsigned int start_offset;
1987
    size_t size = type_memsize(type, &align);
1988
    var_list_t *fields;
1989 1990 1991 1992 1993
    size_t nbranch = 0;
    type_t *deftype = NULL;
    short nodeftype = 0xffff;
    var_t *f;

1994 1995
    guard_rec(type);

1996 1997
    if (type->type == RPC_FC_ENCAPSULATED_UNION)
    {
1998 1999
        const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
        fields = uv->type->fields_or_args;
2000 2001
    }
    else
2002
        fields = type->fields_or_args;
2003

2004 2005 2006 2007 2008
    if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
    {
        expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
        if (cases)
            nbranch += list_count(cases);
2009 2010
        if (f->type)
            write_embedded_types(file, f->attrs, f->type, f->name, TRUE, tfsoff);
2011 2012 2013
    }

    start_offset = *tfsoff;
2014
    update_tfsoff(type, start_offset, file);
2015
    print_start_tfs_comment(file, type, start_offset);
2016 2017
    if (type->type == RPC_FC_ENCAPSULATED_UNION)
    {
2018
        const var_t *sv = LIST_ENTRY(list_head(type->fields_or_args), const var_t, entry);
2019 2020
        const type_t *st = sv->type;

2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
        switch (st->type)
        {
        case RPC_FC_CHAR:
        case RPC_FC_SMALL:
        case RPC_FC_USMALL:
        case RPC_FC_SHORT:
        case RPC_FC_USHORT:
        case RPC_FC_LONG:
        case RPC_FC_ULONG:
        case RPC_FC_ENUM16:
        case RPC_FC_ENUM32:
            print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
            print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
                       0x40 | st->type, string_of_type(st->type));
            *tfsoff += 2;
            break;
        default:
            error("union switch type must be an integer, char, or enum\n");
        }
2040
    }
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
    else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
    {
        static const expr_t dummy_expr;  /* FIXME */
        const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);

        switch (st->type)
        {
        case RPC_FC_CHAR:
        case RPC_FC_SMALL:
        case RPC_FC_USMALL:
        case RPC_FC_SHORT:
        case RPC_FC_USHORT:
        case RPC_FC_LONG:
        case RPC_FC_ULONG:
        case RPC_FC_ENUM16:
        case RPC_FC_ENUM32:
            print_file(file, 2, "0x%x,\t/* %s */\n", type->type, string_of_type(type->type));
            print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
                       st->type, string_of_type(st->type));
            *tfsoff += 2;
            break;
        default:
            error("union switch type must be an integer, char, or enum\n");
        }

        *tfsoff += write_conf_or_var_desc(file, NULL, *tfsoff, st, &dummy_expr );
    }

2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
    print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", size, size);
    print_file(file, 2, "NdrFcShort(0x%x),\t/* %d */\n", nbranch, nbranch);
    *tfsoff += 4;

    if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
    {
        type_t *ft = f->type;
        expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
        int deflt = is_attr(f->attrs, ATTR_DEFAULT);
        expr_t *c;

        if (cases == NULL && !deflt)
            error("union field %s with neither case nor default attribute\n", f->name);

        if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
        {
            /* MIDL doesn't check for duplicate cases, even though that seems
               like a reasonable thing to do, it just dumps them to the TFS
               like we're going to do here.  */
            print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
            *tfsoff += 4;
            write_branch_type(file, ft, tfsoff);
        }

        /* MIDL allows multiple default branches, even though that seems
           illogical, it just chooses the last one, which is what we will
           do.  */
        if (deflt)
        {
            deftype = ft;
            nodeftype = 0;
        }
    }

    if (deftype)
    {
        write_branch_type(file, deftype, tfsoff);
    }
    else
    {
        print_file(file, 2, "NdrFcShort(0x%x),\n", nodeftype);
        *tfsoff += 2;
    }

    return start_offset;
2114 2115
}

2116 2117
static size_t write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
                           unsigned int *typeformat_offset)
2118 2119 2120
{
    size_t i;
    size_t start_offset = *typeformat_offset;
2121
    expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
2122

2123 2124 2125 2126
    if (iid)
    {
        print_file(file, 2, "0x2f,  /* FC_IP */\n");
        print_file(file, 2, "0x5c,  /* FC_PAD */\n");
2127
        *typeformat_offset
2128
            += write_conf_or_var_desc(file, NULL, 0, type, iid) + 2;
2129 2130 2131
    }
    else
    {
2132
        const type_t *base = is_ptr(type) ? type->ref : type;
2133
        const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
2134

2135 2136
        if (! uuid)
            error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
2137

2138
        update_tfsoff(type, start_offset, file);
2139
        print_start_tfs_comment(file, type, start_offset);
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
        print_file(file, 2, "0x2f,\t/* FC_IP */\n");
        print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
        print_file(file, 2, "NdrFcLong(0x%08lx),\n", uuid->Data1);
        print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
        print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
        for (i = 0; i < 8; ++i)
            print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);

        if (file)
            fprintf(file, "\n");
2150

2151 2152
        *typeformat_offset += 18;
    }
2153 2154 2155
    return start_offset;
}

2156 2157 2158 2159 2160
static size_t write_contexthandle_tfs(FILE *file, const type_t *type,
                                      const var_t *var,
                                      unsigned int *typeformat_offset)
{
    size_t start_offset = *typeformat_offset;
2161 2162 2163
    unsigned char flags = 0;

    if (is_attr(current_iface->attrs, ATTR_STRICTCONTEXTHANDLE))
2164
        flags |= NDR_STRICT_CONTEXT_HANDLE;
2165 2166 2167 2168

    if (is_ptr(type))
        flags |= 0x80;
    if (is_attr(var->attrs, ATTR_IN))
2169
    {
2170
        flags |= 0x40;
2171
        if (!is_attr(var->attrs, ATTR_OUT))
2172
            flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
2173
    }
2174 2175 2176 2177 2178
    if (is_attr(var->attrs, ATTR_OUT))
        flags |= 0x20;

    WRITE_FCTYPE(file, FC_BIND_CONTEXT, *typeformat_offset);
    print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
2179 2180
    /* return and can't be null values overlap */
    if (((flags & 0x21) != 0x21) && (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL))
2181
        print_file(file, 0, "can't be null, ");
2182
    if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
2183
        print_file(file, 0, "serialize, ");
2184
    if (flags & NDR_CONTEXT_HANDLE_NO_SERIALIZE)
2185
        print_file(file, 0, "no serialize, ");
2186
    if (flags & NDR_STRICT_CONTEXT_HANDLE)
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
        print_file(file, 0, "strict, ");
    if ((flags & 0x21) == 0x20)
        print_file(file, 0, "out, ");
    if ((flags & 0x21) == 0x21)
        print_file(file, 0, "return, ");
    if (flags & 0x40)
        print_file(file, 0, "in, ");
    if (flags & 0x80)
        print_file(file, 0, "via ptr, ");
    print_file(file, 0, "*/\n");
    print_file(file, 2, "0, /* FIXME: rundown routine index*/\n");
    print_file(file, 2, "0, /* FIXME: param num */\n");
    *typeformat_offset += 4;

    return start_offset;
}

2204
static size_t write_typeformatstring_var(FILE *file, int indent, const func_t *func,
2205 2206
                                         type_t *type, const var_t *var,
                                         unsigned int *typeformat_offset)
2207
{
2208
    size_t offset;
2209

2210 2211 2212
    if (is_context_handle(type))
        return write_contexthandle_tfs(file, type, var, typeformat_offset);

2213 2214 2215 2216 2217 2218
    if (is_user_type(type))
    {
        write_user_tfs(file, type, typeformat_offset);
        return type->typestring_offset;
    }

2219
    if (is_string_type(var->attrs, type))
2220
        return write_string_tfs(file, var->attrs, type, var->name, typeformat_offset);
2221

2222
    if (is_array(type))
2223
    {
2224
        int ptr_type;
2225 2226
        size_t off;
        off = write_array_tfs(file, var->attrs, type, var->name, typeformat_offset);
2227
        ptr_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
2228
        /* Top level pointers to conformant arrays may be handled specially
Austin English's avatar
Austin English committed
2229
           since we can bypass the pointer, but if the array is buried
2230 2231 2232 2233 2234 2235
           beneath another pointer (e.g., "[size_is(,n)] int **p" then we
           always need to write the pointer.  */
        if (!ptr_type && var->type != type)
          /* FIXME:  This should use pointer_default, but the information
             isn't kept around for arrays.  */
          ptr_type = RPC_FC_UP;
2236
        if (ptr_type && ptr_type != RPC_FC_RP)
2237 2238 2239 2240 2241
        {
            unsigned int absoff = type->typestring_offset;
            short reloff = absoff - (*typeformat_offset + 2);
            off = *typeformat_offset;
            print_file(file, 0, "/* %d */\n", off);
2242 2243
            print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
                       string_of_type(ptr_type));
2244 2245 2246 2247 2248 2249
            print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
                       reloff, reloff, absoff);
            *typeformat_offset += 4;
        }
        return off;
    }
2250

2251 2252 2253 2254 2255
    if (!is_ptr(type))
    {
        /* basic types don't need a type format string */
        if (is_base_type(type->type))
            return 0;
2256

2257
        switch (type->type)
2258
        {
2259 2260 2261 2262 2263 2264 2265 2266 2267
        case RPC_FC_STRUCT:
        case RPC_FC_PSTRUCT:
        case RPC_FC_CSTRUCT:
        case RPC_FC_CPSTRUCT:
        case RPC_FC_CVSTRUCT:
        case RPC_FC_BOGUS_STRUCT:
            return write_struct_tfs(file, type, var->name, typeformat_offset);
        case RPC_FC_ENCAPSULATED_UNION:
        case RPC_FC_NON_ENCAPSULATED_UNION:
2268
            return write_union_tfs(file, type, typeformat_offset);
2269 2270 2271 2272 2273 2274
        case RPC_FC_IGNORE:
        case RPC_FC_BIND_PRIMITIVE:
            /* nothing to do */
            return 0;
        default:
            error("write_typeformatstring_var: Unsupported type 0x%x for variable %s\n", type->type, var->name);
2275
        }
2276 2277 2278 2279 2280 2281 2282
    }
    else if (last_ptr(type))
    {
        size_t start_offset = *typeformat_offset;
        int in_attr = is_attr(var->attrs, ATTR_IN);
        int out_attr = is_attr(var->attrs, ATTR_OUT);
        const type_t *base = type->ref;
2283

2284 2285 2286
        if (base->type == RPC_FC_IP
            || (base->type == 0
                && is_attr(var->attrs, ATTR_IIDIS)))
2287
        {
2288
            return write_ip_tfs(file, var->attrs, type, typeformat_offset);
2289
        }
2290

2291 2292
        /* special case for pointers to base types */
        if (is_base_type(base->type))
2293
        {
2294
            print_file(file, indent, "0x%x, 0x%x,    /* %s %s[simple_pointer] */\n",
2295 2296
                       type->type, (!in_attr && out_attr) ? 0x0C : 0x08,
                       string_of_type(type->type),
2297 2298 2299 2300 2301
                       (!in_attr && out_attr) ? "[allocated_on_stack] " : "");
            print_file(file, indent, "0x%02x,    /* %s */\n", base->type, string_of_type(base->type));
            print_file(file, indent, "0x5c,          /* FC_PAD */\n");
            *typeformat_offset += 4;
            return start_offset;
2302
        }
2303
    }
2304 2305 2306 2307 2308 2309

    assert(is_ptr(type));

    offset = write_typeformatstring_var(file, indent, func, type->ref, var, typeformat_offset);
    if (file)
        fprintf(file, "/* %2u */\n", *typeformat_offset);
2310
    return write_pointer_only_tfs(file, var->attrs, type->type,
2311 2312
                           !last_ptr(type) ? 0x10 : 0,
                           offset, typeformat_offset);
2313 2314
}

2315
static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
2316
                                const char *name, int write_ptr, unsigned int *tfsoff)
2317
{
2318
    int retmask = 0;
2319

2320 2321 2322 2323
    if (is_user_type(type))
    {
        write_user_tfs(file, type, tfsoff);
    }
2324 2325 2326 2327
    else if (is_string_type(attrs, type))
    {
        write_string_tfs(file, attrs, type, name, tfsoff);
    }
2328
    else if (is_ptr(type))
2329 2330 2331
    {
        type_t *ref = type->ref;

2332 2333 2334
        if (ref->type == RPC_FC_IP
            || (ref->type == 0
                && is_attr(attrs, ATTR_IIDIS)))
2335
        {
2336
            write_ip_tfs(file, attrs, type, tfsoff);
2337 2338 2339 2340 2341
        }
        else
        {
            if (!processed(ref) && !is_base_type(ref->type))
                retmask |= write_embedded_types(file, NULL, ref, name, TRUE, tfsoff);
2342

2343 2344
            if (write_ptr)
                write_pointer_tfs(file, type, tfsoff);
2345

2346 2347
            retmask |= 1;
        }
2348
    }
2349 2350
    else if (type->declarray && is_conformant_array(type))
        ;    /* conformant arrays and strings are handled specially */
2351 2352
    else if (is_array(type))
    {
2353
        write_array_tfs(file, attrs, type, name, tfsoff);
2354 2355
        if (is_conformant_array(type))
            retmask |= 1;
2356
    }
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
    else if (is_struct(type->type))
    {
        if (!processed(type))
            write_struct_tfs(file, type, name, tfsoff);
    }
    else if (is_union(type->type))
    {
        if (!processed(type))
            write_union_tfs(file, type, tfsoff);
    }
2367 2368 2369 2370 2371
    else if (!is_base_type(type->type))
        error("write_embedded_types: unknown embedded type for %s (0x%x)\n",
              name, type->type);

    return retmask;
2372 2373
}

2374 2375
static size_t process_tfs_stmts(FILE *file, const statement_list_t *stmts,
                                type_pred_t pred, unsigned int *typeformat_offset)
2376 2377
{
    const var_t *var;
2378
    const statement_t *stmt;
2379

2380
    if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
2381
    {
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
        const type_t *iface;
        if (stmt->type == STMT_LIBRARY)
        {
            process_tfs_stmts(file, stmt->u.lib->stmts, pred, typeformat_offset);
            continue;
        }
        else if (stmt->type != STMT_TYPE || stmt->u.type->type != RPC_FC_IP)
            continue;

        iface = stmt->u.type;
        if (!pred(iface))
2393 2394
            continue;

2395
        if (iface->funcs)
2396
        {
2397
            const func_t *func;
2398
            current_iface = iface;
2399
            LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
2400
            {
2401
                if (is_local(func->def->attrs)) continue;
2402

2403 2404 2405 2406 2407
                if (!is_void(get_func_return_type(func)))
                {
                    var_t v = *func->def;
                    v.type = get_func_return_type(func);
                    update_tfsoff(get_func_return_type(func),
2408
                                  write_typeformatstring_var(
2409
                                      file, 2, NULL, get_func_return_type(func),
2410
                                      &v, typeformat_offset),
2411
                                  file);
2412
                }
2413

2414
                current_func = func;
2415
                if (func->args)
2416
                    LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
2417 2418 2419 2420
                        update_tfsoff(
                            var->type,
                            write_typeformatstring_var(
                                file, 2, func, var->type, var,
2421
                                typeformat_offset),
2422
                            file);
2423 2424 2425 2426
            }
        }
    }

2427 2428 2429 2430 2431 2432 2433 2434
    return *typeformat_offset + 1;
}

static size_t process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
{
    unsigned int typeformat_offset = 2;

    return process_tfs_stmts(file, stmts, pred, &typeformat_offset);
2435 2436 2437
}


2438
void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
{
    int indent = 0;

    print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
    print_file(file, indent, "{\n");
    indent++;
    print_file(file, indent, "0,\n");
    print_file(file, indent, "{\n");
    indent++;
    print_file(file, indent, "NdrFcShort(0x0),\n");

2450
    set_all_tfswrite(TRUE);
2451
    process_tfs(file, stmts, pred);
2452

2453 2454 2455 2456 2457 2458 2459 2460
    print_file(file, indent, "0x0\n");
    indent--;
    print_file(file, indent, "}\n");
    indent--;
    print_file(file, indent, "};\n");
    print_file(file, indent, "\n");
}

2461
static unsigned int get_required_buffer_size_type(
2462
    const type_t *type, const char *name, unsigned int *alignment)
2463
{
2464 2465 2466
    const char *uname;
    const type_t *utype;

2467
    *alignment = 0;
2468
    if ((utype = get_user_type(type, &uname)))
2469
    {
2470
        return get_required_buffer_size_type(utype, uname, alignment);
2471
    }
2472
    else
2473
    {
2474
        switch (type->type)
2475
        {
2476 2477
        case RPC_FC_BYTE:
        case RPC_FC_CHAR:
2478 2479
        case RPC_FC_USMALL:
        case RPC_FC_SMALL:
2480
            *alignment = 4;
2481
            return 1;
2482

2483 2484 2485
        case RPC_FC_WCHAR:
        case RPC_FC_USHORT:
        case RPC_FC_SHORT:
2486
        case RPC_FC_ENUM16:
2487
            *alignment = 4;
2488
            return 2;
2489

2490 2491
        case RPC_FC_ULONG:
        case RPC_FC_LONG:
2492
        case RPC_FC_ENUM32:
2493 2494
        case RPC_FC_FLOAT:
        case RPC_FC_ERROR_STATUS_T:
2495
            *alignment = 4;
2496
            return 4;
2497

2498 2499
        case RPC_FC_HYPER:
        case RPC_FC_DOUBLE:
2500
            *alignment = 8;
2501
            return 8;
2502

2503 2504 2505 2506
        case RPC_FC_IGNORE:
        case RPC_FC_BIND_PRIMITIVE:
            return 0;

2507
        case RPC_FC_STRUCT:
2508
        case RPC_FC_PSTRUCT:
2509
        {
2510
            size_t size = 0;
2511
            const var_t *field;
2512 2513
            if (!type->fields_or_args) return 0;
            LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
2514 2515
            {
                unsigned int alignment;
2516 2517
                size += get_required_buffer_size_type(field->type, field->name,
                                                      &alignment);
2518
            }
2519
            return size;
2520 2521
        }

2522
        case RPC_FC_RP:
2523 2524 2525 2526
            return
                is_base_type( type->ref->type ) || type->ref->type == RPC_FC_STRUCT
                ? get_required_buffer_size_type( type->ref, name, alignment )
                : 0;
2527 2528 2529

        case RPC_FC_SMFARRAY:
        case RPC_FC_LGFARRAY:
2530
            return type->dim * get_required_buffer_size_type(type->ref, name, alignment);
2531

2532 2533
        default:
            return 0;
2534
        }
2535 2536 2537
    }
}

2538
static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
2539
{
2540 2541
    int in_attr = is_attr(var->attrs, ATTR_IN);
    int out_attr = is_attr(var->attrs, ATTR_OUT);
2542
    const type_t *t;
2543 2544 2545 2546

    if (!in_attr && !out_attr)
        in_attr = 1;

2547 2548
    *alignment = 0;

2549 2550 2551 2552 2553 2554 2555
    for (t = var->type; is_ptr(t); t = t->ref)
        if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
        {
            *alignment = 4;
            return 20;
        }

2556
    if (pass == PASS_OUT)
2557
    {
2558
        if (out_attr && is_ptr(var->type))
2559 2560 2561
        {
            type_t *type = var->type;

2562 2563 2564 2565
            if (type->type == RPC_FC_STRUCT)
            {
                const var_t *field;
                unsigned int size = 36;
2566

2567 2568
                if (!type->fields_or_args) return size;
                LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
2569 2570 2571
                {
                    unsigned int align;
                    size += get_required_buffer_size_type(
2572
                        field->type, field->name, &align);
2573 2574 2575
                }
                return size;
            }
2576
        }
2577
        return 0;
2578
    }
2579 2580
    else
    {
2581 2582
        if ((!out_attr || in_attr) && !var->type->size_is
            && !is_attr(var->attrs, ATTR_STRING) && !var->type->declarray)
2583
        {
2584
            if (is_ptr(var->type))
2585 2586
            {
                type_t *type = var->type;
2587

2588 2589 2590 2591 2592 2593 2594 2595
                if (is_base_type(type->type))
                {
                    return 25;
                }
                else if (type->type == RPC_FC_STRUCT)
                {
                    unsigned int size = 36;
                    const var_t *field;
2596

2597 2598
                    if (!type->fields_or_args) return size;
                    LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
2599 2600 2601
                    {
                        unsigned int align;
                        size += get_required_buffer_size_type(
2602
                            field->type, field->name, &align);
2603 2604 2605 2606 2607 2608
                    }
                    return size;
                }
            }
        }

2609
        return get_required_buffer_size_type(var->type, var->name, alignment);
2610
    }
2611 2612
}

2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626
static unsigned int get_function_buffer_size( const func_t *func, enum pass pass )
{
    const var_t *var;
    unsigned int total_size = 0, alignment;

    if (func->args)
    {
        LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
        {
            total_size += get_required_buffer_size(var, &alignment, pass);
            total_size += alignment;
        }
    }

2627
    if (pass == PASS_OUT && !is_void(get_func_return_type(func)))
2628
    {
2629 2630 2631
        var_t v = *func->def;
        v.type = get_func_return_type(func);
        total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
2632 2633 2634 2635 2636
        total_size += alignment;
    }
    return total_size;
}

2637
static void print_phase_function(FILE *file, int indent, const char *type,
2638
                                 const char *local_var_prefix, enum remoting_phase phase,
2639
                                 const var_t *var, unsigned int type_offset)
2640
{
2641
    const char *function;
2642 2643 2644
    switch (phase)
    {
    case PHASE_BUFFERSIZE:
2645 2646
        function = "BufferSize";
        break;
2647
    case PHASE_MARSHAL:
2648 2649
        function = "Marshall";
        break;
2650
    case PHASE_UNMARSHAL:
2651 2652
        function = "Unmarshall";
        break;
2653
    case PHASE_FREE:
2654 2655 2656 2657 2658
        function = "Free";
        break;
    default:
        assert(0);
        return;
2659
    }
2660 2661 2662

    print_file(file, indent, "Ndr%s%s(\n", type, function);
    indent++;
2663
    print_file(file, indent, "&__frame->_StubMsg,\n");
2664
    print_file(file, indent, "%s%s%s%s%s,\n",
2665 2666
               (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
               (phase == PHASE_UNMARSHAL || decl_indirect(var->type)) ? "&" : "",
2667
               local_var_prefix,
2668 2669
               (phase == PHASE_UNMARSHAL && decl_indirect(var->type)) ? "_p_" : "",
               var->name);
2670 2671 2672 2673 2674
    print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
               type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
    if (phase == PHASE_UNMARSHAL)
        print_file(file, indent, "0);\n");
    indent--;
2675 2676
}

2677 2678
void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
                          enum remoting_phase phase, enum pass pass, const var_t *var,
2679 2680
                          const char *varname)
{
2681
    type_t *type = var->type;
2682 2683
    unsigned int size;
    unsigned int alignment = 0;
2684
    unsigned char rtype;
2685 2686 2687 2688 2689

    /* no work to do for other phases, buffer sizing is done elsewhere */
    if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
        return;

2690
    rtype = is_ptr(type) ? type->ref->type : type->type;
2691

2692
    switch (rtype)
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704
    {
        case RPC_FC_BYTE:
        case RPC_FC_CHAR:
        case RPC_FC_SMALL:
        case RPC_FC_USMALL:
            size = 1;
            alignment = 1;
            break;

        case RPC_FC_WCHAR:
        case RPC_FC_USHORT:
        case RPC_FC_SHORT:
2705
        case RPC_FC_ENUM16:
2706 2707 2708 2709 2710 2711
            size = 2;
            alignment = 2;
            break;

        case RPC_FC_ULONG:
        case RPC_FC_LONG:
2712
        case RPC_FC_ENUM32:
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
        case RPC_FC_FLOAT:
        case RPC_FC_ERROR_STATUS_T:
            size = 4;
            alignment = 4;
            break;

        case RPC_FC_HYPER:
        case RPC_FC_DOUBLE:
            size = 8;
            alignment = 8;
            break;

        case RPC_FC_IGNORE:
        case RPC_FC_BIND_PRIMITIVE:
            /* no marshalling needed */
            return;

        default:
2731
            error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n", var->name, rtype);
2732 2733 2734
            size = 0;
    }

2735
    if (phase == PHASE_MARSHAL)
2736 2737
        print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (long)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
    print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((long)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
2738 2739 2740 2741 2742
                alignment - 1, alignment - 1);

    if (phase == PHASE_MARSHAL)
    {
        print_file(file, indent, "*(");
2743
        write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2744
        if (is_ptr(type))
2745
            fprintf(file, " *)__frame->_StubMsg.Buffer = *");
2746
        else
2747
            fprintf(file, " *)__frame->_StubMsg.Buffer = ");
2748
        fprintf(file, "%s%s", local_var_prefix, varname);
2749 2750 2751 2752
        fprintf(file, ";\n");
    }
    else if (phase == PHASE_UNMARSHAL)
    {
2753
        print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
2754
        write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2755
        fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
2756 2757 2758
        print_file(file, indent, "{\n");
        print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
        print_file(file, indent, "}\n");
2759 2760 2761 2762
        if (pass == PASS_IN || pass == PASS_RETURN)
            print_file(file, indent, "");
        else
            print_file(file, indent, "*");
2763
        fprintf(file, "%s%s", local_var_prefix, varname);
2764
        if (pass == PASS_IN && is_ptr(type))
2765 2766 2767
            fprintf(file, " = (");
        else
            fprintf(file, " = *(");
2768
        write_type_decl(file, is_ptr(type) ? type->ref : type, NULL);
2769
        fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
2770 2771
    }

2772
    print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
2773
    write_type_decl(file, var->type, NULL);
2774 2775 2776
    fprintf(file, ");\n");
}

2777 2778
/* returns whether the MaxCount, Offset or ActualCount members need to be
 * filled in for the specified phase */
2779
static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
2780 2781 2782 2783
{
    return (phase != PHASE_UNMARSHAL);
}

2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
expr_t *get_size_is_expr(const type_t *t, const char *name)
{
    expr_t *x = NULL;

    for ( ; is_ptr(t) || is_array(t); t = t->ref)
        if (t->size_is)
        {
            if (!x)
                x = t->size_is;
            else
                error("%s: multidimensional conformant"
                      " arrays not supported at the top level\n",
                      name);
        }

    return x;
}

2802 2803
static void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
                                              enum remoting_phase phase, const var_t *var)
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
{
    const type_t *type = var->type;
    /* get fundamental type for the argument */
    for (;;)
    {
        if (is_attr(type->attrs, ATTR_WIREMARSHAL))
            break;
        else if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
            break;
        else if (is_array(type) || is_string_type(var->attrs, type))
        {
            if (is_conformance_needed_for_phase(phase))
            {
                if (type->size_is)
                {
2819
                    print_file(file, indent, "__frame->_StubMsg.MaxCount = (unsigned long)");
2820
                    write_expr(file, type->size_is, 1, 1, NULL, NULL, local_var_prefix);
2821 2822 2823 2824
                    fprintf(file, ";\n\n");
                }
                if (type->length_is)
                {
2825
                    print_file(file, indent, "__frame->_StubMsg.Offset = (unsigned long)0;\n"); /* FIXME */
2826 2827 2828
                    print_file(file, indent, "__frame->_StubMsg.ActualCount = (unsigned long)");
                    write_expr(file, type->length_is, 1, 1, NULL, NULL, local_var_prefix);
                    fprintf(file, ";\n\n");
2829 2830 2831 2832 2833 2834 2835 2836
                }
            }
            break;
        }
        else if (type->type == RPC_FC_NON_ENCAPSULATED_UNION)
        {
            if (is_conformance_needed_for_phase(phase))
            {
2837
                print_file(file, indent, "__frame->_StubMsg.MaxCount = (unsigned long)");
2838
                write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
                fprintf(file, ";\n\n");
            }
            break;
        }
        else if (type->type == RPC_FC_IP)
        {
            expr_t *iid;

            if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
            {
2849
                print_file( file, indent, "__frame->_StubMsg.MaxCount = (unsigned long) " );
2850
                write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861
                fprintf( file, ";\n\n" );
            }
            break;
        }
        else if (is_ptr(type))
            type = type->ref;
        else
            break;
    }
}

2862 2863
static void write_remoting_arg(FILE *file, int indent, const func_t *func, const char *local_var_prefix,
                               enum pass pass, enum remoting_phase phase, const var_t *var)
2864
{
2865
    int in_attr, out_attr, pointer_type;
2866 2867 2868
    const type_t *type = var->type;
    unsigned char rtype;
    size_t start_offset = type->typestring_offset;
2869

2870 2871 2872
    pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
    if (!pointer_type)
        pointer_type = RPC_FC_RP;
2873

2874 2875 2876 2877
    in_attr = is_attr(var->attrs, ATTR_IN);
    out_attr = is_attr(var->attrs, ATTR_OUT);
    if (!in_attr && !out_attr)
        in_attr = 1;
2878

2879
    if (phase != PHASE_FREE)
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890
        switch (pass)
        {
        case PASS_IN:
            if (!in_attr) return;
            break;
        case PASS_OUT:
            if (!out_attr) return;
            break;
        case PASS_RETURN:
            break;
        }
2891

2892
    write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var);
2893
    rtype = type->type;
2894

2895 2896 2897
    if (is_context_handle(type))
    {
        if (phase == PHASE_MARSHAL)
2898
        {
2899 2900
            if (pass == PASS_IN)
            {
2901 2902 2903 2904
                /* if the context_handle attribute appears in the chain of types
                 * without pointers being followed, then the context handle must
                 * be direct, otherwise it is a pointer */
                int is_ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
2905
                print_file(file, indent, "NdrClientContextMarshall(\n");
2906
                print_file(file, indent + 1, "&__frame->_StubMsg,\n");
2907
                print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", is_ch_ptr ? "*" : "", local_var_prefix, var->name);
2908 2909 2910 2911
                print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
            }
            else
            {
2912
                print_file(file, indent, "NdrServerContextNewMarshall(\n");
2913
                print_file(file, indent + 1, "&__frame->_StubMsg,\n");
2914
                print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
2915 2916
                print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->type));
                print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
2917 2918 2919 2920 2921 2922
            }
        }
        else if (phase == PHASE_UNMARSHAL)
        {
            if (pass == PASS_OUT)
            {
2923
                if (!in_attr)
2924
                    print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
2925
                print_file(file, indent, "NdrClientContextUnmarshall(\n");
2926
                print_file(file, indent + 1, "&__frame->_StubMsg,\n");
2927
                print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s,\n", local_var_prefix, var->name);
2928
                print_file(file, indent + 1, "__frame->_Handle);\n");
2929 2930
            }
            else
2931
            {
2932
                print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
2933
                print_file(file, indent + 1, "&__frame->_StubMsg,\n");
2934 2935
                print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
            }
2936
        }
2937 2938 2939
    }
    else if (is_user_type(var->type))
    {
2940
        print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
2941 2942 2943 2944
    }
    else if (is_string_type(var->attrs, var->type))
    {
        if (is_array(type) && !is_conformant_array(type))
2945 2946
            print_phase_function(file, indent, "NonConformantString", local_var_prefix,
                                 phase, var, start_offset);
2947
        else
2948
        {
2949
            if (phase == PHASE_FREE || pass == PASS_RETURN || pointer_type == RPC_FC_UP)
2950
                print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
2951
                                     start_offset - (type->size_is ? 4 : 2));
2952
            else
2953 2954
                print_phase_function(file, indent, "ConformantString", local_var_prefix,
                                     phase, var, start_offset);
2955 2956 2957 2958 2959 2960
        }
    }
    else if (is_array(type))
    {
        unsigned char tc = type->type;
        const char *array_type = "FixedArray";
2961

2962 2963 2964 2965 2966 2967 2968 2969
        /* We already have the size_is expression since it's at the
           top level, but do checks for multidimensional conformant
           arrays.  When we handle them, we'll need to extend this
           function to return a list, and then we'll actually use
           the return value.  */
        get_size_is_expr(type, var->name);

        if (tc == RPC_FC_SMVARRAY || tc == RPC_FC_LGVARRAY)
2970
        {
2971
            array_type = "VaryingArray";
2972
        }
2973
        else if (tc == RPC_FC_CARRAY)
2974
        {
2975
            array_type = "ConformantArray";
2976
        }
2977
        else if (tc == RPC_FC_CVARRAY || tc == RPC_FC_BOGUS_ARRAY)
2978
        {
2979 2980 2981
            array_type = (tc == RPC_FC_BOGUS_ARRAY
                          ? "ComplexArray"
                          : "ConformantVaryingArray");
2982
        }
2983 2984

        if (pointer_type != RPC_FC_RP) array_type = "Pointer";
2985
        print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
2986
        if (phase == PHASE_FREE && pointer_type == RPC_FC_RP)
2987
        {
2988
            /* these are all unmarshalled by allocating memory */
2989 2990
            if (type->type == RPC_FC_BOGUS_ARRAY ||
                type->type == RPC_FC_CVARRAY ||
2991 2992
                ((type->type == RPC_FC_SMVARRAY || type->type == RPC_FC_LGVARRAY) && in_attr) ||
                (type->type == RPC_FC_CARRAY && !in_attr))
2993
            {
2994
                print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
2995
                indent++;
2996
                print_file(file, indent, "__frame->_StubMsg.pfnFree(%s%s);\n", local_var_prefix, var->name);
2997 2998
            }
        }
2999 3000 3001
    }
    else if (!is_ptr(var->type) && is_base_type(rtype))
    {
3002
        if (phase != PHASE_FREE)
3003
            print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3004 3005 3006 3007
    }
    else if (!is_ptr(var->type))
    {
        switch (rtype)
3008
        {
3009 3010
        case RPC_FC_STRUCT:
        case RPC_FC_PSTRUCT:
3011
            print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
3012 3013 3014
            break;
        case RPC_FC_CSTRUCT:
        case RPC_FC_CPSTRUCT:
3015
            print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
3016 3017
            break;
        case RPC_FC_CVSTRUCT:
3018
            print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
3019 3020
            break;
        case RPC_FC_BOGUS_STRUCT:
3021
            print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
3022 3023 3024
            break;
        case RPC_FC_RP:
            if (is_base_type( var->type->ref->type ))
3025
            {
3026
                print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3027
            }
3028
            else if (var->type->ref->type == RPC_FC_STRUCT)
3029 3030
            {
                if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
3031
                    print_phase_function(file, indent, local_var_prefix, "SimpleStruct", phase, var, start_offset + 4);
3032
            }
3033
            else
3034
            {
3035
                expr_t *iid;
3036
                if ((iid = get_attrp( var->attrs, ATTR_IIDIS )))
3037
                {
3038
                    print_file( file, indent, "__frame->_StubMsg.MaxCount = (unsigned long) " );
3039
                    write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
3040 3041
                    fprintf( file, ";\n\n" );
                }
3042
                print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052
            }
            break;
        default:
            error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, rtype);
        }
    }
    else
    {
        if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && is_base_type(rtype))
        {
3053
            if (phase != PHASE_FREE)
3054
                print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
3055 3056 3057 3058
        }
        else if (last_ptr(var->type) && (pointer_type == RPC_FC_RP) && (rtype == RPC_FC_STRUCT))
        {
            if (phase != PHASE_BUFFERSIZE && phase != PHASE_FREE)
3059
                print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset + 4);
3060 3061 3062 3063
        }
        else
        {
            if (var->type->ref->type == RPC_FC_IP)
3064
                print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
3065
            else
3066
                print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
3067
        }
3068 3069 3070 3071
    }
    fprintf(file, "\n");
}

3072
void write_remoting_arguments(FILE *file, int indent, const func_t *func, const char *local_var_prefix,
3073 3074 3075 3076 3077
                              enum pass pass, enum remoting_phase phase)
{
    if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
    {
        unsigned int size = get_function_buffer_size( func, pass );
3078
        print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
3079 3080 3081 3082 3083 3084
    }

    if (pass == PASS_RETURN)
    {
        var_t var;
        var = *func->def;
3085
        var.type = get_func_return_type(func);
3086
        var.name = xstrdup( "_RetVal" );
3087
        write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, &var );
3088 3089 3090 3091 3092 3093 3094 3095
        free( var.name );
    }
    else
    {
        const var_t *var;
        if (!func->args)
            return;
        LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3096
            write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
3097 3098
    }
}
3099 3100


3101
size_t get_size_procformatstring_type(const char *name, const type_t *type, const attr_list_t *attrs)
3102
{
3103
    return write_procformatstring_type(NULL, 0, name, type, attrs, FALSE);
3104 3105 3106
}


3107 3108 3109 3110 3111 3112 3113 3114
size_t get_size_procformatstring_func(const func_t *func)
{
    const var_t *var;
    size_t size = 0;

    /* argument list size */
    if (func->args)
        LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
3115
            size += get_size_procformatstring_type(var->name, var->type, var->attrs);
3116 3117

    /* return value size */
3118
    if (is_void(get_func_return_type(func)))
3119 3120
        size += 2; /* FC_END and FC_PAD */
    else
3121
        size += get_size_procformatstring_type("return value", get_func_return_type(func), NULL);
3122 3123 3124 3125

    return size;
}

3126
size_t get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
3127
{
3128
    const statement_t *stmt;
3129
    size_t size = 1;
3130
    const func_t *func;
3131

3132
    if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
3133
    {
3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
        const type_t *iface;
        if (stmt->type == STMT_LIBRARY)
        {
            size += get_size_procformatstring(stmt->u.lib->stmts, pred) - 1;
            continue;
        }
        else if (stmt->type != STMT_TYPE && stmt->u.type->type != RPC_FC_IP)
            continue;

        iface = stmt->u.type;
        if (!pred(iface))
3145 3146
            continue;

3147 3148
        if (iface->funcs)
            LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
3149 3150
                if (!is_local(func->def->attrs))
                    size += get_size_procformatstring_func( func );
3151 3152 3153 3154
    }
    return size;
}

3155
size_t get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
3156
{
3157
    set_all_tfswrite(FALSE);
3158
    return process_tfs(NULL, stmts, pred);
3159 3160
}

3161 3162 3163 3164 3165 3166 3167
void declare_stub_args( FILE *file, int indent, const func_t *func )
{
    int in_attr, out_attr;
    int i = 0;
    const var_t *var;

    /* declare return value '_RetVal' */
3168
    if (!is_void(get_func_return_type(func)))
3169 3170
    {
        print_file(file, indent, "");
3171
        write_type_decl_left(file, get_func_return_type(func));
3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186
        fprintf(file, " _RetVal;\n");
    }

    if (!func->args)
        return;

    LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
    {
        int is_string = is_attr(var->attrs, ATTR_STRING);

        in_attr = is_attr(var->attrs, ATTR_IN);
        out_attr = is_attr(var->attrs, ATTR_OUT);
        if (!out_attr && !in_attr)
            in_attr = 1;

3187 3188 3189
        if (is_context_handle(var->type))
            print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
        else
3190
        {
3191 3192 3193
            if (!in_attr && !var->type->size_is && !is_string)
            {
                print_file(file, indent, "");
3194 3195
                write_type_decl(file, var->type->declarray ? var->type : var->type->ref,
                                "_W%u", i++);
3196 3197 3198
                fprintf(file, ";\n");
            }

3199
            print_file(file, indent, "");
3200
            write_type_decl_left(file, var->type);
3201 3202
            fprintf(file, " ");
            if (var->type->declarray) {
3203
                fprintf(file, "(*%s)", var->name);
3204
            } else
3205
                fprintf(file, "%s", var->name);
3206
            write_type_right(file, var->type, FALSE);
3207
            fprintf(file, ";\n");
3208

3209
            if (decl_indirect(var->type))
3210
                print_file(file, indent, "void *_p_%s;\n", var->name);
3211
        }
3212 3213 3214 3215
    }
}


3216
void assign_stub_out_args( FILE *file, int indent, const func_t *func, const char *local_var_prefix )
3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234
{
    int in_attr, out_attr;
    int i = 0, sep = 0;
    const var_t *var;

    if (!func->args)
        return;

    LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
    {
        int is_string = is_attr(var->attrs, ATTR_STRING);
        in_attr = is_attr(var->attrs, ATTR_IN);
        out_attr = is_attr(var->attrs, ATTR_OUT);
        if (!out_attr && !in_attr)
            in_attr = 1;

        if (!in_attr)
        {
3235
            print_file(file, indent, "%s%s", local_var_prefix, var->name);
3236

3237 3238 3239
            if (is_context_handle(var->type))
            {
                fprintf(file, " = NdrContextHandleInitialize(\n");
3240
                print_file(file, indent + 1, "&__frame->_StubMsg,\n");
3241 3242 3243 3244
                print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
                           var->type->typestring_offset);
            }
            else if (var->type->size_is)
3245
            {
3246
                unsigned int size, align = 0;
3247 3248
                type_t *type = var->type;

3249
                fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
3250
                for ( ; type->size_is ; type = type->ref)
3251
                {
3252
                    write_expr(file, type->size_is, TRUE, TRUE, NULL, NULL, local_var_prefix);
3253 3254
                    fprintf(file, " * ");
                }
3255
                size = type_memsize(type, &align);
3256
                fprintf(file, "%u);\n", size);
3257 3258 3259
            }
            else if (!is_string)
            {
3260
                fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
3261
                if (is_ptr(var->type) && !last_ptr(var->type))
3262
                    print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273
                i++;
            }

            sep = 1;
        }
    }
    if (sep)
        fprintf(file, "\n");
}


3274 3275
int write_expr_eval_routines(FILE *file, const char *iface)
{
3276
    static const char *var_name = "pS";
3277
    static const char *var_name_expr = "pS->";
3278 3279 3280 3281 3282 3283
    int result = 0;
    struct expr_eval_routine *eval;
    unsigned short callback_offset = 0;

    LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
    {
3284
        const char *name = eval->structure->name;
3285
        result = 1;
3286 3287 3288 3289 3290

        print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
                   iface, name, callback_offset);
        print_file(file, 0, "{\n");
        print_file (file, 1, "%s *%s = (%s *)(pStubMsg->StackTop - %u);\n",
3291
                    name, var_name, name, eval->baseoff);
3292 3293
        print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
        print_file(file, 1, "pStubMsg->MaxCount = (unsigned long)");
3294
        write_expr(file, eval->expr, 1, 1, var_name_expr, eval->structure, "");
3295
        fprintf(file, ";\n");
3296
        print_file(file, 0, "}\n\n");
3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312
        callback_offset++;
    }
    return result;
}

void write_expr_eval_routine_list(FILE *file, const char *iface)
{
    struct expr_eval_routine *eval;
    struct expr_eval_routine *cursor;
    unsigned short callback_offset = 0;

    fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
    fprintf(file, "{\n");

    LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
    {
3313 3314
        const char *name = eval->structure->name;
        print_file(file, 1, "%s_%sExprEval_%04u,\n", iface, name, callback_offset);
3315 3316 3317 3318 3319 3320 3321
        callback_offset++;
        list_remove(&eval->entry);
        free(eval);
    }

    fprintf(file, "};\n\n");
}
3322

3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335
void write_user_quad_list(FILE *file)
{
    user_type_t *ut;

    if (list_empty(&user_type_list))
        return;

    fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
    fprintf(file, "{\n");
    LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
    {
        const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
        print_file(file, 1, "{\n");
3336 3337 3338 3339
        print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
        print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
        print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
        print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
3340 3341 3342 3343
        print_file(file, 1, "}%s\n", sep);
    }
    fprintf(file, "};\n\n");
}
3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377

void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
{
    const struct str_list_entry_t *endpoint;
    const char *p;

    /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
    print_file( f, 0, "static const unsigned char * %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
    LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
    {
        print_file( f, 1, "{ (const unsigned char *)\"" );
        for (p = endpoint->str; *p && *p != ':'; p++)
        {
            if (*p == '"' || *p == '\\') fputc( '\\', f );
            fputc( *p, f );
        }
        if (!*p) goto error;
        if (p[1] != '[') goto error;

        fprintf( f, "\", (const unsigned char *)\"" );
        for (p += 2; *p && *p != ']'; p++)
        {
            if (*p == '"' || *p == '\\') fputc( '\\', f );
            fputc( *p, f );
        }
        if (*p != ']') goto error;
        fprintf( f, "\" },\n" );
    }
    print_file( f, 0, "};\n\n" );
    return;

error:
    error("Invalid endpoint syntax '%s'\n", endpoint->str);
}
3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390

void write_exceptions( FILE *file )
{
    fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
    fprintf( file, "\n");
    fprintf( file, "#include \"wine/exception.h\"\n");
    fprintf( file, "#undef RpcTryExcept\n");
    fprintf( file, "#undef RpcExcept\n");
    fprintf( file, "#undef RpcEndExcept\n");
    fprintf( file, "#undef RpcTryFinally\n");
    fprintf( file, "#undef RpcFinally\n");
    fprintf( file, "#undef RpcEndFinally\n");
    fprintf( file, "#undef RpcExceptionCode\n");
3391
    fprintf( file, "#undef RpcAbnormalTermination\n");
3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
    fprintf( file, "\n");
    fprintf( file, "struct __exception_frame;\n");
    fprintf( file, "typedef int (*__filter_func)(EXCEPTION_RECORD *, struct __exception_frame *);\n");
    fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
    fprintf( file, "\n");
    fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
    fprintf( file, "    EXCEPTION_REGISTRATION_RECORD frame; \\\n");
    fprintf( file, "    __filter_func                 filter; \\\n");
    fprintf( file, "    __finally_func                finally; \\\n");
    fprintf( file, "    sigjmp_buf                    jmp; \\\n");
    fprintf( file, "    DWORD                         code; \\\n");
3403
    fprintf( file, "    unsigned char                 abnormal_termination; \\\n");
3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
    fprintf( file, "    unsigned char                 filter_level; \\\n");
    fprintf( file, "    unsigned char                 finally_level;\n");
    fprintf( file, "\n");
    fprintf( file, "struct __exception_frame\n{\n");
    fprintf( file, "    __DECL_EXCEPTION_FRAME;\n");
    fprintf( file, "};\n");
    fprintf( file, "\n");
    fprintf( file, "static DWORD __widl_exception_handler( EXCEPTION_RECORD *record,\n");
    fprintf( file, "                                       EXCEPTION_REGISTRATION_RECORD *frame,\n");
    fprintf( file, "                                       CONTEXT *context,\n");
    fprintf( file, "                                       EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
    fprintf( file, "{\n");
    fprintf( file, "    struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
    fprintf( file, "\n");
    fprintf( file, "    if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
    fprintf( file, "    {\n" );
    fprintf( file, "        if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
3421 3422
    fprintf( file, "        {\n" );
    fprintf( file, "            exc_frame->abnormal_termination = 1;\n");
3423
    fprintf( file, "            exc_frame->finally( exc_frame );\n");
3424
    fprintf( file, "        }\n" );
3425 3426 3427 3428 3429 3430 3431 3432
    fprintf( file, "        return ExceptionContinueSearch;\n");
    fprintf( file, "    }\n" );
    fprintf( file, "    exc_frame->code = record->ExceptionCode;\n");
    fprintf( file, "    if (exc_frame->filter_level && exc_frame->filter( record, exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
    fprintf( file, "    {\n");
    fprintf( file, "        __wine_rtl_unwind( frame, record );\n");
    fprintf( file, "        if (exc_frame->finally_level > exc_frame->filter_level)\n" );
    fprintf( file, "        {\n");
3433
    fprintf( file, "            exc_frame->abnormal_termination = 1;\n");
3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472
    fprintf( file, "            exc_frame->finally( exc_frame );\n");
    fprintf( file, "            __wine_pop_frame( frame );\n");
    fprintf( file, "        }\n");
    fprintf( file, "        exc_frame->filter_level = 0;\n");
    fprintf( file, "        siglongjmp( exc_frame->jmp, 1 );\n");
    fprintf( file, "    }\n");
    fprintf( file, "    return ExceptionContinueSearch;\n");
    fprintf( file, "}\n");
    fprintf( file, "\n");
    fprintf( file, "#define RpcTryExcept \\\n");
    fprintf( file, "    if (!sigsetjmp( __frame->jmp, 0 )) \\\n");
    fprintf( file, "    { \\\n");
    fprintf( file, "        if (!__frame->finally_level) \\\n" );
    fprintf( file, "            __wine_push_frame( &__frame->frame ); \\\n");
    fprintf( file, "        __frame->filter_level = __frame->finally_level + 1;\n" );
    fprintf( file, "\n");
    fprintf( file, "#define RpcExcept(expr) \\\n");
    fprintf( file, "        if (!__frame->finally_level) \\\n" );
    fprintf( file, "            __wine_pop_frame( &__frame->frame ); \\\n");
    fprintf( file, "        __frame->filter_level = 0; \\\n" );
    fprintf( file, "    } \\\n");
    fprintf( file, "    else \\\n");
    fprintf( file, "\n");
    fprintf( file, "#define RpcEndExcept\n");
    fprintf( file, "\n");
    fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
    fprintf( file, "\n");
    fprintf( file, "#define RpcTryFinally \\\n");
    fprintf( file, "    if (!__frame->filter_level) \\\n");
    fprintf( file, "        __wine_push_frame( &__frame->frame ); \\\n");
    fprintf( file, "    __frame->finally_level = __frame->filter_level + 1;\n");
    fprintf( file, "\n");
    fprintf( file, "#define RpcFinally \\\n");
    fprintf( file, "    if (!__frame->filter_level) \\\n");
    fprintf( file, "        __wine_pop_frame( &__frame->frame ); \\\n");
    fprintf( file, "    __frame->finally_level = 0;\n");
    fprintf( file, "\n");
    fprintf( file, "#define RpcEndFinally\n");
    fprintf( file, "\n");
3473 3474
    fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
    fprintf( file, "\n");
3475 3476 3477 3478 3479
    fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
    fprintf( file, "    do { \\\n");
    fprintf( file, "        __frame->frame.Handler = __widl_exception_handler; \\\n");
    fprintf( file, "        __frame->filter = (__filter_func)(filter_func); \\\n" );
    fprintf( file, "        __frame->finally = (__finally_func)(finally_func); \\\n");
3480
    fprintf( file, "        __frame->abnormal_termination = 0; \\\n");
3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491
    fprintf( file, "        __frame->filter_level = 0; \\\n");
    fprintf( file, "        __frame->finally_level = 0; \\\n");
    fprintf( file, "    } while (0)\n");
    fprintf( file, "\n");
    fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
    fprintf( file, "\n");
    fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) do {} while(0)\n");
    fprintf( file, "#define __DECL_EXCEPTION_FRAME\n");
    fprintf( file, "\n");
    fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");
}