widl: Turn on verbose errors, which gives a little more information in the case of...
[wine.git] / tools / widl / parser.y
blob46418a330495ee109e2f3b2cc739cfca68fec287
1 %{
2 /*
3 * IDL Compiler
5 * Copyright 2002 Ove Kaaven
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.
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.
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
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include <string.h>
30 #ifdef HAVE_ALLOCA_H
31 #include <alloca.h>
32 #endif
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
38 #include "typelib.h"
39 #include "typegen.h"
41 #if defined(YYBYACC)
42 /* Berkeley yacc (byacc) doesn't seem to know about these */
43 /* Some *BSD supplied versions do define these though */
44 # ifndef YYEMPTY
45 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
46 # endif
47 # ifndef YYLEX
48 # define YYLEX yylex()
49 # endif
51 #elif defined(YYBISON)
52 /* Bison was used for original development */
53 /* #define YYEMPTY -2 */
54 /* #define YYLEX yylex() */
56 #else
57 /* No yacc we know yet */
58 # if !defined(YYEMPTY) || !defined(YYLEX)
59 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
60 # elif defined(__GNUC__) /* gcc defines the #warning directive */
61 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
62 /* #else we just take a chance that it works... */
63 # endif
64 #endif
66 #define YYERROR_VERBOSE
68 unsigned char pointer_default = RPC_FC_UP;
69 static int is_object_interface = FALSE;
71 typedef struct list typelist_t;
72 struct typenode {
73 type_t *type;
74 struct list entry;
77 typelist_t incomplete_types = LIST_INIT(incomplete_types);
79 static void add_incomplete(type_t *t);
80 static void fix_incomplete(void);
82 static str_list_t *append_str(str_list_t *list, char *str);
83 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
84 static attr_t *make_attr(enum attr_type type);
85 static attr_t *make_attrv(enum attr_type type, unsigned long val);
86 static attr_t *make_attrp(enum attr_type type, void *val);
87 static expr_t *make_expr(enum expr_type type);
88 static expr_t *make_exprl(enum expr_type type, long val);
89 static expr_t *make_exprd(enum expr_type type, double val);
90 static expr_t *make_exprs(enum expr_type type, char *val);
91 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr);
92 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
93 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
94 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
95 static type_t *make_type(unsigned char type, type_t *ref);
96 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
97 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
98 static void set_type(var_t *v, type_t *type, const pident_t *pident, array_dims_t *arr, int top);
99 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
100 static ifref_t *make_ifref(type_t *iface);
101 static var_list_t *append_var(var_list_t *list, var_t *var);
102 static var_t *make_var(char *name);
103 static pident_list_t *append_pident(pident_list_t *list, pident_t *p);
104 static pident_t *make_pident(var_t *var);
105 static func_list_t *append_func(func_list_t *list, func_t *func);
106 static func_t *make_func(var_t *def, var_list_t *args);
107 static type_t *make_class(char *name);
108 static type_t *make_safearray(type_t *type);
109 static type_t *make_builtin(char *name);
110 static type_t *make_int(int sign);
112 static type_t *reg_type(type_t *type, const char *name, int t);
113 static type_t *reg_typedefs(type_t *type, var_list_t *names, attr_list_t *attrs);
114 static type_t *find_type(const char *name, int t);
115 static type_t *find_type2(char *name, int t);
116 static type_t *get_type(unsigned char type, char *name, int t);
117 static type_t *get_typev(unsigned char type, var_t *name, int t);
118 static int get_struct_type(var_list_t *fields);
120 static var_t *reg_const(var_t *var);
121 static var_t *find_const(char *name, int f);
123 static void write_libid(const char *name, const attr_list_t *attr);
124 static void write_clsid(type_t *cls);
125 static void write_diid(type_t *iface);
126 static void write_iid(type_t *iface);
128 static int compute_method_indexes(type_t *iface);
129 static char *gen_name(void);
130 static void process_typedefs(var_list_t *names);
131 static void check_arg(var_t *arg);
132 static void check_all_user_types(ifref_list_t *ifaces);
134 #define tsENUM 1
135 #define tsSTRUCT 2
136 #define tsUNION 3
139 %union {
140 attr_t *attr;
141 attr_list_t *attr_list;
142 str_list_t *str_list;
143 expr_t *expr;
144 expr_list_t *expr_list;
145 array_dims_t *array_dims;
146 type_t *type;
147 var_t *var;
148 var_list_t *var_list;
149 pident_t *pident;
150 pident_list_t *pident_list;
151 func_t *func;
152 func_list_t *func_list;
153 ifref_t *ifref;
154 ifref_list_t *ifref_list;
155 char *str;
156 UUID *uuid;
157 unsigned int num;
158 double dbl;
159 interface_info_t ifinfo;
162 %token <str> aIDENTIFIER
163 %token <str> aKNOWNTYPE
164 %token <num> aNUM aHEXNUM
165 %token <dbl> aDOUBLE
166 %token <str> aSTRING
167 %token <uuid> aUUID
168 %token aEOF
169 %token SHL SHR
170 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
171 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
172 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
173 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
174 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
175 %token tDEFAULT
176 %token tDEFAULTCOLLELEM
177 %token tDEFAULTVALUE
178 %token tDEFAULTVTABLE
179 %token tDISPLAYBIND
180 %token tDISPINTERFACE
181 %token tDLLNAME tDOUBLE tDUAL
182 %token tENDPOINT
183 %token tENTRY tENUM tERRORSTATUST
184 %token tEXPLICITHANDLE tEXTERN
185 %token tFALSE
186 %token tFASTCALL
187 %token tFLOAT
188 %token tHANDLE
189 %token tHANDLET
190 %token tHELPCONTEXT tHELPFILE
191 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
192 %token tHIDDEN
193 %token tHYPER tID tIDEMPOTENT
194 %token tIIDIS
195 %token tIMMEDIATEBIND
196 %token tIMPLICITHANDLE
197 %token tIMPORT tIMPORTLIB
198 %token tIN tINLINE
199 %token tINPUTSYNC
200 %token tINT tINT64
201 %token tINTERFACE
202 %token tLCID
203 %token tLENGTHIS tLIBRARY
204 %token tLOCAL
205 %token tLONG
206 %token tMETHODS
207 %token tMODULE
208 %token tNONBROWSABLE
209 %token tNONCREATABLE
210 %token tNONEXTENSIBLE
211 %token tOBJECT tODL tOLEAUTOMATION
212 %token tOPTIONAL
213 %token tOUT
214 %token tPASCAL
215 %token tPOINTERDEFAULT
216 %token tPROPERTIES
217 %token tPROPGET tPROPPUT tPROPPUTREF
218 %token tPTR
219 %token tPUBLIC
220 %token tRANGE
221 %token tREADONLY tREF
222 %token tREQUESTEDIT
223 %token tRESTRICTED
224 %token tRETVAL
225 %token tSAFEARRAY
226 %token tSHORT
227 %token tSIGNED
228 %token tSINGLE
229 %token tSIZEIS tSIZEOF
230 %token tSMALL
231 %token tSOURCE
232 %token tSTDCALL
233 %token tSTRICTCONTEXTHANDLE
234 %token tSTRING tSTRUCT
235 %token tSWITCH tSWITCHIS tSWITCHTYPE
236 %token tTRANSMITAS
237 %token tTRUE
238 %token tTYPEDEF
239 %token tUNION
240 %token tUNIQUE
241 %token tUNSIGNED
242 %token tUUID
243 %token tV1ENUM
244 %token tVARARG
245 %token tVERSION
246 %token tVOID
247 %token tWCHAR tWIREMARSHAL
249 %type <attr> attribute
250 %type <attr_list> m_attributes attributes attrib_list
251 %type <str_list> str_list
252 %type <expr> m_expr expr expr_const
253 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_const
254 %type <array_dims> array array_list
255 %type <ifinfo> interfacehdr
256 %type <type> inherit interface interfacedef interfacedec
257 %type <type> dispinterface dispinterfacehdr dispinterfacedef
258 %type <type> module modulehdr moduledef
259 %type <type> base_type int_std
260 %type <type> enumdef structdef uniondef
261 %type <type> type
262 %type <ifref> coclass_int
263 %type <ifref_list> gbl_statements coclass_ints
264 %type <var> arg field s_field case enum constdef externdef
265 %type <var_list> m_args no_args args fields cases enums enum_list dispint_props
266 %type <var> m_ident t_ident ident
267 %type <pident> pident func_ident direct_ident
268 %type <pident_list> pident_list
269 %type <func> funcdef
270 %type <func_list> int_statements dispint_meths
271 %type <type> coclass coclasshdr coclassdef
272 %type <num> pointer_type version
273 %type <str> libraryhdr callconv
274 %type <uuid> uuid_string
275 %type <num> import_start
277 %left ','
278 %right '?' ':'
279 %left '|'
280 %left '&'
281 %left '-' '+'
282 %left '*' '/'
283 %left SHL SHR
284 %right '~'
285 %right CAST
286 %right PPTR
287 %right NEG
288 %right ADDRESSOF
292 input: gbl_statements { fix_incomplete();
293 check_all_user_types($1);
294 write_proxies($1);
295 write_client($1);
296 write_server($1);
297 write_dlldata($1);
301 gbl_statements: { $$ = NULL; }
302 | gbl_statements interfacedec { $$ = $1; }
303 | gbl_statements interfacedef { $$ = append_ifref( $1, make_ifref($2) ); }
304 | gbl_statements coclass ';' { $$ = $1;
305 reg_type($2, $2->name, 0);
306 if (!parse_only && do_header) write_coclass_forward($2);
308 | gbl_statements coclassdef { $$ = $1;
309 add_typelib_entry($2);
310 reg_type($2, $2->name, 0);
311 if (!parse_only && do_header) write_coclass_forward($2);
313 | gbl_statements moduledef { $$ = $1; add_typelib_entry($2); }
314 | gbl_statements librarydef { $$ = $1; }
315 | gbl_statements statement { $$ = $1; }
318 imp_statements: {}
319 | imp_statements interfacedec { if (!parse_only) add_typelib_entry($2); }
320 | imp_statements interfacedef { if (!parse_only) add_typelib_entry($2); }
321 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
322 | imp_statements coclassdef { if (!parse_only) add_typelib_entry($2);
323 reg_type($2, $2->name, 0);
324 if (!parse_only && do_header) write_coclass_forward($2);
326 | imp_statements moduledef { if (!parse_only) add_typelib_entry($2); }
327 | imp_statements statement {}
328 | imp_statements importlib {}
329 | imp_statements librarydef {}
332 int_statements: { $$ = NULL; }
333 | int_statements funcdef ';' { $$ = append_func( $1, $2 ); }
334 | int_statements statement { $$ = $1; }
337 semicolon_opt:
338 | ';'
341 statement: constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
342 | cppquote {}
343 | enumdef ';' { if (!parse_only && do_header) {
344 write_type_def_or_decl(header, $1, FALSE, NULL);
345 fprintf(header, ";\n\n");
348 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
349 | import {}
350 | structdef ';' { if (!parse_only && do_header) {
351 write_type_def_or_decl(header, $1, FALSE, NULL);
352 fprintf(header, ";\n\n");
355 | typedef ';' {}
356 | uniondef ';' { if (!parse_only && do_header) {
357 write_type_def_or_decl(header, $1, FALSE, NULL);
358 fprintf(header, ";\n\n");
363 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
365 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
366 $$ = do_import($2);
367 if (!$$) yychar = aEOF;
371 import: import_start imp_statements aEOF
372 { if ($1) pop_import(); }
375 importlib: tIMPORTLIB '(' aSTRING ')'
376 semicolon_opt { if(!parse_only) add_importlib($3); }
379 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
381 library_start: attributes libraryhdr '{' { if (!parse_only) start_typelib($2, $1);
382 if (!parse_only && do_header) write_library($2, $1);
383 if (!parse_only && do_idfile) write_libid($2, $1);
386 librarydef: library_start imp_statements '}'
387 semicolon_opt { if (!parse_only) end_typelib(); }
390 m_args: { $$ = NULL; }
391 | args
394 no_args: tVOID { $$ = NULL; }
397 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
398 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
399 | no_args
402 /* split into two rules to get bison to resolve a tVOID conflict */
403 arg: attributes type pident array { $$ = $3->var;
404 $$->attrs = $1;
405 set_type($$, $2, $3, $4, TRUE);
406 free($3);
408 | type pident array { $$ = $2->var;
409 set_type($$, $1, $2, $3, TRUE);
410 free($2);
414 array: { $$ = NULL; }
415 | '[' array_list ']' { $$ = $2; }
416 | '[' '*' ']' { $$ = append_array( NULL, make_expr(EXPR_VOID) ); }
419 array_list: m_expr /* size of first dimension is optional */ { $$ = append_array( NULL, $1 ); }
420 | array_list ',' expr { $$ = append_array( $1, $3 ); }
421 | array_list ']' '[' expr { $$ = append_array( $1, $4 ); }
424 m_attributes: { $$ = NULL; }
425 | attributes
428 attributes:
429 '[' attrib_list ']' { $$ = $2;
430 if (!$$)
431 error_loc("empty attribute lists unsupported\n");
435 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
436 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
437 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
440 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
441 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
444 attribute: { $$ = NULL; }
445 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
446 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
447 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
448 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
449 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
450 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
451 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
452 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
453 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
454 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
455 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
456 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
457 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
458 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
459 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
460 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
461 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
462 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
463 | tDUAL { $$ = make_attr(ATTR_DUAL); }
464 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
465 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
466 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
467 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
468 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
469 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
470 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
471 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
472 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
473 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
474 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
475 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
476 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
477 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
478 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
479 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
480 | tIN { $$ = make_attr(ATTR_IN); }
481 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
482 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
483 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
484 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
485 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
486 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
487 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
488 | tODL { $$ = make_attr(ATTR_ODL); }
489 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
490 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
491 | tOUT { $$ = make_attr(ATTR_OUT); }
492 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
493 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
494 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
495 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
496 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
497 | tRANGE '(' expr_const ',' expr_const ')' { expr_list_t *list = append_expr( NULL, $3 );
498 list = append_expr( list, $5 );
499 $$ = make_attrp(ATTR_RANGE, list); }
500 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
501 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
502 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
503 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
504 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
505 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
506 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
507 | tSTRING { $$ = make_attr(ATTR_STRING); }
508 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
509 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
510 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
511 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
512 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
513 | tVARARG { $$ = make_attr(ATTR_VARARG); }
514 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
515 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
516 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
519 uuid_string:
520 aUUID
521 | aSTRING { if (!is_valid_uuid($1))
522 error_loc("invalid UUID: %s\n", $1);
523 $$ = parse_uuid($1); }
526 callconv: tCDECL { $$ = $<str>1; }
527 | tFASTCALL { $$ = $<str>1; }
528 | tPASCAL { $$ = $<str>1; }
529 | tSTDCALL { $$ = $<str>1; }
532 cases: { $$ = NULL; }
533 | cases case { $$ = append_var( $1, $2 ); }
536 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
537 $$ = $4; if (!$$) $$ = make_var(NULL);
538 $$->attrs = append_attr( $$->attrs, a );
540 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
541 $$ = $3; if (!$$) $$ = make_var(NULL);
542 $$->attrs = append_attr( $$->attrs, a );
546 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
547 set_type($$, $2, NULL, NULL, FALSE);
548 $$->eval = $5;
552 enums: { $$ = NULL; }
553 | enum_list ',' { $$ = $1; }
554 | enum_list
557 enum_list: enum { if (!$1->eval)
558 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
559 $$ = append_var( NULL, $1 );
561 | enum_list ',' enum { if (!$3->eval)
563 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
564 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
566 $$ = append_var( $1, $3 );
570 enum: ident '=' expr_const { $$ = reg_const($1);
571 $$->eval = $3;
572 $$->type = make_int(0);
574 | ident { $$ = reg_const($1);
575 $$->type = make_int(0);
579 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
580 $$->kind = TKIND_ENUM;
581 $$->fields_or_args = $4;
582 $$->defined = TRUE;
583 if(in_typelib)
584 add_typelib_entry($$);
588 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
589 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
593 exprs: { $$ = make_expr(EXPR_VOID); }
594 | expr_list
597 expr_list: expr
598 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
602 m_expr: { $$ = make_expr(EXPR_VOID); }
603 | expr
606 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
607 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
608 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
609 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
610 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
611 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
612 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
613 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
614 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
615 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
616 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
617 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
618 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
619 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
620 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
621 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
622 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
623 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
624 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
625 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
626 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
627 | '(' expr ')' { $$ = $2; }
630 expr_list_const: expr_const { $$ = append_expr( NULL, $1 ); }
631 | expr_list_const ',' expr_const { $$ = append_expr( $1, $3 ); }
634 expr_const: expr { $$ = $1;
635 if (!$$->is_const)
636 error_loc("expression is not constant\n");
640 externdef: tEXTERN tCONST type ident { $$ = $4;
641 set_type($$, $3, NULL, NULL, FALSE);
645 fields: { $$ = NULL; }
646 | fields field { $$ = append_var( $1, $2 ); }
649 field: s_field ';' { $$ = $1; }
650 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
651 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
652 | ';' { $$ = NULL; }
655 s_field: m_attributes type pident array { $$ = $3->var;
656 $$->attrs = $1;
657 set_type($$, $2, $3, $4, FALSE);
658 free($3);
662 funcdef:
663 m_attributes type pident { var_t *v = $3->var;
664 var_list_t *args = $3->args;
665 v->attrs = $1;
666 set_type(v, $2, $3, NULL, FALSE);
667 free($3);
668 $$ = make_func(v, args);
669 if (is_attr(v->attrs, ATTR_IN)) {
670 error_loc("inapplicable attribute [in] for function '%s'\n",$$->def->name);
675 m_ident: { $$ = NULL; }
676 | ident
679 t_ident: { $$ = NULL; }
680 | aIDENTIFIER { $$ = make_var($1); }
681 | aKNOWNTYPE { $$ = make_var($1); }
684 ident: aIDENTIFIER { $$ = make_var($1); }
685 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
686 | aKNOWNTYPE { $$ = make_var($<str>1); }
689 base_type: tBYTE { $$ = make_builtin($<str>1); }
690 | tWCHAR { $$ = make_builtin($<str>1); }
691 | int_std
692 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
693 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
694 switch ($$->type) {
695 case RPC_FC_CHAR: break;
696 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
697 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
698 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
699 case RPC_FC_HYPER:
700 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
702 $$ = alias($$, "MIDL_uhyper");
703 $$->sign = 0;
705 break;
706 default: break;
709 | tUNSIGNED { $$ = make_int(-1); }
710 | tFLOAT { $$ = make_builtin($<str>1); }
711 | tSINGLE { $$ = duptype(find_type("float", 0), 1); }
712 | tDOUBLE { $$ = make_builtin($<str>1); }
713 | tBOOLEAN { $$ = make_builtin($<str>1); }
714 | tERRORSTATUST { $$ = make_builtin($<str>1); }
715 | tHANDLET { $$ = make_builtin($<str>1); }
718 m_int:
719 | tINT
722 int_std: tINT { $$ = make_builtin($<str>1); }
723 | tSHORT m_int { $$ = make_builtin($<str>1); }
724 | tSMALL { $$ = make_builtin($<str>1); }
725 | tLONG m_int { $$ = make_builtin($<str>1); }
726 | tHYPER m_int { $$ = make_builtin($<str>1); }
727 | tINT64 { $$ = make_builtin($<str>1); }
728 | tCHAR { $$ = make_builtin($<str>1); }
731 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
732 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
733 if ($$->defined) error_loc("multiple definition error\n");
734 if ($$->kind != TKIND_COCLASS) error_loc("%s was not declared a coclass\n", $2);
738 coclasshdr: attributes coclass { $$ = $2;
739 $$->attrs = $1;
740 if (!parse_only && do_header)
741 write_coclass($$);
742 if (!parse_only && do_idfile)
743 write_clsid($$);
747 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
748 { $$ = $1;
749 $$->ifaces = $3;
750 $$->defined = TRUE;
754 coclass_ints: { $$ = NULL; }
755 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
758 coclass_int:
759 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
762 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
763 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
766 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
767 is_object_interface = TRUE;
768 $$ = $2;
769 if ($$->defined) error_loc("multiple definition error\n");
770 attrs = make_attr(ATTR_DISPINTERFACE);
771 $$->attrs = append_attr( $1, attrs );
772 $$->ref = find_type("IDispatch", 0);
773 if (!$$->ref) error_loc("IDispatch is undefined\n");
774 $$->defined = TRUE;
775 if (!parse_only && do_header) write_forward($$);
779 dispint_props: tPROPERTIES ':' { $$ = NULL; }
780 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
783 dispint_meths: tMETHODS ':' { $$ = NULL; }
784 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
787 dispinterfacedef: dispinterfacehdr '{'
788 dispint_props
789 dispint_meths
790 '}' { $$ = $1;
791 $$->fields_or_args = $3;
792 $$->funcs = $4;
793 if (!parse_only && do_header) write_dispinterface($$);
794 if (!parse_only && do_idfile) write_diid($$);
796 | dispinterfacehdr
797 '{' interface ';' '}' { $$ = $1;
798 $$->fields_or_args = $3->fields_or_args;
799 $$->funcs = $3->funcs;
800 if (!parse_only && do_header) write_dispinterface($$);
801 if (!parse_only && do_idfile) write_diid($$);
805 inherit: { $$ = NULL; }
806 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
809 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
810 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
813 interfacehdr: attributes interface { $$.interface = $2;
814 $$.old_pointer_default = pointer_default;
815 if (is_attr($1, ATTR_POINTERDEFAULT))
816 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
817 is_object_interface = is_object($1);
818 if ($2->defined) error_loc("multiple definition error\n");
819 $2->attrs = $1;
820 $2->defined = TRUE;
821 if (!parse_only && do_header) write_forward($2);
825 interfacedef: interfacehdr inherit
826 '{' int_statements '}' semicolon_opt { $$ = $1.interface;
827 $$->ref = $2;
828 $$->funcs = $4;
829 compute_method_indexes($$);
830 if (!parse_only && do_header) write_interface($$);
831 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
832 if (!parse_only && do_idfile) write_iid($$);
833 pointer_default = $1.old_pointer_default;
835 /* MIDL is able to import the definition of a base class from inside the
836 * definition of a derived class, I'll try to support it with this rule */
837 | interfacehdr ':' aIDENTIFIER
838 '{' import int_statements '}'
839 semicolon_opt { $$ = $1.interface;
840 $$->ref = find_type2($3, 0);
841 if (!$$->ref) error_loc("base class '%s' not found in import\n", $3);
842 $$->funcs = $6;
843 compute_method_indexes($$);
844 if (!parse_only && do_header) write_interface($$);
845 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
846 if (!parse_only && do_idfile) write_iid($$);
847 pointer_default = $1.old_pointer_default;
849 | dispinterfacedef semicolon_opt { $$ = $1; }
852 interfacedec:
853 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
854 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
857 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
858 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
861 modulehdr: attributes module { $$ = $2;
862 $$->attrs = $1;
866 moduledef: modulehdr '{' int_statements '}'
867 semicolon_opt { $$ = $1;
868 $$->funcs = $3;
869 /* FIXME: if (!parse_only && do_header) write_module($$); */
873 pident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
874 | tCONST pident { $$ = $2; /* FIXME */ }
875 | callconv pident { $$ = $2;
876 if ($$->callconv) parser_warning("multiple calling conventions %s, %s for function %s\n", $$->callconv, $1, $$->var->name);
877 $$->callconv = $1;
879 | direct_ident
882 func_ident: direct_ident '(' m_args ')'
883 { $$ = $1;
884 $1->args = $3;
885 $1->is_func = TRUE;
889 direct_ident: ident { $$ = make_pident($1); }
890 | '(' pident ')' { $$ = $2; }
891 | func_ident { $$ = $1;
892 $$->func_ptr_level = $$->ptr_level;
893 $$->ptr_level = 0;
897 pident_list:
898 pident { $$ = append_pident( NULL, $1 ); }
899 | pident_list ',' pident { $$ = append_pident( $1, $3 ); }
902 pointer_type:
903 tREF { $$ = RPC_FC_RP; }
904 | tUNIQUE { $$ = RPC_FC_UP; }
905 | tPTR { $$ = RPC_FC_FP; }
908 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
909 /* overwrite RPC_FC_STRUCT with a more exact type */
910 $$->type = get_struct_type( $4 );
911 $$->kind = TKIND_RECORD;
912 $$->fields_or_args = $4;
913 $$->defined = TRUE;
914 if(in_typelib)
915 add_typelib_entry($$);
919 type: tVOID { $$ = duptype(find_type("void", 0), 1); }
920 | aKNOWNTYPE { $$ = find_type($1, 0); }
921 | base_type { $$ = $1; }
922 | tCONST type { $$ = duptype($2, 1); $$->is_const = TRUE; }
923 | enumdef { $$ = $1; }
924 | tENUM aIDENTIFIER { $$ = find_type2($2, tsENUM); }
925 | structdef { $$ = $1; }
926 | tSTRUCT aIDENTIFIER { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
927 | uniondef { $$ = $1; }
928 | tUNION aIDENTIFIER { $$ = find_type2($2, tsUNION); }
929 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
932 typedef: tTYPEDEF m_attributes type pident_list { reg_typedefs($3, $4, $2);
933 process_typedefs($4);
937 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
938 $$->kind = TKIND_UNION;
939 $$->fields_or_args = $4;
940 $$->defined = TRUE;
942 | tUNION t_ident
943 tSWITCH '(' s_field ')'
944 m_ident '{' cases '}' { var_t *u = $7;
945 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
946 $$->kind = TKIND_UNION;
947 if (!u) u = make_var( xstrdup("tagged_union") );
948 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
949 u->type->kind = TKIND_UNION;
950 u->type->fields_or_args = $9;
951 u->type->defined = TRUE;
952 $$->fields_or_args = append_var( $$->fields_or_args, $5 );
953 $$->fields_or_args = append_var( $$->fields_or_args, u );
954 $$->defined = TRUE;
958 version:
959 aNUM { $$ = MAKEVERSION($1, 0); }
960 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
965 static void decl_builtin(const char *name, unsigned char type)
967 type_t *t = make_type(type, NULL);
968 t->name = xstrdup(name);
969 reg_type(t, name, 0);
972 static type_t *make_builtin(char *name)
974 /* NAME is strdup'd in the lexer */
975 type_t *t = duptype(find_type(name, 0), 0);
976 t->name = name;
977 return t;
980 static type_t *make_int(int sign)
982 type_t *t = duptype(find_type("int", 0), 1);
984 t->sign = sign;
985 if (sign < 0)
986 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
988 return t;
991 void init_types(void)
993 decl_builtin("void", 0);
994 decl_builtin("byte", RPC_FC_BYTE);
995 decl_builtin("wchar_t", RPC_FC_WCHAR);
996 decl_builtin("int", RPC_FC_LONG); /* win32 */
997 decl_builtin("short", RPC_FC_SHORT);
998 decl_builtin("small", RPC_FC_SMALL);
999 decl_builtin("long", RPC_FC_LONG);
1000 decl_builtin("hyper", RPC_FC_HYPER);
1001 decl_builtin("__int64", RPC_FC_HYPER);
1002 decl_builtin("char", RPC_FC_CHAR);
1003 decl_builtin("float", RPC_FC_FLOAT);
1004 decl_builtin("double", RPC_FC_DOUBLE);
1005 decl_builtin("boolean", RPC_FC_BYTE);
1006 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
1007 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
1010 static str_list_t *append_str(str_list_t *list, char *str)
1012 struct str_list_entry_t *entry;
1014 if (!str) return list;
1015 if (!list)
1017 list = xmalloc( sizeof(*list) );
1018 list_init( list );
1020 entry = xmalloc( sizeof(*entry) );
1021 entry->str = str;
1022 list_add_tail( list, &entry->entry );
1023 return list;
1026 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1028 if (!attr) return list;
1029 if (!list)
1031 list = xmalloc( sizeof(*list) );
1032 list_init( list );
1034 list_add_tail( list, &attr->entry );
1035 return list;
1038 static attr_t *make_attr(enum attr_type type)
1040 attr_t *a = xmalloc(sizeof(attr_t));
1041 a->type = type;
1042 a->u.ival = 0;
1043 return a;
1046 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1048 attr_t *a = xmalloc(sizeof(attr_t));
1049 a->type = type;
1050 a->u.ival = val;
1051 return a;
1054 static attr_t *make_attrp(enum attr_type type, void *val)
1056 attr_t *a = xmalloc(sizeof(attr_t));
1057 a->type = type;
1058 a->u.pval = val;
1059 return a;
1062 static expr_t *make_expr(enum expr_type type)
1064 expr_t *e = xmalloc(sizeof(expr_t));
1065 e->type = type;
1066 e->ref = NULL;
1067 e->u.lval = 0;
1068 e->is_const = FALSE;
1069 e->cval = 0;
1070 return e;
1073 static expr_t *make_exprl(enum expr_type type, long val)
1075 expr_t *e = xmalloc(sizeof(expr_t));
1076 e->type = type;
1077 e->ref = NULL;
1078 e->u.lval = val;
1079 e->is_const = FALSE;
1080 /* check for numeric constant */
1081 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
1082 /* make sure true/false value is valid */
1083 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
1084 e->is_const = TRUE;
1085 e->cval = val;
1087 return e;
1090 static expr_t *make_exprd(enum expr_type type, double val)
1092 expr_t *e = xmalloc(sizeof(expr_t));
1093 e->type = type;
1094 e->ref = NULL;
1095 e->u.dval = val;
1096 e->is_const = TRUE;
1097 e->cval = val;
1098 return e;
1101 static expr_t *make_exprs(enum expr_type type, char *val)
1103 expr_t *e;
1104 e = xmalloc(sizeof(expr_t));
1105 e->type = type;
1106 e->ref = NULL;
1107 e->u.sval = val;
1108 e->is_const = FALSE;
1109 /* check for predefined constants */
1110 if (type == EXPR_IDENTIFIER) {
1111 var_t *c = find_const(val, 0);
1112 if (c) {
1113 e->u.sval = c->name;
1114 free(val);
1115 e->is_const = TRUE;
1116 e->cval = c->eval->cval;
1119 return e;
1122 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr)
1124 expr_t *e;
1125 e = xmalloc(sizeof(expr_t));
1126 e->type = type;
1127 e->ref = expr;
1128 e->u.tref = tref;
1129 e->is_const = FALSE;
1130 /* check for cast of constant expression */
1131 if (type == EXPR_SIZEOF) {
1132 switch (tref->type) {
1133 case RPC_FC_BYTE:
1134 case RPC_FC_CHAR:
1135 case RPC_FC_SMALL:
1136 case RPC_FC_USMALL:
1137 e->is_const = TRUE;
1138 e->cval = 1;
1139 break;
1140 case RPC_FC_WCHAR:
1141 case RPC_FC_USHORT:
1142 case RPC_FC_SHORT:
1143 e->is_const = TRUE;
1144 e->cval = 2;
1145 break;
1146 case RPC_FC_LONG:
1147 case RPC_FC_ULONG:
1148 case RPC_FC_FLOAT:
1149 case RPC_FC_ERROR_STATUS_T:
1150 e->is_const = TRUE;
1151 e->cval = 4;
1152 break;
1153 case RPC_FC_HYPER:
1154 case RPC_FC_DOUBLE:
1155 e->is_const = TRUE;
1156 e->cval = 8;
1157 break;
1160 if (type == EXPR_CAST && expr->is_const) {
1161 e->is_const = TRUE;
1162 e->cval = expr->cval;
1164 return e;
1167 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
1169 expr_t *e;
1170 if (type == EXPR_ADDRESSOF && expr->type != EXPR_IDENTIFIER)
1171 error("address-of operator applied to invalid expression\n");
1172 e = xmalloc(sizeof(expr_t));
1173 e->type = type;
1174 e->ref = expr;
1175 e->u.lval = 0;
1176 e->is_const = FALSE;
1177 /* check for compile-time optimization */
1178 if (expr->is_const) {
1179 e->is_const = TRUE;
1180 switch (type) {
1181 case EXPR_NEG:
1182 e->cval = -expr->cval;
1183 break;
1184 case EXPR_NOT:
1185 e->cval = ~expr->cval;
1186 break;
1187 default:
1188 e->is_const = FALSE;
1189 break;
1192 return e;
1195 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1197 expr_t *e;
1198 e = xmalloc(sizeof(expr_t));
1199 e->type = type;
1200 e->ref = expr1;
1201 e->u.ext = expr2;
1202 e->is_const = FALSE;
1203 /* check for compile-time optimization */
1204 if (expr1->is_const && expr2->is_const) {
1205 e->is_const = TRUE;
1206 switch (type) {
1207 case EXPR_ADD:
1208 e->cval = expr1->cval + expr2->cval;
1209 break;
1210 case EXPR_SUB:
1211 e->cval = expr1->cval - expr2->cval;
1212 break;
1213 case EXPR_MUL:
1214 e->cval = expr1->cval * expr2->cval;
1215 break;
1216 case EXPR_DIV:
1217 e->cval = expr1->cval / expr2->cval;
1218 break;
1219 case EXPR_OR:
1220 e->cval = expr1->cval | expr2->cval;
1221 break;
1222 case EXPR_AND:
1223 e->cval = expr1->cval & expr2->cval;
1224 break;
1225 case EXPR_SHL:
1226 e->cval = expr1->cval << expr2->cval;
1227 break;
1228 case EXPR_SHR:
1229 e->cval = expr1->cval >> expr2->cval;
1230 break;
1231 default:
1232 e->is_const = FALSE;
1233 break;
1236 return e;
1239 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1241 expr_t *e;
1242 e = xmalloc(sizeof(expr_t));
1243 e->type = type;
1244 e->ref = expr1;
1245 e->u.ext = expr2;
1246 e->ext2 = expr3;
1247 e->is_const = FALSE;
1248 /* check for compile-time optimization */
1249 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1250 e->is_const = TRUE;
1251 switch (type) {
1252 case EXPR_COND:
1253 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1254 break;
1255 default:
1256 e->is_const = FALSE;
1257 break;
1260 return e;
1263 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1265 if (!expr) return list;
1266 if (!list)
1268 list = xmalloc( sizeof(*list) );
1269 list_init( list );
1271 list_add_tail( list, &expr->entry );
1272 return list;
1275 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1277 if (!expr) return list;
1278 if (!list)
1280 list = xmalloc( sizeof(*list) );
1281 list_init( list );
1283 list_add_tail( list, &expr->entry );
1284 return list;
1287 static struct list type_pool = LIST_INIT(type_pool);
1288 typedef struct
1290 type_t data;
1291 struct list link;
1292 } type_pool_node_t;
1294 type_t *alloc_type(void)
1296 type_pool_node_t *node = xmalloc(sizeof *node);
1297 list_add_tail(&type_pool, &node->link);
1298 return &node->data;
1301 void set_all_tfswrite(int val)
1303 type_pool_node_t *node;
1304 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1305 node->data.tfswrite = val;
1308 static type_t *make_type(unsigned char type, type_t *ref)
1310 type_t *t = alloc_type();
1311 t->name = NULL;
1312 t->kind = TKIND_PRIMITIVE;
1313 t->type = type;
1314 t->ref = ref;
1315 t->attrs = NULL;
1316 t->orig = NULL;
1317 t->funcs = NULL;
1318 t->fields_or_args = NULL;
1319 t->ifaces = NULL;
1320 t->dim = 0;
1321 t->size_is = NULL;
1322 t->length_is = NULL;
1323 t->typestring_offset = 0;
1324 t->ptrdesc = 0;
1325 t->declarray = FALSE;
1326 t->ignore = (parse_only != 0);
1327 t->is_const = FALSE;
1328 t->sign = 0;
1329 t->defined = FALSE;
1330 t->written = FALSE;
1331 t->user_types_registered = FALSE;
1332 t->tfswrite = FALSE;
1333 t->typelib_idx = -1;
1334 return t;
1337 static void set_type(var_t *v, type_t *type, const pident_t *pident, array_dims_t *arr,
1338 int top)
1340 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1341 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1342 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1343 int ptr_type = ptr_attr;
1344 int sizeless, has_varconf;
1345 expr_t *dim;
1346 type_t *atype, **ptype;
1347 int ptr_level = (pident ? pident->ptr_level : 0);
1349 v->type = type;
1351 if (!ptr_type && top)
1352 ptr_type = RPC_FC_RP;
1354 for ( ; 0 < ptr_level; --ptr_level)
1356 v->type = make_type(pointer_default, v->type);
1357 if (ptr_level == 1 && ptr_type && !arr)
1359 v->type->type = ptr_type;
1360 ptr_type = 0;
1364 if (ptr_type && !arr)
1366 if (is_ptr(v->type))
1368 if (v->type->type != ptr_type)
1370 v->type = duptype(v->type, 1);
1371 v->type->type = ptr_type;
1374 else if (!arr && ptr_attr)
1375 error("%s: pointer attribute applied to non-pointer type\n", v->name);
1378 if (pident && pident->is_func) {
1379 int func_ptr_level = pident->func_ptr_level;
1380 v->type = make_type(RPC_FC_FUNCTION, v->type);
1381 v->type->fields_or_args = pident->args;
1382 if (pident->callconv)
1383 v->type->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, pident->callconv));
1384 else if (is_object_interface) {
1385 static char *stdmethodcalltype;
1386 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1387 v->type->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1389 for (; func_ptr_level > 0; func_ptr_level--)
1390 v->type = make_type(ptr_type, v->type);
1393 sizeless = FALSE;
1394 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1396 if (sizeless)
1397 error("%s: only the first array dimension can be unspecified\n", v->name);
1399 if (dim->is_const)
1401 unsigned int align = 0;
1402 size_t size = type_memsize(v->type, &align);
1404 if (dim->cval <= 0)
1405 error("%s: array dimension must be positive\n", v->name);
1407 if (0xffffffffuL / size < (unsigned long) dim->cval)
1408 error("%s: total array size is too large\n", v->name);
1409 else if (0xffffuL < size * dim->cval)
1410 v->type = make_type(RPC_FC_LGFARRAY, v->type);
1411 else
1412 v->type = make_type(RPC_FC_SMFARRAY, v->type);
1414 else
1416 sizeless = TRUE;
1417 v->type = make_type(RPC_FC_CARRAY, v->type);
1420 v->type->declarray = TRUE;
1421 v->type->dim = dim->cval;
1424 ptype = &v->type;
1425 has_varconf = FALSE;
1426 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1428 if (dim->type != EXPR_VOID)
1430 has_varconf = TRUE;
1431 atype = *ptype = duptype(*ptype, 0);
1433 if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1434 error("%s: cannot specify size_is for a fixed sized array\n", v->name);
1436 if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1437 error("%s: size_is attribute applied to illegal type\n", v->name);
1439 atype->type = RPC_FC_CARRAY;
1440 atype->size_is = dim;
1443 ptype = &(*ptype)->ref;
1444 if (*ptype == NULL)
1445 error("%s: too many expressions in size_is attribute\n", v->name);
1448 ptype = &v->type;
1449 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1451 if (dim->type != EXPR_VOID)
1453 has_varconf = TRUE;
1454 atype = *ptype = duptype(*ptype, 0);
1456 if (atype->type == RPC_FC_SMFARRAY)
1457 atype->type = RPC_FC_SMVARRAY;
1458 else if (atype->type == RPC_FC_LGFARRAY)
1459 atype->type = RPC_FC_LGVARRAY;
1460 else if (atype->type == RPC_FC_CARRAY)
1461 atype->type = RPC_FC_CVARRAY;
1462 else
1463 error("%s: length_is attribute applied to illegal type\n", v->name);
1465 atype->length_is = dim;
1468 ptype = &(*ptype)->ref;
1469 if (*ptype == NULL)
1470 error("%s: too many expressions in length_is attribute\n", v->name);
1473 if (has_varconf && !last_array(v->type))
1475 ptype = &v->type;
1476 for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1478 *ptype = duptype(*ptype, 0);
1479 (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1483 if (is_array(v->type))
1485 const type_t *rt = v->type->ref;
1486 if (is_user_type(rt))
1487 v->type->type = RPC_FC_BOGUS_ARRAY;
1488 else
1489 switch (rt->type)
1491 case RPC_FC_BOGUS_STRUCT:
1492 case RPC_FC_NON_ENCAPSULATED_UNION:
1493 case RPC_FC_ENCAPSULATED_UNION:
1494 case RPC_FC_ENUM16:
1495 v->type->type = RPC_FC_BOGUS_ARRAY;
1496 break;
1497 /* FC_RP should be above, but widl overuses these, and will break things. */
1498 case RPC_FC_UP:
1499 case RPC_FC_RP:
1500 if (rt->ref->type == RPC_FC_IP)
1501 v->type->type = RPC_FC_BOGUS_ARRAY;
1502 break;
1507 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1509 if (!iface) return list;
1510 if (!list)
1512 list = xmalloc( sizeof(*list) );
1513 list_init( list );
1515 list_add_tail( list, &iface->entry );
1516 return list;
1519 static ifref_t *make_ifref(type_t *iface)
1521 ifref_t *l = xmalloc(sizeof(ifref_t));
1522 l->iface = iface;
1523 l->attrs = NULL;
1524 return l;
1527 static var_list_t *append_var(var_list_t *list, var_t *var)
1529 if (!var) return list;
1530 if (!list)
1532 list = xmalloc( sizeof(*list) );
1533 list_init( list );
1535 list_add_tail( list, &var->entry );
1536 return list;
1539 static var_t *make_var(char *name)
1541 var_t *v = xmalloc(sizeof(var_t));
1542 v->name = name;
1543 v->type = NULL;
1544 v->attrs = NULL;
1545 v->eval = NULL;
1546 return v;
1549 static pident_list_t *append_pident(pident_list_t *list, pident_t *p)
1551 if (!p) return list;
1552 if (!list) {
1553 list = xmalloc(sizeof(*list));
1554 list_init(list);
1556 list_add_tail(list, &p->entry);
1557 return list;
1560 static pident_t *make_pident(var_t *var)
1562 pident_t *p = xmalloc(sizeof(*p));
1563 p->var = var;
1564 p->is_func = FALSE;
1565 p->ptr_level = 0;
1566 p->func_ptr_level = 0;
1567 p->args = NULL;
1568 p->callconv = NULL;
1569 return p;
1572 static func_list_t *append_func(func_list_t *list, func_t *func)
1574 if (!func) return list;
1575 if (!list)
1577 list = xmalloc( sizeof(*list) );
1578 list_init( list );
1580 list_add_tail( list, &func->entry );
1581 return list;
1584 static func_t *make_func(var_t *def, var_list_t *args)
1586 func_t *f = xmalloc(sizeof(func_t));
1587 f->def = def;
1588 f->args = args;
1589 f->ignore = parse_only;
1590 f->idx = -1;
1591 return f;
1594 static type_t *make_class(char *name)
1596 type_t *c = make_type(0, NULL);
1597 c->name = name;
1598 c->kind = TKIND_COCLASS;
1599 return c;
1602 static type_t *make_safearray(type_t *type)
1604 type_t *sa = duptype(find_type("SAFEARRAY", 0), 1);
1605 sa->ref = type;
1606 return make_type(pointer_default, sa);
1609 #define HASHMAX 64
1611 static int hash_ident(const char *name)
1613 const char *p = name;
1614 int sum = 0;
1615 /* a simple sum hash is probably good enough */
1616 while (*p) {
1617 sum += *p;
1618 p++;
1620 return sum & (HASHMAX-1);
1623 /***** type repository *****/
1625 struct rtype {
1626 const char *name;
1627 type_t *type;
1628 int t;
1629 struct rtype *next;
1632 struct rtype *type_hash[HASHMAX];
1634 static type_t *reg_type(type_t *type, const char *name, int t)
1636 struct rtype *nt;
1637 int hash;
1638 if (!name) {
1639 error_loc("registering named type without name\n");
1640 return type;
1642 hash = hash_ident(name);
1643 nt = xmalloc(sizeof(struct rtype));
1644 nt->name = name;
1645 nt->type = type;
1646 nt->t = t;
1647 nt->next = type_hash[hash];
1648 type_hash[hash] = nt;
1649 return type;
1652 static int is_incomplete(const type_t *t)
1654 return !t->defined && (is_struct(t->type) || is_union(t->type));
1657 static void add_incomplete(type_t *t)
1659 struct typenode *tn = xmalloc(sizeof *tn);
1660 tn->type = t;
1661 list_add_tail(&incomplete_types, &tn->entry);
1664 static void fix_type(type_t *t)
1666 if (t->kind == TKIND_ALIAS && is_incomplete(t)) {
1667 type_t *ot = t->orig;
1668 fix_type(ot);
1669 t->fields_or_args = ot->fields_or_args;
1670 t->defined = ot->defined;
1674 static void fix_incomplete(void)
1676 struct typenode *tn, *next;
1678 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1679 fix_type(tn->type);
1680 free(tn);
1684 static type_t *reg_typedefs(type_t *type, pident_list_t *pidents, attr_list_t *attrs)
1686 type_t *ptr = type;
1687 const pident_t *pident;
1688 int ptrc = 0;
1689 int is_str = is_attr(attrs, ATTR_STRING);
1690 unsigned char ptr_type = get_attrv(attrs, ATTR_POINTERTYPE);
1692 if (is_str)
1694 type_t *t = type;
1695 unsigned char c;
1697 while (is_ptr(t))
1698 t = t->ref;
1700 c = t->type;
1701 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1703 pident = LIST_ENTRY( list_head( pidents ), const pident_t, entry );
1704 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1705 pident->var->name);
1709 /* We must generate names for tagless enum, struct or union.
1710 Typedef-ing a tagless enum, struct or union means we want the typedef
1711 to be included in a library whether it has other attributes or not,
1712 hence the public attribute. */
1713 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1714 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1716 if (! is_attr(attrs, ATTR_PUBLIC))
1717 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1718 type->name = gen_name();
1721 LIST_FOR_EACH_ENTRY( pident, pidents, const pident_t, entry )
1723 var_t *name = pident->var;
1725 if (name->name) {
1726 type_t *cur = ptr;
1727 int cptr = pident->ptr_level;
1728 if (cptr > ptrc) {
1729 while (cptr > ptrc) {
1730 cur = ptr = make_type(pointer_default, cur);
1731 ptrc++;
1733 } else {
1734 while (cptr < ptrc) {
1735 cur = cur->ref;
1736 cptr++;
1739 if (pident->is_func) {
1740 int func_ptr_level = pident->func_ptr_level;
1741 cur = make_type(RPC_FC_FUNCTION, cur);
1742 cur->fields_or_args = pident->args;
1743 if (pident->callconv)
1744 cur->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, pident->callconv));
1745 else if (is_object_interface) {
1746 static char *stdmethodcalltype;
1747 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1748 cur->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1750 for (; func_ptr_level > 0; func_ptr_level--)
1751 cur = make_type(pointer_default, cur);
1753 cur = alias(cur, name->name);
1754 cur->attrs = attrs;
1755 if (ptr_type)
1757 if (is_ptr(cur))
1758 cur->type = ptr_type;
1759 else
1760 error_loc("'%s': pointer attribute applied to non-pointer type\n",
1761 cur->name);
1763 else if (is_str && ! is_ptr(cur))
1764 error_loc("'%s': [string] attribute applied to non-pointer type\n",
1765 cur->name);
1767 if (is_incomplete(cur))
1768 add_incomplete(cur);
1769 reg_type(cur, cur->name, 0);
1772 return type;
1775 static type_t *find_type(const char *name, int t)
1777 struct rtype *cur = type_hash[hash_ident(name)];
1778 while (cur && (cur->t != t || strcmp(cur->name, name)))
1779 cur = cur->next;
1780 if (!cur) {
1781 error_loc("type '%s' not found\n", name);
1782 return NULL;
1784 return cur->type;
1787 static type_t *find_type2(char *name, int t)
1789 type_t *tp = find_type(name, t);
1790 free(name);
1791 return tp;
1794 int is_type(const char *name)
1796 struct rtype *cur = type_hash[hash_ident(name)];
1797 while (cur && (cur->t || strcmp(cur->name, name)))
1798 cur = cur->next;
1799 if (cur) return TRUE;
1800 return FALSE;
1803 static type_t *get_type(unsigned char type, char *name, int t)
1805 struct rtype *cur = NULL;
1806 type_t *tp;
1807 if (name) {
1808 cur = type_hash[hash_ident(name)];
1809 while (cur && (cur->t != t || strcmp(cur->name, name)))
1810 cur = cur->next;
1812 if (cur) {
1813 free(name);
1814 return cur->type;
1816 tp = make_type(type, NULL);
1817 tp->name = name;
1818 if (!name) return tp;
1819 return reg_type(tp, name, t);
1822 static type_t *get_typev(unsigned char type, var_t *name, int t)
1824 char *sname = NULL;
1825 if (name) {
1826 sname = name->name;
1827 free(name);
1829 return get_type(type, sname, t);
1832 static int get_struct_type(var_list_t *fields)
1834 int has_pointer = 0;
1835 int has_conformance = 0;
1836 int has_variance = 0;
1837 var_t *field;
1839 if (get_padding(fields))
1840 return RPC_FC_BOGUS_STRUCT;
1842 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
1844 type_t *t = field->type;
1846 if (is_user_type(t))
1847 return RPC_FC_BOGUS_STRUCT;
1849 if (is_ptr(t))
1852 t = t->ref;
1853 while (is_ptr(t));
1855 switch (t->type)
1857 case RPC_FC_IP:
1858 case RPC_FC_ENCAPSULATED_UNION:
1859 case RPC_FC_NON_ENCAPSULATED_UNION:
1860 case RPC_FC_BOGUS_STRUCT:
1861 return RPC_FC_BOGUS_STRUCT;
1864 has_pointer = 1;
1865 continue;
1868 if (field->type->declarray)
1870 if (is_string_type(field->attrs, field->type))
1872 if (is_conformant_array(field->type))
1873 has_conformance = 1;
1874 has_variance = 1;
1875 continue;
1878 if (is_array(field->type->ref))
1879 return RPC_FC_BOGUS_STRUCT;
1881 if (is_conformant_array(field->type))
1883 has_conformance = 1;
1884 if (field->type->declarray && list_next(fields, &field->entry))
1885 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1886 field->name);
1888 if (field->type->length_is)
1889 has_variance = 1;
1891 t = field->type->ref;
1894 switch (t->type)
1897 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1898 * Simple types don't effect the type of struct.
1899 * A struct containing a simple struct is still a simple struct.
1900 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1902 case 0: /* void pointer */
1903 case RPC_FC_BYTE:
1904 case RPC_FC_CHAR:
1905 case RPC_FC_SMALL:
1906 case RPC_FC_USMALL:
1907 case RPC_FC_WCHAR:
1908 case RPC_FC_SHORT:
1909 case RPC_FC_USHORT:
1910 case RPC_FC_LONG:
1911 case RPC_FC_ULONG:
1912 case RPC_FC_INT3264:
1913 case RPC_FC_UINT3264:
1914 case RPC_FC_HYPER:
1915 case RPC_FC_FLOAT:
1916 case RPC_FC_DOUBLE:
1917 case RPC_FC_STRUCT:
1918 case RPC_FC_ENUM32:
1919 break;
1921 case RPC_FC_RP:
1922 case RPC_FC_UP:
1923 case RPC_FC_FP:
1924 case RPC_FC_OP:
1925 case RPC_FC_CARRAY:
1926 case RPC_FC_CVARRAY:
1927 case RPC_FC_BOGUS_ARRAY:
1928 has_pointer = 1;
1929 break;
1932 * Propagate member attributes
1933 * a struct should be at least as complex as its member
1935 case RPC_FC_CVSTRUCT:
1936 has_conformance = 1;
1937 has_variance = 1;
1938 has_pointer = 1;
1939 break;
1941 case RPC_FC_CPSTRUCT:
1942 has_conformance = 1;
1943 if (list_next( fields, &field->entry ))
1944 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1945 field->name);
1946 has_pointer = 1;
1947 break;
1949 case RPC_FC_CSTRUCT:
1950 has_conformance = 1;
1951 if (list_next( fields, &field->entry ))
1952 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1953 field->name);
1954 break;
1956 case RPC_FC_PSTRUCT:
1957 has_pointer = 1;
1958 break;
1960 default:
1961 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
1962 /* fallthru - treat it as complex */
1964 /* as soon as we see one of these these members, it's bogus... */
1965 case RPC_FC_ENCAPSULATED_UNION:
1966 case RPC_FC_NON_ENCAPSULATED_UNION:
1967 case RPC_FC_BOGUS_STRUCT:
1968 case RPC_FC_ENUM16:
1969 return RPC_FC_BOGUS_STRUCT;
1973 if( has_variance )
1975 if ( has_conformance )
1976 return RPC_FC_CVSTRUCT;
1977 else
1978 return RPC_FC_BOGUS_STRUCT;
1980 if( has_conformance && has_pointer )
1981 return RPC_FC_CPSTRUCT;
1982 if( has_conformance )
1983 return RPC_FC_CSTRUCT;
1984 if( has_pointer )
1985 return RPC_FC_PSTRUCT;
1986 return RPC_FC_STRUCT;
1989 /***** constant repository *****/
1991 struct rconst {
1992 char *name;
1993 var_t *var;
1994 struct rconst *next;
1997 struct rconst *const_hash[HASHMAX];
1999 static var_t *reg_const(var_t *var)
2001 struct rconst *nc;
2002 int hash;
2003 if (!var->name) {
2004 error_loc("registering constant without name\n");
2005 return var;
2007 hash = hash_ident(var->name);
2008 nc = xmalloc(sizeof(struct rconst));
2009 nc->name = var->name;
2010 nc->var = var;
2011 nc->next = const_hash[hash];
2012 const_hash[hash] = nc;
2013 return var;
2016 static var_t *find_const(char *name, int f)
2018 struct rconst *cur = const_hash[hash_ident(name)];
2019 while (cur && strcmp(cur->name, name))
2020 cur = cur->next;
2021 if (!cur) {
2022 if (f) error_loc("constant '%s' not found\n", name);
2023 return NULL;
2025 return cur->var;
2028 static void write_libid(const char *name, const attr_list_t *attr)
2030 const UUID *uuid = get_attrp(attr, ATTR_UUID);
2031 write_guid(idfile, "LIBID", name, uuid);
2034 static void write_clsid(type_t *cls)
2036 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
2037 write_guid(idfile, "CLSID", cls->name, uuid);
2040 static void write_diid(type_t *iface)
2042 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2043 write_guid(idfile, "DIID", iface->name, uuid);
2046 static void write_iid(type_t *iface)
2048 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2049 write_guid(idfile, "IID", iface->name, uuid);
2052 static int compute_method_indexes(type_t *iface)
2054 int idx;
2055 func_t *f;
2057 if (iface->ref)
2058 idx = compute_method_indexes(iface->ref);
2059 else
2060 idx = 0;
2062 if (!iface->funcs)
2063 return idx;
2065 LIST_FOR_EACH_ENTRY( f, iface->funcs, func_t, entry )
2066 if (! is_callas(f->def->attrs))
2067 f->idx = idx++;
2069 return idx;
2072 static char *gen_name(void)
2074 static const char format[] = "__WIDL_%s_generated_name_%08lX";
2075 static unsigned long n = 0;
2076 static const char *file_id;
2077 static size_t size;
2078 char *name;
2080 if (! file_id)
2082 char *dst = dup_basename(input_name, ".idl");
2083 file_id = dst;
2085 for (; *dst; ++dst)
2086 if (! isalnum((unsigned char) *dst))
2087 *dst = '_';
2089 size = sizeof format - 7 + strlen(file_id) + 8;
2092 name = xmalloc(size);
2093 sprintf(name, format, file_id, n++);
2094 return name;
2097 static void process_typedefs(pident_list_t *pidents)
2099 pident_t *pident, *next;
2101 if (!pidents) return;
2102 LIST_FOR_EACH_ENTRY_SAFE( pident, next, pidents, pident_t, entry )
2104 var_t *var = pident->var;
2105 type_t *type = find_type(var->name, 0);
2107 if (! parse_only && do_header)
2108 write_typedef(type);
2109 if (in_typelib && type->attrs)
2110 add_typelib_entry(type);
2112 free(pident);
2113 free(var);
2117 static void check_arg(var_t *arg)
2119 type_t *t = arg->type;
2121 if (t->type == 0 && ! is_var_ptr(arg))
2122 error_loc("argument '%s' has void type\n", arg->name);
2125 static void check_all_user_types(ifref_list_t *ifrefs)
2127 const ifref_t *ifref;
2128 const func_t *f;
2130 if (ifrefs) LIST_FOR_EACH_ENTRY(ifref, ifrefs, const ifref_t, entry)
2132 const func_list_t *fs = ifref->iface->funcs;
2133 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const func_t, entry)
2134 check_for_additional_prototype_types(f->args);
2138 int is_valid_uuid(const char *s)
2140 int i;
2142 for (i = 0; i < 36; ++i)
2143 if (i == 8 || i == 13 || i == 18 || i == 23)
2145 if (s[i] != '-')
2146 return FALSE;
2148 else
2149 if (!isxdigit(s[i]))
2150 return FALSE;
2152 return s[i] == '\0';