blob: 2c1017b9f2b9fc67382086ae3a0245c7b27de00d [file] [log] [blame]
/*
* Format String Generator for IDL Compiler
*
* Copyright 2005-2006 Eric Kohl
* Copyright 2005-2006 Robert Shearman
*
* 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
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include "widl.h"
#include "utils.h"
#include "parser.h"
#include "header.h"
#include "typetree.h"
#include "typegen.h"
#include "expr.h"
/* 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)))
static const type_t *current_structure;
static const var_t *current_func;
static const type_t *current_iface;
static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
struct expr_eval_routine
{
struct list entry;
const type_t *iface;
const type_t *cont_type;
char *name;
unsigned int baseoff;
const expr_t *expr;
};
enum type_context
{
TYPE_CONTEXT_TOPLEVELPARAM,
TYPE_CONTEXT_PARAM,
TYPE_CONTEXT_CONTAINER,
TYPE_CONTEXT_CONTAINER_NO_POINTERS,
TYPE_CONTEXT_RETVAL,
};
/* parameter flags in Oif mode */
static const unsigned short MustSize = 0x0001;
static const unsigned short MustFree = 0x0002;
static const unsigned short IsPipe = 0x0004;
static const unsigned short IsIn = 0x0008;
static const unsigned short IsOut = 0x0010;
static const unsigned short IsReturn = 0x0020;
static const unsigned short IsBasetype = 0x0040;
static const unsigned short IsByValue = 0x0080;
static const unsigned short IsSimpleRef = 0x0100;
/* static const unsigned short IsDontCallFreeInst = 0x0200; */
/* static const unsigned short SaveForAsyncFinish = 0x0400; */
static unsigned int field_memsize(const type_t *type, unsigned int *offset);
static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
const char *name, unsigned int *typestring_offset);
static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
const char *name, int write_ptr, unsigned int *tfsoff);
static const var_t *find_array_or_string_in_struct(const type_t *type);
static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
type_t *type, enum type_context context,
const char *name, unsigned int *typestring_offset);
static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
const attr_list_t *attrs, int toplevel_param,
unsigned int *alignment );
static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
static const char *string_of_type(unsigned char type)
{
switch (type)
{
case FC_BYTE: return "FC_BYTE";
case FC_CHAR: return "FC_CHAR";
case FC_SMALL: return "FC_SMALL";
case FC_USMALL: return "FC_USMALL";
case FC_WCHAR: return "FC_WCHAR";
case FC_SHORT: return "FC_SHORT";
case FC_USHORT: return "FC_USHORT";
case FC_LONG: return "FC_LONG";
case FC_ULONG: return "FC_ULONG";
case FC_FLOAT: return "FC_FLOAT";
case FC_HYPER: return "FC_HYPER";
case FC_DOUBLE: return "FC_DOUBLE";
case FC_ENUM16: return "FC_ENUM16";
case FC_ENUM32: return "FC_ENUM32";
case FC_IGNORE: return "FC_IGNORE";
case FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
case FC_RP: return "FC_RP";
case FC_UP: return "FC_UP";
case FC_OP: return "FC_OP";
case FC_FP: return "FC_FP";
case FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
case FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
case FC_STRUCT: return "FC_STRUCT";
case FC_PSTRUCT: return "FC_PSTRUCT";
case FC_CSTRUCT: return "FC_CSTRUCT";
case FC_CPSTRUCT: return "FC_CPSTRUCT";
case FC_CVSTRUCT: return "FC_CVSTRUCT";
case FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
case FC_SMFARRAY: return "FC_SMFARRAY";
case FC_LGFARRAY: return "FC_LGFARRAY";
case FC_SMVARRAY: return "FC_SMVARRAY";
case FC_LGVARRAY: return "FC_LGVARRAY";
case FC_CARRAY: return "FC_CARRAY";
case FC_CVARRAY: return "FC_CVARRAY";
case FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
case FC_ALIGNM2: return "FC_ALIGNM2";
case FC_ALIGNM4: return "FC_ALIGNM4";
case FC_ALIGNM8: return "FC_ALIGNM8";
case FC_POINTER: return "FC_POINTER";
case FC_C_CSTRING: return "FC_C_CSTRING";
case FC_C_WSTRING: return "FC_C_WSTRING";
case FC_CSTRING: return "FC_CSTRING";
case FC_WSTRING: return "FC_WSTRING";
case FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
case FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
case FC_REPRESENT_AS: return "FC_REPRESENT_AS";
case FC_IP: return "FC_IP";
case FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
case FC_BIND_GENERIC: return "FC_BIND_GENERIC";
case FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
case FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
case FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
case FC_STRUCTPAD1: return "FC_STRUCTPAD1";
case FC_STRUCTPAD2: return "FC_STRUCTPAD2";
case FC_STRUCTPAD3: return "FC_STRUCTPAD3";
case FC_STRUCTPAD4: return "FC_STRUCTPAD4";
case FC_STRUCTPAD5: return "FC_STRUCTPAD5";
case FC_STRUCTPAD6: return "FC_STRUCTPAD6";
case FC_STRUCTPAD7: return "FC_STRUCTPAD7";
case FC_STRING_SIZED: return "FC_STRING_SIZED";
case FC_NO_REPEAT: return "FC_NO_REPEAT";
case FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
case FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
case FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
case FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
case FC_PP: return "FC_PP";
case FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
case FC_DEREFERENCE: return "FC_DEREFERENCE";
case FC_DIV_2: return "FC_DIV_2";
case FC_MULT_2: return "FC_MULT_2";
case FC_ADD_1: return "FC_ADD_1";
case FC_SUB_1: return "FC_SUB_1";
case FC_CALLBACK: return "FC_CALLBACK";
case FC_CONSTANT_IID: return "FC_CONSTANT_IID";
case FC_END: return "FC_END";
case FC_PAD: return "FC_PAD";
case FC_USER_MARSHAL: return "FC_USER_MARSHAL";
case FC_RANGE: return "FC_RANGE";
case FC_INT3264: return "FC_INT3264";
case FC_UINT3264: return "FC_UINT3264";
default:
error("string_of_type: unknown type 0x%02x\n", type);
return NULL;
}
}
static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
{
const type_t *t = type;
for (;;)
{
if (is_attr(t->attrs, attr))
return get_attrp(t->attrs, attr);
else if (type_is_alias(t))
t = type_alias_get_aliasee_type(t);
else return NULL;
}
}
unsigned char get_basic_fc(const type_t *type)
{
int sign = type_basic_get_sign(type);
switch (type_basic_get_type(type))
{
case TYPE_BASIC_INT8: return (sign <= 0 ? FC_SMALL : FC_USMALL);
case TYPE_BASIC_INT16: return (sign <= 0 ? FC_SHORT : FC_USHORT);
case TYPE_BASIC_INT32:
case TYPE_BASIC_LONG: return (sign <= 0 ? FC_LONG : FC_ULONG);
case TYPE_BASIC_INT64: return FC_HYPER;
case TYPE_BASIC_INT: return (sign <= 0 ? FC_LONG : FC_ULONG);
case TYPE_BASIC_INT3264: return (sign <= 0 ? FC_INT3264 : FC_UINT3264);
case TYPE_BASIC_BYTE: return FC_BYTE;
case TYPE_BASIC_CHAR: return FC_CHAR;
case TYPE_BASIC_WCHAR: return FC_WCHAR;
case TYPE_BASIC_HYPER: return FC_HYPER;
case TYPE_BASIC_FLOAT: return FC_FLOAT;
case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
}
return 0;
}
static unsigned char get_basic_fc_signed(const type_t *type)
{
switch (type_basic_get_type(type))
{
case TYPE_BASIC_INT8: return FC_SMALL;
case TYPE_BASIC_INT16: return FC_SHORT;
case TYPE_BASIC_INT32: return FC_LONG;
case TYPE_BASIC_INT64: return FC_HYPER;
case TYPE_BASIC_INT: return FC_LONG;
case TYPE_BASIC_INT3264: return FC_INT3264;
case TYPE_BASIC_LONG: return FC_LONG;
case TYPE_BASIC_BYTE: return FC_BYTE;
case TYPE_BASIC_CHAR: return FC_CHAR;
case TYPE_BASIC_WCHAR: return FC_WCHAR;
case TYPE_BASIC_HYPER: return FC_HYPER;
case TYPE_BASIC_FLOAT: return FC_FLOAT;
case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
}
return 0;
}
static inline unsigned int clamp_align(unsigned int align)
{
unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
if(align > packing) align = packing;
return align;
}
unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
{
const type_t *t;
int pointer_type;
assert(is_ptr(type) || is_array(type));
pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
if (pointer_type)
return pointer_type;
for (t = type; type_is_alias(t); t = type_alias_get_aliasee_type(t))
{
pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
if (pointer_type)
return pointer_type;
}
if (toplevel_param)
return FC_RP;
if ((pointer_type = get_attrv(current_iface->attrs, ATTR_POINTERDEFAULT)))
return pointer_type;
return FC_UP;
}
static unsigned char get_pointer_fc_context( const type_t *type, const attr_list_t *attrs,
enum type_context context )
{
int pointer_fc = get_pointer_fc(type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM);
if (pointer_fc == FC_UP && is_attr( attrs, ATTR_OUT ) &&
(context == TYPE_CONTEXT_PARAM || context == TYPE_CONTEXT_RETVAL) && is_object( current_iface ))
pointer_fc = FC_OP;
return pointer_fc;
}
static unsigned char get_enum_fc(const type_t *type)
{
assert(type_get_type(type) == TYPE_ENUM);
if (is_aliaschain_attr(type, ATTR_V1ENUM))
return FC_ENUM32;
else
return FC_ENUM16;
}
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 (type_is_alias(t))
t = type_alias_get_aliasee_type(t);
else
return NULL;
}
}
static int is_user_type(const type_t *t)
{
return get_user_type(t, NULL) != NULL;
}
enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
{
if (is_user_type(type))
return TGT_USER_TYPE;
if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
return TGT_CTXT_HANDLE;
if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
return TGT_STRING;
switch (type_get_type(type))
{
case TYPE_BASIC:
if (!(flags & TDT_IGNORE_RANGES) &&
(is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
return TGT_RANGE;
return TGT_BASIC;
case TYPE_ENUM:
if (!(flags & TDT_IGNORE_RANGES) &&
(is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
return TGT_RANGE;
return TGT_ENUM;
case TYPE_POINTER:
if (type_get_type(type_pointer_get_ref_type(type)) == TYPE_INTERFACE ||
type_get_type(type_pointer_get_ref_type(type)) == TYPE_RUNTIMECLASS ||
type_get_type(type_pointer_get_ref_type(type)) == TYPE_DELEGATE ||
(type_get_type(type_pointer_get_ref_type(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
return TGT_IFACE_POINTER;
else if (is_aliaschain_attr(type_pointer_get_ref_type(type), ATTR_CONTEXTHANDLE))
return TGT_CTXT_HANDLE_POINTER;
else
return TGT_POINTER;
case TYPE_STRUCT:
return TGT_STRUCT;
case TYPE_ENCAPSULATED_UNION:
case TYPE_UNION:
return TGT_UNION;
case TYPE_ARRAY:
return TGT_ARRAY;
case TYPE_FUNCTION:
case TYPE_COCLASS:
case TYPE_INTERFACE:
case TYPE_MODULE:
case TYPE_VOID:
case TYPE_ALIAS:
case TYPE_BITFIELD:
case TYPE_RUNTIMECLASS:
case TYPE_DELEGATE:
break;
case TYPE_APICONTRACT:
case TYPE_PARAMETERIZED_TYPE:
case TYPE_PARAMETER:
/* not supposed to be here */
assert(0);
break;
}
return TGT_INVALID;
}
static int cant_be_null(const var_t *v)
{
switch (typegen_detect_type(v->declspec.type, v->attrs, TDT_IGNORE_STRINGS))
{
case TGT_ARRAY:
if (!type_array_is_decl_as_ptr( v->declspec.type )) return 0;
/* fall through */
case TGT_POINTER:
return (get_pointer_fc(v->declspec.type, v->attrs, TRUE) == FC_RP);
case TGT_CTXT_HANDLE_POINTER:
return TRUE;
default:
return 0;
}
}
static int get_padding(const var_list_t *fields)
{
unsigned short offset = 0;
unsigned 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->declspec.type;
unsigned int align = 0;
unsigned int size = type_memsize_and_alignment(ft, &align);
align = clamp_align(align);
if (align > salign) salign = align;
offset = ROUND_SIZE(offset, align);
offset += size;
}
return ROUNDING(offset, salign);
}
static unsigned int get_stack_size( const var_t *var, int *by_value )
{
unsigned int stack_size;
int by_val;
switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
{
case TGT_BASIC:
case TGT_ENUM:
case TGT_RANGE:
case TGT_STRUCT:
case TGT_UNION:
case TGT_USER_TYPE:
stack_size = type_memsize( var->declspec.type );
by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
break;
default:
by_val = 0;
break;
}
if (!by_val) stack_size = pointer_size;
if (by_value) *by_value = by_val;
return ROUND_SIZE( stack_size, pointer_size );
}
static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
const type_t *type, int is_return )
{
unsigned char flags = 0;
int is_out;
if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
if (is_ptr(type) &&
!is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
!is_attr( attrs, ATTR_CONTEXTHANDLE ))
flags |= HANDLE_PARAM_IS_VIA_PTR;
if (is_return) return flags | HANDLE_PARAM_IS_OUT | HANDLE_PARAM_IS_RETURN;
is_out = is_attr(attrs, ATTR_OUT);
if (is_attr(attrs, ATTR_IN) || !is_out)
{
flags |= HANDLE_PARAM_IS_IN;
if (!is_out) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
}
if (is_out) flags |= HANDLE_PARAM_IS_OUT;
return flags;
}
static unsigned int get_rpc_flags( const attr_list_t *attrs )
{
unsigned int flags = 0;
if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
return flags;
}
unsigned char get_struct_fc(const type_t *type)
{
int has_pointer = 0;
int has_conformance = 0;
int has_variance = 0;
var_t *field;
var_list_t *fields;
fields = type_struct_get_fields(type);
if (get_padding(fields))
return FC_BOGUS_STRUCT;
if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
{
type_t *t = field->declspec.type;
enum typegen_type typegen_type;
typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
{
if (is_string_type(field->attrs, field->declspec.type))
{
if (is_conformant_array(t))
has_conformance = 1;
has_variance = 1;
continue;
}
if (is_array(type_array_get_element_type(field->declspec.type)))
return FC_BOGUS_STRUCT;
if (type_array_has_conformance(field->declspec.type))
{
has_conformance = 1;
if (list_next(fields, &field->entry))
error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
field->name);
}
if (type_array_has_variance(t))
has_variance = 1;
t = type_array_get_element_type(t);
typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
}
switch (typegen_type)
{
case TGT_USER_TYPE:
case TGT_IFACE_POINTER:
return FC_BOGUS_STRUCT;
case TGT_BASIC:
if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
return FC_BOGUS_STRUCT;
break;
case TGT_ENUM:
if (get_enum_fc(t) == FC_ENUM16)
return FC_BOGUS_STRUCT;
break;
case TGT_POINTER:
case TGT_ARRAY:
if (get_pointer_fc(t, field->attrs, FALSE) == FC_RP || pointer_size != 4)
return FC_BOGUS_STRUCT;
has_pointer = 1;
break;
case TGT_UNION:
return FC_BOGUS_STRUCT;
case TGT_STRUCT:
{
unsigned char fc = get_struct_fc(t);
switch (fc)
{
case FC_STRUCT:
break;
case FC_CVSTRUCT:
has_conformance = 1;
has_variance = 1;
has_pointer = 1;
break;
case FC_CPSTRUCT:
has_conformance = 1;
if (list_next( fields, &field->entry ))
error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
field->name);
has_pointer = 1;
break;
case FC_CSTRUCT:
has_conformance = 1;
if (list_next( fields, &field->entry ))
error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
field->name);
break;
case FC_PSTRUCT:
has_pointer = 1;
break;
default:
error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
/* fallthru - treat it as complex */
/* as soon as we see one of these these members, it's bogus... */
case FC_BOGUS_STRUCT:
return FC_BOGUS_STRUCT;
}
break;
}
case TGT_RANGE:
return FC_BOGUS_STRUCT;
case TGT_STRING:
/* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
case TGT_INVALID:
case TGT_CTXT_HANDLE:
case TGT_CTXT_HANDLE_POINTER:
/* checking after parsing should mean that we don't get here. if we do,
* it's a checker bug */
assert(0);
}
}
if( has_variance )
{
if ( has_conformance )
return FC_CVSTRUCT;
else
return FC_BOGUS_STRUCT;
}
if( has_conformance && has_pointer )
return FC_CPSTRUCT;
if( has_conformance )
return FC_CSTRUCT;
if( has_pointer )
return FC_PSTRUCT;
return FC_STRUCT;
}
static unsigned char get_array_fc(const type_t *type)
{
unsigned char fc;
const expr_t *size_is;
const type_t *elem_type;
elem_type = type_array_get_element_type(type);
size_is = type_array_get_conformance(type);
if (!size_is)
{
unsigned int size = type_memsize(elem_type);
if (size * type_array_get_dim(type) > 0xffffuL)
fc = FC_LGFARRAY;
else
fc = FC_SMFARRAY;
}
else
fc = FC_CARRAY;
if (type_array_has_variance(type))
{
if (fc == FC_SMFARRAY)
fc = FC_SMVARRAY;
else if (fc == FC_LGFARRAY)
fc = FC_LGVARRAY;
else if (fc == FC_CARRAY)
fc = FC_CVARRAY;
}
switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
{
case TGT_USER_TYPE:
fc = FC_BOGUS_ARRAY;
break;
case TGT_BASIC:
if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
pointer_size != 4)
fc = FC_BOGUS_ARRAY;
break;
case TGT_STRUCT:
switch (get_struct_fc(elem_type))
{
case FC_BOGUS_STRUCT:
fc = FC_BOGUS_ARRAY;
break;
}
break;
case TGT_ENUM:
/* is 16-bit enum - if so, wire size differs from mem size and so
* the array cannot be block copied, which means the array is complex */
if (get_enum_fc(elem_type) == FC_ENUM16)
fc = FC_BOGUS_ARRAY;
break;
case TGT_UNION:
case TGT_IFACE_POINTER:
fc = FC_BOGUS_ARRAY;
break;
case TGT_POINTER:
/* ref pointers cannot just be block copied. unique pointers to
* interfaces need special treatment. either case means the array is
* complex */
if (get_pointer_fc(elem_type, NULL, FALSE) == FC_RP || pointer_size != 4)
fc = FC_BOGUS_ARRAY;
break;
case TGT_RANGE:
fc = FC_BOGUS_ARRAY;
break;
case TGT_CTXT_HANDLE:
case TGT_CTXT_HANDLE_POINTER:
case TGT_STRING:
case TGT_INVALID:
case TGT_ARRAY:
/* nothing to do for everything else */
break;
}
return fc;
}
static int is_non_complex_struct(const type_t *type)
{
return (type_get_type(type) == TYPE_STRUCT &&
get_struct_fc(type) != FC_BOGUS_STRUCT);
}
static int type_has_pointers(const type_t *type)
{
switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
{
case TGT_USER_TYPE:
return FALSE;
case TGT_POINTER:
return TRUE;
case TGT_ARRAY:
return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element_type(type));
case TGT_STRUCT:
{
var_list_t *fields = type_struct_get_fields(type);
const var_t *field;
if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
{
if (type_has_pointers(field->declspec.type))
return TRUE;
}
break;
}
case TGT_UNION:
{
var_list_t *fields;
const var_t *field;
fields = type_union_get_cases(type);
if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
{
if (field->declspec.type && type_has_pointers(field->declspec.type))
return TRUE;
}
break;
}
case TGT_CTXT_HANDLE:
case TGT_CTXT_HANDLE_POINTER:
case TGT_STRING:
case TGT_IFACE_POINTER:
case TGT_BASIC:
case TGT_ENUM:
case TGT_RANGE:
case TGT_INVALID:
break;
}
return FALSE;
}
static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
int toplevel_param)
{
switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
{
case TGT_USER_TYPE:
return FALSE;
case TGT_POINTER:
if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
return TRUE;
else
return FALSE;
case TGT_ARRAY:
if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
return TRUE;
else
return type_has_full_pointer(type_array_get_element_type(type), NULL, FALSE);
case TGT_STRUCT:
{
var_list_t *fields = type_struct_get_fields(type);
const var_t *field;
if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
{
if (type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
return TRUE;
}
break;
}
case TGT_UNION:
{
var_list_t *fields;
const var_t *field;
fields = type_union_get_cases(type);
if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
{
if (field->declspec.type && type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
return TRUE;
}
break;
}
case TGT_CTXT_HANDLE:
case TGT_CTXT_HANDLE_POINTER:
case TGT_STRING:
case TGT_IFACE_POINTER:
case TGT_BASIC:
case TGT_ENUM:
case TGT_RANGE:
case TGT_INVALID:
break;
}
return FALSE;
}
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;
}
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;
}
static int is_embedded_complex(const type_t *type)
{
switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
{
case TGT_USER_TYPE:
case TGT_STRUCT:
case TGT_UNION:
case TGT_ARRAY:
case TGT_IFACE_POINTER:
return TRUE;
default:
return FALSE;
}
}
static const char *get_context_handle_type_name(const type_t *type)
{
const type_t *t;
for (t = type;
is_ptr(t) || type_is_alias(t);
t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
return t->name;
assert(0);
return NULL;
}
#define WRITE_FCTYPE(file, fctype, typestring_offset) \
do { \
if (file) \
fprintf(file, "/* %2u */\n", typestring_offset); \
print_file((file), 2, "0x%02x,\t/* " #fctype " */\n", fctype); \
} \
while (0)
static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
static void print_file(FILE *file, int indent, const char *format, ...)
{
va_list va;
va_start(va, format);
print(file, indent, format, va);
va_end(va);
}
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);
}
}
static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
{
if (decl_indirect(t))
{
print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
local_var_prefix, n, local_var_prefix, n);
print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
}
else if (is_ptr(t) || is_array(t))
print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
}
void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
{
const var_t *var = type_function_get_retval(func->declspec.type);
if (!is_void(var->declspec.type))
write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
if (!type_function_get_args(func->declspec.type))
return;
LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
fprintf(file, "\n");
}
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");
}
void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
{
clear_all_offsets();
print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
get_size_typeformatstring(stmts, pred));
print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
get_size_procformatstring(stmts, pred));
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");
}
int decl_indirect(const type_t *t)
{
if (is_user_type(t))
return TRUE;
return (type_get_type(t) != TYPE_BASIC &&
type_get_type(t) != TYPE_ENUM &&
type_get_type(t) != TYPE_POINTER &&
type_get_type(t) != TYPE_ARRAY);
}
static unsigned char get_parameter_fc( const var_t *var, int is_return, unsigned short *flags,
unsigned int *stack_size, unsigned int *typestring_offset )
{
unsigned int alignment, server_size = 0, buffer_size = 0;
unsigned char fc = 0;
int is_byval;
int is_in = is_attr(var->attrs, ATTR_IN);
int is_out = is_attr(var->attrs, ATTR_OUT);
if (is_return) is_out = TRUE;
else if (!is_in && !is_out) is_in = TRUE;
*flags = 0;
*stack_size = get_stack_size( var, &is_byval );
*typestring_offset = var->typestring_offset;
if (is_in) *flags |= IsIn;
if (is_out) *flags |= IsOut;
if (is_return) *flags |= IsReturn;
if (!is_string_type( var->attrs, var->declspec.type ))
buffer_size = get_required_buffer_size_type( var->declspec.type, NULL, var->attrs, TRUE, &alignment );
switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
{
case TGT_BASIC:
*flags |= IsBasetype;
fc = get_basic_fc_signed( var->declspec.type );
if (fc == FC_BIND_PRIMITIVE)
{
buffer_size = 4; /* actually 0 but avoids setting MustSize */
fc = FC_LONG;
}
break;
case TGT_ENUM:
*flags |= IsBasetype;
fc = get_enum_fc( var->declspec.type );
break;
case TGT_RANGE:
*flags |= IsByValue;
break;
case TGT_STRUCT:
case TGT_UNION:
case TGT_USER_TYPE:
*flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
break;
case TGT_IFACE_POINTER:
*flags |= MustFree;
break;
case TGT_ARRAY:
*flags |= MustFree;
if (type_array_is_decl_as_ptr(var->declspec.type)
&& type_array_get_ptr_tfsoff(var->declspec.type)
&& get_pointer_fc(var->declspec.type, var->attrs, !is_return) == FC_RP)
{
*typestring_offset = var->declspec.type->typestring_offset;
*flags |= IsSimpleRef;
}
break;
case TGT_STRING:
*flags |= MustFree;
if (is_declptr( var->declspec.type ) && get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
{
/* skip over pointer description straight to string description */
if (is_conformant_array( var->declspec.type )) *typestring_offset += 4;
else *typestring_offset += 2;
*flags |= IsSimpleRef;
}
break;
case TGT_CTXT_HANDLE_POINTER:
*flags |= IsSimpleRef;
*typestring_offset += 4;
/* fall through */
case TGT_CTXT_HANDLE:
buffer_size = 20;
break;
case TGT_POINTER:
if (get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
{
const type_t *ref = type_pointer_get_ref_type( var->declspec.type );
if (!is_string_type( var->attrs, ref ))
buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
{
case TGT_BASIC:
*flags |= IsSimpleRef | IsBasetype;
fc = get_basic_fc( ref );
if (!is_in && is_out) server_size = pointer_size;
break;
case TGT_ENUM:
if ((fc = get_enum_fc( ref )) == FC_ENUM32)
{
*flags |= IsSimpleRef | IsBasetype;
if (!is_in && is_out) server_size = pointer_size;
}
else
{
server_size = pointer_size;
}
break;
case TGT_UNION:
case TGT_USER_TYPE:
case TGT_RANGE:
*flags |= MustFree | IsSimpleRef;
*typestring_offset = ref->typestring_offset;
if (!is_in && is_out) server_size = type_memsize( ref );
break;
case TGT_ARRAY:
*flags |= MustFree;
if (!type_array_is_decl_as_ptr(ref))
{
*flags |= IsSimpleRef;
*typestring_offset = ref->typestring_offset;
}
if (!is_in && is_out) server_size = type_memsize( ref );
break;
case TGT_STRING:
case TGT_POINTER:
case TGT_CTXT_HANDLE:
case TGT_CTXT_HANDLE_POINTER:
*flags |= MustFree;
server_size = pointer_size;
break;
case TGT_IFACE_POINTER:
*flags |= MustFree;
if (is_in && is_out) server_size = pointer_size;
break;
case TGT_STRUCT:
*flags |= IsSimpleRef | MustFree;
*typestring_offset = ref->typestring_offset;
switch (get_struct_fc(ref))
{
case FC_STRUCT:
case FC_PSTRUCT:
case FC_BOGUS_STRUCT:
if (!is_in && is_out) server_size = type_memsize( ref );
break;
default:
break;
}
break;
case TGT_INVALID:
assert(0);
}
}
else /* not ref pointer */
{
*flags |= MustFree;
}
break;
case TGT_INVALID:
assert(0);
}
if (!buffer_size) *flags |= MustSize;
if (server_size)
{
server_size = (server_size + 7) / 8;
if (server_size < 8) *flags |= server_size << 13;
}
return fc;
}
static unsigned char get_func_oi2_flags( const var_t *func )
{
const var_t *var;
var_list_t *args = type_function_get_args( func->declspec.type );
var_t *retval = type_function_get_retval( func->declspec.type );
unsigned char oi2_flags = 0x40; /* HasExtensions */
unsigned short flags;
unsigned int stack_size, typestring_offset;
if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
{
get_parameter_fc( var, 0, &flags, &stack_size, &typestring_offset );
if (flags & MustSize)
{
if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
}
}
if (!is_void( retval->declspec.type ))
{
oi2_flags |= 0x04; /* HasRet */
get_parameter_fc( retval, 1, &flags, &stack_size, &typestring_offset );
if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
}
return oi2_flags;
}
static unsigned int write_new_procformatstring_type(FILE *file, int indent, const var_t *var,
int is_return, unsigned int *stack_offset)
{
char buffer[128];
unsigned int stack_size, typestring_offset;
unsigned short flags;
unsigned char fc = get_parameter_fc( var, is_return, &flags, &stack_size, &typestring_offset );
strcpy( buffer, "/* flags:" );
if (flags & MustSize) strcat( buffer, " must size," );
if (flags & MustFree) strcat( buffer, " must free," );
if (flags & IsPipe) strcat( buffer, " pipe," );
if (flags & IsIn) strcat( buffer, " in," );
if (flags & IsOut) strcat( buffer, " out," );
if (flags & IsReturn) strcat( buffer, " return," );
if (flags & IsBasetype) strcat( buffer, " base type," );
if (flags & IsByValue) strcat( buffer, " by value," );
if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
strcpy( buffer + strlen( buffer ) - 1, " */" );
print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
print_file( file, indent, "NdrFcShort(0x%x), /* stack offset = %u */\n",
*stack_offset, *stack_offset );
if (flags & IsBasetype)
{
print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
print_file( file, indent, "0x0,\n" );
}
else
print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
typestring_offset, typestring_offset );
*stack_offset += max( stack_size, pointer_size );
return 6;
}
static unsigned int write_old_procformatstring_type(FILE *file, int indent, const var_t *var,
int is_return, int is_interpreted)
{
unsigned int size;
int is_in = is_attr(var->attrs, ATTR_IN);
int is_out = is_attr(var->attrs, ATTR_OUT);
if (!is_in && !is_out) is_in = TRUE;
if (type_get_type(var->declspec.type) == TYPE_BASIC ||
type_get_type(var->declspec.type) == TYPE_ENUM)
{
unsigned char fc;
if (is_return)
print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
else
print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
if (type_get_type(var->declspec.type) == TYPE_ENUM)
{
fc = get_enum_fc(var->declspec.type);
}
else
{
fc = get_basic_fc_signed(var->declspec.type);
if (fc == FC_BIND_PRIMITIVE)
fc = FC_IGNORE;
}
print_file(file, indent, "0x%02x, /* %s */\n",
fc, string_of_type(fc));
size = 2; /* includes param type prefix */
}
else
{
unsigned short offset = var->typestring_offset;
if (!is_interpreted && is_array(var->declspec.type)
&& type_array_is_decl_as_ptr(var->declspec.type)
&& type_array_get_ptr_tfsoff(var->declspec.type))
offset = var->declspec.type->typestring_offset;
if (is_return)
print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
else if (is_in && is_out)
print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
else if (is_out)
print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
else
print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
size = get_stack_size( var, NULL );
print_file(file, indent, "0x%02x,\n", size / pointer_size );
print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
size = 4; /* includes param type prefix */
}
return size;
}
int is_interpreted_func( const type_t *iface, const var_t *func )
{
const char *str;
const var_t *var;
const var_list_t *args = type_function_get_args( func->declspec.type );
const type_t *ret_type = type_function_get_rettype( func->declspec.type );
if (type_get_type( ret_type ) == TYPE_BASIC)
{
switch (type_basic_get_type( ret_type ))
{
case TYPE_BASIC_INT64:
case TYPE_BASIC_HYPER:
/* return value must fit in a long_ptr */
if (pointer_size < 8) return 0;
break;
case TYPE_BASIC_FLOAT:
case TYPE_BASIC_DOUBLE:
/* floating point values can't be returned */
return 0;
default:
break;
}
}
if (get_stub_mode() != MODE_Oif && args)
{
LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
switch (type_get_type( var->declspec.type ))
{
case TYPE_BASIC:
switch (type_basic_get_type( var->declspec.type ))
{
/* floating point arguments are not supported in Oi mode */
case TYPE_BASIC_FLOAT: return 0;
case TYPE_BASIC_DOUBLE: return 0;
default: break;
}
break;
/* unions passed by value are not supported in Oi mode */
case TYPE_UNION: return 0;
case TYPE_ENCAPSULATED_UNION: return 0;
default: break;
}
}
if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
return (get_stub_mode() != MODE_Os);
}
static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
const var_t *func, unsigned int *offset,
unsigned short num_proc )
{
var_t *var;
var_list_t *args = type_function_get_args( func->declspec.type );
unsigned char explicit_fc, implicit_fc;
unsigned char handle_flags;
const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
unsigned char oi_flags = Oi_HAS_RPCFLAGS | Oi_USE_NEW_INIT_ROUTINES;
unsigned int rpc_flags = get_rpc_flags( func->attrs );
unsigned int nb_args = 0;
unsigned int stack_size = 0;
unsigned short param_num = 0;
unsigned short handle_stack_offset = 0;
unsigned short handle_param_num = 0;
if (is_full_pointer_function( func )) oi_flags |= Oi_FULL_PTR_USED;
if (is_object( iface ))
{
oi_flags |= Oi_OBJECT_PROC;
if (get_stub_mode() == MODE_Oif) oi_flags |= Oi_OBJ_USE_V2_INTERPRETER;
stack_size += pointer_size;
}
if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
{
if (var == handle_var)
{
handle_stack_offset = stack_size;
handle_param_num = param_num;
}
stack_size += get_stack_size( var, NULL );
param_num++;
nb_args++;
}
if (!is_void( type_function_get_rettype( func->declspec.type )))
{
stack_size += pointer_size;
nb_args++;
}
print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
print_file( file, indent, "0x%02x,\n", oi_flags );
print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
print_file( file, indent, "NdrFcShort(0x%x),\t/* stack size = %u */\n", stack_size, stack_size );
*offset += 10;
if (!implicit_fc)
{
switch (explicit_fc)
{
case FC_BIND_PRIMITIVE:
handle_flags = 0;
print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
print_file( file, indent, "0x%02x,\n", handle_flags );
print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
handle_stack_offset, handle_stack_offset );
*offset += 4;
break;
case FC_BIND_GENERIC:
handle_flags = type_memsize( handle_var->declspec.type );
print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
print_file( file, indent, "0x%02x,\n", handle_flags );
print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
handle_stack_offset, handle_stack_offset );
print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->declspec.type ) );
print_file( file, indent, "0x%x,\t/* FC_PAD */\n", FC_PAD);
*offset += 6;
break;
case FC_BIND_CONTEXT:
handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->declspec.type, 0 );
print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
print_file( file, indent, "0x%02x,\n", handle_flags );
print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
handle_stack_offset, handle_stack_offset );
print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->declspec.type ) );
print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
*offset += 6;
break;
}
}
if (get_stub_mode() == MODE_Oif)
{
unsigned char oi2_flags = get_func_oi2_flags( func );
unsigned char ext_flags = 0;
unsigned int size;
if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
if (iface == type_iface_get_async_iface(iface)) oi2_flags |= 0x20;
size = get_function_buffer_size( func, PASS_IN );
print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %u */\n", size, size );
size = get_function_buffer_size( func, PASS_OUT );
print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %u */\n", size, size );
print_file( file, indent, "0x%02x,\n", oi2_flags );
print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
print_file( file, indent, "0x%02x,\n", ext_flags );
print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
*offset += 14;
if (pointer_size == 8)
{
unsigned short pos = 0, fpu_mask = 0;
if (is_object( iface )) pos += 2;
if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
{
if (type_get_type( var->declspec.type ) == TYPE_BASIC)
{
switch (type_basic_get_type( var->declspec.type ))
{
case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
default: break;
}
}
pos += 2;
if (pos >= 16) break;
}
print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
*offset += 2;
}
}
}
static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
const var_t *func, unsigned int *offset,
unsigned short num_proc )
{
unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
int is_interpreted = is_interpreted_func( iface, func );
int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
var_t *retval = type_function_get_retval( func->declspec.type );
if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
/* emit argument data */
if (type_function_get_args(func->declspec.type))
{
const var_t *var;
LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
{
print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
if (is_new_style)
*offset += write_new_procformatstring_type(file, indent, var, FALSE, &stack_offset);
else
*offset += write_old_procformatstring_type(file, indent, var, FALSE, is_interpreted);
}
}
/* emit return value data */
if (is_void(retval->declspec.type))
{
if (!is_new_style)
{
print_file(file, 0, "/* %u (void) */\n", *offset);
print_file(file, indent, "0x5b,\t/* FC_END */\n");
print_file(file, indent, "0x5c,\t/* FC_PAD */\n");
*offset += 2;
}
}
else
{
print_file( file, 0, "/* %u (return value) */\n", *offset );
if (is_new_style)
*offset += write_new_procformatstring_type(file, indent, retval, TRUE, &stack_offset);
else
*offset += write_old_procformatstring_type(file, indent, retval, TRUE, is_interpreted);
}
}
static void for_each_iface(const statement_list_t *stmts,
void (*proc)(type_t *iface, FILE *file, int indent, unsigned int *offset),
type_pred_t pred, FILE *file, int indent, unsigned int *offset)
{
const statement_t *stmt;
type_t *iface;
if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
{
if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
continue;
iface = stmt->u.type;
if (!pred(iface)) continue;
proc(iface, file, indent, offset);
if (type_iface_get_async_iface(iface))
proc(type_iface_get_async_iface(iface), file, indent, offset);
}
}
static void write_iface_procformatstring(type_t *iface, FILE *file, int indent, unsigned int *offset)
{
const statement_t *stmt;
const type_t *parent = type_iface_get_inherit( iface );
int count = parent ? count_methods( parent ) : 0;
STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
{
var_t *func = stmt->u.var;
if (is_local(func->attrs)) continue;
write_procformatstring_func( file, indent, iface, func, offset, count++ );
}
}
void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
{
int indent = 0;
unsigned int offset = 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++;
for_each_iface(stmts, write_iface_procformatstring, pred, file, indent, &offset);
print_file(file, indent, "0x0\n");
indent--;
print_file(file, indent, "}\n");
indent--;
print_file(file, indent, "};\n");
print_file(file, indent, "\n");
}
void write_procformatstring_offsets( FILE *file, const type_t *iface )
{
const statement_t *stmt;
int indent = 0;
print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
iface->name );
print_file( file, indent, "{\n" );
indent++;
STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
{
var_t *func = stmt->u.var;
if (is_local( func->attrs )) continue;
print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
}
indent--;
print_file( file, indent, "};\n\n" );
}
static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
{
unsigned char fc;
if (type_get_type(type) == TYPE_BASIC)
fc = get_basic_fc_signed(type);
else if (type_get_type(type) == TYPE_ENUM)
fc = get_enum_fc(type);
else
return 0;
print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
*typestring_offset += 1;
return 1;
}
/* write conformance / variance descriptor */
static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
unsigned int baseoff, const type_t *type,
const expr_t *expr)
{
unsigned char operator_type = 0;
unsigned char conftype = FC_NORMAL_CONFORMANCE;
const char *conftype_string = "field";
const expr_t *subexpr;
const type_t *iface = NULL;
const char *name;
if (!expr)
{
print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
return 4;
}
if (expr->is_const)
{
if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
error("write_conf_or_var_desc: constant value %d 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 = %d */\n",
FC_CONSTANT_CONFORMANCE, expr->cval);
print_file(file, 2, "0x%x,\n", expr->cval >> 16);
print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
return 4;
}
if (!cont_type) /* top-level conformance */
{
conftype = FC_TOP_LEVEL_CONFORMANCE;
conftype_string = "parameter";
cont_type = current_func->declspec.type;
name = current_func->name;
iface = current_iface;
}
else
{
name = cont_type->name;
if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
{
conftype = FC_POINTER_CONFORMANCE;
conftype_string = "field pointer";
}
}
subexpr = expr;
switch (subexpr->type)
{
case EXPR_PPTR:
subexpr = subexpr->ref;
operator_type = FC_DEREFERENCE;
break;
case EXPR_DIV:
if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
{
subexpr = subexpr->ref;
operator_type = FC_DIV_2;
}
break;
case EXPR_MUL:
if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
{
subexpr = subexpr->ref;
operator_type = FC_MULT_2;
}
break;
case EXPR_SUB:
if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
{
subexpr = subexpr->ref;
operator_type = FC_SUB_1;
}
break;
case EXPR_ADD:
if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
{
subexpr = subexpr->ref;
operator_type = FC_ADD_1;
}
break;
default:
break;
}
if (subexpr->type == EXPR_IDENTIFIER)
{
const type_t *correlation_variable = NULL;
unsigned char param_type = 0;
unsigned int offset = 0;
const var_t *var;
struct expr_loc expr_loc;
if (type_get_type(cont_type) == TYPE_FUNCTION)
{
var_list_t *args = type_function_get_args( cont_type );
if (is_object( iface )) offset += pointer_size;
if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
{
if (var->name && !strcmp(var->name, subexpr->u.sval))
{
expr_loc.v = var;
correlation_variable = var->declspec.type;
break;
}
offset += get_stack_size( var, NULL );
}
}
else
{
var_list_t *fields = type_struct_get_fields( cont_type );
if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
{
unsigned int size = field_memsize( var->declspec.type, &offset );
if (var->name && !strcmp(var->name, subexpr->u.sval))
{
expr_loc.v = var;
correlation_variable = var->declspec.type;
break;
}
offset += size;
}
}
if (!correlation_variable)
error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
expr_loc.attr = NULL;
correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
offset -= baseoff;
if (type_get_type(correlation_variable) == TYPE_BASIC)
{
switch (get_basic_fc(correlation_variable))
{
case FC_CHAR:
case FC_SMALL:
param_type = FC_SMALL;
break;
case FC_BYTE:
case FC_USMALL:
param_type = FC_USMALL;
break;
case FC_WCHAR:
case FC_SHORT:
param_type = FC_SHORT;
break;
case FC_USHORT:
param_type = FC_USHORT;
break;
case FC_LONG:
param_type = FC_LONG;
break;
case FC_ULONG:
param_type = FC_ULONG;
break;
default:
error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
get_basic_fc(correlation_variable));
}
}
else if (type_get_type(correlation_variable) == TYPE_ENUM)
{
if (get_enum_fc(correlation_variable) == FC_ENUM32)
param_type = FC_LONG;
else
param_type = FC_SHORT;
}
else if (type_get_type(correlation_variable) == TYPE_POINTER)
{
if (pointer_size == 8)
param_type = FC_HYPER;
else
param_type = FC_LONG;
}
else
{
error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
subexpr->u.sval);
return 0;
}
print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
operator_type ? string_of_type(operator_type) : "no operators");
print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
(unsigned short)offset, offset);
}
else if (!iface || is_interpreted_func( iface, current_func ))
{
unsigned int callback_offset = 0;
struct expr_eval_routine *eval;
int found = 0;
LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
{
if (eval->cont_type == cont_type ||
(type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
eval->iface == iface &&
eval->name && name && !strcmp(eval->name, name) &&
!compare_expr(eval->expr, expr)))
{
found = 1;
break;
}
callback_offset++;
}
if (!found)
{
eval = xmalloc (sizeof(*eval));
eval->iface = iface;
eval->cont_type = cont_type;
eval->name = xstrdup( name );
eval->baseoff = baseoff;
eval->expr = expr;
list_add_tail (&expr_eval_routines, &eval->entry);
}
if (callback_offset > USHRT_MAX)
error("Maximum number of callback routines reached\n");
print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
print_file(file, 2, "0x%x,\t/* %s */\n", FC_CALLBACK, "FC_CALLBACK");
print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
}
else /* output a dummy corr desc that isn't used */
{
print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
print_file(file, 2, "0x0,\n" );
print_file(file, 2, "NdrFcShort(0x0),\n" );
}
return 4;
}
/* return size and start offset of a data field based on current offset */
static unsigned int field_memsize(const type_t *type, unsigned int *offset)
{
unsigned int align = 0;
unsigned int size = type_memsize_and_alignment( type, &align );
*offset = ROUND_SIZE( *offset, align );
return size;
}
static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
{
unsigned int size = 0;
unsigned int max_align;
const var_t *v;
if (!fields) return 0;
LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
{
unsigned int falign = 0;
unsigned int fsize = type_memsize_and_alignment(v->declspec.type, &falign);
if (*align < falign) *align = falign;
falign = clamp_align(falign);
size = ROUND_SIZE(size, falign);
size += fsize;
}
max_align = clamp_align(*align);
size = ROUND_SIZE(size, max_align);
return size;
}
static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
{
unsigned int 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->declspec.type)
{
size = type_memsize_and_alignment(v->declspec.type, &align);
if (maxs < size) maxs = size;
if (*pmaxa < align) *pmaxa = align;
}
}
return maxs;
}
unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
{
unsigned int size = 0;
switch (type_get_type(t))
{
case TYPE_BASIC:
switch (get_basic_fc(t))
{
case FC_BYTE:
case FC_CHAR:
case FC_USMALL:
case FC_SMALL:
size = 1;
if (size > *align) *align = size;
break;
case FC_WCHAR:
case FC_USHORT:
case FC_SHORT:
size = 2;
if (size > *align) *align = size;
break;
case FC_ULONG:
case FC_LONG:
case FC_ERROR_STATUS_T:
case FC_FLOAT:
size = 4;
if (size > *align) *align = size;
break;
case FC_HYPER:
case FC_DOUBLE:
size = 8;
if (size > *align) *align = size;
break;
case FC_INT3264:
case FC_UINT3264:
case FC_BIND_PRIMITIVE:
assert( pointer_size );
size = pointer_size;
if (size > *align) *align = size;
break;
default:
error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
size = 0;
}
break;
case TYPE_ENUM:
switch (get_enum_fc(t))
{
case FC_ENUM16:
case FC_ENUM32:
size = 4;
if (size > *align) *align = size;
break;
default:
error("type_memsize: Unknown enum type\n");
size = 0;
}
break;
case TYPE_STRUCT:
size = fields_memsize(type_struct_get_fields(t), align);
break;
case TYPE_ENCAPSULATED_UNION:
size = fields_memsize(type_encapsulated_union_get_fields(t), align);
break;
case TYPE_UNION:
size = union_memsize(type_union_get_cases(t), align);
break;
case TYPE_POINTER:
case TYPE_INTERFACE:
assert( pointer_size );
size = pointer_size;
if (size > *align) *align = size;
break;
case TYPE_ARRAY:
if (!type_array_is_decl_as_ptr(t))
{
if (is_conformant_array(t))
{
type_memsize_and_alignment(type_array_get_element_type(t), align);
size = 0;
}
else
size = type_array_get_dim(t) *
type_memsize_and_alignment(type_array_get_element_type(t), align);
}
else /* declared as a pointer */
{
assert( pointer_size );
size = pointer_size;
if (size > *align) *align = size;
}
break;
case TYPE_ALIAS:
case TYPE_VOID:
case TYPE_COCLASS:
case TYPE_MODULE:
case TYPE_FUNCTION:
case TYPE_BITFIELD:
case TYPE_APICONTRACT:
case TYPE_RUNTIMECLASS:
case TYPE_PARAMETERIZED_TYPE:
case TYPE_PARAMETER:
case TYPE_DELEGATE:
/* these types should not be encountered here due to language
* restrictions (interface, void, coclass, module), logical
* restrictions (alias - due to type_get_type call above) or
* checking restrictions (function, bitfield). */
assert(0);
}
return size;
}
unsigned int type_memsize(const type_t *t)
{
unsigned int align = 0;
return type_memsize_and_alignment( t, &align );
}
static unsigned int type_buffer_alignment(const type_t *t)
{
const var_list_t *fields;
const var_t *var;
unsigned int max = 0, align;
switch (type_get_type(t))
{
case TYPE_BASIC:
switch (get_basic_fc(t))
{
case FC_BYTE:
case FC_CHAR:
case FC_USMALL:
case FC_SMALL:
return 1;
case FC_WCHAR:
case FC_USHORT:
case FC_SHORT:
return 2;
case FC_ULONG:
case FC_LONG:
case FC_ERROR_STATUS_T:
case FC_FLOAT:
case FC_INT3264:
case FC_UINT3264:
return 4;
case FC_HYPER:
case FC_DOUBLE:
return 8;
default:
error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
}
break;
case TYPE_ENUM:
switch (get_enum_fc(t))
{
case FC_ENUM16:
return 2;
case FC_ENUM32:
return 4;
default:
error("type_buffer_alignment: Unknown enum type\n");
}
break;
case TYPE_STRUCT:
if (!(fields = type_struct_get_fields(t))) break;
LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
{
if (!var->declspec.type) continue;
align = type_buffer_alignment( var->declspec.type );
if (max < align) max = align;
}
break;
case TYPE_ENCAPSULATED_UNION:
if (!(fields = type_encapsulated_union_get_fields(t))) break;
LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
{
if (!var->declspec.type) continue;
align = type_buffer_alignment( var->declspec.type );
if (max < align) max = align;
}
break;
case TYPE_UNION:
if (!(fields = type_union_get_cases(t))) break;
LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
{
if (!var->declspec.type) continue;
align = type_buffer_alignment( var->declspec.type );
if (max < align) max = align;
}
break;
case TYPE_ARRAY:
if (!type_array_is_decl_as_ptr(t))
return type_buffer_alignment( type_array_get_element_type(t) );
/* else fall through */
case TYPE_POINTER:
return 4;
case TYPE_INTERFACE:
case TYPE_ALIAS:
case TYPE_VOID:
case TYPE_COCLASS:
case TYPE_MODULE:
case TYPE_FUNCTION:
case TYPE_BITFIELD:
case TYPE_APICONTRACT:
case TYPE_RUNTIMECLASS:
case TYPE_PARAMETERIZED_TYPE:
case TYPE_PARAMETER:
case TYPE_DELEGATE:
/* these types should not be encountered here due to language
* restrictions (interface, void, coclass, module), logical
* restrictions (alias - due to type_get_type call above) or
* checking restrictions (function, bitfield). */
assert(0);
}
return max;
}
int is_full_pointer_function(const var_t *func)
{
const var_t *var;
if (type_has_full_pointer(type_function_get_rettype(func->declspec.type), func->attrs, TRUE))
return TRUE;
if (!type_function_get_args(func->declspec.type))
return FALSE;
LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
if (type_has_full_pointer( var->declspec.type, var->attrs, TRUE ))
return TRUE;
return FALSE;
}
void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
{
print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
fprintf(file, "\n");
}
void write_full_pointer_free(FILE *file, int indent, const var_t *func)
{
print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
fprintf(file, "\n");
}
static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
const type_t *type,
enum type_context context,
unsigned int offset,
unsigned int *typeformat_offset)
{
unsigned int start_offset = *typeformat_offset;
short reloff = offset - (*typeformat_offset + 2);
int in_attr, out_attr;
int pointer_type;
unsigned char flags = 0;
pointer_type = get_pointer_fc_context(type, attrs, context);
in_attr = is_attr(attrs, ATTR_IN);
out_attr = is_attr(attrs, ATTR_OUT);
if (!in_attr && !out_attr) in_attr = 1;
if (!is_interpreted_func(current_iface, current_func))
{
if (out_attr && !in_attr && pointer_type == FC_RP)
flags |= FC_ALLOCED_ON_STACK;
}
else if (get_stub_mode() == MODE_Oif)
{
if (context == TYPE_CONTEXT_TOPLEVELPARAM && is_ptr(type) && pointer_type == FC_RP)
{
switch (typegen_detect_type(type_pointer_get_ref_type(type), NULL, TDT_ALL_TYPES))
{
case TGT_STRING:
case TGT_POINTER:
case TGT_CTXT_HANDLE:
case TGT_CTXT_HANDLE_POINTER:
case TGT_ARRAY:
flags |= FC_ALLOCED_ON_STACK;
break;
case TGT_IFACE_POINTER:
if (in_attr && out_attr)
flags |= FC_ALLOCED_ON_STACK;
break;
default:
break;
}
}
}
if (is_ptr(type))
{
type_t *ref = type_pointer_get_ref_type(type);
if(is_declptr(ref) && !is_user_type(ref))
flags |= FC_POINTER_DEREF;
if (pointer_type != FC_RP) {
flags |= get_attrv(type->attrs, ATTR_ALLOCATE);
}
}
print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
pointer_type,
flags,
string_of_type(pointer_type));
if (file)
{
if (flags & FC_ALLOCED_ON_STACK)
fprintf(file, " [allocated_on_stack]");
if (flags & FC_POINTER_DEREF)
fprintf(file, " [pointer_deref]");
if (flags & FC_DONT_FREE)
fprintf(file, " [dont_free]");
if (flags & FC_ALLOCATE_ALL_NODES)
fprintf(file, " [all_nodes]");
fprintf(file, " */\n");
}
print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
*typeformat_offset += 4;
return start_offset;
}
static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
const type_t *type, enum type_context context)
{
unsigned char fc;
unsigned char pointer_fc;
const type_t *ref;
int in_attr = is_attr(attrs, ATTR_IN);
int out_attr = is_attr(attrs, ATTR_OUT);
unsigned char flags = FC_SIMPLE_POINTER;
/* 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(attrs, type))
error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
pointer_fc = get_pointer_fc_context(type, attrs, context);
ref = type_pointer_get_ref_type(type);
if (type_get_type(ref) == TYPE_ENUM)
fc = get_enum_fc(ref);
else
fc = get_basic_fc(ref);
if (!is_interpreted_func(current_iface, current_func))
{
if (out_attr && !in_attr && pointer_fc == FC_RP)
flags |= FC_ALLOCED_ON_STACK;
}
else if (get_stub_mode() == MODE_Oif)
{
if (context == TYPE_CONTEXT_TOPLEVELPARAM && fc == FC_ENUM16 && pointer_fc == FC_RP)
flags |= FC_ALLOCED_ON_STACK;
}
print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
pointer_fc, flags, string_of_type(pointer_fc),
flags & FC_ALLOCED_ON_STACK ? "[allocated_on_stack] " : "");
print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
return 4;
}
static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
{
const decl_spec_t ds = {.type = t};
print_file(file, 0, "/* %u (", tfsoff);
write_type_decl(file, &ds, NULL);
print_file(file, 0, ") */\n");
}
static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
type_t *type, unsigned int ref_offset,
enum type_context context,
unsigned int *typestring_offset)
{
unsigned int offset = *typestring_offset;
type_t *ref = type_pointer_get_ref_type(type);
print_start_tfs_comment(file, type, offset);
update_tfsoff(type, offset, file);
switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
{
case TGT_BASIC:
case TGT_ENUM:
*typestring_offset += write_simple_pointer(file, attrs, type, context);
break;
default:
if (ref_offset)
write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
break;
}
return offset;
}
static int processed(const type_t *type)
{
return type->typestring_offset && !type->tfswrite;
}
static int user_type_has_variable_size(const type_t *t)
{
if (is_ptr(t))
return TRUE;
else if (type_get_type(t) == TYPE_STRUCT)
{
switch (get_struct_fc(t))
{
case FC_PSTRUCT:
case FC_CSTRUCT:
case FC_CPSTRUCT:
case 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;
}
static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
{
unsigned int start, absoff, flags;
const char *name = NULL;
type_t *utype = get_user_type(type, &name);
unsigned int usize = type_memsize(utype);
unsigned int ualign = type_buffer_alignment(utype);
unsigned int size = type_memsize(type);
unsigned short funoff = user_type_offset(name);
short reloff;
if (processed(type)) return type->typestring_offset;
guard_rec(type);
if(user_type_has_variable_size(utype)) usize = 0;
if (type_get_type(utype) == TYPE_BASIC ||
type_get_type(utype) == TYPE_ENUM)
{
unsigned char fc;
if (type_get_type(utype) == TYPE_ENUM)
fc = get_enum_fc(utype);
else
fc = get_basic_fc(utype);
absoff = *tfsoff;
print_start_tfs_comment(file, utype, absoff);
print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
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;
}
if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_RP)
flags = 0x40;
else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_UP)
flags = 0x80;
else
flags = 0;
start = *tfsoff;
update_tfsoff(type, start, file);
print_start_tfs_comment(file, type, start);
print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", FC_USER_MARSHAL);
print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
flags | (ualign - 1), ualign - 1, flags);
print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
*tfsoff += 8;
reloff = absoff - *tfsoff;
print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
*tfsoff += 2;
return start;
}
static void write_member_type(FILE *file, const type_t *cont,
int cont_is_complex, const attr_list_t *attrs,
const type_t *type, unsigned int *corroff,
unsigned int *tfsoff)
{
if (is_embedded_complex(type) && !is_conformant_array(type))
{
unsigned int absoff;
short reloff;
if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
{
absoff = *corroff;
*corroff += 8;
}
else
{
absoff = type->typestring_offset;
}
reloff = absoff - (*tfsoff + 2);
print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
/* padding is represented using FC_STRUCTPAD* types, so presumably
* this is left over in the format for historical purposes in MIDL
* or rpcrt4. */
print_file(file, 2, "0x0,\n");
print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
reloff, reloff, absoff);
*tfsoff += 4;
}
else if (is_ptr(type) || is_conformant_array(type))
{
unsigned char fc = cont_is_complex ? FC_POINTER : FC_LONG;
print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
*tfsoff += 1;
}
else if (!write_base_type(file, type, tfsoff))
error("Unsupported member type %d\n", type_get_type(type));
}
static void write_array_element_type(FILE *file, const attr_list_t *attrs, const type_t *type,
int cont_is_complex, unsigned int *tfsoff)
{
type_t *elem = type_array_get_element_type(type);
if (!is_embedded_complex(elem) && is_ptr(elem))
{
type_t *ref = type_pointer_get_ref_type(elem);
if (processed(ref))
{
write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
ref->typestring_offset, tfsoff);
return;
}
if (cont_is_complex && is_string_type(attrs, elem))
{
write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
return;
}
if (!is_string_type(NULL, elem) &&
(type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
{
*tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
return;
}
}
write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
}
static void write_end(FILE *file, unsigned int *tfsoff)
{
if (*tfsoff % 2 == 0)
{
print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
*tfsoff += 1;
}
print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
*tfsoff += 1;
}
static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
{
unsigned int offset = 0;
var_list_t *fs = type_struct_get_fields(type);
var_t *f;
if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
{
type_t *ft = f->declspec.type;
unsigned int size = field_memsize( ft, &offset );
if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
{
short reloff;
unsigned int absoff = ft->typestring_offset;
if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
absoff += 8; /* we already have a corr descr, skip it */
reloff = absoff - (*tfsoff + 6);
print_file(file, 0, "/* %d */\n", *tfsoff);
print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", FC_LONG);
write_conf_or_var_desc(file, current_structure, offset, ft,
get_attrp(f->attrs, ATTR_SWITCHIS));
print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
(unsigned short)reloff, reloff, absoff);
*tfsoff += 8;
}
offset += size;
}
}
static int write_pointer_description_offsets(
FILE *file, const attr_list_t *attrs, type_t *type,