makedep: Don't add dependencies for tests of disabled dlls.
[wine.git] / tools / widl / write_msft.c
blob457b20df259036c6264d8b0fe11fd812ae404088
1 /*
2 * Typelib v2 (MSFT) generation
4 * Copyright 2004 Alastair Bridgewater
5 * 2004, 2005 Huw Davies
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * --------------------------------------------------------------------------------------
22 * Known problems:
24 * Badly incomplete.
26 * Only works on little-endian systems.
30 #include "config.h"
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <time.h>
39 #define NONAMELESSUNION
41 #include "widl.h"
42 #include "typelib.h"
43 #include "typelib_struct.h"
44 #include "utils.h"
45 #include "header.h"
46 #include "hash.h"
47 #include "typetree.h"
48 #include "parser.h"
49 #include "typegen.h"
51 enum MSFT_segment_index {
52 MSFT_SEG_TYPEINFO = 0, /* type information */
53 MSFT_SEG_IMPORTINFO, /* import information */
54 MSFT_SEG_IMPORTFILES, /* import filenames */
55 MSFT_SEG_REFERENCES, /* references (?) */
56 MSFT_SEG_GUIDHASH, /* hash table for guids? */
57 MSFT_SEG_GUID, /* guid storage */
58 MSFT_SEG_NAMEHASH, /* hash table for names */
59 MSFT_SEG_NAME, /* name storage */
60 MSFT_SEG_STRING, /* string storage */
61 MSFT_SEG_TYPEDESC, /* type descriptions */
62 MSFT_SEG_ARRAYDESC, /* array descriptions */
63 MSFT_SEG_CUSTDATA, /* custom data */
64 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
65 MSFT_SEG_UNKNOWN, /* ??? */
66 MSFT_SEG_UNKNOWN2, /* ??? */
67 MSFT_SEG_MAX /* total number of segments */
70 typedef struct tagMSFT_ImpFile {
71 int guid;
72 int lcid;
73 int version;
74 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
75 } MSFT_ImpFile;
77 typedef struct _msft_typelib_t
79 typelib_t *typelib;
80 MSFT_Header typelib_header;
81 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
82 unsigned char *typelib_segment_data[MSFT_SEG_MAX];
83 int typelib_segment_block_length[MSFT_SEG_MAX];
85 int typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
87 int *typelib_namehash_segment;
88 int *typelib_guidhash_segment;
90 int help_string_dll_offset;
92 struct _msft_typeinfo_t *typeinfos;
93 struct _msft_typeinfo_t *last_typeinfo;
94 } msft_typelib_t;
96 typedef struct _msft_typeinfo_t
98 msft_typelib_t *typelib;
99 MSFT_TypeInfoBase *typeinfo;
101 int typekind;
103 unsigned int var_data_allocated;
104 int *var_data;
106 unsigned int func_data_allocated;
107 int *func_data;
109 int vars_allocated;
110 int *var_indices;
111 int *var_names;
112 int *var_offsets;
114 int funcs_allocated;
115 int *func_indices;
116 int *func_names;
117 int *func_offsets;
119 int datawidth;
121 struct _msft_typeinfo_t *next_typeinfo;
122 } msft_typeinfo_t;
126 /*================== Internal functions ===================================*/
128 /****************************************************************************
129 * ctl2_init_header
131 * Initializes the type library header of a new typelib.
133 static void ctl2_init_header(
134 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
136 typelib->typelib_header.magic1 = 0x5446534d;
137 typelib->typelib_header.magic2 = 0x00010002;
138 typelib->typelib_header.posguid = -1;
139 typelib->typelib_header.lcid = 0x0409;
140 typelib->typelib_header.lcid2 = 0x0;
141 typelib->typelib_header.varflags = 0x40;
142 typelib->typelib_header.version = 0;
143 typelib->typelib_header.flags = 0;
144 typelib->typelib_header.nrtypeinfos = 0;
145 typelib->typelib_header.helpstring = -1;
146 typelib->typelib_header.helpstringcontext = 0;
147 typelib->typelib_header.helpcontext = 0;
148 typelib->typelib_header.nametablecount = 0;
149 typelib->typelib_header.nametablechars = 0;
150 typelib->typelib_header.NameOffset = -1;
151 typelib->typelib_header.helpfile = -1;
152 typelib->typelib_header.CustomDataOffset = -1;
153 typelib->typelib_header.res44 = 0x20;
154 typelib->typelib_header.res48 = 0x80;
155 typelib->typelib_header.dispatchpos = -1;
156 typelib->typelib_header.nimpinfos = 0;
159 /****************************************************************************
160 * ctl2_init_segdir
162 * Initializes the segment directory of a new typelib.
164 static void ctl2_init_segdir(
165 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
167 int i;
168 MSFT_pSeg *segdir;
170 segdir = &typelib->typelib_segdir[MSFT_SEG_TYPEINFO];
172 for (i = 0; i < MSFT_SEG_MAX; i++) {
173 segdir[i].offset = -1;
174 segdir[i].length = 0;
175 segdir[i].res08 = -1;
176 segdir[i].res0c = 0x0f;
180 /****************************************************************************
181 * ctl2_hash_guid
183 * Generates a hash key from a GUID.
185 * RETURNS
187 * The hash key for the GUID.
189 static int ctl2_hash_guid(const struct uuid *guid)
191 int hash;
192 int i;
194 hash = 0;
195 for (i = 0; i < 8; i ++) {
196 hash ^= ((const short *)guid)[i];
199 return hash & 0x1f;
202 /****************************************************************************
203 * ctl2_find_guid
205 * Locates a guid in a type library.
207 * RETURNS
209 * The offset into the GUID segment of the guid, or -1 if not found.
211 static int ctl2_find_guid(
212 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
213 int hash_key, /* [I] The hash key for the guid. */
214 const struct uuid *guid) /* [I] The guid to find. */
216 int offset;
217 MSFT_GuidEntry *guidentry;
219 offset = typelib->typelib_guidhash_segment[hash_key];
220 while (offset != -1) {
221 guidentry = (MSFT_GuidEntry *)&typelib->typelib_segment_data[MSFT_SEG_GUID][offset];
223 if (!memcmp(guidentry, guid, sizeof(*guid))) return offset;
225 offset = guidentry->next_hash;
228 return offset;
231 /****************************************************************************
232 * ctl2_find_name
234 * Locates a name in a type library.
236 * RETURNS
238 * The offset into the NAME segment of the name, or -1 if not found.
240 * NOTES
242 * The name must be encoded as with ctl2_encode_name().
244 static int ctl2_find_name(
245 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
246 char *name) /* [I] The encoded name to find. */
248 int offset;
249 int *namestruct;
251 offset = typelib->typelib_namehash_segment[name[2] & 0x7f];
252 while (offset != -1) {
253 namestruct = (int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][offset];
255 if (!((namestruct[2] ^ *((int *)name)) & 0xffff00ff)) {
256 /* hash codes and lengths match, final test */
257 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
260 /* move to next item in hash bucket */
261 offset = namestruct[1];
264 return offset;
267 /****************************************************************************
268 * ctl2_encode_name
270 * Encodes a name string to a form suitable for storing into a type library
271 * or comparing to a name stored in a type library.
273 * RETURNS
275 * The length of the encoded name, including padding and length+hash fields.
277 * NOTES
279 * Will throw an exception if name or result are NULL. Is not multithread
280 * safe in the slightest.
282 static int ctl2_encode_name(
283 msft_typelib_t *typelib, /* [I] The typelib to operate against (used for LCID only). */
284 const char *name, /* [I] The name string to encode. */
285 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
287 char *converted_name;
288 size_t length, size;
289 int offset;
290 int value;
292 length = strlen(name);
293 size = (length + 7) & ~3;
294 converted_name = xmalloc(size + 1);
295 memcpy(converted_name + 4, name, length);
296 converted_name[length + 4] = 0;
298 value = lhash_val_of_name_sys(typelib->typelib_header.varflags & 0x0f, typelib->typelib_header.lcid, converted_name + 4);
300 converted_name[0] = length & 0xff;
301 converted_name[1] = length >> 8;
302 converted_name[2] = value;
303 converted_name[3] = value >> 8;
305 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
307 *result = converted_name;
309 return size;
312 /****************************************************************************
313 * ctl2_encode_string
315 * Encodes a string to a form suitable for storing into a type library or
316 * comparing to a string stored in a type library.
318 * RETURNS
320 * The length of the encoded string, including padding and length fields.
322 * NOTES
324 * Will throw an exception if string or result are NULL. Is not multithread
325 * safe in the slightest.
327 static int ctl2_encode_string(
328 const char *string, /* [I] The string to encode. */
329 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
331 char *converted_string;
332 size_t length, size;
333 int offset;
335 length = strlen(string);
336 size = (length + 5) & ~3;
337 if (length < 3) size += 4;
338 converted_string = xmalloc(size);
339 memcpy(converted_string + 2, string, length);
340 converted_string[0] = length & 0xff;
341 converted_string[1] = (length >> 8) & 0xff;
343 if(length < 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
344 for(offset = 0; offset < 4; offset++)
345 converted_string[length + offset + 2] = 0x57;
346 length += 4;
348 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
350 *result = converted_string;
351 return size;
354 /****************************************************************************
355 * ctl2_alloc_segment
357 * Allocates memory from a segment in a type library.
359 * RETURNS
361 * Success: The offset within the segment of the new data area.
363 * BUGS
365 * Does not (yet) handle the case where the allocated segment memory needs to grow.
367 static int ctl2_alloc_segment(
368 msft_typelib_t *typelib, /* [I] The type library in which to allocate. */
369 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
370 int size, /* [I] The amount to allocate. */
371 int block_size) /* [I] Initial allocation block size, or 0 for default. */
373 int offset;
375 if(!typelib->typelib_segment_data[segment]) {
376 if (!block_size) block_size = 0x2000;
378 typelib->typelib_segment_block_length[segment] = block_size;
379 typelib->typelib_segment_data[segment] = xmalloc(block_size);
380 if (!typelib->typelib_segment_data[segment]) return -1;
381 memset(typelib->typelib_segment_data[segment], 0x57, block_size);
384 while ((typelib->typelib_segdir[segment].length + size) > typelib->typelib_segment_block_length[segment]) {
385 unsigned char *block;
387 block_size = typelib->typelib_segment_block_length[segment];
388 block = xrealloc(typelib->typelib_segment_data[segment], block_size << 1);
390 if (segment == MSFT_SEG_TYPEINFO) {
391 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
392 msft_typeinfo_t *typeinfo;
394 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
395 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - typelib->typelib_segment_data[segment]];
399 memset(block + block_size, 0x57, block_size);
400 typelib->typelib_segment_block_length[segment] = block_size << 1;
401 typelib->typelib_segment_data[segment] = block;
404 offset = typelib->typelib_segdir[segment].length;
405 typelib->typelib_segdir[segment].length += size;
407 return offset;
410 /****************************************************************************
411 * ctl2_alloc_typeinfo
413 * Allocates and initializes a typeinfo structure in a type library.
415 * RETURNS
417 * Success: The offset of the new typeinfo.
418 * Failure: -1 (this is invariably an out of memory condition).
420 static int ctl2_alloc_typeinfo(
421 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
422 int nameoffset) /* [I] The offset of the name for this typeinfo. */
424 int offset;
425 MSFT_TypeInfoBase *typeinfo;
427 offset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
429 typelib->typelib_typeinfo_offsets[typelib->typelib_header.nrtypeinfos++] = offset;
431 typeinfo = (void *)(typelib->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
433 typeinfo->typekind = (typelib->typelib_header.nrtypeinfos - 1) << 16;
434 typeinfo->memoffset = -1; /* should be EOF if no elements */
435 typeinfo->res2 = 0;
436 typeinfo->res3 = -1;
437 typeinfo->res4 = 3;
438 typeinfo->res5 = 0;
439 typeinfo->cElement = 0;
440 typeinfo->res7 = 0;
441 typeinfo->res8 = 0;
442 typeinfo->res9 = 0;
443 typeinfo->resA = 0;
444 typeinfo->posguid = -1;
445 typeinfo->flags = 0;
446 typeinfo->NameOffset = nameoffset;
447 typeinfo->version = 0;
448 typeinfo->docstringoffs = -1;
449 typeinfo->helpstringcontext = 0;
450 typeinfo->helpcontext = 0;
451 typeinfo->oCustData = -1;
452 typeinfo->cbSizeVft = 0;
453 typeinfo->cImplTypes = 0;
454 typeinfo->size = 0;
455 typeinfo->datatype1 = -1;
456 typeinfo->datatype2 = 0;
457 typeinfo->res18 = 0;
458 typeinfo->res19 = -1;
460 return offset;
463 /****************************************************************************
464 * ctl2_alloc_guid
466 * Allocates and initializes a GUID structure in a type library. Also updates
467 * the GUID hash table as needed.
469 * RETURNS
471 * Success: The offset of the new GUID.
473 static int ctl2_alloc_guid(
474 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
475 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
477 int offset;
478 MSFT_GuidEntry *guid_space;
479 int hash_key;
481 chat("adding uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
482 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
483 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
484 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
486 hash_key = ctl2_hash_guid(&guid->guid);
488 offset = ctl2_find_guid(typelib, hash_key, &guid->guid);
489 if (offset != -1)
491 if (is_warning_enabled(2368))
492 warning("duplicate uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
493 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
494 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
495 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
496 return -1;
499 offset = ctl2_alloc_segment(typelib, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
501 guid_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_GUID] + offset);
502 *guid_space = *guid;
504 guid_space->next_hash = typelib->typelib_guidhash_segment[hash_key];
505 typelib->typelib_guidhash_segment[hash_key] = offset;
507 return offset;
510 /****************************************************************************
511 * ctl2_alloc_name
513 * Allocates and initializes a name within a type library. Also updates the
514 * name hash table as needed.
516 * RETURNS
518 * Success: The offset within the segment of the new name.
519 * Failure: -1 (this is invariably an out of memory condition).
521 static int ctl2_alloc_name(
522 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
523 const char *name) /* [I] The name to store. */
525 int length;
526 int offset;
527 MSFT_NameIntro *name_space;
528 char *encoded_name;
530 length = ctl2_encode_name(typelib, name, &encoded_name);
532 offset = ctl2_find_name(typelib, encoded_name);
533 if (offset != -1)
535 free(encoded_name);
536 return offset;
539 offset = ctl2_alloc_segment(typelib, MSFT_SEG_NAME, length + 8, 0);
541 name_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_NAME] + offset);
542 name_space->hreftype = -1;
543 name_space->next_hash = -1;
544 memcpy(&name_space->namelen, encoded_name, length);
546 if (typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
547 name_space->next_hash = typelib->typelib_namehash_segment[encoded_name[2] & 0x7f];
549 typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
551 typelib->typelib_header.nametablecount += 1;
552 typelib->typelib_header.nametablechars += *encoded_name;
554 free(encoded_name);
555 return offset;
558 /****************************************************************************
559 * ctl2_alloc_string
561 * Allocates and initializes a string in a type library.
563 * RETURNS
565 * Success: The offset within the segment of the new string.
566 * Failure: -1 (this is invariably an out of memory condition).
568 static int ctl2_alloc_string(
569 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
570 const char *string) /* [I] The string to store. */
572 int length;
573 int offset;
574 unsigned char *string_space;
575 char *encoded_string;
577 length = ctl2_encode_string(string, &encoded_string);
579 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_STRING].length;
580 offset += (((typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
581 typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
582 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
585 offset = ctl2_alloc_segment(typelib, MSFT_SEG_STRING, length, 0);
587 string_space = typelib->typelib_segment_data[MSFT_SEG_STRING] + offset;
588 memcpy(string_space, encoded_string, length);
589 free(encoded_string);
591 return offset;
594 /****************************************************************************
595 * alloc_msft_importinfo
597 * Allocates and initializes an import information structure in a type library.
599 * RETURNS
601 * Success: The offset of the new importinfo.
602 * Failure: -1 (this is invariably an out of memory condition).
604 static int alloc_msft_importinfo(
605 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
606 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
608 int offset;
609 MSFT_ImpInfo *impinfo_space;
611 for (offset = 0;
612 offset < typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
613 offset += sizeof(MSFT_ImpInfo)) {
614 if (!memcmp(&(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][offset]),
615 impinfo, sizeof(MSFT_ImpInfo))) {
616 return offset;
620 impinfo->flags |= typelib->typelib_header.nimpinfos++;
622 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
624 impinfo_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
625 *impinfo_space = *impinfo;
627 return offset;
630 /****************************************************************************
631 * alloc_importfile
633 * Allocates and initializes an import file definition in a type library.
635 * RETURNS
637 * Success: The offset of the new importinfo.
638 * Failure: -1 (this is invariably an out of memory condition).
640 static int alloc_importfile(
641 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
642 int guidoffset, /* [I] The offset to the GUID for the imported library. */
643 int major_version, /* [I] The major version number of the imported library. */
644 int minor_version, /* [I] The minor version number of the imported library. */
645 const char *filename) /* [I] The filename of the imported library. */
647 int length;
648 int offset;
649 MSFT_ImpFile *importfile;
650 char *encoded_string;
652 length = ctl2_encode_string(filename, &encoded_string);
654 encoded_string[0] <<= 2;
655 encoded_string[0] |= 1;
657 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
658 offset += (((typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
659 typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 0xc) {
660 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
663 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
665 importfile = (MSFT_ImpFile *)&typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
666 importfile->guid = guidoffset;
667 importfile->lcid = typelib->typelib_header.lcid2;
668 importfile->version = major_version | (minor_version << 16);
669 memcpy(&importfile->filename, encoded_string, length);
670 free(encoded_string);
672 return offset;
675 static void alloc_importinfo(msft_typelib_t *typelib, importinfo_t *importinfo)
677 importlib_t *importlib = importinfo->importlib;
679 chat("alloc_importinfo: %s\n", importinfo->name);
681 if(!importlib->allocated) {
682 MSFT_GuidEntry guid;
683 int guid_idx;
685 chat("allocating importlib %s\n", importlib->name);
687 importlib->allocated = -1;
689 memcpy(&guid.guid, &importlib->guid, sizeof(guid.guid));
690 guid.hreftype = 2;
692 guid_idx = ctl2_alloc_guid(typelib, &guid);
694 importlib->offset = alloc_importfile(typelib, guid_idx, importlib->version & 0xffff,
695 importlib->version >> 16, importlib->name);
698 if(importinfo->offset == -1 || !(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID)) {
699 MSFT_ImpInfo impinfo;
701 impinfo.flags = importinfo->flags;
702 impinfo.oImpFile = importlib->offset;
704 if(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID) {
705 MSFT_GuidEntry guid;
707 guid.hreftype = 0;
708 memcpy(&guid.guid, &importinfo->guid, sizeof(guid.guid));
710 impinfo.oGuid = ctl2_alloc_guid(typelib, &guid);
712 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
714 typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo.oGuid+sizeof(guid.guid)]
715 = importinfo->offset+1;
717 if(!strcmp(importinfo->name, "IDispatch"))
718 typelib->typelib_header.dispatchpos = importinfo->offset+1;
719 }else {
720 impinfo.oGuid = importinfo->id;
721 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
726 static importinfo_t *find_importinfo(msft_typelib_t *typelib, const char *name)
728 importlib_t *importlib;
729 int i;
731 chat("search importlib %s\n", name);
733 if(!name)
734 return NULL;
736 LIST_FOR_EACH_ENTRY( importlib, &typelib->typelib->importlibs, importlib_t, entry )
738 for(i=0; i < importlib->ntypeinfos; i++) {
739 if(!strcmp(name, importlib->importinfos[i].name)) {
740 chat("Found %s in importlib.\n", name);
741 return importlib->importinfos+i;
746 return NULL;
749 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure);
750 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface);
751 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration);
752 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion);
753 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls);
754 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
755 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
758 /****************************************************************************
759 * encode_type
761 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
762 * segments as needed.
764 * RETURNS
766 * Success: 0.
767 * Failure: -1.
769 static int encode_type(
770 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
771 int vt, /* [I] vt to encode */
772 type_t *type, /* [I] type */
773 int *encoded_type, /* [O] The encoded type description. */
774 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
776 int default_type;
777 int scratch;
778 int typeoffset;
779 int *typedata;
780 int target_type;
781 int child_size = 0;
783 chat("encode_type vt %d type %p\n", vt, type);
785 default_type = 0x80000000 | (vt << 16) | vt;
786 if (!decoded_size) decoded_size = &scratch;
788 *decoded_size = 0;
790 switch (vt) {
791 case VT_I1:
792 case VT_UI1:
793 *encoded_type = default_type;
794 break;
796 case VT_INT:
797 *encoded_type = 0x80000000 | (VT_I4 << 16) | VT_INT;
798 break;
800 case VT_UINT:
801 *encoded_type = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
802 break;
804 case VT_UI2:
805 case VT_I2:
806 case VT_BOOL:
807 *encoded_type = default_type;
808 break;
810 case VT_I4:
811 case VT_UI4:
812 case VT_R4:
813 case VT_ERROR:
814 case VT_HRESULT:
815 *encoded_type = default_type;
816 break;
818 case VT_R8:
819 case VT_I8:
820 case VT_UI8:
821 *encoded_type = default_type;
822 break;
824 case VT_CY:
825 case VT_DATE:
826 *encoded_type = default_type;
827 break;
829 case VT_DECIMAL:
830 *encoded_type = default_type;
831 break;
833 case VT_VOID:
834 *encoded_type = 0x80000000 | (VT_EMPTY << 16) | vt;
835 break;
837 case VT_UNKNOWN:
838 case VT_DISPATCH:
839 case VT_BSTR:
840 *encoded_type = default_type;
841 break;
843 case VT_VARIANT:
844 *encoded_type = default_type;
845 break;
847 case VT_LPSTR:
848 case VT_LPWSTR:
849 *encoded_type = 0xfffe0000 | vt;
850 break;
852 case VT_PTR:
854 int next_vt;
855 for(next_vt = 0; is_ptr(type); type = type_pointer_get_ref_type(type)) {
856 next_vt = get_type_vt(type_pointer_get_ref_type(type));
857 if (next_vt != 0)
858 break;
860 /* if no type found then it must be void */
861 if (next_vt == 0)
862 next_vt = VT_VOID;
864 encode_type(typelib, next_vt, type_pointer_get_ref_type(type),
865 &target_type, &child_size);
866 /* these types already have an implicit pointer, so we don't need to
867 * add another */
868 if(next_vt == VT_DISPATCH || next_vt == VT_UNKNOWN) {
869 chat("encode_type: skipping ptr\n");
870 *encoded_type = target_type;
871 *decoded_size = child_size;
872 break;
875 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
876 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
877 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
880 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
881 int mix_field;
883 if (target_type & 0x80000000) {
884 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
885 } else {
886 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
887 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
890 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
891 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
893 typedata[0] = (mix_field << 16) | VT_PTR;
894 typedata[1] = target_type;
897 *encoded_type = typeoffset;
899 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
900 break;
903 case VT_SAFEARRAY:
905 type_t *element_type = type_alias_get_aliasee_type(type_array_get_element_type(type));
906 int next_vt = get_type_vt(element_type);
908 encode_type(typelib, next_vt, type_alias_get_aliasee_type(type_array_get_element_type(type)),
909 &target_type, &child_size);
911 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
912 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
913 if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
916 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
917 int mix_field;
919 if (target_type & 0x80000000) {
920 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
921 } else {
922 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
923 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
926 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
927 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
929 typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
930 typedata[1] = target_type;
933 *encoded_type = typeoffset;
935 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
936 break;
939 case VT_USERDEFINED:
941 importinfo_t *importinfo;
942 int typeinfo_offset;
944 if (type->typelib_idx > -1)
946 chat("encode_type: VT_USERDEFINED - found already defined type %s at %d\n",
947 type->name, type->typelib_idx);
948 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
950 else if ((importinfo = find_importinfo(typelib, type->name)))
952 chat("encode_type: VT_USERDEFINED - found imported type %s in %s\n",
953 type->name, importinfo->importlib->name);
954 alloc_importinfo(typelib, importinfo);
955 typeinfo_offset = importinfo->offset | 0x1;
957 else
959 /* Typedefs without the [public] attribute aren't included in the
960 * typelib, unless the aliasee is an anonymous UDT or the typedef
961 * is wire-marshalled. In the latter case the wire-marshal type,
962 * which may be a non-public alias, is used instead. */
963 while (type_is_alias(type))
965 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
967 type = get_attrp(type->attrs, ATTR_WIREMARSHAL);
968 break;
970 else if (!is_attr(type->attrs, ATTR_PUBLIC))
971 type = type_alias_get_aliasee_type(type);
972 else
973 break;
976 chat("encode_type: VT_USERDEFINED - adding new type %s, real type %d\n",
977 type->name, type_get_type(type));
979 switch (type_get_type_detect_alias(type))
981 case TYPE_STRUCT:
982 case TYPE_ENCAPSULATED_UNION:
983 add_structure_typeinfo(typelib, type);
984 break;
985 case TYPE_INTERFACE:
986 add_interface_typeinfo(typelib, type);
987 break;
988 case TYPE_ENUM:
989 add_enum_typeinfo(typelib, type);
990 break;
991 case TYPE_UNION:
992 add_union_typeinfo(typelib, type);
993 break;
994 case TYPE_COCLASS:
995 add_coclass_typeinfo(typelib, type);
996 break;
997 case TYPE_ALIAS:
998 add_typedef_typeinfo(typelib, type);
999 break;
1000 default:
1001 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
1002 type_get_type(type));
1005 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
1007 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1008 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1009 if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == typeinfo_offset)) break;
1012 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1013 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1014 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1016 typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1017 typedata[1] = typeinfo_offset;
1020 *encoded_type = typeoffset;
1021 break;
1024 default:
1025 error("encode_type: unrecognized type %d.\n", vt);
1026 *encoded_type = default_type;
1027 break;
1030 return 0;
1033 static void dump_type(type_t *t)
1035 chat("dump_type: %p name %s type %d attrs %p\n", t, t->name, type_get_type(t), t->attrs);
1038 static int encode_var(
1039 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
1040 type_t *type, /* [I] The type description to encode. */
1041 var_t *var, /* [I] The var to encode. */
1042 int *encoded_type, /* [O] The encoded type description. */
1043 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1045 int typeoffset;
1046 int *typedata;
1047 int target_type;
1048 int child_size;
1049 int vt;
1050 int scratch;
1052 if (!decoded_size) decoded_size = &scratch;
1053 *decoded_size = 0;
1055 chat("encode_var: var %p type %p type->name %s\n",
1056 var, type, type->name ? type->name : "NULL");
1058 if (is_array(type) && !type_array_is_decl_as_ptr(type)) {
1059 int num_dims, arrayoffset;
1060 type_t *atype;
1061 int *arraydata;
1063 num_dims = 0;
1064 for (atype = type;
1065 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1066 atype = type_array_get_element_type(atype))
1067 ++num_dims;
1069 chat("array with %d dimensions\n", num_dims);
1070 encode_var(typelib, atype, var, &target_type, NULL);
1071 arrayoffset = ctl2_alloc_segment(typelib, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1072 arraydata = (void *)&typelib->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1074 arraydata[0] = target_type;
1075 arraydata[1] = num_dims;
1076 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1078 arraydata += 2;
1079 for (atype = type;
1080 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1081 atype = type_array_get_element_type(atype))
1083 arraydata[0] = type_array_get_dim(atype);
1084 arraydata[1] = 0;
1085 arraydata += 2;
1088 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1089 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1091 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1092 typedata[1] = arrayoffset;
1094 *encoded_type = typeoffset;
1095 *decoded_size = 20 /*sizeof(ARRAYDESC)*/ + (num_dims - 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1096 return 0;
1099 vt = get_type_vt(type);
1100 if (vt == VT_PTR) {
1101 type_t *ref = is_ptr(type) ?
1102 type_pointer_get_ref_type(type) : type_array_get_element_type(type);
1103 int skip_ptr = encode_var(typelib, ref, var, &target_type, &child_size);
1105 if(skip_ptr == 2) {
1106 chat("encode_var: skipping ptr\n");
1107 *encoded_type = target_type;
1108 *decoded_size = child_size;
1109 return 0;
1112 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1113 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1114 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1117 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1118 int mix_field;
1120 if (target_type & 0x80000000) {
1121 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1122 } else if (get_type_vt(ref) == VT_SAFEARRAY) {
1123 type_t *element_type = type_alias_get_aliasee_type(type_array_get_element_type(ref));
1124 mix_field = get_type_vt(element_type) | VT_ARRAY | VT_BYREF;
1125 } else {
1126 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1127 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1130 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1131 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1133 typedata[0] = (mix_field << 16) | VT_PTR;
1134 typedata[1] = target_type;
1137 *encoded_type = typeoffset;
1139 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1140 return 0;
1143 dump_type(type);
1145 encode_type(typelib, vt, type, encoded_type, decoded_size);
1146 /* these types already have an implicit pointer, so we don't need to
1147 * add another */
1148 if(vt == VT_DISPATCH || vt == VT_UNKNOWN) return 2;
1149 return 0;
1152 static unsigned int get_ulong_val(unsigned int val, int vt)
1154 switch(vt) {
1155 case VT_I2:
1156 case VT_BOOL:
1157 case VT_UI2:
1158 return val & 0xffff;
1159 case VT_I1:
1160 case VT_UI1:
1161 return val & 0xff;
1164 return val;
1167 static void write_int_value(msft_typelib_t *typelib, int *out, int vt, int value)
1169 const unsigned int lv = get_ulong_val(value, vt);
1170 if ((lv & 0x3ffffff) == lv) {
1171 *out = 0x80000000;
1172 *out |= vt << 26;
1173 *out |= lv;
1174 } else {
1175 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1176 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1177 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &value, 4);
1178 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1179 *out = offset;
1183 static void write_string_value(msft_typelib_t *typelib, int *out, const char *value)
1185 int len = strlen(value), seg_len = (len + 6 + 3) & ~0x3;
1186 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1187 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = VT_BSTR;
1188 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &len, sizeof(len));
1189 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1190 len += 6;
1191 while(len < seg_len) {
1192 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1193 len++;
1195 *out = offset;
1198 static void write_default_value(msft_typelib_t *typelib, type_t *type, expr_t *expr, int *out)
1200 int vt;
1202 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT) {
1203 vt = get_type_vt(type);
1204 if (vt != VT_BSTR && vt != VT_VARIANT)
1205 error("string default value applied to non-string type\n");
1206 chat("default value '%s'\n", expr->u.sval);
1207 write_string_value(typelib, out, expr->u.sval);
1208 return;
1211 if (type_get_type(type) == TYPE_ENUM) {
1212 vt = VT_I4;
1213 } else if (is_ptr(type)) {
1214 vt = get_type_vt(type_pointer_get_ref_type(type));
1215 if (vt == VT_USERDEFINED)
1216 vt = VT_I4;
1217 if (expr->cval)
1218 warning("non-null pointer default value\n");
1219 } else {
1220 vt = get_type_vt(type);
1221 switch(vt) {
1222 case VT_I2:
1223 case VT_I4:
1224 case VT_R4:
1225 case VT_BOOL:
1226 case VT_I1:
1227 case VT_UI1:
1228 case VT_UI2:
1229 case VT_UI4:
1230 case VT_INT:
1231 case VT_UINT:
1232 case VT_HRESULT:
1233 break;
1234 case VT_VARIANT: {
1235 switch (expr->type) {
1236 case EXPR_DOUBLE:
1237 vt = VT_R4;
1238 break;
1239 case EXPR_NUM:
1240 vt = VT_I4;
1241 break;
1242 default:
1243 warning("can't write default VT_VARIANT value for expression type %d.\n", expr->type);
1244 return;
1246 break;
1248 default:
1249 warning("can't write value of type %d yet\n", vt);
1250 return;
1254 write_int_value(typelib, out, vt, expr->cval);
1257 static void set_custdata(msft_typelib_t *typelib, const struct uuid *guid,
1258 int vt, const void *value, int *offset)
1260 int guidoffset;
1261 int custoffset;
1262 int *custdata;
1263 int data_out;
1264 int hash_key;
1266 hash_key = ctl2_hash_guid(guid);
1267 guidoffset = ctl2_find_guid(typelib, hash_key, guid);
1268 if(guidoffset == -1) {
1269 /* add GUID that was not already present */
1270 MSFT_GuidEntry guidentry;
1271 guidentry.guid = *guid;
1273 guidentry.hreftype = -1;
1274 guidentry.next_hash = -1;
1276 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1279 if(vt == VT_BSTR)
1280 /* TODO midl appears to share a single reference if the same string is used as custdata in multiple places */
1281 write_string_value(typelib, &data_out, value);
1282 else
1283 write_int_value(typelib, &data_out, vt, *(int*)value);
1285 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1287 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1288 custdata[0] = guidoffset;
1289 custdata[1] = data_out;
1290 custdata[2] = *offset;
1291 *offset = custoffset;
1294 static void set_custdata_attr(msft_typelib_t *typelib, attr_custdata_t *custdata, int *offset)
1296 switch(custdata->pval->type) {
1297 case EXPR_STRLIT:
1298 case EXPR_WSTRLIT:
1299 set_custdata(typelib, &custdata->id, VT_BSTR, custdata->pval->u.sval, offset);
1300 break;
1301 case EXPR_HEXNUM:
1302 case EXPR_NUM:
1303 set_custdata(typelib, &custdata->id, VT_I4, &custdata->pval->u.lval, offset);
1304 break;
1305 default:
1306 error("custom() attribute with unknown type\n");
1307 break;
1311 static int add_func_desc(msft_typeinfo_t* typeinfo, var_t *func, int index)
1313 int offset, name_offset;
1314 int *typedata, typedata_size;
1315 int i, id, next_idx;
1316 int decoded_size, extra_attr = 0;
1317 int num_params = 0, num_optional = 0, num_defaults = 0;
1318 int has_arg_custdata = 0;
1319 var_t *arg;
1320 unsigned char *namedata;
1321 const attr_t *attr;
1322 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1323 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1324 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1325 int func_custdata_offset = -1;
1326 int entry = -1, entry_is_ord = 0;
1327 int lcid_retval_count = 0;
1329 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1331 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1333 switch(typeinfo->typekind) {
1334 case TKIND_DISPATCH:
1335 funckind = 0x4; /* FUNC_DISPATCH */
1336 break;
1337 case TKIND_MODULE:
1338 funckind = 0x3; /* FUNC_STATIC */
1339 break;
1340 default:
1341 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1342 break;
1345 if (is_local( func->attrs )) {
1346 chat("add_func_desc: skipping local function\n");
1347 return FALSE;
1350 if (type_function_get_args(func->declspec.type))
1351 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1353 num_params++;
1354 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1355 if(attr->type == ATTR_DEFAULTVALUE)
1356 num_defaults++;
1357 else if(attr->type == ATTR_OPTIONAL)
1358 num_optional++;
1359 else if(attr->type == ATTR_CUSTOM)
1360 has_arg_custdata = 1;
1364 chat("add_func_desc: num of params %d\n", num_params);
1366 name_offset = ctl2_alloc_name(typeinfo->typelib, func->name);
1368 if (func->attrs) LIST_FOR_EACH_ENTRY( attr, func->attrs, const attr_t, entry ) {
1369 expr_t *expr = attr->u.pval;
1370 switch(attr->type) {
1371 case ATTR_BINDABLE:
1372 funcflags |= 0x4; /* FUNCFLAG_FBINDABLE */
1373 break;
1374 case ATTR_CUSTOM:
1375 set_custdata_attr(typeinfo->typelib, attr->u.pval, &func_custdata_offset);
1376 break;
1377 case ATTR_DEFAULTBIND:
1378 funcflags |= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1379 break;
1380 case ATTR_DEFAULTCOLLELEM:
1381 funcflags |= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1382 break;
1383 case ATTR_DISPLAYBIND:
1384 funcflags |= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1385 break;
1386 case ATTR_ENTRY:
1387 extra_attr = max(extra_attr, 3);
1388 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1389 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1390 else {
1391 entry = expr->cval;
1392 entry_is_ord = 1;
1394 break;
1395 case ATTR_HELPCONTEXT:
1396 extra_attr = max(extra_attr, 1);
1397 help_context = expr->u.lval;
1398 break;
1399 case ATTR_HELPSTRING:
1400 extra_attr = max(extra_attr, 2);
1401 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1402 break;
1403 case ATTR_HELPSTRINGCONTEXT:
1404 extra_attr = max(extra_attr, 6);
1405 help_string_context = expr->u.lval;
1406 break;
1407 case ATTR_HIDDEN:
1408 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1409 break;
1410 case ATTR_ID:
1411 id = expr->cval;
1412 break;
1413 case ATTR_IMMEDIATEBIND:
1414 funcflags |= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1415 break;
1416 case ATTR_NONBROWSABLE:
1417 funcflags |= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1418 break;
1419 case ATTR_OUT:
1420 break;
1421 case ATTR_OVERLOAD:
1422 break;
1423 case ATTR_PROPGET:
1424 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1425 break;
1426 case ATTR_PROPPUT:
1427 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1428 break;
1429 case ATTR_PROPPUTREF:
1430 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1431 break;
1432 /* FIXME: FUNCFLAG_FREPLACEABLE */
1433 case ATTR_REQUESTEDIT:
1434 funcflags |= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1435 break;
1436 case ATTR_RESTRICTED:
1437 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1438 break;
1439 case ATTR_SOURCE:
1440 funcflags |= 0x2; /* FUNCFLAG_FSOURCE */
1441 break;
1442 case ATTR_UIDEFAULT:
1443 funcflags |= 0x200; /* FUNCFLAG_FUIDEFAULT */
1444 break;
1445 case ATTR_USESGETLASTERROR:
1446 funcflags |= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1447 break;
1448 case ATTR_VARARG:
1449 if (num_optional || num_defaults)
1450 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1451 else
1452 num_optional = -1;
1453 break;
1454 default:
1455 break;
1459 if(has_arg_custdata || func_custdata_offset != -1) {
1460 extra_attr = max(extra_attr, 7 + num_params);
1463 /* allocate type data space for us */
1464 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1466 if (!typeinfo->func_data) {
1467 typeinfo->func_data = xmalloc(0x100);
1468 typeinfo->func_data_allocated = 0x100;
1469 typeinfo->func_data[0] = 0;
1472 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1473 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1474 typeinfo->func_data[0] + typedata_size + sizeof(int));
1475 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1478 offset = typeinfo->func_data[0];
1479 typeinfo->func_data[0] += typedata_size;
1480 typedata = typeinfo->func_data + (offset >> 2) + 1;
1483 /* find func with the same name - if it exists use its id */
1484 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1485 if(name_offset == typeinfo->func_names[i]) {
1486 id = typeinfo->func_indices[i];
1487 break;
1491 /* find the first func with the same id and link via the hiword of typedata[4] */
1492 next_idx = index;
1493 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1494 if(id == typeinfo->func_indices[i]) {
1495 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1496 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1497 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1498 break;
1502 /* fill out the basic type information */
1503 typedata[0] = typedata_size | (index << 16);
1504 encode_var(typeinfo->typelib, type_function_get_rettype(func->declspec.type), func,
1505 &typedata[1], &decoded_size);
1506 typedata[2] = funcflags;
1507 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1508 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1509 if(has_arg_custdata || func_custdata_offset != -1) typedata[4] |= 0x0080;
1510 if(num_defaults) typedata[4] |= 0x1000;
1511 if(entry_is_ord) typedata[4] |= 0x2000;
1512 typedata[5] = (num_optional << 16) | num_params;
1514 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1515 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1516 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1517 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1519 switch(extra_attr) {
1520 default:
1521 if(extra_attr > 7 + num_params) warning("unknown number of optional attrs\n");
1522 /* typedata[13..+num_params] = arg_custdata_offset handled in below loop */
1523 case 7: typedata[12] = func_custdata_offset;
1524 case 6: typedata[11] = help_string_context;
1525 case 5: typedata[10] = -1;
1526 case 4: typedata[9] = -1;
1527 case 3: typedata[8] = entry;
1528 case 2: typedata[7] = help_string_offset;
1529 case 1: typedata[6] = help_context;
1530 case 0:
1531 break;
1534 if (type_function_get_args(func->declspec.type))
1536 i = 0;
1537 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1539 int paramflags = 0;
1540 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1541 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1542 int arg_custdata_offset = -1;
1544 if(defaultdata) *defaultdata = -1;
1546 encode_var(typeinfo->typelib, arg->declspec.type, arg, paramdata, &decoded_size);
1547 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1548 switch(attr->type) {
1549 case ATTR_CUSTOM:
1550 set_custdata_attr(typeinfo->typelib, attr->u.pval, &arg_custdata_offset);
1551 break;
1552 case ATTR_DEFAULTVALUE:
1554 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1555 write_default_value(typeinfo->typelib, arg->declspec.type, (expr_t *)attr->u.pval, defaultdata);
1556 break;
1558 case ATTR_IN:
1559 paramflags |= 0x01; /* PARAMFLAG_FIN */
1560 break;
1561 case ATTR_OPTIONAL:
1562 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1563 break;
1564 case ATTR_OUT:
1565 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1566 break;
1567 case ATTR_PARAMLCID:
1568 paramflags |= 0x04; /* PARAMFLAG_LCID */
1569 lcid_retval_count++;
1570 break;
1571 case ATTR_RETVAL:
1572 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1573 lcid_retval_count++;
1574 break;
1575 default:
1576 chat("unhandled param attr %d\n", attr->type);
1577 break;
1579 if(extra_attr > 7 + i) {
1580 typedata[13+i] = arg_custdata_offset;
1583 paramdata[1] = -1;
1584 paramdata[2] = paramflags;
1585 typedata[3] += decoded_size << 16;
1587 i++;
1591 if(lcid_retval_count == 1)
1592 typedata[4] |= 0x4000;
1593 else if(lcid_retval_count == 2)
1594 typedata[4] |= 0x8000;
1596 if(typeinfo->funcs_allocated == 0) {
1597 typeinfo->funcs_allocated = 10;
1598 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1599 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1600 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1602 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1603 typeinfo->funcs_allocated *= 2;
1604 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1605 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1606 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1609 /* update the index data */
1610 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1611 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1612 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1614 /* ??? */
1615 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1616 typeinfo->typeinfo->res2 <<= 1;
1617 /* ??? */
1618 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1620 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1621 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1622 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1624 /* adjust size of VTBL */
1625 if(funckind != 0x3 /* FUNC_STATIC */)
1626 typeinfo->typeinfo->cbSizeVft += pointer_size;
1628 /* Increment the number of function elements */
1629 typeinfo->typeinfo->cElement += 1;
1631 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1632 if (*((int *)namedata) == -1) {
1633 *((int *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1634 if(typeinfo->typekind == TKIND_MODULE)
1635 namedata[9] |= 0x10;
1636 } else
1637 namedata[9] &= ~0x10;
1639 if(typeinfo->typekind == TKIND_MODULE)
1640 namedata[9] |= 0x20;
1642 if (type_function_get_args(func->declspec.type))
1644 i = 0;
1645 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1647 /* don't give the last arg of a [propput*] func a name */
1648 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1650 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1651 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1652 paramdata[1] = offset;
1654 i++;
1657 return TRUE;
1660 static void add_var_desc(msft_typeinfo_t *typeinfo, unsigned int index, var_t* var)
1662 int offset, id;
1663 unsigned int typedata_size;
1664 int extra_attr = 0;
1665 int *typedata;
1666 unsigned int var_datawidth, var_alignment = 0;
1667 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1668 int alignment;
1669 int varflags = 0;
1670 const attr_t *attr;
1671 unsigned char *namedata;
1672 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1673 int var_custdata_offset = -1;
1675 if (!var->name)
1676 var->name = gen_name();
1678 chat("add_var_desc(%d, %s)\n", index, var->name);
1680 id = 0x40000000 + index;
1682 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1683 expr_t *expr = attr->u.pval;
1684 switch(attr->type) {
1685 case ATTR_BINDABLE:
1686 varflags |= 0x04; /* VARFLAG_FBINDABLE */
1687 break;
1688 case ATTR_CUSTOM:
1689 extra_attr = max(extra_attr,4);
1690 set_custdata_attr(typeinfo->typelib, attr->u.pval, &var_custdata_offset);
1691 break;
1692 case ATTR_DEFAULTBIND:
1693 varflags |= 0x20; /* VARFLAG_FDEFAULTBIND */
1694 break;
1695 case ATTR_DEFAULTCOLLELEM:
1696 varflags |= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1697 break;
1698 case ATTR_DISPLAYBIND:
1699 varflags |= 0x10; /* VARFLAG_FDISPLAYBIND */
1700 break;
1701 case ATTR_HIDDEN:
1702 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1703 break;
1704 case ATTR_ID:
1705 id = expr->cval;
1706 break;
1707 case ATTR_IMMEDIATEBIND:
1708 varflags |= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1709 break;
1710 case ATTR_NONBROWSABLE:
1711 varflags |= 0x400; /* VARFLAG_FNONBROWSABLE */
1712 break;
1713 case ATTR_READONLY:
1714 varflags |= 0x01; /* VARFLAG_FREADONLY */
1715 break;
1716 /* FIXME: VARFLAG_FREPLACEABLE */
1717 case ATTR_REQUESTEDIT:
1718 varflags |= 0x08; /* VARFLAG_FREQUESTEDIT */
1719 break;
1720 case ATTR_RESTRICTED:
1721 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1722 break;
1723 case ATTR_SOURCE:
1724 varflags |= 0x02; /* VARFLAG_FSOURCE */
1725 break;
1726 case ATTR_UIDEFAULT:
1727 varflags |= 0x0200; /* VARFLAG_FUIDEFAULT */
1728 break;
1729 default:
1730 break;
1734 /* allocate type data space for us */
1735 typedata_size = 0x14 + extra_attr * sizeof(int);
1737 if (!typeinfo->var_data) {
1738 typeinfo->var_data = xmalloc(0x100);
1739 typeinfo->var_data_allocated = 0x100;
1740 typeinfo->var_data[0] = 0;
1743 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1744 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1745 typeinfo->var_data[0] + typedata_size + sizeof(int));
1746 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1749 offset = typeinfo->var_data[0];
1750 typeinfo->var_data[0] += typedata_size;
1751 typedata = typeinfo->var_data + (offset >> 2) + 1;
1753 /* fill out the basic type information */
1754 typedata[0] = typedata_size | (index << 16);
1755 typedata[2] = varflags;
1756 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1758 if(typeinfo->vars_allocated == 0) {
1759 typeinfo->vars_allocated = 10;
1760 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1761 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1762 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1764 if(typeinfo->vars_allocated == var_num) {
1765 typeinfo->vars_allocated *= 2;
1766 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1767 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1768 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1770 /* update the index data */
1771 typeinfo->var_indices[var_num] = id;
1772 typeinfo->var_names[var_num] = -1;
1773 typeinfo->var_offsets[var_num] = offset;
1775 /* figure out type widths and whatnot */
1776 var_datawidth = type_memsize_and_alignment(var->declspec.type, &var_alignment);
1777 encode_var(typeinfo->typelib, var->declspec.type, var, &typedata[1], &var_type_size);
1779 /* pad out starting position to data width */
1780 typeinfo->datawidth += var_alignment - 1;
1781 typeinfo->datawidth &= ~(var_alignment - 1);
1783 switch(typeinfo->typekind) {
1784 case TKIND_ENUM:
1785 write_int_value(typeinfo->typelib, &typedata[4], VT_I4, var->eval->cval);
1786 var_kind = 2; /* VAR_CONST */
1787 var_type_size += 16; /* sizeof(VARIANT) */
1788 typeinfo->datawidth = var_datawidth;
1789 break;
1790 case TKIND_RECORD:
1791 typedata[4] = typeinfo->datawidth;
1792 typeinfo->datawidth += var_datawidth;
1793 break;
1794 case TKIND_UNION:
1795 typedata[4] = 0;
1796 typeinfo->datawidth = max(typeinfo->datawidth, var_datawidth);
1797 break;
1798 case TKIND_DISPATCH:
1799 var_kind = 3; /* VAR_DISPATCH */
1800 typedata[4] = 0;
1801 typeinfo->datawidth = pointer_size;
1802 break;
1803 default:
1804 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1805 break;
1808 /* add type description size to total required allocation */
1809 typedata[3] += var_type_size << 16 | var_kind;
1811 switch(extra_attr) {
1812 case 5: typedata[9] = -1 /*help_string_context*/;
1813 case 4: typedata[8] = var_custdata_offset;
1814 case 3: typedata[7] = -1;
1815 case 2: typedata[6] = -1 /*help_string_offset*/;
1816 case 1: typedata[5] = -1 /*help_context*/;
1817 case 0:
1818 break;
1819 default:
1820 warning("unknown number of optional attrs\n");
1823 /* fix type alignment */
1824 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1825 if (alignment < var_alignment) {
1826 alignment = var_alignment;
1827 typeinfo->typeinfo->typekind &= ~0xffc0;
1828 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1831 /* ??? */
1832 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1833 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1834 typeinfo->typeinfo->res2 <<= 1;
1837 /* ??? */
1838 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1839 typeinfo->typeinfo->res3 += 0x2c;
1841 /* increment the number of variable elements */
1842 typeinfo->typeinfo->cElement += 0x10000;
1844 /* pad data width to alignment */
1845 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1847 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1849 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1850 if (*((int *)namedata) == -1) {
1851 *((int *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1852 if(typeinfo->typekind != TKIND_DISPATCH)
1853 namedata[9] |= 0x10;
1854 } else
1855 namedata[9] &= ~0x10;
1857 if (typeinfo->typekind == TKIND_ENUM) {
1858 namedata[9] |= 0x20;
1860 typeinfo->var_names[var_num] = offset;
1863 static void add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1865 if(importinfo) {
1866 alloc_importinfo(typeinfo->typelib, importinfo);
1867 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1868 }else {
1869 if(ref->typelib_idx == -1)
1870 add_interface_typeinfo(typeinfo->typelib, ref);
1871 if(ref->typelib_idx == -1)
1872 error("add_impl_type: unable to add inherited interface\n");
1874 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1877 typeinfo->typeinfo->cImplTypes++;
1880 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1881 const char *name, const attr_list_t *attrs)
1883 const attr_t *attr;
1884 msft_typeinfo_t *msft_typeinfo;
1885 int nameoffset;
1886 int typeinfo_offset;
1887 MSFT_TypeInfoBase *typeinfo;
1888 MSFT_GuidEntry guidentry;
1890 chat("create_msft_typeinfo: name %s kind %d index %d\n", name, kind, typelib->typelib_header.nrtypeinfos);
1892 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1893 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1895 msft_typeinfo->typelib = typelib;
1897 nameoffset = ctl2_alloc_name(typelib, name);
1898 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1899 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1901 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1902 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1904 msft_typeinfo->typekind = kind;
1905 msft_typeinfo->typeinfo = typeinfo;
1907 typeinfo->typekind |= kind | 0x20;
1909 if(kind == TKIND_COCLASS)
1910 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1912 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1913 switch(attr->type) {
1914 case ATTR_AGGREGATABLE:
1915 if (kind == TKIND_COCLASS)
1916 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1917 break;
1919 case ATTR_APPOBJECT:
1920 if (kind == TKIND_COCLASS)
1921 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1922 break;
1924 case ATTR_CONTROL:
1925 if (kind == TKIND_COCLASS)
1926 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1927 break;
1928 case ATTR_CUSTOM:
1929 set_custdata_attr(typelib, attr->u.pval, &typeinfo->oCustData);
1930 break;
1931 case ATTR_DLLNAME:
1933 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1934 typeinfo->datatype1 = offset;
1935 break;
1938 case ATTR_DUAL:
1939 /* FIXME: check interface is compatible */
1940 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1941 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1942 break;
1944 case ATTR_HELPCONTEXT:
1946 expr_t *expr = (expr_t*)attr->u.pval;
1947 typeinfo->helpcontext = expr->cval;
1948 break;
1950 case ATTR_HELPSTRING:
1952 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1953 if (offset == -1) break;
1954 typeinfo->docstringoffs = offset;
1955 break;
1957 case ATTR_HELPSTRINGCONTEXT:
1959 expr_t *expr = (expr_t*)attr->u.pval;
1960 typeinfo->helpstringcontext = expr->cval;
1961 break;
1963 case ATTR_HIDDEN:
1964 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1965 break;
1967 case ATTR_LICENSED:
1968 typeinfo->flags |= 0x04; /* TYPEFLAG_FLICENSED */
1969 break;
1971 case ATTR_NONCREATABLE:
1972 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1973 break;
1975 case ATTR_NONEXTENSIBLE:
1976 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1977 break;
1979 case ATTR_OLEAUTOMATION:
1980 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1981 break;
1983 /* FIXME: TYPEFLAG_FPREDCLID */
1985 case ATTR_PROXY:
1986 typeinfo->flags |= 0x4000; /* TYPEFLAG_FPROXY */
1987 break;
1989 /* FIXME: TYPEFLAG_FREPLACEABLE */
1991 case ATTR_RESTRICTED:
1992 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
1993 break;
1995 case ATTR_UUID:
1996 guidentry.guid = *(struct uuid *)attr->u.pval;
1997 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1998 guidentry.next_hash = -1;
1999 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
2000 #if 0
2001 if (IsEqualIID(guid, &IID_IDispatch)) {
2002 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
2004 #endif
2005 break;
2007 case ATTR_VERSION:
2008 typeinfo->version = attr->u.ival;
2009 break;
2011 default:
2012 break;
2016 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
2017 typelib->last_typeinfo = msft_typeinfo;
2018 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
2021 return msft_typeinfo;
2024 static void add_dispatch(msft_typelib_t *typelib)
2026 int guid_offset, impfile_offset, hash_key;
2027 MSFT_GuidEntry guidentry;
2028 MSFT_ImpInfo impinfo;
2029 static const struct uuid stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2030 static const struct uuid iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2032 if(typelib->typelib_header.dispatchpos != -1) return;
2034 guidentry.guid = stdole;
2035 guidentry.hreftype = 2;
2036 guidentry.next_hash = -1;
2037 hash_key = ctl2_hash_guid(&guidentry.guid);
2038 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2039 if (guid_offset == -1)
2040 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2041 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
2043 guidentry.guid = iid_idispatch;
2044 guidentry.hreftype = 1;
2045 guidentry.next_hash = -1;
2046 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
2047 impinfo.oImpFile = impfile_offset;
2048 hash_key = ctl2_hash_guid(&guidentry.guid);
2049 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2050 if (guid_offset == -1)
2051 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2052 impinfo.oGuid = guid_offset;
2053 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
2056 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
2058 int num_parents = 0, num_funcs = 0;
2059 importinfo_t *importinfo = NULL;
2060 const statement_t *stmt_func;
2061 type_t *inherit, *ref;
2062 int idx = 0;
2063 var_t *func;
2064 var_t *var;
2065 msft_typeinfo_t *msft_typeinfo;
2067 if (-1 < dispinterface->typelib_idx)
2068 return;
2070 inherit = type_dispiface_get_inherit(dispinterface);
2072 if (inherit)
2074 importinfo = find_importinfo(typelib, inherit->name);
2076 if (!importinfo && type_iface_get_inherit(inherit) && inherit->typelib_idx == -1)
2077 add_interface_typeinfo(typelib, inherit);
2080 /* check typelib_idx again, it could have been added while resolving the parent interface */
2081 if (-1 < dispinterface->typelib_idx)
2082 return;
2084 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2085 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
2086 dispinterface->attrs);
2088 msft_typeinfo->typeinfo->size = pointer_size;
2089 msft_typeinfo->typeinfo->typekind |= pointer_size << 11 | pointer_size << 6;
2091 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2092 add_dispatch(typelib);
2094 if (inherit)
2096 add_impl_type(msft_typeinfo, inherit, importinfo);
2097 msft_typeinfo->typeinfo->typekind |= 0x10;
2100 /* count the number of inherited interfaces and non-local functions */
2101 for (ref = inherit; ref; ref = type_iface_get_inherit(ref))
2103 num_parents++;
2104 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) )
2106 var_t *func = stmt_func->u.var;
2107 if (!is_local(func->attrs)) num_funcs++;
2110 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2111 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2113 msft_typeinfo->typeinfo->cImplTypes = 1; /* IDispatch */
2115 /* count the no of methods, as the variable indices come after the funcs */
2116 if (dispinterface->details.iface->disp_methods)
2117 LIST_FOR_EACH_ENTRY( func, dispinterface->details.iface->disp_methods, var_t, entry )
2118 idx++;
2120 if (type_dispiface_get_props(dispinterface))
2121 LIST_FOR_EACH_ENTRY( var, type_dispiface_get_props(dispinterface), var_t, entry )
2122 add_var_desc(msft_typeinfo, idx++, var);
2124 if (type_dispiface_get_methods(dispinterface))
2126 idx = 0;
2127 LIST_FOR_EACH_ENTRY( func, type_dispiface_get_methods(dispinterface), var_t, entry )
2128 if(add_func_desc(msft_typeinfo, func, idx))
2129 idx++;
2132 typelib->typelib->reg_ifaces = xrealloc(typelib->typelib->reg_ifaces,
2133 (typelib->typelib->reg_iface_count + 1) * sizeof(dispinterface));
2134 typelib->typelib->reg_ifaces[typelib->typelib->reg_iface_count++] = dispinterface;
2137 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
2139 int idx = 0;
2140 const statement_t *stmt_func;
2141 type_t *ref;
2142 msft_typeinfo_t *msft_typeinfo;
2143 importinfo_t *ref_importinfo = NULL;
2144 int num_parents = 0, num_funcs = 0;
2145 type_t *inherit;
2146 const type_t *derived;
2148 if (-1 < interface->typelib_idx)
2149 return;
2151 if (!interface->details.iface)
2153 error( "interface %s is referenced but not defined\n", interface->name );
2154 return;
2157 if (is_attr(interface->attrs, ATTR_DISPINTERFACE)) {
2158 add_dispinterface_typeinfo(typelib, interface);
2159 return;
2162 /* midl adds the parent interface first, unless the parent itself
2163 has no parent (i.e. it stops before IUnknown). */
2165 inherit = type_iface_get_inherit(interface);
2167 if(inherit) {
2168 ref_importinfo = find_importinfo(typelib, inherit->name);
2170 if(!ref_importinfo && type_iface_get_inherit(inherit) &&
2171 inherit->typelib_idx == -1)
2172 add_interface_typeinfo(typelib, inherit);
2175 /* check typelib_idx again, it could have been added while resolving the parent interface */
2176 if (-1 < interface->typelib_idx)
2177 return;
2179 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2180 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
2181 msft_typeinfo->typeinfo->size = pointer_size;
2182 msft_typeinfo->typeinfo->typekind |= 0x0200;
2183 msft_typeinfo->typeinfo->typekind |= pointer_size << 11;
2185 for (derived = inherit; derived; derived = type_iface_get_inherit(derived))
2186 if (derived->name && !strcmp(derived->name, "IDispatch"))
2187 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2189 if(type_iface_get_inherit(interface))
2190 add_impl_type(msft_typeinfo, type_iface_get_inherit(interface),
2191 ref_importinfo);
2193 /* count the number of inherited interfaces and non-local functions */
2194 for(ref = inherit; ref; ref = type_iface_get_inherit(ref)) {
2195 num_parents++;
2196 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) ) {
2197 var_t *func = stmt_func->u.var;
2198 if (!is_local(func->attrs)) num_funcs++;
2201 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2202 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2204 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(interface) ) {
2205 var_t *func = stmt_func->u.var;
2206 if(add_func_desc(msft_typeinfo, func, idx))
2207 idx++;
2210 if (is_attr(interface->attrs, ATTR_OLEAUTOMATION) || is_attr(interface->attrs, ATTR_DUAL))
2212 typelib->typelib->reg_ifaces = xrealloc(typelib->typelib->reg_ifaces,
2213 (typelib->typelib->reg_iface_count + 1) * sizeof(interface));
2214 typelib->typelib->reg_ifaces[typelib->typelib->reg_iface_count++] = interface;
2218 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2220 var_list_t *fields;
2221 int idx = 0;
2222 var_t *cur;
2223 msft_typeinfo_t *msft_typeinfo;
2225 if (-1 < structure->typelib_idx)
2226 return;
2228 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2229 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2230 msft_typeinfo->typeinfo->size = 0;
2232 if (type_get_type(structure) == TYPE_STRUCT)
2233 fields = type_struct_get_fields(structure);
2234 else
2235 fields = type_encapsulated_union_get_fields(structure);
2237 if (fields)
2239 LIST_FOR_EACH_ENTRY( cur, fields, var_t, entry )
2240 add_var_desc(msft_typeinfo, idx++, cur);
2244 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2246 int idx = 0;
2247 var_t *cur;
2248 msft_typeinfo_t *msft_typeinfo;
2250 if (-1 < enumeration->typelib_idx)
2251 return;
2253 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2254 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2255 msft_typeinfo->typeinfo->size = 0;
2257 if (type_enum_get_values(enumeration))
2258 LIST_FOR_EACH_ENTRY( cur, type_enum_get_values(enumeration), var_t, entry )
2259 add_var_desc(msft_typeinfo, idx++, cur);
2262 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion)
2264 int idx = 0;
2265 var_t *cur;
2266 msft_typeinfo_t *msft_typeinfo;
2268 if (-1 < tunion->typelib_idx)
2269 return;
2271 if (!tunion->name)
2272 tunion->name = gen_name();
2274 tunion->typelib_idx = typelib->typelib_header.nrtypeinfos;
2275 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_UNION, tunion->name, tunion->attrs);
2276 msft_typeinfo->typeinfo->size = 0;
2278 if (type_union_get_cases(tunion))
2279 LIST_FOR_EACH_ENTRY(cur, type_union_get_cases(tunion), var_t, entry)
2280 add_var_desc(msft_typeinfo, idx++, cur);
2283 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2285 msft_typeinfo_t *msft_typeinfo = NULL;
2286 int datatype1, datatype2, duplicate = 0;
2287 unsigned int size, alignment = 0;
2288 type_t *type;
2290 if (-1 < tdef->typelib_idx)
2291 return;
2293 type = type_alias_get_aliasee_type(tdef);
2295 if (!type->name || strcmp(tdef->name, type->name) != 0)
2297 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2298 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2300 else
2301 duplicate = 1;
2303 encode_type(typelib, get_type_vt(type), type, &datatype1, &datatype2);
2304 size = type_memsize_and_alignment(type, &alignment);
2306 if (msft_typeinfo)
2308 msft_typeinfo->typeinfo->datatype1 = datatype1;
2309 msft_typeinfo->typeinfo->size = size;
2310 msft_typeinfo->typeinfo->datatype2 = datatype2;
2311 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2314 /* avoid adding duplicate type definitions */
2315 if (duplicate)
2316 tdef->typelib_idx = type->typelib_idx;
2319 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2321 msft_typeinfo_t *msft_typeinfo;
2322 typeref_t *iref;
2323 int num_ifaces = 0, offset, i;
2324 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2325 int have_default = 0, have_default_source = 0;
2326 const attr_t *attr;
2327 typeref_list_t *ifaces;
2329 if (-1 < cls->typelib_idx)
2330 return;
2332 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2333 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2335 ifaces = type_coclass_get_ifaces(cls);
2336 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, typeref_t, entry ) num_ifaces++;
2338 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2339 num_ifaces * sizeof(*ref), 0);
2341 i = 0;
2342 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, typeref_t, entry ) {
2343 if(iref->type->typelib_idx == -1)
2344 add_interface_typeinfo(typelib, iref->type);
2345 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2346 ref->reftype = typelib->typelib_typeinfo_offsets[iref->type->typelib_idx];
2347 ref->flags = 0;
2348 ref->oCustData = -1;
2349 ref->onext = -1;
2350 if(i < num_ifaces - 1)
2351 ref->onext = offset + (i + 1) * sizeof(*ref);
2353 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2354 switch(attr->type) {
2355 case ATTR_DEFAULT:
2356 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2357 break;
2358 case ATTR_DEFAULTVTABLE:
2359 ref->flags |= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2360 break;
2361 case ATTR_RESTRICTED:
2362 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2363 break;
2364 case ATTR_SOURCE:
2365 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2366 break;
2367 default:
2368 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2371 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2372 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2373 have_default_source = 1;
2374 else
2375 have_default = 1;
2378 /* If the interface is non-restricted and we haven't already had one then
2379 remember it so that we can use it as a default later */
2380 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2381 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2382 if(!first_source)
2383 first_source = ref;
2385 else if(!first)
2386 first = ref;
2388 i++;
2391 /* If we haven't had a default interface, then set the default flags on the
2392 first ones */
2393 if(!have_default && first)
2394 first->flags |= 0x1;
2395 if(!have_default_source && first_source)
2396 first_source->flags |= 0x1;
2398 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2399 msft_typeinfo->typeinfo->size = pointer_size;
2400 msft_typeinfo->typeinfo->typekind |= 0x2200;
2403 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2405 int idx = 0;
2406 const statement_t *stmt;
2407 msft_typeinfo_t *msft_typeinfo;
2409 if (-1 < module->typelib_idx)
2410 return;
2412 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2413 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2414 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2416 STATEMENTS_FOR_EACH_FUNC( stmt, module->details.module->stmts ) {
2417 var_t *func = stmt->u.var;
2418 if(add_func_desc(msft_typeinfo, func, idx))
2419 idx++;
2422 msft_typeinfo->typeinfo->size = idx;
2425 static void add_type_typeinfo(msft_typelib_t *typelib, type_t *type)
2427 switch (type_get_type(type)) {
2428 case TYPE_INTERFACE:
2429 add_interface_typeinfo(typelib, type);
2430 break;
2431 case TYPE_STRUCT:
2432 case TYPE_ENCAPSULATED_UNION:
2433 add_structure_typeinfo(typelib, type);
2434 break;
2435 case TYPE_ENUM:
2436 add_enum_typeinfo(typelib, type);
2437 break;
2438 case TYPE_UNION:
2439 add_union_typeinfo(typelib, type);
2440 break;
2441 case TYPE_COCLASS:
2442 add_coclass_typeinfo(typelib, type);
2443 break;
2444 case TYPE_BASIC:
2445 case TYPE_POINTER:
2446 case TYPE_ARRAY:
2447 break;
2448 default:
2449 error("add_entry: unhandled type 0x%x for %s\n",
2450 type_get_type(type), type->name);
2451 break;
2455 static void add_entry(msft_typelib_t *typelib, const statement_t *stmt)
2457 switch(stmt->type) {
2458 case STMT_LIBRARY:
2459 case STMT_IMPORT:
2460 case STMT_PRAGMA:
2461 case STMT_CPPQUOTE:
2462 case STMT_DECLARATION:
2463 /* not included in typelib */
2464 break;
2465 case STMT_IMPORTLIB:
2466 /* not processed here */
2467 break;
2468 case STMT_TYPEDEF:
2470 typeref_t *ref;
2471 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry) {
2472 /* if the type is public then add the typedef, otherwise attempt
2473 * to add the aliased type */
2474 if (is_attr(ref->type->attrs, ATTR_PUBLIC))
2475 add_typedef_typeinfo(typelib, ref->type);
2476 else
2477 add_type_typeinfo(typelib, type_alias_get_aliasee_type(ref->type));
2479 break;
2481 case STMT_MODULE:
2482 add_module_typeinfo(typelib, stmt->u.type);
2483 break;
2484 case STMT_TYPE:
2485 case STMT_TYPEREF:
2487 type_t *type = stmt->u.type;
2488 add_type_typeinfo(typelib, type);
2489 break;
2494 static void set_name(msft_typelib_t *typelib)
2496 int offset;
2498 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2499 if (offset == -1) return;
2500 typelib->typelib_header.NameOffset = offset;
2501 return;
2504 static void set_version(msft_typelib_t *typelib)
2506 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2509 static void set_guid(msft_typelib_t *typelib)
2511 MSFT_GuidEntry guidentry = { {0}, -2, -1 };
2512 int offset;
2513 struct uuid *ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2515 if (ptr) guidentry.guid = *ptr;
2517 offset = ctl2_alloc_guid(typelib, &guidentry);
2518 typelib->typelib_header.posguid = offset;
2520 return;
2523 static void set_doc_string(msft_typelib_t *typelib)
2525 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2527 if (str)
2529 int offset = ctl2_alloc_string(typelib, str);
2530 if (offset != -1) typelib->typelib_header.helpstring = offset;
2534 static void set_help_file_name(msft_typelib_t *typelib)
2536 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2538 if (str)
2540 int offset = ctl2_alloc_string(typelib, str);
2541 if (offset != -1)
2543 typelib->typelib_header.helpfile = offset;
2544 typelib->typelib_header.varflags |= 0x10;
2549 static void set_help_context(msft_typelib_t *typelib)
2551 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2552 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2555 static void set_help_string_dll(msft_typelib_t *typelib)
2557 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2559 if (str)
2561 int offset = ctl2_alloc_string(typelib, str);
2562 if (offset != -1)
2564 typelib->help_string_dll_offset = offset;
2565 typelib->typelib_header.varflags |= 0x100;
2570 static void set_help_string_context(msft_typelib_t *typelib)
2572 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2573 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2576 static void set_lcid(msft_typelib_t *typelib)
2578 const expr_t *lcid_expr = get_attrp( typelib->typelib->attrs, ATTR_LIBLCID );
2579 if(lcid_expr)
2581 typelib->typelib_header.lcid = lcid_expr->cval;
2582 typelib->typelib_header.lcid2 = lcid_expr->cval;
2586 static void set_lib_flags(msft_typelib_t *typelib)
2588 const attr_t *attr;
2590 typelib->typelib_header.flags = 0;
2591 if (!typelib->typelib->attrs) return;
2592 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2594 switch(attr->type) {
2595 case ATTR_CONTROL:
2596 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2597 break;
2598 case ATTR_HIDDEN:
2599 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2600 break;
2601 case ATTR_RESTRICTED:
2602 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2603 break;
2604 default:
2605 break;
2608 return;
2611 static void ctl2_write_segment(msft_typelib_t *typelib, int segment)
2613 if (typelib->typelib_segment_data[segment])
2614 put_data(typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length);
2617 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2619 msft_typeinfo_t *typeinfo;
2621 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2622 typeinfo->typeinfo->memoffset = filesize;
2623 if (typeinfo->func_data)
2624 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2625 if (typeinfo->var_data)
2626 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2627 if (typeinfo->func_data || typeinfo->var_data)
2628 filesize += 4;
2632 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2634 if (typelib->typelib_segdir[segment].length) {
2635 typelib->typelib_segdir[segment].offset = filepos;
2636 } else {
2637 typelib->typelib_segdir[segment].offset = -1;
2640 return typelib->typelib_segdir[segment].length;
2644 static void ctl2_write_typeinfos(msft_typelib_t *typelib)
2646 msft_typeinfo_t *typeinfo;
2647 int typedata_size;
2649 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2650 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2651 typedata_size = 0;
2652 if (typeinfo->func_data)
2653 typedata_size = typeinfo->func_data[0];
2654 if (typeinfo->var_data)
2655 typedata_size += typeinfo->var_data[0];
2656 put_data(&typedata_size, sizeof(int));
2657 if (typeinfo->func_data)
2658 put_data(typeinfo->func_data + 1, typeinfo->func_data[0]);
2659 if (typeinfo->var_data)
2660 put_data(typeinfo->var_data + 1, typeinfo->var_data[0]);
2661 if (typeinfo->func_indices)
2662 put_data(typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2663 if (typeinfo->var_indices)
2664 put_data(typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2665 if (typeinfo->func_names)
2666 put_data(typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2667 if (typeinfo->var_names)
2668 put_data(typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2669 if (typeinfo->func_offsets)
2670 put_data(typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2671 if (typeinfo->var_offsets) {
2672 int add = 0, i, offset;
2673 if(typeinfo->func_data)
2674 add = typeinfo->func_data[0];
2675 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2676 offset = typeinfo->var_offsets[i];
2677 offset += add;
2678 put_data(&offset, 4);
2684 static void save_all_changes(msft_typelib_t *typelib)
2686 int filepos;
2688 chat("save_all_changes(%p)\n", typelib);
2690 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2691 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2692 filepos += typelib->typelib_header.nrtypeinfos * 4;
2694 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2695 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2696 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2697 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2698 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2699 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2700 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2701 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2702 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2703 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2704 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2705 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2706 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2708 ctl2_finalize_typeinfos(typelib, filepos);
2710 init_output_buffer();
2712 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2713 if(typelib->typelib_header.varflags & 0x100)
2714 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2716 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2717 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2718 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2719 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2720 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2721 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2722 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2723 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2724 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2725 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2726 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2727 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2728 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2729 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2730 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2732 ctl2_write_typeinfos(typelib);
2734 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2736 char typelib_id[13] = "#1";
2738 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2739 if (expr)
2740 sprintf( typelib_id, "#%d", expr->cval );
2741 add_output_to_resources( "TYPELIB", typelib_id );
2742 if (strendswith( typelib_name, "_t.res" )) /* add typelib registration */
2743 output_typelib_regscript( typelib->typelib );
2745 else flush_output_buffer( typelib_name );
2748 int create_msft_typelib(typelib_t *typelib)
2750 msft_typelib_t *msft;
2751 int failed = 0;
2752 const statement_t *stmt;
2753 const attr_t *attr;
2754 time_t cur_time;
2755 char *time_override;
2756 unsigned int version = 7 << 24 | 555; /* 7.00.0555 */
2757 static const struct uuid midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2758 static const struct uuid midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2759 static const struct uuid midl_info_guid = {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2760 char info_string[128];
2762 msft = xmalloc(sizeof(*msft));
2763 memset(msft, 0, sizeof(*msft));
2764 msft->typelib = typelib;
2766 ctl2_init_header(msft);
2767 ctl2_init_segdir(msft);
2769 msft->typelib_header.varflags |= (pointer_size == 8) ? SYS_WIN64 : SYS_WIN32;
2772 * The following two calls return an offset or -1 if out of memory. We
2773 * specifically need an offset of 0, however, so...
2775 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2776 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2778 if(failed)
2780 free(msft);
2781 return 0;
2784 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2785 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2787 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2788 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2790 set_lib_flags(msft);
2791 set_lcid(msft);
2792 set_help_file_name(msft);
2793 set_doc_string(msft);
2794 set_guid(msft);
2795 set_version(msft);
2796 set_name(msft);
2797 set_help_context(msft);
2798 set_help_string_dll(msft);
2799 set_help_string_context(msft);
2801 if (typelib->attrs) LIST_FOR_EACH_ENTRY( attr, typelib->attrs, const attr_t, entry ) {
2802 switch(attr->type) {
2803 case ATTR_CUSTOM:
2804 set_custdata_attr(msft, attr->u.pval, &msft->typelib_header.CustomDataOffset);
2805 break;
2806 default:
2807 break;
2811 /* midl adds two sets of custom data to the library: the current unix time
2812 and midl's version number */
2813 time_override = getenv( "WIDL_TIME_OVERRIDE");
2814 cur_time = time_override ? atol( time_override) : time(NULL);
2815 sprintf(info_string, "Created by WIDL version %s at %s", PACKAGE_VERSION, ctime(&cur_time));
2816 set_custdata(msft, &midl_info_guid, VT_BSTR, info_string, &msft->typelib_header.CustomDataOffset);
2817 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2818 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2820 if (typelib->stmts)
2821 LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
2822 add_entry(msft, stmt);
2824 save_all_changes(msft);
2825 free(msft);
2826 return 1;