ddraw/tests: Rewrite blt_test().
[wine.git] / tools / widl / write_msft.c
blob8b65eadf0f3bf55137760a19ed4058ec288ef7aa
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"
55 enum MSFT_segment_index {
56 MSFT_SEG_TYPEINFO = 0, /* type information */
57 MSFT_SEG_IMPORTINFO, /* import information */
58 MSFT_SEG_IMPORTFILES, /* import filenames */
59 MSFT_SEG_REFERENCES, /* references (?) */
60 MSFT_SEG_GUIDHASH, /* hash table for guids? */
61 MSFT_SEG_GUID, /* guid storage */
62 MSFT_SEG_NAMEHASH, /* hash table for names */
63 MSFT_SEG_NAME, /* name storage */
64 MSFT_SEG_STRING, /* string storage */
65 MSFT_SEG_TYPEDESC, /* type descriptions */
66 MSFT_SEG_ARRAYDESC, /* array descriptions */
67 MSFT_SEG_CUSTDATA, /* custom data */
68 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
69 MSFT_SEG_UNKNOWN, /* ??? */
70 MSFT_SEG_UNKNOWN2, /* ??? */
71 MSFT_SEG_MAX /* total number of segments */
74 typedef struct tagMSFT_ImpFile {
75 int guid;
76 LCID lcid;
77 int version;
78 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
79 } MSFT_ImpFile;
81 typedef struct _msft_typelib_t
83 typelib_t *typelib;
84 MSFT_Header typelib_header;
85 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
86 unsigned char *typelib_segment_data[MSFT_SEG_MAX];
87 int typelib_segment_block_length[MSFT_SEG_MAX];
89 INT typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
91 INT *typelib_namehash_segment;
92 INT *typelib_guidhash_segment;
94 INT help_string_dll_offset;
96 struct _msft_typeinfo_t *typeinfos;
97 struct _msft_typeinfo_t *last_typeinfo;
98 } msft_typelib_t;
100 typedef struct _msft_typeinfo_t
102 msft_typelib_t *typelib;
103 MSFT_TypeInfoBase *typeinfo;
105 int typekind;
107 unsigned int var_data_allocated;
108 int *var_data;
110 unsigned int func_data_allocated;
111 int *func_data;
113 int vars_allocated;
114 int *var_indices;
115 int *var_names;
116 int *var_offsets;
118 int funcs_allocated;
119 int *func_indices;
120 int *func_names;
121 int *func_offsets;
123 int datawidth;
125 struct _msft_typeinfo_t *next_typeinfo;
126 } msft_typeinfo_t;
130 /*================== Internal functions ===================================*/
132 /****************************************************************************
133 * ctl2_init_header
135 * Initializes the type library header of a new typelib.
137 static void ctl2_init_header(
138 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
140 typelib->typelib_header.magic1 = 0x5446534d;
141 typelib->typelib_header.magic2 = 0x00010002;
142 typelib->typelib_header.posguid = -1;
143 typelib->typelib_header.lcid = 0x0409;
144 typelib->typelib_header.lcid2 = 0x0;
145 typelib->typelib_header.varflags = 0x40;
146 typelib->typelib_header.version = 0;
147 typelib->typelib_header.flags = 0;
148 typelib->typelib_header.nrtypeinfos = 0;
149 typelib->typelib_header.helpstring = -1;
150 typelib->typelib_header.helpstringcontext = 0;
151 typelib->typelib_header.helpcontext = 0;
152 typelib->typelib_header.nametablecount = 0;
153 typelib->typelib_header.nametablechars = 0;
154 typelib->typelib_header.NameOffset = -1;
155 typelib->typelib_header.helpfile = -1;
156 typelib->typelib_header.CustomDataOffset = -1;
157 typelib->typelib_header.res44 = 0x20;
158 typelib->typelib_header.res48 = 0x80;
159 typelib->typelib_header.dispatchpos = -1;
160 typelib->typelib_header.nimpinfos = 0;
163 /****************************************************************************
164 * ctl2_init_segdir
166 * Initializes the segment directory of a new typelib.
168 static void ctl2_init_segdir(
169 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
171 int i;
172 MSFT_pSeg *segdir;
174 segdir = &typelib->typelib_segdir[MSFT_SEG_TYPEINFO];
176 for (i = 0; i < MSFT_SEG_MAX; i++) {
177 segdir[i].offset = -1;
178 segdir[i].length = 0;
179 segdir[i].res08 = -1;
180 segdir[i].res0c = 0x0f;
184 /****************************************************************************
185 * ctl2_hash_guid
187 * Generates a hash key from a GUID.
189 * RETURNS
191 * The hash key for the GUID.
193 static int ctl2_hash_guid(
194 REFGUID guid) /* [I] The guid to hash. */
196 int hash;
197 int i;
199 hash = 0;
200 for (i = 0; i < 8; i ++) {
201 hash ^= ((const short *)guid)[i];
204 return hash & 0x1f;
207 /****************************************************************************
208 * ctl2_find_guid
210 * Locates a guid in a type library.
212 * RETURNS
214 * The offset into the GUID segment of the guid, or -1 if not found.
216 static int ctl2_find_guid(
217 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
218 int hash_key, /* [I] The hash key for the guid. */
219 REFGUID guid) /* [I] The guid to find. */
221 int offset;
222 MSFT_GuidEntry *guidentry;
224 offset = typelib->typelib_guidhash_segment[hash_key];
225 while (offset != -1) {
226 guidentry = (MSFT_GuidEntry *)&typelib->typelib_segment_data[MSFT_SEG_GUID][offset];
228 if (!memcmp(guidentry, guid, sizeof(GUID))) return offset;
230 offset = guidentry->next_hash;
233 return offset;
236 /****************************************************************************
237 * ctl2_find_name
239 * Locates a name in a type library.
241 * RETURNS
243 * The offset into the NAME segment of the name, or -1 if not found.
245 * NOTES
247 * The name must be encoded as with ctl2_encode_name().
249 static int ctl2_find_name(
250 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
251 char *name) /* [I] The encoded name to find. */
253 int offset;
254 int *namestruct;
256 offset = typelib->typelib_namehash_segment[name[2] & 0x7f];
257 while (offset != -1) {
258 namestruct = (int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][offset];
260 if (!((namestruct[2] ^ *((int *)name)) & 0xffff00ff)) {
261 /* hash codes and lengths match, final test */
262 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
265 /* move to next item in hash bucket */
266 offset = namestruct[1];
269 return offset;
272 /****************************************************************************
273 * ctl2_encode_name
275 * Encodes a name string to a form suitable for storing into a type library
276 * or comparing to a name stored in a type library.
278 * RETURNS
280 * The length of the encoded name, including padding and length+hash fields.
282 * NOTES
284 * Will throw an exception if name or result are NULL. Is not multithread
285 * safe in the slightest.
287 static int ctl2_encode_name(
288 msft_typelib_t *typelib, /* [I] The typelib to operate against (used for LCID only). */
289 const char *name, /* [I] The name string to encode. */
290 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
292 int length;
293 static char converted_name[0x104];
294 int offset;
295 int value;
297 length = strlen(name);
298 memcpy(converted_name + 4, name, length);
300 converted_name[length + 4] = 0;
303 value = lhash_val_of_name_sys(typelib->typelib_header.varflags & 0x0f, typelib->typelib_header.lcid, converted_name + 4);
305 #ifdef WORDS_BIGENDIAN
306 converted_name[3] = length & 0xff;
307 converted_name[2] = 0x00;
308 converted_name[1] = value;
309 converted_name[0] = value >> 8;
310 #else
311 converted_name[0] = length & 0xff;
312 converted_name[1] = 0x00;
313 converted_name[2] = value;
314 converted_name[3] = value >> 8;
315 #endif
317 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
319 *result = converted_name;
321 return (length + 7) & ~3;
324 /****************************************************************************
325 * ctl2_encode_string
327 * Encodes a string to a form suitable for storing into a type library or
328 * comparing to a string stored in a type library.
330 * RETURNS
332 * The length of the encoded string, including padding and length fields.
334 * NOTES
336 * Will throw an exception if string or result are NULL. Is not multithread
337 * safe in the slightest.
339 static int ctl2_encode_string(
340 const char *string, /* [I] The string to encode. */
341 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
343 int length;
344 static char converted_string[0x104];
345 int offset;
347 length = strlen(string);
348 memcpy(converted_string + 2, string, length);
350 #ifdef WORDS_BIGENDIAN
351 converted_string[1] = length & 0xff;
352 converted_string[0] = (length >> 8) & 0xff;
353 #else
354 converted_string[0] = length & 0xff;
355 converted_string[1] = (length >> 8) & 0xff;
356 #endif
358 if(length < 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
359 for(offset = 0; offset < 4; offset++)
360 converted_string[length + offset + 2] = 0x57;
361 length += 4;
363 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
365 *result = converted_string;
367 return (length + 5) & ~3;
370 /****************************************************************************
371 * ctl2_alloc_segment
373 * Allocates memory from a segment in a type library.
375 * RETURNS
377 * Success: The offset within the segment of the new data area.
379 * BUGS
381 * Does not (yet) handle the case where the allocated segment memory needs to grow.
383 static int ctl2_alloc_segment(
384 msft_typelib_t *typelib, /* [I] The type library in which to allocate. */
385 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
386 int size, /* [I] The amount to allocate. */
387 int block_size) /* [I] Initial allocation block size, or 0 for default. */
389 int offset;
391 if(!typelib->typelib_segment_data[segment]) {
392 if (!block_size) block_size = 0x2000;
394 typelib->typelib_segment_block_length[segment] = block_size;
395 typelib->typelib_segment_data[segment] = xmalloc(block_size);
396 if (!typelib->typelib_segment_data[segment]) return -1;
397 memset(typelib->typelib_segment_data[segment], 0x57, block_size);
400 while ((typelib->typelib_segdir[segment].length + size) > typelib->typelib_segment_block_length[segment]) {
401 unsigned char *block;
403 block_size = typelib->typelib_segment_block_length[segment];
404 block = xrealloc(typelib->typelib_segment_data[segment], block_size << 1);
406 if (segment == MSFT_SEG_TYPEINFO) {
407 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
408 msft_typeinfo_t *typeinfo;
410 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
411 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - typelib->typelib_segment_data[segment]];
415 memset(block + block_size, 0x57, block_size);
416 typelib->typelib_segment_block_length[segment] = block_size << 1;
417 typelib->typelib_segment_data[segment] = block;
420 offset = typelib->typelib_segdir[segment].length;
421 typelib->typelib_segdir[segment].length += size;
423 return offset;
426 /****************************************************************************
427 * ctl2_alloc_typeinfo
429 * Allocates and initializes a typeinfo structure in a type library.
431 * RETURNS
433 * Success: The offset of the new typeinfo.
434 * Failure: -1 (this is invariably an out of memory condition).
436 static int ctl2_alloc_typeinfo(
437 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
438 int nameoffset) /* [I] The offset of the name for this typeinfo. */
440 int offset;
441 MSFT_TypeInfoBase *typeinfo;
443 offset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
445 typelib->typelib_typeinfo_offsets[typelib->typelib_header.nrtypeinfos++] = offset;
447 typeinfo = (void *)(typelib->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
449 typeinfo->typekind = (typelib->typelib_header.nrtypeinfos - 1) << 16;
450 typeinfo->memoffset = -1; /* should be EOF if no elements */
451 typeinfo->res2 = 0;
452 typeinfo->res3 = -1;
453 typeinfo->res4 = 3;
454 typeinfo->res5 = 0;
455 typeinfo->cElement = 0;
456 typeinfo->res7 = 0;
457 typeinfo->res8 = 0;
458 typeinfo->res9 = 0;
459 typeinfo->resA = 0;
460 typeinfo->posguid = -1;
461 typeinfo->flags = 0;
462 typeinfo->NameOffset = nameoffset;
463 typeinfo->version = 0;
464 typeinfo->docstringoffs = -1;
465 typeinfo->helpstringcontext = 0;
466 typeinfo->helpcontext = 0;
467 typeinfo->oCustData = -1;
468 typeinfo->cbSizeVft = 0;
469 typeinfo->cImplTypes = 0;
470 typeinfo->size = 0;
471 typeinfo->datatype1 = -1;
472 typeinfo->datatype2 = 0;
473 typeinfo->res18 = 0;
474 typeinfo->res19 = -1;
476 return offset;
479 /****************************************************************************
480 * ctl2_alloc_guid
482 * Allocates and initializes a GUID structure in a type library. Also updates
483 * the GUID hash table as needed.
485 * RETURNS
487 * Success: The offset of the new GUID.
489 static int ctl2_alloc_guid(
490 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
491 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
493 int offset;
494 MSFT_GuidEntry *guid_space;
495 int hash_key;
497 chat("adding uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
498 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
499 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
500 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
502 hash_key = ctl2_hash_guid(&guid->guid);
504 offset = ctl2_find_guid(typelib, hash_key, &guid->guid);
505 if (offset != -1)
507 if (pedantic)
508 warning("duplicate uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
509 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
510 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
511 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
512 return -1;
515 offset = ctl2_alloc_segment(typelib, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
517 guid_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_GUID] + offset);
518 *guid_space = *guid;
520 guid_space->next_hash = typelib->typelib_guidhash_segment[hash_key];
521 typelib->typelib_guidhash_segment[hash_key] = offset;
523 return offset;
526 /****************************************************************************
527 * ctl2_alloc_name
529 * Allocates and initializes a name within a type library. Also updates the
530 * name hash table as needed.
532 * RETURNS
534 * Success: The offset within the segment of the new name.
535 * Failure: -1 (this is invariably an out of memory condition).
537 static int ctl2_alloc_name(
538 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
539 const char *name) /* [I] The name to store. */
541 int length;
542 int offset;
543 MSFT_NameIntro *name_space;
544 char *encoded_name;
546 length = ctl2_encode_name(typelib, name, &encoded_name);
548 offset = ctl2_find_name(typelib, encoded_name);
549 if (offset != -1) return offset;
551 offset = ctl2_alloc_segment(typelib, MSFT_SEG_NAME, length + 8, 0);
553 name_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_NAME] + offset);
554 name_space->hreftype = -1;
555 name_space->next_hash = -1;
556 memcpy(&name_space->namelen, encoded_name, length);
558 if (typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
559 name_space->next_hash = typelib->typelib_namehash_segment[encoded_name[2] & 0x7f];
561 typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
563 typelib->typelib_header.nametablecount += 1;
564 typelib->typelib_header.nametablechars += *encoded_name;
566 return offset;
569 /****************************************************************************
570 * ctl2_alloc_string
572 * Allocates and initializes a string in a type library.
574 * RETURNS
576 * Success: The offset within the segment of the new string.
577 * Failure: -1 (this is invariably an out of memory condition).
579 static int ctl2_alloc_string(
580 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
581 const char *string) /* [I] The string to store. */
583 int length;
584 int offset;
585 unsigned char *string_space;
586 char *encoded_string;
588 length = ctl2_encode_string(string, &encoded_string);
590 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_STRING].length;
591 offset += (((typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
592 typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
593 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
596 offset = ctl2_alloc_segment(typelib, MSFT_SEG_STRING, length, 0);
598 string_space = typelib->typelib_segment_data[MSFT_SEG_STRING] + offset;
599 memcpy(string_space, encoded_string, length);
601 return offset;
604 /****************************************************************************
605 * alloc_msft_importinfo
607 * Allocates and initializes an import information structure in a type library.
609 * RETURNS
611 * Success: The offset of the new importinfo.
612 * Failure: -1 (this is invariably an out of memory condition).
614 static int alloc_msft_importinfo(
615 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
616 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
618 int offset;
619 MSFT_ImpInfo *impinfo_space;
621 for (offset = 0;
622 offset < typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
623 offset += sizeof(MSFT_ImpInfo)) {
624 if (!memcmp(&(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][offset]),
625 impinfo, sizeof(MSFT_ImpInfo))) {
626 return offset;
630 impinfo->flags |= typelib->typelib_header.nimpinfos++;
632 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
634 impinfo_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
635 *impinfo_space = *impinfo;
637 return offset;
640 /****************************************************************************
641 * alloc_importfile
643 * Allocates and initializes an import file definition in a type library.
645 * RETURNS
647 * Success: The offset of the new importinfo.
648 * Failure: -1 (this is invariably an out of memory condition).
650 static int alloc_importfile(
651 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
652 int guidoffset, /* [I] The offset to the GUID for the imported library. */
653 int major_version, /* [I] The major version number of the imported library. */
654 int minor_version, /* [I] The minor version number of the imported library. */
655 const char *filename) /* [I] The filename of the imported library. */
657 int length;
658 int offset;
659 MSFT_ImpFile *importfile;
660 char *encoded_string;
662 length = ctl2_encode_string(filename, &encoded_string);
664 encoded_string[0] <<= 2;
665 encoded_string[0] |= 1;
667 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
668 offset += (((typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
669 typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 0xc) {
670 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
673 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
675 importfile = (MSFT_ImpFile *)&typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
676 importfile->guid = guidoffset;
677 importfile->lcid = typelib->typelib_header.lcid2;
678 importfile->version = major_version | (minor_version << 16);
679 memcpy(&importfile->filename, encoded_string, length);
681 return offset;
684 static void alloc_importinfo(msft_typelib_t *typelib, importinfo_t *importinfo)
686 importlib_t *importlib = importinfo->importlib;
688 chat("alloc_importinfo: %s\n", importinfo->name);
690 if(!importlib->allocated) {
691 MSFT_GuidEntry guid;
692 int guid_idx;
694 chat("allocating importlib %s\n", importlib->name);
696 importlib->allocated = -1;
698 memcpy(&guid.guid, &importlib->guid, sizeof(GUID));
699 guid.hreftype = 2;
701 guid_idx = ctl2_alloc_guid(typelib, &guid);
703 alloc_importfile(typelib, guid_idx, importlib->version&0xffff,
704 importlib->version>>16, importlib->name);
707 if(importinfo->offset == -1 || !(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID)) {
708 MSFT_ImpInfo impinfo;
710 impinfo.flags = importinfo->flags;
711 impinfo.oImpFile = 0;
713 if(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID) {
714 MSFT_GuidEntry guid;
716 guid.hreftype = 0;
717 memcpy(&guid.guid, &importinfo->guid, sizeof(GUID));
719 impinfo.oGuid = ctl2_alloc_guid(typelib, &guid);
721 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
723 typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo.oGuid+sizeof(GUID)]
724 = importinfo->offset+1;
726 if(!strcmp(importinfo->name, "IDispatch"))
727 typelib->typelib_header.dispatchpos = importinfo->offset+1;
728 }else {
729 impinfo.oGuid = importinfo->id;
730 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
735 static importinfo_t *find_importinfo(msft_typelib_t *typelib, const char *name)
737 importlib_t *importlib;
738 int i;
740 chat("search importlib %s\n", name);
742 if(!name)
743 return NULL;
745 LIST_FOR_EACH_ENTRY( importlib, &typelib->typelib->importlibs, importlib_t, entry )
747 for(i=0; i < importlib->ntypeinfos; i++) {
748 if(!strcmp(name, importlib->importinfos[i].name)) {
749 chat("Found %s in importlib.\n", name);
750 return importlib->importinfos+i;
755 return NULL;
758 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure);
759 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface);
760 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration);
761 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion);
762 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls);
763 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
766 /****************************************************************************
767 * encode_type
769 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
770 * segments as needed.
772 * RETURNS
774 * Success: 0.
775 * Failure: -1.
777 static int encode_type(
778 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
779 int vt, /* [I] vt to encode */
780 type_t *type, /* [I] type */
781 int *encoded_type, /* [O] The encoded type description. */
782 int *width, /* [O] The width of the type, or NULL. */
783 int *alignment, /* [O] The alignment of the type, or NULL. */
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 (!width) width = &scratch;
797 if (!alignment) alignment = &scratch;
798 if (!decoded_size) decoded_size = &scratch;
801 switch (vt) {
802 case VT_I1:
803 case VT_UI1:
804 *encoded_type = default_type;
805 *width = 1;
806 *alignment = 1;
807 break;
809 case VT_INT:
810 *encoded_type = 0x80000000 | (VT_I4 << 16) | VT_INT;
811 if ((typelib->typelib_header.varflags & 0x0f) == SYS_WIN16) {
812 *width = 2;
813 *alignment = 2;
814 } else {
815 *width = 4;
816 *alignment = 4;
818 break;
820 case VT_UINT:
821 *encoded_type = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
822 if ((typelib->typelib_header.varflags & 0x0f) == SYS_WIN16) {
823 *width = 2;
824 *alignment = 2;
825 } else {
826 *width = 4;
827 *alignment = 4;
829 break;
831 case VT_UI2:
832 case VT_I2:
833 case VT_BOOL:
834 *encoded_type = default_type;
835 *width = 2;
836 *alignment = 2;
837 break;
839 case VT_I4:
840 case VT_UI4:
841 case VT_R4:
842 case VT_ERROR:
843 case VT_HRESULT:
844 *encoded_type = default_type;
845 *width = 4;
846 *alignment = 4;
847 break;
849 case VT_R8:
850 case VT_I8:
851 case VT_UI8:
852 *encoded_type = default_type;
853 *width = 8;
854 *alignment = 8;
855 break;
857 case VT_CY:
858 case VT_DATE:
859 *encoded_type = default_type;
860 *width = 8;
861 *alignment = 8;
862 break;
864 case VT_DECIMAL:
865 *encoded_type = default_type;
866 *width = 16;
867 *alignment = 8;
868 break;
870 case VT_VOID:
871 *encoded_type = 0x80000000 | (VT_EMPTY << 16) | vt;
872 *width = 0;
873 *alignment = 1;
874 break;
876 case VT_UNKNOWN:
877 case VT_DISPATCH:
878 case VT_BSTR:
879 *encoded_type = default_type;
880 *width = pointer_size;
881 *alignment = 4;
882 break;
884 case VT_VARIANT:
885 *encoded_type = default_type;
886 break;
888 case VT_LPSTR:
889 case VT_LPWSTR:
890 *encoded_type = 0xfffe0000 | vt;
891 *width = pointer_size;
892 *alignment = 4;
893 break;
895 case VT_PTR:
897 int next_vt;
898 for(next_vt = 0; is_ptr(type); type = type_pointer_get_ref(type)) {
899 next_vt = get_type_vt(type_pointer_get_ref(type));
900 if (next_vt != 0)
901 break;
903 /* if no type found then it must be void */
904 if (next_vt == 0)
905 next_vt = VT_VOID;
907 encode_type(typelib, next_vt, type_pointer_get_ref(type),
908 &target_type, NULL, NULL, &child_size);
909 /* these types already have an implicit pointer, so we don't need to
910 * add another */
911 if(next_vt == VT_DISPATCH || next_vt == VT_UNKNOWN) {
912 chat("encode_type: skipping ptr\n");
913 *encoded_type = target_type;
914 *width = pointer_size;
915 *alignment = 4;
916 *decoded_size = child_size;
917 break;
920 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
921 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
922 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
925 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
926 int mix_field;
928 if (target_type & 0x80000000) {
929 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
930 } else {
931 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
932 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
935 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
936 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
938 typedata[0] = (mix_field << 16) | VT_PTR;
939 typedata[1] = target_type;
942 *encoded_type = typeoffset;
944 *width = pointer_size;
945 *alignment = 4;
946 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
947 break;
950 case VT_SAFEARRAY:
952 type_t *element_type = type_alias_get_aliasee(type_array_get_element(type));
953 int next_vt = get_type_vt(element_type);
955 encode_type(typelib, next_vt, type_alias_get_aliasee(type_array_get_element(type)), &target_type, NULL, NULL, &child_size);
957 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
958 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
959 if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
962 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
963 int mix_field;
965 if (target_type & 0x80000000) {
966 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
967 } else {
968 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
969 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
972 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
973 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
975 typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
976 typedata[1] = target_type;
979 *encoded_type = typeoffset;
981 *width = pointer_size;
982 *alignment = 4;
983 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
984 break;
987 case VT_USERDEFINED:
989 int typeinfo_offset;
991 /* typedef'd types without public attribute aren't included in the typelib */
992 while (type->typelib_idx < 0 && type_is_alias(type) && !is_attr(type->attrs, ATTR_PUBLIC))
993 type = type_alias_get_aliasee(type);
995 chat("encode_type: VT_USERDEFINED - type %p name = %s real type %d idx %d\n", type,
996 type->name, type_get_type(type), type->typelib_idx);
998 if(type->typelib_idx == -1) {
999 chat("encode_type: trying to ref not added type\n");
1000 switch (type_get_type(type)) {
1001 case TYPE_STRUCT:
1002 add_structure_typeinfo(typelib, type);
1003 break;
1004 case TYPE_INTERFACE:
1005 add_interface_typeinfo(typelib, type);
1006 break;
1007 case TYPE_ENUM:
1008 add_enum_typeinfo(typelib, type);
1009 break;
1010 case TYPE_UNION:
1011 add_union_typeinfo(typelib, type);
1012 break;
1013 case TYPE_COCLASS:
1014 add_coclass_typeinfo(typelib, type);
1015 break;
1016 default:
1017 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
1018 type_get_type(type));
1022 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
1023 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1024 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1025 if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == typeinfo_offset)) break;
1028 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1029 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1030 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1032 typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1033 typedata[1] = typeinfo_offset;
1036 *encoded_type = typeoffset;
1037 *width = 0;
1038 *alignment = 1;
1039 break;
1042 default:
1043 error("encode_type: unrecognized type %d.\n", vt);
1044 *encoded_type = default_type;
1045 *width = 0;
1046 *alignment = 1;
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 *width, /* [O] The width of the type, or NULL. */
1064 int *alignment, /* [O] The alignment of the type, or NULL. */
1065 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1067 int typeoffset;
1068 int *typedata;
1069 int target_type;
1070 int child_size;
1071 int vt;
1072 int scratch;
1074 if (!width) width = &scratch;
1075 if (!alignment) alignment = &scratch;
1076 if (!decoded_size) decoded_size = &scratch;
1077 *decoded_size = 0;
1079 chat("encode_var: var %p type %p type->name %s\n",
1080 var, type, type->name ? type->name : "NULL");
1082 if (is_array(type) && !type_array_is_decl_as_ptr(type)) {
1083 int num_dims, elements = 1, arrayoffset;
1084 type_t *atype;
1085 int *arraydata;
1087 num_dims = 0;
1088 for (atype = type;
1089 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1090 atype = type_array_get_element(atype))
1091 ++num_dims;
1093 chat("array with %d dimensions\n", num_dims);
1094 encode_var(typelib, atype, var, &target_type, width, alignment, NULL);
1095 arrayoffset = ctl2_alloc_segment(typelib, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1096 arraydata = (void *)&typelib->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1098 arraydata[0] = target_type;
1099 arraydata[1] = num_dims;
1100 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1102 arraydata += 2;
1103 for (atype = type;
1104 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1105 atype = type_array_get_element(atype))
1107 arraydata[0] = type_array_get_dim(atype);
1108 arraydata[1] = 0;
1109 arraydata += 2;
1110 elements *= type_array_get_dim(atype);
1113 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1114 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1116 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1117 typedata[1] = arrayoffset;
1119 *encoded_type = typeoffset;
1120 *width = *width * elements;
1121 *decoded_size = 20 /*sizeof(ARRAYDESC)*/ + (num_dims - 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1122 return 0;
1125 vt = get_type_vt(type);
1126 if (vt == VT_PTR) {
1127 type_t *ref = is_ptr(type) ?
1128 type_pointer_get_ref(type) : type_array_get_element(type);
1129 int skip_ptr = encode_var(typelib, ref, var,
1130 &target_type, NULL, NULL, &child_size);
1132 if(skip_ptr == 2) {
1133 chat("encode_var: skipping ptr\n");
1134 *encoded_type = target_type;
1135 *decoded_size = child_size;
1136 *width = pointer_size;
1137 *alignment = 4;
1138 return 0;
1141 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1142 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1143 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1146 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1147 int mix_field;
1149 if (target_type & 0x80000000) {
1150 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1151 } else if (is_array(ref)) {
1152 type_t *element_type = type_alias_get_aliasee(type_array_get_element(ref));
1153 mix_field = get_type_vt(element_type) | VT_ARRAY | VT_BYREF;
1154 } else {
1155 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1156 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1159 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1160 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1162 typedata[0] = (mix_field << 16) | VT_PTR;
1163 typedata[1] = target_type;
1166 *encoded_type = typeoffset;
1168 *width = pointer_size;
1169 *alignment = 4;
1170 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1171 return 0;
1174 dump_type(type);
1176 encode_type(typelib, vt, type, encoded_type, width, alignment, decoded_size);
1177 /* these types already have an implicit pointer, so we don't need to
1178 * add another */
1179 if(vt == VT_DISPATCH || vt == VT_UNKNOWN) return 2;
1180 return 0;
1183 static unsigned int get_ulong_val(unsigned int val, int vt)
1185 switch(vt) {
1186 case VT_I2:
1187 case VT_BOOL:
1188 case VT_UI2:
1189 return val & 0xffff;
1190 case VT_I1:
1191 case VT_UI1:
1192 return val & 0xff;
1195 return val;
1198 static void write_value(msft_typelib_t* typelib, int *out, int vt, const void *value)
1200 switch(vt) {
1201 case VT_I2:
1202 case VT_I4:
1203 case VT_R4:
1204 case VT_BOOL:
1205 case VT_I1:
1206 case VT_UI1:
1207 case VT_UI2:
1208 case VT_UI4:
1209 case VT_INT:
1210 case VT_UINT:
1211 case VT_HRESULT:
1212 case VT_PTR:
1213 case VT_UNKNOWN:
1214 case VT_DISPATCH:
1216 const unsigned int lv = get_ulong_val(*(const unsigned int *)value, vt);
1217 if((lv & 0x3ffffff) == lv) {
1218 *out = 0x80000000;
1219 *out |= vt << 26;
1220 *out |= lv;
1221 } else {
1222 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1223 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1224 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], value, 4);
1225 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1226 *out = offset;
1228 return;
1230 case VT_BSTR:
1232 const char *s = (const char *) value;
1233 int len = strlen(s), seg_len = (len + 6 + 3) & ~0x3;
1234 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1235 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1236 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &len, sizeof(len));
1237 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1238 len += 6;
1239 while(len < seg_len) {
1240 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1241 len++;
1243 *out = offset;
1244 return;
1247 default:
1248 warning("can't write value of type %d yet\n", vt);
1250 return;
1253 static HRESULT set_custdata(msft_typelib_t *typelib, REFGUID guid,
1254 int vt, const void *value, int *offset)
1256 MSFT_GuidEntry guidentry;
1257 int guidoffset;
1258 int custoffset;
1259 int *custdata;
1260 int data_out;
1262 guidentry.guid = *guid;
1264 guidentry.hreftype = -1;
1265 guidentry.next_hash = -1;
1267 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1268 write_value(typelib, &data_out, vt, value);
1270 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1272 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1273 custdata[0] = guidoffset;
1274 custdata[1] = data_out;
1275 custdata[2] = *offset;
1276 *offset = custoffset;
1278 return S_OK;
1281 /* It's possible to have a default value for pointer arguments too.
1282 In this case default value has a referenced type, e.g.
1283 'LONG*' argument gets VT_I4, 'DOUBLE*' - VT_R8. IUnknown* and IDispatch*
1284 are recognised too and stored as VT_UNKNOWN and VT_DISPATCH.
1285 But IUnknown/IDispatch arguments can only have default value of 0
1286 (or expression that resolves to zero) while other pointers can have
1287 any default value. */
1288 static int get_defaultvalue_vt(type_t *type)
1290 int vt;
1291 if (type_get_type(type) == TYPE_ENUM)
1292 vt = VT_I4;
1293 else
1295 vt = get_type_vt(type);
1296 if (vt == VT_PTR && is_ptr(type)) {
1297 vt = get_type_vt(type_pointer_get_ref(type));
1298 /* The only acceptable value for pointers to non-basic types
1299 is NULL, it's stored as VT_I4 for both 32 and 64 bit typelibs. */
1300 if (vt == VT_USERDEFINED)
1301 vt = VT_I4;
1305 return vt;
1308 static HRESULT add_func_desc(msft_typeinfo_t* typeinfo, var_t *func, int index)
1310 int offset, name_offset;
1311 int *typedata, typedata_size;
1312 int i, id, next_idx;
1313 int decoded_size, extra_attr = 0;
1314 int num_params = 0, num_optional = 0, num_defaults = 0;
1315 var_t *arg;
1316 unsigned char *namedata;
1317 const attr_t *attr;
1318 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1319 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1320 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1321 int entry = -1, entry_is_ord = 0;
1322 int lcid_retval_count = 0;
1324 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1326 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1328 switch(typeinfo->typekind) {
1329 case TKIND_DISPATCH:
1330 funckind = 0x4; /* FUNC_DISPATCH */
1331 break;
1332 case TKIND_MODULE:
1333 funckind = 0x3; /* FUNC_STATIC */
1334 break;
1335 default:
1336 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1337 break;
1340 if (is_local( func->attrs )) {
1341 chat("add_func_desc: skipping local function\n");
1342 return S_FALSE;
1345 if (type_get_function_args(func->type))
1346 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1348 num_params++;
1349 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1350 if(attr->type == ATTR_DEFAULTVALUE)
1351 num_defaults++;
1352 else if(attr->type == ATTR_OPTIONAL)
1353 num_optional++;
1357 chat("add_func_desc: num of params %d\n", num_params);
1359 name_offset = ctl2_alloc_name(typeinfo->typelib, func->name);
1361 if (func->attrs) LIST_FOR_EACH_ENTRY( attr, func->attrs, const attr_t, entry ) {
1362 expr_t *expr = attr->u.pval;
1363 switch(attr->type) {
1364 case ATTR_BINDABLE:
1365 funcflags |= 0x4; /* FUNCFLAG_FBINDABLE */
1366 break;
1367 case ATTR_DEFAULTBIND:
1368 funcflags |= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1369 break;
1370 case ATTR_DEFAULTCOLLELEM:
1371 funcflags |= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1372 break;
1373 case ATTR_DISPLAYBIND:
1374 funcflags |= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1375 break;
1376 case ATTR_ENTRY:
1377 extra_attr = max(extra_attr, 3);
1378 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1379 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1380 else {
1381 entry = expr->cval;
1382 entry_is_ord = 1;
1384 break;
1385 case ATTR_HELPCONTEXT:
1386 extra_attr = max(extra_attr, 1);
1387 help_context = expr->u.lval;
1388 break;
1389 case ATTR_HELPSTRING:
1390 extra_attr = max(extra_attr, 2);
1391 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1392 break;
1393 case ATTR_HELPSTRINGCONTEXT:
1394 extra_attr = max(extra_attr, 6);
1395 help_string_context = expr->u.lval;
1396 break;
1397 case ATTR_HIDDEN:
1398 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1399 break;
1400 case ATTR_ID:
1401 id = expr->cval;
1402 break;
1403 case ATTR_IMMEDIATEBIND:
1404 funcflags |= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1405 break;
1406 case ATTR_NONBROWSABLE:
1407 funcflags |= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1408 break;
1409 case ATTR_OUT:
1410 break;
1411 case ATTR_PROPGET:
1412 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1413 break;
1414 case ATTR_PROPPUT:
1415 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1416 break;
1417 case ATTR_PROPPUTREF:
1418 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1419 break;
1420 /* FIXME: FUNCFLAG_FREPLACEABLE */
1421 case ATTR_REQUESTEDIT:
1422 funcflags |= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1423 break;
1424 case ATTR_RESTRICTED:
1425 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1426 break;
1427 case ATTR_SOURCE:
1428 funcflags |= 0x2; /* FUNCFLAG_FSOURCE */
1429 break;
1430 case ATTR_UIDEFAULT:
1431 funcflags |= 0x200; /* FUNCFLAG_FUIDEFAULT */
1432 break;
1433 case ATTR_USESGETLASTERROR:
1434 funcflags |= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1435 break;
1436 case ATTR_VARARG:
1437 if (num_optional || num_defaults)
1438 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1439 else
1440 num_optional = -1;
1441 break;
1442 default:
1443 break;
1447 /* allocate type data space for us */
1448 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1450 if (!typeinfo->func_data) {
1451 typeinfo->func_data = xmalloc(0x100);
1452 typeinfo->func_data_allocated = 0x100;
1453 typeinfo->func_data[0] = 0;
1456 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1457 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1458 typeinfo->func_data[0] + typedata_size + sizeof(int));
1459 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1462 offset = typeinfo->func_data[0];
1463 typeinfo->func_data[0] += typedata_size;
1464 typedata = typeinfo->func_data + (offset >> 2) + 1;
1467 /* find func with the same name - if it exists use its id */
1468 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1469 if(name_offset == typeinfo->func_names[i]) {
1470 id = typeinfo->func_indices[i];
1471 break;
1475 /* find the first func with the same id and link via the hiword of typedata[4] */
1476 next_idx = index;
1477 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1478 if(id == typeinfo->func_indices[i]) {
1479 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1480 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1481 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1482 break;
1486 /* fill out the basic type information */
1487 typedata[0] = typedata_size | (index << 16);
1488 encode_var(typeinfo->typelib, type_function_get_rettype(func->type), func, &typedata[1], NULL, NULL, &decoded_size);
1489 typedata[2] = funcflags;
1490 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1491 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1492 if(num_defaults) typedata[4] |= 0x1000;
1493 if(entry_is_ord) typedata[4] |= 0x2000;
1494 typedata[5] = (num_optional << 16) | num_params;
1496 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1497 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1498 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1499 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1501 switch(extra_attr) {
1502 case 6: typedata[11] = help_string_context;
1503 case 5: typedata[10] = -1;
1504 case 4: typedata[9] = -1;
1505 case 3: typedata[8] = entry;
1506 case 2: typedata[7] = help_string_offset;
1507 case 1: typedata[6] = help_context;
1508 case 0:
1509 break;
1510 default:
1511 warning("unknown number of optional attrs\n");
1514 if (type_get_function_args(func->type))
1516 i = 0;
1517 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1519 int paramflags = 0;
1520 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1521 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1523 if(defaultdata) *defaultdata = -1;
1525 encode_var(typeinfo->typelib, arg->type, arg, paramdata, NULL, NULL, &decoded_size);
1526 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1527 switch(attr->type) {
1528 case ATTR_DEFAULTVALUE:
1530 int vt;
1531 expr_t *expr = (expr_t *)attr->u.pval;
1532 vt = get_defaultvalue_vt(arg->type);
1533 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1534 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1536 if (vt != VT_BSTR) error("string default value applied to non-string type\n");
1537 chat("default value '%s'\n", expr->u.sval);
1538 write_value(typeinfo->typelib, defaultdata, vt, expr->u.sval);
1540 else
1542 chat("default value %d\n", expr->cval);
1543 write_value(typeinfo->typelib, defaultdata, vt, &expr->cval);
1545 break;
1547 case ATTR_IN:
1548 paramflags |= 0x01; /* PARAMFLAG_FIN */
1549 break;
1550 case ATTR_OPTIONAL:
1551 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1552 break;
1553 case ATTR_OUT:
1554 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1555 break;
1556 case ATTR_PARAMLCID:
1557 paramflags |= 0x04; /* PARAMFLAG_LCID */
1558 lcid_retval_count++;
1559 break;
1560 case ATTR_RETVAL:
1561 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1562 lcid_retval_count++;
1563 break;
1564 default:
1565 chat("unhandled param attr %d\n", attr->type);
1566 break;
1569 paramdata[1] = -1;
1570 paramdata[2] = paramflags;
1571 typedata[3] += decoded_size << 16;
1573 i++;
1577 if(lcid_retval_count == 1)
1578 typedata[4] |= 0x4000;
1579 else if(lcid_retval_count == 2)
1580 typedata[4] |= 0x8000;
1582 if(typeinfo->funcs_allocated == 0) {
1583 typeinfo->funcs_allocated = 10;
1584 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1585 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1586 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1588 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1589 typeinfo->funcs_allocated *= 2;
1590 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1591 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1592 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1595 /* update the index data */
1596 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1597 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1598 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1600 /* ??? */
1601 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1602 typeinfo->typeinfo->res2 <<= 1;
1603 /* ??? */
1604 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1606 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1607 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1608 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1610 /* adjust size of VTBL */
1611 if(funckind != 0x3 /* FUNC_STATIC */)
1612 typeinfo->typeinfo->cbSizeVft += pointer_size;
1614 /* Increment the number of function elements */
1615 typeinfo->typeinfo->cElement += 1;
1617 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1618 if (*((INT *)namedata) == -1) {
1619 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1620 if(typeinfo->typekind == TKIND_MODULE)
1621 namedata[9] |= 0x10;
1622 } else
1623 namedata[9] &= ~0x10;
1625 if(typeinfo->typekind == TKIND_MODULE)
1626 namedata[9] |= 0x20;
1628 if (type_get_function_args(func->type))
1630 i = 0;
1631 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1633 /* don't give the last arg of a [propput*] func a name */
1634 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1636 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1637 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1638 paramdata[1] = offset;
1640 i++;
1643 return S_OK;
1646 static HRESULT add_var_desc(msft_typeinfo_t *typeinfo, UINT index, var_t* var)
1648 int offset, id;
1649 unsigned int typedata_size;
1650 INT *typedata;
1651 int var_datawidth;
1652 int var_alignment;
1653 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1654 int alignment;
1655 int varflags = 0;
1656 const attr_t *attr;
1657 unsigned char *namedata;
1658 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1660 chat("add_var_desc(%d, %s)\n", index, var->name);
1662 id = 0x40000000 + index;
1664 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1665 expr_t *expr = attr->u.pval;
1666 switch(attr->type) {
1667 case ATTR_BINDABLE:
1668 varflags |= 0x04; /* VARFLAG_FBINDABLE */
1669 break;
1670 case ATTR_DEFAULTBIND:
1671 varflags |= 0x20; /* VARFLAG_FDEFAULTBIND */
1672 break;
1673 case ATTR_DEFAULTCOLLELEM:
1674 varflags |= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1675 break;
1676 case ATTR_DISPLAYBIND:
1677 varflags |= 0x10; /* VARFLAG_FDISPLAYBIND */
1678 break;
1679 case ATTR_HIDDEN:
1680 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1681 break;
1682 case ATTR_ID:
1683 id = expr->cval;
1684 break;
1685 case ATTR_IMMEDIATEBIND:
1686 varflags |= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1687 break;
1688 case ATTR_NONBROWSABLE:
1689 varflags |= 0x400; /* VARFLAG_FNONBROWSABLE */
1690 break;
1691 case ATTR_READONLY:
1692 varflags |= 0x01; /* VARFLAG_FREADONLY */
1693 break;
1694 /* FIXME: VARFLAG_FREPLACEABLE */
1695 case ATTR_REQUESTEDIT:
1696 varflags |= 0x08; /* VARFLAG_FREQUESTEDIT */
1697 break;
1698 case ATTR_RESTRICTED:
1699 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1700 break;
1701 case ATTR_SOURCE:
1702 varflags |= 0x02; /* VARFLAG_FSOURCE */
1703 break;
1704 case ATTR_UIDEFAULT:
1705 varflags |= 0x0200; /* VARFLAG_FUIDEFAULT */
1706 break;
1707 default:
1708 break;
1712 /* allocate type data space for us */
1713 typedata_size = 0x14;
1715 if (!typeinfo->var_data) {
1716 typeinfo->var_data = xmalloc(0x100);
1717 typeinfo->var_data_allocated = 0x100;
1718 typeinfo->var_data[0] = 0;
1721 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1722 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1723 typeinfo->var_data[0] + typedata_size + sizeof(int));
1724 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1727 offset = typeinfo->var_data[0];
1728 typeinfo->var_data[0] += typedata_size;
1729 typedata = typeinfo->var_data + (offset >> 2) + 1;
1731 /* fill out the basic type information */
1732 typedata[0] = typedata_size | (index << 16);
1733 typedata[2] = varflags;
1734 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1736 if(typeinfo->vars_allocated == 0) {
1737 typeinfo->vars_allocated = 10;
1738 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1739 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1740 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1742 if(typeinfo->vars_allocated == var_num) {
1743 typeinfo->vars_allocated *= 2;
1744 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1745 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1746 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1748 /* update the index data */
1749 typeinfo->var_indices[var_num] = id;
1750 typeinfo->var_names[var_num] = -1;
1751 typeinfo->var_offsets[var_num] = offset;
1753 /* figure out type widths and whatnot */
1754 encode_var(typeinfo->typelib, var->type, var, &typedata[1], &var_datawidth,
1755 &var_alignment, &var_type_size);
1757 /* pad out starting position to data width */
1758 typeinfo->datawidth += var_alignment - 1;
1759 typeinfo->datawidth &= ~(var_alignment - 1);
1761 switch(typeinfo->typekind) {
1762 case TKIND_ENUM:
1763 write_value(typeinfo->typelib, &typedata[4], VT_I4, &var->eval->cval);
1764 var_kind = 2; /* VAR_CONST */
1765 var_type_size += 16; /* sizeof(VARIANT) */
1766 typeinfo->datawidth = var_datawidth;
1767 break;
1768 case TKIND_RECORD:
1769 typedata[4] = typeinfo->datawidth;
1770 typeinfo->datawidth += var_datawidth;
1771 break;
1772 case TKIND_UNION:
1773 typedata[4] = typeinfo->datawidth;
1774 typeinfo->datawidth = max(typeinfo->datawidth, var_datawidth);
1775 break;
1776 case TKIND_DISPATCH:
1777 var_kind = 3; /* VAR_DISPATCH */
1778 typeinfo->datawidth = pointer_size;
1779 var_alignment = 4;
1780 break;
1781 default:
1782 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1783 break;
1786 /* add type description size to total required allocation */
1787 typedata[3] += var_type_size << 16 | var_kind;
1789 /* fix type alignment */
1790 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1791 if (alignment < var_alignment) {
1792 alignment = var_alignment;
1793 typeinfo->typeinfo->typekind &= ~0xffc0;
1794 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1797 /* ??? */
1798 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1799 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1800 typeinfo->typeinfo->res2 <<= 1;
1803 /* ??? */
1804 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1805 typeinfo->typeinfo->res3 += 0x2c;
1807 /* increment the number of variable elements */
1808 typeinfo->typeinfo->cElement += 0x10000;
1810 /* pad data width to alignment */
1811 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1813 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1814 if (offset == -1) return E_OUTOFMEMORY;
1816 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1817 if (*((INT *)namedata) == -1) {
1818 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1819 if(typeinfo->typekind != TKIND_DISPATCH)
1820 namedata[9] |= 0x10;
1821 } else
1822 namedata[9] &= ~0x10;
1824 if (typeinfo->typekind == TKIND_ENUM) {
1825 namedata[9] |= 0x20;
1827 typeinfo->var_names[var_num] = offset;
1829 return S_OK;
1832 static HRESULT add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1834 if(importinfo) {
1835 alloc_importinfo(typeinfo->typelib, importinfo);
1836 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1837 }else {
1838 if(ref->typelib_idx == -1)
1839 add_interface_typeinfo(typeinfo->typelib, ref);
1840 if(ref->typelib_idx == -1)
1841 error("add_impl_type: unable to add inherited interface\n");
1843 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1846 typeinfo->typeinfo->cImplTypes++;
1847 return S_OK;
1850 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1851 const char *name, const attr_list_t *attrs)
1853 const attr_t *attr;
1854 msft_typeinfo_t *msft_typeinfo;
1855 int nameoffset;
1856 int typeinfo_offset;
1857 MSFT_TypeInfoBase *typeinfo;
1858 MSFT_GuidEntry guidentry;
1860 chat("create_msft_typeinfo: name %s kind %d index %d\n", name, kind, typelib->typelib_header.nrtypeinfos);
1862 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1863 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1865 msft_typeinfo->typelib = typelib;
1867 nameoffset = ctl2_alloc_name(typelib, name);
1868 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1869 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1871 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1872 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1874 msft_typeinfo->typekind = kind;
1875 msft_typeinfo->typeinfo = typeinfo;
1877 typeinfo->typekind |= kind | 0x20;
1879 if(kind == TKIND_COCLASS)
1880 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1882 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1883 switch(attr->type) {
1884 case ATTR_AGGREGATABLE:
1885 if (kind == TKIND_COCLASS)
1886 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1887 break;
1889 case ATTR_APPOBJECT:
1890 if (kind == TKIND_COCLASS)
1891 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1892 break;
1894 case ATTR_CONTROL:
1895 if (kind == TKIND_COCLASS)
1896 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1897 break;
1899 case ATTR_DLLNAME:
1901 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1902 typeinfo->datatype1 = offset;
1903 break;
1906 case ATTR_DUAL:
1907 /* FIXME: check interface is compatible */
1908 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1909 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1910 break;
1912 case ATTR_HELPCONTEXT:
1914 expr_t *expr = (expr_t*)attr->u.pval;
1915 typeinfo->helpcontext = expr->cval;
1916 break;
1918 case ATTR_HELPSTRING:
1920 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1921 if (offset == -1) break;
1922 typeinfo->docstringoffs = offset;
1923 break;
1925 case ATTR_HELPSTRINGCONTEXT:
1927 expr_t *expr = (expr_t*)attr->u.pval;
1928 typeinfo->helpstringcontext = expr->cval;
1929 break;
1931 case ATTR_HIDDEN:
1932 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1933 break;
1935 case ATTR_LICENSED:
1936 typeinfo->flags |= 0x04; /* TYPEFLAG_FLICENSED */
1937 break;
1939 case ATTR_NONCREATABLE:
1940 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1941 break;
1943 case ATTR_NONEXTENSIBLE:
1944 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1945 break;
1947 case ATTR_OLEAUTOMATION:
1948 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1949 break;
1951 /* FIXME: TYPEFLAG_FPREDCLID */
1953 case ATTR_PROXY:
1954 typeinfo->flags |= 0x4000; /* TYPEFLAG_FPROXY */
1955 break;
1957 /* FIXME: TYPEFLAG_FREPLACEABLE */
1959 case ATTR_RESTRICTED:
1960 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
1961 break;
1963 case ATTR_UUID:
1964 guidentry.guid = *(GUID*)attr->u.pval;
1965 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1966 guidentry.next_hash = -1;
1967 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
1968 #if 0
1969 if (IsEqualIID(guid, &IID_IDispatch)) {
1970 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1972 #endif
1973 break;
1975 case ATTR_VERSION:
1976 typeinfo->version = attr->u.ival;
1977 break;
1979 default:
1980 break;
1984 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
1985 typelib->last_typeinfo = msft_typeinfo;
1986 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
1989 return msft_typeinfo;
1992 static void add_dispatch(msft_typelib_t *typelib)
1994 int guid_offset, impfile_offset, hash_key;
1995 MSFT_GuidEntry guidentry;
1996 MSFT_ImpInfo impinfo;
1997 GUID stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1998 GUID iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2000 if(typelib->typelib_header.dispatchpos != -1) return;
2002 guidentry.guid = stdole;
2003 guidentry.hreftype = 2;
2004 guidentry.next_hash = -1;
2005 hash_key = ctl2_hash_guid(&guidentry.guid);
2006 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2007 if (guid_offset == -1)
2008 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2009 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
2011 guidentry.guid = iid_idispatch;
2012 guidentry.hreftype = 1;
2013 guidentry.next_hash = -1;
2014 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
2015 impinfo.oImpFile = impfile_offset;
2016 hash_key = ctl2_hash_guid(&guidentry.guid);
2017 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2018 if (guid_offset == -1)
2019 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2020 impinfo.oGuid = guid_offset;
2021 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
2024 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
2026 int idx = 0;
2027 var_t *func;
2028 var_t *var;
2029 msft_typeinfo_t *msft_typeinfo;
2031 if (-1 < dispinterface->typelib_idx)
2032 return;
2034 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2035 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
2036 dispinterface->attrs);
2038 msft_typeinfo->typeinfo->size = pointer_size;
2039 msft_typeinfo->typeinfo->typekind |= 0x2100;
2041 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2042 add_dispatch(typelib);
2043 msft_typeinfo->typeinfo->cImplTypes = 1;
2045 /* count the no of methods, as the variable indices come after the funcs */
2046 if (dispinterface->details.iface->disp_methods)
2047 LIST_FOR_EACH_ENTRY( func, dispinterface->details.iface->disp_methods, var_t, entry )
2048 idx++;
2050 if (type_dispiface_get_props(dispinterface))
2051 LIST_FOR_EACH_ENTRY( var, type_dispiface_get_props(dispinterface), var_t, entry )
2052 add_var_desc(msft_typeinfo, idx++, var);
2054 if (type_dispiface_get_methods(dispinterface))
2056 idx = 0;
2057 LIST_FOR_EACH_ENTRY( func, type_dispiface_get_methods(dispinterface), var_t, entry )
2058 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2059 idx++;
2063 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
2065 int idx = 0;
2066 const statement_t *stmt_func;
2067 type_t *ref;
2068 msft_typeinfo_t *msft_typeinfo;
2069 importinfo_t *ref_importinfo = NULL;
2070 int num_parents = 0, num_funcs = 0;
2071 type_t *inherit;
2072 const type_t *derived;
2074 if (-1 < interface->typelib_idx)
2075 return;
2077 if (!interface->details.iface)
2079 error( "interface %s is referenced but not defined\n", interface->name );
2080 return;
2083 if (is_attr(interface->attrs, ATTR_DISPINTERFACE)) {
2084 add_dispinterface_typeinfo(typelib, interface);
2085 return;
2088 /* midl adds the parent interface first, unless the parent itself
2089 has no parent (i.e. it stops before IUnknown). */
2091 inherit = type_iface_get_inherit(interface);
2093 if(inherit) {
2094 ref_importinfo = find_importinfo(typelib, inherit->name);
2096 if(!ref_importinfo && type_iface_get_inherit(inherit) &&
2097 inherit->typelib_idx == -1)
2098 add_interface_typeinfo(typelib, inherit);
2101 /* check typelib_idx again, it could have been added while resolving the parent interface */
2102 if (-1 < interface->typelib_idx)
2103 return;
2105 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2106 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
2107 msft_typeinfo->typeinfo->size = pointer_size;
2108 msft_typeinfo->typeinfo->typekind |= 0x2200;
2110 for (derived = inherit; derived; derived = type_iface_get_inherit(derived))
2111 if (derived->name && !strcmp(derived->name, "IDispatch"))
2112 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2114 /* can't be dual if it doesn't derive from IDispatch */
2115 if (!(msft_typeinfo->typeinfo->flags & 0x1000)) /* TYPEFLAG_FDISPATCHABLE */
2116 msft_typeinfo->typeinfo->flags &= ~0x40; /* TYPEFLAG_FDUAL */
2118 if(type_iface_get_inherit(interface))
2119 add_impl_type(msft_typeinfo, type_iface_get_inherit(interface),
2120 ref_importinfo);
2122 /* count the number of inherited interfaces and non-local functions */
2123 for(ref = inherit; ref; ref = type_iface_get_inherit(ref)) {
2124 num_parents++;
2125 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) ) {
2126 var_t *func = stmt_func->u.var;
2127 if (!is_local(func->attrs)) num_funcs++;
2130 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2131 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2133 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(interface) ) {
2134 var_t *func = stmt_func->u.var;
2135 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2136 idx++;
2140 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2142 int idx = 0;
2143 var_t *cur;
2144 msft_typeinfo_t *msft_typeinfo;
2146 if (-1 < structure->typelib_idx)
2147 return;
2149 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2150 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2151 msft_typeinfo->typeinfo->size = 0;
2153 if (type_struct_get_fields(structure))
2154 LIST_FOR_EACH_ENTRY( cur, type_struct_get_fields(structure), var_t, entry )
2155 add_var_desc(msft_typeinfo, idx++, cur);
2158 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2160 int idx = 0;
2161 var_t *cur;
2162 msft_typeinfo_t *msft_typeinfo;
2164 if (-1 < enumeration->typelib_idx)
2165 return;
2167 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2168 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2169 msft_typeinfo->typeinfo->size = 0;
2171 if (type_enum_get_values(enumeration))
2172 LIST_FOR_EACH_ENTRY( cur, type_enum_get_values(enumeration), var_t, entry )
2173 add_var_desc(msft_typeinfo, idx++, cur);
2176 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion)
2178 int idx = 0;
2179 var_t *cur;
2180 msft_typeinfo_t *msft_typeinfo;
2182 if (-1 < tunion->typelib_idx)
2183 return;
2185 tunion->typelib_idx = typelib->typelib_header.nrtypeinfos;
2186 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_UNION, tunion->name, tunion->attrs);
2187 msft_typeinfo->typeinfo->size = 0;
2189 if (type_union_get_cases(tunion))
2190 LIST_FOR_EACH_ENTRY(cur, type_union_get_cases(tunion), var_t, entry)
2191 add_var_desc(msft_typeinfo, idx++, cur);
2194 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2196 msft_typeinfo_t *msft_typeinfo = NULL;
2197 int alignment, datatype1, datatype2, size, duplicate = 0;
2198 type_t *type;
2200 if (-1 < tdef->typelib_idx)
2201 return;
2203 type = type_alias_get_aliasee(tdef);
2205 if (!type->name || strcmp(tdef->name, type->name) != 0)
2207 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2208 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2210 else
2211 duplicate = 1;
2213 encode_type(typelib, get_type_vt(type), type,
2214 &datatype1, &size, &alignment, &datatype2);
2216 if (msft_typeinfo)
2218 msft_typeinfo->typeinfo->datatype1 = datatype1;
2219 msft_typeinfo->typeinfo->size = size;
2220 msft_typeinfo->typeinfo->datatype2 = datatype2;
2221 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2224 /* avoid adding duplicate type definitions */
2225 if (duplicate)
2226 tdef->typelib_idx = type->typelib_idx;
2229 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2231 msft_typeinfo_t *msft_typeinfo;
2232 ifref_t *iref;
2233 int num_ifaces = 0, offset, i;
2234 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2235 int have_default = 0, have_default_source = 0;
2236 const attr_t *attr;
2237 ifref_list_t *ifaces;
2239 if (-1 < cls->typelib_idx)
2240 return;
2242 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2243 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2245 ifaces = type_coclass_get_ifaces(cls);
2246 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) num_ifaces++;
2248 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2249 num_ifaces * sizeof(*ref), 0);
2251 i = 0;
2252 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) {
2253 if(iref->iface->typelib_idx == -1)
2254 add_interface_typeinfo(typelib, iref->iface);
2255 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2256 ref->reftype = typelib->typelib_typeinfo_offsets[iref->iface->typelib_idx];
2257 ref->flags = 0;
2258 ref->oCustData = -1;
2259 ref->onext = -1;
2260 if(i < num_ifaces - 1)
2261 ref->onext = offset + (i + 1) * sizeof(*ref);
2263 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2264 switch(attr->type) {
2265 case ATTR_DEFAULT:
2266 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2267 break;
2268 case ATTR_DEFAULTVTABLE:
2269 ref->flags |= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2270 break;
2271 case ATTR_RESTRICTED:
2272 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2273 break;
2274 case ATTR_SOURCE:
2275 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2276 break;
2277 default:
2278 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2281 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2282 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2283 have_default_source = 1;
2284 else
2285 have_default = 1;
2288 /* If the interface is non-restricted and we haven't already had one then
2289 remember it so that we can use it as a default later */
2290 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2291 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2292 if(!first_source)
2293 first_source = ref;
2295 else if(!first)
2296 first = ref;
2298 i++;
2301 /* If we haven't had a default interface, then set the default flags on the
2302 first ones */
2303 if(!have_default && first)
2304 first->flags |= 0x1;
2305 if(!have_default_source && first_source)
2306 first_source->flags |= 0x1;
2308 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2309 msft_typeinfo->typeinfo->size = pointer_size;
2310 msft_typeinfo->typeinfo->typekind |= 0x2200;
2313 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2315 int idx = 0;
2316 const statement_t *stmt;
2317 msft_typeinfo_t *msft_typeinfo;
2319 if (-1 < module->typelib_idx)
2320 return;
2322 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2323 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2324 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2326 STATEMENTS_FOR_EACH_FUNC( stmt, module->details.module->stmts ) {
2327 var_t *func = stmt->u.var;
2328 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2329 idx++;
2332 msft_typeinfo->typeinfo->size = idx;
2335 static void add_type_typeinfo(msft_typelib_t *typelib, type_t *type)
2337 switch (type_get_type(type)) {
2338 case TYPE_INTERFACE:
2339 add_interface_typeinfo(typelib, type);
2340 break;
2341 case TYPE_STRUCT:
2342 add_structure_typeinfo(typelib, type);
2343 break;
2344 case TYPE_ENUM:
2345 add_enum_typeinfo(typelib, type);
2346 break;
2347 case TYPE_UNION:
2348 add_union_typeinfo(typelib, type);
2349 break;
2350 case TYPE_COCLASS:
2351 add_coclass_typeinfo(typelib, type);
2352 break;
2353 case TYPE_BASIC:
2354 case TYPE_POINTER:
2355 break;
2356 default:
2357 error("add_entry: unhandled type 0x%x for %s\n",
2358 type_get_type(type), type->name);
2359 break;
2363 static void add_entry(msft_typelib_t *typelib, const statement_t *stmt)
2365 switch(stmt->type) {
2366 case STMT_LIBRARY:
2367 case STMT_IMPORT:
2368 case STMT_PRAGMA:
2369 case STMT_CPPQUOTE:
2370 case STMT_DECLARATION:
2371 /* not included in typelib */
2372 break;
2373 case STMT_IMPORTLIB:
2374 /* not processed here */
2375 break;
2376 case STMT_TYPEDEF:
2378 const type_list_t *type_entry = stmt->u.type_list;
2379 for (; type_entry; type_entry = type_entry->next) {
2380 /* if the type is public then add the typedef, otherwise attempt
2381 * to add the aliased type */
2382 if (is_attr(type_entry->type->attrs, ATTR_PUBLIC))
2383 add_typedef_typeinfo(typelib, type_entry->type);
2384 else
2385 add_type_typeinfo(typelib, type_alias_get_aliasee(type_entry->type));
2387 break;
2389 case STMT_MODULE:
2390 add_module_typeinfo(typelib, stmt->u.type);
2391 break;
2392 case STMT_TYPE:
2393 case STMT_TYPEREF:
2395 type_t *type = stmt->u.type;
2396 add_type_typeinfo(typelib, type);
2397 break;
2402 static void set_name(msft_typelib_t *typelib)
2404 int offset;
2406 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2407 if (offset == -1) return;
2408 typelib->typelib_header.NameOffset = offset;
2409 return;
2412 static void set_version(msft_typelib_t *typelib)
2414 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2417 static void set_guid(msft_typelib_t *typelib)
2419 MSFT_GuidEntry guidentry;
2420 int offset;
2421 void *ptr;
2422 GUID guid = {0,0,0,{0,0,0,0,0,0}};
2424 guidentry.guid = guid;
2425 guidentry.hreftype = -2;
2426 guidentry.next_hash = -1;
2428 ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2429 if (ptr) guidentry.guid = *(GUID *)ptr;
2431 offset = ctl2_alloc_guid(typelib, &guidentry);
2432 typelib->typelib_header.posguid = offset;
2434 return;
2437 static void set_doc_string(msft_typelib_t *typelib)
2439 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2441 if (str)
2443 int offset = ctl2_alloc_string(typelib, str);
2444 if (offset != -1) typelib->typelib_header.helpstring = offset;
2448 static void set_help_file_name(msft_typelib_t *typelib)
2450 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2452 if (str)
2454 int offset = ctl2_alloc_string(typelib, str);
2455 if (offset != -1)
2457 typelib->typelib_header.helpfile = offset;
2458 typelib->typelib_header.varflags |= 0x10;
2463 static void set_help_context(msft_typelib_t *typelib)
2465 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2466 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2469 static void set_help_string_dll(msft_typelib_t *typelib)
2471 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2473 if (str)
2475 int offset = ctl2_alloc_string(typelib, str);
2476 if (offset != -1)
2478 typelib->help_string_dll_offset = offset;
2479 typelib->typelib_header.varflags |= 0x100;
2484 static void set_help_string_context(msft_typelib_t *typelib)
2486 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2487 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2490 static void set_lcid(msft_typelib_t *typelib)
2492 const expr_t *lcid_expr = get_attrp( typelib->typelib->attrs, ATTR_LIBLCID );
2493 if(lcid_expr)
2495 typelib->typelib_header.lcid = lcid_expr->cval;
2496 typelib->typelib_header.lcid2 = lcid_expr->cval;
2500 static void set_lib_flags(msft_typelib_t *typelib)
2502 const attr_t *attr;
2504 typelib->typelib_header.flags = 0;
2505 if (!typelib->typelib->attrs) return;
2506 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2508 switch(attr->type) {
2509 case ATTR_CONTROL:
2510 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2511 break;
2512 case ATTR_HIDDEN:
2513 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2514 break;
2515 case ATTR_RESTRICTED:
2516 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2517 break;
2518 default:
2519 break;
2522 return;
2525 static void ctl2_write_segment(msft_typelib_t *typelib, int segment)
2527 put_data(typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length);
2530 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2532 msft_typeinfo_t *typeinfo;
2534 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2535 typeinfo->typeinfo->memoffset = filesize;
2536 if (typeinfo->func_data)
2537 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2538 if (typeinfo->var_data)
2539 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2540 if (typeinfo->func_data || typeinfo->var_data)
2541 filesize += 4;
2545 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2547 if (typelib->typelib_segdir[segment].length) {
2548 typelib->typelib_segdir[segment].offset = filepos;
2549 } else {
2550 typelib->typelib_segdir[segment].offset = -1;
2553 return typelib->typelib_segdir[segment].length;
2557 static void ctl2_write_typeinfos(msft_typelib_t *typelib)
2559 msft_typeinfo_t *typeinfo;
2560 int typedata_size;
2562 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2563 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2564 typedata_size = 0;
2565 if (typeinfo->func_data)
2566 typedata_size = typeinfo->func_data[0];
2567 if (typeinfo->var_data)
2568 typedata_size += typeinfo->var_data[0];
2569 put_data(&typedata_size, sizeof(int));
2570 if (typeinfo->func_data)
2571 put_data(typeinfo->func_data + 1, typeinfo->func_data[0]);
2572 if (typeinfo->var_data)
2573 put_data(typeinfo->var_data + 1, typeinfo->var_data[0]);
2574 if (typeinfo->func_indices)
2575 put_data(typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2576 if (typeinfo->var_indices)
2577 put_data(typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2578 if (typeinfo->func_names)
2579 put_data(typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2580 if (typeinfo->var_names)
2581 put_data(typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2582 if (typeinfo->func_offsets)
2583 put_data(typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2584 if (typeinfo->var_offsets) {
2585 int add = 0, i, offset;
2586 if(typeinfo->func_data)
2587 add = typeinfo->func_data[0];
2588 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2589 offset = typeinfo->var_offsets[i];
2590 offset += add;
2591 put_data(&offset, 4);
2597 static void save_all_changes(msft_typelib_t *typelib)
2599 int filepos;
2601 chat("save_all_changes(%p)\n", typelib);
2603 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2604 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2605 filepos += typelib->typelib_header.nrtypeinfos * 4;
2607 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2608 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2609 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2610 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2611 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2612 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2613 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2614 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2615 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2616 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2617 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2618 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2619 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2621 ctl2_finalize_typeinfos(typelib, filepos);
2623 byte_swapped = 0;
2624 init_output_buffer();
2626 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2627 if(typelib->typelib_header.varflags & 0x100)
2628 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2630 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2631 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2632 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2633 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2634 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2635 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2636 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2637 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2638 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2639 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2640 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2641 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2642 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2643 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2644 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2646 ctl2_write_typeinfos(typelib);
2648 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2650 char typelib_id[13] = "#1";
2652 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2653 if (expr)
2654 sprintf( typelib_id, "#%d", expr->cval );
2655 add_output_to_resources( "TYPELIB", typelib_id );
2656 output_typelib_regscript( typelib->typelib );
2657 flush_output_resources( typelib_name );
2659 else flush_output_buffer( typelib_name );
2662 int create_msft_typelib(typelib_t *typelib)
2664 msft_typelib_t *msft;
2665 int failed = 0;
2666 const statement_t *stmt;
2667 time_t cur_time;
2668 char *time_override;
2669 unsigned int version = 7 << 24 | 555; /* 7.00.0555 */
2670 GUID midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2671 GUID midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2672 GUID midl_info_guid = {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2673 char info_string[128];
2675 pointer_size = (typelib_kind == SYS_WIN64) ? 8 : 4;
2677 msft = xmalloc(sizeof(*msft));
2678 memset(msft, 0, sizeof(*msft));
2679 msft->typelib = typelib;
2681 ctl2_init_header(msft);
2682 ctl2_init_segdir(msft);
2684 msft->typelib_header.varflags |= typelib_kind;
2687 * The following two calls return an offset or -1 if out of memory. We
2688 * specifically need an offset of 0, however, so...
2690 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2691 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2693 if(failed)
2695 free(msft);
2696 return 0;
2699 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2700 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2702 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2703 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2705 set_lib_flags(msft);
2706 set_lcid(msft);
2707 set_help_file_name(msft);
2708 set_doc_string(msft);
2709 set_guid(msft);
2710 set_version(msft);
2711 set_name(msft);
2712 set_help_context(msft);
2713 set_help_string_dll(msft);
2714 set_help_string_context(msft);
2716 /* midl adds two sets of custom data to the library: the current unix time
2717 and midl's version number */
2718 time_override = getenv( "WIDL_TIME_OVERRIDE");
2719 cur_time = time_override ? atol( time_override) : time(NULL);
2720 sprintf(info_string, "Created by WIDL version %s at %s\n", PACKAGE_VERSION, ctime(&cur_time));
2721 set_custdata(msft, &midl_info_guid, VT_BSTR, info_string, &msft->typelib_header.CustomDataOffset);
2722 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2723 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2725 if (typelib->stmts)
2726 LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
2727 add_entry(msft, stmt);
2729 save_all_changes(msft);
2730 free(msft);
2731 return 1;