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
, 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
);
1088 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
1089 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1091 typedata
[0] = (0x7ffe << 16) | VT_CARRAY
;
1092 typedata
[1] = arrayoffset
;
1094 *encoded_type
= typeoffset
;
1095 *decoded_size
= 20 /*sizeof(ARRAYDESC)*/ + (num_dims
- 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1099 vt
= get_type_vt(type
);
1101 type_t
*ref
= is_ptr(type
) ?
1102 type_pointer_get_ref_type(type
) : type_array_get_element_type(type
);
1103 int skip_ptr
= encode_var(typelib
, ref
, var
, &target_type
, &child_size
);
1106 chat("encode_var: skipping ptr\n");
1107 *encoded_type
= target_type
;
1108 *decoded_size
= child_size
;
1112 for (typeoffset
= 0; typeoffset
< typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
; typeoffset
+= 8) {
1113 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1114 if (((typedata
[0] & 0xffff) == VT_PTR
) && (typedata
[1] == target_type
)) break;
1117 if (typeoffset
== typelib
->typelib_segdir
[MSFT_SEG_TYPEDESC
].length
) {
1120 if (target_type
& 0x80000000) {
1121 mix_field
= ((target_type
>> 16) & 0x3fff) | VT_BYREF
;
1122 } else if (get_type_vt(ref
) == VT_SAFEARRAY
) {
1123 type_t
*element_type
= type_alias_get_aliasee_type(type_array_get_element_type(ref
));
1124 mix_field
= get_type_vt(element_type
) | VT_ARRAY
| VT_BYREF
;
1126 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][target_type
];
1127 mix_field
= ((typedata
[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1130 typeoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_TYPEDESC
, 8, 0);
1131 typedata
= (void *)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEDESC
][typeoffset
];
1133 typedata
[0] = (mix_field
<< 16) | VT_PTR
;
1134 typedata
[1] = target_type
;
1137 *encoded_type
= typeoffset
;
1139 *decoded_size
= 8 /*sizeof(TYPEDESC)*/ + child_size
;
1145 encode_type(typelib
, vt
, type
, encoded_type
, decoded_size
);
1146 /* these types already have an implicit pointer, so we don't need to
1148 if(vt
== VT_DISPATCH
|| vt
== VT_UNKNOWN
) return 2;
1152 static unsigned int get_ulong_val(unsigned int val
, int vt
)
1158 return val
& 0xffff;
1167 static void write_int_value(msft_typelib_t
*typelib
, int *out
, int vt
, int value
)
1169 const unsigned int lv
= get_ulong_val(value
, vt
);
1170 if ((lv
& 0x3ffffff) == lv
) {
1175 int offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATA
, 8, 0);
1176 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
]) = vt
;
1177 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+2], &value
, 4);
1178 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+6]) = 0x5757;
1183 static void write_string_value(msft_typelib_t
*typelib
, int *out
, const char *value
)
1185 int len
= strlen(value
), seg_len
= (len
+ 6 + 3) & ~0x3;
1186 int offset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATA
, seg_len
, 0);
1187 *((unsigned short *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
]) = VT_BSTR
;
1188 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+2], &len
, sizeof(len
));
1189 memcpy(&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+6], value
, len
);
1191 while(len
< seg_len
) {
1192 *((char *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATA
][offset
+len
]) = 0x57;
1198 static void write_default_value(msft_typelib_t
*typelib
, type_t
*type
, expr_t
*expr
, int *out
)
1202 if (expr
->type
== EXPR_STRLIT
|| expr
->type
== EXPR_WSTRLIT
) {
1203 vt
= get_type_vt(type
);
1204 if (vt
!= VT_BSTR
&& vt
!= VT_VARIANT
)
1205 error("string default value applied to non-string type\n");
1206 chat("default value '%s'\n", expr
->u
.sval
);
1207 write_string_value(typelib
, out
, expr
->u
.sval
);
1211 if (type_get_type(type
) == TYPE_ENUM
) {
1213 } else if (is_ptr(type
)) {
1214 vt
= get_type_vt(type_pointer_get_ref_type(type
));
1215 if (vt
== VT_USERDEFINED
)
1218 warning("non-null pointer default value\n");
1220 vt
= get_type_vt(type
);
1235 switch (expr
->type
) {
1243 warning("can't write default VT_VARIANT value for expression type %d.\n", expr
->type
);
1249 warning("can't write value of type %d yet\n", vt
);
1254 write_int_value(typelib
, out
, vt
, expr
->cval
);
1257 static void set_custdata(msft_typelib_t
*typelib
, const struct uuid
*guid
,
1258 int vt
, const void *value
, int *offset
)
1266 hash_key
= ctl2_hash_guid(guid
);
1267 guidoffset
= ctl2_find_guid(typelib
, hash_key
, guid
);
1268 if(guidoffset
== -1) {
1269 /* add GUID that was not already present */
1270 MSFT_GuidEntry guidentry
;
1271 guidentry
.guid
= *guid
;
1273 guidentry
.hreftype
= -1;
1274 guidentry
.next_hash
= -1;
1276 guidoffset
= ctl2_alloc_guid(typelib
, &guidentry
);
1280 /* TODO midl appears to share a single reference if the same string is used as custdata in multiple places */
1281 write_string_value(typelib
, &data_out
, value
);
1283 write_int_value(typelib
, &data_out
, vt
, *(int*)value
);
1285 custoffset
= ctl2_alloc_segment(typelib
, MSFT_SEG_CUSTDATAGUID
, 12, 0);
1287 custdata
= (int *)&typelib
->typelib_segment_data
[MSFT_SEG_CUSTDATAGUID
][custoffset
];
1288 custdata
[0] = guidoffset
;
1289 custdata
[1] = data_out
;
1290 custdata
[2] = *offset
;
1291 *offset
= custoffset
;
1294 static void set_custdata_attr(msft_typelib_t
*typelib
, attr_custdata_t
*custdata
, int *offset
)
1296 switch(custdata
->pval
->type
) {
1299 set_custdata(typelib
, &custdata
->id
, VT_BSTR
, custdata
->pval
->u
.sval
, offset
);
1303 set_custdata(typelib
, &custdata
->id
, VT_I4
, &custdata
->pval
->u
.lval
, offset
);
1306 error("custom() attribute with unknown type\n");
1311 static int add_func_desc(msft_typeinfo_t
* typeinfo
, var_t
*func
, int index
)
1313 int offset
, name_offset
;
1314 int *typedata
, typedata_size
;
1315 int i
, id
, next_idx
;
1316 int decoded_size
, extra_attr
= 0;
1317 int num_params
= 0, num_optional
= 0, num_defaults
= 0;
1318 int has_arg_custdata
= 0;
1320 unsigned char *namedata
;
1322 unsigned int funcflags
= 0, callconv
= 4 /* CC_STDCALL */;
1323 unsigned int funckind
, invokekind
= 1 /* INVOKE_FUNC */;
1324 int help_context
= 0, help_string_context
= 0, help_string_offset
= -1;
1325 int func_custdata_offset
= -1;
1326 int entry
= -1, entry_is_ord
= 0;
1327 int lcid_retval_count
= 0;
1329 chat("add_func_desc(%p,%d)\n", typeinfo
, index
);
1331 id
= ((0x6000 | (typeinfo
->typeinfo
->datatype2
& 0xffff)) << 16) | index
;
1333 switch(typeinfo
->typekind
) {
1334 case TKIND_DISPATCH
:
1335 funckind
= 0x4; /* FUNC_DISPATCH */
1338 funckind
= 0x3; /* FUNC_STATIC */
1341 funckind
= 0x1; /* FUNC_PUREVIRTUAL */
1345 if (is_local( func
->attrs
)) {
1346 chat("add_func_desc: skipping local function\n");
1350 if (type_function_get_args(func
->declspec
.type
))
1351 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), var_t
, entry
)
1354 if (arg
->attrs
) LIST_FOR_EACH_ENTRY( attr
, arg
->attrs
, const attr_t
, entry
) {
1355 if(attr
->type
== ATTR_DEFAULTVALUE
)
1357 else if(attr
->type
== ATTR_OPTIONAL
)
1359 else if(attr
->type
== ATTR_CUSTOM
)
1360 has_arg_custdata
= 1;
1364 chat("add_func_desc: num of params %d\n", num_params
);
1366 name_offset
= ctl2_alloc_name(typeinfo
->typelib
, func
->name
);
1368 if (func
->attrs
) LIST_FOR_EACH_ENTRY( attr
, func
->attrs
, const attr_t
, entry
) {
1369 expr_t
*expr
= attr
->u
.pval
;
1370 switch(attr
->type
) {
1372 funcflags
|= 0x4; /* FUNCFLAG_FBINDABLE */
1375 set_custdata_attr(typeinfo
->typelib
, attr
->u
.pval
, &func_custdata_offset
);
1377 case ATTR_DEFAULTBIND
:
1378 funcflags
|= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1380 case ATTR_DEFAULTCOLLELEM
:
1381 funcflags
|= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1383 case ATTR_DISPLAYBIND
:
1384 funcflags
|= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1387 extra_attr
= max(extra_attr
, 3);
1388 if (expr
->type
== EXPR_STRLIT
|| expr
->type
== EXPR_WSTRLIT
)
1389 entry
= ctl2_alloc_string(typeinfo
->typelib
, attr
->u
.pval
);
1395 case ATTR_HELPCONTEXT
:
1396 extra_attr
= max(extra_attr
, 1);
1397 help_context
= expr
->u
.lval
;
1399 case ATTR_HELPSTRING
:
1400 extra_attr
= max(extra_attr
, 2);
1401 help_string_offset
= ctl2_alloc_string(typeinfo
->typelib
, attr
->u
.pval
);
1403 case ATTR_HELPSTRINGCONTEXT
:
1404 extra_attr
= max(extra_attr
, 6);
1405 help_string_context
= expr
->u
.lval
;
1408 funcflags
|= 0x40; /* FUNCFLAG_FHIDDEN */
1413 case ATTR_IMMEDIATEBIND
:
1414 funcflags
|= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1416 case ATTR_NONBROWSABLE
:
1417 funcflags
|= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1424 invokekind
= 0x2; /* INVOKE_PROPERTYGET */
1427 invokekind
= 0x4; /* INVOKE_PROPERTYPUT */
1429 case ATTR_PROPPUTREF
:
1430 invokekind
= 0x8; /* INVOKE_PROPERTYPUTREF */
1432 /* FIXME: FUNCFLAG_FREPLACEABLE */
1433 case ATTR_REQUESTEDIT
:
1434 funcflags
|= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1436 case ATTR_RESTRICTED
:
1437 funcflags
|= 0x1; /* FUNCFLAG_FRESTRICTED */
1440 funcflags
|= 0x2; /* FUNCFLAG_FSOURCE */
1442 case ATTR_UIDEFAULT
:
1443 funcflags
|= 0x200; /* FUNCFLAG_FUIDEFAULT */
1445 case ATTR_USESGETLASTERROR
:
1446 funcflags
|= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1449 if (num_optional
|| num_defaults
)
1450 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1459 if(has_arg_custdata
|| func_custdata_offset
!= -1) {
1460 extra_attr
= max(extra_attr
, 7 + num_params
);
1463 /* allocate type data space for us */
1464 typedata_size
= 0x18 + extra_attr
* sizeof(int) + (num_params
* (num_defaults
? 16 : 12));
1466 if (!typeinfo
->func_data
) {
1467 typeinfo
->func_data
= xmalloc(0x100);
1468 typeinfo
->func_data_allocated
= 0x100;
1469 typeinfo
->func_data
[0] = 0;
1472 if(typeinfo
->func_data
[0] + typedata_size
+ sizeof(int) > typeinfo
->func_data_allocated
) {
1473 typeinfo
->func_data_allocated
= max(typeinfo
->func_data_allocated
* 2,
1474 typeinfo
->func_data
[0] + typedata_size
+ sizeof(int));
1475 typeinfo
->func_data
= xrealloc(typeinfo
->func_data
, typeinfo
->func_data_allocated
);
1478 offset
= typeinfo
->func_data
[0];
1479 typeinfo
->func_data
[0] += typedata_size
;
1480 typedata
= typeinfo
->func_data
+ (offset
>> 2) + 1;
1483 /* find func with the same name - if it exists use its id */
1484 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
& 0xffff); i
++) {
1485 if(name_offset
== typeinfo
->func_names
[i
]) {
1486 id
= typeinfo
->func_indices
[i
];
1491 /* find the first func with the same id and link via the hiword of typedata[4] */
1493 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
& 0xffff); i
++) {
1494 if(id
== typeinfo
->func_indices
[i
]) {
1495 next_idx
= typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] >> 16;
1496 typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] &= 0xffff;
1497 typeinfo
->func_data
[(typeinfo
->func_offsets
[i
] >> 2) + 1 + 4] |= (index
<< 16);
1502 /* fill out the basic type information */
1503 typedata
[0] = typedata_size
| (index
<< 16);
1504 encode_var(typeinfo
->typelib
, type_function_get_rettype(func
->declspec
.type
), func
,
1505 &typedata
[1], &decoded_size
);
1506 typedata
[2] = funcflags
;
1507 typedata
[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size
) << 16) | typeinfo
->typeinfo
->cbSizeVft
;
1508 typedata
[4] = (next_idx
<< 16) | (callconv
<< 8) | (invokekind
<< 3) | funckind
;
1509 if(has_arg_custdata
|| func_custdata_offset
!= -1) typedata
[4] |= 0x0080;
1510 if(num_defaults
) typedata
[4] |= 0x1000;
1511 if(entry_is_ord
) typedata
[4] |= 0x2000;
1512 typedata
[5] = (num_optional
<< 16) | num_params
;
1514 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1515 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1516 typedata
[3] += (16 /*sizeof(ELEMDESC)*/ * num_params
) << 16;
1517 typedata
[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults
) << 16;
1519 switch(extra_attr
) {
1521 if(extra_attr
> 7 + num_params
) warning("unknown number of optional attrs\n");
1522 /* typedata[13..+num_params] = arg_custdata_offset handled in below loop */
1523 case 7: typedata
[12] = func_custdata_offset
;
1524 case 6: typedata
[11] = help_string_context
;
1525 case 5: typedata
[10] = -1;
1526 case 4: typedata
[9] = -1;
1527 case 3: typedata
[8] = entry
;
1528 case 2: typedata
[7] = help_string_offset
;
1529 case 1: typedata
[6] = help_context
;
1534 if (type_function_get_args(func
->declspec
.type
))
1537 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), var_t
, entry
)
1540 int *paramdata
= typedata
+ 6 + extra_attr
+ (num_defaults
? num_params
: 0) + i
* 3;
1541 int *defaultdata
= num_defaults
? typedata
+ 6 + extra_attr
+ i
: NULL
;
1542 int arg_custdata_offset
= -1;
1544 if(defaultdata
) *defaultdata
= -1;
1546 encode_var(typeinfo
->typelib
, arg
->declspec
.type
, arg
, paramdata
, &decoded_size
);
1547 if (arg
->attrs
) LIST_FOR_EACH_ENTRY( attr
, arg
->attrs
, const attr_t
, entry
) {
1548 switch(attr
->type
) {
1550 set_custdata_attr(typeinfo
->typelib
, attr
->u
.pval
, &arg_custdata_offset
);
1552 case ATTR_DEFAULTVALUE
:
1554 paramflags
|= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1555 write_default_value(typeinfo
->typelib
, arg
->declspec
.type
, (expr_t
*)attr
->u
.pval
, defaultdata
);
1559 paramflags
|= 0x01; /* PARAMFLAG_FIN */
1562 paramflags
|= 0x10; /* PARAMFLAG_FOPT */
1565 paramflags
|= 0x02; /* PARAMFLAG_FOUT */
1567 case ATTR_PARAMLCID
:
1568 paramflags
|= 0x04; /* PARAMFLAG_LCID */
1569 lcid_retval_count
++;
1572 paramflags
|= 0x08; /* PARAMFLAG_FRETVAL */
1573 lcid_retval_count
++;
1576 chat("unhandled param attr %d\n", attr
->type
);
1579 if(extra_attr
> 7 + i
) {
1580 typedata
[13+i
] = arg_custdata_offset
;
1584 paramdata
[2] = paramflags
;
1585 typedata
[3] += decoded_size
<< 16;
1591 if(lcid_retval_count
== 1)
1592 typedata
[4] |= 0x4000;
1593 else if(lcid_retval_count
== 2)
1594 typedata
[4] |= 0x8000;
1596 if(typeinfo
->funcs_allocated
== 0) {
1597 typeinfo
->funcs_allocated
= 10;
1598 typeinfo
->func_indices
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1599 typeinfo
->func_names
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1600 typeinfo
->func_offsets
= xmalloc(typeinfo
->funcs_allocated
* sizeof(int));
1602 if(typeinfo
->funcs_allocated
== (typeinfo
->typeinfo
->cElement
& 0xffff)) {
1603 typeinfo
->funcs_allocated
*= 2;
1604 typeinfo
->func_indices
= xrealloc(typeinfo
->func_indices
, typeinfo
->funcs_allocated
* sizeof(int));
1605 typeinfo
->func_names
= xrealloc(typeinfo
->func_names
, typeinfo
->funcs_allocated
* sizeof(int));
1606 typeinfo
->func_offsets
= xrealloc(typeinfo
->func_offsets
, typeinfo
->funcs_allocated
* sizeof(int));
1609 /* update the index data */
1610 typeinfo
->func_indices
[typeinfo
->typeinfo
->cElement
& 0xffff] = id
;
1611 typeinfo
->func_offsets
[typeinfo
->typeinfo
->cElement
& 0xffff] = offset
;
1612 typeinfo
->func_names
[typeinfo
->typeinfo
->cElement
& 0xffff] = name_offset
;
1615 if (!typeinfo
->typeinfo
->res2
) typeinfo
->typeinfo
->res2
= 0x20;
1616 typeinfo
->typeinfo
->res2
<<= 1;
1618 if (index
< 2) typeinfo
->typeinfo
->res2
+= num_params
<< 4;
1620 if (typeinfo
->typeinfo
->res3
== -1) typeinfo
->typeinfo
->res3
= 0;
1621 typeinfo
->typeinfo
->res3
+= 0x38 + num_params
* 0x10;
1622 if(num_defaults
) typeinfo
->typeinfo
->res3
+= num_params
* 0x4;
1624 /* adjust size of VTBL */
1625 if(funckind
!= 0x3 /* FUNC_STATIC */)
1626 typeinfo
->typeinfo
->cbSizeVft
+= pointer_size
;
1628 /* Increment the number of function elements */
1629 typeinfo
->typeinfo
->cElement
+= 1;
1631 namedata
= typeinfo
->typelib
->typelib_segment_data
[MSFT_SEG_NAME
] + name_offset
;
1632 if (*((int *)namedata
) == -1) {
1633 *((int *)namedata
) = typeinfo
->typelib
->typelib_typeinfo_offsets
[typeinfo
->typeinfo
->typekind
>> 16];
1634 if(typeinfo
->typekind
== TKIND_MODULE
)
1635 namedata
[9] |= 0x10;
1637 namedata
[9] &= ~0x10;
1639 if(typeinfo
->typekind
== TKIND_MODULE
)
1640 namedata
[9] |= 0x20;
1642 if (type_function_get_args(func
->declspec
.type
))
1645 LIST_FOR_EACH_ENTRY( arg
, type_function_get_args(func
->declspec
.type
), var_t
, entry
)
1647 /* don't give the last arg of a [propput*] func a name */
1648 if(i
!= num_params
- 1 || (invokekind
!= 0x4 /* INVOKE_PROPERTYPUT */ && invokekind
!= 0x8 /* INVOKE_PROPERTYPUTREF */))
1650 int *paramdata
= typedata
+ 6 + extra_attr
+ (num_defaults
? num_params
: 0) + i
* 3;
1651 offset
= ctl2_alloc_name(typeinfo
->typelib
, arg
->name
);
1652 paramdata
[1] = offset
;
1660 static void add_var_desc(msft_typeinfo_t
*typeinfo
, unsigned int index
, var_t
* var
)
1663 unsigned int typedata_size
;
1666 unsigned int var_datawidth
, var_alignment
= 0;
1667 int var_type_size
, var_kind
= 0 /* VAR_PERINSTANCE */;
1671 unsigned char *namedata
;
1672 int var_num
= (typeinfo
->typeinfo
->cElement
>> 16) & 0xffff;
1673 int var_custdata_offset
= -1;
1676 var
->name
= gen_name();
1678 chat("add_var_desc(%d, %s)\n", index
, var
->name
);
1680 id
= 0x40000000 + index
;
1682 if (var
->attrs
) LIST_FOR_EACH_ENTRY( attr
, var
->attrs
, const attr_t
, entry
) {
1683 expr_t
*expr
= attr
->u
.pval
;
1684 switch(attr
->type
) {
1686 varflags
|= 0x04; /* VARFLAG_FBINDABLE */
1689 extra_attr
= max(extra_attr
,4);
1690 set_custdata_attr(typeinfo
->typelib
, attr
->u
.pval
, &var_custdata_offset
);
1692 case ATTR_DEFAULTBIND
:
1693 varflags
|= 0x20; /* VARFLAG_FDEFAULTBIND */
1695 case ATTR_DEFAULTCOLLELEM
:
1696 varflags
|= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1698 case ATTR_DISPLAYBIND
:
1699 varflags
|= 0x10; /* VARFLAG_FDISPLAYBIND */
1702 varflags
|= 0x40; /* VARFLAG_FHIDDEN */
1707 case ATTR_IMMEDIATEBIND
:
1708 varflags
|= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1710 case ATTR_NONBROWSABLE
:
1711 varflags
|= 0x400; /* VARFLAG_FNONBROWSABLE */
1714 varflags
|= 0x01; /* VARFLAG_FREADONLY */
1716 /* FIXME: VARFLAG_FREPLACEABLE */
1717 case ATTR_REQUESTEDIT
:
1718 varflags
|= 0x08; /* VARFLAG_FREQUESTEDIT */
1720 case ATTR_RESTRICTED
:
1721 varflags
|= 0x80; /* VARFLAG_FRESTRICTED */
1724 varflags
|= 0x02; /* VARFLAG_FSOURCE */
1726 case ATTR_UIDEFAULT
:
1727 varflags
|= 0x0200; /* VARFLAG_FUIDEFAULT */
1734 /* allocate type data space for us */
1735 typedata_size
= 0x14 + extra_attr
* sizeof(int);
1737 if (!typeinfo
->var_data
) {
1738 typeinfo
->var_data
= xmalloc(0x100);
1739 typeinfo
->var_data_allocated
= 0x100;
1740 typeinfo
->var_data
[0] = 0;
1743 if(typeinfo
->var_data
[0] + typedata_size
+ sizeof(int) > typeinfo
->var_data_allocated
) {
1744 typeinfo
->var_data_allocated
= max(typeinfo
->var_data_allocated
* 2,
1745 typeinfo
->var_data
[0] + typedata_size
+ sizeof(int));
1746 typeinfo
->var_data
= xrealloc(typeinfo
->var_data
, typeinfo
->var_data_allocated
);
1749 offset
= typeinfo
->var_data
[0];
1750 typeinfo
->var_data
[0] += typedata_size
;
1751 typedata
= typeinfo
->var_data
+ (offset
>> 2) + 1;
1753 /* fill out the basic type information */
1754 typedata
[0] = typedata_size
| (index
<< 16);
1755 typedata
[2] = varflags
;
1756 typedata
[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1758 if(typeinfo
->vars_allocated
== 0) {
1759 typeinfo
->vars_allocated
= 10;
1760 typeinfo
->var_indices
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1761 typeinfo
->var_names
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1762 typeinfo
->var_offsets
= xmalloc(typeinfo
->vars_allocated
* sizeof(int));
1764 if(typeinfo
->vars_allocated
== var_num
) {
1765 typeinfo
->vars_allocated
*= 2;
1766 typeinfo
->var_indices
= xrealloc(typeinfo
->var_indices
, typeinfo
->vars_allocated
* sizeof(int));
1767 typeinfo
->var_names
= xrealloc(typeinfo
->var_names
, typeinfo
->vars_allocated
* sizeof(int));
1768 typeinfo
->var_offsets
= xrealloc(typeinfo
->var_offsets
, typeinfo
->vars_allocated
* sizeof(int));
1770 /* update the index data */
1771 typeinfo
->var_indices
[var_num
] = id
;
1772 typeinfo
->var_names
[var_num
] = -1;
1773 typeinfo
->var_offsets
[var_num
] = offset
;
1775 /* figure out type widths and whatnot */
1776 var_datawidth
= type_memsize_and_alignment(var
->declspec
.type
, &var_alignment
);
1777 encode_var(typeinfo
->typelib
, var
->declspec
.type
, var
, &typedata
[1], &var_type_size
);
1779 /* pad out starting position to data width */
1780 typeinfo
->datawidth
+= var_alignment
- 1;
1781 typeinfo
->datawidth
&= ~(var_alignment
- 1);
1783 switch(typeinfo
->typekind
) {
1785 write_int_value(typeinfo
->typelib
, &typedata
[4], VT_I4
, var
->eval
->cval
);
1786 var_kind
= 2; /* VAR_CONST */
1787 var_type_size
+= 16; /* sizeof(VARIANT) */
1788 typeinfo
->datawidth
= var_datawidth
;
1791 typedata
[4] = typeinfo
->datawidth
;
1792 typeinfo
->datawidth
+= var_datawidth
;
1796 typeinfo
->datawidth
= max(typeinfo
->datawidth
, var_datawidth
);
1798 case TKIND_DISPATCH
:
1799 var_kind
= 3; /* VAR_DISPATCH */
1801 typeinfo
->datawidth
= pointer_size
;
1804 error("add_var_desc: unhandled type kind %d\n", typeinfo
->typekind
);
1808 /* add type description size to total required allocation */
1809 typedata
[3] += var_type_size
<< 16 | var_kind
;
1811 switch(extra_attr
) {
1812 case 5: typedata
[9] = -1 /*help_string_context*/;
1813 case 4: typedata
[8] = var_custdata_offset
;
1814 case 3: typedata
[7] = -1;
1815 case 2: typedata
[6] = -1 /*help_string_offset*/;
1816 case 1: typedata
[5] = -1 /*help_context*/;
1820 warning("unknown number of optional attrs\n");
1823 /* fix type alignment */
1824 alignment
= (typeinfo
->typeinfo
->typekind
>> 11) & 0x1f;
1825 if (alignment
< var_alignment
) {
1826 alignment
= var_alignment
;
1827 typeinfo
->typeinfo
->typekind
&= ~0xffc0;
1828 typeinfo
->typeinfo
->typekind
|= alignment
<< 11 | alignment
<< 6;
1832 if (!typeinfo
->typeinfo
->res2
) typeinfo
->typeinfo
->res2
= 0x1a;
1833 if ((index
== 0) || (index
== 1) || (index
== 2) || (index
== 4) || (index
== 9)) {
1834 typeinfo
->typeinfo
->res2
<<= 1;
1838 if (typeinfo
->typeinfo
->res3
== -1) typeinfo
->typeinfo
->res3
= 0;
1839 typeinfo
->typeinfo
->res3
+= 0x2c;
1841 /* increment the number of variable elements */
1842 typeinfo
->typeinfo
->cElement
+= 0x10000;
1844 /* pad data width to alignment */
1845 typeinfo
->typeinfo
->size
= (typeinfo
->datawidth
+ (alignment
- 1)) & ~(alignment
- 1);
1847 offset
= ctl2_alloc_name(typeinfo
->typelib
, var
->name
);
1849 namedata
= typeinfo
->typelib
->typelib_segment_data
[MSFT_SEG_NAME
] + offset
;
1850 if (*((int *)namedata
) == -1) {
1851 *((int *)namedata
) = typeinfo
->typelib
->typelib_typeinfo_offsets
[typeinfo
->typeinfo
->typekind
>> 16];
1852 if(typeinfo
->typekind
!= TKIND_DISPATCH
)
1853 namedata
[9] |= 0x10;
1855 namedata
[9] &= ~0x10;
1857 if (typeinfo
->typekind
== TKIND_ENUM
) {
1858 namedata
[9] |= 0x20;
1860 typeinfo
->var_names
[var_num
] = offset
;
1863 static void add_impl_type(msft_typeinfo_t
*typeinfo
, type_t
*ref
, importinfo_t
*importinfo
)
1866 alloc_importinfo(typeinfo
->typelib
, importinfo
);
1867 typeinfo
->typeinfo
->datatype1
= importinfo
->offset
+1;
1869 if(ref
->typelib_idx
== -1)
1870 add_interface_typeinfo(typeinfo
->typelib
, ref
);
1871 if(ref
->typelib_idx
== -1)
1872 error("add_impl_type: unable to add inherited interface\n");
1874 typeinfo
->typeinfo
->datatype1
= typeinfo
->typelib
->typelib_typeinfo_offsets
[ref
->typelib_idx
];
1877 typeinfo
->typeinfo
->cImplTypes
++;
1880 static msft_typeinfo_t
*create_msft_typeinfo(msft_typelib_t
*typelib
, enum type_kind kind
,
1881 const char *name
, const attr_list_t
*attrs
)
1884 msft_typeinfo_t
*msft_typeinfo
;
1886 int typeinfo_offset
;
1887 MSFT_TypeInfoBase
*typeinfo
;
1888 MSFT_GuidEntry guidentry
;
1890 chat("create_msft_typeinfo: name %s kind %d index %d\n", name
, kind
, typelib
->typelib_header
.nrtypeinfos
);
1892 msft_typeinfo
= xmalloc(sizeof(*msft_typeinfo
));
1893 memset( msft_typeinfo
, 0, sizeof(*msft_typeinfo
) );
1895 msft_typeinfo
->typelib
= typelib
;
1897 nameoffset
= ctl2_alloc_name(typelib
, name
);
1898 typeinfo_offset
= ctl2_alloc_typeinfo(typelib
, nameoffset
);
1899 typeinfo
= (MSFT_TypeInfoBase
*)&typelib
->typelib_segment_data
[MSFT_SEG_TYPEINFO
][typeinfo_offset
];
1901 typelib
->typelib_segment_data
[MSFT_SEG_NAME
][nameoffset
+ 9] = 0x38;
1902 *((int *)&typelib
->typelib_segment_data
[MSFT_SEG_NAME
][nameoffset
]) = typeinfo_offset
;
1904 msft_typeinfo
->typekind
= kind
;
1905 msft_typeinfo
->typeinfo
= typeinfo
;
1907 typeinfo
->typekind
|= kind
| 0x20;
1909 if(kind
== TKIND_COCLASS
)
1910 typeinfo
->flags
|= 0x2; /* TYPEFLAG_FCANCREATE */
1912 if (attrs
) LIST_FOR_EACH_ENTRY( attr
, attrs
, const attr_t
, entry
) {
1913 switch(attr
->type
) {
1914 case ATTR_AGGREGATABLE
:
1915 if (kind
== TKIND_COCLASS
)
1916 typeinfo
->flags
|= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1919 case ATTR_APPOBJECT
:
1920 if (kind
== TKIND_COCLASS
)
1921 typeinfo
->flags
|= 0x1; /* TYPEFLAG_FAPPOBJECT */
1925 if (kind
== TKIND_COCLASS
)
1926 typeinfo
->flags
|= 0x20; /* TYPEFLAG_FCONTROL */
1929 set_custdata_attr(typelib
, attr
->u
.pval
, &typeinfo
->oCustData
);
1933 int offset
= ctl2_alloc_string(typelib
, attr
->u
.pval
);
1934 typeinfo
->datatype1
= offset
;
1939 /* FIXME: check interface is compatible */
1940 typeinfo
->typekind
= (typeinfo
->typekind
& ~0xff) | 0x34;
1941 typeinfo
->flags
|= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1944 case ATTR_HELPCONTEXT
:
1946 expr_t
*expr
= (expr_t
*)attr
->u
.pval
;
1947 typeinfo
->helpcontext
= expr
->cval
;
1950 case ATTR_HELPSTRING
:
1952 int offset
= ctl2_alloc_string(typelib
, attr
->u
.pval
);
1953 if (offset
== -1) break;
1954 typeinfo
->docstringoffs
= offset
;
1957 case ATTR_HELPSTRINGCONTEXT
:
1959 expr_t
*expr
= (expr_t
*)attr
->u
.pval
;
1960 typeinfo
->helpstringcontext
= expr
->cval
;
1964 typeinfo
->flags
|= 0x10; /* TYPEFLAG_FHIDDEN */
1968 typeinfo
->flags
|= 0x04; /* TYPEFLAG_FLICENSED */
1971 case ATTR_NONCREATABLE
:
1972 typeinfo
->flags
&= ~0x2; /* TYPEFLAG_FCANCREATE */
1975 case ATTR_NONEXTENSIBLE
:
1976 typeinfo
->flags
|= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1979 case ATTR_OLEAUTOMATION
:
1980 typeinfo
->flags
|= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1983 /* FIXME: TYPEFLAG_FPREDCLID */
1986 typeinfo
->flags
|= 0x4000; /* TYPEFLAG_FPROXY */
1989 /* FIXME: TYPEFLAG_FREPLACEABLE */
1991 case ATTR_RESTRICTED
:
1992 typeinfo
->flags
|= 0x200; /* TYPEFLAG_FRESTRICTED */
1996 guidentry
.guid
= *(struct uuid
*)attr
->u
.pval
;
1997 guidentry
.hreftype
= typelib
->typelib_typeinfo_offsets
[typeinfo
->typekind
>> 16];
1998 guidentry
.next_hash
= -1;
1999 typeinfo
->posguid
= ctl2_alloc_guid(typelib
, &guidentry
);
2001 if (IsEqualIID(guid
, &IID_IDispatch
)) {
2002 typelib
->typelib_header
.dispatchpos
= typelib
->typelib_typeinfo_offsets
[typeinfo
->typekind
>> 16];
2008 typeinfo
->version
= attr
->u
.ival
;
2016 if (typelib
->last_typeinfo
) typelib
->last_typeinfo
->next_typeinfo
= msft_typeinfo
;
2017 typelib
->last_typeinfo
= msft_typeinfo
;
2018 if (!typelib
->typeinfos
) typelib
->typeinfos
= msft_typeinfo
;
2021 return msft_typeinfo
;
2024 static void add_dispatch(msft_typelib_t
*typelib
)
2026 int guid_offset
, impfile_offset
, hash_key
;
2027 MSFT_GuidEntry guidentry
;
2028 MSFT_ImpInfo impinfo
;
2029 static const struct uuid stdole
= {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2030 static const struct uuid iid_idispatch
= {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2032 if(typelib
->typelib_header
.dispatchpos
!= -1) return;
2034 guidentry
.guid
= stdole
;
2035 guidentry
.hreftype
= 2;
2036 guidentry
.next_hash
= -1;
2037 hash_key
= ctl2_hash_guid(&guidentry
.guid
);
2038 guid_offset
= ctl2_find_guid(typelib
, hash_key
, &guidentry
.guid
);
2039 if (guid_offset
== -1)
2040 guid_offset
= ctl2_alloc_guid(typelib
, &guidentry
);
2041 impfile_offset
= alloc_importfile(typelib
, guid_offset
, 2, 0, "stdole2.tlb");
2043 guidentry
.guid
= iid_idispatch
;
2044 guidentry
.hreftype
= 1;
2045 guidentry
.next_hash
= -1;
2046 impinfo
.flags
= TKIND_INTERFACE
<< 24 | MSFT_IMPINFO_OFFSET_IS_GUID
;
2047 impinfo
.oImpFile
= impfile_offset
;
2048 hash_key
= ctl2_hash_guid(&guidentry
.guid
);
2049 guid_offset
= ctl2_find_guid(typelib
, hash_key
, &guidentry
.guid
);
2050 if (guid_offset
== -1)
2051 guid_offset
= ctl2_alloc_guid(typelib
, &guidentry
);
2052 impinfo
.oGuid
= guid_offset
;
2053 typelib
->typelib_header
.dispatchpos
= alloc_msft_importinfo(typelib
, &impinfo
) | 0x01;
2056 static void add_dispinterface_typeinfo(msft_typelib_t
*typelib
, type_t
*dispinterface
)
2058 int num_parents
= 0, num_funcs
= 0;
2059 importinfo_t
*importinfo
= NULL
;
2060 const statement_t
*stmt_func
;
2061 type_t
*inherit
, *ref
;
2065 msft_typeinfo_t
*msft_typeinfo
;
2067 if (-1 < dispinterface
->typelib_idx
)
2070 inherit
= type_dispiface_get_inherit(dispinterface
);
2074 importinfo
= find_importinfo(typelib
, inherit
->name
);
2076 if (!importinfo
&& type_iface_get_inherit(inherit
) && inherit
->typelib_idx
== -1)
2077 add_interface_typeinfo(typelib
, inherit
);
2080 /* check typelib_idx again, it could have been added while resolving the parent interface */
2081 if (-1 < dispinterface
->typelib_idx
)
2084 dispinterface
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2085 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_DISPATCH
, dispinterface
->name
,
2086 dispinterface
->attrs
);
2088 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2089 msft_typeinfo
->typeinfo
->typekind
|= pointer_size
<< 11 | pointer_size
<< 6;
2091 msft_typeinfo
->typeinfo
->flags
|= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2092 add_dispatch(typelib
);
2096 add_impl_type(msft_typeinfo
, inherit
, importinfo
);
2097 msft_typeinfo
->typeinfo
->typekind
|= 0x10;
2100 /* count the number of inherited interfaces and non-local functions */
2101 for (ref
= inherit
; ref
; ref
= type_iface_get_inherit(ref
))
2104 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(ref
) )
2106 var_t
*func
= stmt_func
->u
.var
;
2107 if (!is_local(func
->attrs
)) num_funcs
++;
2110 msft_typeinfo
->typeinfo
->datatype2
= num_funcs
<< 16 | num_parents
;
2111 msft_typeinfo
->typeinfo
->cbSizeVft
= num_funcs
* pointer_size
;
2113 msft_typeinfo
->typeinfo
->cImplTypes
= 1; /* IDispatch */
2115 /* count the no of methods, as the variable indices come after the funcs */
2116 if (dispinterface
->details
.iface
->disp_methods
)
2117 LIST_FOR_EACH_ENTRY( func
, dispinterface
->details
.iface
->disp_methods
, var_t
, entry
)
2120 if (type_dispiface_get_props(dispinterface
))
2121 LIST_FOR_EACH_ENTRY( var
, type_dispiface_get_props(dispinterface
), var_t
, entry
)
2122 add_var_desc(msft_typeinfo
, idx
++, var
);
2124 if (type_dispiface_get_methods(dispinterface
))
2127 LIST_FOR_EACH_ENTRY( func
, type_dispiface_get_methods(dispinterface
), var_t
, entry
)
2128 if(add_func_desc(msft_typeinfo
, func
, idx
))
2132 typelib
->typelib
->reg_ifaces
= xrealloc(typelib
->typelib
->reg_ifaces
,
2133 (typelib
->typelib
->reg_iface_count
+ 1) * sizeof(dispinterface
));
2134 typelib
->typelib
->reg_ifaces
[typelib
->typelib
->reg_iface_count
++] = dispinterface
;
2137 static void add_interface_typeinfo(msft_typelib_t
*typelib
, type_t
*interface
)
2140 const statement_t
*stmt_func
;
2142 msft_typeinfo_t
*msft_typeinfo
;
2143 importinfo_t
*ref_importinfo
= NULL
;
2144 int num_parents
= 0, num_funcs
= 0;
2146 const type_t
*derived
;
2148 if (-1 < interface
->typelib_idx
)
2151 if (!interface
->details
.iface
)
2153 error( "interface %s is referenced but not defined\n", interface
->name
);
2157 if (is_attr(interface
->attrs
, ATTR_DISPINTERFACE
)) {
2158 add_dispinterface_typeinfo(typelib
, interface
);
2162 /* midl adds the parent interface first, unless the parent itself
2163 has no parent (i.e. it stops before IUnknown). */
2165 inherit
= type_iface_get_inherit(interface
);
2168 ref_importinfo
= find_importinfo(typelib
, inherit
->name
);
2170 if(!ref_importinfo
&& type_iface_get_inherit(inherit
) &&
2171 inherit
->typelib_idx
== -1)
2172 add_interface_typeinfo(typelib
, inherit
);
2175 /* check typelib_idx again, it could have been added while resolving the parent interface */
2176 if (-1 < interface
->typelib_idx
)
2179 interface
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2180 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_INTERFACE
, interface
->name
, interface
->attrs
);
2181 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2182 msft_typeinfo
->typeinfo
->typekind
|= 0x0200;
2183 msft_typeinfo
->typeinfo
->typekind
|= pointer_size
<< 11;
2185 for (derived
= inherit
; derived
; derived
= type_iface_get_inherit(derived
))
2186 if (derived
->name
&& !strcmp(derived
->name
, "IDispatch"))
2187 msft_typeinfo
->typeinfo
->flags
|= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2189 if(type_iface_get_inherit(interface
))
2190 add_impl_type(msft_typeinfo
, type_iface_get_inherit(interface
),
2193 /* count the number of inherited interfaces and non-local functions */
2194 for(ref
= inherit
; ref
; ref
= type_iface_get_inherit(ref
)) {
2196 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(ref
) ) {
2197 var_t
*func
= stmt_func
->u
.var
;
2198 if (!is_local(func
->attrs
)) num_funcs
++;
2201 msft_typeinfo
->typeinfo
->datatype2
= num_funcs
<< 16 | num_parents
;
2202 msft_typeinfo
->typeinfo
->cbSizeVft
= num_funcs
* pointer_size
;
2204 STATEMENTS_FOR_EACH_FUNC( stmt_func
, type_iface_get_stmts(interface
) ) {
2205 var_t
*func
= stmt_func
->u
.var
;
2206 if(add_func_desc(msft_typeinfo
, func
, idx
))
2210 if (is_attr(interface
->attrs
, ATTR_OLEAUTOMATION
) || is_attr(interface
->attrs
, ATTR_DUAL
))
2212 typelib
->typelib
->reg_ifaces
= xrealloc(typelib
->typelib
->reg_ifaces
,
2213 (typelib
->typelib
->reg_iface_count
+ 1) * sizeof(interface
));
2214 typelib
->typelib
->reg_ifaces
[typelib
->typelib
->reg_iface_count
++] = interface
;
2218 static void add_structure_typeinfo(msft_typelib_t
*typelib
, type_t
*structure
)
2223 msft_typeinfo_t
*msft_typeinfo
;
2225 if (-1 < structure
->typelib_idx
)
2228 structure
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2229 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_RECORD
, structure
->name
, structure
->attrs
);
2230 msft_typeinfo
->typeinfo
->size
= 0;
2232 if (type_get_type(structure
) == TYPE_STRUCT
)
2233 fields
= type_struct_get_fields(structure
);
2235 fields
= type_encapsulated_union_get_fields(structure
);
2239 LIST_FOR_EACH_ENTRY( cur
, fields
, var_t
, entry
)
2240 add_var_desc(msft_typeinfo
, idx
++, cur
);
2244 static void add_enum_typeinfo(msft_typelib_t
*typelib
, type_t
*enumeration
)
2248 msft_typeinfo_t
*msft_typeinfo
;
2250 if (-1 < enumeration
->typelib_idx
)
2253 enumeration
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2254 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_ENUM
, enumeration
->name
, enumeration
->attrs
);
2255 msft_typeinfo
->typeinfo
->size
= 0;
2257 if (type_enum_get_values(enumeration
))
2258 LIST_FOR_EACH_ENTRY( cur
, type_enum_get_values(enumeration
), var_t
, entry
)
2259 add_var_desc(msft_typeinfo
, idx
++, cur
);
2262 static void add_union_typeinfo(msft_typelib_t
*typelib
, type_t
*tunion
)
2266 msft_typeinfo_t
*msft_typeinfo
;
2268 if (-1 < tunion
->typelib_idx
)
2272 tunion
->name
= gen_name();
2274 tunion
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2275 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_UNION
, tunion
->name
, tunion
->attrs
);
2276 msft_typeinfo
->typeinfo
->size
= 0;
2278 if (type_union_get_cases(tunion
))
2279 LIST_FOR_EACH_ENTRY(cur
, type_union_get_cases(tunion
), var_t
, entry
)
2280 add_var_desc(msft_typeinfo
, idx
++, cur
);
2283 static void add_typedef_typeinfo(msft_typelib_t
*typelib
, type_t
*tdef
)
2285 msft_typeinfo_t
*msft_typeinfo
= NULL
;
2286 int datatype1
, datatype2
, duplicate
= 0;
2287 unsigned int size
, alignment
= 0;
2290 if (-1 < tdef
->typelib_idx
)
2293 type
= type_alias_get_aliasee_type(tdef
);
2295 if (!type
->name
|| strcmp(tdef
->name
, type
->name
) != 0)
2297 tdef
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2298 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_ALIAS
, tdef
->name
, tdef
->attrs
);
2303 encode_type(typelib
, get_type_vt(type
), type
, &datatype1
, &datatype2
);
2304 size
= type_memsize_and_alignment(type
, &alignment
);
2308 msft_typeinfo
->typeinfo
->datatype1
= datatype1
;
2309 msft_typeinfo
->typeinfo
->size
= size
;
2310 msft_typeinfo
->typeinfo
->datatype2
= datatype2
;
2311 msft_typeinfo
->typeinfo
->typekind
|= (alignment
<< 11 | alignment
<< 6);
2314 /* avoid adding duplicate type definitions */
2316 tdef
->typelib_idx
= type
->typelib_idx
;
2319 static void add_coclass_typeinfo(msft_typelib_t
*typelib
, type_t
*cls
)
2321 msft_typeinfo_t
*msft_typeinfo
;
2323 int num_ifaces
= 0, offset
, i
;
2324 MSFT_RefRecord
*ref
, *first
= NULL
, *first_source
= NULL
;
2325 int have_default
= 0, have_default_source
= 0;
2327 typeref_list_t
*ifaces
;
2329 if (-1 < cls
->typelib_idx
)
2332 cls
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2333 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_COCLASS
, cls
->name
, cls
->attrs
);
2335 ifaces
= type_coclass_get_ifaces(cls
);
2336 if (ifaces
) LIST_FOR_EACH_ENTRY( iref
, ifaces
, typeref_t
, entry
) num_ifaces
++;
2338 offset
= msft_typeinfo
->typeinfo
->datatype1
= ctl2_alloc_segment(typelib
, MSFT_SEG_REFERENCES
,
2339 num_ifaces
* sizeof(*ref
), 0);
2342 if (ifaces
) LIST_FOR_EACH_ENTRY( iref
, ifaces
, typeref_t
, entry
) {
2343 if(iref
->type
->typelib_idx
== -1)
2344 add_interface_typeinfo(typelib
, iref
->type
);
2345 ref
= (MSFT_RefRecord
*) (typelib
->typelib_segment_data
[MSFT_SEG_REFERENCES
] + offset
+ i
* sizeof(*ref
));
2346 ref
->reftype
= typelib
->typelib_typeinfo_offsets
[iref
->type
->typelib_idx
];
2348 ref
->oCustData
= -1;
2350 if(i
< num_ifaces
- 1)
2351 ref
->onext
= offset
+ (i
+ 1) * sizeof(*ref
);
2353 if (iref
->attrs
) LIST_FOR_EACH_ENTRY( attr
, iref
->attrs
, const attr_t
, entry
) {
2354 switch(attr
->type
) {
2356 ref
->flags
|= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2358 case ATTR_DEFAULTVTABLE
:
2359 ref
->flags
|= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2361 case ATTR_RESTRICTED
:
2362 ref
->flags
|= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2365 ref
->flags
|= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2368 warning("add_coclass_typeinfo: unhandled attr %d\n", attr
->type
);
2371 if(ref
->flags
& 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2372 if(ref
->flags
& 0x2) /* IMPLTYPEFLAG_SOURCE */
2373 have_default_source
= 1;
2378 /* If the interface is non-restricted and we haven't already had one then
2379 remember it so that we can use it as a default later */
2380 if((ref
->flags
& 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2381 if(ref
->flags
& 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2391 /* If we haven't had a default interface, then set the default flags on the
2393 if(!have_default
&& first
)
2394 first
->flags
|= 0x1;
2395 if(!have_default_source
&& first_source
)
2396 first_source
->flags
|= 0x1;
2398 msft_typeinfo
->typeinfo
->cImplTypes
= num_ifaces
;
2399 msft_typeinfo
->typeinfo
->size
= pointer_size
;
2400 msft_typeinfo
->typeinfo
->typekind
|= 0x2200;
2403 static void add_module_typeinfo(msft_typelib_t
*typelib
, type_t
*module
)
2406 const statement_t
*stmt
;
2407 msft_typeinfo_t
*msft_typeinfo
;
2409 if (-1 < module
->typelib_idx
)
2412 module
->typelib_idx
= typelib
->typelib_header
.nrtypeinfos
;
2413 msft_typeinfo
= create_msft_typeinfo(typelib
, TKIND_MODULE
, module
->name
, module
->attrs
);
2414 msft_typeinfo
->typeinfo
->typekind
|= 0x0a00;
2416 STATEMENTS_FOR_EACH_FUNC( stmt
, module
->details
.module
->stmts
) {
2417 var_t
*func
= stmt
->u
.var
;
2418 if(add_func_desc(msft_typeinfo
, func
, idx
))
2422 msft_typeinfo
->typeinfo
->size
= idx
;
2425 static void add_type_typeinfo(msft_typelib_t
*typelib
, type_t
*type
)
2427 switch (type_get_type(type
)) {
2428 case TYPE_INTERFACE
:
2429 add_interface_typeinfo(typelib
, type
);
2432 case TYPE_ENCAPSULATED_UNION
:
2433 add_structure_typeinfo(typelib
, type
);
2436 add_enum_typeinfo(typelib
, type
);
2439 add_union_typeinfo(typelib
, type
);
2442 add_coclass_typeinfo(typelib
, type
);
2449 error("add_entry: unhandled type 0x%x for %s\n",
2450 type_get_type(type
), type
->name
);
2455 static void add_entry(msft_typelib_t
*typelib
, const statement_t
*stmt
)
2457 switch(stmt
->type
) {
2462 case STMT_DECLARATION
:
2463 /* not included in typelib */
2465 case STMT_IMPORTLIB
:
2466 /* not processed here */
2471 if (stmt
->u
.type_list
) LIST_FOR_EACH_ENTRY(ref
, stmt
->u
.type_list
, typeref_t
, entry
) {
2472 /* if the type is public then add the typedef, otherwise attempt
2473 * to add the aliased type */
2474 if (is_attr(ref
->type
->attrs
, ATTR_PUBLIC
))
2475 add_typedef_typeinfo(typelib
, ref
->type
);
2477 add_type_typeinfo(typelib
, type_alias_get_aliasee_type(ref
->type
));
2482 add_module_typeinfo(typelib
, stmt
->u
.type
);
2487 type_t
*type
= stmt
->u
.type
;
2488 add_type_typeinfo(typelib
, type
);
2494 static void set_name(msft_typelib_t
*typelib
)
2498 offset
= ctl2_alloc_name(typelib
, typelib
->typelib
->name
);
2499 if (offset
== -1) return;
2500 typelib
->typelib_header
.NameOffset
= offset
;
2504 static void set_version(msft_typelib_t
*typelib
)
2506 typelib
->typelib_header
.version
= get_attrv( typelib
->typelib
->attrs
, ATTR_VERSION
);
2509 static void set_guid(msft_typelib_t
*typelib
)
2511 MSFT_GuidEntry guidentry
= { {0}, -2, -1 };
2513 struct uuid
*ptr
= get_attrp( typelib
->typelib
->attrs
, ATTR_UUID
);
2515 if (ptr
) guidentry
.guid
= *ptr
;
2517 offset
= ctl2_alloc_guid(typelib
, &guidentry
);
2518 typelib
->typelib_header
.posguid
= offset
;
2523 static void set_doc_string(msft_typelib_t
*typelib
)
2525 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRING
);
2529 int offset
= ctl2_alloc_string(typelib
, str
);
2530 if (offset
!= -1) typelib
->typelib_header
.helpstring
= offset
;
2534 static void set_help_file_name(msft_typelib_t
*typelib
)
2536 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPFILE
);
2540 int offset
= ctl2_alloc_string(typelib
, str
);
2543 typelib
->typelib_header
.helpfile
= offset
;
2544 typelib
->typelib_header
.varflags
|= 0x10;
2549 static void set_help_context(msft_typelib_t
*typelib
)
2551 const expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPCONTEXT
);
2552 if (expr
) typelib
->typelib_header
.helpcontext
= expr
->cval
;
2555 static void set_help_string_dll(msft_typelib_t
*typelib
)
2557 char *str
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRINGDLL
);
2561 int offset
= ctl2_alloc_string(typelib
, str
);
2564 typelib
->help_string_dll_offset
= offset
;
2565 typelib
->typelib_header
.varflags
|= 0x100;
2570 static void set_help_string_context(msft_typelib_t
*typelib
)
2572 const expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_HELPSTRINGCONTEXT
);
2573 if (expr
) typelib
->typelib_header
.helpstringcontext
= expr
->cval
;
2576 static void set_lcid(msft_typelib_t
*typelib
)
2578 const expr_t
*lcid_expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_LIBLCID
);
2581 typelib
->typelib_header
.lcid
= lcid_expr
->cval
;
2582 typelib
->typelib_header
.lcid2
= lcid_expr
->cval
;
2586 static void set_lib_flags(msft_typelib_t
*typelib
)
2590 typelib
->typelib_header
.flags
= 0;
2591 if (!typelib
->typelib
->attrs
) return;
2592 LIST_FOR_EACH_ENTRY( attr
, typelib
->typelib
->attrs
, const attr_t
, entry
)
2594 switch(attr
->type
) {
2596 typelib
->typelib_header
.flags
|= 0x02; /* LIBFLAG_FCONTROL */
2599 typelib
->typelib_header
.flags
|= 0x04; /* LIBFLAG_FHIDDEN */
2601 case ATTR_RESTRICTED
:
2602 typelib
->typelib_header
.flags
|= 0x01; /* LIBFLAG_FRESTRICTED */
2611 static void ctl2_write_segment(msft_typelib_t
*typelib
, int segment
)
2613 if (typelib
->typelib_segment_data
[segment
])
2614 put_data(typelib
->typelib_segment_data
[segment
], typelib
->typelib_segdir
[segment
].length
);
2617 static void ctl2_finalize_typeinfos(msft_typelib_t
*typelib
, int filesize
)
2619 msft_typeinfo_t
*typeinfo
;
2621 for (typeinfo
= typelib
->typeinfos
; typeinfo
; typeinfo
= typeinfo
->next_typeinfo
) {
2622 typeinfo
->typeinfo
->memoffset
= filesize
;
2623 if (typeinfo
->func_data
)
2624 filesize
+= typeinfo
->func_data
[0] + ((typeinfo
->typeinfo
->cElement
& 0xffff) * 12);
2625 if (typeinfo
->var_data
)
2626 filesize
+= typeinfo
->var_data
[0] + (((typeinfo
->typeinfo
->cElement
>> 16) & 0xffff) * 12);
2627 if (typeinfo
->func_data
|| typeinfo
->var_data
)
2632 static int ctl2_finalize_segment(msft_typelib_t
*typelib
, int filepos
, int segment
)
2634 if (typelib
->typelib_segdir
[segment
].length
) {
2635 typelib
->typelib_segdir
[segment
].offset
= filepos
;
2637 typelib
->typelib_segdir
[segment
].offset
= -1;
2640 return typelib
->typelib_segdir
[segment
].length
;
2644 static void ctl2_write_typeinfos(msft_typelib_t
*typelib
)
2646 msft_typeinfo_t
*typeinfo
;
2649 for (typeinfo
= typelib
->typeinfos
; typeinfo
; typeinfo
= typeinfo
->next_typeinfo
) {
2650 if (!typeinfo
->func_data
&& !typeinfo
->var_data
) continue;
2652 if (typeinfo
->func_data
)
2653 typedata_size
= typeinfo
->func_data
[0];
2654 if (typeinfo
->var_data
)
2655 typedata_size
+= typeinfo
->var_data
[0];
2656 put_data(&typedata_size
, sizeof(int));
2657 if (typeinfo
->func_data
)
2658 put_data(typeinfo
->func_data
+ 1, typeinfo
->func_data
[0]);
2659 if (typeinfo
->var_data
)
2660 put_data(typeinfo
->var_data
+ 1, typeinfo
->var_data
[0]);
2661 if (typeinfo
->func_indices
)
2662 put_data(typeinfo
->func_indices
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2663 if (typeinfo
->var_indices
)
2664 put_data(typeinfo
->var_indices
, (typeinfo
->typeinfo
->cElement
>> 16) * 4);
2665 if (typeinfo
->func_names
)
2666 put_data(typeinfo
->func_names
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2667 if (typeinfo
->var_names
)
2668 put_data(typeinfo
->var_names
, (typeinfo
->typeinfo
->cElement
>> 16) * 4);
2669 if (typeinfo
->func_offsets
)
2670 put_data(typeinfo
->func_offsets
, (typeinfo
->typeinfo
->cElement
& 0xffff) * 4);
2671 if (typeinfo
->var_offsets
) {
2672 int add
= 0, i
, offset
;
2673 if(typeinfo
->func_data
)
2674 add
= typeinfo
->func_data
[0];
2675 for(i
= 0; i
< (typeinfo
->typeinfo
->cElement
>> 16); i
++) {
2676 offset
= typeinfo
->var_offsets
[i
];
2678 put_data(&offset
, 4);
2684 static void save_all_changes(msft_typelib_t
*typelib
)
2688 chat("save_all_changes(%p)\n", typelib
);
2690 filepos
= sizeof(MSFT_Header
) + sizeof(MSFT_SegDir
);
2691 if(typelib
->typelib_header
.varflags
& 0x100) filepos
+= 4; /* helpstringdll */
2692 filepos
+= typelib
->typelib_header
.nrtypeinfos
* 4;
2694 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_TYPEINFO
);
2695 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_GUIDHASH
);
2696 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_GUID
);
2697 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_REFERENCES
);
2698 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_IMPORTINFO
);
2699 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_IMPORTFILES
);
2700 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_NAMEHASH
);
2701 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_NAME
);
2702 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_STRING
);
2703 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_TYPEDESC
);
2704 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_ARRAYDESC
);
2705 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_CUSTDATA
);
2706 filepos
+= ctl2_finalize_segment(typelib
, filepos
, MSFT_SEG_CUSTDATAGUID
);
2708 ctl2_finalize_typeinfos(typelib
, filepos
);
2710 init_output_buffer();
2712 put_data(&typelib
->typelib_header
, sizeof(typelib
->typelib_header
));
2713 if(typelib
->typelib_header
.varflags
& 0x100)
2714 put_data(&typelib
->help_string_dll_offset
, sizeof(typelib
->help_string_dll_offset
));
2716 put_data(typelib
->typelib_typeinfo_offsets
, typelib
->typelib_header
.nrtypeinfos
* 4);
2717 put_data(&typelib
->typelib_segdir
, sizeof(typelib
->typelib_segdir
));
2718 ctl2_write_segment( typelib
, MSFT_SEG_TYPEINFO
);
2719 ctl2_write_segment( typelib
, MSFT_SEG_GUIDHASH
);
2720 ctl2_write_segment( typelib
, MSFT_SEG_GUID
);
2721 ctl2_write_segment( typelib
, MSFT_SEG_REFERENCES
);
2722 ctl2_write_segment( typelib
, MSFT_SEG_IMPORTINFO
);
2723 ctl2_write_segment( typelib
, MSFT_SEG_IMPORTFILES
);
2724 ctl2_write_segment( typelib
, MSFT_SEG_NAMEHASH
);
2725 ctl2_write_segment( typelib
, MSFT_SEG_NAME
);
2726 ctl2_write_segment( typelib
, MSFT_SEG_STRING
);
2727 ctl2_write_segment( typelib
, MSFT_SEG_TYPEDESC
);
2728 ctl2_write_segment( typelib
, MSFT_SEG_ARRAYDESC
);
2729 ctl2_write_segment( typelib
, MSFT_SEG_CUSTDATA
);
2730 ctl2_write_segment( typelib
, MSFT_SEG_CUSTDATAGUID
);
2732 ctl2_write_typeinfos(typelib
);
2734 if (strendswith( typelib_name
, ".res" )) /* create a binary resource file */
2736 char typelib_id
[13] = "#1";
2738 expr_t
*expr
= get_attrp( typelib
->typelib
->attrs
, ATTR_ID
);
2740 sprintf( typelib_id
, "#%d", expr
->cval
);
2741 add_output_to_resources( "TYPELIB", typelib_id
);
2742 if (strendswith( typelib_name
, "_t.res" )) /* add typelib registration */
2743 output_typelib_regscript( typelib
->typelib
);
2745 else flush_output_buffer( typelib_name
);
2748 int create_msft_typelib(typelib_t
*typelib
)
2750 msft_typelib_t
*msft
;
2752 const statement_t
*stmt
;
2755 char *time_override
;
2756 unsigned int version
= 7 << 24 | 555; /* 7.00.0555 */
2757 static const struct uuid midl_time_guid
= {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2758 static const struct uuid midl_version_guid
= {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2759 static const struct uuid midl_info_guid
= {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2760 char info_string
[128];
2762 msft
= xmalloc(sizeof(*msft
));
2763 memset(msft
, 0, sizeof(*msft
));
2764 msft
->typelib
= typelib
;
2766 ctl2_init_header(msft
);
2767 ctl2_init_segdir(msft
);
2769 msft
->typelib_header
.varflags
|= (pointer_size
== 8) ? SYS_WIN64
: SYS_WIN32
;
2772 * The following two calls return an offset or -1 if out of memory. We
2773 * specifically need an offset of 0, however, so...
2775 if (ctl2_alloc_segment(msft
, MSFT_SEG_GUIDHASH
, 0x80, 0x80)) { failed
= 1; }
2776 if (ctl2_alloc_segment(msft
, MSFT_SEG_NAMEHASH
, 0x200, 0x200)) { failed
= 1; }
2784 msft
->typelib_guidhash_segment
= (int *)msft
->typelib_segment_data
[MSFT_SEG_GUIDHASH
];
2785 msft
->typelib_namehash_segment
= (int *)msft
->typelib_segment_data
[MSFT_SEG_NAMEHASH
];
2787 memset(msft
->typelib_guidhash_segment
, 0xff, 0x80);
2788 memset(msft
->typelib_namehash_segment
, 0xff, 0x200);
2790 set_lib_flags(msft
);
2792 set_help_file_name(msft
);
2793 set_doc_string(msft
);
2797 set_help_context(msft
);
2798 set_help_string_dll(msft
);
2799 set_help_string_context(msft
);
2801 if (typelib
->attrs
) LIST_FOR_EACH_ENTRY( attr
, typelib
->attrs
, const attr_t
, entry
) {
2802 switch(attr
->type
) {
2804 set_custdata_attr(msft
, attr
->u
.pval
, &msft
->typelib_header
.CustomDataOffset
);
2811 /* midl adds two sets of custom data to the library: the current unix time
2812 and midl's version number */
2813 time_override
= getenv( "WIDL_TIME_OVERRIDE");
2814 cur_time
= time_override
? atol( time_override
) : time(NULL
);
2815 sprintf(info_string
, "Created by WIDL version %s at %s", PACKAGE_VERSION
, ctime(&cur_time
));
2816 set_custdata(msft
, &midl_info_guid
, VT_BSTR
, info_string
, &msft
->typelib_header
.CustomDataOffset
);
2817 set_custdata(msft
, &midl_time_guid
, VT_UI4
, &cur_time
, &msft
->typelib_header
.CustomDataOffset
);
2818 set_custdata(msft
, &midl_version_guid
, VT_UI4
, &version
, &msft
->typelib_header
.CustomDataOffset
);
2821 LIST_FOR_EACH_ENTRY( stmt
, typelib
->stmts
, const statement_t
, entry
)
2822 add_entry(msft
, stmt
);
2824 save_all_changes(msft
);