hidclass.sys: Implement IRP_MJ_READ for HID Devices.
[wine/multimedia.git] / tools / widl / write_msft.c
blob26600e43471c12a4a8978d203bbb235d5b8793b2
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 if (is_array(ref)) {
1135 type_t *element_type = type_alias_get_aliasee(type_array_get_element(ref));
1136 mix_field = get_type_vt(element_type) | VT_ARRAY | VT_BYREF;
1137 } else {
1138 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1139 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1142 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1143 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1145 typedata[0] = (mix_field << 16) | VT_PTR;
1146 typedata[1] = target_type;
1149 *encoded_type = typeoffset;
1151 *width = pointer_size;
1152 *alignment = 4;
1153 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1154 return 0;
1157 dump_type(type);
1159 encode_type(typelib, vt, type, encoded_type, width, alignment, decoded_size);
1160 /* these types already have an implicit pointer, so we don't need to
1161 * add another */
1162 if(vt == VT_DISPATCH || vt == VT_UNKNOWN) return 2;
1163 return 0;
1166 static unsigned int get_ulong_val(unsigned int val, int vt)
1168 switch(vt) {
1169 case VT_I2:
1170 case VT_BOOL:
1171 case VT_UI2:
1172 return val & 0xffff;
1173 case VT_I1:
1174 case VT_UI1:
1175 return val & 0xff;
1178 return val;
1181 static void write_value(msft_typelib_t* typelib, int *out, int vt, const void *value)
1183 switch(vt) {
1184 case VT_I2:
1185 case VT_I4:
1186 case VT_R4:
1187 case VT_BOOL:
1188 case VT_I1:
1189 case VT_UI1:
1190 case VT_UI2:
1191 case VT_UI4:
1192 case VT_INT:
1193 case VT_UINT:
1194 case VT_HRESULT:
1195 case VT_PTR:
1196 case VT_UNKNOWN:
1197 case VT_DISPATCH:
1199 const unsigned int lv = get_ulong_val(*(const unsigned int *)value, vt);
1200 if((lv & 0x3ffffff) == lv) {
1201 *out = 0x80000000;
1202 *out |= vt << 26;
1203 *out |= lv;
1204 } else {
1205 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1206 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1207 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], value, 4);
1208 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1209 *out = offset;
1211 return;
1213 case VT_BSTR:
1215 const char *s = (const char *) value;
1216 int len = strlen(s), seg_len = (len + 6 + 3) & ~0x3;
1217 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1218 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1219 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &len, sizeof(len));
1220 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1221 len += 6;
1222 while(len < seg_len) {
1223 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1224 len++;
1226 *out = offset;
1227 return;
1230 default:
1231 warning("can't write value of type %d yet\n", vt);
1233 return;
1236 static HRESULT set_custdata(msft_typelib_t *typelib, REFGUID guid,
1237 int vt, const void *value, int *offset)
1239 MSFT_GuidEntry guidentry;
1240 int guidoffset;
1241 int custoffset;
1242 int *custdata;
1243 int data_out;
1245 guidentry.guid = *guid;
1247 guidentry.hreftype = -1;
1248 guidentry.next_hash = -1;
1250 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1251 write_value(typelib, &data_out, vt, value);
1253 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1255 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1256 custdata[0] = guidoffset;
1257 custdata[1] = data_out;
1258 custdata[2] = *offset;
1259 *offset = custoffset;
1261 return S_OK;
1264 /* It's possible to have a default value for pointer arguments too.
1265 In this case default value has a referenced type, e.g.
1266 'LONG*' argument gets VT_I4, 'DOUBLE*' - VT_R8. IUnknown* and IDispatch*
1267 are recognised too and stored as VT_UNKNOWN and VT_DISPATCH.
1268 But IUnknown/IDispatch arguments can only have default value of 0
1269 (or expression that resolves to zero) while other pointers can have
1270 any default value. */
1271 static int get_defaultvalue_vt(type_t *type)
1273 int vt;
1274 if (type_get_type(type) == TYPE_ENUM)
1275 vt = VT_I4;
1276 else
1278 vt = get_type_vt(type);
1279 if (vt == VT_PTR && is_ptr(type)) {
1280 vt = get_type_vt(type_pointer_get_ref(type));
1281 /* The only acceptable value for pointers to non-basic types
1282 is NULL, it's stored as VT_I4 for both 32 and 64 bit typelibs. */
1283 if (vt == VT_USERDEFINED)
1284 vt = VT_I4;
1288 return vt;
1291 static HRESULT add_func_desc(msft_typeinfo_t* typeinfo, var_t *func, int index)
1293 int offset, name_offset;
1294 int *typedata, typedata_size;
1295 int i, id, next_idx;
1296 int decoded_size, extra_attr = 0;
1297 int num_params = 0, num_optional = 0, num_defaults = 0;
1298 var_t *arg;
1299 unsigned char *namedata;
1300 const attr_t *attr;
1301 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1302 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1303 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1304 int entry = -1, entry_is_ord = 0;
1305 int lcid_retval_count = 0;
1307 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1309 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1311 switch(typeinfo->typekind) {
1312 case TKIND_DISPATCH:
1313 funckind = 0x4; /* FUNC_DISPATCH */
1314 break;
1315 case TKIND_MODULE:
1316 funckind = 0x3; /* FUNC_STATIC */
1317 break;
1318 default:
1319 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1320 break;
1323 if (is_local( func->attrs )) {
1324 chat("add_func_desc: skipping local function\n");
1325 return S_FALSE;
1328 if (type_get_function_args(func->type))
1329 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1331 num_params++;
1332 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1333 if(attr->type == ATTR_DEFAULTVALUE)
1334 num_defaults++;
1335 else if(attr->type == ATTR_OPTIONAL)
1336 num_optional++;
1340 chat("add_func_desc: num of params %d\n", num_params);
1342 name_offset = ctl2_alloc_name(typeinfo->typelib, func->name);
1344 if (func->attrs) LIST_FOR_EACH_ENTRY( attr, func->attrs, const attr_t, entry ) {
1345 expr_t *expr = attr->u.pval;
1346 switch(attr->type) {
1347 case ATTR_BINDABLE:
1348 funcflags |= 0x4; /* FUNCFLAG_FBINDABLE */
1349 break;
1350 case ATTR_DEFAULTBIND:
1351 funcflags |= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1352 break;
1353 case ATTR_DEFAULTCOLLELEM:
1354 funcflags |= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1355 break;
1356 case ATTR_DISPLAYBIND:
1357 funcflags |= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1358 break;
1359 case ATTR_ENTRY:
1360 extra_attr = max(extra_attr, 3);
1361 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1362 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1363 else {
1364 entry = expr->cval;
1365 entry_is_ord = 1;
1367 break;
1368 case ATTR_HELPCONTEXT:
1369 extra_attr = max(extra_attr, 1);
1370 help_context = expr->u.lval;
1371 break;
1372 case ATTR_HELPSTRING:
1373 extra_attr = max(extra_attr, 2);
1374 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1375 break;
1376 case ATTR_HELPSTRINGCONTEXT:
1377 extra_attr = max(extra_attr, 6);
1378 help_string_context = expr->u.lval;
1379 break;
1380 case ATTR_HIDDEN:
1381 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1382 break;
1383 case ATTR_ID:
1384 id = expr->cval;
1385 break;
1386 case ATTR_IMMEDIATEBIND:
1387 funcflags |= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1388 break;
1389 case ATTR_NONBROWSABLE:
1390 funcflags |= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1391 break;
1392 case ATTR_OUT:
1393 break;
1394 case ATTR_PROPGET:
1395 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1396 break;
1397 case ATTR_PROPPUT:
1398 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1399 break;
1400 case ATTR_PROPPUTREF:
1401 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1402 break;
1403 /* FIXME: FUNCFLAG_FREPLACEABLE */
1404 case ATTR_REQUESTEDIT:
1405 funcflags |= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1406 break;
1407 case ATTR_RESTRICTED:
1408 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1409 break;
1410 case ATTR_SOURCE:
1411 funcflags |= 0x2; /* FUNCFLAG_FSOURCE */
1412 break;
1413 case ATTR_UIDEFAULT:
1414 funcflags |= 0x200; /* FUNCFLAG_FUIDEFAULT */
1415 break;
1416 case ATTR_USESGETLASTERROR:
1417 funcflags |= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1418 break;
1419 case ATTR_VARARG:
1420 if (num_optional || num_defaults)
1421 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1422 else
1423 num_optional = -1;
1424 break;
1425 default:
1426 break;
1430 /* allocate type data space for us */
1431 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1433 if (!typeinfo->func_data) {
1434 typeinfo->func_data = xmalloc(0x100);
1435 typeinfo->func_data_allocated = 0x100;
1436 typeinfo->func_data[0] = 0;
1439 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1440 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1441 typeinfo->func_data[0] + typedata_size + sizeof(int));
1442 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1445 offset = typeinfo->func_data[0];
1446 typeinfo->func_data[0] += typedata_size;
1447 typedata = typeinfo->func_data + (offset >> 2) + 1;
1450 /* find func with the same name - if it exists use its id */
1451 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1452 if(name_offset == typeinfo->func_names[i]) {
1453 id = typeinfo->func_indices[i];
1454 break;
1458 /* find the first func with the same id and link via the hiword of typedata[4] */
1459 next_idx = index;
1460 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1461 if(id == typeinfo->func_indices[i]) {
1462 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1463 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1464 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1465 break;
1469 /* fill out the basic type information */
1470 typedata[0] = typedata_size | (index << 16);
1471 encode_var(typeinfo->typelib, type_function_get_rettype(func->type), func, &typedata[1], NULL, NULL, &decoded_size);
1472 typedata[2] = funcflags;
1473 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1474 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1475 if(num_defaults) typedata[4] |= 0x1000;
1476 if(entry_is_ord) typedata[4] |= 0x2000;
1477 typedata[5] = (num_optional << 16) | num_params;
1479 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1480 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1481 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1482 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1484 switch(extra_attr) {
1485 case 6: typedata[11] = help_string_context;
1486 case 5: typedata[10] = -1;
1487 case 4: typedata[9] = -1;
1488 case 3: typedata[8] = entry;
1489 case 2: typedata[7] = help_string_offset;
1490 case 1: typedata[6] = help_context;
1491 case 0:
1492 break;
1493 default:
1494 warning("unknown number of optional attrs\n");
1497 if (type_get_function_args(func->type))
1499 i = 0;
1500 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1502 int paramflags = 0;
1503 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1504 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1506 if(defaultdata) *defaultdata = -1;
1508 encode_var(typeinfo->typelib, arg->type, arg, paramdata, NULL, NULL, &decoded_size);
1509 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1510 switch(attr->type) {
1511 case ATTR_DEFAULTVALUE:
1513 int vt;
1514 expr_t *expr = (expr_t *)attr->u.pval;
1515 vt = get_defaultvalue_vt(arg->type);
1516 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1517 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1519 if (vt != VT_BSTR) error("string default value applied to non-string type\n");
1520 chat("default value '%s'\n", expr->u.sval);
1521 write_value(typeinfo->typelib, defaultdata, vt, expr->u.sval);
1523 else
1525 chat("default value %d\n", expr->cval);
1526 write_value(typeinfo->typelib, defaultdata, vt, &expr->cval);
1528 break;
1530 case ATTR_IN:
1531 paramflags |= 0x01; /* PARAMFLAG_FIN */
1532 break;
1533 case ATTR_OPTIONAL:
1534 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1535 break;
1536 case ATTR_OUT:
1537 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1538 break;
1539 case ATTR_PARAMLCID:
1540 paramflags |= 0x04; /* PARAMFLAG_LCID */
1541 lcid_retval_count++;
1542 break;
1543 case ATTR_RETVAL:
1544 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1545 lcid_retval_count++;
1546 break;
1547 default:
1548 chat("unhandled param attr %d\n", attr->type);
1549 break;
1552 paramdata[1] = -1;
1553 paramdata[2] = paramflags;
1554 typedata[3] += decoded_size << 16;
1556 i++;
1560 if(lcid_retval_count == 1)
1561 typedata[4] |= 0x4000;
1562 else if(lcid_retval_count == 2)
1563 typedata[4] |= 0x8000;
1565 if(typeinfo->funcs_allocated == 0) {
1566 typeinfo->funcs_allocated = 10;
1567 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1568 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1569 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1571 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1572 typeinfo->funcs_allocated *= 2;
1573 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1574 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1575 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1578 /* update the index data */
1579 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1580 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1581 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1583 /* ??? */
1584 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1585 typeinfo->typeinfo->res2 <<= 1;
1586 /* ??? */
1587 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1589 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1590 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1591 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1593 /* adjust size of VTBL */
1594 if(funckind != 0x3 /* FUNC_STATIC */)
1595 typeinfo->typeinfo->cbSizeVft += pointer_size;
1597 /* Increment the number of function elements */
1598 typeinfo->typeinfo->cElement += 1;
1600 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1601 if (*((INT *)namedata) == -1) {
1602 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1603 if(typeinfo->typekind == TKIND_MODULE)
1604 namedata[9] |= 0x10;
1605 } else
1606 namedata[9] &= ~0x10;
1608 if(typeinfo->typekind == TKIND_MODULE)
1609 namedata[9] |= 0x20;
1611 if (type_get_function_args(func->type))
1613 i = 0;
1614 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1616 /* don't give the last arg of a [propput*] func a name */
1617 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1619 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1620 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1621 paramdata[1] = offset;
1623 i++;
1626 return S_OK;
1629 static HRESULT add_var_desc(msft_typeinfo_t *typeinfo, UINT index, var_t* var)
1631 int offset, id;
1632 unsigned int typedata_size;
1633 INT *typedata;
1634 int var_datawidth;
1635 int var_alignment;
1636 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1637 int alignment;
1638 int varflags = 0;
1639 const attr_t *attr;
1640 unsigned char *namedata;
1641 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1643 chat("add_var_desc(%d, %s)\n", index, var->name);
1645 id = 0x40000000 + index;
1647 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1648 expr_t *expr = attr->u.pval;
1649 switch(attr->type) {
1650 case ATTR_BINDABLE:
1651 varflags |= 0x04; /* VARFLAG_FBINDABLE */
1652 break;
1653 case ATTR_DEFAULTBIND:
1654 varflags |= 0x20; /* VARFLAG_FDEFAULTBIND */
1655 break;
1656 case ATTR_DEFAULTCOLLELEM:
1657 varflags |= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1658 break;
1659 case ATTR_DISPLAYBIND:
1660 varflags |= 0x10; /* VARFLAG_FDISPLAYBIND */
1661 break;
1662 case ATTR_HIDDEN:
1663 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1664 break;
1665 case ATTR_ID:
1666 id = expr->cval;
1667 break;
1668 case ATTR_IMMEDIATEBIND:
1669 varflags |= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1670 break;
1671 case ATTR_NONBROWSABLE:
1672 varflags |= 0x400; /* VARFLAG_FNONBROWSABLE */
1673 break;
1674 case ATTR_READONLY:
1675 varflags |= 0x01; /* VARFLAG_FREADONLY */
1676 break;
1677 /* FIXME: VARFLAG_FREPLACEABLE */
1678 case ATTR_REQUESTEDIT:
1679 varflags |= 0x08; /* VARFLAG_FREQUESTEDIT */
1680 break;
1681 case ATTR_RESTRICTED:
1682 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1683 break;
1684 case ATTR_SOURCE:
1685 varflags |= 0x02; /* VARFLAG_FSOURCE */
1686 break;
1687 case ATTR_UIDEFAULT:
1688 varflags |= 0x0200; /* VARFLAG_FUIDEFAULT */
1689 break;
1690 default:
1691 break;
1695 /* allocate type data space for us */
1696 typedata_size = 0x14;
1698 if (!typeinfo->var_data) {
1699 typeinfo->var_data = xmalloc(0x100);
1700 typeinfo->var_data_allocated = 0x100;
1701 typeinfo->var_data[0] = 0;
1704 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1705 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1706 typeinfo->var_data[0] + typedata_size + sizeof(int));
1707 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1710 offset = typeinfo->var_data[0];
1711 typeinfo->var_data[0] += typedata_size;
1712 typedata = typeinfo->var_data + (offset >> 2) + 1;
1714 /* fill out the basic type information */
1715 typedata[0] = typedata_size | (index << 16);
1716 typedata[2] = varflags;
1717 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1719 if(typeinfo->vars_allocated == 0) {
1720 typeinfo->vars_allocated = 10;
1721 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1722 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1723 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1725 if(typeinfo->vars_allocated == var_num) {
1726 typeinfo->vars_allocated *= 2;
1727 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1728 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1729 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1731 /* update the index data */
1732 typeinfo->var_indices[var_num] = id;
1733 typeinfo->var_names[var_num] = -1;
1734 typeinfo->var_offsets[var_num] = offset;
1736 /* figure out type widths and whatnot */
1737 encode_var(typeinfo->typelib, var->type, var, &typedata[1], &var_datawidth,
1738 &var_alignment, &var_type_size);
1740 /* pad out starting position to data width */
1741 typeinfo->datawidth += var_alignment - 1;
1742 typeinfo->datawidth &= ~(var_alignment - 1);
1744 switch(typeinfo->typekind) {
1745 case TKIND_ENUM:
1746 write_value(typeinfo->typelib, &typedata[4], VT_I4, &var->eval->cval);
1747 var_kind = 2; /* VAR_CONST */
1748 var_type_size += 16; /* sizeof(VARIANT) */
1749 typeinfo->datawidth = var_datawidth;
1750 break;
1751 case TKIND_RECORD:
1752 typedata[4] = typeinfo->datawidth;
1753 typeinfo->datawidth += var_datawidth;
1754 break;
1755 case TKIND_DISPATCH:
1756 var_kind = 3; /* VAR_DISPATCH */
1757 typeinfo->datawidth = pointer_size;
1758 var_alignment = 4;
1759 break;
1760 default:
1761 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1762 break;
1765 /* add type description size to total required allocation */
1766 typedata[3] += var_type_size << 16 | var_kind;
1768 /* fix type alignment */
1769 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1770 if (alignment < var_alignment) {
1771 alignment = var_alignment;
1772 typeinfo->typeinfo->typekind &= ~0xffc0;
1773 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1776 /* ??? */
1777 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1778 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1779 typeinfo->typeinfo->res2 <<= 1;
1782 /* ??? */
1783 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1784 typeinfo->typeinfo->res3 += 0x2c;
1786 /* increment the number of variable elements */
1787 typeinfo->typeinfo->cElement += 0x10000;
1789 /* pad data width to alignment */
1790 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1792 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1793 if (offset == -1) return E_OUTOFMEMORY;
1795 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1796 if (*((INT *)namedata) == -1) {
1797 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1798 if(typeinfo->typekind != TKIND_DISPATCH)
1799 namedata[9] |= 0x10;
1800 } else
1801 namedata[9] &= ~0x10;
1803 if (typeinfo->typekind == TKIND_ENUM) {
1804 namedata[9] |= 0x20;
1806 typeinfo->var_names[var_num] = offset;
1808 return S_OK;
1811 static HRESULT add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1813 if(importinfo) {
1814 alloc_importinfo(typeinfo->typelib, importinfo);
1815 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1816 }else {
1817 if(ref->typelib_idx == -1)
1818 add_interface_typeinfo(typeinfo->typelib, ref);
1819 if(ref->typelib_idx == -1)
1820 error("add_impl_type: unable to add inherited interface\n");
1822 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1825 typeinfo->typeinfo->cImplTypes++;
1826 return S_OK;
1829 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1830 const char *name, const attr_list_t *attrs)
1832 const attr_t *attr;
1833 msft_typeinfo_t *msft_typeinfo;
1834 int nameoffset;
1835 int typeinfo_offset;
1836 MSFT_TypeInfoBase *typeinfo;
1837 MSFT_GuidEntry guidentry;
1839 chat("create_msft_typeinfo: name %s kind %d\n", name, kind);
1841 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1842 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1844 msft_typeinfo->typelib = typelib;
1846 nameoffset = ctl2_alloc_name(typelib, name);
1847 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1848 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1850 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1851 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1853 msft_typeinfo->typekind = kind;
1854 msft_typeinfo->typeinfo = typeinfo;
1856 typeinfo->typekind |= kind | 0x20;
1858 if(kind == TKIND_COCLASS)
1859 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1861 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1862 switch(attr->type) {
1863 case ATTR_AGGREGATABLE:
1864 if (kind == TKIND_COCLASS)
1865 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1866 break;
1868 case ATTR_APPOBJECT:
1869 if (kind == TKIND_COCLASS)
1870 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1871 break;
1873 case ATTR_CONTROL:
1874 if (kind == TKIND_COCLASS)
1875 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1876 break;
1878 case ATTR_DLLNAME:
1880 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1881 typeinfo->datatype1 = offset;
1882 break;
1885 case ATTR_DUAL:
1886 /* FIXME: check interface is compatible */
1887 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1888 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1889 break;
1891 case ATTR_HELPCONTEXT:
1893 expr_t *expr = (expr_t*)attr->u.pval;
1894 typeinfo->helpcontext = expr->cval;
1895 break;
1897 case ATTR_HELPSTRING:
1899 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1900 if (offset == -1) break;
1901 typeinfo->docstringoffs = offset;
1902 break;
1904 case ATTR_HELPSTRINGCONTEXT:
1906 expr_t *expr = (expr_t*)attr->u.pval;
1907 typeinfo->helpstringcontext = expr->cval;
1908 break;
1910 case ATTR_HIDDEN:
1911 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1912 break;
1914 case ATTR_LICENSED:
1915 typeinfo->flags |= 0x04; /* TYPEFLAG_FLICENSED */
1916 break;
1918 case ATTR_NONCREATABLE:
1919 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1920 break;
1922 case ATTR_NONEXTENSIBLE:
1923 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1924 break;
1926 case ATTR_OLEAUTOMATION:
1927 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1928 break;
1930 /* FIXME: TYPEFLAG_FPREDCLID */
1932 case ATTR_PROXY:
1933 typeinfo->flags |= 0x4000; /* TYPEFLAG_FPROXY */
1934 break;
1936 /* FIXME: TYPEFLAG_FREPLACEABLE */
1938 case ATTR_RESTRICTED:
1939 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
1940 break;
1942 case ATTR_UUID:
1943 guidentry.guid = *(GUID*)attr->u.pval;
1944 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1945 guidentry.next_hash = -1;
1946 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
1947 #if 0
1948 if (IsEqualIID(guid, &IID_IDispatch)) {
1949 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1951 #endif
1952 break;
1954 case ATTR_VERSION:
1955 typeinfo->version = attr->u.ival;
1956 break;
1958 default:
1959 break;
1963 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
1964 typelib->last_typeinfo = msft_typeinfo;
1965 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
1968 return msft_typeinfo;
1971 static void add_dispatch(msft_typelib_t *typelib)
1973 int guid_offset, impfile_offset;
1974 MSFT_GuidEntry guidentry;
1975 MSFT_ImpInfo impinfo;
1976 GUID stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1977 GUID iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
1979 if(typelib->typelib_header.dispatchpos != -1) return;
1981 guidentry.guid = stdole;
1982 guidentry.hreftype = 2;
1983 guidentry.next_hash = -1;
1984 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
1985 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
1987 guidentry.guid = iid_idispatch;
1988 guidentry.hreftype = 1;
1989 guidentry.next_hash = -1;
1990 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
1991 impinfo.oImpFile = impfile_offset;
1992 impinfo.oGuid = ctl2_alloc_guid(typelib, &guidentry);
1993 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
1996 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
1998 int idx = 0;
1999 var_t *func;
2000 var_t *var;
2001 msft_typeinfo_t *msft_typeinfo;
2003 if (-1 < dispinterface->typelib_idx)
2004 return;
2006 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2007 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
2008 dispinterface->attrs);
2010 msft_typeinfo->typeinfo->size = pointer_size;
2011 msft_typeinfo->typeinfo->typekind |= 0x2100;
2013 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2014 add_dispatch(typelib);
2015 msft_typeinfo->typeinfo->cImplTypes = 1;
2017 /* count the no of methods, as the variable indices come after the funcs */
2018 if (dispinterface->details.iface->disp_methods)
2019 LIST_FOR_EACH_ENTRY( func, dispinterface->details.iface->disp_methods, var_t, entry )
2020 idx++;
2022 if (type_dispiface_get_props(dispinterface))
2023 LIST_FOR_EACH_ENTRY( var, type_dispiface_get_props(dispinterface), var_t, entry )
2024 add_var_desc(msft_typeinfo, idx++, var);
2026 if (type_dispiface_get_methods(dispinterface))
2028 idx = 0;
2029 LIST_FOR_EACH_ENTRY( func, type_dispiface_get_methods(dispinterface), var_t, entry )
2030 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2031 idx++;
2035 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
2037 int idx = 0;
2038 const statement_t *stmt_func;
2039 type_t *ref;
2040 msft_typeinfo_t *msft_typeinfo;
2041 importinfo_t *ref_importinfo = NULL;
2042 int num_parents = 0, num_funcs = 0;
2043 type_t *inherit;
2044 const type_t *derived;
2046 if (-1 < interface->typelib_idx)
2047 return;
2049 if (!interface->details.iface)
2051 error( "interface %s is referenced but not defined\n", interface->name );
2052 return;
2055 if (is_attr(interface->attrs, ATTR_DISPINTERFACE)) {
2056 add_dispinterface_typeinfo(typelib, interface);
2057 return;
2060 /* midl adds the parent interface first, unless the parent itself
2061 has no parent (i.e. it stops before IUnknown). */
2063 inherit = type_iface_get_inherit(interface);
2065 if(inherit) {
2066 ref_importinfo = find_importinfo(typelib, inherit->name);
2068 if(!ref_importinfo && type_iface_get_inherit(inherit) &&
2069 inherit->typelib_idx == -1)
2070 add_interface_typeinfo(typelib, inherit);
2073 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2074 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
2075 msft_typeinfo->typeinfo->size = pointer_size;
2076 msft_typeinfo->typeinfo->typekind |= 0x2200;
2078 for (derived = inherit; derived; derived = type_iface_get_inherit(derived))
2079 if (derived->name && !strcmp(derived->name, "IDispatch"))
2080 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2082 /* can't be dual if it doesn't derive from IDispatch */
2083 if (!(msft_typeinfo->typeinfo->flags & 0x1000)) /* TYPEFLAG_FDISPATCHABLE */
2084 msft_typeinfo->typeinfo->flags &= ~0x40; /* TYPEFLAG_FDUAL */
2086 if(type_iface_get_inherit(interface))
2087 add_impl_type(msft_typeinfo, type_iface_get_inherit(interface),
2088 ref_importinfo);
2090 /* count the number of inherited interfaces and non-local functions */
2091 for(ref = inherit; ref; ref = type_iface_get_inherit(ref)) {
2092 num_parents++;
2093 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) ) {
2094 var_t *func = stmt_func->u.var;
2095 if (!is_local(func->attrs)) num_funcs++;
2098 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2099 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2101 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(interface) ) {
2102 var_t *func = stmt_func->u.var;
2103 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2104 idx++;
2108 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2110 int idx = 0;
2111 var_t *cur;
2112 msft_typeinfo_t *msft_typeinfo;
2114 if (-1 < structure->typelib_idx)
2115 return;
2117 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2118 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2119 msft_typeinfo->typeinfo->size = 0;
2121 if (type_struct_get_fields(structure))
2122 LIST_FOR_EACH_ENTRY( cur, type_struct_get_fields(structure), var_t, entry )
2123 add_var_desc(msft_typeinfo, idx++, cur);
2126 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2128 int idx = 0;
2129 var_t *cur;
2130 msft_typeinfo_t *msft_typeinfo;
2132 if (-1 < enumeration->typelib_idx)
2133 return;
2135 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2136 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2137 msft_typeinfo->typeinfo->size = 0;
2139 if (type_enum_get_values(enumeration))
2140 LIST_FOR_EACH_ENTRY( cur, type_enum_get_values(enumeration), var_t, entry )
2141 add_var_desc(msft_typeinfo, idx++, cur);
2144 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2146 msft_typeinfo_t *msft_typeinfo;
2147 int alignment;
2149 if (-1 < tdef->typelib_idx)
2150 return;
2152 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2153 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2154 encode_type(typelib, get_type_vt(type_alias_get_aliasee(tdef)),
2155 type_alias_get_aliasee(tdef),
2156 &msft_typeinfo->typeinfo->datatype1,
2157 &msft_typeinfo->typeinfo->size,
2158 &alignment, &msft_typeinfo->typeinfo->datatype2);
2159 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2162 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2164 msft_typeinfo_t *msft_typeinfo;
2165 ifref_t *iref;
2166 int num_ifaces = 0, offset, i;
2167 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2168 int have_default = 0, have_default_source = 0;
2169 const attr_t *attr;
2170 ifref_list_t *ifaces;
2172 if (-1 < cls->typelib_idx)
2173 return;
2175 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2176 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2178 ifaces = type_coclass_get_ifaces(cls);
2179 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) num_ifaces++;
2181 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2182 num_ifaces * sizeof(*ref), 0);
2184 i = 0;
2185 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) {
2186 if(iref->iface->typelib_idx == -1)
2187 add_interface_typeinfo(typelib, iref->iface);
2188 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2189 ref->reftype = typelib->typelib_typeinfo_offsets[iref->iface->typelib_idx];
2190 ref->flags = 0;
2191 ref->oCustData = -1;
2192 ref->onext = -1;
2193 if(i < num_ifaces - 1)
2194 ref->onext = offset + (i + 1) * sizeof(*ref);
2196 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2197 switch(attr->type) {
2198 case ATTR_DEFAULT:
2199 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2200 break;
2201 case ATTR_DEFAULTVTABLE:
2202 ref->flags |= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2203 break;
2204 case ATTR_RESTRICTED:
2205 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2206 break;
2207 case ATTR_SOURCE:
2208 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2209 break;
2210 default:
2211 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2214 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2215 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2216 have_default_source = 1;
2217 else
2218 have_default = 1;
2221 /* If the interface is non-restricted and we haven't already had one then
2222 remember it so that we can use it as a default later */
2223 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2224 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2225 if(!first_source)
2226 first_source = ref;
2228 else if(!first)
2229 first = ref;
2231 i++;
2234 /* If we haven't had a default interface, then set the default flags on the
2235 first ones */
2236 if(!have_default && first)
2237 first->flags |= 0x1;
2238 if(!have_default_source && first_source)
2239 first_source->flags |= 0x1;
2241 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2242 msft_typeinfo->typeinfo->size = pointer_size;
2243 msft_typeinfo->typeinfo->typekind |= 0x2200;
2246 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2248 int idx = 0;
2249 const statement_t *stmt;
2250 msft_typeinfo_t *msft_typeinfo;
2252 if (-1 < module->typelib_idx)
2253 return;
2255 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2256 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2257 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2259 STATEMENTS_FOR_EACH_FUNC( stmt, module->details.module->stmts ) {
2260 var_t *func = stmt->u.var;
2261 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2262 idx++;
2265 msft_typeinfo->typeinfo->size = idx;
2268 static void add_type_typeinfo(msft_typelib_t *typelib, type_t *type)
2270 switch (type_get_type(type)) {
2271 case TYPE_INTERFACE:
2272 add_interface_typeinfo(typelib, type);
2273 break;
2274 case TYPE_STRUCT:
2275 add_structure_typeinfo(typelib, type);
2276 break;
2277 case TYPE_ENUM:
2278 add_enum_typeinfo(typelib, type);
2279 break;
2280 case TYPE_COCLASS:
2281 add_coclass_typeinfo(typelib, type);
2282 break;
2283 case TYPE_BASIC:
2284 case TYPE_POINTER:
2285 break;
2286 default:
2287 error("add_entry: unhandled type 0x%x for %s\n",
2288 type_get_type(type), type->name);
2289 break;
2293 static void add_entry(msft_typelib_t *typelib, const statement_t *stmt)
2295 switch(stmt->type) {
2296 case STMT_LIBRARY:
2297 case STMT_IMPORT:
2298 case STMT_PRAGMA:
2299 case STMT_CPPQUOTE:
2300 case STMT_DECLARATION:
2301 /* not included in typelib */
2302 break;
2303 case STMT_IMPORTLIB:
2304 /* not processed here */
2305 break;
2306 case STMT_TYPEDEF:
2308 const type_list_t *type_entry = stmt->u.type_list;
2309 for (; type_entry; type_entry = type_entry->next) {
2310 /* if the type is public then add the typedef, otherwise attempt
2311 * to add the aliased type */
2312 if (is_attr(type_entry->type->attrs, ATTR_PUBLIC))
2313 add_typedef_typeinfo(typelib, type_entry->type);
2314 else
2315 add_type_typeinfo(typelib, type_alias_get_aliasee(type_entry->type));
2317 break;
2319 case STMT_MODULE:
2320 add_module_typeinfo(typelib, stmt->u.type);
2321 break;
2322 case STMT_TYPE:
2323 case STMT_TYPEREF:
2325 type_t *type = stmt->u.type;
2326 add_type_typeinfo(typelib, type);
2327 break;
2332 static void set_name(msft_typelib_t *typelib)
2334 int offset;
2336 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2337 if (offset == -1) return;
2338 typelib->typelib_header.NameOffset = offset;
2339 return;
2342 static void set_version(msft_typelib_t *typelib)
2344 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2347 static void set_guid(msft_typelib_t *typelib)
2349 MSFT_GuidEntry guidentry;
2350 int offset;
2351 void *ptr;
2352 GUID guid = {0,0,0,{0,0,0,0,0,0}};
2354 guidentry.guid = guid;
2355 guidentry.hreftype = -2;
2356 guidentry.next_hash = -1;
2358 ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2359 if (ptr) guidentry.guid = *(GUID *)ptr;
2361 offset = ctl2_alloc_guid(typelib, &guidentry);
2362 typelib->typelib_header.posguid = offset;
2364 return;
2367 static void set_doc_string(msft_typelib_t *typelib)
2369 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2371 if (str)
2373 int offset = ctl2_alloc_string(typelib, str);
2374 if (offset != -1) typelib->typelib_header.helpstring = offset;
2378 static void set_help_file_name(msft_typelib_t *typelib)
2380 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2382 if (str)
2384 int offset = ctl2_alloc_string(typelib, str);
2385 if (offset != -1)
2387 typelib->typelib_header.helpfile = offset;
2388 typelib->typelib_header.varflags |= 0x10;
2393 static void set_help_context(msft_typelib_t *typelib)
2395 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2396 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2399 static void set_help_string_dll(msft_typelib_t *typelib)
2401 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2403 if (str)
2405 int offset = ctl2_alloc_string(typelib, str);
2406 if (offset != -1)
2408 typelib->help_string_dll_offset = offset;
2409 typelib->typelib_header.varflags |= 0x100;
2414 static void set_help_string_context(msft_typelib_t *typelib)
2416 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2417 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2420 static void set_lcid(msft_typelib_t *typelib)
2422 const expr_t *lcid_expr = get_attrp( typelib->typelib->attrs, ATTR_LIBLCID );
2423 if(lcid_expr)
2425 typelib->typelib_header.lcid = lcid_expr->cval;
2426 typelib->typelib_header.lcid2 = lcid_expr->cval;
2430 static void set_lib_flags(msft_typelib_t *typelib)
2432 const attr_t *attr;
2434 typelib->typelib_header.flags = 0;
2435 if (!typelib->typelib->attrs) return;
2436 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2438 switch(attr->type) {
2439 case ATTR_CONTROL:
2440 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2441 break;
2442 case ATTR_HIDDEN:
2443 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2444 break;
2445 case ATTR_RESTRICTED:
2446 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2447 break;
2448 default:
2449 break;
2452 return;
2455 static void ctl2_write_segment(msft_typelib_t *typelib, int segment)
2457 put_data(typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length);
2460 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2462 msft_typeinfo_t *typeinfo;
2464 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2465 typeinfo->typeinfo->memoffset = filesize;
2466 if (typeinfo->func_data)
2467 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2468 if (typeinfo->var_data)
2469 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2470 if (typeinfo->func_data || typeinfo->var_data)
2471 filesize += 4;
2475 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2477 if (typelib->typelib_segdir[segment].length) {
2478 typelib->typelib_segdir[segment].offset = filepos;
2479 } else {
2480 typelib->typelib_segdir[segment].offset = -1;
2483 return typelib->typelib_segdir[segment].length;
2487 static void ctl2_write_typeinfos(msft_typelib_t *typelib)
2489 msft_typeinfo_t *typeinfo;
2490 int typedata_size;
2492 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2493 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2494 typedata_size = 0;
2495 if (typeinfo->func_data)
2496 typedata_size = typeinfo->func_data[0];
2497 if (typeinfo->var_data)
2498 typedata_size += typeinfo->var_data[0];
2499 put_data(&typedata_size, sizeof(int));
2500 if (typeinfo->func_data)
2501 put_data(typeinfo->func_data + 1, typeinfo->func_data[0]);
2502 if (typeinfo->var_data)
2503 put_data(typeinfo->var_data + 1, typeinfo->var_data[0]);
2504 if (typeinfo->func_indices)
2505 put_data(typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2506 if (typeinfo->var_indices)
2507 put_data(typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2508 if (typeinfo->func_names)
2509 put_data(typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2510 if (typeinfo->var_names)
2511 put_data(typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2512 if (typeinfo->func_offsets)
2513 put_data(typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2514 if (typeinfo->var_offsets) {
2515 int add = 0, i, offset;
2516 if(typeinfo->func_data)
2517 add = typeinfo->func_data[0];
2518 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2519 offset = typeinfo->var_offsets[i];
2520 offset += add;
2521 put_data(&offset, 4);
2527 static void save_all_changes(msft_typelib_t *typelib)
2529 int filepos;
2531 chat("save_all_changes(%p)\n", typelib);
2533 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2534 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2535 filepos += typelib->typelib_header.nrtypeinfos * 4;
2537 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2538 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2539 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2540 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2541 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2542 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2543 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2544 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2545 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2546 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2547 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2548 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2549 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2551 ctl2_finalize_typeinfos(typelib, filepos);
2553 byte_swapped = 0;
2554 init_output_buffer();
2556 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2557 if(typelib->typelib_header.varflags & 0x100)
2558 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2560 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2561 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2562 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2563 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2564 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2565 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2566 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2567 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2568 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2569 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2570 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2571 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2572 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2573 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2574 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2576 ctl2_write_typeinfos(typelib);
2578 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2580 char typelib_id[13] = "#1";
2582 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2583 if (expr)
2584 sprintf( typelib_id, "#%d", expr->cval );
2585 add_output_to_resources( "TYPELIB", typelib_id );
2586 output_typelib_regscript( typelib->typelib );
2587 flush_output_resources( typelib_name );
2589 else flush_output_buffer( typelib_name );
2592 int create_msft_typelib(typelib_t *typelib)
2594 msft_typelib_t *msft;
2595 int failed = 0;
2596 const statement_t *stmt;
2597 time_t cur_time;
2598 char *time_override;
2599 unsigned int version = 7 << 24 | 555; /* 7.00.0555 */
2600 GUID midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2601 GUID midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2602 GUID midl_info_guid = {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2603 char info_string[128];
2605 pointer_size = (typelib_kind == SYS_WIN64) ? 8 : 4;
2607 msft = xmalloc(sizeof(*msft));
2608 memset(msft, 0, sizeof(*msft));
2609 msft->typelib = typelib;
2611 ctl2_init_header(msft);
2612 ctl2_init_segdir(msft);
2614 msft->typelib_header.varflags |= typelib_kind;
2617 * The following two calls return an offset or -1 if out of memory. We
2618 * specifically need an offset of 0, however, so...
2620 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2621 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2623 if(failed)
2625 free(msft);
2626 return 0;
2629 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2630 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2632 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2633 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2635 set_lib_flags(msft);
2636 set_lcid(msft);
2637 set_help_file_name(msft);
2638 set_doc_string(msft);
2639 set_guid(msft);
2640 set_version(msft);
2641 set_name(msft);
2642 set_help_context(msft);
2643 set_help_string_dll(msft);
2644 set_help_string_context(msft);
2646 /* midl adds two sets of custom data to the library: the current unix time
2647 and midl's version number */
2648 time_override = getenv( "WIDL_TIME_OVERRIDE");
2649 cur_time = time_override ? atol( time_override) : time(NULL);
2650 sprintf(info_string, "Created by WIDL version %s at %s\n", PACKAGE_VERSION, ctime(&cur_time));
2651 set_custdata(msft, &midl_info_guid, VT_BSTR, info_string, &msft->typelib_header.CustomDataOffset);
2652 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2653 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2655 if (typelib->stmts)
2656 LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
2657 add_entry(msft, stmt);
2659 save_all_changes(msft);
2660 free(msft);
2661 return 1;