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.
39 #define NONAMELESSUNION
43 #include "typelib_struct.h"
51 enum MSFT_segment_index
{
52 MSFT_SEG_TYPEINFO
= 0, /* type information */
53 MSFT_SEG_IMPORTINFO
, /* import information */
54 MSFT_SEG_IMPORTFILES
, /* import filenames */
55 MSFT_SEG_REFERENCES
, /* references (?) */
56 MSFT_SEG_GUIDHASH
, /* hash table for guids? */
57 MSFT_SEG_GUID
, /* guid storage */
58 MSFT_SEG_NAMEHASH
, /* hash table for names */
59 MSFT_SEG_NAME
, /* name storage */
60 MSFT_SEG_STRING
, /* string storage */
61 MSFT_SEG_TYPEDESC
, /* type descriptions */
62 MSFT_SEG_ARRAYDESC
, /* array descriptions */
63 MSFT_SEG_CUSTDATA
, /* custom data */
64 MSFT_SEG_CUSTDATAGUID
, /* custom data guids */
65 MSFT_SEG_UNKNOWN
, /* ??? */
66 MSFT_SEG_UNKNOWN2
, /* ??? */
67 MSFT_SEG_MAX
/* total number of segments */
70 typedef struct tagMSFT_ImpFile
{
74 char filename
[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
77 typedef struct _msft_typelib_t
80 MSFT_Header typelib_header
;
81 MSFT_pSeg typelib_segdir
[MSFT_SEG_MAX
];
82 unsigned char *typelib_segment_data
[MSFT_SEG_MAX
];
83 int typelib_segment_block_length
[MSFT_SEG_MAX
];
85 int typelib_typeinfo_offsets
[0x200]; /* Hope that's enough. */
87 int *typelib_namehash_segment
;
88 int *typelib_guidhash_segment
;
90 int help_string_dll_offset
;
92 struct _msft_typeinfo_t
*typeinfos
;
93 struct _msft_typeinfo_t
*last_typeinfo
;
96 typedef struct _msft_typeinfo_t
98 msft_typelib_t
*typelib
;
99 MSFT_TypeInfoBase
*typeinfo
;
103 unsigned int var_data_allocated
;
106 unsigned int func_data_allocated
;
121 struct _msft_typeinfo_t
*next_typeinfo
;
126 /*================== Internal functions ===================================*/
128 /****************************************************************************
131 * Initializes the type library header of a new typelib.
133 static void ctl2_init_header(
134 msft_typelib_t
*typelib
) /* [I] The typelib to initialize. */
136 typelib
->typelib_header
.magic1
= 0x5446534d;
137 typelib
->typelib_header
.magic2
= 0x00010002;
138 typelib
->typelib_header
.posguid
= -1;
139 typelib
->typelib_header
.lcid
= 0x0409;
140 typelib
->typelib_header
.lcid2
= 0x0;
141 typelib
->typelib_header
.varflags
= 0x40;
142 typelib
->typelib_header
.version
= 0;
143 typelib
->typelib_header
.flags
= 0;
144 typelib
->typelib_header
.nrtypeinfos
= 0;
145 typelib
->typelib_header
.helpstring
= -1;
146 typelib
->typelib_header
.helpstringcontext
= 0;
147 typelib
->typelib_header
.helpcontext
= 0;
148 typelib
->typelib_header
.nametablecount
= 0;
149 typelib
->typelib_header
.nametablechars
= 0;
150 typelib
->typelib_header
.NameOffset
= -1;
151 typelib
->typelib_header
.helpfile
= -1;
152 typelib
->typelib_header
.CustomDataOffset
= -1;
153 typelib
->typelib_header
.res44
= 0x20;
154 typelib
->typelib_header
.res48
= 0x80;
155 typelib
->typelib_header
.dispatchpos
= -1;
156 typelib
->typelib_header
.nimpinfos
= 0;
159 /****************************************************************************
162 * Initializes the segment directory of a new typelib.
164 static void ctl2_init_segdir(
165 msft_typelib_t
*typelib
) /* [I] The typelib to initialize. */
170 segdir
= &typelib
->typelib_segdir
[MSFT_SEG_TYPEINFO
];
172 for (i
= 0; i
< MSFT_SEG_MAX
; i
++) {
173 segdir
[i
].offset
= -1;
174 segdir
[i
].length
= 0;
175 segdir
[i
].res08
= -1;
176 segdir
[i
].res0c
= 0x0f;
180 /****************************************************************************
183 * Generates a hash key from a GUID.
187 * The hash key for the GUID.
189 static int ctl2_hash_guid(const struct uuid
*guid
)
195 for (i
= 0; i
< 8; i
++) {
196 hash
^= ((const short *)guid
)[i
];
202 /****************************************************************************
205 * Locates a guid in a type library.
209 * The offset into the GUID segment of the guid, or -1 if not found.
211 static int ctl2_find_guid(
212 msft_typelib_t
*typelib
, /* [I] The typelib to operate against. */
213 int hash_key
, /* [I] The hash key for the guid. */
214 const struct uuid
*guid
) /* [I] The guid to find. */
217 MSFT_GuidEntry
*guidentry
;
219 offset
= typelib
->typelib_guidhash_segment
[hash_key
];
220 while (offset
!= -1) {
221 guidentry
= (MSFT_GuidEntry
*)&typelib
->typelib_segment_data
[MSFT_SEG_GUID
][offset
];
223 if (!memcmp(guidentry
, guid
, sizeof(*guid
))) return offset
;
225 offset
= guidentry
->next_hash
;
231 /****************************************************************************
234 * Locates a name in a type library.
238 * The offset into the NAME segment of the name, or -1 if not found.
242 * The name must be encoded as with ctl2_encode_name().
244 static int ctl2_find_name(
245 msft_typelib_t
*typelib
, /* [I] The typelib to operate against. */
246 char *name
) /* [I] The encoded name to find. */
251 offset
= typelib
->typelib_namehash_segment
[name
[2] & 0x7f];
252 while (offset
!= -1) {
253 namestruct
= (int *)&typelib
->typelib_segment_data
[MSFT_SEG_NAME
][offset
];
255 if (!((namestruct
[2] ^ *((int *)name
)) & 0xffff00ff)) {
256 /* hash codes and lengths match, final test */
257 if (!strncasecmp(name
+4, (void *)(namestruct
+3), name
[0])) break;
260 /* move to next item in hash bucket */
261 offset
= namestruct
[1];
267 /****************************************************************************
270 * Encodes a name string to a form suitable for storing into a type library
271 * or comparing to a name stored in a type library.
275 * The length of the encoded name, including padding and length+hash fields.
279 * Will throw an exception if name or result are NULL. Is not multithread
280 * safe in the slightest.
282 static int ctl2_encode_name(
283 msft_typelib_t
*typelib
, /* [I] The typelib to operate against (used for LCID only). */
284 const char *name
, /* [I] The name string to encode. */
285 char **result
) /* [O] A pointer to a pointer to receive the encoded name. */
287 char *converted_name
;
292 length
= strlen(name
);
293 size
= (length
+ 7) & ~3;
294 converted_name
= xmalloc(size
+ 1);
295 memcpy(converted_name
+ 4, name
, length
);
296 converted_name
[length
+ 4] = 0;
298 value
= lhash_val_of_name_sys(typelib
->typelib_header
.varflags
& 0x0f, typelib
->typelib_header
.lcid
, converted_name
+ 4);
300 converted_name
[0] = length
& 0xff;
301 converted_name
[1] = length
>> 8;
302 converted_name
[2] = value
;
303 converted_name
[3] = value
>> 8;
305 for (offset
= (4 - length
) & 3; offset
; offset
--) converted_name
[length
+ offset
+ 3] = 0x57;
307 *result
= converted_name
;
312 /****************************************************************************
315 * Encodes a string to a form suitable for storing into a type library or
316 * comparing to a string stored in a type library.
320 * The length of the encoded string, including padding and length fields.
324 * Will throw an exception if string or result are NULL. Is not multithread
325 * safe in the slightest.
327 static int ctl2_encode_string(
328 const char *string
, /* [I] The string to encode. */
329 char **result
) /* [O] A pointer to a pointer to receive the encoded string. */
331 char *converted_string
;
335 length
= strlen(string
);
336 size
= (length
+ 5) & ~3;
337 if (length
< 3) size
+= 4;
338 converted_string
= xmalloc(size
);
339 memcpy(converted_string
+ 2, string
, length
);
340 converted_string
[0] = length
& 0xff;
341 converted_string
[1] = (length
>> 8) & 0xff;
343 if(length
< 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
344 for(offset
= 0; offset
< 4; offset
++)
345 converted_string
[length
+ offset
+ 2] = 0x57;
348 for (offset
= (4 - (length
+ 2)) & 3; offset
; offset
--) converted_string
[length
+ offset
+ 1] = 0x57;
350 *result
= converted_string
;
354 /****************************************************************************
357 * Allocates memory from a segment in a type library.
361 * Success: The offset within the segment of the new data area.
365 * Does not (yet) handle the case where the allocated segment memory needs to grow.
367 static int ctl2_alloc_segment(
368 msft_typelib_t
*typelib
, /* [I] The type library in which to allocate. */
369 enum MSFT_segment_index segment
, /* [I] The segment in which to allocate. */
370 int size
, /* [I] The amount to allocate. */
371 int block_size
) /* [I] Initial allocation block size, or 0 for default. */
375 if(!typelib
->typelib_segment_data
[segment
]) {
376 if (!block_size
) block_size
= 0x2000;
378 typelib
->typelib_segment_block_length
[segment
] = block_size
;
379 typelib
->typelib_segment_data
[segment
] = xmalloc(block_size
);
380 if (!typelib
->typelib_segment_data
[segment
]) return -1;
381 memset(typelib
->typelib_segment_data
[segment
], 0x57, block_size
);
384 while ((typelib
->typelib_segdir
[segment
].length
+ size
) > typelib
->typelib_segment_block_length
[segment
]) {
385 unsigned char *block
;
387 block_size
= typelib
->typelib_segment_block_length
[segment
];
388 block
= xrealloc(typelib
->typelib_segment_data
[segment
], block_size
<< 1);
390 if (segment
== MSFT_SEG_TYPEINFO
) {
391 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
392 msft_typeinfo_t
*typeinfo
;
394 for (typeinfo
= typelib
->typeinfos
; typeinfo
; typeinfo
= typeinfo
->next_typeinfo
) {
395 typeinfo
->typeinfo
= (void *)&block
[((unsigned char *)typeinfo
->typeinfo
) - typelib
->typelib_segment_data
[segment
]];
399 memset(block
+ block_size
, 0x57, block_size
);
400 typelib
->typelib_segment_block_length
[segment
] = block_size
<< 1;
401 typelib
->typelib_segment_data
[segment
] = block
;
404 offset
= typelib
->typelib_segdir
[segment
].length
;
405 typelib
->typelib_segdir
[segment
].length
+= size
;
410 /****************************************************************************
411 * ctl2_alloc_typeinfo
413 * Allocates and initializes a typeinfo structure in a type library.
417 * Success: The offset of the new typeinfo.
418 * Failure: -1 (this is invariably an out of memory condition).
420 static int ctl2_alloc_typeinfo(
421 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
422 int nameoffset
) /* [I] The offset of the name for this typeinfo. */
425 MSFT_TypeInfoBase
*typeinfo
;
427 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEINFO
, sizeof(MSFT_TypeInfoBase
), 0);
429 typelib
->typelib_typeinfo_offsets
[typelib
->typelib_header
.nrtypeinfos
++] = offset
;
431 typeinfo
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_TYPEINFO
] + offset
);
433 typeinfo
->typekind
= (typelib
->typelib_header
.nrtypeinfos
- 1) << 16;
434 typeinfo
->memoffset
= -1; /* should be EOF if no elements */
439 typeinfo
->cElement
= 0;
444 typeinfo
->posguid
= -1;
446 typeinfo
->NameOffset
= nameoffset
;
447 typeinfo
->version
= 0;
448 typeinfo
->docstringoffs
= -1;
449 typeinfo
->helpstringcontext
= 0;
450 typeinfo
->helpcontext
= 0;
451 typeinfo
->oCustData
= -1;
452 typeinfo
->cbSizeVft
= 0;
453 typeinfo
->cImplTypes
= 0;
455 typeinfo
->datatype1
= -1;
456 typeinfo
->datatype2
= 0;
458 typeinfo
->res19
= -1;
463 /****************************************************************************
466 * Allocates and initializes a GUID structure in a type library. Also updates
467 * the GUID hash table as needed.
471 * Success: The offset of the new GUID.
473 static int ctl2_alloc_guid(
474 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
475 MSFT_GuidEntry
*guid
) /* [I] The GUID to store. */
478 MSFT_GuidEntry
*guid_space
;
481 chat("adding uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
482 guid
->guid
.Data1
, guid
->guid
.Data2
, guid
->guid
.Data3
,
483 guid
->guid
.Data4
[0], guid
->guid
.Data4
[1], guid
->guid
.Data4
[2], guid
->guid
.Data4
[3],
484 guid
->guid
.Data4
[4], guid
->guid
.Data4
[5], guid
->guid
.Data4
[6], guid
->guid
.Data4
[7]);
486 hash_key
= ctl2_hash_guid(&guid
->guid
);
488 offset
= ctl2_find_guid(typelib
, hash_key
, &guid
->guid
);
491 if (is_warning_enabled(2368))
492 warning("duplicate uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
493 guid
->guid
.Data1
, guid
->guid
.Data2
, guid
->guid
.Data3
,
494 guid
->guid
.Data4
[0], guid
->guid
.Data4
[1], guid
->guid
.Data4
[2], guid
->guid
.Data4
[3],
495 guid
->guid
.Data4
[4], guid
->guid
.Data4
[5], guid
->guid
.Data4
[6], guid
->guid
.Data4
[7]);
499 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_GUID
, sizeof(MSFT_GuidEntry
), 0);
501 guid_space
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_GUID
] + offset
);
504 guid_space
->next_hash
= typelib
->typelib_guidhash_segment
[hash_key
];
505 typelib
->typelib_guidhash_segment
[hash_key
] = offset
;
510 /****************************************************************************
513 * Allocates and initializes a name within a type library. Also updates the
514 * name hash table as needed.
518 * Success: The offset within the segment of the new name.
519 * Failure: -1 (this is invariably an out of memory condition).
521 static int ctl2_alloc_name(
522 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
523 const char *name
) /* [I] The name to store. */
527 MSFT_NameIntro
*name_space
;
530 length
= ctl2_encode_name(typelib
, name
, &encoded_name
);
532 offset
= ctl2_find_name(typelib
, encoded_name
);
539 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_NAME
, length
+ 8, 0);
541 name_space
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_NAME
] + offset
);
542 name_space
->hreftype
= -1;
543 name_space
->next_hash
= -1;
544 memcpy(&name_space
->namelen
, encoded_name
, length
);
546 if (typelib
->typelib_namehash_segment
[encoded_name
[2] & 0x7f] != -1)
547 name_space
->next_hash
= typelib
->typelib_namehash_segment
[encoded_name
[2] & 0x7f];
549 typelib
->typelib_namehash_segment
[encoded_name
[2] & 0x7f] = offset
;
551 typelib
->typelib_header
.nametablecount
+= 1;
552 typelib
->typelib_header
.nametablechars
+= *encoded_name
;
558 /****************************************************************************
561 * Allocates and initializes a string in a type library.
565 * Success: The offset within the segment of the new string.
566 * Failure: -1 (this is invariably an out of memory condition).
568 static int ctl2_alloc_string(
569 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
570 const char *string
) /* [I] The string to store. */
574 unsigned char *string_space
;
575 char *encoded_string
;
577 length
= ctl2_encode_string(string
, &encoded_string
);
579 for (offset
= 0; offset
< typelib
->typelib_segdir
[MSFT_SEG_STRING
].length
;
580 offset
+= (((typelib
->typelib_segment_data
[MSFT_SEG_STRING
][offset
+ 1] << 8) |
581 typelib
->typelib_segment_data
[MSFT_SEG_STRING
][offset
+ 0]) + 5) & ~3) {
582 if (!memcmp(encoded_string
, typelib
->typelib_segment_data
[MSFT_SEG_STRING
] + offset
, length
)) return offset
;
585 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_STRING
, length
, 0);
587 string_space
= typelib
->typelib_segment_data
[MSFT_SEG_STRING
] + offset
;
588 memcpy(string_space
, encoded_string
, length
);
589 free(encoded_string
);
594 /****************************************************************************
595 * alloc_msft_importinfo
597 * Allocates and initializes an import information structure in a type library.
601 * Success: The offset of the new importinfo.
602 * Failure: -1 (this is invariably an out of memory condition).
604 static int alloc_msft_importinfo(
605 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
606 MSFT_ImpInfo
*impinfo
) /* [I] The import information to store. */
609 MSFT_ImpInfo
*impinfo_space
;
612 offset
< typelib
->typelib_segdir
[MSFT_SEG_IMPORTINFO
].length
;
613 offset
+= sizeof(MSFT_ImpInfo
)) {
614 if (!memcmp(&(typelib
->typelib_segment_data
[MSFT_SEG_IMPORTINFO
][offset
]),
615 impinfo
, sizeof(MSFT_ImpInfo
))) {
620 impinfo
->flags
|= typelib
->typelib_header
.nimpinfos
++;
622 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_IMPORTINFO
, sizeof(MSFT_ImpInfo
), 0);
624 impinfo_space
= (void *)(typelib
->typelib_segment_data
[MSFT_SEG_IMPORTINFO
] + offset
);
625 *impinfo_space
= *impinfo
;
630 /****************************************************************************
633 * Allocates and initializes an import file definition in a type library.
637 * Success: The offset of the new importinfo.
638 * Failure: -1 (this is invariably an out of memory condition).
640 static int alloc_importfile(
641 msft_typelib_t
*typelib
, /* [I] The type library to allocate in. */
642 int guidoffset
, /* [I] The offset to the GUID for the imported library. */
643 int major_version
, /* [I] The major version number of the imported library. */
644 int minor_version
, /* [I] The minor version number of the imported library. */
645 const char *filename
) /* [I] The filename of the imported library. */
649 MSFT_ImpFile
*importfile
;
650 char *encoded_string
;
652 length
= ctl2_encode_string(filename
, &encoded_string
);
654 encoded_string
[0] <<= 2;
655 encoded_string
[0] |= 1;
657 for (offset
= 0; offset
< typelib
->typelib_segdir
[MSFT_SEG_IMPORTFILES
].length
;
658 offset
+= (((typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
][offset
+ 0xd] << 8) |
659 typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
][offset
+ 0xc]) >> 2) + 0xc) {
660 if (!memcmp(encoded_string
, typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
] + offset
+ 0xc, length
)) return offset
;
663 offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_IMPORTFILES
, length
+ 0xc, 0);
665 importfile
= (MSFT_ImpFile
*)&typelib
->typelib_segment_data
[MSFT_SEG_IMPORTFILES
][offset
];
666 importfile
->guid
= guidoffset
;
667 importfile
->lcid
= typelib
->typelib_header
.lcid2
;
668 importfile
->version
= major_version
| (minor_version
<< 16);
669 memcpy(&importfile
->filename
, encoded_string
, length
);
670 free(encoded_string
);
675 static void alloc_importinfo(msft_typelib_t
*typelib
, importinfo_t
*importinfo
)
677 importlib_t
*importlib
= importinfo
->importlib
;
679 chat("alloc_importinfo: %s\n", importinfo
->name
);
681 if(!importlib
->allocated
) {
685 chat("allocating importlib %s\n", importlib
->name
);
687 importlib
->allocated
= -1;
689 memcpy(&guid
.guid
, &importlib
->guid
, sizeof(guid
.guid
));
692 guid_idx
= ctl2_alloc_guid(typelib
, &guid
);
694 importlib
->offset
= alloc_importfile(typelib
, guid_idx
, importlib
->version
& 0xffff,
695 importlib
->version
>> 16, importlib
->name
);
698 if(importinfo
->offset
== -1 || !(importinfo
->flags
& MSFT_IMPINFO_OFFSET_IS_GUID
)) {
699 MSFT_ImpInfo impinfo
;
701 impinfo
.flags
= importinfo
->flags
;
702 impinfo
.oImpFile
= importlib
->offset
;
704 if(importinfo
->flags
& MSFT_IMPINFO_OFFSET_IS_GUID
) {
708 memcpy(&guid
.guid
, &importinfo
->guid
, sizeof(guid
.guid
));
710 impinfo
.oGuid
= ctl2_alloc_guid(typelib
, &guid
);
712 importinfo
->offset
= alloc_msft_importinfo(typelib
, &impinfo
);
714 typelib
->typelib_segment_data
[MSFT_SEG_GUID
][impinfo
.oGuid
+sizeof(guid
.guid
)]
715 = importinfo
->offset
+1;
717 if(!strcmp(importinfo
->name
, "IDispatch"))
718 typelib
->typelib_header
.dispatchpos
= importinfo
->offset
+1;
720 impinfo
.oGuid
= importinfo
->id
;
721 importinfo
->offset
= alloc_msft_importinfo(typelib
, &impinfo
);
726 static importinfo_t
*find_importinfo(msft_typelib_t
*typelib
, const char *name
)
728 importlib_t
*importlib
;
731 chat("search importlib %s\n", name
);
736 LIST_FOR_EACH_ENTRY( importlib
, &typelib
->typelib
->importlibs
, importlib_t
, entry
)
738 for(i
=0; i
< importlib
->ntypeinfos
; i
++) {
739 if(!strcmp(name
, importlib
->importinfos
[i
].name
)) {
740 chat("Found %s in importlib.\n", name
);
741 return importlib
->importinfos
+i
;
749 static void add_structure_typeinfo(msft_typelib_t
*typelib
, type_t
*structure
);
750 static void add_interface_typeinfo(msft_typelib_t
*typelib
, type_t
*interface
);
751 static void add_enum_typeinfo(msft_typelib_t
*typelib
, type_t
*enumeration
);
752 static void add_union_typeinfo(msft_typelib_t
*typelib
, type_t
*tunion
);
753 static void add_coclass_typeinfo(msft_typelib_t
*typelib
, type_t
*cls
);
754 static void add_dispinterface_typeinfo(msft_typelib_t
*typelib
, type_t
*dispinterface
);
755 static void add_typedef_typeinfo(msft_typelib_t
*typelib
, type_t
*dispinterface
);
758 /****************************************************************************
761 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
762 * segments as needed.
769 static int encode_type(
770 msft_typelib_t
*typelib
, /* [I] The type library in which to encode the TYPEDESC. */
771 int vt
, /* [I] vt to encode */
772 type_t
*type
, /* [I] type */
773 int *encoded_type
, /* [O] The encoded type description. */
774 int *decoded_size
) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
783 chat("encode_type vt %d type %p\n", vt
, type
);
785 default_type
= 0x80000000 | (vt
<< 16) | vt
;
786 if (!decoded_size
) decoded_size
= &scratch
;
793 *encoded_type
= default_type
;
797 *encoded_type
= 0x80000000 | (VT_I4
<< 16) | VT_INT
;
801 *encoded_type
= 0x80000000 | (VT_UI4
<< 16) | VT_UINT
;
807 *encoded_type
= default_type
;
815 *encoded_type
= default_type
;
821 *encoded_type
= default_type
;
826 *encoded_type
= default_type
;
830 *encoded_type
= default_type
;
834 *encoded_type
= 0x80000000 | (VT_EMPTY
<< 16) | vt
;
840 *encoded_type
= default_type
;
844 *encoded_type
= default_type
;
849 *encoded_type
= 0xfffe0000 | vt
;
855 for(next_vt
= 0; is_ptr(type
); type
= type_pointer_get_ref_type(type
)) {
856 next_vt
= get_type_vt(type_pointer_get_ref_type(type
));
860 /* if no type found then it must be void */
864 encode_type(typelib
, next_vt
, type_pointer_get_ref_type(type
),
865 &target_type
, &child_size
);
866 /* these types already have an implicit pointer, so we don't need to
868 if(next_vt
== VT_DISPATCH
|| next_vt
== VT_UNKNOWN
) {
869 chat("encode_type: skipping ptr\n");
870 *encoded_type
= target_type
;
871 *decoded_size
= child_size
;
875 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
876 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
877 if (((typedata
[0] & 0xffff) == VT_PTR
) && (typedata
[1] == target_type
)) break;
880 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
883 if (target_type
& 0x80000000) {
884 mix_field
= ((target_type
>> 16) & 0x3fff) | VT_BYREF
;
886 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][target_type
];
887 mix_field
= ((typedata
[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
890 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
891 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
893 typedata
[0] = (mix_field
<< 16) | VT_PTR
;
894 typedata
[1] = target_type
;
897 *encoded_type
= typeoffset
;
899 *decoded_size
= 8 /*sizeof(TYPEDESC)*/ + child_size
;
905 type_t
*element_type
= type_alias_get_aliasee_type(type_array_get_element_type(type
));
906 int next_vt
= get_type_vt(element_type
);
908 encode_type(typelib
, next_vt
, type_alias_get_aliasee_type(type_array_get_element_type(type
)),
909 &target_type
, &child_size
);
911 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
912 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
913 if (((typedata
[0] & 0xffff) == VT_SAFEARRAY
) && (typedata
[1] == target_type
)) break;
916 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
919 if (target_type
& 0x80000000) {
920 mix_field
= ((target_type
>> 16) & VT_TYPEMASK
) | VT_ARRAY
;
922 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][target_type
];
923 mix_field
= ((typedata
[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
926 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
927 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
929 typedata
[0] = (mix_field
<< 16) | VT_SAFEARRAY
;
930 typedata
[1] = target_type
;
933 *encoded_type
= typeoffset
;
935 *decoded_size
= 8 /*sizeof(TYPEDESC)*/ + child_size
;
941 importinfo_t
*importinfo
;
944 if (type
->typelib_idx
> -1)
946 chat("encode_type: VT_USERDEFINED - found already defined type %s at %d\n",
947 type
->name
, type
->typelib_idx
);
948 typeinfo_offset
= typelib
->typelib_typeinfo_offsets
[type
->typelib_idx
];
950 else if ((importinfo
= find_importinfo(typelib
, type
->name
)))
952 chat("encode_type: VT_USERDEFINED - found imported type %s in %s\n",
953 type
->name
, importinfo
->importlib
->name
);
954 alloc_importinfo(typelib
, importinfo
);
955 typeinfo_offset
= importinfo
->offset
| 0x1;
959 /* Typedefs without the [public] attribute aren't included in the
960 * typelib, unless the aliasee is an anonymous UDT or the typedef
961 * is wire-marshalled. In the latter case the wire-marshal type,
962 * which may be a non-public alias, is used instead. */
963 while (type_is_alias(type
))
965 if (is_attr(type
->attrs
, ATTR_WIREMARSHAL
))
967 type
= get_attrp(type
->attrs
, ATTR_WIREMARSHAL
);
970 else if (!is_attr(type
->attrs
, ATTR_PUBLIC
))
971 type
= type_alias_get_aliasee_type(type
);
976 chat("encode_type: VT_USERDEFINED - adding new type %s, real type %d\n",
977 type
->name
, type_get_type(type
));
979 switch (type_get_type_detect_alias(type
))
982 case TYPE_ENCAPSULATED_UNION
:
983 add_structure_typeinfo(typelib
, type
);
986 add_interface_typeinfo(typelib
, type
);
989 add_enum_typeinfo(typelib
, type
);
992 add_union_typeinfo(typelib
, type
);
995 add_coclass_typeinfo(typelib
, type
);
998 add_typedef_typeinfo(typelib
, type
);
1001 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
1002 type_get_type(type
));
1005 typeinfo_offset
= typelib
->typelib_typeinfo_offsets
[type
->typelib_idx
];
1007 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
1008 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1009 if ((typedata
[0] == ((0x7fff << 16) | VT_USERDEFINED
)) && (typedata
[1] == typeinfo_offset
)) break;
1012 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
1013 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
1014 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1016 typedata
[0] = (0x7fff << 16) | VT_USERDEFINED
;
1017 typedata
[1] = typeinfo_offset
;
1020 *encoded_type
= typeoffset
;
1025 error("encode_type: unrecognized type %d.\n", vt
);
1026 *encoded_type
= default_type
;
1033 static void dump_type(type_t
*t
)
1035 chat("dump_type: %p name %s type %d attrs %p\n", t
, t
->name
, type_get_type(t
), t
->attrs
);
1038 static int encode_var(
1039 msft_typelib_t
*typelib
, /* [I] The type library in which to encode the TYPEDESC. */
1040 type_t
*type
, /* [I] The type description to encode. */
1041 var_t
*var
, /* [I] The var to encode. */
1042 int *encoded_type
, /* [O] The encoded type description. */
1043 int *decoded_size
) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1052 if (!decoded_size
) decoded_size
= &scratch
;
1055 chat("encode_var: var %p type %p type->name %s\n",
1056 var
, type
, type
->name
? type
->name
: "NULL");
1058 if (is_array(type
) && !type_array_is_decl_as_ptr(type
)) {
1059 int num_dims
, elements
= 1, arrayoffset
;
1065 is_array(atype
) && !type_array_is_decl_as_ptr(atype
);
1066 atype
= type_array_get_element_type(atype
))
1069 chat("array with %d dimensions\n", num_dims
);
1070 encode_var(typelib
, atype
, var
, &target_type
, NULL
);
1071 arrayoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_ARRAYDESC
, (2 + 2 * num_dims
) * sizeof(int), 0);
1072 arraydata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_ARRAYDESC
][arrayoffset
];
1074 arraydata
[0] = target_type
;
1075 arraydata
[1] = num_dims
;
1076 arraydata
[1] |= ((num_dims
* 2 * sizeof(int)) << 16);
1080 is_array(atype
) && !type_array_is_decl_as_ptr(atype
);
1081 atype
= type_array_get_element_type(atype
))
1083 arraydata
[0] = type_array_get_dim(atype
);
1086 elements
*= type_array_get_dim(atype
);
1089 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
1090 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1092 typedata
[0] = (0x7ffe << 16) | VT_CARRAY
;
1093 typedata
[1] = arrayoffset
;
1095 *encoded_type
= typeoffset
;
1096 *decoded_size
= 20 /*sizeof(ARRAYDESC)*/ + (num_dims
- 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1100 vt
= get_type_vt(type
);
1102 type_t
*ref
= is_ptr(type
) ?
1103 type_pointer_get_ref_type(type
) : type_array_get_element_type(type
);
1104 int skip_ptr
= encode_var(typelib
, ref
, var
, &target_type
, &child_size
);
1107 chat("encode_var: skipping ptr\n");
1108 *encoded_type
= target_type
;
1109 *decoded_size
= child_size
;
1113 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
1114 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1115 if (((typedata
[0] & 0xffff) == VT_PTR
) && (typedata
[1] == target_type
)) break;
1118 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
1121 if (target_type
& 0x80000000) {
1122 mix_field
= ((target_type
>> 16) & 0x3fff) | VT_BYREF
;
1123 } else if (get_type_vt(ref
) == VT_SAFEARRAY
) {
1124 type_t
*element_type
= type_alias_get_aliasee_type(type_array_get_element_type(ref
));
1125 mix_field
= get_type_vt(element_type
) | VT_ARRAY
| VT_BYREF
;
1127 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][target_type
];
1128 mix_field
= ((typedata
[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1131 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
1132 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1134 typedata
[0] = (mix_field
<< 16) | VT_PTR
;
1135 typedata
[1] = target_type
;
1138 *encoded_type
= typeoffset
;
1140 *decoded_size
= 8 /*sizeof(TYPEDESC)*/ + child_size
;
1146 encode_type(typelib
, vt
, type
, encoded_type
, decoded_size
);
1147 /* these types already have an implicit pointer, so we don't need to
1149 if(vt
== VT_DISPATCH
|| vt
== VT_UNKNOWN
) return 2;
1153 static unsigned int get_ulong_val(unsigned int val
, int vt
)
1159 return val
& 0xffff;
1168 static void write_int_value(msft_typelib_t
*typelib
, int *out
, int vt
, int value
)
1170 const unsigned int lv
= get_ulong_val(value
, vt
);
1171 if ((lv
& 0x3ffffff) == lv
) {
1176 int offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATA
, 8, 0);
1177 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
]) = vt
;
1178 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+2], &value
, 4);
1179 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+6]) = 0x5757;
1184 static void write_string_value(msft_typelib_t
*typelib
, int *out
, const char *value
)
1186 int len
= strlen(value
), seg_len
= (len
+ 6 + 3) & ~0x3;
1187 int offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATA
, seg_len
, 0);
1188 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
]) = VT_BSTR
;
1189 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+2], &len
, sizeof(len
));
1190 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+6], value
, len
);
1192 while(len
< seg_len
) {
1193 *((char *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+len
]) = 0x57;
1199 static void write_default_value(msft_typelib_t
*typelib
, type_t
*type
, expr_t
*expr
, int *out
)
1203 if (expr
->type
== EXPR_STRLIT
|| expr
->type
== EXPR_WSTRLIT
) {
1204 vt
= get_type_vt(type
);
1205 if (vt
!= VT_BSTR
&& vt
!= VT_VARIANT
)
1206 error("string default value applied to non-string type\n");
1207 chat("default value '%s'\n", expr
->u
.sval
);
1208 write_string_value(typelib
, out
, expr
->u
.sval
);
1212 if (type_get_type(type
) == TYPE_ENUM
) {
1214 } else if (is_ptr(type
)) {
1215 vt
= get_type_vt(type_pointer_get_ref_type(type
));
1216 if (vt
== VT_USERDEFINED
)
1219 warning("non-null pointer default value\n");
1221 vt
= get_type_vt(type
);
1236 switch (expr
->type
) {
1244 warning("can't write default VT_VARIANT value for expression type %d.\n", expr
->type
);
1250 warning("can't write value of type %d yet\n", vt
);
1255 write_int_value(typelib
, out
, vt
, expr
->cval
);
1258 static void set_custdata(msft_typelib_t
*typelib
, const struct uuid
*guid
,
1259 int vt
, const void *value
, int *offset
)
1267 hash_key
= ctl2_hash_guid(guid
);
1268 guidoffset
= ctl2_find_guid(typelib
, hash_key
, guid
);
1269 if(guidoffset
== -1) {
1270 /* add GUID that was not already present */
1271 MSFT_GuidEntry guidentry
;
1272 guidentry
.guid
= *guid
;
1274 guidentry
.hreftype
= -1;
1275 guidentry
.next_hash
= -1;
1277 guidoffset
= ctl2_alloc_guid(typelib
, &guidentry
);
1281 /* TODO midl appears to share a single reference if the same string is used as custdata in multiple places */
1282 write_string_value(typelib
, &data_out
, value
);
1284 write_int_value(typelib
, &data_out
, vt
, *(int*)value
);
1286 custoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATAGUID
, 12, 0);
1288 custdata
= (int *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATAGUID
][custoffset
];
1289 custdata
[0] = guidoffset
;
1290 custdata
[1] = data_out
;
1291 custdata
[2] = *offset
;
1292 *offset
= custoffset
;
1295 static void set_custdata_attr(msft_typelib_t
*typelib
, attr_custdata_t
*custdata
, int *offset
)
1297 switch(custdata
->pval
->type
) {
1300 set_custdata(typelib
, &custdata
->id
, VT_BSTR
, custdata
->pval
->u
.sval
, offset
);
1304 set_custdata(typelib
, &custdata
->id
, VT_I4
, &custdata
->pval
->u
.lval
, offset
);
1307 error("custom() attribute with unknown type\n");
1312 static int add_func_desc(msft_typeinfo_t
* typeinfo
, var_t
*func
, int index
)
1314 int offset
, name_offset
;
1315 int *typedata
, typedata_size
;
1316 int i
, id
, next_idx
;
1317 int decoded_size
, extra_attr
= 0;
1318 int num_params
= 0, num_optional
= 0, num_defaults
= 0;
1319 int has_arg_custdata
= 0;
1321 unsigned char *namedata
;
1323 unsigned int funcflags
= 0, callconv
= 4 /* CC_STDCALL */;
1324 unsigned int funckind
, invokekind
= 1 /* INVOKE_FUNC */;
1325 int help_context
= 0, help_string_context
= 0, help_string_offset
= -1;
1326 int func_custdata_offset
= -1;
1327 int entry
= -1, entry_is_ord
= 0;
1328 int lcid_retval_count
= 0;
1330 chat("add_func_desc(%p,%d)\n", typeinfo
, index
);
1332 id
= ((0x6000 | (typeinfo
->typeinfo
->datatype2
& 0xffff)) << 16) | index
;
1334 switch(typeinfo
->typekind
) {
1335 case TKIND_DISPATCH
:
1336 funckind
= 0x4; /* FUNC_DISPATCH */
1339 funckind
= 0x3; /* FUNC_STATIC */
1342 funckind
= 0x1; /* FUNC_PUREVIRTUAL */
1346 if (is_local( func
->attrs
)) {
1347 chat("add_func_desc: skipping local function\n");
1351 if (type_function_get_args(func
->declspec
.type
))
1352 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), var_t
, entry
)
1355 if (arg
->attrs
) LIST_FOR_EACH_ENTRY( attr
, arg
->attrs
, const attr_t
, entry
) {
1356 if(attr
->type
== ATTR_DEFAULTVALUE
)
1358 else if(attr
->type
== ATTR_OPTIONAL
)
1360 else if(attr
->type
== ATTR_CUSTOM
)
1361 has_arg_custdata
= 1;
1365 chat("add_func_desc: num of params %d\n", num_params
);
1367 name_offset
= ctl2_alloc_name(typeinfo
->typelib
, func
->name
);
1369 if (func
->attrs
) LIST_FOR_EACH_ENTRY( attr
, func
->attrs
, const attr_t
, entry
) {
1370 expr_t
*expr
= attr
->u
.pval
;
1371 switch(attr
->type
) {
1373 funcflags
|= 0x4; /* FUNCFLAG_FBINDABLE */
1376 set_custdata_attr(typeinfo
->typelib
, attr
->u
.pval
, &func_custdata_offset
);
1378 case ATTR_DEFAULTBIND
:
1379 funcflags
|= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1381 case ATTR_DEFAULTCOLLELEM
:
1382 funcflags
|= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1384 case ATTR_DISPLAYBIND
:
1385 funcflags
|= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1388 extra_attr
= max(extra_attr
, 3);
1389 if (expr
->type
== EXPR_STRLIT
|| expr
->type
== EXPR_WSTRLIT
)
1390 entry
= ctl2_alloc_string(typeinfo
->typelib
, attr
->u
.pval
);
1396 case ATTR_HELPCONTEXT
:
1397 extra_attr
= max(extra_attr
, 1);
1398 help_context
= expr
->u
.lval
;
1400 case ATTR_HELPSTRING
:
1401 extra_attr
= max(extra_attr
, 2);
1402 help_string_offset
= ctl2_alloc_string(typeinfo
->typelib
, attr
->u
.pval
);
1404 case ATTR_HELPSTRINGCONTEXT
:
1405 extra_attr
= max(extra_attr
, 6);
1406 help_string_context
= expr
->u
.lval
;
1409 funcflags
|= 0x40; /* FUNCFLAG_FHIDDEN */
1414 case ATTR_IMMEDIATEBIND
:
1415 funcflags
|= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1417 case ATTR_NONBROWSABLE
:
1418 funcflags
|= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1425 invokekind
= 0x2; /* INVOKE_PROPERTYGET */
1428 invokekind
= 0x4; /* INVOKE_PROPERTYPUT */
1430 case ATTR_PROPPUTREF
:
1431 invokekind
= 0x8; /* INVOKE_PROPERTYPUTREF */
1433 /* FIXME: FUNCFLAG_FREPLACEABLE */
1434 case ATTR_REQUESTEDIT
:
1435 funcflags
|= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1437 case ATTR_RESTRICTED
:
1438 funcflags
|= 0x1; /* FUNCFLAG_FRESTRICTED */
1441 funcflags
|= 0x2; /* FUNCFLAG_FSOURCE */
1443 case ATTR_UIDEFAULT
:
1444 funcflags
|= 0x200; /* FUNCFLAG_FUIDEFAULT */
1446 case ATTR_USESGETLASTERROR
:
1447 funcflags
|= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1450 if (num_optional
|| num_defaults
)
1451 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1460 if(has_arg_custdata
|| func_custdata_offset
!= -1) {
1461 extra_attr
= max(extra_attr
, 7 + num_params
);
1464 /* allocate type data space for us */
1465 typedata_size
= 0x18 + extra_attr
* sizeof(int) + (num_params
* (num_defaults
? 16 : 12));
1467 if (!typeinfo
->func_data
) {
1468 typeinfo
->func_data
= xmalloc(0x100);
1469 typeinfo
->func_data_allocated
= 0x100;
1470 typeinfo
->func_data
[0] = 0;
1473 if(typeinfo
->func_data
[0] + typedata_size
+ sizeof(int) > typeinfo
->func_data_allocated
) {
1474 typeinfo
->func_data_allocated
= max(typeinfo
->func_data_allocated
* 2,
1475 typeinfo
->func_data
[0] + typedata_size
+ sizeof(int));
1476 typeinfo
->func_data
= xrealloc(typeinfo
->func_data
, typeinfo
->func_data_allocated
);
1479 offset
= typeinfo
->func_data
[0];
1480 typeinfo
->func_data
[0] += typedata_size
;
1481 typedata
= typeinfo
->func_data
+ (offset
>> 2) + 1;
1484 /* find func with the same name - if it exists use its id */
1485 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
& 0xffff); i
++) {
1486 if(name_offset
== typeinfo
->func_names
[i
]) {
1487 id
= typeinfo
->func_indices
[i
];
1492 /* find the first func with the same id and link via the hiword of typedata[4] */
1494 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
& 0xffff); i
++) {
1495 if(id
== typeinfo
->func_indices
[i
]) {
1496 next_idx
= typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] >> 16;
1497 typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] &= 0xffff;
1498 typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] |= (index
<< 16);
1503 /* fill out the basic type information */
1504 typedata
[0] = typedata_size
| (index
<< 16);
1505 encode_var(typeinfo
->typelib
, type_function_get_rettype(func
->declspec
.type
), func
,
1506 &typedata
[1], &decoded_size
);
1507 typedata
[2] = funcflags
;
1508 typedata
[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size
) << 16) | typeinfo
->typeinfo
->cbSizeVft
;
1509 typedata
[4] = (next_idx
<< 16) | (callconv
<< 8) | (invokekind
<< 3) | funckind
;
1510 if(has_arg_custdata
|| func_custdata_offset
!= -1) typedata
[4] |= 0x0080;
1511 if(num_defaults
) typedata
[4] |= 0x1000;
1512 if(entry_is_ord
) typedata
[4] |= 0x2000;
1513 typedata
[5] = (num_optional
<< 16) | num_params
;
1515 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1516 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1517 typedata
[3] += (16 /*sizeof(ELEMDESC)*/ * num_params
) << 16;
1518 typedata
[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults
) << 16;
1520 switch(extra_attr
) {
1522 if(extra_attr
> 7 + num_params
) warning("unknown number of optional attrs\n");
1523 /* typedata[13..+num_params] = arg_custdata_offset handled in below loop */
1524 case 7: typedata
[12] = func_custdata_offset
;
1525 case 6: typedata
[11] = help_string_context
;
1526 case 5: typedata
[10] = -1;
1527 case 4: typedata
[9] = -1;
1528 case 3: typedata
[8] = entry
;
1529 case 2: typedata
[7] = help_string_offset
;
1530 case 1: typedata
[6] = help_context
;
1535 if (type_function_get_args(func
->declspec
.type
))
1538 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), var_t
, entry
)
1541 int *paramdata
= typedata
+ 6 + extra_attr
+ (num_defaults
? num_params
: 0) + i
* 3;
1542 int *defaultdata
= num_defaults
? typedata
+ 6 + extra_attr
+ i
: NULL
;
1543 int arg_custdata_offset
= -1;
1545 if(defaultdata
) *defaultdata
= -1;
1547 encode_var(typeinfo
->typelib
, arg
->declspec
.type
, arg
, paramdata
, &decoded_size
);
1548 if (arg
->attrs
) LIST_FOR_EACH_ENTRY( attr
, arg
->attrs
, const attr_t
, entry
) {
1549 switch(attr
->type
) {
1551 set_custdata_attr(typeinfo
->typelib
, attr
->u
.pval
, &arg_custdata_offset
);
1553 case ATTR_DEFAULTVALUE
:
1555 paramflags
|= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1556 write_default_value(typeinfo
->typelib
, arg
->declspec
.type
, (expr_t
*)attr
->u
.pval
, defaultdata
);
1560 paramflags
|= 0x01; /* PARAMFLAG_FIN */
1563 paramflags
|= 0x10; /* PARAMFLAG_FOPT */
1566 paramflags
|= 0x02; /* PARAMFLAG_FOUT */
1568 case ATTR_PARAMLCID
:
1569 paramflags
|= 0x04; /* PARAMFLAG_LCID */
1570 lcid_retval_count
++;
1573 paramflags
|= 0x08; /* PARAMFLAG_FRETVAL */
1574 lcid_retval_count
++;
1577 chat("unhandled param attr %d\n", attr
->type
);
1580 if(extra_attr
> 7 + i
) {
1581 typedata
[13+i
] = arg_custdata_offset
;
1585 paramdata
[2] = paramflags
;
1586 typedata
[3] += decoded_size
<< 16;
1592 if(lcid_retval_count
== 1)
1593 typedata
[4] |= 0x4000;
1594 else if(lcid_retval_count
== 2)
1595 typedata
[4] |= 0x8000;
1597 if(typeinfo
->funcs_allocated
== 0) {
1598 typeinfo
->funcs_allocated
= 10;
1599 typeinfo
->func_indices
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1600 typeinfo
->func_names
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1601 typeinfo
->func_offsets
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1603 if(typeinfo
->funcs_allocated
== (typeinfo
->typeinfo
->cElement
& 0xffff)) {
1604 typeinfo
->funcs_allocated
*= 2;
1605 typeinfo
->func_indices
= xrealloc(typeinfo
->func_indices
, typeinfo
->funcs_allocated
* sizeof(int));
1606 typeinfo
->func_names
= xrealloc(typeinfo
->func_names
, typeinfo
->funcs_allocated
* sizeof(int));
1607 typeinfo
->func_offsets
= xrealloc(typeinfo
->func_offsets
, typeinfo
->funcs_allocated
* sizeof(int));
1610 /* update the index data */
1611 typeinfo
->func_indices
[typeinfo
->typeinfo
->cElement
& 0xffff] = id
;
1612 typeinfo
->func_offsets
[typeinfo
->typeinfo
->cElement
& 0xffff] = offset
;
1613 typeinfo
->func_names
[typeinfo
->typeinfo
->cElement
& 0xffff] = name_offset
;
1616 if (!typeinfo
->typeinfo
->res2
) typeinfo
->typeinfo
->res2
= 0x20;
1617 typeinfo
->typeinfo
->res2
<<= 1;
1619 if (index
< 2) typeinfo
->typeinfo
->res2
+= num_params
<< 4;
1621 if (typeinfo
->typeinfo
->res3
== -1) typeinfo
->typeinfo
->res3
= 0;
1622 typeinfo
->typeinfo
->res3
+= 0x38 + num_params
* 0x10;
1623 if(num_defaults
) typeinfo
->typeinfo
->res3
+= num_params
* 0x4;
1625 /* adjust size of VTBL */
1626 if(funckind
!= 0x3 /* FUNC_STATIC */)
1627 typeinfo
->typeinfo
->cbSizeVft
+= pointer_size
;
1629 /* Increment the number of function elements */
1630 typeinfo
->typeinfo
->cElement
+= 1;
1632 namedata
= typeinfo
->typelib
->typelib_segment_data
[MSFT_SEG_NAME
] + name_offset
;
1633 if (*((int *)namedata
) == -1) {
1634 *((int *)namedata
) = typeinfo
->typelib
->typelib_typeinfo_offsets
[typeinfo
->typeinfo
->typekind
>> 16];
1635 if(typeinfo
->typekind
== TKIND_MODULE
)
1636 namedata
[9] |= 0x10;
1638 namedata
[9] &= ~0x10;
1640 if(typeinfo
->typekind
== TKIND_MODULE
)
1641 namedata
[9] |= 0x20;
1643 if (type_function_get_args(func
->declspec
.type
))
1646 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), var_t
, entry
)
1648 /* don't give the last arg of a [propput*] func a name */
1649 if(i
!= num_params
- 1 || (invokekind
!= 0x4 /* INVOKE_PROPERTYPUT */ && invokekind
!= 0x8 /* INVOKE_PROPERTYPUTREF */))
1651 int *paramdata
= typedata
+ 6 + extra_attr
+ (num_defaults
? num_params
: 0) + i
* 3;
1652 offset
= ctl2_alloc_name(typeinfo
->typelib
, arg
->name
);
1653 paramdata
[1] = offset
;
1661 static void add_var_desc(msft_typeinfo_t
*typeinfo
, unsigned int index
, var_t
* var
)
1664 unsigned int typedata_size
;
1667 unsigned int var_datawidth
, var_alignment
= 0;
1668 int var_type_size
, var_kind
= 0 /* VAR_PERINSTANCE */;
1672 unsigned char *namedata
;
1673 int var_num
= (typeinfo
->typeinfo
->cElement
>> 16) & 0xffff;
1674 int var_custdata_offset
= -1;
1677 var
->name
= gen_name();
1679 chat("add_var_desc(%d, %s)\n", index
, var
->name
);
1681 id
= 0x40000000 + index
;
1683 if (var
->attrs
) LIST_FOR_EACH_ENTRY( attr
, var
->attrs
, const attr_t
, entry
) {
1684 expr_t
*expr
= attr
->u
.pval
;
1685 switch(attr
->type
) {
1687 varflags
|= 0x04; /* VARFLAG_FBINDABLE */
1690 extra_attr
= max(extra_attr
,4);
1691 set_custdata_attr(typeinfo
->typelib
, attr
->u
.pval
, &var_custdata_offset
);
1693 case ATTR_DEFAULTBIND
:
1694 varflags
|= 0x20; /* VARFLAG_FDEFAULTBIND */
1696 case ATTR_DEFAULTCOLLELEM
:
1697 varflags
|= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1699 case ATTR_DISPLAYBIND
:
1700 varflags
|= 0x10; /* VARFLAG_FDISPLAYBIND */
1703 varflags
|= 0x40; /* VARFLAG_FHIDDEN */
1708 case ATTR_IMMEDIATEBIND
:
1709 varflags
|= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1711 case ATTR_NONBROWSABLE
:
1712 varflags
|= 0x400; /* VARFLAG_FNONBROWSABLE */
1715 varflags
|= 0x01; /* VARFLAG_FREADONLY */
1717 /* FIXME: VARFLAG_FREPLACEABLE */
1718 case ATTR_REQUESTEDIT
:
1719 varflags
|= 0x08; /* VARFLAG_FREQUESTEDIT */
1721 case ATTR_RESTRICTED
:
1722 varflags
|= 0x80; /* VARFLAG_FRESTRICTED */
1725 varflags
|= 0x02; /* VARFLAG_FSOURCE */
1727 case ATTR_UIDEFAULT
:
1728 varflags
|= 0x0200; /* VARFLAG_FUIDEFAULT */
1735 /* allocate type data space for us */
1736 typedata_size
= 0x14 + extra_attr
* sizeof(int);
1738 if (!typeinfo
->var_data
) {
1739 typeinfo
->var_data
= xmalloc(0x100);
1740 typeinfo
->var_data_allocated
= 0x100;
1741 typeinfo
->var_data
[0] = 0;
1744 if(typeinfo
->var_data
[0] + typedata_size
+ sizeof(int) > typeinfo
->var_data_allocated
) {
1745 typeinfo
->var_data_allocated
= max(typeinfo
->var_data_allocated
* 2,
1746 typeinfo
->var_data
[0] + typedata_size
+ sizeof(int));
1747 typeinfo
->var_data
= xrealloc(typeinfo
->var_data
, typeinfo
->var_data_allocated
);
1750 offset
= typeinfo
->var_data
[0];
1751 typeinfo
->var_data
[0] += typedata_size
;
1752 typedata
= typeinfo
->var_data
+ (offset
>> 2) + 1;
1754 /* fill out the basic type information */
1755 typedata
[0] = typedata_size
| (index
<< 16);
1756 typedata
[2] = varflags
;
1757 typedata
[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1759 if(typeinfo
->vars_allocated
== 0) {
1760 typeinfo
->vars_allocated
= 10;
1761 typeinfo
->var_indices
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1762 typeinfo
->var_names
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1763 typeinfo
->var_offsets
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1765 if(typeinfo
->vars_allocated
== var_num
) {
1766 typeinfo
->vars_allocated
*= 2;
1767 typeinfo
->var_indices
= xrealloc(typeinfo
->var_indices
, typeinfo
->vars_allocated
* sizeof(int));
1768 typeinfo
->var_names
= xrealloc(typeinfo
->var_names
, typeinfo
->vars_allocated
* sizeof(int));
1769 typeinfo
->var_offsets
= xrealloc(typeinfo
->var_offsets
, typeinfo
->vars_allocated
* sizeof(int));
1771 /* update the index data */
1772 typeinfo
->var_indices
[var_num
] = id
;
1773 typeinfo
->var_names
[var_num
] = -1;
1774 typeinfo
->var_offsets
[var_num
] = offset
;
1776 /* figure out type widths and whatnot */
1777 var_datawidth
= type_memsize_and_alignment(var
->declspec
.type
, &var_alignment
);
1778 encode_var(typeinfo
->typelib
, var
->declspec
.type
, var
, &typedata
[1], &var_type_size
);
1780 /* pad out starting position to data width */
1781 typeinfo
->datawidth
+= var_alignment
- 1;
1782 typeinfo
->datawidth
&= ~(var_alignment
- 1);
1784 switch(typeinfo
->typekind
) {
1786 write_int_value(typeinfo
->typelib
, &typedata
[4], VT_I4
, var
->eval
->cval
);
1787 var_kind
= 2; /* VAR_CONST */
1788 var_type_size
+= 16; /* sizeof(VARIANT) */
1789 typeinfo
->datawidth
= var_datawidth
;
1792 typedata
[4] = typeinfo
->datawidth
;
1793 typeinfo
->datawidth
+= var_datawidth
;
1797 typeinfo
->datawidth
= max(typeinfo
->datawidth
, var_datawidth
);
1799 case TKIND_DISPATCH
:
1800 var_kind
= 3; /* VAR_DISPATCH */
1802 typeinfo
->datawidth
= pointer_size
;
1805 error("add_var_desc: unhandled type kind %d\n", typeinfo
->typekind
);
1809 /* add type description size to total required allocation */
1810 typedata
[3] += var_type_size
<< 16 | var_kind
;
1812 switch(extra_attr
) {
1813 case 5: typedata
[9] = -1 /*help_string_context*/;
1814 case 4: typedata
[8] = var_custdata_offset
;
1815 case 3: typedata
[7] = -1;
1816 case 2: typedata
[6] = -1 /*help_string_offset*/;
1817 case 1: typedata
[5] = -1 /*help_context*/;
1821 warning("unknown number of optional attrs\n");
1824 /* fix type alignment */
1825 alignment
= (typeinfo
->typeinfo
->typekind
>> 11) & 0x1f;
1826 if (alignment
< var_alignment
) {
1827 alignment
= var_alignment
;
1828 typeinfo
->typeinfo
->typekind
&= ~0xffc0;
1829 typeinfo
->typeinfo
->typekind
|= alignment
<< 11 | alignment
<< 6;
1833 if (!typeinfo
->typeinfo
->res2
) typeinfo
->typeinfo
->res2
= 0x1a;
1834 if ((index
== 0) || (index
== 1) || (index
== 2) || (index
== 4) || (index
== 9)) {
1835 typeinfo
->typeinfo
->res2
<<= 1;
1839 if (typeinfo
->typeinfo
->res3
== -1) typeinfo
->typeinfo
->res3
= 0;
1840 typeinfo
->typeinfo
->res3
+= 0x2c;
1842 /* increment the number of variable elements */
1843 typeinfo
->typeinfo
->cElement
+= 0x10000;
1845 /* pad data width to alignment */
1846 typeinfo
->typeinfo
->size
= (typeinfo
->datawidth
+ (alignment
- 1)) & ~(alignment
- 1);
1848 offset
= ctl2_alloc_name(typeinfo
->typelib
, var
->name
);
1850 namedata
= typeinfo
->typelib
->typelib_segment_data
[MSFT_SEG_NAME
] + offset
;
1851 if (*((int *)namedata
) == -1) {
1852 *((int *)namedata
) = typeinfo
->typelib
->typelib_typeinfo_offsets
[typeinfo
->typeinfo
->typekind
>> 16];
1853 if(typeinfo
->typekind
!= TKIND_DISPATCH
)
1854 namedata
[9] |= 0x10;
1856 namedata
[9] &= ~0x10;
1858 if (typeinfo
->typekind
== TKIND_ENUM
) {
1859 namedata
[9] |= 0x20;
1861 typeinfo
->var_names
[var_num
] = offset
;
1864 static void add_impl_type(msft_typeinfo_t
*typeinfo
, type_t
*ref
, importinfo_t
*importinfo
)
1867 alloc_importinfo(typeinfo
->typelib
, importinfo
);
1868 typeinfo
->typeinfo
->datatype1
= importinfo
->offset
+1;
1870 if(ref
->typelib_idx
== -1)
1871 add_interface_typeinfo(typeinfo
->typelib
, ref
);
1872 if(ref
->typelib_idx
== -1)
1873 error("add_impl_type: unable to add inherited interface\n");
1875 typeinfo
->typeinfo
->datatype1
= typeinfo
->typelib
->typelib_typeinfo_offsets
[ref
->typelib_idx
];
1878 typeinfo
->typeinfo
->cImplTypes
++;
1881 static msft_typeinfo_t
*create_msft_typeinfo(msft_typelib_t
*typelib
, enum type_kind kind
,
1882 const char *name
, const attr_list_t
*attrs
)
1885 msft_typeinfo_t
*msft_typeinfo
;
1887 int typeinfo_offset
;
1888 MSFT_TypeInfoBase
*typeinfo
;
1889 MSFT_GuidEntry guidentry
;
1891 chat("create_msft_typeinfo: name %s kind %d index %d\n", name
, kind
, typelib
->typelib_header
.nrtypeinfos
);
1893 msft_typeinfo
= xmalloc(sizeof(*msft_typeinfo
));
1894 memset( msft_typeinfo
, 0, sizeof(*msft_typeinfo
) );
1896 msft_typeinfo
->typelib
= typelib
;
1898 nameoffset
= ctl2_alloc_name(typelib
, name
);
1899 typeinfo_offset
= ctl2_alloc_typeinfo(typelib
, nameoffset
);
1900 typeinfo
= (MSFT_TypeInfoBase
*)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEINFO
][typeinfo_offset
];
1902 typelib
->typelib_segment_data
[MSFT_SEG_NAME
][nameoffset
+ 9] = 0x38;
1903 *((int *)&typelib
->typelib_segment_data
[MSFT_SEG_NAME
][nameoffset
]) = typeinfo_offset
;
1905 msft_typeinfo
->typekind
= kind
;
1906 msft_typeinfo
->typeinfo
= typeinfo
;
1908 typeinfo
->typekind
|= kind
| 0x20;
1910 if(kind
== TKIND_COCLASS
)
1911 typeinfo
->flags
|= 0x2; /* TYPEFLAG_FCANCREATE */
1913 if (attrs
) LIST_FOR_EACH_ENTRY( attr
, attrs
, const attr_t
, entry
) {
1914 switch(attr
->type
) {
1915 case ATTR_AGGREGATABLE
:
1916 if (kind
== TKIND_COCLASS
)
1917 typeinfo
->flags
|= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1920 case ATTR_APPOBJECT
:
1921 if (kind
== TKIND_COCLASS
)
1922 typeinfo
->flags
|= 0x1; /* TYPEFLAG_FAPPOBJECT */
1926 if (kind
== TKIND_COCLASS
)
1927 typeinfo
->flags
|= 0x20; /* TYPEFLAG_FCONTROL */
1930 set_custdata_attr(typelib
, attr
->u
.pval
, &typeinfo
->oCustData
);
1934 int offset
= ctl2_alloc_string(typelib
, attr
->u
.pval
);
1935 typeinfo
->datatype1
= offset
;
1940 /* FIXME: check interface is compatible */
1941 typeinfo
->typekind
= (typeinfo
->typekind
& ~0xff) | 0x34;
1942 typeinfo
->flags
|= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1945 case ATTR_HELPCONTEXT
:
1947 expr_t
*expr
= (expr_t
*)attr
->u
.pval
;
1948 typeinfo
->helpcontext
= expr
->cval
;
1951 case ATTR_HELPSTRING
:
1953 int offset
= ctl2_alloc_string(typelib
, attr
->u
.pval
);
1954 if (offset
== -1) break;
1955 typeinfo
->docstringoffs
= offset
;
1958 case ATTR_HELPSTRINGCONTEXT
:
1960 expr_t
*expr
= (expr_t
*)attr
->u
.pval
;
1961 typeinfo
->helpstringcontext
= expr
->cval
;
1965 typeinfo
->flags
|= 0x10; /* TYPEFLAG_FHIDDEN */
1969 typeinfo
->flags
|= 0x04; /* TYPEFLAG_FLICENSED */
1972 case ATTR_NONCREATABLE
:
1973 typeinfo
->flags
&= ~0x2; /* TYPEFLAG_FCANCREATE */
1976 case ATTR_NONEXTENSIBLE
:
1977 typeinfo
->flags
|= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1980 case ATTR_OLEAUTOMATION
:
1981 typeinfo
->flags
|= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1984 /* FIXME: TYPEFLAG_FPREDCLID */
1987 typeinfo
->flags
|= 0x4000; /* TYPEFLAG_FPROXY */
1990 /* FIXME: TYPEFLAG_FREPLACEABLE */
1992 case ATTR_RESTRICTED
:
1993 typeinfo
->flags
|= 0x200; /* TYPEFLAG_FRESTRICTED */
1997 guidentry
.guid
= *(struct uuid
*)attr
->u
.pval
;
1998 guidentry
.hreftype
= typelib
->typelib_typeinfo_offsets
[typeinfo
->typekind
>> 16];
1999 guidentry
.next_hash
= -1;
2000 typeinfo
->posguid
= ctl2_alloc_guid(typelib
, &guidentry
);
2002 if (IsEqualIID(guid
, &IID_IDispatch
)) {
2003 typelib
->typelib_header
.dispatchpos
= typelib
->typelib_typeinfo_offsets
[typeinfo
->typekind
>> 16];
2009 typeinfo
->version
= attr
->u
.ival
;
2017 if (typelib
->last_typeinfo
) typelib
->last_typeinfo
->next_typeinfo
= msft_typeinfo
;
2018 typelib
->last_typeinfo
= msft_typeinfo
;
2019 if (!typelib
->typeinfos
) typelib
->typeinfos
= msft_typeinfo
;
2022 return msft_typeinfo
;
2025 static void add_dispatch(msft_typelib_t
*typelib
)
2027 int guid_offset
, impfile_offset
, hash_key
;
2028 MSFT_GuidEntry guidentry
;
2029 MSFT_ImpInfo impinfo
;
2030 static const struct uuid stdole
= {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2031 static const struct uuid iid_idispatch
= {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2033 if(typelib
->typelib_header
.dispatchpos
!= -1) return;
2035 guidentry
.guid
= stdole
;
2036 guidentry
.hreftype
= 2;
2037 guidentry
.next_hash
= -1;
2038 hash_key
= ctl2_hash_guid(&guidentry
.guid
);
2039 guid_offset
= ctl2_find_guid(typelib
, hash_key
, &guidentry
.guid
);
2040 if (guid_offset
== -1)
2041 guid_offset
= ctl2_alloc_guid(typelib
, &guidentry
);
2042 impfile_offset
= alloc_importfile(typelib
, guid_offset
, 2, 0, "stdole2.tlb");
2044 guidentry
.guid
= iid_idispatch
;
2045 guidentry
.hreftype
= 1;
2046 guidentry
.next_hash
= -1;
2047 impinfo
.flags
= TKIND_INTERFACE
<< 24 | MSFT_IMPINFO_OFFSET_IS_GUID
;
2048 impinfo
.oImpFile
= impfile_offset
;
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 impinfo
.oGuid
= guid_offset
;
2054 typelib
->typelib_header
.dispatchpos
= alloc_msft_importinfo(typelib
, &impinfo
) | 0x01;
2057 static void add_dispinterface_typeinfo(msft_typelib_t
*typelib
, type_t
*dispinterface
)
2059 int num_parents
= 0, num_funcs
= 0;
2060 importinfo_t
*importinfo
= NULL
;
2061 const statement_t
*stmt_func
;
2062 type_t
*inherit
, *ref
;
2066 msft_typeinfo_t
*msft_typeinfo
;
2068 if (-1 < dispinterface
->typelib_idx
)
2071 inherit
= type_dispiface_get_inherit(dispinterface
);
2075 importinfo
= find_importinfo(typelib
, inherit
->name
);
2077 if (!importinfo
&& type_iface_get_inherit(inherit
) && inherit
->typelib_idx
== -1)
2078 add_interface_typeinfo(typelib
, inherit
);
2081 /* check typelib_idx again, it could have been added while resolving the parent interface */
2082 if (-1 < dispinterface
->typelib_idx
)
2085 dispinterface
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2086 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_DISPATCH
, dispinterface
->name
,
2087 dispinterface
->attrs
);
2089 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2090 msft_typeinfo
->typeinfo
->typekind
|= pointer_size
<< 11 | pointer_size
<< 6;
2092 msft_typeinfo
->typeinfo
->flags
|= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2093 add_dispatch(typelib
);
2097 add_impl_type(msft_typeinfo
, inherit
, importinfo
);
2098 msft_typeinfo
->typeinfo
->typekind
|= 0x10;
2101 /* count the number of inherited interfaces and non-local functions */
2102 for (ref
= inherit
; ref
; ref
= type_iface_get_inherit(ref
))
2105 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(ref
) )
2107 var_t
*func
= stmt_func
->u
.var
;
2108 if (!is_local(func
->attrs
)) num_funcs
++;
2111 msft_typeinfo
->typeinfo
->datatype2
= num_funcs
<< 16 | num_parents
;
2112 msft_typeinfo
->typeinfo
->cbSizeVft
= num_funcs
* pointer_size
;
2114 msft_typeinfo
->typeinfo
->cImplTypes
= 1; /* IDispatch */
2116 /* count the no of methods, as the variable indices come after the funcs */
2117 if (dispinterface
->details
.iface
->disp_methods
)
2118 LIST_FOR_EACH_ENTRY( func
, dispinterface
->details
.iface
->disp_methods
, var_t
, entry
)
2121 if (type_dispiface_get_props(dispinterface
))
2122 LIST_FOR_EACH_ENTRY( var
, type_dispiface_get_props(dispinterface
), var_t
, entry
)
2123 add_var_desc(msft_typeinfo
, idx
++, var
);
2125 if (type_dispiface_get_methods(dispinterface
))
2128 LIST_FOR_EACH_ENTRY( func
, type_dispiface_get_methods(dispinterface
), var_t
, entry
)
2129 if(add_func_desc(msft_typeinfo
, func
, idx
))
2133 typelib
->typelib
->reg_ifaces
= xrealloc(typelib
->typelib
->reg_ifaces
,
2134 (typelib
->typelib
->reg_iface_count
+ 1) * sizeof(dispinterface
));
2135 typelib
->typelib
->reg_ifaces
[typelib
->typelib
->reg_iface_count
++] = dispinterface
;
2138 static void add_interface_typeinfo(msft_typelib_t
*typelib
, type_t
*interface
)
2141 const statement_t
*stmt_func
;
2143 msft_typeinfo_t
*msft_typeinfo
;
2144 importinfo_t
*ref_importinfo
= NULL
;
2145 int num_parents
= 0, num_funcs
= 0;
2147 const type_t
*derived
;
2149 if (-1 < interface
->typelib_idx
)
2152 if (!interface
->details
.iface
)
2154 error( "interface %s is referenced but not defined\n", interface
->name
);
2158 if (is_attr(interface
->attrs
, ATTR_DISPINTERFACE
)) {
2159 add_dispinterface_typeinfo(typelib
, interface
);
2163 /* midl adds the parent interface first, unless the parent itself
2164 has no parent (i.e. it stops before IUnknown). */
2166 inherit
= type_iface_get_inherit(interface
);
2169 ref_importinfo
= find_importinfo(typelib
, inherit
->name
);
2171 if(!ref_importinfo
&& type_iface_get_inherit(inherit
) &&
2172 inherit
->typelib_idx
== -1)
2173 add_interface_typeinfo(typelib
, inherit
);
2176 /* check typelib_idx again, it could have been added while resolving the parent interface */
2177 if (-1 < interface
->typelib_idx
)
2180 interface
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2181 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_INTERFACE
, interface
->name
, interface
->attrs
);
2182 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2183 msft_typeinfo
->typeinfo
->typekind
|= 0x0200;
2184 msft_typeinfo
->typeinfo
->typekind
|= pointer_size
<< 11;
2186 for (derived
= inherit
; derived
; derived
= type_iface_get_inherit(derived
))
2187 if (derived
->name
&& !strcmp(derived
->name
, "IDispatch"))
2188 msft_typeinfo
->typeinfo
->flags
|= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2190 if(type_iface_get_inherit(interface
))
2191 add_impl_type(msft_typeinfo
, type_iface_get_inherit(interface
),
2194 /* count the number of inherited interfaces and non-local functions */
2195 for(ref
= inherit
; ref
; ref
= type_iface_get_inherit(ref
)) {
2197 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(ref
) ) {
2198 var_t
*func
= stmt_func
->u
.var
;
2199 if (!is_local(func
->attrs
)) num_funcs
++;
2202 msft_typeinfo
->typeinfo
->datatype2
= num_funcs
<< 16 | num_parents
;
2203 msft_typeinfo
->typeinfo
->cbSizeVft
= num_funcs
* pointer_size
;
2205 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(interface
) ) {
2206 var_t
*func
= stmt_func
->u
.var
;
2207 if(add_func_desc(msft_typeinfo
, func
, idx
))
2211 if (is_attr(interface
->attrs
, ATTR_OLEAUTOMATION
) || is_attr(interface
->attrs
, ATTR_DUAL
))
2213 typelib
->typelib
->reg_ifaces
= xrealloc(typelib
->typelib
->reg_ifaces
,
2214 (typelib
->typelib
->reg_iface_count
+ 1) * sizeof(interface
));
2215 typelib
->typelib
->reg_ifaces
[typelib
->typelib
->reg_iface_count
++] = interface
;
2219 static void add_structure_typeinfo(msft_typelib_t
*typelib
, type_t
*structure
)
2224 msft_typeinfo_t
*msft_typeinfo
;
2226 if (-1 < structure
->typelib_idx
)
2229 structure
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2230 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_RECORD
, structure
->name
, structure
->attrs
);
2231 msft_typeinfo
->typeinfo
->size
= 0;
2233 if (type_get_type(structure
) == TYPE_STRUCT
)
2234 fields
= type_struct_get_fields(structure
);
2236 fields
= type_encapsulated_union_get_fields(structure
);
2240 LIST_FOR_EACH_ENTRY( cur
, fields
, var_t
, entry
)
2241 add_var_desc(msft_typeinfo
, idx
++, cur
);
2245 static void add_enum_typeinfo(msft_typelib_t
*typelib
, type_t
*enumeration
)
2249 msft_typeinfo_t
*msft_typeinfo
;
2251 if (-1 < enumeration
->typelib_idx
)
2254 enumeration
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2255 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_ENUM
, enumeration
->name
, enumeration
->attrs
);
2256 msft_typeinfo
->typeinfo
->size
= 0;
2258 if (type_enum_get_values(enumeration
))
2259 LIST_FOR_EACH_ENTRY( cur
, type_enum_get_values(enumeration
), var_t
, entry
)
2260 add_var_desc(msft_typeinfo
, idx
++, cur
);
2263 static void add_union_typeinfo(msft_typelib_t
*typelib
, type_t
*tunion
)
2267 msft_typeinfo_t
*msft_typeinfo
;
2269 if (-1 < tunion
->typelib_idx
)
2273 tunion
->name
= gen_name();
2275 tunion
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2276 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_UNION
, tunion
->name
, tunion
->attrs
);
2277 msft_typeinfo
->typeinfo
->size
= 0;
2279 if (type_union_get_cases(tunion
))
2280 LIST_FOR_EACH_ENTRY(cur
, type_union_get_cases(tunion
), var_t
, entry
)
2281 add_var_desc(msft_typeinfo
, idx
++, cur
);
2284 static void add_typedef_typeinfo(msft_typelib_t
*typelib
, type_t
*tdef
)
2286 msft_typeinfo_t
*msft_typeinfo
= NULL
;
2287 int datatype1
, datatype2
, duplicate
= 0;
2288 unsigned int size
, alignment
= 0;
2291 if (-1 < tdef
->typelib_idx
)
2294 type
= type_alias_get_aliasee_type(tdef
);
2296 if (!type
->name
|| strcmp(tdef
->name
, type
->name
) != 0)
2298 tdef
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2299 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_ALIAS
, tdef
->name
, tdef
->attrs
);
2304 encode_type(typelib
, get_type_vt(type
), type
, &datatype1
, &datatype2
);
2305 size
= type_memsize_and_alignment(type
, &alignment
);
2309 msft_typeinfo
->typeinfo
->datatype1
= datatype1
;
2310 msft_typeinfo
->typeinfo
->size
= size
;
2311 msft_typeinfo
->typeinfo
->datatype2
= datatype2
;
2312 msft_typeinfo
->typeinfo
->typekind
|= (alignment
<< 11 | alignment
<< 6);
2315 /* avoid adding duplicate type definitions */
2317 tdef
->typelib_idx
= type
->typelib_idx
;
2320 static void add_coclass_typeinfo(msft_typelib_t
*typelib
, type_t
*cls
)
2322 msft_typeinfo_t
*msft_typeinfo
;
2324 int num_ifaces
= 0, offset
, i
;
2325 MSFT_RefRecord
*ref
, *first
= NULL
, *first_source
= NULL
;
2326 int have_default
= 0, have_default_source
= 0;
2328 typeref_list_t
*ifaces
;
2330 if (-1 < cls
->typelib_idx
)
2333 cls
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2334 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_COCLASS
, cls
->name
, cls
->attrs
);
2336 ifaces
= type_coclass_get_ifaces(cls
);
2337 if (ifaces
) LIST_FOR_EACH_ENTRY( iref
, ifaces
, typeref_t
, entry
) num_ifaces
++;
2339 offset
= msft_typeinfo
->typeinfo
->datatype1
= ctl2_alloc_segment(typelib
, MSFT_SEG_REFERENCES
,
2340 num_ifaces
* sizeof(*ref
), 0);
2343 if (ifaces
) LIST_FOR_EACH_ENTRY( iref
, ifaces
, typeref_t
, entry
) {
2344 if(iref
->type
->typelib_idx
== -1)
2345 add_interface_typeinfo(typelib
, iref
->type
);
2346 ref
= (MSFT_RefRecord
*) (typelib
->typelib_segment_data
[MSFT_SEG_REFERENCES
] + offset
+ i
* sizeof(*ref
));
2347 ref
->reftype
= typelib
->typelib_typeinfo_offsets
[iref
->type
->typelib_idx
];
2349 ref
->oCustData
= -1;
2351 if(i
< num_ifaces
- 1)
2352 ref
->onext
= offset
+ (i
+ 1) * sizeof(*ref
);
2354 if (iref
->attrs
) LIST_FOR_EACH_ENTRY( attr
, iref
->attrs
, const attr_t
, entry
) {
2355 switch(attr
->type
) {
2357 ref
->flags
|= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2359 case ATTR_DEFAULTVTABLE
:
2360 ref
->flags
|= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2362 case ATTR_RESTRICTED
:
2363 ref
->flags
|= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2366 ref
->flags
|= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2369 warning("add_coclass_typeinfo: unhandled attr %d\n", attr
->type
);
2372 if(ref
->flags
& 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2373 if(ref
->flags
& 0x2) /* IMPLTYPEFLAG_SOURCE */
2374 have_default_source
= 1;
2379 /* If the interface is non-restricted and we haven't already had one then
2380 remember it so that we can use it as a default later */
2381 if((ref
->flags
& 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2382 if(ref
->flags
& 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2392 /* If we haven't had a default interface, then set the default flags on the
2394 if(!have_default
&& first
)
2395 first
->flags
|= 0x1;
2396 if(!have_default_source
&& first_source
)
2397 first_source
->flags
|= 0x1;
2399 msft_typeinfo
->typeinfo
->cImplTypes
= num_ifaces
;
2400 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2401 msft_typeinfo
->typeinfo
->typekind
|= 0x2200;
2404 static void add_module_typeinfo(msft_typelib_t
*typelib
, type_t
*module
)
2407 const statement_t
*stmt
;
2408 msft_typeinfo_t
*msft_typeinfo
;
2410 if (-1 < module
->typelib_idx
)
2413 module
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2414 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_MODULE
, module
->name
, module
->attrs
);
2415 msft_typeinfo
->typeinfo
->typekind
|= 0x0a00;
2417 STATEMENTS_FOR_EACH_FUNC( stmt
, module
->details
.module
->stmts
) {
2418 var_t
*func
= stmt
->u
.var
;
2419 if(add_func_desc(msft_typeinfo
, func
, idx
))
2423 msft_typeinfo
->typeinfo
->size
= idx
;
2426 static void add_type_typeinfo(msft_typelib_t
*typelib
, type_t
*type
)
2428 switch (type_get_type(type
)) {
2429 case TYPE_INTERFACE
:
2430 add_interface_typeinfo(typelib
, type
);
2433 case TYPE_ENCAPSULATED_UNION
:
2434 add_structure_typeinfo(typelib
, type
);
2437 add_enum_typeinfo(typelib
, type
);
2440 add_union_typeinfo(typelib
, type
);
2443 add_coclass_typeinfo(typelib
, type
);
2450 error("add_entry: unhandled type 0x%x for %s\n",
2451 type_get_type(type
), type
->name
);
2456 static void add_entry(msft_typelib_t
*typelib
, const statement_t
*stmt
)
2458 switch(stmt
->type
) {
2463 case STMT_DECLARATION
:
2464 /* not included in typelib */
2466 case STMT_IMPORTLIB
:
2467 /* not processed here */
2472 if (stmt
->u
.type_list
) LIST_FOR_EACH_ENTRY(ref
, stmt
->u
.type_list
, typeref_t
, entry
) {
2473 /* if the type is public then add the typedef, otherwise attempt
2474 * to add the aliased type */
2475 if (is_attr(ref
->type
->attrs
, ATTR_PUBLIC
))
2476 add_typedef_typeinfo(typelib
, ref
->type
);
2478 add_type_typeinfo(typelib
, type_alias_get_aliasee_type(ref
->type
));
2483 add_module_typeinfo(typelib
, stmt
->u
.type
);
2488 type_t
*type
= stmt
->u
.type
;
2489 add_type_typeinfo(typelib
, type
);
2495 static void set_name(msft_typelib_t
*typelib
)
2499 offset
= ctl2_alloc_name(typelib
, typelib
->typelib
->name
);
2500 if (offset
== -1) return;
2501 typelib
->typelib_header
.NameOffset
= offset
;
2505 static void set_version(msft_typelib_t
*typelib
)
2507 typelib
->typelib_header
.version
= get_attrv( typelib
->typelib
->attrs
, ATTR_VERSION
);
2510 static void set_guid(msft_typelib_t
*typelib
)
2512 MSFT_GuidEntry guidentry
= { {0}, -2, -1 };
2514 struct uuid
*ptr
= get_attrp( typelib
->typelib
->attrs
, ATTR_UUID
);
2516 if (ptr
) guidentry
.guid
= *ptr
;
2518 offset
= ctl2_alloc_guid(typelib
, &guidentry
);
2519 typelib
->typelib_header
.posguid
= offset
;
2524 static void set_doc_string(msft_typelib_t
*typelib
)
2526 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRING
);
2530 int offset
= ctl2_alloc_string(typelib
, str
);
2531 if (offset
!= -1) typelib
->typelib_header
.helpstring
= offset
;
2535 static void set_help_file_name(msft_typelib_t
*typelib
)
2537 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPFILE
);
2541 int offset
= ctl2_alloc_string(typelib
, str
);
2544 typelib
->typelib_header
.helpfile
= offset
;
2545 typelib
->typelib_header
.varflags
|= 0x10;
2550 static void set_help_context(msft_typelib_t
*typelib
)
2552 const expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPCONTEXT
);
2553 if (expr
) typelib
->typelib_header
.helpcontext
= expr
->cval
;
2556 static void set_help_string_dll(msft_typelib_t
*typelib
)
2558 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRINGDLL
);
2562 int offset
= ctl2_alloc_string(typelib
, str
);
2565 typelib
->help_string_dll_offset
= offset
;
2566 typelib
->typelib_header
.varflags
|= 0x100;
2571 static void set_help_string_context(msft_typelib_t
*typelib
)
2573 const expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRINGCONTEXT
);
2574 if (expr
) typelib
->typelib_header
.helpstringcontext
= expr
->cval
;
2577 static void set_lcid(msft_typelib_t
*typelib
)
2579 const expr_t
*lcid_expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_LIBLCID
);
2582 typelib
->typelib_header
.lcid
= lcid_expr
->cval
;
2583 typelib
->typelib_header
.lcid2
= lcid_expr
->cval
;
2587 static void set_lib_flags(msft_typelib_t
*typelib
)
2591 typelib
->typelib_header
.flags
= 0;
2592 if (!typelib
->typelib
->attrs
) return;
2593 LIST_FOR_EACH_ENTRY( attr
, typelib
->typelib
->attrs
, const attr_t
, entry
)
2595 switch(attr
->type
) {
2597 typelib
->typelib_header
.flags
|= 0x02; /* LIBFLAG_FCONTROL */
2600 typelib
->typelib_header
.flags
|= 0x04; /* LIBFLAG_FHIDDEN */
2602 case ATTR_RESTRICTED
:
2603 typelib
->typelib_header
.flags
|= 0x01; /* LIBFLAG_FRESTRICTED */
2612 static void ctl2_write_segment(msft_typelib_t
*typelib
, int segment
)
2614 if (typelib
->typelib_segment_data
[segment
])
2615 put_data(typelib
->typelib_segment_data
[segment
], typelib
->typelib_segdir
[segment
].length
);
2618 static void ctl2_finalize_typeinfos(msft_typelib_t
*typelib
, int filesize
)
2620 msft_typeinfo_t
*typeinfo
;
2622 for (typeinfo
= typelib
->typeinfos
; typeinfo
; typeinfo
= typeinfo
->next_typeinfo
) {
2623 typeinfo
->typeinfo
->memoffset
= filesize
;
2624 if (typeinfo
->func_data
)
2625 filesize
+= typeinfo
->func_data
[0] + ((typeinfo
->typeinfo
->cElement
& 0xffff) * 12);
2626 if (typeinfo
->var_data
)
2627 filesize
+= typeinfo
->var_data
[0] + (((typeinfo
->typeinfo
->cElement
>> 16) & 0xffff) * 12);
2628 if (typeinfo
->func_data
|| typeinfo
->var_data
)
2633 static int ctl2_finalize_segment(msft_typelib_t
*typelib
, int filepos
, int segment
)
2635 if (typelib
->typelib_segdir
[segment
].length
) {
2636 typelib
->typelib_segdir
[segment
].offset
= filepos
;
2638 typelib
->typelib_segdir
[segment
].offset
= -1;
2641 return typelib
->typelib_segdir
[segment
].length
;
2645 static void ctl2_write_typeinfos(msft_typelib_t
*typelib
)
2647 msft_typeinfo_t
*typeinfo
;
2650 for (typeinfo
= typelib
->typeinfos
; typeinfo
; typeinfo
= typeinfo
->next_typeinfo
) {
2651 if (!typeinfo
->func_data
&& !typeinfo
->var_data
) continue;
2653 if (typeinfo
->func_data
)
2654 typedata_size
= typeinfo
->func_data
[0];
2655 if (typeinfo
->var_data
)
2656 typedata_size
+= typeinfo
->var_data
[0];
2657 put_data(&typedata_size
, sizeof(int));
2658 if (typeinfo
->func_data
)
2659 put_data(typeinfo
->func_data
+ 1, typeinfo
->func_data
[0]);
2660 if (typeinfo
->var_data
)
2661 put_data(typeinfo
->var_data
+ 1, typeinfo
->var_data
[0]);
2662 if (typeinfo
->func_indices
)
2663 put_data(typeinfo
->func_indices
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2664 if (typeinfo
->var_indices
)
2665 put_data(typeinfo
->var_indices
, (typeinfo
->typeinfo
->cElement
>> 16) * 4);
2666 if (typeinfo
->func_names
)
2667 put_data(typeinfo
->func_names
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2668 if (typeinfo
->var_names
)
2669 put_data(typeinfo
->var_names
, (typeinfo
->typeinfo
->cElement
>> 16) * 4);
2670 if (typeinfo
->func_offsets
)
2671 put_data(typeinfo
->func_offsets
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2672 if (typeinfo
->var_offsets
) {
2673 int add
= 0, i
, offset
;
2674 if(typeinfo
->func_data
)
2675 add
= typeinfo
->func_data
[0];
2676 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
>> 16); i
++) {
2677 offset
= typeinfo
->var_offsets
[i
];
2679 put_data(&offset
, 4);
2685 static void save_all_changes(msft_typelib_t
*typelib
)
2689 chat("save_all_changes(%p)\n", typelib
);
2691 filepos
= sizeof(MSFT_Header
) + sizeof(MSFT_SegDir
);
2692 if(typelib
->typelib_header
.varflags
& 0x100) filepos
+= 4; /* helpstringdll */
2693 filepos
+= typelib
->typelib_header
.nrtypeinfos
* 4;
2695 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_TYPEINFO
);
2696 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_GUIDHASH
);
2697 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_GUID
);
2698 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_REFERENCES
);
2699 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_IMPORTINFO
);
2700 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_IMPORTFILES
);
2701 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_NAMEHASH
);
2702 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_NAME
);
2703 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_STRING
);
2704 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_TYPEDESC
);
2705 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_ARRAYDESC
);
2706 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_CUSTDATA
);
2707 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_CUSTDATAGUID
);
2709 ctl2_finalize_typeinfos(typelib
, filepos
);
2711 init_output_buffer();
2713 put_data(&typelib
->typelib_header
, sizeof(typelib
->typelib_header
));
2714 if(typelib
->typelib_header
.varflags
& 0x100)
2715 put_data(&typelib
->help_string_dll_offset
, sizeof(typelib
->help_string_dll_offset
));
2717 put_data(typelib
->typelib_typeinfo_offsets
, typelib
->typelib_header
.nrtypeinfos
* 4);
2718 put_data(&typelib
->typelib_segdir
, sizeof(typelib
->typelib_segdir
));
2719 ctl2_write_segment( typelib
, MSFT_SEG_TYPEINFO
);
2720 ctl2_write_segment( typelib
, MSFT_SEG_GUIDHASH
);
2721 ctl2_write_segment( typelib
, MSFT_SEG_GUID
);
2722 ctl2_write_segment( typelib
, MSFT_SEG_REFERENCES
);
2723 ctl2_write_segment( typelib
, MSFT_SEG_IMPORTINFO
);
2724 ctl2_write_segment( typelib
, MSFT_SEG_IMPORTFILES
);
2725 ctl2_write_segment( typelib
, MSFT_SEG_NAMEHASH
);
2726 ctl2_write_segment( typelib
, MSFT_SEG_NAME
);
2727 ctl2_write_segment( typelib
, MSFT_SEG_STRING
);
2728 ctl2_write_segment( typelib
, MSFT_SEG_TYPEDESC
);
2729 ctl2_write_segment( typelib
, MSFT_SEG_ARRAYDESC
);
2730 ctl2_write_segment( typelib
, MSFT_SEG_CUSTDATA
);
2731 ctl2_write_segment( typelib
, MSFT_SEG_CUSTDATAGUID
);
2733 ctl2_write_typeinfos(typelib
);
2735 if (strendswith( typelib_name
, ".res" )) /* create a binary resource file */
2737 char typelib_id
[13] = "#1";
2739 expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_ID
);
2741 sprintf( typelib_id
, "#%d", expr
->cval
);
2742 add_output_to_resources( "TYPELIB", typelib_id
);
2743 if (strendswith( typelib_name
, "_t.res" )) /* add typelib registration */
2744 output_typelib_regscript( typelib
->typelib
);
2746 else flush_output_buffer( typelib_name
);
2749 int create_msft_typelib(typelib_t
*typelib
)
2751 msft_typelib_t
*msft
;
2753 const statement_t
*stmt
;
2756 char *time_override
;
2757 unsigned int version
= 7 << 24 | 555; /* 7.00.0555 */
2758 static const struct uuid midl_time_guid
= {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2759 static const struct uuid midl_version_guid
= {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2760 static const struct uuid midl_info_guid
= {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2761 char info_string
[128];
2763 msft
= xmalloc(sizeof(*msft
));
2764 memset(msft
, 0, sizeof(*msft
));
2765 msft
->typelib
= typelib
;
2767 ctl2_init_header(msft
);
2768 ctl2_init_segdir(msft
);
2770 msft
->typelib_header
.varflags
|= (pointer_size
== 8) ? SYS_WIN64
: SYS_WIN32
;
2773 * The following two calls return an offset or -1 if out of memory. We
2774 * specifically need an offset of 0, however, so...
2776 if (ctl2_alloc_segment(msft
, MSFT_SEG_GUIDHASH
, 0x80, 0x80)) { failed
= 1; }
2777 if (ctl2_alloc_segment(msft
, MSFT_SEG_NAMEHASH
, 0x200, 0x200)) { failed
= 1; }
2785 msft
->typelib_guidhash_segment
= (int *)msft
->typelib_segment_data
[MSFT_SEG_GUIDHASH
];
2786 msft
->typelib_namehash_segment
= (int *)msft
->typelib_segment_data
[MSFT_SEG_NAMEHASH
];
2788 memset(msft
->typelib_guidhash_segment
, 0xff, 0x80);
2789 memset(msft
->typelib_namehash_segment
, 0xff, 0x200);
2791 set_lib_flags(msft
);
2793 set_help_file_name(msft
);
2794 set_doc_string(msft
);
2798 set_help_context(msft
);
2799 set_help_string_dll(msft
);
2800 set_help_string_context(msft
);
2802 if (typelib
->attrs
) LIST_FOR_EACH_ENTRY( attr
, typelib
->attrs
, const attr_t
, entry
) {
2803 switch(attr
->type
) {
2805 set_custdata_attr(msft
, attr
->u
.pval
, &msft
->typelib_header
.CustomDataOffset
);
2812 /* midl adds two sets of custom data to the library: the current unix time
2813 and midl's version number */
2814 time_override
= getenv( "WIDL_TIME_OVERRIDE");
2815 cur_time
= time_override
? atol( time_override
) : time(NULL
);
2816 sprintf(info_string
, "Created by WIDL version %s at %s", PACKAGE_VERSION
, ctime(&cur_time
));
2817 set_custdata(msft
, &midl_info_guid
, VT_BSTR
, info_string
, &msft
->typelib_header
.CustomDataOffset
);
2818 set_custdata(msft
, &midl_time_guid
, VT_UI4
, &cur_time
, &msft
->typelib_header
.CustomDataOffset
);
2819 set_custdata(msft
, &midl_version_guid
, VT_UI4
, &version
, &msft
->typelib_header
.CustomDataOffset
);
2822 LIST_FOR_EACH_ENTRY( stmt
, typelib
->stmts
, const statement_t
, entry
)
2823 add_entry(msft
, stmt
);
2825 save_all_changes(msft
);