ntdll: Initialize the system locale on the Unix side.
[wine.git] / tools / widl / write_msft.c
blobabeb37f34a4d22d90f94ce54eaebfa2c4bd7f1b7
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, elements = 1, 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;
1086 elements *= type_array_get_dim(atype);
1089 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1090 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1092 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1093 typedata[1] = arrayoffset;
1095 *encoded_type = typeoffset;
1096 *decoded_size = 20 /*sizeof(ARRAYDESC)*/ + (num_dims - 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1097 return 0;
1100 vt = get_type_vt(type);
1101 if (vt == VT_PTR) {
1102 type_t *ref = is_ptr(type) ?
1103 type_pointer_get_ref_type(type) : type_array_get_element_type(type);
1104 int skip_ptr = encode_var(typelib, ref, var, &target_type, &child_size);
1106 if(skip_ptr == 2) {
1107 chat("encode_var: skipping ptr\n");
1108 *encoded_type = target_type;
1109 *decoded_size = child_size;
1110 return 0;
1113 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1114 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1115 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1118 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1119 int mix_field;
1121 if (target_type & 0x80000000) {
1122 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1123 } else if (get_type_vt(ref) == VT_SAFEARRAY) {
1124 type_t *element_type = type_alias_get_aliasee_type(type_array_get_element_type(ref));
1125 mix_field = get_type_vt(element_type) | VT_ARRAY | VT_BYREF;
1126 } else {
1127 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1128 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1131 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1132 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1134 typedata[0] = (mix_field << 16) | VT_PTR;
1135 typedata[1] = target_type;
1138 *encoded_type = typeoffset;
1140 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1141 return 0;
1144 dump_type(type);
1146 encode_type(typelib, vt, type, encoded_type, decoded_size);
1147 /* these types already have an implicit pointer, so we don't need to
1148 * add another */
1149 if(vt == VT_DISPATCH || vt == VT_UNKNOWN) return 2;
1150 return 0;
1153 static unsigned int get_ulong_val(unsigned int val, int vt)
1155 switch(vt) {
1156 case VT_I2:
1157 case VT_BOOL:
1158 case VT_UI2:
1159 return val & 0xffff;
1160 case VT_I1:
1161 case VT_UI1:
1162 return val & 0xff;
1165 return val;
1168 static void write_int_value(msft_typelib_t *typelib, int *out, int vt, int value)
1170 const unsigned int lv = get_ulong_val(value, vt);
1171 if ((lv & 0x3ffffff) == lv) {
1172 *out = 0x80000000;
1173 *out |= vt << 26;
1174 *out |= lv;
1175 } else {
1176 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1177 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1178 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &value, 4);
1179 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1180 *out = offset;
1184 static void write_string_value(msft_typelib_t *typelib, int *out, const char *value)
1186 int len = strlen(value), seg_len = (len + 6 + 3) & ~0x3;
1187 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1188 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = VT_BSTR;
1189 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &len, sizeof(len));
1190 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1191 len += 6;
1192 while(len < seg_len) {
1193 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1194 len++;
1196 *out = offset;
1199 static void write_default_value(msft_typelib_t *typelib, type_t *type, expr_t *expr, int *out)
1201 int vt;
1203 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT) {
1204 vt = get_type_vt(type);
1205 if (vt != VT_BSTR && vt != VT_VARIANT)
1206 error("string default value applied to non-string type\n");
1207 chat("default value '%s'\n", expr->u.sval);
1208 write_string_value(typelib, out, expr->u.sval);
1209 return;
1212 if (type_get_type(type) == TYPE_ENUM) {
1213 vt = VT_I4;
1214 } else if (is_ptr(type)) {
1215 vt = get_type_vt(type_pointer_get_ref_type(type));
1216 if (vt == VT_USERDEFINED)
1217 vt = VT_I4;
1218 if (expr->cval)
1219 warning("non-null pointer default value\n");
1220 } else {
1221 vt = get_type_vt(type);
1222 switch(vt) {
1223 case VT_I2:
1224 case VT_I4:
1225 case VT_R4:
1226 case VT_BOOL:
1227 case VT_I1:
1228 case VT_UI1:
1229 case VT_UI2:
1230 case VT_UI4:
1231 case VT_INT:
1232 case VT_UINT:
1233 case VT_HRESULT:
1234 break;
1235 case VT_VARIANT: {
1236 switch (expr->type) {
1237 case EXPR_DOUBLE:
1238 vt = VT_R4;
1239 break;
1240 case EXPR_NUM:
1241 vt = VT_I4;
1242 break;
1243 default:
1244 warning("can't write default VT_VARIANT value for expression type %d.\n", expr->type);
1245 return;
1247 break;
1249 default:
1250 warning("can't write value of type %d yet\n", vt);
1251 return;
1255 write_int_value(typelib, out, vt, expr->cval);
1258 static void set_custdata(msft_typelib_t *typelib, const struct uuid *guid,
1259 int vt, const void *value, int *offset)
1261 int guidoffset;
1262 int custoffset;
1263 int *custdata;
1264 int data_out;
1265 int hash_key;
1267 hash_key = ctl2_hash_guid(guid);
1268 guidoffset = ctl2_find_guid(typelib, hash_key, guid);
1269 if(guidoffset == -1) {
1270 /* add GUID that was not already present */
1271 MSFT_GuidEntry guidentry;
1272 guidentry.guid = *guid;
1274 guidentry.hreftype = -1;
1275 guidentry.next_hash = -1;
1277 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1280 if(vt == VT_BSTR)
1281 /* TODO midl appears to share a single reference if the same string is used as custdata in multiple places */
1282 write_string_value(typelib, &data_out, value);
1283 else
1284 write_int_value(typelib, &data_out, vt, *(int*)value);
1286 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1288 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1289 custdata[0] = guidoffset;
1290 custdata[1] = data_out;
1291 custdata[2] = *offset;
1292 *offset = custoffset;
1295 static void set_custdata_attr(msft_typelib_t *typelib, attr_custdata_t *custdata, int *offset)
1297 switch(custdata->pval->type) {
1298 case EXPR_STRLIT:
1299 case EXPR_WSTRLIT:
1300 set_custdata(typelib, &custdata->id, VT_BSTR, custdata->pval->u.sval, offset);
1301 break;
1302 case EXPR_HEXNUM:
1303 case EXPR_NUM:
1304 set_custdata(typelib, &custdata->id, VT_I4, &custdata->pval->u.lval, offset);
1305 break;
1306 default:
1307 error("custom() attribute with unknown type\n");
1308 break;
1312 static int add_func_desc(msft_typeinfo_t* typeinfo, var_t *func, int index)
1314 int offset, name_offset;
1315 int *typedata, typedata_size;
1316 int i, id, next_idx;
1317 int decoded_size, extra_attr = 0;
1318 int num_params = 0, num_optional = 0, num_defaults = 0;
1319 int has_arg_custdata = 0;
1320 var_t *arg;
1321 unsigned char *namedata;
1322 const attr_t *attr;
1323 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1324 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1325 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1326 int func_custdata_offset = -1;
1327 int entry = -1, entry_is_ord = 0;
1328 int lcid_retval_count = 0;
1330 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1332 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1334 switch(typeinfo->typekind) {
1335 case TKIND_DISPATCH:
1336 funckind = 0x4; /* FUNC_DISPATCH */
1337 break;
1338 case TKIND_MODULE:
1339 funckind = 0x3; /* FUNC_STATIC */
1340 break;
1341 default:
1342 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1343 break;
1346 if (is_local( func->attrs )) {
1347 chat("add_func_desc: skipping local function\n");
1348 return FALSE;
1351 if (type_function_get_args(func->declspec.type))
1352 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1354 num_params++;
1355 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1356 if(attr->type == ATTR_DEFAULTVALUE)
1357 num_defaults++;
1358 else if(attr->type == ATTR_OPTIONAL)
1359 num_optional++;
1360 else if(attr->type == ATTR_CUSTOM)
1361 has_arg_custdata = 1;
1365 chat("add_func_desc: num of params %d\n", num_params);
1367 name_offset = ctl2_alloc_name(typeinfo->typelib, func->name);
1369 if (func->attrs) LIST_FOR_EACH_ENTRY( attr, func->attrs, const attr_t, entry ) {
1370 expr_t *expr = attr->u.pval;
1371 switch(attr->type) {
1372 case ATTR_BINDABLE:
1373 funcflags |= 0x4; /* FUNCFLAG_FBINDABLE */
1374 break;
1375 case ATTR_CUSTOM:
1376 set_custdata_attr(typeinfo->typelib, attr->u.pval, &func_custdata_offset);
1377 break;
1378 case ATTR_DEFAULTBIND:
1379 funcflags |= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1380 break;
1381 case ATTR_DEFAULTCOLLELEM:
1382 funcflags |= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1383 break;
1384 case ATTR_DISPLAYBIND:
1385 funcflags |= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1386 break;
1387 case ATTR_ENTRY:
1388 extra_attr = max(extra_attr, 3);
1389 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1390 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1391 else {
1392 entry = expr->cval;
1393 entry_is_ord = 1;
1395 break;
1396 case ATTR_HELPCONTEXT:
1397 extra_attr = max(extra_attr, 1);
1398 help_context = expr->u.lval;
1399 break;
1400 case ATTR_HELPSTRING:
1401 extra_attr = max(extra_attr, 2);
1402 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1403 break;
1404 case ATTR_HELPSTRINGCONTEXT:
1405 extra_attr = max(extra_attr, 6);
1406 help_string_context = expr->u.lval;
1407 break;
1408 case ATTR_HIDDEN:
1409 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1410 break;
1411 case ATTR_ID:
1412 id = expr->cval;
1413 break;
1414 case ATTR_IMMEDIATEBIND:
1415 funcflags |= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1416 break;
1417 case ATTR_NONBROWSABLE:
1418 funcflags |= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1419 break;
1420 case ATTR_OUT:
1421 break;
1422 case ATTR_OVERLOAD:
1423 break;
1424 case ATTR_PROPGET:
1425 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1426 break;
1427 case ATTR_PROPPUT:
1428 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1429 break;
1430 case ATTR_PROPPUTREF:
1431 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1432 break;
1433 /* FIXME: FUNCFLAG_FREPLACEABLE */
1434 case ATTR_REQUESTEDIT:
1435 funcflags |= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1436 break;
1437 case ATTR_RESTRICTED:
1438 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1439 break;
1440 case ATTR_SOURCE:
1441 funcflags |= 0x2; /* FUNCFLAG_FSOURCE */
1442 break;
1443 case ATTR_UIDEFAULT:
1444 funcflags |= 0x200; /* FUNCFLAG_FUIDEFAULT */
1445 break;
1446 case ATTR_USESGETLASTERROR:
1447 funcflags |= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1448 break;
1449 case ATTR_VARARG:
1450 if (num_optional || num_defaults)
1451 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1452 else
1453 num_optional = -1;
1454 break;
1455 default:
1456 break;
1460 if(has_arg_custdata || func_custdata_offset != -1) {
1461 extra_attr = max(extra_attr, 7 + num_params);
1464 /* allocate type data space for us */
1465 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1467 if (!typeinfo->func_data) {
1468 typeinfo->func_data = xmalloc(0x100);
1469 typeinfo->func_data_allocated = 0x100;
1470 typeinfo->func_data[0] = 0;
1473 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1474 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1475 typeinfo->func_data[0] + typedata_size + sizeof(int));
1476 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1479 offset = typeinfo->func_data[0];
1480 typeinfo->func_data[0] += typedata_size;
1481 typedata = typeinfo->func_data + (offset >> 2) + 1;
1484 /* find func with the same name - if it exists use its id */
1485 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1486 if(name_offset == typeinfo->func_names[i]) {
1487 id = typeinfo->func_indices[i];
1488 break;
1492 /* find the first func with the same id and link via the hiword of typedata[4] */
1493 next_idx = index;
1494 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1495 if(id == typeinfo->func_indices[i]) {
1496 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1497 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1498 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1499 break;
1503 /* fill out the basic type information */
1504 typedata[0] = typedata_size | (index << 16);
1505 encode_var(typeinfo->typelib, type_function_get_rettype(func->declspec.type), func,
1506 &typedata[1], &decoded_size);
1507 typedata[2] = funcflags;
1508 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1509 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1510 if(has_arg_custdata || func_custdata_offset != -1) typedata[4] |= 0x0080;
1511 if(num_defaults) typedata[4] |= 0x1000;
1512 if(entry_is_ord) typedata[4] |= 0x2000;
1513 typedata[5] = (num_optional << 16) | num_params;
1515 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1516 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1517 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1518 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1520 switch(extra_attr) {
1521 default:
1522 if(extra_attr > 7 + num_params) warning("unknown number of optional attrs\n");
1523 /* typedata[13..+num_params] = arg_custdata_offset handled in below loop */
1524 case 7: typedata[12] = func_custdata_offset;
1525 case 6: typedata[11] = help_string_context;
1526 case 5: typedata[10] = -1;
1527 case 4: typedata[9] = -1;
1528 case 3: typedata[8] = entry;
1529 case 2: typedata[7] = help_string_offset;
1530 case 1: typedata[6] = help_context;
1531 case 0:
1532 break;
1535 if (type_function_get_args(func->declspec.type))
1537 i = 0;
1538 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1540 int paramflags = 0;
1541 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1542 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1543 int arg_custdata_offset = -1;
1545 if(defaultdata) *defaultdata = -1;
1547 encode_var(typeinfo->typelib, arg->declspec.type, arg, paramdata, &decoded_size);
1548 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1549 switch(attr->type) {
1550 case ATTR_CUSTOM:
1551 set_custdata_attr(typeinfo->typelib, attr->u.pval, &arg_custdata_offset);
1552 break;
1553 case ATTR_DEFAULTVALUE:
1555 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1556 write_default_value(typeinfo->typelib, arg->declspec.type, (expr_t *)attr->u.pval, defaultdata);
1557 break;
1559 case ATTR_IN:
1560 paramflags |= 0x01; /* PARAMFLAG_FIN */
1561 break;
1562 case ATTR_OPTIONAL:
1563 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1564 break;
1565 case ATTR_OUT:
1566 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1567 break;
1568 case ATTR_PARAMLCID:
1569 paramflags |= 0x04; /* PARAMFLAG_LCID */
1570 lcid_retval_count++;
1571 break;
1572 case ATTR_RETVAL:
1573 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1574 lcid_retval_count++;
1575 break;
1576 default:
1577 chat("unhandled param attr %d\n", attr->type);
1578 break;
1580 if(extra_attr > 7 + i) {
1581 typedata[13+i] = arg_custdata_offset;
1584 paramdata[1] = -1;
1585 paramdata[2] = paramflags;
1586 typedata[3] += decoded_size << 16;
1588 i++;
1592 if(lcid_retval_count == 1)
1593 typedata[4] |= 0x4000;
1594 else if(lcid_retval_count == 2)
1595 typedata[4] |= 0x8000;
1597 if(typeinfo->funcs_allocated == 0) {
1598 typeinfo->funcs_allocated = 10;
1599 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1600 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1601 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1603 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1604 typeinfo->funcs_allocated *= 2;
1605 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1606 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1607 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1610 /* update the index data */
1611 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1612 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1613 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1615 /* ??? */
1616 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1617 typeinfo->typeinfo->res2 <<= 1;
1618 /* ??? */
1619 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1621 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1622 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1623 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1625 /* adjust size of VTBL */
1626 if(funckind != 0x3 /* FUNC_STATIC */)
1627 typeinfo->typeinfo->cbSizeVft += pointer_size;
1629 /* Increment the number of function elements */
1630 typeinfo->typeinfo->cElement += 1;
1632 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1633 if (*((int *)namedata) == -1) {
1634 *((int *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1635 if(typeinfo->typekind == TKIND_MODULE)
1636 namedata[9] |= 0x10;
1637 } else
1638 namedata[9] &= ~0x10;
1640 if(typeinfo->typekind == TKIND_MODULE)
1641 namedata[9] |= 0x20;
1643 if (type_function_get_args(func->declspec.type))
1645 i = 0;
1646 LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), var_t, entry )
1648 /* don't give the last arg of a [propput*] func a name */
1649 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1651 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1652 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1653 paramdata[1] = offset;
1655 i++;
1658 return TRUE;
1661 static void add_var_desc(msft_typeinfo_t *typeinfo, unsigned int index, var_t* var)
1663 int offset, id;
1664 unsigned int typedata_size;
1665 int extra_attr = 0;
1666 int *typedata;
1667 unsigned int var_datawidth, var_alignment = 0;
1668 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1669 int alignment;
1670 int varflags = 0;
1671 const attr_t *attr;
1672 unsigned char *namedata;
1673 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1674 int var_custdata_offset = -1;
1676 if (!var->name)
1677 var->name = gen_name();
1679 chat("add_var_desc(%d, %s)\n", index, var->name);
1681 id = 0x40000000 + index;
1683 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1684 expr_t *expr = attr->u.pval;
1685 switch(attr->type) {
1686 case ATTR_BINDABLE:
1687 varflags |= 0x04; /* VARFLAG_FBINDABLE */
1688 break;
1689 case ATTR_CUSTOM:
1690 extra_attr = max(extra_attr,4);
1691 set_custdata_attr(typeinfo->typelib, attr->u.pval, &var_custdata_offset);
1692 break;
1693 case ATTR_DEFAULTBIND:
1694 varflags |= 0x20; /* VARFLAG_FDEFAULTBIND */
1695 break;
1696 case ATTR_DEFAULTCOLLELEM:
1697 varflags |= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1698 break;
1699 case ATTR_DISPLAYBIND:
1700 varflags |= 0x10; /* VARFLAG_FDISPLAYBIND */
1701 break;
1702 case ATTR_HIDDEN:
1703 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1704 break;
1705 case ATTR_ID:
1706 id = expr->cval;
1707 break;
1708 case ATTR_IMMEDIATEBIND:
1709 varflags |= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1710 break;
1711 case ATTR_NONBROWSABLE:
1712 varflags |= 0x400; /* VARFLAG_FNONBROWSABLE */
1713 break;
1714 case ATTR_READONLY:
1715 varflags |= 0x01; /* VARFLAG_FREADONLY */
1716 break;
1717 /* FIXME: VARFLAG_FREPLACEABLE */
1718 case ATTR_REQUESTEDIT:
1719 varflags |= 0x08; /* VARFLAG_FREQUESTEDIT */
1720 break;
1721 case ATTR_RESTRICTED:
1722 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1723 break;
1724 case ATTR_SOURCE:
1725 varflags |= 0x02; /* VARFLAG_FSOURCE */
1726 break;
1727 case ATTR_UIDEFAULT:
1728 varflags |= 0x0200; /* VARFLAG_FUIDEFAULT */
1729 break;
1730 default:
1731 break;
1735 /* allocate type data space for us */
1736 typedata_size = 0x14 + extra_attr * sizeof(int);
1738 if (!typeinfo->var_data) {
1739 typeinfo->var_data = xmalloc(0x100);
1740 typeinfo->var_data_allocated = 0x100;
1741 typeinfo->var_data[0] = 0;
1744 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1745 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1746 typeinfo->var_data[0] + typedata_size + sizeof(int));
1747 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1750 offset = typeinfo->var_data[0];
1751 typeinfo->var_data[0] += typedata_size;
1752 typedata = typeinfo->var_data + (offset >> 2) + 1;
1754 /* fill out the basic type information */
1755 typedata[0] = typedata_size | (index << 16);
1756 typedata[2] = varflags;
1757 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1759 if(typeinfo->vars_allocated == 0) {
1760 typeinfo->vars_allocated = 10;
1761 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1762 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1763 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1765 if(typeinfo->vars_allocated == var_num) {
1766 typeinfo->vars_allocated *= 2;
1767 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1768 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1769 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1771 /* update the index data */
1772 typeinfo->var_indices[var_num] = id;
1773 typeinfo->var_names[var_num] = -1;
1774 typeinfo->var_offsets[var_num] = offset;
1776 /* figure out type widths and whatnot */
1777 var_datawidth = type_memsize_and_alignment(var->declspec.type, &var_alignment);
1778 encode_var(typeinfo->typelib, var->declspec.type, var, &typedata[1], &var_type_size);
1780 /* pad out starting position to data width */
1781 typeinfo->datawidth += var_alignment - 1;
1782 typeinfo->datawidth &= ~(var_alignment - 1);
1784 switch(typeinfo->typekind) {
1785 case TKIND_ENUM:
1786 write_int_value(typeinfo->typelib, &typedata[4], VT_I4, var->eval->cval);
1787 var_kind = 2; /* VAR_CONST */
1788 var_type_size += 16; /* sizeof(VARIANT) */
1789 typeinfo->datawidth = var_datawidth;
1790 break;
1791 case TKIND_RECORD:
1792 typedata[4] = typeinfo->datawidth;
1793 typeinfo->datawidth += var_datawidth;
1794 break;
1795 case TKIND_UNION:
1796 typedata[4] = 0;
1797 typeinfo->datawidth = max(typeinfo->datawidth, var_datawidth);
1798 break;
1799 case TKIND_DISPATCH:
1800 var_kind = 3; /* VAR_DISPATCH */
1801 typedata[4] = 0;
1802 typeinfo->datawidth = pointer_size;
1803 break;
1804 default:
1805 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1806 break;
1809 /* add type description size to total required allocation */
1810 typedata[3] += var_type_size << 16 | var_kind;
1812 switch(extra_attr) {
1813 case 5: typedata[9] = -1 /*help_string_context*/;
1814 case 4: typedata[8] = var_custdata_offset;
1815 case 3: typedata[7] = -1;
1816 case 2: typedata[6] = -1 /*help_string_offset*/;
1817 case 1: typedata[5] = -1 /*help_context*/;
1818 case 0:
1819 break;
1820 default:
1821 warning("unknown number of optional attrs\n");
1824 /* fix type alignment */
1825 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1826 if (alignment < var_alignment) {
1827 alignment = var_alignment;
1828 typeinfo->typeinfo->typekind &= ~0xffc0;
1829 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1832 /* ??? */
1833 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1834 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1835 typeinfo->typeinfo->res2 <<= 1;
1838 /* ??? */
1839 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1840 typeinfo->typeinfo->res3 += 0x2c;
1842 /* increment the number of variable elements */
1843 typeinfo->typeinfo->cElement += 0x10000;
1845 /* pad data width to alignment */
1846 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1848 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1850 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1851 if (*((int *)namedata) == -1) {
1852 *((int *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1853 if(typeinfo->typekind != TKIND_DISPATCH)
1854 namedata[9] |= 0x10;
1855 } else
1856 namedata[9] &= ~0x10;
1858 if (typeinfo->typekind == TKIND_ENUM) {
1859 namedata[9] |= 0x20;
1861 typeinfo->var_names[var_num] = offset;
1864 static void add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1866 if(importinfo) {
1867 alloc_importinfo(typeinfo->typelib, importinfo);
1868 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1869 }else {
1870 if(ref->typelib_idx == -1)
1871 add_interface_typeinfo(typeinfo->typelib, ref);
1872 if(ref->typelib_idx == -1)
1873 error("add_impl_type: unable to add inherited interface\n");
1875 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1878 typeinfo->typeinfo->cImplTypes++;
1881 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1882 const char *name, const attr_list_t *attrs)
1884 const attr_t *attr;
1885 msft_typeinfo_t *msft_typeinfo;
1886 int nameoffset;
1887 int typeinfo_offset;
1888 MSFT_TypeInfoBase *typeinfo;
1889 MSFT_GuidEntry guidentry;
1891 chat("create_msft_typeinfo: name %s kind %d index %d\n", name, kind, typelib->typelib_header.nrtypeinfos);
1893 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1894 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1896 msft_typeinfo->typelib = typelib;
1898 nameoffset = ctl2_alloc_name(typelib, name);
1899 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1900 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1902 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1903 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1905 msft_typeinfo->typekind = kind;
1906 msft_typeinfo->typeinfo = typeinfo;
1908 typeinfo->typekind |= kind | 0x20;
1910 if(kind == TKIND_COCLASS)
1911 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1913 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1914 switch(attr->type) {
1915 case ATTR_AGGREGATABLE:
1916 if (kind == TKIND_COCLASS)
1917 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1918 break;
1920 case ATTR_APPOBJECT:
1921 if (kind == TKIND_COCLASS)
1922 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1923 break;
1925 case ATTR_CONTROL:
1926 if (kind == TKIND_COCLASS)
1927 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1928 break;
1929 case ATTR_CUSTOM:
1930 set_custdata_attr(typelib, attr->u.pval, &typeinfo->oCustData);
1931 break;
1932 case ATTR_DLLNAME:
1934 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1935 typeinfo->datatype1 = offset;
1936 break;
1939 case ATTR_DUAL:
1940 /* FIXME: check interface is compatible */
1941 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1942 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1943 break;
1945 case ATTR_HELPCONTEXT:
1947 expr_t *expr = (expr_t*)attr->u.pval;
1948 typeinfo->helpcontext = expr->cval;
1949 break;
1951 case ATTR_HELPSTRING:
1953 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1954 if (offset == -1) break;
1955 typeinfo->docstringoffs = offset;
1956 break;
1958 case ATTR_HELPSTRINGCONTEXT:
1960 expr_t *expr = (expr_t*)attr->u.pval;
1961 typeinfo->helpstringcontext = expr->cval;
1962 break;
1964 case ATTR_HIDDEN:
1965 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1966 break;
1968 case ATTR_LICENSED:
1969 typeinfo->flags |= 0x04; /* TYPEFLAG_FLICENSED */
1970 break;
1972 case ATTR_NONCREATABLE:
1973 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1974 break;
1976 case ATTR_NONEXTENSIBLE:
1977 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1978 break;
1980 case ATTR_OLEAUTOMATION:
1981 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1982 break;
1984 /* FIXME: TYPEFLAG_FPREDCLID */
1986 case ATTR_PROXY:
1987 typeinfo->flags |= 0x4000; /* TYPEFLAG_FPROXY */
1988 break;
1990 /* FIXME: TYPEFLAG_FREPLACEABLE */
1992 case ATTR_RESTRICTED:
1993 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
1994 break;
1996 case ATTR_UUID:
1997 guidentry.guid = *(struct uuid *)attr->u.pval;
1998 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1999 guidentry.next_hash = -1;
2000 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
2001 #if 0
2002 if (IsEqualIID(guid, &IID_IDispatch)) {
2003 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
2005 #endif
2006 break;
2008 case ATTR_VERSION:
2009 typeinfo->version = attr->u.ival;
2010 break;
2012 default:
2013 break;
2017 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
2018 typelib->last_typeinfo = msft_typeinfo;
2019 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
2022 return msft_typeinfo;
2025 static void add_dispatch(msft_typelib_t *typelib)
2027 int guid_offset, impfile_offset, hash_key;
2028 MSFT_GuidEntry guidentry;
2029 MSFT_ImpInfo impinfo;
2030 static const struct uuid stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2031 static const struct uuid iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2033 if(typelib->typelib_header.dispatchpos != -1) return;
2035 guidentry.guid = stdole;
2036 guidentry.hreftype = 2;
2037 guidentry.next_hash = -1;
2038 hash_key = ctl2_hash_guid(&guidentry.guid);
2039 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2040 if (guid_offset == -1)
2041 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2042 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
2044 guidentry.guid = iid_idispatch;
2045 guidentry.hreftype = 1;
2046 guidentry.next_hash = -1;
2047 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
2048 impinfo.oImpFile = impfile_offset;
2049 hash_key = ctl2_hash_guid(&guidentry.guid);
2050 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2051 if (guid_offset == -1)
2052 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2053 impinfo.oGuid = guid_offset;
2054 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
2057 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
2059 int num_parents = 0, num_funcs = 0;
2060 importinfo_t *importinfo = NULL;
2061 const statement_t *stmt_func;
2062 type_t *inherit, *ref;
2063 int idx = 0;
2064 var_t *func;
2065 var_t *var;
2066 msft_typeinfo_t *msft_typeinfo;
2068 if (-1 < dispinterface->typelib_idx)
2069 return;
2071 inherit = type_dispiface_get_inherit(dispinterface);
2073 if (inherit)
2075 importinfo = find_importinfo(typelib, inherit->name);
2077 if (!importinfo && type_iface_get_inherit(inherit) && inherit->typelib_idx == -1)
2078 add_interface_typeinfo(typelib, inherit);
2081 /* check typelib_idx again, it could have been added while resolving the parent interface */
2082 if (-1 < dispinterface->typelib_idx)
2083 return;
2085 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2086 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
2087 dispinterface->attrs);
2089 msft_typeinfo->typeinfo->size = pointer_size;
2090 msft_typeinfo->typeinfo->typekind |= pointer_size << 11 | pointer_size << 6;
2092 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2093 add_dispatch(typelib);
2095 if (inherit)
2097 add_impl_type(msft_typeinfo, inherit, importinfo);
2098 msft_typeinfo->typeinfo->typekind |= 0x10;
2101 /* count the number of inherited interfaces and non-local functions */
2102 for (ref = inherit; ref; ref = type_iface_get_inherit(ref))
2104 num_parents++;
2105 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) )
2107 var_t *func = stmt_func->u.var;
2108 if (!is_local(func->attrs)) num_funcs++;
2111 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2112 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2114 msft_typeinfo->typeinfo->cImplTypes = 1; /* IDispatch */
2116 /* count the no of methods, as the variable indices come after the funcs */
2117 if (dispinterface->details.iface->disp_methods)
2118 LIST_FOR_EACH_ENTRY( func, dispinterface->details.iface->disp_methods, var_t, entry )
2119 idx++;
2121 if (type_dispiface_get_props(dispinterface))
2122 LIST_FOR_EACH_ENTRY( var, type_dispiface_get_props(dispinterface), var_t, entry )
2123 add_var_desc(msft_typeinfo, idx++, var);
2125 if (type_dispiface_get_methods(dispinterface))
2127 idx = 0;
2128 LIST_FOR_EACH_ENTRY( func, type_dispiface_get_methods(dispinterface), var_t, entry )
2129 if(add_func_desc(msft_typeinfo, func, idx))
2130 idx++;
2133 typelib->typelib->reg_ifaces = xrealloc(typelib->typelib->reg_ifaces,
2134 (typelib->typelib->reg_iface_count + 1) * sizeof(dispinterface));
2135 typelib->typelib->reg_ifaces[typelib->typelib->reg_iface_count++] = dispinterface;
2138 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
2140 int idx = 0;
2141 const statement_t *stmt_func;
2142 type_t *ref;
2143 msft_typeinfo_t *msft_typeinfo;
2144 importinfo_t *ref_importinfo = NULL;
2145 int num_parents = 0, num_funcs = 0;
2146 type_t *inherit;
2147 const type_t *derived;
2149 if (-1 < interface->typelib_idx)
2150 return;
2152 if (!interface->details.iface)
2154 error( "interface %s is referenced but not defined\n", interface->name );
2155 return;
2158 if (is_attr(interface->attrs, ATTR_DISPINTERFACE)) {
2159 add_dispinterface_typeinfo(typelib, interface);
2160 return;
2163 /* midl adds the parent interface first, unless the parent itself
2164 has no parent (i.e. it stops before IUnknown). */
2166 inherit = type_iface_get_inherit(interface);
2168 if(inherit) {
2169 ref_importinfo = find_importinfo(typelib, inherit->name);
2171 if(!ref_importinfo && type_iface_get_inherit(inherit) &&
2172 inherit->typelib_idx == -1)
2173 add_interface_typeinfo(typelib, inherit);
2176 /* check typelib_idx again, it could have been added while resolving the parent interface */
2177 if (-1 < interface->typelib_idx)
2178 return;
2180 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2181 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
2182 msft_typeinfo->typeinfo->size = pointer_size;
2183 msft_typeinfo->typeinfo->typekind |= 0x0200;
2184 msft_typeinfo->typeinfo->typekind |= pointer_size << 11;
2186 for (derived = inherit; derived; derived = type_iface_get_inherit(derived))
2187 if (derived->name && !strcmp(derived->name, "IDispatch"))
2188 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2190 if(type_iface_get_inherit(interface))
2191 add_impl_type(msft_typeinfo, type_iface_get_inherit(interface),
2192 ref_importinfo);
2194 /* count the number of inherited interfaces and non-local functions */
2195 for(ref = inherit; ref; ref = type_iface_get_inherit(ref)) {
2196 num_parents++;
2197 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) ) {
2198 var_t *func = stmt_func->u.var;
2199 if (!is_local(func->attrs)) num_funcs++;
2202 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2203 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2205 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(interface) ) {
2206 var_t *func = stmt_func->u.var;
2207 if(add_func_desc(msft_typeinfo, func, idx))
2208 idx++;
2211 if (is_attr(interface->attrs, ATTR_OLEAUTOMATION) || is_attr(interface->attrs, ATTR_DUAL))
2213 typelib->typelib->reg_ifaces = xrealloc(typelib->typelib->reg_ifaces,
2214 (typelib->typelib->reg_iface_count + 1) * sizeof(interface));
2215 typelib->typelib->reg_ifaces[typelib->typelib->reg_iface_count++] = interface;
2219 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2221 var_list_t *fields;
2222 int idx = 0;
2223 var_t *cur;
2224 msft_typeinfo_t *msft_typeinfo;
2226 if (-1 < structure->typelib_idx)
2227 return;
2229 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2230 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2231 msft_typeinfo->typeinfo->size = 0;
2233 if (type_get_type(structure) == TYPE_STRUCT)
2234 fields = type_struct_get_fields(structure);
2235 else
2236 fields = type_encapsulated_union_get_fields(structure);
2238 if (fields)
2240 LIST_FOR_EACH_ENTRY( cur, fields, var_t, entry )
2241 add_var_desc(msft_typeinfo, idx++, cur);
2245 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2247 int idx = 0;
2248 var_t *cur;
2249 msft_typeinfo_t *msft_typeinfo;
2251 if (-1 < enumeration->typelib_idx)
2252 return;
2254 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2255 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2256 msft_typeinfo->typeinfo->size = 0;
2258 if (type_enum_get_values(enumeration))
2259 LIST_FOR_EACH_ENTRY( cur, type_enum_get_values(enumeration), var_t, entry )
2260 add_var_desc(msft_typeinfo, idx++, cur);
2263 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion)
2265 int idx = 0;
2266 var_t *cur;
2267 msft_typeinfo_t *msft_typeinfo;
2269 if (-1 < tunion->typelib_idx)
2270 return;
2272 if (!tunion->name)
2273 tunion->name = gen_name();
2275 tunion->typelib_idx = typelib->typelib_header.nrtypeinfos;
2276 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_UNION, tunion->name, tunion->attrs);
2277 msft_typeinfo->typeinfo->size = 0;
2279 if (type_union_get_cases(tunion))
2280 LIST_FOR_EACH_ENTRY(cur, type_union_get_cases(tunion), var_t, entry)
2281 add_var_desc(msft_typeinfo, idx++, cur);
2284 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2286 msft_typeinfo_t *msft_typeinfo = NULL;
2287 int datatype1, datatype2, duplicate = 0;
2288 unsigned int size, alignment = 0;
2289 type_t *type;
2291 if (-1 < tdef->typelib_idx)
2292 return;
2294 type = type_alias_get_aliasee_type(tdef);
2296 if (!type->name || strcmp(tdef->name, type->name) != 0)
2298 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2299 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2301 else
2302 duplicate = 1;
2304 encode_type(typelib, get_type_vt(type), type, &datatype1, &datatype2);
2305 size = type_memsize_and_alignment(type, &alignment);
2307 if (msft_typeinfo)
2309 msft_typeinfo->typeinfo->datatype1 = datatype1;
2310 msft_typeinfo->typeinfo->size = size;
2311 msft_typeinfo->typeinfo->datatype2 = datatype2;
2312 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2315 /* avoid adding duplicate type definitions */
2316 if (duplicate)
2317 tdef->typelib_idx = type->typelib_idx;
2320 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2322 msft_typeinfo_t *msft_typeinfo;
2323 typeref_t *iref;
2324 int num_ifaces = 0, offset, i;
2325 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2326 int have_default = 0, have_default_source = 0;
2327 const attr_t *attr;
2328 typeref_list_t *ifaces;
2330 if (-1 < cls->typelib_idx)
2331 return;
2333 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2334 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2336 ifaces = type_coclass_get_ifaces(cls);
2337 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, typeref_t, entry ) num_ifaces++;
2339 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2340 num_ifaces * sizeof(*ref), 0);
2342 i = 0;
2343 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, typeref_t, entry ) {
2344 if(iref->type->typelib_idx == -1)
2345 add_interface_typeinfo(typelib, iref->type);
2346 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2347 ref->reftype = typelib->typelib_typeinfo_offsets[iref->type->typelib_idx];
2348 ref->flags = 0;
2349 ref->oCustData = -1;
2350 ref->onext = -1;
2351 if(i < num_ifaces - 1)
2352 ref->onext = offset + (i + 1) * sizeof(*ref);
2354 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2355 switch(attr->type) {
2356 case ATTR_DEFAULT:
2357 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2358 break;
2359 case ATTR_DEFAULTVTABLE:
2360 ref->flags |= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2361 break;
2362 case ATTR_RESTRICTED:
2363 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2364 break;
2365 case ATTR_SOURCE:
2366 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2367 break;
2368 default:
2369 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2372 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2373 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2374 have_default_source = 1;
2375 else
2376 have_default = 1;
2379 /* If the interface is non-restricted and we haven't already had one then
2380 remember it so that we can use it as a default later */
2381 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2382 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2383 if(!first_source)
2384 first_source = ref;
2386 else if(!first)
2387 first = ref;
2389 i++;
2392 /* If we haven't had a default interface, then set the default flags on the
2393 first ones */
2394 if(!have_default && first)
2395 first->flags |= 0x1;
2396 if(!have_default_source && first_source)
2397 first_source->flags |= 0x1;
2399 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2400 msft_typeinfo->typeinfo->size = pointer_size;
2401 msft_typeinfo->typeinfo->typekind |= 0x2200;
2404 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2406 int idx = 0;
2407 const statement_t *stmt;
2408 msft_typeinfo_t *msft_typeinfo;
2410 if (-1 < module->typelib_idx)
2411 return;
2413 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2414 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2415 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2417 STATEMENTS_FOR_EACH_FUNC( stmt, module->details.module->stmts ) {
2418 var_t *func = stmt->u.var;
2419 if(add_func_desc(msft_typeinfo, func, idx))
2420 idx++;
2423 msft_typeinfo->typeinfo->size = idx;
2426 static void add_type_typeinfo(msft_typelib_t *typelib, type_t *type)
2428 switch (type_get_type(type)) {
2429 case TYPE_INTERFACE:
2430 add_interface_typeinfo(typelib, type);
2431 break;
2432 case TYPE_STRUCT:
2433 case TYPE_ENCAPSULATED_UNION:
2434 add_structure_typeinfo(typelib, type);
2435 break;
2436 case TYPE_ENUM:
2437 add_enum_typeinfo(typelib, type);
2438 break;
2439 case TYPE_UNION:
2440 add_union_typeinfo(typelib, type);
2441 break;
2442 case TYPE_COCLASS:
2443 add_coclass_typeinfo(typelib, type);
2444 break;
2445 case TYPE_BASIC:
2446 case TYPE_POINTER:
2447 case TYPE_ARRAY:
2448 break;
2449 default:
2450 error("add_entry: unhandled type 0x%x for %s\n",
2451 type_get_type(type), type->name);
2452 break;
2456 static void add_entry(msft_typelib_t *typelib, const statement_t *stmt)
2458 switch(stmt->type) {
2459 case STMT_LIBRARY:
2460 case STMT_IMPORT:
2461 case STMT_PRAGMA:
2462 case STMT_CPPQUOTE:
2463 case STMT_DECLARATION:
2464 /* not included in typelib */
2465 break;
2466 case STMT_IMPORTLIB:
2467 /* not processed here */
2468 break;
2469 case STMT_TYPEDEF:
2471 typeref_t *ref;
2472 if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry) {
2473 /* if the type is public then add the typedef, otherwise attempt
2474 * to add the aliased type */
2475 if (is_attr(ref->type->attrs, ATTR_PUBLIC))
2476 add_typedef_typeinfo(typelib, ref->type);
2477 else
2478 add_type_typeinfo(typelib, type_alias_get_aliasee_type(ref->type));
2480 break;
2482 case STMT_MODULE:
2483 add_module_typeinfo(typelib, stmt->u.type);
2484 break;
2485 case STMT_TYPE:
2486 case STMT_TYPEREF:
2488 type_t *type = stmt->u.type;
2489 add_type_typeinfo(typelib, type);
2490 break;
2495 static void set_name(msft_typelib_t *typelib)
2497 int offset;
2499 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2500 if (offset == -1) return;
2501 typelib->typelib_header.NameOffset = offset;
2502 return;
2505 static void set_version(msft_typelib_t *typelib)
2507 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2510 static void set_guid(msft_typelib_t *typelib)
2512 MSFT_GuidEntry guidentry = { {0}, -2, -1 };
2513 int offset;
2514 struct uuid *ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2516 if (ptr) guidentry.guid = *ptr;
2518 offset = ctl2_alloc_guid(typelib, &guidentry);
2519 typelib->typelib_header.posguid = offset;
2521 return;
2524 static void set_doc_string(msft_typelib_t *typelib)
2526 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2528 if (str)
2530 int offset = ctl2_alloc_string(typelib, str);
2531 if (offset != -1) typelib->typelib_header.helpstring = offset;
2535 static void set_help_file_name(msft_typelib_t *typelib)
2537 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2539 if (str)
2541 int offset = ctl2_alloc_string(typelib, str);
2542 if (offset != -1)
2544 typelib->typelib_header.helpfile = offset;
2545 typelib->typelib_header.varflags |= 0x10;
2550 static void set_help_context(msft_typelib_t *typelib)
2552 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2553 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2556 static void set_help_string_dll(msft_typelib_t *typelib)
2558 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2560 if (str)
2562 int offset = ctl2_alloc_string(typelib, str);
2563 if (offset != -1)
2565 typelib->help_string_dll_offset = offset;
2566 typelib->typelib_header.varflags |= 0x100;
2571 static void set_help_string_context(msft_typelib_t *typelib)
2573 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2574 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2577 static void set_lcid(msft_typelib_t *typelib)
2579 const expr_t *lcid_expr = get_attrp( typelib->typelib->attrs, ATTR_LIBLCID );
2580 if(lcid_expr)
2582 typelib->typelib_header.lcid = lcid_expr->cval;
2583 typelib->typelib_header.lcid2 = lcid_expr->cval;
2587 static void set_lib_flags(msft_typelib_t *typelib)
2589 const attr_t *attr;
2591 typelib->typelib_header.flags = 0;
2592 if (!typelib->typelib->attrs) return;
2593 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2595 switch(attr->type) {
2596 case ATTR_CONTROL:
2597 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2598 break;
2599 case ATTR_HIDDEN:
2600 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2601 break;
2602 case ATTR_RESTRICTED:
2603 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2604 break;
2605 default:
2606 break;
2609 return;
2612 static void ctl2_write_segment(msft_typelib_t *typelib, int segment)
2614 if (typelib->typelib_segment_data[segment])
2615 put_data(typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length);
2618 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2620 msft_typeinfo_t *typeinfo;
2622 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2623 typeinfo->typeinfo->memoffset = filesize;
2624 if (typeinfo->func_data)
2625 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2626 if (typeinfo->var_data)
2627 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2628 if (typeinfo->func_data || typeinfo->var_data)
2629 filesize += 4;
2633 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2635 if (typelib->typelib_segdir[segment].length) {
2636 typelib->typelib_segdir[segment].offset = filepos;
2637 } else {
2638 typelib->typelib_segdir[segment].offset = -1;
2641 return typelib->typelib_segdir[segment].length;
2645 static void ctl2_write_typeinfos(msft_typelib_t *typelib)
2647 msft_typeinfo_t *typeinfo;
2648 int typedata_size;
2650 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2651 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2652 typedata_size = 0;
2653 if (typeinfo->func_data)
2654 typedata_size = typeinfo->func_data[0];
2655 if (typeinfo->var_data)
2656 typedata_size += typeinfo->var_data[0];
2657 put_data(&typedata_size, sizeof(int));
2658 if (typeinfo->func_data)
2659 put_data(typeinfo->func_data + 1, typeinfo->func_data[0]);
2660 if (typeinfo->var_data)
2661 put_data(typeinfo->var_data + 1, typeinfo->var_data[0]);
2662 if (typeinfo->func_indices)
2663 put_data(typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2664 if (typeinfo->var_indices)
2665 put_data(typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2666 if (typeinfo->func_names)
2667 put_data(typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2668 if (typeinfo->var_names)
2669 put_data(typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2670 if (typeinfo->func_offsets)
2671 put_data(typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2672 if (typeinfo->var_offsets) {
2673 int add = 0, i, offset;
2674 if(typeinfo->func_data)
2675 add = typeinfo->func_data[0];
2676 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2677 offset = typeinfo->var_offsets[i];
2678 offset += add;
2679 put_data(&offset, 4);
2685 static void save_all_changes(msft_typelib_t *typelib)
2687 int filepos;
2689 chat("save_all_changes(%p)\n", typelib);
2691 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2692 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2693 filepos += typelib->typelib_header.nrtypeinfos * 4;
2695 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2696 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2697 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2698 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2699 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2700 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2701 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2702 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2703 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2704 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2705 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2706 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2707 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2709 ctl2_finalize_typeinfos(typelib, filepos);
2711 init_output_buffer();
2713 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2714 if(typelib->typelib_header.varflags & 0x100)
2715 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2717 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2718 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2719 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2720 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2721 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2722 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2723 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2724 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2725 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2726 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2727 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2728 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2729 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2730 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2731 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2733 ctl2_write_typeinfos(typelib);
2735 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2737 char typelib_id[13] = "#1";
2739 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2740 if (expr)
2741 sprintf( typelib_id, "#%d", expr->cval );
2742 add_output_to_resources( "TYPELIB", typelib_id );
2743 if (strendswith( typelib_name, "_t.res" )) /* add typelib registration */
2744 output_typelib_regscript( typelib->typelib );
2746 else flush_output_buffer( typelib_name );
2749 int create_msft_typelib(typelib_t *typelib)
2751 msft_typelib_t *msft;
2752 int failed = 0;
2753 const statement_t *stmt;
2754 const attr_t *attr;
2755 time_t cur_time;
2756 char *time_override;
2757 unsigned int version = 7 << 24 | 555; /* 7.00.0555 */
2758 static const struct uuid midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2759 static const struct uuid midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2760 static const struct uuid midl_info_guid = {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2761 char info_string[128];
2763 msft = xmalloc(sizeof(*msft));
2764 memset(msft, 0, sizeof(*msft));
2765 msft->typelib = typelib;
2767 ctl2_init_header(msft);
2768 ctl2_init_segdir(msft);
2770 msft->typelib_header.varflags |= (pointer_size == 8) ? SYS_WIN64 : SYS_WIN32;
2773 * The following two calls return an offset or -1 if out of memory. We
2774 * specifically need an offset of 0, however, so...
2776 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2777 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2779 if(failed)
2781 free(msft);
2782 return 0;
2785 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2786 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2788 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2789 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2791 set_lib_flags(msft);
2792 set_lcid(msft);
2793 set_help_file_name(msft);
2794 set_doc_string(msft);
2795 set_guid(msft);
2796 set_version(msft);
2797 set_name(msft);
2798 set_help_context(msft);
2799 set_help_string_dll(msft);
2800 set_help_string_context(msft);
2802 if (typelib->attrs) LIST_FOR_EACH_ENTRY( attr, typelib->attrs, const attr_t, entry ) {
2803 switch(attr->type) {
2804 case ATTR_CUSTOM:
2805 set_custdata_attr(msft, attr->u.pval, &msft->typelib_header.CustomDataOffset);
2806 break;
2807 default:
2808 break;
2812 /* midl adds two sets of custom data to the library: the current unix time
2813 and midl's version number */
2814 time_override = getenv( "WIDL_TIME_OVERRIDE");
2815 cur_time = time_override ? atol( time_override) : time(NULL);
2816 sprintf(info_string, "Created by WIDL version %s at %s", PACKAGE_VERSION, ctime(&cur_time));
2817 set_custdata(msft, &midl_info_guid, VT_BSTR, info_string, &msft->typelib_header.CustomDataOffset);
2818 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2819 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2821 if (typelib->stmts)
2822 LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
2823 add_entry(msft, stmt);
2825 save_all_changes(msft);
2826 free(msft);
2827 return 1;