msvcp140/tests: Use wide-char string literals.
[wine.git] / tools / widl / write_msft.c
blob5728f041f6fde250dceb9560da903f54419bc8d8
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 int guidoffset;
1267 int custoffset;
1268 int *custdata;
1269 int data_out;
1270 int hash_key;
1272 hash_key = ctl2_hash_guid(guid);
1273 guidoffset = ctl2_find_guid(typelib, hash_key, guid);
1274 if(guidoffset == -1) {
1275 /* add GUID that was not already present */
1276 MSFT_GuidEntry guidentry;
1277 guidentry.guid = *guid;
1279 guidentry.hreftype = -1;
1280 guidentry.next_hash = -1;
1282 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1285 if(vt == VT_BSTR)
1286 /* TODO midl appears to share a single reference if the same string is used as custdata in multiple places */
1287 write_string_value(typelib, &data_out, value);
1288 else
1289 write_int_value(typelib, &data_out, vt, *(int*)value);
1291 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1293 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1294 custdata[0] = guidoffset;
1295 custdata[1] = data_out;
1296 custdata[2] = *offset;
1297 *offset = custoffset;
1299 return S_OK;
1302 static HRESULT set_custdata_attr(msft_typelib_t *typelib, attr_custdata_t *custdata, int *offset)
1304 switch(custdata->pval->type) {
1305 case EXPR_STRLIT:
1306 case EXPR_WSTRLIT:
1307 set_custdata(typelib, &custdata->id, VT_BSTR, custdata->pval->u.sval, offset);
1308 break;
1309 case EXPR_HEXNUM:
1310 case EXPR_NUM:
1311 set_custdata(typelib, &custdata->id, VT_I4, &custdata->pval->u.lval, offset);
1312 break;
1313 default:
1314 error("custom() attribute with unknown type\n");
1315 break;
1318 return S_OK;
1321 static HRESULT add_func_desc(msft_typeinfo_t* typeinfo, var_t *func, int index)
1323 int offset, name_offset;
1324 int *typedata, typedata_size;
1325 int i, id, next_idx;
1326 int decoded_size, extra_attr = 0;
1327 int num_params = 0, num_optional = 0, num_defaults = 0;
1328 int has_arg_custdata = 0;
1329 var_t *arg;
1330 unsigned char *namedata;
1331 const attr_t *attr;
1332 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1333 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1334 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1335 int func_custdata_offset = -1;
1336 int entry = -1, entry_is_ord = 0;
1337 int lcid_retval_count = 0;
1339 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1341 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1343 switch(typeinfo->typekind) {
1344 case TKIND_DISPATCH:
1345 funckind = 0x4; /* FUNC_DISPATCH */
1346 break;
1347 case TKIND_MODULE:
1348 funckind = 0x3; /* FUNC_STATIC */
1349 break;
1350 default:
1351 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1352 break;
1355 if (is_local( func->attrs )) {
1356 chat("add_func_desc: skipping local function\n");
1357 return S_FALSE;
1360 if (type_function_get_args(func->declspec.type))
1361 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1363 num_params++;
1364 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1365 if(attr->type == ATTR_DEFAULTVALUE)
1366 num_defaults++;
1367 else if(attr->type == ATTR_OPTIONAL)
1368 num_optional++;
1369 else if(attr->type == ATTR_CUSTOM)
1370 has_arg_custdata = 1;
1374 chat("add_func_desc: num of params %d\n", num_params);
1376 name_offset = ctl2_alloc_name(typeinfo->typelib, func->name);
1378 if (func->attrs) LIST_FOR_EACH_ENTRY( attr, func->attrs, const attr_t, entry ) {
1379 expr_t *expr = attr->u.pval;
1380 switch(attr->type) {
1381 case ATTR_BINDABLE:
1382 funcflags |= 0x4; /* FUNCFLAG_FBINDABLE */
1383 break;
1384 case ATTR_CUSTOM:
1385 set_custdata_attr(typeinfo->typelib, attr->u.pval, &func_custdata_offset);
1386 break;
1387 case ATTR_DEFAULTBIND:
1388 funcflags |= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1389 break;
1390 case ATTR_DEFAULTCOLLELEM:
1391 funcflags |= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1392 break;
1393 case ATTR_DISPLAYBIND:
1394 funcflags |= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1395 break;
1396 case ATTR_ENTRY:
1397 extra_attr = max(extra_attr, 3);
1398 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1399 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1400 else {
1401 entry = expr->cval;
1402 entry_is_ord = 1;
1404 break;
1405 case ATTR_HELPCONTEXT:
1406 extra_attr = max(extra_attr, 1);
1407 help_context = expr->u.lval;
1408 break;
1409 case ATTR_HELPSTRING:
1410 extra_attr = max(extra_attr, 2);
1411 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1412 break;
1413 case ATTR_HELPSTRINGCONTEXT:
1414 extra_attr = max(extra_attr, 6);
1415 help_string_context = expr->u.lval;
1416 break;
1417 case ATTR_HIDDEN:
1418 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1419 break;
1420 case ATTR_ID:
1421 id = expr->cval;
1422 break;
1423 case ATTR_IMMEDIATEBIND:
1424 funcflags |= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1425 break;
1426 case ATTR_NONBROWSABLE:
1427 funcflags |= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1428 break;
1429 case ATTR_OUT:
1430 break;
1431 case ATTR_PROPGET:
1432 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1433 break;
1434 case ATTR_PROPPUT:
1435 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1436 break;
1437 case ATTR_PROPPUTREF:
1438 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1439 break;
1440 /* FIXME: FUNCFLAG_FREPLACEABLE */
1441 case ATTR_REQUESTEDIT:
1442 funcflags |= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1443 break;
1444 case ATTR_RESTRICTED:
1445 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1446 break;
1447 case ATTR_SOURCE:
1448 funcflags |= 0x2; /* FUNCFLAG_FSOURCE */
1449 break;
1450 case ATTR_UIDEFAULT:
1451 funcflags |= 0x200; /* FUNCFLAG_FUIDEFAULT */
1452 break;
1453 case ATTR_USESGETLASTERROR:
1454 funcflags |= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1455 break;
1456 case ATTR_VARARG:
1457 if (num_optional || num_defaults)
1458 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1459 else
1460 num_optional = -1;
1461 break;
1462 default:
1463 break;
1467 if(has_arg_custdata || func_custdata_offset != -1) {
1468 extra_attr = max(extra_attr, 7 + num_params);
1471 /* allocate type data space for us */
1472 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1474 if (!typeinfo->func_data) {
1475 typeinfo->func_data = xmalloc(0x100);
1476 typeinfo->func_data_allocated = 0x100;
1477 typeinfo->func_data[0] = 0;
1480 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1481 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1482 typeinfo->func_data[0] + typedata_size + sizeof(int));
1483 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1486 offset = typeinfo->func_data[0];
1487 typeinfo->func_data[0] += typedata_size;
1488 typedata = typeinfo->func_data + (offset >> 2) + 1;
1491 /* find func with the same name - if it exists use its id */
1492 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1493 if(name_offset == typeinfo->func_names[i]) {
1494 id = typeinfo->func_indices[i];
1495 break;
1499 /* find the first func with the same id and link via the hiword of typedata[4] */
1500 next_idx = index;
1501 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1502 if(id == typeinfo->func_indices[i]) {
1503 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1504 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1505 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1506 break;
1510 /* fill out the basic type information */
1511 typedata[0] = typedata_size | (index << 16);
1512 encode_var(typeinfo->typelib, type_function_get_rettype(func->declspec.type), func,
1513 &typedata[1], &decoded_size);
1514 typedata[2] = funcflags;
1515 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1516 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1517 if(has_arg_custdata || func_custdata_offset != -1) typedata[4] |= 0x0080;
1518 if(num_defaults) typedata[4] |= 0x1000;
1519 if(entry_is_ord) typedata[4] |= 0x2000;
1520 typedata[5] = (num_optional << 16) | num_params;
1522 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1523 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1524 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1525 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1527 switch(extra_attr) {
1528 default:
1529 if(extra_attr > 7 + num_params) warning("unknown number of optional attrs\n");
1530 /* typedata[13..+num_params] = arg_custdata_offset handled in below loop */
1531 case 7: typedata[12] = func_custdata_offset;
1532 case 6: typedata[11] = help_string_context;
1533 case 5: typedata[10] = -1;
1534 case 4: typedata[9] = -1;
1535 case 3: typedata[8] = entry;
1536 case 2: typedata[7] = help_string_offset;
1537 case 1: typedata[6] = help_context;
1538 case 0:
1539 break;
1542 if (type_function_get_args(func->declspec.type))
1544 i = 0;
1545 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1547 int paramflags = 0;
1548 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1549 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1550 int arg_custdata_offset = -1;
1552 if(defaultdata) *defaultdata = -1;
1554 encode_var(typeinfo->typelib, arg->declspec.type, arg, paramdata, &decoded_size);
1555 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1556 switch(attr->type) {
1557 case ATTR_CUSTOM:
1558 set_custdata_attr(typeinfo->typelib, attr->u.pval, &arg_custdata_offset);
1559 break;
1560 case ATTR_DEFAULTVALUE:
1562 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1563 write_default_value(typeinfo->typelib, arg->declspec.type, (expr_t *)attr->u.pval, defaultdata);
1564 break;
1566 case ATTR_IN:
1567 paramflags |= 0x01; /* PARAMFLAG_FIN */
1568 break;
1569 case ATTR_OPTIONAL:
1570 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1571 break;
1572 case ATTR_OUT:
1573 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1574 break;
1575 case ATTR_PARAMLCID:
1576 paramflags |= 0x04; /* PARAMFLAG_LCID */
1577 lcid_retval_count++;
1578 break;
1579 case ATTR_RETVAL:
1580 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1581 lcid_retval_count++;
1582 break;
1583 default:
1584 chat("unhandled param attr %d\n", attr->type);
1585 break;
1587 if(extra_attr > 7 + i) {
1588 typedata[13+i] = arg_custdata_offset;
1591 paramdata[1] = -1;
1592 paramdata[2] = paramflags;
1593 typedata[3] += decoded_size << 16;
1595 i++;
1599 if(lcid_retval_count == 1)
1600 typedata[4] |= 0x4000;
1601 else if(lcid_retval_count == 2)
1602 typedata[4] |= 0x8000;
1604 if(typeinfo->funcs_allocated == 0) {
1605 typeinfo->funcs_allocated = 10;
1606 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1607 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1608 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1610 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1611 typeinfo->funcs_allocated *= 2;
1612 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1613 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1614 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1617 /* update the index data */
1618 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1619 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1620 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1622 /* ??? */
1623 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1624 typeinfo->typeinfo->res2 <<= 1;
1625 /* ??? */
1626 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1628 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1629 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1630 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1632 /* adjust size of VTBL */
1633 if(funckind != 0x3 /* FUNC_STATIC */)
1634 typeinfo->typeinfo->cbSizeVft += pointer_size;
1636 /* Increment the number of function elements */
1637 typeinfo->typeinfo->cElement += 1;
1639 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1640 if (*((INT *)namedata) == -1) {
1641 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1642 if(typeinfo->typekind == TKIND_MODULE)
1643 namedata[9] |= 0x10;
1644 } else
1645 namedata[9] &= ~0x10;
1647 if(typeinfo->typekind == TKIND_MODULE)
1648 namedata[9] |= 0x20;
1650 if (type_function_get_args(func->declspec.type))
1652 i = 0;
1653 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1655 /* don't give the last arg of a [propput*] func a name */
1656 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1658 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1659 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1660 paramdata[1] = offset;
1662 i++;
1665 return S_OK;
1668 static HRESULT add_var_desc(msft_typeinfo_t *typeinfo, UINT index, var_t* var)
1670 int offset, id;
1671 unsigned int typedata_size;
1672 int extra_attr = 0;
1673 INT *typedata;
1674 unsigned int var_datawidth, var_alignment = 0;
1675 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1676 int alignment;
1677 int varflags = 0;
1678 const attr_t *attr;
1679 unsigned char *namedata;
1680 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1681 int var_custdata_offset = -1;
1683 if (!var->name)
1684 var->name = gen_name();
1686 chat("add_var_desc(%d, %s)\n", index, var->name);
1688 id = 0x40000000 + index;
1690 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1691 expr_t *expr = attr->u.pval;
1692 switch(attr->type) {
1693 case ATTR_BINDABLE:
1694 varflags |= 0x04; /* VARFLAG_FBINDABLE */
1695 break;
1696 case ATTR_CUSTOM:
1697 extra_attr = max(extra_attr,4);
1698 set_custdata_attr(typeinfo->typelib, attr->u.pval, &var_custdata_offset);
1699 break;
1700 case ATTR_DEFAULTBIND:
1701 varflags |= 0x20; /* VARFLAG_FDEFAULTBIND */
1702 break;
1703 case ATTR_DEFAULTCOLLELEM:
1704 varflags |= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1705 break;
1706 case ATTR_DISPLAYBIND:
1707 varflags |= 0x10; /* VARFLAG_FDISPLAYBIND */
1708 break;
1709 case ATTR_HIDDEN:
1710 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1711 break;
1712 case ATTR_ID:
1713 id = expr->cval;
1714 break;
1715 case ATTR_IMMEDIATEBIND:
1716 varflags |= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1717 break;
1718 case ATTR_NONBROWSABLE:
1719 varflags |= 0x400; /* VARFLAG_FNONBROWSABLE */
1720 break;
1721 case ATTR_READONLY:
1722 varflags |= 0x01; /* VARFLAG_FREADONLY */
1723 break;
1724 /* FIXME: VARFLAG_FREPLACEABLE */
1725 case ATTR_REQUESTEDIT:
1726 varflags |= 0x08; /* VARFLAG_FREQUESTEDIT */
1727 break;
1728 case ATTR_RESTRICTED:
1729 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1730 break;
1731 case ATTR_SOURCE:
1732 varflags |= 0x02; /* VARFLAG_FSOURCE */
1733 break;
1734 case ATTR_UIDEFAULT:
1735 varflags |= 0x0200; /* VARFLAG_FUIDEFAULT */
1736 break;
1737 default:
1738 break;
1742 /* allocate type data space for us */
1743 typedata_size = 0x14 + extra_attr * sizeof(int);
1745 if (!typeinfo->var_data) {
1746 typeinfo->var_data = xmalloc(0x100);
1747 typeinfo->var_data_allocated = 0x100;
1748 typeinfo->var_data[0] = 0;
1751 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1752 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1753 typeinfo->var_data[0] + typedata_size + sizeof(int));
1754 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1757 offset = typeinfo->var_data[0];
1758 typeinfo->var_data[0] += typedata_size;
1759 typedata = typeinfo->var_data + (offset >> 2) + 1;
1761 /* fill out the basic type information */
1762 typedata[0] = typedata_size | (index << 16);
1763 typedata[2] = varflags;
1764 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1766 if(typeinfo->vars_allocated == 0) {
1767 typeinfo->vars_allocated = 10;
1768 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1769 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1770 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1772 if(typeinfo->vars_allocated == var_num) {
1773 typeinfo->vars_allocated *= 2;
1774 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1775 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1776 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1778 /* update the index data */
1779 typeinfo->var_indices[var_num] = id;
1780 typeinfo->var_names[var_num] = -1;
1781 typeinfo->var_offsets[var_num] = offset;
1783 /* figure out type widths and whatnot */
1784 var_datawidth = type_memsize_and_alignment(var->declspec.type, &var_alignment);
1785 encode_var(typeinfo->typelib, var->declspec.type, var, &typedata[1], &var_type_size);
1787 /* pad out starting position to data width */
1788 typeinfo->datawidth += var_alignment - 1;
1789 typeinfo->datawidth &= ~(var_alignment - 1);
1791 switch(typeinfo->typekind) {
1792 case TKIND_ENUM:
1793 write_int_value(typeinfo->typelib, &typedata[4], VT_I4, var->eval->cval);
1794 var_kind = 2; /* VAR_CONST */
1795 var_type_size += 16; /* sizeof(VARIANT) */
1796 typeinfo->datawidth = var_datawidth;
1797 break;
1798 case TKIND_RECORD:
1799 typedata[4] = typeinfo->datawidth;
1800 typeinfo->datawidth += var_datawidth;
1801 break;
1802 case TKIND_UNION:
1803 typedata[4] = 0;
1804 typeinfo->datawidth = max(typeinfo->datawidth, var_datawidth);
1805 break;
1806 case TKIND_DISPATCH:
1807 var_kind = 3; /* VAR_DISPATCH */
1808 typedata[4] = 0;
1809 typeinfo->datawidth = pointer_size;
1810 break;
1811 default:
1812 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1813 break;
1816 /* add type description size to total required allocation */
1817 typedata[3] += var_type_size << 16 | var_kind;
1819 switch(extra_attr) {
1820 case 5: typedata[9] = -1 /*help_string_context*/;
1821 case 4: typedata[8] = var_custdata_offset;
1822 case 3: typedata[7] = -1;
1823 case 2: typedata[6] = -1 /*help_string_offset*/;
1824 case 1: typedata[5] = -1 /*help_context*/;
1825 case 0:
1826 break;
1827 default:
1828 warning("unknown number of optional attrs\n");
1831 /* fix type alignment */
1832 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1833 if (alignment < var_alignment) {
1834 alignment = var_alignment;
1835 typeinfo->typeinfo->typekind &= ~0xffc0;
1836 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1839 /* ??? */
1840 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1841 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1842 typeinfo->typeinfo->res2 <<= 1;
1845 /* ??? */
1846 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1847 typeinfo->typeinfo->res3 += 0x2c;
1849 /* increment the number of variable elements */
1850 typeinfo->typeinfo->cElement += 0x10000;
1852 /* pad data width to alignment */
1853 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1855 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1856 if (offset == -1) return E_OUTOFMEMORY;
1858 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1859 if (*((INT *)namedata) == -1) {
1860 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1861 if(typeinfo->typekind != TKIND_DISPATCH)
1862 namedata[9] |= 0x10;
1863 } else
1864 namedata[9] &= ~0x10;
1866 if (typeinfo->typekind == TKIND_ENUM) {
1867 namedata[9] |= 0x20;
1869 typeinfo->var_names[var_num] = offset;
1871 return S_OK;
1874 static HRESULT add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1876 if(importinfo) {
1877 alloc_importinfo(typeinfo->typelib, importinfo);
1878 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1879 }else {
1880 if(ref->typelib_idx == -1)
1881 add_interface_typeinfo(typeinfo->typelib, ref);
1882 if(ref->typelib_idx == -1)
1883 error("add_impl_type: unable to add inherited interface\n");
1885 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1888 typeinfo->typeinfo->cImplTypes++;
1889 return S_OK;
1892 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1893 const char *name, const attr_list_t *attrs)
1895 const attr_t *attr;
1896 msft_typeinfo_t *msft_typeinfo;
1897 int nameoffset;
1898 int typeinfo_offset;
1899 MSFT_TypeInfoBase *typeinfo;
1900 MSFT_GuidEntry guidentry;
1902 chat("create_msft_typeinfo: name %s kind %d index %d\n", name, kind, typelib->typelib_header.nrtypeinfos);
1904 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1905 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1907 msft_typeinfo->typelib = typelib;
1909 nameoffset = ctl2_alloc_name(typelib, name);
1910 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1911 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1913 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1914 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1916 msft_typeinfo->typekind = kind;
1917 msft_typeinfo->typeinfo = typeinfo;
1919 typeinfo->typekind |= kind | 0x20;
1921 if(kind == TKIND_COCLASS)
1922 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1924 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1925 switch(attr->type) {
1926 case ATTR_AGGREGATABLE:
1927 if (kind == TKIND_COCLASS)
1928 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1929 break;
1931 case ATTR_APPOBJECT:
1932 if (kind == TKIND_COCLASS)
1933 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1934 break;
1936 case ATTR_CONTROL:
1937 if (kind == TKIND_COCLASS)
1938 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1939 break;
1940 case ATTR_CUSTOM:
1941 set_custdata_attr(typelib, attr->u.pval, &typeinfo->oCustData);
1942 break;
1943 case ATTR_DLLNAME:
1945 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1946 typeinfo->datatype1 = offset;
1947 break;
1950 case ATTR_DUAL:
1951 /* FIXME: check interface is compatible */
1952 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1953 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1954 break;
1956 case ATTR_HELPCONTEXT:
1958 expr_t *expr = (expr_t*)attr->u.pval;
1959 typeinfo->helpcontext = expr->cval;
1960 break;
1962 case ATTR_HELPSTRING:
1964 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1965 if (offset == -1) break;
1966 typeinfo->docstringoffs = offset;
1967 break;
1969 case ATTR_HELPSTRINGCONTEXT:
1971 expr_t *expr = (expr_t*)attr->u.pval;
1972 typeinfo->helpstringcontext = expr->cval;
1973 break;
1975 case ATTR_HIDDEN:
1976 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1977 break;
1979 case ATTR_LICENSED:
1980 typeinfo->flags |= 0x04; /* TYPEFLAG_FLICENSED */
1981 break;
1983 case ATTR_NONCREATABLE:
1984 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1985 break;
1987 case ATTR_NONEXTENSIBLE:
1988 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1989 break;
1991 case ATTR_OLEAUTOMATION:
1992 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1993 break;
1995 /* FIXME: TYPEFLAG_FPREDCLID */
1997 case ATTR_PROXY:
1998 typeinfo->flags |= 0x4000; /* TYPEFLAG_FPROXY */
1999 break;
2001 /* FIXME: TYPEFLAG_FREPLACEABLE */
2003 case ATTR_RESTRICTED:
2004 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
2005 break;
2007 case ATTR_UUID:
2008 guidentry.guid = *(GUID*)attr->u.pval;
2009 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
2010 guidentry.next_hash = -1;
2011 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
2012 #if 0
2013 if (IsEqualIID(guid, &IID_IDispatch)) {
2014 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
2016 #endif
2017 break;
2019 case ATTR_VERSION:
2020 typeinfo->version = attr->u.ival;
2021 break;
2023 default:
2024 break;
2028 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
2029 typelib->last_typeinfo = msft_typeinfo;
2030 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
2033 return msft_typeinfo;
2036 static void add_dispatch(msft_typelib_t *typelib)
2038 int guid_offset, impfile_offset, hash_key;
2039 MSFT_GuidEntry guidentry;
2040 MSFT_ImpInfo impinfo;
2041 GUID stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2042 GUID iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2044 if(typelib->typelib_header.dispatchpos != -1) return;
2046 guidentry.guid = stdole;
2047 guidentry.hreftype = 2;
2048 guidentry.next_hash = -1;
2049 hash_key = ctl2_hash_guid(&guidentry.guid);
2050 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2051 if (guid_offset == -1)
2052 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2053 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
2055 guidentry.guid = iid_idispatch;
2056 guidentry.hreftype = 1;
2057 guidentry.next_hash = -1;
2058 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
2059 impinfo.oImpFile = impfile_offset;
2060 hash_key = ctl2_hash_guid(&guidentry.guid);
2061 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2062 if (guid_offset == -1)
2063 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2064 impinfo.oGuid = guid_offset;
2065 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
2068 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
2070 int num_parents = 0, num_funcs = 0;
2071 importinfo_t *importinfo = NULL;
2072 const statement_t *stmt_func;
2073 type_t *inherit, *ref;
2074 int idx = 0;
2075 var_t *func;
2076 var_t *var;
2077 msft_typeinfo_t *msft_typeinfo;
2079 if (-1 < dispinterface->typelib_idx)
2080 return;
2082 inherit = type_dispiface_get_inherit(dispinterface);
2084 if (inherit)
2086 importinfo = find_importinfo(typelib, inherit->name);
2088 if (!importinfo && type_iface_get_inherit(inherit) && inherit->typelib_idx == -1)
2089 add_interface_typeinfo(typelib, inherit);
2092 /* check typelib_idx again, it could have been added while resolving the parent interface */
2093 if (-1 < dispinterface->typelib_idx)
2094 return;
2096 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2097 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
2098 dispinterface->attrs);
2100 msft_typeinfo->typeinfo->size = pointer_size;
2101 msft_typeinfo->typeinfo->typekind |= pointer_size << 11 | pointer_size << 6;
2103 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2104 add_dispatch(typelib);
2106 if (inherit)
2108 add_impl_type(msft_typeinfo, inherit, importinfo);
2109 msft_typeinfo->typeinfo->typekind |= 0x10;
2112 /* count the number of inherited interfaces and non-local functions */
2113 for (ref = inherit; ref; ref = type_iface_get_inherit(ref))
2115 num_parents++;
2116 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) )
2118 var_t *func = stmt_func->u.var;
2119 if (!is_local(func->attrs)) num_funcs++;
2122 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2123 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2125 msft_typeinfo->typeinfo->cImplTypes = 1; /* IDispatch */
2127 /* count the no of methods, as the variable indices come after the funcs */
2128 if (dispinterface->details.iface->disp_methods)
2129 LIST_FOR_EACH_ENTRY( func, dispinterface->details.iface->disp_methods, var_t, entry )
2130 idx++;
2132 if (type_dispiface_get_props(dispinterface))
2133 LIST_FOR_EACH_ENTRY( var, type_dispiface_get_props(dispinterface), var_t, entry )
2134 add_var_desc(msft_typeinfo, idx++, var);
2136 if (type_dispiface_get_methods(dispinterface))
2138 idx = 0;
2139 LIST_FOR_EACH_ENTRY( func, type_dispiface_get_methods(dispinterface), var_t, entry )
2140 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2141 idx++;
2144 typelib->typelib->reg_ifaces = xrealloc(typelib->typelib->reg_ifaces,
2145 (typelib->typelib->reg_iface_count + 1) * sizeof(dispinterface));
2146 typelib->typelib->reg_ifaces[typelib->typelib->reg_iface_count++] = dispinterface;
2149 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
2151 int idx = 0;
2152 const statement_t *stmt_func;
2153 type_t *ref;
2154 msft_typeinfo_t *msft_typeinfo;
2155 importinfo_t *ref_importinfo = NULL;
2156 int num_parents = 0, num_funcs = 0;
2157 type_t *inherit;
2158 const type_t *derived;
2160 if (-1 < interface->typelib_idx)
2161 return;
2163 if (!interface->details.iface)
2165 error( "interface %s is referenced but not defined\n", interface->name );
2166 return;
2169 if (is_attr(interface->attrs, ATTR_DISPINTERFACE)) {
2170 add_dispinterface_typeinfo(typelib, interface);
2171 return;
2174 /* midl adds the parent interface first, unless the parent itself
2175 has no parent (i.e. it stops before IUnknown). */
2177 inherit = type_iface_get_inherit(interface);
2179 if(inherit) {
2180 ref_importinfo = find_importinfo(typelib, inherit->name);
2182 if(!ref_importinfo && type_iface_get_inherit(inherit) &&
2183 inherit->typelib_idx == -1)
2184 add_interface_typeinfo(typelib, inherit);
2187 /* check typelib_idx again, it could have been added while resolving the parent interface */
2188 if (-1 < interface->typelib_idx)
2189 return;
2191 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2192 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
2193 msft_typeinfo->typeinfo->size = pointer_size;
2194 msft_typeinfo->typeinfo->typekind |= 0x0200;
2195 msft_typeinfo->typeinfo->typekind |= pointer_size << 11;
2197 for (derived = inherit; derived; derived = type_iface_get_inherit(derived))
2198 if (derived->name && !strcmp(derived->name, "IDispatch"))
2199 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2201 if(type_iface_get_inherit(interface))
2202 add_impl_type(msft_typeinfo, type_iface_get_inherit(interface),
2203 ref_importinfo);
2205 /* count the number of inherited interfaces and non-local functions */
2206 for(ref = inherit; ref; ref = type_iface_get_inherit(ref)) {
2207 num_parents++;
2208 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) ) {
2209 var_t *func = stmt_func->u.var;
2210 if (!is_local(func->attrs)) num_funcs++;
2213 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2214 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2216 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(interface) ) {
2217 var_t *func = stmt_func->u.var;
2218 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2219 idx++;
2222 if (is_attr(interface->attrs, ATTR_OLEAUTOMATION) || is_attr(interface->attrs, ATTR_DUAL))
2224 typelib->typelib->reg_ifaces = xrealloc(typelib->typelib->reg_ifaces,
2225 (typelib->typelib->reg_iface_count + 1) * sizeof(interface));
2226 typelib->typelib->reg_ifaces[typelib->typelib->reg_iface_count++] = interface;
2230 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2232 var_list_t *fields;
2233 int idx = 0;
2234 var_t *cur;
2235 msft_typeinfo_t *msft_typeinfo;
2237 if (-1 < structure->typelib_idx)
2238 return;
2240 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2241 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2242 msft_typeinfo->typeinfo->size = 0;
2244 if (type_get_type(structure) == TYPE_STRUCT)
2245 fields = type_struct_get_fields(structure);
2246 else
2247 fields = type_encapsulated_union_get_fields(structure);
2249 if (fields)
2251 LIST_FOR_EACH_ENTRY( cur, fields, var_t, entry )
2252 add_var_desc(msft_typeinfo, idx++, cur);
2256 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2258 int idx = 0;
2259 var_t *cur;
2260 msft_typeinfo_t *msft_typeinfo;
2262 if (-1 < enumeration->typelib_idx)
2263 return;
2265 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2266 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2267 msft_typeinfo->typeinfo->size = 0;
2269 if (type_enum_get_values(enumeration))
2270 LIST_FOR_EACH_ENTRY( cur, type_enum_get_values(enumeration), var_t, entry )
2271 add_var_desc(msft_typeinfo, idx++, cur);
2274 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion)
2276 int idx = 0;
2277 var_t *cur;
2278 msft_typeinfo_t *msft_typeinfo;
2280 if (-1 < tunion->typelib_idx)
2281 return;
2283 if (!tunion->name)
2284 tunion->name = gen_name();
2286 tunion->typelib_idx = typelib->typelib_header.nrtypeinfos;
2287 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_UNION, tunion->name, tunion->attrs);
2288 msft_typeinfo->typeinfo->size = 0;
2290 if (type_union_get_cases(tunion))
2291 LIST_FOR_EACH_ENTRY(cur, type_union_get_cases(tunion), var_t, entry)
2292 add_var_desc(msft_typeinfo, idx++, cur);
2295 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2297 msft_typeinfo_t *msft_typeinfo = NULL;
2298 int datatype1, datatype2, duplicate = 0;
2299 unsigned int size, alignment = 0;
2300 type_t *type;
2302 if (-1 < tdef->typelib_idx)
2303 return;
2305 type = type_alias_get_aliasee_type(tdef);
2307 if (!type->name || strcmp(tdef->name, type->name) != 0)
2309 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2310 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2312 else
2313 duplicate = 1;
2315 encode_type(typelib, get_type_vt(type), type, &datatype1, &datatype2);
2316 size = type_memsize_and_alignment(type, &alignment);
2318 if (msft_typeinfo)
2320 msft_typeinfo->typeinfo->datatype1 = datatype1;
2321 msft_typeinfo->typeinfo->size = size;
2322 msft_typeinfo->typeinfo->datatype2 = datatype2;
2323 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2326 /* avoid adding duplicate type definitions */
2327 if (duplicate)
2328 tdef->typelib_idx = type->typelib_idx;
2331 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2333 msft_typeinfo_t *msft_typeinfo;
2334 ifref_t *iref;
2335 int num_ifaces = 0, offset, i;
2336 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2337 int have_default = 0, have_default_source = 0;
2338 const attr_t *attr;
2339 ifref_list_t *ifaces;
2341 if (-1 < cls->typelib_idx)
2342 return;
2344 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2345 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2347 ifaces = type_coclass_get_ifaces(cls);
2348 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) num_ifaces++;
2350 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2351 num_ifaces * sizeof(*ref), 0);
2353 i = 0;
2354 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) {
2355 if(iref->iface->typelib_idx == -1)
2356 add_interface_typeinfo(typelib, iref->iface);
2357 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2358 ref->reftype = typelib->typelib_typeinfo_offsets[iref->iface->typelib_idx];
2359 ref->flags = 0;
2360 ref->oCustData = -1;
2361 ref->onext = -1;
2362 if(i < num_ifaces - 1)
2363 ref->onext = offset + (i + 1) * sizeof(*ref);
2365 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2366 switch(attr->type) {
2367 case ATTR_DEFAULT:
2368 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2369 break;
2370 case ATTR_DEFAULTVTABLE:
2371 ref->flags |= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2372 break;
2373 case ATTR_RESTRICTED:
2374 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2375 break;
2376 case ATTR_SOURCE:
2377 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2378 break;
2379 default:
2380 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2383 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2384 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2385 have_default_source = 1;
2386 else
2387 have_default = 1;
2390 /* If the interface is non-restricted and we haven't already had one then
2391 remember it so that we can use it as a default later */
2392 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2393 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2394 if(!first_source)
2395 first_source = ref;
2397 else if(!first)
2398 first = ref;
2400 i++;
2403 /* If we haven't had a default interface, then set the default flags on the
2404 first ones */
2405 if(!have_default && first)
2406 first->flags |= 0x1;
2407 if(!have_default_source && first_source)
2408 first_source->flags |= 0x1;
2410 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2411 msft_typeinfo->typeinfo->size = pointer_size;
2412 msft_typeinfo->typeinfo->typekind |= 0x2200;
2415 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2417 int idx = 0;
2418 const statement_t *stmt;
2419 msft_typeinfo_t *msft_typeinfo;
2421 if (-1 < module->typelib_idx)
2422 return;
2424 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2425 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2426 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2428 STATEMENTS_FOR_EACH_FUNC( stmt, module->details.module->stmts ) {
2429 var_t *func = stmt->u.var;
2430 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2431 idx++;
2434 msft_typeinfo->typeinfo->size = idx;
2437 static void add_type_typeinfo(msft_typelib_t *typelib, type_t *type)
2439 switch (type_get_type(type)) {
2440 case TYPE_INTERFACE:
2441 add_interface_typeinfo(typelib, type);
2442 break;
2443 case TYPE_STRUCT:
2444 case TYPE_ENCAPSULATED_UNION:
2445 add_structure_typeinfo(typelib, type);
2446 break;
2447 case TYPE_ENUM:
2448 add_enum_typeinfo(typelib, type);
2449 break;
2450 case TYPE_UNION:
2451 add_union_typeinfo(typelib, type);
2452 break;
2453 case TYPE_COCLASS:
2454 add_coclass_typeinfo(typelib, type);
2455 break;
2456 case TYPE_BASIC:
2457 case TYPE_POINTER:
2458 case TYPE_ARRAY:
2459 break;
2460 default:
2461 error("add_entry: unhandled type 0x%x for %s\n",
2462 type_get_type(type), type->name);
2463 break;
2467 static void add_entry(msft_typelib_t *typelib, const statement_t *stmt)
2469 switch(stmt->type) {
2470 case STMT_LIBRARY:
2471 case STMT_IMPORT:
2472 case STMT_PRAGMA:
2473 case STMT_CPPQUOTE:
2474 case STMT_DECLARATION:
2475 /* not included in typelib */
2476 break;
2477 case STMT_IMPORTLIB:
2478 /* not processed here */
2479 break;
2480 case STMT_TYPEDEF:
2482 const type_list_t *type_entry = stmt->u.type_list;
2483 for (; type_entry; type_entry = type_entry->next) {
2484 /* if the type is public then add the typedef, otherwise attempt
2485 * to add the aliased type */
2486 if (is_attr(type_entry->type->attrs, ATTR_PUBLIC))
2487 add_typedef_typeinfo(typelib, type_entry->type);
2488 else
2489 add_type_typeinfo(typelib, type_alias_get_aliasee_type(type_entry->type));
2491 break;
2493 case STMT_MODULE:
2494 add_module_typeinfo(typelib, stmt->u.type);
2495 break;
2496 case STMT_TYPE:
2497 case STMT_TYPEREF:
2499 type_t *type = stmt->u.type;
2500 add_type_typeinfo(typelib, type);
2501 break;
2506 static void set_name(msft_typelib_t *typelib)
2508 int offset;
2510 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2511 if (offset == -1) return;
2512 typelib->typelib_header.NameOffset = offset;
2513 return;
2516 static void set_version(msft_typelib_t *typelib)
2518 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2521 static void set_guid(msft_typelib_t *typelib)
2523 MSFT_GuidEntry guidentry;
2524 int offset;
2525 void *ptr;
2526 GUID guid = {0,0,0,{0,0,0,0,0,0}};
2528 guidentry.guid = guid;
2529 guidentry.hreftype = -2;
2530 guidentry.next_hash = -1;
2532 ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2533 if (ptr) guidentry.guid = *(GUID *)ptr;
2535 offset = ctl2_alloc_guid(typelib, &guidentry);
2536 typelib->typelib_header.posguid = offset;
2538 return;
2541 static void set_doc_string(msft_typelib_t *typelib)
2543 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2545 if (str)
2547 int offset = ctl2_alloc_string(typelib, str);
2548 if (offset != -1) typelib->typelib_header.helpstring = offset;
2552 static void set_help_file_name(msft_typelib_t *typelib)
2554 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2556 if (str)
2558 int offset = ctl2_alloc_string(typelib, str);
2559 if (offset != -1)
2561 typelib->typelib_header.helpfile = offset;
2562 typelib->typelib_header.varflags |= 0x10;
2567 static void set_help_context(msft_typelib_t *typelib)
2569 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2570 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2573 static void set_help_string_dll(msft_typelib_t *typelib)
2575 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2577 if (str)
2579 int offset = ctl2_alloc_string(typelib, str);
2580 if (offset != -1)
2582 typelib->help_string_dll_offset = offset;
2583 typelib->typelib_header.varflags |= 0x100;
2588 static void set_help_string_context(msft_typelib_t *typelib)
2590 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2591 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2594 static void set_lcid(msft_typelib_t *typelib)
2596 const expr_t *lcid_expr = get_attrp( typelib->typelib->attrs, ATTR_LIBLCID );
2597 if(lcid_expr)
2599 typelib->typelib_header.lcid = lcid_expr->cval;
2600 typelib->typelib_header.lcid2 = lcid_expr->cval;
2604 static void set_lib_flags(msft_typelib_t *typelib)
2606 const attr_t *attr;
2608 typelib->typelib_header.flags = 0;
2609 if (!typelib->typelib->attrs) return;
2610 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2612 switch(attr->type) {
2613 case ATTR_CONTROL:
2614 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2615 break;
2616 case ATTR_HIDDEN:
2617 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2618 break;
2619 case ATTR_RESTRICTED:
2620 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2621 break;
2622 default:
2623 break;
2626 return;
2629 static void ctl2_write_segment(msft_typelib_t *typelib, int segment)
2631 if (typelib->typelib_segment_data[segment])
2632 put_data(typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length);
2635 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2637 msft_typeinfo_t *typeinfo;
2639 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2640 typeinfo->typeinfo->memoffset = filesize;
2641 if (typeinfo->func_data)
2642 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2643 if (typeinfo->var_data)
2644 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2645 if (typeinfo->func_data || typeinfo->var_data)
2646 filesize += 4;
2650 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2652 if (typelib->typelib_segdir[segment].length) {
2653 typelib->typelib_segdir[segment].offset = filepos;
2654 } else {
2655 typelib->typelib_segdir[segment].offset = -1;
2658 return typelib->typelib_segdir[segment].length;
2662 static void ctl2_write_typeinfos(msft_typelib_t *typelib)
2664 msft_typeinfo_t *typeinfo;
2665 int typedata_size;
2667 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2668 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2669 typedata_size = 0;
2670 if (typeinfo->func_data)
2671 typedata_size = typeinfo->func_data[0];
2672 if (typeinfo->var_data)
2673 typedata_size += typeinfo->var_data[0];
2674 put_data(&typedata_size, sizeof(int));
2675 if (typeinfo->func_data)
2676 put_data(typeinfo->func_data + 1, typeinfo->func_data[0]);
2677 if (typeinfo->var_data)
2678 put_data(typeinfo->var_data + 1, typeinfo->var_data[0]);
2679 if (typeinfo->func_indices)
2680 put_data(typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2681 if (typeinfo->var_indices)
2682 put_data(typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2683 if (typeinfo->func_names)
2684 put_data(typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2685 if (typeinfo->var_names)
2686 put_data(typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2687 if (typeinfo->func_offsets)
2688 put_data(typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2689 if (typeinfo->var_offsets) {
2690 int add = 0, i, offset;
2691 if(typeinfo->func_data)
2692 add = typeinfo->func_data[0];
2693 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2694 offset = typeinfo->var_offsets[i];
2695 offset += add;
2696 put_data(&offset, 4);
2702 static void save_all_changes(msft_typelib_t *typelib)
2704 int filepos;
2706 chat("save_all_changes(%p)\n", typelib);
2708 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2709 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2710 filepos += typelib->typelib_header.nrtypeinfos * 4;
2712 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2713 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2714 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2715 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2716 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2717 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2718 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2719 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2720 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2721 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2722 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2723 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2724 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2726 ctl2_finalize_typeinfos(typelib, filepos);
2728 byte_swapped = 0;
2729 init_output_buffer();
2731 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2732 if(typelib->typelib_header.varflags & 0x100)
2733 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2735 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2736 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2737 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2738 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2739 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2740 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2741 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2742 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2743 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2744 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2745 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2746 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2747 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2748 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2749 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2751 ctl2_write_typeinfos(typelib);
2753 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2755 char typelib_id[13] = "#1";
2757 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2758 if (expr)
2759 sprintf( typelib_id, "#%d", expr->cval );
2760 add_output_to_resources( "TYPELIB", typelib_id );
2761 output_typelib_regscript( typelib->typelib );
2763 else flush_output_buffer( typelib_name );
2766 int create_msft_typelib(typelib_t *typelib)
2768 msft_typelib_t *msft;
2769 int failed = 0;
2770 const statement_t *stmt;
2771 const attr_t *attr;
2772 time_t cur_time;
2773 char *time_override;
2774 unsigned int version = 7 << 24 | 555; /* 7.00.0555 */
2775 GUID midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2776 GUID midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2777 GUID midl_info_guid = {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2778 char info_string[128];
2780 msft = xmalloc(sizeof(*msft));
2781 memset(msft, 0, sizeof(*msft));
2782 msft->typelib = typelib;
2784 ctl2_init_header(msft);
2785 ctl2_init_segdir(msft);
2787 msft->typelib_header.varflags |= (pointer_size == 8) ? SYS_WIN64 : SYS_WIN32;
2790 * The following two calls return an offset or -1 if out of memory. We
2791 * specifically need an offset of 0, however, so...
2793 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2794 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2796 if(failed)
2798 free(msft);
2799 return 0;
2802 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2803 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2805 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2806 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2808 set_lib_flags(msft);
2809 set_lcid(msft);
2810 set_help_file_name(msft);
2811 set_doc_string(msft);
2812 set_guid(msft);
2813 set_version(msft);
2814 set_name(msft);
2815 set_help_context(msft);
2816 set_help_string_dll(msft);
2817 set_help_string_context(msft);
2819 if (typelib->attrs) LIST_FOR_EACH_ENTRY( attr, typelib->attrs, const attr_t, entry ) {
2820 switch(attr->type) {
2821 case ATTR_CUSTOM:
2822 set_custdata_attr(msft, attr->u.pval, &msft->typelib_header.CustomDataOffset);
2823 break;
2824 default:
2825 break;
2829 /* midl adds two sets of custom data to the library: the current unix time
2830 and midl's version number */
2831 time_override = getenv( "WIDL_TIME_OVERRIDE");
2832 cur_time = time_override ? atol( time_override) : time(NULL);
2833 sprintf(info_string, "Created by WIDL version %s at %s", PACKAGE_VERSION, ctime(&cur_time));
2834 set_custdata(msft, &midl_info_guid, VT_BSTR, info_string, &msft->typelib_header.CustomDataOffset);
2835 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2836 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2838 if (typelib->stmts)
2839 LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
2840 add_entry(msft, stmt);
2842 save_all_changes(msft);
2843 free(msft);
2844 return 1;