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
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
|
|
|
|
|
|
|
#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"
|
2004-01-07 05:21:27 +01:00
|
|
|
|
|
|
|
int in_typelib = 0;
|
|
|
|
|
2005-01-05 18:12:25 +01:00
|
|
|
static typelib_t *typelib;
|
2004-01-07 05:21:27 +01:00
|
|
|
|
2006-08-25 03:01:18 +02:00
|
|
|
type_t *duptype(type_t *t, int dupname)
|
|
|
|
{
|
2007-10-09 01:47:28 +02:00
|
|
|
type_t *d = alloc_type();
|
2006-08-25 03:01:18 +02:00
|
|
|
|
|
|
|
*d = *t;
|
|
|
|
if (dupname && t->name)
|
|
|
|
d->name = xstrdup(t->name);
|
|
|
|
|
|
|
|
d->orig = t;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
type_t *alias(type_t *t, const char *name)
|
|
|
|
{
|
|
|
|
type_t *a = duptype(t, 0);
|
|
|
|
|
|
|
|
a->name = xstrdup(name);
|
|
|
|
a->kind = TKIND_ALIAS;
|
|
|
|
a->attrs = NULL;
|
2007-06-06 03:50:31 +02:00
|
|
|
a->declarray = FALSE;
|
2006-08-25 03:01:18 +02:00
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2006-09-08 01:57:16 +02:00
|
|
|
int is_ptr(const type_t *t)
|
2006-08-29 23:27:27 +02:00
|
|
|
{
|
|
|
|
unsigned char c = t->type;
|
|
|
|
return c == RPC_FC_RP
|
|
|
|
|| c == RPC_FC_UP
|
|
|
|
|| c == RPC_FC_FP
|
2006-09-02 01:35:49 +02:00
|
|
|
|| c == RPC_FC_OP;
|
2006-08-29 23:27:27 +02:00
|
|
|
}
|
|
|
|
|
2007-06-06 03:50:31 +02:00
|
|
|
int is_array(const type_t *t)
|
|
|
|
{
|
|
|
|
switch (t->type)
|
|
|
|
{
|
|
|
|
case RPC_FC_SMFARRAY:
|
|
|
|
case RPC_FC_LGFARRAY:
|
|
|
|
case RPC_FC_SMVARRAY:
|
|
|
|
case RPC_FC_LGVARRAY:
|
|
|
|
case RPC_FC_CARRAY:
|
|
|
|
case RPC_FC_CVARRAY:
|
|
|
|
case RPC_FC_BOGUS_ARRAY:
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
#define NTYPES (sizeof(oatypes)/sizeof(oatypes[0]))
|
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
|
|
|
|
kwp = bsearch(&key, oatypes, NTYPES, sizeof(oatypes[0]), kw_cmp_func);
|
|
|
|
#else
|
|
|
|
{
|
2005-07-14 14:18:38 +02:00
|
|
|
unsigned int i;
|
2004-01-07 05:21:27 +01:00
|
|
|
for (kwp=NULL, i=0; i < NTYPES; i++)
|
|
|
|
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))
|
|
|
|
switch (t->ref->type)
|
|
|
|
{
|
|
|
|
case RPC_FC_CHAR: return VT_LPSTR;
|
|
|
|
case RPC_FC_WCHAR: return VT_LPWSTR;
|
|
|
|
default: break;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2008-04-26 10:51:28 +02:00
|
|
|
if (t->kind == TKIND_ALIAS && is_attr(t->attrs, ATTR_PUBLIC))
|
2006-08-29 23:26:03 +02:00
|
|
|
return VT_USERDEFINED;
|
|
|
|
|
2004-01-07 05:21:27 +01:00
|
|
|
switch (t->type) {
|
|
|
|
case RPC_FC_BYTE:
|
|
|
|
case RPC_FC_USMALL:
|
|
|
|
return VT_UI1;
|
|
|
|
case RPC_FC_CHAR:
|
|
|
|
case RPC_FC_SMALL:
|
|
|
|
return VT_I1;
|
|
|
|
case RPC_FC_WCHAR:
|
|
|
|
return VT_I2; /* mktyplib seems to parse wchar_t as short */
|
|
|
|
case RPC_FC_SHORT:
|
|
|
|
return VT_I2;
|
|
|
|
case RPC_FC_USHORT:
|
|
|
|
return VT_UI2;
|
|
|
|
case RPC_FC_LONG:
|
2006-08-25 03:01:18 +02:00
|
|
|
if (match(t->name, "int")) return VT_INT;
|
2004-01-07 05:21:27 +01:00
|
|
|
return VT_I4;
|
|
|
|
case RPC_FC_ULONG:
|
2006-08-25 03:01:18 +02:00
|
|
|
if (match(t->name, "int")) return VT_UINT;
|
2004-01-07 05:21:27 +01:00
|
|
|
return VT_UI4;
|
|
|
|
case RPC_FC_HYPER:
|
|
|
|
if (t->sign < 0) return VT_UI8;
|
2006-08-25 03:01:18 +02:00
|
|
|
if (match(t->name, "MIDL_uhyper")) return VT_UI8;
|
2004-01-07 05:21:27 +01:00
|
|
|
return VT_I8;
|
|
|
|
case RPC_FC_FLOAT:
|
|
|
|
return VT_R4;
|
|
|
|
case RPC_FC_DOUBLE:
|
|
|
|
return VT_R8;
|
|
|
|
case RPC_FC_RP:
|
|
|
|
case RPC_FC_UP:
|
|
|
|
case RPC_FC_OP:
|
|
|
|
case RPC_FC_FP:
|
2007-08-30 01:27:20 +02:00
|
|
|
case RPC_FC_CARRAY:
|
|
|
|
case RPC_FC_CVARRAY:
|
2005-01-12 20:28:59 +01:00
|
|
|
if(t->ref)
|
2007-03-07 14:32:22 +01:00
|
|
|
{
|
|
|
|
if (match(t->ref->name, "SAFEARRAY"))
|
|
|
|
return VT_SAFEARRAY;
|
2005-01-12 20:28:59 +01:00
|
|
|
return VT_PTR;
|
2007-03-07 14:32:22 +01:00
|
|
|
}
|
2005-01-12 20:28:59 +01:00
|
|
|
|
2004-01-07 05:21:27 +01:00
|
|
|
error("get_type_vt: unknown-deref-type: %d\n", t->ref->type);
|
|
|
|
break;
|
2005-01-14 17:50:16 +01:00
|
|
|
case RPC_FC_IP:
|
|
|
|
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
|
|
|
|
2005-02-14 12:05:41 +01:00
|
|
|
case RPC_FC_ENUM16:
|
2005-01-06 21:45:21 +01:00
|
|
|
case RPC_FC_STRUCT:
|
2005-01-28 14:59:08 +01:00
|
|
|
case RPC_FC_PSTRUCT:
|
2005-06-09 11:45:26 +02:00
|
|
|
case RPC_FC_CSTRUCT:
|
|
|
|
case RPC_FC_CPSTRUCT:
|
|
|
|
case RPC_FC_CVSTRUCT:
|
|
|
|
case RPC_FC_BOGUS_STRUCT:
|
2008-04-24 20:06:28 +02:00
|
|
|
case RPC_FC_COCLASS:
|
2005-01-06 21:45:21 +01:00
|
|
|
return VT_USERDEFINED;
|
2005-01-12 20:28:59 +01:00
|
|
|
case 0:
|
2006-08-29 23:26:03 +02:00
|
|
|
return t->kind == TKIND_PRIMITIVE ? VT_VOID : VT_USERDEFINED;
|
2004-01-07 05:21:27 +01:00
|
|
|
default:
|
2005-06-09 11:45:26 +02:00
|
|
|
error("get_type_vt: unknown type: 0x%02x\n", t->type);
|
2004-01-07 05:21:27 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-24 20:06:28 +02:00
|
|
|
void start_typelib(typelib_t *typelib_type)
|
2004-01-07 05:21:27 +01:00
|
|
|
{
|
2005-01-05 18:12:25 +01:00
|
|
|
in_typelib++;
|
2005-01-26 20:40:47 +01:00
|
|
|
if (!do_typelib) return;
|
2005-01-05 18:12:25 +01:00
|
|
|
|
2008-04-24 20:06:28 +02:00
|
|
|
typelib = typelib_type;
|
2005-01-05 18:12:25 +01:00
|
|
|
typelib->filename = xstrdup(typelib_name);
|
2004-01-07 05:21:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void end_typelib(void)
|
|
|
|
{
|
2005-01-05 18:12:25 +01:00
|
|
|
in_typelib--;
|
|
|
|
if (!typelib) return;
|
|
|
|
|
2005-01-06 21:45:21 +01:00
|
|
|
create_msft_typelib(typelib);
|
2004-01-07 05:21:27 +01:00
|
|
|
}
|
|
|
|
|
2006-08-29 23:26:43 +02:00
|
|
|
void add_typelib_entry(type_t *t)
|
2004-01-07 05:21:27 +01:00
|
|
|
{
|
2005-01-05 18:12:25 +01:00
|
|
|
typelib_entry_t *entry;
|
|
|
|
if (!typelib) return;
|
2004-01-07 05:21:27 +01:00
|
|
|
|
2006-08-29 23:26:43 +02:00
|
|
|
chat("add kind %i: %s\n", t->kind, t->name);
|
2005-01-05 18:12:25 +01:00
|
|
|
entry = xmalloc(sizeof(*entry));
|
2006-08-29 23:26:43 +02:00
|
|
|
entry->type = t;
|
2007-01-22 14:10:30 +01:00
|
|
|
list_add_tail( &typelib->entries, &entry->entry );
|
2004-01-07 05:21:27 +01:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_importlib(importlib_t *importlib)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
INT magic;
|
|
|
|
char *file_name;
|
|
|
|
|
|
|
|
file_name = wpp_find_include(importlib->name, NULL);
|
|
|
|
if(file_name) {
|
|
|
|
fd = open(file_name, O_RDONLY);
|
|
|
|
free(file_name);
|
|
|
|
}else {
|
|
|
|
fd = open(importlib->name, O_RDONLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fd < 0)
|
|
|
|
error("Could not open importlib %s.\n", importlib->name);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-04-21 12:20:56 +02:00
|
|
|
void add_importlib(const char *name)
|
|
|
|
{
|
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
|
|
|
}
|