wbemprox: Add some properties of SoftwareLicensingProduct class.
[wine.git] / tools / widl / write_msft.c
blob2df1b318b2dc63eccd180995f41c906f0b2dacb5
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 #define NONAMELESSUNION
41 #include "widl.h"
42 #include "winerror.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winnls.h"
46 #include "typelib.h"
47 #include "typelib_struct.h"
48 #include "utils.h"
49 #include "header.h"
50 #include "hash.h"
51 #include "typetree.h"
52 #include "parser.h"
53 #include "typegen.h"
55 enum MSFT_segment_index {
56 MSFT_SEG_TYPEINFO = 0, /* type information */
57 MSFT_SEG_IMPORTINFO, /* import information */
58 MSFT_SEG_IMPORTFILES, /* import filenames */
59 MSFT_SEG_REFERENCES, /* references (?) */
60 MSFT_SEG_GUIDHASH, /* hash table for guids? */
61 MSFT_SEG_GUID, /* guid storage */
62 MSFT_SEG_NAMEHASH, /* hash table for names */
63 MSFT_SEG_NAME, /* name storage */
64 MSFT_SEG_STRING, /* string storage */
65 MSFT_SEG_TYPEDESC, /* type descriptions */
66 MSFT_SEG_ARRAYDESC, /* array descriptions */
67 MSFT_SEG_CUSTDATA, /* custom data */
68 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
69 MSFT_SEG_UNKNOWN, /* ??? */
70 MSFT_SEG_UNKNOWN2, /* ??? */
71 MSFT_SEG_MAX /* total number of segments */
74 typedef struct tagMSFT_ImpFile {
75 int guid;
76 LCID lcid;
77 int version;
78 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
79 } MSFT_ImpFile;
81 typedef struct _msft_typelib_t
83 typelib_t *typelib;
84 MSFT_Header typelib_header;
85 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
86 unsigned char *typelib_segment_data[MSFT_SEG_MAX];
87 int typelib_segment_block_length[MSFT_SEG_MAX];
89 INT typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
91 INT *typelib_namehash_segment;
92 INT *typelib_guidhash_segment;
94 INT help_string_dll_offset;
96 struct _msft_typeinfo_t *typeinfos;
97 struct _msft_typeinfo_t *last_typeinfo;
98 } msft_typelib_t;
100 typedef struct _msft_typeinfo_t
102 msft_typelib_t *typelib;
103 MSFT_TypeInfoBase *typeinfo;
105 int typekind;
107 unsigned int var_data_allocated;
108 int *var_data;
110 unsigned int func_data_allocated;
111 int *func_data;
113 int vars_allocated;
114 int *var_indices;
115 int *var_names;
116 int *var_offsets;
118 int funcs_allocated;
119 int *func_indices;
120 int *func_names;
121 int *func_offsets;
123 int datawidth;
125 struct _msft_typeinfo_t *next_typeinfo;
126 } msft_typeinfo_t;
130 /*================== Internal functions ===================================*/
132 /****************************************************************************
133 * ctl2_init_header
135 * Initializes the type library header of a new typelib.
137 static void ctl2_init_header(
138 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
140 typelib->typelib_header.magic1 = 0x5446534d;
141 typelib->typelib_header.magic2 = 0x00010002;
142 typelib->typelib_header.posguid = -1;
143 typelib->typelib_header.lcid = 0x0409;
144 typelib->typelib_header.lcid2 = 0x0;
145 typelib->typelib_header.varflags = 0x40;
146 typelib->typelib_header.version = 0;
147 typelib->typelib_header.flags = 0;
148 typelib->typelib_header.nrtypeinfos = 0;
149 typelib->typelib_header.helpstring = -1;
150 typelib->typelib_header.helpstringcontext = 0;
151 typelib->typelib_header.helpcontext = 0;
152 typelib->typelib_header.nametablecount = 0;
153 typelib->typelib_header.nametablechars = 0;
154 typelib->typelib_header.NameOffset = -1;
155 typelib->typelib_header.helpfile = -1;
156 typelib->typelib_header.CustomDataOffset = -1;
157 typelib->typelib_header.res44 = 0x20;
158 typelib->typelib_header.res48 = 0x80;
159 typelib->typelib_header.dispatchpos = -1;
160 typelib->typelib_header.nimpinfos = 0;
163 /****************************************************************************
164 * ctl2_init_segdir
166 * Initializes the segment directory of a new typelib.
168 static void ctl2_init_segdir(
169 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
171 int i;
172 MSFT_pSeg *segdir;
174 segdir = &typelib->typelib_segdir[MSFT_SEG_TYPEINFO];
176 for (i = 0; i < MSFT_SEG_MAX; i++) {
177 segdir[i].offset = -1;
178 segdir[i].length = 0;
179 segdir[i].res08 = -1;
180 segdir[i].res0c = 0x0f;
184 /****************************************************************************
185 * ctl2_hash_guid
187 * Generates a hash key from a GUID.
189 * RETURNS
191 * The hash key for the GUID.
193 static int ctl2_hash_guid(
194 REFGUID guid) /* [I] The guid to hash. */
196 int hash;
197 int i;
199 hash = 0;
200 for (i = 0; i < 8; i ++) {
201 hash ^= ((const short *)guid)[i];
204 return hash & 0x1f;
207 /****************************************************************************
208 * ctl2_find_guid
210 * Locates a guid in a type library.
212 * RETURNS
214 * The offset into the GUID segment of the guid, or -1 if not found.
216 static int ctl2_find_guid(
217 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
218 int hash_key, /* [I] The hash key for the guid. */
219 REFGUID guid) /* [I] The guid to find. */
221 int offset;
222 MSFT_GuidEntry *guidentry;
224 offset = typelib->typelib_guidhash_segment[hash_key];
225 while (offset != -1) {
226 guidentry = (MSFT_GuidEntry *)&typelib->typelib_segment_data[MSFT_SEG_GUID][offset];
228 if (!memcmp(guidentry, guid, sizeof(GUID))) return offset;
230 offset = guidentry->next_hash;
233 return offset;
236 /****************************************************************************
237 * ctl2_find_name
239 * Locates a name in a type library.
241 * RETURNS
243 * The offset into the NAME segment of the name, or -1 if not found.
245 * NOTES
247 * The name must be encoded as with ctl2_encode_name().
249 static int ctl2_find_name(
250 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
251 char *name) /* [I] The encoded name to find. */
253 int offset;
254 int *namestruct;
256 offset = typelib->typelib_namehash_segment[name[2] & 0x7f];
257 while (offset != -1) {
258 namestruct = (int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][offset];
260 if (!((namestruct[2] ^ *((int *)name)) & 0xffff00ff)) {
261 /* hash codes and lengths match, final test */
262 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
265 /* move to next item in hash bucket */
266 offset = namestruct[1];
269 return offset;
272 /****************************************************************************
273 * ctl2_encode_name
275 * Encodes a name string to a form suitable for storing into a type library
276 * or comparing to a name stored in a type library.
278 * RETURNS
280 * The length of the encoded name, including padding and length+hash fields.
282 * NOTES
284 * Will throw an exception if name or result are NULL. Is not multithread
285 * safe in the slightest.
287 static int ctl2_encode_name(
288 msft_typelib_t *typelib, /* [I] The typelib to operate against (used for LCID only). */
289 const char *name, /* [I] The name string to encode. */
290 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
292 char *converted_name;
293 size_t length, size;
294 int offset;
295 int value;
297 length = strlen(name);
298 size = (length + 7) & ~3;
299 converted_name = xmalloc(size + 1);
300 memcpy(converted_name + 4, name, length);
301 converted_name[length + 4] = 0;
303 value = lhash_val_of_name_sys(typelib->typelib_header.varflags & 0x0f, typelib->typelib_header.lcid, converted_name + 4);
305 converted_name[0] = length & 0xff;
306 converted_name[1] = length >> 8;
307 converted_name[2] = value;
308 converted_name[3] = value >> 8;
310 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
312 *result = converted_name;
314 return size;
317 /****************************************************************************
318 * ctl2_encode_string
320 * Encodes a string to a form suitable for storing into a type library or
321 * comparing to a string stored in a type library.
323 * RETURNS
325 * The length of the encoded string, including padding and length fields.
327 * NOTES
329 * Will throw an exception if string or result are NULL. Is not multithread
330 * safe in the slightest.
332 static int ctl2_encode_string(
333 const char *string, /* [I] The string to encode. */
334 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
336 char *converted_string;
337 size_t length, size;
338 int offset;
340 length = strlen(string);
341 size = (length + 5) & ~3;
342 if (length < 3) size += 4;
343 converted_string = xmalloc(size);
344 memcpy(converted_string + 2, string, length);
345 converted_string[0] = length & 0xff;
346 converted_string[1] = (length >> 8) & 0xff;
348 if(length < 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
349 for(offset = 0; offset < 4; offset++)
350 converted_string[length + offset + 2] = 0x57;
351 length += 4;
353 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
355 *result = converted_string;
356 return size;
359 /****************************************************************************
360 * ctl2_alloc_segment
362 * Allocates memory from a segment in a type library.
364 * RETURNS
366 * Success: The offset within the segment of the new data area.
368 * BUGS
370 * Does not (yet) handle the case where the allocated segment memory needs to grow.
372 static int ctl2_alloc_segment(
373 msft_typelib_t *typelib, /* [I] The type library in which to allocate. */
374 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
375 int size, /* [I] The amount to allocate. */
376 int block_size) /* [I] Initial allocation block size, or 0 for default. */
378 int offset;
380 if(!typelib->typelib_segment_data[segment]) {
381 if (!block_size) block_size = 0x2000;
383 typelib->typelib_segment_block_length[segment] = block_size;
384 typelib->typelib_segment_data[segment] = xmalloc(block_size);
385 if (!typelib->typelib_segment_data[segment]) return -1;
386 memset(typelib->typelib_segment_data[segment], 0x57, block_size);
389 while ((typelib->typelib_segdir[segment].length + size) > typelib->typelib_segment_block_length[segment]) {
390 unsigned char *block;
392 block_size = typelib->typelib_segment_block_length[segment];
393 block = xrealloc(typelib->typelib_segment_data[segment], block_size << 1);
395 if (segment == MSFT_SEG_TYPEINFO) {
396 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
397 msft_typeinfo_t *typeinfo;
399 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
400 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - typelib->typelib_segment_data[segment]];
404 memset(block + block_size, 0x57, block_size);
405 typelib->typelib_segment_block_length[segment] = block_size << 1;
406 typelib->typelib_segment_data[segment] = block;
409 offset = typelib->typelib_segdir[segment].length;
410 typelib->typelib_segdir[segment].length += size;
412 return offset;
415 /****************************************************************************
416 * ctl2_alloc_typeinfo
418 * Allocates and initializes a typeinfo structure in a type library.
420 * RETURNS
422 * Success: The offset of the new typeinfo.
423 * Failure: -1 (this is invariably an out of memory condition).
425 static int ctl2_alloc_typeinfo(
426 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
427 int nameoffset) /* [I] The offset of the name for this typeinfo. */
429 int offset;
430 MSFT_TypeInfoBase *typeinfo;
432 offset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
434 typelib->typelib_typeinfo_offsets[typelib->typelib_header.nrtypeinfos++] = offset;
436 typeinfo = (void *)(typelib->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
438 typeinfo->typekind = (typelib->typelib_header.nrtypeinfos - 1) << 16;
439 typeinfo->memoffset = -1; /* should be EOF if no elements */
440 typeinfo->res2 = 0;
441 typeinfo->res3 = -1;
442 typeinfo->res4 = 3;
443 typeinfo->res5 = 0;
444 typeinfo->cElement = 0;
445 typeinfo->res7 = 0;
446 typeinfo->res8 = 0;
447 typeinfo->res9 = 0;
448 typeinfo->resA = 0;
449 typeinfo->posguid = -1;
450 typeinfo->flags = 0;
451 typeinfo->NameOffset = nameoffset;
452 typeinfo->version = 0;
453 typeinfo->docstringoffs = -1;
454 typeinfo->helpstringcontext = 0;
455 typeinfo->helpcontext = 0;
456 typeinfo->oCustData = -1;
457 typeinfo->cbSizeVft = 0;
458 typeinfo->cImplTypes = 0;
459 typeinfo->size = 0;
460 typeinfo->datatype1 = -1;
461 typeinfo->datatype2 = 0;
462 typeinfo->res18 = 0;
463 typeinfo->res19 = -1;
465 return offset;
468 /****************************************************************************
469 * ctl2_alloc_guid
471 * Allocates and initializes a GUID structure in a type library. Also updates
472 * the GUID hash table as needed.
474 * RETURNS
476 * Success: The offset of the new GUID.
478 static int ctl2_alloc_guid(
479 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
480 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
482 int offset;
483 MSFT_GuidEntry *guid_space;
484 int hash_key;
486 chat("adding uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
487 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
488 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
489 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
491 hash_key = ctl2_hash_guid(&guid->guid);
493 offset = ctl2_find_guid(typelib, hash_key, &guid->guid);
494 if (offset != -1)
496 if (is_warning_enabled(2368))
497 warning("duplicate uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
498 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
499 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
500 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
501 return -1;
504 offset = ctl2_alloc_segment(typelib, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
506 guid_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_GUID] + offset);
507 *guid_space = *guid;
509 guid_space->next_hash = typelib->typelib_guidhash_segment[hash_key];
510 typelib->typelib_guidhash_segment[hash_key] = offset;
512 return offset;
515 /****************************************************************************
516 * ctl2_alloc_name
518 * Allocates and initializes a name within a type library. Also updates the
519 * name hash table as needed.
521 * RETURNS
523 * Success: The offset within the segment of the new name.
524 * Failure: -1 (this is invariably an out of memory condition).
526 static int ctl2_alloc_name(
527 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
528 const char *name) /* [I] The name to store. */
530 int length;
531 int offset;
532 MSFT_NameIntro *name_space;
533 char *encoded_name;
535 length = ctl2_encode_name(typelib, name, &encoded_name);
537 offset = ctl2_find_name(typelib, encoded_name);
538 if (offset != -1)
540 free(encoded_name);
541 return offset;
544 offset = ctl2_alloc_segment(typelib, MSFT_SEG_NAME, length + 8, 0);
546 name_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_NAME] + offset);
547 name_space->hreftype = -1;
548 name_space->next_hash = -1;
549 memcpy(&name_space->namelen, encoded_name, length);
551 if (typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
552 name_space->next_hash = typelib->typelib_namehash_segment[encoded_name[2] & 0x7f];
554 typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
556 typelib->typelib_header.nametablecount += 1;
557 typelib->typelib_header.nametablechars += *encoded_name;
559 free(encoded_name);
560 return offset;
563 /****************************************************************************
564 * ctl2_alloc_string
566 * Allocates and initializes a string in a type library.
568 * RETURNS
570 * Success: The offset within the segment of the new string.
571 * Failure: -1 (this is invariably an out of memory condition).
573 static int ctl2_alloc_string(
574 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
575 const char *string) /* [I] The string to store. */
577 int length;
578 int offset;
579 unsigned char *string_space;
580 char *encoded_string;
582 length = ctl2_encode_string(string, &encoded_string);
584 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_STRING].length;
585 offset += (((typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
586 typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
587 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
590 offset = ctl2_alloc_segment(typelib, MSFT_SEG_STRING, length, 0);
592 string_space = typelib->typelib_segment_data[MSFT_SEG_STRING] + offset;
593 memcpy(string_space, encoded_string, length);
594 free(encoded_string);
596 return offset;
599 /****************************************************************************
600 * alloc_msft_importinfo
602 * Allocates and initializes an import information structure in a type library.
604 * RETURNS
606 * Success: The offset of the new importinfo.
607 * Failure: -1 (this is invariably an out of memory condition).
609 static int alloc_msft_importinfo(
610 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
611 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
613 int offset;
614 MSFT_ImpInfo *impinfo_space;
616 for (offset = 0;
617 offset < typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
618 offset += sizeof(MSFT_ImpInfo)) {
619 if (!memcmp(&(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][offset]),
620 impinfo, sizeof(MSFT_ImpInfo))) {
621 return offset;
625 impinfo->flags |= typelib->typelib_header.nimpinfos++;
627 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
629 impinfo_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
630 *impinfo_space = *impinfo;
632 return offset;
635 /****************************************************************************
636 * alloc_importfile
638 * Allocates and initializes an import file definition in a type library.
640 * RETURNS
642 * Success: The offset of the new importinfo.
643 * Failure: -1 (this is invariably an out of memory condition).
645 static int alloc_importfile(
646 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
647 int guidoffset, /* [I] The offset to the GUID for the imported library. */
648 int major_version, /* [I] The major version number of the imported library. */
649 int minor_version, /* [I] The minor version number of the imported library. */
650 const char *filename) /* [I] The filename of the imported library. */
652 int length;
653 int offset;
654 MSFT_ImpFile *importfile;
655 char *encoded_string;
657 length = ctl2_encode_string(filename, &encoded_string);
659 encoded_string[0] <<= 2;
660 encoded_string[0] |= 1;
662 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
663 offset += (((typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
664 typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 0xc) {
665 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
668 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
670 importfile = (MSFT_ImpFile *)&typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
671 importfile->guid = guidoffset;
672 importfile->lcid = typelib->typelib_header.lcid2;
673 importfile->version = major_version | (minor_version << 16);
674 memcpy(&importfile->filename, encoded_string, length);
675 free(encoded_string);
677 return offset;
680 static void alloc_importinfo(msft_typelib_t *typelib, importinfo_t *importinfo)
682 importlib_t *importlib = importinfo->importlib;
684 chat("alloc_importinfo: %s\n", importinfo->name);
686 if(!importlib->allocated) {
687 MSFT_GuidEntry guid;
688 int guid_idx;
690 chat("allocating importlib %s\n", importlib->name);
692 importlib->allocated = -1;
694 memcpy(&guid.guid, &importlib->guid, sizeof(GUID));
695 guid.hreftype = 2;
697 guid_idx = ctl2_alloc_guid(typelib, &guid);
699 importlib->offset = alloc_importfile(typelib, guid_idx, importlib->version & 0xffff,
700 importlib->version >> 16, importlib->name);
703 if(importinfo->offset == -1 || !(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID)) {
704 MSFT_ImpInfo impinfo;
706 impinfo.flags = importinfo->flags;
707 impinfo.oImpFile = importlib->offset;
709 if(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID) {
710 MSFT_GuidEntry guid;
712 guid.hreftype = 0;
713 memcpy(&guid.guid, &importinfo->guid, sizeof(GUID));
715 impinfo.oGuid = ctl2_alloc_guid(typelib, &guid);
717 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
719 typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo.oGuid+sizeof(GUID)]
720 = importinfo->offset+1;
722 if(!strcmp(importinfo->name, "IDispatch"))
723 typelib->typelib_header.dispatchpos = importinfo->offset+1;
724 }else {
725 impinfo.oGuid = importinfo->id;
726 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
731 static importinfo_t *find_importinfo(msft_typelib_t *typelib, const char *name)
733 importlib_t *importlib;
734 int i;
736 chat("search importlib %s\n", name);
738 if(!name)
739 return NULL;
741 LIST_FOR_EACH_ENTRY( importlib, &typelib->typelib->importlibs, importlib_t, entry )
743 for(i=0; i < importlib->ntypeinfos; i++) {
744 if(!strcmp(name, importlib->importinfos[i].name)) {
745 chat("Found %s in importlib.\n", name);
746 return importlib->importinfos+i;
751 return NULL;
754 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure);
755 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface);
756 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration);
757 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion);
758 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls);
759 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
760 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
763 /****************************************************************************
764 * encode_type
766 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
767 * segments as needed.
769 * RETURNS
771 * Success: 0.
772 * Failure: -1.
774 static int encode_type(
775 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
776 int vt, /* [I] vt to encode */
777 type_t *type, /* [I] type */
778 int *encoded_type, /* [O] The encoded type description. */
779 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
781 int default_type;
782 int scratch;
783 int typeoffset;
784 int *typedata;
785 int target_type;
786 int child_size = 0;
788 chat("encode_type vt %d type %p\n", vt, type);
790 default_type = 0x80000000 | (vt << 16) | vt;
791 if (!decoded_size) decoded_size = &scratch;
793 *decoded_size = 0;
795 switch (vt) {
796 case VT_I1:
797 case VT_UI1:
798 *encoded_type = default_type;
799 break;
801 case VT_INT:
802 *encoded_type = 0x80000000 | (VT_I4 << 16) | VT_INT;
803 break;
805 case VT_UINT:
806 *encoded_type = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
807 break;
809 case VT_UI2:
810 case VT_I2:
811 case VT_BOOL:
812 *encoded_type = default_type;
813 break;
815 case VT_I4:
816 case VT_UI4:
817 case VT_R4:
818 case VT_ERROR:
819 case VT_HRESULT:
820 *encoded_type = default_type;
821 break;
823 case VT_R8:
824 case VT_I8:
825 case VT_UI8:
826 *encoded_type = default_type;
827 break;
829 case VT_CY:
830 case VT_DATE:
831 *encoded_type = default_type;
832 break;
834 case VT_DECIMAL:
835 *encoded_type = default_type;
836 break;
838 case VT_VOID:
839 *encoded_type = 0x80000000 | (VT_EMPTY << 16) | vt;
840 break;
842 case VT_UNKNOWN:
843 case VT_DISPATCH:
844 case VT_BSTR:
845 *encoded_type = default_type;
846 break;
848 case VT_VARIANT:
849 *encoded_type = default_type;
850 break;
852 case VT_LPSTR:
853 case VT_LPWSTR:
854 *encoded_type = 0xfffe0000 | vt;
855 break;
857 case VT_PTR:
859 int next_vt;
860 for(next_vt = 0; is_ptr(type); type = type_pointer_get_ref_type(type)) {
861 next_vt = get_type_vt(type_pointer_get_ref_type(type));
862 if (next_vt != 0)
863 break;
865 /* if no type found then it must be void */
866 if (next_vt == 0)
867 next_vt = VT_VOID;
869 encode_type(typelib, next_vt, type_pointer_get_ref_type(type),
870 &target_type, &child_size);
871 /* these types already have an implicit pointer, so we don't need to
872 * add another */
873 if(next_vt == VT_DISPATCH || next_vt == VT_UNKNOWN) {
874 chat("encode_type: skipping ptr\n");
875 *encoded_type = target_type;
876 *decoded_size = child_size;
877 break;
880 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
881 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
882 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
885 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
886 int mix_field;
888 if (target_type & 0x80000000) {
889 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
890 } else {
891 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
892 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
895 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
896 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
898 typedata[0] = (mix_field << 16) | VT_PTR;
899 typedata[1] = target_type;
902 *encoded_type = typeoffset;
904 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
905 break;
908 case VT_SAFEARRAY:
910 type_t *element_type = type_alias_get_aliasee_type(type_array_get_element_type(type));
911 int next_vt = get_type_vt(element_type);
913 encode_type(typelib, next_vt, type_alias_get_aliasee_type(type_array_get_element_type(type)),
914 &target_type, &child_size);
916 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
917 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
918 if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
921 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
922 int mix_field;
924 if (target_type & 0x80000000) {
925 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
926 } else {
927 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
928 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
931 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
932 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
934 typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
935 typedata[1] = target_type;
938 *encoded_type = typeoffset;
940 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
941 break;
944 case VT_USERDEFINED:
946 importinfo_t *importinfo;
947 int typeinfo_offset;
949 if (type->typelib_idx > -1)
951 chat("encode_type: VT_USERDEFINED - found already defined type %s at %d\n",
952 type->name, type->typelib_idx);
953 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
955 else if ((importinfo = find_importinfo(typelib, type->name)))
957 chat("encode_type: VT_USERDEFINED - found imported type %s in %s\n",
958 type->name, importinfo->importlib->name);
959 alloc_importinfo(typelib, importinfo);
960 typeinfo_offset = importinfo->offset | 0x1;
962 else
964 /* Typedefs without the [public] attribute aren't included in the
965 * typelib, unless the aliasee is an anonymous UDT or the typedef
966 * is wire-marshalled. In the latter case the wire-marshal type,
967 * which may be a non-public alias, is used instead. */
968 while (type_is_alias(type))
970 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
972 type = get_attrp(type->attrs, ATTR_WIREMARSHAL);
973 break;
975 else if (!is_attr(type->attrs, ATTR_PUBLIC))
976 type = type_alias_get_aliasee_type(type);
977 else
978 break;
981 chat("encode_type: VT_USERDEFINED - adding new type %s, real type %d\n",
982 type->name, type_get_type(type));
984 switch (type_get_type_detect_alias(type))
986 case TYPE_STRUCT:
987 case TYPE_ENCAPSULATED_UNION:
988 add_structure_typeinfo(typelib, type);
989 break;
990 case TYPE_INTERFACE:
991 add_interface_typeinfo(typelib, type);
992 break;
993 case TYPE_ENUM:
994 add_enum_typeinfo(typelib, type);
995 break;
996 case TYPE_UNION:
997 add_union_typeinfo(typelib, type);
998 break;
999 case TYPE_COCLASS:
1000 add_coclass_typeinfo(typelib, type);
1001 break;
1002 case TYPE_ALIAS:
1003 add_typedef_typeinfo(typelib, type);
1004 break;
1005 default:
1006 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
1007 type_get_type(type));
1010 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
1012 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1013 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1014 if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == typeinfo_offset)) break;
1017 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1018 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1019 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1021 typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1022 typedata[1] = typeinfo_offset;
1025 *encoded_type = typeoffset;
1026 break;
1029 default:
1030 error("encode_type: unrecognized type %d.\n", vt);
1031 *encoded_type = default_type;
1032 break;
1035 return 0;
1038 static void dump_type(type_t *t)
1040 chat("dump_type: %p name %s type %d attrs %p\n", t, t->name, type_get_type(t), t->attrs);
1043 static int encode_var(
1044 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
1045 type_t *type, /* [I] The type description to encode. */
1046 var_t *var, /* [I] The var to encode. */
1047 int *encoded_type, /* [O] The encoded type description. */
1048 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1050 int typeoffset;
1051 int *typedata;
1052 int target_type;
1053 int child_size;
1054 int vt;
1055 int scratch;
1057 if (!decoded_size) decoded_size = &scratch;
1058 *decoded_size = 0;
1060 chat("encode_var: var %p type %p type->name %s\n",
1061 var, type, type->name ? type->name : "NULL");
1063 if (is_array(type) && !type_array_is_decl_as_ptr(type)) {
1064 int num_dims, elements = 1, arrayoffset;
1065 type_t *atype;
1066 int *arraydata;
1068 num_dims = 0;
1069 for (atype = type;
1070 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1071 atype = type_array_get_element_type(atype))
1072 ++num_dims;
1074 chat("array with %d dimensions\n", num_dims);
1075 encode_var(typelib, atype, var, &target_type, NULL);
1076 arrayoffset = ctl2_alloc_segment(typelib, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1077 arraydata = (void *)&typelib->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1079 arraydata[0] = target_type;
1080 arraydata[1] = num_dims;
1081 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1083 arraydata += 2;
1084 for (atype = type;
1085 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1086 atype = type_array_get_element_type(atype))
1088 arraydata[0] = type_array_get_dim(atype);
1089 arraydata[1] = 0;
1090 arraydata += 2;
1091 elements *= type_array_get_dim(atype);
1094 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1095 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1097 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1098 typedata[1] = arrayoffset;
1100 *encoded_type = typeoffset;
1101 *decoded_size = 20 /*sizeof(ARRAYDESC)*/ + (num_dims - 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1102 return 0;
1105 vt = get_type_vt(type);
1106 if (vt == VT_PTR) {
1107 type_t *ref = is_ptr(type) ?
1108 type_pointer_get_ref_type(type) : type_array_get_element_type(type);
1109 int skip_ptr = encode_var(typelib, ref, var, &target_type, &child_size);
1111 if(skip_ptr == 2) {
1112 chat("encode_var: skipping ptr\n");
1113 *encoded_type = target_type;
1114 *decoded_size = child_size;
1115 return 0;
1118 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1119 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1120 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1123 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1124 int mix_field;
1126 if (target_type & 0x80000000) {
1127 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1128 } else if (get_type_vt(ref) == VT_SAFEARRAY) {
1129 type_t *element_type = type_alias_get_aliasee_type(type_array_get_element_type(ref));
1130 mix_field = get_type_vt(element_type) | VT_ARRAY | VT_BYREF;
1131 } else {
1132 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1133 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1136 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1137 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1139 typedata[0] = (mix_field << 16) | VT_PTR;
1140 typedata[1] = target_type;
1143 *encoded_type = typeoffset;
1145 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1146 return 0;
1149 dump_type(type);
1151 encode_type(typelib, vt, type, encoded_type, decoded_size);
1152 /* these types already have an implicit pointer, so we don't need to
1153 * add another */
1154 if(vt == VT_DISPATCH || vt == VT_UNKNOWN) return 2;
1155 return 0;
1158 static unsigned int get_ulong_val(unsigned int val, int vt)
1160 switch(vt) {
1161 case VT_I2:
1162 case VT_BOOL:
1163 case VT_UI2:
1164 return val & 0xffff;
1165 case VT_I1:
1166 case VT_UI1:
1167 return val & 0xff;
1170 return val;
1173 static void write_int_value(msft_typelib_t *typelib, int *out, int vt, int value)
1175 const unsigned int lv = get_ulong_val(value, vt);
1176 if ((lv & 0x3ffffff) == lv) {
1177 *out = 0x80000000;
1178 *out |= vt << 26;
1179 *out |= lv;
1180 } else {
1181 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1182 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1183 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &value, 4);
1184 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1185 *out = offset;
1189 static void write_string_value(msft_typelib_t *typelib, int *out, const char *value)
1191 int len = strlen(value), seg_len = (len + 6 + 3) & ~0x3;
1192 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1193 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = VT_BSTR;
1194 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &len, sizeof(len));
1195 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1196 len += 6;
1197 while(len < seg_len) {
1198 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1199 len++;
1201 *out = offset;
1204 static void write_default_value(msft_typelib_t *typelib, type_t *type, expr_t *expr, int *out)
1206 int vt;
1208 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT) {
1209 vt = get_type_vt(type);
1210 if (vt != VT_BSTR && vt != VT_VARIANT)
1211 error("string default value applied to non-string type\n");
1212 chat("default value '%s'\n", expr->u.sval);
1213 write_string_value(typelib, out, expr->u.sval);
1214 return;
1217 if (type_get_type(type) == TYPE_ENUM) {
1218 vt = VT_I4;
1219 } else if (is_ptr(type)) {
1220 vt = get_type_vt(type_pointer_get_ref_type(type));
1221 if (vt == VT_USERDEFINED)
1222 vt = VT_I4;
1223 if (expr->cval)
1224 warning("non-null pointer default value\n");
1225 } else {
1226 vt = get_type_vt(type);
1227 switch(vt) {
1228 case VT_I2:
1229 case VT_I4:
1230 case VT_R4:
1231 case VT_BOOL:
1232 case VT_I1:
1233 case VT_UI1:
1234 case VT_UI2:
1235 case VT_UI4:
1236 case VT_INT:
1237 case VT_UINT:
1238 case VT_HRESULT:
1239 break;
1240 case VT_VARIANT: {
1241 switch (expr->type) {
1242 case EXPR_DOUBLE:
1243 vt = VT_R4;
1244 break;
1245 case EXPR_NUM:
1246 vt = VT_I4;
1247 break;
1248 default:
1249 warning("can't write default VT_VARIANT value for expression type %d.\n", expr->type);
1250 return;
1252 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 typeref_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 typeref_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, typeref_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, typeref_t, entry ) {
2355 if(iref->type->typelib_idx == -1)
2356 add_interface_typeinfo(typelib, iref->type);
2357 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2358 ref->reftype = typelib->typelib_typeinfo_offsets[iref->type->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 typeref_t *ref;
2483 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry) {
2484 /* if the type is public then add the typedef, otherwise attempt
2485 * to add the aliased type */
2486 if (is_attr(ref->type->attrs, ATTR_PUBLIC))
2487 add_typedef_typeinfo(typelib, ref->type);
2488 else
2489 add_type_typeinfo(typelib, type_alias_get_aliasee_type(ref->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 init_output_buffer();
2730 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2731 if(typelib->typelib_header.varflags & 0x100)
2732 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2734 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2735 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2736 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2737 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2738 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2739 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2740 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2741 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2742 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2743 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2744 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2745 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2746 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2747 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2748 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2750 ctl2_write_typeinfos(typelib);
2752 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2754 char typelib_id[13] = "#1";
2756 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2757 if (expr)
2758 sprintf( typelib_id, "#%d", expr->cval );
2759 add_output_to_resources( "TYPELIB", typelib_id );
2760 if (strendswith( typelib_name, "_t.res" )) /* add typelib registration */
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;