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
49 #include "typelib_struct.h"
57 enum MSFT_segment_index
{
58 MSFT_SEG_TYPEINFO
= 0, /* type information */
59 MSFT_SEG_IMPORTINFO
, /* import information */
60 MSFT_SEG_IMPORTFILES
, /* import filenames */
61 MSFT_SEG_REFERENCES
, /* references (?) */
62 MSFT_SEG_GUIDHASH
, /* hash table for guids? */
63 MSFT_SEG_GUID
, /* guid storage */
64 MSFT_SEG_NAMEHASH
, /* hash table for names */
65 MSFT_SEG_NAME
, /* name storage */
66 MSFT_SEG_STRING
, /* string storage */
67 MSFT_SEG_TYPEDESC
, /* type descriptions */
68 MSFT_SEG_ARRAYDESC
, /* array descriptions */
69 MSFT_SEG_CUSTDATA
, /* custom data */
70 MSFT_SEG_CUSTDATAGUID
, /* custom data guids */
71 MSFT_SEG_UNKNOWN
, /* ??? */
72 MSFT_SEG_UNKNOWN2
, /* ??? */
73 MSFT_SEG_MAX
/* total number of segments */
76 typedef struct tagMSFT_ImpFile
{
80 char filename
[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
83 typedef struct _msft_typelib_t
86 MSFT_Header typelib_header
;
87 MSFT_pSeg typelib_segdir
[MSFT_SEG_MAX
];
88 unsigned char *typelib_segment_data
[MSFT_SEG_MAX
];
89 int typelib_segment_block_length
[MSFT_SEG_MAX
];
91 INT typelib_typeinfo_offsets
[0x200]; /* Hope that's enough. */
93 INT
*typelib_namehash_segment
;
94 INT
*typelib_guidhash_segment
;
96 INT help_string_dll_offset
;
98 struct _msft_typeinfo_t
*typeinfos
;
99 struct _msft_typeinfo_t
*last_typeinfo
;
102 typedef struct _msft_typeinfo_t
104 msft_typelib_t
*typelib
;
105 MSFT_TypeInfoBase
*typeinfo
;
109 unsigned int var_data_allocated
;
112 unsigned int func_data_allocated
;
127 struct _msft_typeinfo_t
*next_typeinfo
;
132 /*================== Internal functions ===================================*/
134 /****************************************************************************
137 * Initializes the type library header of a new typelib.
139 static void ctl2_init_header(
140 msft_typelib_t
*typelib
) /* [I] The typelib to initialize. */
142 typelib
->typelib_header
.magic1
= 0x5446534d;
143 typelib
->typelib_header
.magic2
= 0x00010002;
144 typelib
->typelib_header
.posguid
= -1;
145 typelib
->typelib_header
.lcid
= 0x0409;
146 typelib
->typelib_header
.lcid2
= 0x0;
147 typelib
->typelib_header
.varflags
= 0x40;
148 typelib
->typelib_header
.version
= 0;
149 typelib
->typelib_header
.flags
= 0;
150 typelib
->typelib_header
.nrtypeinfos
= 0;
151 typelib
->typelib_header
.helpstring
= -1;
152 typelib
->typelib_header
.helpstringcontext
= 0;
153 typelib
->typelib_header
.helpcontext
= 0;
154 typelib
->typelib_header
.nametablecount
= 0;
155 typelib
->typelib_header
.nametablechars
= 0;
156 typelib
->typelib_header
.NameOffset
= -1;
157 typelib
->typelib_header
.helpfile
= -1;
158 typelib
->typelib_header
.CustomDataOffset
= -1;
159 typelib
->typelib_header
.res44
= 0x20;
160 typelib
->typelib_header
.res48
= 0x80;
161 typelib
->typelib_header
.dispatchpos
= -1;
162 typelib
->typelib_header
.nimpinfos
= 0;
165 /****************************************************************************
168 * Initializes the segment directory of a new typelib.
170 static void ctl2_init_segdir(
171 msft_typelib_t
*typelib
) /* [I] The typelib to initialize. */
176 segdir
= &typelib
->typelib_segdir
[MSFT_SEG_TYPEINFO
];
178 for (i
= 0; i
< MSFT_SEG_MAX
; i
++) {
179 segdir
[i
].offset
= -1;
180 segdir
[i
].length
= 0;
181 segdir
[i
].res08
= -1;
182 segdir
[i
].res0c
= 0x0f;
186 /****************************************************************************
189 * Generates a hash key from a GUID.
193 * The hash key for the GUID.
195 static int ctl2_hash_guid(
196 REFGUID guid
) /* [I] The guid to hash. */
202 for (i
= 0; i
< 8; i
++) {
203 hash
^= ((const short *)guid
)[i
];
209 /****************************************************************************
212 * Locates a guid in a type library.
216 * The offset into the GUID segment of the guid, or -1 if not found.
218 static int ctl2_find_guid(
219 msft_typelib_t
*typelib
, /* [I] The typelib to operate against. */
220 int hash_key
, /* [I] The hash key for the guid. */
221 REFGUID guid
) /* [I] The guid to find. */
224 MSFT_GuidEntry
*guidentry
;
226 offset
= typelib
->typelib_guidhash_segment
[hash_key
];
227 while (offset
!= -1) {
228 guidentry
= (MSFT_GuidEntry
*)&typelib
->typelib_segment_data
[MSFT_SEG_GUID
][offset
];
230 if (!memcmp(guidentry
, guid
, sizeof(GUID
))) return offset
;
232 offset
= guidentry
->next_hash
;
238 /****************************************************************************
241 * Locates a name in a type library.
245 * The offset into the NAME segment of the name, or -1 if not found.
249 * The name must be encoded as with ctl2_encode_name().
251 static int ctl2_find_name(
252 msft_typelib_t
*typelib
, /* [I] The typelib to operate against. */
253 char *name
) /* [I] The encoded name to find. */
258 offset
= typelib
->typelib_namehash_segment
[name
[2] & 0x7f];
259 while (offset
!= -1) {
260 namestruct
= (int *)&typelib
->typelib_segment_data
[MSFT_SEG_NAME
][offset
];
262 if (!((namestruct
[2] ^ *((int *)name
)) & 0xffff00ff)) {
263 /* hash codes and lengths match, final test */
264 if (!strncasecmp(name
+4, (void *)(namestruct
+3), name
[0])) break;
267 /* move to next item in hash bucket */
268 offset
= namestruct
[1];
274 /****************************************************************************
277 * Encodes a name string to a form suitable for storing into a type library
278 * or comparing to a name stored in a type library.
282 * The length of the encoded name, including padding and length+hash fields.
286 * Will throw an exception if name or result are NULL. Is not multithread
287 * safe in the slightest.
289 static int ctl2_encode_name(
290 msft_typelib_t
*typelib
, /* [I] The typelib to operate against (used for LCID only). */
291 const char *name
, /* [I] The name string to encode. */
292 char **result
) /* [O] A pointer to a pointer to receive the encoded name. */
294 char *converted_name
;
299 length
= strlen(name
);
300 size
= (length
+ 7) & ~3;
301 converted_name
= xmalloc(size
+ 1);
302 memcpy(converted_name
+ 4, name
, length
);
303 converted_name
[length
+ 4] = 0;
305 value
= lhash_val_of_name_sys(typelib
->typelib_header
.varflags
& 0x0f, typelib
->typelib_header
.lcid
, converted_name
+ 4);
307 #ifdef WORDS_BIGENDIAN
308 converted_name
[3] = length
& 0xff;
309 converted_name
[2] = length
>> 8;
310 converted_name
[1] = value
;
311 converted_name
[0] = value
>> 8;
313 converted_name
[0] = length
& 0xff;
314 converted_name
[1] = length
>> 8;
315 converted_name
[2] = value
;
316 converted_name
[3] = value
>> 8;
319 for (offset
= (4 - length
) & 3; offset
; offset
--) converted_name
[length
+ offset
+ 3] = 0x57;
321 *result
= converted_name
;
326 /****************************************************************************
329 * Encodes a string to a form suitable for storing into a type library or
330 * comparing to a string stored in a type library.
334 * The length of the encoded string, including padding and length fields.
338 * Will throw an exception if string or result are NULL. Is not multithread
339 * safe in the slightest.
341 static int ctl2_encode_string(
342 const char *string
, /* [I] The string to encode. */
343 char **result
) /* [O] A pointer to a pointer to receive the encoded string. */
345 char *converted_string
;
349 length
= strlen(string
);
350 size
= (length
+ 5) & ~3;
351 if (length
< 3) size
+= 4;
352 converted_string
= xmalloc(size
);
353 memcpy(converted_string
+ 2, string
, length
);
355 #ifdef WORDS_BIGENDIAN
356 converted_string
[1] = length
& 0xff;
357 converted_string
[0] = (length
>> 8) & 0xff;
359 converted_string
[0] = length
& 0xff;
360 converted_string
[1] = (length
>> 8) & 0xff;
363 if(length
< 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
364 for(offset
= 0; offset
< 4; offset
++)
365 converted_string
[length
+ offset
+ 2] = 0x57;
368 for (offset
= (4 - (length
+ 2)) & 3; offset
; offset
--) converted_string
[length
+ offset
+ 1] = 0x57;
370 *result
= converted_string
;
374 /****************************************************************************
377 * Allocates memory from a segment in a type library.
381 * Success: The offset within the segment of the new data area.
385 * Does not (yet) handle the case where the allocated segment memory needs to grow.
387 static int ctl2_alloc_segment(
388 msft_typelib_t
*typelib
, /* [I] The type library in which to allocate. */
389 enum MSFT_segment_index segment
, /* [I] The segment in which to allocate. */
390 int size
, /* [I] The amount to allocate. */
391 int block_size
) /* [I] Initial allocation block size, or 0 for default. */
395 if(!typelib
->typelib_segment_data
[segment
]) {
396 if (!block_size
) block_size
= 0x2000;
398 typelib
->typelib_segment_block_length
[segment
] = block_size
;
399 typelib
->typelib_segment_data
[segment
] = xmalloc(block_size
);
400 if (!typelib
->typelib_segment_data
[segment
]) return -1;
401 memset(typelib
->typelib_segment_data
[segment
], 0x57, block_size
);
404 while ((typelib
->typelib_segdir
[segment
].length
+ size
) > typelib
->typelib_segment_block_length
[segment
]) {
405 unsigned char *block
;
407 block_size
= typelib
->typelib_segment_block_length
[segment
];
408 block
= xrealloc(typelib
->typelib_segment_data
[segment
], block_size
<< 1);
410 if (segment
== MSFT_SEG_TYPEINFO
) {
411 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
412 msft_typeinfo_t
*typeinfo
;
414 for (typeinfo
= typelib
->typeinfos
; typeinfo
; typeinfo
= typeinfo
->next_typeinfo
) {
415 typeinfo
->typeinfo
= (void *)&block
[((unsigned char *)typeinfo
->typeinfo
) - typelib
->typelib_segment_data
[segment
]];
419 memset(block
+ block_size
, 0x57, block_size
);
420 typelib
->typelib_segment_block_length
[segment
] = block_size
<< 1;
421 typelib
->typelib_segment_data
[segment
] = block
;
424 offset
= typelib
->typelib_segdir
[segment
].length
;
425 typelib
->typelib_segdir
[segment
].length
+= size
;
430 /****************************************************************************
431 * ctl2_alloc_typeinfo
433 * Allocates and initializes a typeinfo structure in a type library.
437 * Success: The offset of the new typeinfo.
438 * Failure: -1 (this is invariably an out of memory condition).
440 static int ctl2_alloc_typeinfo(
441 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
442 int nameoffset
) /* [I] The offset of the name for this typeinfo. */
445 MSFT_TypeInfoBase
*typeinfo
;
447 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEINFO
, sizeof(MSFT_TypeInfoBase
), 0);
449 typelib
->typelib_typeinfo_offsets
[typelib
->typelib_header
.nrtypeinfos
++] = offset
;
451 typeinfo
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_TYPEINFO
] + offset
);
453 typeinfo
->typekind
= (typelib
->typelib_header
.nrtypeinfos
- 1) << 16;
454 typeinfo
->memoffset
= -1; /* should be EOF if no elements */
459 typeinfo
->cElement
= 0;
464 typeinfo
->posguid
= -1;
466 typeinfo
->NameOffset
= nameoffset
;
467 typeinfo
->version
= 0;
468 typeinfo
->docstringoffs
= -1;
469 typeinfo
->helpstringcontext
= 0;
470 typeinfo
->helpcontext
= 0;
471 typeinfo
->oCustData
= -1;
472 typeinfo
->cbSizeVft
= 0;
473 typeinfo
->cImplTypes
= 0;
475 typeinfo
->datatype1
= -1;
476 typeinfo
->datatype2
= 0;
478 typeinfo
->res19
= -1;
483 /****************************************************************************
486 * Allocates and initializes a GUID structure in a type library. Also updates
487 * the GUID hash table as needed.
491 * Success: The offset of the new GUID.
493 static int ctl2_alloc_guid(
494 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
495 MSFT_GuidEntry
*guid
) /* [I] The GUID to store. */
498 MSFT_GuidEntry
*guid_space
;
501 chat("adding uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
502 guid
->guid
.Data1
, guid
->guid
.Data2
, guid
->guid
.Data3
,
503 guid
->guid
.Data4
[0], guid
->guid
.Data4
[1], guid
->guid
.Data4
[2], guid
->guid
.Data4
[3],
504 guid
->guid
.Data4
[4], guid
->guid
.Data4
[5], guid
->guid
.Data4
[6], guid
->guid
.Data4
[7]);
506 hash_key
= ctl2_hash_guid(&guid
->guid
);
508 offset
= ctl2_find_guid(typelib
, hash_key
, &guid
->guid
);
511 if (is_warning_enabled(2368))
512 warning("duplicate uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
513 guid
->guid
.Data1
, guid
->guid
.Data2
, guid
->guid
.Data3
,
514 guid
->guid
.Data4
[0], guid
->guid
.Data4
[1], guid
->guid
.Data4
[2], guid
->guid
.Data4
[3],
515 guid
->guid
.Data4
[4], guid
->guid
.Data4
[5], guid
->guid
.Data4
[6], guid
->guid
.Data4
[7]);
519 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_GUID
, sizeof(MSFT_GuidEntry
), 0);
521 guid_space
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_GUID
] + offset
);
524 guid_space
->next_hash
= typelib
->typelib_guidhash_segment
[hash_key
];
525 typelib
->typelib_guidhash_segment
[hash_key
] = offset
;
530 /****************************************************************************
533 * Allocates and initializes a name within a type library. Also updates the
534 * name hash table as needed.
538 * Success: The offset within the segment of the new name.
539 * Failure: -1 (this is invariably an out of memory condition).
541 static int ctl2_alloc_name(
542 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
543 const char *name
) /* [I] The name to store. */
547 MSFT_NameIntro
*name_space
;
550 length
= ctl2_encode_name(typelib
, name
, &encoded_name
);
552 offset
= ctl2_find_name(typelib
, encoded_name
);
559 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_NAME
, length
+ 8, 0);
561 name_space
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_NAME
] + offset
);
562 name_space
->hreftype
= -1;
563 name_space
->next_hash
= -1;
564 memcpy(&name_space
->namelen
, encoded_name
, length
);
566 if (typelib
->typelib_namehash_segment
[encoded_name
[2] & 0x7f] != -1)
567 name_space
->next_hash
= typelib
->typelib_namehash_segment
[encoded_name
[2] & 0x7f];
569 typelib
->typelib_namehash_segment
[encoded_name
[2] & 0x7f] = offset
;
571 typelib
->typelib_header
.nametablecount
+= 1;
572 typelib
->typelib_header
.nametablechars
+= *encoded_name
;
578 /****************************************************************************
581 * Allocates and initializes a string in a type library.
585 * Success: The offset within the segment of the new string.
586 * Failure: -1 (this is invariably an out of memory condition).
588 static int ctl2_alloc_string(
589 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
590 const char *string
) /* [I] The string to store. */
594 unsigned char *string_space
;
595 char *encoded_string
;
597 length
= ctl2_encode_string(string
, &encoded_string
);
599 for (offset
= 0; offset
< typelib
->typelib_segdir
[MSFT_SEG_STRING
].length
;
600 offset
+= (((typelib
->typelib_segment_data
[MSFT_SEG_STRING
][offset
+ 1] << 8) |
601 typelib
->typelib_segment_data
[MSFT_SEG_STRING
][offset
+ 0]) + 5) & ~3) {
602 if (!memcmp(encoded_string
, typelib
->typelib_segment_data
[MSFT_SEG_STRING
] + offset
, length
)) return offset
;
605 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_STRING
, length
, 0);
607 string_space
= typelib
->typelib_segment_data
[MSFT_SEG_STRING
] + offset
;
608 memcpy(string_space
, encoded_string
, length
);
609 free(encoded_string
);
614 /****************************************************************************
615 * alloc_msft_importinfo
617 * Allocates and initializes an import information structure in a type library.
621 * Success: The offset of the new importinfo.
622 * Failure: -1 (this is invariably an out of memory condition).
624 static int alloc_msft_importinfo(
625 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
626 MSFT_ImpInfo
*impinfo
) /* [I] The import information to store. */
629 MSFT_ImpInfo
*impinfo_space
;
632 offset
< typelib
->typelib_segdir
[MSFT_SEG_IMPORTINFO
].length
;
633 offset
+= sizeof(MSFT_ImpInfo
)) {
634 if (!memcmp(&(typelib
->typelib_segment_data
[MSFT_SEG_IMPORTINFO
][offset
]),
635 impinfo
, sizeof(MSFT_ImpInfo
))) {
640 impinfo
->flags
|= typelib
->typelib_header
.nimpinfos
++;
642 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_IMPORTINFO
, sizeof(MSFT_ImpInfo
), 0);
644 impinfo_space
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_IMPORTINFO
] + offset
);
645 *impinfo_space
= *impinfo
;
650 /****************************************************************************
653 * Allocates and initializes an import file definition in a type library.
657 * Success: The offset of the new importinfo.
658 * Failure: -1 (this is invariably an out of memory condition).
660 static int alloc_importfile(
661 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
662 int guidoffset
, /* [I] The offset to the GUID for the imported library. */
663 int major_version
, /* [I] The major version number of the imported library. */
664 int minor_version
, /* [I] The minor version number of the imported library. */
665 const char *filename
) /* [I] The filename of the imported library. */
669 MSFT_ImpFile
*importfile
;
670 char *encoded_string
;
672 length
= ctl2_encode_string(filename
, &encoded_string
);
674 encoded_string
[0] <<= 2;
675 encoded_string
[0] |= 1;
677 for (offset
= 0; offset
< typelib
->typelib_segdir
[MSFT_SEG_IMPORTFILES
].length
;
678 offset
+= (((typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
][offset
+ 0xd] << 8) |
679 typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
][offset
+ 0xc]) >> 2) + 0xc) {
680 if (!memcmp(encoded_string
, typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
] + offset
+ 0xc, length
)) return offset
;
683 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_IMPORTFILES
, length
+ 0xc, 0);
685 importfile
= (MSFT_ImpFile
*)&typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
][offset
];
686 importfile
->guid
= guidoffset
;
687 importfile
->lcid
= typelib
->typelib_header
.lcid2
;
688 importfile
->version
= major_version
| (minor_version
<< 16);
689 memcpy(&importfile
->filename
, encoded_string
, length
);
690 free(encoded_string
);
695 static void alloc_importinfo(msft_typelib_t
*typelib
, importinfo_t
*importinfo
)
697 importlib_t
*importlib
= importinfo
->importlib
;
699 chat("alloc_importinfo: %s\n", importinfo
->name
);
701 if(!importlib
->allocated
) {
705 chat("allocating importlib %s\n", importlib
->name
);
707 importlib
->allocated
= -1;
709 memcpy(&guid
.guid
, &importlib
->guid
, sizeof(GUID
));
712 guid_idx
= ctl2_alloc_guid(typelib
, &guid
);
714 importlib
->offset
= alloc_importfile(typelib
, guid_idx
, importlib
->version
& 0xffff,
715 importlib
->version
>> 16, importlib
->name
);
718 if(importinfo
->offset
== -1 || !(importinfo
->flags
& MSFT_IMPINFO_OFFSET_IS_GUID
)) {
719 MSFT_ImpInfo impinfo
;
721 impinfo
.flags
= importinfo
->flags
;
722 impinfo
.oImpFile
= importlib
->offset
;
724 if(importinfo
->flags
& MSFT_IMPINFO_OFFSET_IS_GUID
) {
728 memcpy(&guid
.guid
, &importinfo
->guid
, sizeof(GUID
));
730 impinfo
.oGuid
= ctl2_alloc_guid(typelib
, &guid
);
732 importinfo
->offset
= alloc_msft_importinfo(typelib
, &impinfo
);
734 typelib
->typelib_segment_data
[MSFT_SEG_GUID
][impinfo
.oGuid
+sizeof(GUID
)]
735 = importinfo
->offset
+1;
737 if(!strcmp(importinfo
->name
, "IDispatch"))
738 typelib
->typelib_header
.dispatchpos
= importinfo
->offset
+1;
740 impinfo
.oGuid
= importinfo
->id
;
741 importinfo
->offset
= alloc_msft_importinfo(typelib
, &impinfo
);
746 static importinfo_t
*find_importinfo(msft_typelib_t
*typelib
, const char *name
)
748 importlib_t
*importlib
;
751 chat("search importlib %s\n", name
);
756 LIST_FOR_EACH_ENTRY( importlib
, &typelib
->typelib
->importlibs
, importlib_t
, entry
)
758 for(i
=0; i
< importlib
->ntypeinfos
; i
++) {
759 if(!strcmp(name
, importlib
->importinfos
[i
].name
)) {
760 chat("Found %s in importlib.\n", name
);
761 return importlib
->importinfos
+i
;
769 static void add_structure_typeinfo(msft_typelib_t
*typelib
, type_t
*structure
);
770 static void add_interface_typeinfo(msft_typelib_t
*typelib
, type_t
*interface
);
771 static void add_enum_typeinfo(msft_typelib_t
*typelib
, type_t
*enumeration
);
772 static void add_union_typeinfo(msft_typelib_t
*typelib
, type_t
*tunion
);
773 static void add_coclass_typeinfo(msft_typelib_t
*typelib
, type_t
*cls
);
774 static void add_dispinterface_typeinfo(msft_typelib_t
*typelib
, type_t
*dispinterface
);
775 static void add_typedef_typeinfo(msft_typelib_t
*typelib
, type_t
*dispinterface
);
778 /****************************************************************************
781 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
782 * segments as needed.
789 static int encode_type(
790 msft_typelib_t
*typelib
, /* [I] The type library in which to encode the TYPEDESC. */
791 int vt
, /* [I] vt to encode */
792 type_t
*type
, /* [I] type */
793 int *encoded_type
, /* [O] The encoded type description. */
794 int *decoded_size
) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
803 chat("encode_type vt %d type %p\n", vt
, type
);
805 default_type
= 0x80000000 | (vt
<< 16) | vt
;
806 if (!decoded_size
) decoded_size
= &scratch
;
813 *encoded_type
= default_type
;
817 *encoded_type
= 0x80000000 | (VT_I4
<< 16) | VT_INT
;
821 *encoded_type
= 0x80000000 | (VT_UI4
<< 16) | VT_UINT
;
827 *encoded_type
= default_type
;
835 *encoded_type
= default_type
;
841 *encoded_type
= default_type
;
846 *encoded_type
= default_type
;
850 *encoded_type
= default_type
;
854 *encoded_type
= 0x80000000 | (VT_EMPTY
<< 16) | vt
;
860 *encoded_type
= default_type
;
864 *encoded_type
= default_type
;
869 *encoded_type
= 0xfffe0000 | vt
;
875 for(next_vt
= 0; is_ptr(type
); type
= type_pointer_get_ref_type(type
)) {
876 next_vt
= get_type_vt(type_pointer_get_ref_type(type
));
880 /* if no type found then it must be void */
884 encode_type(typelib
, next_vt
, type_pointer_get_ref_type(type
),
885 &target_type
, &child_size
);
886 /* these types already have an implicit pointer, so we don't need to
888 if(next_vt
== VT_DISPATCH
|| next_vt
== VT_UNKNOWN
) {
889 chat("encode_type: skipping ptr\n");
890 *encoded_type
= target_type
;
891 *decoded_size
= child_size
;
895 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
896 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
897 if (((typedata
[0] & 0xffff) == VT_PTR
) && (typedata
[1] == target_type
)) break;
900 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
903 if (target_type
& 0x80000000) {
904 mix_field
= ((target_type
>> 16) & 0x3fff) | VT_BYREF
;
906 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][target_type
];
907 mix_field
= ((typedata
[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
910 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
911 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
913 typedata
[0] = (mix_field
<< 16) | VT_PTR
;
914 typedata
[1] = target_type
;
917 *encoded_type
= typeoffset
;
919 *decoded_size
= 8 /*sizeof(TYPEDESC)*/ + child_size
;
925 type_t
*element_type
= type_alias_get_aliasee_type(type_array_get_element_type(type
));
926 int next_vt
= get_type_vt(element_type
);
928 encode_type(typelib
, next_vt
, type_alias_get_aliasee_type(type_array_get_element_type(type
)),
929 &target_type
, &child_size
);
931 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
932 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
933 if (((typedata
[0] & 0xffff) == VT_SAFEARRAY
) && (typedata
[1] == target_type
)) break;
936 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
939 if (target_type
& 0x80000000) {
940 mix_field
= ((target_type
>> 16) & VT_TYPEMASK
) | VT_ARRAY
;
942 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][target_type
];
943 mix_field
= ((typedata
[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
946 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
947 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
949 typedata
[0] = (mix_field
<< 16) | VT_SAFEARRAY
;
950 typedata
[1] = target_type
;
953 *encoded_type
= typeoffset
;
955 *decoded_size
= 8 /*sizeof(TYPEDESC)*/ + child_size
;
961 importinfo_t
*importinfo
;
964 if (type
->typelib_idx
> -1)
966 chat("encode_type: VT_USERDEFINED - found already defined type %s at %d\n",
967 type
->name
, type
->typelib_idx
);
968 typeinfo_offset
= typelib
->typelib_typeinfo_offsets
[type
->typelib_idx
];
970 else if ((importinfo
= find_importinfo(typelib
, type
->name
)))
972 chat("encode_type: VT_USERDEFINED - found imported type %s in %s\n",
973 type
->name
, importinfo
->importlib
->name
);
974 alloc_importinfo(typelib
, importinfo
);
975 typeinfo_offset
= importinfo
->offset
| 0x1;
979 /* Typedefs without the [public] attribute aren't included in the
980 * typelib, unless the aliasee is an anonymous UDT or the typedef
981 * is wire-marshalled. In the latter case the wire-marshal type,
982 * which may be a non-public alias, is used instead. */
983 while (type_is_alias(type
))
985 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
))
987 type
= get_attrp(type
->attrs
, ATTR_WIREMARSHAL
);
990 else if (!is_attr(type
->attrs
, ATTR_PUBLIC
))
991 type
= type_alias_get_aliasee_type(type
);
996 chat("encode_type: VT_USERDEFINED - adding new type %s, real type %d\n",
997 type
->name
, type_get_type(type
));
999 switch (type_get_type_detect_alias(type
))
1002 case TYPE_ENCAPSULATED_UNION
:
1003 add_structure_typeinfo(typelib
, type
);
1005 case TYPE_INTERFACE
:
1006 add_interface_typeinfo(typelib
, type
);
1009 add_enum_typeinfo(typelib
, type
);
1012 add_union_typeinfo(typelib
, type
);
1015 add_coclass_typeinfo(typelib
, type
);
1018 add_typedef_typeinfo(typelib
, type
);
1021 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
1022 type_get_type(type
));
1025 typeinfo_offset
= typelib
->typelib_typeinfo_offsets
[type
->typelib_idx
];
1027 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
1028 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1029 if ((typedata
[0] == ((0x7fff << 16) | VT_USERDEFINED
)) && (typedata
[1] == typeinfo_offset
)) break;
1032 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
1033 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
1034 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1036 typedata
[0] = (0x7fff << 16) | VT_USERDEFINED
;
1037 typedata
[1] = typeinfo_offset
;
1040 *encoded_type
= typeoffset
;
1045 error("encode_type: unrecognized type %d.\n", vt
);
1046 *encoded_type
= default_type
;
1053 static void dump_type(type_t
*t
)
1055 chat("dump_type: %p name %s type %d attrs %p\n", t
, t
->name
, type_get_type(t
), t
->attrs
);
1058 static int encode_var(
1059 msft_typelib_t
*typelib
, /* [I] The type library in which to encode the TYPEDESC. */
1060 type_t
*type
, /* [I] The type description to encode. */
1061 var_t
*var
, /* [I] The var to encode. */
1062 int *encoded_type
, /* [O] The encoded type description. */
1063 int *decoded_size
) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1072 if (!decoded_size
) decoded_size
= &scratch
;
1075 chat("encode_var: var %p type %p type->name %s\n",
1076 var
, type
, type
->name
? type
->name
: "NULL");
1078 if (is_array(type
) && !type_array_is_decl_as_ptr(type
)) {
1079 int num_dims
, elements
= 1, arrayoffset
;
1085 is_array(atype
) && !type_array_is_decl_as_ptr(atype
);
1086 atype
= type_array_get_element_type(atype
))
1089 chat("array with %d dimensions\n", num_dims
);
1090 encode_var(typelib
, atype
, var
, &target_type
, NULL
);
1091 arrayoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_ARRAYDESC
, (2 + 2 * num_dims
) * sizeof(int), 0);
1092 arraydata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_ARRAYDESC
][arrayoffset
];
1094 arraydata
[0] = target_type
;
1095 arraydata
[1] = num_dims
;
1096 arraydata
[1] |= ((num_dims
* 2 * sizeof(int)) << 16);
1100 is_array(atype
) && !type_array_is_decl_as_ptr(atype
);
1101 atype
= type_array_get_element_type(atype
))
1103 arraydata
[0] = type_array_get_dim(atype
);
1106 elements
*= type_array_get_dim(atype
);
1109 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
1110 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1112 typedata
[0] = (0x7ffe << 16) | VT_CARRAY
;
1113 typedata
[1] = arrayoffset
;
1115 *encoded_type
= typeoffset
;
1116 *decoded_size
= 20 /*sizeof(ARRAYDESC)*/ + (num_dims
- 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1120 vt
= get_type_vt(type
);
1122 type_t
*ref
= is_ptr(type
) ?
1123 type_pointer_get_ref_type(type
) : type_array_get_element_type(type
);
1124 int skip_ptr
= encode_var(typelib
, ref
, var
, &target_type
, &child_size
);
1127 chat("encode_var: skipping ptr\n");
1128 *encoded_type
= target_type
;
1129 *decoded_size
= child_size
;
1133 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
1134 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1135 if (((typedata
[0] & 0xffff) == VT_PTR
) && (typedata
[1] == target_type
)) break;
1138 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
1141 if (target_type
& 0x80000000) {
1142 mix_field
= ((target_type
>> 16) & 0x3fff) | VT_BYREF
;
1143 } else if (get_type_vt(ref
) == VT_SAFEARRAY
) {
1144 type_t
*element_type
= type_alias_get_aliasee_type(type_array_get_element_type(ref
));
1145 mix_field
= get_type_vt(element_type
) | VT_ARRAY
| VT_BYREF
;
1147 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][target_type
];
1148 mix_field
= ((typedata
[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1151 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
1152 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1154 typedata
[0] = (mix_field
<< 16) | VT_PTR
;
1155 typedata
[1] = target_type
;
1158 *encoded_type
= typeoffset
;
1160 *decoded_size
= 8 /*sizeof(TYPEDESC)*/ + child_size
;
1166 encode_type(typelib
, vt
, type
, encoded_type
, decoded_size
);
1167 /* these types already have an implicit pointer, so we don't need to
1169 if(vt
== VT_DISPATCH
|| vt
== VT_UNKNOWN
) return 2;
1173 static unsigned int get_ulong_val(unsigned int val
, int vt
)
1179 return val
& 0xffff;
1188 static void write_int_value(msft_typelib_t
*typelib
, int *out
, int vt
, int value
)
1190 const unsigned int lv
= get_ulong_val(value
, vt
);
1191 if ((lv
& 0x3ffffff) == lv
) {
1196 int offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATA
, 8, 0);
1197 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
]) = vt
;
1198 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+2], &value
, 4);
1199 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+6]) = 0x5757;
1204 static void write_string_value(msft_typelib_t
*typelib
, int *out
, const char *value
)
1206 int len
= strlen(value
), seg_len
= (len
+ 6 + 3) & ~0x3;
1207 int offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATA
, seg_len
, 0);
1208 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
]) = VT_BSTR
;
1209 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+2], &len
, sizeof(len
));
1210 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+6], value
, len
);
1212 while(len
< seg_len
) {
1213 *((char *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+len
]) = 0x57;
1219 static void write_default_value(msft_typelib_t
*typelib
, type_t
*type
, expr_t
*expr
, int *out
)
1223 if (expr
->type
== EXPR_STRLIT
|| expr
->type
== EXPR_WSTRLIT
) {
1224 if (get_type_vt(type
) != VT_BSTR
)
1225 error("string default value applied to non-string type\n");
1226 chat("default value '%s'\n", expr
->u
.sval
);
1227 write_string_value(typelib
, out
, expr
->u
.sval
);
1231 if (type_get_type(type
) == TYPE_ENUM
) {
1233 } else if (is_ptr(type
)) {
1234 vt
= get_type_vt(type_pointer_get_ref_type(type
));
1235 if (vt
== VT_USERDEFINED
)
1238 warning("non-null pointer default value\n");
1240 vt
= get_type_vt(type
);
1255 warning("can't write value of type %d yet\n", vt
);
1260 write_int_value(typelib
, out
, vt
, expr
->cval
);
1263 static HRESULT
set_custdata(msft_typelib_t
*typelib
, REFGUID guid
,
1264 int vt
, const void *value
, int *offset
)
1272 hash_key
= ctl2_hash_guid(guid
);
1273 guidoffset
= ctl2_find_guid(typelib
, hash_key
, guid
);
1274 if(guidoffset
== -1) {
1275 /* add GUID that was not already present */
1276 MSFT_GuidEntry guidentry
;
1277 guidentry
.guid
= *guid
;
1279 guidentry
.hreftype
= -1;
1280 guidentry
.next_hash
= -1;
1282 guidoffset
= ctl2_alloc_guid(typelib
, &guidentry
);
1286 /* TODO midl appears to share a single reference if the same string is used as custdata in multiple places */
1287 write_string_value(typelib
, &data_out
, value
);
1289 write_int_value(typelib
, &data_out
, vt
, *(int*)value
);
1291 custoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATAGUID
, 12, 0);
1293 custdata
= (int *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATAGUID
][custoffset
];
1294 custdata
[0] = guidoffset
;
1295 custdata
[1] = data_out
;
1296 custdata
[2] = *offset
;
1297 *offset
= custoffset
;
1302 static HRESULT
set_custdata_attr(msft_typelib_t
*typelib
, attr_custdata_t
*custdata
, int *offset
)
1304 switch(custdata
->pval
->type
) {
1307 set_custdata(typelib
, &custdata
->id
, VT_BSTR
, custdata
->pval
->u
.sval
, offset
);
1311 set_custdata(typelib
, &custdata
->id
, VT_I4
, &custdata
->pval
->u
.lval
, offset
);
1314 error("custom() attribute with unknown type\n");
1321 static HRESULT
add_func_desc(msft_typeinfo_t
* typeinfo
, var_t
*func
, int index
)
1323 int offset
, name_offset
;
1324 int *typedata
, typedata_size
;
1325 int i
, id
, next_idx
;
1326 int decoded_size
, extra_attr
= 0;
1327 int num_params
= 0, num_optional
= 0, num_defaults
= 0;
1328 int has_arg_custdata
= 0;
1330 unsigned char *namedata
;
1332 unsigned int funcflags
= 0, callconv
= 4 /* CC_STDCALL */;
1333 unsigned int funckind
, invokekind
= 1 /* INVOKE_FUNC */;
1334 int help_context
= 0, help_string_context
= 0, help_string_offset
= -1;
1335 int func_custdata_offset
= -1;
1336 int entry
= -1, entry_is_ord
= 0;
1337 int lcid_retval_count
= 0;
1339 chat("add_func_desc(%p,%d)\n", typeinfo
, index
);
1341 id
= ((0x6000 | (typeinfo
->typeinfo
->datatype2
& 0xffff)) << 16) | index
;
1343 switch(typeinfo
->typekind
) {
1344 case TKIND_DISPATCH
:
1345 funckind
= 0x4; /* FUNC_DISPATCH */
1348 funckind
= 0x3; /* FUNC_STATIC */
1351 funckind
= 0x1; /* FUNC_PUREVIRTUAL */
1355 if (is_local( func
->attrs
)) {
1356 chat("add_func_desc: skipping local function\n");
1360 if (type_function_get_args(func
->declspec
.type
))
1361 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), var_t
, entry
)
1364 if (arg
->attrs
) LIST_FOR_EACH_ENTRY( attr
, arg
->attrs
, const attr_t
, entry
) {
1365 if(attr
->type
== ATTR_DEFAULTVALUE
)
1367 else if(attr
->type
== ATTR_OPTIONAL
)
1369 else if(attr
->type
== ATTR_CUSTOM
)
1370 has_arg_custdata
= 1;
1374 chat("add_func_desc: num of params %d\n", num_params
);
1376 name_offset
= ctl2_alloc_name(typeinfo
->typelib
, func
->name
);
1378 if (func
->attrs
) LIST_FOR_EACH_ENTRY( attr
, func
->attrs
, const attr_t
, entry
) {
1379 expr_t
*expr
= attr
->u
.pval
;
1380 switch(attr
->type
) {
1382 funcflags
|= 0x4; /* FUNCFLAG_FBINDABLE */
1385 set_custdata_attr(typeinfo
->typelib
, attr
->u
.pval
, &func_custdata_offset
);
1387 case ATTR_DEFAULTBIND
:
1388 funcflags
|= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1390 case ATTR_DEFAULTCOLLELEM
:
1391 funcflags
|= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1393 case ATTR_DISPLAYBIND
:
1394 funcflags
|= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1397 extra_attr
= max(extra_attr
, 3);
1398 if (expr
->type
== EXPR_STRLIT
|| expr
->type
== EXPR_WSTRLIT
)
1399 entry
= ctl2_alloc_string(typeinfo
->typelib
, attr
->u
.pval
);
1405 case ATTR_HELPCONTEXT
:
1406 extra_attr
= max(extra_attr
, 1);
1407 help_context
= expr
->u
.lval
;
1409 case ATTR_HELPSTRING
:
1410 extra_attr
= max(extra_attr
, 2);
1411 help_string_offset
= ctl2_alloc_string(typeinfo
->typelib
, attr
->u
.pval
);
1413 case ATTR_HELPSTRINGCONTEXT
:
1414 extra_attr
= max(extra_attr
, 6);
1415 help_string_context
= expr
->u
.lval
;
1418 funcflags
|= 0x40; /* FUNCFLAG_FHIDDEN */
1423 case ATTR_IMMEDIATEBIND
:
1424 funcflags
|= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1426 case ATTR_NONBROWSABLE
:
1427 funcflags
|= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1432 invokekind
= 0x2; /* INVOKE_PROPERTYGET */
1435 invokekind
= 0x4; /* INVOKE_PROPERTYPUT */
1437 case ATTR_PROPPUTREF
:
1438 invokekind
= 0x8; /* INVOKE_PROPERTYPUTREF */
1440 /* FIXME: FUNCFLAG_FREPLACEABLE */
1441 case ATTR_REQUESTEDIT
:
1442 funcflags
|= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1444 case ATTR_RESTRICTED
:
1445 funcflags
|= 0x1; /* FUNCFLAG_FRESTRICTED */
1448 funcflags
|= 0x2; /* FUNCFLAG_FSOURCE */
1450 case ATTR_UIDEFAULT
:
1451 funcflags
|= 0x200; /* FUNCFLAG_FUIDEFAULT */
1453 case ATTR_USESGETLASTERROR
:
1454 funcflags
|= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1457 if (num_optional
|| num_defaults
)
1458 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1467 if(has_arg_custdata
|| func_custdata_offset
!= -1) {
1468 extra_attr
= max(extra_attr
, 7 + num_params
);
1471 /* allocate type data space for us */
1472 typedata_size
= 0x18 + extra_attr
* sizeof(int) + (num_params
* (num_defaults
? 16 : 12));
1474 if (!typeinfo
->func_data
) {
1475 typeinfo
->func_data
= xmalloc(0x100);
1476 typeinfo
->func_data_allocated
= 0x100;
1477 typeinfo
->func_data
[0] = 0;
1480 if(typeinfo
->func_data
[0] + typedata_size
+ sizeof(int) > typeinfo
->func_data_allocated
) {
1481 typeinfo
->func_data_allocated
= max(typeinfo
->func_data_allocated
* 2,
1482 typeinfo
->func_data
[0] + typedata_size
+ sizeof(int));
1483 typeinfo
->func_data
= xrealloc(typeinfo
->func_data
, typeinfo
->func_data_allocated
);
1486 offset
= typeinfo
->func_data
[0];
1487 typeinfo
->func_data
[0] += typedata_size
;
1488 typedata
= typeinfo
->func_data
+ (offset
>> 2) + 1;
1491 /* find func with the same name - if it exists use its id */
1492 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
& 0xffff); i
++) {
1493 if(name_offset
== typeinfo
->func_names
[i
]) {
1494 id
= typeinfo
->func_indices
[i
];
1499 /* find the first func with the same id and link via the hiword of typedata[4] */
1501 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
& 0xffff); i
++) {
1502 if(id
== typeinfo
->func_indices
[i
]) {
1503 next_idx
= typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] >> 16;
1504 typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] &= 0xffff;
1505 typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] |= (index
<< 16);
1510 /* fill out the basic type information */
1511 typedata
[0] = typedata_size
| (index
<< 16);
1512 encode_var(typeinfo
->typelib
, type_function_get_rettype(func
->declspec
.type
), func
,
1513 &typedata
[1], &decoded_size
);
1514 typedata
[2] = funcflags
;
1515 typedata
[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size
) << 16) | typeinfo
->typeinfo
->cbSizeVft
;
1516 typedata
[4] = (next_idx
<< 16) | (callconv
<< 8) | (invokekind
<< 3) | funckind
;
1517 if(has_arg_custdata
|| func_custdata_offset
!= -1) typedata
[4] |= 0x0080;
1518 if(num_defaults
) typedata
[4] |= 0x1000;
1519 if(entry_is_ord
) typedata
[4] |= 0x2000;
1520 typedata
[5] = (num_optional
<< 16) | num_params
;
1522 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1523 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1524 typedata
[3] += (16 /*sizeof(ELEMDESC)*/ * num_params
) << 16;
1525 typedata
[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults
) << 16;
1527 switch(extra_attr
) {
1529 if(extra_attr
> 7 + num_params
) warning("unknown number of optional attrs\n");
1530 /* typedata[13..+num_params] = arg_custdata_offset handled in below loop */
1531 case 7: typedata
[12] = func_custdata_offset
;
1532 case 6: typedata
[11] = help_string_context
;
1533 case 5: typedata
[10] = -1;
1534 case 4: typedata
[9] = -1;
1535 case 3: typedata
[8] = entry
;
1536 case 2: typedata
[7] = help_string_offset
;
1537 case 1: typedata
[6] = help_context
;
1542 if (type_function_get_args(func
->declspec
.type
))
1545 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), var_t
, entry
)
1548 int *paramdata
= typedata
+ 6 + extra_attr
+ (num_defaults
? num_params
: 0) + i
* 3;
1549 int *defaultdata
= num_defaults
? typedata
+ 6 + extra_attr
+ i
: NULL
;
1550 int arg_custdata_offset
= -1;
1552 if(defaultdata
) *defaultdata
= -1;
1554 encode_var(typeinfo
->typelib
, arg
->declspec
.type
, arg
, paramdata
, &decoded_size
);
1555 if (arg
->attrs
) LIST_FOR_EACH_ENTRY( attr
, arg
->attrs
, const attr_t
, entry
) {
1556 switch(attr
->type
) {
1558 set_custdata_attr(typeinfo
->typelib
, attr
->u
.pval
, &arg_custdata_offset
);
1560 case ATTR_DEFAULTVALUE
:
1562 paramflags
|= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1563 write_default_value(typeinfo
->typelib
, arg
->declspec
.type
, (expr_t
*)attr
->u
.pval
, defaultdata
);
1567 paramflags
|= 0x01; /* PARAMFLAG_FIN */
1570 paramflags
|= 0x10; /* PARAMFLAG_FOPT */
1573 paramflags
|= 0x02; /* PARAMFLAG_FOUT */
1575 case ATTR_PARAMLCID
:
1576 paramflags
|= 0x04; /* PARAMFLAG_LCID */
1577 lcid_retval_count
++;
1580 paramflags
|= 0x08; /* PARAMFLAG_FRETVAL */
1581 lcid_retval_count
++;
1584 chat("unhandled param attr %d\n", attr
->type
);
1587 if(extra_attr
> 7 + i
) {
1588 typedata
[13+i
] = arg_custdata_offset
;
1592 paramdata
[2] = paramflags
;
1593 typedata
[3] += decoded_size
<< 16;
1599 if(lcid_retval_count
== 1)
1600 typedata
[4] |= 0x4000;
1601 else if(lcid_retval_count
== 2)
1602 typedata
[4] |= 0x8000;
1604 if(typeinfo
->funcs_allocated
== 0) {
1605 typeinfo
->funcs_allocated
= 10;
1606 typeinfo
->func_indices
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1607 typeinfo
->func_names
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1608 typeinfo
->func_offsets
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1610 if(typeinfo
->funcs_allocated
== (typeinfo
->typeinfo
->cElement
& 0xffff)) {
1611 typeinfo
->funcs_allocated
*= 2;
1612 typeinfo
->func_indices
= xrealloc(typeinfo
->func_indices
, typeinfo
->funcs_allocated
* sizeof(int));
1613 typeinfo
->func_names
= xrealloc(typeinfo
->func_names
, typeinfo
->funcs_allocated
* sizeof(int));
1614 typeinfo
->func_offsets
= xrealloc(typeinfo
->func_offsets
, typeinfo
->funcs_allocated
* sizeof(int));
1617 /* update the index data */
1618 typeinfo
->func_indices
[typeinfo
->typeinfo
->cElement
& 0xffff] = id
;
1619 typeinfo
->func_offsets
[typeinfo
->typeinfo
->cElement
& 0xffff] = offset
;
1620 typeinfo
->func_names
[typeinfo
->typeinfo
->cElement
& 0xffff] = name_offset
;
1623 if (!typeinfo
->typeinfo
->res2
) typeinfo
->typeinfo
->res2
= 0x20;
1624 typeinfo
->typeinfo
->res2
<<= 1;
1626 if (index
< 2) typeinfo
->typeinfo
->res2
+= num_params
<< 4;
1628 if (typeinfo
->typeinfo
->res3
== -1) typeinfo
->typeinfo
->res3
= 0;
1629 typeinfo
->typeinfo
->res3
+= 0x38 + num_params
* 0x10;
1630 if(num_defaults
) typeinfo
->typeinfo
->res3
+= num_params
* 0x4;
1632 /* adjust size of VTBL */
1633 if(funckind
!= 0x3 /* FUNC_STATIC */)
1634 typeinfo
->typeinfo
->cbSizeVft
+= pointer_size
;
1636 /* Increment the number of function elements */
1637 typeinfo
->typeinfo
->cElement
+= 1;
1639 namedata
= typeinfo
->typelib
->typelib_segment_data
[MSFT_SEG_NAME
] + name_offset
;
1640 if (*((INT
*)namedata
) == -1) {
1641 *((INT
*)namedata
) = typeinfo
->typelib
->typelib_typeinfo_offsets
[typeinfo
->typeinfo
->typekind
>> 16];
1642 if(typeinfo
->typekind
== TKIND_MODULE
)
1643 namedata
[9] |= 0x10;
1645 namedata
[9] &= ~0x10;
1647 if(typeinfo
->typekind
== TKIND_MODULE
)
1648 namedata
[9] |= 0x20;
1650 if (type_function_get_args(func
->declspec
.type
))
1653 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), var_t
, entry
)
1655 /* don't give the last arg of a [propput*] func a name */
1656 if(i
!= num_params
- 1 || (invokekind
!= 0x4 /* INVOKE_PROPERTYPUT */ && invokekind
!= 0x8 /* INVOKE_PROPERTYPUTREF */))
1658 int *paramdata
= typedata
+ 6 + extra_attr
+ (num_defaults
? num_params
: 0) + i
* 3;
1659 offset
= ctl2_alloc_name(typeinfo
->typelib
, arg
->name
);
1660 paramdata
[1] = offset
;
1668 static HRESULT
add_var_desc(msft_typeinfo_t
*typeinfo
, UINT index
, var_t
* var
)
1671 unsigned int typedata_size
;
1674 unsigned int var_datawidth
, var_alignment
= 0;
1675 int var_type_size
, var_kind
= 0 /* VAR_PERINSTANCE */;
1679 unsigned char *namedata
;
1680 int var_num
= (typeinfo
->typeinfo
->cElement
>> 16) & 0xffff;
1681 int var_custdata_offset
= -1;
1684 var
->name
= gen_name();
1686 chat("add_var_desc(%d, %s)\n", index
, var
->name
);
1688 id
= 0x40000000 + index
;
1690 if (var
->attrs
) LIST_FOR_EACH_ENTRY( attr
, var
->attrs
, const attr_t
, entry
) {
1691 expr_t
*expr
= attr
->u
.pval
;
1692 switch(attr
->type
) {
1694 varflags
|= 0x04; /* VARFLAG_FBINDABLE */
1697 extra_attr
= max(extra_attr
,4);
1698 set_custdata_attr(typeinfo
->typelib
, attr
->u
.pval
, &var_custdata_offset
);
1700 case ATTR_DEFAULTBIND
:
1701 varflags
|= 0x20; /* VARFLAG_FDEFAULTBIND */
1703 case ATTR_DEFAULTCOLLELEM
:
1704 varflags
|= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1706 case ATTR_DISPLAYBIND
:
1707 varflags
|= 0x10; /* VARFLAG_FDISPLAYBIND */
1710 varflags
|= 0x40; /* VARFLAG_FHIDDEN */
1715 case ATTR_IMMEDIATEBIND
:
1716 varflags
|= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1718 case ATTR_NONBROWSABLE
:
1719 varflags
|= 0x400; /* VARFLAG_FNONBROWSABLE */
1722 varflags
|= 0x01; /* VARFLAG_FREADONLY */
1724 /* FIXME: VARFLAG_FREPLACEABLE */
1725 case ATTR_REQUESTEDIT
:
1726 varflags
|= 0x08; /* VARFLAG_FREQUESTEDIT */
1728 case ATTR_RESTRICTED
:
1729 varflags
|= 0x80; /* VARFLAG_FRESTRICTED */
1732 varflags
|= 0x02; /* VARFLAG_FSOURCE */
1734 case ATTR_UIDEFAULT
:
1735 varflags
|= 0x0200; /* VARFLAG_FUIDEFAULT */
1742 /* allocate type data space for us */
1743 typedata_size
= 0x14 + extra_attr
* sizeof(int);
1745 if (!typeinfo
->var_data
) {
1746 typeinfo
->var_data
= xmalloc(0x100);
1747 typeinfo
->var_data_allocated
= 0x100;
1748 typeinfo
->var_data
[0] = 0;
1751 if(typeinfo
->var_data
[0] + typedata_size
+ sizeof(int) > typeinfo
->var_data_allocated
) {
1752 typeinfo
->var_data_allocated
= max(typeinfo
->var_data_allocated
* 2,
1753 typeinfo
->var_data
[0] + typedata_size
+ sizeof(int));
1754 typeinfo
->var_data
= xrealloc(typeinfo
->var_data
, typeinfo
->var_data_allocated
);
1757 offset
= typeinfo
->var_data
[0];
1758 typeinfo
->var_data
[0] += typedata_size
;
1759 typedata
= typeinfo
->var_data
+ (offset
>> 2) + 1;
1761 /* fill out the basic type information */
1762 typedata
[0] = typedata_size
| (index
<< 16);
1763 typedata
[2] = varflags
;
1764 typedata
[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1766 if(typeinfo
->vars_allocated
== 0) {
1767 typeinfo
->vars_allocated
= 10;
1768 typeinfo
->var_indices
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1769 typeinfo
->var_names
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1770 typeinfo
->var_offsets
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1772 if(typeinfo
->vars_allocated
== var_num
) {
1773 typeinfo
->vars_allocated
*= 2;
1774 typeinfo
->var_indices
= xrealloc(typeinfo
->var_indices
, typeinfo
->vars_allocated
* sizeof(int));
1775 typeinfo
->var_names
= xrealloc(typeinfo
->var_names
, typeinfo
->vars_allocated
* sizeof(int));
1776 typeinfo
->var_offsets
= xrealloc(typeinfo
->var_offsets
, typeinfo
->vars_allocated
* sizeof(int));
1778 /* update the index data */
1779 typeinfo
->var_indices
[var_num
] = id
;
1780 typeinfo
->var_names
[var_num
] = -1;
1781 typeinfo
->var_offsets
[var_num
] = offset
;
1783 /* figure out type widths and whatnot */
1784 var_datawidth
= type_memsize_and_alignment(var
->declspec
.type
, &var_alignment
);
1785 encode_var(typeinfo
->typelib
, var
->declspec
.type
, var
, &typedata
[1], &var_type_size
);
1787 /* pad out starting position to data width */
1788 typeinfo
->datawidth
+= var_alignment
- 1;
1789 typeinfo
->datawidth
&= ~(var_alignment
- 1);
1791 switch(typeinfo
->typekind
) {
1793 write_int_value(typeinfo
->typelib
, &typedata
[4], VT_I4
, var
->eval
->cval
);
1794 var_kind
= 2; /* VAR_CONST */
1795 var_type_size
+= 16; /* sizeof(VARIANT) */
1796 typeinfo
->datawidth
= var_datawidth
;
1799 typedata
[4] = typeinfo
->datawidth
;
1800 typeinfo
->datawidth
+= var_datawidth
;
1804 typeinfo
->datawidth
= max(typeinfo
->datawidth
, var_datawidth
);
1806 case TKIND_DISPATCH
:
1807 var_kind
= 3; /* VAR_DISPATCH */
1809 typeinfo
->datawidth
= pointer_size
;
1812 error("add_var_desc: unhandled type kind %d\n", typeinfo
->typekind
);
1816 /* add type description size to total required allocation */
1817 typedata
[3] += var_type_size
<< 16 | var_kind
;
1819 switch(extra_attr
) {
1820 case 5: typedata
[9] = -1 /*help_string_context*/;
1821 case 4: typedata
[8] = var_custdata_offset
;
1822 case 3: typedata
[7] = -1;
1823 case 2: typedata
[6] = -1 /*help_string_offset*/;
1824 case 1: typedata
[5] = -1 /*help_context*/;
1828 warning("unknown number of optional attrs\n");
1831 /* fix type alignment */
1832 alignment
= (typeinfo
->typeinfo
->typekind
>> 11) & 0x1f;
1833 if (alignment
< var_alignment
) {
1834 alignment
= var_alignment
;
1835 typeinfo
->typeinfo
->typekind
&= ~0xffc0;
1836 typeinfo
->typeinfo
->typekind
|= alignment
<< 11 | alignment
<< 6;
1840 if (!typeinfo
->typeinfo
->res2
) typeinfo
->typeinfo
->res2
= 0x1a;
1841 if ((index
== 0) || (index
== 1) || (index
== 2) || (index
== 4) || (index
== 9)) {
1842 typeinfo
->typeinfo
->res2
<<= 1;
1846 if (typeinfo
->typeinfo
->res3
== -1) typeinfo
->typeinfo
->res3
= 0;
1847 typeinfo
->typeinfo
->res3
+= 0x2c;
1849 /* increment the number of variable elements */
1850 typeinfo
->typeinfo
->cElement
+= 0x10000;
1852 /* pad data width to alignment */
1853 typeinfo
->typeinfo
->size
= (typeinfo
->datawidth
+ (alignment
- 1)) & ~(alignment
- 1);
1855 offset
= ctl2_alloc_name(typeinfo
->typelib
, var
->name
);
1856 if (offset
== -1) return E_OUTOFMEMORY
;
1858 namedata
= typeinfo
->typelib
->typelib_segment_data
[MSFT_SEG_NAME
] + offset
;
1859 if (*((INT
*)namedata
) == -1) {
1860 *((INT
*)namedata
) = typeinfo
->typelib
->typelib_typeinfo_offsets
[typeinfo
->typeinfo
->typekind
>> 16];
1861 if(typeinfo
->typekind
!= TKIND_DISPATCH
)
1862 namedata
[9] |= 0x10;
1864 namedata
[9] &= ~0x10;
1866 if (typeinfo
->typekind
== TKIND_ENUM
) {
1867 namedata
[9] |= 0x20;
1869 typeinfo
->var_names
[var_num
] = offset
;
1874 static HRESULT
add_impl_type(msft_typeinfo_t
*typeinfo
, type_t
*ref
, importinfo_t
*importinfo
)
1877 alloc_importinfo(typeinfo
->typelib
, importinfo
);
1878 typeinfo
->typeinfo
->datatype1
= importinfo
->offset
+1;
1880 if(ref
->typelib_idx
== -1)
1881 add_interface_typeinfo(typeinfo
->typelib
, ref
);
1882 if(ref
->typelib_idx
== -1)
1883 error("add_impl_type: unable to add inherited interface\n");
1885 typeinfo
->typeinfo
->datatype1
= typeinfo
->typelib
->typelib_typeinfo_offsets
[ref
->typelib_idx
];
1888 typeinfo
->typeinfo
->cImplTypes
++;
1892 static msft_typeinfo_t
*create_msft_typeinfo(msft_typelib_t
*typelib
, enum type_kind kind
,
1893 const char *name
, const attr_list_t
*attrs
)
1896 msft_typeinfo_t
*msft_typeinfo
;
1898 int typeinfo_offset
;
1899 MSFT_TypeInfoBase
*typeinfo
;
1900 MSFT_GuidEntry guidentry
;
1902 chat("create_msft_typeinfo: name %s kind %d index %d\n", name
, kind
, typelib
->typelib_header
.nrtypeinfos
);
1904 msft_typeinfo
= xmalloc(sizeof(*msft_typeinfo
));
1905 memset( msft_typeinfo
, 0, sizeof(*msft_typeinfo
) );
1907 msft_typeinfo
->typelib
= typelib
;
1909 nameoffset
= ctl2_alloc_name(typelib
, name
);
1910 typeinfo_offset
= ctl2_alloc_typeinfo(typelib
, nameoffset
);
1911 typeinfo
= (MSFT_TypeInfoBase
*)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEINFO
][typeinfo_offset
];
1913 typelib
->typelib_segment_data
[MSFT_SEG_NAME
][nameoffset
+ 9] = 0x38;
1914 *((int *)&typelib
->typelib_segment_data
[MSFT_SEG_NAME
][nameoffset
]) = typeinfo_offset
;
1916 msft_typeinfo
->typekind
= kind
;
1917 msft_typeinfo
->typeinfo
= typeinfo
;
1919 typeinfo
->typekind
|= kind
| 0x20;
1921 if(kind
== TKIND_COCLASS
)
1922 typeinfo
->flags
|= 0x2; /* TYPEFLAG_FCANCREATE */
1924 if (attrs
) LIST_FOR_EACH_ENTRY( attr
, attrs
, const attr_t
, entry
) {
1925 switch(attr
->type
) {
1926 case ATTR_AGGREGATABLE
:
1927 if (kind
== TKIND_COCLASS
)
1928 typeinfo
->flags
|= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1931 case ATTR_APPOBJECT
:
1932 if (kind
== TKIND_COCLASS
)
1933 typeinfo
->flags
|= 0x1; /* TYPEFLAG_FAPPOBJECT */
1937 if (kind
== TKIND_COCLASS
)
1938 typeinfo
->flags
|= 0x20; /* TYPEFLAG_FCONTROL */
1941 set_custdata_attr(typelib
, attr
->u
.pval
, &typeinfo
->oCustData
);
1945 int offset
= ctl2_alloc_string(typelib
, attr
->u
.pval
);
1946 typeinfo
->datatype1
= offset
;
1951 /* FIXME: check interface is compatible */
1952 typeinfo
->typekind
= (typeinfo
->typekind
& ~0xff) | 0x34;
1953 typeinfo
->flags
|= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1956 case ATTR_HELPCONTEXT
:
1958 expr_t
*expr
= (expr_t
*)attr
->u
.pval
;
1959 typeinfo
->helpcontext
= expr
->cval
;
1962 case ATTR_HELPSTRING
:
1964 int offset
= ctl2_alloc_string(typelib
, attr
->u
.pval
);
1965 if (offset
== -1) break;
1966 typeinfo
->docstringoffs
= offset
;
1969 case ATTR_HELPSTRINGCONTEXT
:
1971 expr_t
*expr
= (expr_t
*)attr
->u
.pval
;
1972 typeinfo
->helpstringcontext
= expr
->cval
;
1976 typeinfo
->flags
|= 0x10; /* TYPEFLAG_FHIDDEN */
1980 typeinfo
->flags
|= 0x04; /* TYPEFLAG_FLICENSED */
1983 case ATTR_NONCREATABLE
:
1984 typeinfo
->flags
&= ~0x2; /* TYPEFLAG_FCANCREATE */
1987 case ATTR_NONEXTENSIBLE
:
1988 typeinfo
->flags
|= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1991 case ATTR_OLEAUTOMATION
:
1992 typeinfo
->flags
|= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1995 /* FIXME: TYPEFLAG_FPREDCLID */
1998 typeinfo
->flags
|= 0x4000; /* TYPEFLAG_FPROXY */
2001 /* FIXME: TYPEFLAG_FREPLACEABLE */
2003 case ATTR_RESTRICTED
:
2004 typeinfo
->flags
|= 0x200; /* TYPEFLAG_FRESTRICTED */
2008 guidentry
.guid
= *(GUID
*)attr
->u
.pval
;
2009 guidentry
.hreftype
= typelib
->typelib_typeinfo_offsets
[typeinfo
->typekind
>> 16];
2010 guidentry
.next_hash
= -1;
2011 typeinfo
->posguid
= ctl2_alloc_guid(typelib
, &guidentry
);
2013 if (IsEqualIID(guid
, &IID_IDispatch
)) {
2014 typelib
->typelib_header
.dispatchpos
= typelib
->typelib_typeinfo_offsets
[typeinfo
->typekind
>> 16];
2020 typeinfo
->version
= attr
->u
.ival
;
2028 if (typelib
->last_typeinfo
) typelib
->last_typeinfo
->next_typeinfo
= msft_typeinfo
;
2029 typelib
->last_typeinfo
= msft_typeinfo
;
2030 if (!typelib
->typeinfos
) typelib
->typeinfos
= msft_typeinfo
;
2033 return msft_typeinfo
;
2036 static void add_dispatch(msft_typelib_t
*typelib
)
2038 int guid_offset
, impfile_offset
, hash_key
;
2039 MSFT_GuidEntry guidentry
;
2040 MSFT_ImpInfo impinfo
;
2041 GUID stdole
= {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2042 GUID iid_idispatch
= {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2044 if(typelib
->typelib_header
.dispatchpos
!= -1) return;
2046 guidentry
.guid
= stdole
;
2047 guidentry
.hreftype
= 2;
2048 guidentry
.next_hash
= -1;
2049 hash_key
= ctl2_hash_guid(&guidentry
.guid
);
2050 guid_offset
= ctl2_find_guid(typelib
, hash_key
, &guidentry
.guid
);
2051 if (guid_offset
== -1)
2052 guid_offset
= ctl2_alloc_guid(typelib
, &guidentry
);
2053 impfile_offset
= alloc_importfile(typelib
, guid_offset
, 2, 0, "stdole2.tlb");
2055 guidentry
.guid
= iid_idispatch
;
2056 guidentry
.hreftype
= 1;
2057 guidentry
.next_hash
= -1;
2058 impinfo
.flags
= TKIND_INTERFACE
<< 24 | MSFT_IMPINFO_OFFSET_IS_GUID
;
2059 impinfo
.oImpFile
= impfile_offset
;
2060 hash_key
= ctl2_hash_guid(&guidentry
.guid
);
2061 guid_offset
= ctl2_find_guid(typelib
, hash_key
, &guidentry
.guid
);
2062 if (guid_offset
== -1)
2063 guid_offset
= ctl2_alloc_guid(typelib
, &guidentry
);
2064 impinfo
.oGuid
= guid_offset
;
2065 typelib
->typelib_header
.dispatchpos
= alloc_msft_importinfo(typelib
, &impinfo
) | 0x01;
2068 static void add_dispinterface_typeinfo(msft_typelib_t
*typelib
, type_t
*dispinterface
)
2070 int num_parents
= 0, num_funcs
= 0;
2071 importinfo_t
*importinfo
= NULL
;
2072 const statement_t
*stmt_func
;
2073 type_t
*inherit
, *ref
;
2077 msft_typeinfo_t
*msft_typeinfo
;
2079 if (-1 < dispinterface
->typelib_idx
)
2082 inherit
= type_dispiface_get_inherit(dispinterface
);
2086 importinfo
= find_importinfo(typelib
, inherit
->name
);
2088 if (!importinfo
&& type_iface_get_inherit(inherit
) && inherit
->typelib_idx
== -1)
2089 add_interface_typeinfo(typelib
, inherit
);
2092 /* check typelib_idx again, it could have been added while resolving the parent interface */
2093 if (-1 < dispinterface
->typelib_idx
)
2096 dispinterface
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2097 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_DISPATCH
, dispinterface
->name
,
2098 dispinterface
->attrs
);
2100 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2101 msft_typeinfo
->typeinfo
->typekind
|= pointer_size
<< 11 | pointer_size
<< 6;
2103 msft_typeinfo
->typeinfo
->flags
|= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2104 add_dispatch(typelib
);
2108 add_impl_type(msft_typeinfo
, inherit
, importinfo
);
2109 msft_typeinfo
->typeinfo
->typekind
|= 0x10;
2112 /* count the number of inherited interfaces and non-local functions */
2113 for (ref
= inherit
; ref
; ref
= type_iface_get_inherit(ref
))
2116 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(ref
) )
2118 var_t
*func
= stmt_func
->u
.var
;
2119 if (!is_local(func
->attrs
)) num_funcs
++;
2122 msft_typeinfo
->typeinfo
->datatype2
= num_funcs
<< 16 | num_parents
;
2123 msft_typeinfo
->typeinfo
->cbSizeVft
= num_funcs
* pointer_size
;
2125 msft_typeinfo
->typeinfo
->cImplTypes
= 1; /* IDispatch */
2127 /* count the no of methods, as the variable indices come after the funcs */
2128 if (dispinterface
->details
.iface
->disp_methods
)
2129 LIST_FOR_EACH_ENTRY( func
, dispinterface
->details
.iface
->disp_methods
, var_t
, entry
)
2132 if (type_dispiface_get_props(dispinterface
))
2133 LIST_FOR_EACH_ENTRY( var
, type_dispiface_get_props(dispinterface
), var_t
, entry
)
2134 add_var_desc(msft_typeinfo
, idx
++, var
);
2136 if (type_dispiface_get_methods(dispinterface
))
2139 LIST_FOR_EACH_ENTRY( func
, type_dispiface_get_methods(dispinterface
), var_t
, entry
)
2140 if(add_func_desc(msft_typeinfo
, func
, idx
) == S_OK
)
2144 typelib
->typelib
->reg_ifaces
= xrealloc(typelib
->typelib
->reg_ifaces
,
2145 (typelib
->typelib
->reg_iface_count
+ 1) * sizeof(dispinterface
));
2146 typelib
->typelib
->reg_ifaces
[typelib
->typelib
->reg_iface_count
++] = dispinterface
;
2149 static void add_interface_typeinfo(msft_typelib_t
*typelib
, type_t
*interface
)
2152 const statement_t
*stmt_func
;
2154 msft_typeinfo_t
*msft_typeinfo
;
2155 importinfo_t
*ref_importinfo
= NULL
;
2156 int num_parents
= 0, num_funcs
= 0;
2158 const type_t
*derived
;
2160 if (-1 < interface
->typelib_idx
)
2163 if (!interface
->details
.iface
)
2165 error( "interface %s is referenced but not defined\n", interface
->name
);
2169 if (is_attr(interface
->attrs
, ATTR_DISPINTERFACE
)) {
2170 add_dispinterface_typeinfo(typelib
, interface
);
2174 /* midl adds the parent interface first, unless the parent itself
2175 has no parent (i.e. it stops before IUnknown). */
2177 inherit
= type_iface_get_inherit(interface
);
2180 ref_importinfo
= find_importinfo(typelib
, inherit
->name
);
2182 if(!ref_importinfo
&& type_iface_get_inherit(inherit
) &&
2183 inherit
->typelib_idx
== -1)
2184 add_interface_typeinfo(typelib
, inherit
);
2187 /* check typelib_idx again, it could have been added while resolving the parent interface */
2188 if (-1 < interface
->typelib_idx
)
2191 interface
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2192 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_INTERFACE
, interface
->name
, interface
->attrs
);
2193 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2194 msft_typeinfo
->typeinfo
->typekind
|= 0x0200;
2195 msft_typeinfo
->typeinfo
->typekind
|= pointer_size
<< 11;
2197 for (derived
= inherit
; derived
; derived
= type_iface_get_inherit(derived
))
2198 if (derived
->name
&& !strcmp(derived
->name
, "IDispatch"))
2199 msft_typeinfo
->typeinfo
->flags
|= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2201 if(type_iface_get_inherit(interface
))
2202 add_impl_type(msft_typeinfo
, type_iface_get_inherit(interface
),
2205 /* count the number of inherited interfaces and non-local functions */
2206 for(ref
= inherit
; ref
; ref
= type_iface_get_inherit(ref
)) {
2208 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(ref
) ) {
2209 var_t
*func
= stmt_func
->u
.var
;
2210 if (!is_local(func
->attrs
)) num_funcs
++;
2213 msft_typeinfo
->typeinfo
->datatype2
= num_funcs
<< 16 | num_parents
;
2214 msft_typeinfo
->typeinfo
->cbSizeVft
= num_funcs
* pointer_size
;
2216 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(interface
) ) {
2217 var_t
*func
= stmt_func
->u
.var
;
2218 if(add_func_desc(msft_typeinfo
, func
, idx
) == S_OK
)
2222 if (is_attr(interface
->attrs
, ATTR_OLEAUTOMATION
) || is_attr(interface
->attrs
, ATTR_DUAL
))
2224 typelib
->typelib
->reg_ifaces
= xrealloc(typelib
->typelib
->reg_ifaces
,
2225 (typelib
->typelib
->reg_iface_count
+ 1) * sizeof(interface
));
2226 typelib
->typelib
->reg_ifaces
[typelib
->typelib
->reg_iface_count
++] = interface
;
2230 static void add_structure_typeinfo(msft_typelib_t
*typelib
, type_t
*structure
)
2235 msft_typeinfo_t
*msft_typeinfo
;
2237 if (-1 < structure
->typelib_idx
)
2240 structure
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2241 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_RECORD
, structure
->name
, structure
->attrs
);
2242 msft_typeinfo
->typeinfo
->size
= 0;
2244 if (type_get_type(structure
) == TYPE_STRUCT
)
2245 fields
= type_struct_get_fields(structure
);
2247 fields
= type_encapsulated_union_get_fields(structure
);
2251 LIST_FOR_EACH_ENTRY( cur
, fields
, var_t
, entry
)
2252 add_var_desc(msft_typeinfo
, idx
++, cur
);
2256 static void add_enum_typeinfo(msft_typelib_t
*typelib
, type_t
*enumeration
)
2260 msft_typeinfo_t
*msft_typeinfo
;
2262 if (-1 < enumeration
->typelib_idx
)
2265 enumeration
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2266 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_ENUM
, enumeration
->name
, enumeration
->attrs
);
2267 msft_typeinfo
->typeinfo
->size
= 0;
2269 if (type_enum_get_values(enumeration
))
2270 LIST_FOR_EACH_ENTRY( cur
, type_enum_get_values(enumeration
), var_t
, entry
)
2271 add_var_desc(msft_typeinfo
, idx
++, cur
);
2274 static void add_union_typeinfo(msft_typelib_t
*typelib
, type_t
*tunion
)
2278 msft_typeinfo_t
*msft_typeinfo
;
2280 if (-1 < tunion
->typelib_idx
)
2284 tunion
->name
= gen_name();
2286 tunion
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2287 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_UNION
, tunion
->name
, tunion
->attrs
);
2288 msft_typeinfo
->typeinfo
->size
= 0;
2290 if (type_union_get_cases(tunion
))
2291 LIST_FOR_EACH_ENTRY(cur
, type_union_get_cases(tunion
), var_t
, entry
)
2292 add_var_desc(msft_typeinfo
, idx
++, cur
);
2295 static void add_typedef_typeinfo(msft_typelib_t
*typelib
, type_t
*tdef
)
2297 msft_typeinfo_t
*msft_typeinfo
= NULL
;
2298 int datatype1
, datatype2
, duplicate
= 0;
2299 unsigned int size
, alignment
= 0;
2302 if (-1 < tdef
->typelib_idx
)
2305 type
= type_alias_get_aliasee_type(tdef
);
2307 if (!type
->name
|| strcmp(tdef
->name
, type
->name
) != 0)
2309 tdef
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2310 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_ALIAS
, tdef
->name
, tdef
->attrs
);
2315 encode_type(typelib
, get_type_vt(type
), type
, &datatype1
, &datatype2
);
2316 size
= type_memsize_and_alignment(type
, &alignment
);
2320 msft_typeinfo
->typeinfo
->datatype1
= datatype1
;
2321 msft_typeinfo
->typeinfo
->size
= size
;
2322 msft_typeinfo
->typeinfo
->datatype2
= datatype2
;
2323 msft_typeinfo
->typeinfo
->typekind
|= (alignment
<< 11 | alignment
<< 6);
2326 /* avoid adding duplicate type definitions */
2328 tdef
->typelib_idx
= type
->typelib_idx
;
2331 static void add_coclass_typeinfo(msft_typelib_t
*typelib
, type_t
*cls
)
2333 msft_typeinfo_t
*msft_typeinfo
;
2335 int num_ifaces
= 0, offset
, i
;
2336 MSFT_RefRecord
*ref
, *first
= NULL
, *first_source
= NULL
;
2337 int have_default
= 0, have_default_source
= 0;
2339 ifref_list_t
*ifaces
;
2341 if (-1 < cls
->typelib_idx
)
2344 cls
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2345 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_COCLASS
, cls
->name
, cls
->attrs
);
2347 ifaces
= type_coclass_get_ifaces(cls
);
2348 if (ifaces
) LIST_FOR_EACH_ENTRY( iref
, ifaces
, ifref_t
, entry
) num_ifaces
++;
2350 offset
= msft_typeinfo
->typeinfo
->datatype1
= ctl2_alloc_segment(typelib
, MSFT_SEG_REFERENCES
,
2351 num_ifaces
* sizeof(*ref
), 0);
2354 if (ifaces
) LIST_FOR_EACH_ENTRY( iref
, ifaces
, ifref_t
, entry
) {
2355 if(iref
->type
->typelib_idx
== -1)
2356 add_interface_typeinfo(typelib
, iref
->type
);
2357 ref
= (MSFT_RefRecord
*) (typelib
->typelib_segment_data
[MSFT_SEG_REFERENCES
] + offset
+ i
* sizeof(*ref
));
2358 ref
->reftype
= typelib
->typelib_typeinfo_offsets
[iref
->type
->typelib_idx
];
2360 ref
->oCustData
= -1;
2362 if(i
< num_ifaces
- 1)
2363 ref
->onext
= offset
+ (i
+ 1) * sizeof(*ref
);
2365 if (iref
->attrs
) LIST_FOR_EACH_ENTRY( attr
, iref
->attrs
, const attr_t
, entry
) {
2366 switch(attr
->type
) {
2368 ref
->flags
|= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2370 case ATTR_DEFAULTVTABLE
:
2371 ref
->flags
|= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2373 case ATTR_RESTRICTED
:
2374 ref
->flags
|= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2377 ref
->flags
|= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2380 warning("add_coclass_typeinfo: unhandled attr %d\n", attr
->type
);
2383 if(ref
->flags
& 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2384 if(ref
->flags
& 0x2) /* IMPLTYPEFLAG_SOURCE */
2385 have_default_source
= 1;
2390 /* If the interface is non-restricted and we haven't already had one then
2391 remember it so that we can use it as a default later */
2392 if((ref
->flags
& 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2393 if(ref
->flags
& 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2403 /* If we haven't had a default interface, then set the default flags on the
2405 if(!have_default
&& first
)
2406 first
->flags
|= 0x1;
2407 if(!have_default_source
&& first_source
)
2408 first_source
->flags
|= 0x1;
2410 msft_typeinfo
->typeinfo
->cImplTypes
= num_ifaces
;
2411 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2412 msft_typeinfo
->typeinfo
->typekind
|= 0x2200;
2415 static void add_module_typeinfo(msft_typelib_t
*typelib
, type_t
*module
)
2418 const statement_t
*stmt
;
2419 msft_typeinfo_t
*msft_typeinfo
;
2421 if (-1 < module
->typelib_idx
)
2424 module
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2425 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_MODULE
, module
->name
, module
->attrs
);
2426 msft_typeinfo
->typeinfo
->typekind
|= 0x0a00;
2428 STATEMENTS_FOR_EACH_FUNC( stmt
, module
->details
.module
->stmts
) {
2429 var_t
*func
= stmt
->u
.var
;
2430 if(add_func_desc(msft_typeinfo
, func
, idx
) == S_OK
)
2434 msft_typeinfo
->typeinfo
->size
= idx
;
2437 static void add_type_typeinfo(msft_typelib_t
*typelib
, type_t
*type
)
2439 switch (type_get_type(type
)) {
2440 case TYPE_INTERFACE
:
2441 add_interface_typeinfo(typelib
, type
);
2444 case TYPE_ENCAPSULATED_UNION
:
2445 add_structure_typeinfo(typelib
, type
);
2448 add_enum_typeinfo(typelib
, type
);
2451 add_union_typeinfo(typelib
, type
);
2454 add_coclass_typeinfo(typelib
, type
);
2461 error("add_entry: unhandled type 0x%x for %s\n",
2462 type_get_type(type
), type
->name
);
2467 static void add_entry(msft_typelib_t
*typelib
, const statement_t
*stmt
)
2469 switch(stmt
->type
) {
2474 case STMT_DECLARATION
:
2475 /* not included in typelib */
2477 case STMT_IMPORTLIB
:
2478 /* not processed here */
2482 const type_list_t
*type_entry
= stmt
->u
.type_list
;
2483 for (; type_entry
; type_entry
= type_entry
->next
) {
2484 /* if the type is public then add the typedef, otherwise attempt
2485 * to add the aliased type */
2486 if (is_attr(type_entry
->type
->attrs
, ATTR_PUBLIC
))
2487 add_typedef_typeinfo(typelib
, type_entry
->type
);
2489 add_type_typeinfo(typelib
, type_alias_get_aliasee_type(type_entry
->type
));
2494 add_module_typeinfo(typelib
, stmt
->u
.type
);
2499 type_t
*type
= stmt
->u
.type
;
2500 add_type_typeinfo(typelib
, type
);
2506 static void set_name(msft_typelib_t
*typelib
)
2510 offset
= ctl2_alloc_name(typelib
, typelib
->typelib
->name
);
2511 if (offset
== -1) return;
2512 typelib
->typelib_header
.NameOffset
= offset
;
2516 static void set_version(msft_typelib_t
*typelib
)
2518 typelib
->typelib_header
.version
= get_attrv( typelib
->typelib
->attrs
, ATTR_VERSION
);
2521 static void set_guid(msft_typelib_t
*typelib
)
2523 MSFT_GuidEntry guidentry
;
2526 GUID guid
= {0,0,0,{0,0,0,0,0,0}};
2528 guidentry
.guid
= guid
;
2529 guidentry
.hreftype
= -2;
2530 guidentry
.next_hash
= -1;
2532 ptr
= get_attrp( typelib
->typelib
->attrs
, ATTR_UUID
);
2533 if (ptr
) guidentry
.guid
= *(GUID
*)ptr
;
2535 offset
= ctl2_alloc_guid(typelib
, &guidentry
);
2536 typelib
->typelib_header
.posguid
= offset
;
2541 static void set_doc_string(msft_typelib_t
*typelib
)
2543 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRING
);
2547 int offset
= ctl2_alloc_string(typelib
, str
);
2548 if (offset
!= -1) typelib
->typelib_header
.helpstring
= offset
;
2552 static void set_help_file_name(msft_typelib_t
*typelib
)
2554 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPFILE
);
2558 int offset
= ctl2_alloc_string(typelib
, str
);
2561 typelib
->typelib_header
.helpfile
= offset
;
2562 typelib
->typelib_header
.varflags
|= 0x10;
2567 static void set_help_context(msft_typelib_t
*typelib
)
2569 const expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPCONTEXT
);
2570 if (expr
) typelib
->typelib_header
.helpcontext
= expr
->cval
;
2573 static void set_help_string_dll(msft_typelib_t
*typelib
)
2575 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRINGDLL
);
2579 int offset
= ctl2_alloc_string(typelib
, str
);
2582 typelib
->help_string_dll_offset
= offset
;
2583 typelib
->typelib_header
.varflags
|= 0x100;
2588 static void set_help_string_context(msft_typelib_t
*typelib
)
2590 const expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRINGCONTEXT
);
2591 if (expr
) typelib
->typelib_header
.helpstringcontext
= expr
->cval
;
2594 static void set_lcid(msft_typelib_t
*typelib
)
2596 const expr_t
*lcid_expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_LIBLCID
);
2599 typelib
->typelib_header
.lcid
= lcid_expr
->cval
;
2600 typelib
->typelib_header
.lcid2
= lcid_expr
->cval
;
2604 static void set_lib_flags(msft_typelib_t
*typelib
)
2608 typelib
->typelib_header
.flags
= 0;
2609 if (!typelib
->typelib
->attrs
) return;
2610 LIST_FOR_EACH_ENTRY( attr
, typelib
->typelib
->attrs
, const attr_t
, entry
)
2612 switch(attr
->type
) {
2614 typelib
->typelib_header
.flags
|= 0x02; /* LIBFLAG_FCONTROL */
2617 typelib
->typelib_header
.flags
|= 0x04; /* LIBFLAG_FHIDDEN */
2619 case ATTR_RESTRICTED
:
2620 typelib
->typelib_header
.flags
|= 0x01; /* LIBFLAG_FRESTRICTED */
2629 static void ctl2_write_segment(msft_typelib_t
*typelib
, int segment
)
2631 if (typelib
->typelib_segment_data
[segment
])
2632 put_data(typelib
->typelib_segment_data
[segment
], typelib
->typelib_segdir
[segment
].length
);
2635 static void ctl2_finalize_typeinfos(msft_typelib_t
*typelib
, int filesize
)
2637 msft_typeinfo_t
*typeinfo
;
2639 for (typeinfo
= typelib
->typeinfos
; typeinfo
; typeinfo
= typeinfo
->next_typeinfo
) {
2640 typeinfo
->typeinfo
->memoffset
= filesize
;
2641 if (typeinfo
->func_data
)
2642 filesize
+= typeinfo
->func_data
[0] + ((typeinfo
->typeinfo
->cElement
& 0xffff) * 12);
2643 if (typeinfo
->var_data
)
2644 filesize
+= typeinfo
->var_data
[0] + (((typeinfo
->typeinfo
->cElement
>> 16) & 0xffff) * 12);
2645 if (typeinfo
->func_data
|| typeinfo
->var_data
)
2650 static int ctl2_finalize_segment(msft_typelib_t
*typelib
, int filepos
, int segment
)
2652 if (typelib
->typelib_segdir
[segment
].length
) {
2653 typelib
->typelib_segdir
[segment
].offset
= filepos
;
2655 typelib
->typelib_segdir
[segment
].offset
= -1;
2658 return typelib
->typelib_segdir
[segment
].length
;
2662 static void ctl2_write_typeinfos(msft_typelib_t
*typelib
)
2664 msft_typeinfo_t
*typeinfo
;
2667 for (typeinfo
= typelib
->typeinfos
; typeinfo
; typeinfo
= typeinfo
->next_typeinfo
) {
2668 if (!typeinfo
->func_data
&& !typeinfo
->var_data
) continue;
2670 if (typeinfo
->func_data
)
2671 typedata_size
= typeinfo
->func_data
[0];
2672 if (typeinfo
->var_data
)
2673 typedata_size
+= typeinfo
->var_data
[0];
2674 put_data(&typedata_size
, sizeof(int));
2675 if (typeinfo
->func_data
)
2676 put_data(typeinfo
->func_data
+ 1, typeinfo
->func_data
[0]);
2677 if (typeinfo
->var_data
)
2678 put_data(typeinfo
->var_data
+ 1, typeinfo
->var_data
[0]);
2679 if (typeinfo
->func_indices
)
2680 put_data(typeinfo
->func_indices
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2681 if (typeinfo
->var_indices
)
2682 put_data(typeinfo
->var_indices
, (typeinfo
->typeinfo
->cElement
>> 16) * 4);
2683 if (typeinfo
->func_names
)
2684 put_data(typeinfo
->func_names
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2685 if (typeinfo
->var_names
)
2686 put_data(typeinfo
->var_names
, (typeinfo
->typeinfo
->cElement
>> 16) * 4);
2687 if (typeinfo
->func_offsets
)
2688 put_data(typeinfo
->func_offsets
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2689 if (typeinfo
->var_offsets
) {
2690 int add
= 0, i
, offset
;
2691 if(typeinfo
->func_data
)
2692 add
= typeinfo
->func_data
[0];
2693 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
>> 16); i
++) {
2694 offset
= typeinfo
->var_offsets
[i
];
2696 put_data(&offset
, 4);
2702 static void save_all_changes(msft_typelib_t
*typelib
)
2706 chat("save_all_changes(%p)\n", typelib
);
2708 filepos
= sizeof(MSFT_Header
) + sizeof(MSFT_SegDir
);
2709 if(typelib
->typelib_header
.varflags
& 0x100) filepos
+= 4; /* helpstringdll */
2710 filepos
+= typelib
->typelib_header
.nrtypeinfos
* 4;
2712 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_TYPEINFO
);
2713 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_GUIDHASH
);
2714 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_GUID
);
2715 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_REFERENCES
);
2716 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_IMPORTINFO
);
2717 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_IMPORTFILES
);
2718 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_NAMEHASH
);
2719 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_NAME
);
2720 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_STRING
);
2721 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_TYPEDESC
);
2722 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_ARRAYDESC
);
2723 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_CUSTDATA
);
2724 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_CUSTDATAGUID
);
2726 ctl2_finalize_typeinfos(typelib
, filepos
);
2729 init_output_buffer();
2731 put_data(&typelib
->typelib_header
, sizeof(typelib
->typelib_header
));
2732 if(typelib
->typelib_header
.varflags
& 0x100)
2733 put_data(&typelib
->help_string_dll_offset
, sizeof(typelib
->help_string_dll_offset
));
2735 put_data(typelib
->typelib_typeinfo_offsets
, typelib
->typelib_header
.nrtypeinfos
* 4);
2736 put_data(&typelib
->typelib_segdir
, sizeof(typelib
->typelib_segdir
));
2737 ctl2_write_segment( typelib
, MSFT_SEG_TYPEINFO
);
2738 ctl2_write_segment( typelib
, MSFT_SEG_GUIDHASH
);
2739 ctl2_write_segment( typelib
, MSFT_SEG_GUID
);
2740 ctl2_write_segment( typelib
, MSFT_SEG_REFERENCES
);
2741 ctl2_write_segment( typelib
, MSFT_SEG_IMPORTINFO
);
2742 ctl2_write_segment( typelib
, MSFT_SEG_IMPORTFILES
);
2743 ctl2_write_segment( typelib
, MSFT_SEG_NAMEHASH
);
2744 ctl2_write_segment( typelib
, MSFT_SEG_NAME
);
2745 ctl2_write_segment( typelib
, MSFT_SEG_STRING
);
2746 ctl2_write_segment( typelib
, MSFT_SEG_TYPEDESC
);
2747 ctl2_write_segment( typelib
, MSFT_SEG_ARRAYDESC
);
2748 ctl2_write_segment( typelib
, MSFT_SEG_CUSTDATA
);
2749 ctl2_write_segment( typelib
, MSFT_SEG_CUSTDATAGUID
);
2751 ctl2_write_typeinfos(typelib
);
2753 if (strendswith( typelib_name
, ".res" )) /* create a binary resource file */
2755 char typelib_id
[13] = "#1";
2757 expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_ID
);
2759 sprintf( typelib_id
, "#%d", expr
->cval
);
2760 add_output_to_resources( "TYPELIB", typelib_id
);
2761 output_typelib_regscript( typelib
->typelib
);
2763 else flush_output_buffer( typelib_name
);
2766 int create_msft_typelib(typelib_t
*typelib
)
2768 msft_typelib_t
*msft
;
2770 const statement_t
*stmt
;
2773 char *time_override
;
2774 unsigned int version
= 7 << 24 | 555; /* 7.00.0555 */
2775 GUID midl_time_guid
= {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2776 GUID midl_version_guid
= {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2777 GUID midl_info_guid
= {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2778 char info_string
[128];
2780 msft
= xmalloc(sizeof(*msft
));
2781 memset(msft
, 0, sizeof(*msft
));
2782 msft
->typelib
= typelib
;
2784 ctl2_init_header(msft
);
2785 ctl2_init_segdir(msft
);
2787 msft
->typelib_header
.varflags
|= (pointer_size
== 8) ? SYS_WIN64
: SYS_WIN32
;
2790 * The following two calls return an offset or -1 if out of memory. We
2791 * specifically need an offset of 0, however, so...
2793 if (ctl2_alloc_segment(msft
, MSFT_SEG_GUIDHASH
, 0x80, 0x80)) { failed
= 1; }
2794 if (ctl2_alloc_segment(msft
, MSFT_SEG_NAMEHASH
, 0x200, 0x200)) { failed
= 1; }
2802 msft
->typelib_guidhash_segment
= (int *)msft
->typelib_segment_data
[MSFT_SEG_GUIDHASH
];
2803 msft
->typelib_namehash_segment
= (int *)msft
->typelib_segment_data
[MSFT_SEG_NAMEHASH
];
2805 memset(msft
->typelib_guidhash_segment
, 0xff, 0x80);
2806 memset(msft
->typelib_namehash_segment
, 0xff, 0x200);
2808 set_lib_flags(msft
);
2810 set_help_file_name(msft
);
2811 set_doc_string(msft
);
2815 set_help_context(msft
);
2816 set_help_string_dll(msft
);
2817 set_help_string_context(msft
);
2819 if (typelib
->attrs
) LIST_FOR_EACH_ENTRY( attr
, typelib
->attrs
, const attr_t
, entry
) {
2820 switch(attr
->type
) {
2822 set_custdata_attr(msft
, attr
->u
.pval
, &msft
->typelib_header
.CustomDataOffset
);
2829 /* midl adds two sets of custom data to the library: the current unix time
2830 and midl's version number */
2831 time_override
= getenv( "WIDL_TIME_OVERRIDE");
2832 cur_time
= time_override
? atol( time_override
) : time(NULL
);
2833 sprintf(info_string
, "Created by WIDL version %s at %s", PACKAGE_VERSION
, ctime(&cur_time
));
2834 set_custdata(msft
, &midl_info_guid
, VT_BSTR
, info_string
, &msft
->typelib_header
.CustomDataOffset
);
2835 set_custdata(msft
, &midl_time_guid
, VT_UI4
, &cur_time
, &msft
->typelib_header
.CustomDataOffset
);
2836 set_custdata(msft
, &midl_version_guid
, VT_UI4
, &version
, &msft
->typelib_header
.CustomDataOffset
);
2839 LIST_FOR_EACH_ENTRY( stmt
, typelib
->stmts
, const statement_t
, entry
)
2840 add_entry(msft
, stmt
);
2842 save_all_changes(msft
);