widl: Store the aliasee as a decl_spec_t structure.
[wine.git] / tools / widl / write_msft.c
blob420404af6dab0ba0463c15100ade2f88dcb551f3
1 /*
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 * --------------------------------------------------------------------------------------
22 * Known problems:
24 * Badly incomplete.
26 * Only works on little-endian systems.
30 #include "config.h"
31 #include "wine/port.h"
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <time.h>
40 #define NONAMELESSUNION
42 #include "winerror.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winnls.h"
47 #include "widl.h"
48 #include "typelib.h"
49 #include "typelib_struct.h"
50 #include "utils.h"
51 #include "header.h"
52 #include "hash.h"
53 #include "typetree.h"
54 #include "parser.h"
55 #include "typegen.h"
57 enum MSFT_segment_index {
58 MSFT_SEG_TYPEINFO = 0, /* type information */
59 MSFT_SEG_IMPORTINFO, /* import information */
60 MSFT_SEG_IMPORTFILES, /* import filenames */
61 MSFT_SEG_REFERENCES, /* references (?) */
62 MSFT_SEG_GUIDHASH, /* hash table for guids? */
63 MSFT_SEG_GUID, /* guid storage */
64 MSFT_SEG_NAMEHASH, /* hash table for names */
65 MSFT_SEG_NAME, /* name storage */
66 MSFT_SEG_STRING, /* string storage */
67 MSFT_SEG_TYPEDESC, /* type descriptions */
68 MSFT_SEG_ARRAYDESC, /* array descriptions */
69 MSFT_SEG_CUSTDATA, /* custom data */
70 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
71 MSFT_SEG_UNKNOWN, /* ??? */
72 MSFT_SEG_UNKNOWN2, /* ??? */
73 MSFT_SEG_MAX /* total number of segments */
76 typedef struct tagMSFT_ImpFile {
77 int guid;
78 LCID lcid;
79 int version;
80 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
81 } MSFT_ImpFile;
83 typedef struct _msft_typelib_t
85 typelib_t *typelib;
86 MSFT_Header typelib_header;
87 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
88 unsigned char *typelib_segment_data[MSFT_SEG_MAX];
89 int typelib_segment_block_length[MSFT_SEG_MAX];
91 INT typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
93 INT *typelib_namehash_segment;
94 INT *typelib_guidhash_segment;
96 INT help_string_dll_offset;
98 struct _msft_typeinfo_t *typeinfos;
99 struct _msft_typeinfo_t *last_typeinfo;
100 } msft_typelib_t;
102 typedef struct _msft_typeinfo_t
104 msft_typelib_t *typelib;
105 MSFT_TypeInfoBase *typeinfo;
107 int typekind;
109 unsigned int var_data_allocated;
110 int *var_data;
112 unsigned int func_data_allocated;
113 int *func_data;
115 int vars_allocated;
116 int *var_indices;
117 int *var_names;
118 int *var_offsets;
120 int funcs_allocated;
121 int *func_indices;
122 int *func_names;
123 int *func_offsets;
125 int datawidth;
127 struct _msft_typeinfo_t *next_typeinfo;
128 } msft_typeinfo_t;
132 /*================== Internal functions ===================================*/
134 /****************************************************************************
135 * ctl2_init_header
137 * Initializes the type library header of a new typelib.
139 static void ctl2_init_header(
140 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
142 typelib->typelib_header.magic1 = 0x5446534d;
143 typelib->typelib_header.magic2 = 0x00010002;
144 typelib->typelib_header.posguid = -1;
145 typelib->typelib_header.lcid = 0x0409;
146 typelib->typelib_header.lcid2 = 0x0;
147 typelib->typelib_header.varflags = 0x40;
148 typelib->typelib_header.version = 0;
149 typelib->typelib_header.flags = 0;
150 typelib->typelib_header.nrtypeinfos = 0;
151 typelib->typelib_header.helpstring = -1;
152 typelib->typelib_header.helpstringcontext = 0;
153 typelib->typelib_header.helpcontext = 0;
154 typelib->typelib_header.nametablecount = 0;
155 typelib->typelib_header.nametablechars = 0;
156 typelib->typelib_header.NameOffset = -1;
157 typelib->typelib_header.helpfile = -1;
158 typelib->typelib_header.CustomDataOffset = -1;
159 typelib->typelib_header.res44 = 0x20;
160 typelib->typelib_header.res48 = 0x80;
161 typelib->typelib_header.dispatchpos = -1;
162 typelib->typelib_header.nimpinfos = 0;
165 /****************************************************************************
166 * ctl2_init_segdir
168 * Initializes the segment directory of a new typelib.
170 static void ctl2_init_segdir(
171 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
173 int i;
174 MSFT_pSeg *segdir;
176 segdir = &typelib->typelib_segdir[MSFT_SEG_TYPEINFO];
178 for (i = 0; i < MSFT_SEG_MAX; i++) {
179 segdir[i].offset = -1;
180 segdir[i].length = 0;
181 segdir[i].res08 = -1;
182 segdir[i].res0c = 0x0f;
186 /****************************************************************************
187 * ctl2_hash_guid
189 * Generates a hash key from a GUID.
191 * RETURNS
193 * The hash key for the GUID.
195 static int ctl2_hash_guid(
196 REFGUID guid) /* [I] The guid to hash. */
198 int hash;
199 int i;
201 hash = 0;
202 for (i = 0; i < 8; i ++) {
203 hash ^= ((const short *)guid)[i];
206 return hash & 0x1f;
209 /****************************************************************************
210 * ctl2_find_guid
212 * Locates a guid in a type library.
214 * RETURNS
216 * The offset into the GUID segment of the guid, or -1 if not found.
218 static int ctl2_find_guid(
219 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
220 int hash_key, /* [I] The hash key for the guid. */
221 REFGUID guid) /* [I] The guid to find. */
223 int offset;
224 MSFT_GuidEntry *guidentry;
226 offset = typelib->typelib_guidhash_segment[hash_key];
227 while (offset != -1) {
228 guidentry = (MSFT_GuidEntry *)&typelib->typelib_segment_data[MSFT_SEG_GUID][offset];
230 if (!memcmp(guidentry, guid, sizeof(GUID))) return offset;
232 offset = guidentry->next_hash;
235 return offset;
238 /****************************************************************************
239 * ctl2_find_name
241 * Locates a name in a type library.
243 * RETURNS
245 * The offset into the NAME segment of the name, or -1 if not found.
247 * NOTES
249 * The name must be encoded as with ctl2_encode_name().
251 static int ctl2_find_name(
252 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
253 char *name) /* [I] The encoded name to find. */
255 int offset;
256 int *namestruct;
258 offset = typelib->typelib_namehash_segment[name[2] & 0x7f];
259 while (offset != -1) {
260 namestruct = (int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][offset];
262 if (!((namestruct[2] ^ *((int *)name)) & 0xffff00ff)) {
263 /* hash codes and lengths match, final test */
264 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
267 /* move to next item in hash bucket */
268 offset = namestruct[1];
271 return offset;
274 /****************************************************************************
275 * ctl2_encode_name
277 * Encodes a name string to a form suitable for storing into a type library
278 * or comparing to a name stored in a type library.
280 * RETURNS
282 * The length of the encoded name, including padding and length+hash fields.
284 * NOTES
286 * Will throw an exception if name or result are NULL. Is not multithread
287 * safe in the slightest.
289 static int ctl2_encode_name(
290 msft_typelib_t *typelib, /* [I] The typelib to operate against (used for LCID only). */
291 const char *name, /* [I] The name string to encode. */
292 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
294 int length;
295 static char converted_name[0x104];
296 int offset;
297 int value;
299 length = strlen(name);
300 memcpy(converted_name + 4, name, length);
302 converted_name[length + 4] = 0;
305 value = lhash_val_of_name_sys(typelib->typelib_header.varflags & 0x0f, typelib->typelib_header.lcid, converted_name + 4);
307 #ifdef WORDS_BIGENDIAN
308 converted_name[3] = length & 0xff;
309 converted_name[2] = 0x00;
310 converted_name[1] = value;
311 converted_name[0] = value >> 8;
312 #else
313 converted_name[0] = length & 0xff;
314 converted_name[1] = 0x00;
315 converted_name[2] = value;
316 converted_name[3] = value >> 8;
317 #endif
319 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
321 *result = converted_name;
323 return (length + 7) & ~3;
326 /****************************************************************************
327 * ctl2_encode_string
329 * Encodes a string to a form suitable for storing into a type library or
330 * comparing to a string stored in a type library.
332 * RETURNS
334 * The length of the encoded string, including padding and length fields.
336 * NOTES
338 * Will throw an exception if string or result are NULL. Is not multithread
339 * safe in the slightest.
341 static int ctl2_encode_string(
342 const char *string, /* [I] The string to encode. */
343 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
345 int length;
346 static char converted_string[0x104];
347 int offset;
349 length = strlen(string);
350 memcpy(converted_string + 2, string, length);
352 #ifdef WORDS_BIGENDIAN
353 converted_string[1] = length & 0xff;
354 converted_string[0] = (length >> 8) & 0xff;
355 #else
356 converted_string[0] = length & 0xff;
357 converted_string[1] = (length >> 8) & 0xff;
358 #endif
360 if(length < 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
361 for(offset = 0; offset < 4; offset++)
362 converted_string[length + offset + 2] = 0x57;
363 length += 4;
365 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
367 *result = converted_string;
369 return (length + 5) & ~3;
372 /****************************************************************************
373 * ctl2_alloc_segment
375 * Allocates memory from a segment in a type library.
377 * RETURNS
379 * Success: The offset within the segment of the new data area.
381 * BUGS
383 * Does not (yet) handle the case where the allocated segment memory needs to grow.
385 static int ctl2_alloc_segment(
386 msft_typelib_t *typelib, /* [I] The type library in which to allocate. */
387 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
388 int size, /* [I] The amount to allocate. */
389 int block_size) /* [I] Initial allocation block size, or 0 for default. */
391 int offset;
393 if(!typelib->typelib_segment_data[segment]) {
394 if (!block_size) block_size = 0x2000;
396 typelib->typelib_segment_block_length[segment] = block_size;
397 typelib->typelib_segment_data[segment] = xmalloc(block_size);
398 if (!typelib->typelib_segment_data[segment]) return -1;
399 memset(typelib->typelib_segment_data[segment], 0x57, block_size);
402 while ((typelib->typelib_segdir[segment].length + size) > typelib->typelib_segment_block_length[segment]) {
403 unsigned char *block;
405 block_size = typelib->typelib_segment_block_length[segment];
406 block = xrealloc(typelib->typelib_segment_data[segment], block_size << 1);
408 if (segment == MSFT_SEG_TYPEINFO) {
409 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
410 msft_typeinfo_t *typeinfo;
412 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
413 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - typelib->typelib_segment_data[segment]];
417 memset(block + block_size, 0x57, block_size);
418 typelib->typelib_segment_block_length[segment] = block_size << 1;
419 typelib->typelib_segment_data[segment] = block;
422 offset = typelib->typelib_segdir[segment].length;
423 typelib->typelib_segdir[segment].length += size;
425 return offset;
428 /****************************************************************************
429 * ctl2_alloc_typeinfo
431 * Allocates and initializes a typeinfo structure in a type library.
433 * RETURNS
435 * Success: The offset of the new typeinfo.
436 * Failure: -1 (this is invariably an out of memory condition).
438 static int ctl2_alloc_typeinfo(
439 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
440 int nameoffset) /* [I] The offset of the name for this typeinfo. */
442 int offset;
443 MSFT_TypeInfoBase *typeinfo;
445 offset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
447 typelib->typelib_typeinfo_offsets[typelib->typelib_header.nrtypeinfos++] = offset;
449 typeinfo = (void *)(typelib->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
451 typeinfo->typekind = (typelib->typelib_header.nrtypeinfos - 1) << 16;
452 typeinfo->memoffset = -1; /* should be EOF if no elements */
453 typeinfo->res2 = 0;
454 typeinfo->res3 = -1;
455 typeinfo->res4 = 3;
456 typeinfo->res5 = 0;
457 typeinfo->cElement = 0;
458 typeinfo->res7 = 0;
459 typeinfo->res8 = 0;
460 typeinfo->res9 = 0;
461 typeinfo->resA = 0;
462 typeinfo->posguid = -1;
463 typeinfo->flags = 0;
464 typeinfo->NameOffset = nameoffset;
465 typeinfo->version = 0;
466 typeinfo->docstringoffs = -1;
467 typeinfo->helpstringcontext = 0;
468 typeinfo->helpcontext = 0;
469 typeinfo->oCustData = -1;
470 typeinfo->cbSizeVft = 0;
471 typeinfo->cImplTypes = 0;
472 typeinfo->size = 0;
473 typeinfo->datatype1 = -1;
474 typeinfo->datatype2 = 0;
475 typeinfo->res18 = 0;
476 typeinfo->res19 = -1;
478 return offset;
481 /****************************************************************************
482 * ctl2_alloc_guid
484 * Allocates and initializes a GUID structure in a type library. Also updates
485 * the GUID hash table as needed.
487 * RETURNS
489 * Success: The offset of the new GUID.
491 static int ctl2_alloc_guid(
492 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
493 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
495 int offset;
496 MSFT_GuidEntry *guid_space;
497 int hash_key;
499 chat("adding uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
500 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
501 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
502 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
504 hash_key = ctl2_hash_guid(&guid->guid);
506 offset = ctl2_find_guid(typelib, hash_key, &guid->guid);
507 if (offset != -1)
509 if (is_warning_enabled(2368))
510 warning("duplicate uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
511 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
512 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
513 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
514 return -1;
517 offset = ctl2_alloc_segment(typelib, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
519 guid_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_GUID] + offset);
520 *guid_space = *guid;
522 guid_space->next_hash = typelib->typelib_guidhash_segment[hash_key];
523 typelib->typelib_guidhash_segment[hash_key] = offset;
525 return offset;
528 /****************************************************************************
529 * ctl2_alloc_name
531 * Allocates and initializes a name within a type library. Also updates the
532 * name hash table as needed.
534 * RETURNS
536 * Success: The offset within the segment of the new name.
537 * Failure: -1 (this is invariably an out of memory condition).
539 static int ctl2_alloc_name(
540 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
541 const char *name) /* [I] The name to store. */
543 int length;
544 int offset;
545 MSFT_NameIntro *name_space;
546 char *encoded_name;
548 length = ctl2_encode_name(typelib, name, &encoded_name);
550 offset = ctl2_find_name(typelib, encoded_name);
551 if (offset != -1) return offset;
553 offset = ctl2_alloc_segment(typelib, MSFT_SEG_NAME, length + 8, 0);
555 name_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_NAME] + offset);
556 name_space->hreftype = -1;
557 name_space->next_hash = -1;
558 memcpy(&name_space->namelen, encoded_name, length);
560 if (typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
561 name_space->next_hash = typelib->typelib_namehash_segment[encoded_name[2] & 0x7f];
563 typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
565 typelib->typelib_header.nametablecount += 1;
566 typelib->typelib_header.nametablechars += *encoded_name;
568 return offset;
571 /****************************************************************************
572 * ctl2_alloc_string
574 * Allocates and initializes a string in a type library.
576 * RETURNS
578 * Success: The offset within the segment of the new string.
579 * Failure: -1 (this is invariably an out of memory condition).
581 static int ctl2_alloc_string(
582 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
583 const char *string) /* [I] The string to store. */
585 int length;
586 int offset;
587 unsigned char *string_space;
588 char *encoded_string;
590 length = ctl2_encode_string(string, &encoded_string);
592 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_STRING].length;
593 offset += (((typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
594 typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
595 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
598 offset = ctl2_alloc_segment(typelib, MSFT_SEG_STRING, length, 0);
600 string_space = typelib->typelib_segment_data[MSFT_SEG_STRING] + offset;
601 memcpy(string_space, encoded_string, length);
603 return offset;
606 /****************************************************************************
607 * alloc_msft_importinfo
609 * Allocates and initializes an import information structure in a type library.
611 * RETURNS
613 * Success: The offset of the new importinfo.
614 * Failure: -1 (this is invariably an out of memory condition).
616 static int alloc_msft_importinfo(
617 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
618 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
620 int offset;
621 MSFT_ImpInfo *impinfo_space;
623 for (offset = 0;
624 offset < typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
625 offset += sizeof(MSFT_ImpInfo)) {
626 if (!memcmp(&(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][offset]),
627 impinfo, sizeof(MSFT_ImpInfo))) {
628 return offset;
632 impinfo->flags |= typelib->typelib_header.nimpinfos++;
634 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
636 impinfo_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
637 *impinfo_space = *impinfo;
639 return offset;
642 /****************************************************************************
643 * alloc_importfile
645 * Allocates and initializes an import file definition in a type library.
647 * RETURNS
649 * Success: The offset of the new importinfo.
650 * Failure: -1 (this is invariably an out of memory condition).
652 static int alloc_importfile(
653 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
654 int guidoffset, /* [I] The offset to the GUID for the imported library. */
655 int major_version, /* [I] The major version number of the imported library. */
656 int minor_version, /* [I] The minor version number of the imported library. */
657 const char *filename) /* [I] The filename of the imported library. */
659 int length;
660 int offset;
661 MSFT_ImpFile *importfile;
662 char *encoded_string;
664 length = ctl2_encode_string(filename, &encoded_string);
666 encoded_string[0] <<= 2;
667 encoded_string[0] |= 1;
669 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
670 offset += (((typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
671 typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 0xc) {
672 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
675 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
677 importfile = (MSFT_ImpFile *)&typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
678 importfile->guid = guidoffset;
679 importfile->lcid = typelib->typelib_header.lcid2;
680 importfile->version = major_version | (minor_version << 16);
681 memcpy(&importfile->filename, encoded_string, length);
683 return offset;
686 static void alloc_importinfo(msft_typelib_t *typelib, importinfo_t *importinfo)
688 importlib_t *importlib = importinfo->importlib;
690 chat("alloc_importinfo: %s\n", importinfo->name);
692 if(!importlib->allocated) {
693 MSFT_GuidEntry guid;
694 int guid_idx;
696 chat("allocating importlib %s\n", importlib->name);
698 importlib->allocated = -1;
700 memcpy(&guid.guid, &importlib->guid, sizeof(GUID));
701 guid.hreftype = 2;
703 guid_idx = ctl2_alloc_guid(typelib, &guid);
705 importlib->offset = alloc_importfile(typelib, guid_idx, importlib->version & 0xffff,
706 importlib->version >> 16, importlib->name);
709 if(importinfo->offset == -1 || !(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID)) {
710 MSFT_ImpInfo impinfo;
712 impinfo.flags = importinfo->flags;
713 impinfo.oImpFile = importlib->offset;
715 if(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID) {
716 MSFT_GuidEntry guid;
718 guid.hreftype = 0;
719 memcpy(&guid.guid, &importinfo->guid, sizeof(GUID));
721 impinfo.oGuid = ctl2_alloc_guid(typelib, &guid);
723 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
725 typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo.oGuid+sizeof(GUID)]
726 = importinfo->offset+1;
728 if(!strcmp(importinfo->name, "IDispatch"))
729 typelib->typelib_header.dispatchpos = importinfo->offset+1;
730 }else {
731 impinfo.oGuid = importinfo->id;
732 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
737 static importinfo_t *find_importinfo(msft_typelib_t *typelib, const char *name)
739 importlib_t *importlib;
740 int i;
742 chat("search importlib %s\n", name);
744 if(!name)
745 return NULL;
747 LIST_FOR_EACH_ENTRY( importlib, &typelib->typelib->importlibs, importlib_t, entry )
749 for(i=0; i < importlib->ntypeinfos; i++) {
750 if(!strcmp(name, importlib->importinfos[i].name)) {
751 chat("Found %s in importlib.\n", name);
752 return importlib->importinfos+i;
757 return NULL;
760 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure);
761 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface);
762 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration);
763 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion);
764 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls);
765 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
768 /****************************************************************************
769 * encode_type
771 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
772 * segments as needed.
774 * RETURNS
776 * Success: 0.
777 * Failure: -1.
779 static int encode_type(
780 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
781 int vt, /* [I] vt to encode */
782 type_t *type, /* [I] type */
783 int *encoded_type, /* [O] The encoded type description. */
784 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
786 int default_type;
787 int scratch;
788 int typeoffset;
789 int *typedata;
790 int target_type;
791 int child_size = 0;
793 chat("encode_type vt %d type %p\n", vt, type);
795 default_type = 0x80000000 | (vt << 16) | vt;
796 if (!decoded_size) decoded_size = &scratch;
798 *decoded_size = 0;
800 switch (vt) {
801 case VT_I1:
802 case VT_UI1:
803 *encoded_type = default_type;
804 break;
806 case VT_INT:
807 *encoded_type = 0x80000000 | (VT_I4 << 16) | VT_INT;
808 break;
810 case VT_UINT:
811 *encoded_type = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
812 break;
814 case VT_UI2:
815 case VT_I2:
816 case VT_BOOL:
817 *encoded_type = default_type;
818 break;
820 case VT_I4:
821 case VT_UI4:
822 case VT_R4:
823 case VT_ERROR:
824 case VT_HRESULT:
825 *encoded_type = default_type;
826 break;
828 case VT_R8:
829 case VT_I8:
830 case VT_UI8:
831 *encoded_type = default_type;
832 break;
834 case VT_CY:
835 case VT_DATE:
836 *encoded_type = default_type;
837 break;
839 case VT_DECIMAL:
840 *encoded_type = default_type;
841 break;
843 case VT_VOID:
844 *encoded_type = 0x80000000 | (VT_EMPTY << 16) | vt;
845 break;
847 case VT_UNKNOWN:
848 case VT_DISPATCH:
849 case VT_BSTR:
850 *encoded_type = default_type;
851 break;
853 case VT_VARIANT:
854 *encoded_type = default_type;
855 break;
857 case VT_LPSTR:
858 case VT_LPWSTR:
859 *encoded_type = 0xfffe0000 | vt;
860 break;
862 case VT_PTR:
864 int next_vt;
865 for(next_vt = 0; is_ptr(type); type = type_pointer_get_ref_type(type)) {
866 next_vt = get_type_vt(type_pointer_get_ref_type(type));
867 if (next_vt != 0)
868 break;
870 /* if no type found then it must be void */
871 if (next_vt == 0)
872 next_vt = VT_VOID;
874 encode_type(typelib, next_vt, type_pointer_get_ref_type(type),
875 &target_type, &child_size);
876 /* these types already have an implicit pointer, so we don't need to
877 * add another */
878 if(next_vt == VT_DISPATCH || next_vt == VT_UNKNOWN) {
879 chat("encode_type: skipping ptr\n");
880 *encoded_type = target_type;
881 *decoded_size = child_size;
882 break;
885 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
886 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
887 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
890 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
891 int mix_field;
893 if (target_type & 0x80000000) {
894 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
895 } else {
896 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
897 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
900 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
901 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
903 typedata[0] = (mix_field << 16) | VT_PTR;
904 typedata[1] = target_type;
907 *encoded_type = typeoffset;
909 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
910 break;
913 case VT_SAFEARRAY:
915 type_t *element_type = type_alias_get_aliasee_type(type_array_get_element_type(type));
916 int next_vt = get_type_vt(element_type);
918 encode_type(typelib, next_vt, type_alias_get_aliasee_type(type_array_get_element_type(type)),
919 &target_type, &child_size);
921 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
922 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
923 if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
926 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
927 int mix_field;
929 if (target_type & 0x80000000) {
930 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
931 } else {
932 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
933 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
936 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
937 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
939 typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
940 typedata[1] = target_type;
943 *encoded_type = typeoffset;
945 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
946 break;
949 case VT_USERDEFINED:
951 importinfo_t *importinfo;
952 int typeinfo_offset;
954 if (type->typelib_idx > -1)
956 chat("encode_type: VT_USERDEFINED - found already defined type %s at %d\n",
957 type->name, type->typelib_idx);
958 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
960 else if ((importinfo = find_importinfo(typelib, type->name)))
962 chat("encode_type: VT_USERDEFINED - found imported type %s in %s\n",
963 type->name, importinfo->importlib->name);
964 alloc_importinfo(typelib, importinfo);
965 typeinfo_offset = importinfo->offset | 0x1;
967 else
969 /* typedef'd types without public attribute aren't included in the typelib */
970 while (type_is_alias(type) && !is_attr(type->attrs, ATTR_PUBLIC))
971 type = type_alias_get_aliasee_type(type);
973 chat("encode_type: VT_USERDEFINED - adding new type %s, real type %d\n",
974 type->name, type_get_type(type));
976 switch (type_get_type(type))
978 case TYPE_STRUCT:
979 add_structure_typeinfo(typelib, type);
980 break;
981 case TYPE_INTERFACE:
982 add_interface_typeinfo(typelib, type);
983 break;
984 case TYPE_ENUM:
985 add_enum_typeinfo(typelib, type);
986 break;
987 case TYPE_UNION:
988 add_union_typeinfo(typelib, type);
989 break;
990 case TYPE_COCLASS:
991 add_coclass_typeinfo(typelib, type);
992 break;
993 default:
994 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
995 type_get_type(type));
998 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
1000 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1001 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1002 if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == typeinfo_offset)) break;
1005 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1006 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1007 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1009 typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1010 typedata[1] = typeinfo_offset;
1013 *encoded_type = typeoffset;
1014 break;
1017 default:
1018 error("encode_type: unrecognized type %d.\n", vt);
1019 *encoded_type = default_type;
1020 break;
1023 return 0;
1026 static void dump_type(type_t *t)
1028 chat("dump_type: %p name %s type %d attrs %p\n", t, t->name, type_get_type(t), t->attrs);
1031 static int encode_var(
1032 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
1033 type_t *type, /* [I] The type description to encode. */
1034 var_t *var, /* [I] The var to encode. */
1035 int *encoded_type, /* [O] The encoded type description. */
1036 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1038 int typeoffset;
1039 int *typedata;
1040 int target_type;
1041 int child_size;
1042 int vt;
1043 int scratch;
1045 if (!decoded_size) decoded_size = &scratch;
1046 *decoded_size = 0;
1048 chat("encode_var: var %p type %p type->name %s\n",
1049 var, type, type->name ? type->name : "NULL");
1051 if (is_array(type) && !type_array_is_decl_as_ptr(type)) {
1052 int num_dims, elements = 1, arrayoffset;
1053 type_t *atype;
1054 int *arraydata;
1056 num_dims = 0;
1057 for (atype = type;
1058 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1059 atype = type_array_get_element_type(atype))
1060 ++num_dims;
1062 chat("array with %d dimensions\n", num_dims);
1063 encode_var(typelib, atype, var, &target_type, NULL);
1064 arrayoffset = ctl2_alloc_segment(typelib, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1065 arraydata = (void *)&typelib->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1067 arraydata[0] = target_type;
1068 arraydata[1] = num_dims;
1069 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1071 arraydata += 2;
1072 for (atype = type;
1073 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1074 atype = type_array_get_element_type(atype))
1076 arraydata[0] = type_array_get_dim(atype);
1077 arraydata[1] = 0;
1078 arraydata += 2;
1079 elements *= type_array_get_dim(atype);
1082 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1083 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1085 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1086 typedata[1] = arrayoffset;
1088 *encoded_type = typeoffset;
1089 *decoded_size = 20 /*sizeof(ARRAYDESC)*/ + (num_dims - 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1090 return 0;
1093 vt = get_type_vt(type);
1094 if (vt == VT_PTR) {
1095 type_t *ref = is_ptr(type) ?
1096 type_pointer_get_ref_type(type) : type_array_get_element_type(type);
1097 int skip_ptr = encode_var(typelib, ref, var, &target_type, &child_size);
1099 if(skip_ptr == 2) {
1100 chat("encode_var: skipping ptr\n");
1101 *encoded_type = target_type;
1102 *decoded_size = child_size;
1103 return 0;
1106 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1107 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1108 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1111 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1112 int mix_field;
1114 if (target_type & 0x80000000) {
1115 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1116 } else if (get_type_vt(ref) == VT_SAFEARRAY) {
1117 type_t *element_type = type_alias_get_aliasee_type(type_array_get_element_type(ref));
1118 mix_field = get_type_vt(element_type) | VT_ARRAY | VT_BYREF;
1119 } else {
1120 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1121 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1124 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1125 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1127 typedata[0] = (mix_field << 16) | VT_PTR;
1128 typedata[1] = target_type;
1131 *encoded_type = typeoffset;
1133 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1134 return 0;
1137 dump_type(type);
1139 encode_type(typelib, vt, type, encoded_type, decoded_size);
1140 /* these types already have an implicit pointer, so we don't need to
1141 * add another */
1142 if(vt == VT_DISPATCH || vt == VT_UNKNOWN) return 2;
1143 return 0;
1146 static unsigned int get_ulong_val(unsigned int val, int vt)
1148 switch(vt) {
1149 case VT_I2:
1150 case VT_BOOL:
1151 case VT_UI2:
1152 return val & 0xffff;
1153 case VT_I1:
1154 case VT_UI1:
1155 return val & 0xff;
1158 return val;
1161 static void write_int_value(msft_typelib_t *typelib, int *out, int vt, int value)
1163 const unsigned int lv = get_ulong_val(value, vt);
1164 if ((lv & 0x3ffffff) == lv) {
1165 *out = 0x80000000;
1166 *out |= vt << 26;
1167 *out |= lv;
1168 } else {
1169 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1170 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1171 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &value, 4);
1172 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1173 *out = offset;
1177 static void write_string_value(msft_typelib_t *typelib, int *out, const char *value)
1179 int len = strlen(value), seg_len = (len + 6 + 3) & ~0x3;
1180 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1181 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = VT_BSTR;
1182 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &len, sizeof(len));
1183 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1184 len += 6;
1185 while(len < seg_len) {
1186 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1187 len++;
1189 *out = offset;
1192 static void write_default_value(msft_typelib_t *typelib, type_t *type, expr_t *expr, int *out)
1194 int vt;
1196 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT) {
1197 if (get_type_vt(type) != VT_BSTR)
1198 error("string default value applied to non-string type\n");
1199 chat("default value '%s'\n", expr->u.sval);
1200 write_string_value(typelib, out, expr->u.sval);
1201 return;
1204 if (type_get_type(type) == TYPE_ENUM) {
1205 vt = VT_I4;
1206 } else if (is_ptr(type)) {
1207 vt = get_type_vt(type_pointer_get_ref_type(type));
1208 if (vt == VT_USERDEFINED)
1209 vt = VT_I4;
1210 if (expr->cval)
1211 warning("non-null pointer default value\n");
1212 } else {
1213 vt = get_type_vt(type);
1214 switch(vt) {
1215 case VT_I2:
1216 case VT_I4:
1217 case VT_R4:
1218 case VT_BOOL:
1219 case VT_I1:
1220 case VT_UI1:
1221 case VT_UI2:
1222 case VT_UI4:
1223 case VT_INT:
1224 case VT_UINT:
1225 case VT_HRESULT:
1226 break;
1227 default:
1228 warning("can't write value of type %d yet\n", vt);
1229 return;
1233 write_int_value(typelib, out, vt, expr->cval);
1236 static HRESULT set_custdata(msft_typelib_t *typelib, REFGUID guid,
1237 int vt, const void *value, int *offset)
1239 MSFT_GuidEntry guidentry;
1240 int guidoffset;
1241 int custoffset;
1242 int *custdata;
1243 int data_out;
1245 guidentry.guid = *guid;
1247 guidentry.hreftype = -1;
1248 guidentry.next_hash = -1;
1250 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1251 if(vt == VT_BSTR)
1252 write_string_value(typelib, &data_out, value);
1253 else
1254 write_int_value(typelib, &data_out, vt, *(int*)value);
1256 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1258 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1259 custdata[0] = guidoffset;
1260 custdata[1] = data_out;
1261 custdata[2] = *offset;
1262 *offset = custoffset;
1264 return S_OK;
1267 static HRESULT add_func_desc(msft_typeinfo_t* typeinfo, var_t *func, int index)
1269 int offset, name_offset;
1270 int *typedata, typedata_size;
1271 int i, id, next_idx;
1272 int decoded_size, extra_attr = 0;
1273 int num_params = 0, num_optional = 0, num_defaults = 0;
1274 var_t *arg;
1275 unsigned char *namedata;
1276 const attr_t *attr;
1277 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1278 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1279 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1280 int entry = -1, entry_is_ord = 0;
1281 int lcid_retval_count = 0;
1283 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1285 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1287 switch(typeinfo->typekind) {
1288 case TKIND_DISPATCH:
1289 funckind = 0x4; /* FUNC_DISPATCH */
1290 break;
1291 case TKIND_MODULE:
1292 funckind = 0x3; /* FUNC_STATIC */
1293 break;
1294 default:
1295 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1296 break;
1299 if (is_local( func->attrs )) {
1300 chat("add_func_desc: skipping local function\n");
1301 return S_FALSE;
1304 if (type_function_get_args(func->declspec.type))
1305 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1307 num_params++;
1308 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1309 if(attr->type == ATTR_DEFAULTVALUE)
1310 num_defaults++;
1311 else if(attr->type == ATTR_OPTIONAL)
1312 num_optional++;
1316 chat("add_func_desc: num of params %d\n", num_params);
1318 name_offset = ctl2_alloc_name(typeinfo->typelib, func->name);
1320 if (func->attrs) LIST_FOR_EACH_ENTRY( attr, func->attrs, const attr_t, entry ) {
1321 expr_t *expr = attr->u.pval;
1322 switch(attr->type) {
1323 case ATTR_BINDABLE:
1324 funcflags |= 0x4; /* FUNCFLAG_FBINDABLE */
1325 break;
1326 case ATTR_DEFAULTBIND:
1327 funcflags |= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1328 break;
1329 case ATTR_DEFAULTCOLLELEM:
1330 funcflags |= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1331 break;
1332 case ATTR_DISPLAYBIND:
1333 funcflags |= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1334 break;
1335 case ATTR_ENTRY:
1336 extra_attr = max(extra_attr, 3);
1337 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1338 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1339 else {
1340 entry = expr->cval;
1341 entry_is_ord = 1;
1343 break;
1344 case ATTR_HELPCONTEXT:
1345 extra_attr = max(extra_attr, 1);
1346 help_context = expr->u.lval;
1347 break;
1348 case ATTR_HELPSTRING:
1349 extra_attr = max(extra_attr, 2);
1350 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1351 break;
1352 case ATTR_HELPSTRINGCONTEXT:
1353 extra_attr = max(extra_attr, 6);
1354 help_string_context = expr->u.lval;
1355 break;
1356 case ATTR_HIDDEN:
1357 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1358 break;
1359 case ATTR_ID:
1360 id = expr->cval;
1361 break;
1362 case ATTR_IMMEDIATEBIND:
1363 funcflags |= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1364 break;
1365 case ATTR_NONBROWSABLE:
1366 funcflags |= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1367 break;
1368 case ATTR_OUT:
1369 break;
1370 case ATTR_PROPGET:
1371 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1372 break;
1373 case ATTR_PROPPUT:
1374 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1375 break;
1376 case ATTR_PROPPUTREF:
1377 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1378 break;
1379 /* FIXME: FUNCFLAG_FREPLACEABLE */
1380 case ATTR_REQUESTEDIT:
1381 funcflags |= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1382 break;
1383 case ATTR_RESTRICTED:
1384 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1385 break;
1386 case ATTR_SOURCE:
1387 funcflags |= 0x2; /* FUNCFLAG_FSOURCE */
1388 break;
1389 case ATTR_UIDEFAULT:
1390 funcflags |= 0x200; /* FUNCFLAG_FUIDEFAULT */
1391 break;
1392 case ATTR_USESGETLASTERROR:
1393 funcflags |= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1394 break;
1395 case ATTR_VARARG:
1396 if (num_optional || num_defaults)
1397 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1398 else
1399 num_optional = -1;
1400 break;
1401 default:
1402 break;
1406 /* allocate type data space for us */
1407 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1409 if (!typeinfo->func_data) {
1410 typeinfo->func_data = xmalloc(0x100);
1411 typeinfo->func_data_allocated = 0x100;
1412 typeinfo->func_data[0] = 0;
1415 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1416 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1417 typeinfo->func_data[0] + typedata_size + sizeof(int));
1418 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1421 offset = typeinfo->func_data[0];
1422 typeinfo->func_data[0] += typedata_size;
1423 typedata = typeinfo->func_data + (offset >> 2) + 1;
1426 /* find func with the same name - if it exists use its id */
1427 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1428 if(name_offset == typeinfo->func_names[i]) {
1429 id = typeinfo->func_indices[i];
1430 break;
1434 /* find the first func with the same id and link via the hiword of typedata[4] */
1435 next_idx = index;
1436 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1437 if(id == typeinfo->func_indices[i]) {
1438 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1439 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1440 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1441 break;
1445 /* fill out the basic type information */
1446 typedata[0] = typedata_size | (index << 16);
1447 encode_var(typeinfo->typelib, type_function_get_rettype(func->declspec.type), func,
1448 &typedata[1], &decoded_size);
1449 typedata[2] = funcflags;
1450 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1451 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1452 if(num_defaults) typedata[4] |= 0x1000;
1453 if(entry_is_ord) typedata[4] |= 0x2000;
1454 typedata[5] = (num_optional << 16) | num_params;
1456 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1457 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1458 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1459 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1461 switch(extra_attr) {
1462 case 6: typedata[11] = help_string_context;
1463 case 5: typedata[10] = -1;
1464 case 4: typedata[9] = -1;
1465 case 3: typedata[8] = entry;
1466 case 2: typedata[7] = help_string_offset;
1467 case 1: typedata[6] = help_context;
1468 case 0:
1469 break;
1470 default:
1471 warning("unknown number of optional attrs\n");
1474 if (type_function_get_args(func->declspec.type))
1476 i = 0;
1477 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1479 int paramflags = 0;
1480 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1481 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1483 if(defaultdata) *defaultdata = -1;
1485 encode_var(typeinfo->typelib, arg->declspec.type, arg, paramdata, &decoded_size);
1486 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1487 switch(attr->type) {
1488 case ATTR_DEFAULTVALUE:
1490 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1491 write_default_value(typeinfo->typelib, arg->declspec.type, (expr_t *)attr->u.pval, defaultdata);
1492 break;
1494 case ATTR_IN:
1495 paramflags |= 0x01; /* PARAMFLAG_FIN */
1496 break;
1497 case ATTR_OPTIONAL:
1498 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1499 break;
1500 case ATTR_OUT:
1501 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1502 break;
1503 case ATTR_PARAMLCID:
1504 paramflags |= 0x04; /* PARAMFLAG_LCID */
1505 lcid_retval_count++;
1506 break;
1507 case ATTR_RETVAL:
1508 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1509 lcid_retval_count++;
1510 break;
1511 default:
1512 chat("unhandled param attr %d\n", attr->type);
1513 break;
1516 paramdata[1] = -1;
1517 paramdata[2] = paramflags;
1518 typedata[3] += decoded_size << 16;
1520 i++;
1524 if(lcid_retval_count == 1)
1525 typedata[4] |= 0x4000;
1526 else if(lcid_retval_count == 2)
1527 typedata[4] |= 0x8000;
1529 if(typeinfo->funcs_allocated == 0) {
1530 typeinfo->funcs_allocated = 10;
1531 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1532 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1533 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1535 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1536 typeinfo->funcs_allocated *= 2;
1537 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1538 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1539 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1542 /* update the index data */
1543 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1544 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1545 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1547 /* ??? */
1548 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1549 typeinfo->typeinfo->res2 <<= 1;
1550 /* ??? */
1551 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1553 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1554 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1555 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1557 /* adjust size of VTBL */
1558 if(funckind != 0x3 /* FUNC_STATIC */)
1559 typeinfo->typeinfo->cbSizeVft += pointer_size;
1561 /* Increment the number of function elements */
1562 typeinfo->typeinfo->cElement += 1;
1564 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1565 if (*((INT *)namedata) == -1) {
1566 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1567 if(typeinfo->typekind == TKIND_MODULE)
1568 namedata[9] |= 0x10;
1569 } else
1570 namedata[9] &= ~0x10;
1572 if(typeinfo->typekind == TKIND_MODULE)
1573 namedata[9] |= 0x20;
1575 if (type_function_get_args(func->declspec.type))
1577 i = 0;
1578 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1580 /* don't give the last arg of a [propput*] func a name */
1581 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1583 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1584 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1585 paramdata[1] = offset;
1587 i++;
1590 return S_OK;
1593 static HRESULT add_var_desc(msft_typeinfo_t *typeinfo, UINT index, var_t* var)
1595 int offset, id;
1596 unsigned int typedata_size;
1597 INT *typedata;
1598 unsigned int var_datawidth, var_alignment = 0;
1599 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1600 int alignment;
1601 int varflags = 0;
1602 const attr_t *attr;
1603 unsigned char *namedata;
1604 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1606 chat("add_var_desc(%d, %s)\n", index, var->name);
1608 id = 0x40000000 + index;
1610 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1611 expr_t *expr = attr->u.pval;
1612 switch(attr->type) {
1613 case ATTR_BINDABLE:
1614 varflags |= 0x04; /* VARFLAG_FBINDABLE */
1615 break;
1616 case ATTR_DEFAULTBIND:
1617 varflags |= 0x20; /* VARFLAG_FDEFAULTBIND */
1618 break;
1619 case ATTR_DEFAULTCOLLELEM:
1620 varflags |= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1621 break;
1622 case ATTR_DISPLAYBIND:
1623 varflags |= 0x10; /* VARFLAG_FDISPLAYBIND */
1624 break;
1625 case ATTR_HIDDEN:
1626 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1627 break;
1628 case ATTR_ID:
1629 id = expr->cval;
1630 break;
1631 case ATTR_IMMEDIATEBIND:
1632 varflags |= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1633 break;
1634 case ATTR_NONBROWSABLE:
1635 varflags |= 0x400; /* VARFLAG_FNONBROWSABLE */
1636 break;
1637 case ATTR_READONLY:
1638 varflags |= 0x01; /* VARFLAG_FREADONLY */
1639 break;
1640 /* FIXME: VARFLAG_FREPLACEABLE */
1641 case ATTR_REQUESTEDIT:
1642 varflags |= 0x08; /* VARFLAG_FREQUESTEDIT */
1643 break;
1644 case ATTR_RESTRICTED:
1645 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1646 break;
1647 case ATTR_SOURCE:
1648 varflags |= 0x02; /* VARFLAG_FSOURCE */
1649 break;
1650 case ATTR_UIDEFAULT:
1651 varflags |= 0x0200; /* VARFLAG_FUIDEFAULT */
1652 break;
1653 default:
1654 break;
1658 /* allocate type data space for us */
1659 typedata_size = 0x14;
1661 if (!typeinfo->var_data) {
1662 typeinfo->var_data = xmalloc(0x100);
1663 typeinfo->var_data_allocated = 0x100;
1664 typeinfo->var_data[0] = 0;
1667 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1668 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1669 typeinfo->var_data[0] + typedata_size + sizeof(int));
1670 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1673 offset = typeinfo->var_data[0];
1674 typeinfo->var_data[0] += typedata_size;
1675 typedata = typeinfo->var_data + (offset >> 2) + 1;
1677 /* fill out the basic type information */
1678 typedata[0] = typedata_size | (index << 16);
1679 typedata[2] = varflags;
1680 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1682 if(typeinfo->vars_allocated == 0) {
1683 typeinfo->vars_allocated = 10;
1684 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1685 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1686 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1688 if(typeinfo->vars_allocated == var_num) {
1689 typeinfo->vars_allocated *= 2;
1690 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1691 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1692 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1694 /* update the index data */
1695 typeinfo->var_indices[var_num] = id;
1696 typeinfo->var_names[var_num] = -1;
1697 typeinfo->var_offsets[var_num] = offset;
1699 /* figure out type widths and whatnot */
1700 var_datawidth = type_memsize_and_alignment(var->declspec.type, &var_alignment);
1701 encode_var(typeinfo->typelib, var->declspec.type, var, &typedata[1], &var_type_size);
1703 /* pad out starting position to data width */
1704 typeinfo->datawidth += var_alignment - 1;
1705 typeinfo->datawidth &= ~(var_alignment - 1);
1707 switch(typeinfo->typekind) {
1708 case TKIND_ENUM:
1709 write_int_value(typeinfo->typelib, &typedata[4], VT_I4, var->eval->cval);
1710 var_kind = 2; /* VAR_CONST */
1711 var_type_size += 16; /* sizeof(VARIANT) */
1712 typeinfo->datawidth = var_datawidth;
1713 break;
1714 case TKIND_RECORD:
1715 typedata[4] = typeinfo->datawidth;
1716 typeinfo->datawidth += var_datawidth;
1717 break;
1718 case TKIND_UNION:
1719 typedata[4] = typeinfo->datawidth;
1720 typeinfo->datawidth = max(typeinfo->datawidth, var_datawidth);
1721 break;
1722 case TKIND_DISPATCH:
1723 var_kind = 3; /* VAR_DISPATCH */
1724 typeinfo->datawidth = pointer_size;
1725 break;
1726 default:
1727 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1728 break;
1731 /* add type description size to total required allocation */
1732 typedata[3] += var_type_size << 16 | var_kind;
1734 /* fix type alignment */
1735 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1736 if (alignment < var_alignment) {
1737 alignment = var_alignment;
1738 typeinfo->typeinfo->typekind &= ~0xffc0;
1739 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1742 /* ??? */
1743 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1744 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1745 typeinfo->typeinfo->res2 <<= 1;
1748 /* ??? */
1749 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1750 typeinfo->typeinfo->res3 += 0x2c;
1752 /* increment the number of variable elements */
1753 typeinfo->typeinfo->cElement += 0x10000;
1755 /* pad data width to alignment */
1756 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1758 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1759 if (offset == -1) return E_OUTOFMEMORY;
1761 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1762 if (*((INT *)namedata) == -1) {
1763 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1764 if(typeinfo->typekind != TKIND_DISPATCH)
1765 namedata[9] |= 0x10;
1766 } else
1767 namedata[9] &= ~0x10;
1769 if (typeinfo->typekind == TKIND_ENUM) {
1770 namedata[9] |= 0x20;
1772 typeinfo->var_names[var_num] = offset;
1774 return S_OK;
1777 static HRESULT add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1779 if(importinfo) {
1780 alloc_importinfo(typeinfo->typelib, importinfo);
1781 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1782 }else {
1783 if(ref->typelib_idx == -1)
1784 add_interface_typeinfo(typeinfo->typelib, ref);
1785 if(ref->typelib_idx == -1)
1786 error("add_impl_type: unable to add inherited interface\n");
1788 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1791 typeinfo->typeinfo->cImplTypes++;
1792 return S_OK;
1795 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1796 const char *name, const attr_list_t *attrs)
1798 const attr_t *attr;
1799 msft_typeinfo_t *msft_typeinfo;
1800 int nameoffset;
1801 int typeinfo_offset;
1802 MSFT_TypeInfoBase *typeinfo;
1803 MSFT_GuidEntry guidentry;
1805 chat("create_msft_typeinfo: name %s kind %d index %d\n", name, kind, typelib->typelib_header.nrtypeinfos);
1807 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1808 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1810 msft_typeinfo->typelib = typelib;
1812 nameoffset = ctl2_alloc_name(typelib, name);
1813 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1814 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1816 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1817 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1819 msft_typeinfo->typekind = kind;
1820 msft_typeinfo->typeinfo = typeinfo;
1822 typeinfo->typekind |= kind | 0x20;
1824 if(kind == TKIND_COCLASS)
1825 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1827 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1828 switch(attr->type) {
1829 case ATTR_AGGREGATABLE:
1830 if (kind == TKIND_COCLASS)
1831 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1832 break;
1834 case ATTR_APPOBJECT:
1835 if (kind == TKIND_COCLASS)
1836 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1837 break;
1839 case ATTR_CONTROL:
1840 if (kind == TKIND_COCLASS)
1841 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1842 break;
1844 case ATTR_DLLNAME:
1846 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1847 typeinfo->datatype1 = offset;
1848 break;
1851 case ATTR_DUAL:
1852 /* FIXME: check interface is compatible */
1853 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1854 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1855 break;
1857 case ATTR_HELPCONTEXT:
1859 expr_t *expr = (expr_t*)attr->u.pval;
1860 typeinfo->helpcontext = expr->cval;
1861 break;
1863 case ATTR_HELPSTRING:
1865 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1866 if (offset == -1) break;
1867 typeinfo->docstringoffs = offset;
1868 break;
1870 case ATTR_HELPSTRINGCONTEXT:
1872 expr_t *expr = (expr_t*)attr->u.pval;
1873 typeinfo->helpstringcontext = expr->cval;
1874 break;
1876 case ATTR_HIDDEN:
1877 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1878 break;
1880 case ATTR_LICENSED:
1881 typeinfo->flags |= 0x04; /* TYPEFLAG_FLICENSED */
1882 break;
1884 case ATTR_NONCREATABLE:
1885 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1886 break;
1888 case ATTR_NONEXTENSIBLE:
1889 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1890 break;
1892 case ATTR_OLEAUTOMATION:
1893 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1894 break;
1896 /* FIXME: TYPEFLAG_FPREDCLID */
1898 case ATTR_PROXY:
1899 typeinfo->flags |= 0x4000; /* TYPEFLAG_FPROXY */
1900 break;
1902 /* FIXME: TYPEFLAG_FREPLACEABLE */
1904 case ATTR_RESTRICTED:
1905 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
1906 break;
1908 case ATTR_UUID:
1909 guidentry.guid = *(GUID*)attr->u.pval;
1910 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1911 guidentry.next_hash = -1;
1912 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
1913 #if 0
1914 if (IsEqualIID(guid, &IID_IDispatch)) {
1915 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1917 #endif
1918 break;
1920 case ATTR_VERSION:
1921 typeinfo->version = attr->u.ival;
1922 break;
1924 default:
1925 break;
1929 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
1930 typelib->last_typeinfo = msft_typeinfo;
1931 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
1934 return msft_typeinfo;
1937 static void add_dispatch(msft_typelib_t *typelib)
1939 int guid_offset, impfile_offset, hash_key;
1940 MSFT_GuidEntry guidentry;
1941 MSFT_ImpInfo impinfo;
1942 GUID stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1943 GUID iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1945 if(typelib->typelib_header.dispatchpos != -1) return;
1947 guidentry.guid = stdole;
1948 guidentry.hreftype = 2;
1949 guidentry.next_hash = -1;
1950 hash_key = ctl2_hash_guid(&guidentry.guid);
1951 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
1952 if (guid_offset == -1)
1953 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
1954 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
1956 guidentry.guid = iid_idispatch;
1957 guidentry.hreftype = 1;
1958 guidentry.next_hash = -1;
1959 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
1960 impinfo.oImpFile = impfile_offset;
1961 hash_key = ctl2_hash_guid(&guidentry.guid);
1962 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
1963 if (guid_offset == -1)
1964 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
1965 impinfo.oGuid = guid_offset;
1966 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
1969 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
1971 int num_parents = 0, num_funcs = 0;
1972 importinfo_t *importinfo = NULL;
1973 const statement_t *stmt_func;
1974 type_t *inherit, *ref;
1975 int idx = 0;
1976 var_t *func;
1977 var_t *var;
1978 msft_typeinfo_t *msft_typeinfo;
1980 if (-1 < dispinterface->typelib_idx)
1981 return;
1983 inherit = type_dispiface_get_inherit(dispinterface);
1985 if (inherit)
1987 importinfo = find_importinfo(typelib, inherit->name);
1989 if (!importinfo && type_iface_get_inherit(inherit) && inherit->typelib_idx == -1)
1990 add_interface_typeinfo(typelib, inherit);
1993 /* check typelib_idx again, it could have been added while resolving the parent interface */
1994 if (-1 < dispinterface->typelib_idx)
1995 return;
1997 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
1998 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
1999 dispinterface->attrs);
2001 msft_typeinfo->typeinfo->size = pointer_size;
2002 msft_typeinfo->typeinfo->typekind |= pointer_size << 11 | pointer_size << 6;
2004 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2005 add_dispatch(typelib);
2007 if (inherit)
2009 add_impl_type(msft_typeinfo, inherit, importinfo);
2010 msft_typeinfo->typeinfo->typekind |= 0x10;
2013 /* count the number of inherited interfaces and non-local functions */
2014 for (ref = inherit; ref; ref = type_iface_get_inherit(ref))
2016 num_parents++;
2017 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) )
2019 var_t *func = stmt_func->u.var;
2020 if (!is_local(func->attrs)) num_funcs++;
2023 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2024 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2026 msft_typeinfo->typeinfo->cImplTypes = 1; /* IDispatch */
2028 /* count the no of methods, as the variable indices come after the funcs */
2029 if (dispinterface->details.iface->disp_methods)
2030 LIST_FOR_EACH_ENTRY( func, dispinterface->details.iface->disp_methods, var_t, entry )
2031 idx++;
2033 if (type_dispiface_get_props(dispinterface))
2034 LIST_FOR_EACH_ENTRY( var, type_dispiface_get_props(dispinterface), var_t, entry )
2035 add_var_desc(msft_typeinfo, idx++, var);
2037 if (type_dispiface_get_methods(dispinterface))
2039 idx = 0;
2040 LIST_FOR_EACH_ENTRY( func, type_dispiface_get_methods(dispinterface), var_t, entry )
2041 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2042 idx++;
2046 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
2048 int idx = 0;
2049 const statement_t *stmt_func;
2050 type_t *ref;
2051 msft_typeinfo_t *msft_typeinfo;
2052 importinfo_t *ref_importinfo = NULL;
2053 int num_parents = 0, num_funcs = 0;
2054 type_t *inherit;
2055 const type_t *derived;
2057 if (-1 < interface->typelib_idx)
2058 return;
2060 if (!interface->details.iface)
2062 error( "interface %s is referenced but not defined\n", interface->name );
2063 return;
2066 if (is_attr(interface->attrs, ATTR_DISPINTERFACE)) {
2067 add_dispinterface_typeinfo(typelib, interface);
2068 return;
2071 /* midl adds the parent interface first, unless the parent itself
2072 has no parent (i.e. it stops before IUnknown). */
2074 inherit = type_iface_get_inherit(interface);
2076 if(inherit) {
2077 ref_importinfo = find_importinfo(typelib, inherit->name);
2079 if(!ref_importinfo && type_iface_get_inherit(inherit) &&
2080 inherit->typelib_idx == -1)
2081 add_interface_typeinfo(typelib, inherit);
2084 /* check typelib_idx again, it could have been added while resolving the parent interface */
2085 if (-1 < interface->typelib_idx)
2086 return;
2088 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2089 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
2090 msft_typeinfo->typeinfo->size = pointer_size;
2091 msft_typeinfo->typeinfo->typekind |= 0x0200;
2092 msft_typeinfo->typeinfo->typekind |= pointer_size << 11;
2094 for (derived = inherit; derived; derived = type_iface_get_inherit(derived))
2095 if (derived->name && !strcmp(derived->name, "IDispatch"))
2096 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2098 if(type_iface_get_inherit(interface))
2099 add_impl_type(msft_typeinfo, type_iface_get_inherit(interface),
2100 ref_importinfo);
2102 /* count the number of inherited interfaces and non-local functions */
2103 for(ref = inherit; ref; ref = type_iface_get_inherit(ref)) {
2104 num_parents++;
2105 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 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(interface) ) {
2114 var_t *func = stmt_func->u.var;
2115 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2116 idx++;
2120 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2122 int idx = 0;
2123 var_t *cur;
2124 msft_typeinfo_t *msft_typeinfo;
2126 if (-1 < structure->typelib_idx)
2127 return;
2129 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2130 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2131 msft_typeinfo->typeinfo->size = 0;
2133 if (type_struct_get_fields(structure))
2134 LIST_FOR_EACH_ENTRY( cur, type_struct_get_fields(structure), var_t, entry )
2135 add_var_desc(msft_typeinfo, idx++, cur);
2138 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2140 int idx = 0;
2141 var_t *cur;
2142 msft_typeinfo_t *msft_typeinfo;
2144 if (-1 < enumeration->typelib_idx)
2145 return;
2147 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2148 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2149 msft_typeinfo->typeinfo->size = 0;
2151 if (type_enum_get_values(enumeration))
2152 LIST_FOR_EACH_ENTRY( cur, type_enum_get_values(enumeration), var_t, entry )
2153 add_var_desc(msft_typeinfo, idx++, cur);
2156 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion)
2158 int idx = 0;
2159 var_t *cur;
2160 msft_typeinfo_t *msft_typeinfo;
2162 if (-1 < tunion->typelib_idx)
2163 return;
2165 tunion->typelib_idx = typelib->typelib_header.nrtypeinfos;
2166 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_UNION, tunion->name, tunion->attrs);
2167 msft_typeinfo->typeinfo->size = 0;
2169 if (type_union_get_cases(tunion))
2170 LIST_FOR_EACH_ENTRY(cur, type_union_get_cases(tunion), var_t, entry)
2171 add_var_desc(msft_typeinfo, idx++, cur);
2174 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2176 msft_typeinfo_t *msft_typeinfo = NULL;
2177 int datatype1, datatype2, duplicate = 0;
2178 unsigned int size, alignment = 0;
2179 type_t *type;
2181 if (-1 < tdef->typelib_idx)
2182 return;
2184 type = type_alias_get_aliasee_type(tdef);
2186 if (!type->name || strcmp(tdef->name, type->name) != 0)
2188 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2189 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2191 else
2192 duplicate = 1;
2194 encode_type(typelib, get_type_vt(type), type, &datatype1, &datatype2);
2195 size = type_memsize_and_alignment(type, &alignment);
2197 if (msft_typeinfo)
2199 msft_typeinfo->typeinfo->datatype1 = datatype1;
2200 msft_typeinfo->typeinfo->size = size;
2201 msft_typeinfo->typeinfo->datatype2 = datatype2;
2202 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2205 /* avoid adding duplicate type definitions */
2206 if (duplicate)
2207 tdef->typelib_idx = type->typelib_idx;
2210 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2212 msft_typeinfo_t *msft_typeinfo;
2213 ifref_t *iref;
2214 int num_ifaces = 0, offset, i;
2215 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2216 int have_default = 0, have_default_source = 0;
2217 const attr_t *attr;
2218 ifref_list_t *ifaces;
2220 if (-1 < cls->typelib_idx)
2221 return;
2223 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2224 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2226 ifaces = type_coclass_get_ifaces(cls);
2227 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) num_ifaces++;
2229 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2230 num_ifaces * sizeof(*ref), 0);
2232 i = 0;
2233 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) {
2234 if(iref->iface->typelib_idx == -1)
2235 add_interface_typeinfo(typelib, iref->iface);
2236 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2237 ref->reftype = typelib->typelib_typeinfo_offsets[iref->iface->typelib_idx];
2238 ref->flags = 0;
2239 ref->oCustData = -1;
2240 ref->onext = -1;
2241 if(i < num_ifaces - 1)
2242 ref->onext = offset + (i + 1) * sizeof(*ref);
2244 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2245 switch(attr->type) {
2246 case ATTR_DEFAULT:
2247 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2248 break;
2249 case ATTR_DEFAULTVTABLE:
2250 ref->flags |= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2251 break;
2252 case ATTR_RESTRICTED:
2253 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2254 break;
2255 case ATTR_SOURCE:
2256 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2257 break;
2258 default:
2259 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2262 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2263 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2264 have_default_source = 1;
2265 else
2266 have_default = 1;
2269 /* If the interface is non-restricted and we haven't already had one then
2270 remember it so that we can use it as a default later */
2271 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2272 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2273 if(!first_source)
2274 first_source = ref;
2276 else if(!first)
2277 first = ref;
2279 i++;
2282 /* If we haven't had a default interface, then set the default flags on the
2283 first ones */
2284 if(!have_default && first)
2285 first->flags |= 0x1;
2286 if(!have_default_source && first_source)
2287 first_source->flags |= 0x1;
2289 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2290 msft_typeinfo->typeinfo->size = pointer_size;
2291 msft_typeinfo->typeinfo->typekind |= 0x2200;
2294 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2296 int idx = 0;
2297 const statement_t *stmt;
2298 msft_typeinfo_t *msft_typeinfo;
2300 if (-1 < module->typelib_idx)
2301 return;
2303 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2304 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2305 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2307 STATEMENTS_FOR_EACH_FUNC( stmt, module->details.module->stmts ) {
2308 var_t *func = stmt->u.var;
2309 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2310 idx++;
2313 msft_typeinfo->typeinfo->size = idx;
2316 static void add_type_typeinfo(msft_typelib_t *typelib, type_t *type)
2318 switch (type_get_type(type)) {
2319 case TYPE_INTERFACE:
2320 add_interface_typeinfo(typelib, type);
2321 break;
2322 case TYPE_STRUCT:
2323 add_structure_typeinfo(typelib, type);
2324 break;
2325 case TYPE_ENUM:
2326 add_enum_typeinfo(typelib, type);
2327 break;
2328 case TYPE_UNION:
2329 add_union_typeinfo(typelib, type);
2330 break;
2331 case TYPE_COCLASS:
2332 add_coclass_typeinfo(typelib, type);
2333 break;
2334 case TYPE_BASIC:
2335 case TYPE_POINTER:
2336 case TYPE_ARRAY:
2337 break;
2338 default:
2339 error("add_entry: unhandled type 0x%x for %s\n",
2340 type_get_type(type), type->name);
2341 break;
2345 static void add_entry(msft_typelib_t *typelib, const statement_t *stmt)
2347 switch(stmt->type) {
2348 case STMT_LIBRARY:
2349 case STMT_IMPORT:
2350 case STMT_PRAGMA:
2351 case STMT_CPPQUOTE:
2352 case STMT_DECLARATION:
2353 /* not included in typelib */
2354 break;
2355 case STMT_IMPORTLIB:
2356 /* not processed here */
2357 break;
2358 case STMT_TYPEDEF:
2360 const type_list_t *type_entry = stmt->u.type_list;
2361 for (; type_entry; type_entry = type_entry->next) {
2362 /* if the type is public then add the typedef, otherwise attempt
2363 * to add the aliased type */
2364 if (is_attr(type_entry->type->attrs, ATTR_PUBLIC))
2365 add_typedef_typeinfo(typelib, type_entry->type);
2366 else
2367 add_type_typeinfo(typelib, type_alias_get_aliasee_type(type_entry->type));
2369 break;
2371 case STMT_MODULE:
2372 add_module_typeinfo(typelib, stmt->u.type);
2373 break;
2374 case STMT_TYPE:
2375 case STMT_TYPEREF:
2377 type_t *type = stmt->u.type;
2378 add_type_typeinfo(typelib, type);
2379 break;
2384 static void set_name(msft_typelib_t *typelib)
2386 int offset;
2388 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2389 if (offset == -1) return;
2390 typelib->typelib_header.NameOffset = offset;
2391 return;
2394 static void set_version(msft_typelib_t *typelib)
2396 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2399 static void set_guid(msft_typelib_t *typelib)
2401 MSFT_GuidEntry guidentry;
2402 int offset;
2403 void *ptr;
2404 GUID guid = {0,0,0,{0,0,0,0,0,0}};
2406 guidentry.guid = guid;
2407 guidentry.hreftype = -2;
2408 guidentry.next_hash = -1;
2410 ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2411 if (ptr) guidentry.guid = *(GUID *)ptr;
2413 offset = ctl2_alloc_guid(typelib, &guidentry);
2414 typelib->typelib_header.posguid = offset;
2416 return;
2419 static void set_doc_string(msft_typelib_t *typelib)
2421 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2423 if (str)
2425 int offset = ctl2_alloc_string(typelib, str);
2426 if (offset != -1) typelib->typelib_header.helpstring = offset;
2430 static void set_help_file_name(msft_typelib_t *typelib)
2432 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2434 if (str)
2436 int offset = ctl2_alloc_string(typelib, str);
2437 if (offset != -1)
2439 typelib->typelib_header.helpfile = offset;
2440 typelib->typelib_header.varflags |= 0x10;
2445 static void set_help_context(msft_typelib_t *typelib)
2447 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2448 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2451 static void set_help_string_dll(msft_typelib_t *typelib)
2453 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2455 if (str)
2457 int offset = ctl2_alloc_string(typelib, str);
2458 if (offset != -1)
2460 typelib->help_string_dll_offset = offset;
2461 typelib->typelib_header.varflags |= 0x100;
2466 static void set_help_string_context(msft_typelib_t *typelib)
2468 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2469 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2472 static void set_lcid(msft_typelib_t *typelib)
2474 const expr_t *lcid_expr = get_attrp( typelib->typelib->attrs, ATTR_LIBLCID );
2475 if(lcid_expr)
2477 typelib->typelib_header.lcid = lcid_expr->cval;
2478 typelib->typelib_header.lcid2 = lcid_expr->cval;
2482 static void set_lib_flags(msft_typelib_t *typelib)
2484 const attr_t *attr;
2486 typelib->typelib_header.flags = 0;
2487 if (!typelib->typelib->attrs) return;
2488 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2490 switch(attr->type) {
2491 case ATTR_CONTROL:
2492 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2493 break;
2494 case ATTR_HIDDEN:
2495 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2496 break;
2497 case ATTR_RESTRICTED:
2498 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2499 break;
2500 default:
2501 break;
2504 return;
2507 static void ctl2_write_segment(msft_typelib_t *typelib, int segment)
2509 put_data(typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length);
2512 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2514 msft_typeinfo_t *typeinfo;
2516 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2517 typeinfo->typeinfo->memoffset = filesize;
2518 if (typeinfo->func_data)
2519 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2520 if (typeinfo->var_data)
2521 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2522 if (typeinfo->func_data || typeinfo->var_data)
2523 filesize += 4;
2527 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2529 if (typelib->typelib_segdir[segment].length) {
2530 typelib->typelib_segdir[segment].offset = filepos;
2531 } else {
2532 typelib->typelib_segdir[segment].offset = -1;
2535 return typelib->typelib_segdir[segment].length;
2539 static void ctl2_write_typeinfos(msft_typelib_t *typelib)
2541 msft_typeinfo_t *typeinfo;
2542 int typedata_size;
2544 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2545 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2546 typedata_size = 0;
2547 if (typeinfo->func_data)
2548 typedata_size = typeinfo->func_data[0];
2549 if (typeinfo->var_data)
2550 typedata_size += typeinfo->var_data[0];
2551 put_data(&typedata_size, sizeof(int));
2552 if (typeinfo->func_data)
2553 put_data(typeinfo->func_data + 1, typeinfo->func_data[0]);
2554 if (typeinfo->var_data)
2555 put_data(typeinfo->var_data + 1, typeinfo->var_data[0]);
2556 if (typeinfo->func_indices)
2557 put_data(typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2558 if (typeinfo->var_indices)
2559 put_data(typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2560 if (typeinfo->func_names)
2561 put_data(typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2562 if (typeinfo->var_names)
2563 put_data(typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2564 if (typeinfo->func_offsets)
2565 put_data(typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2566 if (typeinfo->var_offsets) {
2567 int add = 0, i, offset;
2568 if(typeinfo->func_data)
2569 add = typeinfo->func_data[0];
2570 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2571 offset = typeinfo->var_offsets[i];
2572 offset += add;
2573 put_data(&offset, 4);
2579 static void save_all_changes(msft_typelib_t *typelib)
2581 int filepos;
2583 chat("save_all_changes(%p)\n", typelib);
2585 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2586 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2587 filepos += typelib->typelib_header.nrtypeinfos * 4;
2589 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2590 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2591 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2592 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2593 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2594 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2595 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2596 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2597 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2598 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2599 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2600 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2601 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2603 ctl2_finalize_typeinfos(typelib, filepos);
2605 byte_swapped = 0;
2606 init_output_buffer();
2608 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2609 if(typelib->typelib_header.varflags & 0x100)
2610 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2612 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2613 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2614 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2615 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2616 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2617 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2618 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2619 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2620 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2621 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2622 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2623 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2624 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2625 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2626 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2628 ctl2_write_typeinfos(typelib);
2630 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2632 char typelib_id[13] = "#1";
2634 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2635 if (expr)
2636 sprintf( typelib_id, "#%d", expr->cval );
2637 add_output_to_resources( "TYPELIB", typelib_id );
2638 output_typelib_regscript( typelib->typelib );
2640 else flush_output_buffer( typelib_name );
2643 int create_msft_typelib(typelib_t *typelib)
2645 msft_typelib_t *msft;
2646 int failed = 0;
2647 const statement_t *stmt;
2648 time_t cur_time;
2649 char *time_override;
2650 unsigned int version = 7 << 24 | 555; /* 7.00.0555 */
2651 GUID midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2652 GUID midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2653 GUID midl_info_guid = {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2654 char info_string[128];
2656 msft = xmalloc(sizeof(*msft));
2657 memset(msft, 0, sizeof(*msft));
2658 msft->typelib = typelib;
2660 ctl2_init_header(msft);
2661 ctl2_init_segdir(msft);
2663 msft->typelib_header.varflags |= (pointer_size == 8) ? SYS_WIN64 : SYS_WIN32;
2666 * The following two calls return an offset or -1 if out of memory. We
2667 * specifically need an offset of 0, however, so...
2669 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2670 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2672 if(failed)
2674 free(msft);
2675 return 0;
2678 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2679 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2681 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2682 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2684 set_lib_flags(msft);
2685 set_lcid(msft);
2686 set_help_file_name(msft);
2687 set_doc_string(msft);
2688 set_guid(msft);
2689 set_version(msft);
2690 set_name(msft);
2691 set_help_context(msft);
2692 set_help_string_dll(msft);
2693 set_help_string_context(msft);
2695 /* midl adds two sets of custom data to the library: the current unix time
2696 and midl's version number */
2697 time_override = getenv( "WIDL_TIME_OVERRIDE");
2698 cur_time = time_override ? atol( time_override) : time(NULL);
2699 sprintf(info_string, "Created by WIDL version %s at %s\n", PACKAGE_VERSION, ctime(&cur_time));
2700 set_custdata(msft, &midl_info_guid, VT_BSTR, info_string, &msft->typelib_header.CustomDataOffset);
2701 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2702 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2704 if (typelib->stmts)
2705 LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
2706 add_entry(msft, stmt);
2708 save_all_changes(msft);
2709 free(msft);
2710 return 1;