comctl32: Send WM_NOTIFY when expanding ancestors of the new selection.
[wine.git] / tools / widl / write_msft.c
blob4c85849d2adfc3da35fbe7fb2fb93075100c12d6
1 /*
2 * Typelib v2 (MSFT) generation
4 * Copyright 2004 Alastair Bridgewater
5 * 2004, 2005 Huw Davies
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * --------------------------------------------------------------------------------------
22 * Known problems:
24 * Badly incomplete.
26 * Only works on little-endian systems.
30 #include "config.h"
31 #include "wine/port.h"
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <time.h>
40 #define NONAMELESSUNION
42 #include "winerror.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winnls.h"
47 #include "widl.h"
48 #include "typelib.h"
49 #include "typelib_struct.h"
50 #include "utils.h"
51 #include "header.h"
52 #include "hash.h"
53 #include "typetree.h"
55 enum MSFT_segment_index {
56 MSFT_SEG_TYPEINFO = 0, /* type information */
57 MSFT_SEG_IMPORTINFO, /* import information */
58 MSFT_SEG_IMPORTFILES, /* import filenames */
59 MSFT_SEG_REFERENCES, /* references (?) */
60 MSFT_SEG_GUIDHASH, /* hash table for guids? */
61 MSFT_SEG_GUID, /* guid storage */
62 MSFT_SEG_NAMEHASH, /* hash table for names */
63 MSFT_SEG_NAME, /* name storage */
64 MSFT_SEG_STRING, /* string storage */
65 MSFT_SEG_TYPEDESC, /* type descriptions */
66 MSFT_SEG_ARRAYDESC, /* array descriptions */
67 MSFT_SEG_CUSTDATA, /* custom data */
68 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
69 MSFT_SEG_UNKNOWN, /* ??? */
70 MSFT_SEG_UNKNOWN2, /* ??? */
71 MSFT_SEG_MAX /* total number of segments */
74 typedef struct tagMSFT_ImpFile {
75 int guid;
76 LCID lcid;
77 int version;
78 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
79 } MSFT_ImpFile;
81 typedef struct _msft_typelib_t
83 typelib_t *typelib;
84 MSFT_Header typelib_header;
85 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
86 unsigned char *typelib_segment_data[MSFT_SEG_MAX];
87 int typelib_segment_block_length[MSFT_SEG_MAX];
89 INT typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
91 INT *typelib_namehash_segment;
92 INT *typelib_guidhash_segment;
94 INT help_string_dll_offset;
96 struct _msft_typeinfo_t *typeinfos;
97 struct _msft_typeinfo_t *last_typeinfo;
98 } msft_typelib_t;
100 typedef struct _msft_typeinfo_t
102 msft_typelib_t *typelib;
103 MSFT_TypeInfoBase *typeinfo;
105 int typekind;
107 unsigned int var_data_allocated;
108 int *var_data;
110 unsigned int func_data_allocated;
111 int *func_data;
113 int vars_allocated;
114 int *var_indices;
115 int *var_names;
116 int *var_offsets;
118 int funcs_allocated;
119 int *func_indices;
120 int *func_names;
121 int *func_offsets;
123 int datawidth;
125 struct _msft_typeinfo_t *next_typeinfo;
126 } msft_typeinfo_t;
130 /*================== Internal functions ===================================*/
132 /****************************************************************************
133 * ctl2_init_header
135 * Initializes the type library header of a new typelib.
137 static void ctl2_init_header(
138 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
140 typelib->typelib_header.magic1 = 0x5446534d;
141 typelib->typelib_header.magic2 = 0x00010002;
142 typelib->typelib_header.posguid = -1;
143 typelib->typelib_header.lcid = 0x0409;
144 typelib->typelib_header.lcid2 = 0x0;
145 typelib->typelib_header.varflags = 0x40;
146 typelib->typelib_header.version = 0;
147 typelib->typelib_header.flags = 0;
148 typelib->typelib_header.nrtypeinfos = 0;
149 typelib->typelib_header.helpstring = -1;
150 typelib->typelib_header.helpstringcontext = 0;
151 typelib->typelib_header.helpcontext = 0;
152 typelib->typelib_header.nametablecount = 0;
153 typelib->typelib_header.nametablechars = 0;
154 typelib->typelib_header.NameOffset = -1;
155 typelib->typelib_header.helpfile = -1;
156 typelib->typelib_header.CustomDataOffset = -1;
157 typelib->typelib_header.res44 = 0x20;
158 typelib->typelib_header.res48 = 0x80;
159 typelib->typelib_header.dispatchpos = -1;
160 typelib->typelib_header.nimpinfos = 0;
163 /****************************************************************************
164 * ctl2_init_segdir
166 * Initializes the segment directory of a new typelib.
168 static void ctl2_init_segdir(
169 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
171 int i;
172 MSFT_pSeg *segdir;
174 segdir = &typelib->typelib_segdir[MSFT_SEG_TYPEINFO];
176 for (i = 0; i < MSFT_SEG_MAX; i++) {
177 segdir[i].offset = -1;
178 segdir[i].length = 0;
179 segdir[i].res08 = -1;
180 segdir[i].res0c = 0x0f;
184 /****************************************************************************
185 * ctl2_hash_guid
187 * Generates a hash key from a GUID.
189 * RETURNS
191 * The hash key for the GUID.
193 static int ctl2_hash_guid(
194 REFGUID guid) /* [I] The guid to hash. */
196 int hash;
197 int i;
199 hash = 0;
200 for (i = 0; i < 8; i ++) {
201 hash ^= ((const short *)guid)[i];
204 return hash & 0x1f;
207 /****************************************************************************
208 * ctl2_find_guid
210 * Locates a guid in a type library.
212 * RETURNS
214 * The offset into the GUID segment of the guid, or -1 if not found.
216 static int ctl2_find_guid(
217 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
218 int hash_key, /* [I] The hash key for the guid. */
219 REFGUID guid) /* [I] The guid to find. */
221 int offset;
222 MSFT_GuidEntry *guidentry;
224 offset = typelib->typelib_guidhash_segment[hash_key];
225 while (offset != -1) {
226 guidentry = (MSFT_GuidEntry *)&typelib->typelib_segment_data[MSFT_SEG_GUID][offset];
228 if (!memcmp(guidentry, guid, sizeof(GUID))) return offset;
230 offset = guidentry->next_hash;
233 return offset;
236 /****************************************************************************
237 * ctl2_find_name
239 * Locates a name in a type library.
241 * RETURNS
243 * The offset into the NAME segment of the name, or -1 if not found.
245 * NOTES
247 * The name must be encoded as with ctl2_encode_name().
249 static int ctl2_find_name(
250 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
251 char *name) /* [I] The encoded name to find. */
253 int offset;
254 int *namestruct;
256 offset = typelib->typelib_namehash_segment[name[2] & 0x7f];
257 while (offset != -1) {
258 namestruct = (int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][offset];
260 if (!((namestruct[2] ^ *((int *)name)) & 0xffff00ff)) {
261 /* hash codes and lengths match, final test */
262 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
265 /* move to next item in hash bucket */
266 offset = namestruct[1];
269 return offset;
272 /****************************************************************************
273 * ctl2_encode_name
275 * Encodes a name string to a form suitable for storing into a type library
276 * or comparing to a name stored in a type library.
278 * RETURNS
280 * The length of the encoded name, including padding and length+hash fields.
282 * NOTES
284 * Will throw an exception if name or result are NULL. Is not multithread
285 * safe in the slightest.
287 static int ctl2_encode_name(
288 msft_typelib_t *typelib, /* [I] The typelib to operate against (used for LCID only). */
289 const char *name, /* [I] The name string to encode. */
290 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
292 int length;
293 static char converted_name[0x104];
294 int offset;
295 int value;
297 length = strlen(name);
298 memcpy(converted_name + 4, name, length);
300 converted_name[length + 4] = 0;
303 value = lhash_val_of_name_sys(typelib->typelib_header.varflags & 0x0f, typelib->typelib_header.lcid, converted_name + 4);
305 #ifdef WORDS_BIGENDIAN
306 converted_name[3] = length & 0xff;
307 converted_name[2] = 0x00;
308 converted_name[1] = value;
309 converted_name[0] = value >> 8;
310 #else
311 converted_name[0] = length & 0xff;
312 converted_name[1] = 0x00;
313 converted_name[2] = value;
314 converted_name[3] = value >> 8;
315 #endif
317 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
319 *result = converted_name;
321 return (length + 7) & ~3;
324 /****************************************************************************
325 * ctl2_encode_string
327 * Encodes a string to a form suitable for storing into a type library or
328 * comparing to a string stored in a type library.
330 * RETURNS
332 * The length of the encoded string, including padding and length fields.
334 * NOTES
336 * Will throw an exception if string or result are NULL. Is not multithread
337 * safe in the slightest.
339 static int ctl2_encode_string(
340 const char *string, /* [I] The string to encode. */
341 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
343 int length;
344 static char converted_string[0x104];
345 int offset;
347 length = strlen(string);
348 memcpy(converted_string + 2, string, length);
350 #ifdef WORDS_BIGENDIAN
351 converted_string[1] = length & 0xff;
352 converted_string[0] = (length >> 8) & 0xff;
353 #else
354 converted_string[0] = length & 0xff;
355 converted_string[1] = (length >> 8) & 0xff;
356 #endif
358 if(length < 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
359 for(offset = 0; offset < 4; offset++)
360 converted_string[length + offset + 2] = 0x57;
361 length += 4;
363 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
365 *result = converted_string;
367 return (length + 5) & ~3;
370 /****************************************************************************
371 * ctl2_alloc_segment
373 * Allocates memory from a segment in a type library.
375 * RETURNS
377 * Success: The offset within the segment of the new data area.
379 * BUGS
381 * Does not (yet) handle the case where the allocated segment memory needs to grow.
383 static int ctl2_alloc_segment(
384 msft_typelib_t *typelib, /* [I] The type library in which to allocate. */
385 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
386 int size, /* [I] The amount to allocate. */
387 int block_size) /* [I] Initial allocation block size, or 0 for default. */
389 int offset;
391 if(!typelib->typelib_segment_data[segment]) {
392 if (!block_size) block_size = 0x2000;
394 typelib->typelib_segment_block_length[segment] = block_size;
395 typelib->typelib_segment_data[segment] = xmalloc(block_size);
396 if (!typelib->typelib_segment_data[segment]) return -1;
397 memset(typelib->typelib_segment_data[segment], 0x57, block_size);
400 while ((typelib->typelib_segdir[segment].length + size) > typelib->typelib_segment_block_length[segment]) {
401 unsigned char *block;
403 block_size = typelib->typelib_segment_block_length[segment];
404 block = xrealloc(typelib->typelib_segment_data[segment], block_size << 1);
406 if (segment == MSFT_SEG_TYPEINFO) {
407 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
408 msft_typeinfo_t *typeinfo;
410 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
411 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - typelib->typelib_segment_data[segment]];
415 memset(block + block_size, 0x57, block_size);
416 typelib->typelib_segment_block_length[segment] = block_size << 1;
417 typelib->typelib_segment_data[segment] = block;
420 offset = typelib->typelib_segdir[segment].length;
421 typelib->typelib_segdir[segment].length += size;
423 return offset;
426 /****************************************************************************
427 * ctl2_alloc_typeinfo
429 * Allocates and initializes a typeinfo structure in a type library.
431 * RETURNS
433 * Success: The offset of the new typeinfo.
434 * Failure: -1 (this is invariably an out of memory condition).
436 static int ctl2_alloc_typeinfo(
437 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
438 int nameoffset) /* [I] The offset of the name for this typeinfo. */
440 int offset;
441 MSFT_TypeInfoBase *typeinfo;
443 offset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
445 typelib->typelib_typeinfo_offsets[typelib->typelib_header.nrtypeinfos++] = offset;
447 typeinfo = (void *)(typelib->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
449 typeinfo->typekind = (typelib->typelib_header.nrtypeinfos - 1) << 16;
450 typeinfo->memoffset = -1; /* should be EOF if no elements */
451 typeinfo->res2 = 0;
452 typeinfo->res3 = -1;
453 typeinfo->res4 = 3;
454 typeinfo->res5 = 0;
455 typeinfo->cElement = 0;
456 typeinfo->res7 = 0;
457 typeinfo->res8 = 0;
458 typeinfo->res9 = 0;
459 typeinfo->resA = 0;
460 typeinfo->posguid = -1;
461 typeinfo->flags = 0;
462 typeinfo->NameOffset = nameoffset;
463 typeinfo->version = 0;
464 typeinfo->docstringoffs = -1;
465 typeinfo->helpstringcontext = 0;
466 typeinfo->helpcontext = 0;
467 typeinfo->oCustData = -1;
468 typeinfo->cbSizeVft = 0;
469 typeinfo->cImplTypes = 0;
470 typeinfo->size = 0;
471 typeinfo->datatype1 = -1;
472 typeinfo->datatype2 = 0;
473 typeinfo->res18 = 0;
474 typeinfo->res19 = -1;
476 return offset;
479 /****************************************************************************
480 * ctl2_alloc_guid
482 * Allocates and initializes a GUID structure in a type library. Also updates
483 * the GUID hash table as needed.
485 * RETURNS
487 * Success: The offset of the new GUID.
489 static int ctl2_alloc_guid(
490 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
491 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
493 int offset;
494 MSFT_GuidEntry *guid_space;
495 int hash_key;
497 hash_key = ctl2_hash_guid(&guid->guid);
499 offset = ctl2_find_guid(typelib, hash_key, &guid->guid);
500 if (offset != -1) return offset;
502 offset = ctl2_alloc_segment(typelib, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
504 guid_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_GUID] + offset);
505 *guid_space = *guid;
507 guid_space->next_hash = typelib->typelib_guidhash_segment[hash_key];
508 typelib->typelib_guidhash_segment[hash_key] = offset;
510 return offset;
513 /****************************************************************************
514 * ctl2_alloc_name
516 * Allocates and initializes a name within a type library. Also updates the
517 * name hash table as needed.
519 * RETURNS
521 * Success: The offset within the segment of the new name.
522 * Failure: -1 (this is invariably an out of memory condition).
524 static int ctl2_alloc_name(
525 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
526 const char *name) /* [I] The name to store. */
528 int length;
529 int offset;
530 MSFT_NameIntro *name_space;
531 char *encoded_name;
533 length = ctl2_encode_name(typelib, name, &encoded_name);
535 offset = ctl2_find_name(typelib, encoded_name);
536 if (offset != -1) return offset;
538 offset = ctl2_alloc_segment(typelib, MSFT_SEG_NAME, length + 8, 0);
540 name_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_NAME] + offset);
541 name_space->hreftype = -1;
542 name_space->next_hash = -1;
543 memcpy(&name_space->namelen, encoded_name, length);
545 if (typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
546 name_space->next_hash = typelib->typelib_namehash_segment[encoded_name[2] & 0x7f];
548 typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
550 typelib->typelib_header.nametablecount += 1;
551 typelib->typelib_header.nametablechars += *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);
588 return offset;
591 /****************************************************************************
592 * alloc_msft_importinfo
594 * Allocates and initializes an import information structure in a type library.
596 * RETURNS
598 * Success: The offset of the new importinfo.
599 * Failure: -1 (this is invariably an out of memory condition).
601 static int alloc_msft_importinfo(
602 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
603 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
605 int offset;
606 MSFT_ImpInfo *impinfo_space;
608 for (offset = 0;
609 offset < typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
610 offset += sizeof(MSFT_ImpInfo)) {
611 if (!memcmp(&(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][offset]),
612 impinfo, sizeof(MSFT_ImpInfo))) {
613 return offset;
617 impinfo->flags |= typelib->typelib_header.nimpinfos++;
619 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
621 impinfo_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
622 *impinfo_space = *impinfo;
624 return offset;
627 /****************************************************************************
628 * alloc_importfile
630 * Allocates and initializes an import file definition in a type library.
632 * RETURNS
634 * Success: The offset of the new importinfo.
635 * Failure: -1 (this is invariably an out of memory condition).
637 static int alloc_importfile(
638 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
639 int guidoffset, /* [I] The offset to the GUID for the imported library. */
640 int major_version, /* [I] The major version number of the imported library. */
641 int minor_version, /* [I] The minor version number of the imported library. */
642 const char *filename) /* [I] The filename of the imported library. */
644 int length;
645 int offset;
646 MSFT_ImpFile *importfile;
647 char *encoded_string;
649 length = ctl2_encode_string(filename, &encoded_string);
651 encoded_string[0] <<= 2;
652 encoded_string[0] |= 1;
654 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
655 offset += (((typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
656 typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 0xc) {
657 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
660 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
662 importfile = (MSFT_ImpFile *)&typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
663 importfile->guid = guidoffset;
664 importfile->lcid = typelib->typelib_header.lcid2;
665 importfile->version = major_version | (minor_version << 16);
666 memcpy(&importfile->filename, encoded_string, length);
668 return offset;
671 static void alloc_importinfo(msft_typelib_t *typelib, importinfo_t *importinfo)
673 importlib_t *importlib = importinfo->importlib;
675 chat("alloc_importinfo: %s\n", importinfo->name);
677 if(!importlib->allocated) {
678 MSFT_GuidEntry guid;
679 int guid_idx;
681 chat("allocating importlib %s\n", importlib->name);
683 importlib->allocated = -1;
685 memcpy(&guid.guid, &importlib->guid, sizeof(GUID));
686 guid.hreftype = 2;
688 guid_idx = ctl2_alloc_guid(typelib, &guid);
690 alloc_importfile(typelib, guid_idx, importlib->version&0xffff,
691 importlib->version>>16, importlib->name);
694 if(importinfo->offset == -1 || !(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID)) {
695 MSFT_ImpInfo impinfo;
697 impinfo.flags = importinfo->flags;
698 impinfo.oImpFile = 0;
700 if(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID) {
701 MSFT_GuidEntry guid;
703 guid.hreftype = 0;
704 memcpy(&guid.guid, &importinfo->guid, sizeof(GUID));
706 impinfo.oGuid = ctl2_alloc_guid(typelib, &guid);
708 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
710 typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo.oGuid+sizeof(GUID)]
711 = importinfo->offset+1;
713 if(!strcmp(importinfo->name, "IDispatch"))
714 typelib->typelib_header.dispatchpos = importinfo->offset+1;
715 }else {
716 impinfo.oGuid = importinfo->id;
717 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
722 static importinfo_t *find_importinfo(msft_typelib_t *typelib, const char *name)
724 importlib_t *importlib;
725 int i;
727 chat("search importlib %s\n", name);
729 if(!name)
730 return NULL;
732 LIST_FOR_EACH_ENTRY( importlib, &typelib->typelib->importlibs, importlib_t, entry )
734 for(i=0; i < importlib->ntypeinfos; i++) {
735 if(!strcmp(name, importlib->importinfos[i].name)) {
736 chat("Found %s in importlib.\n", name);
737 return importlib->importinfos+i;
742 return NULL;
745 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure);
746 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface);
747 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration);
748 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls);
749 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
752 /****************************************************************************
753 * encode_type
755 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
756 * segments as needed.
758 * RETURNS
760 * Success: 0.
761 * Failure: -1.
763 static int encode_type(
764 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
765 int vt, /* [I] vt to encode */
766 type_t *type, /* [I] type */
767 int *encoded_type, /* [O] The encoded type description. */
768 int *width, /* [O] The width of the type, or NULL. */
769 int *alignment, /* [O] The alignment of the type, or NULL. */
770 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
772 int default_type;
773 int scratch;
774 int typeoffset;
775 int *typedata;
776 int target_type;
777 int child_size = 0;
779 chat("encode_type vt %d type %p\n", vt, type);
781 default_type = 0x80000000 | (vt << 16) | vt;
782 if (!width) width = &scratch;
783 if (!alignment) alignment = &scratch;
784 if (!decoded_size) decoded_size = &scratch;
787 switch (vt) {
788 case VT_I1:
789 case VT_UI1:
790 *encoded_type = default_type;
791 *width = 1;
792 *alignment = 1;
793 break;
795 case VT_INT:
796 *encoded_type = 0x80000000 | (VT_I4 << 16) | VT_INT;
797 if ((typelib->typelib_header.varflags & 0x0f) == SYS_WIN16) {
798 *width = 2;
799 *alignment = 2;
800 } else {
801 *width = 4;
802 *alignment = 4;
804 break;
806 case VT_UINT:
807 *encoded_type = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
808 if ((typelib->typelib_header.varflags & 0x0f) == SYS_WIN16) {
809 *width = 2;
810 *alignment = 2;
811 } else {
812 *width = 4;
813 *alignment = 4;
815 break;
817 case VT_UI2:
818 case VT_I2:
819 case VT_BOOL:
820 *encoded_type = default_type;
821 *width = 2;
822 *alignment = 2;
823 break;
825 case VT_I4:
826 case VT_UI4:
827 case VT_R4:
828 case VT_ERROR:
829 case VT_HRESULT:
830 *encoded_type = default_type;
831 *width = 4;
832 *alignment = 4;
833 break;
835 case VT_R8:
836 case VT_I8:
837 case VT_UI8:
838 *encoded_type = default_type;
839 *width = 8;
840 *alignment = 8;
841 break;
843 case VT_CY:
844 case VT_DATE:
845 *encoded_type = default_type;
846 *width = 8;
847 *alignment = 8;
848 break;
850 case VT_DECIMAL:
851 *encoded_type = default_type;
852 *width = 16;
853 *alignment = 8;
854 break;
856 case VT_VOID:
857 *encoded_type = 0x80000000 | (VT_EMPTY << 16) | vt;
858 *width = 0;
859 *alignment = 1;
860 break;
862 case VT_UNKNOWN:
863 case VT_DISPATCH:
864 case VT_BSTR:
865 *encoded_type = default_type;
866 *width = pointer_size;
867 *alignment = 4;
868 break;
870 case VT_VARIANT:
871 *encoded_type = default_type;
872 break;
874 case VT_LPSTR:
875 case VT_LPWSTR:
876 *encoded_type = 0xfffe0000 | vt;
877 *width = pointer_size;
878 *alignment = 4;
879 break;
881 case VT_PTR:
883 int next_vt;
884 for(next_vt = 0; is_ptr(type); type = type_pointer_get_ref(type)) {
885 next_vt = get_type_vt(type_pointer_get_ref(type));
886 if (next_vt != 0)
887 break;
889 /* if no type found then it must be void */
890 if (next_vt == 0)
891 next_vt = VT_VOID;
893 encode_type(typelib, next_vt, type_pointer_get_ref(type),
894 &target_type, NULL, NULL, &child_size);
895 /* these types already have an implicit pointer, so we don't need to
896 * add another */
897 if(next_vt == VT_DISPATCH || next_vt == VT_UNKNOWN) {
898 chat("encode_type: skipping ptr\n");
899 *encoded_type = target_type;
900 *width = pointer_size;
901 *alignment = 4;
902 *decoded_size = child_size;
903 break;
906 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
907 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
908 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
911 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
912 int mix_field;
914 if (target_type & 0x80000000) {
915 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
916 } else {
917 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
918 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
921 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
922 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
924 typedata[0] = (mix_field << 16) | VT_PTR;
925 typedata[1] = target_type;
928 *encoded_type = typeoffset;
930 *width = pointer_size;
931 *alignment = 4;
932 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
933 break;
936 case VT_SAFEARRAY:
938 type_t *element_type = type_alias_get_aliasee(type_array_get_element(type));
939 int next_vt = get_type_vt(element_type);
941 encode_type(typelib, next_vt, type_alias_get_aliasee(type_array_get_element(type)), &target_type, NULL, NULL, &child_size);
943 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
944 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
945 if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
948 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
949 int mix_field;
951 if (target_type & 0x80000000) {
952 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
953 } else {
954 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
955 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
958 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
959 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
961 typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
962 typedata[1] = target_type;
965 *encoded_type = typeoffset;
967 *width = pointer_size;
968 *alignment = 4;
969 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
970 break;
973 case VT_USERDEFINED:
975 int typeinfo_offset;
977 /* typedef'd types without public attribute aren't included in the typelib */
978 while (type->typelib_idx < 0 && type_is_alias(type) && !is_attr(type->attrs, ATTR_PUBLIC))
979 type = type_alias_get_aliasee(type);
981 chat("encode_type: VT_USERDEFINED - type %p name = %s real type %d idx %d\n", type,
982 type->name, type_get_type(type), type->typelib_idx);
984 if(type->typelib_idx == -1) {
985 chat("encode_type: trying to ref not added type\n");
986 switch (type_get_type(type)) {
987 case TYPE_STRUCT:
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_COCLASS:
997 add_coclass_typeinfo(typelib, type);
998 break;
999 default:
1000 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
1001 type_get_type(type));
1005 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
1006 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1007 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1008 if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == typeinfo_offset)) break;
1011 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1012 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1013 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1015 typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1016 typedata[1] = typeinfo_offset;
1019 *encoded_type = typeoffset;
1020 *width = 0;
1021 *alignment = 1;
1022 break;
1025 default:
1026 error("encode_type: unrecognized type %d.\n", vt);
1027 *encoded_type = default_type;
1028 *width = 0;
1029 *alignment = 1;
1030 break;
1033 return 0;
1036 static void dump_type(type_t *t)
1038 chat("dump_type: %p name %s type %d attrs %p\n", t, t->name, type_get_type(t), t->attrs);
1041 static int encode_var(
1042 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
1043 type_t *type, /* [I] The type description to encode. */
1044 var_t *var, /* [I] The var to encode. */
1045 int *encoded_type, /* [O] The encoded type description. */
1046 int *width, /* [O] The width of the type, or NULL. */
1047 int *alignment, /* [O] The alignment of the type, or NULL. */
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 (!width) width = &scratch;
1058 if (!alignment) alignment = &scratch;
1059 if (!decoded_size) decoded_size = &scratch;
1060 *decoded_size = 0;
1062 chat("encode_var: var %p type %p type->name %s\n",
1063 var, type, type->name ? type->name : "NULL");
1065 if (is_array(type) && !type_array_is_decl_as_ptr(type)) {
1066 int num_dims, elements = 1, arrayoffset;
1067 type_t *atype;
1068 int *arraydata;
1070 num_dims = 0;
1071 for (atype = type;
1072 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1073 atype = type_array_get_element(atype))
1074 ++num_dims;
1076 chat("array with %d dimensions\n", num_dims);
1077 encode_var(typelib, atype, var, &target_type, width, alignment, NULL);
1078 arrayoffset = ctl2_alloc_segment(typelib, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1079 arraydata = (void *)&typelib->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1081 arraydata[0] = target_type;
1082 arraydata[1] = num_dims;
1083 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1085 arraydata += 2;
1086 for (atype = type;
1087 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1088 atype = type_array_get_element(atype))
1090 arraydata[0] = type_array_get_dim(atype);
1091 arraydata[1] = 0;
1092 arraydata += 2;
1093 elements *= type_array_get_dim(atype);
1096 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1097 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1099 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1100 typedata[1] = arrayoffset;
1102 *encoded_type = typeoffset;
1103 *width = *width * elements;
1104 *decoded_size = 20 /*sizeof(ARRAYDESC)*/ + (num_dims - 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1105 return 0;
1108 vt = get_type_vt(type);
1109 if (vt == VT_PTR) {
1110 type_t *ref = is_ptr(type) ?
1111 type_pointer_get_ref(type) : type_array_get_element(type);
1112 int skip_ptr = encode_var(typelib, ref, var,
1113 &target_type, NULL, NULL, &child_size);
1115 if(skip_ptr == 2) {
1116 chat("encode_var: skipping ptr\n");
1117 *encoded_type = target_type;
1118 *decoded_size = child_size;
1119 *width = pointer_size;
1120 *alignment = 4;
1121 return 0;
1124 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1125 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1126 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1129 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1130 int mix_field;
1132 if (target_type & 0x80000000) {
1133 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1134 } else {
1135 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1136 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1139 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1140 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1142 typedata[0] = (mix_field << 16) | VT_PTR;
1143 typedata[1] = target_type;
1146 *encoded_type = typeoffset;
1148 *width = pointer_size;
1149 *alignment = 4;
1150 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1151 return 0;
1154 dump_type(type);
1156 encode_type(typelib, vt, type, encoded_type, width, alignment, decoded_size);
1157 /* these types already have an implicit pointer, so we don't need to
1158 * add another */
1159 if(vt == VT_DISPATCH || vt == VT_UNKNOWN) return 2;
1160 return 0;
1163 static unsigned int get_ulong_val(unsigned int val, int vt)
1165 switch(vt) {
1166 case VT_I2:
1167 case VT_BOOL:
1168 case VT_UI2:
1169 return val & 0xffff;
1170 case VT_I1:
1171 case VT_UI1:
1172 return val & 0xff;
1175 return val;
1178 static void write_value(msft_typelib_t* typelib, int *out, int vt, const void *value)
1180 switch(vt) {
1181 case VT_I2:
1182 case VT_I4:
1183 case VT_R4:
1184 case VT_BOOL:
1185 case VT_I1:
1186 case VT_UI1:
1187 case VT_UI2:
1188 case VT_UI4:
1189 case VT_INT:
1190 case VT_UINT:
1191 case VT_HRESULT:
1192 case VT_PTR:
1193 case VT_UNKNOWN:
1194 case VT_DISPATCH:
1196 const unsigned int lv = get_ulong_val(*(const unsigned int *)value, vt);
1197 if((lv & 0x3ffffff) == lv) {
1198 *out = 0x80000000;
1199 *out |= vt << 26;
1200 *out |= lv;
1201 } else {
1202 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1203 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1204 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], value, 4);
1205 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1206 *out = offset;
1208 return;
1210 case VT_BSTR:
1212 const char *s = (const char *) value;
1213 int len = strlen(s), seg_len = (len + 6 + 3) & ~0x3;
1214 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1215 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1216 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &len, sizeof(len));
1217 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1218 len += 6;
1219 while(len < seg_len) {
1220 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1221 len++;
1223 *out = offset;
1224 return;
1227 default:
1228 warning("can't write value of type %d yet\n", vt);
1230 return;
1233 static HRESULT set_custdata(msft_typelib_t *typelib, REFGUID guid,
1234 int vt, const void *value, int *offset)
1236 MSFT_GuidEntry guidentry;
1237 int guidoffset;
1238 int custoffset;
1239 int *custdata;
1240 int data_out;
1242 guidentry.guid = *guid;
1244 guidentry.hreftype = -1;
1245 guidentry.next_hash = -1;
1247 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1248 write_value(typelib, &data_out, vt, value);
1250 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1252 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1253 custdata[0] = guidoffset;
1254 custdata[1] = data_out;
1255 custdata[2] = *offset;
1256 *offset = custoffset;
1258 return S_OK;
1261 /* It's possible to have a default value for pointer arguments too.
1262 In this case default value has a referenced type, e.g.
1263 'LONG*' argument gets VT_I4, 'DOUBLE*' - VT_R8. IUnknown* and IDispatch*
1264 are recognised too and stored as VT_UNKNOWN and VT_DISPATCH.
1265 But IUnknown/IDispatch arguments can only have default value of 0
1266 (or expression that resolves to zero) while other pointers can have
1267 any default value. */
1268 static int get_defaultvalue_vt(type_t *type)
1270 int vt = get_type_vt(type);
1271 if (type_get_type(type) == TYPE_ENUM)
1272 vt = VT_I4;
1273 else
1275 vt = get_type_vt(type);
1276 if (vt == VT_PTR && is_ptr(type)) {
1277 vt = get_type_vt(type_pointer_get_ref(type));
1278 /* The only acceptable value for pointers to non-basic types
1279 is NULL, it's stored as VT_I4 for both 32 and 64 bit typelibs. */
1280 if (vt == VT_USERDEFINED)
1281 vt = VT_I4;
1285 return vt;
1288 static HRESULT add_func_desc(msft_typeinfo_t* typeinfo, var_t *func, int index)
1290 int offset, name_offset;
1291 int *typedata, typedata_size;
1292 int i, id, next_idx;
1293 int decoded_size, extra_attr = 0;
1294 int num_params = 0, num_optional = 0, num_defaults = 0;
1295 var_t *arg;
1296 unsigned char *namedata;
1297 const attr_t *attr;
1298 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1299 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1300 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1301 int entry = -1, entry_is_ord = 0;
1302 int lcid_retval_count = 0;
1304 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1306 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1308 switch(typeinfo->typekind) {
1309 case TKIND_DISPATCH:
1310 funckind = 0x4; /* FUNC_DISPATCH */
1311 break;
1312 case TKIND_MODULE:
1313 funckind = 0x3; /* FUNC_STATIC */
1314 break;
1315 default:
1316 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1317 break;
1320 if (is_local( func->attrs )) {
1321 chat("add_func_desc: skipping local function\n");
1322 return S_FALSE;
1325 if (type_get_function_args(func->type))
1326 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1328 num_params++;
1329 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1330 if(attr->type == ATTR_DEFAULTVALUE)
1331 num_defaults++;
1332 else if(attr->type == ATTR_OPTIONAL)
1333 num_optional++;
1337 chat("add_func_desc: num of params %d\n", num_params);
1339 name_offset = ctl2_alloc_name(typeinfo->typelib, func->name);
1341 if (func->attrs) LIST_FOR_EACH_ENTRY( attr, func->attrs, const attr_t, entry ) {
1342 expr_t *expr = attr->u.pval;
1343 switch(attr->type) {
1344 case ATTR_BINDABLE:
1345 funcflags |= 0x4; /* FUNCFLAG_FBINDABLE */
1346 break;
1347 case ATTR_DEFAULTBIND:
1348 funcflags |= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1349 break;
1350 case ATTR_DEFAULTCOLLELEM:
1351 funcflags |= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1352 break;
1353 case ATTR_DISPLAYBIND:
1354 funcflags |= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1355 break;
1356 case ATTR_ENTRY:
1357 extra_attr = max(extra_attr, 3);
1358 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1359 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1360 else {
1361 entry = expr->cval;
1362 entry_is_ord = 1;
1364 break;
1365 case ATTR_HELPCONTEXT:
1366 extra_attr = max(extra_attr, 1);
1367 help_context = expr->u.lval;
1368 break;
1369 case ATTR_HELPSTRING:
1370 extra_attr = max(extra_attr, 2);
1371 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1372 break;
1373 case ATTR_HELPSTRINGCONTEXT:
1374 extra_attr = max(extra_attr, 6);
1375 help_string_context = expr->u.lval;
1376 break;
1377 case ATTR_HIDDEN:
1378 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1379 break;
1380 case ATTR_ID:
1381 id = expr->cval;
1382 break;
1383 case ATTR_IMMEDIATEBIND:
1384 funcflags |= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1385 break;
1386 case ATTR_NONBROWSABLE:
1387 funcflags |= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1388 break;
1389 case ATTR_OUT:
1390 break;
1391 case ATTR_PROPGET:
1392 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1393 break;
1394 case ATTR_PROPPUT:
1395 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1396 break;
1397 case ATTR_PROPPUTREF:
1398 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1399 break;
1400 /* FIXME: FUNCFLAG_FREPLACEABLE */
1401 case ATTR_REQUESTEDIT:
1402 funcflags |= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1403 break;
1404 case ATTR_RESTRICTED:
1405 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1406 break;
1407 case ATTR_SOURCE:
1408 funcflags |= 0x2; /* FUNCFLAG_FSOURCE */
1409 break;
1410 case ATTR_UIDEFAULT:
1411 funcflags |= 0x200; /* FUNCFLAG_FUIDEFAULT */
1412 break;
1413 case ATTR_USESGETLASTERROR:
1414 funcflags |= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1415 break;
1416 case ATTR_VARARG:
1417 if (num_optional || num_defaults)
1418 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1419 else
1420 num_optional = -1;
1421 break;
1422 default:
1423 break;
1427 /* allocate type data space for us */
1428 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1430 if (!typeinfo->func_data) {
1431 typeinfo->func_data = xmalloc(0x100);
1432 typeinfo->func_data_allocated = 0x100;
1433 typeinfo->func_data[0] = 0;
1436 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1437 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1438 typeinfo->func_data[0] + typedata_size + sizeof(int));
1439 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1442 offset = typeinfo->func_data[0];
1443 typeinfo->func_data[0] += typedata_size;
1444 typedata = typeinfo->func_data + (offset >> 2) + 1;
1447 /* find func with the same name - if it exists use its id */
1448 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1449 if(name_offset == typeinfo->func_names[i]) {
1450 id = typeinfo->func_indices[i];
1451 break;
1455 /* find the first func with the same id and link via the hiword of typedata[4] */
1456 next_idx = index;
1457 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1458 if(id == typeinfo->func_indices[i]) {
1459 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1460 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1461 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1462 break;
1466 /* fill out the basic type information */
1467 typedata[0] = typedata_size | (index << 16);
1468 encode_var(typeinfo->typelib, type_function_get_rettype(func->type), func, &typedata[1], NULL, NULL, &decoded_size);
1469 typedata[2] = funcflags;
1470 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1471 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1472 if(num_defaults) typedata[4] |= 0x1000;
1473 if(entry_is_ord) typedata[4] |= 0x2000;
1474 typedata[5] = (num_optional << 16) | num_params;
1476 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1477 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1478 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1479 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1481 switch(extra_attr) {
1482 case 6: typedata[11] = help_string_context;
1483 case 5: typedata[10] = -1;
1484 case 4: typedata[9] = -1;
1485 case 3: typedata[8] = entry;
1486 case 2: typedata[7] = help_string_offset;
1487 case 1: typedata[6] = help_context;
1488 case 0:
1489 break;
1490 default:
1491 warning("unknown number of optional attrs\n");
1494 if (type_get_function_args(func->type))
1496 i = 0;
1497 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1499 int paramflags = 0;
1500 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1501 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1503 if(defaultdata) *defaultdata = -1;
1505 encode_var(typeinfo->typelib, arg->type, arg, paramdata, NULL, NULL, &decoded_size);
1506 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1507 switch(attr->type) {
1508 case ATTR_DEFAULTVALUE:
1510 int vt;
1511 expr_t *expr = (expr_t *)attr->u.pval;
1512 vt = get_defaultvalue_vt(arg->type);
1513 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1514 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1516 if (vt != VT_BSTR) error("string default value applied to non-string type\n");
1517 chat("default value '%s'\n", expr->u.sval);
1518 write_value(typeinfo->typelib, defaultdata, vt, expr->u.sval);
1520 else
1522 chat("default value %d\n", expr->cval);
1523 write_value(typeinfo->typelib, defaultdata, vt, &expr->cval);
1525 break;
1527 case ATTR_IN:
1528 paramflags |= 0x01; /* PARAMFLAG_FIN */
1529 break;
1530 case ATTR_OPTIONAL:
1531 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1532 break;
1533 case ATTR_OUT:
1534 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1535 break;
1536 case ATTR_PARAMLCID:
1537 paramflags |= 0x04; /* PARAMFLAG_LCID */
1538 lcid_retval_count++;
1539 break;
1540 case ATTR_RETVAL:
1541 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1542 lcid_retval_count++;
1543 break;
1544 default:
1545 chat("unhandled param attr %d\n", attr->type);
1546 break;
1549 paramdata[1] = -1;
1550 paramdata[2] = paramflags;
1551 typedata[3] += decoded_size << 16;
1553 i++;
1557 if(lcid_retval_count == 1)
1558 typedata[4] |= 0x4000;
1559 else if(lcid_retval_count == 2)
1560 typedata[4] |= 0x8000;
1562 if(typeinfo->funcs_allocated == 0) {
1563 typeinfo->funcs_allocated = 10;
1564 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1565 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1566 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1568 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1569 typeinfo->funcs_allocated *= 2;
1570 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1571 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1572 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1575 /* update the index data */
1576 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1577 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1578 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1580 /* ??? */
1581 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1582 typeinfo->typeinfo->res2 <<= 1;
1583 /* ??? */
1584 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1586 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1587 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1588 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1590 /* adjust size of VTBL */
1591 if(funckind != 0x3 /* FUNC_STATIC */)
1592 typeinfo->typeinfo->cbSizeVft += pointer_size;
1594 /* Increment the number of function elements */
1595 typeinfo->typeinfo->cElement += 1;
1597 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1598 if (*((INT *)namedata) == -1) {
1599 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1600 if(typeinfo->typekind == TKIND_MODULE)
1601 namedata[9] |= 0x10;
1602 } else
1603 namedata[9] &= ~0x10;
1605 if(typeinfo->typekind == TKIND_MODULE)
1606 namedata[9] |= 0x20;
1608 if (type_get_function_args(func->type))
1610 i = 0;
1611 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1613 /* don't give the last arg of a [propput*] func a name */
1614 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1616 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1617 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1618 paramdata[1] = offset;
1620 i++;
1623 return S_OK;
1626 static HRESULT add_var_desc(msft_typeinfo_t *typeinfo, UINT index, var_t* var)
1628 int offset, id;
1629 unsigned int typedata_size;
1630 INT *typedata;
1631 int var_datawidth;
1632 int var_alignment;
1633 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1634 int alignment;
1635 int varflags = 0;
1636 const attr_t *attr;
1637 unsigned char *namedata;
1638 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1640 chat("add_var_desc(%d, %s)\n", index, var->name);
1642 id = 0x40000000 + index;
1644 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1645 expr_t *expr = attr->u.pval;
1646 switch(attr->type) {
1647 case ATTR_BINDABLE:
1648 varflags |= 0x04; /* VARFLAG_FBINDABLE */
1649 break;
1650 case ATTR_DEFAULTBIND:
1651 varflags |= 0x20; /* VARFLAG_FDEFAULTBIND */
1652 break;
1653 case ATTR_DEFAULTCOLLELEM:
1654 varflags |= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1655 break;
1656 case ATTR_DISPLAYBIND:
1657 varflags |= 0x10; /* VARFLAG_FDISPLAYBIND */
1658 break;
1659 case ATTR_HIDDEN:
1660 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1661 break;
1662 case ATTR_ID:
1663 id = expr->cval;
1664 break;
1665 case ATTR_IMMEDIATEBIND:
1666 varflags |= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1667 break;
1668 case ATTR_NONBROWSABLE:
1669 varflags |= 0x400; /* VARFLAG_FNONBROWSABLE */
1670 break;
1671 case ATTR_READONLY:
1672 varflags |= 0x01; /* VARFLAG_FREADONLY */
1673 break;
1674 /* FIXME: VARFLAG_FREPLACEABLE */
1675 case ATTR_REQUESTEDIT:
1676 varflags |= 0x08; /* VARFLAG_FREQUESTEDIT */
1677 break;
1678 case ATTR_RESTRICTED:
1679 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1680 break;
1681 case ATTR_SOURCE:
1682 varflags |= 0x02; /* VARFLAG_FSOURCE */
1683 break;
1684 case ATTR_UIDEFAULT:
1685 varflags |= 0x0200; /* VARFLAG_FUIDEFAULT */
1686 break;
1687 default:
1688 break;
1692 /* allocate type data space for us */
1693 typedata_size = 0x14;
1695 if (!typeinfo->var_data) {
1696 typeinfo->var_data = xmalloc(0x100);
1697 typeinfo->var_data_allocated = 0x100;
1698 typeinfo->var_data[0] = 0;
1701 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1702 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1703 typeinfo->var_data[0] + typedata_size + sizeof(int));
1704 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1707 offset = typeinfo->var_data[0];
1708 typeinfo->var_data[0] += typedata_size;
1709 typedata = typeinfo->var_data + (offset >> 2) + 1;
1711 /* fill out the basic type information */
1712 typedata[0] = typedata_size | (index << 16);
1713 typedata[2] = varflags;
1714 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1716 if(typeinfo->vars_allocated == 0) {
1717 typeinfo->vars_allocated = 10;
1718 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1719 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1720 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1722 if(typeinfo->vars_allocated == var_num) {
1723 typeinfo->vars_allocated *= 2;
1724 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1725 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1726 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1728 /* update the index data */
1729 typeinfo->var_indices[var_num] = id;
1730 typeinfo->var_names[var_num] = -1;
1731 typeinfo->var_offsets[var_num] = offset;
1733 /* figure out type widths and whatnot */
1734 encode_var(typeinfo->typelib, var->type, var, &typedata[1], &var_datawidth,
1735 &var_alignment, &var_type_size);
1737 /* pad out starting position to data width */
1738 typeinfo->datawidth += var_alignment - 1;
1739 typeinfo->datawidth &= ~(var_alignment - 1);
1741 switch(typeinfo->typekind) {
1742 case TKIND_ENUM:
1743 write_value(typeinfo->typelib, &typedata[4], VT_I4, &var->eval->cval);
1744 var_kind = 2; /* VAR_CONST */
1745 var_type_size += 16; /* sizeof(VARIANT) */
1746 typeinfo->datawidth = var_datawidth;
1747 break;
1748 case TKIND_RECORD:
1749 typedata[4] = typeinfo->datawidth;
1750 typeinfo->datawidth += var_datawidth;
1751 break;
1752 case TKIND_DISPATCH:
1753 var_kind = 3; /* VAR_DISPATCH */
1754 typeinfo->datawidth = pointer_size;
1755 var_alignment = 4;
1756 break;
1757 default:
1758 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1759 break;
1762 /* add type description size to total required allocation */
1763 typedata[3] += var_type_size << 16 | var_kind;
1765 /* fix type alignment */
1766 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1767 if (alignment < var_alignment) {
1768 alignment = var_alignment;
1769 typeinfo->typeinfo->typekind &= ~0xffc0;
1770 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1773 /* ??? */
1774 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1775 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1776 typeinfo->typeinfo->res2 <<= 1;
1779 /* ??? */
1780 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1781 typeinfo->typeinfo->res3 += 0x2c;
1783 /* increment the number of variable elements */
1784 typeinfo->typeinfo->cElement += 0x10000;
1786 /* pad data width to alignment */
1787 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1789 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1790 if (offset == -1) return E_OUTOFMEMORY;
1792 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1793 if (*((INT *)namedata) == -1) {
1794 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1795 if(typeinfo->typekind != TKIND_DISPATCH)
1796 namedata[9] |= 0x10;
1797 } else
1798 namedata[9] &= ~0x10;
1800 if (typeinfo->typekind == TKIND_ENUM) {
1801 namedata[9] |= 0x20;
1803 typeinfo->var_names[var_num] = offset;
1805 return S_OK;
1808 static HRESULT add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1810 if(importinfo) {
1811 alloc_importinfo(typeinfo->typelib, importinfo);
1812 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1813 }else {
1814 if(ref->typelib_idx == -1)
1815 add_interface_typeinfo(typeinfo->typelib, ref);
1816 if(ref->typelib_idx == -1)
1817 error("add_impl_type: unable to add inherited interface\n");
1819 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1822 typeinfo->typeinfo->cImplTypes++;
1823 return S_OK;
1826 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1827 const char *name, const attr_list_t *attrs)
1829 const attr_t *attr;
1830 msft_typeinfo_t *msft_typeinfo;
1831 int nameoffset;
1832 int typeinfo_offset;
1833 MSFT_TypeInfoBase *typeinfo;
1834 MSFT_GuidEntry guidentry;
1836 chat("create_msft_typeinfo: name %s kind %d\n", name, kind);
1838 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1839 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1841 msft_typeinfo->typelib = typelib;
1843 nameoffset = ctl2_alloc_name(typelib, name);
1844 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1845 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1847 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1848 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1850 msft_typeinfo->typekind = kind;
1851 msft_typeinfo->typeinfo = typeinfo;
1853 typeinfo->typekind |= kind | 0x20;
1855 if(kind == TKIND_COCLASS)
1856 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1858 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1859 switch(attr->type) {
1860 case ATTR_AGGREGATABLE:
1861 if (kind == TKIND_COCLASS)
1862 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1863 break;
1865 case ATTR_APPOBJECT:
1866 if (kind == TKIND_COCLASS)
1867 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1868 break;
1870 case ATTR_CONTROL:
1871 if (kind == TKIND_COCLASS)
1872 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1873 break;
1875 case ATTR_DLLNAME:
1877 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1878 typeinfo->datatype1 = offset;
1879 break;
1882 case ATTR_DUAL:
1883 /* FIXME: check interface is compatible */
1884 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1885 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1886 break;
1888 case ATTR_HELPCONTEXT:
1890 expr_t *expr = (expr_t*)attr->u.pval;
1891 typeinfo->helpcontext = expr->cval;
1892 break;
1894 case ATTR_HELPSTRING:
1896 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1897 if (offset == -1) break;
1898 typeinfo->docstringoffs = offset;
1899 break;
1901 case ATTR_HELPSTRINGCONTEXT:
1903 expr_t *expr = (expr_t*)attr->u.pval;
1904 typeinfo->helpstringcontext = expr->cval;
1905 break;
1907 case ATTR_HIDDEN:
1908 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1909 break;
1911 case ATTR_LICENSED:
1912 typeinfo->flags |= 0x04; /* TYPEFLAG_FLICENSED */
1913 break;
1915 case ATTR_NONCREATABLE:
1916 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1917 break;
1919 case ATTR_NONEXTENSIBLE:
1920 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1921 break;
1923 case ATTR_OLEAUTOMATION:
1924 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1925 break;
1927 /* FIXME: TYPEFLAG_FPREDCLID */
1929 case ATTR_PROXY:
1930 typeinfo->flags |= 0x4000; /* TYPEFLAG_FPROXY */
1931 break;
1933 /* FIXME: TYPEFLAG_FREPLACEABLE */
1935 case ATTR_RESTRICTED:
1936 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
1937 break;
1939 case ATTR_UUID:
1940 guidentry.guid = *(GUID*)attr->u.pval;
1941 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1942 guidentry.next_hash = -1;
1943 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
1944 #if 0
1945 if (IsEqualIID(guid, &IID_IDispatch)) {
1946 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1948 #endif
1949 break;
1951 case ATTR_VERSION:
1952 typeinfo->version = attr->u.ival;
1953 break;
1955 default:
1956 break;
1960 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
1961 typelib->last_typeinfo = msft_typeinfo;
1962 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
1965 return msft_typeinfo;
1968 static void add_dispatch(msft_typelib_t *typelib)
1970 int guid_offset, impfile_offset;
1971 MSFT_GuidEntry guidentry;
1972 MSFT_ImpInfo impinfo;
1973 GUID stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1974 GUID iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1976 if(typelib->typelib_header.dispatchpos != -1) return;
1978 guidentry.guid = stdole;
1979 guidentry.hreftype = 2;
1980 guidentry.next_hash = -1;
1981 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
1982 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
1984 guidentry.guid = iid_idispatch;
1985 guidentry.hreftype = 1;
1986 guidentry.next_hash = -1;
1987 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
1988 impinfo.oImpFile = impfile_offset;
1989 impinfo.oGuid = ctl2_alloc_guid(typelib, &guidentry);
1990 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
1993 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
1995 int idx = 0;
1996 var_t *func;
1997 var_t *var;
1998 msft_typeinfo_t *msft_typeinfo;
2000 if (-1 < dispinterface->typelib_idx)
2001 return;
2003 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2004 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
2005 dispinterface->attrs);
2007 msft_typeinfo->typeinfo->size = pointer_size;
2008 msft_typeinfo->typeinfo->typekind |= 0x2100;
2010 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2011 add_dispatch(typelib);
2012 msft_typeinfo->typeinfo->cImplTypes = 1;
2014 /* count the no of methods, as the variable indices come after the funcs */
2015 if (dispinterface->details.iface->disp_methods)
2016 LIST_FOR_EACH_ENTRY( func, dispinterface->details.iface->disp_methods, var_t, entry )
2017 idx++;
2019 if (type_dispiface_get_props(dispinterface))
2020 LIST_FOR_EACH_ENTRY( var, type_dispiface_get_props(dispinterface), var_t, entry )
2021 add_var_desc(msft_typeinfo, idx++, var);
2023 if (type_dispiface_get_methods(dispinterface))
2025 idx = 0;
2026 LIST_FOR_EACH_ENTRY( func, type_dispiface_get_methods(dispinterface), var_t, entry )
2027 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2028 idx++;
2032 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
2034 int idx = 0;
2035 const statement_t *stmt_func;
2036 type_t *ref;
2037 msft_typeinfo_t *msft_typeinfo;
2038 importinfo_t *ref_importinfo = NULL;
2039 int num_parents = 0, num_funcs = 0;
2040 type_t *inherit;
2041 const type_t *derived;
2043 if (-1 < interface->typelib_idx)
2044 return;
2046 if (!interface->details.iface)
2048 error( "interface %s is referenced but not defined\n", interface->name );
2049 return;
2052 if (is_attr(interface->attrs, ATTR_DISPINTERFACE)) {
2053 add_dispinterface_typeinfo(typelib, interface);
2054 return;
2057 /* midl adds the parent interface first, unless the parent itself
2058 has no parent (i.e. it stops before IUnknown). */
2060 inherit = type_iface_get_inherit(interface);
2062 if(inherit) {
2063 ref_importinfo = find_importinfo(typelib, inherit->name);
2065 if(!ref_importinfo && type_iface_get_inherit(inherit) &&
2066 inherit->typelib_idx == -1)
2067 add_interface_typeinfo(typelib, inherit);
2070 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2071 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
2072 msft_typeinfo->typeinfo->size = pointer_size;
2073 msft_typeinfo->typeinfo->typekind |= 0x2200;
2075 for (derived = inherit; derived; derived = type_iface_get_inherit(derived))
2076 if (derived->name && !strcmp(derived->name, "IDispatch"))
2077 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2079 /* can't be dual if it doesn't derive from IDispatch */
2080 if (!(msft_typeinfo->typeinfo->flags & 0x1000)) /* TYPEFLAG_FDISPATCHABLE */
2081 msft_typeinfo->typeinfo->flags &= ~0x40; /* TYPEFLAG_FDUAL */
2083 if(type_iface_get_inherit(interface))
2084 add_impl_type(msft_typeinfo, type_iface_get_inherit(interface),
2085 ref_importinfo);
2087 /* count the number of inherited interfaces and non-local functions */
2088 for(ref = inherit; ref; ref = type_iface_get_inherit(ref)) {
2089 num_parents++;
2090 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) ) {
2091 var_t *func = stmt_func->u.var;
2092 if (!is_local(func->attrs)) num_funcs++;
2095 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2096 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2098 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(interface) ) {
2099 var_t *func = stmt_func->u.var;
2100 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2101 idx++;
2105 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2107 int idx = 0;
2108 var_t *cur;
2109 msft_typeinfo_t *msft_typeinfo;
2111 if (-1 < structure->typelib_idx)
2112 return;
2114 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2115 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2116 msft_typeinfo->typeinfo->size = 0;
2118 if (type_struct_get_fields(structure))
2119 LIST_FOR_EACH_ENTRY( cur, type_struct_get_fields(structure), var_t, entry )
2120 add_var_desc(msft_typeinfo, idx++, cur);
2123 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2125 int idx = 0;
2126 var_t *cur;
2127 msft_typeinfo_t *msft_typeinfo;
2129 if (-1 < enumeration->typelib_idx)
2130 return;
2132 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2133 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2134 msft_typeinfo->typeinfo->size = 0;
2136 if (type_enum_get_values(enumeration))
2137 LIST_FOR_EACH_ENTRY( cur, type_enum_get_values(enumeration), var_t, entry )
2138 add_var_desc(msft_typeinfo, idx++, cur);
2141 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2143 msft_typeinfo_t *msft_typeinfo;
2144 int alignment;
2146 if (-1 < tdef->typelib_idx)
2147 return;
2149 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2150 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2151 encode_type(typelib, get_type_vt(type_alias_get_aliasee(tdef)),
2152 type_alias_get_aliasee(tdef),
2153 &msft_typeinfo->typeinfo->datatype1,
2154 &msft_typeinfo->typeinfo->size,
2155 &alignment, &msft_typeinfo->typeinfo->datatype2);
2156 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2159 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2161 msft_typeinfo_t *msft_typeinfo;
2162 ifref_t *iref;
2163 int num_ifaces = 0, offset, i;
2164 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2165 int have_default = 0, have_default_source = 0;
2166 const attr_t *attr;
2167 ifref_list_t *ifaces;
2169 if (-1 < cls->typelib_idx)
2170 return;
2172 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2173 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2175 ifaces = type_coclass_get_ifaces(cls);
2176 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) num_ifaces++;
2178 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2179 num_ifaces * sizeof(*ref), 0);
2181 i = 0;
2182 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) {
2183 if(iref->iface->typelib_idx == -1)
2184 add_interface_typeinfo(typelib, iref->iface);
2185 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2186 ref->reftype = typelib->typelib_typeinfo_offsets[iref->iface->typelib_idx];
2187 ref->flags = 0;
2188 ref->oCustData = -1;
2189 ref->onext = -1;
2190 if(i < num_ifaces - 1)
2191 ref->onext = offset + (i + 1) * sizeof(*ref);
2193 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2194 switch(attr->type) {
2195 case ATTR_DEFAULT:
2196 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2197 break;
2198 case ATTR_DEFAULTVTABLE:
2199 ref->flags |= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2200 break;
2201 case ATTR_RESTRICTED:
2202 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2203 break;
2204 case ATTR_SOURCE:
2205 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2206 break;
2207 default:
2208 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2211 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2212 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2213 have_default_source = 1;
2214 else
2215 have_default = 1;
2218 /* If the interface is non-restricted and we haven't already had one then
2219 remember it so that we can use it as a default later */
2220 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2221 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2222 if(!first_source)
2223 first_source = ref;
2225 else if(!first)
2226 first = ref;
2228 i++;
2231 /* If we haven't had a default interface, then set the default flags on the
2232 first ones */
2233 if(!have_default && first)
2234 first->flags |= 0x1;
2235 if(!have_default_source && first_source)
2236 first_source->flags |= 0x1;
2238 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2239 msft_typeinfo->typeinfo->size = pointer_size;
2240 msft_typeinfo->typeinfo->typekind |= 0x2200;
2243 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2245 int idx = 0;
2246 const statement_t *stmt;
2247 msft_typeinfo_t *msft_typeinfo;
2249 if (-1 < module->typelib_idx)
2250 return;
2252 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2253 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2254 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2256 STATEMENTS_FOR_EACH_FUNC( stmt, module->details.module->stmts ) {
2257 var_t *func = stmt->u.var;
2258 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2259 idx++;
2262 msft_typeinfo->typeinfo->size = idx;
2265 static void add_type_typeinfo(msft_typelib_t *typelib, type_t *type)
2267 switch (type_get_type(type)) {
2268 case TYPE_INTERFACE:
2269 add_interface_typeinfo(typelib, type);
2270 break;
2271 case TYPE_STRUCT:
2272 add_structure_typeinfo(typelib, type);
2273 break;
2274 case TYPE_ENUM:
2275 add_enum_typeinfo(typelib, type);
2276 break;
2277 case TYPE_COCLASS:
2278 add_coclass_typeinfo(typelib, type);
2279 break;
2280 case TYPE_BASIC:
2281 case TYPE_POINTER:
2282 break;
2283 default:
2284 error("add_entry: unhandled type 0x%x for %s\n",
2285 type_get_type(type), type->name);
2286 break;
2290 static void add_entry(msft_typelib_t *typelib, const statement_t *stmt)
2292 switch(stmt->type) {
2293 case STMT_LIBRARY:
2294 case STMT_IMPORT:
2295 case STMT_PRAGMA:
2296 case STMT_CPPQUOTE:
2297 case STMT_DECLARATION:
2298 /* not included in typelib */
2299 break;
2300 case STMT_IMPORTLIB:
2301 /* not processed here */
2302 break;
2303 case STMT_TYPEDEF:
2305 const type_list_t *type_entry = stmt->u.type_list;
2306 for (; type_entry; type_entry = type_entry->next) {
2307 /* if the type is public then add the typedef, otherwise attempt
2308 * to add the aliased type */
2309 if (is_attr(type_entry->type->attrs, ATTR_PUBLIC))
2310 add_typedef_typeinfo(typelib, type_entry->type);
2311 else
2312 add_type_typeinfo(typelib, type_alias_get_aliasee(type_entry->type));
2314 break;
2316 case STMT_MODULE:
2317 add_module_typeinfo(typelib, stmt->u.type);
2318 break;
2319 case STMT_TYPE:
2320 case STMT_TYPEREF:
2322 type_t *type = stmt->u.type;
2323 add_type_typeinfo(typelib, type);
2324 break;
2329 static void set_name(msft_typelib_t *typelib)
2331 int offset;
2333 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2334 if (offset == -1) return;
2335 typelib->typelib_header.NameOffset = offset;
2336 return;
2339 static void set_version(msft_typelib_t *typelib)
2341 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2344 static void set_guid(msft_typelib_t *typelib)
2346 MSFT_GuidEntry guidentry;
2347 int offset;
2348 void *ptr;
2349 GUID guid = {0,0,0,{0,0,0,0,0,0}};
2351 guidentry.guid = guid;
2352 guidentry.hreftype = -2;
2353 guidentry.next_hash = -1;
2355 ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2356 if (ptr) guidentry.guid = *(GUID *)ptr;
2358 offset = ctl2_alloc_guid(typelib, &guidentry);
2359 typelib->typelib_header.posguid = offset;
2361 return;
2364 static void set_doc_string(msft_typelib_t *typelib)
2366 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2368 if (str)
2370 int offset = ctl2_alloc_string(typelib, str);
2371 if (offset != -1) typelib->typelib_header.helpstring = offset;
2375 static void set_help_file_name(msft_typelib_t *typelib)
2377 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2379 if (str)
2381 int offset = ctl2_alloc_string(typelib, str);
2382 if (offset != -1)
2384 typelib->typelib_header.helpfile = offset;
2385 typelib->typelib_header.varflags |= 0x10;
2390 static void set_help_context(msft_typelib_t *typelib)
2392 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2393 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2396 static void set_help_string_dll(msft_typelib_t *typelib)
2398 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2400 if (str)
2402 int offset = ctl2_alloc_string(typelib, str);
2403 if (offset != -1)
2405 typelib->help_string_dll_offset = offset;
2406 typelib->typelib_header.varflags |= 0x100;
2411 static void set_help_string_context(msft_typelib_t *typelib)
2413 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2414 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2417 static void set_lcid(msft_typelib_t *typelib)
2419 const expr_t *lcid_expr = get_attrp( typelib->typelib->attrs, ATTR_LIBLCID );
2420 if(lcid_expr)
2422 typelib->typelib_header.lcid = lcid_expr->cval;
2423 typelib->typelib_header.lcid2 = lcid_expr->cval;
2427 static void set_lib_flags(msft_typelib_t *typelib)
2429 const attr_t *attr;
2431 typelib->typelib_header.flags = 0;
2432 if (!typelib->typelib->attrs) return;
2433 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2435 switch(attr->type) {
2436 case ATTR_CONTROL:
2437 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2438 break;
2439 case ATTR_HIDDEN:
2440 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2441 break;
2442 case ATTR_RESTRICTED:
2443 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2444 break;
2445 default:
2446 break;
2449 return;
2452 static void ctl2_write_segment(msft_typelib_t *typelib, int segment)
2454 put_data(typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length);
2457 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2459 msft_typeinfo_t *typeinfo;
2461 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2462 typeinfo->typeinfo->memoffset = filesize;
2463 if (typeinfo->func_data)
2464 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2465 if (typeinfo->var_data)
2466 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2467 if (typeinfo->func_data || typeinfo->var_data)
2468 filesize += 4;
2472 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2474 if (typelib->typelib_segdir[segment].length) {
2475 typelib->typelib_segdir[segment].offset = filepos;
2476 } else {
2477 typelib->typelib_segdir[segment].offset = -1;
2480 return typelib->typelib_segdir[segment].length;
2484 static void ctl2_write_typeinfos(msft_typelib_t *typelib)
2486 msft_typeinfo_t *typeinfo;
2487 int typedata_size;
2489 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2490 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2491 typedata_size = 0;
2492 if (typeinfo->func_data)
2493 typedata_size = typeinfo->func_data[0];
2494 if (typeinfo->var_data)
2495 typedata_size += typeinfo->var_data[0];
2496 put_data(&typedata_size, sizeof(int));
2497 if (typeinfo->func_data)
2498 put_data(typeinfo->func_data + 1, typeinfo->func_data[0]);
2499 if (typeinfo->var_data)
2500 put_data(typeinfo->var_data + 1, typeinfo->var_data[0]);
2501 if (typeinfo->func_indices)
2502 put_data(typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2503 if (typeinfo->var_indices)
2504 put_data(typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2505 if (typeinfo->func_names)
2506 put_data(typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2507 if (typeinfo->var_names)
2508 put_data(typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2509 if (typeinfo->func_offsets)
2510 put_data(typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2511 if (typeinfo->var_offsets) {
2512 int add = 0, i, offset;
2513 if(typeinfo->func_data)
2514 add = typeinfo->func_data[0];
2515 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2516 offset = typeinfo->var_offsets[i];
2517 offset += add;
2518 put_data(&offset, 4);
2524 static void save_all_changes(msft_typelib_t *typelib)
2526 int filepos;
2528 chat("save_all_changes(%p)\n", typelib);
2530 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2531 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2532 filepos += typelib->typelib_header.nrtypeinfos * 4;
2534 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2535 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2536 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2537 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2538 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2539 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2540 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2541 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2542 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2543 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2544 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2545 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2546 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2548 ctl2_finalize_typeinfos(typelib, filepos);
2550 byte_swapped = 0;
2551 init_output_buffer();
2553 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2554 if(typelib->typelib_header.varflags & 0x100)
2555 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2557 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2558 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2559 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2560 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2561 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2562 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2563 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2564 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2565 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2566 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2567 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2568 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2569 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2570 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2571 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2573 ctl2_write_typeinfos(typelib);
2575 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2577 char typelib_id[13] = "#1";
2579 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2580 if (expr)
2581 sprintf( typelib_id, "#%d", expr->cval );
2582 add_output_to_resources( "TYPELIB", typelib_id );
2583 output_typelib_regscript( typelib->typelib );
2584 flush_output_resources( typelib_name );
2586 else flush_output_buffer( typelib_name );
2589 int create_msft_typelib(typelib_t *typelib)
2591 msft_typelib_t *msft;
2592 int failed = 0;
2593 const statement_t *stmt;
2594 time_t cur_time;
2595 char *time_override;
2596 unsigned int version = 7 << 24 | 555; /* 7.00.0555 */
2597 GUID midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2598 GUID midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2599 GUID midl_info_guid = {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2600 char info_string[128];
2602 pointer_size = (typelib_kind == SYS_WIN64) ? 8 : 4;
2604 msft = xmalloc(sizeof(*msft));
2605 memset(msft, 0, sizeof(*msft));
2606 msft->typelib = typelib;
2608 ctl2_init_header(msft);
2609 ctl2_init_segdir(msft);
2611 msft->typelib_header.varflags |= typelib_kind;
2614 * The following two calls return an offset or -1 if out of memory. We
2615 * specifically need an offset of 0, however, so...
2617 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2618 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2620 if(failed)
2622 free(msft);
2623 return 0;
2626 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2627 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2629 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2630 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2632 set_lib_flags(msft);
2633 set_lcid(msft);
2634 set_help_file_name(msft);
2635 set_doc_string(msft);
2636 set_guid(msft);
2637 set_version(msft);
2638 set_name(msft);
2639 set_help_context(msft);
2640 set_help_string_dll(msft);
2641 set_help_string_context(msft);
2643 /* midl adds two sets of custom data to the library: the current unix time
2644 and midl's version number */
2645 time_override = getenv( "WIDL_TIME_OVERRIDE");
2646 cur_time = time_override ? atol( time_override) : time(NULL);
2647 sprintf(info_string, "Created by WIDL version %s at %s\n", PACKAGE_VERSION, ctime(&cur_time));
2648 set_custdata(msft, &midl_info_guid, VT_BSTR, info_string, &msft->typelib_header.CustomDataOffset);
2649 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2650 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2652 if (typelib->stmts)
2653 LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
2654 add_entry(msft, stmt);
2656 save_all_changes(msft);
2657 free(msft);
2658 return 1;