windowscodecs: Silence fixme for IID_CMetaBitmapRenderTarget.
[wine.git] / tools / widl / write_msft.c
blobce99c23f16a86b6e162029d32a08b28bcce32206
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"
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <time.h>
39 #include "widl.h"
40 #include "typelib.h"
41 #include "typelib_struct.h"
42 #include "utils.h"
43 #include "header.h"
44 #include "hash.h"
45 #include "typetree.h"
46 #include "parser.h"
47 #include "typegen.h"
49 enum MSFT_segment_index {
50 MSFT_SEG_TYPEINFO = 0, /* type information */
51 MSFT_SEG_IMPORTINFO, /* import information */
52 MSFT_SEG_IMPORTFILES, /* import filenames */
53 MSFT_SEG_REFERENCES, /* references (?) */
54 MSFT_SEG_GUIDHASH, /* hash table for guids? */
55 MSFT_SEG_GUID, /* guid storage */
56 MSFT_SEG_NAMEHASH, /* hash table for names */
57 MSFT_SEG_NAME, /* name storage */
58 MSFT_SEG_STRING, /* string storage */
59 MSFT_SEG_TYPEDESC, /* type descriptions */
60 MSFT_SEG_ARRAYDESC, /* array descriptions */
61 MSFT_SEG_CUSTDATA, /* custom data */
62 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
63 MSFT_SEG_UNKNOWN, /* ??? */
64 MSFT_SEG_UNKNOWN2, /* ??? */
65 MSFT_SEG_MAX /* total number of segments */
68 typedef struct tagMSFT_ImpFile {
69 int guid;
70 int lcid;
71 int version;
72 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
73 } MSFT_ImpFile;
75 typedef struct _msft_typelib_t
77 typelib_t *typelib;
78 MSFT_Header typelib_header;
79 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
80 unsigned char *typelib_segment_data[MSFT_SEG_MAX];
81 int typelib_segment_block_length[MSFT_SEG_MAX];
83 int typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
85 int *typelib_namehash_segment;
86 int *typelib_guidhash_segment;
88 int help_string_dll_offset;
90 struct _msft_typeinfo_t *typeinfos;
91 struct _msft_typeinfo_t *last_typeinfo;
92 } msft_typelib_t;
94 typedef struct _msft_typeinfo_t
96 msft_typelib_t *typelib;
97 MSFT_TypeInfoBase *typeinfo;
99 int typekind;
101 unsigned int var_data_allocated;
102 int *var_data;
104 unsigned int func_data_allocated;
105 int *func_data;
107 int vars_allocated;
108 int *var_indices;
109 int *var_names;
110 int *var_offsets;
112 int funcs_allocated;
113 int *func_indices;
114 int *func_names;
115 int *func_offsets;
117 int datawidth;
119 struct _msft_typeinfo_t *next_typeinfo;
120 } msft_typeinfo_t;
124 /*================== Internal functions ===================================*/
126 /****************************************************************************
127 * ctl2_init_header
129 * Initializes the type library header of a new typelib.
131 static void ctl2_init_header(
132 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
134 typelib->typelib_header.magic1 = 0x5446534d;
135 typelib->typelib_header.magic2 = 0x00010002;
136 typelib->typelib_header.posguid = -1;
137 typelib->typelib_header.lcid = 0x0409;
138 typelib->typelib_header.lcid2 = 0x0;
139 typelib->typelib_header.varflags = 0x40;
140 typelib->typelib_header.version = 0;
141 typelib->typelib_header.flags = 0;
142 typelib->typelib_header.nrtypeinfos = 0;
143 typelib->typelib_header.helpstring = -1;
144 typelib->typelib_header.helpstringcontext = 0;
145 typelib->typelib_header.helpcontext = 0;
146 typelib->typelib_header.nametablecount = 0;
147 typelib->typelib_header.nametablechars = 0;
148 typelib->typelib_header.NameOffset = -1;
149 typelib->typelib_header.helpfile = -1;
150 typelib->typelib_header.CustomDataOffset = -1;
151 typelib->typelib_header.res44 = 0x20;
152 typelib->typelib_header.res48 = 0x80;
153 typelib->typelib_header.dispatchpos = -1;
154 typelib->typelib_header.nimpinfos = 0;
157 /****************************************************************************
158 * ctl2_init_segdir
160 * Initializes the segment directory of a new typelib.
162 static void ctl2_init_segdir(
163 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
165 int i;
166 MSFT_pSeg *segdir;
168 segdir = &typelib->typelib_segdir[MSFT_SEG_TYPEINFO];
170 for (i = 0; i < MSFT_SEG_MAX; i++) {
171 segdir[i].offset = -1;
172 segdir[i].length = 0;
173 segdir[i].res08 = -1;
174 segdir[i].res0c = 0x0f;
178 /****************************************************************************
179 * ctl2_hash_guid
181 * Generates a hash key from a GUID.
183 * RETURNS
185 * The hash key for the GUID.
187 static int ctl2_hash_guid(const struct uuid *guid)
189 int hash;
190 int i;
192 hash = 0;
193 for (i = 0; i < 8; i ++) {
194 hash ^= ((const short *)guid)[i];
197 return hash & 0x1f;
200 /****************************************************************************
201 * ctl2_find_guid
203 * Locates a guid in a type library.
205 * RETURNS
207 * The offset into the GUID segment of the guid, or -1 if not found.
209 static int ctl2_find_guid(
210 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
211 int hash_key, /* [I] The hash key for the guid. */
212 const struct uuid *guid) /* [I] The guid to find. */
214 int offset;
215 MSFT_GuidEntry *guidentry;
217 offset = typelib->typelib_guidhash_segment[hash_key];
218 while (offset != -1) {
219 guidentry = (MSFT_GuidEntry *)&typelib->typelib_segment_data[MSFT_SEG_GUID][offset];
221 if (!memcmp(guidentry, guid, sizeof(*guid))) return offset;
223 offset = guidentry->next_hash;
226 return offset;
229 /****************************************************************************
230 * ctl2_find_name
232 * Locates a name in a type library.
234 * RETURNS
236 * The offset into the NAME segment of the name, or -1 if not found.
238 * NOTES
240 * The name must be encoded as with ctl2_encode_name().
242 static int ctl2_find_name(
243 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
244 char *name) /* [I] The encoded name to find. */
246 int offset;
247 int *namestruct;
249 offset = typelib->typelib_namehash_segment[name[2] & 0x7f];
250 while (offset != -1) {
251 namestruct = (int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][offset];
253 if (!((namestruct[2] ^ *((int *)name)) & 0xffff00ff)) {
254 /* hash codes and lengths match, final test */
255 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
258 /* move to next item in hash bucket */
259 offset = namestruct[1];
262 return offset;
265 /****************************************************************************
266 * ctl2_encode_name
268 * Encodes a name string to a form suitable for storing into a type library
269 * or comparing to a name stored in a type library.
271 * RETURNS
273 * The length of the encoded name, including padding and length+hash fields.
275 * NOTES
277 * Will throw an exception if name or result are NULL. Is not multithread
278 * safe in the slightest.
280 static int ctl2_encode_name(
281 msft_typelib_t *typelib, /* [I] The typelib to operate against (used for LCID only). */
282 const char *name, /* [I] The name string to encode. */
283 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
285 char *converted_name;
286 size_t length, size;
287 int offset;
288 int value;
290 length = strlen(name);
291 size = (length + 7) & ~3;
292 converted_name = xmalloc(size + 1);
293 memcpy(converted_name + 4, name, length);
294 converted_name[length + 4] = 0;
296 value = lhash_val_of_name_sys(typelib->typelib_header.varflags & 0x0f, typelib->typelib_header.lcid, converted_name + 4);
298 converted_name[0] = length & 0xff;
299 converted_name[1] = length >> 8;
300 converted_name[2] = value;
301 converted_name[3] = value >> 8;
303 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
305 *result = converted_name;
307 return size;
310 /****************************************************************************
311 * ctl2_encode_string
313 * Encodes a string to a form suitable for storing into a type library or
314 * comparing to a string stored in a type library.
316 * RETURNS
318 * The length of the encoded string, including padding and length fields.
320 * NOTES
322 * Will throw an exception if string or result are NULL. Is not multithread
323 * safe in the slightest.
325 static int ctl2_encode_string(
326 const char *string, /* [I] The string to encode. */
327 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
329 char *converted_string;
330 size_t length, size;
331 int offset;
333 length = strlen(string);
334 size = (length + 5) & ~3;
335 if (length < 3) size += 4;
336 converted_string = xmalloc(size);
337 memcpy(converted_string + 2, string, length);
338 converted_string[0] = length & 0xff;
339 converted_string[1] = (length >> 8) & 0xff;
341 if(length < 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
342 for(offset = 0; offset < 4; offset++)
343 converted_string[length + offset + 2] = 0x57;
344 length += 4;
346 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
348 *result = converted_string;
349 return size;
352 /****************************************************************************
353 * ctl2_alloc_segment
355 * Allocates memory from a segment in a type library.
357 * RETURNS
359 * Success: The offset within the segment of the new data area.
361 * BUGS
363 * Does not (yet) handle the case where the allocated segment memory needs to grow.
365 static int ctl2_alloc_segment(
366 msft_typelib_t *typelib, /* [I] The type library in which to allocate. */
367 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
368 int size, /* [I] The amount to allocate. */
369 int block_size) /* [I] Initial allocation block size, or 0 for default. */
371 int offset;
373 if(!typelib->typelib_segment_data[segment]) {
374 if (!block_size) block_size = 0x2000;
376 typelib->typelib_segment_block_length[segment] = block_size;
377 typelib->typelib_segment_data[segment] = xmalloc(block_size);
378 if (!typelib->typelib_segment_data[segment]) return -1;
379 memset(typelib->typelib_segment_data[segment], 0x57, block_size);
382 while ((typelib->typelib_segdir[segment].length + size) > typelib->typelib_segment_block_length[segment]) {
383 unsigned char *block;
385 block_size = typelib->typelib_segment_block_length[segment];
386 block = xrealloc(typelib->typelib_segment_data[segment], block_size << 1);
388 if (segment == MSFT_SEG_TYPEINFO) {
389 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
390 msft_typeinfo_t *typeinfo;
392 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
393 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - typelib->typelib_segment_data[segment]];
397 memset(block + block_size, 0x57, block_size);
398 typelib->typelib_segment_block_length[segment] = block_size << 1;
399 typelib->typelib_segment_data[segment] = block;
402 offset = typelib->typelib_segdir[segment].length;
403 typelib->typelib_segdir[segment].length += size;
405 return offset;
408 /****************************************************************************
409 * ctl2_alloc_typeinfo
411 * Allocates and initializes a typeinfo structure in a type library.
413 * RETURNS
415 * Success: The offset of the new typeinfo.
416 * Failure: -1 (this is invariably an out of memory condition).
418 static int ctl2_alloc_typeinfo(
419 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
420 int nameoffset) /* [I] The offset of the name for this typeinfo. */
422 int offset;
423 MSFT_TypeInfoBase *typeinfo;
425 offset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
427 typelib->typelib_typeinfo_offsets[typelib->typelib_header.nrtypeinfos++] = offset;
429 typeinfo = (void *)(typelib->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
431 typeinfo->typekind = (typelib->typelib_header.nrtypeinfos - 1) << 16;
432 typeinfo->memoffset = -1; /* should be EOF if no elements */
433 typeinfo->res2 = 0;
434 typeinfo->res3 = -1;
435 typeinfo->res4 = 3;
436 typeinfo->res5 = 0;
437 typeinfo->cElement = 0;
438 typeinfo->res7 = 0;
439 typeinfo->res8 = 0;
440 typeinfo->res9 = 0;
441 typeinfo->resA = 0;
442 typeinfo->posguid = -1;
443 typeinfo->flags = 0;
444 typeinfo->NameOffset = nameoffset;
445 typeinfo->version = 0;
446 typeinfo->docstringoffs = -1;
447 typeinfo->helpstringcontext = 0;
448 typeinfo->helpcontext = 0;
449 typeinfo->oCustData = -1;
450 typeinfo->cbSizeVft = 0;
451 typeinfo->cImplTypes = 0;
452 typeinfo->size = 0;
453 typeinfo->datatype1 = -1;
454 typeinfo->datatype2 = 0;
455 typeinfo->res18 = 0;
456 typeinfo->res19 = -1;
458 return offset;
461 /****************************************************************************
462 * ctl2_alloc_guid
464 * Allocates and initializes a GUID structure in a type library. Also updates
465 * the GUID hash table as needed.
467 * RETURNS
469 * Success: The offset of the new GUID.
471 static int ctl2_alloc_guid(
472 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
473 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
475 int offset;
476 MSFT_GuidEntry *guid_space;
477 int hash_key;
479 chat("adding uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
480 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
481 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
482 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
484 hash_key = ctl2_hash_guid(&guid->guid);
486 offset = ctl2_find_guid(typelib, hash_key, &guid->guid);
487 if (offset != -1)
489 if (is_warning_enabled(2368))
490 warning("duplicate uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
491 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
492 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
493 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
494 return -1;
497 offset = ctl2_alloc_segment(typelib, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
499 guid_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_GUID] + offset);
500 *guid_space = *guid;
502 guid_space->next_hash = typelib->typelib_guidhash_segment[hash_key];
503 typelib->typelib_guidhash_segment[hash_key] = offset;
505 return offset;
508 /****************************************************************************
509 * ctl2_alloc_name
511 * Allocates and initializes a name within a type library. Also updates the
512 * name hash table as needed.
514 * RETURNS
516 * Success: The offset within the segment of the new name.
517 * Failure: -1 (this is invariably an out of memory condition).
519 static int ctl2_alloc_name(
520 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
521 const char *name) /* [I] The name to store. */
523 int length;
524 int offset;
525 MSFT_NameIntro *name_space;
526 char *encoded_name;
528 length = ctl2_encode_name(typelib, name, &encoded_name);
530 offset = ctl2_find_name(typelib, encoded_name);
531 if (offset != -1)
533 free(encoded_name);
534 return offset;
537 offset = ctl2_alloc_segment(typelib, MSFT_SEG_NAME, length + 8, 0);
539 name_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_NAME] + offset);
540 name_space->hreftype = -1;
541 name_space->next_hash = -1;
542 memcpy(&name_space->namelen, encoded_name, length);
544 if (typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
545 name_space->next_hash = typelib->typelib_namehash_segment[encoded_name[2] & 0x7f];
547 typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
549 typelib->typelib_header.nametablecount += 1;
550 typelib->typelib_header.nametablechars += *encoded_name;
552 free(encoded_name);
553 return offset;
556 /****************************************************************************
557 * ctl2_alloc_string
559 * Allocates and initializes a string in a type library.
561 * RETURNS
563 * Success: The offset within the segment of the new string.
564 * Failure: -1 (this is invariably an out of memory condition).
566 static int ctl2_alloc_string(
567 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
568 const char *string) /* [I] The string to store. */
570 int length;
571 int offset;
572 unsigned char *string_space;
573 char *encoded_string;
575 length = ctl2_encode_string(string, &encoded_string);
577 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_STRING].length;
578 offset += (((typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
579 typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
580 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
583 offset = ctl2_alloc_segment(typelib, MSFT_SEG_STRING, length, 0);
585 string_space = typelib->typelib_segment_data[MSFT_SEG_STRING] + offset;
586 memcpy(string_space, encoded_string, length);
587 free(encoded_string);
589 return offset;
592 /****************************************************************************
593 * alloc_msft_importinfo
595 * Allocates and initializes an import information structure in a type library.
597 * RETURNS
599 * Success: The offset of the new importinfo.
600 * Failure: -1 (this is invariably an out of memory condition).
602 static int alloc_msft_importinfo(
603 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
604 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
606 int offset;
607 MSFT_ImpInfo *impinfo_space;
609 for (offset = 0;
610 offset < typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
611 offset += sizeof(MSFT_ImpInfo)) {
612 if (!memcmp(&(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][offset]),
613 impinfo, sizeof(MSFT_ImpInfo))) {
614 return offset;
618 impinfo->flags |= typelib->typelib_header.nimpinfos++;
620 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
622 impinfo_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
623 *impinfo_space = *impinfo;
625 return offset;
628 /****************************************************************************
629 * alloc_importfile
631 * Allocates and initializes an import file definition in a type library.
633 * RETURNS
635 * Success: The offset of the new importinfo.
636 * Failure: -1 (this is invariably an out of memory condition).
638 static int alloc_importfile(
639 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
640 int guidoffset, /* [I] The offset to the GUID for the imported library. */
641 int major_version, /* [I] The major version number of the imported library. */
642 int minor_version, /* [I] The minor version number of the imported library. */
643 const char *filename) /* [I] The filename of the imported library. */
645 int length;
646 int offset;
647 MSFT_ImpFile *importfile;
648 char *encoded_string;
650 length = ctl2_encode_string(filename, &encoded_string);
652 encoded_string[0] <<= 2;
653 encoded_string[0] |= 1;
655 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
656 offset += (((typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
657 typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 0xc) {
658 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
661 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
663 importfile = (MSFT_ImpFile *)&typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
664 importfile->guid = guidoffset;
665 importfile->lcid = typelib->typelib_header.lcid2;
666 importfile->version = major_version | (minor_version << 16);
667 memcpy(&importfile->filename, encoded_string, length);
668 free(encoded_string);
670 return offset;
673 static void alloc_importinfo(msft_typelib_t *typelib, importinfo_t *importinfo)
675 importlib_t *importlib = importinfo->importlib;
677 chat("alloc_importinfo: %s\n", importinfo->name);
679 if(!importlib->allocated) {
680 MSFT_GuidEntry guid;
681 int guid_idx;
683 chat("allocating importlib %s\n", importlib->name);
685 importlib->allocated = -1;
687 memcpy(&guid.guid, &importlib->guid, sizeof(guid.guid));
688 guid.hreftype = 2;
690 guid_idx = ctl2_alloc_guid(typelib, &guid);
692 importlib->offset = alloc_importfile(typelib, guid_idx, importlib->version & 0xffff,
693 importlib->version >> 16, importlib->name);
696 if(importinfo->offset == -1 || !(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID)) {
697 MSFT_ImpInfo impinfo;
699 impinfo.flags = importinfo->flags;
700 impinfo.oImpFile = importlib->offset;
702 if(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID) {
703 MSFT_GuidEntry guid;
705 guid.hreftype = 0;
706 memcpy(&guid.guid, &importinfo->guid, sizeof(guid.guid));
708 impinfo.oGuid = ctl2_alloc_guid(typelib, &guid);
710 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
712 typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo.oGuid+sizeof(guid.guid)]
713 = importinfo->offset+1;
715 if(!strcmp(importinfo->name, "IDispatch"))
716 typelib->typelib_header.dispatchpos = importinfo->offset+1;
717 }else {
718 impinfo.oGuid = importinfo->id;
719 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
724 static importinfo_t *find_importinfo(msft_typelib_t *typelib, const char *name)
726 importlib_t *importlib;
727 int i;
729 chat("search importlib %s\n", name);
731 if(!name)
732 return NULL;
734 LIST_FOR_EACH_ENTRY( importlib, &typelib->typelib->importlibs, importlib_t, entry )
736 for(i=0; i < importlib->ntypeinfos; i++) {
737 if(!strcmp(name, importlib->importinfos[i].name)) {
738 chat("Found %s in importlib.\n", name);
739 return importlib->importinfos+i;
744 return NULL;
747 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure);
748 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface);
749 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration);
750 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion);
751 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls);
752 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
753 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
756 /****************************************************************************
757 * encode_type
759 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
760 * segments as needed.
762 * RETURNS
764 * Success: 0.
765 * Failure: -1.
767 static int encode_type(
768 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
769 int vt, /* [I] vt to encode */
770 type_t *type, /* [I] type */
771 int *encoded_type, /* [O] The encoded type description. */
772 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
774 int default_type;
775 int scratch;
776 int typeoffset;
777 int *typedata;
778 int target_type;
779 int child_size = 0;
781 chat("encode_type vt %d type %p\n", vt, type);
783 default_type = 0x80000000 | (vt << 16) | vt;
784 if (!decoded_size) decoded_size = &scratch;
786 *decoded_size = 0;
788 switch (vt) {
789 case VT_I1:
790 case VT_UI1:
791 *encoded_type = default_type;
792 break;
794 case VT_INT:
795 *encoded_type = 0x80000000 | (VT_I4 << 16) | VT_INT;
796 break;
798 case VT_UINT:
799 *encoded_type = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
800 break;
802 case VT_UI2:
803 case VT_I2:
804 case VT_BOOL:
805 *encoded_type = default_type;
806 break;
808 case VT_I4:
809 case VT_UI4:
810 case VT_R4:
811 case VT_ERROR:
812 case VT_HRESULT:
813 *encoded_type = default_type;
814 break;
816 case VT_R8:
817 case VT_I8:
818 case VT_UI8:
819 *encoded_type = default_type;
820 break;
822 case VT_CY:
823 case VT_DATE:
824 *encoded_type = default_type;
825 break;
827 case VT_DECIMAL:
828 *encoded_type = default_type;
829 break;
831 case VT_VOID:
832 *encoded_type = 0x80000000 | (VT_EMPTY << 16) | vt;
833 break;
835 case VT_UNKNOWN:
836 case VT_DISPATCH:
837 case VT_BSTR:
838 *encoded_type = default_type;
839 break;
841 case VT_VARIANT:
842 *encoded_type = default_type;
843 break;
845 case VT_LPSTR:
846 case VT_LPWSTR:
847 *encoded_type = 0xfffe0000 | vt;
848 break;
850 case VT_PTR:
852 int next_vt;
853 for(next_vt = 0; is_ptr(type); type = type_pointer_get_ref_type(type)) {
854 next_vt = get_type_vt(type_pointer_get_ref_type(type));
855 if (next_vt != 0)
856 break;
858 /* if no type found then it must be void */
859 if (next_vt == 0)
860 next_vt = VT_VOID;
862 encode_type(typelib, next_vt, type_pointer_get_ref_type(type),
863 &target_type, &child_size);
864 /* these types already have an implicit pointer, so we don't need to
865 * add another */
866 if(next_vt == VT_DISPATCH || next_vt == VT_UNKNOWN) {
867 chat("encode_type: skipping ptr\n");
868 *encoded_type = target_type;
869 *decoded_size = child_size;
870 break;
873 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
874 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
875 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
878 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
879 int mix_field;
881 if (target_type & 0x80000000) {
882 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
883 } else {
884 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
885 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
888 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
889 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
891 typedata[0] = (mix_field << 16) | VT_PTR;
892 typedata[1] = target_type;
895 *encoded_type = typeoffset;
897 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
898 break;
901 case VT_SAFEARRAY:
903 type_t *element_type = type_alias_get_aliasee_type(type_array_get_element_type(type));
904 int next_vt = get_type_vt(element_type);
906 encode_type(typelib, next_vt, type_alias_get_aliasee_type(type_array_get_element_type(type)),
907 &target_type, &child_size);
909 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
910 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
911 if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
914 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
915 int mix_field;
917 if (target_type & 0x80000000) {
918 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
919 } else {
920 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
921 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
924 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
925 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
927 typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
928 typedata[1] = target_type;
931 *encoded_type = typeoffset;
933 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
934 break;
937 case VT_USERDEFINED:
939 importinfo_t *importinfo;
940 int typeinfo_offset;
942 if (type->typelib_idx > -1)
944 chat("encode_type: VT_USERDEFINED - found already defined type %s at %d\n",
945 type->name, type->typelib_idx);
946 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
948 else if ((importinfo = find_importinfo(typelib, type->name)))
950 chat("encode_type: VT_USERDEFINED - found imported type %s in %s\n",
951 type->name, importinfo->importlib->name);
952 alloc_importinfo(typelib, importinfo);
953 typeinfo_offset = importinfo->offset | 0x1;
955 else
957 /* Typedefs without the [public] attribute aren't included in the
958 * typelib, unless the aliasee is an anonymous UDT or the typedef
959 * is wire-marshalled. In the latter case the wire-marshal type,
960 * which may be a non-public alias, is used instead. */
961 while (type_is_alias(type))
963 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
965 type = get_attrp(type->attrs, ATTR_WIREMARSHAL);
966 break;
968 else if (!is_attr(type->attrs, ATTR_PUBLIC))
969 type = type_alias_get_aliasee_type(type);
970 else
971 break;
974 chat("encode_type: VT_USERDEFINED - adding new type %s, real type %d\n",
975 type->name, type_get_type(type));
977 switch (type_get_type_detect_alias(type))
979 case TYPE_STRUCT:
980 case TYPE_ENCAPSULATED_UNION:
981 add_structure_typeinfo(typelib, type);
982 break;
983 case TYPE_INTERFACE:
984 add_interface_typeinfo(typelib, type);
985 break;
986 case TYPE_ENUM:
987 add_enum_typeinfo(typelib, type);
988 break;
989 case TYPE_UNION:
990 add_union_typeinfo(typelib, type);
991 break;
992 case TYPE_COCLASS:
993 add_coclass_typeinfo(typelib, type);
994 break;
995 case TYPE_ALIAS:
996 add_typedef_typeinfo(typelib, type);
997 break;
998 default:
999 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
1000 type_get_type(type));
1003 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
1005 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1006 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1007 if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == typeinfo_offset)) break;
1010 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1011 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1012 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1014 typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1015 typedata[1] = typeinfo_offset;
1018 *encoded_type = typeoffset;
1019 break;
1022 default:
1023 error("encode_type: unrecognized type %d.\n", vt);
1024 *encoded_type = default_type;
1025 break;
1028 return 0;
1031 static void dump_type(type_t *t)
1033 chat("dump_type: %p name %s type %d attrs %p\n", t, t->name, type_get_type(t), t->attrs);
1036 static int encode_var(
1037 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
1038 type_t *type, /* [I] The type description to encode. */
1039 var_t *var, /* [I] The var to encode. */
1040 int *encoded_type, /* [O] The encoded type description. */
1041 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1043 int typeoffset;
1044 int *typedata;
1045 int target_type;
1046 int child_size;
1047 int vt;
1048 int scratch;
1050 if (!decoded_size) decoded_size = &scratch;
1051 *decoded_size = 0;
1053 chat("encode_var: var %p type %p type->name %s\n",
1054 var, type, type->name ? type->name : "NULL");
1056 if (is_array(type) && !type_array_is_decl_as_ptr(type)) {
1057 int num_dims, arrayoffset;
1058 type_t *atype;
1059 int *arraydata;
1061 num_dims = 0;
1062 for (atype = type;
1063 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1064 atype = type_array_get_element_type(atype))
1065 ++num_dims;
1067 chat("array with %d dimensions\n", num_dims);
1068 encode_var(typelib, atype, var, &target_type, NULL);
1069 arrayoffset = ctl2_alloc_segment(typelib, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1070 arraydata = (void *)&typelib->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1072 arraydata[0] = target_type;
1073 arraydata[1] = num_dims;
1074 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1076 arraydata += 2;
1077 for (atype = type;
1078 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1079 atype = type_array_get_element_type(atype))
1081 arraydata[0] = type_array_get_dim(atype);
1082 arraydata[1] = 0;
1083 arraydata += 2;
1086 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1087 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1089 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1090 typedata[1] = arrayoffset;
1092 *encoded_type = typeoffset;
1093 *decoded_size = 20 /*sizeof(ARRAYDESC)*/ + (num_dims - 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1094 return 0;
1097 vt = get_type_vt(type);
1098 if (vt == VT_PTR) {
1099 type_t *ref = is_ptr(type) ?
1100 type_pointer_get_ref_type(type) : type_array_get_element_type(type);
1101 int skip_ptr = encode_var(typelib, ref, var, &target_type, &child_size);
1103 if(skip_ptr == 2) {
1104 chat("encode_var: skipping ptr\n");
1105 *encoded_type = target_type;
1106 *decoded_size = child_size;
1107 return 0;
1110 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1111 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1112 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1115 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1116 int mix_field;
1118 if (target_type & 0x80000000) {
1119 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1120 } else if (get_type_vt(ref) == VT_SAFEARRAY) {
1121 type_t *element_type = type_alias_get_aliasee_type(type_array_get_element_type(ref));
1122 mix_field = get_type_vt(element_type) | VT_ARRAY | VT_BYREF;
1123 } else {
1124 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1125 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1128 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1129 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1131 typedata[0] = (mix_field << 16) | VT_PTR;
1132 typedata[1] = target_type;
1135 *encoded_type = typeoffset;
1137 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1138 return 0;
1141 dump_type(type);
1143 encode_type(typelib, vt, type, encoded_type, decoded_size);
1144 /* these types already have an implicit pointer, so we don't need to
1145 * add another */
1146 if(vt == VT_DISPATCH || vt == VT_UNKNOWN) return 2;
1147 return 0;
1150 static unsigned int get_ulong_val(unsigned int val, int vt)
1152 switch(vt) {
1153 case VT_I2:
1154 case VT_BOOL:
1155 case VT_UI2:
1156 return val & 0xffff;
1157 case VT_I1:
1158 case VT_UI1:
1159 return val & 0xff;
1162 return val;
1165 static void write_int_value(msft_typelib_t *typelib, int *out, int vt, int value)
1167 const unsigned int lv = get_ulong_val(value, vt);
1168 if ((lv & 0x3ffffff) == lv) {
1169 *out = 0x80000000;
1170 *out |= vt << 26;
1171 *out |= lv;
1172 } else {
1173 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1174 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1175 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &value, 4);
1176 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1177 *out = offset;
1181 static void write_string_value(msft_typelib_t *typelib, int *out, const char *value)
1183 int len = strlen(value), seg_len = (len + 6 + 3) & ~0x3;
1184 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1185 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = VT_BSTR;
1186 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &len, sizeof(len));
1187 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1188 len += 6;
1189 while(len < seg_len) {
1190 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1191 len++;
1193 *out = offset;
1196 static void write_default_value(msft_typelib_t *typelib, type_t *type, expr_t *expr, int *out)
1198 int vt;
1200 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT) {
1201 vt = get_type_vt(type);
1202 if (vt != VT_BSTR && vt != VT_VARIANT)
1203 error("string default value applied to non-string type\n");
1204 chat("default value '%s'\n", expr->u.sval);
1205 write_string_value(typelib, out, expr->u.sval);
1206 return;
1209 if (type_get_type(type) == TYPE_ENUM) {
1210 vt = VT_I4;
1211 } else if (is_ptr(type)) {
1212 vt = get_type_vt(type_pointer_get_ref_type(type));
1213 if (vt == VT_USERDEFINED)
1214 vt = VT_I4;
1215 if (expr->cval)
1216 warning("non-null pointer default value\n");
1217 } else {
1218 vt = get_type_vt(type);
1219 switch(vt) {
1220 case VT_I2:
1221 case VT_I4:
1222 case VT_R4:
1223 case VT_BOOL:
1224 case VT_I1:
1225 case VT_UI1:
1226 case VT_UI2:
1227 case VT_UI4:
1228 case VT_INT:
1229 case VT_UINT:
1230 case VT_HRESULT:
1231 break;
1232 case VT_VARIANT: {
1233 switch (expr->type) {
1234 case EXPR_DOUBLE:
1235 vt = VT_R4;
1236 break;
1237 case EXPR_NUM:
1238 vt = VT_I4;
1239 break;
1240 default:
1241 warning("can't write default VT_VARIANT value for expression type %d.\n", expr->type);
1242 return;
1244 break;
1246 default:
1247 warning("can't write value of type %d yet\n", vt);
1248 return;
1252 write_int_value(typelib, out, vt, expr->cval);
1255 static void set_custdata(msft_typelib_t *typelib, const struct uuid *guid,
1256 int vt, const void *value, int *offset)
1258 int guidoffset;
1259 int custoffset;
1260 int *custdata;
1261 int data_out;
1262 int hash_key;
1264 hash_key = ctl2_hash_guid(guid);
1265 guidoffset = ctl2_find_guid(typelib, hash_key, guid);
1266 if(guidoffset == -1) {
1267 /* add GUID that was not already present */
1268 MSFT_GuidEntry guidentry;
1269 guidentry.guid = *guid;
1271 guidentry.hreftype = -1;
1272 guidentry.next_hash = -1;
1274 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1277 if(vt == VT_BSTR)
1278 /* TODO midl appears to share a single reference if the same string is used as custdata in multiple places */
1279 write_string_value(typelib, &data_out, value);
1280 else
1281 write_int_value(typelib, &data_out, vt, *(int*)value);
1283 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1285 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1286 custdata[0] = guidoffset;
1287 custdata[1] = data_out;
1288 custdata[2] = *offset;
1289 *offset = custoffset;
1292 static void set_custdata_attr(msft_typelib_t *typelib, attr_custdata_t *custdata, int *offset)
1294 switch(custdata->pval->type) {
1295 case EXPR_STRLIT:
1296 case EXPR_WSTRLIT:
1297 set_custdata(typelib, &custdata->id, VT_BSTR, custdata->pval->u.sval, offset);
1298 break;
1299 case EXPR_HEXNUM:
1300 case EXPR_NUM:
1301 set_custdata(typelib, &custdata->id, VT_I4, &custdata->pval->u.lval, offset);
1302 break;
1303 default:
1304 error("custom() attribute with unknown type\n");
1305 break;
1309 static int add_func_desc(msft_typeinfo_t* typeinfo, var_t *func, int index)
1311 int offset, name_offset;
1312 int *typedata, typedata_size;
1313 int i, id, next_idx;
1314 int decoded_size, extra_attr = 0;
1315 int num_params = 0, num_optional = 0, num_defaults = 0;
1316 int has_arg_custdata = 0;
1317 var_t *arg;
1318 unsigned char *namedata;
1319 const attr_t *attr;
1320 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1321 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1322 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1323 int func_custdata_offset = -1;
1324 int entry = -1, entry_is_ord = 0;
1325 int lcid_retval_count = 0;
1327 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1329 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1331 switch(typeinfo->typekind) {
1332 case TKIND_DISPATCH:
1333 funckind = 0x4; /* FUNC_DISPATCH */
1334 break;
1335 case TKIND_MODULE:
1336 funckind = 0x3; /* FUNC_STATIC */
1337 break;
1338 default:
1339 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1340 break;
1343 if (is_local( func->attrs )) {
1344 chat("add_func_desc: skipping local function\n");
1345 return FALSE;
1348 if (type_function_get_args(func->declspec.type))
1349 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1351 num_params++;
1352 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1353 if(attr->type == ATTR_DEFAULTVALUE)
1354 num_defaults++;
1355 else if(attr->type == ATTR_OPTIONAL)
1356 num_optional++;
1357 else if(attr->type == ATTR_CUSTOM)
1358 has_arg_custdata = 1;
1362 chat("add_func_desc: num of params %d\n", num_params);
1364 name_offset = ctl2_alloc_name(typeinfo->typelib, func->name);
1366 if (func->attrs) LIST_FOR_EACH_ENTRY( attr, func->attrs, const attr_t, entry ) {
1367 expr_t *expr = attr->u.pval;
1368 switch(attr->type) {
1369 case ATTR_BINDABLE:
1370 funcflags |= 0x4; /* FUNCFLAG_FBINDABLE */
1371 break;
1372 case ATTR_CUSTOM:
1373 set_custdata_attr(typeinfo->typelib, attr->u.pval, &func_custdata_offset);
1374 break;
1375 case ATTR_DEFAULTBIND:
1376 funcflags |= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1377 break;
1378 case ATTR_DEFAULTCOLLELEM:
1379 funcflags |= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1380 break;
1381 case ATTR_DISPLAYBIND:
1382 funcflags |= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1383 break;
1384 case ATTR_ENTRY:
1385 extra_attr = max(extra_attr, 3);
1386 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1387 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1388 else {
1389 entry = expr->cval;
1390 entry_is_ord = 1;
1392 break;
1393 case ATTR_HELPCONTEXT:
1394 extra_attr = max(extra_attr, 1);
1395 help_context = expr->u.lval;
1396 break;
1397 case ATTR_HELPSTRING:
1398 extra_attr = max(extra_attr, 2);
1399 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1400 break;
1401 case ATTR_HELPSTRINGCONTEXT:
1402 extra_attr = max(extra_attr, 6);
1403 help_string_context = expr->u.lval;
1404 break;
1405 case ATTR_HIDDEN:
1406 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1407 break;
1408 case ATTR_ID:
1409 id = expr->cval;
1410 break;
1411 case ATTR_IMMEDIATEBIND:
1412 funcflags |= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1413 break;
1414 case ATTR_NONBROWSABLE:
1415 funcflags |= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1416 break;
1417 case ATTR_OUT:
1418 break;
1419 case ATTR_DEFAULT_OVERLOAD:
1420 case ATTR_OVERLOAD:
1421 break;
1422 case ATTR_PROPGET:
1423 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1424 break;
1425 case ATTR_PROPPUT:
1426 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1427 break;
1428 case ATTR_PROPPUTREF:
1429 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1430 break;
1431 /* FIXME: FUNCFLAG_FREPLACEABLE */
1432 case ATTR_REQUESTEDIT:
1433 funcflags |= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1434 break;
1435 case ATTR_RESTRICTED:
1436 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1437 break;
1438 case ATTR_SOURCE:
1439 funcflags |= 0x2; /* FUNCFLAG_FSOURCE */
1440 break;
1441 case ATTR_UIDEFAULT:
1442 funcflags |= 0x200; /* FUNCFLAG_FUIDEFAULT */
1443 break;
1444 case ATTR_USESGETLASTERROR:
1445 funcflags |= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1446 break;
1447 case ATTR_VARARG:
1448 if (num_optional || num_defaults)
1449 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1450 else
1451 num_optional = -1;
1452 break;
1453 default:
1454 break;
1458 if(has_arg_custdata || func_custdata_offset != -1) {
1459 extra_attr = max(extra_attr, 7 + num_params);
1462 /* allocate type data space for us */
1463 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1465 if (!typeinfo->func_data) {
1466 typeinfo->func_data = xmalloc(0x100);
1467 typeinfo->func_data_allocated = 0x100;
1468 typeinfo->func_data[0] = 0;
1471 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1472 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1473 typeinfo->func_data[0] + typedata_size + sizeof(int));
1474 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1477 offset = typeinfo->func_data[0];
1478 typeinfo->func_data[0] += typedata_size;
1479 typedata = typeinfo->func_data + (offset >> 2) + 1;
1482 /* find func with the same name - if it exists use its id */
1483 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1484 if(name_offset == typeinfo->func_names[i]) {
1485 id = typeinfo->func_indices[i];
1486 break;
1490 /* find the first func with the same id and link via the hiword of typedata[4] */
1491 next_idx = index;
1492 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1493 if(id == typeinfo->func_indices[i]) {
1494 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1495 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1496 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1497 break;
1501 /* fill out the basic type information */
1502 typedata[0] = typedata_size | (index << 16);
1503 encode_var(typeinfo->typelib, type_function_get_rettype(func->declspec.type), func,
1504 &typedata[1], &decoded_size);
1505 typedata[2] = funcflags;
1506 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1507 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1508 if(has_arg_custdata || func_custdata_offset != -1) typedata[4] |= 0x0080;
1509 if(num_defaults) typedata[4] |= 0x1000;
1510 if(entry_is_ord) typedata[4] |= 0x2000;
1511 typedata[5] = (num_optional << 16) | num_params;
1513 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1514 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1515 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1516 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1518 switch(extra_attr) {
1519 default:
1520 if(extra_attr > 7 + num_params) warning("unknown number of optional attrs\n");
1521 /* typedata[13..+num_params] = arg_custdata_offset handled in below loop */
1522 case 7: typedata[12] = func_custdata_offset;
1523 case 6: typedata[11] = help_string_context;
1524 case 5: typedata[10] = -1;
1525 case 4: typedata[9] = -1;
1526 case 3: typedata[8] = entry;
1527 case 2: typedata[7] = help_string_offset;
1528 case 1: typedata[6] = help_context;
1529 case 0:
1530 break;
1533 if (type_function_get_args(func->declspec.type))
1535 i = 0;
1536 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1538 int paramflags = 0;
1539 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1540 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1541 int arg_custdata_offset = -1;
1543 if(defaultdata) *defaultdata = -1;
1545 encode_var(typeinfo->typelib, arg->declspec.type, arg, paramdata, &decoded_size);
1546 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1547 switch(attr->type) {
1548 case ATTR_CUSTOM:
1549 set_custdata_attr(typeinfo->typelib, attr->u.pval, &arg_custdata_offset);
1550 break;
1551 case ATTR_DEFAULTVALUE:
1553 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1554 write_default_value(typeinfo->typelib, arg->declspec.type, (expr_t *)attr->u.pval, defaultdata);
1555 break;
1557 case ATTR_IN:
1558 paramflags |= 0x01; /* PARAMFLAG_FIN */
1559 break;
1560 case ATTR_OPTIONAL:
1561 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1562 break;
1563 case ATTR_OUT:
1564 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1565 break;
1566 case ATTR_PARAMLCID:
1567 paramflags |= 0x04; /* PARAMFLAG_LCID */
1568 lcid_retval_count++;
1569 break;
1570 case ATTR_RETVAL:
1571 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1572 lcid_retval_count++;
1573 break;
1574 default:
1575 chat("unhandled param attr %d\n", attr->type);
1576 break;
1578 if(extra_attr > 7 + i) {
1579 typedata[13+i] = arg_custdata_offset;
1582 paramdata[1] = -1;
1583 paramdata[2] = paramflags;
1584 typedata[3] += decoded_size << 16;
1586 i++;
1590 if(lcid_retval_count == 1)
1591 typedata[4] |= 0x4000;
1592 else if(lcid_retval_count == 2)
1593 typedata[4] |= 0x8000;
1595 if(typeinfo->funcs_allocated == 0) {
1596 typeinfo->funcs_allocated = 10;
1597 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1598 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1599 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1601 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1602 typeinfo->funcs_allocated *= 2;
1603 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1604 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1605 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1608 /* update the index data */
1609 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1610 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1611 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1613 /* ??? */
1614 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1615 typeinfo->typeinfo->res2 <<= 1;
1616 /* ??? */
1617 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1619 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1620 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1621 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1623 /* adjust size of VTBL */
1624 if(funckind != 0x3 /* FUNC_STATIC */)
1625 typeinfo->typeinfo->cbSizeVft += pointer_size;
1627 /* Increment the number of function elements */
1628 typeinfo->typeinfo->cElement += 1;
1630 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1631 if (*((int *)namedata) == -1) {
1632 *((int *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1633 if(typeinfo->typekind == TKIND_MODULE)
1634 namedata[9] |= 0x10;
1635 } else
1636 namedata[9] &= ~0x10;
1638 if(typeinfo->typekind == TKIND_MODULE)
1639 namedata[9] |= 0x20;
1641 if (type_function_get_args(func->declspec.type))
1643 i = 0;
1644 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1646 /* don't give the last arg of a [propput*] func a name */
1647 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1649 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1650 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1651 paramdata[1] = offset;
1653 i++;
1656 return TRUE;
1659 static void add_var_desc(msft_typeinfo_t *typeinfo, unsigned int index, var_t* var)
1661 int offset, id;
1662 unsigned int typedata_size;
1663 int extra_attr = 0;
1664 int *typedata;
1665 unsigned int var_datawidth, var_alignment = 0;
1666 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1667 int alignment;
1668 int varflags = 0;
1669 const attr_t *attr;
1670 unsigned char *namedata;
1671 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1672 int var_custdata_offset = -1;
1674 if (!var->name)
1675 var->name = gen_name();
1677 chat("add_var_desc(%d, %s)\n", index, var->name);
1679 id = 0x40000000 + index;
1681 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1682 expr_t *expr = attr->u.pval;
1683 switch(attr->type) {
1684 case ATTR_BINDABLE:
1685 varflags |= 0x04; /* VARFLAG_FBINDABLE */
1686 break;
1687 case ATTR_CUSTOM:
1688 extra_attr = max(extra_attr,4);
1689 set_custdata_attr(typeinfo->typelib, attr->u.pval, &var_custdata_offset);
1690 break;
1691 case ATTR_DEFAULTBIND:
1692 varflags |= 0x20; /* VARFLAG_FDEFAULTBIND */
1693 break;
1694 case ATTR_DEFAULTCOLLELEM:
1695 varflags |= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1696 break;
1697 case ATTR_DISPLAYBIND:
1698 varflags |= 0x10; /* VARFLAG_FDISPLAYBIND */
1699 break;
1700 case ATTR_HIDDEN:
1701 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1702 break;
1703 case ATTR_ID:
1704 id = expr->cval;
1705 break;
1706 case ATTR_IMMEDIATEBIND:
1707 varflags |= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1708 break;
1709 case ATTR_NONBROWSABLE:
1710 varflags |= 0x400; /* VARFLAG_FNONBROWSABLE */
1711 break;
1712 case ATTR_READONLY:
1713 varflags |= 0x01; /* VARFLAG_FREADONLY */
1714 break;
1715 /* FIXME: VARFLAG_FREPLACEABLE */
1716 case ATTR_REQUESTEDIT:
1717 varflags |= 0x08; /* VARFLAG_FREQUESTEDIT */
1718 break;
1719 case ATTR_RESTRICTED:
1720 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1721 break;
1722 case ATTR_SOURCE:
1723 varflags |= 0x02; /* VARFLAG_FSOURCE */
1724 break;
1725 case ATTR_UIDEFAULT:
1726 varflags |= 0x0200; /* VARFLAG_FUIDEFAULT */
1727 break;
1728 default:
1729 break;
1733 /* allocate type data space for us */
1734 typedata_size = 0x14 + extra_attr * sizeof(int);
1736 if (!typeinfo->var_data) {
1737 typeinfo->var_data = xmalloc(0x100);
1738 typeinfo->var_data_allocated = 0x100;
1739 typeinfo->var_data[0] = 0;
1742 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1743 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1744 typeinfo->var_data[0] + typedata_size + sizeof(int));
1745 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1748 offset = typeinfo->var_data[0];
1749 typeinfo->var_data[0] += typedata_size;
1750 typedata = typeinfo->var_data + (offset >> 2) + 1;
1752 /* fill out the basic type information */
1753 typedata[0] = typedata_size | (index << 16);
1754 typedata[2] = varflags;
1755 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1757 if(typeinfo->vars_allocated == 0) {
1758 typeinfo->vars_allocated = 10;
1759 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1760 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1761 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1763 if(typeinfo->vars_allocated == var_num) {
1764 typeinfo->vars_allocated *= 2;
1765 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1766 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1767 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1769 /* update the index data */
1770 typeinfo->var_indices[var_num] = id;
1771 typeinfo->var_names[var_num] = -1;
1772 typeinfo->var_offsets[var_num] = offset;
1774 /* figure out type widths and whatnot */
1775 var_datawidth = type_memsize_and_alignment(var->declspec.type, &var_alignment);
1776 encode_var(typeinfo->typelib, var->declspec.type, var, &typedata[1], &var_type_size);
1778 /* pad out starting position to data width */
1779 typeinfo->datawidth += var_alignment - 1;
1780 typeinfo->datawidth &= ~(var_alignment - 1);
1782 switch(typeinfo->typekind) {
1783 case TKIND_ENUM:
1784 write_int_value(typeinfo->typelib, &typedata[4], VT_I4, var->eval->cval);
1785 var_kind = 2; /* VAR_CONST */
1786 var_type_size += 16; /* sizeof(VARIANT) */
1787 typeinfo->datawidth = var_datawidth;
1788 break;
1789 case TKIND_RECORD:
1790 typedata[4] = typeinfo->datawidth;
1791 typeinfo->datawidth += var_datawidth;
1792 break;
1793 case TKIND_UNION:
1794 typedata[4] = 0;
1795 typeinfo->datawidth = max(typeinfo->datawidth, var_datawidth);
1796 break;
1797 case TKIND_DISPATCH:
1798 var_kind = 3; /* VAR_DISPATCH */
1799 typedata[4] = 0;
1800 typeinfo->datawidth = pointer_size;
1801 break;
1802 default:
1803 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1804 break;
1807 /* add type description size to total required allocation */
1808 typedata[3] += var_type_size << 16 | var_kind;
1810 switch(extra_attr) {
1811 case 5: typedata[9] = -1 /*help_string_context*/;
1812 case 4: typedata[8] = var_custdata_offset;
1813 case 3: typedata[7] = -1;
1814 case 2: typedata[6] = -1 /*help_string_offset*/;
1815 case 1: typedata[5] = -1 /*help_context*/;
1816 case 0:
1817 break;
1818 default:
1819 warning("unknown number of optional attrs\n");
1822 /* fix type alignment */
1823 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1824 if (alignment < var_alignment) {
1825 alignment = var_alignment;
1826 typeinfo->typeinfo->typekind &= ~0xffc0;
1827 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1830 /* ??? */
1831 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1832 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1833 typeinfo->typeinfo->res2 <<= 1;
1836 /* ??? */
1837 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1838 typeinfo->typeinfo->res3 += 0x2c;
1840 /* increment the number of variable elements */
1841 typeinfo->typeinfo->cElement += 0x10000;
1843 /* pad data width to alignment */
1844 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1846 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1848 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1849 if (*((int *)namedata) == -1) {
1850 *((int *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1851 if(typeinfo->typekind != TKIND_DISPATCH)
1852 namedata[9] |= 0x10;
1853 } else
1854 namedata[9] &= ~0x10;
1856 if (typeinfo->typekind == TKIND_ENUM) {
1857 namedata[9] |= 0x20;
1859 typeinfo->var_names[var_num] = offset;
1862 static void add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1864 if(importinfo) {
1865 alloc_importinfo(typeinfo->typelib, importinfo);
1866 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1867 }else {
1868 if(ref->typelib_idx == -1)
1869 add_interface_typeinfo(typeinfo->typelib, ref);
1870 if(ref->typelib_idx == -1)
1871 error("add_impl_type: unable to add inherited interface\n");
1873 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1876 typeinfo->typeinfo->cImplTypes++;
1879 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1880 const char *name, const attr_list_t *attrs)
1882 const attr_t *attr;
1883 msft_typeinfo_t *msft_typeinfo;
1884 int nameoffset;
1885 int typeinfo_offset;
1886 MSFT_TypeInfoBase *typeinfo;
1887 MSFT_GuidEntry guidentry;
1889 chat("create_msft_typeinfo: name %s kind %d index %d\n", name, kind, typelib->typelib_header.nrtypeinfos);
1891 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1892 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1894 msft_typeinfo->typelib = typelib;
1896 nameoffset = ctl2_alloc_name(typelib, name);
1897 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1898 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1900 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1901 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1903 msft_typeinfo->typekind = kind;
1904 msft_typeinfo->typeinfo = typeinfo;
1906 typeinfo->typekind |= kind | 0x20;
1908 if(kind == TKIND_COCLASS)
1909 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1911 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1912 switch(attr->type) {
1913 case ATTR_AGGREGATABLE:
1914 if (kind == TKIND_COCLASS)
1915 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1916 break;
1918 case ATTR_APPOBJECT:
1919 if (kind == TKIND_COCLASS)
1920 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1921 break;
1923 case ATTR_CONTROL:
1924 if (kind == TKIND_COCLASS)
1925 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1926 break;
1927 case ATTR_CUSTOM:
1928 set_custdata_attr(typelib, attr->u.pval, &typeinfo->oCustData);
1929 break;
1930 case ATTR_DLLNAME:
1932 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1933 typeinfo->datatype1 = offset;
1934 break;
1937 case ATTR_DUAL:
1938 /* FIXME: check interface is compatible */
1939 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1940 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1941 break;
1943 case ATTR_HELPCONTEXT:
1945 expr_t *expr = (expr_t*)attr->u.pval;
1946 typeinfo->helpcontext = expr->cval;
1947 break;
1949 case ATTR_HELPSTRING:
1951 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1952 if (offset == -1) break;
1953 typeinfo->docstringoffs = offset;
1954 break;
1956 case ATTR_HELPSTRINGCONTEXT:
1958 expr_t *expr = (expr_t*)attr->u.pval;
1959 typeinfo->helpstringcontext = expr->cval;
1960 break;
1962 case ATTR_HIDDEN:
1963 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1964 break;
1966 case ATTR_LICENSED:
1967 typeinfo->flags |= 0x04; /* TYPEFLAG_FLICENSED */
1968 break;
1970 case ATTR_NONCREATABLE:
1971 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1972 break;
1974 case ATTR_NONEXTENSIBLE:
1975 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1976 break;
1978 case ATTR_OLEAUTOMATION:
1979 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1980 break;
1982 /* FIXME: TYPEFLAG_FPREDCLID */
1984 case ATTR_PROXY:
1985 typeinfo->flags |= 0x4000; /* TYPEFLAG_FPROXY */
1986 break;
1988 /* FIXME: TYPEFLAG_FREPLACEABLE */
1990 case ATTR_RESTRICTED:
1991 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
1992 break;
1994 case ATTR_UUID:
1995 guidentry.guid = *(struct uuid *)attr->u.pval;
1996 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1997 guidentry.next_hash = -1;
1998 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
1999 #if 0
2000 if (IsEqualIID(guid, &IID_IDispatch)) {
2001 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
2003 #endif
2004 break;
2006 case ATTR_VERSION:
2007 typeinfo->version = attr->u.ival;
2008 break;
2010 default:
2011 break;
2015 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
2016 typelib->last_typeinfo = msft_typeinfo;
2017 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
2020 return msft_typeinfo;
2023 static void add_dispatch(msft_typelib_t *typelib)
2025 int guid_offset, impfile_offset, hash_key;
2026 MSFT_GuidEntry guidentry;
2027 MSFT_ImpInfo impinfo;
2028 static const struct uuid stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2029 static const struct uuid iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2031 if(typelib->typelib_header.dispatchpos != -1) return;
2033 guidentry.guid = stdole;
2034 guidentry.hreftype = 2;
2035 guidentry.next_hash = -1;
2036 hash_key = ctl2_hash_guid(&guidentry.guid);
2037 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2038 if (guid_offset == -1)
2039 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2040 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
2042 guidentry.guid = iid_idispatch;
2043 guidentry.hreftype = 1;
2044 guidentry.next_hash = -1;
2045 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
2046 impinfo.oImpFile = impfile_offset;
2047 hash_key = ctl2_hash_guid(&guidentry.guid);
2048 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2049 if (guid_offset == -1)
2050 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2051 impinfo.oGuid = guid_offset;
2052 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
2055 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
2057 int num_parents = 0, num_funcs = 0;
2058 importinfo_t *importinfo = NULL;
2059 const statement_t *stmt_func;
2060 type_t *inherit, *ref;
2061 int idx = 0;
2062 var_t *func;
2063 var_t *var;
2064 msft_typeinfo_t *msft_typeinfo;
2066 if (-1 < dispinterface->typelib_idx)
2067 return;
2069 inherit = type_dispiface_get_inherit(dispinterface);
2071 if (inherit)
2073 importinfo = find_importinfo(typelib, inherit->name);
2075 if (!importinfo && type_iface_get_inherit(inherit) && inherit->typelib_idx == -1)
2076 add_interface_typeinfo(typelib, inherit);
2079 /* check typelib_idx again, it could have been added while resolving the parent interface */
2080 if (-1 < dispinterface->typelib_idx)
2081 return;
2083 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2084 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
2085 dispinterface->attrs);
2087 msft_typeinfo->typeinfo->size = pointer_size;
2088 msft_typeinfo->typeinfo->typekind |= pointer_size << 11 | pointer_size << 6;
2090 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2091 add_dispatch(typelib);
2093 if (inherit)
2095 add_impl_type(msft_typeinfo, inherit, importinfo);
2096 msft_typeinfo->typeinfo->typekind |= 0x10;
2099 /* count the number of inherited interfaces and non-local functions */
2100 for (ref = inherit; ref; ref = type_iface_get_inherit(ref))
2102 num_parents++;
2103 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) )
2105 var_t *func = stmt_func->u.var;
2106 if (!is_local(func->attrs)) num_funcs++;
2109 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2110 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2112 msft_typeinfo->typeinfo->cImplTypes = 1; /* IDispatch */
2114 /* count the no of methods, as the variable indices come after the funcs */
2115 if (dispinterface->details.iface->disp_methods)
2116 LIST_FOR_EACH_ENTRY( func, dispinterface->details.iface->disp_methods, var_t, entry )
2117 idx++;
2119 if (type_dispiface_get_props(dispinterface))
2120 LIST_FOR_EACH_ENTRY( var, type_dispiface_get_props(dispinterface), var_t, entry )
2121 add_var_desc(msft_typeinfo, idx++, var);
2123 if (type_dispiface_get_methods(dispinterface))
2125 idx = 0;
2126 LIST_FOR_EACH_ENTRY( func, type_dispiface_get_methods(dispinterface), var_t, entry )
2127 if(add_func_desc(msft_typeinfo, func, idx))
2128 idx++;
2131 typelib->typelib->reg_ifaces = xrealloc(typelib->typelib->reg_ifaces,
2132 (typelib->typelib->reg_iface_count + 1) * sizeof(dispinterface));
2133 typelib->typelib->reg_ifaces[typelib->typelib->reg_iface_count++] = dispinterface;
2136 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
2138 int idx = 0;
2139 const statement_t *stmt_func;
2140 type_t *ref;
2141 msft_typeinfo_t *msft_typeinfo;
2142 importinfo_t *ref_importinfo = NULL;
2143 int num_parents = 0, num_funcs = 0;
2144 type_t *inherit;
2145 const type_t *derived;
2147 if (-1 < interface->typelib_idx)
2148 return;
2150 if (!interface->details.iface)
2152 error( "interface %s is referenced but not defined\n", interface->name );
2153 return;
2156 if (is_attr(interface->attrs, ATTR_DISPINTERFACE)) {
2157 add_dispinterface_typeinfo(typelib, interface);
2158 return;
2161 /* midl adds the parent interface first, unless the parent itself
2162 has no parent (i.e. it stops before IUnknown). */
2164 inherit = type_iface_get_inherit(interface);
2166 if(inherit) {
2167 ref_importinfo = find_importinfo(typelib, inherit->name);
2169 if(!ref_importinfo && type_iface_get_inherit(inherit) &&
2170 inherit->typelib_idx == -1)
2171 add_interface_typeinfo(typelib, inherit);
2174 /* check typelib_idx again, it could have been added while resolving the parent interface */
2175 if (-1 < interface->typelib_idx)
2176 return;
2178 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2179 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
2180 msft_typeinfo->typeinfo->size = pointer_size;
2181 msft_typeinfo->typeinfo->typekind |= 0x0200;
2182 msft_typeinfo->typeinfo->typekind |= pointer_size << 11;
2184 for (derived = inherit; derived; derived = type_iface_get_inherit(derived))
2185 if (derived->name && !strcmp(derived->name, "IDispatch"))
2186 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2188 if(type_iface_get_inherit(interface))
2189 add_impl_type(msft_typeinfo, type_iface_get_inherit(interface),
2190 ref_importinfo);
2192 /* count the number of inherited interfaces and non-local functions */
2193 for(ref = inherit; ref; ref = type_iface_get_inherit(ref)) {
2194 num_parents++;
2195 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) ) {
2196 var_t *func = stmt_func->u.var;
2197 if (!is_local(func->attrs)) num_funcs++;
2200 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2201 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2203 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(interface) ) {
2204 var_t *func = stmt_func->u.var;
2205 if(add_func_desc(msft_typeinfo, func, idx))
2206 idx++;
2209 if (is_attr(interface->attrs, ATTR_OLEAUTOMATION) || is_attr(interface->attrs, ATTR_DUAL))
2211 typelib->typelib->reg_ifaces = xrealloc(typelib->typelib->reg_ifaces,
2212 (typelib->typelib->reg_iface_count + 1) * sizeof(interface));
2213 typelib->typelib->reg_ifaces[typelib->typelib->reg_iface_count++] = interface;
2217 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2219 var_list_t *fields;
2220 int idx = 0;
2221 var_t *cur;
2222 msft_typeinfo_t *msft_typeinfo;
2224 if (-1 < structure->typelib_idx)
2225 return;
2227 if (!structure->name)
2228 structure->name = gen_name();
2230 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2231 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2232 msft_typeinfo->typeinfo->size = 0;
2234 if (type_get_type(structure) == TYPE_STRUCT)
2235 fields = type_struct_get_fields(structure);
2236 else
2237 fields = type_encapsulated_union_get_fields(structure);
2239 if (fields)
2241 LIST_FOR_EACH_ENTRY( cur, fields, var_t, entry )
2242 add_var_desc(msft_typeinfo, idx++, cur);
2246 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2248 int idx = 0;
2249 var_t *cur;
2250 msft_typeinfo_t *msft_typeinfo;
2252 if (-1 < enumeration->typelib_idx)
2253 return;
2255 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2256 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2257 msft_typeinfo->typeinfo->size = 0;
2259 if (type_enum_get_values(enumeration))
2260 LIST_FOR_EACH_ENTRY( cur, type_enum_get_values(enumeration), var_t, entry )
2261 add_var_desc(msft_typeinfo, idx++, cur);
2264 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion)
2266 int idx = 0;
2267 var_t *cur;
2268 msft_typeinfo_t *msft_typeinfo;
2270 if (-1 < tunion->typelib_idx)
2271 return;
2273 if (!tunion->name)
2274 tunion->name = gen_name();
2276 tunion->typelib_idx = typelib->typelib_header.nrtypeinfos;
2277 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_UNION, tunion->name, tunion->attrs);
2278 msft_typeinfo->typeinfo->size = 0;
2280 if (type_union_get_cases(tunion))
2281 LIST_FOR_EACH_ENTRY(cur, type_union_get_cases(tunion), var_t, entry)
2282 add_var_desc(msft_typeinfo, idx++, cur);
2285 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2287 msft_typeinfo_t *msft_typeinfo = NULL;
2288 int datatype1, datatype2, duplicate = 0;
2289 unsigned int size, alignment = 0;
2290 type_t *type;
2292 if (-1 < tdef->typelib_idx)
2293 return;
2295 type = type_alias_get_aliasee_type(tdef);
2297 if (!type->name || strcmp(tdef->name, type->name) != 0)
2299 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2300 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2302 else
2303 duplicate = 1;
2305 encode_type(typelib, get_type_vt(type), type, &datatype1, &datatype2);
2306 size = type_memsize_and_alignment(type, &alignment);
2308 if (msft_typeinfo)
2310 msft_typeinfo->typeinfo->datatype1 = datatype1;
2311 msft_typeinfo->typeinfo->size = size;
2312 msft_typeinfo->typeinfo->datatype2 = datatype2;
2313 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2316 /* avoid adding duplicate type definitions */
2317 if (duplicate)
2318 tdef->typelib_idx = type->typelib_idx;
2321 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2323 msft_typeinfo_t *msft_typeinfo;
2324 typeref_t *iref;
2325 int num_ifaces = 0, offset, i;
2326 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2327 int have_default = 0, have_default_source = 0;
2328 const attr_t *attr;
2329 typeref_list_t *ifaces;
2331 if (-1 < cls->typelib_idx)
2332 return;
2334 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2335 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2337 ifaces = type_coclass_get_ifaces(cls);
2338 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, typeref_t, entry ) num_ifaces++;
2340 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2341 num_ifaces * sizeof(*ref), 0);
2343 i = 0;
2344 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, typeref_t, entry ) {
2345 if(iref->type->typelib_idx == -1)
2346 add_interface_typeinfo(typelib, iref->type);
2347 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2348 ref->reftype = typelib->typelib_typeinfo_offsets[iref->type->typelib_idx];
2349 ref->flags = 0;
2350 ref->oCustData = -1;
2351 ref->onext = -1;
2352 if(i < num_ifaces - 1)
2353 ref->onext = offset + (i + 1) * sizeof(*ref);
2355 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2356 switch(attr->type) {
2357 case ATTR_DEFAULT:
2358 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2359 break;
2360 case ATTR_DEFAULTVTABLE:
2361 ref->flags |= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2362 break;
2363 case ATTR_RESTRICTED:
2364 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2365 break;
2366 case ATTR_SOURCE:
2367 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2368 break;
2369 default:
2370 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2373 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2374 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2375 have_default_source = 1;
2376 else
2377 have_default = 1;
2380 /* If the interface is non-restricted and we haven't already had one then
2381 remember it so that we can use it as a default later */
2382 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2383 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2384 if(!first_source)
2385 first_source = ref;
2387 else if(!first)
2388 first = ref;
2390 i++;
2393 /* If we haven't had a default interface, then set the default flags on the
2394 first ones */
2395 if(!have_default && first)
2396 first->flags |= 0x1;
2397 if(!have_default_source && first_source)
2398 first_source->flags |= 0x1;
2400 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2401 msft_typeinfo->typeinfo->size = pointer_size;
2402 msft_typeinfo->typeinfo->typekind |= 0x2200;
2405 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2407 int idx = 0;
2408 const statement_t *stmt;
2409 msft_typeinfo_t *msft_typeinfo;
2411 if (-1 < module->typelib_idx)
2412 return;
2414 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2415 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2416 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2418 STATEMENTS_FOR_EACH_FUNC( stmt, module->details.module->stmts ) {
2419 var_t *func = stmt->u.var;
2420 if(add_func_desc(msft_typeinfo, func, idx))
2421 idx++;
2424 msft_typeinfo->typeinfo->size = idx;
2427 static void add_type_typeinfo(msft_typelib_t *typelib, type_t *type)
2429 switch (type_get_type(type)) {
2430 case TYPE_INTERFACE:
2431 add_interface_typeinfo(typelib, type);
2432 break;
2433 case TYPE_STRUCT:
2434 case TYPE_ENCAPSULATED_UNION:
2435 add_structure_typeinfo(typelib, type);
2436 break;
2437 case TYPE_ENUM:
2438 add_enum_typeinfo(typelib, type);
2439 break;
2440 case TYPE_UNION:
2441 add_union_typeinfo(typelib, type);
2442 break;
2443 case TYPE_COCLASS:
2444 add_coclass_typeinfo(typelib, type);
2445 break;
2446 case TYPE_BASIC:
2447 case TYPE_POINTER:
2448 case TYPE_ARRAY:
2449 break;
2450 default:
2451 error("add_entry: unhandled type 0x%x for %s\n",
2452 type_get_type(type), type->name);
2453 break;
2457 static void add_entry(msft_typelib_t *typelib, const statement_t *stmt)
2459 switch(stmt->type) {
2460 case STMT_LIBRARY:
2461 case STMT_IMPORT:
2462 case STMT_PRAGMA:
2463 case STMT_CPPQUOTE:
2464 case STMT_DECLARATION:
2465 /* not included in typelib */
2466 break;
2467 case STMT_IMPORTLIB:
2468 /* not processed here */
2469 break;
2470 case STMT_TYPEDEF:
2472 typeref_t *ref;
2473 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry) {
2474 /* if the type is public then add the typedef, otherwise attempt
2475 * to add the aliased type */
2476 if (is_attr(ref->type->attrs, ATTR_PUBLIC))
2477 add_typedef_typeinfo(typelib, ref->type);
2478 else
2479 add_type_typeinfo(typelib, type_alias_get_aliasee_type(ref->type));
2481 break;
2483 case STMT_MODULE:
2484 add_module_typeinfo(typelib, stmt->u.type);
2485 break;
2486 case STMT_TYPE:
2487 case STMT_TYPEREF:
2489 type_t *type = stmt->u.type;
2490 add_type_typeinfo(typelib, type);
2491 break;
2496 static void set_name(msft_typelib_t *typelib)
2498 int offset;
2500 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2501 if (offset == -1) return;
2502 typelib->typelib_header.NameOffset = offset;
2503 return;
2506 static void set_version(msft_typelib_t *typelib)
2508 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2511 static void set_guid(msft_typelib_t *typelib)
2513 MSFT_GuidEntry guidentry = { {0}, -2, -1 };
2514 int offset;
2515 struct uuid *ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2517 if (ptr) guidentry.guid = *ptr;
2519 offset = ctl2_alloc_guid(typelib, &guidentry);
2520 typelib->typelib_header.posguid = offset;
2522 return;
2525 static void set_doc_string(msft_typelib_t *typelib)
2527 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2529 if (str)
2531 int offset = ctl2_alloc_string(typelib, str);
2532 if (offset != -1) typelib->typelib_header.helpstring = offset;
2536 static void set_help_file_name(msft_typelib_t *typelib)
2538 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2540 if (str)
2542 int offset = ctl2_alloc_string(typelib, str);
2543 if (offset != -1)
2545 typelib->typelib_header.helpfile = offset;
2546 typelib->typelib_header.varflags |= 0x10;
2551 static void set_help_context(msft_typelib_t *typelib)
2553 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2554 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2557 static void set_help_string_dll(msft_typelib_t *typelib)
2559 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2561 if (str)
2563 int offset = ctl2_alloc_string(typelib, str);
2564 if (offset != -1)
2566 typelib->help_string_dll_offset = offset;
2567 typelib->typelib_header.varflags |= 0x100;
2572 static void set_help_string_context(msft_typelib_t *typelib)
2574 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2575 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2578 static void set_lcid(msft_typelib_t *typelib)
2580 const expr_t *lcid_expr = get_attrp( typelib->typelib->attrs, ATTR_LIBLCID );
2581 if(lcid_expr)
2583 typelib->typelib_header.lcid = lcid_expr->cval;
2584 typelib->typelib_header.lcid2 = lcid_expr->cval;
2588 static void set_lib_flags(msft_typelib_t *typelib)
2590 const attr_t *attr;
2592 typelib->typelib_header.flags = 0;
2593 if (!typelib->typelib->attrs) return;
2594 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2596 switch(attr->type) {
2597 case ATTR_CONTROL:
2598 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2599 break;
2600 case ATTR_HIDDEN:
2601 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2602 break;
2603 case ATTR_RESTRICTED:
2604 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2605 break;
2606 default:
2607 break;
2610 return;
2613 static void ctl2_write_segment(msft_typelib_t *typelib, int segment)
2615 if (typelib->typelib_segment_data[segment])
2616 put_data(typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length);
2619 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2621 msft_typeinfo_t *typeinfo;
2623 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2624 typeinfo->typeinfo->memoffset = filesize;
2625 if (typeinfo->func_data)
2626 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2627 if (typeinfo->var_data)
2628 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2629 if (typeinfo->func_data || typeinfo->var_data)
2630 filesize += 4;
2634 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2636 if (typelib->typelib_segdir[segment].length) {
2637 typelib->typelib_segdir[segment].offset = filepos;
2638 } else {
2639 typelib->typelib_segdir[segment].offset = -1;
2642 return typelib->typelib_segdir[segment].length;
2646 static void ctl2_write_typeinfos(msft_typelib_t *typelib)
2648 msft_typeinfo_t *typeinfo;
2649 int typedata_size;
2651 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2652 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2653 typedata_size = 0;
2654 if (typeinfo->func_data)
2655 typedata_size = typeinfo->func_data[0];
2656 if (typeinfo->var_data)
2657 typedata_size += typeinfo->var_data[0];
2658 put_data(&typedata_size, sizeof(int));
2659 if (typeinfo->func_data)
2660 put_data(typeinfo->func_data + 1, typeinfo->func_data[0]);
2661 if (typeinfo->var_data)
2662 put_data(typeinfo->var_data + 1, typeinfo->var_data[0]);
2663 if (typeinfo->func_indices)
2664 put_data(typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2665 if (typeinfo->var_indices)
2666 put_data(typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2667 if (typeinfo->func_names)
2668 put_data(typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2669 if (typeinfo->var_names)
2670 put_data(typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2671 if (typeinfo->func_offsets)
2672 put_data(typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2673 if (typeinfo->var_offsets) {
2674 int add = 0, i, offset;
2675 if(typeinfo->func_data)
2676 add = typeinfo->func_data[0];
2677 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2678 offset = typeinfo->var_offsets[i];
2679 offset += add;
2680 put_data(&offset, 4);
2686 static void save_all_changes(msft_typelib_t *typelib)
2688 int filepos;
2690 chat("save_all_changes(%p)\n", typelib);
2692 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2693 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2694 filepos += typelib->typelib_header.nrtypeinfos * 4;
2696 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2697 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2698 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2699 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2700 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2701 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2702 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2703 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2704 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2705 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2706 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2707 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2708 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2710 ctl2_finalize_typeinfos(typelib, filepos);
2712 init_output_buffer();
2714 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2715 if(typelib->typelib_header.varflags & 0x100)
2716 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2718 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2719 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2720 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2721 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2722 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2723 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2724 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2725 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2726 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2727 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2728 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2729 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2730 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2731 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2732 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2734 ctl2_write_typeinfos(typelib);
2736 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2738 char typelib_id[13] = "#1";
2740 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2741 if (expr)
2742 sprintf( typelib_id, "#%d", expr->cval );
2743 add_output_to_resources( "TYPELIB", typelib_id );
2744 if (strendswith( typelib_name, "_t.res" )) /* add typelib registration */
2745 output_typelib_regscript( typelib->typelib );
2747 else flush_output_buffer( typelib_name );
2750 int create_msft_typelib(typelib_t *typelib)
2752 msft_typelib_t *msft;
2753 int failed = 0;
2754 const statement_t *stmt;
2755 const attr_t *attr;
2756 time_t cur_time;
2757 char *time_override;
2758 unsigned int version = 7 << 24 | 555; /* 7.00.0555 */
2759 static const struct uuid midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2760 static const struct uuid midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2761 static const struct uuid midl_info_guid = {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2762 char info_string[128];
2764 msft = xmalloc(sizeof(*msft));
2765 memset(msft, 0, sizeof(*msft));
2766 msft->typelib = typelib;
2768 ctl2_init_header(msft);
2769 ctl2_init_segdir(msft);
2771 msft->typelib_header.varflags |= (pointer_size == 8) ? SYS_WIN64 : SYS_WIN32;
2774 * The following two calls return an offset or -1 if out of memory. We
2775 * specifically need an offset of 0, however, so...
2777 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2778 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2780 if(failed)
2782 free(msft);
2783 return 0;
2786 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2787 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2789 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2790 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2792 set_lib_flags(msft);
2793 set_lcid(msft);
2794 set_help_file_name(msft);
2795 set_doc_string(msft);
2796 set_guid(msft);
2797 set_version(msft);
2798 set_name(msft);
2799 set_help_context(msft);
2800 set_help_string_dll(msft);
2801 set_help_string_context(msft);
2803 if (typelib->attrs) LIST_FOR_EACH_ENTRY( attr, typelib->attrs, const attr_t, entry ) {
2804 switch(attr->type) {
2805 case ATTR_CUSTOM:
2806 set_custdata_attr(msft, attr->u.pval, &msft->typelib_header.CustomDataOffset);
2807 break;
2808 default:
2809 break;
2813 /* midl adds two sets of custom data to the library: the current unix time
2814 and midl's version number */
2815 time_override = getenv( "WIDL_TIME_OVERRIDE");
2816 cur_time = time_override ? atol( time_override) : time(NULL);
2817 sprintf(info_string, "Created by WIDL version %s at %s", PACKAGE_VERSION, ctime(&cur_time));
2818 set_custdata(msft, &midl_info_guid, VT_BSTR, info_string, &msft->typelib_header.CustomDataOffset);
2819 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2820 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2822 if (typelib->stmts)
2823 LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
2824 add_entry(msft, stmt);
2826 save_all_changes(msft);
2827 free(msft);
2828 return 1;