widl: Check that attributes are applicable for libraries, modules, dispinterfaces...
[wine/hacks.git] / tools / widl / parser.y
blob9824634b59d8400afb01c21a0095094042c8a03a
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);
133 static const attr_list_t *check_iface_attrs(const char *name, const attr_list_t *attrs);
134 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs);
135 static attr_list_t *check_typedef_attrs(attr_list_t *attrs);
136 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs);
137 static const attr_list_t *check_library_attrs(const char *name, const attr_list_t *attrs);
138 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
139 static const attr_list_t *check_module_attrs(const char *name, const attr_list_t *attrs);
140 static const attr_list_t *check_coclass_attrs(const char *name, const attr_list_t *attrs);
142 #define tsENUM 1
143 #define tsSTRUCT 2
144 #define tsUNION 3
147 %union {
148 attr_t *attr;
149 attr_list_t *attr_list;
150 str_list_t *str_list;
151 expr_t *expr;
152 expr_list_t *expr_list;
153 array_dims_t *array_dims;
154 type_t *type;
155 var_t *var;
156 var_list_t *var_list;
157 pident_t *pident;
158 pident_list_t *pident_list;
159 func_t *func;
160 func_list_t *func_list;
161 ifref_t *ifref;
162 ifref_list_t *ifref_list;
163 char *str;
164 UUID *uuid;
165 unsigned int num;
166 double dbl;
167 interface_info_t ifinfo;
170 %token <str> aIDENTIFIER
171 %token <str> aKNOWNTYPE
172 %token <num> aNUM aHEXNUM
173 %token <dbl> aDOUBLE
174 %token <str> aSTRING
175 %token <uuid> aUUID
176 %token aEOF
177 %token SHL SHR
178 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
179 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
180 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
181 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
182 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
183 %token tDEFAULT
184 %token tDEFAULTCOLLELEM
185 %token tDEFAULTVALUE
186 %token tDEFAULTVTABLE
187 %token tDISPLAYBIND
188 %token tDISPINTERFACE
189 %token tDLLNAME tDOUBLE tDUAL
190 %token tENDPOINT
191 %token tENTRY tENUM tERRORSTATUST
192 %token tEXPLICITHANDLE tEXTERN
193 %token tFALSE
194 %token tFASTCALL
195 %token tFLOAT
196 %token tHANDLE
197 %token tHANDLET
198 %token tHELPCONTEXT tHELPFILE
199 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
200 %token tHIDDEN
201 %token tHYPER tID tIDEMPOTENT
202 %token tIIDIS
203 %token tIMMEDIATEBIND
204 %token tIMPLICITHANDLE
205 %token tIMPORT tIMPORTLIB
206 %token tIN tINLINE
207 %token tINPUTSYNC
208 %token tINT tINT64
209 %token tINTERFACE
210 %token tLCID
211 %token tLENGTHIS tLIBRARY
212 %token tLOCAL
213 %token tLONG
214 %token tMETHODS
215 %token tMODULE
216 %token tNONBROWSABLE
217 %token tNONCREATABLE
218 %token tNONEXTENSIBLE
219 %token tOBJECT tODL tOLEAUTOMATION
220 %token tOPTIONAL
221 %token tOUT
222 %token tPASCAL
223 %token tPOINTERDEFAULT
224 %token tPROPERTIES
225 %token tPROPGET tPROPPUT tPROPPUTREF
226 %token tPTR
227 %token tPUBLIC
228 %token tRANGE
229 %token tREADONLY tREF
230 %token tREQUESTEDIT
231 %token tRESTRICTED
232 %token tRETVAL
233 %token tSAFEARRAY
234 %token tSHORT
235 %token tSIGNED
236 %token tSINGLE
237 %token tSIZEIS tSIZEOF
238 %token tSMALL
239 %token tSOURCE
240 %token tSTDCALL
241 %token tSTRICTCONTEXTHANDLE
242 %token tSTRING tSTRUCT
243 %token tSWITCH tSWITCHIS tSWITCHTYPE
244 %token tTRANSMITAS
245 %token tTRUE
246 %token tTYPEDEF
247 %token tUNION
248 %token tUNIQUE
249 %token tUNSIGNED
250 %token tUUID
251 %token tV1ENUM
252 %token tVARARG
253 %token tVERSION
254 %token tVOID
255 %token tWCHAR tWIREMARSHAL
257 %type <attr> attribute
258 %type <attr_list> m_attributes attributes attrib_list
259 %type <str_list> str_list
260 %type <expr> m_expr expr expr_const
261 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_const
262 %type <array_dims> array array_list
263 %type <ifinfo> interfacehdr
264 %type <type> inherit interface interfacedef interfacedec
265 %type <type> dispinterface dispinterfacehdr dispinterfacedef
266 %type <type> module modulehdr moduledef
267 %type <type> base_type int_std
268 %type <type> enumdef structdef uniondef
269 %type <type> type
270 %type <ifref> coclass_int
271 %type <ifref_list> gbl_statements coclass_ints
272 %type <var> arg field s_field case enum constdef externdef
273 %type <var_list> m_args no_args args fields cases enums enum_list dispint_props
274 %type <var> m_ident t_ident ident
275 %type <pident> pident func_ident direct_ident
276 %type <pident_list> pident_list
277 %type <func> funcdef
278 %type <func_list> int_statements dispint_meths
279 %type <type> coclass coclasshdr coclassdef
280 %type <num> pointer_type version
281 %type <str> libraryhdr callconv
282 %type <uuid> uuid_string
283 %type <num> import_start
285 %left ','
286 %right '?' ':'
287 %left '|'
288 %left '&'
289 %left '-' '+'
290 %left '*' '/'
291 %left SHL SHR
292 %right '~'
293 %right CAST
294 %right PPTR
295 %right NEG
296 %right ADDRESSOF
300 input: gbl_statements { fix_incomplete();
301 check_all_user_types($1);
302 write_proxies($1);
303 write_client($1);
304 write_server($1);
305 write_dlldata($1);
309 gbl_statements: { $$ = NULL; }
310 | gbl_statements interfacedec { $$ = $1; }
311 | gbl_statements interfacedef { $$ = append_ifref( $1, make_ifref($2) ); }
312 | gbl_statements coclass ';' { $$ = $1;
313 reg_type($2, $2->name, 0);
314 if (!parse_only && do_header) write_coclass_forward($2);
316 | gbl_statements coclassdef { $$ = $1;
317 add_typelib_entry($2);
318 reg_type($2, $2->name, 0);
319 if (!parse_only && do_header) write_coclass_forward($2);
321 | gbl_statements moduledef { $$ = $1; add_typelib_entry($2); }
322 | gbl_statements librarydef { $$ = $1; }
323 | gbl_statements statement { $$ = $1; }
326 imp_statements: {}
327 | imp_statements interfacedec { if (!parse_only) add_typelib_entry($2); }
328 | imp_statements interfacedef { if (!parse_only) add_typelib_entry($2); }
329 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
330 | imp_statements coclassdef { if (!parse_only) add_typelib_entry($2);
331 reg_type($2, $2->name, 0);
332 if (!parse_only && do_header) write_coclass_forward($2);
334 | imp_statements moduledef { if (!parse_only) add_typelib_entry($2); }
335 | imp_statements statement {}
336 | imp_statements importlib {}
337 | imp_statements librarydef {}
340 int_statements: { $$ = NULL; }
341 | int_statements funcdef ';' { $$ = append_func( $1, $2 ); }
342 | int_statements statement { $$ = $1; }
345 semicolon_opt:
346 | ';'
349 statement: constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
350 | cppquote {}
351 | enumdef ';' { if (!parse_only && do_header) {
352 write_type_def_or_decl(header, $1, FALSE, NULL);
353 fprintf(header, ";\n\n");
356 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
357 | import {}
358 | structdef ';' { if (!parse_only && do_header) {
359 write_type_def_or_decl(header, $1, FALSE, NULL);
360 fprintf(header, ";\n\n");
363 | typedef ';' {}
364 | uniondef ';' { if (!parse_only && do_header) {
365 write_type_def_or_decl(header, $1, FALSE, NULL);
366 fprintf(header, ";\n\n");
371 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
373 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
374 $$ = do_import($2);
375 if (!$$) yychar = aEOF;
379 import: import_start imp_statements aEOF
380 { if ($1) pop_import(); }
383 importlib: tIMPORTLIB '(' aSTRING ')'
384 semicolon_opt { if(!parse_only) add_importlib($3); }
387 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
389 library_start: attributes libraryhdr '{' { check_library_attrs($2, $1);
390 if (!parse_only) start_typelib($2, $1);
391 if (!parse_only && do_header) write_library($2, $1);
392 if (!parse_only && do_idfile) write_libid($2, $1);
395 librarydef: library_start imp_statements '}'
396 semicolon_opt { if (!parse_only) end_typelib(); }
399 m_args: { $$ = NULL; }
400 | args
403 no_args: tVOID { $$ = NULL; }
406 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
407 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
408 | no_args
411 /* split into two rules to get bison to resolve a tVOID conflict */
412 arg: attributes type pident array { $$ = $3->var;
413 $$->attrs = $1;
414 set_type($$, $2, $3, $4, TRUE);
415 free($3);
417 | type pident array { $$ = $2->var;
418 set_type($$, $1, $2, $3, TRUE);
419 free($2);
423 array: { $$ = NULL; }
424 | '[' array_list ']' { $$ = $2; }
425 | '[' '*' ']' { $$ = append_array( NULL, make_expr(EXPR_VOID) ); }
428 array_list: m_expr /* size of first dimension is optional */ { $$ = append_array( NULL, $1 ); }
429 | array_list ',' expr { $$ = append_array( $1, $3 ); }
430 | array_list ']' '[' expr { $$ = append_array( $1, $4 ); }
433 m_attributes: { $$ = NULL; }
434 | attributes
437 attributes:
438 '[' attrib_list ']' { $$ = $2;
439 if (!$$)
440 error_loc("empty attribute lists unsupported\n");
444 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
445 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
446 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
449 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
450 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
453 attribute: { $$ = NULL; }
454 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
455 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
456 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
457 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
458 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
459 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
460 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
461 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
462 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
463 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
464 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
465 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
466 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
467 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
468 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
469 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
470 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
471 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
472 | tDUAL { $$ = make_attr(ATTR_DUAL); }
473 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
474 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
475 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
476 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
477 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
478 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
479 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
480 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
481 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
482 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
483 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
484 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
485 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
486 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
487 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
488 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
489 | tIN { $$ = make_attr(ATTR_IN); }
490 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
491 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
492 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
493 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
494 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
495 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
496 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
497 | tODL { $$ = make_attr(ATTR_ODL); }
498 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
499 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
500 | tOUT { $$ = make_attr(ATTR_OUT); }
501 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
502 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
503 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
504 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
505 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
506 | tRANGE '(' expr_const ',' expr_const ')' { expr_list_t *list = append_expr( NULL, $3 );
507 list = append_expr( list, $5 );
508 $$ = make_attrp(ATTR_RANGE, list); }
509 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
510 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
511 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
512 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
513 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
514 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
515 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
516 | tSTRING { $$ = make_attr(ATTR_STRING); }
517 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
518 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
519 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
520 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
521 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
522 | tVARARG { $$ = make_attr(ATTR_VARARG); }
523 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
524 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
525 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
528 uuid_string:
529 aUUID
530 | aSTRING { if (!is_valid_uuid($1))
531 error_loc("invalid UUID: %s\n", $1);
532 $$ = parse_uuid($1); }
535 callconv: tCDECL { $$ = $<str>1; }
536 | tFASTCALL { $$ = $<str>1; }
537 | tPASCAL { $$ = $<str>1; }
538 | tSTDCALL { $$ = $<str>1; }
541 cases: { $$ = NULL; }
542 | cases case { $$ = append_var( $1, $2 ); }
545 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
546 $$ = $4; if (!$$) $$ = make_var(NULL);
547 $$->attrs = append_attr( $$->attrs, a );
549 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
550 $$ = $3; if (!$$) $$ = make_var(NULL);
551 $$->attrs = append_attr( $$->attrs, a );
555 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
556 set_type($$, $2, NULL, NULL, FALSE);
557 $$->eval = $5;
561 enums: { $$ = NULL; }
562 | enum_list ',' { $$ = $1; }
563 | enum_list
566 enum_list: enum { if (!$1->eval)
567 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
568 $$ = append_var( NULL, $1 );
570 | enum_list ',' enum { if (!$3->eval)
572 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
573 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
575 $$ = append_var( $1, $3 );
579 enum: ident '=' expr_const { $$ = reg_const($1);
580 $$->eval = $3;
581 $$->type = make_int(0);
583 | ident { $$ = reg_const($1);
584 $$->type = make_int(0);
588 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
589 $$->kind = TKIND_ENUM;
590 $$->fields_or_args = $4;
591 $$->defined = TRUE;
592 if(in_typelib)
593 add_typelib_entry($$);
597 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
598 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
602 exprs: { $$ = make_expr(EXPR_VOID); }
603 | expr_list
606 expr_list: expr
607 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
611 m_expr: { $$ = make_expr(EXPR_VOID); }
612 | expr
615 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
616 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
617 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
618 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
619 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
620 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
621 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
622 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
623 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
624 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
625 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
626 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
627 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
628 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
629 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
630 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
631 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
632 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
633 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
634 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
635 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
636 | '(' expr ')' { $$ = $2; }
639 expr_list_const: expr_const { $$ = append_expr( NULL, $1 ); }
640 | expr_list_const ',' expr_const { $$ = append_expr( $1, $3 ); }
643 expr_const: expr { $$ = $1;
644 if (!$$->is_const)
645 error_loc("expression is not constant\n");
649 externdef: tEXTERN tCONST type ident { $$ = $4;
650 set_type($$, $3, NULL, NULL, FALSE);
654 fields: { $$ = NULL; }
655 | fields field { $$ = append_var( $1, $2 ); }
658 field: s_field ';' { $$ = $1; }
659 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
660 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
661 | ';' { $$ = NULL; }
664 s_field: m_attributes type pident array { $$ = $3->var;
665 $$->attrs = check_field_attrs($$->name, $1);
666 set_type($$, $2, $3, $4, FALSE);
667 free($3);
671 funcdef:
672 m_attributes type pident { var_t *v = $3->var;
673 var_list_t *args = $3->args;
674 v->attrs = check_function_attrs(v->name, $1);
675 set_type(v, $2, $3, NULL, FALSE);
676 free($3);
677 $$ = make_func(v, args);
681 m_ident: { $$ = NULL; }
682 | ident
685 t_ident: { $$ = NULL; }
686 | aIDENTIFIER { $$ = make_var($1); }
687 | aKNOWNTYPE { $$ = make_var($1); }
690 ident: aIDENTIFIER { $$ = make_var($1); }
691 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
692 | aKNOWNTYPE { $$ = make_var($<str>1); }
695 base_type: tBYTE { $$ = make_builtin($<str>1); }
696 | tWCHAR { $$ = make_builtin($<str>1); }
697 | int_std
698 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
699 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
700 switch ($$->type) {
701 case RPC_FC_CHAR: break;
702 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
703 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
704 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
705 case RPC_FC_HYPER:
706 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
708 $$ = alias($$, "MIDL_uhyper");
709 $$->sign = 0;
711 break;
712 default: break;
715 | tUNSIGNED { $$ = make_int(-1); }
716 | tFLOAT { $$ = make_builtin($<str>1); }
717 | tSINGLE { $$ = duptype(find_type("float", 0), 1); }
718 | tDOUBLE { $$ = make_builtin($<str>1); }
719 | tBOOLEAN { $$ = make_builtin($<str>1); }
720 | tERRORSTATUST { $$ = make_builtin($<str>1); }
721 | tHANDLET { $$ = make_builtin($<str>1); }
724 m_int:
725 | tINT
728 int_std: tINT { $$ = make_builtin($<str>1); }
729 | tSHORT m_int { $$ = make_builtin($<str>1); }
730 | tSMALL { $$ = make_builtin($<str>1); }
731 | tLONG m_int { $$ = make_builtin($<str>1); }
732 | tHYPER m_int { $$ = make_builtin($<str>1); }
733 | tINT64 { $$ = make_builtin($<str>1); }
734 | tCHAR { $$ = make_builtin($<str>1); }
737 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
738 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
739 if ($$->defined) error_loc("multiple definition error\n");
740 if ($$->kind != TKIND_COCLASS) error_loc("%s was not declared a coclass\n", $2);
744 coclasshdr: attributes coclass { $$ = $2;
745 $$->attrs = check_coclass_attrs($2->name, $1);
746 if (!parse_only && do_header)
747 write_coclass($$);
748 if (!parse_only && do_idfile)
749 write_clsid($$);
753 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
754 { $$ = $1;
755 $$->ifaces = $3;
756 $$->defined = TRUE;
760 coclass_ints: { $$ = NULL; }
761 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
764 coclass_int:
765 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
768 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
769 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
772 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
773 is_object_interface = TRUE;
774 $$ = $2;
775 if ($$->defined) error_loc("multiple definition error\n");
776 attrs = make_attr(ATTR_DISPINTERFACE);
777 $$->attrs = append_attr( check_dispiface_attrs($2->name, $1), attrs );
778 $$->ref = find_type("IDispatch", 0);
779 if (!$$->ref) error_loc("IDispatch is undefined\n");
780 $$->defined = TRUE;
781 if (!parse_only && do_header) write_forward($$);
785 dispint_props: tPROPERTIES ':' { $$ = NULL; }
786 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
789 dispint_meths: tMETHODS ':' { $$ = NULL; }
790 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
793 dispinterfacedef: dispinterfacehdr '{'
794 dispint_props
795 dispint_meths
796 '}' { $$ = $1;
797 $$->fields_or_args = $3;
798 $$->funcs = $4;
799 if (!parse_only && do_header) write_dispinterface($$);
800 if (!parse_only && do_idfile) write_diid($$);
802 | dispinterfacehdr
803 '{' interface ';' '}' { $$ = $1;
804 $$->fields_or_args = $3->fields_or_args;
805 $$->funcs = $3->funcs;
806 if (!parse_only && do_header) write_dispinterface($$);
807 if (!parse_only && do_idfile) write_diid($$);
811 inherit: { $$ = NULL; }
812 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
815 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
816 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
819 interfacehdr: attributes interface { $$.interface = $2;
820 $$.old_pointer_default = pointer_default;
821 if (is_attr($1, ATTR_POINTERDEFAULT))
822 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
823 is_object_interface = is_object($1);
824 if ($2->defined) error_loc("multiple definition error\n");
825 $2->attrs = check_iface_attrs($2->name, $1);
826 $2->defined = TRUE;
827 if (!parse_only && do_header) write_forward($2);
831 interfacedef: interfacehdr inherit
832 '{' int_statements '}' semicolon_opt { $$ = $1.interface;
833 $$->ref = $2;
834 $$->funcs = $4;
835 compute_method_indexes($$);
836 if (!parse_only && do_header) write_interface($$);
837 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
838 if (!parse_only && do_idfile) write_iid($$);
839 pointer_default = $1.old_pointer_default;
841 /* MIDL is able to import the definition of a base class from inside the
842 * definition of a derived class, I'll try to support it with this rule */
843 | interfacehdr ':' aIDENTIFIER
844 '{' import int_statements '}'
845 semicolon_opt { $$ = $1.interface;
846 $$->ref = find_type2($3, 0);
847 if (!$$->ref) error_loc("base class '%s' not found in import\n", $3);
848 $$->funcs = $6;
849 compute_method_indexes($$);
850 if (!parse_only && do_header) write_interface($$);
851 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
852 if (!parse_only && do_idfile) write_iid($$);
853 pointer_default = $1.old_pointer_default;
855 | dispinterfacedef semicolon_opt { $$ = $1; }
858 interfacedec:
859 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
860 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
863 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
864 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
867 modulehdr: attributes module { $$ = $2;
868 $$->attrs = check_module_attrs($2->name, $1);
872 moduledef: modulehdr '{' int_statements '}'
873 semicolon_opt { $$ = $1;
874 $$->funcs = $3;
875 /* FIXME: if (!parse_only && do_header) write_module($$); */
879 pident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
880 | tCONST pident { $$ = $2; /* FIXME */ }
881 | callconv pident { $$ = $2;
882 if ($$->callconv) parser_warning("multiple calling conventions %s, %s for function %s\n", $$->callconv, $1, $$->var->name);
883 $$->callconv = $1;
885 | direct_ident
888 func_ident: direct_ident '(' m_args ')'
889 { $$ = $1;
890 $1->args = $3;
891 $1->is_func = TRUE;
895 direct_ident: ident { $$ = make_pident($1); }
896 | '(' pident ')' { $$ = $2; }
897 | func_ident { $$ = $1;
898 $$->func_ptr_level = $$->ptr_level;
899 $$->ptr_level = 0;
903 pident_list:
904 pident { $$ = append_pident( NULL, $1 ); }
905 | pident_list ',' pident { $$ = append_pident( $1, $3 ); }
908 pointer_type:
909 tREF { $$ = RPC_FC_RP; }
910 | tUNIQUE { $$ = RPC_FC_UP; }
911 | tPTR { $$ = RPC_FC_FP; }
914 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
915 /* overwrite RPC_FC_STRUCT with a more exact type */
916 $$->type = get_struct_type( $4 );
917 $$->kind = TKIND_RECORD;
918 $$->fields_or_args = $4;
919 $$->defined = TRUE;
920 if(in_typelib)
921 add_typelib_entry($$);
925 type: tVOID { $$ = duptype(find_type("void", 0), 1); }
926 | aKNOWNTYPE { $$ = find_type($1, 0); }
927 | base_type { $$ = $1; }
928 | tCONST type { $$ = duptype($2, 1); $$->is_const = TRUE; }
929 | enumdef { $$ = $1; }
930 | tENUM aIDENTIFIER { $$ = find_type2($2, tsENUM); }
931 | structdef { $$ = $1; }
932 | tSTRUCT aIDENTIFIER { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
933 | uniondef { $$ = $1; }
934 | tUNION aIDENTIFIER { $$ = find_type2($2, tsUNION); }
935 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
938 typedef: tTYPEDEF m_attributes type pident_list { reg_typedefs($3, $4, check_typedef_attrs($2));
939 process_typedefs($4);
943 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
944 $$->kind = TKIND_UNION;
945 $$->fields_or_args = $4;
946 $$->defined = TRUE;
948 | tUNION t_ident
949 tSWITCH '(' s_field ')'
950 m_ident '{' cases '}' { var_t *u = $7;
951 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
952 $$->kind = TKIND_UNION;
953 if (!u) u = make_var( xstrdup("tagged_union") );
954 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
955 u->type->kind = TKIND_UNION;
956 u->type->fields_or_args = $9;
957 u->type->defined = TRUE;
958 $$->fields_or_args = append_var( $$->fields_or_args, $5 );
959 $$->fields_or_args = append_var( $$->fields_or_args, u );
960 $$->defined = TRUE;
964 version:
965 aNUM { $$ = MAKEVERSION($1, 0); }
966 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
971 static void decl_builtin(const char *name, unsigned char type)
973 type_t *t = make_type(type, NULL);
974 t->name = xstrdup(name);
975 reg_type(t, name, 0);
978 static type_t *make_builtin(char *name)
980 /* NAME is strdup'd in the lexer */
981 type_t *t = duptype(find_type(name, 0), 0);
982 t->name = name;
983 return t;
986 static type_t *make_int(int sign)
988 type_t *t = duptype(find_type("int", 0), 1);
990 t->sign = sign;
991 if (sign < 0)
992 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
994 return t;
997 void init_types(void)
999 decl_builtin("void", 0);
1000 decl_builtin("byte", RPC_FC_BYTE);
1001 decl_builtin("wchar_t", RPC_FC_WCHAR);
1002 decl_builtin("int", RPC_FC_LONG); /* win32 */
1003 decl_builtin("short", RPC_FC_SHORT);
1004 decl_builtin("small", RPC_FC_SMALL);
1005 decl_builtin("long", RPC_FC_LONG);
1006 decl_builtin("hyper", RPC_FC_HYPER);
1007 decl_builtin("__int64", RPC_FC_HYPER);
1008 decl_builtin("char", RPC_FC_CHAR);
1009 decl_builtin("float", RPC_FC_FLOAT);
1010 decl_builtin("double", RPC_FC_DOUBLE);
1011 decl_builtin("boolean", RPC_FC_BYTE);
1012 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
1013 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
1016 static str_list_t *append_str(str_list_t *list, char *str)
1018 struct str_list_entry_t *entry;
1020 if (!str) return list;
1021 if (!list)
1023 list = xmalloc( sizeof(*list) );
1024 list_init( list );
1026 entry = xmalloc( sizeof(*entry) );
1027 entry->str = str;
1028 list_add_tail( list, &entry->entry );
1029 return list;
1032 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1034 if (!attr) return list;
1035 if (!list)
1037 list = xmalloc( sizeof(*list) );
1038 list_init( list );
1040 list_add_tail( list, &attr->entry );
1041 return list;
1044 static attr_t *make_attr(enum attr_type type)
1046 attr_t *a = xmalloc(sizeof(attr_t));
1047 a->type = type;
1048 a->u.ival = 0;
1049 return a;
1052 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1054 attr_t *a = xmalloc(sizeof(attr_t));
1055 a->type = type;
1056 a->u.ival = val;
1057 return a;
1060 static attr_t *make_attrp(enum attr_type type, void *val)
1062 attr_t *a = xmalloc(sizeof(attr_t));
1063 a->type = type;
1064 a->u.pval = val;
1065 return a;
1068 static expr_t *make_expr(enum expr_type type)
1070 expr_t *e = xmalloc(sizeof(expr_t));
1071 e->type = type;
1072 e->ref = NULL;
1073 e->u.lval = 0;
1074 e->is_const = FALSE;
1075 e->cval = 0;
1076 return e;
1079 static expr_t *make_exprl(enum expr_type type, long val)
1081 expr_t *e = xmalloc(sizeof(expr_t));
1082 e->type = type;
1083 e->ref = NULL;
1084 e->u.lval = val;
1085 e->is_const = FALSE;
1086 /* check for numeric constant */
1087 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
1088 /* make sure true/false value is valid */
1089 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
1090 e->is_const = TRUE;
1091 e->cval = val;
1093 return e;
1096 static expr_t *make_exprd(enum expr_type type, double val)
1098 expr_t *e = xmalloc(sizeof(expr_t));
1099 e->type = type;
1100 e->ref = NULL;
1101 e->u.dval = val;
1102 e->is_const = TRUE;
1103 e->cval = val;
1104 return e;
1107 static expr_t *make_exprs(enum expr_type type, char *val)
1109 expr_t *e;
1110 e = xmalloc(sizeof(expr_t));
1111 e->type = type;
1112 e->ref = NULL;
1113 e->u.sval = val;
1114 e->is_const = FALSE;
1115 /* check for predefined constants */
1116 if (type == EXPR_IDENTIFIER) {
1117 var_t *c = find_const(val, 0);
1118 if (c) {
1119 e->u.sval = c->name;
1120 free(val);
1121 e->is_const = TRUE;
1122 e->cval = c->eval->cval;
1125 return e;
1128 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr)
1130 expr_t *e;
1131 e = xmalloc(sizeof(expr_t));
1132 e->type = type;
1133 e->ref = expr;
1134 e->u.tref = tref;
1135 e->is_const = FALSE;
1136 /* check for cast of constant expression */
1137 if (type == EXPR_SIZEOF) {
1138 switch (tref->type) {
1139 case RPC_FC_BYTE:
1140 case RPC_FC_CHAR:
1141 case RPC_FC_SMALL:
1142 case RPC_FC_USMALL:
1143 e->is_const = TRUE;
1144 e->cval = 1;
1145 break;
1146 case RPC_FC_WCHAR:
1147 case RPC_FC_USHORT:
1148 case RPC_FC_SHORT:
1149 e->is_const = TRUE;
1150 e->cval = 2;
1151 break;
1152 case RPC_FC_LONG:
1153 case RPC_FC_ULONG:
1154 case RPC_FC_FLOAT:
1155 case RPC_FC_ERROR_STATUS_T:
1156 e->is_const = TRUE;
1157 e->cval = 4;
1158 break;
1159 case RPC_FC_HYPER:
1160 case RPC_FC_DOUBLE:
1161 e->is_const = TRUE;
1162 e->cval = 8;
1163 break;
1166 if (type == EXPR_CAST && expr->is_const) {
1167 e->is_const = TRUE;
1168 e->cval = expr->cval;
1170 return e;
1173 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
1175 expr_t *e;
1176 if (type == EXPR_ADDRESSOF && expr->type != EXPR_IDENTIFIER)
1177 error("address-of operator applied to invalid expression\n");
1178 e = xmalloc(sizeof(expr_t));
1179 e->type = type;
1180 e->ref = expr;
1181 e->u.lval = 0;
1182 e->is_const = FALSE;
1183 /* check for compile-time optimization */
1184 if (expr->is_const) {
1185 e->is_const = TRUE;
1186 switch (type) {
1187 case EXPR_NEG:
1188 e->cval = -expr->cval;
1189 break;
1190 case EXPR_NOT:
1191 e->cval = ~expr->cval;
1192 break;
1193 default:
1194 e->is_const = FALSE;
1195 break;
1198 return e;
1201 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1203 expr_t *e;
1204 e = xmalloc(sizeof(expr_t));
1205 e->type = type;
1206 e->ref = expr1;
1207 e->u.ext = expr2;
1208 e->is_const = FALSE;
1209 /* check for compile-time optimization */
1210 if (expr1->is_const && expr2->is_const) {
1211 e->is_const = TRUE;
1212 switch (type) {
1213 case EXPR_ADD:
1214 e->cval = expr1->cval + expr2->cval;
1215 break;
1216 case EXPR_SUB:
1217 e->cval = expr1->cval - expr2->cval;
1218 break;
1219 case EXPR_MUL:
1220 e->cval = expr1->cval * expr2->cval;
1221 break;
1222 case EXPR_DIV:
1223 e->cval = expr1->cval / expr2->cval;
1224 break;
1225 case EXPR_OR:
1226 e->cval = expr1->cval | expr2->cval;
1227 break;
1228 case EXPR_AND:
1229 e->cval = expr1->cval & expr2->cval;
1230 break;
1231 case EXPR_SHL:
1232 e->cval = expr1->cval << expr2->cval;
1233 break;
1234 case EXPR_SHR:
1235 e->cval = expr1->cval >> expr2->cval;
1236 break;
1237 default:
1238 e->is_const = FALSE;
1239 break;
1242 return e;
1245 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1247 expr_t *e;
1248 e = xmalloc(sizeof(expr_t));
1249 e->type = type;
1250 e->ref = expr1;
1251 e->u.ext = expr2;
1252 e->ext2 = expr3;
1253 e->is_const = FALSE;
1254 /* check for compile-time optimization */
1255 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1256 e->is_const = TRUE;
1257 switch (type) {
1258 case EXPR_COND:
1259 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1260 break;
1261 default:
1262 e->is_const = FALSE;
1263 break;
1266 return e;
1269 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1271 if (!expr) return list;
1272 if (!list)
1274 list = xmalloc( sizeof(*list) );
1275 list_init( list );
1277 list_add_tail( list, &expr->entry );
1278 return list;
1281 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1283 if (!expr) return list;
1284 if (!list)
1286 list = xmalloc( sizeof(*list) );
1287 list_init( list );
1289 list_add_tail( list, &expr->entry );
1290 return list;
1293 static struct list type_pool = LIST_INIT(type_pool);
1294 typedef struct
1296 type_t data;
1297 struct list link;
1298 } type_pool_node_t;
1300 type_t *alloc_type(void)
1302 type_pool_node_t *node = xmalloc(sizeof *node);
1303 list_add_tail(&type_pool, &node->link);
1304 return &node->data;
1307 void set_all_tfswrite(int val)
1309 type_pool_node_t *node;
1310 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1311 node->data.tfswrite = val;
1314 static type_t *make_type(unsigned char type, type_t *ref)
1316 type_t *t = alloc_type();
1317 t->name = NULL;
1318 t->kind = TKIND_PRIMITIVE;
1319 t->type = type;
1320 t->ref = ref;
1321 t->attrs = NULL;
1322 t->orig = NULL;
1323 t->funcs = NULL;
1324 t->fields_or_args = NULL;
1325 t->ifaces = NULL;
1326 t->dim = 0;
1327 t->size_is = NULL;
1328 t->length_is = NULL;
1329 t->typestring_offset = 0;
1330 t->ptrdesc = 0;
1331 t->declarray = FALSE;
1332 t->ignore = (parse_only != 0);
1333 t->is_const = FALSE;
1334 t->sign = 0;
1335 t->defined = FALSE;
1336 t->written = FALSE;
1337 t->user_types_registered = FALSE;
1338 t->tfswrite = FALSE;
1339 t->typelib_idx = -1;
1340 return t;
1343 static void set_type(var_t *v, type_t *type, const pident_t *pident, array_dims_t *arr,
1344 int top)
1346 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1347 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1348 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1349 int ptr_type = ptr_attr;
1350 int sizeless, has_varconf;
1351 expr_t *dim;
1352 type_t *atype, **ptype;
1353 int ptr_level = (pident ? pident->ptr_level : 0);
1355 v->type = type;
1357 if (!ptr_type && top)
1358 ptr_type = RPC_FC_RP;
1360 for ( ; 0 < ptr_level; --ptr_level)
1362 v->type = make_type(pointer_default, v->type);
1363 if (ptr_level == 1 && ptr_type && !arr)
1365 v->type->type = ptr_type;
1366 ptr_type = 0;
1370 if (ptr_type && !arr)
1372 if (is_ptr(v->type))
1374 if (v->type->type != ptr_type)
1376 v->type = duptype(v->type, 1);
1377 v->type->type = ptr_type;
1380 else if (!arr && ptr_attr)
1381 error("%s: pointer attribute applied to non-pointer type\n", v->name);
1384 if (pident && pident->is_func) {
1385 int func_ptr_level = pident->func_ptr_level;
1386 v->type = make_type(RPC_FC_FUNCTION, v->type);
1387 v->type->fields_or_args = pident->args;
1388 if (pident->callconv)
1389 v->type->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, pident->callconv));
1390 else if (is_object_interface) {
1391 static char *stdmethodcalltype;
1392 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1393 v->type->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1395 for (; func_ptr_level > 0; func_ptr_level--)
1396 v->type = make_type(ptr_type, v->type);
1399 sizeless = FALSE;
1400 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1402 if (sizeless)
1403 error("%s: only the first array dimension can be unspecified\n", v->name);
1405 if (dim->is_const)
1407 unsigned int align = 0;
1408 size_t size = type_memsize(v->type, &align);
1410 if (dim->cval <= 0)
1411 error("%s: array dimension must be positive\n", v->name);
1413 if (0xffffffffuL / size < (unsigned long) dim->cval)
1414 error("%s: total array size is too large\n", v->name);
1415 else if (0xffffuL < size * dim->cval)
1416 v->type = make_type(RPC_FC_LGFARRAY, v->type);
1417 else
1418 v->type = make_type(RPC_FC_SMFARRAY, v->type);
1420 else
1422 sizeless = TRUE;
1423 v->type = make_type(RPC_FC_CARRAY, v->type);
1426 v->type->declarray = TRUE;
1427 v->type->dim = dim->cval;
1430 ptype = &v->type;
1431 has_varconf = FALSE;
1432 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1434 if (dim->type != EXPR_VOID)
1436 has_varconf = TRUE;
1437 atype = *ptype = duptype(*ptype, 0);
1439 if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1440 error("%s: cannot specify size_is for a fixed sized array\n", v->name);
1442 if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1443 error("%s: size_is attribute applied to illegal type\n", v->name);
1445 atype->type = RPC_FC_CARRAY;
1446 atype->size_is = dim;
1449 ptype = &(*ptype)->ref;
1450 if (*ptype == NULL)
1451 error("%s: too many expressions in size_is attribute\n", v->name);
1454 ptype = &v->type;
1455 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1457 if (dim->type != EXPR_VOID)
1459 has_varconf = TRUE;
1460 atype = *ptype = duptype(*ptype, 0);
1462 if (atype->type == RPC_FC_SMFARRAY)
1463 atype->type = RPC_FC_SMVARRAY;
1464 else if (atype->type == RPC_FC_LGFARRAY)
1465 atype->type = RPC_FC_LGVARRAY;
1466 else if (atype->type == RPC_FC_CARRAY)
1467 atype->type = RPC_FC_CVARRAY;
1468 else
1469 error("%s: length_is attribute applied to illegal type\n", v->name);
1471 atype->length_is = dim;
1474 ptype = &(*ptype)->ref;
1475 if (*ptype == NULL)
1476 error("%s: too many expressions in length_is attribute\n", v->name);
1479 if (has_varconf && !last_array(v->type))
1481 ptype = &v->type;
1482 for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1484 *ptype = duptype(*ptype, 0);
1485 (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1489 if (is_array(v->type))
1491 const type_t *rt = v->type->ref;
1492 if (is_user_type(rt))
1493 v->type->type = RPC_FC_BOGUS_ARRAY;
1494 else
1495 switch (rt->type)
1497 case RPC_FC_BOGUS_STRUCT:
1498 case RPC_FC_NON_ENCAPSULATED_UNION:
1499 case RPC_FC_ENCAPSULATED_UNION:
1500 case RPC_FC_ENUM16:
1501 v->type->type = RPC_FC_BOGUS_ARRAY;
1502 break;
1503 /* FC_RP should be above, but widl overuses these, and will break things. */
1504 case RPC_FC_UP:
1505 case RPC_FC_RP:
1506 if (rt->ref->type == RPC_FC_IP)
1507 v->type->type = RPC_FC_BOGUS_ARRAY;
1508 break;
1513 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1515 if (!iface) return list;
1516 if (!list)
1518 list = xmalloc( sizeof(*list) );
1519 list_init( list );
1521 list_add_tail( list, &iface->entry );
1522 return list;
1525 static ifref_t *make_ifref(type_t *iface)
1527 ifref_t *l = xmalloc(sizeof(ifref_t));
1528 l->iface = iface;
1529 l->attrs = NULL;
1530 return l;
1533 static var_list_t *append_var(var_list_t *list, var_t *var)
1535 if (!var) return list;
1536 if (!list)
1538 list = xmalloc( sizeof(*list) );
1539 list_init( list );
1541 list_add_tail( list, &var->entry );
1542 return list;
1545 static var_t *make_var(char *name)
1547 var_t *v = xmalloc(sizeof(var_t));
1548 v->name = name;
1549 v->type = NULL;
1550 v->attrs = NULL;
1551 v->eval = NULL;
1552 return v;
1555 static pident_list_t *append_pident(pident_list_t *list, pident_t *p)
1557 if (!p) return list;
1558 if (!list) {
1559 list = xmalloc(sizeof(*list));
1560 list_init(list);
1562 list_add_tail(list, &p->entry);
1563 return list;
1566 static pident_t *make_pident(var_t *var)
1568 pident_t *p = xmalloc(sizeof(*p));
1569 p->var = var;
1570 p->is_func = FALSE;
1571 p->ptr_level = 0;
1572 p->func_ptr_level = 0;
1573 p->args = NULL;
1574 p->callconv = NULL;
1575 return p;
1578 static func_list_t *append_func(func_list_t *list, func_t *func)
1580 if (!func) return list;
1581 if (!list)
1583 list = xmalloc( sizeof(*list) );
1584 list_init( list );
1586 list_add_tail( list, &func->entry );
1587 return list;
1590 static func_t *make_func(var_t *def, var_list_t *args)
1592 func_t *f = xmalloc(sizeof(func_t));
1593 f->def = def;
1594 f->args = args;
1595 f->ignore = parse_only;
1596 f->idx = -1;
1597 return f;
1600 static type_t *make_class(char *name)
1602 type_t *c = make_type(0, NULL);
1603 c->name = name;
1604 c->kind = TKIND_COCLASS;
1605 return c;
1608 static type_t *make_safearray(type_t *type)
1610 type_t *sa = duptype(find_type("SAFEARRAY", 0), 1);
1611 sa->ref = type;
1612 return make_type(pointer_default, sa);
1615 #define HASHMAX 64
1617 static int hash_ident(const char *name)
1619 const char *p = name;
1620 int sum = 0;
1621 /* a simple sum hash is probably good enough */
1622 while (*p) {
1623 sum += *p;
1624 p++;
1626 return sum & (HASHMAX-1);
1629 /***** type repository *****/
1631 struct rtype {
1632 const char *name;
1633 type_t *type;
1634 int t;
1635 struct rtype *next;
1638 struct rtype *type_hash[HASHMAX];
1640 static type_t *reg_type(type_t *type, const char *name, int t)
1642 struct rtype *nt;
1643 int hash;
1644 if (!name) {
1645 error_loc("registering named type without name\n");
1646 return type;
1648 hash = hash_ident(name);
1649 nt = xmalloc(sizeof(struct rtype));
1650 nt->name = name;
1651 nt->type = type;
1652 nt->t = t;
1653 nt->next = type_hash[hash];
1654 type_hash[hash] = nt;
1655 return type;
1658 static int is_incomplete(const type_t *t)
1660 return !t->defined && (is_struct(t->type) || is_union(t->type));
1663 static void add_incomplete(type_t *t)
1665 struct typenode *tn = xmalloc(sizeof *tn);
1666 tn->type = t;
1667 list_add_tail(&incomplete_types, &tn->entry);
1670 static void fix_type(type_t *t)
1672 if (t->kind == TKIND_ALIAS && is_incomplete(t)) {
1673 type_t *ot = t->orig;
1674 fix_type(ot);
1675 t->fields_or_args = ot->fields_or_args;
1676 t->defined = ot->defined;
1680 static void fix_incomplete(void)
1682 struct typenode *tn, *next;
1684 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1685 fix_type(tn->type);
1686 free(tn);
1690 static type_t *reg_typedefs(type_t *type, pident_list_t *pidents, attr_list_t *attrs)
1692 type_t *ptr = type;
1693 const pident_t *pident;
1694 int ptrc = 0;
1695 int is_str = is_attr(attrs, ATTR_STRING);
1696 unsigned char ptr_type = get_attrv(attrs, ATTR_POINTERTYPE);
1698 if (is_str)
1700 type_t *t = type;
1701 unsigned char c;
1703 while (is_ptr(t))
1704 t = t->ref;
1706 c = t->type;
1707 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1709 pident = LIST_ENTRY( list_head( pidents ), const pident_t, entry );
1710 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1711 pident->var->name);
1715 /* We must generate names for tagless enum, struct or union.
1716 Typedef-ing a tagless enum, struct or union means we want the typedef
1717 to be included in a library whether it has other attributes or not,
1718 hence the public attribute. */
1719 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1720 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1722 if (! is_attr(attrs, ATTR_PUBLIC))
1723 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1724 type->name = gen_name();
1727 LIST_FOR_EACH_ENTRY( pident, pidents, const pident_t, entry )
1729 var_t *name = pident->var;
1731 if (name->name) {
1732 type_t *cur = ptr;
1733 int cptr = pident->ptr_level;
1734 if (cptr > ptrc) {
1735 while (cptr > ptrc) {
1736 cur = ptr = make_type(pointer_default, cur);
1737 ptrc++;
1739 } else {
1740 while (cptr < ptrc) {
1741 cur = cur->ref;
1742 cptr++;
1745 if (pident->is_func) {
1746 int func_ptr_level = pident->func_ptr_level;
1747 cur = make_type(RPC_FC_FUNCTION, cur);
1748 cur->fields_or_args = pident->args;
1749 if (pident->callconv)
1750 cur->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, pident->callconv));
1751 else if (is_object_interface) {
1752 static char *stdmethodcalltype;
1753 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1754 cur->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1756 for (; func_ptr_level > 0; func_ptr_level--)
1757 cur = make_type(pointer_default, cur);
1759 cur = alias(cur, name->name);
1760 cur->attrs = attrs;
1761 if (ptr_type)
1763 if (is_ptr(cur))
1764 cur->type = ptr_type;
1765 else
1766 error_loc("'%s': pointer attribute applied to non-pointer type\n",
1767 cur->name);
1769 else if (is_str && ! is_ptr(cur))
1770 error_loc("'%s': [string] attribute applied to non-pointer type\n",
1771 cur->name);
1773 if (is_incomplete(cur))
1774 add_incomplete(cur);
1775 reg_type(cur, cur->name, 0);
1778 return type;
1781 static type_t *find_type(const char *name, int t)
1783 struct rtype *cur = type_hash[hash_ident(name)];
1784 while (cur && (cur->t != t || strcmp(cur->name, name)))
1785 cur = cur->next;
1786 if (!cur) {
1787 error_loc("type '%s' not found\n", name);
1788 return NULL;
1790 return cur->type;
1793 static type_t *find_type2(char *name, int t)
1795 type_t *tp = find_type(name, t);
1796 free(name);
1797 return tp;
1800 int is_type(const char *name)
1802 struct rtype *cur = type_hash[hash_ident(name)];
1803 while (cur && (cur->t || strcmp(cur->name, name)))
1804 cur = cur->next;
1805 if (cur) return TRUE;
1806 return FALSE;
1809 static type_t *get_type(unsigned char type, char *name, int t)
1811 struct rtype *cur = NULL;
1812 type_t *tp;
1813 if (name) {
1814 cur = type_hash[hash_ident(name)];
1815 while (cur && (cur->t != t || strcmp(cur->name, name)))
1816 cur = cur->next;
1818 if (cur) {
1819 free(name);
1820 return cur->type;
1822 tp = make_type(type, NULL);
1823 tp->name = name;
1824 if (!name) return tp;
1825 return reg_type(tp, name, t);
1828 static type_t *get_typev(unsigned char type, var_t *name, int t)
1830 char *sname = NULL;
1831 if (name) {
1832 sname = name->name;
1833 free(name);
1835 return get_type(type, sname, t);
1838 static int get_struct_type(var_list_t *fields)
1840 int has_pointer = 0;
1841 int has_conformance = 0;
1842 int has_variance = 0;
1843 var_t *field;
1845 if (get_padding(fields))
1846 return RPC_FC_BOGUS_STRUCT;
1848 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
1850 type_t *t = field->type;
1852 if (is_user_type(t))
1853 return RPC_FC_BOGUS_STRUCT;
1855 if (is_ptr(t))
1858 t = t->ref;
1859 while (is_ptr(t));
1861 switch (t->type)
1863 case RPC_FC_IP:
1864 case RPC_FC_ENCAPSULATED_UNION:
1865 case RPC_FC_NON_ENCAPSULATED_UNION:
1866 case RPC_FC_BOGUS_STRUCT:
1867 return RPC_FC_BOGUS_STRUCT;
1870 has_pointer = 1;
1871 continue;
1874 if (field->type->declarray)
1876 if (is_string_type(field->attrs, field->type))
1878 if (is_conformant_array(field->type))
1879 has_conformance = 1;
1880 has_variance = 1;
1881 continue;
1884 if (is_array(field->type->ref))
1885 return RPC_FC_BOGUS_STRUCT;
1887 if (is_conformant_array(field->type))
1889 has_conformance = 1;
1890 if (field->type->declarray && list_next(fields, &field->entry))
1891 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1892 field->name);
1894 if (field->type->length_is)
1895 has_variance = 1;
1897 t = field->type->ref;
1900 switch (t->type)
1903 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1904 * Simple types don't effect the type of struct.
1905 * A struct containing a simple struct is still a simple struct.
1906 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1908 case 0: /* void pointer */
1909 case RPC_FC_BYTE:
1910 case RPC_FC_CHAR:
1911 case RPC_FC_SMALL:
1912 case RPC_FC_USMALL:
1913 case RPC_FC_WCHAR:
1914 case RPC_FC_SHORT:
1915 case RPC_FC_USHORT:
1916 case RPC_FC_LONG:
1917 case RPC_FC_ULONG:
1918 case RPC_FC_INT3264:
1919 case RPC_FC_UINT3264:
1920 case RPC_FC_HYPER:
1921 case RPC_FC_FLOAT:
1922 case RPC_FC_DOUBLE:
1923 case RPC_FC_STRUCT:
1924 case RPC_FC_ENUM32:
1925 break;
1927 case RPC_FC_RP:
1928 case RPC_FC_UP:
1929 case RPC_FC_FP:
1930 case RPC_FC_OP:
1931 case RPC_FC_CARRAY:
1932 case RPC_FC_CVARRAY:
1933 case RPC_FC_BOGUS_ARRAY:
1934 has_pointer = 1;
1935 break;
1938 * Propagate member attributes
1939 * a struct should be at least as complex as its member
1941 case RPC_FC_CVSTRUCT:
1942 has_conformance = 1;
1943 has_variance = 1;
1944 has_pointer = 1;
1945 break;
1947 case RPC_FC_CPSTRUCT:
1948 has_conformance = 1;
1949 if (list_next( fields, &field->entry ))
1950 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1951 field->name);
1952 has_pointer = 1;
1953 break;
1955 case RPC_FC_CSTRUCT:
1956 has_conformance = 1;
1957 if (list_next( fields, &field->entry ))
1958 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1959 field->name);
1960 break;
1962 case RPC_FC_PSTRUCT:
1963 has_pointer = 1;
1964 break;
1966 default:
1967 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
1968 /* fallthru - treat it as complex */
1970 /* as soon as we see one of these these members, it's bogus... */
1971 case RPC_FC_ENCAPSULATED_UNION:
1972 case RPC_FC_NON_ENCAPSULATED_UNION:
1973 case RPC_FC_BOGUS_STRUCT:
1974 case RPC_FC_ENUM16:
1975 return RPC_FC_BOGUS_STRUCT;
1979 if( has_variance )
1981 if ( has_conformance )
1982 return RPC_FC_CVSTRUCT;
1983 else
1984 return RPC_FC_BOGUS_STRUCT;
1986 if( has_conformance && has_pointer )
1987 return RPC_FC_CPSTRUCT;
1988 if( has_conformance )
1989 return RPC_FC_CSTRUCT;
1990 if( has_pointer )
1991 return RPC_FC_PSTRUCT;
1992 return RPC_FC_STRUCT;
1995 /***** constant repository *****/
1997 struct rconst {
1998 char *name;
1999 var_t *var;
2000 struct rconst *next;
2003 struct rconst *const_hash[HASHMAX];
2005 static var_t *reg_const(var_t *var)
2007 struct rconst *nc;
2008 int hash;
2009 if (!var->name) {
2010 error_loc("registering constant without name\n");
2011 return var;
2013 hash = hash_ident(var->name);
2014 nc = xmalloc(sizeof(struct rconst));
2015 nc->name = var->name;
2016 nc->var = var;
2017 nc->next = const_hash[hash];
2018 const_hash[hash] = nc;
2019 return var;
2022 static var_t *find_const(char *name, int f)
2024 struct rconst *cur = const_hash[hash_ident(name)];
2025 while (cur && strcmp(cur->name, name))
2026 cur = cur->next;
2027 if (!cur) {
2028 if (f) error_loc("constant '%s' not found\n", name);
2029 return NULL;
2031 return cur->var;
2034 static void write_libid(const char *name, const attr_list_t *attr)
2036 const UUID *uuid = get_attrp(attr, ATTR_UUID);
2037 write_guid(idfile, "LIBID", name, uuid);
2040 static void write_clsid(type_t *cls)
2042 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
2043 write_guid(idfile, "CLSID", cls->name, uuid);
2046 static void write_diid(type_t *iface)
2048 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2049 write_guid(idfile, "DIID", iface->name, uuid);
2052 static void write_iid(type_t *iface)
2054 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2055 write_guid(idfile, "IID", iface->name, uuid);
2058 static int compute_method_indexes(type_t *iface)
2060 int idx;
2061 func_t *f;
2063 if (iface->ref)
2064 idx = compute_method_indexes(iface->ref);
2065 else
2066 idx = 0;
2068 if (!iface->funcs)
2069 return idx;
2071 LIST_FOR_EACH_ENTRY( f, iface->funcs, func_t, entry )
2072 if (! is_callas(f->def->attrs))
2073 f->idx = idx++;
2075 return idx;
2078 static char *gen_name(void)
2080 static const char format[] = "__WIDL_%s_generated_name_%08lX";
2081 static unsigned long n = 0;
2082 static const char *file_id;
2083 static size_t size;
2084 char *name;
2086 if (! file_id)
2088 char *dst = dup_basename(input_name, ".idl");
2089 file_id = dst;
2091 for (; *dst; ++dst)
2092 if (! isalnum((unsigned char) *dst))
2093 *dst = '_';
2095 size = sizeof format - 7 + strlen(file_id) + 8;
2098 name = xmalloc(size);
2099 sprintf(name, format, file_id, n++);
2100 return name;
2103 static void process_typedefs(pident_list_t *pidents)
2105 pident_t *pident, *next;
2107 if (!pidents) return;
2108 LIST_FOR_EACH_ENTRY_SAFE( pident, next, pidents, pident_t, entry )
2110 var_t *var = pident->var;
2111 type_t *type = find_type(var->name, 0);
2113 if (! parse_only && do_header)
2114 write_typedef(type);
2115 if (in_typelib && type->attrs)
2116 add_typelib_entry(type);
2118 free(pident);
2119 free(var);
2123 struct allowed_attr
2125 unsigned int dce_compatible : 1;
2126 unsigned int acf : 1;
2127 unsigned int on_interface : 1;
2128 unsigned int on_function : 1;
2129 unsigned int on_arg : 1;
2130 unsigned int on_type : 1;
2131 unsigned int on_field : 1;
2132 unsigned int on_library : 1;
2133 unsigned int on_dispinterface : 1;
2134 unsigned int on_module : 1;
2135 unsigned int on_coclass : 1;
2136 const char *display_name;
2139 struct allowed_attr allowed_attr[] =
2141 /* attr { D ACF I Fn ARG T Fi L DI M C <display name> } */
2142 /* ATTR_AGGREGATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "aggregatable" },
2143 /* ATTR_APPOBJECT */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "appobject" },
2144 /* ATTR_ASYNC */ { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, "async" },
2145 /* ATTR_AUTO_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "auto_handle" },
2146 /* ATTR_BINDABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "bindable" },
2147 /* ATTR_CALLAS */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "call_as" },
2148 /* ATTR_CALLCONV */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2149 /* ATTR_CASE */ { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "case" },
2150 /* ATTR_CONTEXTHANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "context_handle" },
2151 /* ATTR_CONTROL */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "control" },
2152 /* ATTR_DEFAULT */ { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, "default" },
2153 /* ATTR_DEFAULTCOLLELEM */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "defaultcollelem" },
2154 /* ATTR_DEFAULTVALUE_EXPR */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2155 /* ATTR_DEFAULTVALUE_STRING */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2156 /* ATTR_DEFAULTVTABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "defaultvtable" },
2157 /* ATTR_DISPINTERFACE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2158 /* ATTR_DISPLAYBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "displaybind" },
2159 /* ATTR_DLLNAME */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "dllname" },
2160 /* ATTR_DUAL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "dual" },
2161 /* ATTR_ENDPOINT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "endpoint" },
2162 /* ATTR_ENTRY_ORDINAL */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "entry" },
2163 /* ATTR_ENTRY_STRING */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "entry" },
2164 /* ATTR_EXPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "explicit_handle" },
2165 /* ATTR_HANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "handle" },
2166 /* ATTR_HELPCONTEXT */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpcontext" },
2167 /* ATTR_HELPFILE */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpfile" },
2168 /* ATTR_HELPSTRING */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpstring" },
2169 /* ATTR_HELPSTRINGCONTEXT */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpstringcontext" },
2170 /* ATTR_HELPSTRINGDLL */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpstringdll" },
2171 /* ATTR_HIDDEN */ { 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, "hidden" },
2172 /* ATTR_ID */ { 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, "id" },
2173 /* ATTR_IDEMPOTENT */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "idempotent" },
2174 /* ATTR_IIDIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "iid_is" },
2175 /* ATTR_IMMEDIATEBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "immediatebind" },
2176 /* ATTR_IMPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "implicit_handle" },
2177 /* ATTR_IN */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "in" },
2178 /* ATTR_INPUTSYNC */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inputsync" },
2179 /* ATTR_LENGTHIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "length_is" },
2180 /* ATTR_LOCAL */ { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, "local" },
2181 /* ATTR_NONBROWSABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "nonbrowsable" },
2182 /* ATTR_NONCREATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "noncreatable" },
2183 /* ATTR_NONEXTENSIBLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "nonextensible" },
2184 /* ATTR_OBJECT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "object" },
2185 /* ATTR_ODL */ { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, "odl" },
2186 /* ATTR_OLEAUTOMATION */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "oleautomation" },
2187 /* ATTR_OPTIONAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "optional" },
2188 /* ATTR_OUT */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "out" },
2189 /* ATTR_POINTERDEFAULT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "pointer_default" },
2190 /* ATTR_POINTERTYPE */ { 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, "ref, unique or ptr" },
2191 /* ATTR_PROPGET */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propget" },
2192 /* ATTR_PROPPUT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propput" },
2193 /* ATTR_PROPPUTREF */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propputref" },
2194 /* ATTR_PUBLIC */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "public" },
2195 /* ATTR_RANGE */ { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, "range" },
2196 /* ATTR_READONLY */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "readonly" },
2197 /* ATTR_REQUESTEDIT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "requestedit" },
2198 /* ATTR_RESTRICTED */ { 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, "restricted" },
2199 /* ATTR_RETVAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "retval" },
2200 /* ATTR_SIZEIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "size_is" },
2201 /* ATTR_SOURCE */ { 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, "source" },
2202 /* ATTR_STRICTCONTEXTHANDLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "strict_context_handle" },
2203 /* ATTR_STRING */ { 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, "string" },
2204 /* ATTR_SWITCHIS */ { 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "switch_is" },
2205 /* ATTR_SWITCHTYPE */ { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, "switch_type" },
2206 /* ATTR_TRANSMITAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "transmit_as" },
2207 /* ATTR_UUID */ { 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, "uuid" },
2208 /* ATTR_V1ENUM */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "v1_enum" },
2209 /* ATTR_VARARG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "vararg" },
2210 /* ATTR_VERSION */ { 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, "version" },
2211 /* ATTR_WIREMARSHAL */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "wire_marshal" },
2214 static const attr_list_t *check_iface_attrs(const char *name, const attr_list_t *attrs)
2216 const attr_t *attr;
2217 if (!attrs) return attrs;
2218 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2220 if (!allowed_attr[attr->type].on_interface)
2221 error_loc("inapplicable attribute %s for interface %s\n",
2222 allowed_attr[attr->type].display_name, name);
2224 return attrs;
2227 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs)
2229 const attr_t *attr;
2230 if (!attrs) return attrs;
2231 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2233 if (!allowed_attr[attr->type].on_function)
2234 error_loc("inapplicable attribute %s for function %s\n",
2235 allowed_attr[attr->type].display_name, name);
2237 return attrs;
2240 static void check_arg(var_t *arg)
2242 const type_t *t = arg->type;
2243 const attr_t *attr;
2245 if (t->type == 0 && ! is_var_ptr(arg))
2246 error_loc("argument '%s' has void type\n", arg->name);
2248 if (arg->attrs)
2250 LIST_FOR_EACH_ENTRY(attr, arg->attrs, const attr_t, entry)
2252 if (!allowed_attr[attr->type].on_arg)
2253 error_loc("inapplicable attribute %s for argument %s\n",
2254 allowed_attr[attr->type].display_name, arg->name);
2259 static attr_list_t *check_typedef_attrs(attr_list_t *attrs)
2261 const attr_t *attr;
2262 if (!attrs) return attrs;
2263 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2265 if (!allowed_attr[attr->type].on_type)
2266 error_loc("inapplicable attribute %s for typedef\n",
2267 allowed_attr[attr->type].display_name);
2269 return attrs;
2272 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs)
2274 const attr_t *attr;
2275 if (!attrs) return attrs;
2276 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2278 if (!allowed_attr[attr->type].on_field)
2279 error_loc("inapplicable attribute %s for field %s\n",
2280 allowed_attr[attr->type].display_name, name);
2282 return attrs;
2285 static const attr_list_t *check_library_attrs(const char *name, const attr_list_t *attrs)
2287 const attr_t *attr;
2288 if (!attrs) return attrs;
2289 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2291 if (!allowed_attr[attr->type].on_library)
2292 error_loc("inapplicable attribute %s for library %s\n",
2293 allowed_attr[attr->type].display_name, name);
2295 return attrs;
2298 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs)
2300 const attr_t *attr;
2301 if (!attrs) return attrs;
2302 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2304 if (!allowed_attr[attr->type].on_dispinterface)
2305 error_loc("inapplicable attribute %s for dispinterface %s\n",
2306 allowed_attr[attr->type].display_name, name);
2308 return attrs;
2311 static const attr_list_t *check_module_attrs(const char *name, const attr_list_t *attrs)
2313 const attr_t *attr;
2314 if (!attrs) return attrs;
2315 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2317 if (!allowed_attr[attr->type].on_module)
2318 error_loc("inapplicable attribute %s for module %s\n",
2319 allowed_attr[attr->type].display_name, name);
2321 return attrs;
2324 static const attr_list_t *check_coclass_attrs(const char *name, const attr_list_t *attrs)
2326 const attr_t *attr;
2327 if (!attrs) return attrs;
2328 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2330 if (!allowed_attr[attr->type].on_coclass)
2331 error_loc("inapplicable attribute %s for coclass %s\n",
2332 allowed_attr[attr->type].display_name, name);
2334 return attrs;
2337 static void check_all_user_types(ifref_list_t *ifrefs)
2339 const ifref_t *ifref;
2340 const func_t *f;
2342 if (ifrefs) LIST_FOR_EACH_ENTRY(ifref, ifrefs, const ifref_t, entry)
2344 const func_list_t *fs = ifref->iface->funcs;
2345 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const func_t, entry)
2346 check_for_additional_prototype_types(f->args);
2350 int is_valid_uuid(const char *s)
2352 int i;
2354 for (i = 0; i < 36; ++i)
2355 if (i == 8 || i == 13 || i == 18 || i == 23)
2357 if (s[i] != '-')
2358 return FALSE;
2360 else
2361 if (!isxdigit(s[i]))
2362 return FALSE;
2364 return s[i] == '\0';