widl: Add support for the broadcast and idempotent operation attributes.
[wine/multimedia.git] / tools / widl / parser.y
blob81eacbad7939addd2798ce0c44577b973d091af5
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);
141 const char *get_attr_display_name(enum attr_type type);
143 #define tsENUM 1
144 #define tsSTRUCT 2
145 #define tsUNION 3
148 %union {
149 attr_t *attr;
150 attr_list_t *attr_list;
151 str_list_t *str_list;
152 expr_t *expr;
153 expr_list_t *expr_list;
154 array_dims_t *array_dims;
155 type_t *type;
156 var_t *var;
157 var_list_t *var_list;
158 pident_t *pident;
159 pident_list_t *pident_list;
160 func_t *func;
161 func_list_t *func_list;
162 ifref_t *ifref;
163 ifref_list_t *ifref_list;
164 char *str;
165 UUID *uuid;
166 unsigned int num;
167 double dbl;
168 interface_info_t ifinfo;
171 %token <str> aIDENTIFIER
172 %token <str> aKNOWNTYPE
173 %token <num> aNUM aHEXNUM
174 %token <dbl> aDOUBLE
175 %token <str> aSTRING
176 %token <uuid> aUUID
177 %token aEOF
178 %token SHL SHR
179 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
180 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
181 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
182 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
183 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
184 %token tDEFAULT
185 %token tDEFAULTCOLLELEM
186 %token tDEFAULTVALUE
187 %token tDEFAULTVTABLE
188 %token tDISPLAYBIND
189 %token tDISPINTERFACE
190 %token tDLLNAME tDOUBLE tDUAL
191 %token tENDPOINT
192 %token tENTRY tENUM tERRORSTATUST
193 %token tEXPLICITHANDLE tEXTERN
194 %token tFALSE
195 %token tFASTCALL
196 %token tFLOAT
197 %token tHANDLE
198 %token tHANDLET
199 %token tHELPCONTEXT tHELPFILE
200 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
201 %token tHIDDEN
202 %token tHYPER tID tIDEMPOTENT
203 %token tIIDIS
204 %token tIMMEDIATEBIND
205 %token tIMPLICITHANDLE
206 %token tIMPORT tIMPORTLIB
207 %token tIN tINLINE
208 %token tINPUTSYNC
209 %token tINT tINT64
210 %token tINTERFACE
211 %token tLCID
212 %token tLENGTHIS tLIBRARY
213 %token tLOCAL
214 %token tLONG
215 %token tMETHODS
216 %token tMODULE
217 %token tNONBROWSABLE
218 %token tNONCREATABLE
219 %token tNONEXTENSIBLE
220 %token tOBJECT tODL tOLEAUTOMATION
221 %token tOPTIONAL
222 %token tOUT
223 %token tPASCAL
224 %token tPOINTERDEFAULT
225 %token tPROPERTIES
226 %token tPROPGET tPROPPUT tPROPPUTREF
227 %token tPTR
228 %token tPUBLIC
229 %token tRANGE
230 %token tREADONLY tREF
231 %token tREQUESTEDIT
232 %token tRESTRICTED
233 %token tRETVAL
234 %token tSAFEARRAY
235 %token tSHORT
236 %token tSIGNED
237 %token tSINGLE
238 %token tSIZEIS tSIZEOF
239 %token tSMALL
240 %token tSOURCE
241 %token tSTDCALL
242 %token tSTRICTCONTEXTHANDLE
243 %token tSTRING tSTRUCT
244 %token tSWITCH tSWITCHIS tSWITCHTYPE
245 %token tTRANSMITAS
246 %token tTRUE
247 %token tTYPEDEF
248 %token tUNION
249 %token tUNIQUE
250 %token tUNSIGNED
251 %token tUUID
252 %token tV1ENUM
253 %token tVARARG
254 %token tVERSION
255 %token tVOID
256 %token tWCHAR tWIREMARSHAL
258 %type <attr> attribute
259 %type <attr_list> m_attributes attributes attrib_list
260 %type <str_list> str_list
261 %type <expr> m_expr expr expr_const
262 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_const
263 %type <array_dims> array array_list
264 %type <ifinfo> interfacehdr
265 %type <type> inherit interface interfacedef interfacedec
266 %type <type> dispinterface dispinterfacehdr dispinterfacedef
267 %type <type> module modulehdr moduledef
268 %type <type> base_type int_std
269 %type <type> enumdef structdef uniondef
270 %type <type> type
271 %type <ifref> coclass_int
272 %type <ifref_list> gbl_statements coclass_ints
273 %type <var> arg field s_field case enum constdef externdef
274 %type <var_list> m_args no_args args fields cases enums enum_list dispint_props
275 %type <var> m_ident t_ident ident
276 %type <pident> pident func_ident direct_ident
277 %type <pident_list> pident_list
278 %type <func> funcdef
279 %type <func_list> int_statements dispint_meths
280 %type <type> coclass coclasshdr coclassdef
281 %type <num> pointer_type version
282 %type <str> libraryhdr callconv
283 %type <uuid> uuid_string
284 %type <num> import_start
286 %left ','
287 %right '?' ':'
288 %left '|'
289 %left '&'
290 %left '-' '+'
291 %left '*' '/'
292 %left SHL SHR
293 %right '~'
294 %right CAST
295 %right PPTR
296 %right NEG
297 %right ADDRESSOF
301 input: gbl_statements { fix_incomplete();
302 check_all_user_types($1);
303 write_proxies($1);
304 write_client($1);
305 write_server($1);
306 write_dlldata($1);
310 gbl_statements: { $$ = NULL; }
311 | gbl_statements interfacedec { $$ = $1; }
312 | gbl_statements interfacedef { $$ = append_ifref( $1, make_ifref($2) ); }
313 | gbl_statements coclass ';' { $$ = $1;
314 reg_type($2, $2->name, 0);
315 if (!parse_only && do_header) write_coclass_forward($2);
317 | gbl_statements coclassdef { $$ = $1;
318 add_typelib_entry($2);
319 reg_type($2, $2->name, 0);
320 if (!parse_only && do_header) write_coclass_forward($2);
322 | gbl_statements moduledef { $$ = $1; add_typelib_entry($2); }
323 | gbl_statements librarydef { $$ = $1; }
324 | gbl_statements statement { $$ = $1; }
327 imp_statements: {}
328 | imp_statements interfacedec { if (!parse_only) add_typelib_entry($2); }
329 | imp_statements interfacedef { if (!parse_only) add_typelib_entry($2); }
330 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
331 | imp_statements coclassdef { if (!parse_only) add_typelib_entry($2);
332 reg_type($2, $2->name, 0);
333 if (!parse_only && do_header) write_coclass_forward($2);
335 | imp_statements moduledef { if (!parse_only) add_typelib_entry($2); }
336 | imp_statements statement {}
337 | imp_statements importlib {}
338 | imp_statements librarydef {}
341 int_statements: { $$ = NULL; }
342 | int_statements funcdef ';' { $$ = append_func( $1, $2 ); }
343 | int_statements statement { $$ = $1; }
346 semicolon_opt:
347 | ';'
350 statement: constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
351 | cppquote {}
352 | enumdef ';' { if (!parse_only && do_header) {
353 write_type_def_or_decl(header, $1, FALSE, NULL);
354 fprintf(header, ";\n\n");
357 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
358 | import {}
359 | structdef ';' { if (!parse_only && do_header) {
360 write_type_def_or_decl(header, $1, FALSE, NULL);
361 fprintf(header, ";\n\n");
364 | typedef ';' {}
365 | uniondef ';' { if (!parse_only && do_header) {
366 write_type_def_or_decl(header, $1, FALSE, NULL);
367 fprintf(header, ";\n\n");
372 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
374 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
375 $$ = do_import($2);
376 if (!$$) yychar = aEOF;
380 import: import_start imp_statements aEOF
381 { if ($1) pop_import(); }
384 importlib: tIMPORTLIB '(' aSTRING ')'
385 semicolon_opt { if(!parse_only) add_importlib($3); }
388 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
390 library_start: attributes libraryhdr '{' { check_library_attrs($2, $1);
391 if (!parse_only) start_typelib($2, $1);
392 if (!parse_only && do_header) write_library($2, $1);
393 if (!parse_only && do_idfile) write_libid($2, $1);
396 librarydef: library_start imp_statements '}'
397 semicolon_opt { if (!parse_only) end_typelib(); }
400 m_args: { $$ = NULL; }
401 | args
404 no_args: tVOID { $$ = NULL; }
407 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
408 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
409 | no_args
412 /* split into two rules to get bison to resolve a tVOID conflict */
413 arg: attributes type pident array { $$ = $3->var;
414 $$->attrs = $1;
415 set_type($$, $2, $3, $4, TRUE);
416 free($3);
418 | type pident array { $$ = $2->var;
419 set_type($$, $1, $2, $3, TRUE);
420 free($2);
424 array: { $$ = NULL; }
425 | '[' array_list ']' { $$ = $2; }
426 | '[' '*' ']' { $$ = append_array( NULL, make_expr(EXPR_VOID) ); }
429 array_list: m_expr /* size of first dimension is optional */ { $$ = append_array( NULL, $1 ); }
430 | array_list ',' expr { $$ = append_array( $1, $3 ); }
431 | array_list ']' '[' expr { $$ = append_array( $1, $4 ); }
434 m_attributes: { $$ = NULL; }
435 | attributes
438 attributes:
439 '[' attrib_list ']' { $$ = $2;
440 if (!$$)
441 error_loc("empty attribute lists unsupported\n");
445 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
446 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
447 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
450 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
451 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
454 attribute: { $$ = NULL; }
455 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
456 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
457 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
458 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
459 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
460 | tBROADCAST { $$ = make_attr(ATTR_BROADCAST); }
461 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
462 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
463 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
464 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
465 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
466 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
467 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
468 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
469 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
470 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
471 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
472 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
473 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
474 | tDUAL { $$ = make_attr(ATTR_DUAL); }
475 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
476 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
477 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
478 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
479 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
480 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
481 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
482 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
483 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
484 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
485 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
486 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
487 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
488 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
489 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
490 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
491 | tIN { $$ = make_attr(ATTR_IN); }
492 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
493 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
494 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
495 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
496 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
497 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
498 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
499 | tODL { $$ = make_attr(ATTR_ODL); }
500 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
501 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
502 | tOUT { $$ = make_attr(ATTR_OUT); }
503 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
504 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
505 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
506 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
507 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
508 | tRANGE '(' expr_const ',' expr_const ')' { expr_list_t *list = append_expr( NULL, $3 );
509 list = append_expr( list, $5 );
510 $$ = make_attrp(ATTR_RANGE, list); }
511 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
512 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
513 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
514 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
515 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
516 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
517 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
518 | tSTRING { $$ = make_attr(ATTR_STRING); }
519 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
520 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
521 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
522 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
523 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
524 | tVARARG { $$ = make_attr(ATTR_VARARG); }
525 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
526 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
527 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
530 uuid_string:
531 aUUID
532 | aSTRING { if (!is_valid_uuid($1))
533 error_loc("invalid UUID: %s\n", $1);
534 $$ = parse_uuid($1); }
537 callconv: tCDECL { $$ = $<str>1; }
538 | tFASTCALL { $$ = $<str>1; }
539 | tPASCAL { $$ = $<str>1; }
540 | tSTDCALL { $$ = $<str>1; }
543 cases: { $$ = NULL; }
544 | cases case { $$ = append_var( $1, $2 ); }
547 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
548 $$ = $4; if (!$$) $$ = make_var(NULL);
549 $$->attrs = append_attr( $$->attrs, a );
551 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
552 $$ = $3; if (!$$) $$ = make_var(NULL);
553 $$->attrs = append_attr( $$->attrs, a );
557 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
558 set_type($$, $2, NULL, NULL, FALSE);
559 $$->eval = $5;
563 enums: { $$ = NULL; }
564 | enum_list ',' { $$ = $1; }
565 | enum_list
568 enum_list: enum { if (!$1->eval)
569 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
570 $$ = append_var( NULL, $1 );
572 | enum_list ',' enum { if (!$3->eval)
574 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
575 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
577 $$ = append_var( $1, $3 );
581 enum: ident '=' expr_const { $$ = reg_const($1);
582 $$->eval = $3;
583 $$->type = make_int(0);
585 | ident { $$ = reg_const($1);
586 $$->type = make_int(0);
590 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
591 $$->kind = TKIND_ENUM;
592 $$->fields_or_args = $4;
593 $$->defined = TRUE;
594 if(in_typelib)
595 add_typelib_entry($$);
599 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
600 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
604 exprs: { $$ = make_expr(EXPR_VOID); }
605 | expr_list
608 expr_list: expr
609 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
613 m_expr: { $$ = make_expr(EXPR_VOID); }
614 | expr
617 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
618 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
619 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
620 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
621 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
622 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
623 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
624 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
625 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
626 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
627 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
628 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
629 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
630 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
631 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
632 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
633 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
634 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
635 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
636 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
637 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
638 | '(' expr ')' { $$ = $2; }
641 expr_list_const: expr_const { $$ = append_expr( NULL, $1 ); }
642 | expr_list_const ',' expr_const { $$ = append_expr( $1, $3 ); }
645 expr_const: expr { $$ = $1;
646 if (!$$->is_const)
647 error_loc("expression is not constant\n");
651 externdef: tEXTERN tCONST type ident { $$ = $4;
652 set_type($$, $3, NULL, NULL, FALSE);
656 fields: { $$ = NULL; }
657 | fields field { $$ = append_var( $1, $2 ); }
660 field: s_field ';' { $$ = $1; }
661 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
662 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
663 | ';' { $$ = NULL; }
666 s_field: m_attributes type pident array { $$ = $3->var;
667 $$->attrs = check_field_attrs($$->name, $1);
668 set_type($$, $2, $3, $4, FALSE);
669 free($3);
673 funcdef:
674 m_attributes type pident { var_t *v = $3->var;
675 var_list_t *args = $3->args;
676 v->attrs = check_function_attrs(v->name, $1);
677 set_type(v, $2, $3, NULL, FALSE);
678 free($3);
679 $$ = make_func(v, args);
683 m_ident: { $$ = NULL; }
684 | ident
687 t_ident: { $$ = NULL; }
688 | aIDENTIFIER { $$ = make_var($1); }
689 | aKNOWNTYPE { $$ = make_var($1); }
692 ident: aIDENTIFIER { $$ = make_var($1); }
693 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
694 | aKNOWNTYPE { $$ = make_var($<str>1); }
697 base_type: tBYTE { $$ = make_builtin($<str>1); }
698 | tWCHAR { $$ = make_builtin($<str>1); }
699 | int_std
700 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
701 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
702 switch ($$->type) {
703 case RPC_FC_CHAR: break;
704 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
705 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
706 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
707 case RPC_FC_HYPER:
708 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
710 $$ = alias($$, "MIDL_uhyper");
711 $$->sign = 0;
713 break;
714 default: break;
717 | tUNSIGNED { $$ = make_int(-1); }
718 | tFLOAT { $$ = make_builtin($<str>1); }
719 | tSINGLE { $$ = duptype(find_type("float", 0), 1); }
720 | tDOUBLE { $$ = make_builtin($<str>1); }
721 | tBOOLEAN { $$ = make_builtin($<str>1); }
722 | tERRORSTATUST { $$ = make_builtin($<str>1); }
723 | tHANDLET { $$ = make_builtin($<str>1); }
726 m_int:
727 | tINT
730 int_std: tINT { $$ = make_builtin($<str>1); }
731 | tSHORT m_int { $$ = make_builtin($<str>1); }
732 | tSMALL { $$ = make_builtin($<str>1); }
733 | tLONG m_int { $$ = make_builtin($<str>1); }
734 | tHYPER m_int { $$ = make_builtin($<str>1); }
735 | tINT64 { $$ = make_builtin($<str>1); }
736 | tCHAR { $$ = make_builtin($<str>1); }
739 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
740 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
741 if ($$->defined) error_loc("multiple definition error\n");
742 if ($$->kind != TKIND_COCLASS) error_loc("%s was not declared a coclass\n", $2);
746 coclasshdr: attributes coclass { $$ = $2;
747 $$->attrs = check_coclass_attrs($2->name, $1);
748 if (!parse_only && do_header)
749 write_coclass($$);
750 if (!parse_only && do_idfile)
751 write_clsid($$);
755 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
756 { $$ = $1;
757 $$->ifaces = $3;
758 $$->defined = TRUE;
762 coclass_ints: { $$ = NULL; }
763 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
766 coclass_int:
767 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
770 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
771 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
774 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
775 is_object_interface = TRUE;
776 $$ = $2;
777 if ($$->defined) error_loc("multiple definition error\n");
778 attrs = make_attr(ATTR_DISPINTERFACE);
779 $$->attrs = append_attr( check_dispiface_attrs($2->name, $1), attrs );
780 $$->ref = find_type("IDispatch", 0);
781 if (!$$->ref) error_loc("IDispatch is undefined\n");
782 $$->defined = TRUE;
783 if (!parse_only && do_header) write_forward($$);
787 dispint_props: tPROPERTIES ':' { $$ = NULL; }
788 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
791 dispint_meths: tMETHODS ':' { $$ = NULL; }
792 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
795 dispinterfacedef: dispinterfacehdr '{'
796 dispint_props
797 dispint_meths
798 '}' { $$ = $1;
799 $$->fields_or_args = $3;
800 $$->funcs = $4;
801 if (!parse_only && do_header) write_dispinterface($$);
802 if (!parse_only && do_idfile) write_diid($$);
804 | dispinterfacehdr
805 '{' interface ';' '}' { $$ = $1;
806 $$->fields_or_args = $3->fields_or_args;
807 $$->funcs = $3->funcs;
808 if (!parse_only && do_header) write_dispinterface($$);
809 if (!parse_only && do_idfile) write_diid($$);
813 inherit: { $$ = NULL; }
814 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
817 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
818 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
821 interfacehdr: attributes interface { $$.interface = $2;
822 $$.old_pointer_default = pointer_default;
823 if (is_attr($1, ATTR_POINTERDEFAULT))
824 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
825 is_object_interface = is_object($1);
826 if ($2->defined) error_loc("multiple definition error\n");
827 $2->attrs = check_iface_attrs($2->name, $1);
828 $2->defined = TRUE;
829 if (!parse_only && do_header) write_forward($2);
833 interfacedef: interfacehdr inherit
834 '{' int_statements '}' semicolon_opt { $$ = $1.interface;
835 $$->ref = $2;
836 $$->funcs = $4;
837 compute_method_indexes($$);
838 if (!parse_only && do_header) write_interface($$);
839 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
840 if (!parse_only && do_idfile) write_iid($$);
841 pointer_default = $1.old_pointer_default;
843 /* MIDL is able to import the definition of a base class from inside the
844 * definition of a derived class, I'll try to support it with this rule */
845 | interfacehdr ':' aIDENTIFIER
846 '{' import int_statements '}'
847 semicolon_opt { $$ = $1.interface;
848 $$->ref = find_type2($3, 0);
849 if (!$$->ref) error_loc("base class '%s' not found in import\n", $3);
850 $$->funcs = $6;
851 compute_method_indexes($$);
852 if (!parse_only && do_header) write_interface($$);
853 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
854 if (!parse_only && do_idfile) write_iid($$);
855 pointer_default = $1.old_pointer_default;
857 | dispinterfacedef semicolon_opt { $$ = $1; }
860 interfacedec:
861 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
862 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
865 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
866 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
869 modulehdr: attributes module { $$ = $2;
870 $$->attrs = check_module_attrs($2->name, $1);
874 moduledef: modulehdr '{' int_statements '}'
875 semicolon_opt { $$ = $1;
876 $$->funcs = $3;
877 /* FIXME: if (!parse_only && do_header) write_module($$); */
881 pident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
882 | tCONST pident { $$ = $2; /* FIXME */ }
883 | callconv pident { $$ = $2;
884 if ($$->callconv) parser_warning("multiple calling conventions %s, %s for function %s\n", $$->callconv, $1, $$->var->name);
885 $$->callconv = $1;
887 | direct_ident
890 func_ident: direct_ident '(' m_args ')'
891 { $$ = $1;
892 $1->args = $3;
893 $1->is_func = TRUE;
897 direct_ident: ident { $$ = make_pident($1); }
898 | '(' pident ')' { $$ = $2; }
899 | func_ident { $$ = $1;
900 $$->func_ptr_level = $$->ptr_level;
901 $$->ptr_level = 0;
905 pident_list:
906 pident { $$ = append_pident( NULL, $1 ); }
907 | pident_list ',' pident { $$ = append_pident( $1, $3 ); }
910 pointer_type:
911 tREF { $$ = RPC_FC_RP; }
912 | tUNIQUE { $$ = RPC_FC_UP; }
913 | tPTR { $$ = RPC_FC_FP; }
916 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
917 /* overwrite RPC_FC_STRUCT with a more exact type */
918 $$->type = get_struct_type( $4 );
919 $$->kind = TKIND_RECORD;
920 $$->fields_or_args = $4;
921 $$->defined = TRUE;
922 if(in_typelib)
923 add_typelib_entry($$);
927 type: tVOID { $$ = duptype(find_type("void", 0), 1); }
928 | aKNOWNTYPE { $$ = find_type($1, 0); }
929 | base_type { $$ = $1; }
930 | tCONST type { $$ = duptype($2, 1); $$->is_const = TRUE; }
931 | enumdef { $$ = $1; }
932 | tENUM aIDENTIFIER { $$ = find_type2($2, tsENUM); }
933 | structdef { $$ = $1; }
934 | tSTRUCT aIDENTIFIER { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
935 | uniondef { $$ = $1; }
936 | tUNION aIDENTIFIER { $$ = find_type2($2, tsUNION); }
937 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
940 typedef: tTYPEDEF m_attributes type pident_list { reg_typedefs($3, $4, check_typedef_attrs($2));
941 process_typedefs($4);
945 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
946 $$->kind = TKIND_UNION;
947 $$->fields_or_args = $4;
948 $$->defined = TRUE;
950 | tUNION t_ident
951 tSWITCH '(' s_field ')'
952 m_ident '{' cases '}' { var_t *u = $7;
953 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
954 $$->kind = TKIND_UNION;
955 if (!u) u = make_var( xstrdup("tagged_union") );
956 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
957 u->type->kind = TKIND_UNION;
958 u->type->fields_or_args = $9;
959 u->type->defined = TRUE;
960 $$->fields_or_args = append_var( $$->fields_or_args, $5 );
961 $$->fields_or_args = append_var( $$->fields_or_args, u );
962 $$->defined = TRUE;
966 version:
967 aNUM { $$ = MAKEVERSION($1, 0); }
968 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
973 static void decl_builtin(const char *name, unsigned char type)
975 type_t *t = make_type(type, NULL);
976 t->name = xstrdup(name);
977 reg_type(t, name, 0);
980 static type_t *make_builtin(char *name)
982 /* NAME is strdup'd in the lexer */
983 type_t *t = duptype(find_type(name, 0), 0);
984 t->name = name;
985 return t;
988 static type_t *make_int(int sign)
990 type_t *t = duptype(find_type("int", 0), 1);
992 t->sign = sign;
993 if (sign < 0)
994 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
996 return t;
999 void init_types(void)
1001 decl_builtin("void", 0);
1002 decl_builtin("byte", RPC_FC_BYTE);
1003 decl_builtin("wchar_t", RPC_FC_WCHAR);
1004 decl_builtin("int", RPC_FC_LONG); /* win32 */
1005 decl_builtin("short", RPC_FC_SHORT);
1006 decl_builtin("small", RPC_FC_SMALL);
1007 decl_builtin("long", RPC_FC_LONG);
1008 decl_builtin("hyper", RPC_FC_HYPER);
1009 decl_builtin("__int64", RPC_FC_HYPER);
1010 decl_builtin("char", RPC_FC_CHAR);
1011 decl_builtin("float", RPC_FC_FLOAT);
1012 decl_builtin("double", RPC_FC_DOUBLE);
1013 decl_builtin("boolean", RPC_FC_BYTE);
1014 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
1015 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
1018 static str_list_t *append_str(str_list_t *list, char *str)
1020 struct str_list_entry_t *entry;
1022 if (!str) return list;
1023 if (!list)
1025 list = xmalloc( sizeof(*list) );
1026 list_init( list );
1028 entry = xmalloc( sizeof(*entry) );
1029 entry->str = str;
1030 list_add_tail( list, &entry->entry );
1031 return list;
1034 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1036 attr_t *attr_existing;
1037 if (!attr) return list;
1038 if (!list)
1040 list = xmalloc( sizeof(*list) );
1041 list_init( list );
1043 LIST_FOR_EACH_ENTRY(attr_existing, list, attr_t, entry)
1044 if (attr_existing->type == attr->type)
1046 parser_warning("duplicate attribute %s\n", get_attr_display_name(attr->type));
1047 /* use the last attribute, like MIDL does */
1048 list_remove(&attr_existing->entry);
1049 break;
1051 list_add_tail( list, &attr->entry );
1052 return list;
1055 static attr_t *make_attr(enum attr_type type)
1057 attr_t *a = xmalloc(sizeof(attr_t));
1058 a->type = type;
1059 a->u.ival = 0;
1060 return a;
1063 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1065 attr_t *a = xmalloc(sizeof(attr_t));
1066 a->type = type;
1067 a->u.ival = val;
1068 return a;
1071 static attr_t *make_attrp(enum attr_type type, void *val)
1073 attr_t *a = xmalloc(sizeof(attr_t));
1074 a->type = type;
1075 a->u.pval = val;
1076 return a;
1079 static expr_t *make_expr(enum expr_type type)
1081 expr_t *e = xmalloc(sizeof(expr_t));
1082 e->type = type;
1083 e->ref = NULL;
1084 e->u.lval = 0;
1085 e->is_const = FALSE;
1086 e->cval = 0;
1087 return e;
1090 static expr_t *make_exprl(enum expr_type type, long val)
1092 expr_t *e = xmalloc(sizeof(expr_t));
1093 e->type = type;
1094 e->ref = NULL;
1095 e->u.lval = val;
1096 e->is_const = FALSE;
1097 /* check for numeric constant */
1098 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
1099 /* make sure true/false value is valid */
1100 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
1101 e->is_const = TRUE;
1102 e->cval = val;
1104 return e;
1107 static expr_t *make_exprd(enum expr_type type, double val)
1109 expr_t *e = xmalloc(sizeof(expr_t));
1110 e->type = type;
1111 e->ref = NULL;
1112 e->u.dval = val;
1113 e->is_const = TRUE;
1114 e->cval = val;
1115 return e;
1118 static expr_t *make_exprs(enum expr_type type, char *val)
1120 expr_t *e;
1121 e = xmalloc(sizeof(expr_t));
1122 e->type = type;
1123 e->ref = NULL;
1124 e->u.sval = val;
1125 e->is_const = FALSE;
1126 /* check for predefined constants */
1127 if (type == EXPR_IDENTIFIER) {
1128 var_t *c = find_const(val, 0);
1129 if (c) {
1130 e->u.sval = c->name;
1131 free(val);
1132 e->is_const = TRUE;
1133 e->cval = c->eval->cval;
1136 return e;
1139 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr)
1141 expr_t *e;
1142 e = xmalloc(sizeof(expr_t));
1143 e->type = type;
1144 e->ref = expr;
1145 e->u.tref = tref;
1146 e->is_const = FALSE;
1147 /* check for cast of constant expression */
1148 if (type == EXPR_SIZEOF) {
1149 switch (tref->type) {
1150 case RPC_FC_BYTE:
1151 case RPC_FC_CHAR:
1152 case RPC_FC_SMALL:
1153 case RPC_FC_USMALL:
1154 e->is_const = TRUE;
1155 e->cval = 1;
1156 break;
1157 case RPC_FC_WCHAR:
1158 case RPC_FC_USHORT:
1159 case RPC_FC_SHORT:
1160 e->is_const = TRUE;
1161 e->cval = 2;
1162 break;
1163 case RPC_FC_LONG:
1164 case RPC_FC_ULONG:
1165 case RPC_FC_FLOAT:
1166 case RPC_FC_ERROR_STATUS_T:
1167 e->is_const = TRUE;
1168 e->cval = 4;
1169 break;
1170 case RPC_FC_HYPER:
1171 case RPC_FC_DOUBLE:
1172 e->is_const = TRUE;
1173 e->cval = 8;
1174 break;
1177 if (type == EXPR_CAST && expr->is_const) {
1178 e->is_const = TRUE;
1179 e->cval = expr->cval;
1181 return e;
1184 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
1186 expr_t *e;
1187 if (type == EXPR_ADDRESSOF && expr->type != EXPR_IDENTIFIER)
1188 error("address-of operator applied to invalid expression\n");
1189 e = xmalloc(sizeof(expr_t));
1190 e->type = type;
1191 e->ref = expr;
1192 e->u.lval = 0;
1193 e->is_const = FALSE;
1194 /* check for compile-time optimization */
1195 if (expr->is_const) {
1196 e->is_const = TRUE;
1197 switch (type) {
1198 case EXPR_NEG:
1199 e->cval = -expr->cval;
1200 break;
1201 case EXPR_NOT:
1202 e->cval = ~expr->cval;
1203 break;
1204 default:
1205 e->is_const = FALSE;
1206 break;
1209 return e;
1212 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1214 expr_t *e;
1215 e = xmalloc(sizeof(expr_t));
1216 e->type = type;
1217 e->ref = expr1;
1218 e->u.ext = expr2;
1219 e->is_const = FALSE;
1220 /* check for compile-time optimization */
1221 if (expr1->is_const && expr2->is_const) {
1222 e->is_const = TRUE;
1223 switch (type) {
1224 case EXPR_ADD:
1225 e->cval = expr1->cval + expr2->cval;
1226 break;
1227 case EXPR_SUB:
1228 e->cval = expr1->cval - expr2->cval;
1229 break;
1230 case EXPR_MUL:
1231 e->cval = expr1->cval * expr2->cval;
1232 break;
1233 case EXPR_DIV:
1234 e->cval = expr1->cval / expr2->cval;
1235 break;
1236 case EXPR_OR:
1237 e->cval = expr1->cval | expr2->cval;
1238 break;
1239 case EXPR_AND:
1240 e->cval = expr1->cval & expr2->cval;
1241 break;
1242 case EXPR_SHL:
1243 e->cval = expr1->cval << expr2->cval;
1244 break;
1245 case EXPR_SHR:
1246 e->cval = expr1->cval >> expr2->cval;
1247 break;
1248 default:
1249 e->is_const = FALSE;
1250 break;
1253 return e;
1256 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1258 expr_t *e;
1259 e = xmalloc(sizeof(expr_t));
1260 e->type = type;
1261 e->ref = expr1;
1262 e->u.ext = expr2;
1263 e->ext2 = expr3;
1264 e->is_const = FALSE;
1265 /* check for compile-time optimization */
1266 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1267 e->is_const = TRUE;
1268 switch (type) {
1269 case EXPR_COND:
1270 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1271 break;
1272 default:
1273 e->is_const = FALSE;
1274 break;
1277 return e;
1280 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1282 if (!expr) return list;
1283 if (!list)
1285 list = xmalloc( sizeof(*list) );
1286 list_init( list );
1288 list_add_tail( list, &expr->entry );
1289 return list;
1292 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1294 if (!expr) return list;
1295 if (!list)
1297 list = xmalloc( sizeof(*list) );
1298 list_init( list );
1300 list_add_tail( list, &expr->entry );
1301 return list;
1304 static struct list type_pool = LIST_INIT(type_pool);
1305 typedef struct
1307 type_t data;
1308 struct list link;
1309 } type_pool_node_t;
1311 type_t *alloc_type(void)
1313 type_pool_node_t *node = xmalloc(sizeof *node);
1314 list_add_tail(&type_pool, &node->link);
1315 return &node->data;
1318 void set_all_tfswrite(int val)
1320 type_pool_node_t *node;
1321 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1322 node->data.tfswrite = val;
1325 static type_t *make_type(unsigned char type, type_t *ref)
1327 type_t *t = alloc_type();
1328 t->name = NULL;
1329 t->kind = TKIND_PRIMITIVE;
1330 t->type = type;
1331 t->ref = ref;
1332 t->attrs = NULL;
1333 t->orig = NULL;
1334 t->funcs = NULL;
1335 t->fields_or_args = NULL;
1336 t->ifaces = NULL;
1337 t->dim = 0;
1338 t->size_is = NULL;
1339 t->length_is = NULL;
1340 t->typestring_offset = 0;
1341 t->ptrdesc = 0;
1342 t->declarray = FALSE;
1343 t->ignore = (parse_only != 0);
1344 t->is_const = FALSE;
1345 t->sign = 0;
1346 t->defined = FALSE;
1347 t->written = FALSE;
1348 t->user_types_registered = FALSE;
1349 t->tfswrite = FALSE;
1350 t->typelib_idx = -1;
1351 return t;
1354 static void set_type(var_t *v, type_t *type, const pident_t *pident, array_dims_t *arr,
1355 int top)
1357 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1358 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1359 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1360 int ptr_type = ptr_attr;
1361 int sizeless, has_varconf;
1362 expr_t *dim;
1363 type_t *atype, **ptype;
1364 int ptr_level = (pident ? pident->ptr_level : 0);
1366 v->type = type;
1368 if (!ptr_type && top)
1369 ptr_type = RPC_FC_RP;
1371 for ( ; 0 < ptr_level; --ptr_level)
1373 v->type = make_type(pointer_default, v->type);
1374 if (ptr_level == 1 && ptr_type && !arr)
1376 v->type->type = ptr_type;
1377 ptr_type = 0;
1381 if (ptr_type && !arr)
1383 if (is_ptr(v->type))
1385 if (v->type->type != ptr_type)
1387 v->type = duptype(v->type, 1);
1388 v->type->type = ptr_type;
1391 else if (!arr && ptr_attr)
1392 error("%s: pointer attribute applied to non-pointer type\n", v->name);
1395 if (pident && pident->is_func) {
1396 int func_ptr_level = pident->func_ptr_level;
1397 v->type = make_type(RPC_FC_FUNCTION, v->type);
1398 v->type->fields_or_args = pident->args;
1399 if (pident->callconv)
1400 v->type->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, pident->callconv));
1401 else if (is_object_interface) {
1402 static char *stdmethodcalltype;
1403 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1404 v->type->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1406 for (; func_ptr_level > 0; func_ptr_level--)
1407 v->type = make_type(ptr_type, v->type);
1410 sizeless = FALSE;
1411 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1413 if (sizeless)
1414 error("%s: only the first array dimension can be unspecified\n", v->name);
1416 if (dim->is_const)
1418 unsigned int align = 0;
1419 size_t size = type_memsize(v->type, &align);
1421 if (dim->cval <= 0)
1422 error("%s: array dimension must be positive\n", v->name);
1424 if (0xffffffffuL / size < (unsigned long) dim->cval)
1425 error("%s: total array size is too large\n", v->name);
1426 else if (0xffffuL < size * dim->cval)
1427 v->type = make_type(RPC_FC_LGFARRAY, v->type);
1428 else
1429 v->type = make_type(RPC_FC_SMFARRAY, v->type);
1431 else
1433 sizeless = TRUE;
1434 v->type = make_type(RPC_FC_CARRAY, v->type);
1437 v->type->declarray = TRUE;
1438 v->type->dim = dim->cval;
1441 ptype = &v->type;
1442 has_varconf = FALSE;
1443 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1445 if (dim->type != EXPR_VOID)
1447 has_varconf = TRUE;
1448 atype = *ptype = duptype(*ptype, 0);
1450 if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1451 error("%s: cannot specify size_is for a fixed sized array\n", v->name);
1453 if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1454 error("%s: size_is attribute applied to illegal type\n", v->name);
1456 atype->type = RPC_FC_CARRAY;
1457 atype->size_is = dim;
1460 ptype = &(*ptype)->ref;
1461 if (*ptype == NULL)
1462 error("%s: too many expressions in size_is attribute\n", v->name);
1465 ptype = &v->type;
1466 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1468 if (dim->type != EXPR_VOID)
1470 has_varconf = TRUE;
1471 atype = *ptype = duptype(*ptype, 0);
1473 if (atype->type == RPC_FC_SMFARRAY)
1474 atype->type = RPC_FC_SMVARRAY;
1475 else if (atype->type == RPC_FC_LGFARRAY)
1476 atype->type = RPC_FC_LGVARRAY;
1477 else if (atype->type == RPC_FC_CARRAY)
1478 atype->type = RPC_FC_CVARRAY;
1479 else
1480 error("%s: length_is attribute applied to illegal type\n", v->name);
1482 atype->length_is = dim;
1485 ptype = &(*ptype)->ref;
1486 if (*ptype == NULL)
1487 error("%s: too many expressions in length_is attribute\n", v->name);
1490 if (has_varconf && !last_array(v->type))
1492 ptype = &v->type;
1493 for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1495 *ptype = duptype(*ptype, 0);
1496 (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1500 if (is_array(v->type))
1502 const type_t *rt = v->type->ref;
1503 if (is_user_type(rt))
1504 v->type->type = RPC_FC_BOGUS_ARRAY;
1505 else
1506 switch (rt->type)
1508 case RPC_FC_BOGUS_STRUCT:
1509 case RPC_FC_NON_ENCAPSULATED_UNION:
1510 case RPC_FC_ENCAPSULATED_UNION:
1511 case RPC_FC_ENUM16:
1512 v->type->type = RPC_FC_BOGUS_ARRAY;
1513 break;
1514 /* FC_RP should be above, but widl overuses these, and will break things. */
1515 case RPC_FC_UP:
1516 case RPC_FC_RP:
1517 if (rt->ref->type == RPC_FC_IP)
1518 v->type->type = RPC_FC_BOGUS_ARRAY;
1519 break;
1524 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1526 if (!iface) return list;
1527 if (!list)
1529 list = xmalloc( sizeof(*list) );
1530 list_init( list );
1532 list_add_tail( list, &iface->entry );
1533 return list;
1536 static ifref_t *make_ifref(type_t *iface)
1538 ifref_t *l = xmalloc(sizeof(ifref_t));
1539 l->iface = iface;
1540 l->attrs = NULL;
1541 return l;
1544 static var_list_t *append_var(var_list_t *list, var_t *var)
1546 if (!var) return list;
1547 if (!list)
1549 list = xmalloc( sizeof(*list) );
1550 list_init( list );
1552 list_add_tail( list, &var->entry );
1553 return list;
1556 static var_t *make_var(char *name)
1558 var_t *v = xmalloc(sizeof(var_t));
1559 v->name = name;
1560 v->type = NULL;
1561 v->attrs = NULL;
1562 v->eval = NULL;
1563 return v;
1566 static pident_list_t *append_pident(pident_list_t *list, pident_t *p)
1568 if (!p) return list;
1569 if (!list) {
1570 list = xmalloc(sizeof(*list));
1571 list_init(list);
1573 list_add_tail(list, &p->entry);
1574 return list;
1577 static pident_t *make_pident(var_t *var)
1579 pident_t *p = xmalloc(sizeof(*p));
1580 p->var = var;
1581 p->is_func = FALSE;
1582 p->ptr_level = 0;
1583 p->func_ptr_level = 0;
1584 p->args = NULL;
1585 p->callconv = NULL;
1586 return p;
1589 static func_list_t *append_func(func_list_t *list, func_t *func)
1591 if (!func) return list;
1592 if (!list)
1594 list = xmalloc( sizeof(*list) );
1595 list_init( list );
1597 list_add_tail( list, &func->entry );
1598 return list;
1601 static func_t *make_func(var_t *def, var_list_t *args)
1603 func_t *f = xmalloc(sizeof(func_t));
1604 f->def = def;
1605 f->args = args;
1606 f->ignore = parse_only;
1607 f->idx = -1;
1608 return f;
1611 static type_t *make_class(char *name)
1613 type_t *c = make_type(0, NULL);
1614 c->name = name;
1615 c->kind = TKIND_COCLASS;
1616 return c;
1619 static type_t *make_safearray(type_t *type)
1621 type_t *sa = duptype(find_type("SAFEARRAY", 0), 1);
1622 sa->ref = type;
1623 return make_type(pointer_default, sa);
1626 #define HASHMAX 64
1628 static int hash_ident(const char *name)
1630 const char *p = name;
1631 int sum = 0;
1632 /* a simple sum hash is probably good enough */
1633 while (*p) {
1634 sum += *p;
1635 p++;
1637 return sum & (HASHMAX-1);
1640 /***** type repository *****/
1642 struct rtype {
1643 const char *name;
1644 type_t *type;
1645 int t;
1646 struct rtype *next;
1649 struct rtype *type_hash[HASHMAX];
1651 static type_t *reg_type(type_t *type, const char *name, int t)
1653 struct rtype *nt;
1654 int hash;
1655 if (!name) {
1656 error_loc("registering named type without name\n");
1657 return type;
1659 hash = hash_ident(name);
1660 nt = xmalloc(sizeof(struct rtype));
1661 nt->name = name;
1662 nt->type = type;
1663 nt->t = t;
1664 nt->next = type_hash[hash];
1665 type_hash[hash] = nt;
1666 return type;
1669 static int is_incomplete(const type_t *t)
1671 return !t->defined && (is_struct(t->type) || is_union(t->type));
1674 static void add_incomplete(type_t *t)
1676 struct typenode *tn = xmalloc(sizeof *tn);
1677 tn->type = t;
1678 list_add_tail(&incomplete_types, &tn->entry);
1681 static void fix_type(type_t *t)
1683 if (t->kind == TKIND_ALIAS && is_incomplete(t)) {
1684 type_t *ot = t->orig;
1685 fix_type(ot);
1686 t->fields_or_args = ot->fields_or_args;
1687 t->defined = ot->defined;
1691 static void fix_incomplete(void)
1693 struct typenode *tn, *next;
1695 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1696 fix_type(tn->type);
1697 free(tn);
1701 static type_t *reg_typedefs(type_t *type, pident_list_t *pidents, attr_list_t *attrs)
1703 type_t *ptr = type;
1704 const pident_t *pident;
1705 int ptrc = 0;
1706 int is_str = is_attr(attrs, ATTR_STRING);
1707 unsigned char ptr_type = get_attrv(attrs, ATTR_POINTERTYPE);
1709 if (is_str)
1711 type_t *t = type;
1712 unsigned char c;
1714 while (is_ptr(t))
1715 t = t->ref;
1717 c = t->type;
1718 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1720 pident = LIST_ENTRY( list_head( pidents ), const pident_t, entry );
1721 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1722 pident->var->name);
1726 /* We must generate names for tagless enum, struct or union.
1727 Typedef-ing a tagless enum, struct or union means we want the typedef
1728 to be included in a library whether it has other attributes or not,
1729 hence the public attribute. */
1730 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1731 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1733 if (! is_attr(attrs, ATTR_PUBLIC))
1734 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1735 type->name = gen_name();
1738 LIST_FOR_EACH_ENTRY( pident, pidents, const pident_t, entry )
1740 var_t *name = pident->var;
1742 if (name->name) {
1743 type_t *cur = ptr;
1744 int cptr = pident->ptr_level;
1745 if (cptr > ptrc) {
1746 while (cptr > ptrc) {
1747 cur = ptr = make_type(pointer_default, cur);
1748 ptrc++;
1750 } else {
1751 while (cptr < ptrc) {
1752 cur = cur->ref;
1753 cptr++;
1756 if (pident->is_func) {
1757 int func_ptr_level = pident->func_ptr_level;
1758 cur = make_type(RPC_FC_FUNCTION, cur);
1759 cur->fields_or_args = pident->args;
1760 if (pident->callconv)
1761 cur->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, pident->callconv));
1762 else if (is_object_interface) {
1763 static char *stdmethodcalltype;
1764 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1765 cur->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1767 for (; func_ptr_level > 0; func_ptr_level--)
1768 cur = make_type(pointer_default, cur);
1770 cur = alias(cur, name->name);
1771 cur->attrs = attrs;
1772 if (ptr_type)
1774 if (is_ptr(cur))
1775 cur->type = ptr_type;
1776 else
1777 error_loc("'%s': pointer attribute applied to non-pointer type\n",
1778 cur->name);
1780 else if (is_str && ! is_ptr(cur))
1781 error_loc("'%s': [string] attribute applied to non-pointer type\n",
1782 cur->name);
1784 if (is_incomplete(cur))
1785 add_incomplete(cur);
1786 reg_type(cur, cur->name, 0);
1789 return type;
1792 static type_t *find_type(const char *name, int t)
1794 struct rtype *cur = type_hash[hash_ident(name)];
1795 while (cur && (cur->t != t || strcmp(cur->name, name)))
1796 cur = cur->next;
1797 if (!cur) {
1798 error_loc("type '%s' not found\n", name);
1799 return NULL;
1801 return cur->type;
1804 static type_t *find_type2(char *name, int t)
1806 type_t *tp = find_type(name, t);
1807 free(name);
1808 return tp;
1811 int is_type(const char *name)
1813 struct rtype *cur = type_hash[hash_ident(name)];
1814 while (cur && (cur->t || strcmp(cur->name, name)))
1815 cur = cur->next;
1816 if (cur) return TRUE;
1817 return FALSE;
1820 static type_t *get_type(unsigned char type, char *name, int t)
1822 struct rtype *cur = NULL;
1823 type_t *tp;
1824 if (name) {
1825 cur = type_hash[hash_ident(name)];
1826 while (cur && (cur->t != t || strcmp(cur->name, name)))
1827 cur = cur->next;
1829 if (cur) {
1830 free(name);
1831 return cur->type;
1833 tp = make_type(type, NULL);
1834 tp->name = name;
1835 if (!name) return tp;
1836 return reg_type(tp, name, t);
1839 static type_t *get_typev(unsigned char type, var_t *name, int t)
1841 char *sname = NULL;
1842 if (name) {
1843 sname = name->name;
1844 free(name);
1846 return get_type(type, sname, t);
1849 static int get_struct_type(var_list_t *fields)
1851 int has_pointer = 0;
1852 int has_conformance = 0;
1853 int has_variance = 0;
1854 var_t *field;
1856 if (get_padding(fields))
1857 return RPC_FC_BOGUS_STRUCT;
1859 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
1861 type_t *t = field->type;
1863 if (is_user_type(t))
1864 return RPC_FC_BOGUS_STRUCT;
1866 if (is_ptr(t))
1869 t = t->ref;
1870 while (is_ptr(t));
1872 switch (t->type)
1874 case RPC_FC_IP:
1875 case RPC_FC_ENCAPSULATED_UNION:
1876 case RPC_FC_NON_ENCAPSULATED_UNION:
1877 case RPC_FC_BOGUS_STRUCT:
1878 return RPC_FC_BOGUS_STRUCT;
1881 has_pointer = 1;
1882 continue;
1885 if (field->type->declarray)
1887 if (is_string_type(field->attrs, field->type))
1889 if (is_conformant_array(field->type))
1890 has_conformance = 1;
1891 has_variance = 1;
1892 continue;
1895 if (is_array(field->type->ref))
1896 return RPC_FC_BOGUS_STRUCT;
1898 if (is_conformant_array(field->type))
1900 has_conformance = 1;
1901 if (field->type->declarray && list_next(fields, &field->entry))
1902 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1903 field->name);
1905 if (field->type->length_is)
1906 has_variance = 1;
1908 t = field->type->ref;
1911 switch (t->type)
1914 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1915 * Simple types don't effect the type of struct.
1916 * A struct containing a simple struct is still a simple struct.
1917 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1919 case 0: /* void pointer */
1920 case RPC_FC_BYTE:
1921 case RPC_FC_CHAR:
1922 case RPC_FC_SMALL:
1923 case RPC_FC_USMALL:
1924 case RPC_FC_WCHAR:
1925 case RPC_FC_SHORT:
1926 case RPC_FC_USHORT:
1927 case RPC_FC_LONG:
1928 case RPC_FC_ULONG:
1929 case RPC_FC_INT3264:
1930 case RPC_FC_UINT3264:
1931 case RPC_FC_HYPER:
1932 case RPC_FC_FLOAT:
1933 case RPC_FC_DOUBLE:
1934 case RPC_FC_STRUCT:
1935 case RPC_FC_ENUM32:
1936 break;
1938 case RPC_FC_RP:
1939 case RPC_FC_UP:
1940 case RPC_FC_FP:
1941 case RPC_FC_OP:
1942 case RPC_FC_CARRAY:
1943 case RPC_FC_CVARRAY:
1944 case RPC_FC_BOGUS_ARRAY:
1945 has_pointer = 1;
1946 break;
1949 * Propagate member attributes
1950 * a struct should be at least as complex as its member
1952 case RPC_FC_CVSTRUCT:
1953 has_conformance = 1;
1954 has_variance = 1;
1955 has_pointer = 1;
1956 break;
1958 case RPC_FC_CPSTRUCT:
1959 has_conformance = 1;
1960 if (list_next( fields, &field->entry ))
1961 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1962 field->name);
1963 has_pointer = 1;
1964 break;
1966 case RPC_FC_CSTRUCT:
1967 has_conformance = 1;
1968 if (list_next( fields, &field->entry ))
1969 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1970 field->name);
1971 break;
1973 case RPC_FC_PSTRUCT:
1974 has_pointer = 1;
1975 break;
1977 default:
1978 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
1979 /* fallthru - treat it as complex */
1981 /* as soon as we see one of these these members, it's bogus... */
1982 case RPC_FC_ENCAPSULATED_UNION:
1983 case RPC_FC_NON_ENCAPSULATED_UNION:
1984 case RPC_FC_BOGUS_STRUCT:
1985 case RPC_FC_ENUM16:
1986 return RPC_FC_BOGUS_STRUCT;
1990 if( has_variance )
1992 if ( has_conformance )
1993 return RPC_FC_CVSTRUCT;
1994 else
1995 return RPC_FC_BOGUS_STRUCT;
1997 if( has_conformance && has_pointer )
1998 return RPC_FC_CPSTRUCT;
1999 if( has_conformance )
2000 return RPC_FC_CSTRUCT;
2001 if( has_pointer )
2002 return RPC_FC_PSTRUCT;
2003 return RPC_FC_STRUCT;
2006 /***** constant repository *****/
2008 struct rconst {
2009 char *name;
2010 var_t *var;
2011 struct rconst *next;
2014 struct rconst *const_hash[HASHMAX];
2016 static var_t *reg_const(var_t *var)
2018 struct rconst *nc;
2019 int hash;
2020 if (!var->name) {
2021 error_loc("registering constant without name\n");
2022 return var;
2024 hash = hash_ident(var->name);
2025 nc = xmalloc(sizeof(struct rconst));
2026 nc->name = var->name;
2027 nc->var = var;
2028 nc->next = const_hash[hash];
2029 const_hash[hash] = nc;
2030 return var;
2033 static var_t *find_const(char *name, int f)
2035 struct rconst *cur = const_hash[hash_ident(name)];
2036 while (cur && strcmp(cur->name, name))
2037 cur = cur->next;
2038 if (!cur) {
2039 if (f) error_loc("constant '%s' not found\n", name);
2040 return NULL;
2042 return cur->var;
2045 static void write_libid(const char *name, const attr_list_t *attr)
2047 const UUID *uuid = get_attrp(attr, ATTR_UUID);
2048 write_guid(idfile, "LIBID", name, uuid);
2051 static void write_clsid(type_t *cls)
2053 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
2054 write_guid(idfile, "CLSID", cls->name, uuid);
2057 static void write_diid(type_t *iface)
2059 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2060 write_guid(idfile, "DIID", iface->name, uuid);
2063 static void write_iid(type_t *iface)
2065 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2066 write_guid(idfile, "IID", iface->name, uuid);
2069 static int compute_method_indexes(type_t *iface)
2071 int idx;
2072 func_t *f;
2074 if (iface->ref)
2075 idx = compute_method_indexes(iface->ref);
2076 else
2077 idx = 0;
2079 if (!iface->funcs)
2080 return idx;
2082 LIST_FOR_EACH_ENTRY( f, iface->funcs, func_t, entry )
2083 if (! is_callas(f->def->attrs))
2084 f->idx = idx++;
2086 return idx;
2089 static char *gen_name(void)
2091 static const char format[] = "__WIDL_%s_generated_name_%08lX";
2092 static unsigned long n = 0;
2093 static const char *file_id;
2094 static size_t size;
2095 char *name;
2097 if (! file_id)
2099 char *dst = dup_basename(input_name, ".idl");
2100 file_id = dst;
2102 for (; *dst; ++dst)
2103 if (! isalnum((unsigned char) *dst))
2104 *dst = '_';
2106 size = sizeof format - 7 + strlen(file_id) + 8;
2109 name = xmalloc(size);
2110 sprintf(name, format, file_id, n++);
2111 return name;
2114 static void process_typedefs(pident_list_t *pidents)
2116 pident_t *pident, *next;
2118 if (!pidents) return;
2119 LIST_FOR_EACH_ENTRY_SAFE( pident, next, pidents, pident_t, entry )
2121 var_t *var = pident->var;
2122 type_t *type = find_type(var->name, 0);
2124 if (! parse_only && do_header)
2125 write_typedef(type);
2126 if (in_typelib && type->attrs)
2127 add_typelib_entry(type);
2129 free(pident);
2130 free(var);
2134 struct allowed_attr
2136 unsigned int dce_compatible : 1;
2137 unsigned int acf : 1;
2138 unsigned int on_interface : 1;
2139 unsigned int on_function : 1;
2140 unsigned int on_arg : 1;
2141 unsigned int on_type : 1;
2142 unsigned int on_field : 1;
2143 unsigned int on_library : 1;
2144 unsigned int on_dispinterface : 1;
2145 unsigned int on_module : 1;
2146 unsigned int on_coclass : 1;
2147 const char *display_name;
2150 struct allowed_attr allowed_attr[] =
2152 /* attr { D ACF I Fn ARG T Fi L DI M C <display name> } */
2153 /* ATTR_AGGREGATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "aggregatable" },
2154 /* ATTR_APPOBJECT */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "appobject" },
2155 /* ATTR_ASYNC */ { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, "async" },
2156 /* ATTR_AUTO_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "auto_handle" },
2157 /* ATTR_BINDABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "bindable" },
2158 /* ATTR_BROADCAST */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "broadcast" },
2159 /* ATTR_CALLAS */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "call_as" },
2160 /* ATTR_CALLCONV */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2161 /* ATTR_CASE */ { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "case" },
2162 /* ATTR_CONTEXTHANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "context_handle" },
2163 /* ATTR_CONTROL */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "control" },
2164 /* ATTR_DEFAULT */ { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, "default" },
2165 /* ATTR_DEFAULTCOLLELEM */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "defaultcollelem" },
2166 /* ATTR_DEFAULTVALUE_EXPR */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2167 /* ATTR_DEFAULTVALUE_STRING */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2168 /* ATTR_DEFAULTVTABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "defaultvtable" },
2169 /* ATTR_DISPINTERFACE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2170 /* ATTR_DISPLAYBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "displaybind" },
2171 /* ATTR_DLLNAME */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "dllname" },
2172 /* ATTR_DUAL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "dual" },
2173 /* ATTR_ENDPOINT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "endpoint" },
2174 /* ATTR_ENTRY_ORDINAL */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "entry" },
2175 /* ATTR_ENTRY_STRING */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "entry" },
2176 /* ATTR_EXPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "explicit_handle" },
2177 /* ATTR_HANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "handle" },
2178 /* ATTR_HELPCONTEXT */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpcontext" },
2179 /* ATTR_HELPFILE */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpfile" },
2180 /* ATTR_HELPSTRING */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpstring" },
2181 /* ATTR_HELPSTRINGCONTEXT */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpstringcontext" },
2182 /* ATTR_HELPSTRINGDLL */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpstringdll" },
2183 /* ATTR_HIDDEN */ { 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, "hidden" },
2184 /* ATTR_ID */ { 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, "id" },
2185 /* ATTR_IDEMPOTENT */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "idempotent" },
2186 /* ATTR_IIDIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "iid_is" },
2187 /* ATTR_IMMEDIATEBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "immediatebind" },
2188 /* ATTR_IMPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "implicit_handle" },
2189 /* ATTR_IN */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "in" },
2190 /* ATTR_INPUTSYNC */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inputsync" },
2191 /* ATTR_LENGTHIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "length_is" },
2192 /* ATTR_LOCAL */ { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, "local" },
2193 /* ATTR_NONBROWSABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "nonbrowsable" },
2194 /* ATTR_NONCREATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "noncreatable" },
2195 /* ATTR_NONEXTENSIBLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "nonextensible" },
2196 /* ATTR_OBJECT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "object" },
2197 /* ATTR_ODL */ { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, "odl" },
2198 /* ATTR_OLEAUTOMATION */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "oleautomation" },
2199 /* ATTR_OPTIONAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "optional" },
2200 /* ATTR_OUT */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "out" },
2201 /* ATTR_POINTERDEFAULT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "pointer_default" },
2202 /* ATTR_POINTERTYPE */ { 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, "ref, unique or ptr" },
2203 /* ATTR_PROPGET */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propget" },
2204 /* ATTR_PROPPUT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propput" },
2205 /* ATTR_PROPPUTREF */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propputref" },
2206 /* ATTR_PUBLIC */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "public" },
2207 /* ATTR_RANGE */ { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, "range" },
2208 /* ATTR_READONLY */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "readonly" },
2209 /* ATTR_REQUESTEDIT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "requestedit" },
2210 /* ATTR_RESTRICTED */ { 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, "restricted" },
2211 /* ATTR_RETVAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "retval" },
2212 /* ATTR_SIZEIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "size_is" },
2213 /* ATTR_SOURCE */ { 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, "source" },
2214 /* ATTR_STRICTCONTEXTHANDLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "strict_context_handle" },
2215 /* ATTR_STRING */ { 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, "string" },
2216 /* ATTR_SWITCHIS */ { 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "switch_is" },
2217 /* ATTR_SWITCHTYPE */ { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, "switch_type" },
2218 /* ATTR_TRANSMITAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "transmit_as" },
2219 /* ATTR_UUID */ { 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, "uuid" },
2220 /* ATTR_V1ENUM */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "v1_enum" },
2221 /* ATTR_VARARG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "vararg" },
2222 /* ATTR_VERSION */ { 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, "version" },
2223 /* ATTR_WIREMARSHAL */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "wire_marshal" },
2226 const char *get_attr_display_name(enum attr_type type)
2228 return allowed_attr[type].display_name;
2231 static const attr_list_t *check_iface_attrs(const char *name, const attr_list_t *attrs)
2233 const attr_t *attr;
2234 if (!attrs) return attrs;
2235 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2237 if (!allowed_attr[attr->type].on_interface)
2238 error_loc("inapplicable attribute %s for interface %s\n",
2239 allowed_attr[attr->type].display_name, name);
2241 return attrs;
2244 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs)
2246 const attr_t *attr;
2247 if (!attrs) return attrs;
2248 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2250 if (!allowed_attr[attr->type].on_function)
2251 error_loc("inapplicable attribute %s for function %s\n",
2252 allowed_attr[attr->type].display_name, name);
2254 return attrs;
2257 static void check_arg(var_t *arg)
2259 const type_t *t = arg->type;
2260 const attr_t *attr;
2262 if (t->type == 0 && ! is_var_ptr(arg))
2263 error_loc("argument '%s' has void type\n", arg->name);
2265 if (arg->attrs)
2267 LIST_FOR_EACH_ENTRY(attr, arg->attrs, const attr_t, entry)
2269 if (!allowed_attr[attr->type].on_arg)
2270 error_loc("inapplicable attribute %s for argument %s\n",
2271 allowed_attr[attr->type].display_name, arg->name);
2276 static attr_list_t *check_typedef_attrs(attr_list_t *attrs)
2278 const attr_t *attr;
2279 if (!attrs) return attrs;
2280 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2282 if (!allowed_attr[attr->type].on_type)
2283 error_loc("inapplicable attribute %s for typedef\n",
2284 allowed_attr[attr->type].display_name);
2286 return attrs;
2289 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs)
2291 const attr_t *attr;
2292 if (!attrs) return attrs;
2293 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2295 if (!allowed_attr[attr->type].on_field)
2296 error_loc("inapplicable attribute %s for field %s\n",
2297 allowed_attr[attr->type].display_name, name);
2299 return attrs;
2302 static const attr_list_t *check_library_attrs(const char *name, const attr_list_t *attrs)
2304 const attr_t *attr;
2305 if (!attrs) return attrs;
2306 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2308 if (!allowed_attr[attr->type].on_library)
2309 error_loc("inapplicable attribute %s for library %s\n",
2310 allowed_attr[attr->type].display_name, name);
2312 return attrs;
2315 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs)
2317 const attr_t *attr;
2318 if (!attrs) return attrs;
2319 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2321 if (!allowed_attr[attr->type].on_dispinterface)
2322 error_loc("inapplicable attribute %s for dispinterface %s\n",
2323 allowed_attr[attr->type].display_name, name);
2325 return attrs;
2328 static const attr_list_t *check_module_attrs(const char *name, const attr_list_t *attrs)
2330 const attr_t *attr;
2331 if (!attrs) return attrs;
2332 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2334 if (!allowed_attr[attr->type].on_module)
2335 error_loc("inapplicable attribute %s for module %s\n",
2336 allowed_attr[attr->type].display_name, name);
2338 return attrs;
2341 static const attr_list_t *check_coclass_attrs(const char *name, const attr_list_t *attrs)
2343 const attr_t *attr;
2344 if (!attrs) return attrs;
2345 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2347 if (!allowed_attr[attr->type].on_coclass)
2348 error_loc("inapplicable attribute %s for coclass %s\n",
2349 allowed_attr[attr->type].display_name, name);
2351 return attrs;
2354 static void check_all_user_types(ifref_list_t *ifrefs)
2356 const ifref_t *ifref;
2357 const func_t *f;
2359 if (ifrefs) LIST_FOR_EACH_ENTRY(ifref, ifrefs, const ifref_t, entry)
2361 const func_list_t *fs = ifref->iface->funcs;
2362 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const func_t, entry)
2363 check_for_additional_prototype_types(f->args);
2367 int is_valid_uuid(const char *s)
2369 int i;
2371 for (i = 0; i < 36; ++i)
2372 if (i == 8 || i == 13 || i == 18 || i == 23)
2374 if (s[i] != '-')
2375 return FALSE;
2377 else
2378 if (!isxdigit(s[i]))
2379 return FALSE;
2381 return s[i] == '\0';