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>
|
2004-01-07 05:21:27 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <signal.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)
|
|
|
|
{
|
|
|
|
type_t *d = xmalloc(sizeof *d);
|
|
|
|
|
|
|
|
*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;
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2006-08-29 23:27:27 +02:00
|
|
|
int is_ptr(type_t *t)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-01-07 05:21:27 +01:00
|
|
|
/* List of oleauto types that should be recognized by name.
|
|
|
|
* (most of) these seem to be intrinsic types in mktyplib. */
|
|
|
|
|
|
|
|
static struct oatype {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned short builtin_vt(const char *kw)
|
|
|
|
{
|
|
|
|
struct oatype key, *kwp;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
vt = builtin_vt(t->name);
|
|
|
|
if (vt) return vt;
|
|
|
|
}
|
|
|
|
|
2006-08-29 23:26:03 +02:00
|
|
|
if (t->kind == TKIND_ALIAS && t->attrs)
|
|
|
|
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:
|
2005-01-12 20:28:59 +01:00
|
|
|
if(t->ref)
|
|
|
|
return VT_PTR;
|
|
|
|
|
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:
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short get_var_vt(var_t *v)
|
|
|
|
{
|
|
|
|
unsigned short vt;
|
|
|
|
|
2005-01-12 20:28:59 +01:00
|
|
|
chat("get_var_vt: %p tname %s\n", v, v->tname);
|
2004-01-07 05:21:27 +01:00
|
|
|
if (v->tname) {
|
|
|
|
vt = builtin_vt(v->tname);
|
|
|
|
if (vt) return vt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_type_vt(v->type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_typelib(char *name, attr_t *attrs)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
typelib = xmalloc(sizeof(*typelib));
|
|
|
|
typelib->name = xstrdup(name);
|
|
|
|
typelib->filename = xstrdup(typelib_name);
|
|
|
|
typelib->attrs = attrs;
|
2006-08-26 21:39:44 +02:00
|
|
|
typelib->entry = NULL;
|
|
|
|
typelib->importlibs = NULL;
|
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);
|
2005-01-05 18:12:25 +01:00
|
|
|
return;
|
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;
|
2005-01-05 18:12:25 +01:00
|
|
|
LINK(entry, typelib->entry);
|
|
|
|
typelib->entry = entry;
|
2004-01-07 05:21:27 +01:00
|
|
|
}
|
|
|
|
|
2006-05-15 21:55:04 +02:00
|
|
|
static void tlb_read(int fd, void *buf, size_t count)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2006-05-18 22:42:51 +02:00
|
|
|
for(importlib = typelib->importlibs; importlib; importlib = NEXT_LINK(importlib)) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
LINK(importlib, typelib->importlibs);
|
|
|
|
typelib->importlibs = importlib;
|
2006-04-21 12:20:56 +02:00
|
|
|
}
|