2 * Typelib v2 (MSFT) generation
4 * Copyright 2004 Alastair Bridgewater
5 * 2004, 2005 Huw Davies
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * 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
21 * --------------------------------------------------------------------------------------
26 * Only works on little-endian systems.
31 #include "wine/port.h"
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
48 #include "typelib_struct.h"
54 enum MSFT_segment_index
{
55 MSFT_SEG_TYPEINFO
= 0, /* type information */
56 MSFT_SEG_IMPORTINFO
, /* import information */
57 MSFT_SEG_IMPORTFILES
, /* import filenames */
58 MSFT_SEG_REFERENCES
, /* references (?) */
59 MSFT_SEG_GUIDHASH
, /* hash table for guids? */
60 MSFT_SEG_GUID
, /* guid storage */
61 MSFT_SEG_NAMEHASH
, /* hash table for names */
62 MSFT_SEG_NAME
, /* name storage */
63 MSFT_SEG_STRING
, /* string storage */
64 MSFT_SEG_TYPEDESC
, /* type descriptions */
65 MSFT_SEG_ARRAYDESC
, /* array descriptions */
66 MSFT_SEG_CUSTDATA
, /* custom data */
67 MSFT_SEG_CUSTDATAGUID
, /* custom data guids */
68 MSFT_SEG_UNKNOWN
, /* ??? */
69 MSFT_SEG_UNKNOWN2
, /* ??? */
70 MSFT_SEG_MAX
/* total number of segments */
73 typedef struct tagMSFT_ImpFile
{
77 char filename
[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
80 typedef struct _msft_typelib_t
83 MSFT_Header typelib_header
;
84 MSFT_pSeg typelib_segdir
[MSFT_SEG_MAX
];
85 unsigned char *typelib_segment_data
[MSFT_SEG_MAX
];
86 int typelib_segment_block_length
[MSFT_SEG_MAX
];
88 INT typelib_typeinfo_offsets
[0x200]; /* Hope that's enough. */
90 INT
*typelib_namehash_segment
;
91 INT
*typelib_guidhash_segment
;
93 INT help_string_dll_offset
;
95 struct _msft_typeinfo_t
*typeinfos
;
96 struct _msft_typeinfo_t
*last_typeinfo
;
99 typedef struct _msft_typeinfo_t
101 msft_typelib_t
*typelib
;
102 MSFT_TypeInfoBase
*typeinfo
;
106 unsigned int var_data_allocated
;
109 unsigned int func_data_allocated
;
124 struct _msft_typeinfo_t
*next_typeinfo
;
129 /*================== Internal functions ===================================*/
131 /****************************************************************************
134 * Initializes the type library header of a new typelib.
136 static void ctl2_init_header(
137 msft_typelib_t
*typelib
) /* [I] The typelib to initialize. */
139 typelib
->typelib_header
.magic1
= 0x5446534d;
140 typelib
->typelib_header
.magic2
= 0x00010002;
141 typelib
->typelib_header
.posguid
= -1;
142 typelib
->typelib_header
.lcid
= 0x0409;
143 typelib
->typelib_header
.lcid2
= 0x0;
144 typelib
->typelib_header
.varflags
= 0x40;
145 typelib
->typelib_header
.version
= 0;
146 typelib
->typelib_header
.flags
= 0;
147 typelib
->typelib_header
.nrtypeinfos
= 0;
148 typelib
->typelib_header
.helpstring
= -1;
149 typelib
->typelib_header
.helpstringcontext
= 0;
150 typelib
->typelib_header
.helpcontext
= 0;
151 typelib
->typelib_header
.nametablecount
= 0;
152 typelib
->typelib_header
.nametablechars
= 0;
153 typelib
->typelib_header
.NameOffset
= -1;
154 typelib
->typelib_header
.helpfile
= -1;
155 typelib
->typelib_header
.CustomDataOffset
= -1;
156 typelib
->typelib_header
.res44
= 0x20;
157 typelib
->typelib_header
.res48
= 0x80;
158 typelib
->typelib_header
.dispatchpos
= -1;
159 typelib
->typelib_header
.nimpinfos
= 0;
162 /****************************************************************************
165 * Initializes the segment directory of a new typelib.
167 static void ctl2_init_segdir(
168 msft_typelib_t
*typelib
) /* [I] The typelib to initialize. */
173 segdir
= &typelib
->typelib_segdir
[MSFT_SEG_TYPEINFO
];
175 for (i
= 0; i
< MSFT_SEG_MAX
; i
++) {
176 segdir
[i
].offset
= -1;
177 segdir
[i
].length
= 0;
178 segdir
[i
].res08
= -1;
179 segdir
[i
].res0c
= 0x0f;
183 /****************************************************************************
186 * Generates a hash key from a GUID.
190 * The hash key for the GUID.
192 static int ctl2_hash_guid(
193 REFGUID guid
) /* [I] The guid to hash. */
199 for (i
= 0; i
< 8; i
++) {
200 hash
^= ((const short *)guid
)[i
];
206 /****************************************************************************
209 * Locates a guid in a type library.
213 * The offset into the GUID segment of the guid, or -1 if not found.
215 static int ctl2_find_guid(
216 msft_typelib_t
*typelib
, /* [I] The typelib to operate against. */
217 int hash_key
, /* [I] The hash key for the guid. */
218 REFGUID guid
) /* [I] The guid to find. */
221 MSFT_GuidEntry
*guidentry
;
223 offset
= typelib
->typelib_guidhash_segment
[hash_key
];
224 while (offset
!= -1) {
225 guidentry
= (MSFT_GuidEntry
*)&typelib
->typelib_segment_data
[MSFT_SEG_GUID
][offset
];
227 if (!memcmp(guidentry
, guid
, sizeof(GUID
))) return offset
;
229 offset
= guidentry
->next_hash
;
235 /****************************************************************************
238 * Locates a name in a type library.
242 * The offset into the NAME segment of the name, or -1 if not found.
246 * The name must be encoded as with ctl2_encode_name().
248 static int ctl2_find_name(
249 msft_typelib_t
*typelib
, /* [I] The typelib to operate against. */
250 char *name
) /* [I] The encoded name to find. */
255 offset
= typelib
->typelib_namehash_segment
[name
[2] & 0x7f];
256 while (offset
!= -1) {
257 namestruct
= (int *)&typelib
->typelib_segment_data
[MSFT_SEG_NAME
][offset
];
259 if (!((namestruct
[2] ^ *((int *)name
)) & 0xffff00ff)) {
260 /* hash codes and lengths match, final test */
261 if (!strncasecmp(name
+4, (void *)(namestruct
+3), name
[0])) break;
264 /* move to next item in hash bucket */
265 offset
= namestruct
[1];
271 /****************************************************************************
274 * Encodes a name string to a form suitable for storing into a type library
275 * or comparing to a name stored in a type library.
279 * The length of the encoded name, including padding and length+hash fields.
283 * Will throw an exception if name or result are NULL. Is not multithread
284 * safe in the slightest.
286 static int ctl2_encode_name(
287 msft_typelib_t
*typelib
, /* [I] The typelib to operate against (used for LCID only). */
288 const char *name
, /* [I] The name string to encode. */
289 char **result
) /* [O] A pointer to a pointer to receive the encoded name. */
292 static char converted_name
[0x104];
296 length
= strlen(name
);
297 memcpy(converted_name
+ 4, name
, length
);
299 converted_name
[length
+ 4] = 0;
302 value
= lhash_val_of_name_sys(typelib
->typelib_header
.varflags
& 0x0f, typelib
->typelib_header
.lcid
, converted_name
+ 4);
304 #ifdef WORDS_BIGENDIAN
305 converted_name
[3] = length
& 0xff;
306 converted_name
[2] = 0x00;
307 converted_name
[1] = value
;
308 converted_name
[0] = value
>> 8;
310 converted_name
[0] = length
& 0xff;
311 converted_name
[1] = 0x00;
312 converted_name
[2] = value
;
313 converted_name
[3] = value
>> 8;
316 for (offset
= (4 - length
) & 3; offset
; offset
--) converted_name
[length
+ offset
+ 3] = 0x57;
318 *result
= converted_name
;
320 return (length
+ 7) & ~3;
323 /****************************************************************************
326 * Encodes a string to a form suitable for storing into a type library or
327 * comparing to a string stored in a type library.
331 * The length of the encoded string, including padding and length fields.
335 * Will throw an exception if string or result are NULL. Is not multithread
336 * safe in the slightest.
338 static int ctl2_encode_string(
339 const char *string
, /* [I] The string to encode. */
340 char **result
) /* [O] A pointer to a pointer to receive the encoded string. */
343 static char converted_string
[0x104];
346 length
= strlen(string
);
347 memcpy(converted_string
+ 2, string
, length
);
349 #ifdef WORDS_BIGENDIAN
350 converted_string
[1] = length
& 0xff;
351 converted_string
[0] = (length
>> 8) & 0xff;
353 converted_string
[0] = length
& 0xff;
354 converted_string
[1] = (length
>> 8) & 0xff;
357 if(length
< 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
358 for(offset
= 0; offset
< 4; offset
++)
359 converted_string
[length
+ offset
+ 2] = 0x57;
362 for (offset
= (4 - (length
+ 2)) & 3; offset
; offset
--) converted_string
[length
+ offset
+ 1] = 0x57;
364 *result
= converted_string
;
366 return (length
+ 5) & ~3;
369 /****************************************************************************
372 * Allocates memory from a segment in a type library.
376 * Success: The offset within the segment of the new data area.
380 * Does not (yet) handle the case where the allocated segment memory needs to grow.
382 static int ctl2_alloc_segment(
383 msft_typelib_t
*typelib
, /* [I] The type library in which to allocate. */
384 enum MSFT_segment_index segment
, /* [I] The segment in which to allocate. */
385 int size
, /* [I] The amount to allocate. */
386 int block_size
) /* [I] Initial allocation block size, or 0 for default. */
390 if(!typelib
->typelib_segment_data
[segment
]) {
391 if (!block_size
) block_size
= 0x2000;
393 typelib
->typelib_segment_block_length
[segment
] = block_size
;
394 typelib
->typelib_segment_data
[segment
] = xmalloc(block_size
);
395 if (!typelib
->typelib_segment_data
[segment
]) return -1;
396 memset(typelib
->typelib_segment_data
[segment
], 0x57, block_size
);
399 while ((typelib
->typelib_segdir
[segment
].length
+ size
) > typelib
->typelib_segment_block_length
[segment
]) {
400 unsigned char *block
;
402 block_size
= typelib
->typelib_segment_block_length
[segment
];
403 block
= xrealloc(typelib
->typelib_segment_data
[segment
], block_size
<< 1);
405 if (segment
== MSFT_SEG_TYPEINFO
) {
406 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
407 msft_typeinfo_t
*typeinfo
;
409 for (typeinfo
= typelib
->typeinfos
; typeinfo
; typeinfo
= typeinfo
->next_typeinfo
) {
410 typeinfo
->typeinfo
= (void *)&block
[((unsigned char *)typeinfo
->typeinfo
) - typelib
->typelib_segment_data
[segment
]];
414 memset(block
+ block_size
, 0x57, block_size
);
415 typelib
->typelib_segment_block_length
[segment
] = block_size
<< 1;
416 typelib
->typelib_segment_data
[segment
] = block
;
419 offset
= typelib
->typelib_segdir
[segment
].length
;
420 typelib
->typelib_segdir
[segment
].length
+= size
;
425 /****************************************************************************
426 * ctl2_alloc_typeinfo
428 * Allocates and initializes a typeinfo structure in a type library.
432 * Success: The offset of the new typeinfo.
433 * Failure: -1 (this is invariably an out of memory condition).
435 static int ctl2_alloc_typeinfo(
436 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
437 int nameoffset
) /* [I] The offset of the name for this typeinfo. */
440 MSFT_TypeInfoBase
*typeinfo
;
442 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEINFO
, sizeof(MSFT_TypeInfoBase
), 0);
444 typelib
->typelib_typeinfo_offsets
[typelib
->typelib_header
.nrtypeinfos
++] = offset
;
446 typeinfo
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_TYPEINFO
] + offset
);
448 typeinfo
->typekind
= (typelib
->typelib_header
.nrtypeinfos
- 1) << 16;
449 typeinfo
->memoffset
= -1; /* should be EOF if no elements */
454 typeinfo
->cElement
= 0;
459 typeinfo
->posguid
= -1;
461 typeinfo
->NameOffset
= nameoffset
;
462 typeinfo
->version
= 0;
463 typeinfo
->docstringoffs
= -1;
464 typeinfo
->helpstringcontext
= 0;
465 typeinfo
->helpcontext
= 0;
466 typeinfo
->oCustData
= -1;
467 typeinfo
->cbSizeVft
= 0;
468 typeinfo
->cImplTypes
= 0;
470 typeinfo
->datatype1
= -1;
471 typeinfo
->datatype2
= 0;
473 typeinfo
->res19
= -1;
478 /****************************************************************************
481 * Allocates and initializes a GUID structure in a type library. Also updates
482 * the GUID hash table as needed.
486 * Success: The offset of the new GUID.
488 static int ctl2_alloc_guid(
489 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
490 MSFT_GuidEntry
*guid
) /* [I] The GUID to store. */
493 MSFT_GuidEntry
*guid_space
;
496 hash_key
= ctl2_hash_guid(&guid
->guid
);
498 offset
= ctl2_find_guid(typelib
, hash_key
, &guid
->guid
);
499 if (offset
!= -1) return offset
;
501 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_GUID
, sizeof(MSFT_GuidEntry
), 0);
503 guid_space
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_GUID
] + offset
);
506 guid_space
->next_hash
= typelib
->typelib_guidhash_segment
[hash_key
];
507 typelib
->typelib_guidhash_segment
[hash_key
] = offset
;
512 /****************************************************************************
515 * Allocates and initializes a name within a type library. Also updates the
516 * name hash table as needed.
520 * Success: The offset within the segment of the new name.
521 * Failure: -1 (this is invariably an out of memory condition).
523 static int ctl2_alloc_name(
524 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
525 const char *name
) /* [I] The name to store. */
529 MSFT_NameIntro
*name_space
;
532 length
= ctl2_encode_name(typelib
, name
, &encoded_name
);
534 offset
= ctl2_find_name(typelib
, encoded_name
);
535 if (offset
!= -1) return offset
;
537 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_NAME
, length
+ 8, 0);
539 name_space
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_NAME
] + offset
);
540 name_space
->hreftype
= -1;
541 name_space
->next_hash
= -1;
542 memcpy(&name_space
->namelen
, encoded_name
, length
);
544 if (typelib
->typelib_namehash_segment
[encoded_name
[2] & 0x7f] != -1)
545 name_space
->next_hash
= typelib
->typelib_namehash_segment
[encoded_name
[2] & 0x7f];
547 typelib
->typelib_namehash_segment
[encoded_name
[2] & 0x7f] = offset
;
549 typelib
->typelib_header
.nametablecount
+= 1;
550 typelib
->typelib_header
.nametablechars
+= *encoded_name
;
555 /****************************************************************************
558 * Allocates and initializes a string in a type library.
562 * Success: The offset within the segment of the new string.
563 * Failure: -1 (this is invariably an out of memory condition).
565 static int ctl2_alloc_string(
566 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
567 const char *string
) /* [I] The string to store. */
571 unsigned char *string_space
;
572 char *encoded_string
;
574 length
= ctl2_encode_string(string
, &encoded_string
);
576 for (offset
= 0; offset
< typelib
->typelib_segdir
[MSFT_SEG_STRING
].length
;
577 offset
+= (((typelib
->typelib_segment_data
[MSFT_SEG_STRING
][offset
+ 1] << 8) |
578 typelib
->typelib_segment_data
[MSFT_SEG_STRING
][offset
+ 0]) + 5) & ~3) {
579 if (!memcmp(encoded_string
, typelib
->typelib_segment_data
[MSFT_SEG_STRING
] + offset
, length
)) return offset
;
582 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_STRING
, length
, 0);
584 string_space
= typelib
->typelib_segment_data
[MSFT_SEG_STRING
] + offset
;
585 memcpy(string_space
, encoded_string
, length
);
590 /****************************************************************************
591 * alloc_msft_importinfo
593 * Allocates and initializes an import information structure in a type library.
597 * Success: The offset of the new importinfo.
598 * Failure: -1 (this is invariably an out of memory condition).
600 static int alloc_msft_importinfo(
601 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
602 MSFT_ImpInfo
*impinfo
) /* [I] The import information to store. */
605 MSFT_ImpInfo
*impinfo_space
;
608 offset
< typelib
->typelib_segdir
[MSFT_SEG_IMPORTINFO
].length
;
609 offset
+= sizeof(MSFT_ImpInfo
)) {
610 if (!memcmp(&(typelib
->typelib_segment_data
[MSFT_SEG_IMPORTINFO
][offset
]),
611 impinfo
, sizeof(MSFT_ImpInfo
))) {
616 impinfo
->flags
|= typelib
->typelib_header
.nimpinfos
++;
618 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_IMPORTINFO
, sizeof(MSFT_ImpInfo
), 0);
620 impinfo_space
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_IMPORTINFO
] + offset
);
621 *impinfo_space
= *impinfo
;
626 /****************************************************************************
629 * Allocates and initializes an import file definition in a type library.
633 * Success: The offset of the new importinfo.
634 * Failure: -1 (this is invariably an out of memory condition).
636 static int alloc_importfile(
637 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
638 int guidoffset
, /* [I] The offset to the GUID for the imported library. */
639 int major_version
, /* [I] The major version number of the imported library. */
640 int minor_version
, /* [I] The minor version number of the imported library. */
641 const char *filename
) /* [I] The filename of the imported library. */
645 MSFT_ImpFile
*importfile
;
646 char *encoded_string
;
648 length
= ctl2_encode_string(filename
, &encoded_string
);
650 encoded_string
[0] <<= 2;
651 encoded_string
[0] |= 1;
653 for (offset
= 0; offset
< typelib
->typelib_segdir
[MSFT_SEG_IMPORTFILES
].length
;
654 offset
+= (((typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
][offset
+ 0xd] << 8) |
655 typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
][offset
+ 0xc]) >> 2) + 0xc) {
656 if (!memcmp(encoded_string
, typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
] + offset
+ 0xc, length
)) return offset
;
659 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_IMPORTFILES
, length
+ 0xc, 0);
661 importfile
= (MSFT_ImpFile
*)&typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
][offset
];
662 importfile
->guid
= guidoffset
;
663 importfile
->lcid
= typelib
->typelib_header
.lcid2
;
664 importfile
->version
= major_version
| (minor_version
<< 16);
665 memcpy(&importfile
->filename
, encoded_string
, length
);
670 static void alloc_importinfo(msft_typelib_t
*typelib
, importinfo_t
*importinfo
)
672 importlib_t
*importlib
= importinfo
->importlib
;
674 chat("alloc_importinfo: %s\n", importinfo
->name
);
676 if(!importlib
->allocated
) {
680 chat("allocating importlib %s\n", importlib
->name
);
682 importlib
->allocated
= -1;
684 memcpy(&guid
.guid
, &importlib
->guid
, sizeof(GUID
));
687 guid_idx
= ctl2_alloc_guid(typelib
, &guid
);
689 alloc_importfile(typelib
, guid_idx
, importlib
->version
&0xffff,
690 importlib
->version
>>16, importlib
->name
);
693 if(importinfo
->offset
== -1 || !(importinfo
->flags
& MSFT_IMPINFO_OFFSET_IS_GUID
)) {
694 MSFT_ImpInfo impinfo
;
696 impinfo
.flags
= importinfo
->flags
;
697 impinfo
.oImpFile
= 0;
699 if(importinfo
->flags
& MSFT_IMPINFO_OFFSET_IS_GUID
) {
703 memcpy(&guid
.guid
, &importinfo
->guid
, sizeof(GUID
));
705 impinfo
.oGuid
= ctl2_alloc_guid(typelib
, &guid
);
707 importinfo
->offset
= alloc_msft_importinfo(typelib
, &impinfo
);
709 typelib
->typelib_segment_data
[MSFT_SEG_GUID
][impinfo
.oGuid
+sizeof(GUID
)]
710 = importinfo
->offset
+1;
712 if(!strcmp(importinfo
->name
, "IDispatch"))
713 typelib
->typelib_header
.dispatchpos
= importinfo
->offset
+1;
715 impinfo
.oGuid
= importinfo
->id
;
716 importinfo
->offset
= alloc_msft_importinfo(typelib
, &impinfo
);
721 static importinfo_t
*find_importinfo(msft_typelib_t
*typelib
, const char *name
)
723 importlib_t
*importlib
;
726 chat("search importlib %s\n", name
);
731 LIST_FOR_EACH_ENTRY( importlib
, &typelib
->typelib
->importlibs
, importlib_t
, entry
)
733 for(i
=0; i
< importlib
->ntypeinfos
; i
++) {
734 if(!strcmp(name
, importlib
->importinfos
[i
].name
)) {
735 chat("Found %s in importlib.\n", name
);
736 return importlib
->importinfos
+i
;
744 static void add_structure_typeinfo(msft_typelib_t
*typelib
, type_t
*structure
);
745 static void add_interface_typeinfo(msft_typelib_t
*typelib
, type_t
*interface
);
746 static void add_enum_typeinfo(msft_typelib_t
*typelib
, type_t
*enumeration
);
747 static void add_coclass_typeinfo(msft_typelib_t
*typelib
, type_t
*cls
);
748 static void add_dispinterface_typeinfo(msft_typelib_t
*typelib
, type_t
*dispinterface
);
751 /****************************************************************************
754 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
755 * segments as needed.
762 static int encode_type(
763 msft_typelib_t
*typelib
, /* [I] The type library in which to encode the TYPEDESC. */
764 int vt
, /* [I] vt to encode */
765 type_t
*type
, /* [I] type */
766 int *encoded_type
, /* [O] The encoded type description. */
767 int *width
, /* [O] The width of the type, or NULL. */
768 int *alignment
, /* [O] The alignment of the type, or NULL. */
769 int *decoded_size
) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
778 chat("encode_type vt %d type %p\n", vt
, type
);
780 default_type
= 0x80000000 | (vt
<< 16) | vt
;
781 if (!width
) width
= &scratch
;
782 if (!alignment
) alignment
= &scratch
;
783 if (!decoded_size
) decoded_size
= &scratch
;
789 *encoded_type
= default_type
;
795 *encoded_type
= 0x80000000 | (VT_I4
<< 16) | VT_INT
;
796 if ((typelib
->typelib_header
.varflags
& 0x0f) == SYS_WIN16
) {
806 *encoded_type
= 0x80000000 | (VT_UI4
<< 16) | VT_UINT
;
807 if ((typelib
->typelib_header
.varflags
& 0x0f) == SYS_WIN16
) {
819 *encoded_type
= default_type
;
829 *encoded_type
= default_type
;
837 *encoded_type
= default_type
;
844 *encoded_type
= default_type
;
850 *encoded_type
= default_type
;
856 *encoded_type
= 0x80000000 | (VT_EMPTY
<< 16) | vt
;
864 *encoded_type
= default_type
;
865 *width
= pointer_size
;
870 *encoded_type
= default_type
;
875 *encoded_type
= 0xfffe0000 | vt
;
876 *width
= pointer_size
;
883 for(next_vt
= 0; is_ptr(type
); type
= type_pointer_get_ref(type
)) {
884 next_vt
= get_type_vt(type_pointer_get_ref(type
));
888 /* if no type found then it must be void */
892 encode_type(typelib
, next_vt
, type_pointer_get_ref(type
),
893 &target_type
, NULL
, NULL
, &child_size
);
894 /* these types already have an implicit pointer, so we don't need to
896 if(next_vt
== VT_DISPATCH
|| next_vt
== VT_UNKNOWN
) {
897 chat("encode_type: skipping ptr\n");
898 *encoded_type
= target_type
;
899 *width
= pointer_size
;
901 *decoded_size
= child_size
;
905 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
906 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
907 if (((typedata
[0] & 0xffff) == VT_PTR
) && (typedata
[1] == target_type
)) break;
910 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
913 if (target_type
& 0x80000000) {
914 mix_field
= ((target_type
>> 16) & 0x3fff) | VT_BYREF
;
916 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][target_type
];
917 mix_field
= ((typedata
[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
920 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
921 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
923 typedata
[0] = (mix_field
<< 16) | VT_PTR
;
924 typedata
[1] = target_type
;
927 *encoded_type
= typeoffset
;
929 *width
= pointer_size
;
931 *decoded_size
= 8 /*sizeof(TYPEDESC)*/ + child_size
;
937 type_t
*element_type
= type_alias_get_aliasee(type_array_get_element(type
));
938 int next_vt
= get_type_vt(element_type
);
940 encode_type(typelib
, next_vt
, type_alias_get_aliasee(type_array_get_element(type
)), &target_type
, NULL
, NULL
, &child_size
);
942 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
943 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
944 if (((typedata
[0] & 0xffff) == VT_SAFEARRAY
) && (typedata
[1] == target_type
)) break;
947 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
950 if (target_type
& 0x80000000) {
951 mix_field
= ((target_type
>> 16) & VT_TYPEMASK
) | VT_ARRAY
;
953 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][target_type
];
954 mix_field
= ((typedata
[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
957 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
958 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
960 typedata
[0] = (mix_field
<< 16) | VT_SAFEARRAY
;
961 typedata
[1] = target_type
;
964 *encoded_type
= typeoffset
;
966 *width
= pointer_size
;
968 *decoded_size
= 8 /*sizeof(TYPEDESC)*/ + child_size
;
976 /* typedef'd types without public attribute aren't included in the typelib */
977 while (type
->typelib_idx
< 0 && type_is_alias(type
) && !is_attr(type
->attrs
, ATTR_PUBLIC
))
978 type
= type_alias_get_aliasee(type
);
980 chat("encode_type: VT_USERDEFINED - type %p name = %s real type %d idx %d\n", type
,
981 type
->name
, type_get_type(type
), type
->typelib_idx
);
983 if(type
->typelib_idx
== -1) {
984 chat("encode_type: trying to ref not added type\n");
985 switch (type_get_type(type
)) {
987 add_structure_typeinfo(typelib
, type
);
990 add_interface_typeinfo(typelib
, type
);
993 add_enum_typeinfo(typelib
, type
);
996 add_coclass_typeinfo(typelib
, type
);
999 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
1000 type_get_type(type
));
1004 typeinfo_offset
= typelib
->typelib_typeinfo_offsets
[type
->typelib_idx
];
1005 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
1006 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1007 if ((typedata
[0] == ((0x7fff << 16) | VT_USERDEFINED
)) && (typedata
[1] == typeinfo_offset
)) break;
1010 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
1011 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
1012 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1014 typedata
[0] = (0x7fff << 16) | VT_USERDEFINED
;
1015 typedata
[1] = typeinfo_offset
;
1018 *encoded_type
= typeoffset
;
1025 error("encode_type: unrecognized type %d.\n", vt
);
1026 *encoded_type
= default_type
;
1035 static void dump_type(type_t
*t
)
1037 chat("dump_type: %p name %s type %d attrs %p\n", t
, t
->name
, type_get_type(t
), t
->attrs
);
1040 static int encode_var(
1041 msft_typelib_t
*typelib
, /* [I] The type library in which to encode the TYPEDESC. */
1042 type_t
*type
, /* [I] The type description to encode. */
1043 var_t
*var
, /* [I] The var to encode. */
1044 int *encoded_type
, /* [O] The encoded type description. */
1045 int *width
, /* [O] The width of the type, or NULL. */
1046 int *alignment
, /* [O] The alignment of the type, or NULL. */
1047 int *decoded_size
) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1056 if (!width
) width
= &scratch
;
1057 if (!alignment
) alignment
= &scratch
;
1058 if (!decoded_size
) decoded_size
= &scratch
;
1061 chat("encode_var: var %p type %p type->name %s\n",
1062 var
, type
, type
->name
? type
->name
: "NULL");
1064 if (is_array(type
) && !type_array_is_decl_as_ptr(type
)) {
1065 int num_dims
, elements
= 1, arrayoffset
;
1071 is_array(atype
) && !type_array_is_decl_as_ptr(atype
);
1072 atype
= type_array_get_element(atype
))
1075 chat("array with %d dimensions\n", num_dims
);
1076 encode_var(typelib
, atype
, var
, &target_type
, width
, alignment
, NULL
);
1077 arrayoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_ARRAYDESC
, (2 + 2 * num_dims
) * sizeof(int), 0);
1078 arraydata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_ARRAYDESC
][arrayoffset
];
1080 arraydata
[0] = target_type
;
1081 arraydata
[1] = num_dims
;
1082 arraydata
[1] |= ((num_dims
* 2 * sizeof(int)) << 16);
1086 is_array(atype
) && !type_array_is_decl_as_ptr(atype
);
1087 atype
= type_array_get_element(atype
))
1089 arraydata
[0] = type_array_get_dim(atype
);
1092 elements
*= type_array_get_dim(atype
);
1095 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
1096 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1098 typedata
[0] = (0x7ffe << 16) | VT_CARRAY
;
1099 typedata
[1] = arrayoffset
;
1101 *encoded_type
= typeoffset
;
1102 *width
= *width
* elements
;
1103 *decoded_size
= 20 /*sizeof(ARRAYDESC)*/ + (num_dims
- 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1107 vt
= get_type_vt(type
);
1109 type_t
*ref
= is_ptr(type
) ?
1110 type_pointer_get_ref(type
) : type_array_get_element(type
);
1111 int skip_ptr
= encode_var(typelib
, ref
, var
,
1112 &target_type
, NULL
, NULL
, &child_size
);
1115 chat("encode_var: skipping ptr\n");
1116 *encoded_type
= target_type
;
1117 *decoded_size
= child_size
;
1118 *width
= pointer_size
;
1123 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
1124 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1125 if (((typedata
[0] & 0xffff) == VT_PTR
) && (typedata
[1] == target_type
)) break;
1128 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
1131 if (target_type
& 0x80000000) {
1132 mix_field
= ((target_type
>> 16) & 0x3fff) | VT_BYREF
;
1134 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][target_type
];
1135 mix_field
= ((typedata
[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1138 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
1139 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1141 typedata
[0] = (mix_field
<< 16) | VT_PTR
;
1142 typedata
[1] = target_type
;
1145 *encoded_type
= typeoffset
;
1147 *width
= pointer_size
;
1149 *decoded_size
= 8 /*sizeof(TYPEDESC)*/ + child_size
;
1155 encode_type(typelib
, vt
, type
, encoded_type
, width
, alignment
, decoded_size
);
1156 /* these types already have an implicit pointer, so we don't need to
1158 if(vt
== VT_DISPATCH
|| vt
== VT_UNKNOWN
) return 2;
1162 static unsigned int get_ulong_val(unsigned int val
, int vt
)
1168 return val
& 0xffff;
1177 static void write_value(msft_typelib_t
* typelib
, int *out
, int vt
, const void *value
)
1195 const unsigned int lv
= get_ulong_val(*(const unsigned int *)value
, vt
);
1196 if((lv
& 0x3ffffff) == lv
) {
1201 int offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATA
, 8, 0);
1202 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
]) = vt
;
1203 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+2], value
, 4);
1204 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+6]) = 0x5757;
1211 const char *s
= (const char *) value
;
1212 int len
= strlen(s
), seg_len
= (len
+ 6 + 3) & ~0x3;
1213 int offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATA
, seg_len
, 0);
1214 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
]) = vt
;
1215 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+2], &len
, sizeof(len
));
1216 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+6], value
, len
);
1218 while(len
< seg_len
) {
1219 *((char *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+len
]) = 0x57;
1227 warning("can't write value of type %d yet\n", vt
);
1232 static HRESULT
set_custdata(msft_typelib_t
*typelib
, REFGUID guid
,
1233 int vt
, const void *value
, int *offset
)
1235 MSFT_GuidEntry guidentry
;
1241 guidentry
.guid
= *guid
;
1243 guidentry
.hreftype
= -1;
1244 guidentry
.next_hash
= -1;
1246 guidoffset
= ctl2_alloc_guid(typelib
, &guidentry
);
1247 write_value(typelib
, &data_out
, vt
, value
);
1249 custoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATAGUID
, 12, 0);
1251 custdata
= (int *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATAGUID
][custoffset
];
1252 custdata
[0] = guidoffset
;
1253 custdata
[1] = data_out
;
1254 custdata
[2] = *offset
;
1255 *offset
= custoffset
;
1260 /* It's possible to have a default value for pointer arguments too.
1261 In this case default value has a referenced type, e.g.
1262 'LONG*' argument gets VT_I4, 'DOUBLE*' - VT_R8. IUnknown* and IDispatch*
1263 are recognised too and stored as VT_UNKNOWN and VT_DISPATCH.
1264 But IUnknown/IDispatch arguments can only have default value of 0
1265 (or expression that resolves to zero) while other pointers can have
1266 any default value. */
1267 static int get_defaultvalue_vt(type_t
*type
)
1269 int vt
= get_type_vt(type
);
1270 if (type_get_type(type
) == TYPE_ENUM
)
1274 vt
= get_type_vt(type
);
1275 if (vt
== VT_PTR
&& is_ptr(type
)) {
1276 vt
= get_type_vt(type_pointer_get_ref(type
));
1277 /* The only acceptable value for pointers to non-basic types
1278 is NULL, it's stored as VT_I4 for both 32 and 64 bit typelibs. */
1279 if (vt
== VT_USERDEFINED
)
1287 static HRESULT
add_func_desc(msft_typeinfo_t
* typeinfo
, var_t
*func
, int index
)
1289 int offset
, name_offset
;
1290 int *typedata
, typedata_size
;
1291 int i
, id
, next_idx
;
1292 int decoded_size
, extra_attr
= 0;
1293 int num_params
= 0, num_optional
= 0, num_defaults
= 0;
1295 unsigned char *namedata
;
1297 unsigned int funcflags
= 0, callconv
= 4 /* CC_STDCALL */;
1298 unsigned int funckind
, invokekind
= 1 /* INVOKE_FUNC */;
1299 int help_context
= 0, help_string_context
= 0, help_string_offset
= -1;
1300 int entry
= -1, entry_is_ord
= 0;
1301 int lcid_retval_count
= 0;
1303 chat("add_func_desc(%p,%d)\n", typeinfo
, index
);
1305 id
= ((0x6000 | (typeinfo
->typeinfo
->datatype2
& 0xffff)) << 16) | index
;
1307 switch(typeinfo
->typekind
) {
1308 case TKIND_DISPATCH
:
1309 funckind
= 0x4; /* FUNC_DISPATCH */
1312 funckind
= 0x3; /* FUNC_STATIC */
1315 funckind
= 0x1; /* FUNC_PUREVIRTUAL */
1319 if (is_local( func
->attrs
)) {
1320 chat("add_func_desc: skipping local function\n");
1324 if (type_get_function_args(func
->type
))
1325 LIST_FOR_EACH_ENTRY( arg
, type_get_function_args(func
->type
), var_t
, entry
)
1328 if (arg
->attrs
) LIST_FOR_EACH_ENTRY( attr
, arg
->attrs
, const attr_t
, entry
) {
1329 if(attr
->type
== ATTR_DEFAULTVALUE
)
1331 else if(attr
->type
== ATTR_OPTIONAL
)
1336 chat("add_func_desc: num of params %d\n", num_params
);
1338 name_offset
= ctl2_alloc_name(typeinfo
->typelib
, func
->name
);
1340 if (func
->attrs
) LIST_FOR_EACH_ENTRY( attr
, func
->attrs
, const attr_t
, entry
) {
1341 expr_t
*expr
= attr
->u
.pval
;
1342 switch(attr
->type
) {
1344 funcflags
|= 0x4; /* FUNCFLAG_FBINDABLE */
1346 case ATTR_DEFAULTBIND
:
1347 funcflags
|= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1349 case ATTR_DEFAULTCOLLELEM
:
1350 funcflags
|= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1352 case ATTR_DISPLAYBIND
:
1353 funcflags
|= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1356 extra_attr
= max(extra_attr
, 3);
1357 if (expr
->type
== EXPR_STRLIT
|| expr
->type
== EXPR_WSTRLIT
)
1358 entry
= ctl2_alloc_string(typeinfo
->typelib
, attr
->u
.pval
);
1364 case ATTR_HELPCONTEXT
:
1365 extra_attr
= max(extra_attr
, 1);
1366 help_context
= expr
->u
.lval
;
1368 case ATTR_HELPSTRING
:
1369 extra_attr
= max(extra_attr
, 2);
1370 help_string_offset
= ctl2_alloc_string(typeinfo
->typelib
, attr
->u
.pval
);
1372 case ATTR_HELPSTRINGCONTEXT
:
1373 extra_attr
= max(extra_attr
, 6);
1374 help_string_context
= expr
->u
.lval
;
1377 funcflags
|= 0x40; /* FUNCFLAG_FHIDDEN */
1382 case ATTR_IMMEDIATEBIND
:
1383 funcflags
|= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1385 case ATTR_NONBROWSABLE
:
1386 funcflags
|= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1391 invokekind
= 0x2; /* INVOKE_PROPERTYGET */
1394 invokekind
= 0x4; /* INVOKE_PROPERTYPUT */
1396 case ATTR_PROPPUTREF
:
1397 invokekind
= 0x8; /* INVOKE_PROPERTYPUTREF */
1399 /* FIXME: FUNCFLAG_FREPLACEABLE */
1400 case ATTR_REQUESTEDIT
:
1401 funcflags
|= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1403 case ATTR_RESTRICTED
:
1404 funcflags
|= 0x1; /* FUNCFLAG_FRESTRICTED */
1407 funcflags
|= 0x2; /* FUNCFLAG_FSOURCE */
1409 case ATTR_UIDEFAULT
:
1410 funcflags
|= 0x200; /* FUNCFLAG_FUIDEFAULT */
1412 case ATTR_USESGETLASTERROR
:
1413 funcflags
|= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1416 if (num_optional
|| num_defaults
)
1417 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1426 /* allocate type data space for us */
1427 typedata_size
= 0x18 + extra_attr
* sizeof(int) + (num_params
* (num_defaults
? 16 : 12));
1429 if (!typeinfo
->func_data
) {
1430 typeinfo
->func_data
= xmalloc(0x100);
1431 typeinfo
->func_data_allocated
= 0x100;
1432 typeinfo
->func_data
[0] = 0;
1435 if(typeinfo
->func_data
[0] + typedata_size
+ sizeof(int) > typeinfo
->func_data_allocated
) {
1436 typeinfo
->func_data_allocated
= max(typeinfo
->func_data_allocated
* 2,
1437 typeinfo
->func_data
[0] + typedata_size
+ sizeof(int));
1438 typeinfo
->func_data
= xrealloc(typeinfo
->func_data
, typeinfo
->func_data_allocated
);
1441 offset
= typeinfo
->func_data
[0];
1442 typeinfo
->func_data
[0] += typedata_size
;
1443 typedata
= typeinfo
->func_data
+ (offset
>> 2) + 1;
1446 /* find func with the same name - if it exists use its id */
1447 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
& 0xffff); i
++) {
1448 if(name_offset
== typeinfo
->func_names
[i
]) {
1449 id
= typeinfo
->func_indices
[i
];
1454 /* find the first func with the same id and link via the hiword of typedata[4] */
1456 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
& 0xffff); i
++) {
1457 if(id
== typeinfo
->func_indices
[i
]) {
1458 next_idx
= typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] >> 16;
1459 typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] &= 0xffff;
1460 typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] |= (index
<< 16);
1465 /* fill out the basic type information */
1466 typedata
[0] = typedata_size
| (index
<< 16);
1467 encode_var(typeinfo
->typelib
, type_function_get_rettype(func
->type
), func
, &typedata
[1], NULL
, NULL
, &decoded_size
);
1468 typedata
[2] = funcflags
;
1469 typedata
[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size
) << 16) | typeinfo
->typeinfo
->cbSizeVft
;
1470 typedata
[4] = (next_idx
<< 16) | (callconv
<< 8) | (invokekind
<< 3) | funckind
;
1471 if(num_defaults
) typedata
[4] |= 0x1000;
1472 if(entry_is_ord
) typedata
[4] |= 0x2000;
1473 typedata
[5] = (num_optional
<< 16) | num_params
;
1475 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1476 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1477 typedata
[3] += (16 /*sizeof(ELEMDESC)*/ * num_params
) << 16;
1478 typedata
[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults
) << 16;
1480 switch(extra_attr
) {
1481 case 6: typedata
[11] = help_string_context
;
1482 case 5: typedata
[10] = -1;
1483 case 4: typedata
[9] = -1;
1484 case 3: typedata
[8] = entry
;
1485 case 2: typedata
[7] = help_string_offset
;
1486 case 1: typedata
[6] = help_context
;
1490 warning("unknown number of optional attrs\n");
1493 if (type_get_function_args(func
->type
))
1496 LIST_FOR_EACH_ENTRY( arg
, type_get_function_args(func
->type
), var_t
, entry
)
1499 int *paramdata
= typedata
+ 6 + extra_attr
+ (num_defaults
? num_params
: 0) + i
* 3;
1500 int *defaultdata
= num_defaults
? typedata
+ 6 + extra_attr
+ i
: NULL
;
1502 if(defaultdata
) *defaultdata
= -1;
1504 encode_var(typeinfo
->typelib
, arg
->type
, arg
, paramdata
, NULL
, NULL
, &decoded_size
);
1505 if (arg
->attrs
) LIST_FOR_EACH_ENTRY( attr
, arg
->attrs
, const attr_t
, entry
) {
1506 switch(attr
->type
) {
1507 case ATTR_DEFAULTVALUE
:
1510 expr_t
*expr
= (expr_t
*)attr
->u
.pval
;
1511 vt
= get_defaultvalue_vt(arg
->type
);
1512 paramflags
|= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1513 if (expr
->type
== EXPR_STRLIT
|| expr
->type
== EXPR_WSTRLIT
)
1515 if (vt
!= VT_BSTR
) error("string default value applied to non-string type\n");
1516 chat("default value '%s'\n", expr
->u
.sval
);
1517 write_value(typeinfo
->typelib
, defaultdata
, vt
, expr
->u
.sval
);
1521 chat("default value %d\n", expr
->cval
);
1522 write_value(typeinfo
->typelib
, defaultdata
, vt
, &expr
->cval
);
1527 paramflags
|= 0x01; /* PARAMFLAG_FIN */
1530 paramflags
|= 0x10; /* PARAMFLAG_FOPT */
1533 paramflags
|= 0x02; /* PARAMFLAG_FOUT */
1535 case ATTR_PARAMLCID
:
1536 paramflags
|= 0x04; /* PARAMFLAG_LCID */
1537 lcid_retval_count
++;
1540 paramflags
|= 0x08; /* PARAMFLAG_FRETVAL */
1541 lcid_retval_count
++;
1544 chat("unhandled param attr %d\n", attr
->type
);
1549 paramdata
[2] = paramflags
;
1550 typedata
[3] += decoded_size
<< 16;
1556 if(lcid_retval_count
== 1)
1557 typedata
[4] |= 0x4000;
1558 else if(lcid_retval_count
== 2)
1559 typedata
[4] |= 0x8000;
1561 if(typeinfo
->funcs_allocated
== 0) {
1562 typeinfo
->funcs_allocated
= 10;
1563 typeinfo
->func_indices
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1564 typeinfo
->func_names
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1565 typeinfo
->func_offsets
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1567 if(typeinfo
->funcs_allocated
== (typeinfo
->typeinfo
->cElement
& 0xffff)) {
1568 typeinfo
->funcs_allocated
*= 2;
1569 typeinfo
->func_indices
= xrealloc(typeinfo
->func_indices
, typeinfo
->funcs_allocated
* sizeof(int));
1570 typeinfo
->func_names
= xrealloc(typeinfo
->func_names
, typeinfo
->funcs_allocated
* sizeof(int));
1571 typeinfo
->func_offsets
= xrealloc(typeinfo
->func_offsets
, typeinfo
->funcs_allocated
* sizeof(int));
1574 /* update the index data */
1575 typeinfo
->func_indices
[typeinfo
->typeinfo
->cElement
& 0xffff] = id
;
1576 typeinfo
->func_offsets
[typeinfo
->typeinfo
->cElement
& 0xffff] = offset
;
1577 typeinfo
->func_names
[typeinfo
->typeinfo
->cElement
& 0xffff] = name_offset
;
1580 if (!typeinfo
->typeinfo
->res2
) typeinfo
->typeinfo
->res2
= 0x20;
1581 typeinfo
->typeinfo
->res2
<<= 1;
1583 if (index
< 2) typeinfo
->typeinfo
->res2
+= num_params
<< 4;
1585 if (typeinfo
->typeinfo
->res3
== -1) typeinfo
->typeinfo
->res3
= 0;
1586 typeinfo
->typeinfo
->res3
+= 0x38 + num_params
* 0x10;
1587 if(num_defaults
) typeinfo
->typeinfo
->res3
+= num_params
* 0x4;
1589 /* adjust size of VTBL */
1590 if(funckind
!= 0x3 /* FUNC_STATIC */)
1591 typeinfo
->typeinfo
->cbSizeVft
+= pointer_size
;
1593 /* Increment the number of function elements */
1594 typeinfo
->typeinfo
->cElement
+= 1;
1596 namedata
= typeinfo
->typelib
->typelib_segment_data
[MSFT_SEG_NAME
] + name_offset
;
1597 if (*((INT
*)namedata
) == -1) {
1598 *((INT
*)namedata
) = typeinfo
->typelib
->typelib_typeinfo_offsets
[typeinfo
->typeinfo
->typekind
>> 16];
1599 if(typeinfo
->typekind
== TKIND_MODULE
)
1600 namedata
[9] |= 0x10;
1602 namedata
[9] &= ~0x10;
1604 if(typeinfo
->typekind
== TKIND_MODULE
)
1605 namedata
[9] |= 0x20;
1607 if (type_get_function_args(func
->type
))
1610 LIST_FOR_EACH_ENTRY( arg
, type_get_function_args(func
->type
), var_t
, entry
)
1612 /* don't give the last arg of a [propput*] func a name */
1613 if(i
!= num_params
- 1 || (invokekind
!= 0x4 /* INVOKE_PROPERTYPUT */ && invokekind
!= 0x8 /* INVOKE_PROPERTYPUTREF */))
1615 int *paramdata
= typedata
+ 6 + extra_attr
+ (num_defaults
? num_params
: 0) + i
* 3;
1616 offset
= ctl2_alloc_name(typeinfo
->typelib
, arg
->name
);
1617 paramdata
[1] = offset
;
1625 static HRESULT
add_var_desc(msft_typeinfo_t
*typeinfo
, UINT index
, var_t
* var
)
1628 unsigned int typedata_size
;
1632 int var_type_size
, var_kind
= 0 /* VAR_PERINSTANCE */;
1636 unsigned char *namedata
;
1637 int var_num
= (typeinfo
->typeinfo
->cElement
>> 16) & 0xffff;
1639 chat("add_var_desc(%d, %s)\n", index
, var
->name
);
1641 id
= 0x40000000 + index
;
1643 if (var
->attrs
) LIST_FOR_EACH_ENTRY( attr
, var
->attrs
, const attr_t
, entry
) {
1644 expr_t
*expr
= attr
->u
.pval
;
1645 switch(attr
->type
) {
1647 varflags
|= 0x04; /* VARFLAG_FBINDABLE */
1649 case ATTR_DEFAULTBIND
:
1650 varflags
|= 0x20; /* VARFLAG_FDEFAULTBIND */
1652 case ATTR_DEFAULTCOLLELEM
:
1653 varflags
|= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1655 case ATTR_DISPLAYBIND
:
1656 varflags
|= 0x10; /* VARFLAG_FDISPLAYBIND */
1659 varflags
|= 0x40; /* VARFLAG_FHIDDEN */
1664 case ATTR_IMMEDIATEBIND
:
1665 varflags
|= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1667 case ATTR_NONBROWSABLE
:
1668 varflags
|= 0x400; /* VARFLAG_FNONBROWSABLE */
1671 varflags
|= 0x01; /* VARFLAG_FREADONLY */
1673 /* FIXME: VARFLAG_FREPLACEABLE */
1674 case ATTR_REQUESTEDIT
:
1675 varflags
|= 0x08; /* VARFLAG_FREQUESTEDIT */
1677 case ATTR_RESTRICTED
:
1678 varflags
|= 0x80; /* VARFLAG_FRESTRICTED */
1681 varflags
|= 0x02; /* VARFLAG_FSOURCE */
1683 case ATTR_UIDEFAULT
:
1684 varflags
|= 0x0200; /* VARFLAG_FUIDEFAULT */
1691 /* allocate type data space for us */
1692 typedata_size
= 0x14;
1694 if (!typeinfo
->var_data
) {
1695 typeinfo
->var_data
= xmalloc(0x100);
1696 typeinfo
->var_data_allocated
= 0x100;
1697 typeinfo
->var_data
[0] = 0;
1700 if(typeinfo
->var_data
[0] + typedata_size
+ sizeof(int) > typeinfo
->var_data_allocated
) {
1701 typeinfo
->var_data_allocated
= max(typeinfo
->var_data_allocated
* 2,
1702 typeinfo
->var_data
[0] + typedata_size
+ sizeof(int));
1703 typeinfo
->var_data
= xrealloc(typeinfo
->var_data
, typeinfo
->var_data_allocated
);
1706 offset
= typeinfo
->var_data
[0];
1707 typeinfo
->var_data
[0] += typedata_size
;
1708 typedata
= typeinfo
->var_data
+ (offset
>> 2) + 1;
1710 /* fill out the basic type information */
1711 typedata
[0] = typedata_size
| (index
<< 16);
1712 typedata
[2] = varflags
;
1713 typedata
[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1715 if(typeinfo
->vars_allocated
== 0) {
1716 typeinfo
->vars_allocated
= 10;
1717 typeinfo
->var_indices
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1718 typeinfo
->var_names
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1719 typeinfo
->var_offsets
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1721 if(typeinfo
->vars_allocated
== var_num
) {
1722 typeinfo
->vars_allocated
*= 2;
1723 typeinfo
->var_indices
= xrealloc(typeinfo
->var_indices
, typeinfo
->vars_allocated
* sizeof(int));
1724 typeinfo
->var_names
= xrealloc(typeinfo
->var_names
, typeinfo
->vars_allocated
* sizeof(int));
1725 typeinfo
->var_offsets
= xrealloc(typeinfo
->var_offsets
, typeinfo
->vars_allocated
* sizeof(int));
1727 /* update the index data */
1728 typeinfo
->var_indices
[var_num
] = id
;
1729 typeinfo
->var_names
[var_num
] = -1;
1730 typeinfo
->var_offsets
[var_num
] = offset
;
1732 /* figure out type widths and whatnot */
1733 encode_var(typeinfo
->typelib
, var
->type
, var
, &typedata
[1], &var_datawidth
,
1734 &var_alignment
, &var_type_size
);
1736 /* pad out starting position to data width */
1737 typeinfo
->datawidth
+= var_alignment
- 1;
1738 typeinfo
->datawidth
&= ~(var_alignment
- 1);
1740 switch(typeinfo
->typekind
) {
1742 write_value(typeinfo
->typelib
, &typedata
[4], VT_I4
, &var
->eval
->cval
);
1743 var_kind
= 2; /* VAR_CONST */
1744 var_type_size
+= 16; /* sizeof(VARIANT) */
1745 typeinfo
->datawidth
= var_datawidth
;
1748 typedata
[4] = typeinfo
->datawidth
;
1749 typeinfo
->datawidth
+= var_datawidth
;
1751 case TKIND_DISPATCH
:
1752 var_kind
= 3; /* VAR_DISPATCH */
1753 typeinfo
->datawidth
= pointer_size
;
1757 error("add_var_desc: unhandled type kind %d\n", typeinfo
->typekind
);
1761 /* add type description size to total required allocation */
1762 typedata
[3] += var_type_size
<< 16 | var_kind
;
1764 /* fix type alignment */
1765 alignment
= (typeinfo
->typeinfo
->typekind
>> 11) & 0x1f;
1766 if (alignment
< var_alignment
) {
1767 alignment
= var_alignment
;
1768 typeinfo
->typeinfo
->typekind
&= ~0xffc0;
1769 typeinfo
->typeinfo
->typekind
|= alignment
<< 11 | alignment
<< 6;
1773 if (!typeinfo
->typeinfo
->res2
) typeinfo
->typeinfo
->res2
= 0x1a;
1774 if ((index
== 0) || (index
== 1) || (index
== 2) || (index
== 4) || (index
== 9)) {
1775 typeinfo
->typeinfo
->res2
<<= 1;
1779 if (typeinfo
->typeinfo
->res3
== -1) typeinfo
->typeinfo
->res3
= 0;
1780 typeinfo
->typeinfo
->res3
+= 0x2c;
1782 /* increment the number of variable elements */
1783 typeinfo
->typeinfo
->cElement
+= 0x10000;
1785 /* pad data width to alignment */
1786 typeinfo
->typeinfo
->size
= (typeinfo
->datawidth
+ (alignment
- 1)) & ~(alignment
- 1);
1788 offset
= ctl2_alloc_name(typeinfo
->typelib
, var
->name
);
1789 if (offset
== -1) return E_OUTOFMEMORY
;
1791 namedata
= typeinfo
->typelib
->typelib_segment_data
[MSFT_SEG_NAME
] + offset
;
1792 if (*((INT
*)namedata
) == -1) {
1793 *((INT
*)namedata
) = typeinfo
->typelib
->typelib_typeinfo_offsets
[typeinfo
->typeinfo
->typekind
>> 16];
1794 if(typeinfo
->typekind
!= TKIND_DISPATCH
)
1795 namedata
[9] |= 0x10;
1797 namedata
[9] &= ~0x10;
1799 if (typeinfo
->typekind
== TKIND_ENUM
) {
1800 namedata
[9] |= 0x20;
1802 typeinfo
->var_names
[var_num
] = offset
;
1807 static HRESULT
add_impl_type(msft_typeinfo_t
*typeinfo
, type_t
*ref
, importinfo_t
*importinfo
)
1810 alloc_importinfo(typeinfo
->typelib
, importinfo
);
1811 typeinfo
->typeinfo
->datatype1
= importinfo
->offset
+1;
1813 if(ref
->typelib_idx
== -1)
1814 add_interface_typeinfo(typeinfo
->typelib
, ref
);
1815 if(ref
->typelib_idx
== -1)
1816 error("add_impl_type: unable to add inherited interface\n");
1818 typeinfo
->typeinfo
->datatype1
= typeinfo
->typelib
->typelib_typeinfo_offsets
[ref
->typelib_idx
];
1821 typeinfo
->typeinfo
->cImplTypes
++;
1825 static msft_typeinfo_t
*create_msft_typeinfo(msft_typelib_t
*typelib
, enum type_kind kind
,
1826 const char *name
, const attr_list_t
*attrs
)
1829 msft_typeinfo_t
*msft_typeinfo
;
1831 int typeinfo_offset
;
1832 MSFT_TypeInfoBase
*typeinfo
;
1833 MSFT_GuidEntry guidentry
;
1835 chat("create_msft_typeinfo: name %s kind %d\n", name
, kind
);
1837 msft_typeinfo
= xmalloc(sizeof(*msft_typeinfo
));
1838 memset( msft_typeinfo
, 0, sizeof(*msft_typeinfo
) );
1840 msft_typeinfo
->typelib
= typelib
;
1842 nameoffset
= ctl2_alloc_name(typelib
, name
);
1843 typeinfo_offset
= ctl2_alloc_typeinfo(typelib
, nameoffset
);
1844 typeinfo
= (MSFT_TypeInfoBase
*)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEINFO
][typeinfo_offset
];
1846 typelib
->typelib_segment_data
[MSFT_SEG_NAME
][nameoffset
+ 9] = 0x38;
1847 *((int *)&typelib
->typelib_segment_data
[MSFT_SEG_NAME
][nameoffset
]) = typeinfo_offset
;
1849 msft_typeinfo
->typekind
= kind
;
1850 msft_typeinfo
->typeinfo
= typeinfo
;
1852 typeinfo
->typekind
|= kind
| 0x20;
1854 if(kind
== TKIND_COCLASS
)
1855 typeinfo
->flags
|= 0x2; /* TYPEFLAG_FCANCREATE */
1857 if (attrs
) LIST_FOR_EACH_ENTRY( attr
, attrs
, const attr_t
, entry
) {
1858 switch(attr
->type
) {
1859 case ATTR_AGGREGATABLE
:
1860 if (kind
== TKIND_COCLASS
)
1861 typeinfo
->flags
|= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1864 case ATTR_APPOBJECT
:
1865 if (kind
== TKIND_COCLASS
)
1866 typeinfo
->flags
|= 0x1; /* TYPEFLAG_FAPPOBJECT */
1870 if (kind
== TKIND_COCLASS
)
1871 typeinfo
->flags
|= 0x20; /* TYPEFLAG_FCONTROL */
1876 int offset
= ctl2_alloc_string(typelib
, attr
->u
.pval
);
1877 typeinfo
->datatype1
= offset
;
1882 /* FIXME: check interface is compatible */
1883 typeinfo
->typekind
= (typeinfo
->typekind
& ~0xff) | 0x34;
1884 typeinfo
->flags
|= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1887 case ATTR_HELPCONTEXT
:
1889 expr_t
*expr
= (expr_t
*)attr
->u
.pval
;
1890 typeinfo
->helpcontext
= expr
->cval
;
1893 case ATTR_HELPSTRING
:
1895 int offset
= ctl2_alloc_string(typelib
, attr
->u
.pval
);
1896 if (offset
== -1) break;
1897 typeinfo
->docstringoffs
= offset
;
1900 case ATTR_HELPSTRINGCONTEXT
:
1902 expr_t
*expr
= (expr_t
*)attr
->u
.pval
;
1903 typeinfo
->helpstringcontext
= expr
->cval
;
1907 typeinfo
->flags
|= 0x10; /* TYPEFLAG_FHIDDEN */
1911 typeinfo
->flags
|= 0x04; /* TYPEFLAG_FLICENSED */
1914 case ATTR_NONCREATABLE
:
1915 typeinfo
->flags
&= ~0x2; /* TYPEFLAG_FCANCREATE */
1918 case ATTR_NONEXTENSIBLE
:
1919 typeinfo
->flags
|= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1922 case ATTR_OLEAUTOMATION
:
1923 typeinfo
->flags
|= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1926 /* FIXME: TYPEFLAG_FPREDCLID */
1929 typeinfo
->flags
|= 0x4000; /* TYPEFLAG_FPROXY */
1932 /* FIXME: TYPEFLAG_FREPLACEABLE */
1934 case ATTR_RESTRICTED
:
1935 typeinfo
->flags
|= 0x200; /* TYPEFLAG_FRESTRICTED */
1939 guidentry
.guid
= *(GUID
*)attr
->u
.pval
;
1940 guidentry
.hreftype
= typelib
->typelib_typeinfo_offsets
[typeinfo
->typekind
>> 16];
1941 guidentry
.next_hash
= -1;
1942 typeinfo
->posguid
= ctl2_alloc_guid(typelib
, &guidentry
);
1944 if (IsEqualIID(guid
, &IID_IDispatch
)) {
1945 typelib
->typelib_header
.dispatchpos
= typelib
->typelib_typeinfo_offsets
[typeinfo
->typekind
>> 16];
1951 typeinfo
->version
= attr
->u
.ival
;
1959 if (typelib
->last_typeinfo
) typelib
->last_typeinfo
->next_typeinfo
= msft_typeinfo
;
1960 typelib
->last_typeinfo
= msft_typeinfo
;
1961 if (!typelib
->typeinfos
) typelib
->typeinfos
= msft_typeinfo
;
1964 return msft_typeinfo
;
1967 static void add_dispatch(msft_typelib_t
*typelib
)
1969 int guid_offset
, impfile_offset
;
1970 MSFT_GuidEntry guidentry
;
1971 MSFT_ImpInfo impinfo
;
1972 GUID stdole
= {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1973 GUID iid_idispatch
= {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1975 if(typelib
->typelib_header
.dispatchpos
!= -1) return;
1977 guidentry
.guid
= stdole
;
1978 guidentry
.hreftype
= 2;
1979 guidentry
.next_hash
= -1;
1980 guid_offset
= ctl2_alloc_guid(typelib
, &guidentry
);
1981 impfile_offset
= alloc_importfile(typelib
, guid_offset
, 2, 0, "stdole2.tlb");
1983 guidentry
.guid
= iid_idispatch
;
1984 guidentry
.hreftype
= 1;
1985 guidentry
.next_hash
= -1;
1986 impinfo
.flags
= TKIND_INTERFACE
<< 24 | MSFT_IMPINFO_OFFSET_IS_GUID
;
1987 impinfo
.oImpFile
= impfile_offset
;
1988 impinfo
.oGuid
= ctl2_alloc_guid(typelib
, &guidentry
);
1989 typelib
->typelib_header
.dispatchpos
= alloc_msft_importinfo(typelib
, &impinfo
) | 0x01;
1992 static void add_dispinterface_typeinfo(msft_typelib_t
*typelib
, type_t
*dispinterface
)
1997 msft_typeinfo_t
*msft_typeinfo
;
1999 if (-1 < dispinterface
->typelib_idx
)
2002 dispinterface
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2003 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_DISPATCH
, dispinterface
->name
,
2004 dispinterface
->attrs
);
2006 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2007 msft_typeinfo
->typeinfo
->typekind
|= 0x2100;
2009 msft_typeinfo
->typeinfo
->flags
|= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2010 add_dispatch(typelib
);
2011 msft_typeinfo
->typeinfo
->cImplTypes
= 1;
2013 /* count the no of methods, as the variable indices come after the funcs */
2014 if (dispinterface
->details
.iface
->disp_methods
)
2015 LIST_FOR_EACH_ENTRY( func
, dispinterface
->details
.iface
->disp_methods
, var_t
, entry
)
2018 if (type_dispiface_get_props(dispinterface
))
2019 LIST_FOR_EACH_ENTRY( var
, type_dispiface_get_props(dispinterface
), var_t
, entry
)
2020 add_var_desc(msft_typeinfo
, idx
++, var
);
2022 if (type_dispiface_get_methods(dispinterface
))
2025 LIST_FOR_EACH_ENTRY( func
, type_dispiface_get_methods(dispinterface
), var_t
, entry
)
2026 if(add_func_desc(msft_typeinfo
, func
, idx
) == S_OK
)
2031 static void add_interface_typeinfo(msft_typelib_t
*typelib
, type_t
*interface
)
2034 const statement_t
*stmt_func
;
2036 msft_typeinfo_t
*msft_typeinfo
;
2037 importinfo_t
*ref_importinfo
= NULL
;
2038 int num_parents
= 0, num_funcs
= 0;
2040 const type_t
*derived
;
2042 if (-1 < interface
->typelib_idx
)
2045 if (!interface
->details
.iface
)
2047 error( "interface %s is referenced but not defined\n", interface
->name
);
2051 if (is_attr(interface
->attrs
, ATTR_DISPINTERFACE
)) {
2052 add_dispinterface_typeinfo(typelib
, interface
);
2056 /* midl adds the parent interface first, unless the parent itself
2057 has no parent (i.e. it stops before IUnknown). */
2059 inherit
= type_iface_get_inherit(interface
);
2062 ref_importinfo
= find_importinfo(typelib
, inherit
->name
);
2064 if(!ref_importinfo
&& type_iface_get_inherit(inherit
) &&
2065 inherit
->typelib_idx
== -1)
2066 add_interface_typeinfo(typelib
, inherit
);
2069 interface
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2070 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_INTERFACE
, interface
->name
, interface
->attrs
);
2071 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2072 msft_typeinfo
->typeinfo
->typekind
|= 0x2200;
2074 for (derived
= inherit
; derived
; derived
= type_iface_get_inherit(derived
))
2075 if (derived
->name
&& !strcmp(derived
->name
, "IDispatch"))
2076 msft_typeinfo
->typeinfo
->flags
|= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2078 /* can't be dual if it doesn't derive from IDispatch */
2079 if (!(msft_typeinfo
->typeinfo
->flags
& 0x1000)) /* TYPEFLAG_FDISPATCHABLE */
2080 msft_typeinfo
->typeinfo
->flags
&= ~0x40; /* TYPEFLAG_FDUAL */
2082 if(type_iface_get_inherit(interface
))
2083 add_impl_type(msft_typeinfo
, type_iface_get_inherit(interface
),
2086 /* count the number of inherited interfaces and non-local functions */
2087 for(ref
= inherit
; ref
; ref
= type_iface_get_inherit(ref
)) {
2089 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(ref
) ) {
2090 var_t
*func
= stmt_func
->u
.var
;
2091 if (!is_local(func
->attrs
)) num_funcs
++;
2094 msft_typeinfo
->typeinfo
->datatype2
= num_funcs
<< 16 | num_parents
;
2095 msft_typeinfo
->typeinfo
->cbSizeVft
= num_funcs
* pointer_size
;
2097 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(interface
) ) {
2098 var_t
*func
= stmt_func
->u
.var
;
2099 if(add_func_desc(msft_typeinfo
, func
, idx
) == S_OK
)
2104 static void add_structure_typeinfo(msft_typelib_t
*typelib
, type_t
*structure
)
2108 msft_typeinfo_t
*msft_typeinfo
;
2110 if (-1 < structure
->typelib_idx
)
2113 structure
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2114 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_RECORD
, structure
->name
, structure
->attrs
);
2115 msft_typeinfo
->typeinfo
->size
= 0;
2117 if (type_struct_get_fields(structure
))
2118 LIST_FOR_EACH_ENTRY( cur
, type_struct_get_fields(structure
), var_t
, entry
)
2119 add_var_desc(msft_typeinfo
, idx
++, cur
);
2122 static void add_enum_typeinfo(msft_typelib_t
*typelib
, type_t
*enumeration
)
2126 msft_typeinfo_t
*msft_typeinfo
;
2128 if (-1 < enumeration
->typelib_idx
)
2131 enumeration
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2132 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_ENUM
, enumeration
->name
, enumeration
->attrs
);
2133 msft_typeinfo
->typeinfo
->size
= 0;
2135 if (type_enum_get_values(enumeration
))
2136 LIST_FOR_EACH_ENTRY( cur
, type_enum_get_values(enumeration
), var_t
, entry
)
2137 add_var_desc(msft_typeinfo
, idx
++, cur
);
2140 static void add_typedef_typeinfo(msft_typelib_t
*typelib
, type_t
*tdef
)
2142 msft_typeinfo_t
*msft_typeinfo
;
2145 if (-1 < tdef
->typelib_idx
)
2148 tdef
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2149 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_ALIAS
, tdef
->name
, tdef
->attrs
);
2150 encode_type(typelib
, get_type_vt(type_alias_get_aliasee(tdef
)),
2151 type_alias_get_aliasee(tdef
),
2152 &msft_typeinfo
->typeinfo
->datatype1
,
2153 &msft_typeinfo
->typeinfo
->size
,
2154 &alignment
, &msft_typeinfo
->typeinfo
->datatype2
);
2155 msft_typeinfo
->typeinfo
->typekind
|= (alignment
<< 11 | alignment
<< 6);
2158 static void add_coclass_typeinfo(msft_typelib_t
*typelib
, type_t
*cls
)
2160 msft_typeinfo_t
*msft_typeinfo
;
2162 int num_ifaces
= 0, offset
, i
;
2163 MSFT_RefRecord
*ref
, *first
= NULL
, *first_source
= NULL
;
2164 int have_default
= 0, have_default_source
= 0;
2166 ifref_list_t
*ifaces
;
2168 if (-1 < cls
->typelib_idx
)
2171 cls
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2172 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_COCLASS
, cls
->name
, cls
->attrs
);
2174 ifaces
= type_coclass_get_ifaces(cls
);
2175 if (ifaces
) LIST_FOR_EACH_ENTRY( iref
, ifaces
, ifref_t
, entry
) num_ifaces
++;
2177 offset
= msft_typeinfo
->typeinfo
->datatype1
= ctl2_alloc_segment(typelib
, MSFT_SEG_REFERENCES
,
2178 num_ifaces
* sizeof(*ref
), 0);
2181 if (ifaces
) LIST_FOR_EACH_ENTRY( iref
, ifaces
, ifref_t
, entry
) {
2182 if(iref
->iface
->typelib_idx
== -1)
2183 add_interface_typeinfo(typelib
, iref
->iface
);
2184 ref
= (MSFT_RefRecord
*) (typelib
->typelib_segment_data
[MSFT_SEG_REFERENCES
] + offset
+ i
* sizeof(*ref
));
2185 ref
->reftype
= typelib
->typelib_typeinfo_offsets
[iref
->iface
->typelib_idx
];
2187 ref
->oCustData
= -1;
2189 if(i
< num_ifaces
- 1)
2190 ref
->onext
= offset
+ (i
+ 1) * sizeof(*ref
);
2192 if (iref
->attrs
) LIST_FOR_EACH_ENTRY( attr
, iref
->attrs
, const attr_t
, entry
) {
2193 switch(attr
->type
) {
2195 ref
->flags
|= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2197 case ATTR_DEFAULTVTABLE
:
2198 ref
->flags
|= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2200 case ATTR_RESTRICTED
:
2201 ref
->flags
|= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2204 ref
->flags
|= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2207 warning("add_coclass_typeinfo: unhandled attr %d\n", attr
->type
);
2210 if(ref
->flags
& 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2211 if(ref
->flags
& 0x2) /* IMPLTYPEFLAG_SOURCE */
2212 have_default_source
= 1;
2217 /* If the interface is non-restricted and we haven't already had one then
2218 remember it so that we can use it as a default later */
2219 if((ref
->flags
& 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2220 if(ref
->flags
& 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2230 /* If we haven't had a default interface, then set the default flags on the
2232 if(!have_default
&& first
)
2233 first
->flags
|= 0x1;
2234 if(!have_default_source
&& first_source
)
2235 first_source
->flags
|= 0x1;
2237 msft_typeinfo
->typeinfo
->cImplTypes
= num_ifaces
;
2238 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2239 msft_typeinfo
->typeinfo
->typekind
|= 0x2200;
2242 static void add_module_typeinfo(msft_typelib_t
*typelib
, type_t
*module
)
2245 const statement_t
*stmt
;
2246 msft_typeinfo_t
*msft_typeinfo
;
2248 if (-1 < module
->typelib_idx
)
2251 module
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2252 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_MODULE
, module
->name
, module
->attrs
);
2253 msft_typeinfo
->typeinfo
->typekind
|= 0x0a00;
2255 STATEMENTS_FOR_EACH_FUNC( stmt
, module
->details
.module
->stmts
) {
2256 var_t
*func
= stmt
->u
.var
;
2257 if(add_func_desc(msft_typeinfo
, func
, idx
) == S_OK
)
2261 msft_typeinfo
->typeinfo
->size
= idx
;
2264 static void add_type_typeinfo(msft_typelib_t
*typelib
, type_t
*type
)
2266 switch (type_get_type(type
)) {
2267 case TYPE_INTERFACE
:
2268 add_interface_typeinfo(typelib
, type
);
2271 add_structure_typeinfo(typelib
, type
);
2274 add_enum_typeinfo(typelib
, type
);
2277 add_coclass_typeinfo(typelib
, type
);
2283 error("add_entry: unhandled type 0x%x for %s\n",
2284 type_get_type(type
), type
->name
);
2289 static void add_entry(msft_typelib_t
*typelib
, const statement_t
*stmt
)
2291 switch(stmt
->type
) {
2296 case STMT_DECLARATION
:
2297 /* not included in typelib */
2299 case STMT_IMPORTLIB
:
2300 /* not processed here */
2304 const type_list_t
*type_entry
= stmt
->u
.type_list
;
2305 for (; type_entry
; type_entry
= type_entry
->next
) {
2306 /* if the type is public then add the typedef, otherwise attempt
2307 * to add the aliased type */
2308 if (is_attr(type_entry
->type
->attrs
, ATTR_PUBLIC
))
2309 add_typedef_typeinfo(typelib
, type_entry
->type
);
2311 add_type_typeinfo(typelib
, type_alias_get_aliasee(type_entry
->type
));
2316 add_module_typeinfo(typelib
, stmt
->u
.type
);
2321 type_t
*type
= stmt
->u
.type
;
2322 add_type_typeinfo(typelib
, type
);
2328 static void set_name(msft_typelib_t
*typelib
)
2332 offset
= ctl2_alloc_name(typelib
, typelib
->typelib
->name
);
2333 if (offset
== -1) return;
2334 typelib
->typelib_header
.NameOffset
= offset
;
2338 static void set_version(msft_typelib_t
*typelib
)
2340 typelib
->typelib_header
.version
= get_attrv( typelib
->typelib
->attrs
, ATTR_VERSION
);
2343 static void set_guid(msft_typelib_t
*typelib
)
2345 MSFT_GuidEntry guidentry
;
2348 GUID guid
= {0,0,0,{0,0,0,0,0,0}};
2350 guidentry
.guid
= guid
;
2351 guidentry
.hreftype
= -2;
2352 guidentry
.next_hash
= -1;
2354 ptr
= get_attrp( typelib
->typelib
->attrs
, ATTR_UUID
);
2355 if (ptr
) guidentry
.guid
= *(GUID
*)ptr
;
2357 offset
= ctl2_alloc_guid(typelib
, &guidentry
);
2358 typelib
->typelib_header
.posguid
= offset
;
2363 static void set_doc_string(msft_typelib_t
*typelib
)
2365 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRING
);
2369 int offset
= ctl2_alloc_string(typelib
, str
);
2370 if (offset
!= -1) typelib
->typelib_header
.helpstring
= offset
;
2374 static void set_help_file_name(msft_typelib_t
*typelib
)
2376 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPFILE
);
2380 int offset
= ctl2_alloc_string(typelib
, str
);
2383 typelib
->typelib_header
.helpfile
= offset
;
2384 typelib
->typelib_header
.varflags
|= 0x10;
2389 static void set_help_context(msft_typelib_t
*typelib
)
2391 const expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPCONTEXT
);
2392 if (expr
) typelib
->typelib_header
.helpcontext
= expr
->cval
;
2395 static void set_help_string_dll(msft_typelib_t
*typelib
)
2397 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRINGDLL
);
2401 int offset
= ctl2_alloc_string(typelib
, str
);
2404 typelib
->help_string_dll_offset
= offset
;
2405 typelib
->typelib_header
.varflags
|= 0x100;
2410 static void set_help_string_context(msft_typelib_t
*typelib
)
2412 const expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRINGCONTEXT
);
2413 if (expr
) typelib
->typelib_header
.helpstringcontext
= expr
->cval
;
2416 static void set_lcid(msft_typelib_t
*typelib
)
2418 const expr_t
*lcid_expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_LIBLCID
);
2421 typelib
->typelib_header
.lcid
= lcid_expr
->cval
;
2422 typelib
->typelib_header
.lcid2
= lcid_expr
->cval
;
2426 static void set_lib_flags(msft_typelib_t
*typelib
)
2430 typelib
->typelib_header
.flags
= 0;
2431 if (!typelib
->typelib
->attrs
) return;
2432 LIST_FOR_EACH_ENTRY( attr
, typelib
->typelib
->attrs
, const attr_t
, entry
)
2434 switch(attr
->type
) {
2436 typelib
->typelib_header
.flags
|= 0x02; /* LIBFLAG_FCONTROL */
2439 typelib
->typelib_header
.flags
|= 0x04; /* LIBFLAG_FHIDDEN */
2441 case ATTR_RESTRICTED
:
2442 typelib
->typelib_header
.flags
|= 0x01; /* LIBFLAG_FRESTRICTED */
2451 static void ctl2_write_segment(msft_typelib_t
*typelib
, int segment
)
2453 put_data(typelib
->typelib_segment_data
[segment
], typelib
->typelib_segdir
[segment
].length
);
2456 static void ctl2_finalize_typeinfos(msft_typelib_t
*typelib
, int filesize
)
2458 msft_typeinfo_t
*typeinfo
;
2460 for (typeinfo
= typelib
->typeinfos
; typeinfo
; typeinfo
= typeinfo
->next_typeinfo
) {
2461 typeinfo
->typeinfo
->memoffset
= filesize
;
2462 if (typeinfo
->func_data
)
2463 filesize
+= typeinfo
->func_data
[0] + ((typeinfo
->typeinfo
->cElement
& 0xffff) * 12);
2464 if (typeinfo
->var_data
)
2465 filesize
+= typeinfo
->var_data
[0] + (((typeinfo
->typeinfo
->cElement
>> 16) & 0xffff) * 12);
2466 if (typeinfo
->func_data
|| typeinfo
->var_data
)
2471 static int ctl2_finalize_segment(msft_typelib_t
*typelib
, int filepos
, int segment
)
2473 if (typelib
->typelib_segdir
[segment
].length
) {
2474 typelib
->typelib_segdir
[segment
].offset
= filepos
;
2476 typelib
->typelib_segdir
[segment
].offset
= -1;
2479 return typelib
->typelib_segdir
[segment
].length
;
2483 static void ctl2_write_typeinfos(msft_typelib_t
*typelib
)
2485 msft_typeinfo_t
*typeinfo
;
2488 for (typeinfo
= typelib
->typeinfos
; typeinfo
; typeinfo
= typeinfo
->next_typeinfo
) {
2489 if (!typeinfo
->func_data
&& !typeinfo
->var_data
) continue;
2491 if (typeinfo
->func_data
)
2492 typedata_size
= typeinfo
->func_data
[0];
2493 if (typeinfo
->var_data
)
2494 typedata_size
+= typeinfo
->var_data
[0];
2495 put_data(&typedata_size
, sizeof(int));
2496 if (typeinfo
->func_data
)
2497 put_data(typeinfo
->func_data
+ 1, typeinfo
->func_data
[0]);
2498 if (typeinfo
->var_data
)
2499 put_data(typeinfo
->var_data
+ 1, typeinfo
->var_data
[0]);
2500 if (typeinfo
->func_indices
)
2501 put_data(typeinfo
->func_indices
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2502 if (typeinfo
->var_indices
)
2503 put_data(typeinfo
->var_indices
, (typeinfo
->typeinfo
->cElement
>> 16) * 4);
2504 if (typeinfo
->func_names
)
2505 put_data(typeinfo
->func_names
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2506 if (typeinfo
->var_names
)
2507 put_data(typeinfo
->var_names
, (typeinfo
->typeinfo
->cElement
>> 16) * 4);
2508 if (typeinfo
->func_offsets
)
2509 put_data(typeinfo
->func_offsets
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2510 if (typeinfo
->var_offsets
) {
2511 int add
= 0, i
, offset
;
2512 if(typeinfo
->func_data
)
2513 add
= typeinfo
->func_data
[0];
2514 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
>> 16); i
++) {
2515 offset
= typeinfo
->var_offsets
[i
];
2517 put_data(&offset
, 4);
2523 static void save_all_changes(msft_typelib_t
*typelib
)
2527 chat("save_all_changes(%p)\n", typelib
);
2529 filepos
= sizeof(MSFT_Header
) + sizeof(MSFT_SegDir
);
2530 if(typelib
->typelib_header
.varflags
& 0x100) filepos
+= 4; /* helpstringdll */
2531 filepos
+= typelib
->typelib_header
.nrtypeinfos
* 4;
2533 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_TYPEINFO
);
2534 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_GUIDHASH
);
2535 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_GUID
);
2536 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_REFERENCES
);
2537 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_IMPORTINFO
);
2538 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_IMPORTFILES
);
2539 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_NAMEHASH
);
2540 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_NAME
);
2541 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_STRING
);
2542 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_TYPEDESC
);
2543 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_ARRAYDESC
);
2544 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_CUSTDATA
);
2545 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_CUSTDATAGUID
);
2547 ctl2_finalize_typeinfos(typelib
, filepos
);
2550 init_output_buffer();
2552 put_data(&typelib
->typelib_header
, sizeof(typelib
->typelib_header
));
2553 if(typelib
->typelib_header
.varflags
& 0x100)
2554 put_data(&typelib
->help_string_dll_offset
, sizeof(typelib
->help_string_dll_offset
));
2556 put_data(typelib
->typelib_typeinfo_offsets
, typelib
->typelib_header
.nrtypeinfos
* 4);
2557 put_data(&typelib
->typelib_segdir
, sizeof(typelib
->typelib_segdir
));
2558 ctl2_write_segment( typelib
, MSFT_SEG_TYPEINFO
);
2559 ctl2_write_segment( typelib
, MSFT_SEG_GUIDHASH
);
2560 ctl2_write_segment( typelib
, MSFT_SEG_GUID
);
2561 ctl2_write_segment( typelib
, MSFT_SEG_REFERENCES
);
2562 ctl2_write_segment( typelib
, MSFT_SEG_IMPORTINFO
);
2563 ctl2_write_segment( typelib
, MSFT_SEG_IMPORTFILES
);
2564 ctl2_write_segment( typelib
, MSFT_SEG_NAMEHASH
);
2565 ctl2_write_segment( typelib
, MSFT_SEG_NAME
);
2566 ctl2_write_segment( typelib
, MSFT_SEG_STRING
);
2567 ctl2_write_segment( typelib
, MSFT_SEG_TYPEDESC
);
2568 ctl2_write_segment( typelib
, MSFT_SEG_ARRAYDESC
);
2569 ctl2_write_segment( typelib
, MSFT_SEG_CUSTDATA
);
2570 ctl2_write_segment( typelib
, MSFT_SEG_CUSTDATAGUID
);
2572 ctl2_write_typeinfos(typelib
);
2574 if (strendswith( typelib_name
, ".res" )) /* create a binary resource file */
2576 char typelib_id
[13] = "#1";
2578 expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_ID
);
2580 sprintf( typelib_id
, "#%d", expr
->cval
);
2581 add_output_to_resources( "TYPELIB", typelib_id
);
2582 output_typelib_regscript( typelib
->typelib
);
2583 flush_output_resources( typelib_name
);
2585 else flush_output_buffer( typelib_name
);
2588 int create_msft_typelib(typelib_t
*typelib
)
2590 msft_typelib_t
*msft
;
2592 const statement_t
*stmt
;
2594 char *time_override
;
2595 unsigned int version
= 7 << 24 | 555; /* 7.00.0555 */
2596 GUID midl_time_guid
= {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2597 GUID midl_version_guid
= {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2598 GUID midl_info_guid
= {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2599 char info_string
[128];
2601 pointer_size
= (typelib_kind
== SYS_WIN64
) ? 8 : 4;
2603 msft
= xmalloc(sizeof(*msft
));
2604 memset(msft
, 0, sizeof(*msft
));
2605 msft
->typelib
= typelib
;
2607 ctl2_init_header(msft
);
2608 ctl2_init_segdir(msft
);
2610 msft
->typelib_header
.varflags
|= typelib_kind
;
2613 * The following two calls return an offset or -1 if out of memory. We
2614 * specifically need an offset of 0, however, so...
2616 if (ctl2_alloc_segment(msft
, MSFT_SEG_GUIDHASH
, 0x80, 0x80)) { failed
= 1; }
2617 if (ctl2_alloc_segment(msft
, MSFT_SEG_NAMEHASH
, 0x200, 0x200)) { failed
= 1; }
2625 msft
->typelib_guidhash_segment
= (int *)msft
->typelib_segment_data
[MSFT_SEG_GUIDHASH
];
2626 msft
->typelib_namehash_segment
= (int *)msft
->typelib_segment_data
[MSFT_SEG_NAMEHASH
];
2628 memset(msft
->typelib_guidhash_segment
, 0xff, 0x80);
2629 memset(msft
->typelib_namehash_segment
, 0xff, 0x200);
2631 set_lib_flags(msft
);
2633 set_help_file_name(msft
);
2634 set_doc_string(msft
);
2638 set_help_context(msft
);
2639 set_help_string_dll(msft
);
2640 set_help_string_context(msft
);
2642 /* midl adds two sets of custom data to the library: the current unix time
2643 and midl's version number */
2644 time_override
= getenv( "WIDL_TIME_OVERRIDE");
2645 cur_time
= time_override
? atol( time_override
) : time(NULL
);
2646 sprintf(info_string
, "Created by WIDL version %s at %s\n", PACKAGE_VERSION
, ctime(&cur_time
));
2647 set_custdata(msft
, &midl_info_guid
, VT_BSTR
, info_string
, &msft
->typelib_header
.CustomDataOffset
);
2648 set_custdata(msft
, &midl_time_guid
, VT_UI4
, &cur_time
, &msft
->typelib_header
.CustomDataOffset
);
2649 set_custdata(msft
, &midl_version_guid
, VT_UI4
, &version
, &msft
->typelib_header
.CustomDataOffset
);
2652 LIST_FOR_EACH_ENTRY( stmt
, typelib
->stmts
, const statement_t
, entry
)
2653 add_entry(msft
, stmt
);
2655 save_all_changes(msft
);