user32: Clear %edi register when calling window procedure.
[wine.git] / tools / widl / register.c
blob1c369e7c5eebc899c8eb1839a9ce5b6b0ef6e16f
1 /*
2 * Generation of dll registration scripts
4 * Copyright 2010 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <ctype.h>
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
36 #include "typegen.h"
37 #include "typelib.h"
39 static int indent;
41 static const char *format_uuid( const UUID *uuid )
43 static char buffer[40];
44 sprintf( buffer, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
45 uuid->Data1, uuid->Data2, uuid->Data3,
46 uuid->Data4[0], uuid->Data4[1], uuid->Data4[2], uuid->Data4[3],
47 uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7] );
48 return buffer;
51 static const char *get_coclass_threading( const type_t *class )
53 static const char * const models[] =
55 NULL,
56 "Apartment", /* THREADING_APARTMENT */
57 "Neutral", /* THREADING_NEUTRAL */
58 "Single", /* THREADING_SINGLE */
59 "Free", /* THREADING_FREE */
60 "Both", /* THREADING_BOTH */
62 return models[get_attrv( class->attrs, ATTR_THREADING )];
65 static const type_t *find_ps_factory( const statement_list_t *stmts )
67 const statement_t *stmt;
69 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
71 if (stmt->type == STMT_TYPE)
73 const type_t *type = stmt->u.type;
74 if (type_get_type(type) == TYPE_COCLASS && !strcmp( type->name, "PSFactoryBuffer" ))
75 return type;
78 return NULL;
81 static void write_interface( const type_t *iface, const type_t *ps_factory )
83 const UUID *uuid = get_attrp( iface->attrs, ATTR_UUID );
84 const UUID *ps_uuid = get_attrp( ps_factory->attrs, ATTR_UUID );
86 if (!uuid) return;
87 if (!is_object( iface )) return;
88 if (!type_iface_get_inherit(iface)) /* special case for IUnknown */
90 put_str( indent, "'%s' = s '%s'\n", format_uuid( uuid ), iface->name );
91 return;
93 if (is_local( iface->attrs )) return;
94 put_str( indent, "'%s' = s '%s'\n", format_uuid( uuid ), iface->name );
95 put_str( indent, "{\n" );
96 indent++;
97 put_str( indent, "NumMethods = s %u\n", count_methods( iface ));
98 put_str( indent, "ProxyStubClsid32 = s '%s'\n", format_uuid( ps_uuid ));
99 indent--;
100 put_str( indent, "}\n" );
103 static void write_interfaces( const statement_list_t *stmts, const type_t *ps_factory )
105 const statement_t *stmt;
107 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
109 if (stmt->type == STMT_TYPE && type_get_type( stmt->u.type ) == TYPE_INTERFACE)
110 write_interface( stmt->u.type, ps_factory );
114 static void write_typelib_interface( const type_t *iface, const typelib_t *typelib )
116 const UUID *typelib_uuid = get_attrp( typelib->attrs, ATTR_UUID );
117 const UUID *uuid = get_attrp( iface->attrs, ATTR_UUID );
118 unsigned int version = get_attrv( typelib->attrs, ATTR_VERSION );
120 if (!uuid) return;
121 if (!is_object( iface )) return;
122 if (!is_attr( iface->attrs, ATTR_OLEAUTOMATION ) && !is_attr( iface->attrs, ATTR_DISPINTERFACE ))
123 return;
124 put_str( indent, "'%s' = s '%s'\n", format_uuid( uuid ), iface->name );
125 put_str( indent, "{\n" );
126 indent++;
127 put_str( indent, "ProxyStubClsid = s '{00020424-0000-0000-C000-000000000046}'\n" );
128 put_str( indent, "ProxyStubClsid32 = s '{00020424-0000-0000-C000-000000000046}'\n" );
129 if (version)
130 put_str( indent, "TypeLib = s '%s' { val Version = s '%u.%u' }\n",
131 format_uuid( typelib_uuid ), MAJORVERSION(version), MINORVERSION(version) );
132 else
133 put_str( indent, "TypeLib = s '%s'", format_uuid( typelib_uuid ));
134 indent--;
135 put_str( indent, "}\n" );
138 static void write_typelib_interfaces( const typelib_t *typelib )
140 const statement_t *stmt;
142 if (typelib->stmts) LIST_FOR_EACH_ENTRY( stmt, typelib->stmts, const statement_t, entry )
144 if (stmt->type == STMT_TYPE && type_get_type( stmt->u.type ) == TYPE_INTERFACE)
145 write_typelib_interface( stmt->u.type, typelib );
149 static int write_coclass( const type_t *class, const typelib_t *typelib )
151 const UUID *uuid = get_attrp( class->attrs, ATTR_UUID );
152 const char *descr = get_attrp( class->attrs, ATTR_HELPSTRING );
153 const char *progid = get_attrp( class->attrs, ATTR_PROGID );
154 const char *vi_progid = get_attrp( class->attrs, ATTR_VIPROGID );
155 const char *threading = get_coclass_threading( class );
156 unsigned int version = get_attrv( class->attrs, ATTR_VERSION );
158 if (!uuid) return 0;
159 if (typelib && !threading && !progid) return 0;
160 if (!descr) descr = class->name;
162 put_str( indent, "'%s' = s '%s'\n", format_uuid( uuid ), descr );
163 put_str( indent++, "{\n" );
164 if (threading) put_str( indent, "InprocServer32 = s '%%MODULE%%' { val ThreadingModel = s '%s' }\n",
165 threading );
166 if (progid) put_str( indent, "ProgId = s '%s'\n", progid );
167 if (typelib)
169 const UUID *typelib_uuid = get_attrp( typelib->attrs, ATTR_UUID );
170 put_str( indent, "TypeLib = s '%s'\n", format_uuid( typelib_uuid ));
171 if (!version) version = get_attrv( typelib->attrs, ATTR_VERSION );
173 if (version) put_str( indent, "Version = s '%u.%u'\n", MAJORVERSION(version), MINORVERSION(version) );
174 if (vi_progid) put_str( indent, "VersionIndependentProgId = s '%s'\n", vi_progid );
175 put_str( --indent, "}\n" );
176 return 1;
179 static void write_coclasses( const statement_list_t *stmts, const typelib_t *typelib )
181 const statement_t *stmt;
183 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
185 if (stmt->type == STMT_TYPE)
187 const type_t *type = stmt->u.type;
188 if (type_get_type(type) == TYPE_COCLASS) write_coclass( type, typelib );
193 static int write_progid( const type_t *class )
195 const UUID *uuid = get_attrp( class->attrs, ATTR_UUID );
196 const char *descr = get_attrp( class->attrs, ATTR_HELPSTRING );
197 const char *progid = get_attrp( class->attrs, ATTR_PROGID );
198 const char *vi_progid = get_attrp( class->attrs, ATTR_VIPROGID );
200 if (!uuid) return 0;
201 if (!descr) descr = class->name;
203 if (progid)
205 put_str( indent, "'%s' = s '%s'\n", progid, descr );
206 put_str( indent++, "{\n" );
207 put_str( indent, "CLSID = s '%s'\n", format_uuid( uuid ) );
208 put_str( --indent, "}\n" );
210 if (vi_progid)
212 put_str( indent, "'%s' = s '%s'\n", vi_progid, descr );
213 put_str( indent++, "{\n" );
214 put_str( indent, "CLSID = s '%s'\n", format_uuid( uuid ) );
215 if (progid && strcmp( progid, vi_progid )) put_str( indent, "CurVer = s '%s'\n", progid );
216 put_str( --indent, "}\n" );
218 return 1;
221 static void write_progids( const statement_list_t *stmts )
223 const statement_t *stmt;
225 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
227 if (stmt->type == STMT_TYPE)
229 const type_t *type = stmt->u.type;
230 if (type_get_type(type) == TYPE_COCLASS) write_progid( type );
235 void write_regscript( const statement_list_t *stmts )
237 const type_t *ps_factory;
239 if (!do_regscript) return;
240 if (do_everything && !need_proxy_file( stmts )) return;
242 init_output_buffer();
244 put_str( indent, "HKCR\n" );
245 put_str( indent++, "{\n" );
247 put_str( indent, "NoRemove Interface\n" );
248 put_str( indent++, "{\n" );
249 ps_factory = find_ps_factory( stmts );
250 if (ps_factory) write_interfaces( stmts, ps_factory );
251 put_str( --indent, "}\n" );
253 put_str( indent, "NoRemove CLSID\n" );
254 put_str( indent++, "{\n" );
255 write_coclasses( stmts, NULL );
256 put_str( --indent, "}\n" );
258 write_progids( stmts );
259 put_str( --indent, "}\n" );
261 if (strendswith( regscript_name, ".res" )) /* create a binary resource file */
263 add_output_to_resources( "WINE_REGISTRY", regscript_token );
264 flush_output_resources( regscript_name );
266 else
268 FILE *f = fopen( regscript_name, "w" );
269 if (!f) error( "Could not open %s for output\n", regscript_name );
270 if (fwrite( output_buffer, 1, output_buffer_pos, f ) != output_buffer_pos)
271 error( "Failed to write to %s\n", regscript_name );
272 if (fclose( f ))
273 error( "Failed to write to %s\n", regscript_name );
277 void write_typelib_regscript( const statement_list_t *stmts )
279 const statement_t *stmt;
280 unsigned int count = 0;
282 if (!do_typelib) return;
283 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
285 if (stmt->type != STMT_LIBRARY) continue;
286 if (count && !strendswith( typelib_name, ".res" ))
287 error( "Cannot store multiple typelibs into %s\n", typelib_name );
288 else
289 create_msft_typelib( stmt->u.lib );
290 count++;
292 if (count && strendswith( typelib_name, ".res" )) flush_output_resources( typelib_name );
295 void output_typelib_regscript( const typelib_t *typelib )
297 const UUID *typelib_uuid = get_attrp( typelib->attrs, ATTR_UUID );
298 const char *descr = get_attrp( typelib->attrs, ATTR_HELPSTRING );
299 const expr_t *lcid_expr = get_attrp( typelib->attrs, ATTR_LIBLCID );
300 unsigned int version = get_attrv( typelib->attrs, ATTR_VERSION );
301 unsigned int flags = 0;
302 char id_part[12] = "";
303 char *resname = typelib_name;
304 expr_t *expr;
306 if (is_attr( typelib->attrs, ATTR_RESTRICTED )) flags |= 1; /* LIBFLAG_FRESTRICTED */
307 if (is_attr( typelib->attrs, ATTR_CONTROL )) flags |= 2; /* LIBFLAG_FCONTROL */
308 if (is_attr( typelib->attrs, ATTR_HIDDEN )) flags |= 4; /* LIBFLAG_FHIDDEN */
310 put_str( indent, "HKCR\n" );
311 put_str( indent++, "{\n" );
313 put_str( indent, "NoRemove Typelib\n" );
314 put_str( indent++, "{\n" );
315 put_str( indent, "NoRemove '%s'\n", format_uuid( typelib_uuid ));
316 put_str( indent++, "{\n" );
317 put_str( indent, "'%u.%u' = s '%s'\n",
318 MAJORVERSION(version), MINORVERSION(version), descr ? descr : typelib->name );
319 put_str( indent++, "{\n" );
320 expr = get_attrp( typelib->attrs, ATTR_ID );
321 if (expr)
323 sprintf(id_part, "\\%d", expr->cval);
324 resname = xmalloc( strlen(typelib_name) + 20 );
325 sprintf(resname, "%s\\%d", typelib_name, expr->cval);
327 put_str( indent, "'%x' { %s = s '%%MODULE%%%s' }\n",
328 lcid_expr ? lcid_expr->cval : 0, pointer_size == 8 ? "win64" : "win32", id_part );
329 put_str( indent, "FLAGS = s '%u'\n", flags );
330 put_str( --indent, "}\n" );
331 put_str( --indent, "}\n" );
332 put_str( --indent, "}\n" );
334 put_str( indent, "NoRemove Interface\n" );
335 put_str( indent++, "{\n" );
336 write_typelib_interfaces( typelib );
337 put_str( --indent, "}\n" );
339 put_str( indent, "NoRemove CLSID\n" );
340 put_str( indent++, "{\n" );
341 write_coclasses( typelib->stmts, typelib );
342 put_str( --indent, "}\n" );
344 write_progids( typelib->stmts );
345 put_str( --indent, "}\n" );
347 add_output_to_resources( "WINE_REGISTRY", resname );