[WIDL] Sync with Wine Staging 2.9. CORE-13362
[reactos.git] / reactos / sdk / tools / widl / write_msft.c
1 /*
2 * Typelib v2 (MSFT) generation
3 *
4 * Copyright 2004 Alastair Bridgewater
5 * 2004, 2005 Huw Davies
6 *
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.
11 *
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.
16 *
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
20 *
21 * --------------------------------------------------------------------------------------
22 * Known problems:
23 *
24 * Badly incomplete.
25 *
26 * Only works on little-endian systems.
27 *
28 */
29
30 #include "config.h"
31 #include "wine/port.h"
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <time.h>
39
40 #define NONAMELESSUNION
41
42 #include <typedefs.h>
43 #include <nls.h>
44
45 #include "widl.h"
46 #include "typelib.h"
47 #include "typelib_struct.h"
48 #include "utils.h"
49 #include "header.h"
50 #include "hash.h"
51 #include "typetree.h"
52 #include "parser.h"
53
54 #ifdef __REACTOS__
55 #define S_OK 0
56 #define S_FALSE 1
57 #define E_OUTOFMEMORY ((HRESULT)0x8007000EL)
58 #endif
59
60 enum MSFT_segment_index {
61 MSFT_SEG_TYPEINFO = 0, /* type information */
62 MSFT_SEG_IMPORTINFO, /* import information */
63 MSFT_SEG_IMPORTFILES, /* import filenames */
64 MSFT_SEG_REFERENCES, /* references (?) */
65 MSFT_SEG_GUIDHASH, /* hash table for guids? */
66 MSFT_SEG_GUID, /* guid storage */
67 MSFT_SEG_NAMEHASH, /* hash table for names */
68 MSFT_SEG_NAME, /* name storage */
69 MSFT_SEG_STRING, /* string storage */
70 MSFT_SEG_TYPEDESC, /* type descriptions */
71 MSFT_SEG_ARRAYDESC, /* array descriptions */
72 MSFT_SEG_CUSTDATA, /* custom data */
73 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
74 MSFT_SEG_UNKNOWN, /* ??? */
75 MSFT_SEG_UNKNOWN2, /* ??? */
76 MSFT_SEG_MAX /* total number of segments */
77 };
78
79 typedef struct tagMSFT_ImpFile {
80 int guid;
81 LCID lcid;
82 int version;
83 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
84 } MSFT_ImpFile;
85
86 typedef struct _msft_typelib_t
87 {
88 typelib_t *typelib;
89 MSFT_Header typelib_header;
90 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
91 unsigned char *typelib_segment_data[MSFT_SEG_MAX];
92 int typelib_segment_block_length[MSFT_SEG_MAX];
93
94 INT typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
95
96 INT *typelib_namehash_segment;
97 INT *typelib_guidhash_segment;
98
99 INT help_string_dll_offset;
100
101 struct _msft_typeinfo_t *typeinfos;
102 struct _msft_typeinfo_t *last_typeinfo;
103 } msft_typelib_t;
104
105 typedef struct _msft_typeinfo_t
106 {
107 msft_typelib_t *typelib;
108 MSFT_TypeInfoBase *typeinfo;
109
110 int typekind;
111
112 unsigned int var_data_allocated;
113 int *var_data;
114
115 unsigned int func_data_allocated;
116 int *func_data;
117
118 int vars_allocated;
119 int *var_indices;
120 int *var_names;
121 int *var_offsets;
122
123 int funcs_allocated;
124 int *func_indices;
125 int *func_names;
126 int *func_offsets;
127
128 int datawidth;
129
130 struct _msft_typeinfo_t *next_typeinfo;
131 } msft_typeinfo_t;
132
133
134
135 /*================== Internal functions ===================================*/
136
137 /****************************************************************************
138 * ctl2_init_header
139 *
140 * Initializes the type library header of a new typelib.
141 */
142 static void ctl2_init_header(
143 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
144 {
145 typelib->typelib_header.magic1 = 0x5446534d;
146 typelib->typelib_header.magic2 = 0x00010002;
147 typelib->typelib_header.posguid = -1;
148 typelib->typelib_header.lcid = 0x0409;
149 typelib->typelib_header.lcid2 = 0x0;
150 typelib->typelib_header.varflags = 0x40;
151 typelib->typelib_header.version = 0;
152 typelib->typelib_header.flags = 0;
153 typelib->typelib_header.nrtypeinfos = 0;
154 typelib->typelib_header.helpstring = -1;
155 typelib->typelib_header.helpstringcontext = 0;
156 typelib->typelib_header.helpcontext = 0;
157 typelib->typelib_header.nametablecount = 0;
158 typelib->typelib_header.nametablechars = 0;
159 typelib->typelib_header.NameOffset = -1;
160 typelib->typelib_header.helpfile = -1;
161 typelib->typelib_header.CustomDataOffset = -1;
162 typelib->typelib_header.res44 = 0x20;
163 typelib->typelib_header.res48 = 0x80;
164 typelib->typelib_header.dispatchpos = -1;
165 typelib->typelib_header.nimpinfos = 0;
166 }
167
168 /****************************************************************************
169 * ctl2_init_segdir
170 *
171 * Initializes the segment directory of a new typelib.
172 */
173 static void ctl2_init_segdir(
174 msft_typelib_t *typelib) /* [I] The typelib to initialize. */
175 {
176 int i;
177 MSFT_pSeg *segdir;
178
179 segdir = &typelib->typelib_segdir[MSFT_SEG_TYPEINFO];
180
181 for (i = 0; i < MSFT_SEG_MAX; i++) {
182 segdir[i].offset = -1;
183 segdir[i].length = 0;
184 segdir[i].res08 = -1;
185 segdir[i].res0c = 0x0f;
186 }
187 }
188
189 /****************************************************************************
190 * ctl2_hash_guid
191 *
192 * Generates a hash key from a GUID.
193 *
194 * RETURNS
195 *
196 * The hash key for the GUID.
197 */
198 static int ctl2_hash_guid(
199 REFGUID guid) /* [I] The guid to hash. */
200 {
201 int hash;
202 int i;
203
204 hash = 0;
205 for (i = 0; i < 8; i ++) {
206 hash ^= ((const short *)guid)[i];
207 }
208
209 return hash & 0x1f;
210 }
211
212 /****************************************************************************
213 * ctl2_find_guid
214 *
215 * Locates a guid in a type library.
216 *
217 * RETURNS
218 *
219 * The offset into the GUID segment of the guid, or -1 if not found.
220 */
221 static int ctl2_find_guid(
222 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
223 int hash_key, /* [I] The hash key for the guid. */
224 REFGUID guid) /* [I] The guid to find. */
225 {
226 int offset;
227 MSFT_GuidEntry *guidentry;
228
229 offset = typelib->typelib_guidhash_segment[hash_key];
230 while (offset != -1) {
231 guidentry = (MSFT_GuidEntry *)&typelib->typelib_segment_data[MSFT_SEG_GUID][offset];
232
233 if (!memcmp(guidentry, guid, sizeof(GUID))) return offset;
234
235 offset = guidentry->next_hash;
236 }
237
238 return offset;
239 }
240
241 /****************************************************************************
242 * ctl2_find_name
243 *
244 * Locates a name in a type library.
245 *
246 * RETURNS
247 *
248 * The offset into the NAME segment of the name, or -1 if not found.
249 *
250 * NOTES
251 *
252 * The name must be encoded as with ctl2_encode_name().
253 */
254 static int ctl2_find_name(
255 msft_typelib_t *typelib, /* [I] The typelib to operate against. */
256 char *name) /* [I] The encoded name to find. */
257 {
258 int offset;
259 int *namestruct;
260
261 offset = typelib->typelib_namehash_segment[name[2] & 0x7f];
262 while (offset != -1) {
263 namestruct = (int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][offset];
264
265 if (!((namestruct[2] ^ *((int *)name)) & 0xffff00ff)) {
266 /* hash codes and lengths match, final test */
267 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
268 }
269
270 /* move to next item in hash bucket */
271 offset = namestruct[1];
272 }
273
274 return offset;
275 }
276
277 /****************************************************************************
278 * ctl2_encode_name
279 *
280 * Encodes a name string to a form suitable for storing into a type library
281 * or comparing to a name stored in a type library.
282 *
283 * RETURNS
284 *
285 * The length of the encoded name, including padding and length+hash fields.
286 *
287 * NOTES
288 *
289 * Will throw an exception if name or result are NULL. Is not multithread
290 * safe in the slightest.
291 */
292 static int ctl2_encode_name(
293 msft_typelib_t *typelib, /* [I] The typelib to operate against (used for LCID only). */
294 const char *name, /* [I] The name string to encode. */
295 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
296 {
297 int length;
298 static char converted_name[0x104];
299 int offset;
300 int value;
301
302 length = strlen(name);
303 memcpy(converted_name + 4, name, length);
304
305 converted_name[length + 4] = 0;
306
307
308 value = lhash_val_of_name_sys(typelib->typelib_header.varflags & 0x0f, typelib->typelib_header.lcid, converted_name + 4);
309
310 #ifdef WORDS_BIGENDIAN
311 converted_name[3] = length & 0xff;
312 converted_name[2] = 0x00;
313 converted_name[1] = value;
314 converted_name[0] = value >> 8;
315 #else
316 converted_name[0] = length & 0xff;
317 converted_name[1] = 0x00;
318 converted_name[2] = value;
319 converted_name[3] = value >> 8;
320 #endif
321
322 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
323
324 *result = converted_name;
325
326 return (length + 7) & ~3;
327 }
328
329 /****************************************************************************
330 * ctl2_encode_string
331 *
332 * Encodes a string to a form suitable for storing into a type library or
333 * comparing to a string stored in a type library.
334 *
335 * RETURNS
336 *
337 * The length of the encoded string, including padding and length fields.
338 *
339 * NOTES
340 *
341 * Will throw an exception if string or result are NULL. Is not multithread
342 * safe in the slightest.
343 */
344 static int ctl2_encode_string(
345 const char *string, /* [I] The string to encode. */
346 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
347 {
348 int length;
349 static char converted_string[0x104];
350 int offset;
351
352 length = strlen(string);
353 memcpy(converted_string + 2, string, length);
354
355 #ifdef WORDS_BIGENDIAN
356 converted_string[1] = length & 0xff;
357 converted_string[0] = (length >> 8) & 0xff;
358 #else
359 converted_string[0] = length & 0xff;
360 converted_string[1] = (length >> 8) & 0xff;
361 #endif
362
363 if(length < 3) { /* strings of this length are padded with up to 8 bytes incl the 2 byte length */
364 for(offset = 0; offset < 4; offset++)
365 converted_string[length + offset + 2] = 0x57;
366 length += 4;
367 }
368 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
369
370 *result = converted_string;
371
372 return (length + 5) & ~3;
373 }
374
375 /****************************************************************************
376 * ctl2_alloc_segment
377 *
378 * Allocates memory from a segment in a type library.
379 *
380 * RETURNS
381 *
382 * Success: The offset within the segment of the new data area.
383 *
384 * BUGS
385 *
386 * Does not (yet) handle the case where the allocated segment memory needs to grow.
387 */
388 static int ctl2_alloc_segment(
389 msft_typelib_t *typelib, /* [I] The type library in which to allocate. */
390 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
391 int size, /* [I] The amount to allocate. */
392 int block_size) /* [I] Initial allocation block size, or 0 for default. */
393 {
394 int offset;
395
396 if(!typelib->typelib_segment_data[segment]) {
397 if (!block_size) block_size = 0x2000;
398
399 typelib->typelib_segment_block_length[segment] = block_size;
400 typelib->typelib_segment_data[segment] = xmalloc(block_size);
401 if (!typelib->typelib_segment_data[segment]) return -1;
402 memset(typelib->typelib_segment_data[segment], 0x57, block_size);
403 }
404
405 while ((typelib->typelib_segdir[segment].length + size) > typelib->typelib_segment_block_length[segment]) {
406 unsigned char *block;
407
408 block_size = typelib->typelib_segment_block_length[segment];
409 block = xrealloc(typelib->typelib_segment_data[segment], block_size << 1);
410
411 if (segment == MSFT_SEG_TYPEINFO) {
412 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
413 msft_typeinfo_t *typeinfo;
414
415 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
416 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - typelib->typelib_segment_data[segment]];
417 }
418 }
419
420 memset(block + block_size, 0x57, block_size);
421 typelib->typelib_segment_block_length[segment] = block_size << 1;
422 typelib->typelib_segment_data[segment] = block;
423 }
424
425 offset = typelib->typelib_segdir[segment].length;
426 typelib->typelib_segdir[segment].length += size;
427
428 return offset;
429 }
430
431 /****************************************************************************
432 * ctl2_alloc_typeinfo
433 *
434 * Allocates and initializes a typeinfo structure in a type library.
435 *
436 * RETURNS
437 *
438 * Success: The offset of the new typeinfo.
439 * Failure: -1 (this is invariably an out of memory condition).
440 */
441 static int ctl2_alloc_typeinfo(
442 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
443 int nameoffset) /* [I] The offset of the name for this typeinfo. */
444 {
445 int offset;
446 MSFT_TypeInfoBase *typeinfo;
447
448 offset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
449
450 typelib->typelib_typeinfo_offsets[typelib->typelib_header.nrtypeinfos++] = offset;
451
452 typeinfo = (void *)(typelib->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
453
454 typeinfo->typekind = (typelib->typelib_header.nrtypeinfos - 1) << 16;
455 typeinfo->memoffset = -1; /* should be EOF if no elements */
456 typeinfo->res2 = 0;
457 typeinfo->res3 = -1;
458 typeinfo->res4 = 3;
459 typeinfo->res5 = 0;
460 typeinfo->cElement = 0;
461 typeinfo->res7 = 0;
462 typeinfo->res8 = 0;
463 typeinfo->res9 = 0;
464 typeinfo->resA = 0;
465 typeinfo->posguid = -1;
466 typeinfo->flags = 0;
467 typeinfo->NameOffset = nameoffset;
468 typeinfo->version = 0;
469 typeinfo->docstringoffs = -1;
470 typeinfo->helpstringcontext = 0;
471 typeinfo->helpcontext = 0;
472 typeinfo->oCustData = -1;
473 typeinfo->cbSizeVft = 0;
474 typeinfo->cImplTypes = 0;
475 typeinfo->size = 0;
476 typeinfo->datatype1 = -1;
477 typeinfo->datatype2 = 0;
478 typeinfo->res18 = 0;
479 typeinfo->res19 = -1;
480
481 return offset;
482 }
483
484 /****************************************************************************
485 * ctl2_alloc_guid
486 *
487 * Allocates and initializes a GUID structure in a type library. Also updates
488 * the GUID hash table as needed.
489 *
490 * RETURNS
491 *
492 * Success: The offset of the new GUID.
493 */
494 static int ctl2_alloc_guid(
495 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
496 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
497 {
498 int offset;
499 MSFT_GuidEntry *guid_space;
500 int hash_key;
501
502 chat("adding uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
503 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
504 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
505 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
506
507 hash_key = ctl2_hash_guid(&guid->guid);
508
509 offset = ctl2_find_guid(typelib, hash_key, &guid->guid);
510 if (offset != -1)
511 {
512 if (is_warning_enabled(2368))
513 warning("duplicate uuid {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
514 guid->guid.Data1, guid->guid.Data2, guid->guid.Data3,
515 guid->guid.Data4[0], guid->guid.Data4[1], guid->guid.Data4[2], guid->guid.Data4[3],
516 guid->guid.Data4[4], guid->guid.Data4[5], guid->guid.Data4[6], guid->guid.Data4[7]);
517 return -1;
518 }
519
520 offset = ctl2_alloc_segment(typelib, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
521
522 guid_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_GUID] + offset);
523 *guid_space = *guid;
524
525 guid_space->next_hash = typelib->typelib_guidhash_segment[hash_key];
526 typelib->typelib_guidhash_segment[hash_key] = offset;
527
528 return offset;
529 }
530
531 /****************************************************************************
532 * ctl2_alloc_name
533 *
534 * Allocates and initializes a name within a type library. Also updates the
535 * name hash table as needed.
536 *
537 * RETURNS
538 *
539 * Success: The offset within the segment of the new name.
540 * Failure: -1 (this is invariably an out of memory condition).
541 */
542 static int ctl2_alloc_name(
543 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
544 const char *name) /* [I] The name to store. */
545 {
546 int length;
547 int offset;
548 MSFT_NameIntro *name_space;
549 char *encoded_name;
550
551 length = ctl2_encode_name(typelib, name, &encoded_name);
552
553 offset = ctl2_find_name(typelib, encoded_name);
554 if (offset != -1) return offset;
555
556 offset = ctl2_alloc_segment(typelib, MSFT_SEG_NAME, length + 8, 0);
557
558 name_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_NAME] + offset);
559 name_space->hreftype = -1;
560 name_space->next_hash = -1;
561 memcpy(&name_space->namelen, encoded_name, length);
562
563 if (typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
564 name_space->next_hash = typelib->typelib_namehash_segment[encoded_name[2] & 0x7f];
565
566 typelib->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
567
568 typelib->typelib_header.nametablecount += 1;
569 typelib->typelib_header.nametablechars += *encoded_name;
570
571 return offset;
572 }
573
574 /****************************************************************************
575 * ctl2_alloc_string
576 *
577 * Allocates and initializes a string in a type library.
578 *
579 * RETURNS
580 *
581 * Success: The offset within the segment of the new string.
582 * Failure: -1 (this is invariably an out of memory condition).
583 */
584 static int ctl2_alloc_string(
585 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
586 const char *string) /* [I] The string to store. */
587 {
588 int length;
589 int offset;
590 unsigned char *string_space;
591 char *encoded_string;
592
593 length = ctl2_encode_string(string, &encoded_string);
594
595 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_STRING].length;
596 offset += (((typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
597 typelib->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
598 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
599 }
600
601 offset = ctl2_alloc_segment(typelib, MSFT_SEG_STRING, length, 0);
602
603 string_space = typelib->typelib_segment_data[MSFT_SEG_STRING] + offset;
604 memcpy(string_space, encoded_string, length);
605
606 return offset;
607 }
608
609 /****************************************************************************
610 * alloc_msft_importinfo
611 *
612 * Allocates and initializes an import information structure in a type library.
613 *
614 * RETURNS
615 *
616 * Success: The offset of the new importinfo.
617 * Failure: -1 (this is invariably an out of memory condition).
618 */
619 static int alloc_msft_importinfo(
620 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
621 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
622 {
623 int offset;
624 MSFT_ImpInfo *impinfo_space;
625
626 for (offset = 0;
627 offset < typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
628 offset += sizeof(MSFT_ImpInfo)) {
629 if (!memcmp(&(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][offset]),
630 impinfo, sizeof(MSFT_ImpInfo))) {
631 return offset;
632 }
633 }
634
635 impinfo->flags |= typelib->typelib_header.nimpinfos++;
636
637 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
638
639 impinfo_space = (void *)(typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
640 *impinfo_space = *impinfo;
641
642 return offset;
643 }
644
645 /****************************************************************************
646 * alloc_importfile
647 *
648 * Allocates and initializes an import file definition in a type library.
649 *
650 * RETURNS
651 *
652 * Success: The offset of the new importinfo.
653 * Failure: -1 (this is invariably an out of memory condition).
654 */
655 static int alloc_importfile(
656 msft_typelib_t *typelib, /* [I] The type library to allocate in. */
657 int guidoffset, /* [I] The offset to the GUID for the imported library. */
658 int major_version, /* [I] The major version number of the imported library. */
659 int minor_version, /* [I] The minor version number of the imported library. */
660 const char *filename) /* [I] The filename of the imported library. */
661 {
662 int length;
663 int offset;
664 MSFT_ImpFile *importfile;
665 char *encoded_string;
666
667 length = ctl2_encode_string(filename, &encoded_string);
668
669 encoded_string[0] <<= 2;
670 encoded_string[0] |= 1;
671
672 for (offset = 0; offset < typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
673 offset += (((typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
674 typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 0xc) {
675 if (!memcmp(encoded_string, typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
676 }
677
678 offset = ctl2_alloc_segment(typelib, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
679
680 importfile = (MSFT_ImpFile *)&typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
681 importfile->guid = guidoffset;
682 importfile->lcid = typelib->typelib_header.lcid2;
683 importfile->version = major_version | (minor_version << 16);
684 memcpy(&importfile->filename, encoded_string, length);
685
686 return offset;
687 }
688
689 static void alloc_importinfo(msft_typelib_t *typelib, importinfo_t *importinfo)
690 {
691 importlib_t *importlib = importinfo->importlib;
692
693 chat("alloc_importinfo: %s\n", importinfo->name);
694
695 if(!importlib->allocated) {
696 MSFT_GuidEntry guid;
697 int guid_idx;
698
699 chat("allocating importlib %s\n", importlib->name);
700
701 importlib->allocated = -1;
702
703 memcpy(&guid.guid, &importlib->guid, sizeof(GUID));
704 guid.hreftype = 2;
705
706 guid_idx = ctl2_alloc_guid(typelib, &guid);
707
708 alloc_importfile(typelib, guid_idx, importlib->version&0xffff,
709 importlib->version>>16, importlib->name);
710 }
711
712 if(importinfo->offset == -1 || !(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID)) {
713 MSFT_ImpInfo impinfo;
714
715 impinfo.flags = importinfo->flags;
716 impinfo.oImpFile = 0;
717
718 if(importinfo->flags & MSFT_IMPINFO_OFFSET_IS_GUID) {
719 MSFT_GuidEntry guid;
720
721 guid.hreftype = 0;
722 memcpy(&guid.guid, &importinfo->guid, sizeof(GUID));
723
724 impinfo.oGuid = ctl2_alloc_guid(typelib, &guid);
725
726 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
727
728 typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo.oGuid+sizeof(GUID)]
729 = importinfo->offset+1;
730
731 if(!strcmp(importinfo->name, "IDispatch"))
732 typelib->typelib_header.dispatchpos = importinfo->offset+1;
733 }else {
734 impinfo.oGuid = importinfo->id;
735 importinfo->offset = alloc_msft_importinfo(typelib, &impinfo);
736 }
737 }
738 }
739
740 static importinfo_t *find_importinfo(msft_typelib_t *typelib, const char *name)
741 {
742 importlib_t *importlib;
743 int i;
744
745 chat("search importlib %s\n", name);
746
747 if(!name)
748 return NULL;
749
750 LIST_FOR_EACH_ENTRY( importlib, &typelib->typelib->importlibs, importlib_t, entry )
751 {
752 for(i=0; i < importlib->ntypeinfos; i++) {
753 if(!strcmp(name, importlib->importinfos[i].name)) {
754 chat("Found %s in importlib.\n", name);
755 return importlib->importinfos+i;
756 }
757 }
758 }
759
760 return NULL;
761 }
762
763 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure);
764 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface);
765 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration);
766 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion);
767 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls);
768 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface);
769
770
771 /****************************************************************************
772 * encode_type
773 *
774 * Encodes a type, storing information in the TYPEDESC and ARRAYDESC
775 * segments as needed.
776 *
777 * RETURNS
778 *
779 * Success: 0.
780 * Failure: -1.
781 */
782 static int encode_type(
783 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
784 int vt, /* [I] vt to encode */
785 type_t *type, /* [I] type */
786 int *encoded_type, /* [O] The encoded type description. */
787 int *width, /* [O] The width of the type, or NULL. */
788 int *alignment, /* [O] The alignment of the type, or NULL. */
789 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
790 {
791 int default_type;
792 int scratch;
793 int typeoffset;
794 int *typedata;
795 int target_type;
796 int child_size = 0;
797
798 chat("encode_type vt %d type %p\n", vt, type);
799
800 default_type = 0x80000000 | (vt << 16) | vt;
801 if (!width) width = &scratch;
802 if (!alignment) alignment = &scratch;
803 if (!decoded_size) decoded_size = &scratch;
804
805 *decoded_size = 0;
806
807 switch (vt) {
808 case VT_I1:
809 case VT_UI1:
810 *encoded_type = default_type;
811 *width = 1;
812 *alignment = 1;
813 break;
814
815 case VT_INT:
816 *encoded_type = 0x80000000 | (VT_I4 << 16) | VT_INT;
817 if ((typelib->typelib_header.varflags & 0x0f) == SYS_WIN16) {
818 *width = 2;
819 *alignment = 2;
820 } else {
821 *width = 4;
822 *alignment = 4;
823 }
824 break;
825
826 case VT_UINT:
827 *encoded_type = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
828 if ((typelib->typelib_header.varflags & 0x0f) == SYS_WIN16) {
829 *width = 2;
830 *alignment = 2;
831 } else {
832 *width = 4;
833 *alignment = 4;
834 }
835 break;
836
837 case VT_UI2:
838 case VT_I2:
839 case VT_BOOL:
840 *encoded_type = default_type;
841 *width = 2;
842 *alignment = 2;
843 break;
844
845 case VT_I4:
846 case VT_UI4:
847 case VT_R4:
848 case VT_ERROR:
849 case VT_HRESULT:
850 *encoded_type = default_type;
851 *width = 4;
852 *alignment = 4;
853 break;
854
855 case VT_R8:
856 case VT_I8:
857 case VT_UI8:
858 *encoded_type = default_type;
859 *width = 8;
860 *alignment = 8;
861 break;
862
863 case VT_CY:
864 case VT_DATE:
865 *encoded_type = default_type;
866 *width = 8;
867 *alignment = 8;
868 break;
869
870 case VT_DECIMAL:
871 *encoded_type = default_type;
872 *width = 16;
873 *alignment = 8;
874 break;
875
876 case VT_VOID:
877 *encoded_type = 0x80000000 | (VT_EMPTY << 16) | vt;
878 *width = 0;
879 *alignment = 1;
880 break;
881
882 case VT_UNKNOWN:
883 case VT_DISPATCH:
884 case VT_BSTR:
885 *encoded_type = default_type;
886 *width = pointer_size;
887 *alignment = 4;
888 break;
889
890 case VT_VARIANT:
891 *encoded_type = default_type;
892 *width = 8 + 2 * pointer_size;
893 *alignment = 8;
894 break;
895
896 case VT_LPSTR:
897 case VT_LPWSTR:
898 *encoded_type = 0xfffe0000 | vt;
899 *width = pointer_size;
900 *alignment = 4;
901 break;
902
903 case VT_PTR:
904 {
905 int next_vt;
906 for(next_vt = 0; is_ptr(type); type = type_pointer_get_ref(type)) {
907 next_vt = get_type_vt(type_pointer_get_ref(type));
908 if (next_vt != 0)
909 break;
910 }
911 /* if no type found then it must be void */
912 if (next_vt == 0)
913 next_vt = VT_VOID;
914
915 encode_type(typelib, next_vt, type_pointer_get_ref(type),
916 &target_type, NULL, NULL, &child_size);
917 /* these types already have an implicit pointer, so we don't need to
918 * add another */
919 if(next_vt == VT_DISPATCH || next_vt == VT_UNKNOWN) {
920 chat("encode_type: skipping ptr\n");
921 *encoded_type = target_type;
922 *width = pointer_size;
923 *alignment = 4;
924 *decoded_size = child_size;
925 break;
926 }
927
928 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
929 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
930 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
931 }
932
933 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
934 int mix_field;
935
936 if (target_type & 0x80000000) {
937 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
938 } else {
939 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
940 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
941 }
942
943 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
944 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
945
946 typedata[0] = (mix_field << 16) | VT_PTR;
947 typedata[1] = target_type;
948 }
949
950 *encoded_type = typeoffset;
951
952 *width = pointer_size;
953 *alignment = 4;
954 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
955 break;
956 }
957
958 case VT_SAFEARRAY:
959 {
960 type_t *element_type = type_alias_get_aliasee(type_array_get_element(type));
961 int next_vt = get_type_vt(element_type);
962
963 encode_type(typelib, next_vt, type_alias_get_aliasee(type_array_get_element(type)), &target_type, NULL, NULL, &child_size);
964
965 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
966 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
967 if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
968 }
969
970 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
971 int mix_field;
972
973 if (target_type & 0x80000000) {
974 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
975 } else {
976 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
977 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
978 }
979
980 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
981 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
982
983 typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
984 typedata[1] = target_type;
985 }
986
987 *encoded_type = typeoffset;
988
989 *width = pointer_size;
990 *alignment = 4;
991 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
992 break;
993 }
994
995 case VT_USERDEFINED:
996 {
997 int typeinfo_offset;
998
999 /* typedef'd types without public attribute aren't included in the typelib */
1000 while (type->typelib_idx < 0 && type_is_alias(type) && !is_attr(type->attrs, ATTR_PUBLIC))
1001 type = type_alias_get_aliasee(type);
1002
1003 chat("encode_type: VT_USERDEFINED - type %p name = %s real type %d idx %d\n", type,
1004 type->name, type_get_type(type), type->typelib_idx);
1005
1006 if(type->typelib_idx == -1) {
1007 chat("encode_type: trying to ref not added type\n");
1008 switch (type_get_type(type)) {
1009 case TYPE_STRUCT:
1010 add_structure_typeinfo(typelib, type);
1011 break;
1012 case TYPE_INTERFACE:
1013 add_interface_typeinfo(typelib, type);
1014 break;
1015 case TYPE_ENUM:
1016 add_enum_typeinfo(typelib, type);
1017 break;
1018 case TYPE_UNION:
1019 add_union_typeinfo(typelib, type);
1020 break;
1021 case TYPE_COCLASS:
1022 add_coclass_typeinfo(typelib, type);
1023 break;
1024 default:
1025 error("encode_type: VT_USERDEFINED - unhandled type %d\n",
1026 type_get_type(type));
1027 }
1028 }
1029
1030 typeinfo_offset = typelib->typelib_typeinfo_offsets[type->typelib_idx];
1031 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1032 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1033 if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == typeinfo_offset)) break;
1034 }
1035
1036 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1037 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1038 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1039
1040 typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1041 typedata[1] = typeinfo_offset;
1042 }
1043
1044 *encoded_type = typeoffset;
1045 *width = 0;
1046 *alignment = 1;
1047 break;
1048 }
1049
1050 default:
1051 error("encode_type: unrecognized type %d.\n", vt);
1052 *encoded_type = default_type;
1053 *width = 0;
1054 *alignment = 1;
1055 break;
1056 }
1057
1058 return 0;
1059 }
1060
1061 static void dump_type(type_t *t)
1062 {
1063 chat("dump_type: %p name %s type %d attrs %p\n", t, t->name, type_get_type(t), t->attrs);
1064 }
1065
1066 static int encode_var(
1067 msft_typelib_t *typelib, /* [I] The type library in which to encode the TYPEDESC. */
1068 type_t *type, /* [I] The type description to encode. */
1069 var_t *var, /* [I] The var to encode. */
1070 int *encoded_type, /* [O] The encoded type description. */
1071 int *width, /* [O] The width of the type, or NULL. */
1072 int *alignment, /* [O] The alignment of the type, or NULL. */
1073 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1074 {
1075 int typeoffset;
1076 int *typedata;
1077 int target_type;
1078 int child_size;
1079 int vt;
1080 int scratch;
1081
1082 if (!width) width = &scratch;
1083 if (!alignment) alignment = &scratch;
1084 if (!decoded_size) decoded_size = &scratch;
1085 *decoded_size = 0;
1086
1087 chat("encode_var: var %p type %p type->name %s\n",
1088 var, type, type->name ? type->name : "NULL");
1089
1090 if (is_array(type) && !type_array_is_decl_as_ptr(type)) {
1091 int num_dims, elements = 1, arrayoffset;
1092 type_t *atype;
1093 int *arraydata;
1094
1095 num_dims = 0;
1096 for (atype = type;
1097 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1098 atype = type_array_get_element(atype))
1099 ++num_dims;
1100
1101 chat("array with %d dimensions\n", num_dims);
1102 encode_var(typelib, atype, var, &target_type, width, alignment, NULL);
1103 arrayoffset = ctl2_alloc_segment(typelib, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1104 arraydata = (void *)&typelib->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1105
1106 arraydata[0] = target_type;
1107 arraydata[1] = num_dims;
1108 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1109
1110 arraydata += 2;
1111 for (atype = type;
1112 is_array(atype) && !type_array_is_decl_as_ptr(atype);
1113 atype = type_array_get_element(atype))
1114 {
1115 arraydata[0] = type_array_get_dim(atype);
1116 arraydata[1] = 0;
1117 arraydata += 2;
1118 elements *= type_array_get_dim(atype);
1119 }
1120
1121 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1122 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1123
1124 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1125 typedata[1] = arrayoffset;
1126
1127 *encoded_type = typeoffset;
1128 *width = *width * elements;
1129 *decoded_size = 20 /*sizeof(ARRAYDESC)*/ + (num_dims - 1) * 8 /*sizeof(SAFEARRAYBOUND)*/;
1130 return 0;
1131 }
1132
1133 vt = get_type_vt(type);
1134 if (vt == VT_PTR) {
1135 type_t *ref = is_ptr(type) ?
1136 type_pointer_get_ref(type) : type_array_get_element(type);
1137 int skip_ptr = encode_var(typelib, ref, var,
1138 &target_type, NULL, NULL, &child_size);
1139
1140 if(skip_ptr == 2) {
1141 chat("encode_var: skipping ptr\n");
1142 *encoded_type = target_type;
1143 *decoded_size = child_size;
1144 *width = pointer_size;
1145 *alignment = 4;
1146 return 0;
1147 }
1148
1149 for (typeoffset = 0; typeoffset < typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1150 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1151 if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1152 }
1153
1154 if (typeoffset == typelib->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1155 int mix_field;
1156
1157 if (target_type & 0x80000000) {
1158 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1159 } else if (is_array(ref)) {
1160 type_t *element_type = type_alias_get_aliasee(type_array_get_element(ref));
1161 mix_field = get_type_vt(element_type) | VT_ARRAY | VT_BYREF;
1162 } else {
1163 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1164 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1165 }
1166
1167 typeoffset = ctl2_alloc_segment(typelib, MSFT_SEG_TYPEDESC, 8, 0);
1168 typedata = (void *)&typelib->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1169
1170 typedata[0] = (mix_field << 16) | VT_PTR;
1171 typedata[1] = target_type;
1172 }
1173
1174 *encoded_type = typeoffset;
1175
1176 *width = pointer_size;
1177 *alignment = 4;
1178 *decoded_size = 8 /*sizeof(TYPEDESC)*/ + child_size;
1179 return 0;
1180 }
1181
1182 dump_type(type);
1183
1184 encode_type(typelib, vt, type, encoded_type, width, alignment, decoded_size);
1185 /* these types already have an implicit pointer, so we don't need to
1186 * add another */
1187 if(vt == VT_DISPATCH || vt == VT_UNKNOWN) return 2;
1188 return 0;
1189 }
1190
1191 static unsigned int get_ulong_val(unsigned int val, int vt)
1192 {
1193 switch(vt) {
1194 case VT_I2:
1195 case VT_BOOL:
1196 case VT_UI2:
1197 return val & 0xffff;
1198 case VT_I1:
1199 case VT_UI1:
1200 return val & 0xff;
1201 }
1202
1203 return val;
1204 }
1205
1206 static void write_value(msft_typelib_t* typelib, int *out, int vt, const void *value)
1207 {
1208 switch(vt) {
1209 case VT_I2:
1210 case VT_I4:
1211 case VT_R4:
1212 case VT_BOOL:
1213 case VT_I1:
1214 case VT_UI1:
1215 case VT_UI2:
1216 case VT_UI4:
1217 case VT_INT:
1218 case VT_UINT:
1219 case VT_HRESULT:
1220 case VT_PTR:
1221 case VT_UNKNOWN:
1222 case VT_DISPATCH:
1223 {
1224 const unsigned int lv = get_ulong_val(*(const unsigned int *)value, vt);
1225 if((lv & 0x3ffffff) == lv) {
1226 *out = 0x80000000;
1227 *out |= vt << 26;
1228 *out |= lv;
1229 } else {
1230 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, 8, 0);
1231 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1232 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], value, 4);
1233 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6]) = 0x5757;
1234 *out = offset;
1235 }
1236 return;
1237 }
1238 case VT_BSTR:
1239 {
1240 const char *s = (const char *) value;
1241 int len = strlen(s), seg_len = (len + 6 + 3) & ~0x3;
1242 int offset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATA, seg_len, 0);
1243 *((unsigned short *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = vt;
1244 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2], &len, sizeof(len));
1245 memcpy(&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], value, len);
1246 len += 6;
1247 while(len < seg_len) {
1248 *((char *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+len]) = 0x57;
1249 len++;
1250 }
1251 *out = offset;
1252 return;
1253 }
1254
1255 default:
1256 warning("can't write value of type %d yet\n", vt);
1257 }
1258 return;
1259 }
1260
1261 static HRESULT set_custdata(msft_typelib_t *typelib, REFGUID guid,
1262 int vt, const void *value, int *offset)
1263 {
1264 MSFT_GuidEntry guidentry;
1265 int guidoffset;
1266 int custoffset;
1267 int *custdata;
1268 int data_out;
1269
1270 guidentry.guid = *guid;
1271
1272 guidentry.hreftype = -1;
1273 guidentry.next_hash = -1;
1274
1275 guidoffset = ctl2_alloc_guid(typelib, &guidentry);
1276 write_value(typelib, &data_out, vt, value);
1277
1278 custoffset = ctl2_alloc_segment(typelib, MSFT_SEG_CUSTDATAGUID, 12, 0);
1279
1280 custdata = (int *)&typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1281 custdata[0] = guidoffset;
1282 custdata[1] = data_out;
1283 custdata[2] = *offset;
1284 *offset = custoffset;
1285
1286 return S_OK;
1287 }
1288
1289 /* It's possible to have a default value for pointer arguments too.
1290 In this case default value has a referenced type, e.g.
1291 'LONG*' argument gets VT_I4, 'DOUBLE*' - VT_R8. IUnknown* and IDispatch*
1292 are recognised too and stored as VT_UNKNOWN and VT_DISPATCH.
1293 But IUnknown/IDispatch arguments can only have default value of 0
1294 (or expression that resolves to zero) while other pointers can have
1295 any default value. */
1296 static int get_defaultvalue_vt(type_t *type)
1297 {
1298 int vt;
1299 if (type_get_type(type) == TYPE_ENUM)
1300 vt = VT_I4;
1301 else
1302 {
1303 vt = get_type_vt(type);
1304 if (vt == VT_PTR && is_ptr(type)) {
1305 vt = get_type_vt(type_pointer_get_ref(type));
1306 /* The only acceptable value for pointers to non-basic types
1307 is NULL, it's stored as VT_I4 for both 32 and 64 bit typelibs. */
1308 if (vt == VT_USERDEFINED)
1309 vt = VT_I4;
1310 }
1311 }
1312
1313 return vt;
1314 }
1315
1316 static HRESULT add_func_desc(msft_typeinfo_t* typeinfo, var_t *func, int index)
1317 {
1318 int offset, name_offset;
1319 int *typedata, typedata_size;
1320 int i, id, next_idx;
1321 int decoded_size, extra_attr = 0;
1322 int num_params = 0, num_optional = 0, num_defaults = 0;
1323 var_t *arg;
1324 unsigned char *namedata;
1325 const attr_t *attr;
1326 unsigned int funcflags = 0, callconv = 4 /* CC_STDCALL */;
1327 unsigned int funckind, invokekind = 1 /* INVOKE_FUNC */;
1328 int help_context = 0, help_string_context = 0, help_string_offset = -1;
1329 int entry = -1, entry_is_ord = 0;
1330 int lcid_retval_count = 0;
1331
1332 chat("add_func_desc(%p,%d)\n", typeinfo, index);
1333
1334 id = ((0x6000 | (typeinfo->typeinfo->datatype2 & 0xffff)) << 16) | index;
1335
1336 switch(typeinfo->typekind) {
1337 case TKIND_DISPATCH:
1338 funckind = 0x4; /* FUNC_DISPATCH */
1339 break;
1340 case TKIND_MODULE:
1341 funckind = 0x3; /* FUNC_STATIC */
1342 break;
1343 default:
1344 funckind = 0x1; /* FUNC_PUREVIRTUAL */
1345 break;
1346 }
1347
1348 if (is_local( func->attrs )) {
1349 chat("add_func_desc: skipping local function\n");
1350 return S_FALSE;
1351 }
1352
1353 if (type_get_function_args(func->type))
1354 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1355 {
1356 num_params++;
1357 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1358 if(attr->type == ATTR_DEFAULTVALUE)
1359 num_defaults++;
1360 else if(attr->type == ATTR_OPTIONAL)
1361 num_optional++;
1362 }
1363 }
1364
1365 chat("add_func_desc: num of params %d\n", num_params);
1366
1367 name_offset = ctl2_alloc_name(typeinfo->typelib, func->name);
1368
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_DEFAULTBIND:
1376 funcflags |= 0x20; /* FUNCFLAG_FDEFAULTBIND */
1377 break;
1378 case ATTR_DEFAULTCOLLELEM:
1379 funcflags |= 0x100; /* FUNCFLAG_FDEFAULTCOLLELEM */
1380 break;
1381 case ATTR_DISPLAYBIND:
1382 funcflags |= 0x10; /* FUNCFLAG_FDISPLAYBIND */
1383 break;
1384 case ATTR_ENTRY:
1385 extra_attr = max(extra_attr, 3);
1386 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1387 entry = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1388 else {
1389 entry = expr->cval;
1390 entry_is_ord = 1;
1391 }
1392 break;
1393 case ATTR_HELPCONTEXT:
1394 extra_attr = max(extra_attr, 1);
1395 help_context = expr->u.lval;
1396 break;
1397 case ATTR_HELPSTRING:
1398 extra_attr = max(extra_attr, 2);
1399 help_string_offset = ctl2_alloc_string(typeinfo->typelib, attr->u.pval);
1400 break;
1401 case ATTR_HELPSTRINGCONTEXT:
1402 extra_attr = max(extra_attr, 6);
1403 help_string_context = expr->u.lval;
1404 break;
1405 case ATTR_HIDDEN:
1406 funcflags |= 0x40; /* FUNCFLAG_FHIDDEN */
1407 break;
1408 case ATTR_ID:
1409 id = expr->cval;
1410 break;
1411 case ATTR_IMMEDIATEBIND:
1412 funcflags |= 0x1000; /* FUNCFLAG_FIMMEDIATEBIND */
1413 break;
1414 case ATTR_NONBROWSABLE:
1415 funcflags |= 0x400; /* FUNCFLAG_FNONBROWSABLE */
1416 break;
1417 case ATTR_OUT:
1418 break;
1419 case ATTR_PROPGET:
1420 invokekind = 0x2; /* INVOKE_PROPERTYGET */
1421 break;
1422 case ATTR_PROPPUT:
1423 invokekind = 0x4; /* INVOKE_PROPERTYPUT */
1424 break;
1425 case ATTR_PROPPUTREF:
1426 invokekind = 0x8; /* INVOKE_PROPERTYPUTREF */
1427 break;
1428 /* FIXME: FUNCFLAG_FREPLACEABLE */
1429 case ATTR_REQUESTEDIT:
1430 funcflags |= 0x8; /* FUNCFLAG_FREQUESTEDIT */
1431 break;
1432 case ATTR_RESTRICTED:
1433 funcflags |= 0x1; /* FUNCFLAG_FRESTRICTED */
1434 break;
1435 case ATTR_SOURCE:
1436 funcflags |= 0x2; /* FUNCFLAG_FSOURCE */
1437 break;
1438 case ATTR_UIDEFAULT:
1439 funcflags |= 0x200; /* FUNCFLAG_FUIDEFAULT */
1440 break;
1441 case ATTR_USESGETLASTERROR:
1442 funcflags |= 0x80; /* FUNCFLAG_FUSESGETLASTERROR */
1443 break;
1444 case ATTR_VARARG:
1445 if (num_optional || num_defaults)
1446 warning("add_func_desc: ignoring vararg in function with optional or defaultvalue params\n");
1447 else
1448 num_optional = -1;
1449 break;
1450 default:
1451 break;
1452 }
1453 }
1454
1455 /* allocate type data space for us */
1456 typedata_size = 0x18 + extra_attr * sizeof(int) + (num_params * (num_defaults ? 16 : 12));
1457
1458 if (!typeinfo->func_data) {
1459 typeinfo->func_data = xmalloc(0x100);
1460 typeinfo->func_data_allocated = 0x100;
1461 typeinfo->func_data[0] = 0;
1462 }
1463
1464 if(typeinfo->func_data[0] + typedata_size + sizeof(int) > typeinfo->func_data_allocated) {
1465 typeinfo->func_data_allocated = max(typeinfo->func_data_allocated * 2,
1466 typeinfo->func_data[0] + typedata_size + sizeof(int));
1467 typeinfo->func_data = xrealloc(typeinfo->func_data, typeinfo->func_data_allocated);
1468 }
1469
1470 offset = typeinfo->func_data[0];
1471 typeinfo->func_data[0] += typedata_size;
1472 typedata = typeinfo->func_data + (offset >> 2) + 1;
1473
1474
1475 /* find func with the same name - if it exists use its id */
1476 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1477 if(name_offset == typeinfo->func_names[i]) {
1478 id = typeinfo->func_indices[i];
1479 break;
1480 }
1481 }
1482
1483 /* find the first func with the same id and link via the hiword of typedata[4] */
1484 next_idx = index;
1485 for(i = 0; i < (typeinfo->typeinfo->cElement & 0xffff); i++) {
1486 if(id == typeinfo->func_indices[i]) {
1487 next_idx = typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] >> 16;
1488 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] &= 0xffff;
1489 typeinfo->func_data[(typeinfo->func_offsets[i] >> 2) + 1 + 4] |= (index << 16);
1490 break;
1491 }
1492 }
1493
1494 /* fill out the basic type information */
1495 typedata[0] = typedata_size | (index << 16);
1496 encode_var(typeinfo->typelib, type_function_get_rettype(func->type), func, &typedata[1], NULL, NULL, &decoded_size);
1497 typedata[2] = funcflags;
1498 typedata[3] = ((52 /*sizeof(FUNCDESC)*/ + decoded_size) << 16) | typeinfo->typeinfo->cbSizeVft;
1499 typedata[4] = (next_idx << 16) | (callconv << 8) | (invokekind << 3) | funckind;
1500 if(num_defaults) typedata[4] |= 0x1000;
1501 if(entry_is_ord) typedata[4] |= 0x2000;
1502 typedata[5] = (num_optional << 16) | num_params;
1503
1504 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1505 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1506 typedata[3] += (16 /*sizeof(ELEMDESC)*/ * num_params) << 16;
1507 typedata[3] += (24 /*sizeof(PARAMDESCEX)*/ * num_defaults) << 16;
1508
1509 switch(extra_attr) {
1510 case 6: typedata[11] = help_string_context;
1511 case 5: typedata[10] = -1;
1512 case 4: typedata[9] = -1;
1513 case 3: typedata[8] = entry;
1514 case 2: typedata[7] = help_string_offset;
1515 case 1: typedata[6] = help_context;
1516 case 0:
1517 break;
1518 default:
1519 warning("unknown number of optional attrs\n");
1520 }
1521
1522 if (type_get_function_args(func->type))
1523 {
1524 i = 0;
1525 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1526 {
1527 int paramflags = 0;
1528 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1529 int *defaultdata = num_defaults ? typedata + 6 + extra_attr + i : NULL;
1530
1531 if(defaultdata) *defaultdata = -1;
1532
1533 encode_var(typeinfo->typelib, arg->type, arg, paramdata, NULL, NULL, &decoded_size);
1534 if (arg->attrs) LIST_FOR_EACH_ENTRY( attr, arg->attrs, const attr_t, entry ) {
1535 switch(attr->type) {
1536 case ATTR_DEFAULTVALUE:
1537 {
1538 int vt;
1539 expr_t *expr = (expr_t *)attr->u.pval;
1540 vt = get_defaultvalue_vt(arg->type);
1541 paramflags |= 0x30; /* PARAMFLAG_FHASDEFAULT | PARAMFLAG_FOPT */
1542 if (expr->type == EXPR_STRLIT || expr->type == EXPR_WSTRLIT)
1543 {
1544 if (vt != VT_BSTR) error("string default value applied to non-string type\n");
1545 chat("default value '%s'\n", expr->u.sval);
1546 write_value(typeinfo->typelib, defaultdata, vt, expr->u.sval);
1547 }
1548 else
1549 {
1550 chat("default value %d\n", expr->cval);
1551 write_value(typeinfo->typelib, defaultdata, vt, &expr->cval);
1552 }
1553 break;
1554 }
1555 case ATTR_IN:
1556 paramflags |= 0x01; /* PARAMFLAG_FIN */
1557 break;
1558 case ATTR_OPTIONAL:
1559 paramflags |= 0x10; /* PARAMFLAG_FOPT */
1560 break;
1561 case ATTR_OUT:
1562 paramflags |= 0x02; /* PARAMFLAG_FOUT */
1563 break;
1564 case ATTR_PARAMLCID:
1565 paramflags |= 0x04; /* PARAMFLAG_LCID */
1566 lcid_retval_count++;
1567 break;
1568 case ATTR_RETVAL:
1569 paramflags |= 0x08; /* PARAMFLAG_FRETVAL */
1570 lcid_retval_count++;
1571 break;
1572 default:
1573 chat("unhandled param attr %d\n", attr->type);
1574 break;
1575 }
1576 }
1577 paramdata[1] = -1;
1578 paramdata[2] = paramflags;
1579 typedata[3] += decoded_size << 16;
1580
1581 i++;
1582 }
1583 }
1584
1585 if(lcid_retval_count == 1)
1586 typedata[4] |= 0x4000;
1587 else if(lcid_retval_count == 2)
1588 typedata[4] |= 0x8000;
1589
1590 if(typeinfo->funcs_allocated == 0) {
1591 typeinfo->funcs_allocated = 10;
1592 typeinfo->func_indices = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1593 typeinfo->func_names = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1594 typeinfo->func_offsets = xmalloc(typeinfo->funcs_allocated * sizeof(int));
1595 }
1596 if(typeinfo->funcs_allocated == (typeinfo->typeinfo->cElement & 0xffff)) {
1597 typeinfo->funcs_allocated *= 2;
1598 typeinfo->func_indices = xrealloc(typeinfo->func_indices, typeinfo->funcs_allocated * sizeof(int));
1599 typeinfo->func_names = xrealloc(typeinfo->func_names, typeinfo->funcs_allocated * sizeof(int));
1600 typeinfo->func_offsets = xrealloc(typeinfo->func_offsets, typeinfo->funcs_allocated * sizeof(int));
1601 }
1602
1603 /* update the index data */
1604 typeinfo->func_indices[typeinfo->typeinfo->cElement & 0xffff] = id;
1605 typeinfo->func_offsets[typeinfo->typeinfo->cElement & 0xffff] = offset;
1606 typeinfo->func_names[typeinfo->typeinfo->cElement & 0xffff] = name_offset;
1607
1608 /* ??? */
1609 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x20;
1610 typeinfo->typeinfo->res2 <<= 1;
1611 /* ??? */
1612 if (index < 2) typeinfo->typeinfo->res2 += num_params << 4;
1613
1614 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1615 typeinfo->typeinfo->res3 += 0x38 + num_params * 0x10;
1616 if(num_defaults) typeinfo->typeinfo->res3 += num_params * 0x4;
1617
1618 /* adjust size of VTBL */
1619 if(funckind != 0x3 /* FUNC_STATIC */)
1620 typeinfo->typeinfo->cbSizeVft += pointer_size;
1621
1622 /* Increment the number of function elements */
1623 typeinfo->typeinfo->cElement += 1;
1624
1625 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + name_offset;
1626 if (*((INT *)namedata) == -1) {
1627 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1628 if(typeinfo->typekind == TKIND_MODULE)
1629 namedata[9] |= 0x10;
1630 } else
1631 namedata[9] &= ~0x10;
1632
1633 if(typeinfo->typekind == TKIND_MODULE)
1634 namedata[9] |= 0x20;
1635
1636 if (type_get_function_args(func->type))
1637 {
1638 i = 0;
1639 LIST_FOR_EACH_ENTRY( arg, type_get_function_args(func->type), var_t, entry )
1640 {
1641 /* don't give the last arg of a [propput*] func a name */
1642 if(i != num_params - 1 || (invokekind != 0x4 /* INVOKE_PROPERTYPUT */ && invokekind != 0x8 /* INVOKE_PROPERTYPUTREF */))
1643 {
1644 int *paramdata = typedata + 6 + extra_attr + (num_defaults ? num_params : 0) + i * 3;
1645 offset = ctl2_alloc_name(typeinfo->typelib, arg->name);
1646 paramdata[1] = offset;
1647 }
1648 i++;
1649 }
1650 }
1651 return S_OK;
1652 }
1653
1654 static HRESULT add_var_desc(msft_typeinfo_t *typeinfo, UINT index, var_t* var)
1655 {
1656 int offset, id;
1657 unsigned int typedata_size;
1658 INT *typedata;
1659 int var_datawidth;
1660 int var_alignment;
1661 int var_type_size, var_kind = 0 /* VAR_PERINSTANCE */;
1662 int alignment;
1663 int varflags = 0;
1664 const attr_t *attr;
1665 unsigned char *namedata;
1666 int var_num = (typeinfo->typeinfo->cElement >> 16) & 0xffff;
1667
1668 chat("add_var_desc(%d, %s)\n", index, var->name);
1669
1670 id = 0x40000000 + index;
1671
1672 if (var->attrs) LIST_FOR_EACH_ENTRY( attr, var->attrs, const attr_t, entry ) {
1673 expr_t *expr = attr->u.pval;
1674 switch(attr->type) {
1675 case ATTR_BINDABLE:
1676 varflags |= 0x04; /* VARFLAG_FBINDABLE */
1677 break;
1678 case ATTR_DEFAULTBIND:
1679 varflags |= 0x20; /* VARFLAG_FDEFAULTBIND */
1680 break;
1681 case ATTR_DEFAULTCOLLELEM:
1682 varflags |= 0x100; /* VARFLAG_FDEFAULTCOLLELEM */
1683 break;
1684 case ATTR_DISPLAYBIND:
1685 varflags |= 0x10; /* VARFLAG_FDISPLAYBIND */
1686 break;
1687 case ATTR_HIDDEN:
1688 varflags |= 0x40; /* VARFLAG_FHIDDEN */
1689 break;
1690 case ATTR_ID:
1691 id = expr->cval;
1692 break;
1693 case ATTR_IMMEDIATEBIND:
1694 varflags |= 0x1000; /* VARFLAG_FIMMEDIATEBIND */
1695 break;
1696 case ATTR_NONBROWSABLE:
1697 varflags |= 0x400; /* VARFLAG_FNONBROWSABLE */
1698 break;
1699 case ATTR_READONLY:
1700 varflags |= 0x01; /* VARFLAG_FREADONLY */
1701 break;
1702 /* FIXME: VARFLAG_FREPLACEABLE */
1703 case ATTR_REQUESTEDIT:
1704 varflags |= 0x08; /* VARFLAG_FREQUESTEDIT */
1705 break;
1706 case ATTR_RESTRICTED:
1707 varflags |= 0x80; /* VARFLAG_FRESTRICTED */
1708 break;
1709 case ATTR_SOURCE:
1710 varflags |= 0x02; /* VARFLAG_FSOURCE */
1711 break;
1712 case ATTR_UIDEFAULT:
1713 varflags |= 0x0200; /* VARFLAG_FUIDEFAULT */
1714 break;
1715 default:
1716 break;
1717 }
1718 }
1719
1720 /* allocate type data space for us */
1721 typedata_size = 0x14;
1722
1723 if (!typeinfo->var_data) {
1724 typeinfo->var_data = xmalloc(0x100);
1725 typeinfo->var_data_allocated = 0x100;
1726 typeinfo->var_data[0] = 0;
1727 }
1728
1729 if(typeinfo->var_data[0] + typedata_size + sizeof(int) > typeinfo->var_data_allocated) {
1730 typeinfo->var_data_allocated = max(typeinfo->var_data_allocated * 2,
1731 typeinfo->var_data[0] + typedata_size + sizeof(int));
1732 typeinfo->var_data = xrealloc(typeinfo->var_data, typeinfo->var_data_allocated);
1733 }
1734
1735 offset = typeinfo->var_data[0];
1736 typeinfo->var_data[0] += typedata_size;
1737 typedata = typeinfo->var_data + (offset >> 2) + 1;
1738
1739 /* fill out the basic type information */
1740 typedata[0] = typedata_size | (index << 16);
1741 typedata[2] = varflags;
1742 typedata[3] = (36 /*sizeof(VARDESC)*/ << 16) | 0;
1743
1744 if(typeinfo->vars_allocated == 0) {
1745 typeinfo->vars_allocated = 10;
1746 typeinfo->var_indices = xmalloc(typeinfo->vars_allocated * sizeof(int));
1747 typeinfo->var_names = xmalloc(typeinfo->vars_allocated * sizeof(int));
1748 typeinfo->var_offsets = xmalloc(typeinfo->vars_allocated * sizeof(int));
1749 }
1750 if(typeinfo->vars_allocated == var_num) {
1751 typeinfo->vars_allocated *= 2;
1752 typeinfo->var_indices = xrealloc(typeinfo->var_indices, typeinfo->vars_allocated * sizeof(int));
1753 typeinfo->var_names = xrealloc(typeinfo->var_names, typeinfo->vars_allocated * sizeof(int));
1754 typeinfo->var_offsets = xrealloc(typeinfo->var_offsets, typeinfo->vars_allocated * sizeof(int));
1755 }
1756 /* update the index data */
1757 typeinfo->var_indices[var_num] = id;
1758 typeinfo->var_names[var_num] = -1;
1759 typeinfo->var_offsets[var_num] = offset;
1760
1761 /* figure out type widths and whatnot */
1762 encode_var(typeinfo->typelib, var->type, var, &typedata[1], &var_datawidth,
1763 &var_alignment, &var_type_size);
1764
1765 /* pad out starting position to data width */
1766 typeinfo->datawidth += var_alignment - 1;
1767 typeinfo->datawidth &= ~(var_alignment - 1);
1768
1769 switch(typeinfo->typekind) {
1770 case TKIND_ENUM:
1771 write_value(typeinfo->typelib, &typedata[4], VT_I4, &var->eval->cval);
1772 var_kind = 2; /* VAR_CONST */
1773 var_type_size += 16; /* sizeof(VARIANT) */
1774 typeinfo->datawidth = var_datawidth;
1775 break;
1776 case TKIND_RECORD:
1777 typedata[4] = typeinfo->datawidth;
1778 typeinfo->datawidth += var_datawidth;
1779 break;
1780 case TKIND_UNION:
1781 typedata[4] = typeinfo->datawidth;
1782 typeinfo->datawidth = max(typeinfo->datawidth, var_datawidth);
1783 break;
1784 case TKIND_DISPATCH:
1785 var_kind = 3; /* VAR_DISPATCH */
1786 typeinfo->datawidth = pointer_size;
1787 var_alignment = 4;
1788 break;
1789 default:
1790 error("add_var_desc: unhandled type kind %d\n", typeinfo->typekind);
1791 break;
1792 }
1793
1794 /* add type description size to total required allocation */
1795 typedata[3] += var_type_size << 16 | var_kind;
1796
1797 /* fix type alignment */
1798 alignment = (typeinfo->typeinfo->typekind >> 11) & 0x1f;
1799 if (alignment < var_alignment) {
1800 alignment = var_alignment;
1801 typeinfo->typeinfo->typekind &= ~0xffc0;
1802 typeinfo->typeinfo->typekind |= alignment << 11 | alignment << 6;
1803 }
1804
1805 /* ??? */
1806 if (!typeinfo->typeinfo->res2) typeinfo->typeinfo->res2 = 0x1a;
1807 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
1808 typeinfo->typeinfo->res2 <<= 1;
1809 }
1810
1811 /* ??? */
1812 if (typeinfo->typeinfo->res3 == -1) typeinfo->typeinfo->res3 = 0;
1813 typeinfo->typeinfo->res3 += 0x2c;
1814
1815 /* increment the number of variable elements */
1816 typeinfo->typeinfo->cElement += 0x10000;
1817
1818 /* pad data width to alignment */
1819 typeinfo->typeinfo->size = (typeinfo->datawidth + (alignment - 1)) & ~(alignment - 1);
1820
1821 offset = ctl2_alloc_name(typeinfo->typelib, var->name);
1822 if (offset == -1) return E_OUTOFMEMORY;
1823
1824 namedata = typeinfo->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
1825 if (*((INT *)namedata) == -1) {
1826 *((INT *)namedata) = typeinfo->typelib->typelib_typeinfo_offsets[typeinfo->typeinfo->typekind >> 16];
1827 if(typeinfo->typekind != TKIND_DISPATCH)
1828 namedata[9] |= 0x10;
1829 } else
1830 namedata[9] &= ~0x10;
1831
1832 if (typeinfo->typekind == TKIND_ENUM) {
1833 namedata[9] |= 0x20;
1834 }
1835 typeinfo->var_names[var_num] = offset;
1836
1837 return S_OK;
1838 }
1839
1840 static HRESULT add_impl_type(msft_typeinfo_t *typeinfo, type_t *ref, importinfo_t *importinfo)
1841 {
1842 if(importinfo) {
1843 alloc_importinfo(typeinfo->typelib, importinfo);
1844 typeinfo->typeinfo->datatype1 = importinfo->offset+1;
1845 }else {
1846 if(ref->typelib_idx == -1)
1847 add_interface_typeinfo(typeinfo->typelib, ref);
1848 if(ref->typelib_idx == -1)
1849 error("add_impl_type: unable to add inherited interface\n");
1850
1851 typeinfo->typeinfo->datatype1 = typeinfo->typelib->typelib_typeinfo_offsets[ref->typelib_idx];
1852 }
1853
1854 typeinfo->typeinfo->cImplTypes++;
1855 return S_OK;
1856 }
1857
1858 static msft_typeinfo_t *create_msft_typeinfo(msft_typelib_t *typelib, enum type_kind kind,
1859 const char *name, const attr_list_t *attrs)
1860 {
1861 const attr_t *attr;
1862 msft_typeinfo_t *msft_typeinfo;
1863 int nameoffset;
1864 int typeinfo_offset;
1865 MSFT_TypeInfoBase *typeinfo;
1866 MSFT_GuidEntry guidentry;
1867
1868 chat("create_msft_typeinfo: name %s kind %d index %d\n", name, kind, typelib->typelib_header.nrtypeinfos);
1869
1870 msft_typeinfo = xmalloc(sizeof(*msft_typeinfo));
1871 memset( msft_typeinfo, 0, sizeof(*msft_typeinfo) );
1872
1873 msft_typeinfo->typelib = typelib;
1874
1875 nameoffset = ctl2_alloc_name(typelib, name);
1876 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
1877 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
1878
1879 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
1880 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
1881
1882 msft_typeinfo->typekind = kind;
1883 msft_typeinfo->typeinfo = typeinfo;
1884
1885 typeinfo->typekind |= kind | 0x20;
1886
1887 if(kind == TKIND_COCLASS)
1888 typeinfo->flags |= 0x2; /* TYPEFLAG_FCANCREATE */
1889
1890 if (attrs) LIST_FOR_EACH_ENTRY( attr, attrs, const attr_t, entry ) {
1891 switch(attr->type) {
1892 case ATTR_AGGREGATABLE:
1893 if (kind == TKIND_COCLASS)
1894 typeinfo->flags |= 0x400; /* TYPEFLAG_FAGGREGATABLE */
1895 break;
1896
1897 case ATTR_APPOBJECT:
1898 if (kind == TKIND_COCLASS)
1899 typeinfo->flags |= 0x1; /* TYPEFLAG_FAPPOBJECT */
1900 break;
1901
1902 case ATTR_CONTROL:
1903 if (kind == TKIND_COCLASS)
1904 typeinfo->flags |= 0x20; /* TYPEFLAG_FCONTROL */
1905 break;
1906
1907 case ATTR_DLLNAME:
1908 {
1909 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1910 typeinfo->datatype1 = offset;
1911 break;
1912 }
1913
1914 case ATTR_DUAL:
1915 /* FIXME: check interface is compatible */
1916 typeinfo->typekind = (typeinfo->typekind & ~0xff) | 0x34;
1917 typeinfo->flags |= 0x140; /* TYPEFLAG_FDUAL | TYPEFLAG_FOLEAUTOMATION */
1918 break;
1919
1920 case ATTR_HELPCONTEXT:
1921 {
1922 expr_t *expr = (expr_t*)attr->u.pval;
1923 typeinfo->helpcontext = expr->cval;
1924 break;
1925 }
1926 case ATTR_HELPSTRING:
1927 {
1928 int offset = ctl2_alloc_string(typelib, attr->u.pval);
1929 if (offset == -1) break;
1930 typeinfo->docstringoffs = offset;
1931 break;
1932 }
1933 case ATTR_HELPSTRINGCONTEXT:
1934 {
1935 expr_t *expr = (expr_t*)attr->u.pval;
1936 typeinfo->helpstringcontext = expr->cval;
1937 break;
1938 }
1939 case ATTR_HIDDEN:
1940 typeinfo->flags |= 0x10; /* TYPEFLAG_FHIDDEN */
1941 break;
1942
1943 case ATTR_LICENSED:
1944 typeinfo->flags |= 0x04; /* TYPEFLAG_FLICENSED */
1945 break;
1946
1947 case ATTR_NONCREATABLE:
1948 typeinfo->flags &= ~0x2; /* TYPEFLAG_FCANCREATE */
1949 break;
1950
1951 case ATTR_NONEXTENSIBLE:
1952 typeinfo->flags |= 0x80; /* TYPEFLAG_FNONEXTENSIBLE */
1953 break;
1954
1955 case ATTR_OLEAUTOMATION:
1956 typeinfo->flags |= 0x100; /* TYPEFLAG_FOLEAUTOMATION */
1957 break;
1958
1959 /* FIXME: TYPEFLAG_FPREDCLID */
1960
1961 case ATTR_PROXY:
1962 typeinfo->flags |= 0x4000; /* TYPEFLAG_FPROXY */
1963 break;
1964
1965 /* FIXME: TYPEFLAG_FREPLACEABLE */
1966
1967 case ATTR_RESTRICTED:
1968 typeinfo->flags |= 0x200; /* TYPEFLAG_FRESTRICTED */
1969 break;
1970
1971 case ATTR_UUID:
1972 guidentry.guid = *(GUID*)attr->u.pval;
1973 guidentry.hreftype = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1974 guidentry.next_hash = -1;
1975 typeinfo->posguid = ctl2_alloc_guid(typelib, &guidentry);
1976 #if 0
1977 if (IsEqualIID(guid, &IID_IDispatch)) {
1978 typelib->typelib_header.dispatchpos = typelib->typelib_typeinfo_offsets[typeinfo->typekind >> 16];
1979 }
1980 #endif
1981 break;
1982
1983 case ATTR_VERSION:
1984 typeinfo->version = attr->u.ival;
1985 break;
1986
1987 default:
1988 break;
1989 }
1990 }
1991
1992 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = msft_typeinfo;
1993 typelib->last_typeinfo = msft_typeinfo;
1994 if (!typelib->typeinfos) typelib->typeinfos = msft_typeinfo;
1995
1996
1997 return msft_typeinfo;
1998 }
1999
2000 static void add_dispatch(msft_typelib_t *typelib)
2001 {
2002 int guid_offset, impfile_offset, hash_key;
2003 MSFT_GuidEntry guidentry;
2004 MSFT_ImpInfo impinfo;
2005 GUID stdole = {0x00020430,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2006 GUID iid_idispatch = {0x00020400,0x0000,0x0000,{0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
2007
2008 if(typelib->typelib_header.dispatchpos != -1) return;
2009
2010 guidentry.guid = stdole;
2011 guidentry.hreftype = 2;
2012 guidentry.next_hash = -1;
2013 hash_key = ctl2_hash_guid(&guidentry.guid);
2014 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2015 if (guid_offset == -1)
2016 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2017 impfile_offset = alloc_importfile(typelib, guid_offset, 2, 0, "stdole2.tlb");
2018
2019 guidentry.guid = iid_idispatch;
2020 guidentry.hreftype = 1;
2021 guidentry.next_hash = -1;
2022 impinfo.flags = TKIND_INTERFACE << 24 | MSFT_IMPINFO_OFFSET_IS_GUID;
2023 impinfo.oImpFile = impfile_offset;
2024 hash_key = ctl2_hash_guid(&guidentry.guid);
2025 guid_offset = ctl2_find_guid(typelib, hash_key, &guidentry.guid);
2026 if (guid_offset == -1)
2027 guid_offset = ctl2_alloc_guid(typelib, &guidentry);
2028 impinfo.oGuid = guid_offset;
2029 typelib->typelib_header.dispatchpos = alloc_msft_importinfo(typelib, &impinfo) | 0x01;
2030 }
2031
2032 static void add_dispinterface_typeinfo(msft_typelib_t *typelib, type_t *dispinterface)
2033 {
2034 int idx = 0;
2035 var_t *func;
2036 var_t *var;
2037 msft_typeinfo_t *msft_typeinfo;
2038
2039 if (-1 < dispinterface->typelib_idx)
2040 return;
2041
2042 dispinterface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2043 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_DISPATCH, dispinterface->name,
2044 dispinterface->attrs);
2045
2046 msft_typeinfo->typeinfo->size = pointer_size;
2047 msft_typeinfo->typeinfo->typekind |= 0x2100;
2048
2049 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2050 add_dispatch(typelib);
2051 msft_typeinfo->typeinfo->cImplTypes = 1;
2052
2053 /* count the no of methods, as the variable indices come after the funcs */
2054 if (dispinterface->details.iface->disp_methods)
2055 LIST_FOR_EACH_ENTRY( func, dispinterface->details.iface->disp_methods, var_t, entry )
2056 idx++;
2057
2058 if (type_dispiface_get_props(dispinterface))
2059 LIST_FOR_EACH_ENTRY( var, type_dispiface_get_props(dispinterface), var_t, entry )
2060 add_var_desc(msft_typeinfo, idx++, var);
2061
2062 if (type_dispiface_get_methods(dispinterface))
2063 {
2064 idx = 0;
2065 LIST_FOR_EACH_ENTRY( func, type_dispiface_get_methods(dispinterface), var_t, entry )
2066 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2067 idx++;
2068 }
2069 }
2070
2071 static void add_interface_typeinfo(msft_typelib_t *typelib, type_t *interface)
2072 {
2073 int idx = 0;
2074 const statement_t *stmt_func;
2075 type_t *ref;
2076 msft_typeinfo_t *msft_typeinfo;
2077 importinfo_t *ref_importinfo = NULL;
2078 int num_parents = 0, num_funcs = 0;
2079 type_t *inherit;
2080 const type_t *derived;
2081
2082 if (-1 < interface->typelib_idx)
2083 return;
2084
2085 if (!interface->details.iface)
2086 {
2087 error( "interface %s is referenced but not defined\n", interface->name );
2088 return;
2089 }
2090
2091 if (is_attr(interface->attrs, ATTR_DISPINTERFACE)) {
2092 add_dispinterface_typeinfo(typelib, interface);
2093 return;
2094 }
2095
2096 /* midl adds the parent interface first, unless the parent itself
2097 has no parent (i.e. it stops before IUnknown). */
2098
2099 inherit = type_iface_get_inherit(interface);
2100
2101 if(inherit) {
2102 ref_importinfo = find_importinfo(typelib, inherit->name);
2103
2104 if(!ref_importinfo && type_iface_get_inherit(inherit) &&
2105 inherit->typelib_idx == -1)
2106 add_interface_typeinfo(typelib, inherit);
2107 }
2108
2109 /* check typelib_idx again, it could have been added while resolving the parent interface */
2110 if (-1 < interface->typelib_idx)
2111 return;
2112
2113 interface->typelib_idx = typelib->typelib_header.nrtypeinfos;
2114 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_INTERFACE, interface->name, interface->attrs);
2115 msft_typeinfo->typeinfo->size = pointer_size;
2116 msft_typeinfo->typeinfo->typekind |= 0x2200;
2117
2118 for (derived = inherit; derived; derived = type_iface_get_inherit(derived))
2119 if (derived->name && !strcmp(derived->name, "IDispatch"))
2120 msft_typeinfo->typeinfo->flags |= 0x1000; /* TYPEFLAG_FDISPATCHABLE */
2121
2122 /* can't be dual if it doesn't derive from IDispatch */
2123 if (!(msft_typeinfo->typeinfo->flags & 0x1000)) /* TYPEFLAG_FDISPATCHABLE */
2124 msft_typeinfo->typeinfo->flags &= ~0x40; /* TYPEFLAG_FDUAL */
2125
2126 if(type_iface_get_inherit(interface))
2127 add_impl_type(msft_typeinfo, type_iface_get_inherit(interface),
2128 ref_importinfo);
2129
2130 /* count the number of inherited interfaces and non-local functions */
2131 for(ref = inherit; ref; ref = type_iface_get_inherit(ref)) {
2132 num_parents++;
2133 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(ref) ) {
2134 var_t *func = stmt_func->u.var;
2135 if (!is_local(func->attrs)) num_funcs++;
2136 }
2137 }
2138 msft_typeinfo->typeinfo->datatype2 = num_funcs << 16 | num_parents;
2139 msft_typeinfo->typeinfo->cbSizeVft = num_funcs * pointer_size;
2140
2141 STATEMENTS_FOR_EACH_FUNC( stmt_func, type_iface_get_stmts(interface) ) {
2142 var_t *func = stmt_func->u.var;
2143 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2144 idx++;
2145 }
2146 }
2147
2148 static void add_structure_typeinfo(msft_typelib_t *typelib, type_t *structure)
2149 {
2150 int idx = 0;
2151 var_t *cur;
2152 msft_typeinfo_t *msft_typeinfo;
2153
2154 if (-1 < structure->typelib_idx)
2155 return;
2156
2157 structure->typelib_idx = typelib->typelib_header.nrtypeinfos;
2158 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_RECORD, structure->name, structure->attrs);
2159 msft_typeinfo->typeinfo->size = 0;
2160
2161 if (type_struct_get_fields(structure))
2162 LIST_FOR_EACH_ENTRY( cur, type_struct_get_fields(structure), var_t, entry )
2163 add_var_desc(msft_typeinfo, idx++, cur);
2164 }
2165
2166 static void add_enum_typeinfo(msft_typelib_t *typelib, type_t *enumeration)
2167 {
2168 int idx = 0;
2169 var_t *cur;
2170 msft_typeinfo_t *msft_typeinfo;
2171
2172 if (-1 < enumeration->typelib_idx)
2173 return;
2174
2175 enumeration->typelib_idx = typelib->typelib_header.nrtypeinfos;
2176 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ENUM, enumeration->name, enumeration->attrs);
2177 msft_typeinfo->typeinfo->size = 0;
2178
2179 if (type_enum_get_values(enumeration))
2180 LIST_FOR_EACH_ENTRY( cur, type_enum_get_values(enumeration), var_t, entry )
2181 add_var_desc(msft_typeinfo, idx++, cur);
2182 }
2183
2184 static void add_union_typeinfo(msft_typelib_t *typelib, type_t *tunion)
2185 {
2186 int idx = 0;
2187 var_t *cur;
2188 msft_typeinfo_t *msft_typeinfo;
2189
2190 if (-1 < tunion->typelib_idx)
2191 return;
2192
2193 tunion->typelib_idx = typelib->typelib_header.nrtypeinfos;
2194 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_UNION, tunion->name, tunion->attrs);
2195 msft_typeinfo->typeinfo->size = 0;
2196
2197 if (type_union_get_cases(tunion))
2198 LIST_FOR_EACH_ENTRY(cur, type_union_get_cases(tunion), var_t, entry)
2199 add_var_desc(msft_typeinfo, idx++, cur);
2200 }
2201
2202 static void add_typedef_typeinfo(msft_typelib_t *typelib, type_t *tdef)
2203 {
2204 msft_typeinfo_t *msft_typeinfo = NULL;
2205 int alignment, datatype1, datatype2, size, duplicate = 0;
2206 type_t *type;
2207
2208 if (-1 < tdef->typelib_idx)
2209 return;
2210
2211 type = type_alias_get_aliasee(tdef);
2212
2213 if (!type->name || strcmp(tdef->name, type->name) != 0)
2214 {
2215 tdef->typelib_idx = typelib->typelib_header.nrtypeinfos;
2216 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_ALIAS, tdef->name, tdef->attrs);
2217 }
2218 else
2219 duplicate = 1;
2220
2221 encode_type(typelib, get_type_vt(type), type,
2222 &datatype1, &size, &alignment, &datatype2);
2223
2224 if (msft_typeinfo)
2225 {
2226 msft_typeinfo->typeinfo->datatype1 = datatype1;
2227 msft_typeinfo->typeinfo->size = size;
2228 msft_typeinfo->typeinfo->datatype2 = datatype2;
2229 msft_typeinfo->typeinfo->typekind |= (alignment << 11 | alignment << 6);
2230 }
2231
2232 /* avoid adding duplicate type definitions */
2233 if (duplicate)
2234 tdef->typelib_idx = type->typelib_idx;
2235 }
2236
2237 static void add_coclass_typeinfo(msft_typelib_t *typelib, type_t *cls)
2238 {
2239 msft_typeinfo_t *msft_typeinfo;
2240 ifref_t *iref;
2241 int num_ifaces = 0, offset, i;
2242 MSFT_RefRecord *ref, *first = NULL, *first_source = NULL;
2243 int have_default = 0, have_default_source = 0;
2244 const attr_t *attr;
2245 ifref_list_t *ifaces;
2246
2247 if (-1 < cls->typelib_idx)
2248 return;
2249
2250 cls->typelib_idx = typelib->typelib_header.nrtypeinfos;
2251 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_COCLASS, cls->name, cls->attrs);
2252
2253 ifaces = type_coclass_get_ifaces(cls);
2254 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) num_ifaces++;
2255
2256 offset = msft_typeinfo->typeinfo->datatype1 = ctl2_alloc_segment(typelib, MSFT_SEG_REFERENCES,
2257 num_ifaces * sizeof(*ref), 0);
2258
2259 i = 0;
2260 if (ifaces) LIST_FOR_EACH_ENTRY( iref, ifaces, ifref_t, entry ) {
2261 if(iref->iface->typelib_idx == -1)
2262 add_interface_typeinfo(typelib, iref->iface);
2263 ref = (MSFT_RefRecord*) (typelib->typelib_segment_data[MSFT_SEG_REFERENCES] + offset + i * sizeof(*ref));
2264 ref->reftype = typelib->typelib_typeinfo_offsets[iref->iface->typelib_idx];
2265 ref->flags = 0;
2266 ref->oCustData = -1;
2267 ref->onext = -1;
2268 if(i < num_ifaces - 1)
2269 ref->onext = offset + (i + 1) * sizeof(*ref);
2270
2271 if (iref->attrs) LIST_FOR_EACH_ENTRY( attr, iref->attrs, const attr_t, entry ) {
2272 switch(attr->type) {
2273 case ATTR_DEFAULT:
2274 ref->flags |= 0x1; /* IMPLTYPEFLAG_FDEFAULT */
2275 break;
2276 case ATTR_DEFAULTVTABLE:
2277 ref->flags |= 0x8; /* IMPLTYPEFLAG_FDEFAULTVTABLE */
2278 break;
2279 case ATTR_RESTRICTED:
2280 ref->flags |= 0x4; /* IMPLTYPEFLAG_FRESTRICTED */
2281 break;
2282 case ATTR_SOURCE:
2283 ref->flags |= 0x2; /* IMPLTYPEFLAG_FSOURCE */
2284 break;
2285 default:
2286 warning("add_coclass_typeinfo: unhandled attr %d\n", attr->type);
2287 }
2288 }
2289 if(ref->flags & 0x1) { /* IMPLTYPEFLAG_FDEFAULT */
2290 if(ref->flags & 0x2) /* IMPLTYPEFLAG_SOURCE */
2291 have_default_source = 1;
2292 else
2293 have_default = 1;
2294 }
2295
2296 /* If the interface is non-restricted and we haven't already had one then
2297 remember it so that we can use it as a default later */
2298 if((ref->flags & 0x4) == 0) { /* IMPLTYPEFLAG_FRESTRICTED */
2299 if(ref->flags & 0x2) { /* IMPLTYPEFLAG_FSOURCE */
2300 if(!first_source)
2301 first_source = ref;
2302 }
2303 else if(!first)
2304 first = ref;
2305 }
2306 i++;
2307 }
2308
2309 /* If we haven't had a default interface, then set the default flags on the
2310 first ones */
2311 if(!have_default && first)
2312 first->flags |= 0x1;
2313 if(!have_default_source && first_source)
2314 first_source->flags |= 0x1;
2315
2316 msft_typeinfo->typeinfo->cImplTypes = num_ifaces;
2317 msft_typeinfo->typeinfo->size = pointer_size;
2318 msft_typeinfo->typeinfo->typekind |= 0x2200;
2319 }
2320
2321 static void add_module_typeinfo(msft_typelib_t *typelib, type_t *module)
2322 {
2323 int idx = 0;
2324 const statement_t *stmt;
2325 msft_typeinfo_t *msft_typeinfo;
2326
2327 if (-1 < module->typelib_idx)
2328 return;
2329
2330 module->typelib_idx = typelib->typelib_header.nrtypeinfos;
2331 msft_typeinfo = create_msft_typeinfo(typelib, TKIND_MODULE, module->name, module->attrs);
2332 msft_typeinfo->typeinfo->typekind |= 0x0a00;
2333
2334 STATEMENTS_FOR_EACH_FUNC( stmt, module->details.module->stmts ) {
2335 var_t *func = stmt->u.var;
2336 if(add_func_desc(msft_typeinfo, func, idx) == S_OK)
2337 idx++;
2338 }
2339
2340 msft_typeinfo->typeinfo->size = idx;
2341 }
2342
2343 static void add_type_typeinfo(msft_typelib_t *typelib, type_t *type)
2344 {
2345 switch (type_get_type(type)) {
2346 case TYPE_INTERFACE:
2347 add_interface_typeinfo(typelib, type);
2348 break;
2349 case TYPE_STRUCT:
2350 add_structure_typeinfo(typelib, type);
2351 break;
2352 case TYPE_ENUM:
2353 add_enum_typeinfo(typelib, type);
2354 break;
2355 case TYPE_UNION:
2356 add_union_typeinfo(typelib, type);
2357 break;
2358 case TYPE_COCLASS:
2359 add_coclass_typeinfo(typelib, type);
2360 break;
2361 case TYPE_BASIC:
2362 case TYPE_POINTER:
2363 break;
2364 default:
2365 error("add_entry: unhandled type 0x%x for %s\n",
2366 type_get_type(type), type->name);
2367 break;
2368 }
2369 }
2370
2371 static void add_entry(msft_typelib_t *typelib, const statement_t *stmt)
2372 {
2373 switch(stmt->type) {
2374 case STMT_LIBRARY:
2375 case STMT_IMPORT:
2376 case STMT_PRAGMA:
2377 case STMT_CPPQUOTE:
2378 case STMT_DECLARATION:
2379 /* not included in typelib */
2380 break;
2381 case STMT_IMPORTLIB:
2382 /* not processed here */
2383 break;
2384 case STMT_TYPEDEF:
2385 {
2386 const type_list_t *type_entry = stmt->u.type_list;
2387 for (; type_entry; type_entry = type_entry->next) {
2388 /* if the type is public then add the typedef, otherwise attempt
2389 * to add the aliased type */
2390 if (is_attr(type_entry->type->attrs, ATTR_PUBLIC))
2391 add_typedef_typeinfo(typelib, type_entry->type);
2392 else
2393 add_type_typeinfo(typelib, type_alias_get_aliasee(type_entry->type));
2394 }
2395 break;
2396 }
2397 case STMT_MODULE:
2398 add_module_typeinfo(typelib, stmt->u.type);
2399 break;
2400 case STMT_TYPE:
2401 case STMT_TYPEREF:
2402 {
2403 type_t *type = stmt->u.type;
2404 add_type_typeinfo(typelib, type);
2405 break;
2406 }
2407 }
2408 }
2409
2410 static void set_name(msft_typelib_t *typelib)
2411 {
2412 int offset;
2413
2414 offset = ctl2_alloc_name(typelib, typelib->typelib->name);
2415 if (offset == -1) return;
2416 typelib->typelib_header.NameOffset = offset;
2417 return;
2418 }
2419
2420 static void set_version(msft_typelib_t *typelib)
2421 {
2422 typelib->typelib_header.version = get_attrv( typelib->typelib->attrs, ATTR_VERSION );
2423 }
2424
2425 static void set_guid(msft_typelib_t *typelib)
2426 {
2427 MSFT_GuidEntry guidentry;
2428 int offset;
2429 void *ptr;
2430 GUID guid = {0,0,0,{0,0,0,0,0,0}};
2431
2432 guidentry.guid = guid;
2433 guidentry.hreftype = -2;
2434 guidentry.next_hash = -1;
2435
2436 ptr = get_attrp( typelib->typelib->attrs, ATTR_UUID );
2437 if (ptr) guidentry.guid = *(GUID *)ptr;
2438
2439 offset = ctl2_alloc_guid(typelib, &guidentry);
2440 typelib->typelib_header.posguid = offset;
2441
2442 return;
2443 }
2444
2445 static void set_doc_string(msft_typelib_t *typelib)
2446 {
2447 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRING );
2448
2449 if (str)
2450 {
2451 int offset = ctl2_alloc_string(typelib, str);
2452 if (offset != -1) typelib->typelib_header.helpstring = offset;
2453 }
2454 }
2455
2456 static void set_help_file_name(msft_typelib_t *typelib)
2457 {
2458 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPFILE );
2459
2460 if (str)
2461 {
2462 int offset = ctl2_alloc_string(typelib, str);
2463 if (offset != -1)
2464 {
2465 typelib->typelib_header.helpfile = offset;
2466 typelib->typelib_header.varflags |= 0x10;
2467 }
2468 }
2469 }
2470
2471 static void set_help_context(msft_typelib_t *typelib)
2472 {
2473 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPCONTEXT );
2474 if (expr) typelib->typelib_header.helpcontext = expr->cval;
2475 }
2476
2477 static void set_help_string_dll(msft_typelib_t *typelib)
2478 {
2479 char *str = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGDLL );
2480
2481 if (str)
2482 {
2483 int offset = ctl2_alloc_string(typelib, str);
2484 if (offset != -1)
2485 {
2486 typelib->help_string_dll_offset = offset;
2487 typelib->typelib_header.varflags |= 0x100;
2488 }
2489 }
2490 }
2491
2492 static void set_help_string_context(msft_typelib_t *typelib)
2493 {
2494 const expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_HELPSTRINGCONTEXT );
2495 if (expr) typelib->typelib_header.helpstringcontext = expr->cval;
2496 }
2497
2498 static void set_lcid(msft_typelib_t *typelib)
2499 {
2500 const expr_t *lcid_expr = get_attrp( typelib->typelib->attrs, ATTR_LIBLCID );
2501 if(lcid_expr)
2502 {
2503 typelib->typelib_header.lcid = lcid_expr->cval;
2504 typelib->typelib_header.lcid2 = lcid_expr->cval;
2505 }
2506 }
2507
2508 static void set_lib_flags(msft_typelib_t *typelib)
2509 {
2510 const attr_t *attr;
2511
2512 typelib->typelib_header.flags = 0;
2513 if (!typelib->typelib->attrs) return;
2514 LIST_FOR_EACH_ENTRY( attr, typelib->typelib->attrs, const attr_t, entry )
2515 {
2516 switch(attr->type) {
2517 case ATTR_CONTROL:
2518 typelib->typelib_header.flags |= 0x02; /* LIBFLAG_FCONTROL */
2519 break;
2520 case ATTR_HIDDEN:
2521 typelib->typelib_header.flags |= 0x04; /* LIBFLAG_FHIDDEN */
2522 break;
2523 case ATTR_RESTRICTED:
2524 typelib->typelib_header.flags |= 0x01; /* LIBFLAG_FRESTRICTED */
2525 break;
2526 default:
2527 break;
2528 }
2529 }
2530 return;
2531 }
2532
2533 static void ctl2_write_segment(msft_typelib_t *typelib, int segment)
2534 {
2535 put_data(typelib->typelib_segment_data[segment], typelib->typelib_segdir[segment].length);
2536 }
2537
2538 static void ctl2_finalize_typeinfos(msft_typelib_t *typelib, int filesize)
2539 {
2540 msft_typeinfo_t *typeinfo;
2541
2542 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2543 typeinfo->typeinfo->memoffset = filesize;
2544 if (typeinfo->func_data)
2545 filesize += typeinfo->func_data[0] + ((typeinfo->typeinfo->cElement & 0xffff) * 12);
2546 if (typeinfo->var_data)
2547 filesize += typeinfo->var_data[0] + (((typeinfo->typeinfo->cElement >> 16) & 0xffff) * 12);
2548 if (typeinfo->func_data || typeinfo->var_data)
2549 filesize += 4;
2550 }
2551 }
2552
2553 static int ctl2_finalize_segment(msft_typelib_t *typelib, int filepos, int segment)
2554 {
2555 if (typelib->typelib_segdir[segment].length) {
2556 typelib->typelib_segdir[segment].offset = filepos;
2557 } else {
2558 typelib->typelib_segdir[segment].offset = -1;
2559 }
2560
2561 return typelib->typelib_segdir[segment].length;
2562 }
2563
2564
2565 static void ctl2_write_typeinfos(msft_typelib_t *typelib)
2566 {
2567 msft_typeinfo_t *typeinfo;
2568 int typedata_size;
2569
2570 for (typeinfo = typelib->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
2571 if (!typeinfo->func_data && !typeinfo->var_data) continue;
2572 typedata_size = 0;
2573 if (typeinfo->func_data)
2574 typedata_size = typeinfo->func_data[0];
2575 if (typeinfo->var_data)
2576 typedata_size += typeinfo->var_data[0];
2577 put_data(&typedata_size, sizeof(int));
2578 if (typeinfo->func_data)
2579 put_data(typeinfo->func_data + 1, typeinfo->func_data[0]);
2580 if (typeinfo->var_data)
2581 put_data(typeinfo->var_data + 1, typeinfo->var_data[0]);
2582 if (typeinfo->func_indices)
2583 put_data(typeinfo->func_indices, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2584 if (typeinfo->var_indices)
2585 put_data(typeinfo->var_indices, (typeinfo->typeinfo->cElement >> 16) * 4);
2586 if (typeinfo->func_names)
2587 put_data(typeinfo->func_names, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2588 if (typeinfo->var_names)
2589 put_data(typeinfo->var_names, (typeinfo->typeinfo->cElement >> 16) * 4);
2590 if (typeinfo->func_offsets)
2591 put_data(typeinfo->func_offsets, (typeinfo->typeinfo->cElement & 0xffff) * 4);
2592 if (typeinfo->var_offsets) {
2593 int add = 0, i, offset;
2594 if(typeinfo->func_data)
2595 add = typeinfo->func_data[0];
2596 for(i = 0; i < (typeinfo->typeinfo->cElement >> 16); i++) {
2597 offset = typeinfo->var_offsets[i];
2598 offset += add;
2599 put_data(&offset, 4);
2600 }
2601 }
2602 }
2603 }
2604
2605 static void save_all_changes(msft_typelib_t *typelib)
2606 {
2607 int filepos;
2608
2609 chat("save_all_changes(%p)\n", typelib);
2610
2611 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
2612 if(typelib->typelib_header.varflags & 0x100) filepos += 4; /* helpstringdll */
2613 filepos += typelib->typelib_header.nrtypeinfos * 4;
2614
2615 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEINFO);
2616 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUIDHASH);
2617 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_GUID);
2618 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_REFERENCES);
2619 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTINFO);
2620 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_IMPORTFILES);
2621 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAMEHASH);
2622 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_NAME);
2623 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_STRING);
2624 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_TYPEDESC);
2625 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_ARRAYDESC);
2626 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATA);
2627 filepos += ctl2_finalize_segment(typelib, filepos, MSFT_SEG_CUSTDATAGUID);
2628
2629 ctl2_finalize_typeinfos(typelib, filepos);
2630
2631 byte_swapped = 0;
2632 init_output_buffer();
2633
2634 put_data(&typelib->typelib_header, sizeof(typelib->typelib_header));
2635 if(typelib->typelib_header.varflags & 0x100)
2636 put_data(&typelib->help_string_dll_offset, sizeof(typelib->help_string_dll_offset));
2637
2638 put_data(typelib->typelib_typeinfo_offsets, typelib->typelib_header.nrtypeinfos * 4);
2639 put_data(&typelib->typelib_segdir, sizeof(typelib->typelib_segdir));
2640 ctl2_write_segment( typelib, MSFT_SEG_TYPEINFO );
2641 ctl2_write_segment( typelib, MSFT_SEG_GUIDHASH );
2642 ctl2_write_segment( typelib, MSFT_SEG_GUID );
2643 ctl2_write_segment( typelib, MSFT_SEG_REFERENCES );
2644 ctl2_write_segment( typelib, MSFT_SEG_IMPORTINFO );
2645 ctl2_write_segment( typelib, MSFT_SEG_IMPORTFILES );
2646 ctl2_write_segment( typelib, MSFT_SEG_NAMEHASH );
2647 ctl2_write_segment( typelib, MSFT_SEG_NAME );
2648 ctl2_write_segment( typelib, MSFT_SEG_STRING );
2649 ctl2_write_segment( typelib, MSFT_SEG_TYPEDESC );
2650 ctl2_write_segment( typelib, MSFT_SEG_ARRAYDESC );
2651 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATA );
2652 ctl2_write_segment( typelib, MSFT_SEG_CUSTDATAGUID );
2653
2654 ctl2_write_typeinfos(typelib);
2655
2656 if (strendswith( typelib_name, ".res" )) /* create a binary resource file */
2657 {
2658 char typelib_id[13] = "#1";
2659
2660 expr_t *expr = get_attrp( typelib->typelib->attrs, ATTR_ID );
2661 if (expr)
2662 sprintf( typelib_id, "#%d", expr->cval );
2663 add_output_to_resources( "TYPELIB", typelib_id );
2664 output_typelib_regscript( typelib->typelib );
2665 flush_output_resources( typelib_name );
2666 }
2667 else flush_output_buffer( typelib_name );
2668 }
2669
2670 int create_msft_typelib(typelib_t *typelib)
2671 {
2672 msft_typelib_t *msft;
2673 int failed = 0;
2674 const statement_t *stmt;
2675 time_t cur_time;
2676 char *time_override;
2677 unsigned int version = 7 << 24 | 555; /* 7.00.0555 */
2678 GUID midl_time_guid = {0xde77ba63,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2679 GUID midl_version_guid = {0xde77ba64,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2680 GUID midl_info_guid = {0xde77ba65,0x517c,0x11d1,{0xa2,0xda,0x00,0x00,0xf8,0x77,0x3c,0xe9}};
2681 char info_string[128];
2682
2683 pointer_size = (typelib_kind == SYS_WIN64) ? 8 : 4;
2684
2685 msft = xmalloc(sizeof(*msft));
2686 memset(msft, 0, sizeof(*msft));
2687 msft->typelib = typelib;
2688
2689 ctl2_init_header(msft);
2690 ctl2_init_segdir(msft);
2691
2692 msft->typelib_header.varflags |= typelib_kind;
2693
2694 /*
2695 * The following two calls return an offset or -1 if out of memory. We
2696 * specifically need an offset of 0, however, so...
2697 */
2698 if (ctl2_alloc_segment(msft, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
2699 if (ctl2_alloc_segment(msft, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
2700
2701 if(failed)
2702 {
2703 free(msft);
2704 return 0;
2705 }
2706
2707 msft->typelib_guidhash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_GUIDHASH];
2708 msft->typelib_namehash_segment = (int *)msft->typelib_segment_data[MSFT_SEG_NAMEHASH];
2709
2710 memset(msft->typelib_guidhash_segment, 0xff, 0x80);
2711 memset(msft->typelib_namehash_segment, 0xff, 0x200);
2712
2713 set_lib_flags(msft);
2714 set_lcid(msft);
2715 set_help_file_name(msft);
2716 set_doc_string(msft);
2717 set_guid(msft);
2718 set_version(msft);
2719 set_name(msft);
2720 set_help_context(msft);
2721 set_help_string_dll(msft);
2722 set_help_string_context(msft);
2723
2724 /* midl adds two sets of custom data to the library: the current unix time
2725 and midl's version number */
2726 time_override = getenv( "WIDL_TIME_OVERRIDE");
2727 cur_time = time_override ? atol( time_override) : time(NULL);
2728 sprintf(info_string, "Created by WIDL version %s at %s\n", PACKAGE_VERSION, ctime(&cur_time));
2729 set_custdata(msft, &midl_info_guid, VT_BSTR, info_string, &msft->typelib_header.CustomDataOffset);
2730 set_custdata(msft, &midl_time_guid, VT_UI4, &cur_time, &msft->typelib_header.CustomDataOffset);
2731 set_custdata(msft, &midl_version_guid, VT_UI4, &version, &msft->typelib_header.CustomDataOffset);
2732
2733 if (typelib->stmts)
2734 LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
2735 add_entry(msft, stmt);
2736
2737 save_all_changes(msft);
2738 free(msft);
2739 return 1;
2740 }