qcap/tests: Get rid of the VFW capture filter tests.
[wine.git] / tools / widl / write_msft.c
blob7cfe9e146c9a7859eba253c9fe89f5557f434c08
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 char *converted_name;
295 size_t length, size;
296 int offset;
297 int value;
299 length = strlen(name);
300 size = (length + 7) & ~3;
301 converted_name = xmalloc(size + 1);
302 memcpy(converted_name + 4, name, length);
303 converted_name[length + 4] = 0;
305 value = lhash_val_of_name_sys(typelib->typelib_header.varflags & 0x0f, typelib->typelib_header.lcid, converted_name + 4);
307 #ifdef WORDS_BIGENDIAN
308 converted_name[3] = length & 0xff;
309 converted_name[2] = length >> 8;
310 converted_name[1] = value;
311 converted_name[0] = value >> 8;
312 #else
313 converted_name[0] = length & 0xff;
314 converted_name[1] = length >> 8;
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 size;
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 char *converted_string;
346 size_t length, size;
347 int offset;
349 length = strlen(string);
350 size = (length + 5) & ~3;
351 if (length < 3) size += 4;
352 converted_string = xmalloc(size);
353 memcpy(converted_string + 2, string, length);
355 #ifdef WORDS_BIGENDIAN
356 converted_string[1] = length & 0xff;
357 converted_string[0] = (length >> 8) & 0xff;
358 #else
359 converted_string[0] = length & 0xff;
360 converted_string[1] = (length >> 8) & 0xff;
361 #endif
363 if(length < 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
364 for(offset = 0; offset < 4; offset++)
365 converted_string[length + offset + 2] = 0x57;
366 length += 4;
368 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
370 *result = converted_string;
371 return size;
374 /****************************************************************************
375 * ctl2_alloc_segment
377 * Allocates memory from a segment in a type library.
379 * RETURNS
381 * Success: The offset within the segment of the new data area.
383 * BUGS
385 * Does not (yet) handle the case where the allocated segment memory needs to grow.
387 static int ctl2_alloc_segment(
388 msft_typelib_t *typelib, /* [I] The type library in which to allocate. */
389 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
390 int size, /* [I] The amount to allocate. */
391 int block_size) /* [I] Initial allocation block size, or 0 for default. */
393 int offset;
395 if(!typelib->typelib_segment_data[segment]) {
396 if (!block_size) block_size = 0x2000;
398 typelib->typelib_segment_block_length[segment] = block_size;
399 typelib->typelib_segment_data[segment] = xmalloc(block_size);
400 if (!typelib->typelib_segment_data[segment]) return -1;
401 memset(typelib->typelib_segment_data[segment], 0x57, block_size);
404 while ((typelib->typelib_segdir[segment].length + size) > typelib->typelib_segment_block_length[segment]) {
405 unsigned char *block;
407 block_size = typelib->typelib_segment_block_length[segment];
408 block = xrealloc(typelib->typelib_segment_data[segment], block_size << 1);
410 if (segment == MSFT_SEG_TYPEINFO) {
411 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
412 msft_typeinfo_t *typeinfo;
414 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
415 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - typelib->typelib_segment_data[segment]];
419 memset(block + block_size, 0x57, block_size);
420 typelib->typelib_segment_block_length[segment] = block_size << 1;
421 typelib->typelib_segment_data[segment] = block;
424 offset = typelib->typelib_segdir[segment].length;
425 typelib->typelib_segdir[segment].length += size;
427 return offset;
430 /****************************************************************************
431 * ctl2_alloc_typeinfo
433 * Allocates and initializes a typeinfo structure in a type library.
435 * RETURNS
437 * Success: The offset of the new typeinfo.
438 * Failure: -1 (this is invariably an out of memory condition).
440 static int ctl2_alloc_typeinfo(
441 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
442 int nameoffset) /* [I] The offset of the name for this typeinfo. */
444 int offset;
445 MSFT_TypeInfoBase *typeinfo;
447 offset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
449 typelib->typelib_typeinfo_offsets[typelib->typelib_header.nrtypeinfos++] = offset;
451 typeinfo = (void *)(typelib->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
453 typeinfo->typekind = (typelib->typelib_header.nrtypeinfos - 1) << 16;
454 typeinfo->memoffset = -1; /* should be EOF if no elements */
455 typeinfo->res2 = 0;
456 typeinfo->res3 = -1;
457 typeinfo->res4 = 3;
458 typeinfo->res5 = 0;
459 typeinfo->cElement = 0;
460 typeinfo->res7 = 0;
461 typeinfo->res8 = 0;
462 typeinfo->res9 = 0;
463 typeinfo->resA = 0;
464 typeinfo->posguid = -1;
465 typeinfo->flags = 0;
466 typeinfo->NameOffset = nameoffset;
467 typeinfo->version = 0;
468 typeinfo->docstringoffs = -1;
469 typeinfo->helpstringcontext = 0;
470 typeinfo->helpcontext = 0;
471 typeinfo->oCustData = -1;
472 typeinfo->cbSizeVft = 0;
473 typeinfo->cImplTypes = 0;
474 typeinfo->size = 0;
475 typeinfo->datatype1 = -1;
476 typeinfo->datatype2 = 0;
477 typeinfo->res18 = 0;
478 typeinfo->res19 = -1;
480 return offset;
483 /****************************************************************************
484 * ctl2_alloc_guid
486 * Allocates and initializes a GUID structure in a type library. Also updates
487 * the GUID hash table as needed.
489 * RETURNS
491 * Success: The offset of the new GUID.
493 static int ctl2_alloc_guid(
494 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
495 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
497 int offset;
498 MSFT_GuidEntry *guid_space;
499 int hash_key;
501 chat("adding uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
502 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
503 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
504 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
506 hash_key = ctl2_hash_guid(&guid->guid);
508 offset = ctl2_find_guid(typelib, hash_key, &guid->guid);
509 if (offset != -1)
511 if (is_warning_enabled(2368))
512 warning("duplicate uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
513 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
514 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
515 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
516 return -1;
519 offset = ctl2_alloc_segment(typelib, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
521 guid_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_GUID] + offset);
522 *guid_space = *guid;
524 guid_space->next_hash = typelib->typelib_guidhash_segment[hash_key];
525 typelib->typelib_guidhash_segment[hash_key] = offset;
527 return offset;
530 /****************************************************************************
531 * ctl2_alloc_name
533 * Allocates and initializes a name within a type library. Also updates the
534 * name hash table as needed.
536 * RETURNS
538 * Success: The offset within the segment of the new name.
539 * Failure: -1 (this is invariably an out of memory condition).
541 static int ctl2_alloc_name(
542 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
543 const char *name) /* [I] The name to store. */
545 int length;
546 int offset;
547 MSFT_NameIntro *name_space;
548 char *encoded_name;
550 length = ctl2_encode_name(typelib, name, &encoded_name);
552 offset = ctl2_find_name(typelib, encoded_name);
553 if (offset != -1)
555 free(encoded_name);
556 return offset;
559 offset = ctl2_alloc_segment(typelib, MSFT_SEG_NAME, length + 8, 0);
561 name_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_NAME] + offset);
562 name_space->hreftype = -1;
563 name_space->next_hash = -1;
564 memcpy(&name_space->namelen, encoded_name, length);
566 if (typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
567 name_space->next_hash = typelib->typelib_namehash_segment[encoded_name[2] & 0x7f];
569 typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
571 typelib->typelib_header.nametablecount += 1;
572 typelib->typelib_header.nametablechars += *encoded_name;
574 free(encoded_name);
575 return offset;
578 /****************************************************************************
579 * ctl2_alloc_string
581 * Allocates and initializes a string in a type library.
583 * RETURNS
585 * Success: The offset within the segment of the new string.
586 * Failure: -1 (this is invariably an out of memory condition).
588 static int ctl2_alloc_string(
589 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
590 const char *string) /* [I] The string to store. */
592 int length;
593 int offset;
594 unsigned char *string_space;
595 char *encoded_string;
597 length = ctl2_encode_string(string, &encoded_string);
599 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_STRING].length;
600 offset += (((typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
601 typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
602 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
605 offset = ctl2_alloc_segment(typelib, MSFT_SEG_STRING, length, 0);
607 string_space = typelib->typelib_segment_data[MSFT_SEG_STRING] + offset;
608 memcpy(string_space, encoded_string, length);
609 free(encoded_string);
611 return offset;
614 /****************************************************************************
615 * alloc_msft_importinfo
617 * Allocates and initializes an import information structure in a type library.
619 * RETURNS
621 * Success: The offset of the new importinfo.
622 * Failure: -1 (this is invariably an out of memory condition).
624 static int alloc_msft_importinfo(
625 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
626 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
628 int offset;
629 MSFT_ImpInfo *impinfo_space;
631 for (offset = 0;
632 offset < typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
633 offset += sizeof(MSFT_ImpInfo)) {
634 if (!memcmp(&(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][offset]),
635 impinfo, sizeof(MSFT_ImpInfo))) {
636 return offset;
640 impinfo->flags |= typelib->typelib_header.nimpinfos++;
642 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
644 impinfo_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
645 *impinfo_space = *impinfo;
647 return offset;
650 /****************************************************************************
651 * alloc_importfile
653 * Allocates and initializes an import file definition in a type library.
655 * RETURNS
657 * Success: The offset of the new importinfo.
658 * Failure: -1 (this is invariably an out of memory condition).
660 static int alloc_importfile(
661 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
662 int guidoffset, /* [I] The offset to the GUID for the imported library. */
663 int major_version, /* [I] The major version number of the imported library. */
664 int minor_version, /* [I] The minor version number of the imported library. */
665 const char *filename) /* [I] The filename of the imported library. */
667 int length;
668 int offset;
669 MSFT_ImpFile *importfile;
670 char *encoded_string;
672 length = ctl2_encode_string(filename, &encoded_string);
674 encoded_string[0] <<= 2;
675 encoded_string[0] |= 1;
677 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
678 offset += (((typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
679 typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 0xc) {
680 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
683 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
685 importfile = (MSFT_ImpFile *)&typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
686 importfile->guid = guidoffset;
687 importfile->lcid = typelib->typelib_header.lcid2;
688 importfile->version = major_version | (minor_version << 16);
689 memcpy(&importfile->filename, encoded_string, length);
690 free(encoded_string);
692 return offset;
695 static void alloc_importinfo(msft_typelib_t *typelib, importinfo_t *importinfo)
697 importlib_t *importlib = importinfo->importlib;
699 chat("alloc_importinfo: %s\n", importinfo->name);
701 if(!importlib->allocated) {
702 MSFT_GuidEntry guid;
703 int guid_idx;
705 chat("allocating importlib %s\n", importlib->name);
707 importlib->allocated = -1;
709 memcpy(&guid.guid, &importlib->guid, sizeof(GUID));
710 guid.hreftype = 2;
712 guid_idx = ctl2_alloc_guid(typelib, &guid);
714 importlib->offset = alloc_importfile(typelib, guid_idx, importlib->version & 0xffff,
715 importlib->version >> 16, importlib->name);
718 if(importinfo->offset == -1 || !(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID)) {
719 MSFT_ImpInfo impinfo;
721 impinfo.flags = importinfo->flags;
722 impinfo.oImpFile = importlib->offset;
724 if(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID) {
725 MSFT_GuidEntry guid;
727 guid.hreftype = 0;
728 memcpy(&guid.guid, &importinfo->guid, sizeof(GUID));
730 impinfo.oGuid = ctl2_alloc_guid(typelib, &guid);
732 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
734 typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo.oGuid+sizeof(GUID)]
735 = importinfo->offset+1;
737 if(!strcmp(importinfo->name, "IDispatch"))
738 typelib->typelib_header.dispatchpos = importinfo->offset+1;
739 }else {
740 impinfo.oGuid = importinfo->id;
741 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
746 static importinfo_t *find_importinfo(msft_typelib_t *typelib, const char *name)
748 importlib_t *importlib;
749 int i;
751 chat("search importlib %s\n", name);
753 if(!name)
754 return NULL;
756 LIST_FOR_EACH_ENTRY( importlib, &typelib->typelib->importlibs, importlib_t, entry )
758 for(i=0; i < importlib->ntypeinfos; i++) {
759 if(!strcmp(name, importlib->importinfos[i].name)) {
760 chat("Found %s in importlib.\n", name);
761 return importlib->importinfos+i;
766 return NULL;
769 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure);
770 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface);
771 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration);
772 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion);
773 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls);
774 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
775 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
778 /****************************************************************************
779 * encode_type
781 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
782 * segments as needed.
784 * RETURNS
786 * Success: 0.
787 * Failure: -1.
789 static int encode_type(
790 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
791 int vt, /* [I] vt to encode */
792 type_t *type, /* [I] type */
793 int *encoded_type, /* [O] The encoded type description. */
794 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
796 int default_type;
797 int scratch;
798 int typeoffset;
799 int *typedata;
800 int target_type;
801 int child_size = 0;
803 chat("encode_type vt %d type %p\n", vt, type);
805 default_type = 0x80000000 | (vt << 16) | vt;
806 if (!decoded_size) decoded_size = &scratch;
808 *decoded_size = 0;
810 switch (vt) {
811 case VT_I1:
812 case VT_UI1:
813 *encoded_type = default_type;
814 break;
816 case VT_INT:
817 *encoded_type = 0x80000000 | (VT_I4 << 16) | VT_INT;
818 break;
820 case VT_UINT:
821 *encoded_type = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
822 break;
824 case VT_UI2:
825 case VT_I2:
826 case VT_BOOL:
827 *encoded_type = default_type;
828 break;
830 case VT_I4:
831 case VT_UI4:
832 case VT_R4:
833 case VT_ERROR:
834 case VT_HRESULT:
835 *encoded_type = default_type;
836 break;
838 case VT_R8:
839 case VT_I8:
840 case VT_UI8:
841 *encoded_type = default_type;
842 break;
844 case VT_CY:
845 case VT_DATE:
846 *encoded_type = default_type;
847 break;
849 case VT_DECIMAL:
850 *encoded_type = default_type;
851 break;
853 case VT_VOID:
854 *encoded_type = 0x80000000 | (VT_EMPTY << 16) | vt;
855 break;
857 case VT_UNKNOWN:
858 case VT_DISPATCH:
859 case VT_BSTR:
860 *encoded_type = default_type;
861 break;
863 case VT_VARIANT:
864 *encoded_type = default_type;
865 break;
867 case VT_LPSTR:
868 case VT_LPWSTR:
869 *encoded_type = 0xfffe0000 | vt;
870 break;
872 case VT_PTR:
874 int next_vt;
875 for(next_vt = 0; is_ptr(type); type = type_pointer_get_ref_type(type)) {
876 next_vt = get_type_vt(type_pointer_get_ref_type(type));
877 if (next_vt != 0)
878 break;
880 /* if no type found then it must be void */
881 if (next_vt == 0)
882 next_vt = VT_VOID;
884 encode_type(typelib, next_vt, type_pointer_get_ref_type(type),
885 &target_type, &child_size);
886 /* these types already have an implicit pointer, so we don't need to
887 * add another */
888 if(next_vt == VT_DISPATCH || next_vt == VT_UNKNOWN) {
889 chat("encode_type: skipping ptr\n");
890 *encoded_type = target_type;
891 *decoded_size = child_size;
892 break;
895 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
896 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
897 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
900 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
901 int mix_field;
903 if (target_type & 0x80000000) {
904 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
905 } else {
906 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
907 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
910 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
911 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
913 typedata[0] = (mix_field << 16) | VT_PTR;
914 typedata[1] = target_type;
917 *encoded_type = typeoffset;
919 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
920 break;
923 case VT_SAFEARRAY:
925 type_t *element_type = type_alias_get_aliasee_type(type_array_get_element_type(type));
926 int next_vt = get_type_vt(element_type);
928 encode_type(typelib, next_vt, type_alias_get_aliasee_type(type_array_get_element_type(type)),
929 &target_type, &child_size);
931 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
932 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
933 if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
936 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
937 int mix_field;
939 if (target_type & 0x80000000) {
940 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
941 } else {
942 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
943 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
946 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
947 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
949 typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
950 typedata[1] = target_type;
953 *encoded_type = typeoffset;
955 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
956 break;
959 case VT_USERDEFINED:
961 importinfo_t *importinfo;
962 int typeinfo_offset;
964 if (type->typelib_idx > -1)
966 chat("encode_type: VT_USERDEFINED - found already defined type %s at %d\n",
967 type->name, type->typelib_idx);
968 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
970 else if ((importinfo = find_importinfo(typelib, type->name)))
972 chat("encode_type: VT_USERDEFINED - found imported type %s in %s\n",
973 type->name, importinfo->importlib->name);
974 alloc_importinfo(typelib, importinfo);
975 typeinfo_offset = importinfo->offset | 0x1;
977 else
979 /* Typedefs without the [public] attribute aren't included in the
980 * typelib, unless the aliasee is an anonymous UDT or the typedef
981 * is wire-marshalled. In the latter case the wire-marshal type,
982 * which may be a non-public alias, is used instead. */
983 while (type_is_alias(type))
985 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
987 type = get_attrp(type->attrs, ATTR_WIREMARSHAL);
988 break;
990 else if (!is_attr(type->attrs, ATTR_PUBLIC))
991 type = type_alias_get_aliasee_type(type);
992 else
993 break;
996 chat("encode_type: VT_USERDEFINED - adding new type %s, real type %d\n",
997 type->name, type_get_type(type));
999 switch (type_get_type_detect_alias(type))
1001 case TYPE_STRUCT:
1002 case TYPE_ENCAPSULATED_UNION:
1003 add_structure_typeinfo(typelib, type);
1004 break;
1005 case TYPE_INTERFACE:
1006 add_interface_typeinfo(typelib, type);
1007 break;
1008 case TYPE_ENUM:
1009 add_enum_typeinfo(typelib, type);
1010 break;
1011 case TYPE_UNION:
1012 add_union_typeinfo(typelib, type);
1013 break;
1014 case TYPE_COCLASS:
1015 add_coclass_typeinfo(typelib, type);
1016 break;
1017 case TYPE_ALIAS:
1018 add_typedef_typeinfo(typelib, type);
1019 break;
1020 default:
1021 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
1022 type_get_type(type));
1025 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
1027 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1028 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1029 if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == typeinfo_offset)) break;
1032 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1033 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1034 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1036 typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1037 typedata[1] = typeinfo_offset;
1040 *encoded_type = typeoffset;
1041 break;
1044 default:
1045 error("encode_type: unrecognized type %d.\n", vt);
1046 *encoded_type = default_type;
1047 break;
1050 return 0;
1053 static void dump_type(type_t *t)
1055 chat("dump_type: %p name %s type %d attrs %p\n", t, t->name, type_get_type(t), t->attrs);
1058 static int encode_var(
1059 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
1060 type_t *type, /* [I] The type description to encode. */
1061 var_t *var, /* [I] The var to encode. */
1062 int *encoded_type, /* [O] The encoded type description. */
1063 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1065 int typeoffset;
1066 int *typedata;
1067 int target_type;
1068 int child_size;
1069 int vt;
1070 int scratch;
1072 if (!decoded_size) decoded_size = &scratch;
1073 *decoded_size = 0;
1075 chat("encode_var: var %p type %p type->name %s\n",
1076 var, type, type->name ? type->name : "NULL");
1078 if (is_array(type) && !type_array_is_decl_as_ptr(type)) {
1079 int num_dims, elements = 1, arrayoffset;
1080 type_t *atype;
1081 int *arraydata;
1083 num_dims = 0;
1084 for (atype = type;
1085 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1086 atype = type_array_get_element_type(atype))
1087 ++num_dims;
1089 chat("array with %d dimensions\n", num_dims);
1090 encode_var(typelib, atype, var, &target_type, NULL);
1091 arrayoffset = ctl2_alloc_segment(typelib, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1092 arraydata = (void *)&typelib->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1094 arraydata[0] = target_type;
1095 arraydata[1] = num_dims;
1096 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1098 arraydata += 2;
1099 for (atype = type;
1100 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1101 atype = type_array_get_element_type(atype))
1103 arraydata[0] = type_array_get_dim(atype);
1104 arraydata[1] = 0;
1105 arraydata += 2;
1106 elements *= type_array_get_dim(atype);
1109 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1110 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1112 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1113 typedata[1] = arrayoffset;
1115 *encoded_type = typeoffset;
1116 *decoded_size = 20 /*sizeof(ARRAYDESC)*/ + (num_dims - 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1117 return 0;
1120 vt = get_type_vt(type);
1121 if (vt == VT_PTR) {
1122 type_t *ref = is_ptr(type) ?
1123 type_pointer_get_ref_type(type) : type_array_get_element_type(type);
1124 int skip_ptr = encode_var(typelib, ref, var, &target_type, &child_size);
1126 if(skip_ptr == 2) {
1127 chat("encode_var: skipping ptr\n");
1128 *encoded_type = target_type;
1129 *decoded_size = child_size;
1130 return 0;
1133 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1134 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1135 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1138 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1139 int mix_field;
1141 if (target_type & 0x80000000) {
1142 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1143 } else if (get_type_vt(ref) == VT_SAFEARRAY) {
1144 type_t *element_type = type_alias_get_aliasee_type(type_array_get_element_type(ref));
1145 mix_field = get_type_vt(element_type) | VT_ARRAY | VT_BYREF;
1146 } else {
1147 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1148 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1151 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1152 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1154 typedata[0] = (mix_field << 16) | VT_PTR;
1155 typedata[1] = target_type;
1158 *encoded_type = typeoffset;
1160 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1161 return 0;
1164 dump_type(type);
1166 encode_type(typelib, vt, type, encoded_type, decoded_size);
1167 /* these types already have an implicit pointer, so we don't need to
1168 * add another */
1169 if(vt == VT_DISPATCH || vt == VT_UNKNOWN) return 2;
1170 return 0;
1173 static unsigned int get_ulong_val(unsigned int val, int vt)
1175 switch(vt) {
1176 case VT_I2:
1177 case VT_BOOL:
1178 case VT_UI2:
1179 return val & 0xffff;
1180 case VT_I1:
1181 case VT_UI1:
1182 return val & 0xff;
1185 return val;
1188 static void write_int_value(msft_typelib_t *typelib, int *out, int vt, int value)
1190 const unsigned int lv = get_ulong_val(value, vt);
1191 if ((lv & 0x3ffffff) == lv) {
1192 *out = 0x80000000;
1193 *out |= vt << 26;
1194 *out |= lv;
1195 } else {
1196 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1197 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1198 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &value, 4);
1199 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1200 *out = offset;
1204 static void write_string_value(msft_typelib_t *typelib, int *out, const char *value)
1206 int len = strlen(value), seg_len = (len + 6 + 3) & ~0x3;
1207 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1208 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = VT_BSTR;
1209 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &len, sizeof(len));
1210 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1211 len += 6;
1212 while(len < seg_len) {
1213 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1214 len++;
1216 *out = offset;
1219 static void write_default_value(msft_typelib_t *typelib, type_t *type, expr_t *expr, int *out)
1221 int vt;
1223 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT) {
1224 if (get_type_vt(type) != VT_BSTR)
1225 error("string default value applied to non-string type\n");
1226 chat("default value '%s'\n", expr->u.sval);
1227 write_string_value(typelib, out, expr->u.sval);
1228 return;
1231 if (type_get_type(type) == TYPE_ENUM) {
1232 vt = VT_I4;
1233 } else if (is_ptr(type)) {
1234 vt = get_type_vt(type_pointer_get_ref_type(type));
1235 if (vt == VT_USERDEFINED)
1236 vt = VT_I4;
1237 if (expr->cval)
1238 warning("non-null pointer default value\n");
1239 } else {
1240 vt = get_type_vt(type);
1241 switch(vt) {
1242 case VT_I2:
1243 case VT_I4:
1244 case VT_R4:
1245 case VT_BOOL:
1246 case VT_I1:
1247 case VT_UI1:
1248 case VT_UI2:
1249 case VT_UI4:
1250 case VT_INT:
1251 case VT_UINT:
1252 case VT_HRESULT:
1253 break;
1254 default:
1255 warning("can't write value of type %d yet\n", vt);
1256 return;
1260 write_int_value(typelib, out, vt, expr->cval);
1263 static HRESULT set_custdata(msft_typelib_t *typelib, REFGUID guid,
1264 int vt, const void *value, int *offset)
1266 MSFT_GuidEntry guidentry;
1267 int guidoffset;
1268 int custoffset;
1269 int *custdata;
1270 int data_out;
1272 guidentry.guid = *guid;
1274 guidentry.hreftype = -1;
1275 guidentry.next_hash = -1;
1277 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1278 if(vt == VT_BSTR)
1279 write_string_value(typelib, &data_out, value);
1280 else
1281 write_int_value(typelib, &data_out, vt, *(int*)value);
1283 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1285 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1286 custdata[0] = guidoffset;
1287 custdata[1] = data_out;
1288 custdata[2] = *offset;
1289 *offset = custoffset;
1291 return S_OK;
1294 static HRESULT add_func_desc(msft_typeinfo_t* typeinfo, var_t *func, int index)
1296 int offset, name_offset;
1297 int *typedata, typedata_size;
1298 int i, id, next_idx;
1299 int decoded_size, extra_attr = 0;
1300 int num_params = 0, num_optional = 0, num_defaults = 0;
1301 var_t *arg;
1302 unsigned char *namedata;
1303 const attr_t *attr;
1304 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1305 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1306 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1307 int entry = -1, entry_is_ord = 0;
1308 int lcid_retval_count = 0;
1310 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1312 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1314 switch(typeinfo->typekind) {
1315 case TKIND_DISPATCH:
1316 funckind = 0x4; /* FUNC_DISPATCH */
1317 break;
1318 case TKIND_MODULE:
1319 funckind = 0x3; /* FUNC_STATIC */
1320 break;
1321 default:
1322 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1323 break;
1326 if (is_local( func->attrs )) {
1327 chat("add_func_desc: skipping local function\n");
1328 return S_FALSE;
1331 if (type_function_get_args(func->declspec.type))
1332 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1334 num_params++;
1335 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1336 if(attr->type == ATTR_DEFAULTVALUE)
1337 num_defaults++;
1338 else if(attr->type == ATTR_OPTIONAL)
1339 num_optional++;
1343 chat("add_func_desc: num of params %d\n", num_params);
1345 name_offset = ctl2_alloc_name(typeinfo->typelib, func->name);
1347 if (func->attrs) LIST_FOR_EACH_ENTRY( attr, func->attrs, const attr_t, entry ) {
1348 expr_t *expr = attr->u.pval;
1349 switch(attr->type) {
1350 case ATTR_BINDABLE:
1351 funcflags |= 0x4; /* FUNCFLAG_FBINDABLE */
1352 break;
1353 case ATTR_DEFAULTBIND:
1354 funcflags |= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1355 break;
1356 case ATTR_DEFAULTCOLLELEM:
1357 funcflags |= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1358 break;
1359 case ATTR_DISPLAYBIND:
1360 funcflags |= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1361 break;
1362 case ATTR_ENTRY:
1363 extra_attr = max(extra_attr, 3);
1364 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1365 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1366 else {
1367 entry = expr->cval;
1368 entry_is_ord = 1;
1370 break;
1371 case ATTR_HELPCONTEXT:
1372 extra_attr = max(extra_attr, 1);
1373 help_context = expr->u.lval;
1374 break;
1375 case ATTR_HELPSTRING:
1376 extra_attr = max(extra_attr, 2);
1377 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1378 break;
1379 case ATTR_HELPSTRINGCONTEXT:
1380 extra_attr = max(extra_attr, 6);
1381 help_string_context = expr->u.lval;
1382 break;
1383 case ATTR_HIDDEN:
1384 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1385 break;
1386 case ATTR_ID:
1387 id = expr->cval;
1388 break;
1389 case ATTR_IMMEDIATEBIND:
1390 funcflags |= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1391 break;
1392 case ATTR_NONBROWSABLE:
1393 funcflags |= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1394 break;
1395 case ATTR_OUT:
1396 break;
1397 case ATTR_PROPGET:
1398 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1399 break;
1400 case ATTR_PROPPUT:
1401 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1402 break;
1403 case ATTR_PROPPUTREF:
1404 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1405 break;
1406 /* FIXME: FUNCFLAG_FREPLACEABLE */
1407 case ATTR_REQUESTEDIT:
1408 funcflags |= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1409 break;
1410 case ATTR_RESTRICTED:
1411 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1412 break;
1413 case ATTR_SOURCE:
1414 funcflags |= 0x2; /* FUNCFLAG_FSOURCE */
1415 break;
1416 case ATTR_UIDEFAULT:
1417 funcflags |= 0x200; /* FUNCFLAG_FUIDEFAULT */
1418 break;
1419 case ATTR_USESGETLASTERROR:
1420 funcflags |= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1421 break;
1422 case ATTR_VARARG:
1423 if (num_optional || num_defaults)
1424 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1425 else
1426 num_optional = -1;
1427 break;
1428 default:
1429 break;
1433 /* allocate type data space for us */
1434 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1436 if (!typeinfo->func_data) {
1437 typeinfo->func_data = xmalloc(0x100);
1438 typeinfo->func_data_allocated = 0x100;
1439 typeinfo->func_data[0] = 0;
1442 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1443 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1444 typeinfo->func_data[0] + typedata_size + sizeof(int));
1445 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1448 offset = typeinfo->func_data[0];
1449 typeinfo->func_data[0] += typedata_size;
1450 typedata = typeinfo->func_data + (offset >> 2) + 1;
1453 /* find func with the same name - if it exists use its id */
1454 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1455 if(name_offset == typeinfo->func_names[i]) {
1456 id = typeinfo->func_indices[i];
1457 break;
1461 /* find the first func with the same id and link via the hiword of typedata[4] */
1462 next_idx = index;
1463 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1464 if(id == typeinfo->func_indices[i]) {
1465 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1466 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1467 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1468 break;
1472 /* fill out the basic type information */
1473 typedata[0] = typedata_size | (index << 16);
1474 encode_var(typeinfo->typelib, type_function_get_rettype(func->declspec.type), func,
1475 &typedata[1], &decoded_size);
1476 typedata[2] = funcflags;
1477 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1478 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1479 if(num_defaults) typedata[4] |= 0x1000;
1480 if(entry_is_ord) typedata[4] |= 0x2000;
1481 typedata[5] = (num_optional << 16) | num_params;
1483 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1484 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1485 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1486 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1488 switch(extra_attr) {
1489 case 6: typedata[11] = help_string_context;
1490 case 5: typedata[10] = -1;
1491 case 4: typedata[9] = -1;
1492 case 3: typedata[8] = entry;
1493 case 2: typedata[7] = help_string_offset;
1494 case 1: typedata[6] = help_context;
1495 case 0:
1496 break;
1497 default:
1498 warning("unknown number of optional attrs\n");
1501 if (type_function_get_args(func->declspec.type))
1503 i = 0;
1504 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1506 int paramflags = 0;
1507 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1508 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1510 if(defaultdata) *defaultdata = -1;
1512 encode_var(typeinfo->typelib, arg->declspec.type, arg, paramdata, &decoded_size);
1513 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1514 switch(attr->type) {
1515 case ATTR_DEFAULTVALUE:
1517 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1518 write_default_value(typeinfo->typelib, arg->declspec.type, (expr_t *)attr->u.pval, defaultdata);
1519 break;
1521 case ATTR_IN:
1522 paramflags |= 0x01; /* PARAMFLAG_FIN */
1523 break;
1524 case ATTR_OPTIONAL:
1525 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1526 break;
1527 case ATTR_OUT:
1528 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1529 break;
1530 case ATTR_PARAMLCID:
1531 paramflags |= 0x04; /* PARAMFLAG_LCID */
1532 lcid_retval_count++;
1533 break;
1534 case ATTR_RETVAL:
1535 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1536 lcid_retval_count++;
1537 break;
1538 default:
1539 chat("unhandled param attr %d\n", attr->type);
1540 break;
1543 paramdata[1] = -1;
1544 paramdata[2] = paramflags;
1545 typedata[3] += decoded_size << 16;
1547 i++;
1551 if(lcid_retval_count == 1)
1552 typedata[4] |= 0x4000;
1553 else if(lcid_retval_count == 2)
1554 typedata[4] |= 0x8000;
1556 if(typeinfo->funcs_allocated == 0) {
1557 typeinfo->funcs_allocated = 10;
1558 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1559 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1560 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1562 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1563 typeinfo->funcs_allocated *= 2;
1564 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1565 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1566 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1569 /* update the index data */
1570 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1571 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1572 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1574 /* ??? */
1575 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1576 typeinfo->typeinfo->res2 <<= 1;
1577 /* ??? */
1578 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1580 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1581 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1582 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1584 /* adjust size of VTBL */
1585 if(funckind != 0x3 /* FUNC_STATIC */)
1586 typeinfo->typeinfo->cbSizeVft += pointer_size;
1588 /* Increment the number of function elements */
1589 typeinfo->typeinfo->cElement += 1;
1591 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1592 if (*((INT *)namedata) == -1) {
1593 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1594 if(typeinfo->typekind == TKIND_MODULE)
1595 namedata[9] |= 0x10;
1596 } else
1597 namedata[9] &= ~0x10;
1599 if(typeinfo->typekind == TKIND_MODULE)
1600 namedata[9] |= 0x20;
1602 if (type_function_get_args(func->declspec.type))
1604 i = 0;
1605 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1607 /* don't give the last arg of a [propput*] func a name */
1608 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1610 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1611 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1612 paramdata[1] = offset;
1614 i++;
1617 return S_OK;
1620 static HRESULT add_var_desc(msft_typeinfo_t *typeinfo, UINT index, var_t* var)
1622 int offset, id;
1623 unsigned int typedata_size;
1624 INT *typedata;
1625 unsigned int var_datawidth, var_alignment = 0;
1626 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1627 int alignment;
1628 int varflags = 0;
1629 const attr_t *attr;
1630 unsigned char *namedata;
1631 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1633 if (!var->name)
1634 var->name = gen_name();
1636 chat("add_var_desc(%d, %s)\n", index, var->name);
1638 id = 0x40000000 + index;
1640 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1641 expr_t *expr = attr->u.pval;
1642 switch(attr->type) {
1643 case ATTR_BINDABLE:
1644 varflags |= 0x04; /* VARFLAG_FBINDABLE */
1645 break;
1646 case ATTR_DEFAULTBIND:
1647 varflags |= 0x20; /* VARFLAG_FDEFAULTBIND */
1648 break;
1649 case ATTR_DEFAULTCOLLELEM:
1650 varflags |= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1651 break;
1652 case ATTR_DISPLAYBIND:
1653 varflags |= 0x10; /* VARFLAG_FDISPLAYBIND */
1654 break;
1655 case ATTR_HIDDEN:
1656 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1657 break;
1658 case ATTR_ID:
1659 id = expr->cval;
1660 break;
1661 case ATTR_IMMEDIATEBIND:
1662 varflags |= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1663 break;
1664 case ATTR_NONBROWSABLE:
1665 varflags |= 0x400; /* VARFLAG_FNONBROWSABLE */
1666 break;
1667 case ATTR_READONLY:
1668 varflags |= 0x01; /* VARFLAG_FREADONLY */
1669 break;
1670 /* FIXME: VARFLAG_FREPLACEABLE */
1671 case ATTR_REQUESTEDIT:
1672 varflags |= 0x08; /* VARFLAG_FREQUESTEDIT */
1673 break;
1674 case ATTR_RESTRICTED:
1675 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1676 break;
1677 case ATTR_SOURCE:
1678 varflags |= 0x02; /* VARFLAG_FSOURCE */
1679 break;
1680 case ATTR_UIDEFAULT:
1681 varflags |= 0x0200; /* VARFLAG_FUIDEFAULT */
1682 break;
1683 default:
1684 break;
1688 /* allocate type data space for us */
1689 typedata_size = 0x14;
1691 if (!typeinfo->var_data) {
1692 typeinfo->var_data = xmalloc(0x100);
1693 typeinfo->var_data_allocated = 0x100;
1694 typeinfo->var_data[0] = 0;
1697 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1698 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1699 typeinfo->var_data[0] + typedata_size + sizeof(int));
1700 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1703 offset = typeinfo->var_data[0];
1704 typeinfo->var_data[0] += typedata_size;
1705 typedata = typeinfo->var_data + (offset >> 2) + 1;
1707 /* fill out the basic type information */
1708 typedata[0] = typedata_size | (index << 16);
1709 typedata[2] = varflags;
1710 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1712 if(typeinfo->vars_allocated == 0) {
1713 typeinfo->vars_allocated = 10;
1714 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1715 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1716 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1718 if(typeinfo->vars_allocated == var_num) {
1719 typeinfo->vars_allocated *= 2;
1720 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1721 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1722 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1724 /* update the index data */
1725 typeinfo->var_indices[var_num] = id;
1726 typeinfo->var_names[var_num] = -1;
1727 typeinfo->var_offsets[var_num] = offset;
1729 /* figure out type widths and whatnot */
1730 var_datawidth = type_memsize_and_alignment(var->declspec.type, &var_alignment);
1731 encode_var(typeinfo->typelib, var->declspec.type, var, &typedata[1], &var_type_size);
1733 /* pad out starting position to data width */
1734 typeinfo->datawidth += var_alignment - 1;
1735 typeinfo->datawidth &= ~(var_alignment - 1);
1737 switch(typeinfo->typekind) {
1738 case TKIND_ENUM:
1739 write_int_value(typeinfo->typelib, &typedata[4], VT_I4, var->eval->cval);
1740 var_kind = 2; /* VAR_CONST */
1741 var_type_size += 16; /* sizeof(VARIANT) */
1742 typeinfo->datawidth = var_datawidth;
1743 break;
1744 case TKIND_RECORD:
1745 typedata[4] = typeinfo->datawidth;
1746 typeinfo->datawidth += var_datawidth;
1747 break;
1748 case TKIND_UNION:
1749 typedata[4] = typeinfo->datawidth;
1750 typeinfo->datawidth = max(typeinfo->datawidth, var_datawidth);
1751 break;
1752 case TKIND_DISPATCH:
1753 var_kind = 3; /* VAR_DISPATCH */
1754 typeinfo->datawidth = pointer_size;
1755 break;
1756 default:
1757 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1758 break;
1761 /* add type description size to total required allocation */
1762 typedata[3] += var_type_size << 16 | var_kind;
1764 /* fix type alignment */
1765 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1766 if (alignment < var_alignment) {
1767 alignment = var_alignment;
1768 typeinfo->typeinfo->typekind &= ~0xffc0;
1769 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1772 /* ??? */
1773 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1774 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1775 typeinfo->typeinfo->res2 <<= 1;
1778 /* ??? */
1779 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1780 typeinfo->typeinfo->res3 += 0x2c;
1782 /* increment the number of variable elements */
1783 typeinfo->typeinfo->cElement += 0x10000;
1785 /* pad data width to alignment */
1786 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1788 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1789 if (offset == -1) return E_OUTOFMEMORY;
1791 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1792 if (*((INT *)namedata) == -1) {
1793 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1794 if(typeinfo->typekind != TKIND_DISPATCH)
1795 namedata[9] |= 0x10;
1796 } else
1797 namedata[9] &= ~0x10;
1799 if (typeinfo->typekind == TKIND_ENUM) {
1800 namedata[9] |= 0x20;
1802 typeinfo->var_names[var_num] = offset;
1804 return S_OK;
1807 static HRESULT add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1809 if(importinfo) {
1810 alloc_importinfo(typeinfo->typelib, importinfo);
1811 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1812 }else {
1813 if(ref->typelib_idx == -1)
1814 add_interface_typeinfo(typeinfo->typelib, ref);
1815 if(ref->typelib_idx == -1)
1816 error("add_impl_type: unable to add inherited interface\n");
1818 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1821 typeinfo->typeinfo->cImplTypes++;
1822 return S_OK;
1825 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1826 const char *name, const attr_list_t *attrs)
1828 const attr_t *attr;
1829 msft_typeinfo_t *msft_typeinfo;
1830 int nameoffset;
1831 int typeinfo_offset;
1832 MSFT_TypeInfoBase *typeinfo;
1833 MSFT_GuidEntry guidentry;
1835 chat("create_msft_typeinfo: name %s kind %d index %d\n", name, kind, typelib->typelib_header.nrtypeinfos);
1837 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1838 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1840 msft_typeinfo->typelib = typelib;
1842 nameoffset = ctl2_alloc_name(typelib, name);
1843 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1844 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1846 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1847 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1849 msft_typeinfo->typekind = kind;
1850 msft_typeinfo->typeinfo = typeinfo;
1852 typeinfo->typekind |= kind | 0x20;
1854 if(kind == TKIND_COCLASS)
1855 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1857 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1858 switch(attr->type) {
1859 case ATTR_AGGREGATABLE:
1860 if (kind == TKIND_COCLASS)
1861 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1862 break;
1864 case ATTR_APPOBJECT:
1865 if (kind == TKIND_COCLASS)
1866 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1867 break;
1869 case ATTR_CONTROL:
1870 if (kind == TKIND_COCLASS)
1871 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1872 break;
1874 case ATTR_DLLNAME:
1876 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1877 typeinfo->datatype1 = offset;
1878 break;
1881 case ATTR_DUAL:
1882 /* FIXME: check interface is compatible */
1883 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1884 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1885 break;
1887 case ATTR_HELPCONTEXT:
1889 expr_t *expr = (expr_t*)attr->u.pval;
1890 typeinfo->helpcontext = expr->cval;
1891 break;
1893 case ATTR_HELPSTRING:
1895 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1896 if (offset == -1) break;
1897 typeinfo->docstringoffs = offset;
1898 break;
1900 case ATTR_HELPSTRINGCONTEXT:
1902 expr_t *expr = (expr_t*)attr->u.pval;
1903 typeinfo->helpstringcontext = expr->cval;
1904 break;
1906 case ATTR_HIDDEN:
1907 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1908 break;
1910 case ATTR_LICENSED:
1911 typeinfo->flags |= 0x04; /* TYPEFLAG_FLICENSED */
1912 break;
1914 case ATTR_NONCREATABLE:
1915 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1916 break;
1918 case ATTR_NONEXTENSIBLE:
1919 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1920 break;
1922 case ATTR_OLEAUTOMATION:
1923 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1924 break;
1926 /* FIXME: TYPEFLAG_FPREDCLID */
1928 case ATTR_PROXY:
1929 typeinfo->flags |= 0x4000; /* TYPEFLAG_FPROXY */
1930 break;
1932 /* FIXME: TYPEFLAG_FREPLACEABLE */
1934 case ATTR_RESTRICTED:
1935 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
1936 break;
1938 case ATTR_UUID:
1939 guidentry.guid = *(GUID*)attr->u.pval;
1940 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1941 guidentry.next_hash = -1;
1942 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
1943 #if 0
1944 if (IsEqualIID(guid, &IID_IDispatch)) {
1945 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1947 #endif
1948 break;
1950 case ATTR_VERSION:
1951 typeinfo->version = attr->u.ival;
1952 break;
1954 default:
1955 break;
1959 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
1960 typelib->last_typeinfo = msft_typeinfo;
1961 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
1964 return msft_typeinfo;
1967 static void add_dispatch(msft_typelib_t *typelib)
1969 int guid_offset, impfile_offset, hash_key;
1970 MSFT_GuidEntry guidentry;
1971 MSFT_ImpInfo impinfo;
1972 GUID stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1973 GUID iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1975 if(typelib->typelib_header.dispatchpos != -1) return;
1977 guidentry.guid = stdole;
1978 guidentry.hreftype = 2;
1979 guidentry.next_hash = -1;
1980 hash_key = ctl2_hash_guid(&guidentry.guid);
1981 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
1982 if (guid_offset == -1)
1983 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
1984 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
1986 guidentry.guid = iid_idispatch;
1987 guidentry.hreftype = 1;
1988 guidentry.next_hash = -1;
1989 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
1990 impinfo.oImpFile = impfile_offset;
1991 hash_key = ctl2_hash_guid(&guidentry.guid);
1992 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
1993 if (guid_offset == -1)
1994 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
1995 impinfo.oGuid = guid_offset;
1996 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
1999 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
2001 int num_parents = 0, num_funcs = 0;
2002 importinfo_t *importinfo = NULL;
2003 const statement_t *stmt_func;
2004 type_t *inherit, *ref;
2005 int idx = 0;
2006 var_t *func;
2007 var_t *var;
2008 msft_typeinfo_t *msft_typeinfo;
2010 if (-1 < dispinterface->typelib_idx)
2011 return;
2013 inherit = type_dispiface_get_inherit(dispinterface);
2015 if (inherit)
2017 importinfo = find_importinfo(typelib, inherit->name);
2019 if (!importinfo && type_iface_get_inherit(inherit) && inherit->typelib_idx == -1)
2020 add_interface_typeinfo(typelib, inherit);
2023 /* check typelib_idx again, it could have been added while resolving the parent interface */
2024 if (-1 < dispinterface->typelib_idx)
2025 return;
2027 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2028 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
2029 dispinterface->attrs);
2031 msft_typeinfo->typeinfo->size = pointer_size;
2032 msft_typeinfo->typeinfo->typekind |= pointer_size << 11 | pointer_size << 6;
2034 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2035 add_dispatch(typelib);
2037 if (inherit)
2039 add_impl_type(msft_typeinfo, inherit, importinfo);
2040 msft_typeinfo->typeinfo->typekind |= 0x10;
2043 /* count the number of inherited interfaces and non-local functions */
2044 for (ref = inherit; ref; ref = type_iface_get_inherit(ref))
2046 num_parents++;
2047 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) )
2049 var_t *func = stmt_func->u.var;
2050 if (!is_local(func->attrs)) num_funcs++;
2053 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2054 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2056 msft_typeinfo->typeinfo->cImplTypes = 1; /* IDispatch */
2058 /* count the no of methods, as the variable indices come after the funcs */
2059 if (dispinterface->details.iface->disp_methods)
2060 LIST_FOR_EACH_ENTRY( func, dispinterface->details.iface->disp_methods, var_t, entry )
2061 idx++;
2063 if (type_dispiface_get_props(dispinterface))
2064 LIST_FOR_EACH_ENTRY( var, type_dispiface_get_props(dispinterface), var_t, entry )
2065 add_var_desc(msft_typeinfo, idx++, var);
2067 if (type_dispiface_get_methods(dispinterface))
2069 idx = 0;
2070 LIST_FOR_EACH_ENTRY( func, type_dispiface_get_methods(dispinterface), var_t, entry )
2071 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2072 idx++;
2075 typelib->typelib->reg_ifaces = xrealloc(typelib->typelib->reg_ifaces,
2076 (typelib->typelib->reg_iface_count + 1) * sizeof(dispinterface));
2077 typelib->typelib->reg_ifaces[typelib->typelib->reg_iface_count++] = dispinterface;
2080 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
2082 int idx = 0;
2083 const statement_t *stmt_func;
2084 type_t *ref;
2085 msft_typeinfo_t *msft_typeinfo;
2086 importinfo_t *ref_importinfo = NULL;
2087 int num_parents = 0, num_funcs = 0;
2088 type_t *inherit;
2089 const type_t *derived;
2091 if (-1 < interface->typelib_idx)
2092 return;
2094 if (!interface->details.iface)
2096 error( "interface %s is referenced but not defined\n", interface->name );
2097 return;
2100 if (is_attr(interface->attrs, ATTR_DISPINTERFACE)) {
2101 add_dispinterface_typeinfo(typelib, interface);
2102 return;
2105 /* midl adds the parent interface first, unless the parent itself
2106 has no parent (i.e. it stops before IUnknown). */
2108 inherit = type_iface_get_inherit(interface);
2110 if(inherit) {
2111 ref_importinfo = find_importinfo(typelib, inherit->name);
2113 if(!ref_importinfo && type_iface_get_inherit(inherit) &&
2114 inherit->typelib_idx == -1)
2115 add_interface_typeinfo(typelib, inherit);
2118 /* check typelib_idx again, it could have been added while resolving the parent interface */
2119 if (-1 < interface->typelib_idx)
2120 return;
2122 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2123 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
2124 msft_typeinfo->typeinfo->size = pointer_size;
2125 msft_typeinfo->typeinfo->typekind |= 0x0200;
2126 msft_typeinfo->typeinfo->typekind |= pointer_size << 11;
2128 for (derived = inherit; derived; derived = type_iface_get_inherit(derived))
2129 if (derived->name && !strcmp(derived->name, "IDispatch"))
2130 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2132 if(type_iface_get_inherit(interface))
2133 add_impl_type(msft_typeinfo, type_iface_get_inherit(interface),
2134 ref_importinfo);
2136 /* count the number of inherited interfaces and non-local functions */
2137 for(ref = inherit; ref; ref = type_iface_get_inherit(ref)) {
2138 num_parents++;
2139 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) ) {
2140 var_t *func = stmt_func->u.var;
2141 if (!is_local(func->attrs)) num_funcs++;
2144 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2145 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2147 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(interface) ) {
2148 var_t *func = stmt_func->u.var;
2149 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2150 idx++;
2153 if (is_attr(interface->attrs, ATTR_OLEAUTOMATION) || is_attr(interface->attrs, ATTR_DUAL))
2155 typelib->typelib->reg_ifaces = xrealloc(typelib->typelib->reg_ifaces,
2156 (typelib->typelib->reg_iface_count + 1) * sizeof(interface));
2157 typelib->typelib->reg_ifaces[typelib->typelib->reg_iface_count++] = interface;
2161 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2163 var_list_t *fields;
2164 int idx = 0;
2165 var_t *cur;
2166 msft_typeinfo_t *msft_typeinfo;
2168 if (-1 < structure->typelib_idx)
2169 return;
2171 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2172 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2173 msft_typeinfo->typeinfo->size = 0;
2175 if (type_get_type(structure) == TYPE_STRUCT)
2176 fields = type_struct_get_fields(structure);
2177 else
2178 fields = type_encapsulated_union_get_fields(structure);
2180 if (fields)
2182 LIST_FOR_EACH_ENTRY( cur, fields, var_t, entry )
2183 add_var_desc(msft_typeinfo, idx++, cur);
2187 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2189 int idx = 0;
2190 var_t *cur;
2191 msft_typeinfo_t *msft_typeinfo;
2193 if (-1 < enumeration->typelib_idx)
2194 return;
2196 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2197 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2198 msft_typeinfo->typeinfo->size = 0;
2200 if (type_enum_get_values(enumeration))
2201 LIST_FOR_EACH_ENTRY( cur, type_enum_get_values(enumeration), var_t, entry )
2202 add_var_desc(msft_typeinfo, idx++, cur);
2205 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion)
2207 int idx = 0;
2208 var_t *cur;
2209 msft_typeinfo_t *msft_typeinfo;
2211 if (-1 < tunion->typelib_idx)
2212 return;
2214 if (!tunion->name)
2215 tunion->name = gen_name();
2217 tunion->typelib_idx = typelib->typelib_header.nrtypeinfos;
2218 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_UNION, tunion->name, tunion->attrs);
2219 msft_typeinfo->typeinfo->size = 0;
2221 if (type_union_get_cases(tunion))
2222 LIST_FOR_EACH_ENTRY(cur, type_union_get_cases(tunion), var_t, entry)
2223 add_var_desc(msft_typeinfo, idx++, cur);
2226 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2228 msft_typeinfo_t *msft_typeinfo = NULL;
2229 int datatype1, datatype2, duplicate = 0;
2230 unsigned int size, alignment = 0;
2231 type_t *type;
2233 if (-1 < tdef->typelib_idx)
2234 return;
2236 type = type_alias_get_aliasee_type(tdef);
2238 if (!type->name || strcmp(tdef->name, type->name) != 0)
2240 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2241 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2243 else
2244 duplicate = 1;
2246 encode_type(typelib, get_type_vt(type), type, &datatype1, &datatype2);
2247 size = type_memsize_and_alignment(type, &alignment);
2249 if (msft_typeinfo)
2251 msft_typeinfo->typeinfo->datatype1 = datatype1;
2252 msft_typeinfo->typeinfo->size = size;
2253 msft_typeinfo->typeinfo->datatype2 = datatype2;
2254 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2257 /* avoid adding duplicate type definitions */
2258 if (duplicate)
2259 tdef->typelib_idx = type->typelib_idx;
2262 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2264 msft_typeinfo_t *msft_typeinfo;
2265 ifref_t *iref;
2266 int num_ifaces = 0, offset, i;
2267 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2268 int have_default = 0, have_default_source = 0;
2269 const attr_t *attr;
2270 ifref_list_t *ifaces;
2272 if (-1 < cls->typelib_idx)
2273 return;
2275 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2276 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2278 ifaces = type_coclass_get_ifaces(cls);
2279 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) num_ifaces++;
2281 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2282 num_ifaces * sizeof(*ref), 0);
2284 i = 0;
2285 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) {
2286 if(iref->iface->typelib_idx == -1)
2287 add_interface_typeinfo(typelib, iref->iface);
2288 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2289 ref->reftype = typelib->typelib_typeinfo_offsets[iref->iface->typelib_idx];
2290 ref->flags = 0;
2291 ref->oCustData = -1;
2292 ref->onext = -1;
2293 if(i < num_ifaces - 1)
2294 ref->onext = offset + (i + 1) * sizeof(*ref);
2296 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2297 switch(attr->type) {
2298 case ATTR_DEFAULT:
2299 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2300 break;
2301 case ATTR_DEFAULTVTABLE:
2302 ref->flags |= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2303 break;
2304 case ATTR_RESTRICTED:
2305 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2306 break;
2307 case ATTR_SOURCE:
2308 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2309 break;
2310 default:
2311 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2314 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2315 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2316 have_default_source = 1;
2317 else
2318 have_default = 1;
2321 /* If the interface is non-restricted and we haven't already had one then
2322 remember it so that we can use it as a default later */
2323 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2324 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2325 if(!first_source)
2326 first_source = ref;
2328 else if(!first)
2329 first = ref;
2331 i++;
2334 /* If we haven't had a default interface, then set the default flags on the
2335 first ones */
2336 if(!have_default && first)
2337 first->flags |= 0x1;
2338 if(!have_default_source && first_source)
2339 first_source->flags |= 0x1;
2341 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2342 msft_typeinfo->typeinfo->size = pointer_size;
2343 msft_typeinfo->typeinfo->typekind |= 0x2200;
2346 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2348 int idx = 0;
2349 const statement_t *stmt;
2350 msft_typeinfo_t *msft_typeinfo;
2352 if (-1 < module->typelib_idx)
2353 return;
2355 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2356 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2357 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2359 STATEMENTS_FOR_EACH_FUNC( stmt, module->details.module->stmts ) {
2360 var_t *func = stmt->u.var;
2361 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2362 idx++;
2365 msft_typeinfo->typeinfo->size = idx;
2368 static void add_type_typeinfo(msft_typelib_t *typelib, type_t *type)
2370 switch (type_get_type(type)) {
2371 case TYPE_INTERFACE:
2372 add_interface_typeinfo(typelib, type);
2373 break;
2374 case TYPE_STRUCT:
2375 case TYPE_ENCAPSULATED_UNION:
2376 add_structure_typeinfo(typelib, type);
2377 break;
2378 case TYPE_ENUM:
2379 add_enum_typeinfo(typelib, type);
2380 break;
2381 case TYPE_UNION:
2382 add_union_typeinfo(typelib, type);
2383 break;
2384 case TYPE_COCLASS:
2385 add_coclass_typeinfo(typelib, type);
2386 break;
2387 case TYPE_BASIC:
2388 case TYPE_POINTER:
2389 case TYPE_ARRAY:
2390 break;
2391 default:
2392 error("add_entry: unhandled type 0x%x for %s\n",
2393 type_get_type(type), type->name);
2394 break;
2398 static void add_entry(msft_typelib_t *typelib, const statement_t *stmt)
2400 switch(stmt->type) {
2401 case STMT_LIBRARY:
2402 case STMT_IMPORT:
2403 case STMT_PRAGMA:
2404 case STMT_CPPQUOTE:
2405 case STMT_DECLARATION:
2406 /* not included in typelib */
2407 break;
2408 case STMT_IMPORTLIB:
2409 /* not processed here */
2410 break;
2411 case STMT_TYPEDEF:
2413 const type_list_t *type_entry = stmt->u.type_list;
2414 for (; type_entry; type_entry = type_entry->next) {
2415 /* if the type is public then add the typedef, otherwise attempt
2416 * to add the aliased type */
2417 if (is_attr(type_entry->type->attrs, ATTR_PUBLIC))
2418 add_typedef_typeinfo(typelib, type_entry->type);
2419 else
2420 add_type_typeinfo(typelib, type_alias_get_aliasee_type(type_entry->type));
2422 break;
2424 case STMT_MODULE:
2425 add_module_typeinfo(typelib, stmt->u.type);
2426 break;
2427 case STMT_TYPE:
2428 case STMT_TYPEREF:
2430 type_t *type = stmt->u.type;
2431 add_type_typeinfo(typelib, type);
2432 break;
2437 static void set_name(msft_typelib_t *typelib)
2439 int offset;
2441 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2442 if (offset == -1) return;
2443 typelib->typelib_header.NameOffset = offset;
2444 return;
2447 static void set_version(msft_typelib_t *typelib)
2449 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2452 static void set_guid(msft_typelib_t *typelib)
2454 MSFT_GuidEntry guidentry;
2455 int offset;
2456 void *ptr;
2457 GUID guid = {0,0,0,{0,0,0,0,0,0}};
2459 guidentry.guid = guid;
2460 guidentry.hreftype = -2;
2461 guidentry.next_hash = -1;
2463 ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2464 if (ptr) guidentry.guid = *(GUID *)ptr;
2466 offset = ctl2_alloc_guid(typelib, &guidentry);
2467 typelib->typelib_header.posguid = offset;
2469 return;
2472 static void set_doc_string(msft_typelib_t *typelib)
2474 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2476 if (str)
2478 int offset = ctl2_alloc_string(typelib, str);
2479 if (offset != -1) typelib->typelib_header.helpstring = offset;
2483 static void set_help_file_name(msft_typelib_t *typelib)
2485 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2487 if (str)
2489 int offset = ctl2_alloc_string(typelib, str);
2490 if (offset != -1)
2492 typelib->typelib_header.helpfile = offset;
2493 typelib->typelib_header.varflags |= 0x10;
2498 static void set_help_context(msft_typelib_t *typelib)
2500 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2501 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2504 static void set_help_string_dll(msft_typelib_t *typelib)
2506 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2508 if (str)
2510 int offset = ctl2_alloc_string(typelib, str);
2511 if (offset != -1)
2513 typelib->help_string_dll_offset = offset;
2514 typelib->typelib_header.varflags |= 0x100;
2519 static void set_help_string_context(msft_typelib_t *typelib)
2521 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2522 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2525 static void set_lcid(msft_typelib_t *typelib)
2527 const expr_t *lcid_expr = get_attrp( typelib->typelib->attrs, ATTR_LIBLCID );
2528 if(lcid_expr)
2530 typelib->typelib_header.lcid = lcid_expr->cval;
2531 typelib->typelib_header.lcid2 = lcid_expr->cval;
2535 static void set_lib_flags(msft_typelib_t *typelib)
2537 const attr_t *attr;
2539 typelib->typelib_header.flags = 0;
2540 if (!typelib->typelib->attrs) return;
2541 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2543 switch(attr->type) {
2544 case ATTR_CONTROL:
2545 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2546 break;
2547 case ATTR_HIDDEN:
2548 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2549 break;
2550 case ATTR_RESTRICTED:
2551 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2552 break;
2553 default:
2554 break;
2557 return;
2560 static void ctl2_write_segment(msft_typelib_t *typelib, int segment)
2562 put_data(typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length);
2565 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2567 msft_typeinfo_t *typeinfo;
2569 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2570 typeinfo->typeinfo->memoffset = filesize;
2571 if (typeinfo->func_data)
2572 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2573 if (typeinfo->var_data)
2574 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2575 if (typeinfo->func_data || typeinfo->var_data)
2576 filesize += 4;
2580 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2582 if (typelib->typelib_segdir[segment].length) {
2583 typelib->typelib_segdir[segment].offset = filepos;
2584 } else {
2585 typelib->typelib_segdir[segment].offset = -1;
2588 return typelib->typelib_segdir[segment].length;
2592 static void ctl2_write_typeinfos(msft_typelib_t *typelib)
2594 msft_typeinfo_t *typeinfo;
2595 int typedata_size;
2597 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2598 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2599 typedata_size = 0;
2600 if (typeinfo->func_data)
2601 typedata_size = typeinfo->func_data[0];
2602 if (typeinfo->var_data)
2603 typedata_size += typeinfo->var_data[0];
2604 put_data(&typedata_size, sizeof(int));
2605 if (typeinfo->func_data)
2606 put_data(typeinfo->func_data + 1, typeinfo->func_data[0]);
2607 if (typeinfo->var_data)
2608 put_data(typeinfo->var_data + 1, typeinfo->var_data[0]);
2609 if (typeinfo->func_indices)
2610 put_data(typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2611 if (typeinfo->var_indices)
2612 put_data(typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2613 if (typeinfo->func_names)
2614 put_data(typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2615 if (typeinfo->var_names)
2616 put_data(typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2617 if (typeinfo->func_offsets)
2618 put_data(typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2619 if (typeinfo->var_offsets) {
2620 int add = 0, i, offset;
2621 if(typeinfo->func_data)
2622 add = typeinfo->func_data[0];
2623 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2624 offset = typeinfo->var_offsets[i];
2625 offset += add;
2626 put_data(&offset, 4);
2632 static void save_all_changes(msft_typelib_t *typelib)
2634 int filepos;
2636 chat("save_all_changes(%p)\n", typelib);
2638 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2639 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2640 filepos += typelib->typelib_header.nrtypeinfos * 4;
2642 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2643 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2644 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2645 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2646 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2647 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2648 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2649 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2650 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2651 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2652 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2653 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2654 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2656 ctl2_finalize_typeinfos(typelib, filepos);
2658 byte_swapped = 0;
2659 init_output_buffer();
2661 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2662 if(typelib->typelib_header.varflags & 0x100)
2663 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2665 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2666 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2667 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2668 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2669 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2670 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2671 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2672 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2673 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2674 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2675 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2676 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2677 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2678 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2679 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2681 ctl2_write_typeinfos(typelib);
2683 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2685 char typelib_id[13] = "#1";
2687 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2688 if (expr)
2689 sprintf( typelib_id, "#%d", expr->cval );
2690 add_output_to_resources( "TYPELIB", typelib_id );
2691 output_typelib_regscript( typelib->typelib );
2693 else flush_output_buffer( typelib_name );
2696 int create_msft_typelib(typelib_t *typelib)
2698 msft_typelib_t *msft;
2699 int failed = 0;
2700 const statement_t *stmt;
2701 time_t cur_time;
2702 char *time_override;
2703 unsigned int version = 7 << 24 | 555; /* 7.00.0555 */
2704 GUID midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2705 GUID midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2706 GUID midl_info_guid = {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2707 char info_string[128];
2709 msft = xmalloc(sizeof(*msft));
2710 memset(msft, 0, sizeof(*msft));
2711 msft->typelib = typelib;
2713 ctl2_init_header(msft);
2714 ctl2_init_segdir(msft);
2716 msft->typelib_header.varflags |= (pointer_size == 8) ? SYS_WIN64 : SYS_WIN32;
2719 * The following two calls return an offset or -1 if out of memory. We
2720 * specifically need an offset of 0, however, so...
2722 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2723 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2725 if(failed)
2727 free(msft);
2728 return 0;
2731 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2732 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2734 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2735 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2737 set_lib_flags(msft);
2738 set_lcid(msft);
2739 set_help_file_name(msft);
2740 set_doc_string(msft);
2741 set_guid(msft);
2742 set_version(msft);
2743 set_name(msft);
2744 set_help_context(msft);
2745 set_help_string_dll(msft);
2746 set_help_string_context(msft);
2748 /* midl adds two sets of custom data to the library: the current unix time
2749 and midl's version number */
2750 time_override = getenv( "WIDL_TIME_OVERRIDE");
2751 cur_time = time_override ? atol( time_override) : time(NULL);
2752 sprintf(info_string, "Created by WIDL version %s at %s\n", PACKAGE_VERSION, ctime(&cur_time));
2753 set_custdata(msft, &midl_info_guid, VT_BSTR, info_string, &msft->typelib_header.CustomDataOffset);
2754 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2755 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2757 if (typelib->stmts)
2758 LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
2759 add_entry(msft, stmt);
2761 save_all_changes(msft);
2762 free(msft);
2763 return 1;