2004-01-07 05:21:27 +01:00
|
|
|
/*
|
|
|
|
* IDL Compiler
|
|
|
|
*
|
|
|
|
* Copyright 2004 Ove Kaaven
|
2006-05-15 21:55:04 +02:00
|
|
|
* Copyright 2006 Jacek Caban for CodeWeavers
|
2004-01-07 05:21:27 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2004-01-07 05:21:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2006-05-15 21:55:04 +02:00
|
|
|
#include "wine/port.h"
|
|
|
|
#include "wine/wpp.h"
|
2004-01-07 05:21:27 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2006-05-15 21:55:04 +02:00
|
|
|
#include <stdarg.h>
|
2008-02-14 16:35:46 +01:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2004-01-07 05:21:27 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2006-05-15 21:55:04 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
|
2004-01-07 05:21:27 +01:00
|
|
|
#include "widl.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "header.h"
|
|
|
|
#include "typelib.h"
|
2006-05-15 21:55:04 +02:00
|
|
|
#include "widltypes.h"
|
|
|
|
#include "typelib_struct.h"
|
2009-01-06 00:33:43 +01:00
|
|
|
#include "typetree.h"
|
2004-01-07 05:21:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* List of oleauto types that should be recognized by name.
|
2008-05-06 16:05:40 +02:00
|
|
|
* (most of) these seem to be intrinsic types in mktyplib.
|
|
|
|
* This table MUST be alphabetically sorted on the kw field.
|
|
|
|
*/
|
2007-08-30 10:17:50 +02:00
|
|
|
static const struct oatype {
|
2004-01-07 05:21:27 +01:00
|
|
|
const char *kw;
|
|
|
|
unsigned short vt;
|
|
|
|
} oatypes[] = {
|
2005-02-08 13:09:42 +01:00
|
|
|
{"BSTR", VT_BSTR},
|
|
|
|
{"CURRENCY", VT_CY},
|
|
|
|
{"DATE", VT_DATE},
|
|
|
|
{"DECIMAL", VT_DECIMAL},
|
|
|
|
{"HRESULT", VT_HRESULT},
|
|
|
|
{"LPSTR", VT_LPSTR},
|
|
|
|
{"LPWSTR", VT_LPWSTR},
|
|
|
|
{"SCODE", VT_ERROR},
|
|
|
|
{"VARIANT", VT_VARIANT},
|
|
|
|
{"VARIANT_BOOL", VT_BOOL}
|
2004-01-07 05:21:27 +01:00
|
|
|
};
|
2004-12-06 21:43:55 +01:00
|
|
|
#define KWP(p) ((const struct oatype *)(p))
|
2004-01-07 05:21:27 +01:00
|
|
|
|
|
|
|
static int kw_cmp_func(const void *s1, const void *s2)
|
|
|
|
{
|
|
|
|
return strcmp(KWP(s1)->kw, KWP(s2)->kw);
|
|
|
|
}
|
|
|
|
|
2007-08-28 05:24:37 +02:00
|
|
|
static unsigned short builtin_vt(const type_t *t)
|
2004-01-07 05:21:27 +01:00
|
|
|
{
|
2007-08-28 05:24:37 +02:00
|
|
|
const char *kw = t->name;
|
2007-08-30 10:17:50 +02:00
|
|
|
struct oatype key;
|
|
|
|
const struct oatype *kwp;
|
2004-01-07 05:21:27 +01:00
|
|
|
key.kw = kw;
|
|
|
|
#ifdef KW_BSEARCH
|
2019-09-26 17:50:59 +02:00
|
|
|
kwp = bsearch(&key, oatypes, ARRAY_SIZE(oatypes), sizeof(oatypes[0]), kw_cmp_func);
|
2004-01-07 05:21:27 +01:00
|
|
|
#else
|
|
|
|
{
|
2005-07-14 14:18:38 +02:00
|
|
|
unsigned int i;
|
2019-09-26 17:50:59 +02:00
|
|
|
for (kwp = NULL, i = 0; i < ARRAY_SIZE(oatypes); i++)
|
2004-01-07 05:21:27 +01:00
|
|
|
if (!kw_cmp_func(&key, &oatypes[i])) {
|
|
|
|
kwp = &oatypes[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (kwp) {
|
|
|
|
return kwp->vt;
|
|
|
|
}
|
2007-08-28 05:24:37 +02:00
|
|
|
if (is_string_type (t->attrs, t))
|
2009-01-06 00:34:58 +01:00
|
|
|
{
|
2009-02-23 14:48:21 +01:00
|
|
|
const type_t *elem_type;
|
2009-01-06 00:34:58 +01:00
|
|
|
if (is_array(t))
|
2019-07-05 23:51:31 +02:00
|
|
|
elem_type = type_array_get_element_type(t);
|
2009-01-06 00:34:58 +01:00
|
|
|
else
|
2019-07-05 23:51:32 +02:00
|
|
|
elem_type = type_pointer_get_ref_type(t);
|
2009-02-23 14:48:21 +01:00
|
|
|
if (type_get_type(elem_type) == TYPE_BASIC)
|
|
|
|
{
|
2009-03-08 00:24:00 +01:00
|
|
|
switch (type_basic_get_type(elem_type))
|
2007-08-28 05:24:37 +02:00
|
|
|
{
|
2009-03-08 00:24:00 +01:00
|
|
|
case TYPE_BASIC_CHAR: return VT_LPSTR;
|
|
|
|
case TYPE_BASIC_WCHAR: return VT_LPWSTR;
|
2007-08-28 05:24:37 +02:00
|
|
|
default: break;
|
|
|
|
}
|
2009-02-23 14:48:21 +01:00
|
|
|
}
|
2009-01-06 00:34:58 +01:00
|
|
|
}
|
2004-01-07 05:21:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int match(const char*n, const char*m)
|
|
|
|
{
|
|
|
|
if (!n) return 0;
|
|
|
|
return !strcmp(n, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short get_type_vt(type_t *t)
|
|
|
|
{
|
|
|
|
unsigned short vt;
|
|
|
|
|
2005-01-12 20:28:59 +01:00
|
|
|
chat("get_type_vt: %p type->name %s\n", t, t->name);
|
2004-01-07 05:21:27 +01:00
|
|
|
if (t->name) {
|
2007-08-28 05:24:37 +02:00
|
|
|
vt = builtin_vt(t);
|
2004-01-07 05:21:27 +01:00
|
|
|
if (vt) return vt;
|
|
|
|
}
|
|
|
|
|
2019-08-21 18:18:10 +02:00
|
|
|
if (type_is_alias(t) &&
|
|
|
|
(is_attr(t->attrs, ATTR_PUBLIC) || is_attr(t->attrs, ATTR_WIREMARSHAL)))
|
2006-08-29 23:26:03 +02:00
|
|
|
return VT_USERDEFINED;
|
|
|
|
|
2009-02-23 14:48:21 +01:00
|
|
|
switch (type_get_type(t)) {
|
|
|
|
case TYPE_BASIC:
|
2009-03-05 09:21:51 +01:00
|
|
|
switch (type_basic_get_type(t)) {
|
|
|
|
case TYPE_BASIC_BYTE:
|
2009-02-23 14:48:21 +01:00
|
|
|
return VT_UI1;
|
2009-03-05 09:21:51 +01:00
|
|
|
case TYPE_BASIC_CHAR:
|
|
|
|
case TYPE_BASIC_INT8:
|
|
|
|
if (type_basic_get_sign(t) > 0)
|
|
|
|
return VT_UI1;
|
|
|
|
else
|
|
|
|
return VT_I1;
|
|
|
|
case TYPE_BASIC_WCHAR:
|
2009-02-23 14:48:21 +01:00
|
|
|
return VT_I2; /* mktyplib seems to parse wchar_t as short */
|
2009-03-05 09:21:51 +01:00
|
|
|
case TYPE_BASIC_INT16:
|
|
|
|
if (type_basic_get_sign(t) > 0)
|
|
|
|
return VT_UI2;
|
|
|
|
else
|
|
|
|
return VT_I2;
|
|
|
|
case TYPE_BASIC_INT:
|
|
|
|
if (type_basic_get_sign(t) > 0)
|
|
|
|
return VT_UINT;
|
|
|
|
else
|
|
|
|
return VT_INT;
|
|
|
|
case TYPE_BASIC_INT32:
|
2018-11-01 15:02:28 +01:00
|
|
|
case TYPE_BASIC_LONG:
|
2009-03-05 09:21:51 +01:00
|
|
|
case TYPE_BASIC_ERROR_STATUS_T:
|
|
|
|
if (type_basic_get_sign(t) > 0)
|
|
|
|
return VT_UI4;
|
|
|
|
else
|
|
|
|
return VT_I4;
|
|
|
|
case TYPE_BASIC_INT64:
|
|
|
|
case TYPE_BASIC_HYPER:
|
|
|
|
if (type_basic_get_sign(t) > 0)
|
|
|
|
return VT_UI8;
|
|
|
|
else
|
|
|
|
return VT_I8;
|
2009-11-07 15:54:56 +01:00
|
|
|
case TYPE_BASIC_INT3264:
|
2018-11-14 18:18:17 +01:00
|
|
|
if (pointer_size == 8)
|
2009-11-07 15:54:56 +01:00
|
|
|
{
|
|
|
|
if (type_basic_get_sign(t) > 0)
|
|
|
|
return VT_UI8;
|
|
|
|
else
|
|
|
|
return VT_I8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (type_basic_get_sign(t) > 0)
|
|
|
|
return VT_UI4;
|
|
|
|
else
|
|
|
|
return VT_I4;
|
|
|
|
}
|
2009-03-05 09:21:51 +01:00
|
|
|
case TYPE_BASIC_FLOAT:
|
2009-02-23 14:48:21 +01:00
|
|
|
return VT_R4;
|
2009-03-05 09:21:51 +01:00
|
|
|
case TYPE_BASIC_DOUBLE:
|
2009-02-23 14:48:21 +01:00
|
|
|
return VT_R8;
|
2009-03-05 09:21:51 +01:00
|
|
|
case TYPE_BASIC_HANDLE:
|
|
|
|
error("handles can't be used in typelibs\n");
|
2007-03-07 14:32:22 +01:00
|
|
|
}
|
2004-01-07 05:21:27 +01:00
|
|
|
break;
|
2009-02-23 14:48:21 +01:00
|
|
|
|
|
|
|
case TYPE_POINTER:
|
|
|
|
return VT_PTR;
|
|
|
|
|
|
|
|
case TYPE_ARRAY:
|
2009-03-05 09:22:19 +01:00
|
|
|
if (type_array_is_decl_as_ptr(t))
|
|
|
|
{
|
2019-07-05 23:51:31 +02:00
|
|
|
if (match(type_array_get_element_type(t)->name, "SAFEARRAY"))
|
2009-03-05 09:22:19 +01:00
|
|
|
return VT_SAFEARRAY;
|
2018-10-10 01:06:40 +02:00
|
|
|
return VT_PTR;
|
2009-03-05 09:22:19 +01:00
|
|
|
}
|
|
|
|
else
|
2018-10-10 01:06:40 +02:00
|
|
|
return VT_CARRAY;
|
2009-02-23 14:48:21 +01:00
|
|
|
|
|
|
|
case TYPE_INTERFACE:
|
2005-01-14 17:50:16 +01:00
|
|
|
if(match(t->name, "IUnknown"))
|
|
|
|
return VT_UNKNOWN;
|
|
|
|
if(match(t->name, "IDispatch"))
|
|
|
|
return VT_DISPATCH;
|
|
|
|
return VT_USERDEFINED;
|
2005-06-09 11:45:26 +02:00
|
|
|
|
2009-02-23 14:48:21 +01:00
|
|
|
case TYPE_ENUM:
|
|
|
|
case TYPE_STRUCT:
|
|
|
|
case TYPE_COCLASS:
|
|
|
|
case TYPE_MODULE:
|
|
|
|
case TYPE_UNION:
|
|
|
|
case TYPE_ENCAPSULATED_UNION:
|
2005-01-06 21:45:21 +01:00
|
|
|
return VT_USERDEFINED;
|
2009-02-23 14:48:21 +01:00
|
|
|
|
|
|
|
case TYPE_VOID:
|
2009-01-06 00:34:08 +01:00
|
|
|
return VT_VOID;
|
2009-02-23 14:48:21 +01:00
|
|
|
|
|
|
|
case TYPE_ALIAS:
|
|
|
|
/* aliases should be filtered out by the type_get_type call above */
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_FUNCTION:
|
|
|
|
error("get_type_vt: functions not supported\n");
|
|
|
|
break;
|
2009-11-07 15:55:09 +01:00
|
|
|
|
|
|
|
case TYPE_BITFIELD:
|
|
|
|
error("get_type_vt: bitfields not supported\n");
|
|
|
|
break;
|
2004-01-07 05:21:27 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-14 01:34:53 +02:00
|
|
|
static void tlb_read(int fd, void *buf, int count)
|
2006-05-15 21:55:04 +02:00
|
|
|
{
|
|
|
|
if(read(fd, buf, count) < count)
|
|
|
|
error("error while reading importlib.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tlb_lseek(int fd, off_t offset)
|
|
|
|
{
|
|
|
|
if(lseek(fd, offset, SEEK_SET) == -1)
|
|
|
|
error("lseek failed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void msft_read_guid(int fd, MSFT_SegDir *segdir, int offset, GUID *guid)
|
|
|
|
{
|
|
|
|
tlb_lseek(fd, segdir->pGuidTab.offset+offset);
|
|
|
|
tlb_read(fd, guid, sizeof(GUID));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_msft_importlib(importlib_t *importlib, int fd)
|
|
|
|
{
|
|
|
|
MSFT_Header header;
|
|
|
|
MSFT_SegDir segdir;
|
|
|
|
int *typeinfo_offs;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
importlib->allocated = 0;
|
|
|
|
|
|
|
|
tlb_lseek(fd, 0);
|
|
|
|
tlb_read(fd, &header, sizeof(header));
|
|
|
|
|
|
|
|
importlib->version = header.version;
|
|
|
|
|
|
|
|
typeinfo_offs = xmalloc(header.nrtypeinfos*sizeof(INT));
|
|
|
|
tlb_read(fd, typeinfo_offs, header.nrtypeinfos*sizeof(INT));
|
|
|
|
tlb_read(fd, &segdir, sizeof(segdir));
|
|
|
|
|
|
|
|
msft_read_guid(fd, &segdir, header.posguid, &importlib->guid);
|
|
|
|
|
|
|
|
importlib->ntypeinfos = header.nrtypeinfos;
|
|
|
|
importlib->importinfos = xmalloc(importlib->ntypeinfos*sizeof(importinfo_t));
|
|
|
|
|
|
|
|
for(i=0; i < importlib->ntypeinfos; i++) {
|
|
|
|
MSFT_TypeInfoBase base;
|
|
|
|
MSFT_NameIntro nameintro;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
tlb_lseek(fd, sizeof(MSFT_Header) + header.nrtypeinfos*sizeof(INT) + sizeof(MSFT_SegDir)
|
|
|
|
+ typeinfo_offs[i]);
|
|
|
|
tlb_read(fd, &base, sizeof(base));
|
|
|
|
|
|
|
|
importlib->importinfos[i].importlib = importlib;
|
|
|
|
importlib->importinfos[i].flags = (base.typekind&0xf)<<24;
|
|
|
|
importlib->importinfos[i].offset = -1;
|
|
|
|
importlib->importinfos[i].id = i;
|
|
|
|
|
|
|
|
if(base.posguid != -1) {
|
|
|
|
importlib->importinfos[i].flags |= MSFT_IMPINFO_OFFSET_IS_GUID;
|
|
|
|
msft_read_guid(fd, &segdir, base.posguid, &importlib->importinfos[i].guid);
|
|
|
|
}
|
2006-08-26 21:39:44 +02:00
|
|
|
else memset( &importlib->importinfos[i].guid, 0, sizeof(importlib->importinfos[i].guid));
|
2006-05-15 21:55:04 +02:00
|
|
|
|
|
|
|
tlb_lseek(fd, segdir.pNametab.offset + base.NameOffset);
|
|
|
|
tlb_read(fd, &nameintro, sizeof(nameintro));
|
|
|
|
|
|
|
|
len = nameintro.namelen & 0xff;
|
|
|
|
|
|
|
|
importlib->importinfos[i].name = xmalloc(len+1);
|
|
|
|
tlb_read(fd, importlib->importinfos[i].name, len);
|
|
|
|
importlib->importinfos[i].name[len] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(typeinfo_offs);
|
|
|
|
}
|
|
|
|
|
2017-06-16 15:25:48 +02:00
|
|
|
static int open_typelib(const char *name)
|
|
|
|
{
|
|
|
|
char *file_name;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
file_name = wpp_find_include(name, NULL);
|
|
|
|
if(!file_name)
|
|
|
|
return open(name, O_RDONLY | O_BINARY );
|
|
|
|
|
|
|
|
fd = open(file_name, O_RDONLY | O_BINARY );
|
|
|
|
free(file_name);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2006-05-15 21:55:04 +02:00
|
|
|
static void read_importlib(importlib_t *importlib)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
INT magic;
|
|
|
|
|
2017-06-16 15:25:48 +02:00
|
|
|
fd = open_typelib(importlib->name);
|
|
|
|
|
|
|
|
/* widl extension: if importlib name has no .tlb extension, try using .tlb */
|
|
|
|
if(fd < 0) {
|
|
|
|
const char *p = strrchr(importlib->name, '.');
|
|
|
|
size_t len = p ? p - importlib->name : strlen(importlib->name);
|
|
|
|
if(strcmp(importlib->name + len, ".tlb")) {
|
|
|
|
char *tlb_name = xmalloc(len + 5);
|
|
|
|
memcpy(tlb_name, importlib->name, len);
|
|
|
|
strcpy(tlb_name + len, ".tlb");
|
|
|
|
fd = open_typelib(tlb_name);
|
|
|
|
free(tlb_name);
|
|
|
|
}
|
2006-05-15 21:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(fd < 0)
|
2017-06-16 15:25:48 +02:00
|
|
|
error("Could not find importlib %s.\n", importlib->name);
|
2006-05-15 21:55:04 +02:00
|
|
|
|
|
|
|
tlb_read(fd, &magic, sizeof(magic));
|
|
|
|
|
|
|
|
switch(magic) {
|
|
|
|
case MSFT_MAGIC:
|
|
|
|
read_msft_importlib(importlib, fd);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Wrong or unsupported typelib magic %x\n", magic);
|
|
|
|
};
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:47:54 +02:00
|
|
|
void add_importlib(const char *name, typelib_t *typelib)
|
2006-04-21 12:20:56 +02:00
|
|
|
{
|
2006-05-15 21:55:04 +02:00
|
|
|
importlib_t *importlib;
|
|
|
|
|
2006-04-21 12:20:56 +02:00
|
|
|
if(!typelib) return;
|
|
|
|
|
2007-01-22 14:10:30 +01:00
|
|
|
LIST_FOR_EACH_ENTRY( importlib, &typelib->importlibs, importlib_t, entry )
|
2006-05-18 22:42:51 +02:00
|
|
|
if(!strcmp(name, importlib->name))
|
|
|
|
return;
|
|
|
|
|
|
|
|
chat("add_importlib: %s\n", name);
|
|
|
|
|
2006-05-15 21:55:04 +02:00
|
|
|
importlib = xmalloc(sizeof(*importlib));
|
2006-08-26 21:39:44 +02:00
|
|
|
memset( importlib, 0, sizeof(*importlib) );
|
2006-05-15 21:55:04 +02:00
|
|
|
importlib->name = xstrdup(name);
|
|
|
|
|
|
|
|
read_importlib(importlib);
|
2007-01-22 14:10:30 +01:00
|
|
|
list_add_head( &typelib->importlibs, &importlib->entry );
|
2006-04-21 12:20:56 +02:00
|
|
|
}
|