widl: Avoid adding duplicate type definitions if tag and typedef names are the same.
[wine/multimedia.git] / tools / widl / write_msft.c
blob3cbae92a10243aaa33225523769b6c1284115f97
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_union_typeinfo(msft_typelib_t *typelib, type_t *tunion);
749 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls);
750 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
753 /****************************************************************************
754 * encode_type
756 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
757 * segments as needed.
759 * RETURNS
761 * Success: 0.
762 * Failure: -1.
764 static int encode_type(
765 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
766 int vt, /* [I] vt to encode */
767 type_t *type, /* [I] type */
768 int *encoded_type, /* [O] The encoded type description. */
769 int *width, /* [O] The width of the type, or NULL. */
770 int *alignment, /* [O] The alignment of the type, or NULL. */
771 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
773 int default_type;
774 int scratch;
775 int typeoffset;
776 int *typedata;
777 int target_type;
778 int child_size = 0;
780 chat("encode_type vt %d type %p\n", vt, type);
782 default_type = 0x80000000 | (vt << 16) | vt;
783 if (!width) width = &scratch;
784 if (!alignment) alignment = &scratch;
785 if (!decoded_size) decoded_size = &scratch;
788 switch (vt) {
789 case VT_I1:
790 case VT_UI1:
791 *encoded_type = default_type;
792 *width = 1;
793 *alignment = 1;
794 break;
796 case VT_INT:
797 *encoded_type = 0x80000000 | (VT_I4 << 16) | VT_INT;
798 if ((typelib->typelib_header.varflags & 0x0f) == SYS_WIN16) {
799 *width = 2;
800 *alignment = 2;
801 } else {
802 *width = 4;
803 *alignment = 4;
805 break;
807 case VT_UINT:
808 *encoded_type = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
809 if ((typelib->typelib_header.varflags & 0x0f) == SYS_WIN16) {
810 *width = 2;
811 *alignment = 2;
812 } else {
813 *width = 4;
814 *alignment = 4;
816 break;
818 case VT_UI2:
819 case VT_I2:
820 case VT_BOOL:
821 *encoded_type = default_type;
822 *width = 2;
823 *alignment = 2;
824 break;
826 case VT_I4:
827 case VT_UI4:
828 case VT_R4:
829 case VT_ERROR:
830 case VT_HRESULT:
831 *encoded_type = default_type;
832 *width = 4;
833 *alignment = 4;
834 break;
836 case VT_R8:
837 case VT_I8:
838 case VT_UI8:
839 *encoded_type = default_type;
840 *width = 8;
841 *alignment = 8;
842 break;
844 case VT_CY:
845 case VT_DATE:
846 *encoded_type = default_type;
847 *width = 8;
848 *alignment = 8;
849 break;
851 case VT_DECIMAL:
852 *encoded_type = default_type;
853 *width = 16;
854 *alignment = 8;
855 break;
857 case VT_VOID:
858 *encoded_type = 0x80000000 | (VT_EMPTY << 16) | vt;
859 *width = 0;
860 *alignment = 1;
861 break;
863 case VT_UNKNOWN:
864 case VT_DISPATCH:
865 case VT_BSTR:
866 *encoded_type = default_type;
867 *width = pointer_size;
868 *alignment = 4;
869 break;
871 case VT_VARIANT:
872 *encoded_type = default_type;
873 break;
875 case VT_LPSTR:
876 case VT_LPWSTR:
877 *encoded_type = 0xfffe0000 | vt;
878 *width = pointer_size;
879 *alignment = 4;
880 break;
882 case VT_PTR:
884 int next_vt;
885 for(next_vt = 0; is_ptr(type); type = type_pointer_get_ref(type)) {
886 next_vt = get_type_vt(type_pointer_get_ref(type));
887 if (next_vt != 0)
888 break;
890 /* if no type found then it must be void */
891 if (next_vt == 0)
892 next_vt = VT_VOID;
894 encode_type(typelib, next_vt, type_pointer_get_ref(type),
895 &target_type, NULL, NULL, &child_size);
896 /* these types already have an implicit pointer, so we don't need to
897 * add another */
898 if(next_vt == VT_DISPATCH || next_vt == VT_UNKNOWN) {
899 chat("encode_type: skipping ptr\n");
900 *encoded_type = target_type;
901 *width = pointer_size;
902 *alignment = 4;
903 *decoded_size = child_size;
904 break;
907 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
908 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
909 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
912 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
913 int mix_field;
915 if (target_type & 0x80000000) {
916 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
917 } else {
918 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
919 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
922 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
923 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
925 typedata[0] = (mix_field << 16) | VT_PTR;
926 typedata[1] = target_type;
929 *encoded_type = typeoffset;
931 *width = pointer_size;
932 *alignment = 4;
933 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
934 break;
937 case VT_SAFEARRAY:
939 type_t *element_type = type_alias_get_aliasee(type_array_get_element(type));
940 int next_vt = get_type_vt(element_type);
942 encode_type(typelib, next_vt, type_alias_get_aliasee(type_array_get_element(type)), &target_type, NULL, NULL, &child_size);
944 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
945 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
946 if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
949 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
950 int mix_field;
952 if (target_type & 0x80000000) {
953 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
954 } else {
955 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
956 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
959 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
960 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
962 typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
963 typedata[1] = target_type;
966 *encoded_type = typeoffset;
968 *width = pointer_size;
969 *alignment = 4;
970 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
971 break;
974 case VT_USERDEFINED:
976 int typeinfo_offset;
978 /* typedef'd types without public attribute aren't included in the typelib */
979 while (type->typelib_idx < 0 && type_is_alias(type) && !is_attr(type->attrs, ATTR_PUBLIC))
980 type = type_alias_get_aliasee(type);
982 chat("encode_type: VT_USERDEFINED - type %p name = %s real type %d idx %d\n", type,
983 type->name, type_get_type(type), type->typelib_idx);
985 if(type->typelib_idx == -1) {
986 chat("encode_type: trying to ref not added type\n");
987 switch (type_get_type(type)) {
988 case TYPE_STRUCT:
989 add_structure_typeinfo(typelib, type);
990 break;
991 case TYPE_INTERFACE:
992 add_interface_typeinfo(typelib, type);
993 break;
994 case TYPE_ENUM:
995 add_enum_typeinfo(typelib, type);
996 break;
997 case TYPE_UNION:
998 add_union_typeinfo(typelib, type);
999 break;
1000 case TYPE_COCLASS:
1001 add_coclass_typeinfo(typelib, type);
1002 break;
1003 default:
1004 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
1005 type_get_type(type));
1009 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
1010 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1011 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1012 if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == typeinfo_offset)) break;
1015 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1016 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1017 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1019 typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1020 typedata[1] = typeinfo_offset;
1023 *encoded_type = typeoffset;
1024 *width = 0;
1025 *alignment = 1;
1026 break;
1029 default:
1030 error("encode_type: unrecognized type %d.\n", vt);
1031 *encoded_type = default_type;
1032 *width = 0;
1033 *alignment = 1;
1034 break;
1037 return 0;
1040 static void dump_type(type_t *t)
1042 chat("dump_type: %p name %s type %d attrs %p\n", t, t->name, type_get_type(t), t->attrs);
1045 static int encode_var(
1046 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
1047 type_t *type, /* [I] The type description to encode. */
1048 var_t *var, /* [I] The var to encode. */
1049 int *encoded_type, /* [O] The encoded type description. */
1050 int *width, /* [O] The width of the type, or NULL. */
1051 int *alignment, /* [O] The alignment of the type, or NULL. */
1052 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1054 int typeoffset;
1055 int *typedata;
1056 int target_type;
1057 int child_size;
1058 int vt;
1059 int scratch;
1061 if (!width) width = &scratch;
1062 if (!alignment) alignment = &scratch;
1063 if (!decoded_size) decoded_size = &scratch;
1064 *decoded_size = 0;
1066 chat("encode_var: var %p type %p type->name %s\n",
1067 var, type, type->name ? type->name : "NULL");
1069 if (is_array(type) && !type_array_is_decl_as_ptr(type)) {
1070 int num_dims, elements = 1, arrayoffset;
1071 type_t *atype;
1072 int *arraydata;
1074 num_dims = 0;
1075 for (atype = type;
1076 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1077 atype = type_array_get_element(atype))
1078 ++num_dims;
1080 chat("array with %d dimensions\n", num_dims);
1081 encode_var(typelib, atype, var, &target_type, width, alignment, NULL);
1082 arrayoffset = ctl2_alloc_segment(typelib, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1083 arraydata = (void *)&typelib->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1085 arraydata[0] = target_type;
1086 arraydata[1] = num_dims;
1087 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1089 arraydata += 2;
1090 for (atype = type;
1091 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1092 atype = type_array_get_element(atype))
1094 arraydata[0] = type_array_get_dim(atype);
1095 arraydata[1] = 0;
1096 arraydata += 2;
1097 elements *= type_array_get_dim(atype);
1100 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1101 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1103 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1104 typedata[1] = arrayoffset;
1106 *encoded_type = typeoffset;
1107 *width = *width * elements;
1108 *decoded_size = 20 /*sizeof(ARRAYDESC)*/ + (num_dims - 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1109 return 0;
1112 vt = get_type_vt(type);
1113 if (vt == VT_PTR) {
1114 type_t *ref = is_ptr(type) ?
1115 type_pointer_get_ref(type) : type_array_get_element(type);
1116 int skip_ptr = encode_var(typelib, ref, var,
1117 &target_type, NULL, NULL, &child_size);
1119 if(skip_ptr == 2) {
1120 chat("encode_var: skipping ptr\n");
1121 *encoded_type = target_type;
1122 *decoded_size = child_size;
1123 *width = pointer_size;
1124 *alignment = 4;
1125 return 0;
1128 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1129 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1130 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1133 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1134 int mix_field;
1136 if (target_type & 0x80000000) {
1137 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1138 } else if (is_array(ref)) {
1139 type_t *element_type = type_alias_get_aliasee(type_array_get_element(ref));
1140 mix_field = get_type_vt(element_type) | VT_ARRAY | VT_BYREF;
1141 } else {
1142 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1143 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1146 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1147 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1149 typedata[0] = (mix_field << 16) | VT_PTR;
1150 typedata[1] = target_type;
1153 *encoded_type = typeoffset;
1155 *width = pointer_size;
1156 *alignment = 4;
1157 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1158 return 0;
1161 dump_type(type);
1163 encode_type(typelib, vt, type, encoded_type, width, alignment, decoded_size);
1164 /* these types already have an implicit pointer, so we don't need to
1165 * add another */
1166 if(vt == VT_DISPATCH || vt == VT_UNKNOWN) return 2;
1167 return 0;
1170 static unsigned int get_ulong_val(unsigned int val, int vt)
1172 switch(vt) {
1173 case VT_I2:
1174 case VT_BOOL:
1175 case VT_UI2:
1176 return val & 0xffff;
1177 case VT_I1:
1178 case VT_UI1:
1179 return val & 0xff;
1182 return val;
1185 static void write_value(msft_typelib_t* typelib, int *out, int vt, const void *value)
1187 switch(vt) {
1188 case VT_I2:
1189 case VT_I4:
1190 case VT_R4:
1191 case VT_BOOL:
1192 case VT_I1:
1193 case VT_UI1:
1194 case VT_UI2:
1195 case VT_UI4:
1196 case VT_INT:
1197 case VT_UINT:
1198 case VT_HRESULT:
1199 case VT_PTR:
1200 case VT_UNKNOWN:
1201 case VT_DISPATCH:
1203 const unsigned int lv = get_ulong_val(*(const unsigned int *)value, vt);
1204 if((lv & 0x3ffffff) == lv) {
1205 *out = 0x80000000;
1206 *out |= vt << 26;
1207 *out |= lv;
1208 } else {
1209 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1210 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1211 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], value, 4);
1212 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1213 *out = offset;
1215 return;
1217 case VT_BSTR:
1219 const char *s = (const char *) value;
1220 int len = strlen(s), seg_len = (len + 6 + 3) & ~0x3;
1221 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1222 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1223 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &len, sizeof(len));
1224 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1225 len += 6;
1226 while(len < seg_len) {
1227 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1228 len++;
1230 *out = offset;
1231 return;
1234 default:
1235 warning("can't write value of type %d yet\n", vt);
1237 return;
1240 static HRESULT set_custdata(msft_typelib_t *typelib, REFGUID guid,
1241 int vt, const void *value, int *offset)
1243 MSFT_GuidEntry guidentry;
1244 int guidoffset;
1245 int custoffset;
1246 int *custdata;
1247 int data_out;
1249 guidentry.guid = *guid;
1251 guidentry.hreftype = -1;
1252 guidentry.next_hash = -1;
1254 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1255 write_value(typelib, &data_out, vt, value);
1257 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1259 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1260 custdata[0] = guidoffset;
1261 custdata[1] = data_out;
1262 custdata[2] = *offset;
1263 *offset = custoffset;
1265 return S_OK;
1268 /* It's possible to have a default value for pointer arguments too.
1269 In this case default value has a referenced type, e.g.
1270 'LONG*' argument gets VT_I4, 'DOUBLE*' - VT_R8. IUnknown* and IDispatch*
1271 are recognised too and stored as VT_UNKNOWN and VT_DISPATCH.
1272 But IUnknown/IDispatch arguments can only have default value of 0
1273 (or expression that resolves to zero) while other pointers can have
1274 any default value. */
1275 static int get_defaultvalue_vt(type_t *type)
1277 int vt;
1278 if (type_get_type(type) == TYPE_ENUM)
1279 vt = VT_I4;
1280 else
1282 vt = get_type_vt(type);
1283 if (vt == VT_PTR && is_ptr(type)) {
1284 vt = get_type_vt(type_pointer_get_ref(type));
1285 /* The only acceptable value for pointers to non-basic types
1286 is NULL, it's stored as VT_I4 for both 32 and 64 bit typelibs. */
1287 if (vt == VT_USERDEFINED)
1288 vt = VT_I4;
1292 return vt;
1295 static HRESULT add_func_desc(msft_typeinfo_t* typeinfo, var_t *func, int index)
1297 int offset, name_offset;
1298 int *typedata, typedata_size;
1299 int i, id, next_idx;
1300 int decoded_size, extra_attr = 0;
1301 int num_params = 0, num_optional = 0, num_defaults = 0;
1302 var_t *arg;
1303 unsigned char *namedata;
1304 const attr_t *attr;
1305 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1306 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1307 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1308 int entry = -1, entry_is_ord = 0;
1309 int lcid_retval_count = 0;
1311 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1313 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1315 switch(typeinfo->typekind) {
1316 case TKIND_DISPATCH:
1317 funckind = 0x4; /* FUNC_DISPATCH */
1318 break;
1319 case TKIND_MODULE:
1320 funckind = 0x3; /* FUNC_STATIC */
1321 break;
1322 default:
1323 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1324 break;
1327 if (is_local( func->attrs )) {
1328 chat("add_func_desc: skipping local function\n");
1329 return S_FALSE;
1332 if (type_get_function_args(func->type))
1333 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1335 num_params++;
1336 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1337 if(attr->type == ATTR_DEFAULTVALUE)
1338 num_defaults++;
1339 else if(attr->type == ATTR_OPTIONAL)
1340 num_optional++;
1344 chat("add_func_desc: num of params %d\n", num_params);
1346 name_offset = ctl2_alloc_name(typeinfo->typelib, func->name);
1348 if (func->attrs) LIST_FOR_EACH_ENTRY( attr, func->attrs, const attr_t, entry ) {
1349 expr_t *expr = attr->u.pval;
1350 switch(attr->type) {
1351 case ATTR_BINDABLE:
1352 funcflags |= 0x4; /* FUNCFLAG_FBINDABLE */
1353 break;
1354 case ATTR_DEFAULTBIND:
1355 funcflags |= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1356 break;
1357 case ATTR_DEFAULTCOLLELEM:
1358 funcflags |= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1359 break;
1360 case ATTR_DISPLAYBIND:
1361 funcflags |= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1362 break;
1363 case ATTR_ENTRY:
1364 extra_attr = max(extra_attr, 3);
1365 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1366 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1367 else {
1368 entry = expr->cval;
1369 entry_is_ord = 1;
1371 break;
1372 case ATTR_HELPCONTEXT:
1373 extra_attr = max(extra_attr, 1);
1374 help_context = expr->u.lval;
1375 break;
1376 case ATTR_HELPSTRING:
1377 extra_attr = max(extra_attr, 2);
1378 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1379 break;
1380 case ATTR_HELPSTRINGCONTEXT:
1381 extra_attr = max(extra_attr, 6);
1382 help_string_context = expr->u.lval;
1383 break;
1384 case ATTR_HIDDEN:
1385 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1386 break;
1387 case ATTR_ID:
1388 id = expr->cval;
1389 break;
1390 case ATTR_IMMEDIATEBIND:
1391 funcflags |= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1392 break;
1393 case ATTR_NONBROWSABLE:
1394 funcflags |= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1395 break;
1396 case ATTR_OUT:
1397 break;
1398 case ATTR_PROPGET:
1399 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1400 break;
1401 case ATTR_PROPPUT:
1402 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1403 break;
1404 case ATTR_PROPPUTREF:
1405 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1406 break;
1407 /* FIXME: FUNCFLAG_FREPLACEABLE */
1408 case ATTR_REQUESTEDIT:
1409 funcflags |= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1410 break;
1411 case ATTR_RESTRICTED:
1412 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1413 break;
1414 case ATTR_SOURCE:
1415 funcflags |= 0x2; /* FUNCFLAG_FSOURCE */
1416 break;
1417 case ATTR_UIDEFAULT:
1418 funcflags |= 0x200; /* FUNCFLAG_FUIDEFAULT */
1419 break;
1420 case ATTR_USESGETLASTERROR:
1421 funcflags |= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1422 break;
1423 case ATTR_VARARG:
1424 if (num_optional || num_defaults)
1425 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1426 else
1427 num_optional = -1;
1428 break;
1429 default:
1430 break;
1434 /* allocate type data space for us */
1435 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1437 if (!typeinfo->func_data) {
1438 typeinfo->func_data = xmalloc(0x100);
1439 typeinfo->func_data_allocated = 0x100;
1440 typeinfo->func_data[0] = 0;
1443 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1444 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1445 typeinfo->func_data[0] + typedata_size + sizeof(int));
1446 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1449 offset = typeinfo->func_data[0];
1450 typeinfo->func_data[0] += typedata_size;
1451 typedata = typeinfo->func_data + (offset >> 2) + 1;
1454 /* find func with the same name - if it exists use its id */
1455 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1456 if(name_offset == typeinfo->func_names[i]) {
1457 id = typeinfo->func_indices[i];
1458 break;
1462 /* find the first func with the same id and link via the hiword of typedata[4] */
1463 next_idx = index;
1464 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1465 if(id == typeinfo->func_indices[i]) {
1466 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1467 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1468 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1469 break;
1473 /* fill out the basic type information */
1474 typedata[0] = typedata_size | (index << 16);
1475 encode_var(typeinfo->typelib, type_function_get_rettype(func->type), func, &typedata[1], NULL, NULL, &decoded_size);
1476 typedata[2] = funcflags;
1477 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1478 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1479 if(num_defaults) typedata[4] |= 0x1000;
1480 if(entry_is_ord) typedata[4] |= 0x2000;
1481 typedata[5] = (num_optional << 16) | num_params;
1483 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1484 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1485 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1486 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1488 switch(extra_attr) {
1489 case 6: typedata[11] = help_string_context;
1490 case 5: typedata[10] = -1;
1491 case 4: typedata[9] = -1;
1492 case 3: typedata[8] = entry;
1493 case 2: typedata[7] = help_string_offset;
1494 case 1: typedata[6] = help_context;
1495 case 0:
1496 break;
1497 default:
1498 warning("unknown number of optional attrs\n");
1501 if (type_get_function_args(func->type))
1503 i = 0;
1504 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1506 int paramflags = 0;
1507 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1508 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1510 if(defaultdata) *defaultdata = -1;
1512 encode_var(typeinfo->typelib, arg->type, arg, paramdata, NULL, NULL, &decoded_size);
1513 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1514 switch(attr->type) {
1515 case ATTR_DEFAULTVALUE:
1517 int vt;
1518 expr_t *expr = (expr_t *)attr->u.pval;
1519 vt = get_defaultvalue_vt(arg->type);
1520 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1521 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1523 if (vt != VT_BSTR) error("string default value applied to non-string type\n");
1524 chat("default value '%s'\n", expr->u.sval);
1525 write_value(typeinfo->typelib, defaultdata, vt, expr->u.sval);
1527 else
1529 chat("default value %d\n", expr->cval);
1530 write_value(typeinfo->typelib, defaultdata, vt, &expr->cval);
1532 break;
1534 case ATTR_IN:
1535 paramflags |= 0x01; /* PARAMFLAG_FIN */
1536 break;
1537 case ATTR_OPTIONAL:
1538 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1539 break;
1540 case ATTR_OUT:
1541 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1542 break;
1543 case ATTR_PARAMLCID:
1544 paramflags |= 0x04; /* PARAMFLAG_LCID */
1545 lcid_retval_count++;
1546 break;
1547 case ATTR_RETVAL:
1548 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1549 lcid_retval_count++;
1550 break;
1551 default:
1552 chat("unhandled param attr %d\n", attr->type);
1553 break;
1556 paramdata[1] = -1;
1557 paramdata[2] = paramflags;
1558 typedata[3] += decoded_size << 16;
1560 i++;
1564 if(lcid_retval_count == 1)
1565 typedata[4] |= 0x4000;
1566 else if(lcid_retval_count == 2)
1567 typedata[4] |= 0x8000;
1569 if(typeinfo->funcs_allocated == 0) {
1570 typeinfo->funcs_allocated = 10;
1571 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1572 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1573 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1575 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1576 typeinfo->funcs_allocated *= 2;
1577 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1578 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1579 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1582 /* update the index data */
1583 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1584 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1585 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1587 /* ??? */
1588 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1589 typeinfo->typeinfo->res2 <<= 1;
1590 /* ??? */
1591 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1593 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1594 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1595 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1597 /* adjust size of VTBL */
1598 if(funckind != 0x3 /* FUNC_STATIC */)
1599 typeinfo->typeinfo->cbSizeVft += pointer_size;
1601 /* Increment the number of function elements */
1602 typeinfo->typeinfo->cElement += 1;
1604 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1605 if (*((INT *)namedata) == -1) {
1606 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1607 if(typeinfo->typekind == TKIND_MODULE)
1608 namedata[9] |= 0x10;
1609 } else
1610 namedata[9] &= ~0x10;
1612 if(typeinfo->typekind == TKIND_MODULE)
1613 namedata[9] |= 0x20;
1615 if (type_get_function_args(func->type))
1617 i = 0;
1618 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1620 /* don't give the last arg of a [propput*] func a name */
1621 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1623 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1624 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1625 paramdata[1] = offset;
1627 i++;
1630 return S_OK;
1633 static HRESULT add_var_desc(msft_typeinfo_t *typeinfo, UINT index, var_t* var)
1635 int offset, id;
1636 unsigned int typedata_size;
1637 INT *typedata;
1638 int var_datawidth;
1639 int var_alignment;
1640 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1641 int alignment;
1642 int varflags = 0;
1643 const attr_t *attr;
1644 unsigned char *namedata;
1645 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1647 chat("add_var_desc(%d, %s)\n", index, var->name);
1649 id = 0x40000000 + index;
1651 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1652 expr_t *expr = attr->u.pval;
1653 switch(attr->type) {
1654 case ATTR_BINDABLE:
1655 varflags |= 0x04; /* VARFLAG_FBINDABLE */
1656 break;
1657 case ATTR_DEFAULTBIND:
1658 varflags |= 0x20; /* VARFLAG_FDEFAULTBIND */
1659 break;
1660 case ATTR_DEFAULTCOLLELEM:
1661 varflags |= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1662 break;
1663 case ATTR_DISPLAYBIND:
1664 varflags |= 0x10; /* VARFLAG_FDISPLAYBIND */
1665 break;
1666 case ATTR_HIDDEN:
1667 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1668 break;
1669 case ATTR_ID:
1670 id = expr->cval;
1671 break;
1672 case ATTR_IMMEDIATEBIND:
1673 varflags |= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1674 break;
1675 case ATTR_NONBROWSABLE:
1676 varflags |= 0x400; /* VARFLAG_FNONBROWSABLE */
1677 break;
1678 case ATTR_READONLY:
1679 varflags |= 0x01; /* VARFLAG_FREADONLY */
1680 break;
1681 /* FIXME: VARFLAG_FREPLACEABLE */
1682 case ATTR_REQUESTEDIT:
1683 varflags |= 0x08; /* VARFLAG_FREQUESTEDIT */
1684 break;
1685 case ATTR_RESTRICTED:
1686 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1687 break;
1688 case ATTR_SOURCE:
1689 varflags |= 0x02; /* VARFLAG_FSOURCE */
1690 break;
1691 case ATTR_UIDEFAULT:
1692 varflags |= 0x0200; /* VARFLAG_FUIDEFAULT */
1693 break;
1694 default:
1695 break;
1699 /* allocate type data space for us */
1700 typedata_size = 0x14;
1702 if (!typeinfo->var_data) {
1703 typeinfo->var_data = xmalloc(0x100);
1704 typeinfo->var_data_allocated = 0x100;
1705 typeinfo->var_data[0] = 0;
1708 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1709 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1710 typeinfo->var_data[0] + typedata_size + sizeof(int));
1711 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1714 offset = typeinfo->var_data[0];
1715 typeinfo->var_data[0] += typedata_size;
1716 typedata = typeinfo->var_data + (offset >> 2) + 1;
1718 /* fill out the basic type information */
1719 typedata[0] = typedata_size | (index << 16);
1720 typedata[2] = varflags;
1721 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1723 if(typeinfo->vars_allocated == 0) {
1724 typeinfo->vars_allocated = 10;
1725 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1726 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1727 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1729 if(typeinfo->vars_allocated == var_num) {
1730 typeinfo->vars_allocated *= 2;
1731 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1732 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1733 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1735 /* update the index data */
1736 typeinfo->var_indices[var_num] = id;
1737 typeinfo->var_names[var_num] = -1;
1738 typeinfo->var_offsets[var_num] = offset;
1740 /* figure out type widths and whatnot */
1741 encode_var(typeinfo->typelib, var->type, var, &typedata[1], &var_datawidth,
1742 &var_alignment, &var_type_size);
1744 /* pad out starting position to data width */
1745 typeinfo->datawidth += var_alignment - 1;
1746 typeinfo->datawidth &= ~(var_alignment - 1);
1748 switch(typeinfo->typekind) {
1749 case TKIND_ENUM:
1750 write_value(typeinfo->typelib, &typedata[4], VT_I4, &var->eval->cval);
1751 var_kind = 2; /* VAR_CONST */
1752 var_type_size += 16; /* sizeof(VARIANT) */
1753 typeinfo->datawidth = var_datawidth;
1754 break;
1755 case TKIND_RECORD:
1756 typedata[4] = typeinfo->datawidth;
1757 typeinfo->datawidth += var_datawidth;
1758 break;
1759 case TKIND_UNION:
1760 typedata[4] = typeinfo->datawidth;
1761 typeinfo->datawidth = max(typeinfo->datawidth, var_datawidth);
1762 break;
1763 case TKIND_DISPATCH:
1764 var_kind = 3; /* VAR_DISPATCH */
1765 typeinfo->datawidth = pointer_size;
1766 var_alignment = 4;
1767 break;
1768 default:
1769 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1770 break;
1773 /* add type description size to total required allocation */
1774 typedata[3] += var_type_size << 16 | var_kind;
1776 /* fix type alignment */
1777 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1778 if (alignment < var_alignment) {
1779 alignment = var_alignment;
1780 typeinfo->typeinfo->typekind &= ~0xffc0;
1781 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1784 /* ??? */
1785 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1786 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1787 typeinfo->typeinfo->res2 <<= 1;
1790 /* ??? */
1791 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1792 typeinfo->typeinfo->res3 += 0x2c;
1794 /* increment the number of variable elements */
1795 typeinfo->typeinfo->cElement += 0x10000;
1797 /* pad data width to alignment */
1798 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1800 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1801 if (offset == -1) return E_OUTOFMEMORY;
1803 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1804 if (*((INT *)namedata) == -1) {
1805 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1806 if(typeinfo->typekind != TKIND_DISPATCH)
1807 namedata[9] |= 0x10;
1808 } else
1809 namedata[9] &= ~0x10;
1811 if (typeinfo->typekind == TKIND_ENUM) {
1812 namedata[9] |= 0x20;
1814 typeinfo->var_names[var_num] = offset;
1816 return S_OK;
1819 static HRESULT add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1821 if(importinfo) {
1822 alloc_importinfo(typeinfo->typelib, importinfo);
1823 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1824 }else {
1825 if(ref->typelib_idx == -1)
1826 add_interface_typeinfo(typeinfo->typelib, ref);
1827 if(ref->typelib_idx == -1)
1828 error("add_impl_type: unable to add inherited interface\n");
1830 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1833 typeinfo->typeinfo->cImplTypes++;
1834 return S_OK;
1837 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1838 const char *name, const attr_list_t *attrs)
1840 const attr_t *attr;
1841 msft_typeinfo_t *msft_typeinfo;
1842 int nameoffset;
1843 int typeinfo_offset;
1844 MSFT_TypeInfoBase *typeinfo;
1845 MSFT_GuidEntry guidentry;
1847 chat("create_msft_typeinfo: name %s kind %d index %d\n", name, kind, typelib->typelib_header.nrtypeinfos);
1849 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1850 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1852 msft_typeinfo->typelib = typelib;
1854 nameoffset = ctl2_alloc_name(typelib, name);
1855 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1856 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1858 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1859 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1861 msft_typeinfo->typekind = kind;
1862 msft_typeinfo->typeinfo = typeinfo;
1864 typeinfo->typekind |= kind | 0x20;
1866 if(kind == TKIND_COCLASS)
1867 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1869 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1870 switch(attr->type) {
1871 case ATTR_AGGREGATABLE:
1872 if (kind == TKIND_COCLASS)
1873 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1874 break;
1876 case ATTR_APPOBJECT:
1877 if (kind == TKIND_COCLASS)
1878 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1879 break;
1881 case ATTR_CONTROL:
1882 if (kind == TKIND_COCLASS)
1883 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1884 break;
1886 case ATTR_DLLNAME:
1888 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1889 typeinfo->datatype1 = offset;
1890 break;
1893 case ATTR_DUAL:
1894 /* FIXME: check interface is compatible */
1895 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1896 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1897 break;
1899 case ATTR_HELPCONTEXT:
1901 expr_t *expr = (expr_t*)attr->u.pval;
1902 typeinfo->helpcontext = expr->cval;
1903 break;
1905 case ATTR_HELPSTRING:
1907 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1908 if (offset == -1) break;
1909 typeinfo->docstringoffs = offset;
1910 break;
1912 case ATTR_HELPSTRINGCONTEXT:
1914 expr_t *expr = (expr_t*)attr->u.pval;
1915 typeinfo->helpstringcontext = expr->cval;
1916 break;
1918 case ATTR_HIDDEN:
1919 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1920 break;
1922 case ATTR_LICENSED:
1923 typeinfo->flags |= 0x04; /* TYPEFLAG_FLICENSED */
1924 break;
1926 case ATTR_NONCREATABLE:
1927 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1928 break;
1930 case ATTR_NONEXTENSIBLE:
1931 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1932 break;
1934 case ATTR_OLEAUTOMATION:
1935 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1936 break;
1938 /* FIXME: TYPEFLAG_FPREDCLID */
1940 case ATTR_PROXY:
1941 typeinfo->flags |= 0x4000; /* TYPEFLAG_FPROXY */
1942 break;
1944 /* FIXME: TYPEFLAG_FREPLACEABLE */
1946 case ATTR_RESTRICTED:
1947 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
1948 break;
1950 case ATTR_UUID:
1951 guidentry.guid = *(GUID*)attr->u.pval;
1952 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1953 guidentry.next_hash = -1;
1954 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
1955 #if 0
1956 if (IsEqualIID(guid, &IID_IDispatch)) {
1957 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1959 #endif
1960 break;
1962 case ATTR_VERSION:
1963 typeinfo->version = attr->u.ival;
1964 break;
1966 default:
1967 break;
1971 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
1972 typelib->last_typeinfo = msft_typeinfo;
1973 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
1976 return msft_typeinfo;
1979 static void add_dispatch(msft_typelib_t *typelib)
1981 int guid_offset, impfile_offset, hash_key;
1982 MSFT_GuidEntry guidentry;
1983 MSFT_ImpInfo impinfo;
1984 GUID stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1985 GUID iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1987 if(typelib->typelib_header.dispatchpos != -1) return;
1989 guidentry.guid = stdole;
1990 guidentry.hreftype = 2;
1991 guidentry.next_hash = -1;
1992 hash_key = ctl2_hash_guid(&guidentry.guid);
1993 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
1994 if (guid_offset == -1)
1995 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
1996 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
1998 guidentry.guid = iid_idispatch;
1999 guidentry.hreftype = 1;
2000 guidentry.next_hash = -1;
2001 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
2002 impinfo.oImpFile = impfile_offset;
2003 hash_key = ctl2_hash_guid(&guidentry.guid);
2004 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2005 if (guid_offset == -1)
2006 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2007 impinfo.oGuid = guid_offset;
2008 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
2011 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
2013 int idx = 0;
2014 var_t *func;
2015 var_t *var;
2016 msft_typeinfo_t *msft_typeinfo;
2018 if (-1 < dispinterface->typelib_idx)
2019 return;
2021 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2022 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
2023 dispinterface->attrs);
2025 msft_typeinfo->typeinfo->size = pointer_size;
2026 msft_typeinfo->typeinfo->typekind |= 0x2100;
2028 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2029 add_dispatch(typelib);
2030 msft_typeinfo->typeinfo->cImplTypes = 1;
2032 /* count the no of methods, as the variable indices come after the funcs */
2033 if (dispinterface->details.iface->disp_methods)
2034 LIST_FOR_EACH_ENTRY( func, dispinterface->details.iface->disp_methods, var_t, entry )
2035 idx++;
2037 if (type_dispiface_get_props(dispinterface))
2038 LIST_FOR_EACH_ENTRY( var, type_dispiface_get_props(dispinterface), var_t, entry )
2039 add_var_desc(msft_typeinfo, idx++, var);
2041 if (type_dispiface_get_methods(dispinterface))
2043 idx = 0;
2044 LIST_FOR_EACH_ENTRY( func, type_dispiface_get_methods(dispinterface), var_t, entry )
2045 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2046 idx++;
2050 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
2052 int idx = 0;
2053 const statement_t *stmt_func;
2054 type_t *ref;
2055 msft_typeinfo_t *msft_typeinfo;
2056 importinfo_t *ref_importinfo = NULL;
2057 int num_parents = 0, num_funcs = 0;
2058 type_t *inherit;
2059 const type_t *derived;
2061 if (-1 < interface->typelib_idx)
2062 return;
2064 if (!interface->details.iface)
2066 error( "interface %s is referenced but not defined\n", interface->name );
2067 return;
2070 if (is_attr(interface->attrs, ATTR_DISPINTERFACE)) {
2071 add_dispinterface_typeinfo(typelib, interface);
2072 return;
2075 /* midl adds the parent interface first, unless the parent itself
2076 has no parent (i.e. it stops before IUnknown). */
2078 inherit = type_iface_get_inherit(interface);
2080 if(inherit) {
2081 ref_importinfo = find_importinfo(typelib, inherit->name);
2083 if(!ref_importinfo && type_iface_get_inherit(inherit) &&
2084 inherit->typelib_idx == -1)
2085 add_interface_typeinfo(typelib, inherit);
2088 /* check typelib_idx again, it could have been added while resolving the parent interface */
2089 if (-1 < interface->typelib_idx)
2090 return;
2092 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2093 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
2094 msft_typeinfo->typeinfo->size = pointer_size;
2095 msft_typeinfo->typeinfo->typekind |= 0x2200;
2097 for (derived = inherit; derived; derived = type_iface_get_inherit(derived))
2098 if (derived->name && !strcmp(derived->name, "IDispatch"))
2099 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2101 /* can't be dual if it doesn't derive from IDispatch */
2102 if (!(msft_typeinfo->typeinfo->flags & 0x1000)) /* TYPEFLAG_FDISPATCHABLE */
2103 msft_typeinfo->typeinfo->flags &= ~0x40; /* TYPEFLAG_FDUAL */
2105 if(type_iface_get_inherit(interface))
2106 add_impl_type(msft_typeinfo, type_iface_get_inherit(interface),
2107 ref_importinfo);
2109 /* count the number of inherited interfaces and non-local functions */
2110 for(ref = inherit; ref; ref = type_iface_get_inherit(ref)) {
2111 num_parents++;
2112 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) ) {
2113 var_t *func = stmt_func->u.var;
2114 if (!is_local(func->attrs)) num_funcs++;
2117 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2118 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2120 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(interface) ) {
2121 var_t *func = stmt_func->u.var;
2122 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2123 idx++;
2127 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2129 int idx = 0;
2130 var_t *cur;
2131 msft_typeinfo_t *msft_typeinfo;
2133 if (-1 < structure->typelib_idx)
2134 return;
2136 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2137 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2138 msft_typeinfo->typeinfo->size = 0;
2140 if (type_struct_get_fields(structure))
2141 LIST_FOR_EACH_ENTRY( cur, type_struct_get_fields(structure), var_t, entry )
2142 add_var_desc(msft_typeinfo, idx++, cur);
2145 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2147 int idx = 0;
2148 var_t *cur;
2149 msft_typeinfo_t *msft_typeinfo;
2151 if (-1 < enumeration->typelib_idx)
2152 return;
2154 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2155 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2156 msft_typeinfo->typeinfo->size = 0;
2158 if (type_enum_get_values(enumeration))
2159 LIST_FOR_EACH_ENTRY( cur, type_enum_get_values(enumeration), var_t, entry )
2160 add_var_desc(msft_typeinfo, idx++, cur);
2163 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion)
2165 int idx = 0;
2166 var_t *cur;
2167 msft_typeinfo_t *msft_typeinfo;
2169 if (-1 < tunion->typelib_idx)
2170 return;
2172 tunion->typelib_idx = typelib->typelib_header.nrtypeinfos;
2173 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_UNION, tunion->name, tunion->attrs);
2174 msft_typeinfo->typeinfo->size = 0;
2176 if (type_union_get_cases(tunion))
2177 LIST_FOR_EACH_ENTRY(cur, type_union_get_cases(tunion), var_t, entry)
2178 add_var_desc(msft_typeinfo, idx++, cur);
2181 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2183 msft_typeinfo_t *msft_typeinfo = NULL;
2184 int alignment, datatype1, datatype2, size, duplicate = 0;
2185 type_t *type;
2187 if (-1 < tdef->typelib_idx)
2188 return;
2190 type = type_alias_get_aliasee(tdef);
2192 if (!type->name || strcmp(tdef->name, type->name) != 0)
2194 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2195 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2197 else
2198 duplicate = 1;
2200 encode_type(typelib, get_type_vt(type), type,
2201 &datatype1, &size, &alignment, &datatype2);
2203 if (msft_typeinfo)
2205 msft_typeinfo->typeinfo->datatype1 = datatype1;
2206 msft_typeinfo->typeinfo->size = size;
2207 msft_typeinfo->typeinfo->datatype2 = datatype2;
2208 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2211 /* avoid adding duplicate type definitions */
2212 if (duplicate)
2213 tdef->typelib_idx = type->typelib_idx;
2216 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2218 msft_typeinfo_t *msft_typeinfo;
2219 ifref_t *iref;
2220 int num_ifaces = 0, offset, i;
2221 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2222 int have_default = 0, have_default_source = 0;
2223 const attr_t *attr;
2224 ifref_list_t *ifaces;
2226 if (-1 < cls->typelib_idx)
2227 return;
2229 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2230 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2232 ifaces = type_coclass_get_ifaces(cls);
2233 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) num_ifaces++;
2235 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2236 num_ifaces * sizeof(*ref), 0);
2238 i = 0;
2239 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) {
2240 if(iref->iface->typelib_idx == -1)
2241 add_interface_typeinfo(typelib, iref->iface);
2242 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2243 ref->reftype = typelib->typelib_typeinfo_offsets[iref->iface->typelib_idx];
2244 ref->flags = 0;
2245 ref->oCustData = -1;
2246 ref->onext = -1;
2247 if(i < num_ifaces - 1)
2248 ref->onext = offset + (i + 1) * sizeof(*ref);
2250 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2251 switch(attr->type) {
2252 case ATTR_DEFAULT:
2253 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2254 break;
2255 case ATTR_DEFAULTVTABLE:
2256 ref->flags |= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2257 break;
2258 case ATTR_RESTRICTED:
2259 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2260 break;
2261 case ATTR_SOURCE:
2262 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2263 break;
2264 default:
2265 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2268 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2269 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2270 have_default_source = 1;
2271 else
2272 have_default = 1;
2275 /* If the interface is non-restricted and we haven't already had one then
2276 remember it so that we can use it as a default later */
2277 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2278 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2279 if(!first_source)
2280 first_source = ref;
2282 else if(!first)
2283 first = ref;
2285 i++;
2288 /* If we haven't had a default interface, then set the default flags on the
2289 first ones */
2290 if(!have_default && first)
2291 first->flags |= 0x1;
2292 if(!have_default_source && first_source)
2293 first_source->flags |= 0x1;
2295 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2296 msft_typeinfo->typeinfo->size = pointer_size;
2297 msft_typeinfo->typeinfo->typekind |= 0x2200;
2300 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2302 int idx = 0;
2303 const statement_t *stmt;
2304 msft_typeinfo_t *msft_typeinfo;
2306 if (-1 < module->typelib_idx)
2307 return;
2309 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2310 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2311 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2313 STATEMENTS_FOR_EACH_FUNC( stmt, module->details.module->stmts ) {
2314 var_t *func = stmt->u.var;
2315 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2316 idx++;
2319 msft_typeinfo->typeinfo->size = idx;
2322 static void add_type_typeinfo(msft_typelib_t *typelib, type_t *type)
2324 switch (type_get_type(type)) {
2325 case TYPE_INTERFACE:
2326 add_interface_typeinfo(typelib, type);
2327 break;
2328 case TYPE_STRUCT:
2329 add_structure_typeinfo(typelib, type);
2330 break;
2331 case TYPE_ENUM:
2332 add_enum_typeinfo(typelib, type);
2333 break;
2334 case TYPE_UNION:
2335 add_union_typeinfo(typelib, type);
2336 break;
2337 case TYPE_COCLASS:
2338 add_coclass_typeinfo(typelib, type);
2339 break;
2340 case TYPE_BASIC:
2341 case TYPE_POINTER:
2342 break;
2343 default:
2344 error("add_entry: unhandled type 0x%x for %s\n",
2345 type_get_type(type), type->name);
2346 break;
2350 static void add_entry(msft_typelib_t *typelib, const statement_t *stmt)
2352 switch(stmt->type) {
2353 case STMT_LIBRARY:
2354 case STMT_IMPORT:
2355 case STMT_PRAGMA:
2356 case STMT_CPPQUOTE:
2357 case STMT_DECLARATION:
2358 /* not included in typelib */
2359 break;
2360 case STMT_IMPORTLIB:
2361 /* not processed here */
2362 break;
2363 case STMT_TYPEDEF:
2365 const type_list_t *type_entry = stmt->u.type_list;
2366 for (; type_entry; type_entry = type_entry->next) {
2367 /* if the type is public then add the typedef, otherwise attempt
2368 * to add the aliased type */
2369 if (is_attr(type_entry->type->attrs, ATTR_PUBLIC))
2370 add_typedef_typeinfo(typelib, type_entry->type);
2371 else
2372 add_type_typeinfo(typelib, type_alias_get_aliasee(type_entry->type));
2374 break;
2376 case STMT_MODULE:
2377 add_module_typeinfo(typelib, stmt->u.type);
2378 break;
2379 case STMT_TYPE:
2380 case STMT_TYPEREF:
2382 type_t *type = stmt->u.type;
2383 add_type_typeinfo(typelib, type);
2384 break;
2389 static void set_name(msft_typelib_t *typelib)
2391 int offset;
2393 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2394 if (offset == -1) return;
2395 typelib->typelib_header.NameOffset = offset;
2396 return;
2399 static void set_version(msft_typelib_t *typelib)
2401 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2404 static void set_guid(msft_typelib_t *typelib)
2406 MSFT_GuidEntry guidentry;
2407 int offset;
2408 void *ptr;
2409 GUID guid = {0,0,0,{0,0,0,0,0,0}};
2411 guidentry.guid = guid;
2412 guidentry.hreftype = -2;
2413 guidentry.next_hash = -1;
2415 ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2416 if (ptr) guidentry.guid = *(GUID *)ptr;
2418 offset = ctl2_alloc_guid(typelib, &guidentry);
2419 typelib->typelib_header.posguid = offset;
2421 return;
2424 static void set_doc_string(msft_typelib_t *typelib)
2426 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2428 if (str)
2430 int offset = ctl2_alloc_string(typelib, str);
2431 if (offset != -1) typelib->typelib_header.helpstring = offset;
2435 static void set_help_file_name(msft_typelib_t *typelib)
2437 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2439 if (str)
2441 int offset = ctl2_alloc_string(typelib, str);
2442 if (offset != -1)
2444 typelib->typelib_header.helpfile = offset;
2445 typelib->typelib_header.varflags |= 0x10;
2450 static void set_help_context(msft_typelib_t *typelib)
2452 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2453 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2456 static void set_help_string_dll(msft_typelib_t *typelib)
2458 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2460 if (str)
2462 int offset = ctl2_alloc_string(typelib, str);
2463 if (offset != -1)
2465 typelib->help_string_dll_offset = offset;
2466 typelib->typelib_header.varflags |= 0x100;
2471 static void set_help_string_context(msft_typelib_t *typelib)
2473 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2474 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2477 static void set_lcid(msft_typelib_t *typelib)
2479 const expr_t *lcid_expr = get_attrp( typelib->typelib->attrs, ATTR_LIBLCID );
2480 if(lcid_expr)
2482 typelib->typelib_header.lcid = lcid_expr->cval;
2483 typelib->typelib_header.lcid2 = lcid_expr->cval;
2487 static void set_lib_flags(msft_typelib_t *typelib)
2489 const attr_t *attr;
2491 typelib->typelib_header.flags = 0;
2492 if (!typelib->typelib->attrs) return;
2493 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2495 switch(attr->type) {
2496 case ATTR_CONTROL:
2497 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2498 break;
2499 case ATTR_HIDDEN:
2500 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2501 break;
2502 case ATTR_RESTRICTED:
2503 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2504 break;
2505 default:
2506 break;
2509 return;
2512 static void ctl2_write_segment(msft_typelib_t *typelib, int segment)
2514 put_data(typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length);
2517 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2519 msft_typeinfo_t *typeinfo;
2521 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2522 typeinfo->typeinfo->memoffset = filesize;
2523 if (typeinfo->func_data)
2524 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2525 if (typeinfo->var_data)
2526 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2527 if (typeinfo->func_data || typeinfo->var_data)
2528 filesize += 4;
2532 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2534 if (typelib->typelib_segdir[segment].length) {
2535 typelib->typelib_segdir[segment].offset = filepos;
2536 } else {
2537 typelib->typelib_segdir[segment].offset = -1;
2540 return typelib->typelib_segdir[segment].length;
2544 static void ctl2_write_typeinfos(msft_typelib_t *typelib)
2546 msft_typeinfo_t *typeinfo;
2547 int typedata_size;
2549 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2550 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2551 typedata_size = 0;
2552 if (typeinfo->func_data)
2553 typedata_size = typeinfo->func_data[0];
2554 if (typeinfo->var_data)
2555 typedata_size += typeinfo->var_data[0];
2556 put_data(&typedata_size, sizeof(int));
2557 if (typeinfo->func_data)
2558 put_data(typeinfo->func_data + 1, typeinfo->func_data[0]);
2559 if (typeinfo->var_data)
2560 put_data(typeinfo->var_data + 1, typeinfo->var_data[0]);
2561 if (typeinfo->func_indices)
2562 put_data(typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2563 if (typeinfo->var_indices)
2564 put_data(typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2565 if (typeinfo->func_names)
2566 put_data(typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2567 if (typeinfo->var_names)
2568 put_data(typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2569 if (typeinfo->func_offsets)
2570 put_data(typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2571 if (typeinfo->var_offsets) {
2572 int add = 0, i, offset;
2573 if(typeinfo->func_data)
2574 add = typeinfo->func_data[0];
2575 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2576 offset = typeinfo->var_offsets[i];
2577 offset += add;
2578 put_data(&offset, 4);
2584 static void save_all_changes(msft_typelib_t *typelib)
2586 int filepos;
2588 chat("save_all_changes(%p)\n", typelib);
2590 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2591 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2592 filepos += typelib->typelib_header.nrtypeinfos * 4;
2594 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2595 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2596 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2597 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2598 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2599 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2600 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2601 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2602 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2603 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2604 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2605 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2606 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2608 ctl2_finalize_typeinfos(typelib, filepos);
2610 byte_swapped = 0;
2611 init_output_buffer();
2613 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2614 if(typelib->typelib_header.varflags & 0x100)
2615 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2617 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2618 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2619 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2620 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2621 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2622 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2623 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2624 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2625 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2626 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2627 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2628 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2629 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2630 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2631 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2633 ctl2_write_typeinfos(typelib);
2635 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2637 char typelib_id[13] = "#1";
2639 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2640 if (expr)
2641 sprintf( typelib_id, "#%d", expr->cval );
2642 add_output_to_resources( "TYPELIB", typelib_id );
2643 output_typelib_regscript( typelib->typelib );
2644 flush_output_resources( typelib_name );
2646 else flush_output_buffer( typelib_name );
2649 int create_msft_typelib(typelib_t *typelib)
2651 msft_typelib_t *msft;
2652 int failed = 0;
2653 const statement_t *stmt;
2654 time_t cur_time;
2655 char *time_override;
2656 unsigned int version = 7 << 24 | 555; /* 7.00.0555 */
2657 GUID midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2658 GUID midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2659 GUID midl_info_guid = {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2660 char info_string[128];
2662 pointer_size = (typelib_kind == SYS_WIN64) ? 8 : 4;
2664 msft = xmalloc(sizeof(*msft));
2665 memset(msft, 0, sizeof(*msft));
2666 msft->typelib = typelib;
2668 ctl2_init_header(msft);
2669 ctl2_init_segdir(msft);
2671 msft->typelib_header.varflags |= typelib_kind;
2674 * The following two calls return an offset or -1 if out of memory. We
2675 * specifically need an offset of 0, however, so...
2677 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2678 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2680 if(failed)
2682 free(msft);
2683 return 0;
2686 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2687 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2689 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2690 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2692 set_lib_flags(msft);
2693 set_lcid(msft);
2694 set_help_file_name(msft);
2695 set_doc_string(msft);
2696 set_guid(msft);
2697 set_version(msft);
2698 set_name(msft);
2699 set_help_context(msft);
2700 set_help_string_dll(msft);
2701 set_help_string_context(msft);
2703 /* midl adds two sets of custom data to the library: the current unix time
2704 and midl's version number */
2705 time_override = getenv( "WIDL_TIME_OVERRIDE");
2706 cur_time = time_override ? atol( time_override) : time(NULL);
2707 sprintf(info_string, "Created by WIDL version %s at %s\n", PACKAGE_VERSION, ctime(&cur_time));
2708 set_custdata(msft, &midl_info_guid, VT_BSTR, info_string, &msft->typelib_header.CustomDataOffset);
2709 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2710 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2712 if (typelib->stmts)
2713 LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
2714 add_entry(msft, stmt);
2716 save_all_changes(msft);
2717 free(msft);
2718 return 1;